How to Extract B-Frames Using FFmpeg Select Filter
This article explains how to use the FFmpeg select video
filter to isolate and extract B-frames (bi-directional predictive
frames) from a video file. You will learn the exact command-line syntax
required to save these frames as a sequence of images or compile them
into a new video file.
Understanding the Select Filter for B-Frames
In video compression, B-frames rely on both past and future frames
for temporal prediction. To extract only these frames, FFmpeg utilizes
the select filter paired with the pict_type
constant.
By evaluating the expression eq(pict_type,B), FFmpeg
discards all I-frames (keyframes) and P-frames (predictive frames),
processing only the B-frames.
Extracting B-Frames as Images
To extract every B-frame from a video and save them as individual PNG or JPG images, use the following command:
ffmpeg -i input.mp4 -vf "select='eq(pict_type,B)'" -vsync vfr bframe_%04d.pngCommand Breakdown:
-i input.mp4: Specifies the input video file.-vf "select='eq(pict_type,B)'": Applies the video filter to select only frames where the picture type is “B”.-vsync vfr: Configures variable frame rate video synchronization. This prevents FFmpeg from duplicating frames to fill the gaps left by the discarded I and P frames. (Note: In newer versions of FFmpeg, you can use-fps_mode vfrinstead).bframe_%04d.png: The output naming pattern, which generates sequentially numbered images (e.g.,bframe_0001.png,bframe_0002.png).
Extracting B-Frames into a New Video
If you want to extract the B-frames and package them directly into a new video container, you must reset the presentation timestamps (PTS). Otherwise, the output video will stutter or freeze during the timestamps where the deleted I and P frames used to be.
Use this command to output a smooth video consisting solely of B-frames:
ffmpeg -i input.mp4 -vf "select='eq(pict_type,B)',setpts=N/FRAME_RATE/TB" -an output_bframes.mp4Command Breakdown:
setpts=N/FRAME_RATE/TB: Recalculates the presentation timestamps for the selected frames. This forces the extracted B-frames to play back-to-back at the container’s standard frame rate.-an: Disables audio recording, as the audio timeline will no longer match the heavily modified video timeline.