python - 无法使用Python从视频文件中获取正确的Exif数据

标签 python date properties metadata exif

我找不到使用 Python 从视频文件中获取 Exif 属性“DateEncoded”的方法。

我尝试过几种 exif 工具,但是 this specific mp4 file (使用 Android 应用程序 FilmicPro 录制)似乎是一个特例。

Windows 资源管理器在“创建日期”属性下显示正确的日期(参见图片),这实际上是 QuickTime exif 键“Media_DateEncoded”。

Correct Date on Windows Explorer

Adobe Media Encoder 似乎也检索到了正确的日期:

MediaEncoder Metadata panel

但是,我尝试的五种方法都无法以编程方式检索正确的日期:2019 年 1 月 6 日。它总是返回 1 月 7 日!

有没有人能够解释为什么/如何?

这是代码:

import os
import re
import struct
import subprocess as sub
from datetime import datetime
from hachoir.parser import createParser
from hachoir.metadata import extractMetadata
from win32com.propsys import propsys, pscon

# Download ExifTool from https://exiftool.org/
EXIF_TOOL = "exiftool.exe"

def method_1(fpath):
    # On Windows ctime means "Creation time", on Linux "Changed time"
    ctime = os.path.getctime(fpath)
    dtime = datetime.fromtimestamp(ctime)
    return dtime.strftime("%Y:%m:%d %H:%M:%S")

def method_2(fpath):
    '''
    This requires Hachoir installed. For Python3 run:
        >> pip3 install hachoir==3.1.1
        >> pip3 install hachoir-parser
    The property key 'Creation date' is a filesystem metadata, but
    it was the only one returned by Hachoir. What I really need is
    the Quicktime key 'Media_DateEncoded', which Windows Explorer
    calls "Media Created"
    '''
    parser = createParser(fpath)
    with parser:
        metadata = extractMetadata(parser)
    exif_dict = metadata.exportDictionary()['Metadata']
    return exif_dict['Creation date']

def method_3(fpath):
    '''
    This executes this shell comand:
        >> exiftool.exe -h VID_20190106_162804.mp4
    ...which returns an HTML like string from stdout.
    So using regular expression, we look for this section:
        "Media Create Date</td><td>2019:01:07 00:28:08</td></tr>"
    '''
    p = sub.Popen(
        [EXIF_TOOL, '-h',fpath],
        stdout=sub.PIPE,
        encoding='utf8')
    res, err = p.communicate()
    pattern = re.compile(
        r'Media Create Date\</td\>\<td\>(\d{4}:\d{2}:\d{2}\s\d{2}:\d{2}:\d{2})'
    )
    match = re.findall(pattern, res)
    if match:
        return match[0]

def method_4(fpath):
    '''
    Here we look for the Quicktime property key: Media_DateEncoded, which
    Windows Explorer calls "Media Created"
    '''
    fpath = fpath.replace('/', '\\') # Windows api does not work with posix paths
    properties = propsys.SHGetPropertyStoreFromParsingName(fpath)
    dtime = properties.GetValue(pscon.PKEY_Media_DateEncoded).GetValue()
    return dtime.strftime("%Y:%m:%d %H:%M:%S")

def method_5(fpath):
    ATOM_HEADER_SIZE = 8

    # Difference between Unix epoch and QuickTime epoch, in seconds
    EPOCH_ADJUSTER = 2082844800

    # open file and search for moov item
    f = open(fpath, "rb")
    while 1:
        atom_header = f.read(ATOM_HEADER_SIZE)
        if atom_header[4:8] == b'moov':
            break
        atom_size = struct.unpack(">I", atom_header[0:4])[0]
        f.seek(atom_size - 8, 1)

    # found 'moov', look for 'mvhd' and timestamps
    atom_header = f.read(ATOM_HEADER_SIZE)
    if atom_header[4:8] == b'cmov':
        raise Exception("moov atom is compressed")
    elif atom_header[4:8] != b'mvhd':
        raise Exception("expected to find 'mvhd' header")
    else:
        f.seek(4, 1)
        creation_date = struct.unpack(">I", f.read(4))[0]
        dtime = datetime.utcfromtimestamp(creation_date - EPOCH_ADJUSTER)
        return dtime.strftime("%Y:%m:%d %H:%M:%S")

Win32 上的 Python 3.7.3 (v3.7.3:ef4ec6ed12) [MSC v.1916 64 位 (AMD64)]

操作系统:Windows 10 版本 10.0.18362 内部版本 18362

最佳答案

您没有考虑到 Quicktime 元数据时间戳保存为 UTC,而不是本地时间。您需要根据您的时区调整存储的时间。 Windows 理解这一点并相应地显示正确的时间,但并非所有程序都这样做。

使用exiftool提取时间戳时,可以添加-api QuickTimeUTC option exiftool会自动调整计算机当前时区。

关于python - 无法使用Python从视频文件中获取正确的Exif数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59701813/

相关文章:

python - Pandas 在数据框中组合稀疏列

python - 如何在微分方程(SciPy)中使用 if 语句?

python - 递归搜索字典中包含子字符串的路径

swift - 将 nil 日期排序到数组的末尾

java System.getProperties().put();

swift - 提取 Swift 类常量属性的初始化代码

java - 通过 Spring by Environment 注入(inject)属性值

python - 在 python 中从列表中删除类实例有困难

date - 如何在 React Native 中获取当前日期

java - 如何显示带有自定义时区的日期?