Storing Video Clips

The VideoWriter class will save a video clip to a file. The input parameters are the output path at which to save the video file, the frame rate to save, whether to keep colors, and the codec to use. Start by instantiating the VideoWriter object, either by using a with statement, or by directly instantiating the object.

with edgeiq.VideoWriter(output_path="video_clip.avi") as video_writer:

or:

video_writer = edgeiq.VideoWriter(output_path="video_clip.avi")

For each frame that is ready to be added to the clip, use the write_frame() function.

video_writer.write_frame(frame)

For the context manager, the file will be written and closed when the code block exits. Otherwise, the close() function must be called.

Capturing Video on an Event Trigger

The EventVideoWriter makes it simple to capture specific events in video clips. When instantiating the object, choose the frames-per-second you’d like to encode the video at, and the amount of pre-roll and post-roll to record for each event.

# Capture 5 seconds of pre-roll and post-roll at 30 FPS
event_video_writer = edgeiq.EventVideoWriter(pre_roll=150, post_roll=150, fps=30)

Then for each frame call the update() function.

event_video_writer.update(frame)

When an event occurs, start recording the incoming frames, along with any pre-roll frames with the start_event() function.

if event_occurred:
  output_path="video-event{}.avi".format(event_index)
  event_video_writer.start_event(output_path=output_path, callback_function=cb, callback_args=(arg1, arg2))

The callback function is called with the provided arguments when the video capture is complete. Finally, when the event completes, finish saving the video file.

if event_complete:
  event_writer.finish_event()

Due to recording the post-roll, the video will not be saved immediately after finish_event() is called. Use the callback function to perform actions once the video capture completes. For example, to print out the path of the video that completed use the following callback function and arguments.

def cb(output_path):
  print("Video saved: {}".format(output_path))

...

if event_occurred:
  output_path="video-event{}.avi".format(event_index)
  event_video_writer.start_event(output_path=output_path, callback_function=cb, callback_args=(output_path))

The EventVideoWriter class intelligently handles calls to start_event() while it is already recording (during an event or post-roll). In those cases, the current video clip will be extended to capture the new event as well.