VideoWriter

class VideoWriter(output_path='output.avi', fps=30, color=True, codec='MJPG', 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.avi')
...
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 (string) – The path to save the video file at.

  • fps (integer) – The number of frames per second to save.

  • color (boolean) – If True, save the video in color.

  • codec (string) – The video codec to use for saving the video clip.

  • chunk_duration_s (None or integer) – Duration in secs of video chunks.

write_frame(frame)

Save the frame to the video clip.

Parameters

frame (numpy array of frame) – 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='MJPG')

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.avi', video_writer_callback, 'Event 1')

        if event_ended:
            video_writer.finish_event()
Parameters
  • pre_roll (integer) – The number of frames to capture preceding the event trigger.

  • post_roll (integer) – The number of frames to capture after the event completes.

  • fps (integer) – The number of frames per second to save.

  • color (boolean) – If True, save the video with color.

  • codec (string) – The video codec to use for saving the video clip.

property state

The current recording state.

Type

EventVideoWriterState

update(frame)

Provide a frame to be written to the video file if recording.

Parameters

frame (numpy array) – The frame to save.

start_event(output_path, callback_function=None, callback_args=())

Start a video clip recording.

Parameters
  • output_path (string) – The path to save the video.

  • callback_function (function) – 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 GStreamerUdpVideoWriter(dest_ipaddr='127.0.0.1', dest_port=5000, fps=30, color=True)

Send video frames as a GStreamer UDP MJPG stream.

GStreamerUdpVideoWriter can be instantiated as a context manager:

with edgeiq.GStreamerUDPVideoWriter() as video_writer:
    ...

When using GStreamerUDPVideoWriter directly, use the stop() function to stop the writer:

video_writer = edgeiq.GStreamerUdpVideoWriter()
...
video_writer.stop()

Typical usage:

with edgeiq.GStreamerUdpVideoWriter() 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
  • dest_ipaddr (string) – The IP address of the destination.

  • dest_port (integer) – The port of the destination.

  • fps (integer) – The number of frames per second to save.

  • color (boolean) – If True, save the video with color.

write_frame(frame)

Send the frame to the destination.

Parameters

frame (numpy array of frame) – The video frame to send.

close()

Close the UDP video stream.

class MjpgVideoWriter(queue_depth=2, port=5000, max_image_width=640, max_image_height=480, jpg_quality=75, **kwargs)

Host an MJPG video streaming server on the device.

MJPG video streams can be consumed by third-party apps such as VLC.

MjpgVideoWriter can be instantiated as a context manager:

with edgeiq.MjpgVideoWriter() as video_writer:
    ...
    video_writer.write_frame(frame)
    ...

To use MjpgVideoWriter directly, use the start() and close() functions:

video_writer = edgeiq.MjpgVideoWriter().start()
...
video_writer.write_frame(frame)
...
video_writer.close()
Parameters

queue_depth (integer) – The depth of the data queue used for communication between the application process and server process.

start()

Start the MjpgVideoWriter server.

Returns

self

write_frame(frame)

Send image to be displayed by the streamer.

Parameters

frame (numpy array) – The frame to be written to the video stream.

Raises

Any exception that occurs in MjpgVideoWriter process

close()

Stop the MjpgVideoWriter server.

Raises

Any exception that occurs in MjpgVideoWriter process

class RtspVideoWriter(port=5000, num_streams=1, image_width=640, image_height=480, fps=30, **kwargs)

Host an RTSP video streaming server on the device.

The RTSP video stream is H264 encoded and can be consumed by third-party apps such as VLC.

RtspVideoWriter can be instantiated as a context manager:

with edgeiq.RtspVideoWriter() as video_writer:
    ...
    video_writer.write_frame(frame)
    ...

To use RtspVideoWriter directly, use the start() and close() functions:

video_writer = edgeiq.RtspVideoWriter().start()
...
video_writer.write_frame(frame)
...
video_writer.close()

To capture the stream with VLC:

vlc -v rtsp://<device IP address>:<port>/video<stream idx>
Parameters
  • port (integer) – The port to use for the RTSP server.

  • num_streams (integer) – The number of RTSP streams.

  • image_width (integer) – The width of the images provided.

  • image_height (integer) – The height of the images provided.

  • fps (integer) – The framerate of the RTSP stream in frames per second.

start()

Start the RtspVideoWriter server.

Returns

self

write_frame(frame, stream_idx=0)

Send frame to the RTSP server.

Parameters
  • frame (numpy array) – The frame to be written to the video stream.

  • stream_idx (integer) – The stream index to write the frame to.

Raises

Any exception that occurs in RtspVideoWriter process

close()

Stop the RtspVideoWriter server.

Raises

Any exception that occurs in RtspVideoWriter process