CBR, CRF, and Changing Resolution using FFmpeg

In this article, we will go through the process of compressing a video using the H.264/AVC codec (libx264) and discuss the following: 

  • Creating a Constant Bitrate (CBR) encoded video 
  • Using the Constant Rate Factor (CRF) option to set desired quality
  • Change the resolution of the video to 1280x720 or 720p. 

For this purpose, we would be using a sample video with a resolution = 1920x1080 that can be downloaded at this link

Creating a Constant BitRate (CBR) Video

To understand CBR, we need to understand what is bitrate. Bitrate is the amount of information or data stored per second in any media file such as audio or video. In technical terms, it is the rate of the data output of an encoder. For example, 2200 kbps as a bitrate means that 2200 kilobits of data is stored per second. For a deeper discussion on bitrate, don’t miss this article on “bitrate vs. resolution”.

If the bitrate is “constant” throughout a video, it is termed as a CBR video. However, it is not possible to have constant bitrate for all the frames because of the different frame types and encoding priorities. Hence, if the bitrate is constant over a pre-determined time-period, the video is called a CBR-encoded video.

In order to output a constant bitrate video, we would be using the following FFmpeg output options : 

  • -b:v – Specifies the average target output bitrate of the video
  • -maxrate – Specifies the maximum bitrate tolerance 
  • -minrate – Specifies the minimum bitrate tolerance 
  • -bufsize – Specifies the rate control buffer size that helps to maintain the bitrate according to the values of b:v, maxrate and minrate 

After this, we also need to specify the video encoder using the -c:v option. As mentioned earlier, we are going to use the libx264 encoder in all the three cases. 

Let’s say, we need to have a CBR of 2200 kbps for our sample video. The FFmpeg command for the CBR conversion will be as follows : 

ffmpeg -i crowdrun.mp4 -b:v 2200k -maxrate 2200k -minrate 2200k -bufsize 2200k -c:v libx264 output.mp4 

Following are the screenshots of the above command:

FFMpeg CBR Encoding
FFMpeg CBR Encoding

If we try to analyze the bitrate of the output file, we would see that the difference between the highs and the lows of the bitrates in the file is very less. Ideally in CBR mode, the bitrate should be perfectly constant during the entire video but that is the ideal case and is not possible to achieve with software encoders. 

Related:  Audio Transcoding using FFmpeg - Change Audio Codecs using FFmpeg Easily

Using the CRF option to set desired quality 

The Constant Rate Factor or CRF is an option available in the libx264 encoder to set our desired output quality. It enables us to specify a target value that maps to a specific quality by adjusting the bitrates automatically based on the input video. 

This method is better if we want to have the same output quality for different kinds of input videos as FFmpeg would automatically adjust the bitrates based on the input video properties. On the other hand, the previous method will keep the same bitrate which will result in different output qualities for different inputs. 

The CRF value ranges from 0 to 51 where, 

  • 0 is lossless (best) with highest file size 
  • 23 is the default 
  • 51 is the poorest quality with lowest file size 

With low CRF value, the bitrate and hence, the file size of the video will increase. So it is important to decide the value based on the user requirements. 

Let’s say, we need a compressed output video with lesser file size, we can go with the value of 30. The FFmpeg command for this would be: 

ffmpeg -i crowdrun.mp4 -crf 30 -c:v libx264 output.mp4 

Following are the screenshots of the above command:

FFMpeg CRF encoding

FFmpeg CRF encoding

Change the resolution of the video to 1280×720 

FFmpeg provides a huge set of filters for various purposes. One of them is the scale filter that helps to scale the input to any resolution. 

The two parameters for this filter that we would be using are: 

  • w : It specifies the output width of the video 
  • h : It specifies the output height of the video 

Here, we would also add the CRF parameter(discussed in the last point) to compress and reduce the file size of the output video. 

The FFmpeg command for this would be: 

ffmpeg -i crowdrun.mp4 -vf scale=w=1280:h=720 -crf 30 -c:v libx264 output.mp4 

Following are the screenshots of the above command:

FFmpeg changing file resolution

Conclusion

In this article, we learned how to use FFmpeg to produce a CBR-encoded video, CRF-encoded video, and how to change the resolution of a file. These three operations are very important to any video compression system and we hope you are able to use them to further your video compression using FFmpeg.

Be the first to comment

Leave a Reply

Your email address will not be published.


*