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.webm

However, relying on default settings is not recommended, as it can result in poor quality or extremely slow encoding times.

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

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/null

Second 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).

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