How to consider bitrate, -maxrate and -bufsize of a video for web
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.
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 yourmaxrate
(one second) and twice of yourmaxrate
(2 seconds). If this is still not low enough, lower yourmaxrate
and then re-setbufsize
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.