Getting video dimension / resolution / width x height from ffmpeg

前端 未结 8 1587
粉色の甜心
粉色の甜心 2020-11-27 05:33

How would I get the height and width of a video from ffmpeg\'s information output. For example, with the following output:

$ ffmpeg -i video.mp4         


        
8条回答
  •  面向向阳花
    2020-11-27 05:59

    As mentioned here, ffprobe provides a way of retrieving data about a video file. I found the following command useful ffprobe -v quiet -print_format json -show_streams input-video.xxx to see what sort of data you can checkout.

    I then wrote a function that runs the above command and returns the height and width of the video file:

    import subprocess
    import shlex
    import json
    
    # function to find the resolution of the input video file
    def findVideoResolution(pathToInputVideo):
        cmd = "ffprobe -v quiet -print_format json -show_streams"
        args = shlex.split(cmd)
        args.append(pathToInputVideo)
        # run the ffprobe process, decode stdout into utf-8 & convert to JSON
        ffprobeOutput = subprocess.check_output(args).decode('utf-8')
        ffprobeOutput = json.loads(ffprobeOutput)
    
        # find height and width
        height = ffprobeOutput['streams'][0]['height']
        width = ffprobeOutput['streams'][0]['width']
    
        return height, width
    

提交回复
热议问题