Skip to content
Merged
83 changes: 53 additions & 30 deletions encoding/codecs/x264.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ Here's how we get a copy of the x264 encoder:

### Windows

TODO
Official Windows builds are available
[here](https://download.videolan.org/pub/x264/binaries/win64/).
For these examples,
you'll want the latest version
of the standard 8-bit build,
which is the version without "10b" (for 10-bit) in the name.

### Linux/macOS

Expand All @@ -35,7 +40,7 @@ Here are a few examples:

- **Ubuntu/Debian**: `sudo apt install x264`
- **Arch Linux**: `sudo pacman -S x264`
- **macOS**: `brew install x264`
- **macOS**: `brew install x264`https://download.videolan.org/pub/x264/binaries/win64/

## Getting Started

Expand All @@ -47,14 +52,16 @@ and understanding a few basic concepts.
We'll walk through those concepts
with the following examples.

## Example 1
## Example 1: General-Purpose Encoding

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

`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --tune animation --crf 24 -o x264output.mkv -`
```
vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --tune animation --crf 24 -o x264output.mkv -
```

Let's run through what each of these options means:

Expand All @@ -69,7 +76,7 @@ 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 x264, our encoder.
with `x264`, our encoder.

#### `--demuxer y4m`

Expand All @@ -83,18 +90,18 @@ x264 has a set of presets
to switch between faster encoding, or higher quality.
The full list of presets, from fastest to slowest, is:

- ultrafast
- superfast
- veryfast
- faster
- fast
- medium
- slow
- slower
- veryslow
- placebo

You will almost never want to use the very extreme settings,
1. ultrafast
1. superfast
1. veryfast
1. faster
1. fast
1. medium
1. slow
1. slower
1. veryslow
1. placebo

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.
Expand All @@ -104,7 +111,8 @@ we want a fast encode and have chosen `veryfast`.

For the curious,
you can see a full list of the settings enabled by each preset
by running `x264 --fullhelp | less`.
by running `x264 --fullhelp | less` (Linux/Mac)
or `x264 --fullhelp | more` (Windows).
However, this probably won't mean much at the moment.
Don't worry,
this guide will explain later
Expand Down Expand Up @@ -133,7 +141,7 @@ 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 filesize,
we just want the output to meet a certain quality level.
CRF ranges from 0 to 51,
CRF ranges from 0 to 51 (for 8-bit encoding),
with 0 being the best quality
and 51 being the smallest filesize,
but there is a certain range of CRF settings
Expand Down Expand Up @@ -166,11 +174,15 @@ We use `-o` to tell which filename to write the encoded file to.
In this case, x264 will write a file at `x264output.mkv`
in the current directory.

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

## Example 2
## Example 2: Targeted File Size

For the next example,
let's say we want to make sure our encode
Expand All @@ -186,23 +198,30 @@ This means we'll need to know a couple of things:
For this example,
let's say our movie is 2 hours (120 minutes) long.
We'll convert that to seconds:
120 \* 60 = **7200 seconds**.
120 minutes \* 60 minutes/second = **7200 seconds**.
- Our target filesize.
We know that this is 4.7GB,
but we need to convert it to kilobits.
We can do this with the following steps:
- 4.7GB \* 1024 = 4812.8MB
- 4812.8MB \* 1024 = 4928307.2KB (uppercase B = bytes)
- 4928307.2KB \* 8 = **39426457.6Kb** (lowercase b = bits)

```
4.7GiB * 1024MiB/GiB = 4812.8MiB
4812.8MiB * 1024KiB/MiB = 4928307.2KiB
4928307.2KiB * 8Kbits/KiB = 39426457.6Kbits
```

Now we divide the kilobit size we calculated by our video length,
to find our kilobit per second target bitrate:

39426457.6Kb / 7200 seconds = **5475 Kbps**
```
39426457.6Kbits / 7200 seconds = 5475 Kbps
```

And here's how we could add that to our x264 command:

`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --bitrate 5475 -o x264output.mkv -`
```
vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --bitrate 5475 -o x264output.mkv -
```

The `--bitrate` option, by itself,
says that we want to do a 1-pass, average-bitrate encode.
Expand All @@ -211,7 +230,7 @@ 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
## Example 3: 2-Pass Encoding

So far, we've only done 1-pass encodes.
While using CRF 1-pass is great
Expand All @@ -235,12 +254,16 @@ if you need to target a certain bitrate.

Here's how we would run our first pass:

`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --pass 1 --bitrate 5475 -o x264output.mkv -`
```
vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --pass 1 --bitrate 5475 -o x264output.mkv -
```

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

`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --pass 2 --bitrate 5475 -o x264output.mkv -`
```
vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --pass 2 --bitrate 5475 -o x264output.mkv -
```

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

Expand Down