How to Use libvpx-vp9 in FFmpeg
This article provides a straightforward guide on how to invoke the
libvpx-vp9 encoder within an FFmpeg command line. You will
learn the basic command structure, recommended encoding methods like
Constant Quality (CRF) and Two-Pass encoding, and how to optimize
encoding speed and audio compatibility for WebM video delivery.
Basic Syntax
To encode a video to VP9, you must specify the video codec as
libvpx-vp9. The simplest command structure looks like
this:
ffmpeg -i input.mp4 -c:v libvpx-vp9 output.webmHowever, relying on default settings is not recommended, as it can result in poor quality or extremely slow encoding times.
Recommended Encoding Methods
There are two primary ways to encode VP9 videos depending on your requirements: Constant Quality (CRF) and Two-Pass encoding.
1. Constant Quality (CRF)
This method is recommended for most general purposes. It targets a
specific quality level throughout the video. For VP9, you must set the
bitrate limit to 0 (-b:v 0) to enable true CRF mode.
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libopus output.webm-crf 30: The quality level. Values can range from 0 to 63. Lower values mean better quality but larger file sizes. The recommended range for VP9 is 15 to 35 (31 is standard for 1080p).-b:v 0: Required for the encoder to prioritize the CRF quality target over a specific bitrate.-c:a libopus: Encodes the audio to Opus, the standard audio companion for VP9 in WebM containers.
2. Two-Pass Encoding (Target Bitrate)
If you need your output file to be a specific size, you should use two-pass encoding.
First Pass:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -speed 4 -pass 1 -an -f null /dev/nullSecond Pass:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -speed 1 -pass 2 -c:a libopus output.webm(Note: Windows users should use NUL instead of
/dev/null in the first pass).
-b:v 2M: Sets the target video bitrate (e.g., 2 Megabits per second).-pass 1and-pass 2: Tells FFmpeg to generate a log file on the first pass and use it to optimize the encode on the second pass.-an: Disables audio during the first pass to speed up processing.
Controlling Speed and CPU Usage
VP9 encoding is highly CPU-intensive. You can control the speed and
quality trade-off using the -speed (or
-cpu-used) parameter, which accepts values from 0 to 8:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -speed 2 output.webm- Values 0 to 4: Recommended for the final encode. Lower numbers yield better quality per megabyte but take much longer to encode.
- Values 5 to 8: Recommended for real-time streaming or quick drafts. It speeds up the encoding process significantly at the cost of some compression efficiency.