H.264有四種畫質級別,分別是baseline, extended, main, high:
- 1、Baseline Profile:基本畫質。支持I/P 幀,只支持無交錯(Progressive)和CAVLC;
- 2、Extended profile:進階畫質。支持I/P/B/SP/SI 幀,只支持無交錯(Progressive)和CAVLC;(用的少)
- 3、Main profile:主流畫質。提供I/P/B 幀,支持無交錯(Progressive)和交錯(Interlaced), 也支持CAVLC 和CABAC 的支持;
- 4、High profile:高級畫質。在main Profile 的基礎上增加了8x8內部預測、自定義量化、 無損視頻編碼和更多的YUV 格式;
H.264 Baseline profile、Extended profile和Main profile都是針對8位樣本數據、4:2:0格式(YUV)的視頻序列。在相同配置情況下,High profile(HP)可以比Main profile(MP)降低10%的碼率。 根據應用領域的不同,Baseline profile多應用於實時通信領域,Main profile多應用於流媒體領域,High profile則多應用於廣電和存儲領域。
下圖清楚的給出不同的profile&level的性能區別。
profile 主要參數:
Level 主要參數:
ffmpeg如何控制profile&level
ffmpeg -i input.mp4 -profile:v baseline -level 3.0 output.mp4 ffmpeg -i input.mp4 -profile:v main -level 4.2 output.mp4 ffmpeg -i input.mp4 -profile:v high -level 5.1 output.mp4
如果ffmpeg編譯時加了external的libx264,那就這么寫:
ffmpeg -i input.mp4 -c:v libx264 -x264-params "profile=high:level=3.0" output.mp4
從壓縮比例來說,baseline< main < high ,對於帶寬比較局限的在線視頻,可能會選擇high,但有些時候,做個小視頻,希望所有的設備基本都能解碼(有些低端設備或早期的設備只能解碼baseline),那就犧牲文件大小吧,用baseline。自己取舍吧!
蘋果的設備對不同profile的支持。
2. 編碼效率和視頻質量的取舍(preset, crf)
除了上面提到的,強行配置biterate,或者強行配置profile/level,還有2個參數可以控制編碼效率。
一個是preset,一個是crf。
preset也挺粗暴,基本原則就是,如果你覺得編碼太快或太慢了,想改改,可以用profile。
preset有如下參數可用:
ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow and placebo.
編碼加快,意味着信息丟失越嚴重,輸出圖像質量越差。
CRF(Constant Rate Factor): 范圍 0-51: 0是編碼毫無丟失信息, 23 is 默認, 51 是最差的情況。相對合理的區間是18-28.
值越大,壓縮效率越高,但也意味着信息丟失越嚴重,輸出圖像質量越差。
舉個例子吧。
ffmpeg -i input -c:v libx264 -profile:v main -preset:v fast -level 3.1 -x264opts crf=18