Search code examples
videoffmpegffprobe

Getting video dimension / resolution / width x height from ffmpeg


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
...
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 1
    compatible_brands: isomavc1
    creation_time   : 2010-01-24 00:55:16
  Duration: 00:00:35.08, start: 0.000000, bitrate: 354 kb/s
    Stream #0.0(und): Video: h264 (High), yuv420p, 640x360 [PAR 1:1 DAR 16:9], 597 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc
    Metadata:
      creation_time   : 2010-01-24 00:55:16
    Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16, 109 kb/s
    Metadata:
      creation_time   : 2010-01-24 00:55:17
At least one output file must be specified

How would I get height = 640, width= 360?


Solution

  • 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 :-)