gpt4 book ai didi

Python - 如何读取 Windows "Media Created"日期(不是文件创建日期)

转载 作者:可可西里 更新时间:2023-11-01 10:19:56 35 4
gpt4 key购买 nike

我正在转换几个旧视频文件以节省空间。由于这些文件是个人视频,我希望新文件具有旧文件的创建时间。

Windows 有一个名为“媒体创建”的属性,其中包含相机记录的实际时间。文件的修改时间通常不正确,因此有数百个文件不起作用。

如何在 Python 中访问这个“媒体创建”日期?我一直在谷歌搜索像疯了一样,找不到它。以下是创建日期和修改日期匹配时有效的代码示例:

files = []
for file in glob.glob("*.AVI"):
files.append(file)

for orig in files:
origmtime = os.path.getmtime(orig)
origatime = os.path.getatime(orig)
mark = (origatime, origmtime)
for target in glob.glob("*.mp4"):
firstroot = target.split(".mp4")[0]
if firstroot in orig:
os.utime(target, mark)

最佳答案

正如 Borealid 指出的那样,“媒体创建”值不是文件系统元数据。 Windows shell 从文件本身中获取此值作为元数据。它可以在 API 中作为 Windows Property 访问.如果您使用的是 Windows Vista 或更高版本并且拥有 Python extensions for Windows,则可以轻松访问 Windows shell 属性安装。只需调用 SHGetPropertyStoreFromParsingName ,您可以在 propsys 中找到模块。它返回 PyIPropertyStore实例。标记为“已创建媒体”的属性是 System.Media.DateEncoded .您可以使用属性键 PKEY_Media_DateEncoded 访问此属性,您可以在 propsys.pscon 中找到它。在 Python 3 中,返回值为 datetime.datetime。子类,时间为 UTC。在 Python 2 中,该值是一个自定义时间类型,它具有一个提供 strftime 样式格式化的 Format 方法。如果需要将值转换为本地时间,则 pytz模块具有 IANA 时区数据库。

例如:

import pytz
import datetime
from win32com.propsys import propsys, pscon

properties = propsys.SHGetPropertyStoreFromParsingName(filepath)
dt = properties.GetValue(pscon.PKEY_Media_DateEncoded).GetValue()

if not isinstance(dt, datetime.datetime):
# In Python 2, PyWin32 returns a custom time type instead of
# using a datetime subclass. It has a Format method for strftime
# style formatting, but let's just convert it to datetime:
dt = datetime.datetime.fromtimestamp(int(dt))
dt = dt.replace(tzinfo=pytz.timezone('UTC'))

dt_tokyo = dt.astimezone(pytz.timezone('Asia/Tokyo'))

关于Python - 如何读取 Windows "Media Created"日期(不是文件创建日期),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31507038/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com