Extract Keyframes from Video as Images Using FFmpeg
Extracting keyframes (also known as I-frames) from a video is a crucial task for video analysis, thumbnail generation, and editing. This article provides a straightforward, step-by-step guide on how to use the powerful command-line tool FFmpeg to identify, isolate, and save all keyframes from a video stream as individual image files.
The Standard FFmpeg Command
To extract only the I-frames (keyframes) from a video, you can use
FFmpeg’s video filter (-vf) option with the
select filter. Run the following command in your
terminal:
ffmpeg -i input.mp4 -vf "select='eq(pict_type,I)'" -vsync vfr output_%04d.jpgCommand Breakdown
-i input.mp4: Specifies the path to your input video file.-vf "select='eq(pict_type,I)'": Applies a video filter. Theselectfilter evaluates an expression for each frame. Here,eq(pict_type,I)checks if the picture type is equal to “I” (Intra-coded frame / keyframe).-vsync vfr: Sets the video synchronization method to Variable Frame Rate (VFR). This prevents FFmpeg from duplicating frames to maintain the original frame rate, ensuring only the exact keyframes are saved. (Note: In newer versions of FFmpeg, you can also use-fps_mode vfr).output_%04d.jpg: Defines the naming pattern for the output images.%04dis a placeholder that formats the frame numbers sequentially with leading zeros (e.g.,output_0001.jpg,output_0002.jpg).
The Fast Extraction Method
The standard method decodes the entire video stream to find the
keyframes, which can be slow for long videos. You can speed up the
process significantly by telling the decoder to skip reading
non-keyframes entirely using the -skip_frame option:
ffmpeg -skip_frame nokey -i input.mp4 -vsync vfr output_%04d.jpg-skip_frame nokey: Tells the decoder to discard any frame that is not a keyframe before decoding. This drastically reduces CPU usage and processing time because FFmpeg does not have to process P-frames and B-frames.
Choosing the Output Format
While the examples above use JPEG (.jpg), you can export
to other formats simply by changing the file extension in the output
pattern:
- For high-quality, lossless images, use PNG:
output_%04d.png - For web-optimized images, use WebP:
output_%04d.webp