首页 > 编程知识 正文

python ffprobe获取视频信息

时间:2023-05-04 14:08:01 阅读:278306 作者:22

背景
需要获取视频基础信息,如果分辨率、大小、播放时长等,研究发现ffprobe工具已经提供了非常全面的视频信息,用ffprobe分析视频很简单,且看我是怎么在python码中调用ffprobe来提取视频信息的

ffprobe 命令
我们先来看下ffprobe命令分析网络视频

ffprobe -print_format json -show_format -show_streams -v quiet https://1252524126.vod2.myqcloud.com/9764a7a5vodtransgzp1252524126/4e89d74d5285890792243257460/drm/voddrm.token.YTY4YThkNGE4ZDQ4NmVkNXRGWWRTTk1kMmwrUkF3VHZ6YUduS3diR1ZTRzlSZVJzSnVsajFBQ0VWREZtQ3VMbA.v.f230.m3u8?time=1592902124084



输出结果:

{    "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_time_base": "1/48",            "codec_tag_string": "[27][0][0][0]",            "codec_tag": "0x001b",            "width": 1280,            "height": 720,            "coded_width": 1280,            "coded_height": 720,            "has_b_frames": 2,            "sample_aspect_ratio": "1:1",            "display_aspect_ratio": "16:9",            "pix_fmt": "yuv420p",            "level": 31,            "chroma_location": "left",            "refs": 1,            "is_avc": "false",            "nal_length_size": "0",            "r_frame_rate": "24/1",            "avg_frame_rate": "24/1",            "time_base": "1/90000",            "start_pts": 7500,            "start_time": "0.083333",            "bits_per_raw_sample": "8",            "disposition": {                "default": 0,                "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": {                "variant_bitrate": "0"            }        },        {            "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": "[15][0][0][0]",            "codec_tag": "0x000f",            "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/90000",            "start_pts": 3320,            "start_time": "0.036889",            "disposition": {                "default": 0,                "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": {                "variant_bitrate": "0"            }        }    ],    "format": {        "filename": "https://1252524126.vod2.myqcloud.com/9764a7a5vodtransgzp1252524126/4e89d74d5285890792243257460/drm/voddrm.token.YTY4YThkNGE4ZDQ4NmVkNXRGWWRTTk1kMmwrUkF3VHZ6YUduS3diR1ZTRzlSZVJzSnVsNXRGWWRTTk1kMmwrUkF3VHZ6YUduSdiR1ZTRzlSZVJzSnVsajFBQ0VWREZtQ3VMbA.v.f230.m3u8?time=1592902124084",        "nb_streams": 2,        "nb_programs": 1,        "format_name": "hls",        "format_long_name": "Apple HTTP Live Streaming",        "start_time": "0.036889",        "duration": "225.005000",        "size": "14608",        "bit_rate": "519",        "probe_score": 100    }}





这里面已经可以提取到视频的基本信息了,我们只需要通过python调用ffprobe命令就可以了python调用ffprobe获取视频信息

import subprocess,jsonclass FFprobe():    def __init__(self):        self.filepath = ''        self._video_info = {}    def parse(self,filepath):        self.filepath = filepath        try:            res = subprocess.check_output(['ffprobe','-i',self.filepath,'-print_format','json','-show_format','-show_streams','-v','quiet'])            res = res.decode('utf8')            self._video_info = json.loads(res)            # print('_video_info ',self._video_info)        except Exception as e:            print(e)            raise Exception('获取视频信息失败')    def video_width_height(self):        streams = self._video_info['streams'][0]        return (streams['width'],streams['height'])    def video_filesize(self, format = 'gb'):        v_format = self._video_info['format']        size = int(v_format['size'])        kb=1024        mb=kb*1024        gb=mb*1024        tb=gb*1024        if size>=tb:            return "%.1f TB" % float(size / tb)        if size>=gb:            return "%.1f GB" % float(size / gb)        if size>=mb:            return "%.1f MB" % float(size / mb)        if size>=kb:            return "%.1f KB" % float(size / kb)    def video_full_frame(self):        stream = self._video_info['streams'][0]        return stream['nb_frames']    def video_time_length(self):        v_format = self._video_info['format']        return str(int(float(v_format['duration'])/3600)).__add__('小时').__add__(str(int(float(v_format['duration'])%3600/60))).__add__('分钟')    def video_info(self):        item = {            'path': self.filepath,            'height_width': self.video_width_height(),            'filesize': self.video_filesize(),            'time_length': self.video_time_length()        }        print('item = ', item)        return itemif __name__ == "__main__":    ffprobe = FFprobe()    ffprobe.parse('https://1252524126.vod2.myqcloud.com/9764a7a5vodtransgzp1252524126/4e89d74d5285890792243257460/drm/voddrm.token.MWVmNGRkjI2MTEzNDEzMVFxUFU4SEs3d3crUWREbTVTSk55YUhxRW1VTTBjVzMrTFN6T0VnbjhZZmZscTRVcQ.v.f230.m3u8?time=1592901564214')    print(ffprobe.video_info())


 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。