I have not found a nice library for Python but using hachoir
with subprocess
is a dirty workaround. You can grab the library itself from pip, the instructions for Python 3 are here: https://hachoir.readthedocs.io/en/latest/install.html
def get_media_properties(filename):
result = subprocess.Popen(['hachoir-metadata', filename, '--raw', '--level=3'],
stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
results = result.stdout.read().decode('utf-8').split('\r\n')
properties = {}
for item in results:
if item.startswith('- duration: '):
duration = item.lstrip('- duration: ')
if '.' in duration:
t = datetime.datetime.strptime(item.lstrip('- duration: '), '%H:%M:%S.%f')
else:
t = datetime.datetime.strptime(item.lstrip('- duration: '), '%H:%M:%S')
seconds = (t.microsecond / 1e6) + t.second + (t.minute * 60) + (t.hour * 3600)
properties['duration'] = round(seconds)
if item.startswith('- width: '):
properties['width'] = int(item.lstrip('- width: '))
if item.startswith('- height: '):
properties['height'] = int(item.lstrip('- height: '))
return properties
hachoir
supports other properties as well but I am looking for just those three. For the mov
files I tested it also appears to print out the creation date and modification dates. I am using a priority level of 3 so you can try playing with that to see more stuff.