Extract Frames at Regular Intervals with FFmpeg Select
Extracting specific images from a video file is a common task in
video processing and analysis. This article provides a quick and
practical guide on how to use the FFmpeg select video
filter to extract frames at precise, regular intervals—either by time
(e.g., every few seconds) or by frame count (e.g., every Nth frame)—and
save them as image files.
Extracting a Frame Every N Seconds
To extract a single frame at a regular time interval (for example,
every 10 seconds), use the select filter with the
mod (modulo) function on the timestamp variable
t.
Run the following command in your terminal:
ffmpeg -i input.mp4 -vf "select='not(mod(t\,10))',setpts=N_TB" -fps_mode vfr frame_%04d.pngHow it works: * -vf "select='...'":
Applies the video filtergraph. * not(mod(t\,10)): The
select filter evaluates this expression for every frame.
The expression returns true when the presentation time t
divided by 10 has a remainder of 0 (i.e., at 10s, 20s, 30s, etc.). *
setpts=N_TB: Resets the presentation timestamps of the
selected frames to be contiguous. This prevents gaps in the output
sequence. * -fps_mode vfr: Sets the video sync method to
Variable Frame Rate (VFR), which ensures FFmpeg only outputs the
selected frames instead of duplicating frames to match the original
frame rate. (Note: For older versions of FFmpeg, use
-vsync vfr instead). * frame_%04d.png: Saves
the extracted frames as sequential images (e.g.,
frame_0001.png, frame_0002.png).
Extracting Every Nth Frame
If you prefer to extract frames based on frame count rather than time
(for example, extracting every 100th frame), you can use the frame
number variable n instead of t.
Run this command:
ffmpeg -i input.mp4 -vf "select='not(mod(n\,100))',setpts=N_TB" -fps_mode vfr frame_%04d.pngHow it works: * not(mod(n\,100)): The
filter selects a frame whenever the frame index n is a
multiple of 100. * The remaining parameters function exactly the same as
in the time-based extraction command, ensuring clean, sequential image
output without duplicates.