Last active
October 31, 2018 17:30
-
-
Save yiminglin-ai/968ab41ecb4f3864830a8f6e6d317d08 to your computer and use it in GitHub Desktop.
[video misc] #python #video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
def findMetadaWithMediainfo(pathToInputVideo): | |
cmd = "mediainfo --Output=JSON" | |
args = shlex.split(cmd) | |
args.append(pathToInputVideo) | |
# run the ffprobe process, decode stdout into utf-8 & convert to JSON | |
mediainfoOutput = subprocess.check_output(args).decode('utf-8') | |
mediainfoOutput = json.loads(mediainfoOutput)['media']['track'] | |
for track in mediainfoOutput: | |
if track['@type'] == 'Video': | |
height = int(track['Height']) | |
width = int(track['Width']) | |
if 'BitRate' in track.keys(): | |
bit_rate = int(track['BitRate']) | |
else: | |
bit_rate = int(track['Bit rate']) | |
fps = float(track['FrameRate']) | |
# import pprint | |
# pp = pprint.PrettyPrinter(indent=2) | |
# pp.pprint(mediainfoOutput) | |
return height, width, fps, bit_rate | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment