4

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 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.

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)
Tensigh
  • 1,030
  • 5
  • 22
  • 44
  • This is a good first step but it's giving me the wrong date. The date and time are close but they're off. – Tensigh Jul 20 '15 at 10:03
  • Ugh, I should have seen that. Tokyo is 8 or 9 hours ahead of UTC and I was expecting PST so it threw me off. Now I get it. Can you submit this as an answer so I can vote it as the right one? Thanks! – Tensigh Jul 21 '15 at 02:28
  • For `.mov` files, you can try [*Getting metadata for MOV video*](https://stackoverflow.com/a/54683292/3357935). – Stevoisiak Nov 13 '20 at 15:05

2 Answers2

5

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.

For example:

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'))
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • Thank you to both of you for helping. As a note - the reason why I thought it was a file attribute is because Windows lists it as the "creation date" when you view the files in Detailed view. – Tensigh Jul 21 '15 at 04:29
  • eryksun, there's a problem with the last line in your code. It gives me an attribute error on "astimezone" – Tensigh Jul 21 '15 at 13:14
  • 1
    @Tensigh, I had only tested in Python 3. Apparently in Python 2 it's returning a custom type instead of a `datetime.datetime` subclass. Try the workaround. – Eryk Sun Jul 21 '15 at 18:39
  • 1
    @erkysun, if I could, I would give you another upvote. That worked PERFECTLY! Thank you, you've restored my hope in asking for help on SO. – Tensigh Jul 22 '15 at 12:41
  • What did you enter for filepath? I use win10, but I am unable to get the code to run. I passed filepath = '' but it errors out at this line: "dt = datetime.datetime.fromtimestamp(int(dt))" – programmer Dec 06 '20 at 05:29
  • for someone who doing this in a loop. you should call `properties = None` after `GetValue()`, if not the loop will not continue. it takes me hours to figure out the problem. – ryanhex53 Sep 16 '21 at 13:05
1

If the attribute you're talking about came from the camera, it's not a filesystem permission: it's metadata inside the videos themselves which Windows is reading out and presenting to you.

An example of this type of metadata would be a JPEG image's EXIF data: what type of camera took the photo, what settings were used, and so forth.

You would need to open up the .mp4 files and parse the metadata, preferably using some existing library for doing that. You wouldn't be able to get the information from the filesystem because it's not there.

Now if, on the other hand, all you want is the file creation date (which didn't actually come from the camera, but was set when the file was first put onto the current computer, and might have been initialized to some value that was previously on the camera)... That can be gotten with os.path.getctime(orig).

Borealid
  • 95,191
  • 9
  • 106
  • 122