Version 9 (modified by llogan, 8 years ago) (diff)

remove step 2.5 from CRF section; clarify 8-bit info

FFmpeg and x264 Encoding Guide

x264 is a H.264/MPEG-4 AVC encoder. The goal of this guide is to inform new users how to create a high-quality H.264 video.

There are two most popular methods that are useful for general use: Constant Rate Factor (CRF) rate control and Two-Pass Encoding. Explained below.

First you must choose your ratecontrol. A ratecontrol is a method that will decide how many bits will be used for each frame. This will determine the file size and also how quality is distributed.

CRF is basically a "keep this same constant quality always" setting. 0 is considered lossless, 51 is worst quality. You can either set this value, or set a bit rate with one of the other methods, not both. This is easy to use for beginners--just figure out how much quality you want, then record using this setting. It has good results even with just 1 pass.

The default is ABR (Average Bit Rate). Like

ffmpeg -i input -vcodec libx264 -b 1000k ...

This is something of a "running average" that allows for swings above and below this number, with the end goal that the final file will match this number "on average" (so basically, if it gets a lot of black frames, which cost very little, the next few seconds of non-black frames it will encode at very high quality, to bring the average back in line). Using 2-pass can help this method to be more effective. You can also use this in combination with a "max bit rate" setting in order to prevent some of the swing.

You can "fake" a Constant Bit Rate setting by tuning the parameters of the ABR, like

ffmpeg -i myfile.avi -vcodec libx264 -b 4000k -minrate 4000k -maxrate 4000k -bufsize 1835k out.m2v

bufsize is the "rate control buffer" so it will keep that "average" (4000k in this case) across every block of 1835k. So basically it is assumed that the receiver/end player will buffer that much data so it's ok to fluctuate within that time span.

Of course, if it's all just empty/black frames then it will still serve less than that many bits/s (but it will raise the quality level as much as it can to try to satisfy the requested parameters).

You can also "fake" a maximum bit rate by specifying both crf *and* maxrate settings, like

ffmpeg -i input -vcodec libx264 -crf 20 -maxrate 400k -bufsize 1835k

This will effectively "target" crf 20, but if the output exceeds 400k, it will degrade to something less than crf 20 in that case.

If you need help compiling and installing libx264 see one of our FFmpeg and x264 compiling guides.

Constant Rate Factor (CRF)

This method will allow you to choose a certain output quality for the whole file and output file size is of less importance. This provides maximum compression effectiveness with a single pass and each video gets the bitrate it needs. The downsides is that you can't tell it to get a specific filesize or not go over a specific size.

1. Choose a CRF value

The range of the quantizer scale is 0-51; where 0 is lossless, 23 is default, and 51 is worst possible. A lower value is a higher quality and a subjectively sane range is 18-28. Consider 18 to be visually lossless: it should look the same as the input but it isn't technically lossless. Increasing the CRF value +6 is roughly half the bitrate while -6 is roughly twice the bitrate. General usage is to choose the highest quality that still provides an acceptable quality. If the output looks good then try a higher value and if it looks bad then choose a lower value.

Note: The CRF quantizer scale mentioned applies to 8-bit x264 (10-bit x264 quantizer scale is 0-63). You can see what you are using with x264 --help listed under Output bit depth.

2. Choose a preset

A preset is a collection of options that will provide a certain encoding speed:compression ratio. A slower preset will provide better compression (compression is quality per filesize). General usage is to use the slowest preset that you have patience for. Current presets in descending order of speed are: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo. Ignore placebo as it is a joke and a waste of time (see FAQ), but useful for command lines that require a preset when none is actually needed.

You can also choose a tune and/or profile.

3. Continue encoding

Use these same settings for the rest of your videos if you are encoding more. This will ensure that they will all have the same quality.

CRF Example

ffmpeg -i input -c:v libx264 -preset slow -crf 22 -c:a copy output.mkv


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

(50 MB * 8192 [converts MB to kilobits]) / 600 seconds = ~683 kilobits/s total bitrate
683k - 128k (desired audio bitrate) = 555k video bitrate

Two-Pass Example

ffmpeg -i input -c:v libx264 -preset fast -b:v 555k -pass 1 -an -f mp4 - && \
ffmpeg -i input -c:v libx264 -preset fast -b:v 555k -pass 2 -c:a libfaac -b:a 128k output.mp4

As with CRF, choose the slowest preset you can tolerate.

Lossless H.264

You can use -qp 0 or -crf 0 to encode a lossless output. Despite being lossless, other parameters still matter. Only two of the presets are generally useful though: ultrafast or veryslow since either a fast encoding speed or best compression are usually the most important factors.

Lossless Example (fastest encoding)

ffmpeg -i input -c:v libx264 -preset ultrafast -qp 0 output.mkv

Lossless Example (best compression)

ffmpeg -i input -c:v libx264 -preset veryslow -qp 0 output.mkv

Overwriting default preset settings

You can overwrite default preset settings with the x264opts option or by using the libx264 private options (see libx264 AVOptions in ffmpeg -h). This is not recommended unless you know what you are doing. The presets were created by the x264 developers and tweaking values to get a better output is usually a waste of time.


ffmpeg -i input -c:v libx264 -preset slow -crf 22 -x264opts keyint=123:min-keyint=20 -c:a copy output.mkv


-x264opts vbv-bufsize=3600:vbv-maxrate=1800:crf=23

though these are somewhat redundant to other "standard" ffmepg settings, and can also be specified each separately.


Encode a random section instead of the whole video with the -ss and -t options to quickly get a general idea of what the output will look like.

  • -ss: Offset time from beginning. Value can be in seconds or HH:MM:SS format.
  • -t: Output duration. Value can be in seconds or HH:MM:SS format.


Will two-pass provide a better quality than CRF?


Why is placebo a waste of time?

It helps at most ~1% compared to the veryslow preset at the cost of a much higher encoding time. It's diminishing returns: veryslow helps about 3% compared to the slower preset, slower helps about 5% compared to the slow preset, and slow helps about 5-10% compared to the medium preset.

Why doesn't my lossless output look lossless?

Blame the rgb->yuv conversion.

Attachments (1)

Download all attachments as: .zip