[FFmpeg-user] 2 pass CBR or VBR not really fixing the bitrate?

Manuel Tiglio tiglio at icloud.com
Mon Jul 31 21:52:05 EEST 2017

Hi Jonathan, 

This helps 

> Hi,
> how about this command?:
> ffmpeg -i input.mp4 -c:v libx264 -x264-params "nal-hrd=cbr" -b:v 1M -minrate 1M -maxrate 1M -bufsize 2M
> Source: https://trac.ffmpeg.org/wiki/Encode/H.264

Will give it a try. I had seen in other forums discussing this same issued and that this should work, but ‘nal-hrd=cbr” only work for producing ts files. I was wondering if one could also control the bitrate prior to that, at the mp4 level. 

Notice that in the documentation that you quote, just below this example, it says

Constained encoding (VBV / maximum bit rate) <https://trac.ffmpeg.org/wiki/Encode/H.264#ConstainedencodingVBVmaximumbitrate>
You can also use -crf or -b:v with a maximum bit rate by specifying both  -maxrate and -bufsize:

ffmpeg -i input -c:v libx264 -crf 23 -maxrate 1M -bufsize 2M output.mp4
This will effectively "target" -crf 23, but if the output were to exceed 1 MBit/s, the encoder would increase the CRF to prevent bitrate spikes. However, be aware that libx264does not strictly control the maximum bit rate as you specified (the maximum bit rate may be well over 1M for the above file). To reach a perfect maximum bit rate, use two-pass.

In another example, instead of using constant quality (CRF) as a target, the average bitrate is set. A two-pass approach is preferred here:

ffmpeg -i input -c:v libx264 -b:v 1M -maxrate 1M -bufsize 2M -pass 1 -f mp4 /dev/null
ffmpeg -i input -c:v libx264 -b:v 1M -maxrate 1M -bufsize 2M -pass 2 output.mp4

And that, does not seem to work as advertised. That is, the two pass encoding still gives a max rate which is far from the specified target (1M in the above example). 

Somebody must have gone through this before. 

> Of course it needs a bit modification to fit your two pass encoding.

That would be fine. But ideally I would like to have the mp4 files with bitrate control before transmuxing, and keep those mp4 files in case they need to be reused for a number of reasons. 

> You just worry about hls, or your really need to get this close persistence?
> Because we make life streaming with crf and maxrate/buffsize over hls and never had problems with it.

Interesting, as in above? Ie 

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

What does seem to work in ffmpeg is capped crf (in the sense that the obtained crf is close to the specified cap value), but then the lower bitrates are far from that cap and there is degradation in quality. 

Ideally I would like to do 110% VBV, which changing the 2-pass example should be achieved by 

ffmpeg -i input -c:v libx264 -b:v 1M -maxrate 1M -bufsize 2M -pass 1 -f mp4 /dev/null
ffmpeg -i input -c:v libx264 -b:v 1M -maxrate 1.1M -bufsize 2M -pass 2 output.mp4
Or with a bufsize of 1M (one sec)

Thanks Jonathan, would you mind following up on my comments above? I really appreciate it. 


> _______________________________________________
> ffmpeg-user mailing list
> ffmpeg-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-user
> To unsubscribe, visit link above, or email
> ffmpeg-user-request at ffmpeg.org with subject "unsubscribe".

More information about the ffmpeg-user mailing list