如何使用 Python 3.7 提取视频文件的元数据?

Emi*_*oss 5 python video metadata python-3.x

我正在寻找一个简单的库,与 Python 3.7 兼容,它可以提取视频文件的元数据,特别是捕获/录制日期时间;拍摄视频的日期和时间。我主要希望在 .mov 文件上执行此操作。hachoir-metadata据我所知,没有 Python 库;只是一个命令行界面,并且enzyme只适用于 .mkv 文件,尽管这在描述中没有明确说明。我想将记录/捕获数据时间作为字符串检索的原因是我想把它放在文件名中。

在此问题被标记为重复之前:类似的问题要么没有答案,要么已经过时。老实说,我对为什么还没有在 Python 脚本中检索视频元数据的正确方法感到困惑。

Ara*_*syh 6

FFMPEG 是一个合适的库。

安装:pip3 install ffmpeg-python

如何使用:

import ffmpeg
vid = ffmpeg.probe(your_video_address)
print(vid['streams'])
Run Code Online (Sandbox Code Playgroud)

  • `ffmpeg-python` 只是 `ffmpeg` 的包装器。您还需要在系统上安装“ffmpeg”。 (5认同)

Bri*_*een 2

我还没有找到一个好的 Python 库,但使用hachoirwithsubprocess是一个肮脏的解决方法。您可以从 pip 获取库本身,Python 3 的说明如下: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
Run Code Online (Sandbox Code Playgroud)

hachoir也支持其他属性,但我正在寻找这三个属性。对于mov我测试的文件,它似乎还打印出创建日期和修改日期。我使用的优先级为 3,因此您可以尝试使用它来查看更多内容。