FFmpeg H.265 Visually Lossless Settings
Achieving a balance between high video quality and manageable file sizes is a common challenge in video encoding. This article provides a straightforward guide on how to configure FFmpeg using the H.265 (HEVC) codec to achieve visually lossless compression, focusing on the Constant Rate Factor (CRF) and preset parameters to optimize your encoding workflow.
To achieve visually lossless compression with H.265 in FFmpeg, you
need to use the libx265 encoder and properly configure the
Constant Rate Factor (CRF) and encoding preset.
The Visually Lossless Command
Use the following standard command template to encode your video:
ffmpeg -i input.mp4 -c:v libx265 -crf 18 -preset slow -pix_fmt yuv420p10le -c:a copy output.mp4Parameter Breakdown
-c:v libx265: Instructs FFmpeg to use the x265 encoder for H.265/HEVC video compression.-crf 18: Controls the video quality. The Constant Rate Factor (CRF) scale for H.265 ranges from 0 to 51. Lower values mean better quality. While a CRF of 0 is mathematically lossless (resulting in very large files), a CRF between 18 and 21 is considered visually lossless. At this range, the human eye cannot distinguish the compressed video from the original source.-preset slow: Determines the encoding speed-to-compression ratio. Available presets range fromultrafasttoveryslow. Usingsloworslowerallows the encoder to use more advanced algorithms to compress the file further without sacrificing quality. If you are in a hurry, usemedium(the default).-pix_fmt yuv420p10le: Sets the pixel format to 10-bit. Even if your source video is 8-bit, encoding in 10-bit H.265 reduces color banding artifacts (especially in gradients like skies) and actually improves compression efficiency.-c:a copy: Copies the audio stream directly without re-encoding it, preserving the original audio quality and saving processing time.
Adjusting for Specific Needs
- For Ultra-High Definition (4K/8K): You can often increase the CRF to 20 or 22 and still maintain visual losslessness. Because of the high pixel density, compression artifacts are much harder to spot.
- For Animation/CGI: Add the
-tune animationflag to optimize the encoder for flat colors and sharp edges. - For Fast Turnaround: If encoding time is a
constraint, change
-preset slowto-preset fast. The file size will be slightly larger, but the quality will remain visually lossless at the same CRF.