December 26, 2016

Setting constant bitrate for x264 encoder

Live Transcoder for Nimble Streamer has wide range of transcoding capabilities which include H.264 encoding with x264 library licensed for commercial usage by our company so any customer with our Transcoder may use x264 parameters to set up outgoing stream.

This article answers a popular question of our customers - "How can I set up constant bitrate for my streams?" - using x264 encoder settings. This encoder is also known as libx264.

Let us give a couple of short answers and then a full description.

How to set up CRF (Constant Rate Factor) with maximum bitrate

As you may have seen from our screencasts - such as UI sneak preview for ABR scenario setup - you can use web UI to set up transcoding scenario with source streams, transformation blocks and encoder. You can see blue block being sources of streams, green blocks for filters to transform the content and orange blocks as outgoing streams encoders. If you point your mouse to any block, you'll see setup icon - you can click on it to see details dialog.

Click on the orange block (that is the encoder settings box) and set the following custom fields:
  • crf to 20
  • vbv-maxrate to 400
  • vbv-bufsize to 1835
This will set maximum bit rate to 400Kbps with CRF of 20.

This is an equivalent of the following FFmpeg parameters:
-crf 20 -maxrate 400k -bufsize 1835k

How to set up CBR (Constant Bit Rate)

The constant bitrate can be set up almost the same way, in the orange encoder block. If you need bitrate 4Mbps, set the values as follows:

  • bitrate to 4000
  • vbv-maxrate to 4000
  • vbv-bufsize to 4000

This is an equivalent of the following FFmpeg parameters:
-b:v 4000k -minrate 4000k -maxrate 4000k -bufsize 4000k

See the following sections to explanations.

Some internals

Nimble Streamer Live Transcoder uses libx264 which has some flexibility to control the bitrate. As described in FFmpeg docs which uses libx264 as well, the CBR is not supported directly due to very complex codec logic but we can emulate and set maximum bitrate.

If you set vbv-maxrate and vbv-bufsize to something basic like the H.264 High Profile @ Level 4.1 limitations, the encoder will still operate in ABR mode, but will constrain itself to not go outside these specifications.

If you set vbv-maxrate to the same value as bitrate, then the encoder will operate in CBR mode. Notice that it's not a strict CBR where every picture has the same size. vbv-bufsize controls the size of the buffer which allows for bitrate variance while still staying inside the CBR limitations.

If you set only "bitrate" parameter then encoder will work in unconstrained VBR mode, having the parameter value as a target but not as fixed value.

Minimum bitrate (minrate)

As for lower bitrate threshold, the library will need to increase quality if average quality cannot reach min-rate, but in case if max possible quality still cannot fill bandwidth gap you will have bitrate lower than you set. Hence "-minrate 4000k" parameter in the example above will just not work - it's not used for libx264 and was added to FFmpeg guide accidentally. We've checked ffmpeg code - it's just a bug in the docs.

Some cases of FPS affecting bitrate

Please also check Handling fuzzy FPS to get proper bitrate output article which covers cases when frame rate may affect output bitrate.

To get some more details please read this FFmpeg docs page and also this forum thread.

Feel free to visit Live Transcoder webpage for other transcoding features description and contact us if you have any question.

Related documentation

No comments:

Post a Comment

If you face any specific issue or want to ask some question to our team,

This will give much faster and precise response.
Thank you.

Note: Only a member of this blog may post a comment.