How would I get the height and width of a video from ffmpeg
's information output. For example, with the following output --
$ ffmpeg -i 1video.mp4
...
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/david/Desktop/1video.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
? Thank you.
In this blog post theres a rough solution in python:
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):
and check if match returns None on each step:
Could be prettier, but works.
The best way for to answer this question would be for an ffmpeg developer to explain exactly what the format of the ffmpeg output is expected to be and whether we can consistently assume the size to be located in a specified context within it. Until then we can only guess from example what the format usually is.
Here's my attempt. It's verbose compared to these "one-liners", but that's because I'd like to know why it fails when it eventually does.
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:
Example using python (not perfect):
[][][][][][][][][][][][][][][][][][][]['176x120'][][][]
Python one-liners aren't as catchy as perl ones :-)
BAD (\d+x\d+)
GOOD ([0-9]{2,}x[0-9]+)
without re module
Edit: not a good answer because not all videos have that "PAR" information :(
As mentioned here,
ffprobe
provides a way of retrieving data about a video file. I found the following command usefulffprobe -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: