VideoWriter¶
- class VideoWriter(output_path='output.mp4', fps=30, color=True, codec='avc1', chunk_duration_s=None)¶
Save a video clip to a file.
VideoWriter can be instantiated as a context manager:
with edgeiq.VideoWriter('/path/to/output/video.avi') as video_writer: ...
To use VideoWriter directly, make sure to call
close()
when usage is complete:video_writer = edgeiq.VideoWriter('/path/to/output/video.mp4') ... video_writer.close()
Typical usage:
with edgeiq.VideoWriter('/path/to/output/video.avi') as video_writer: while True: <read and process frame> video_writer.write_frame(frame)
- Parameters
output_path (
str
) – The path to save the video file at.fps (
int
) – The number of frames per second to save.color (
bool
) – If True, save the video in color.codec (
str
) – The video codec to use for saving the video clip. Popular options: “avc1” (H264), “mp4v” (MPEG-4), “MJPG” (AVI)chunk_duration_s (
Optional
[int
]) – Duration in secs of video chunks, or disabled if None.
- write_frame(frame)¶
Save the frame to the video clip.
- Parameters
frame (
ndarray
) – The video frame to save.
- close()¶
Finish creating the video clip.
- class EventVideoWriterState(value)¶
States of the EventVideoWriter
- IDLE = 'Idle'¶
- RECORDING = 'Recording'¶
- POST_ROLL = 'Post-roll'¶
- class EventVideoWriter(pre_roll=150, post_roll=150, fps=30, color=True, codec='avc1')¶
Save a segment of a video stream based upon an event trigger.
EventVideoWriter can be instantiated as a context manager:
with edgeiq.EventVideoWriter() as video_writer: ...
To use EventVideoWriter directly, make sure to call
close()
when usage is complete:video_writer = edgeiq.EventVideoWriter() ... video_writer.close()
Typical usage:
def video_writer_callback(event_name): print('Event {} has completed'.format(event_name)) with edgeiq.EventVideoWriter() as video_writer: while True: <read and process frame> ... video_writer.update(frame) if event_started: video_writer.start_event( '/path/to/output/video.mp4', video_writer_callback, 'Event 1') if event_ended: video_writer.finish_event()
- Parameters
pre_roll (
int
) – The number of frames to capture preceding the event trigger.post_roll (
int
) – The number of frames to capture after the event completes.fps (
int
) – The number of frames per second to save.color (
bool
) – If True, save the video with color.codec (
str
) – The video codec to use for saving the video clip. Popular options: “avc1” (H264), “mp4v” (MPEG-4), “MJPG” (AVI)
- property state: EventVideoWriterState¶
The current recording state.
- Return type
- update(frame)¶
Provide a frame to be written to the video file if recording.
- Parameters
frame (
ndarray
) – The frame to save.
- start_event(output_path, callback_function=None, callback_args=())¶
Start a video clip recording.
- Parameters
output_path (
str
) – The path to save the video.callback_function (
Optional
[Callable
[...
,None
]]) – A function to be called upon completing this video write. When set to None no callback is called. Actions in callback_function must be thread-safe.callback_args (tuple) – Arguments to be passed to the callback function. Arguments must be thread-safe.
- finish_event()¶
Finish a recording for an event. Note that recording continues for post_roll frames.
- close()¶
Close the EventVideoWriter.
If a video recording is ongoing, it will be stopped and the recording will be saved.
- class GStreamerCustomVideoWriter(cmd, fps=30, color=True)¶
Send video frames with a custom GStreamer pipeline.
GStreamerCustomVideoWriter can be instantiated as a context manager:
with edgeiq.GStreamerCustomVideoWriter() as video_writer: ...
When using GStreamerCustomVideoWriter directly, use the
close()
function when usage is complete:video_writer = edgeiq.GStreamerCustomVideoWriter() ... video_writer.close()
Typical usage:
# Send an MJPG encoded UDP stream cmd = ' ! '.join([ 'appsrc', 'videoconvert', 'video/x-raw,format=YUY2', 'jpegenc', 'rtpjpegpay', 'udpsink host=127.0.0.1 port=5000' ]) with edgeiq.GStreamerCustomVideoWriter(cmd) as video_writer: while True: ... # Get and process frame video_writer.write_frame(frame)
To capture the stream, run this command on the destination device:
gst-launch-1.0 udpsrc port=5000 ! application/x-rtp,encoding-name=JPEG,payload=26 ! rtpjpegdepay ! jpegdec ! autovideosink
- Parameters
fps (
int
) – The number of frames per second to save.color (
bool
) – If True, write the video with color.
- write_frame(frame)¶
Send the frame to the destination.
- Parameters
frame (
ndarray
) – The video frame to send.
- close()¶
Close the UDP video stream.