Using FFmpeg to Extract Video Frames

Extract video frames with FFmpeg when you need individual pictures rather than a complete recording. A camera may produce many images per second, but an inspection task may need only a few. Sampling can reduce storage and the amount of image data sent for AI analysis.

Wi-Fi camera
MJPEG stream
FFmpeg
Record or sample
JPEG files
Inspect or analyze
Local video processing pipeline: receive the stream, sample frames and save individual images.

Install and check FFmpeg

Obtain FFmpeg from the official download page or your operating system’s package manager. Open a new terminal and check that the executable is available:

ffmpeg -version
ffprobe -version

Use a writable directory for the examples. If FFmpeg is not recognized, check the installation and the PATH environment variable.

Step 1: Record a short video from the camera

Our ESP32-CAM firmware exposes an MJPEG stream at port 81. Verify the stream in a browser first and replace the example address with your camera’s actual IP.

Wi-Fi ESP32 camera on a tripod, project illustration
A camera mounted for stable observation. Illustration from the Bench to Field project.
ffmpeg -i http://192.168.1.200:81/stream -t 10 -an -c:v libx264 -pix_fmt yuv420p clip.mp4

This records approximately ten seconds of media, drops audio and encodes the images into H.264 video in an MP4 container. The FFmpeg build must include the libx264 encoder. The recording duration is based on media timestamps, so startup and network delays may make elapsed wall-clock time longer.

If libx264 is unavailable, check the available encoders with ffmpeg -encoders, or use an FFmpeg build that includes it. A local alternative for preserving the original MJPEG data without re-encoding is:

ffmpeg -i http://192.168.1.200:81/stream -t 10 -an -c:v copy clip.mkv

Step 2: Extract one frame per second

ffmpeg -i clip.mp4 -vf fps=1 -q:v 2 frame_%03d.jpg

The fps=1 filter converts the input to a one-frame-per-second sequence. The filename pattern produces numbered files such as frame_001.jpg and frame_002.jpg. For MJPEG output, a low positive q:v value such as 2 requests high image quality.

Frame 1
0 s (example)
Frame 2
1 s (example)
Frame 3
2 s (example)
Frame 4
3 s (example)

Illustrative sampling timeline; actual selected source timestamps depend on input timing and filter behavior.

Step 3: Change the sampling rate

FilterIntended sampling interval
fps=20.5 seconds
fps=11 second
fps=1/55 seconds

Higher rates preserve more temporal detail but create more files. A fast event between samples can be missed. FFmpeg may duplicate frames when the requested rate is greater than the available source frame rate.

Extract images directly from a live stream

If you do not need a video file, sample the stream directly:

ffmpeg -i http://192.168.1.200:81/stream -t 10 -vf fps=1 -q:v 2 live_%03d.jpg

Open the resulting JPEG files and verify the quality before passing them to an AI assistant. Use a new directory or new output prefix for another run to avoid overwriting an earlier experiment.

Troubleshooting and practical limitations

  • Connection fails: verify the IP address, port, network and camera power.
  • Stream stalls: check Wi-Fi reception and available bandwidth. Start with a single streaming client.
  • No encoder: use a compatible FFmpeg build or the stream-copy example.
  • Missing or blurred events: increase sampling frequency or improve lighting and camera placement.

Frame extraction is not event detection. An external application must interpret the images, manage timing and decide what to do when an event occurs. Keep unsecured camera endpoints on a trusted local network.

References

FFmpeg command-line documentation · FPS video filter · Formats and image sequences.

Build the complete project

See Connect a Camera to Your AI for the hardware, firmware and free source package. These examples use the project’s local HTTP endpoints.

Leave a Comment