How to Extract I-frames with FFmpeg Select Filter
Extracting keyframes, or I-frames, from a video is a common task for
video analysis, editing, and creating preview thumbnails. This article
provides a quick, practical guide on how to use the FFmpeg
select video filter to isolate and export only the I-frames
from a video file, saving them as individual images.
Understanding the Command
An I-frame (Intra-coded frame) is a complete image that does not
require data from other frames to be decoded. To extract these frames
using FFmpeg, you use the select filter with the
pict_type parameter set to I.
Here is the standard command to extract I-frames as PNG images:
ffmpeg -i input.mp4 -vf "select='eq(pict_type,I)'" -vsync vfr frame_%04d.pngCommand Breakdown
-i input.mp4: Specifies the input video file.-vf "select='eq(pict_type,I)'": Applies the video filter (-vf). Theselectfilter evaluates each frame. The expressioneq(pict_type,I)tells FFmpeg to only keep frames where the picture type equals āIā.-vsync vfr: Stands for Variable Frame Rate. This flag (or its modern equivalent-fps_mode vfr) is crucial because it tells FFmpeg to drop the unselected frames (P and B frames) rather than duplicating the extracted I-frames to maintain the original frame rate.frame_%04d.png: Specifies the output format and naming convention.%04dis a placeholder that is replaced by a sequential, four-digit number (e.g.,frame_0001.png,frame_0002.png).
Extracting I-frames as a New Video
If you want to concatenate the extracted I-frames into a new,
fast-forwarded video instead of individual images, you must reset the
presentation timestamps (PTS) using the setpts filter:
ffmpeg -i input.mp4 -vf "select='eq(pict_type,I)',setpts=N/FRAME_RATE/TB" -an output.mp4In this command, setpts=N/FRAME_RATE/TB recalculates the
timestamps of the selected frames so they play sequentially without
pauses. The -an flag removes the audio, as the original
audio stream will no longer sync with the modified video stream.