我正在尝试根据视频文件的创建数据(例如:视频中孩子的年龄)添加评论,如果他们还没有评论的话。我想要读取每个文件的文件描述中的注释部分,以确保它是空的,然后根据文件的创建时间添加注释。足够简单,可以在windows资源管理器(right-click->properties->Details->Description部分->注释中手动完成)。
我知道如何使用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发布于 2020-05-04 02:02:21
感谢@StarGeek为我指明了正确的方向,因为他建议的Exiftool有一个名为PyExifTool的python包装器,允许使用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需要存在并导入。需要注意的是:第一次重命名注释时,它不会显示在Windows资源管理器中(不了解Mac/Linux),即使它存在于元数据中。我不知道为什么会这样。但是,在手动将注释设置为任何内容之后,它可以通过exiftool进行更改,并在Windows资源管理器中可见。目前对我来说已经足够好了,我可以通过一个手动操作选择并更改文件夹中所有文件的注释,然后让python将注释更改为有用的内容。
https://stackoverflow.com/questions/61452071
复制相似问题