如何使用Python 3.7提取视频文件的元数据? [英] How to extract metadata of video files using Python 3.7?

查看:714
本文介绍了如何使用Python 3.7提取视频文件的元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个与Python 3.7兼容的简单库,该库可以提取视频文件的元数据,尤其是捕获/记录日期时间;拍摄视频的日期和时间.我主要是想在.mov文件上执行此操作.据我所知, hachoir-metadata 没有Python库;仅命令行界面,并且 enzyme 仅适用于.mkv文件在说明中没有明确说明.我想以字符串形式检索记录/捕获数据时间的原因是我想将其放入文件名中.

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.

在此问题被标记为重复之前:相似的问题未回答或已过时.坦白地说,我为什么还没有一种合适的方法来检索Python脚本中的视频元数据,对此感到困惑.

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.

推荐答案

我没有找到用于Python的不错的库,但是将hachoirsubprocess结合使用是一种肮脏的解决方法.您可以从pip中获取库本身,有关Python 3的说明在这里: https://hachoir.readthedocs.io/en/latest/install.html

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也支持其他属性,但我只在寻找这三个属性.对于我测试过的mov文件,它似乎还会打印出创建日期和修改日期.我使用的优先级为3,因此您可以尝试使用该优先级来查看更多内容.

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.

这篇关于如何使用Python 3.7提取视频文件的元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆