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.mp4

Command Breakdown:

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.

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.mp4

Available 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.