How to Set VBV Maxrate and Buffer Size in FFmpeg

This article explains how to precisely configure the Video Buffer Verifier (VBV) in FFmpeg using the -maxrate and -bufsize flags. Setting these parameters is essential for controlling bitrate spikes, ensuring smooth playback on hardware-constrained devices, and preventing buffering issues during live streams.

To enforce a strict maximum bitrate in FFmpeg, you must use two parameters in tandem: -maxrate and -bufsize. Setting -maxrate alone will not work; the encoder requires -bufsize to calculate how often to check and throttle the bitrate.

The Core Commands

The basic syntax for applying VBV limits in FFmpeg is as follows:

ffmpeg -i input.mp4 -c:v libx264 -b:v 4000k -maxrate 4000k -bufsize 8000k output.mp4

Understanding the Parameters

How to Choose the Buffer Size

The ratio between -maxrate and -bufsize determines how strictly the bitrate is controlled:

  1. For Live Streaming (1 to 2-second buffer): A buffer size equal to or double the maximum bitrate is standard. This allows the encoder some flexibility to maintain image quality during high-motion scenes while preventing connection drops.
    • Example (2-second buffer): -maxrate 4M -bufsize 8M
  2. For Strict Hardware Limits (1-second or smaller buffer): If target devices have very limited memory, set the buffer size equal to the maxrate. This forces the encoder to check the bitrate limit every second.
    • Example (1-second buffer): -maxrate 4M -bufsize 4M
  3. For Low Latency (Sub-second buffer): If low latency is your primary goal, you can set the buffer size to half of the maxrate. This restricts bitrate spikes severely but may degrade quality during complex scenes.
    • Example (0.5-second buffer): -maxrate 4M -bufsize 2M

Application Across Encoders

The VBV model works across major FFmpeg video encoders, including H.264 (libx264), H.265 (libx265), and VP9 (libvpx-vp9). Ensure your values are formatted correctly using k for kilobits or M for megabits, and always place the -maxrate and -bufsize flags after the specified video codec.