Enable Pyramid B-Frames in FFmpeg with x264

This guide explains how to enable and configure pyramid B-frame structures in FFmpeg using the libx264 encoder. By utilizing B-pyramid settings, you can significantly improve video compression efficiency and visual quality without increasing the overall file size.

Understanding Pyramid B-Frames

By default, standard B-frames (bi-directional predictive frames) cannot be used as references for predicting other frames. When you enable pyramid B-frames (also known as hierarchical B-frames), the encoder allows certain B-frames to be used as references for other B-frames. This creates a high-efficiency nested structure that improves coding efficiency, especially in high-motion scenes.

Prerequisites

For B-pyramid structures to work, you must instruct the encoder to use multiple B-frames. A pyramid structure requires at least 2 B-frames, though a setting of 3 or more is recommended to see the full compression benefits. You set this using the -bf flag.

Setting up the FFmpeg Command

You can enable and configure the pyramid structure using the b-pyramid option inside the -x264-params flag. There are three configuration modes available:

Example Commands

To encode a video with B-pyramid set to normal (recommended for web distribution) and a maximum of 4 consecutive B-frames, use the following command:

ffmpeg -i input.mp4 -c:v libx264 -bf 4 -x264-params b-pyramid=normal -c:a copy output.mp4

If you are encoding video specifically for Blu-ray authoring, use the strict setting instead:

ffmpeg -i input.mp4 -c:v libx264 -bf 4 -x264-params b-pyramid=strict -c:a copy output.mp4

Alternatively, you can use numeric values inside the parameters:

ffmpeg -i input.mp4 -c:v libx264 -bf 4 -x264-params b-pyramid=2 -c:a copy output.mp4

Verification

If you want to verify that your output file contains the pyramid structure, you can inspect the encoding log in your terminal output during the process. Look for the x264 profile summary at the end of the process, which will display b-pyramid=1 (strict) or b-pyramid=2 (normal).