Getting the dimensions of a video file in Python
written by Ian McCracken
at Sunday, April 13, 2008
There might be a better way to do this, but in a pinch, it works, as long as you have ffmpeg installed:
Remember it's stderr, not stdout, because ffmpeg fails if you don't provide an output file. We don't really care, though, because it still prints out the stats of the infile.
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:1])
else:
x = y = 0
return x, y
September 13, 2011 at 4:37 PM
should that not read:
x, y = map(int, match.groups()[0:2])
June 29, 2012 at 5:51 PM
What about size with four digits? it seems to be hard coded as 3.
April 6, 2013 at 7:23 PM
This comment has been removed by the author.
April 6, 2013 at 7:23 PM
Try this regex:
Stream.*Video.*, ([0-9]+)x([0-9]+)
October 9, 2014 at 10:53 AM
This regex will get the resolution properly
Stream.*Video.*\s([0-9]+)x([0-9]+)
Without the \s in the middle, the preceding . will pick up whatever /d characters you don't explicitly ask for in the first grouped statement.
March 8, 2015 at 1:37 PM
jasa internet murah
rokan hulu
Jasa SEO Backlink Surabaya
March 8, 2015 at 1:38 PM
i think this CODE is not properly. . . please check again