Skip to content

Instantly share code, notes, and snippets.

@fossbalaji
Last active September 19, 2021 16:27
Show Gist options
  • Save fossbalaji/c27d294707f5444ff176 to your computer and use it in GitHub Desktop.
Save fossbalaji/c27d294707f5444ff176 to your computer and use it in GitHub Desktop.
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

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