我如何用python从.mp4文件中读取&写入 "评论 "元数据?

问题描述 投票:0回答:1

如果我的视频文件还没有注释,我想根据它们被创建的数据(例如:视频中孩子的年龄)为它们添加注释。我想读取每个文件描述中的注释部分,以确保它是空的,然后根据文件的创建时间添加注释。很简单,在windows资源管理器中手动完成(右键->属性->细节->描述部分->注释)。

我知道如何用stat()从大多数文件中获取一些元数据,如创建日期,但我没有设法获取.mp4文件的注释部分。

from pathlib import Path

testDir = r"C:\temp\test"
current_dir = Path(testDir)

for current_file in current_dir.iterdir():
    info = current_file.stat()
    print(info.st_mtime)
    print(info.comments) # This just throws an 'os.stat_result' object has no attribute 'comments' error
python comments metadata mp4
1个回答
0
投票

感谢@StarGeek给我指出了正确的方向,因为它原来是 Exiftool 他建议有一个名为 PyExifTool 允许用python控制ExifTool.我在这里分享我的解决方案,如果有人感兴趣。

import exiftool

vidFile = r"C:\temp\test\2019-09-02 19.52.14.mp4"
with exiftool.ExifTool() as et:
    vidComment = et.get_tag("comment", vidFile)
    if vidComment is None or vidComment == "":
        newComment = '-comment="written by Pyexiftool"'
        et.execute(bytes(newComment, 'utf-8'), bytes(vidFile,"utf-8"))

需要下载Exiftool,重命名为exiftool(不含选项),然后在Path中引用.exe文件,并导入PyExiftool。PyExiftool需要存在并导入.有一点需要注意:第一次重命名的注释不会在Windows资源管理器中显示(不知道MacLinux),即使它存在于元数据中。我不知道为什么会这样。不过,手动将注释设置为任何内容后,可以通过exiftool进行修改,并在Windows资源管理器中可见。目前对我来说已经足够好了,我可以通过一次手动操作选择并修改一个文件夹中所有文件的注释,然后让python将注释改为有用的内容。

© www.soinside.com 2019 - 2024. All rights reserved.