文章目录
- 1,效果
- 2,ffmpeg获取视频信息
- 2,opencv获取视频信息
1,效果
2,ffmpeg获取视频信息
其中key:‘streams’对应的值是一个list,list中有两个dict类型的值,分别表示视频中视频流和音频流的相关信息。
key:‘format’对应的值是一个dict,其中包含了视频的相关的格式信息、视频时长信息、文件大小信息等。
import ffmpeg
def get_source_info_ffmpeg(source_name):
return_value = 0
try:
info = ffmpeg.probe(source_name)
vs = next(c for c in info['streams'] if c['codec_type'] == 'video')
format_name = info['format']['format_name']
codec_name = vs['codec_name']
duration_ts = float(vs['duration_ts'])
fps = vs['r_frame_rate']
width = vs['width']
height = vs['height']
print("format_name:{} \ncodec_name:{} \nduration_ts:{} \nwidth:{} \nheight:{} \nfps:{}".format(format_name, codec_name, duration_ts, width, height, fps))
except (OSError, TypeError, ValueError, KeyError, SyntaxError) as e:
print("init_source:{} error. {}\n".format(source_name, str(e)))
return_value = 0
return return_value
def main():
source_name = "/nfs-data/wangxy/wangxy/pipeline_data/2.avi"
get_source_info_ffmpeg(source_name)
if __name__ == "__main__":
main()
"""
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "Main",
"codec_type": "video",
"codec_time_base": "1001/48000",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 1024,
"height": 576,
"coded_width": 1024,
"coded_height": 576,
"has_b_frames": 1,
"sample_aspect_ratio": "1:1",
"display_aspect_ratio": "16:9",
"pix_fmt": "yuv420p",
"level": 31,
"color_range": "tv",
"color_space": "bt709",
"color_transfer": "bt709",
"color_primaries": "bt709",
"chroma_location": "left",
"refs": 1,
"is_avc": "true",
"nal_length_size": "4",
"r_frame_rate": "24000/1001",
"avg_frame_rate": "24000/1001",
"time_base": "1/48000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 2883840,
"duration": "60.080000",
"bit_rate": "944746",
"bits_per_raw_sample": "8",
"nb_frames": "1515",
"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": "und",
"handler_name": "Video Media Handler"
}
},
{
"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": 2646441,
"duration": "60.010000",
"bit_rate": "125617",
"max_bit_rate": "125617",
"nb_frames": "2721",
"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": "und",
"handler_name": "GPAC ISO Audio Handler"
}
}
],
"format": {
"filename": "/Users/didi/Desktop/Python/Python3/code/section_35/data/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": "63.189000",
"size": "8500415",
"bit_rate": "1076189",
"probe_score": 100,
"tags": {
"major_brand": "isom",
"minor_version": "512",
"compatible_brands": "isomiso2avc1mp41",
"encoder": "Lavf58.20.100"
}
}
}
"""
2,opencv获取视频信息
from cv2 import cv2
def get_source_info_opencv(source_name):
return_value = 0
try:
cap = cv2.VideoCapture(source_name)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH )
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv2.CAP_PROP_FPS)
num_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
print("width:{} \nheight:{} \nfps:{} \nnum_frames:{}".format(width, height, fps, num_frames))
except (OSError, TypeError, ValueError, KeyError, SyntaxError) as e:
print("init_source:{} error. {}\n".format(source_name, str(e)))
return_value = -1
return return_value
def main():
source_name = "/nfs-data/wangxy/wangxy/pipeline_data/2.avi"
get_source_info_opencv(source_name)
if __name__ == "__main__":
main()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)