How to Encode Lossless VP9 Video with FFmpeg
This guide explains how to configure the libvpx-vp9
encoder in FFmpeg for mathematically lossless video compression. You
will learn the specific command-line parameters required to activate
lossless mode, understand how pixel formats affect the output, and see
practical command examples to implement this in your video processing
workflows.
To achieve true lossless compression with the VP9 encoder
(libvpx-vp9) in FFmpeg, you must explicitly set two key
parameters: -lossless 1 and -crf 0.
The Lossless Command Syntax
The basic command structure for encoding a lossless VP9 video is as follows:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -lossless 1 -crf 0 output.webmParameter Breakdown
-c:v libvpx-vp9: Specifies the VP9 video encoder.-lossless 1: Enables the lossless encoding profile within the libvpx library.-crf 0: Sets the Constant Rate Factor to 0. While-lossless 1triggers the lossless mode, setting-crf 0is required by FFmpeg to ensure that no quantization takes place, resulting in mathematically perfect replication of the input frames.
Controlling Encoding Speed and Compression
Lossless encoding produces large file sizes. You can optimize the
compression efficiency (file size vs. encoding time) using the
-cpu-used parameter:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -lossless 1 -crf 0 -cpu-used 1 output.webm-cpu-used <value>: Accepts integers from0to5for quality-focused encoding.0: Highest compression (smallest file size), but extremely slow encoding speed.5: Fastest encoding speed, but lower compression efficiency (larger file size).- The default value is usually
1or2, which offers a good balance for lossless outputs.
Pixel Format Considerations
By default, FFmpeg will attempt to match the input’s pixel format.
However, for maximum compatibility and to avoid quality loss during
color space conversion, you should explicitly define the pixel format
using the -pix_fmt flag.
For Standard YUV 4:2:0 Video:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -lossless 1 -crf 0 -pix_fmt yuv420p output.webmFor RGB Source Material (e.g., Screen Recordings):
VP9 supports lossless RGB encoding, which is ideal for screencasts to prevent color bleeding:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -lossless 1 -crf 0 -pix_fmt gbrp output.webm