How to Enable VP9 Two-Pass Encoding in FFmpeg
This article provides a straightforward guide on how to enable and configure two-pass encoding for the VP9 video codec using FFmpeg. You will learn the specific command-line syntax required to run both passes, which optimizes video quality and file size by analyzing the video dynamics before final compression.
Two-pass encoding is the recommended method for encoding VP9 videos because it allows the encoder to allocate bitrate more efficiently. The first pass analyzes the video content and writes a log file, while the second pass uses this log file to perform the actual encoding.
Step 1: Run the First Pass
In the first pass, FFmpeg analyzes the input video and creates a log
file (usually named ffmpeg2pass-0.log). You should disable
audio processing during this step using the -an flag to
speed up the process. The output video is discarded using
-f null.
For Linux and macOS:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -pass 1 -an -f null /dev/nullFor Windows:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -pass 1 -an -f null NULStep 2: Run the Second Pass
In the second pass, FFmpeg reads the log file generated in the first pass and performs the actual video and audio encoding to output the final WebM file.
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -pass 2 -c:a libopus -b:a 128k output.webmParameter Breakdown
-c:v libvpx-vp9: Specifies the VP9 video encoder.-b:v 2M: Sets the target video bitrate (e.g., 2 Megabits per second). Ensure this value is identical in both passes.-pass 1and-pass 2: Tells the encoder which pass of the two-pass process is being executed.-an: Disables audio. This is used in the first pass because audio analysis is not needed to calculate video bitrate allocation.-c:a libopus -b:a 128k: Encodes the audio using the Opus codec at 128 kbps during the second pass.
Speed and Quality Optimization (Optional)
VP9 encoding can be slow. To speed up the process, you can add the
-row-mt 1 flag to enable row-based multi-threading, and use
the -speed (or -cpu-used) parameter.
For the first pass, you can use a faster speed setting (e.g.,
-speed 4):
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -speed 4 -row-mt 1 -pass 1 -an -f null /dev/nullFor the second pass, use a lower speed setting for better compression
quality (e.g., -speed 1 or -speed 2):
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -speed 1 -row-mt 1 -pass 2 -c:a libopus -b:a 128k output.webm