Configure B-Frames in FFmpeg libx264
This article provides a quick guide on how to configure the number of
consecutive B-frames (bi-directional predictive frames) when encoding
video using the libx264 library in FFmpeg. You will learn
the specific command-line parameters required to adjust B-frame settings
to optimize either video compression efficiency or playback latency.
To set the number of B-frames in FFmpeg using the
libx264 encoder, use the -bf option. This
parameter defines the maximum number of consecutive B-frames the encoder
is allowed to use.
Basic Command Syntax
The basic syntax to set the maximum number of consecutive B-frames is as follows:
ffmpeg -i input.mp4 -c:v libx264 -bf 3 output.mp4In this example, -bf 3 instructs the encoder to use a
maximum of 3 consecutive B-frames.
Key Configuration Options
Disabling B-frames: For live streaming, video conferencing, or low-latency applications, you can disable B-frames entirely by setting the value to
0. This significantly reduces decoding delay.ffmpeg -i input.mp4 -c:v libx264 -bf 0 output.mp4Increasing B-frames: For high-efficiency file storage where latency is not a concern, you can increase the B-frames to a higher value (typically between
3and16). The default value forlibx264is3.ffmpeg -i input.mp4 -c:v libx264 -bf 5 output.mp4
Controlling B-Frame Decision Strategy
By default, the encoder dynamically decides whether to use the
maximum number of B-frames specified based on the complexity of the
video scene. You can influence this behavior using the
-b_strategy option:
-b_strategy 0: Disables adaptive B-frame detection. The encoder will always use the exact maximum number of B-frames specified by-bf.-b_strategy 1: Enables a fast adaptive decision algorithm.-b_strategy 2: Enables an optimal (but slower) adaptive decision algorithm, which is the default forlibx264.
For example, to force the encoder to always use exactly 4 B-frames without adaptive reduction, use:
ffmpeg -i input.mp4 -c:v libx264 -bf 4 -b_strategy 0 output.mp4