Variable bit rates with "-vb" and "minrate"/"maxrate" settings in FFmpeg

12

6

For rendering, the time it takes is very different.

  • -b 3500K -minrate 0K -maxrate 8000K takes 1hr 17min
  • vb=3000k takes 2.5 hours

What is vb 3000k and how is it different from -b 3500K -minrate 0K -maxrate 8000K? Are these variable bit rate settings?

If I changed to -b 3500K -minrate 3500K -maxrate 3500K, is that constant bit rate?

Scott Downey

Posted 2013-01-14T13:53:49.200

Reputation: 121

Answers

16

Please read the documentation for FFmpeg, and run ffmpeg -h full for the list of options. Also, have a look at this article I wrote, which shows the differences between rate control modes in encoders like x264 and x265.

Generally, here's what the options mean:

  • -b:v (or -vb, the same) specifies the target average bit rate for the encoder to use:

    -b <int> E..VA. set bitrate (in bits/s) (from 0 to INT_MAX)

  • -minrate specifies a minimum tolerance to be used:

    -minrate <int> E..VA. Set minimum bitrate tolerance (in bits/s). Most useful in setting up a CBR encode. It is of little use otherwise. (from INT_MIN to INT_MAX)

  • -maxrate specifies a maximum tolerance. However, as the documentation indicates, this is only used in conjunction with bufsize:

    -maxrate <int> E..VA. Set maximum bitrate tolerance (in bits/s). Requires bufsize to be set. (from INT_MIN to INT_MAX)

    -bufsize <int> E..VA. set ratecontrol buffer size (in bits) (from INT_MIN to INT_MAX)

    This only makes sense for variable bit rate encoding, where instead of using a constant bit rate or constant quality model, the encoder simulates a transmission with a virtual buffer at the decoder. The -minrate/-maxrate/-bufsize options control that buffer size. You typically only use this mode for streaming, since the technique will constrain the bit rate in order to not exceed a certain value which would cause the decoder buffer to over- or underflow.

To summarize, you have several options for limiting bitrate:

  1. To set up a CBR process, you have to check what the encoder offers. Typically, you cannot achieve a "perfect" constant bitrate, since the encoder will not waste bits. Setting -b:v, -minrate, and -maxrate to the same levels will achieve that, for example for libx264:

    ffmpeg -i input.mp4 -c:v libx264 -x264-params "nal-hrd=cbr" -b:v 1M -minrate 1M -maxrate 1M -bufsize 2M output.ts
    

    Warning: This might result in low quality for videos that are hard to encode, and it will waste bits. Unless you absolutely need to achieve a constant rate output, do not use this option.

  2. Set up a constrained / variable bit rate process for streaming. Use -b:v 3500K -maxrate 3500K -bufsize 1000K, for example. You'll have to adjust the rate and buffer sizes to the context obviously. The higher the buffer size, the higher the allowed bitrate variation.

  3. Use a constant quality target and limit the bitrate only to catch spikes. For example, use -c:v libx264 -crf 23 -maxrate 4M -bufsize 4M to encode at variable bitrate with a target CRF of 23, but limit the output to a maximum of 4 MBit/s.

slhck

Posted 2013-01-14T13:53:49.200

Reputation: 182 472

How would you set a Constant Bit Rate for MPEG1 or MPEG2? – Royi – 2017-03-14T13:10:24.577

@Royi Set -minrate, -maxrate, and -b:v to the same level. – slhck – 2017-03-14T13:58:58.967

Will that generate a Video which in its properties (As in MefiaInfo) a Constant Bit Rate will be shown? – Royi – 2017-03-14T14:40:06.647

1How would it encode using a variable bit rate? as in the syntax to use? – Scott Downey – 2013-01-14T14:12:18.897

What encoder do you want to use? – slhck – 2013-01-14T14:12:48.503

ffmpeg, I beleive that is used in kdenlive – Scott Downey – 2013-01-14T14:14:58.133

No, I meant as in: x264 for H.264/MPEG-4 AVC video or XviD for MPEG-4, or Ogg Theora, etc. simply put, what file do you want? MP4? – slhck – 2013-01-14T14:17:49.670

x264 mp4. The current string in kdenlive is this f=mp4 hq=1 acodec=aac ab=%audiobitrate+'k' ar=48000 pix_fmt=yuv420p vcodec=libx264 minrate=0 -b 3500K -minrate 0K -maxrate 12000K g=250 bf=3 b_strategy=1 subcmp=2 cmp=2 coder=1 flags=+loop flags2=dct8x8 qmax=51 subq=7 qmin=10 qcomp=0.6 qdiff=4 trellis=1 aspect=%dar pass=%passes movflags=faststart – Scott Downey – 2013-01-14T14:24:13.470

For x264, use -crf 23 instead of all the other rate options. Less CRF means better quality,and sane values are from 18 to 28, default being 23. x264 used VBR CRF encoding by default, unless told otherwise. – slhck – 2013-01-14T14:27:12.620

So would it be this and what will that do to the final file size?: f=mp4 hq=1 acodec=aac ab=%audiobitrate+'k' ar=48000 pix_fmt=yuv420p vcodec=libx264 -crf 23 g=250 bf=3 b_strategy=1 subcmp=2 cmp=2 coder=1 flags=+loop flags2=dct8x8 qmax=51 subq=7 qmin=10 qcomp=0.6 qdiff=4 trellis=1 aspect=%dar pass=%passes movflags=faststart – Scott Downey – 2013-01-14T14:31:39.380

The final size depends on lots of factors, including the pixel dimensions and frame rate of the source as well as the original quality and content, the amount of motion and spatial detail. I can't even give you a rough estimate here—you need to run a test and see what the resulting average bit rate is, then infer from that. – slhck – 2013-01-14T14:40:15.993

3

See here for more information on encoding with x264 in FFmpeg; especially looks at the presets (I generally use the veryfast preset, in my tests the largest dropoff in filesize was between superfast and veryfast - after that the differences were much more incremental. YMMV of course).

– evilsoup – 2013-01-14T15:00:52.840