Skip to main content

x264

x264 is a software library and command line application for encoding H.264 / AVC developed by VideoLAN, the people behind the ever-popular VLC Media Player and released under GNU GPL. It is written in C and Assembly with almost two decades worth of development and threading optimizations which makes it the fastest software video encoder available, which also happens to be extremely popular.

x264 has great fine detail retention which makes it perfect for high fidelity content.

FFmpeg​

x264 is available in FFmpeg via libx264, to check if you have it, run ffmpeg -h encoder=libx264. You can input non-FFmpeg standard x264 parameters via -x264-params.

Supported Color Space​

x264 supports the following color spaces:

FormatChroma SubsamplingSupported Bit Depth(s)
YUV420P4:2:08-bit
YUVJ420P4:2:08-bit (Full range)
YUV422P4:2:28-bit
YUVJ422P4:2:28-bit (Full range)
YUV444P4:4:48-bit
YUVJ444P4:4:48-bit (Full range)
NV12Semi-planar8-bit
NV16Semi-planar8-bit
NV21Semi-planar8-bit (reversed)
GRAY8-8-bit
YUV420P10LE4:2:010-bit
YUV422P10LE4:2:210-bit
YUV444P10LE4:4:410-bit
GBRP10LE-10-bit
GRAY10LE-10-bit

Installation​

Pre-built binary [Recommended]:

Choose your operating system there, or you can try using your package manager.

Usage​

x264 has been praised for its simple, no-fuss settings.

Here are some examples:

Simple raw Y4M input with CRF 20 and raw 264 bitstream output
x264 --crf 20 -o output.264 input.y4m
Preset slow, CRF 20, Y4M input
x264 --preset slow --crf 20 -o output.264 input.y4m

These next couple of examples utilize FFmpeg to pipe video into x264.

FFmpeg piping
ffmpeg -v error -i input.mkv -f yuv4mpegpipe -strict -1 - | x264 --preset slow --crf 20 --demux y4m - -o output.264
FFmpeg piping, MKV output
ffmpeg -v error -i input.mkv -f yuv4mpegpipe -strict -1 - | x264 --preset slow --crf 20 --demux y4m - -o output.mkv
Output Containers

x264 can output 264, MKV, FLV (Flash Video), and MP4 (If compiled with GPAC or L-SMASH support). For more information about what containers are, see the "Terminology" section on containers.

caution

x264 will use Haali Matroska Muxer for MKV outputs, which has seeking issues. It is recommended to remux back using FFmpeg or mkvmerge/MKVToolNix.

Recommendations​

As x264 is made to "just work", there aren't many advanced parameters to modify. The general guideline is to encode as slowly as you can tolerate.

Preset​

--preset veryslow

The most obvious way to increase fidelity per bit is to allow the encoder to spend more effort, and therefore time, encoding. This preset is decently slow, but preset placebo is even slower.

Threads​

--threads X

It is recommended to increase this value to your CPU's thread count. In most cases, x264 should be able to completely saturate most consumer CPUs.

Open GOP​

--open-gop

Enables Open GOP (Group of Pictures), where each GOP can reference one another, thus improving compression with little speed loss. For unknown reasons it is disabled by default in x264.

AQ Mode​

--aq-mode 3

In short, will make x264 bias to dark areas and spend more bitrate there, thus dark scenes will look less bad. Basically no speed loss.

Reference Frames​

--bframes 8 --ref 12

These parameters are responsible for the amount of reference frames x264 will use for compression, the more the better. Maximum of 16, will definitely increase compute time the higher you go.

MB Tree​

--no-mbtree

This option disables mb-tree rate control. Many claim that mb-tree rate control only assists in x264's ability to score well on metrics like PSNR, and may harm visual quality.

Lossless Encoding​

x264 can also encode lossless video, allowing it to compete with lossless video codecs like FFV1 and UT Video. To encode lossless video, use --qp 0. Slower presets will decrease the size even further while the video remains lossless.

Keep in mind that lossless H.264 can be very difficult to decode if you do not use the ultrafast preset, so it may be worth passing the --tune fastdecode parameter to ensure faster decoding.

Why QP 0 instead of CRF 0?

CRF automatically adjusts a number of quantization parameters to achieve a desired quality output. QP stands for Quantization Parameter, and allows full control over the resulting video quality. In this case, all-intra or lossless, using QP is necesasry, but for lossy encoding CRF will produce better visual fidelity per bit.