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.mp4

In this example, -bf 3 instructs the encoder to use a maximum of 3 consecutive B-frames.

Key Configuration Options

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:

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