# Encoding with x265

High Efficiency Video Coding (HEVC), also known as H.265 and MPEG-H Part 2, is a video compression standard designed as part of the MPEG-H project as a successor to the widely used Advanced Video Coding (AVC, H.264, or MPEG-4 Part 10). Comparing with H.264, HEVC offers up to 50% better data compression at the same level of video quality, or substantially improved video quality at the same bit rate. By 2017, most high-end smartphones and many mid-range devices had HEVC decoding capabilities built in, making it easier to play HEVC videos on a variety of devices.

x265 is an open-source software (codec) for encoding video in HEVC. Because of its better 10-bit color depth support, support for multiple HDR formats, and higher data compression rate compared to x264, x265 has become the mainstream encoding tool for fansubbers.

## Prerequisites

To get started, you’ll need two things:

• A video to encode.
For the examples, we will pipe in a video from VapourSynth, which you should be able to do if you’ve been following the previous sections of this guide.
• The x265 encoder.

It’s worth mentioning that there are several modified versions of x265 specifically improved for animation encoding.

These modifications may include some custom features and performance optimizations. They might also include several tune presets.

Since there is no official build of x265, it is highly recommended to download HandBrake, which is an open-source video transcoder with several built-in codecs and an easy-to-use user interface.

If you want to build your own standalone x265, check the official git repository, all build scripts are provided. For those modified versions, please check their own sites.

## Getting Started

The following contents assume you already have a standalone build of x265, and already added it into your system PATH.

Like x264, x265 is also very configurable and the options may seem overwhelming, too, but you can get started encoding by using the presets x265 provides and understanding a few basic concepts. Check the x265 CLI documentation for the full list of CLI options.

We’ll walk through those concepts with the following examples.

### Example 1: General-Purpose Encoding

Open up a terminal window, and navigate to the folder where your VapourSynth script lives. Let’s use the following command:

vspipe -c y4m myvideo.vpy - | x265 --y4m --profile main10 --preset veryfast --tune animation --crf 20 -o x265output.mkv -


We’ll run through what each of these options means:

#### vspipe -c y4m myvideo.vpy -

This portion loads your VapourSynth script and pipes it to stdout, adding y4m headers that x265 can decode. If you use Linux, you’re probably familiar with how piping works. If you’re not, it’s basically a way of chaining two commands together. In this case, we want to chain vspipe, the program that reads VapourSynth scripts, with x265, our encoder.

#### --y4m

This tells x265 that we’re providing it with a y4m (YUV4MPEG2) file. This matches up with the --y4m flag that we gave to the vspipe command.

#### --profile main10

Enforce the requirements of the specified profile, ensuring the output stream will be decodable by a decoder which supports that profile.

The following profiles are supported in x265 at the time of writing (2023-02-12).

8-bit profiles:

• main, main-intra, mainstillpicture (or msp for short)
• main444-8, main444-intra, main444-stillpicture

10-bit profiles:

• main10, main10-intra
• main422-10, main422-10-intra
• main444-10, main444-10-intra

12-bit profiles:

• main12, main12-intra
• main422-12, main422-12-intra
• main444-12, main444-12-intra

“Intra” means “intra-frame coding”, which is a data compression technique that predicts the values of a particular frame based on the values of the same frame.

#### --preset veryfast

x265 has a set of presets to switch between faster encoding or higher quality. The full list of presets, from fastest to slowest, is:

1. ultrafast
2. superfast
3. veryfast
4. faster
5. fast
6. medium (default)
7. slow
8. slower
9. veryslow
10. placebo

Different presets determine different parameters. Check the official doc for detailed information.

You will almost never want to use the extreme settings, but generally, if you want good quality and don’t care about how long the encode takes, slower or veryslow are recommended. In this example, because we are just demonstrating how x265 works, we want a fast encode and have chosen veryfast.

#### --tune animation

Beyond the preset chosen, x265 allows us to further tune the encoding settings for the type of content we’re working with. The following tunings are generally the most useful:

• film: Recommended for live action videos.
• animation: Recommended for anime or cartoons with flat textures. For 3D animation (e.g. Pixar movies), you may find better results with film.

You don’t need to use a tuning, but it generally helps to produce a better-looking video.

#### --crf 20

Constant Rate Factor (CRF) is a constant-quality, 1-pass encoding mode. In layman’s terms, this means that we don’t need the output to meet a specific file size, we just want the output to meet a certain quality level. CRF ranges from 0 to 51, with 0 being the best quality (lossless) and 51 being the smallest file size, but there is a certain range of CRF settings that are generally most useful. For high quality anime encoding, at least 18.0 is recommended.

#### -o x265output.mkv -

This last portion tells which files to use for the input and output. We use -o to tell which filename to write the encoded file to. In this case, x265 will write a file at x265output.mkv in the current directory.

The last argument we are passing to x265 is the input file. In this case, we pass - for the input file, which tells x265 to use the piped output from vspipe. The input argument is the only positional argument, so it does not need to be last; x265 will recognize it as the only argument without a - marker before it.

### Example 2: Targeted File Size

For the next example, let’s say we want to make sure our encode fits onto a single 4.7GB DVD1. How would we do that in x265?

First, we’ll need to figure out what bitrate our encode should be, in kilobits per second. Everything you need to know is listed in the x264 documentation.

And here’s how we could add that to our x265 command:

vspipe -c y4m myvideo.vpy - | x265 --y4m --preset veryfast --bitrate 5222 -o x265output.mkv -


The --bitrate option, by itself, says that we want to do a 1-pass, average-bitrate encode. In other words, the encoder will still give more bits to sections of the video that have more detail or motion but the average bitrate of the video will be close to what we requested.

### Example 3: 2-Pass Encoding

So far, we’ve only done 1-pass encodes. While using CRF 1-pass is great when you don’t have a target bitrate, it’s recommended not to use 1-pass for targeted-bitrate encodes, because the encoder can’t know what’s coming ahead of the current section of video. This means it can’t make good decisions about what parts of the video need the most bitrate.

How do we fix this? x265 supports what is known as 2-pass encoding. In 2-pass mode, x265 runs through the video twice, the first time analyzing it to determine where to place keyframes and which sections of video need the most bitrate, and the second time performing the actual encode. 2-pass mode is highly recommended if you need to target a certain bitrate.

Here’s how we would run our first pass:

vspipe -c y4m myvideo.vpy - | x265 --y4m --preset veryfast --pass 1 --bitrate 5222 -o x265output.mkv -


This creates a stats file in our current directory, which x265 will use in the second pass:

vspipe -c y4m myvideo.vpy - | x265 --y4m --preset veryfast --pass 2 --bitrate 5222 -o x265output.mkv -


You’ll notice all we had to change was --pass 1 to --pass 2. Simple!

Although x265 will automatically use faster settings for the first pass, it should be no surprise that 2-pass encoding is slower than 1-pass encoding. Therefore, there are still certain use cases where 1-pass, bitrate-targeted video is a good fit, such as streaming.

### Recap

We covered the basics of how to encode in x265, including speed presets, tunings, and three different encoding modes.

Here is a summary of when to use each encoding mode:

• 1-pass Constant Quality (CRF):
• Good for: General-purpose encoding
• Bad for: Streaming; obtaining a certain file size
• 1-pass Average Bitrate:
• Good for: Streaming
• Bad for: Everything else
• 2-pass Average Bitrate:
• Good for: Obtaining a certain file size
• Bad for: Streaming

Coming Soon.