Changes between Version 62 and Version 63 of Encode/H.264


Ignore:
Timestamp:
Oct 31, 2017, 11:32:09 AM (16 months ago)
Author:
slhck
Comment:

cleanup and formatting, remove DivX example, add encoding time chart

Legend:

Unmodified
Added
Removed
Modified
  • Encode/H.264

    v62 v63  
    55The goal of this guide is to inform new users how to create a high-quality H.264 video using the encoder x264.
    66
    7 There are two rate control modes that are usually suggested for general use: [#crf Constant Rate Factor (CRF)] or [#twopass Two-Pass ABR]. The rate control 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. To know more about what the different rate control modes do, see [http://slhck.info/video/2017/03/01/rate-control.html this post].
     7There are two rate control modes that are usually suggested for general use: [#crf Constant Rate Factor (CRF)] or [#twopass Two-Pass ABR]. Rate control decides how many bits will be used for each frame. This will determine the file size and also how quality is distributed. To know more about what the different rate control modes do, see [http://slhck.info/video/2017/03/01/rate-control.html this post].
    88
    99If you need help compiling and installing libx264 see one of our [[CompilationGuide|FFmpeg and x264 compiling guides]].
     
    1111== Constant Rate Factor (CRF) ==#crf
    1212
    13 This method allows the encoder to attempt to achieve a certain output quality for the whole file when output file size is of less importance. This provides maximum compression efficiency with a single pass. Each frame gets the bitrate it needs to keep the requested quality level. The downside is that you can't tell it to get a specific filesize or not go over a specific size or bitrate.
     13Use this mode if you want to keep the best quality and don't care about the file size.
     14
     15This method allows the encoder to attempt to achieve a certain output quality for the whole file when output file size is of less importance. This provides maximum compression efficiency with a single pass. By adjusting the so-called quantizer for each frame, it gets the bitrate it needs to keep the requested quality level. The downside is that you can't tell it to get a specific filesize or not go over a specific size or bitrate, which means that this method is not recommended for encoding videos for streaming over the Internet.
    1416
    1517=== 1. Choose a CRF value ===
    16 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.
    17 
    18 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.
    19 
    20 {{{
    21 #!div style="border: 1pt dotted; margin: 1em; background-color: #fffff9;"
    22 '''Note:''' The CRF quantizer scale mentioned on this page only applies to 8-bit x264 (10-bit x264 quantizer scale is 0-63). You can see what you are using by referring to the `ffmpeg` console output during encoding (`yuv420p` or similar for 8-bit, and `yuv420p10le` or similar for 10-bit). 8-bit is more common among distributors.
    23 }}}
    24 
    25 
    26 === 2. Choose a preset ===
     18The range of the CRF scale is 0–51, where 0 is lossless, 23 is the default for x264, and 51 is worst quality possible. A lower value generally leads to 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.
     19
     20The range is exponential, so increasing the CRF value +6 results in roughly half the bitrate / file size, while -6 leads to roughly twice the bitrate.
     21
     22Choose the highest CRF value that still provides an acceptable quality. If the output looks good, then try a higher value. If it looks bad, choose a lower value.
     23
     24{{{
     25#!div style="border: 1px solid #e5e5c7; margin: 1em; background-color: #ffd;"
     26'''Note:''' The 0–51 CRF quantizer scale mentioned on this page only applies to 8-Bit x264. When compiled with 10-Bit support, x264's quantizer scale is 0–63. You can see what you are using by referring to the `ffmpeg` console output during encoding (`yuv420p` or similar for 8-Bit, and `yuv420p10le` or similar for 10-Bit). 8-Bit is more common among distributors.
     27}}}
     28
     29
     30=== 2. Choose a preset and tune ===
    2731
    2832A 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.
    2933
    30 The general guideline 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`. The default preset is `medium`. Ignore `placebo` as it is not useful (see [#FAQ FAQ]). You can see a list of current presets with `-preset help` (see example below), and what settings they apply with `x264 --fullhelp`.
    31 
    32 You can optionally use `-tune` to change settings based upon the specifics of your input. Current tunings include: `film`, `animation`, `grain`, `stillimage`, `psnr`, `ssim`, `fastdecode`, `zerolatency`. For example, if your input is animation then use the `animation` tuning, or if you want to preserve grain then use the `grain` tuning. If you are unsure of what to use or your input does not match any of tunings then omit the `-tune` option. You can see a list of current tunings with `-tune help`, and what settings they apply with `x264 --fullhelp`.
     34Use the slowest preset that you have patience for. The available presets in descending order of speed are:
     35
     36* `ultrafast`
     37* `superfast`
     38* `veryfast`
     39* `faster`
     40* `fast`
     41* `medium` – default preset
     42* `slow`
     43* `slower`
     44* `veryslow`
     45* `placebo` – ignore this as it is not useful (see [#FAQ FAQ])
     46
     47You can see a list of current presets with `-preset help` (see example below). If you have the `x264` binary installed, you can also see the exact settings these presets apply by running `x264 --fullhelp`.
     48
     49You can optionally use `-tune` to change settings based upon the specifics of your input. Current tunings include:
     50
     51* `film` – use for high quality movie content; lowers deblocking
     52* `animation` – good for cartoons; uses higher deblocking and more reference frames
     53* `grain` – preserves the grain structure in old, grainy film material
     54* `stillimage` – good for slideshow-like content
     55* `fastdecode` – allows faster decoding by disabling certain filters
     56* `zerolatency` – good for fast encoding and low-latency streaming
     57* `psnr` – ignore this as it is only used for codec development
     58* `ssim` – ignore this as it is only used for codec development
     59
     60For example, if your input is animation then use the `animation` tuning, or if you want to preserve grain in a film then use the `grain` tuning. If you are unsure of what to use or your input does not match any of tunings then omit the `-tune` option. You can see a list of current tunings with `-tune help`, and what settings they apply with `x264 --fullhelp`.
    3361
    3462Another optional setting is `-profile:v` which will limit the output to a specific H.264 profile. This can generally be omitted unless the target device only supports a certain profile (see [#Compatibility Compatibility]). Current profiles include: `baseline`, `main`, `high`, `high10`, `high422`, `high444`. Note that usage of `-profile:v` is incompatible with lossless encoding.
    3563
    36 To list all possible internal preset and tunes:
    37 
    38 {{{
    39 ffmpeg -f lavfi -i nullsrc -c:v libx264 -preset help -f mp4 -
    40 }}}
    41 
    42 {{{
    43 #!div style="border: 1pt dotted; margin: 1em; background-color: #fffff9;"
     64To list all possible internal presets and tunes:
     65
     66{{{
     67ffmpeg -hide_banner -f lavfi -i nullsrc -c:v libx264 -preset help -f mp4 -
     68}}}
     69
     70{{{
     71#!div style="border: 1px solid #e5e5c7; margin: 1em; background-color: #ffd;"
    4472'''Note:''' Windows users may need to use `NUL` instead of `-` as the output.
    4573}}}
    4674
    47 === 3. Use your settings ===
    48 
    49 Once you've chosen your settings apply them for the rest of your videos if you are encoding more. This will ensure that they will all have similar quality.
     75If you are encoding a set of videos that are similar, apply the same settings to all these videos. This will ensure that they will all have similar quality.
    5076
    5177=== CRF Example ===
    5278
    53 {{{
    54 ffmpeg -i input -c:v libx264 -preset slow -crf 22 -c:a copy output.mkv
     79This command encodes a video with good quality, using slower preset to achieve better compression:
     80
     81{{{
     82ffmpeg -i input.avi -c:v libx264 -preset slow -crf 22 -c:a copy output.mkv
    5583}}}
    5684
     
    6189== Two-Pass ==#twopass
    6290
    63 This method is generally used if you are targeting a specific output file size and output quality from frame to frame is of less importance. This is best explained with an example. Your video is 10 minutes (600 seconds) long and an output of 200 MiB is desired. Since `bitrate = file size / duration`:
     91Use this method if you are targeting a specific output file size, and if output quality from frame to frame is of less importance. This is best explained with an example. Your video is 10 minutes (600 seconds) long and an output of 200 MiB is desired. Since `bitrate = file size / duration`:
    6492
    6593{{{
     
    72100=== Two-Pass Example ===
    73101
    74 {{{
    75 ffmpeg -y -i input -c:v libx264 -preset medium -b:v 2600k -pass 1 -c:a aac -b:a 128k -f mp4 /dev/null && \
    76 ffmpeg -i input -c:v libx264 -preset medium -b:v 2600k -pass 2 -c:a aac -b:a 128k output.mp4
    77 }}}
    78 
    79 {{{
    80 #!div style="border: 1pt dotted; margin: 1em; background-color: #fffff9;"
    81 '''Note:''' Windows users should use `NUL` instead of `/dev/null`.
    82 }}}
    83 
    84 As with CRF, choose the slowest preset you can tolerate.
    85 
    86 In pass 1 specify a output format with `-f` that matches the output format in pass 2. Also in pass 1, specify the audio codec used in pass 2; in many cases `-an` in pass 1 will not work.
    87 
    88 {{{#!comment
    89 In which cases is the above true?
    90 }}}
    91 
    92 See [http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-dvd-mpeg4.html Making a high quality MPEG-4 ("DivX") rip of a DVD movie]. It is an MEncoder guide, but it will give you an insight about how important it is to use two-pass when you want to efficiently use every bit when you're constrained with storage space.
     102For two-pass, you need to run `ffmpeg` twice, with almost the same settings, except for:
     103
     104* In pass 1 and 2, use the `-pass 1` and `-pass 2` options, respectively.
     105* In pass 1, output to a null file descriptor, not an actual file. (This will generate a logfile that ffmpeg needs for the second pass.)
     106* In pass 1, you need to specify an output format (with `-f`) that matches the output format you will use in pass 2.
     107* In pass 1, specify the audio codec used in pass 2; in many cases, `-an` in pass 1 will not work.
     108
     109For example:
     110
     111{{{
     112ffmpeg -y -i input -c:v libx264 -b:v 2600k -pass 1 -c:a aac -b:a 128k -f mp4 /dev/null && \
     113ffmpeg -i input -c:v libx264 -b:v 2600k -pass 2 -c:a aac -b:a 128k output.mp4
     114}}}
     115
     116{{{
     117#!div style="border: 1px solid #e5e5c7; margin: 1em; background-color: #ffd;"
     118'''Note:''' Windows users should use `NUL` instead of `/dev/null` and `^` instead of `\`.
     119}}}
     120
     121As with CRF, choose the slowest `-preset` you can tolerate, and optionally apply a `-tune` setting and `-profile:v`.
    93122
    94123----
     
    96125== Lossless H.264 ==
    97126
    98 You can use `-crf 0` to encode a lossless output. Two useful presets for this are `ultrafast` or `veryslow` since either a fast encoding speed or best compression are usually the most important factors.
    99 
    100 === Lossless Example (fastest encoding) ===
     127You can use `-crf 0` to create a lossless video. Two useful presets for this are `ultrafast` or `veryslow` since either a fast encoding speed or best compression are usually the most important factors.
     128
     129Fast encoding example:
    101130
    102131{{{
     
    104133}}}
    105134
    106 
    107 === Lossless Example (best compression) ===
     135Best compression example:
    108136
    109137{{{
     
    111139}}}
    112140
    113 Note that lossless output files will likely be huge, and most non-FFmpeg based players will not be able to decode lossless, so if compatibility or file size issues you should not use lossless. If you're looking for an output that is roughly "visually lossless" but not technically lossless use a `-crf` value of around 17 or 18 (you'll have to experiment to see which value is acceptable for you). It will likely be indistinguishable from the source and not result in a huge, possibly incompatible file like true lossless mode.
     141Note that lossless output files will likely be huge, and most non-FFmpeg based players will not be able to decode lossless. Therefore, if compatibility or file size are an issue, you should not use lossless.
     142
     143{{{
     144#!div style="border: 1px solid #c7e5c7; margin: 1em; background-color: #ddffdd;"
     145'''Tip:''' If you're looking for an output that is roughly "visually lossless" but not technically lossless, use a `-crf` value of around 17 or 18 (you'll have to experiment to see which value is acceptable for you). It will likely be indistinguishable from the source and not result in a huge, possibly incompatible file like true lossless mode.
     146}}}
    114147
    115148----
     
    117150== Overwriting default preset settings ==
    118151
    119 You can overwrite default preset settings with the `x264-params` option, or by using the libx264 private options (see `ffmpeg -h encoder=libx264`).  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.
     152While `-preset` chooses the best possible settings for you, you can overwrite these with the `x264-params` option, or by using the libx264 private options (see `ffmpeg -h encoder=libx264`).  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.
    120153
    121154Example:
     
    126159
    127160{{{
    128 #!div style="border: 1pt dotted; margin: 1em; background-color: #fffff9;"
    129 '''Note:''' The old option `x264opts` will be removed. Use `x264-params` instead.
     161#!div style="border: 1px solid #e5e5c7; margin: 1em; background-color: #ffd;"
     162'''Note:''' Do not use the option `x264opts`, as it will be removed. Use `x264-params` instead.
    130163}}}
    131164
     
    136169=== CBR (Constant Bit Rate) ===
    137170
    138 There is no native CBR mode, but you can "simulate" a constant bit rate setting by tuning the parameters of ABR:
     171There is no native or true CBR mode, but you can "simulate" a constant bit rate setting by tuning the parameters of a one-pass average bitrate encode:
    139172
    140173{{{
     
    142175}}}
    143176
    144 In the above example, `-bufsize` is the "rate control buffer" so it will enforce your requested "average" (1 MBit/s in this case) across each 2 MBit worth of video.  So basically it is assumed that the receiver/end player will buffer that much data so it's ok to fluctuate within that much.
    145 
    146 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.
     177In the above example, `-bufsize` is the "rate control buffer", so it will enforce your requested "average" (1 MBit/s in this case) across each 2 MBit worth of video.  Here it is assumed that the receiver / player will buffer that much data, meaning that a fluctuation within that range is acceptable.
     178
     179CBR encodes are usually inefficient if the video is easy to encode (e.g., empty or black frames).
    147180
    148181=== Constained encoding (VBV / maximum bit rate) ===
    149182
    150 You can also use `-crf` or `-b:v` with a maximum bit rate by specifying both  `-maxrate` and `-bufsize`:
     183Use this mode if you want to constrain the maximum bitrate used, or keep the stream's bitrate within certain bounds. This is particularly useful for online streaming, where the client expects a certain average bitrate, but you still want the encoder to adjust the bitrate per-frame.
     184
     185You can use `-crf` or `-b:v` with a maximum bit rate by specifying both  `-maxrate` and `-bufsize`:
    151186
    152187{{{
     
    163198}}}
    164199
    165 
    166 
    167200=== Low Latency ===
    168201
     
    173206==== All devices ====
    174207
    175 If you want your videos to have highest compatibility with older devices:
     208If you want your videos to have highest compatibility with older devices (e.g., old Android phones):
    176209
    177210{{{
     
    194227
    195228{{{
    196 #!div style="border: 1pt dotted; margin: 1em; background-color: #fffff9;"
     229#!div style="border: 1px solid #e5e5c7; margin: 1em; background-color: #ffd;"
    197230'''Note:'''  This table does not include any additional restrictions which may be required by your device.
    198231}}}
     
    232265=== Why is `placebo` a waste of time? ===
    233266
    234 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.
     267It helps at most ~1% in terms of quality, 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.
     268
     269=== How do the different presets influence encoding time? ===
     270
     271This depends on the source material, the target bitrate, and your hardware configuration. In general, the higher the bitrate, the more time needed for encoding.
     272
     273Here is an example that shows the (normalized) encoding time for a two-pass encode of a 1080p video:
     274
     275[[Image(encoding_time.png)]]
     276
     277Going from `medium` to `slow`, the time needed increases by about 30%. The same goes for going from `slow` to `slower`. `veryslow` needs about 45% more time compared to `slower`.
    235278
    236279=== Why doesn't my lossless output look lossless? ===
    237280
    238 Blame the RGB to YUV color space conversion. If you convert to yuv444 it should still be lossless (which is the default now).
     281Blame the RGB to YUV color space conversion. If you convert with `-pix_fmt yuv444p` it should still be lossless (which is the default now).
    239282
    240283=== Will a graphics card make x264 encode faster? ===