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.mp4Understanding the Parameters
-maxrate: This defines the absolute maximum limit of the bitrate. It can be specified in kilobits per second (e.g.,4000kor4M).-bufsize: This defines the size of the receiver’s buffer. The encoder uses this buffer size to determine how much the video bitrate can fluctuate. It is also specified in kilobits or megabits (e.g.,8000kor8M).
How to Choose the Buffer Size
The ratio between -maxrate and -bufsize
determines how strictly the bitrate is controlled:
- 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
- Example (2-second buffer):
- 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
- Example (1-second buffer):
- 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
- Example (0.5-second buffer):
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.