Extract Frames by Duration Using FFmpeg Select Filter
This article explains how to use the FFmpeg select video
filter to extract specific video frames based on time duration and
timestamps. You will learn the exact command-line syntax to capture
frames within a precise time range, extract frames at regular duration
intervals, and save them as images.
Extracting Frames Within a Specific Time Range
To extract frames that fall within a specific start and end time, use
the between(t, start, end) evaluation function inside the
select filter. The variable t represents the
timestamp of the frame in seconds.
Run the following command to extract frames between the 10-second and 15-second marks of a video:
ffmpeg -i input.mp4 -vf "select='between(t,10,15)'" -vsync vfr output_%03d.pngbetween(t,10,15): Selects only the frames where the timestamptis greater than or equal to 10 and less than or equal to 15.-vsync vfr: Tells FFmpeg to use a variable frame rate (VFR) so it does not generate duplicate placeholder frames for the unselected parts of the video. (For newer FFmpeg versions, you can also use-fps_mode passthrough).output_%03d.png: Saves the extracted frames as sequentially numbered PNG files (output_001.png,output_002.png, etc.).
Extracting Frames at Regular Time Intervals
If you want to extract one frame at a specific duration interval (for
example, every 5 seconds), you can calculate the difference between the
current frame’s timestamp and the previously selected frame’s timestamp
using the prev_selected_t variable.
Run this command to extract one frame every 5 seconds:
ffmpeg -i input.mp4 -vf "select='isnan(prev_selected_t)+gte(t-prev_selected_t,5)'" -vsync vfr output_%03d.pngisnan(prev_selected_t): Ensures the very first frame of the video (at timestamp 0) is selected, asprev_selected_tis undefined (NaN) for the first frame.gte(t-prev_selected_t,5): Selects a frame only when the difference between the current frame’s timestamp (t) and the last selected frame’s timestamp is greater than or equal to 5 seconds.
Combining Duration with Scene Changes
The select filter allows you to combine duration rules
with other parameters, such as scene change detection. If you want to
detect scene changes but only within a specific part of the video, you
can combine expressions using logical operators.
To extract scene-change frames (where the change threshold is 40%) only between the 30-second and 60-second marks:
ffmpeg -i input.mp4 -vf "select='between(t,30,60)*gt(scene,0.4)'" -vsync vfr output_%03d.pnggt(scene,0.4): Detects a scene change greater than 40%.*: Acts as an AND operator, ensuring both the time duration condition and the scene change threshold are met before a frame is selected.