How to Extract Keyframes as Images with FFmpeg

Extracting keyframes (also known as I-frames) from a video allows you to capture the highest-quality, standalone frames representing scene changes or reference points without decoding every intermediate frame. This guide demonstrates how to use the open-source multimedia framework FFmpeg to detect all I-frames in a video stream and automatically export them as standalone image files.

The Standard Keyframe Extraction Command

To extract all I-frames from a video file using video filtering, open your terminal or command prompt and run the following command:

ffmpeg -i input.mp4 -vf "select='eq(pict_type,PICT_TYPE_I)'" -vsync vfr keyframe_%04d.png

Breakdown of Arguments

  • -i input.mp4: Specifies the path to your source video file.
  • -vf "select='eq(pict_type,PICT_TYPE_I)'": Uses FFmpeg's video filter engine to evaluate the picture type of each frame. It only selects frames designated as PICT_TYPE_I (Intra-coded frames).
  • -vsync vfr (or -fps_mode vfr in newer FFmpeg releases): Sets the video sync method to Variable Frame Rate. This prevents FFmpeg from duplicating frames to fill the gaps between selected keyframes.
  • keyframe_%04d.png: Defines the naming pattern for the output files. The %04d placeholder produces four-digit sequential numbering (e.g., keyframe_0001.png, keyframe_0002.png).

Faster Extraction Using Decoder Skipping

The filtering method decodes the entire video stream to identify which frames are I-frames. For large files or long videos, you can significantly accelerate the process by telling the decoder to skip non-keyframe packets directly:

ffmpeg -skip_frame nokey -i input.mp4 -vsync vfr -frame_pts true keyframe_%04d.jpg
  • -skip_frame nokey: Instructs the decoder to discard all frames except keyframes before decoding. This drastically reduces CPU usage and processing time.
  • -frame_pts true: Names output files using their Presentation Time Stamp (PTS) index instead of a simple consecutive sequence, making it easier to identify where each frame occurs in the video.

Output Format Options

You can adjust the output file format based on your storage and quality requirements:

  • PNG (Lossless): Best for maximum visual fidelity and computer vision workflows.

    ffmpeg -skip_frame nokey -i input.mp4 -vsync vfr output_%03d.png
  • JPEG (Compressed): Best for saving disk space. You can control the quality using the -q:v flag (values range from 2 to 31, where 2 is the highest quality).

    ffmpeg -skip_frame nokey -i input.mp4 -vsync vfr -q:v 2 output_%03d.jpg