How to consider bitrate, -maxrate and -bufsize of a video for web

38,162

It really depends on your upload speed.

bufsize will determine how religious ffmpeg is about keeping your bitrate constant. If you set a bufsize of 64k, as per FFmpeg Wiki: Limiting the output bitrate, it will calculate its current bitrate every 64 kilobytes and adjust accordingly. Smaller sizes for bufsize can be harmful to quality in that they don't allow enough space between checks for x264 to do sudden changes - you will get blockiness.

If your maxrate is 640kbps, and your bufsize is 64k, then every tenth of a second x264 would check. This is sub-optimal - FFmpeg Wiki: Encoding for streaming sites recommends to run it every 1 to 2 seconds. If this didn't make sense, think of it as maxrate/bufsize = frequency of checks. Keep this frequency between 1 and 2 seconds as a rule of thumb.

If you set both maxrate and bufsize, you should:

  • set maxrate to whatever your lowest upload speed will likely be (in the ffmpeg wiki example, this is 80% of total upload speed, but your mileage may vary).
  • set bufsize to somewhere between the same as your maxrate (one second) and twice of your maxrate (2 seconds). If this is still not low enough, lower your maxrate and then re-set bufsize accordingly.

Then, you'll have to play around a bit, but since you have to start somewhere I'd just start at a maxrate around 600k, which was usually satisfying enough for me back before I used crf for everything.

If you'd like, you can try lower values for bufsize, like for every three or four seconds, just to see how the value changes how your output looks. Then you can determine how much you should worry about it for your video.

There is no normal value, really - what crf does is to optimize output based on what it thinks is the best buffer size for maintaining whatever it's rate is set at. It tries to keep as low a file size while maintaining some quality, at the cost of occasional spikes.

Share:
38,162

Related videos on Youtube

Robin
Author by

Robin

Updated on September 18, 2022

Comments

  • Robin
    Robin over 1 year

    I am using ffmpeg to encode my videos to upload them on the web. I saw this post about using ffmpeg, but didn't quite get as to how to consider the values.

    Suppose I have a video of size 70 MB with a duration of 4 minutes. How would I consider the value for these flags : -b:v, -maxrate and -bufsize for this command?

    ffmpeg -i input -codec:v libx264 -profile:v main -preset slow -b:v ? -maxrate ? -bufsize ? -vf "scale=720:trunc(ow/a/2)*2" -threads 0 -codec:a libfdk_aac -movflags +faststart output
    

    Or is there any normal value, like for the crf values are 19-24? I would really appreciate your help and guidance.

  • Ely
    Ely almost 9 years
    Shouldn't "set bufsize to somewhere between the same as your maxrate (one second) and half of your maxrate (2 seconds)" be "set bufsize to somewhere between the same as your maxrate (one second) and twice your maxrate (2 seconds)" ?
  • Wyatt Ward
    Wyatt Ward almost 9 years
    @Ely I think you are right. Mine would do it every half second. I'll fix that!
  • Robin
    Robin almost 9 years
    @Wyatt8740 Sorry, I was very busy. Just one question though. If I use crf, then I don't need to specify bitrate, buffsize and maxrate? And if so, which is more efficient (use crf or bitrate, buffsize and maxrate)?
  • Ely
    Ely almost 9 years
    @Robin You can use bufsize and maxrate with crf. Remember, crf will adjust bitrate on the fly to match a certain quality, and if parts of the video are very complex, the bitrate will shoot sky-high and you probably don't want that, so better "put a lock" with bufsize and maxrate (but not too strict, or you video will look like crap ! :) )
  • Wyatt Ward
    Wyatt Ward almost 9 years
    @Robin correct, you shouldn't need to use crf with the others. However, you can if you want to constrain it. crf tries to maintain constant quality - you can force it to do so within limits with bufsize and maxrate. You do not need bitrate, though. As Ely said, it will make it better for streaming.
  • Robin
    Robin almost 9 years
    @Wyatt8740 Ok. I think I am getting this. I just saw this post. As you said, the frequency should be between 1 and 2 seconds, but as the -maxrate is 400k and -bufsize is 1835k, the result is 0.2. What's going on here?
  • Wyatt Ward
    Wyatt Ward almost 9 years
    @Robin My recommendation was simply a rule of thumb. This example uses a more aggressive (and probably less efficient) rate of checking. It doesn't allow as much fluctuation in quality as mine would - you want to give it some room to fluctuate but not enough that the size deviates significantly minute-to-minute. I'd recommend the 0.5 rule, but that's not to say it cannot be done other ways. With some video sources that don't change much (for example color bars or a still photo), this may be acceptable. But I'd do closer to 0.5 anyway, and if anything go closer to one second instead of lower.