Changes between Version 4 and Version 5 of Limiting the output bitrate
- Timestamp:
- Nov 6, 2017, 4:03:49 PM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Limiting the output bitrate
v4 v5 1 1 = Introduction = 2 2 3 '' 4 ''' 5 This page is a work in progress (not finished yet) and is intended to describe how to use ffmpeg to produce the output of the constant (or maximum) bit rate. 6 It should also explain the relationship between options `-maxrate`, `-minrate` and `-bufsize` and to show how to properly use those with ffmpeg. 7 There is some more info at: [http://superuser.com/questions/536001/variable-bit-rates-with-vb-and-minrate-maxrate-settings-in-ffmpeg What are -b:v, -minrate, and -maxrate?] 8 ''' 9 '' 3 Limiting the output bit rate to a certain range makes sense if you are live streaming or need to be constrained to a certain specification (such as Blu-ray encoding), where the decoder might not be able to handle large bitrate spikes. 10 4 11 Limiting the output bit rate only makes sense if you are going to do a live streaming or need to be constrained to a certain specification (such as Blu-ray encoding). In all other cases there are other techniques that better fit the desired scenario. We'll enumerate most of the popular scenarios and a proper ffmpeg usage for each one of them. In the following examples we use libx264 encoder since it is one of the most popular, but if you need some other encoder the principle involved is the same. 5 There are several options you can use to steer the encoder's bitrate: 12 6 13 = Creating a live stream with limited bit rate = 7 - `-b:v`: specifies the target (average) bit rate for the encoder to use 8 - `-minrate` specifies a minimum tolerance to be used 9 - `-maxrate` specifies a maximum tolerance. this is only used in conjunction with `bufsize` 10 - `-bufsize` specifies the decoder buffer size, which determines the variability of the output bitrate 14 11 15 In this case, you want to have a live stream with more/less constant bit rate (using the option `-b:v`), to be able to control the bandwidth used. We will instruct the libx264 encoder to simulate a stream transmission with a virtual buffer (just like the real buffer at the decoding side). The technique will constrain the bit rate in order not to exceed a certain threshold value which would take more time to transmit and would cause the decoding buffer to underflow waiting for the new data to arrive.16 17 The typical example would be something like this ([http://www.ffmpeg.org/ffmpeg-all.html#Description as shown in the documentation]):18 12 {{{ 19 ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi 13 #!div style="border: 1px solid #e5e5c7; margin: 1em; background-color: #ffd;" 14 '''Note:''' Constraining the bitrate might result in low quality output if the video is hard to encode. In most cases (such as storing a file for archival), letting the encoder choose the proper bitrate is the constant quality or CRF-based encoding. 20 15 }}} 21 16 22 The key option here is `-bufsize` which tells the encoder how often to calculate the average bit rate and check to see if it conforms to the average bit rate specified on the command line (`-b:v 64k`). If we don't specify the `-bufsize` option, ffmpeg will still calculate and correct the average bit rate produced, but more lazy, at some intervals which can be significantly longer than we would want. This would cause the current bit rate to frequently jump a lot over and below the specified average bit rate and would cause an unsteady output bit rate. If we specify a smaller `-bufsize`, ffmpeg will more frequently check for the output bit rate and constrain it to the specified average bit rate from the command line. Specifying too small `-bufsize` would cause ffmpeg to degrade the output image quality, because it would have to (frequently) conform to the limitations and would not have enough of a free space to use some optimizations (for example, optimizations based on the frame repetitions and similar), because the buffer would not contain enough frames for the optimizations to be effective. 17 = Example: Creating a live stream with limited bit rate = 23 18 24 The suggestion is to play around with the combinations of `-bufsize`, starting from the same value like the one specified for the `-b:v` option, and increasing it until your output bit rate starts jumping too much above/below the specified average bit rate. Then you know you've reached the limit and should lower the `-bufsize` value a little bit in order to get back on the safe side.19 In this case, you want to have a live stream with more/less constant bit rate (using the option `-b:v`), to be able to control the bandwidth used. 25 20 26 There are also `-minrate` and `-maxrate` options which control the minimum and maximum bit rate tolerance (in bits/s). With these options you can give ffmpeg a hint of how much of a bit rate tolerance (a difference from the specified average bit rate) you are willing to accept.21 We will instruct the libx264 encoder to simulate a stream transmission with a virtual buffer (just like the real buffer at the decoding side). The technique will constrain the bit rate in order not to exceed a certain threshold value which would take more time to transmit and would cause the decoding buffer to underflow waiting for the new data to arrive. 27 22 28 = Creating the output file of specific size = 23 The typical example would be something like this: 29 24 30 For this scenario it is by far the best to use a [http://www.ffmpeg.org/ffmpeg.html#Video-Options 2-pass encoding, using the -pass option], like it is explained in the example of [http://trac.ffmpeg.org/wiki/x264EncodingGuide#twopass targeting a specific output file size]. 25 {{{ 26 ffmpeg -i input -c:v libx264 -b:v 2M -maxrate 2M -bufsize 1M output.mp4 27 }}} 31 28 32 = Creating the output of the constant image quality = 29 The key option here is `-bufsize` which tells the encoder how often to calculate the average bit rate and check to see if it conforms to the average bit rate specified on the command line (`-b:v 2M`). 33 30 34 There is a specific option in the libx264 encoder ([http://trac.ffmpeg.org/wiki/x264EncodingGuide#a1.ChooseaCRFvalue -crf]) for this purpose, named "Constant Rate Factor", whose idea is to produce the same perceptual image quality throughout the output. The simplest usage example is: 35 {{{ 36 ffmpeg -i input -c:v libx264 -crf 23 output.mp4 37 }}} 31 = What does `-bufsize` do? = 32 33 Based on the `-bufsize` option, ffmpeg will calculate and correct the average bit rate produced. If we didn't specify `-bufsize`, these intervals could be significantly longer than we would want. This would cause the current bit rate to frequently jump a lot over and below the specified average bit rate and would cause an unsteady output bit rate. 34 35 If we specify a smaller `-bufsize`, ffmpeg will more frequently check for the output bit rate and constrain it to the specified average bit rate from the command line. Hence, lowering `-bufsize` lowers the bitrate variation that the encoder can produce. 36 37 Specifying too small `-bufsize` would cause ffmpeg to degrade the output image quality, because it would have to (frequently) conform to the limitations and would not have enough of a free space to use some optimizations (for example, optimizations based on the frame repetitions and similar), because the buffer would not contain enough frames for the optimizations to be effective. 38 39 The suggestion is to play around with the combinations of `-bufsize`, starting from the same value like the one specified for the `-b:v` option (or even half of it) and increasing it until your output bit rate starts jumping too much above/below the specified average bit rate. Then you know you've reached the limit and should lower the `-bufsize` value a little bit in order to get back on the safe side.
