How to Use FFmpeg for Lossless x264 Encoding
This article explains the exact FFmpeg parameters required to achieve mathematically lossless video encoding using the x264 library. You will learn the primary command-line arguments needed to trigger lossless mode, understand how different pixel formats affect the output, and see practical command examples to ensure your video data remains perfectly preserved without any compression artifacts.
The Core Lossless Parameters
To encode a video losslessly using the x264 encoder in FFmpeg, you must specify the encoder and set the compression quality parameter to zero. The two essential parameters are:
-c:v libx264: Selects the H.264/MPEG-4 AVC encoder.-crf 0or-qp 0: Triggers the mathematically lossless mode in x264.
While Constant Rate Factor (-crf) and Quantization
Parameter (-qp) serve different purposes in lossy
compression, setting either to 0 instructs the x264 encoder
to bypass lossy quantization entirely, resulting in an identical
frame-by-frame reproduction of the input.
Understanding Color Space and Pixel Formats
To achieve “true” lossless encoding, you must also consider the color
space of your source material. If your source video or image sequence is
in RGB color space (such as a screen recording or digital animation) and
you convert it to the standard YUV 4:2:0 color space, you will lose
color detail due to chroma subsampling—even if you use
-crf 0.
For YUV Sources
If your source is already YUV (like most camera footage), you can maintain the original pixel format:
ffmpeg -i input.mp4 -c:v libx264 -crf 0 output.mp4For RGB Sources (True Lossless)
To prevent chroma subsampling on RGB sources, you should use the
specialized libx264rgb encoder. This encoder native-handles
RGB pixel formats without converting them to YUV:
ffmpeg -i input.mp4 -c:v libx264rgb -crf 0 output.mp4Alternatively, if you must use the standard libx264
encoder, you can force a 4:4:4 chroma subsampling format (which does not
discard color data) using the High 4:4:4 Predictive profile:
ffmpeg -i input.mp4 -c:v libx264 -crf 0 -pix_fmt yuv444p output.mp4Optimizing File Size with Presets
Because lossless encoding preserves every bit of data, the resulting
file sizes can be exceptionally large. You can use the
-preset parameter to control the compression
efficiency.
Using a slower preset does not change the visual quality (which is always 100% perfect in lossless mode), but it allows the encoder to use more advanced compression algorithms to reduce the final file size:
ffmpeg -i input.mp4 -c:v libx264 -crf 0 -preset veryslow output.mp4Available presets range from ultrafast (fastest encoding
speed, largest file size) to veryslow (slowest encoding
speed, smallest file size).
Player Compatibility Warning
While lossless H.264 files are standard-compliant, many common
hardware players and mobile devices do not support the profiles required
for lossless playback (specifically the High 10 or High 4:4:4 Predictive
profiles). Lossless x264 files are highly recommended for archiving,
editing, and intermediate mastering, but they should be transcoded to a
standard lossy format (such as YUV 4:2:0 at -crf 18 to
23) for general distribution and playback.