Python - 如何读取 Windows "Media Created" 日期(不是文件创建日期)
Python - how to read Windows "Media Created" date (not file creation date)
我有几个旧视频文件正在转换以保存 space。由于这些文件是个人视频,我希望新文件具有旧文件的创建时间。
Windows 有一个名为 "Media created" 的属性,它具有相机记录的实际时间。文件的修改时间通常不正确,因此有数百个文件不起作用。
如何在 Python 中访问这个 "Media created" 日期?我一直在谷歌搜索像疯了一样,找不到它。以下是创建日期和修改日期匹配时有效的代码示例:
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)
如果您所说的属性来自相机,则它不是文件系统权限:它是视频本身内的元数据,Windows 正在读出并呈现给您。
此类元数据的一个示例是 JPEG 图像的 EXIF 数据:拍摄照片的相机类型、使用的设置等等。
您需要打开 .mp4 文件并解析元数据,最好使用一些现有的库来执行此操作。您将无法从文件系统获取信息,因为它不存在。
现在,另一方面,如果您想要的只是文件创建日期(它实际上并不是来自相机,而是在文件首次放入当前计算机时设置的,并且可能已经初始化为以前在相机上的某个值)...可以通过 os.path.getctime(orig)
.
获得
正如 Borealid 指出的那样,"Media created" 值不是文件系统元数据。 Windows shell 从文件本身中获取此值作为元数据。它可以在 API 中访问,因为 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 模块具有 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'))
我有几个旧视频文件正在转换以保存 space。由于这些文件是个人视频,我希望新文件具有旧文件的创建时间。
Windows 有一个名为 "Media created" 的属性,它具有相机记录的实际时间。文件的修改时间通常不正确,因此有数百个文件不起作用。
如何在 Python 中访问这个 "Media created" 日期?我一直在谷歌搜索像疯了一样,找不到它。以下是创建日期和修改日期匹配时有效的代码示例:
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)
如果您所说的属性来自相机,则它不是文件系统权限:它是视频本身内的元数据,Windows 正在读出并呈现给您。
此类元数据的一个示例是 JPEG 图像的 EXIF 数据:拍摄照片的相机类型、使用的设置等等。
您需要打开 .mp4 文件并解析元数据,最好使用一些现有的库来执行此操作。您将无法从文件系统获取信息,因为它不存在。
现在,另一方面,如果您想要的只是文件创建日期(它实际上并不是来自相机,而是在文件首次放入当前计算机时设置的,并且可能已经初始化为以前在相机上的某个值)...可以通过 os.path.getctime(orig)
.
正如 Borealid 指出的那样,"Media created" 值不是文件系统元数据。 Windows shell 从文件本身中获取此值作为元数据。它可以在 API 中访问,因为 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 模块具有 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'))