Set Target Average Bitrate in FFmpeg libvpx-vp9
This guide provides a straightforward explanation of how to set a
target average bitrate when encoding video with the VP9 codec
(libvpx-vp9) using FFmpeg. It covers the essential
command-line flags for two-pass encoding, constrained quality mode, and
constant bitrate mode to help you achieve your desired file size and
video quality.
To set a target average bitrate in libvpx-vp9, you must
use the -b:v flag followed by your desired bitrate value
(such as 2M for 2 Mbps). Because VP9 is highly optimized
for two-pass encoding, you should always perform a two-pass encode when
aiming for a specific target average bitrate.
Method 1: Two-Pass Constrained Quality (Recommended)
This is the recommended mode for most VP9 encodes. It allows the
encoder to use a target average bitrate while maintaining a maximum
quality threshold using the Constant Rate Factor
(-crf).
Run the following two commands in sequence:
Pass 1:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -crf 30 -pass 1 -an -f null /dev/null(Windows users should use NUL instead of
/dev/null)
Pass 2:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -crf 30 -pass 2 -c:a libopus -b:a 128k output.webmIn this mode, -b:v 2M sets the target average bitrate to
2 Mbps, and -crf 30 acts as a quality ceiling, preventing
the encoder from wasting bits on scenes that do not require them.
Method 2: Pure Two-Pass Average Bitrate (ABR)
If you want the encoder to target an average bitrate without capping
the quality with a CRF value, omit the -crf flag
entirely.
Pass 1:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -pass 1 -an -f null /dev/nullPass 2:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -pass 2 -c:a libopus -b:a 128k output.webmMethod 3: Constant Bitrate (CBR)
For live streaming use cases where you need a strict, unchanging
bitrate, set the target bitrate (-b:v), the minimum bitrate
(-minrate), and the maximum bitrate (-maxrate)
to the exact same value.
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -minrate 2M -maxrate 2M -quality realtime -speed 5 -row-mt 1 -c:a libopus output.webmThis forces the libvpx-vp9 encoder to output a stream
conforming strictly to the 2 Mbps target.