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:

  • Zero B-frames (-bf 0): Disables B-frames entirely. This is highly recommended for real-time video streaming, video conferencing, and low-latency environments because B-frames require looking ahead at future frames, which introduces processing delay.
  • Standard Compression (-bf 2 to -bf 4): This is the default range for most standard video playback (web streaming, local playback). It offers an optimal balance between high compression efficiency and low hardware decoding requirements.
  • Maximum Compression (-bf 16): The maximum limit for H.264. Setting a higher number allows the encoder to compress the video more aggressively, resulting in smaller file sizes for static videos (like slideshows or screencasts), though it requires more CPU power to encode and decode.

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
  • 0: Disables dynamic B-frame selection (the encoder will always use the maximum number of B-frames specified by -bf).
  • 1: Enables fast dynamic B-frame placement (default).
  • 2: Enables high-quality device-dependent path search (slower encoding, but better compression).

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
  • none: Disables B-frames as references (useful for older hardware compatibility).
  • strict: Allows one B-frame per GOP to be used as a reference (Blu-ray standard).
  • normal: Allows multiple B-frames to be used as references.