由于最近测试画中画的功能,需要构造一批特殊的视频和图片进行异常场景的测试,现总结如下:
1.查看视频基本信息
首先将被测视频放在指定目录(/Users/ceshi/Downloads/a.mp4),然后打开终端输入如下命令:
cd /Users/ceshi/Downloads/ mediainfo a.mp4 ffprobe -v quiet -print_format json -show_format -show_streams -show_error -show_chapters a.mp4
输出结果如下:
{
"streams":[
{
"index":0,
"codec_name":"h264",
"codec_long_name":"H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile":"High",
"codec_type":"video",
"codec_tag_string":"avc1",
"codec_tag":"0x31637661",
"width":720,
"height":1280,
"coded_width":720,
"coded_height":1280,
"closed_captions":0,
"film_grain":0,
"has_b_frames":2,
"sample_aspect_ratio":"1:1",
"display_aspect_ratio":"9:16",
"pix_fmt":"yuv420p",
"level":32,
"color_range":"tv",
"color_space":"bt709",
"color_transfer":"bt709",
"color_primaries":"bt709",
"chroma_location":"left",
"field_order":"progressive",
"refs":1,
"is_avc":"true",
"nal_length_size":"4",
"id":"0x1",
"r_frame_rate":"60/1",
"avg_frame_rate":"3940/83",
"time_base":"1/15360",
"start_pts":0,
"start_time":"0.000000",
"duration_ts":318720,
"duration":"20.750000",
"bit_rate":"1790467",
"bits_per_raw_sample":"8",
"nb_frames":"985",
"extradata_size":49,
"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,
"captions":0,
"descriptions":0,
"metadata":0,
"dependent":0,
"still_image":0
},
"tags":{
"language":"und",
"handler_name":"VideoHandler",
"vendor_id":"[0][0][0][0]"
}
},
{
"index":1,
"codec_name":"aac",
"codec_long_name":"AAC (Advanced Audio Coding)",
"profile":"LC",
"codec_type":"audio",
"codec_tag_string":"mp4a",
"codec_tag":"0x6134706d",
"sample_fmt":"fltp",
"sample_rate":"44100",
"channels":2,
"channel_layout":"stereo",
"bits_per_sample":0,
"id":"0x2",
"r_frame_rate":"0/0",
"avg_frame_rate":"0/0",
"time_base":"1/44100",
"start_pts":0,
"start_time":"0.000000",
"duration_ts":917456,
"duration":"20.803991",
"bit_rate":"128009",
"nb_frames":"898",
"extradata_size":2,
"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,
"captions":0,
"descriptions":0,
"metadata":0,
"dependent":0,
"still_image":0
},
"tags":{
"language":"und",
"handler_name":"SoundHandler",
"vendor_id":"[0][0][0][0]"
}
}
],
"chapters":[
],
"format":{
"filename":"a.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":"20.851000",
"size":"5010419",
"bit_rate":"1922370",
"probe_score":100,
"tags":{
"major_brand":"isom",
"minor_version":"512",
"compatible_brands":"isomiso2avc1mp41",
"comment":"vid:v0200fg10000c8rfcnbc77u6dmhs6mo0",
"encoder":"Lavf58.45.100"
}
}
}
2.查看视频I帧/B帧/P帧的分布及数量
查看视频总帧数
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 a.mp4
查看每个帧的分布
ffprobe -show_frames a.mp4 | grep pict_type ffprobe -show_frames -i a.mp4 | grep "pict_type=" > a.txt
关于视频的I帧/B帧/P帧数量可以用如下Python脚本统计
# coding=utf-8
# 打开文件
f = open('/Users/ceshi/Downloads/a.txt', 'r', encoding='UTF-8')
# 读取文件所有行
content = f.readlines()
contentLines = ''
frame = ["I", "P", "B"]
rate = {}
# 依次迭代所有行
for line in content:
# 去除空格
line = line.strip()
# 如果是空行则跳过
if len(line) == 0:
continue
contentLines = contentLines + line
for x in range(0, len(line)):
if line[x] not in rate:
rate[line[x]] = 0
rate[line[x]] += 1
rate = sorted(rate.items(), key=lambda e: e[1], reverse=True)
for i in rate:
if i[0] in frame:
print(i[0], "帧的数量:", i[1])
f.close()
输出结果:
/Users/ceshi/PycharmProjects/pythonProject/venv/bin/python /Users/ceshi/PycharmProjects/PythonProject/count.py B 帧的数量: 705 P 帧的数量: 274 I 帧的数量: 6 Process finished with exit code 0
查看key_frame分布
ffprobe -show_frames a.mp4 | grep key_frame
查看关键帧数量
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 -skip_frame nokey a.mp4
查看关键帧所在帧数
ffprobe -v error -select_streams v -show_frames -show_entries frame=pict_type -of csv a.mp4 | grep -n I | cut -d ':' -f 1 > d.txt
关于GOP间隔可以用如下Python脚本计算:
# coding=utf-8
newList = []
doneList = []
def diffList():
f = open('/Users/ceshi/Downloads/d.txt', 'r', encoding='UTF-8')
content = f.readlines()
for line in content:
line = line.strip()
if len(line) == 0:
continue
newList.append(int(line))
f.close()
for i, j in zip(newList[0::], newList[1::]):
doneList.append(j - i)
print(doneList)
diffList()
输出结果:
/Users/ceshi/PycharmProjects/pythonProject/venv/bin/python /Users/ceshi/PycharmProjects/PythonProject/testnum.py [242, 232, 75, 234, 113] Process finished with exit code 0
3.修改视频的GOP大小和B帧数量
设置关键帧和B帧数量
ffmpeg -i a.mp4 -vcodec libx264 -bf 0 -g 1 -x264-params "keyint=250:min-keyint=50" -crf 26 -an -y a.mp4
-bf 0 : B帧数量
keyint :关键帧的最大间隔,即GOP长度
min-keyint :关键帧的最小间隔
说明:
帧率FPS=视频帧总数/视频时长
GOP设置的建议;[2*FPS,4*FPS]



