Skip to content

Instantly share code, notes, and snippets.

Last active September 19, 2021 16:27
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
Copy link

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