FFmpeg Select Filter: Extract Frames by Time
This article explains how to use the select video filter
in FFmpeg to extract or filter specific frames based on time. You will
learn the syntax for selecting frames within precise time ranges,
starting after a specific timestamp, or capturing frames at regular time
intervals, allowing you to efficiently slice and process your video
files.
The FFmpeg select filter evaluates an expression for
each input frame. If the expression evaluates to a non-zero value, the
frame is selected; otherwise, it is discarded. To filter based on time,
you primarily use the variable t, which represents the
presentation timestamp of the current frame in seconds, and
prev_selected_t, which represents the timestamp of the last
selected frame.
1. Selecting Frames within a Time Range
To select frames that fall within a specific start and end time, use
the between(val, min, max) function.
The following command selects and exports frames between the 10th and 15th seconds of a video:
ffmpeg -i input.mp4 -vf "select='between(t,10,15)'" -vsync vfr output_%03d.pngNote: The -vsync vfr (or -fps_mode vfr
in newer FFmpeg versions) flag is crucial. It tells FFmpeg to use a
variable frame rate, preventing it from duplicating the selected frames
to fill the gaps of the discarded frames.
2. Selecting Frames After a Specific Time
To select all frames after a certain point in time, use the “greater
than or equal to” operator (gte).
The following command discards all frames before the 30-second mark and keeps everything from second 30 onward:
ffmpeg -i input.mp4 -vf "select='gte(t,30)'" -vsync vfr output.mp43. Selecting Frames at Regular Time Intervals
To select one frame at regular intervals (e.g., one frame every 5
seconds), you can measure the difference between the current frame’s
time (t) and the previously selected frame’s time
(prev_selected_t).
The following command extracts one frame every 5 seconds:
ffmpeg -i input.mp4 -vf "select='gte(t-prev_selected_t,5)'" -vsync vfr output_%03d.png4. Combining Time-Based Conditions
You can combine multiple time-related conditions using logical
operators like + (OR) and * (AND).
The following command selects frames that are either between 5 and 10 seconds, OR between 20 and 25 seconds:
ffmpeg -i input.mp4 -vf "select='between(t,5,10)+between(t,20,25)'" -vsync vfr output_%03d.png