Determining the optimal bitrate to use to deliver video is difficult even if you've been streaming for years. Bandwidth costs money, so you want to use smallest the bitrate possible without compromising the quality of the content. There are two standard methods you can use to determine the bitrate to generate for streamed video: average bits per pixel and constant rate factor.
Use average bits per pixel
Bits per pixel (BPP) is the average amount of data applied to each pixel in a video file. It's calculated by multiplying the resolution of the video (its width times its height, in pixels) times the frame rate (the number of frames per second), and dividing by the per-second video data rate (kbps):
(resolution * frames per second) / data rate = BPP
BPP typically ranges from 0.05 to 0.150, depending on the amount of motion in the scene. The more motion there is, the higher the BPP should be.
BPP is also inversely proportional to resolution: The lower the resolution, the higher the BPP can be without generating huge bitrates. That's because there are fewer total pixels to duplicate, or reference, in a lower resolution video than in a higher resolution video.
You can calculate a video's BPP manually, or extract it using the open-source tool MediaInfo. To calculate the size of a bitrate based on BPP, use the formula:
(resolution * frames per second * BPP) / 1000 = bitrate
Say, for example, your stream's resolution is 1920x1080 and it's captured at 25 fps. It's a basketball game, with players zipping up and down the court and taking shots. Such an action-heavy content has a high BPP.
1920 * 1080 * 25 * 0.150 / 1000 = 7776 kbps
That's a hefty bitrate. It will eat up bandwidth and may not play smoothly across lower-bandwidth connections. You may decide to deliver the video at a lower resolution:
1280 * 720 * 25 * 0.150 / 1000 = 3456 kbps
However, if you're streaming content with less action—say, a lecturer behind a podium, with no camera pans or cuts to audience shots—may have a much lower average BPP and can encode it at a higher resolution:
1920 * 1080 * 25 * 0.075 / 1000 = 3888 kbps
Here are some other sample BPP-based bitrate calculations, for contrast:
1280 * 720 * 25 * 0.080 / 1000 = 1843 kbps
854 * 480 * 25 * 0.100 / 1000 = 1024 kbps
480 * 360 * 25 * 0.125 / 1000 = 540 kbps
426 * 240 * 25 * 0.133 / 1000 = 340 kbps
284 * 160 * 25 * 0.150 / 1000 = 170 kbps
Use constant rate factor
Constant rate factor (CRF) is a quality- and rate-control scale for H.264 and H.265 encoders. For 8-bit video, the value is between 0 and 51, where lower values result in less compression and higher quality, and higher values result in more compression, lower quality, and smaller file sizes.
Use CRF to manage customer expections and to generate a target bitrate that prioritizes quality over file size when analyzing your content for delivery. Never deliver actual CRF content, live or VOD, because it can result in larger bitrates that clog the network, cause clients to buffer out, and use wildly unpredictable segment sizes. Once you have created the CRF file, use the bitrate as the target bitrate for your output file.
An FFmpeg command line that creates a CRF file to find a target bitrate might look something like this:
ffmpeg -i [input] -pix_fmt yuv420p -deinterlace -vf "scale=640:360" -vsync 1 -vcodec libx264 -r 29.970 -threads 0 -crf 23 -preset veryfast -profile:v baseline -tune film -g 60 -sc_threshold 0 -an -f mp4 -y outputfile.mp4
Let's look at some of the relevant options in the encoding.
The -crf option uses the default value of 23. The CRF range increments exponentially, so increasing the value by 6 results in about half the bitrate and decreasing by 6 roughly doubles the bitrate. Typically you'll use a CRF of between 17 and 28.
Note: It’s important to process the entire video from the beginning to the end and evaluate the resulting bitrate holistically, as the amount of motion and BPP may vary over the course of the video.
Also notice the -preset and -profile:v options. The veryfast preset and the baseline profile provide a good balance of speed with very little compression. The -tune option allows you to refine the encoding for different types of content, such as for film or animation. Finally, audio has also been excluded from this output using the -an option, as it's redundant and can slow the video encoding.
Note: For more information on CRF encoding options, see the FFmpeg H.264 video encoding guide.
If desired, you can perform mulltipass encoding (using the -pass 1 and -pass 2 options) instead. With multipass processing, the encoder first scans the entire video file to determine the complexity (motion and detail) of the content. In the second pass it encodes the content to optimize the quality over the entire file. Two-pass encoding is good for encoding for specific target bitrate; the downside is that it takes longer to process.
- How to live stream using FFmpeg with Wowza Streaming Engine
- How to encode VOD using FFmpeg with Wowza Streaming Engine
- How to re-stream using FFmpeg with Wowza Streaming Engine
If you're having problems or want to discuss this article, post in our forum.