wiki:

Encode

/

H.265


Version 10 (modified by slhck, 3 weeks ago) (diff)

profiles cannot be set in x265

FFmpeg and H.265 Encoding Guide

H.265 (also known as HEVC) can offer 50–75% more compression efficiency compared to H.264 video, while retaining the same visual quality. ffmpeg has support for HEVC encoding using the x265 encoder.

Getting ffmpeg with libx265 support

ffmpeg needs to be built with the --enable-libx265 configuration flag and requires x265 to be installed on your system. The Compilation Guides show you how to do that.

You can also download a static build, all of which bundle libx265.

Encoding

Similar to x264, the x265 encoder has multiple rate control algorithms, including:

  • 1-pass target bitrate (by setting -b:v)
  • 2-pass target bitrate (see H.264#twopass)
  • Constant Rate Factor (CRF)

In this guide we are going to focus on CRF and Two-Pass encoding, as 1-pass target bitrate encoding is not recommended.

Constant Rate Factor (CRF)

Use this mode if you want to retain good visual quality and don't care about the exact bitrate or filesize of the encoded file. The mode works exactly the same as in x264, so please read the H.264 guide for more info.

As with x264, you need to make two choices:

  • Choose a CRF. The default is 28, and it should visually correspond to libx264 video at CRF 23, but result in about half the file size. Other than that, CRF works just like in x264.
  • Choose a preset. The default is medium. The preset determines how fast the encoding process will be – at the expense of compression efficiency. Put differently, if you choose ultrafast, the encoding process is going to run fast, but the file size will be larger when compared to medium. The visual quality will be the same. Valid presets are ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow and placebo.
  • Choose a tune. By default, this is disabled, and it is generally not required to set a tune option. x265 supports the following -tune options: psnr, ssim, grain, zerolatency, fastdecode. They are rexplained in the H.264 guide.

For example:

ffmpeg -i input -c:v libx265 -crf 28 -c:a aac -b:a 128k output.mp4

This example uses AAC audio at 128 kBit/s. This uses the ffmpeg-internal encoder, but under AAC you will find info about more options.

Two-Pass Encoding

This method is generally used if you are targeting a specific output file size and output quality from frame to frame is of less importance. This is best explained with an example. Your video is 10 minutes (600 seconds) long and an output of 200 MiB is desired. Since bitrate = file size / duration:

(200 MiB * 8192 [converts MiB to kBit]) / 600 seconds = ~2730 kBit/s total bitrate
2730 - 128 kBit/s (desired audio bitrate) = 2602 kBit/s video bitrate

You can also forgo the bitrate calculation if you already know what final (average) bitrate you need.

Two-Pass Example

For two-pass, you need to run ffmpeg twice, with almost the same settings, except for:

  • In pass 1 and 2, use the -x265-params pass=1 and -x265-params pass=2 options, respectively.
  • In pass 1, output to a null file descriptor, not an actual file. (This will generate a logfile that ffmpeg needs for the second pass.)
  • In pass 1, you need to specify an output format (with -f) that matches the output format you will use in pass 2.
  • In pass 1, specify the audio codec used in pass 2; in many cases, -an in pass 1 will not work.

For libx265, the -pass option (that you would use for libx264) is not applicable.

ffmpeg -y -i input -c:v libx265 -b:v 2600k -x265-params pass=1 -c:a aac -b:a 128k -f mp4 /dev/null && \
ffmpeg -i input -c:v libx265 -b:v 2600k -x265-params pass=2 -c:a aac -b:a 128k output.mp4

Note: Windows users should use NUL instead of /dev/null and ^ instead of \.

As with CRF, choose the slowest -preset you can tolerate, and optionally apply a -tune setting. Note that when using faster presets with the same target bitrate, the resulting quality will be lower and vice-versa.

Passing Options

Generally, options are passed to x265 with the -x265-params argument. For fine-tuning the encoding process, you can therefore pass any option that is listed in the x265 documentation. Keep in mind that fine-tuning any of the options is generally not necessary, unless you absolutely know what you need to change.

Setting Profiles

Currently, ffmpeg does not support setting profiles (main, high, ...) via the profile:v option, as libx264 does. However, the profile options can be set manually, as shown in this Super User post.

Further Info