I am looking for a simple library, compatible with Python 3.7, which can extract metadata of video files, specifically the capture/recording datetime; the date and time when the video was shot. I am mainly looking to do this on .mov files. hachoir-metadata
has no Python library as far as I'm aware; only a command-line interface, and enzyme
works only on .mkv files, though this isn't clearly stated in the description. The reason I want to retrieve the recording/capture datatime as a string is that I want to put this in the filename.
Before this question is marked as duplicate: similar questions are either unanswered or outdated. I am honestly puzzled as to why there isn't a proper way to retrieve video metadata in a Python script yet.
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.
来源:https://stackoverflow.com/questions/51342429/how-to-extract-metadata-of-video-files-using-python-3-7