Table of Contents

Ubuntu - Video - Encoding H.265/HEVC with FFmpeg

The FFmpeg wiki estimates that you should be able to produce video files of similar video quality as H.264 that are about half the size.

ffmpeg -c:v libx265 -preset fast -crf 28 -c:a aac -ac 2 -b:a 224k -i <source> <target>

Or for GPU-based encoding, which is much faster but produces a larger file size:

ffmpeg -vcodec hevc_videotoolbox -b:v 6000k -c:a aac -ac 2 -b:a 224k -i <source> <target>

crf 28 in H265 is the equivalent of crf 23 in H264.


CPU versus GPU

libx265 encodes via the CPU while hevc_videotoolbox uses the GPU.

libx265 produces a much smaller file size, but hevc_videotoolbox runs significantly faster (5 to 10 times the speed often).

For hevc_videotoolbox a target bitrate is specified with -b:v 6000k. Trying to get away without doing that, regardless of the selected video profile (main or main10), unless a higher bitrate was forced, resulted in the the output video quality being garbage; totally unwatchable, with visual artifacts and blurriness everywhere. Specifying a target video bitrate works around the problem, at the cost of producing an even larger final video.

The right compromise is to use libx265 for any videos that you want to keep around, but for videos that you’d prefer encoded quickly and will probably delete (say you want them for a single trip), hevc_videotoolbox is perfectly fine. Output videos are still small enough, and the significantly faster encoding speeds mean that FFmpeg finishes far more quickly.

If you’re going for quality/size, make sure you don’t enable any hardware acceleration.


Audio and Dolby Digital Plus

Have FFmpeg downmix to stereo AAC.

-c:a aac -ac 2

The `fast` preset

The libx265 preset setting accepts the wide array of adjectives ultrafast, superfast, veryfaster, faster, fast, medium, slow, slower, veryslow, and placebo. Specifying a faster setting means that encoding speed will be preferred over file size. Both ends of the spectrum have extreme diminishing returns.

There is generallyt very little file size difference between fast, medium, and slow, but some encoding speed difference, so I just default to -preset fast for everything.


Check

To check to see that the file has been encoded correctly, use the ffprobe command like this:

ffprobe video-h265.mp4

The output will show that the video stream (probably stream#0) is encoded using hvec, i.e. H.265.


References

https://trac.ffmpeg.org/wiki/Encode/H.265