Skip to content

Instantly share code, notes, and snippets.

@dvaupel
Last active February 5, 2022 16:00
Show Gist options
  • Save dvaupel/9bb532715d5167239487bdc93bb1de2d to your computer and use it in GitHub Desktop.
Save dvaupel/9bb532715d5167239487bdc93bb1de2d to your computer and use it in GitHub Desktop.
x265 Encoding Guide

x265 Encoding Guide

Rules of thumb for common/useful parameters. Personal notes, maybe useful for others.

Official documentation: https://x265.readthedocs.io/en/master/

FFmpeg x265 guide: https://trac.ffmpeg.org/wiki/Encode/H.265

General

The open source HEVC encoder x265 is more or less stable and can be used comfortably with ffmpeg.

ffmpeg -i input.mp4 -c:v libx265 ... out.mkv

Show all options for which ffmpeg has dedicated parameters:

ffmpeg -h encoder=libx265

All other options of the standalone x265 encoder are available in ffmpeg as well, you just have to wrap them in the -x265_params parameter. E.g.:

ffmpeg ... -vcodec libx265 -x265-params 'option1=foo:option2=bar' ...

Parameters

--aq-mode {0-4} and --aq-strength {0.0-3.0}

x265 divides the source pictures into smaller areas and compresses them according to their complexity (adaptive quantization). The danger is that the encoder might give a seemingly simple area so few bits that the area becomes blocky and pixelated.

To reduce these artifacts, especially in realistic movies with dark and detailed scenes, one should use aq-mode=3. This makes the encoder spend more bits on flat areas.

The adaptive quantization can be further customized by setting --aq-strength. Default is 1.0. Reducing will lead to more even quantization (e.g. subtle dark scenes). Increasing leads to more uneven quantization.

--bframes {0-16}

Maximum number of consecutive b-frames in a GOP. Default is 4.

For videos with many static scenes (anime, dramas, whatever) higher values can slightly reduce the file size, but slightly increases encoding time and RAM usage.

Increase to something like 8 if there are many still scenes1.

-crf {0-51}

Constant rate factor, the main parameter to control the visual quality of the encode. Lower values lead to better quality, but bigger files. Default is 28.

For high quality encodes, CRFs between 15 and 23 are recommended2. Visual transparency is often achieved at around 18.

Note: Constant quality encoding is the default and does not profit from two-pass encoding (unlike variable bitrate encoding).

--limit-sao / --no-sao

SAO stands for the Sample Adaptive Offset loop filter. This technique that can reduce file size by a few percent. It removes ringing and noise to some extent, but also destroys some detail3.

Since the filter is helpful or not depending on source material, x265 offers switches to regulate the filter strength.

Rules of thumb:

  • on (--sao, default): Simple content, flat animation.
  • limited (--limit-sao): Light film grain, detail preservation is important. (todo: limits the execution time/complexity of the filter?)
  • off (--no-sao): Heavy film grain, noise.

-pix_fmt {yuv420, yuv420p10le, ...}

x265 supports bit depths of 8 to 16.

Encoding in 10 bit (yuv420p10le) is generally recommended, because it reduces color banding and other artifacts.

-preset {0-9}

The speed of the encoding process. Higher values mean quicker encoding, but worse quality. Presets are usually adressed by their corresponding names like fast, medium, slow etc. Default is medium.

For high-quality encodes, use medium or below. slow (6) is a sweet spot in regards to speed/quality. The slower the better, use whatever speed you are willing to tolerate.

The preset also effects the output file size. Slower presets lead to bigger files with better quality. In other words: The slower the encoder runs, the more details it carries over in the output and the more it tries to optimize the compression.

--psy-rd {0.0-5.0} and --psy-rdoq {0.0-50.0}

These two parameters are difficult. During the encoding, the x265 encoder blurs the input to compress it more efficienty and save bitrate. This means that high frequency noise ("grain") and fine details are lost to some degree. To limit the smoothing and increase the perceived quality, these psycho-visual options were introduced.

The higher --psy-rd, the more details are preserved from the source images. A typical value is 1.0. If the input is grainy or very detailed, try 2.0. But be careful, the bitrate will increase noticably and if the --psy-rd is too high, the encoder will globally increase (worsen) the CRF.

The higher psy-rdoq, the more the encoder will favour general roughness/graininess (visual energy). To better preserve light/medium film grain and similar effects, increase from default value (1.0) to around 2.0-5.0. For strong film grain, try 10.0 to 15.0. Higher values make the grain look coarse and crisp.

You have to experiment to find optimal values. The higher the CRF/average bitrate is, the higher you can go with both of these values without introducing artifacts.

Example commands

Action movie with heavy grain:

ffmpeg -i <input> -c:v libx265 -pix_fmt yuv420p10le -crf 20  -preset slow \ 
  -x265-params "aq-mode=3:no-sao:psy-rd=2.0:psy-rdoq=15" <output>

Support

If this guide was helpful to you or saved you some time, consider a donation:

Monero: 86eX2CjendoWcMdSeXEJp5LdsSzPFRY15Tf3VZ6B3CrpJhCEnPHG8z28FYxJWd5ScoRawNAfH5KKmeJn7X1sq8x9T6cfU74

Footnotes

  1. https://forum.doom9.org/showthread.php?p=1947883#post1947883

  2. https://www.reddit.com/r/TopazLabs/comments/s2qcz6/beginners_guide_to_x265_ffmpeg_encoding_and/?utm_source=share&utm_medium=web2x&context=3

  3. https://amefs.net/en/archives/1470.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment