How to Set H.265 CRF in FFmpeg
Setting the Constant Rate Factor (CRF) for H.265 (HEVC) encoding in FFmpeg allows you to achieve the optimal balance between video quality and file size. This guide provides a straightforward explanation of how the CRF scale works for H.265, the exact FFmpeg command syntax to use, and recommendations for choosing the right CRF value for your specific video compression needs.
The Basic FFmpeg Command for H.265 CRF
To encode a video using H.265 with a specific CRF value, use the x265
library (libx265) and the -crf flag.
Here is the standard command template:
ffmpeg -i input.mp4 -c:v libx265 -crf 28 output.mp4Command Breakdown:
-i input.mp4: Specifies the path to your input video file.-c:v libx265: Tells FFmpeg to encode the video stream using the H.265/HEVC encoder.-crf 28: Sets the Constant Rate Factor.output.mp4: The name of the resulting compressed file.
Understanding the H.265 CRF Scale
The H.265 CRF scale ranges from 0 to 51: * 0: Completely lossless (produces extremely large file sizes). * 51: The worst possible quality with maximum compression.
Unlike H.264, where the default CRF is 23, the default CRF for H.265 is 28. Because H.265 is a more efficient compression standard, an H.265 CRF of 28 offers visually similar quality to an H.264 CRF of 23, while significantly reducing the file size.
Recommended CRF Values for H.265:
- 18 to 21: Visually lossless. Use this range if you need high-quality archiving and do not mind larger file sizes.
- 22 to 25: Excellent quality. Ideal for high-definition video where file size is still a consideration.
- 26 to 29: Great balance of quality and size (Default range). This is the sweet spot for web streaming and general storage.
- 30+: Significant quality degradation. Only use this if file size is your absolute highest priority.
Improving Compression with Encoder Presets
When using CRF, you should always pair it with the
-preset flag. Presets control the trade-off between
encoding speed and compression efficiency.
ffmpeg -i input.mp4 -c:v libx265 -crf 23 -preset slow output.mp4Available presets include: ultrafast,
superfast, veryfast, faster,
fast, medium (default), slow,
slower, and veryslow.
Choosing a slower preset (like slow or
slower) will not change the quality of the CRF output, but
it will result in a significantly smaller file size at the cost of
longer encoding times.