Changes between Version 59 and Version 60 of Encode/H.264


Ignore:
Timestamp:
Mar 7, 2017, 12:40:38 PM (2 years ago)
Author:
slhck
Comment:

update examples, add guide on rate control modes

Legend:

Unmodified
Added
Removed
Modified
  • Encode/H.264

    v59 v60  
    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.
     7There 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].
    88
    99If you need help compiling and installing libx264 see one of our [[CompilationGuide|FFmpeg and x264 compiling guides]].
     
    6161== Two-Pass ==#twopass
    6262
    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 50 MB is desired. Since `bitrate = file size / duration`:
    64 
    65 {{{
    66 (50 MB * 8192 [converts MB to kilobits]) / 600 seconds = ~683 kilobits/s total bitrate
    67 683k - 128k (desired audio bitrate) = 555k video bitrate
    68 }}}
     63This 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`:
     64
     65{{{
     66(200 MiB * 8192 [converts MiB to kBit]) / 600 seconds = ~2730 kBit/s total bitrate
     672730 - 128 kBit/s (desired audio bitrate) = 2602 kBit/s video bitrate
     68}}}
     69
     70You can also forgo the bitrate calculation if you already know what final (average) bitrate you need for clip.
    6971
    7072=== Two-Pass Example ===
    7173
    7274{{{
    73 ffmpeg -y -i input -c:v libx264 -preset medium -b:v 555k -pass 1 -c:a aac -b:a 128k -f mp4 /dev/null && \
    74 ffmpeg -i input -c:v libx264 -preset medium -b:v 555k -pass 2 -c:a aac -b:a 128k output.mp4
     75ffmpeg -y -i input -c:v libx264 -preset medium -b:v 2600k -pass 1 -c:a aac -b:a 128k -f mp4 /dev/null && \
     76ffmpeg -i input -c:v libx264 -preset medium -b:v 2600k -pass 2 -c:a aac -b:a 128k output.mp4
    7577}}}
    7678
     
    137139
    138140{{{
    139 ffmpeg -i input -c:v libx264 -b:v 4000k -minrate 4000k -maxrate 4000k -bufsize 1835k out.m2v
    140 }}}
    141 
    142 In the above example, `-bufsize` is the "rate control buffer" so it will enforce your requested "average" (4000k in this case) across each 1835k 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.
    143 
    144 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, up to the crf level.
    145 
    146 === CRF with maximum bit rate ===
    147 
    148 You can also also use `-crf` with a maximum bit rate by specifying both `-crf` and `-maxrate`:
    149 {{{
    150 ffmpeg -i input -c:v libx264 -crf 20 -maxrate 400k -bufsize 1835k output.mp4
    151 }}}
    152 
    153 This will effectively "target" `-crf 20`, but if the output exceeds 400kb/s, it will degrade to something less than `-crf 20` in that case.
     141ffmpeg -i input.mp4 -c:v libx264 -x264opts "nal-hrd=cbr" -b:v 1M -minrate 1M -maxrate 1M -bufsize 2M output.ts
     142}}}
     143
     144In 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
     146Of 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.
     147
     148=== Constained encoding (VBV / maximum bit rate) ===
     149
     150You can also also use `-crf` or `-b:v` with a maximum bit rate by specifying both  `-maxrate` and `-bufsize`:
     151
     152{{{
     153ffmpeg -i input -c:v libx264 -crf 23 -maxrate 1M -bufsize 2M output.mp4
     154}}}
     155
     156This will effectively "target" `-crf 23`, but if the output were to exceed 1 MBit/s, the encoder would increase the CRF to prevent bitrate spikes.
     157
     158In another example, instead of using constant quality (CRF) as a target, the average bitrate is set. A two-pass approach is preferred here:
     159
     160{{{
     161ffmpeg -i input -c:v libx264 -b:v 1M -maxrate 1M -bufsize 2M -pass 1 -f mp4 /dev/null
     162ffmpeg -i input -c:v libx264 -b:v 1M -maxrate 1M -bufsize 2M -pass 2 output.mp4
     163}}}
     164
     165
    154166
    155167=== Low Latency ===
     
    241253
    242254* [http://slhck.info/articles/crf Constant Rate Factor Guide]
     255* [http://slhck.info/video/2017/03/01/rate-control.html Understanding Rate Control Modes]