How to Set B-frames in FFmpeg Video Stream

Controlling the number of bidirectional predictive frames (B-frames) in FFmpeg is crucial for balancing video quality, file size, and decoding latency. This article provides a straightforward guide on how to configure the number of B-frames in your FFmpeg video streams using the -bf option, including practical command-line examples for popular encoders like H.264 and H.265.

The -bf Option in FFmpeg

To set the maximum number of consecutive B-frames, use the -bf (or -b_frames) option followed by the desired integer. This parameter is placed after the input file and before the output file in your FFmpeg command.

Here is the basic command syntax:

ffmpeg -i input.mp4 -c:v libx264 -bf 3 output.mp4

In this example, -c:v libx264 selects the H.264 encoder, and -bf 3 limits the encoder to a maximum of 3 consecutive B-frames.

Common B-frame Configurations

The number of B-frames you should choose depends on your specific use case:

Advanced B-frame Tuning

To get the most out of your B-frame configuration, you can pair the -bf flag with other encoder-specific settings:

Enable Dynamic B-frames

By default, setting -bf only defines the maximum limit. The encoder dynamically decides how many B-frames to use frame-by-frame. You can control this decision-making process using the -b_strategy option:

ffmpeg -i input.mp4 -c:v libx264 -bf 3 -b_strategy 1 output.mp4

B-Pyramid (Reference B-frames)

By default, modern encoders allow B-frames to be used as references for other frames, which improves compression. You can control this with the -b-pyramid flag for x264/x265:

ffmpeg -i input.mp4 -c:v libx264 -bf 4 -b-pyramid normal output.mp4