通过命令解析视频获取视频参数
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,将视频角度旋转即可。