| 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 | | }}} |
| | 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`: |
| | 64 | |
| | 65 | {{{ |
| | 66 | (200 MiB * 8192 [converts MiB to kBit]) / 600 seconds = ~2730 kBit/s total bitrate |
| | 67 | 2730 - 128 kBit/s (desired audio bitrate) = 2602 kBit/s video bitrate |
| | 68 | }}} |
| | 69 | |
| | 70 | You can also forgo the bitrate calculation if you already know what final (average) bitrate you need for clip. |
| 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. |
| | 141 | ffmpeg -i input.mp4 -c:v libx264 -x264opts "nal-hrd=cbr" -b:v 1M -minrate 1M -maxrate 1M -bufsize 2M output.ts |
| | 142 | }}} |
| | 143 | |
| | 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. |
| | 147 | |
| | 148 | === Constained encoding (VBV / maximum bit rate) === |
| | 149 | |
| | 150 | You can also also use `-crf` or `-b:v` with a maximum bit rate by specifying both `-maxrate` and `-bufsize`: |
| | 151 | |
| | 152 | {{{ |
| | 153 | ffmpeg -i input -c:v libx264 -crf 23 -maxrate 1M -bufsize 2M output.mp4 |
| | 154 | }}} |
| | 155 | |
| | 156 | This 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 | |
| | 158 | In 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 | {{{ |
| | 161 | ffmpeg -i input -c:v libx264 -b:v 1M -maxrate 1M -bufsize 2M -pass 1 -f mp4 /dev/null |
| | 162 | ffmpeg -i input -c:v libx264 -b:v 1M -maxrate 1M -bufsize 2M -pass 2 output.mp4 |
| | 163 | }}} |
| | 164 | |
| | 165 | |