Changes between Version 64 and Version 65 of Encode/H.264


Ignore:
Timestamp:
Nov 1, 2017, 2:53:26 AM (3 weeks ago)
Author:
llogan
Comment:

HTMLize title to free usage of big heading in content (not sure why trac uses px for headings). Misc minor changes.

Legend:

Unmodified
Added
Removed
Modified
  • Encode/H.264

    v64 v65  
    1 = FFmpeg and H.264 Encoding Guide = 
    2  
    3 [[PageOutline(2, Contents)]] 
    4  
    5 The goal of this guide is to inform new users how to create a high-quality H.264 video using the encoder x264. 
    6  
    7 There 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]. 
    8  
    9 If you need help compiling and installing libx264 see one of our [[CompilationGuide|FFmpeg and x264 compiling guides]]. 
    10  
    11 == Constant Rate Factor (CRF) ==#crf 
     1{{{ 
     2#!html 
     3<h1 style="font-size: 22px;"> 
     4H.264 Video Encoding Guide 
     5</h1> 
     6}}} 
     7 
     8[[PageOutline(1, Contents)]] 
     9 
     10The goal of this H.264 video encoding guide is to inform new users how to create a high-quality video using the encoder x264. 
     11 
     12There 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]. 
     13 
     14If you need help compiling and installing libx264 see one of our [[CompilationGuide|compiling guides]]. 
     15 
     16---- 
     17 
     18= Constant Rate Factor (CRF) =#crf 
    1219 
    1320Use this mode if you want to keep the best quality and don't care about the file size. 
    1421 
    15 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. 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. 
    16  
    17 === 1. Choose a CRF value === 
    18 The 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. 
     22This 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. 
     23 
     24== 1. Choose a CRF value == 
     25The range of the CRF scale is 0–51, where 0 is lossless, 23 is the default, and 51 is worst quality possible. A lower value generally leads to higher quality, and a subjectively sane range is 17–28. Consider 17 or 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. 
    1926 
    2027The 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. 
     
    2431{{{ 
    2532#!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 === 
     33'''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. 
     34}}} 
     35 
     36 
     37== 2. Choose a preset and tune == 
     38 
     39=== Preset === 
    3140 
    3241A 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. 
     
    4554* `placebo` – ignore this as it is not useful (see [#FAQ FAQ]) 
    4655 
    47 You 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`. 
     56You can see a list of current presets with `-preset help` (see [#Listpresetsandtunes example] below). If you have the `x264` binary installed, you can also see the exact settings these presets apply by running `x264 --fullhelp`. 
     57 
     58=== Tune === 
    4859 
    4960You can optionally use `-tune` to change settings based upon the specifics of your input. Current tunings include:  
     
    6071For 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`. 
    6172 
    62 Another 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. 
     73=== Profile === 
     74 
     75Another optional setting is `-profile:v` which will limit the output to a specific H.264 profile. Omit this unless your 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. 
     76 
     77=== List presets and tunes === 
    6378 
    6479To list all possible internal presets and tunes: 
     
    7388}}} 
    7489 
    75 If 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. 
    76  
    77 === CRF Example === 
     90== CRF Example == 
    7891 
    7992This command encodes a video with good quality, using slower preset to achieve better compression: 
     
    8396}}} 
    8497 
    85 Note that in this example the audio stream of the input file is simply [http://ffmpeg.org/ffmpeg.html#Stream-copy stream copied] over to the output and not re-encoded. 
    86  
    87 ---- 
    88  
    89 == Two-Pass ==#twopass 
     98Note that in this example the audio stream of the input file is simply [https://ffmpeg.org/ffmpeg.html#Stream-copy stream copied] over to the output and not re-encoded. 
     99 
     100If you are encoding a set of videos that are similar, apply the same settings to all the videos: this will ensure that they will all have similar quality. 
     101 
     102---- 
     103 
     104= Two-Pass =#twopass 
    90105 
    91106Use 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`: 
     
    98113You can also forgo the bitrate calculation if you already know what final (average) bitrate you need. 
    99114 
    100 === Two-Pass Example === 
     115== Two-Pass Example == 
    101116 
    102117For two-pass, you need to run `ffmpeg` twice, with almost the same settings, except for: 
     
    123138---- 
    124139 
    125 == Lossless H.264 == 
     140= Lossless H.264 = 
    126141 
    127142You 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. 
     
    148163---- 
    149164 
    150 == Overwriting default preset settings == 
     165= Overwriting default preset settings = 
    151166 
    152167While `-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. 
     
    159174 
    160175{{{ 
    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. 
    163 }}} 
    164  
    165 ---- 
    166  
    167 == Additional Information & Tips == 
    168  
    169 === CBR (Constant Bit Rate) === 
     176#!div style="border: 1px solid #e5c7c7; margin: 1em; background-color: #fdd;" 
     177'''Warning:''' Do not use the option `x264opts`, as it will eventually be removed. Use `x264-params` instead. 
     178}}} 
     179 
     180---- 
     181 
     182= Additional Information & Tips = 
     183 
     184== CBR (Constant Bit Rate) == 
    170185 
    171186There 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: 
     
    179194CBR encodes are usually inefficient if the video is easy to encode (e.g., empty or black frames). 
    180195 
    181 === Constained encoding (VBV / maximum bit rate) === 
     196== Constained encoding (VBV / maximum bit rate) == 
    182197 
    183198Use 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. 
     
    198213}}} 
    199214 
    200 === Low Latency === 
    201  
    202 libx264 offers a `-tune zerolatency` option. See the [[StreamingGuide]]. 
    203  
    204 === Compatibility === 
    205  
    206 ==== All devices ==== 
    207  
    208 If you want your videos to have highest compatibility with older devices (e.g., old Android phones): 
     215== Low Latency == 
     216 
     217x264 offers a `-tune zerolatency` option for low latency streaming. 
     218 
     219== Compatibility == 
     220 
     221=== All devices === 
     222 
     223If you want your videos to have highest compatibility with ancient devices (e.g., old Android phones): 
    209224 
    210225{{{ 
     
    214229This disables some advanced features but provides for better compatibility. Typically you may not need this setting (and therefore avoid using `-profile:v` and `-level`), but if you do use this setting it may increase the bit rate compared to what is needed to achieve the same quality in higher profiles. 
    215230 
    216 ==== iOS ==== 
     231=== iOS === 
    217232 
    218233||||||||= '''iOS Compatability''' ([https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/UsingHTTPLiveStreaming/UsingHTTPLiveStreaming.html#//apple_ref/doc/uid/TP40008332-CH102-SW8 source]) =|| 
     
    226241|| High || 4.2 || iPad Air and later, iPhone 5s and later || `-profile:v high -level 4.2` || 
    227242 
    228 {{{ 
    229 #!div style="border: 1px solid #e5e5c7; margin: 1em; background-color: #ffd;" 
    230 '''Note:'''  This table does not include any additional restrictions which may be required by your device. 
    231 }}} 
    232  
    233 ==== !QuickTime ==== 
     243=== !QuickTime === 
    234244 
    235245!QuickTime only supports YUV planar color space with 4:2:0 chroma subsampling (use `-vf format=yuv420p` or `-pix_fmt yuv420p`) for H.264 video. 
    236246 
    237 ==== Blu-ray ==== 
     247=== Blu-ray === 
    238248 
    239249See [http://www.x264bluray.com/ Authoring a professional Blu-ray Disc with x264]. 
    240250 
    241 === Pre-testing your settings === 
     251== Pre-testing your settings == 
    242252 
    243253Encode a random section instead of the whole video with the `-ss` and `-t`/`-to` options to quickly get a general idea of what the output will look like. 
     
    247257* `-to`: Stop writing the output at specified position. Value can be in seconds or HH:MM:SS format. 
    248258 
    249 === `faststart` for web video === 
     259== `faststart` for web video == 
    250260 
    251261You can add `-movflags +faststart` as an output option if your videos are going to be viewed in a browser. This will move some information to the beginning of your file and allow the video to begin playing before it is completely downloaded by the viewer. It is not required if you are going to use a video service such as !YouTube. 
    252262 
    253 === Custom preset file === 
     263== Custom preset file == 
    254264 
    255265Refer to the `-vpre` output option in the [https://ffmpeg.org/ffmpeg.html#Preset-files documentation]. 
     
    257267---- 
    258268 
    259 == FAQ == 
    260  
    261 === Will two-pass provide a better quality than CRF? === 
     269= FAQ = 
     270 
     271== Will two-pass provide a better quality than CRF? == 
    262272 
    263273[http://web.archive.org/web/20140206073211/http://doom10.org/index.php?PHPSESSID=okj08qe73ictdtv532augv8nu7&topic=267.msg2071#msg2071 No], though it does allow you to target a file size more accurately. 
    264274 
    265 === Why is `placebo` a waste of time? === 
     275== Why is `placebo` a waste of time? == 
    266276 
    267277It 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. 
    268278 
    269 === How do the different presets influence encoding time? === 
     279== How do the different presets influence encoding time? == 
    270280 
    271281This depends on the source material, the target bitrate, and your hardware configuration. In general, the higher the bitrate, the more time needed for encoding. 
     
    279289Using `fast` saves about 10% encoding time, `faster` 25%. `ultrafast` will save 55% at the expense of much lower quality. 
    280290 
    281 === Why doesn't my lossless output look lossless? === 
     291== Why doesn't my lossless output look lossless? == 
    282292 
    283293Blame the RGB to YUV color space conversion. If you convert with `-pix_fmt yuv444p` it should still be lossless (which is the default now). 
    284294 
    285 === Will a graphics card make x264 encode faster? === 
    286  
    287 For x264 specifically, probably not. [http://git.videolan.org/?p=x264.git;a=commit;h=3a5f6c0aeacfcb21e7853ab4879f23ec8ae5e042 x264 supports OpenCL] for some lookahead operations. 
    288  
    289 See [[HWAccelIntro]] for information on supported hardware based H.264 encoders and decoders. 
    290  
    291 === Encoding for dumb players === 
    292  
    293 You may need to use `-vf format=yuv420p` (or the alias `-pix_fmt yuv420p`) for your output to work in !QuickTime and most other players. These players only supports the YUV planar color space with 4:2:0 chroma subsampling for H.264 video. Otherwise, depending on your source, `ffmpeg` may output to a pixel format that may be incompatible with these players. 
    294  
    295 ---- 
    296  
    297 == Additional Resources == 
    298  
    299 * [http://slhck.info/articles/crf Constant Rate Factor Guide] 
     295== Will a graphics card make x264 encode faster? == 
     296 
     297For x264 specifically, probably not. [https://git.videolan.org/?p=x264.git;a=commit;h=3a5f6c0aeacfcb21e7853ab4879f23ec8ae5e042 x264 supports OpenCL] for some lookahead operations. 
     298 
     299See [[HWAccelIntro]] for information on supported hardware H.264 encoders and decoders. 
     300 
     301== Encoding for dumb players == 
     302 
     303You may need to use `-vf format=yuv420p` (or the alias `-pix_fmt yuv420p`) for your output to work in !QuickTime and most other players. These players only support the YUV planar color space with 4:2:0 chroma subsampling for H.264 video. Otherwise, depending on your source, `ffmpeg` may output to a pixel format that may be incompatible with these players. 
     304 
     305---- 
     306 
     307= Additional Resources = 
     308 
     309* [http://slhck.info/video/2017/02/24/crf-guide.html Constant Rate Factor Guide] 
    300310* [http://slhck.info/video/2017/03/01/rate-control.html Understanding Rate Control Modes]