Getting video dimension / resolution / width x height from ffmpeg

前端 未结 8 1474
粉色の甜心
粉色の甜心 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:46

    In this blog post theres a rough solution in python:

    import subprocess, re
    pattern = re.compile(r'Stream.*Video.*([0-9]{3,})x([0-9]{3,})')
    
    def get_size(pathtovideo):
        p = subprocess.Popen(['ffmpeg', '-i', pathtovideo],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        match = pattern.search(stderr)
        if match:
            x, y = map(int, match.groups()[0:2])
        else:
            x = y = 0
        return x, y
    

    This however assumes it's 3 digits x 3 digits (i.e. 854x480), you'll need to loop through the possible dimension lengths, such as (1280x720):

    possible_patterns = [re.compile(r'Stream.*Video.*([0-9]{4,})x([0-9]{4,})'), \
                re.compile(r'Stream.*Video.*([0-9]{4,})x([0-9]{3,})'), \
    re.compile(r'Stream.*Video.*([0-9]{3,})x([0-9]{3,})')]
    

    and check if match returns None on each step:

    for pattern in possible_patterns:
        match = pattern.search(stderr)
        if match!=None:
            x, y = map(int, match.groups()[0:2])
            break
    
    if match == None:
        print "COULD NOT GET VIDEO DIMENSIONS"
        x = y = 0
    
    return '%sx%s' % (x, y)
    

    Could be prettier, but works.

    0 讨论(0)
  • 2020-11-27 05:51

    BAD (\d+x\d+)

    $ echo 'Stream #0:0(eng): Video: mjpeg (jpeg / 0x6765706A), yuvj420p, 1280x720, 19939 kb/s, 30 fps, 30 tbr, 30 tbn, 30 tbc' | perl -lane 'print $1 if /(\d+x\d+)/'
    > 0x6765706
    

    GOOD ([0-9]{2,}x[0-9]+)

    $ echo 'Stream #0:0(eng): Video: mjpeg (jpeg / 0x6765706A), yuvj420p, 1280x720, 19939 kb/s, 30 fps, 30 tbr, 30 tbn, 30 tbc' | perl -lane 'print $1 if /([0-9]{2,}x[0-9]+)/'
    > 1280x720
    
    0 讨论(0)
  • 2020-11-27 05:58

    From Fredrik's tip above, here is how I did it using MediaInfo ( http://mediainfo.sourceforge.net/en ):

    >>> p1 = subprocess.Popen(['mediainfo', '--Inform=Video;%Width%x%Height%',         
        '/Users/david/Desktop/10stest720p.mov'],stdout=PIPE)
    >>> dimensions=p1.communicate()[0].strip('\n')
    >>> dimensions
    '1280x688'
    
    0 讨论(0)
  • 2020-11-27 05:58

    without re module

    out = error_message.split()               # make a list from resulting error string
    out.reverse()
    for index, item in enumerate(out):        # extract the item before item= "[PAR"
        if item == "[PAR":                      #
            dimension_string = out[i+1]          #
            video_width, video_height = dimension_string.split("x")
    

    Edit: not a good answer because not all videos have that "PAR" information :(

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-27 06:03

    Have a look at mediainfo Handles most of the formats out there.

    If you looking for a way to parse the output from ffmpeg, use the regexp \d+x\d+

    Example using perl:

    $ ./ffmpeg -i test020.3gp 2>&1 | perl -lane 'print $1 if /(\d+x\d+)/'
    176x120
    

    Example using python (not perfect):

    $ ./ffmpeg -i /nfshome/enilfre/pub/test020.3gp 2>&1 | python -c "import sys,re;[sys.stdout.write(str(re.findall(r'(\d+x\d+)', line))) for line in sys.stdin]"
    

    [][][][][][][][][][][][][][][][][][][]['176x120'][][][]

    Python one-liners aren't as catchy as perl ones :-)

    0 讨论(0)
提交回复
热议问题