This is an old revision of the document!


x264

When encoding, if you want to cover all your bases, there are two H.264 settings to use, and two x264 encoding arguments to pass:

  • H.264 profile: high
  • H.264 level: 4.1
  • x264 preset: medium
  • x264 CRF: 23

Optionally, you can set the x264 tuning as well. Best choices are film, animation, and grain.

H.264 levels

DVDs max out at high 3.1. You don't need 3.2 on DVDs because that is the first level that accepts 720p.

Blu-ray at high 4.1. You can go higher if you want, it's your preference. :)

The levels (baseline, main, high) are used to determine which hardware you want to target, and influences what quality you can expect and what encoding features will be used. Blu-ray uses high profile.

h264 levels: comparison

  • “Main” adds
    • CABAC Entropy Coding (link)
    • Interlaced Coding (PicAFF, MBAFF)
    • B slices
  • “High” adds
    • 4:0:0 Monochrome
    • 8×8 vs 4×4 Transform Adaptivity
    • Quantization Scaling Matrices
    • Separate Cb and Cr QP control

mtune

Keep it simple (for DVDs): use film or animation. For old movies or sources that have artifacts already, use grain or you risk making it even worse.

Choosing an x264 preset

The presets are added to x264 encoder by developers to help you determine what filesize you want.

From ffmpeg's H264 guide: “A preset is a collection of options that will provide a certain encoding speed to compression ratio. A slower preset will provide better compression (compression is quality per filesize). This means that, for example, if you target a certain file size or constant bit rate, you will achieve better quality with a slower preset. Similarly, for constant quality encoding, you will simply save bitrate by choosing a slower preset. The general guideline is to use the slowest preset that you have patience for.”

Short answer: just use medium, and tweak settings somewhere else.

CRF

Changing the CRF level will determine the level of video quality for your encodes. The default is 23. If you think it's not good enough quality, bump it down by one or two, until you find what you like.

“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 or nearly so: it should look the same or nearly the same as the input but it isn't technically lossless. The range is exponential, so increasing the CRF value +6 is roughly half the bitrate while -6 is roughly twice the bitrate. General usage is to choose the highest CRF value 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”

Two-pass encoding

Use a two-pass encode if you want to meet a specific filesize or constant bitrate.

preset comparison gains

  • medium to slow, 5 to 10% quality gain
  • slow to slower, 5%
  • slower to veryslow, 3%
  • veryslow to placebo, 1%

Animation

“All of this combines to make animation at first glance deceptively easy–but in reality quite difficult–to encode.” So true!

His tests on x264

x264 (r1206)
Video format: H.264/AVC High Profile
Settings: –preset placebo –tune ssim –rc-lookahead 250, two-pass

Note: Don't use animation tune on CGI.

motion estimation comparison

See this forum post on an excellent visual comparison between mostion estimation settings.

For quick reference, here's what the x264 presets use:

  • diamond (dia): ultrafast, superfast
  • hexagon (hex): veryfast, faster, fast, medium
  • uneven multi-hexagon (umh): slow, slower, veryslow
  • exhaustive (tesa): placebo

Steve's Defaults

Here are examples of using H.264 high profile at level 4.1 (Blu-ray settings), with x264 CRF 23 on medium preset.

Using MP4 as the container will encode the audio to AAC by default. If using MKV, it depends on the encoder, so I'm specifying it directly.

libav (whose syntax I find easier to remember), see also their x264 guide:

avconv -i source.mpg -vcodec libx264 -profile:v high -level 41 -preset medium -crf 23 -c:a libfdk_aac video.mp4
avconv -i source.mpg -vcodec libx264 -profile:v high -level 41 -preset medium -crf 23 -c:a libfdk_aac video.mkv
ffmpeg -i source.mpg -c:v libx264 -profile:v high -level 4.1 -crf 23 -c:a libfdk_aac video.mp4
ffmpeg -i source.mpg -c:v libx264 -profile:v high -level 4.1 -crf 23 -c:a libfdk_aac video.mkv

Navigation