FFMPEG轉碼優化篇
在解決以上問題之前都需要通過ffmpeg對視頻的的編碼,屬性,以及各種參數有一個大致的瞭解。
通過命令解析視頻獲取視頻參數
ffprobe -v quiet -print_format json -show_format -show_streams test.mp4
獲取的參數格式大致如下:
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "Baseline",
"codec_type": "video",
"codec_time_base": "65899/3932160",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 720,
"height": 480,
"coded_width": 720,
"coded_height": 480,
"has_b_frames": 0,
"sample_aspect_ratio": "1:1",
"display_aspect_ratio": "3:2",
"pix_fmt": "yuv420p",
"level": 30,
"chroma_location": "left",
"refs": 1,
"is_avc": "true",
"nal_length_size": "4",
"r_frame_rate": "30/1",
"avg_frame_rate": "1966080/65899",
"time_base": "1/16384",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 65899,
"duration": "4.022156",
"bit_rate": "491008",
"bits_per_raw_sample": "8",
"nb_frames": "120",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"rotate": "90",
"language": "eng",
"handler_name": "VideoHandler"
},
"side_data_list": [
{
"side_data_type": "Display Matrix",
"displaymatrix": "\n00000000: 0 65536
0\n00000001: -65536 0 0\n00000002: 0
0 1073741824\n",
"rotation": -90
}
]
},
{
"index": 1,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "LC",
"codec_type": "audio",
"codec_time_base": "1/44100",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "fltp",
"sample_rate": "44100",
"channels": 2,
"channel_layout": "stereo",
"bits_per_sample": 0,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/44100",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 178385,
"duration": "4.045011",
"bit_rate": "112697",
"max_bit_rate": "128000",
"nb_frames": "175",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"language": "eng",
"handler_name": "SoundHandler"
}
}
],
"format": {
"filename": "1.mp4",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime / MOV",
"start_time": "0.000000",
"duration": "4.069000",
"size": "309658",
"bit_rate": "608813",
"probe_score": 100,
"tags": {
"major_brand": "isom",
"minor_version": "512",
"compatible_brands": "isomiso2avc1mp41",
"encoder": "Lavf57.71.100"
}
}
}
一,轉碼清晰度問題(無損轉碼黑屏,轉碼失真)
在我們轉碼時使用一般命令會發現轉碼之後畫面失真,清晰度變差,我們可以增加使用-vcodec copy指令來解決該問題,該指令的意思就是將視頻的原畫面直接拷貝過來,不會出現失真現象,使用該指令轉碼速度也會大大提升。
該指令存在一個問題,有時會出現轉碼之後畫面黑屏,只有聲音沒有畫面的現象。
通過命令解析視頻獲取視頻源文件參數
ffprobe -v quiet -print_format json -show_format -show_streams test.mp4
注意觀察codec_long_name參數,如果包含AVC說明該視頻爲h264編碼格式,則支持-vcodec copy,否則不支持,所以會出現轉碼黑屏的現象。這時爲了最大限度的減少清晰度的損失,使用-q:v:0.01。
二.手機拍攝的視頻轉碼角度旋轉。
手機豎屏拍攝的視頻在電腦上會發現視頻的角度出現了問題
同樣使用指令解析獲取視頻參數,參數中會有一個rotate的屬性,該屬性代表視頻被旋轉的角度。
此時我們使用轉碼命令-metadata:s:v rotate=90,將視頻角度旋轉即可。