Created
July 17, 2016 23:14
-
-
Save Hellowlol/d2d459420e4491ef83349be9361b96dd to your computer and use it in GitHub Desktop.
Parser for mediainfo cli in python
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
import subprocess | |
import json | |
def get_media_info(path, format='dict'): | |
""" Note this is media info cli """ | |
cmd = 'mediainfo "%s"' % (path) | |
process = subprocess.Popen(cmd, | |
shell=False, | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
stderr=None) | |
o, e = process.communicate() | |
if format == 'raw': | |
return o | |
mains = {} | |
# make a dict of it | |
for l in o.splitlines()[:-1]: | |
if ':' not in l and l != '': | |
# We assume this is main keys | |
cat = l.strip('\r') | |
mains[cat] = '' | |
sub = {} | |
elif l == '': | |
mains[cat] = sub | |
elif ':' in l: | |
z = l.split(':', 1) | |
k = z[0].strip('\r').strip() | |
v = z[1].strip('\r').strip() | |
sub[k] = v | |
mains['raw_string'] = o | |
if format == 'json': | |
return json.dumps(mains) | |
return mains |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment