1

Im using FFMPEG to encode in H264 a real-time capturing video at 60 fps. I'd like to encode it at zero latency, but also at a constant bit rate. I've tried to follow recommendations I've seen about the configuration to use to achieve a constant bitrate and, while some of them manage to make the encoder output the average bitrate I wanted, there are constant spikes above and below that go way too far from the bitrate I'd like.

Solutions I've read about getting a more strict bitrate mention using lookahead or buffering, but this would add an undesired latency.

What configuration should I follow to achieve a real strict constant bit rate at zero latency? Should I use other encoders such as x264 directly or nvenc? I'm not sure if this would be possible at zero latency.

1 Answer 1

0

It is impossible to encode at exactly zero latency, but you can minimize latency significantly. It's also impossible to encode with true CBR, but you can minimize fluctuations significantly. What x264 calls CBR mode is just VBR mode with very, very strict constraints:

ffmpeg -i input.mp4 -c:v libx264 -tune zerolatency -b:v 6M -maxrate 6M -bufsize 250k -nal-hrd cbr -c:a copy output.mp4 

The lower the buffer size, the more strict the CBR mode will be. If you go too low, you'll begin to run into buffer underflows, however. The smallest it can be is a single frame. In the above example, the input is 24 fps, so the buffer size is set to 250k (aka 6M / 24). If this is starting to cause underflows, increase the size.

There will still be some fluctuation. If you actually want to pad everything with nulls to make the bitrate even more constant, you can output an MPEG-TS stream (-f mpegts with an output file that ends in .ts) and use -muxrate X to pad to X bps. The actual H.264 bitstream is not changed.

Note that CBR mode significantly reduces quality, so you'll need to increase bitrate significantly to compensate. In general, you should avoid CBR mode unless you have a very good reason.

New contributor
forest is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.