How to Extract Every Nth Frame with FFmpeg Select
Extracting specific frames from a video is a fundamental task in
video processing, useful for creating contact sheets, generating
thumbnails, or preparing datasets for machine learning. This article
provides a straightforward guide on how to use FFmpeg’s
select video filter to extract every \(N\)-th frame (for example, every 10th or
100th frame) from a video file and save them as sequential image
files.
The Basic Command
To extract every \(N\)-th frame, you
use the select filter with the mathematical expression
not(mod(n,N)). Here is the standard command to extract
every 10th frame:
ffmpeg -i input.mp4 -vf "select='not(mod(n,10))'" -fps_mode vfr output_%04d.pngCommand Breakdown
-i input.mp4: Specifies the path to your input video file.-vf "select='not(mod(n,10))'": Applies the video filter (-vf).nrepresents the sequential frame number starting at0.mod(n,10)calculates the modulo of the frame number divided by 10. This returns0for frames 0, 10, 20, 30, etc.not(...)negates the result. Sincenot(0)is1(true) andnot(any other number)is0(false), FFmpeg only selects the frames where the frame number is a multiple of 10.
-fps_mode vfr: Sets the frame rate mode to Variable Frame Rate (VFR). This is a critical parameter. Without it, FFmpeg may attempt to duplicate the selected frames to match the original video’s frame rate, resulting in a massive sequence of duplicate images. (Note: In older versions of FFmpeg, use-vsync vfrinstead).output_%04d.png: Specifies the naming convention for the output images.%04dis a placeholder that outputs padded four-digit sequential integers (e.g.,output_0001.png,output_0002.png).
Examples for Different Intervals
To change the interval, simply replace the number 10 in
the modulo function with your desired interval \(N\).
Extract every 24th frame:
ffmpeg -i input.mp4 -vf "select='not(mod(n,24))'" -fps_mode vfr img_%04d.pngExtract every 100th frame:
ffmpeg -i input.mp4 -vf "select='not(mod(n,100))'" -fps_mode vfr img_%04d.png
Alternative: Extracting Frames by Time Interval
If you prefer to extract frames based on time (seconds) rather than
frame counts, you can use the t (time) variable instead of
n (frame number).
To extract one frame every 5 seconds:
ffmpeg -i input.mp4 -vf "select='not(mod(t,5))'" -fps_mode vfr output_%04d.png