FFmpeg Select Filter: How to Extract P-Frames
This guide provides a straightforward tutorial on how to use the
FFmpeg select video filter to isolate and extract P-frames
(predictive frames) from a video file. You will learn the exact
command-line syntax required to filter these specific frames and save
them either as a sequence of images or as a newly encoded video
file.
Understanding the FFmpeg Select Command
To extract P-frames, you must use the select video
filter combined with the pict_type constant. P-frames are
represented by the character P.
Here is the standard command to extract P-frames as a sequence of PNG images:
ffmpeg -i input.mp4 -vf "select='eq(pict_type,P)',setpts=N/FRAME_RATE/TB" -fps_mode vfr output_%04d.pngCommand Breakdown
-i input.mp4: Specifies the path to your input video file.-vf: Introduces the video filtergraph.select='eq(pict_type,P)': This is the core filter. It evaluates each frame and only passes it through if its picture type (pict_type) is equal (eq) toP.setpts=N/FRAME_RATE/TB: Resets the Presentation Timestamps (PTS) of the selected frames. Without this, the output frames would retain their original timestamps, causing massive gaps and rendering issues in the output.-fps_mode vfr: Tells FFmpeg to use a variable frame rate. This prevents the encoder from duplicating frames to fill the gaps left by the discarded I-frames and B-frames. (Note: On older versions of FFmpeg,-vsync vfris used instead).output_%04d.png: Specifies the output format.%04dcreates a sequentially numbered naming pattern (e.g.,output_0001.png,output_0002.png).
Extracting P-Frames to a Video File
If you want to save the extracted P-frames as a continuous video clip rather than individual images, use the following command:
ffmpeg -i input.mp4 -vf "select='eq(pict_type,P)',setpts=N/FRAME_RATE/TB" -an output.mp4-an: Disables audio, as the audio track will no longer sync with the drastically shortened, P-frame-only video stream.