Changes between Version 64 and Version 65 of Encode/H.264
- Timestamp:
- Nov 1, 2017, 12:53:26 AM (9 years ago)
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;"> 4 H.264 Video Encoding Guide 5 </h1> 6 }}} 7 8 [[PageOutline(1, Contents)]] 9 10 The 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 12 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]. 13 14 If you need help compiling and installing libx264 see one of our [[CompilationGuide|compiling guides]]. 15 16 ---- 17 18 = Constant Rate Factor (CRF) =#crf 12 19 13 20 Use this mode if you want to keep the best quality and don't care about the file size. 14 21 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.22 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. 23 24 == 1. Choose a CRF value == 25 The 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. 19 26 20 27 The 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. … … 24 31 {{{ 25 32 #!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 === 31 40 32 41 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. … … 45 54 * `placebo` – ignore this as it is not useful (see [#FAQ FAQ]) 46 55 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`. 56 You 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 === 48 59 49 60 You can optionally use `-tune` to change settings based upon the specifics of your input. Current tunings include: … … 60 71 For 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`. 61 72 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 75 Another 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 === 63 78 64 79 To list all possible internal presets and tunes: … … 73 88 }}} 74 89 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 == 78 91 79 92 This command encodes a video with good quality, using slower preset to achieve better compression: … … 83 96 }}} 84 97 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 98 Note 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 100 If 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 90 105 91 106 Use 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`: … … 98 113 You can also forgo the bitrate calculation if you already know what final (average) bitrate you need. 99 114 100 == = Two-Pass Example ===115 == Two-Pass Example == 101 116 102 117 For two-pass, you need to run `ffmpeg` twice, with almost the same settings, except for: … … 123 138 ---- 124 139 125 = = Lossless H.264 ==140 = Lossless H.264 = 126 141 127 142 You 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. … … 148 163 ---- 149 164 150 = = Overwriting default preset settings ==165 = Overwriting default preset settings = 151 166 152 167 While `-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. … … 159 174 160 175 {{{ 161 #!div style="border: 1px solid #e5 e5c7; margin: 1em; background-color: #ffd;"162 ''' Note:''' Do not use the option `x264opts`, as it willbe 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) == 170 185 171 186 There 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: … … 179 194 CBR encodes are usually inefficient if the video is easy to encode (e.g., empty or black frames). 180 195 181 == = Constained encoding (VBV / maximum bit rate) ===196 == Constained encoding (VBV / maximum bit rate) == 182 197 183 198 Use 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. … … 198 213 }}} 199 214 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 olderdevices (e.g., old Android phones):215 == Low Latency == 216 217 x264 offers a `-tune zerolatency` option for low latency streaming. 218 219 == Compatibility == 220 221 === All devices === 222 223 If you want your videos to have highest compatibility with ancient devices (e.g., old Android phones): 209 224 210 225 {{{ … … 214 229 This 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. 215 230 216 === = iOS ====231 === iOS === 217 232 218 233 ||||||||= '''iOS Compatability''' ([https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/UsingHTTPLiveStreaming/UsingHTTPLiveStreaming.html#//apple_ref/doc/uid/TP40008332-CH102-SW8 source]) =|| … … 226 241 || High || 4.2 || iPad Air and later, iPhone 5s and later || `-profile:v high -level 4.2` || 227 242 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 === 234 244 235 245 !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. 236 246 237 === = Blu-ray ====247 === Blu-ray === 238 248 239 249 See [http://www.x264bluray.com/ Authoring a professional Blu-ray Disc with x264]. 240 250 241 == = Pre-testing your settings ===251 == Pre-testing your settings == 242 252 243 253 Encode 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. … … 247 257 * `-to`: Stop writing the output at specified position. Value can be in seconds or HH:MM:SS format. 248 258 249 == = `faststart` for web video ===259 == `faststart` for web video == 250 260 251 261 You 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. 252 262 253 == = Custom preset file ===263 == Custom preset file == 254 264 255 265 Refer to the `-vpre` output option in the [https://ffmpeg.org/ffmpeg.html#Preset-files documentation]. … … 257 267 ---- 258 268 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? == 262 272 263 273 [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. 264 274 265 == = Why is `placebo` a waste of time? ===275 == Why is `placebo` a waste of time? == 266 276 267 277 It 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 278 269 == = How do the different presets influence encoding time? ===279 == How do the different presets influence encoding time? == 270 280 271 281 This depends on the source material, the target bitrate, and your hardware configuration. In general, the higher the bitrate, the more time needed for encoding. … … 279 289 Using `fast` saves about 10% encoding time, `faster` 25%. `ultrafast` will save 55% at the expense of much lower quality. 280 290 281 == = Why doesn't my lossless output look lossless? ===291 == Why doesn't my lossless output look lossless? == 282 292 283 293 Blame the RGB to YUV color space conversion. If you convert with `-pix_fmt yuv444p` it should still be lossless (which is the default now). 284 294 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 basedH.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 support sthe 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/crfConstant Rate Factor Guide]295 == Will a graphics card make x264 encode faster? == 296 297 For x264 specifically, probably not. [https://git.videolan.org/?p=x264.git;a=commit;h=3a5f6c0aeacfcb21e7853ab4879f23ec8ae5e042 x264 supports OpenCL] for some lookahead operations. 298 299 See [[HWAccelIntro]] for information on supported hardware H.264 encoders and decoders. 300 301 == Encoding for dumb players == 302 303 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 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] 300 310 * [http://slhck.info/video/2017/03/01/rate-control.html Understanding Rate Control Modes]
