zoukankan      html  css  js  c++  java
  • 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("  ----------------------------------------")
    

    效果如下:

  • 相关阅读:
    ORA-1652: unable to extend temp segment by xxx in tablespace
    Oracle导入异常: unable to create INITIAL extent for segment in tablespace MY_DATA(转)
    EL表达式取值
    JSP错误:According to TLD or attribute directive in tag file, attribute value does not accept any expressions
    springmvc注解绑定参数心得
    springmvc绑定参数出现的异常
    springMVC参数的传递接收方式(转)
    springmvc注解@
    跟着开涛springmvc学习(转)
    CSS div文本垂直居中
  • 原文地址:https://www.cnblogs.com/geoffreyone/p/13597838.html
Copyright © 2011-2022 走看看