Skip to content

Instantly share code, notes, and snippets.

@fossbalaji
Last active Sep 19, 2021
Embed
What would you like to do?
How to get video file duration and resolution in python
from subprocess import Popen, PIPE
import re
def getvideodetails(filepath):
cmd = "avconv -i %s" % filepath
p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
di = p.communicate()
for line in di:
if line.rfind("Duration") > 0:
duration = re.findall("Duration: (\d+:\d+:[\d.]+)", line)[0]
if line.rfind("Video") > 0:
resolution = re.findall("(\d+x\d+)", line)[0]
return duration,resolution
# call function with file path
getvideodetails("/home/bala/sample.mp4") # change this line to your video file path
@fossbalaji
Copy link
Author

fossbalaji commented Feb 25, 2016

Incase avconv command not found, In debain based systems
sudo apt-get install libav-tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment