Skip to content

Instantly share code, notes, and snippets.

@Hellowlol
Created July 17, 2016 23:14
Show Gist options
  • Save Hellowlol/d2d459420e4491ef83349be9361b96dd to your computer and use it in GitHub Desktop.
Save Hellowlol/d2d459420e4491ef83349be9361b96dd to your computer and use it in GitHub Desktop.
Parser for mediainfo cli in python
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