How to Configure Two-Pass Encoding Using libvpx
This article provides a straightforward guide on how to configure and
run two-pass video encoding using the libvpx encoder
(specifically for VP9 and VP8 formats) via FFmpeg. You will learn the
exact command-line syntax required for both the analysis and encoding
passes, as well as the key parameters needed to achieve optimal video
quality and file size.
Understanding Two-Pass Encoding
Two-pass encoding optimizes video quality by analyzing the video
content before compressing it: * Pass 1: The encoder
analyzes the video input to identify motion, scene cuts, and complexity.
It writes this analysis data to a log file (usually named
ffmpeg2pass-0.log) without generating a playable video
file. * Pass 2: The encoder uses the log file from the
first pass to allocate the target bitrate efficiently, giving more data
to complex action scenes and less to static scenes.
Step-by-Step Configuration
To perform two-pass encoding with libvpx-vp9 in FFmpeg,
you must run two separate commands in sequence.
Step 1: Run the First Pass
The first pass analyzes the video. Audio processing is disabled to save time, and the output video is discarded because we only need the generated log file.
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -pass 1 -an -f null --c:v libvpx-vp9: Selects the VP9 encoder (uselibvpxif you are encoding to the older VP8 format).-b:v 2M: Sets the target video bitrate (e.g., 2 Megabits per second). This must match in both passes.-pass 1: Instructs the encoder to perform the analysis pass.-an: Disables audio to speed up the process.-f null -: Discards the video output since we only need the log file.
Step 2: Run the Second Pass
The second pass reads the log file created in Step 1, processes the audio, and outputs the final compressed WebM file.
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -pass 2 -c:a libopus -b:a 128k output.webm-pass 2: Instructs the encoder to use the log file from the first pass to write the final video.-c:a libopus: Encodes the audio using the Opus codec, which is standard for WebM.-b:a 128k: Sets the audio bitrate to 128 kbps.
Best Practices for libvpx Two-Pass Encoding
- Keep Settings Identical: Video parameters such as
bitrate (
-b:v), resolution, framerate, and keyframe intervals must be identical in both the first and second-pass commands. - Use the Speed Parameter: You can use the
-row-mt 1flag to enable row-based multi-threading for faster encoding. Additionally, setting-speed 4for the first pass and-speed 1or-speed 2for the second pass provides an optimal balance between speed and quality. - Constrained Quality Mode: For even better results,
you can use Constrained Quality (CQ) mode by adding
-crf(e.g.,-crf 31) and setting a maximum bitrate limit with-maxratealongside your target-b:vparameter.