Changes between Version 4 and Version 5 of Limiting the output bitrate


Ignore:
Timestamp:
Nov 6, 2017, 6:03:49 PM (13 days ago)
Author:
slhck
Comment:

add more explanation for parameters

Legend:

Unmodified
Added
Removed
Modified
  • Limiting the output bitrate

    v4 v5  
    11= Introduction = 
    22 
    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 '' 
     3Limiting 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. 
    104 
    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. 
     5There are several options you can use to steer the encoder's bitrate: 
    126 
    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 
    1411 
    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]): 
    1812{{{ 
    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. 
    2015}}} 
    2116 
    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 = 
    2318 
    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. 
     19In 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. 
    2520 
    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. 
     21We 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. 
    2722 
    28 = Creating the output file of specific size = 
     23The typical example would be something like this: 
    2924 
    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{{{ 
     26ffmpeg -i input -c:v libx264 -b:v 2M -maxrate 2M -bufsize 1M output.mp4 
     27}}} 
    3128 
    32 = Creating the output of the constant image quality = 
     29The 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`). 
    3330 
    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 
     33Based 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 
     35If 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 
     37Specifying 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 
     39The 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.