Skip to content

Instantly share code, notes, and snippets.

@bviews
Forked from oldo/video-metada-finder.py
Created November 14, 2016 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bviews/780dd45dde0b66e0df6fb3b29d08acd9 to your computer and use it in GitHub Desktop.
Save bviews/780dd45dde0b66e0df6fb3b29d08acd9 to your computer and use it in GitHub Desktop.
A python function utilising `ffprobe` to find any metadata related to a video file. Examples of what it can find include bitrate, fps, codec details, duration and many more. This gist returns the video height and width as an example.
#!/usr/local/bin/python3
import subprocess
import shlex
import json
# function to find the resolution of the input video file
def findVideoMetada(pathToInputVideo):
cmd = "ffprobe -v quiet -print_format json -show_streams"
args = shlex.split(cmd)
args.append(pathToInputVideo)
# run the ffprobe process, decode stdout into utf-8 & convert to JSON
ffprobeOutput = subprocess.check_output(args).decode('utf-8')
ffprobeOutput = json.loads(ffprobeOutput)
# prints all the metadata available:
import pprint
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(ffprobeOutput)
# for example, find height and width
height = ffprobeOutput['streams'][0]['height']
width = ffprobeOutput['streams'][0]['width']
print(height, width)
return height, width
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment