How to ensure quality when converting ffmpeg audio and video formats

When we convert videos in multiple formats into flv format, what we focus on is the quality and size of the converted flv video. Let me share with you my practical results, mainly analyzing the four formats of avi, 3gp, mp4 and wmv.

Usually when using ffmpeg for video conversion, the main parameters we use to control audio and video are as follows:

Audio parameters:
-ab bitrate Set audio bitrate
-acodec codec Use codec codec
-ac channels < strong>Set channel, the default is 1
-ar freq Set audio sampling rate
Video parameters:
-r fps Set the frame rate, Default 25
-b bitrate Set bit rate, default 200kb/s
-qscale 6 or 4 Use dynamic bitrate to set
Other parameters such as -i, -y, -s are basically well understood. The first four parameters we mentioned above are mainly used to set audio, and the last three are mainly used to set video. For the audio ar (sampling rate), you can specify 22050, 24000, 44100 or 48000. Generally speaking, the latter two are more common; for the audio ab (code rate), you usually choose 32, 64, 96 or 128; Acodec for audio specifies the encoding method, which can be specified as libmp3lame or aac, etc. These two refer to audio using mp3 and aac formats respectively. During the actual test process, I found that specifying ab as 64 or higher and ar as 22050 or higher when using mp3 for audio encoding has no big impact on the actual video sound quality. Please see the test results below.

For video, r is used to specify the frame rate, usually set to 29.97; for video b (video bitrate), you can specify numbers such as 128, 314, etc., and it has no effect for videos above 1500; for video qscale, it is a dynamic bit rate , can be set to 4 or 6, 4 has higher quality than 6. During the test, I found that setting the -b parameter did not change the quality of the video too much (specifying large or small is almost the same or the same), but setting qscale is completely different. If we want to obtain high quality, we should use Dynamic bitrate instead of specifying the -b parameter. Of course, the disadvantage of using dynamic bitrate is that the video size becomes larger. When using dynamic bitrate, the effect of using 6 is similar to 4, but the size is reduced, so it is usually recommended to use 6 to set the dynamic bitrate.

The above introduces the configuration parameters of (https://so.csdn.net/so/search?q=audio and video&spm=1001.2101.3001.7020) during the conversion process. Based on the above, we are converting The usual commands are as follows:
High quality: ffmpeg -i infile -ab 128 -acodec libmp3lame -ac 1 -ar 22050 -r 29.97 -qscale 6 -y outfile
Low quality: ffmpeg -i infile -ab 128 -acodec libmp3lame -ac 1 -ar 22050 -r 29.97 -b 512 -y outfile

The results of using these two commands to test the four formats of video are as follows. At the same time, the corresponding parameters will be changed during the test. The information of the four videos is as follows:
AVI format: test.avi 14.1M
3GP format: test.3gp 4.30M
MP4 format: test.mp4 16.4M
WMV format: test.wmv 15.0M

1. The test results using high-quality scripts are as follows:
After AVI format conversion: -qscale 6 result=avi_01.flv 6.61M -qscale 4 result=avi_02.flv 9.30M
After 3GP format conversion: -qscale 6 result=3gp_01.flv 4.21M -qscale 4 result=3gp_02.flv 5.30M
After MP4 format conversion: -qscale 6 result=mp4_01.flv 13.1M -qscale 4 result=mp4_02.flv 17.5M
After WMV format conversion: -qscale 6 result=wmv_01.flv 15.9M -qscale 4 result=wmv_02.flv 22.3M

2. The test results using low-quality scripts are as follows:
After AVI format conversion: -b 512 result=avi_03.flv 2.87M -b 1000 result=avi_04.flv 2.87M
After 3GP format conversion: -b 512 result=3gp_03.flv 1.73M -b 1000 result=3gp_04.flv 1.73M
After MP4 format conversion: -b 512 result=mp4_03.flv 5.84M -b 1000 result=mp4_04.flv 5.84M
After WMV format conversion: -b 512 result=wmv_03.flv 6.17M -b 1000 result=wmv_04.flv 6.17M

3. The compression ratio comparison is as follows:
After AVI format conversion: -qscale 6 result=53.12% -qscale 4 result=34.04% -b 512/1000 result=79.65%
After 3GP format conversion: -qscale 6 result=2.093% -qscale 4 result=123.1% -b 512/1000 result=59.77%
After MP4 format conversion: -qscale 6 result=20.12% -qscale 4 result=106.7% -b 512/1000 result=64.39%
After WMV format conversion: -qscale 6 result=106.0% -qscale 4 result=148.7% -b 512/1000 result=58.87%

By comparing the compression sizes before and after, we can see the huge difference between using high-quality conversion and using low-quality conversion when the audio remains consistent. At the same time, different formats of video will also appear during the conversion process. A relatively big difference. Of course, these data do not necessarily represent the same for all videos in these formats. The specifics depend on your application. However, what we can be sure of is that the AVI format video has the highest compression rate after being converted to FLV format, but the results are not certain for AVI with different encodings. At the same time, we have also seen that the volume of videos in certain formats is larger than the original after conversion. We need to pay attention to this when applying.

The following are pictures taken before and after the conversion, you can take a look at them for reference.

1. Before AVI format interception, the three screenshots using -qscale 6 and -b 512 are as follows:

The three screenshots before 2.3GP format interception using -qscale 6 and -b 512 are as follows:

3. The three screenshots taken before capturing in MP4 format using -qscale 6 and -b 512 are as follows:

4. Before WMV format interception, the three screenshots using -qscale 6 and -b 512 are as follows:

There is no test of RM and RMVB formats here because ffmpeg cannot directly convert videos in these two formats. Next time I will use mencoder to test the analysis of converting videos in RM and RMVB formats to FLV.