Changes between Version 2 and Version 3 of Limiting the output bitrate


Ignore:
Timestamp:
Oct 4, 2013, 3:48:23 PM (3 years ago)
Author:
burek
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Limiting the output bitrate

    v2 v3  
    1 This page is a placeholder and is intended to describe how to use ffmpeg to produce the output of the constant (or maximum) bit rate. 
     1= Introduction = 
     2 
     3'' 
     4''' 
     5This 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. 
    26It should also explain the relationship between options -maxrate, -minrate and -bufsize and to show how to properly use those with ffmpeg. 
     7There is some more info at the following link: http://superuser.com/questions/536001/variable-bit-rates-with-vb-and-minrate-maxrate-settings-in-ffmpeg 
     8''' 
     9'' 
    310 
    4 For now, more info at the following link: http://superuser.com/questions/536001/variable-bit-rates-with-vb-and-minrate-maxrate-settings-in-ffmpeg 
     11According to a lot of support questions so far, there is a lot of interest to limit the output bit rate using ffmpeg. To make things clear, limiting the output bit rate only makes sense if you are going to do a live streaming. 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've used libx264 encoder, since it is one of the most popular, among ffmpeg users, but if you need some other encoder, the principle involved is the same, just the exact ffmpeg options need to be fine tuned in addition. 
    512 
     13= Creating a live stream with limited bit rate = 
     14 
     15In 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 
     17The typical example would be something like this ([http://www.ffmpeg.org/ffmpeg-all.html#Description as shown in the documentation]): 
     18{{{ 
     19ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi 
     20}}} 
     21 
     22The 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. 
     23 
     24The 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. 
     25 
     26There 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. 
     27 
     28= Creating the output file of specific size = 
     29 
     30For this scenario it is by far the best to use a [http://www.ffmpeg.org/ffmpeg.html#Video-Options 2-pass encoding, using -pass option], like it is explained in the example of [http://trac.ffmpeg.org/wiki/x264EncodingGuide#twopass targeting a specific output file size]. 
     31 
     32= Creating the output of the constant image quality = 
     33 
     34There 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{{{ 
     36ffmpeg -i input -c:v libx264 -crf 23 output.mp4 
     37}}}