|Version 4 (modified by llogan, 4 years ago) (diff)|
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. It should also explain the relationship between options -maxrate, -minrate and -bufsize and to show how to properly use those with ffmpeg. There is some more info at: What are -b:v, -minrate, and -maxrate?
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.
Creating a live stream with limited bit rate
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.
The typical example would be something like this (as shown in the documentation):
ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi
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.
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.
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.
Creating the output file of specific size
Creating the output of the constant image quality
There is a specific option in the libx264 encoder (-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:
ffmpeg -i input -c:v libx264 -crf 23 output.mp4