Skip to content
systemcoding.dev
Media

Choose CRF and Preset Values for x264 and x265 in FFmpeg

Pick sensible -crf and -preset values for libx264 and libx265 in FFmpeg, understand the quality scale, and compare file sizes without guessing.

6 min read
Summarize in AI
Flat illustration of video frames passing through a codec, with the FFmpeg, VLC media player and GStreamer logos.

Two settings decide almost everything about an x264 or x265 encode: -crf and -preset. Beginners copy -crf 23 -preset medium from a tutorial and never learn what moving either number does. This post explains both for FFmpeg 7.1 with libx264 and libx265: what the CRF scale means, why x265 uses different numbers than x264, how the preset changes time and size without changing the quality target, and how to run a small test on your own footage instead of trusting a table.

What CRF means

CRF stands for constant rate factor. It is a quality target, not a bit rate. The encoder spends more bits on complex scenes (fast motion, grain) and fewer on simple ones (a still title card) to keep the perceived quality about the same. The file size is the outcome, and you cannot predict it before the run.

The scales, as documented in the FFmpeg H.264 and H.265 encoding guides:

EncoderCRF rangeDefaultVisually near-losslessCommon range
libx2640 to 5123about 1818 to 28
libx2650 to 5128about 2222 to 32

Lower is better quality and a bigger file. For x264, a change of 6 roughly doubles or halves the bit rate. The x265 scale is shifted: CRF 28 in x265 is intended to give about the same quality as CRF 23 in x264 at a smaller size, which is why its default is higher. Never reuse an x264 CRF value for x265 without adjusting; -crf 23 on libx265 makes a larger, higher-quality file than you probably wanted.

CRF 0 is lossless in x264 for 8-bit video. It makes huge files, and many players cannot decode the High 4:4:4 Predictive profile it requires, so it is rarely the right choice.

What the preset changes

The preset controls how much effort the encoder spends searching for efficient ways to code each block. From fastest to slowest:

Text
ultrafast superfast veryfast faster fast medium slow slower veryslow placebo

At the same -crf, a slower preset produces a smaller file at about the same quality, and takes longer. The quality target is the CRF; the preset changes the cost of reaching it. placebo is not worth using: the guide's own advice is that it gives a negligible gain for a huge time cost.

A practical reading:

PresetWhen to use it
ultrafast, superfastLive capture where the CPU is the limit; files are much larger
veryfast, faster, fastQuick drafts, previews, large batches on a deadline
mediumThe default; fine for most work
slow, slowerFinal delivery where size matters and you can wait
veryslowArchival, when time is free

Do not pair ultrafast with a high CRF and expect small files. The fast presets turn off tools that save bits.

The two commands

x264:

Shell
ffmpeg -i input.mov -c:v libx264 -crf 20 -preset slow -pix_fmt yuv420p -c:a aac -b:a 160k out264.mp4

x265:

Shell
ffmpeg -i input.mov -c:v libx265 -crf 26 -preset slow -pix_fmt yuv420p -tag:v hvc1 -c:a aac -b:a 160k out265.mp4

For HEVC in MP4, -tag:v hvc1 sets the codec tag that Apple's players expect. Without it, QuickTime and Safari may refuse the file, since FFmpeg's default tag for HEVC in MP4 is hev1. The x265 command also prints x265's own log lines. To quieten them, pass -x265-params log-level=error.

The tune option

-tune adjusts encoder decisions for a type of content. For x264 the useful values are film for live action, animation for cartoons, grain for grainy film, stillimage for slideshows, and zerolatency for streaming with minimum delay. Add it after the preset:

Shell
ffmpeg -i cartoon.mkv -c:v libx264 -crf 20 -preset slow -tune animation -c:a copy cartoon.mp4

The default, no tune, is fine for most things. Use animation for flat colours and grain when you want to keep film noise instead of smoothing it away.

Run a test on your footage

Quality tables are averages. Your footage may be noisier or calmer than the average, and the right CRF depends on it. Cut a representative thirty seconds and encode it at three CRF values:

Shell
ffmpeg -ss 120 -i input.mov -t 30 -an -c:v libx264 -preset slow -crf 18 test18.mp4
ffmpeg -ss 120 -i input.mov -t 30 -an -c:v libx264 -preset slow -crf 22 test22.mp4
ffmpeg -ss 120 -i input.mov -t 30 -an -c:v libx264 -preset slow -crf 26 test26.mp4

Then compare the sizes:

Shell
ls -l test18.mp4 test22.mp4 test26.mp4
Text
-rw-r--r--  1 user  staff  41220118 Nov 23 10:02 test18.mp4
-rw-r--r--  1 user  staff  24816342 Nov 23 10:03 test22.mp4
-rw-r--r--  1 user  staff  14602977 Nov 23 10:04 test26.mp4

These byte counts are illustrative; yours will differ with the footage. The pattern is what to look for: each step of 4 CRF cuts the size by roughly 40 percent in this example. Play the three clips on the device your viewers will use, and pick the highest CRF where you cannot see a difference. For an objective score instead of your eyes, use the VMAF post in this cluster.

Two-pass and constrained modes

CRF is a one-pass mode and the right default for files that live on disk. If you must hit a target file size or a maximum bit rate, such as for a platform upload limit or a streaming ladder, CRF does not fit. Two-pass average bit rate (-b:v with -pass 1 and -pass 2) and constrained CRF with -maxrate and -bufsize do. The constrained VBR post in this cluster covers the second.

Choosing between x264 and x265

x265 reaches the same quality at a lower bit rate, commonly by a large fraction, but it encodes much more slowly and plays on fewer devices. H.264 plays everywhere. For files you share widely, use x264. For files you keep for yourself and where space matters, x265 can pay off. The codec comparison post in this cluster weighs H.264, HEVC, VP9 and AV1.

Common mistakes

  • Using the x264 CRF number on x265.

  • Believing a slower preset gives better quality at the same CRF. It gives a smaller file; the quality target is the same.

  • Using -crf 0 for "lossless" and being surprised by a file ten times the size.

  • Forgetting -pix_fmt yuv420p, so the file will not play in browsers.

  • Forgetting -tag:v hvc1 on HEVC meant for Apple devices.

We settled on -crf 20 -preset slow for masters and -crf 23 -preset medium for drafts after the test above on our own screen recordings; screen content with flat colour tolerates a higher CRF than camera footage, and the test showed it.

Reading the encoder's own report

x264 prints a summary at the end of every run, and it tells you what the encoder actually did. The lines to look for are the average QP, the share of frame types, and the final bit rate. A typical block ends with lines such as kb/s:2329.40. If you ran in CRF mode, that bit rate is the answer to "how big did it get", and you can compare it between two runs on the same clip.

The frame-type line, with counts of I, P and B frames, shows the effect of the preset. A slow preset uses more B-frames and longer reference chains, and the counts show it. You do not need to understand every number. Compare the bit rate between two settings of one clip, keep the lower one that still looks right, and move on.

To make the summary appear you must not silence the log: do not use -loglevel quiet. Leave the default or use -loglevel info, and add -stats when running in a script so the progress line still prints.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.