【问题标题】:Python: how to get display aspect ratio from video?Python:如何从视频中获取显示纵横比?
【发布时间】:2018-01-26 02:09:33
【问题描述】:

我有一个视频,其中 ffmpeg 告诉我存储纵横比 (SAR) 为 4:3,但显示纵横比 DAR 为 16:9。分辨率为 1440x1080。 有没有机会用 Python-OpenCV 或任何其他包找出 16:9 的 DAR?

【问题讨论】:

    标签: python opencv video aspect-ratio


    【解决方案1】:

    我相信这适用于大多数视频(需要 ffmpeg 附带的 ffprobe)

    import subprocess
    import json
    def get_aspect_ratios(video_file):
        cmd = 'ffprobe -i "{}" -v quiet -print_format json -show_format -show_streams'.format(video_file)
    #     jsonstr = subprocess.getoutput(cmd)
        jsonstr = subprocess.check_output(cmd, shell=True, encoding='utf-8')
        r = json.loads(jsonstr)
        # look for "codec_type": "video". take the 1st one if there are mulitple
        video_stream_info = [x for x in r['streams'] if x['codec_type']=='video'][0]
        if 'display_aspect_ratio' in video_stream_info and video_stream_info['display_aspect_ratio']!="0:1":
            a,b = video_stream_info['display_aspect_ratio'].split(':')
            dar = int(a)/int(b)
        else:
            # some video do not have the info of 'display_aspect_ratio'
            w,h = video_stream_info['width'], video_stream_info['height']
            dar = int(w)/int(h)
            ## not sure if we should use this
            #cw,ch = video_stream_info['coded_width'], video_stream_info['coded_height']
            #sar = int(cw)/int(ch)
        if 'sample_aspect_ratio' in video_stream_info and video_stream_info['sample_aspect_ratio']!="0:1":
            # some video do not have the info of 'sample_aspect_ratio'
            a,b = video_stream_info['sample_aspect_ratio'].split(':')
            sar = int(a)/int(b)
        else:
            sar = dar
        par = dar/sar
        return dar, sar, par
    

    ------------ 旧答案--------------------------

    import subprocess
    import json
    cmd = "ffprobe -i D:/out.mp4  -v quiet -print_format json -show_format -show_streams"
    jsonstr = subprocess.getoutput(cmd)
    r = json.loads(jsonstr)
    a,b = r['streams'][0]['display_aspect_ratio'].split(':')
    dar = int(a)/int(b)
    print(a, b, dar)
    

    【讨论】:

      【解决方案2】:

      Storage Aspect Ratio 是图像的宽高比,以像素为单位,可以很容易地从视频文件中计算出来。

      Display Aspect Ratio是图像在屏幕上显示时的宽高比(以厘米或英寸等长度为单位),由Pixel Aspect Ratio和Storage的组合计算得出纵横比。

      SAR × PAR = DAR。

      例如,640 × 480 VGA 图像的 SAR 为 640/480 = 4:3,如果在 4:3 显示器上显示 (DAR = 4:3),则具有方形像素,因此 PAR 为 1: 1.相比之下,720 × 576 D-1 PAL 图像的 SAR 为 720/576 = 5:4,但在 4:3 显示器上显示 (DAR = 4:3)。

      Source

      因此,使用 OpenCV 您可以获得 SAR(像素尺寸比),但我怀疑您是否可以从中获得 constant 显示纵横比(因为它取决于显示)。

      你可以做的是当displaying图像时,你可以得到window property,它有一个标志WND_PROP_ASPECT_RATIO

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-02
        • 2021-02-23
        • 2011-02-03
        • 2019-02-08
        • 1970-01-01
        • 2016-06-03
        • 2022-12-03
        • 2020-04-10
        相关资源
        最近更新 更多