Python-如何读取Windows“媒体创建"日期(不是文件创建日期) [英] Python - how to read Windows "Media Created" date (not file creation date)

查看:770
本文介绍了Python-如何读取Windows“媒体创建"日期(不是文件创建日期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have several old video files that I'm converting to save space. Since these files are personal videos, I want the new files to have the old files' creation time.

Windows具有一个称为媒体创建"的属性,该属性具有照相机记录的实际时间.文件的修改时间通常不正确,因此有数百个文件无法使用.

Windows has an attribute called "Media created" which has the actual time recorded by the camera. The files' modification times are often incorrect so there are hundreds of files where this won't work.

如何在Python中访问创建媒体"日期?我一直在疯狂地搜寻,找不到它.如果创建日期和修改日期匹配,下面是可以使用的代码示例:

How can I access this "Media created" date in Python? I've been googling like crazy and can't find it. Here's a sample of the code that works if the creation date and modify date match:

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属性对其进行访问.如果您使用的是Windows Vista或更高版本,并且具有 Windows的Python扩展,则可以轻松访问Windows shell属性. 已安装.只需致电 SHGetPropertyStoreFromParsingName ,您将在 propsys 模块.它返回一个 PyIPropertyStore 实例.标记为已创建媒体"的属性为 System.Media.DateEncoded .您可以使用属性键PKEY_Media_DateEncoded(在propsys.pscon中找到)访问此属性.在Python 3中,返回值是 datetime.datetime 子类,与在UTC的时间.在Python 2中,该值是具有Format方法的自定义时间类型,该方法提供了strftime样式格式.如果您需要将值转换为本地时间,则 pytz 模块具有时区的IANA数据库

As Borealid noted, the "Media created" value is not filesystem metadata. The Windows shell gets this value as metadata from within the file itself. It's accessible in the API as a Windows Property. You can easily access Windows shell properties if you're using Windows Vista or later and have the Python extensions for Windows installed. Just call SHGetPropertyStoreFromParsingName, which you'll find in the propsys module. It returns a PyIPropertyStore instance. The property that's labelled "Media created" is System.Media.DateEncoded. You can access this property using the property key PKEY_Media_DateEncoded, which you'll find in propsys.pscon. In Python 3 the returned value is a datetime.datetime subclass, with the time in UTC. In Python 2 the value is a custom time type that has a Format method that provides strftime style formatting. If you need to convert the value to local time, the pytz module has the IANA database of time zones.

例如:

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“媒体创建"日期(不是文件创建日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆