• opencv和ffmpeg查询视频信息(python)


    1. 用Opencv获取

    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)
            return_value = {"width" : int(width),
                            "height": int(height),
                            "num_frames": int(num_frames),
                            "fps" : int(fps) if not fps == float('inf') else 15}
            # print("{} 
    height:{} 
    fps:{} 
    num_frames:{}".format(width, height, fps, num_frames))
            # show_statisrics(source_name, fps, width, height, num_frames)
        except (OSError, TypeError, ValueError, KeyError, SyntaxError) as e:
            print("init_source:{} error. {}
    ".format(source_name, str(e)))
            return_value = -1
        return return_value
    

    2. 用ffmpeg获取

    注意python运行如下代码有两个条件:

    • pip install ffmpeg-opencv
    • 在系统安装或者编译ffmpeg(windows、linux、macos)
    def get_source_info_ffmpeg(source_name):
        return_value = 0
        assert os.path.exists(source_name)
        try:
            info = ffmpeg.probe(source_name)
            # print(info)
            # print("---------------------------------")
            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 = int(vs['r_frame_rate'][:-2])
            width = vs['width']
            height = vs['height']
            duration = int(float(vs['duration']))
            # 如果只用ffmpeg,這裡不應該使用Opencv
            cap = cv2.VideoCapture(source_name)
            num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) if cap.get(cv2.CAP_PROP_FRAME_COUNT) else 
                duration * fps
    
            return_value = {"width": width,
                            "height": height,
                            "num_frames": duration * fps ,
                            "fps": int(fps)}
            show_statisrics(source_name, fps, width, height, num_frames)
            # print("format_name:{} 
    codec_name:{} 
    duration_ts:{} 
    {} 
    height:{} 
    fps:{}".format(format_name, codec_name, duration_ts, width, height, fps))
        except (OSError, TypeError, ValueError, KeyError, SyntaxError) as e:
            print("init_source:{} error. {}
    ".format(source_name, str(e)))
            return_value = 0
        return return_value
    

    3. 信息打印

    
    def show_statisrics(source_name, fps, width, height, num_frames):
        print("Video statistics:")
        print("  ----------------------------------------")
        print("  Items      | Info ")
        print("  ----------------------------------------")
        print("  Path       | {:>20s}  ".format(source_name) )
        print("  width      | {:20d} ".format(width) )
        print("  height     | {:20d} ".format(height) )
        print("  num_frames | {:20d} ".format(num_frames) )
        print("  fps        | {:>20d}  ".format(fps) )
        print("  ----------------------------------------")
    

    效果如下:

  • 相关阅读:
    vue模拟接口数据
    修改placeholder的颜色
    network is unreachable mongodb
    数字转时间
    前端下载流文件
    jquery的AJAX中各个事件执行顺序
    移动端谨慎使用overflow:hidden
    时间函数整理
    background-size使用时的注意点
    关于iframe
  • 原文地址:https://www.cnblogs.com/geoffreyone/p/13597838.html
Copyright © 2020-2023  润新知