LaVOZs

The World’s Largest Online Community for Developers

'; python - Get a still frame during webcam video capture on Linux - LavOzs.Com

I want to capture video from a webcam (saving to file), while occasionally getting the most recent still frame (from python code). Is there a way to do this on Linux?

What I've Tried:

# Capture Video:
ffmpeg -f v4l2 -framerate 30 -video_size 1024x576 -i /dev/video0 myvideo.mp4

...

# In another terminal, try to capture the latest still frame:
ffmpeg -sseof -3 -i myvideo.mp4 -update 1 -q:v 1 current_frame.jpg

But, I get varied responses from this last command, such as

Cannot use -sseof, duration of myvideo.mp4 not known

and

[matroska,webm @ 0x55e1aae26900] Duplicate element
    Last message repeated 2 times

Additional Notes/Constraints:

  1. It must be possible to control the solution from python (e.g. calling ffmpeg via subprocess.Popen) to (a) start recording, (b) get frames at arbitrary points, (c) stop recording.
  2. Frame timing doesn't have to be exact. A frame within the last couple seconds is fine.
  3. Any container format is fine (mkv, mp4, etc.)
  4. Other programs are fine, not just ffmpeg

You can have multiple outputs for FFmpeg. Combine your commands:

ffmpeg -f v4l2 -framerate 30 -video_size 1024x576 -i /dev/video0 myvideo.mp4 -r 1 -update 1 current_frame.jpg
Related
How to record webcam video signal with video4linux2?
How to insert frames to compensate for frames lost during capture
Slice a video with ffmpeg based on frame position
Using ffmpeg to change framerate
Getting timestamp of each frame in a video