Playing Video From a File

The FileVideoStream class enables loading video from a local file. To generate a list of all video files in a directory, the list_files() function can be used.

video_paths = edgeiq.list_files(base_path="some/video/dir", valid_exts=(".avi", ".mpeg"))

The video stream can be started in the following way, using either a known path to a video file, or a path returned by list_files():

with edgeiq.FileVideoStream("video.mpeg") as video_stream:

Within the following code block, the next frame can be read out using the read() function:

frame = video_stream.read()

Unlike WebcamVideoStream, the frames are queued up when the app read gets behind the video loading. This means there are no dropped frames when using FileVideoStream. The file connection is automatically cleaned up upon exiting the code block.

To check if there are more frames left in the video, use the more() function. Alternatively, you can catch the NoMoreFrames exception when reading the next frame:

try:
  frame = video_stream.read()
except edgeiq.NoMoreFrames:
  # Do something

If you’d like the video to loop endlessly, this is a good spot to restart the video by calling the start() function again:

while True:
  try:
    frame = video_stream.read()
  except edgeiq.NoMoreFrames:
    video_stream.start()
    continue

Simulating a Real-Time Video Stream

In the process of building your app, it can be useful to test against a video but simulate a real-time video stream like you would get with a webcam. This can be done by setting the play_realtime argument to True when instantiating the FileVideoStream object:

with edgeiq.FileVideoStream("video.mpeg", play_realtime=True) as video_stream:

This works by playing the video file back at the encoded FPS and dropping frames that aren’t read out in time.