Getting video dimension / resolution / width x height from ffmpeg

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


Use ffprobe

Example 1: With keys / variable names

ffprobe -v error -show_entries stream=width,height -of default=noprint_wrappers=1 input.mp4
width=1280
height=720

Example 2: Just width x height

ffprobe -v error -select_streams v -show_entries stream=width,height -of csv=p=0:s=x input.m4v
1280x720

Example 3: JSON

ffprobe -v error -select_streams v -show_entries stream=width,height -of json input.mkv 
{
    "programs": [

    ],
    "streams": [
        {
            "width": 1280,
            "height": 720
        }
    ]
}

Example 4: JSON Compact

ffprobe -v error -select_streams v -show_entries stream=width,height -of json=compact=1 input.mkv 
{
    "programs": [

    ],
    "streams": [
        { "width": 1280, "height": 720 }
    ]
}

Example 5: XML

ffprobe -v error -select_streams v -show_entries stream=width,height -of xml input.mkv 
<?xml version="1.0" encoding="UTF-8"?>
<ffprobe>
    <programs>
    </programs>

    <streams>
        <stream width="1280" height="720"/>
    </streams>
</ffprobe>

What the options do:

  • -v error Make a quiet output, but allow errors to be displayed. Excludes the usual generic FFmpeg output info including version, config, and input details.

  • -show_entries stream=width,height Just show the width and height stream information.

  • -of option chooses the output format (default, compact, csv, flat, ini, json, xml). See FFprobe Documentation: Writers for a description of each format and to view additional formatting options.

  • -select_streams v:0 This can be added in case your input contains multiple video streams. v:0 will select only the first video stream. Otherwise you'll get as many width and height outputs as there are video streams. -select_streams v can be used to show info from all video streams and avoid empty audio stream info in JSON and XML output.

  • See the FFprobe Documentation and FFmpeg Wiki: FFprobe Tips for more info.