Search code examples
pythondatepropertiesmetadataexif

Unable to get correct Exif data from video file using Python


I cannot find a way to get the Exif property "DateEncoded" from a video file using Python.

I have tried several exif tools, but this specific mp4 file (recorded using the Android app FilmicPro) seems to be a special case.

Windows Explorer displays the correct date under the "Create Date" property (see image), which is actually the QuickTime exif key "Media_DateEncoded".

Correct Date on Windows Explorer

Adobe Media Encoder also seems to retrieve the correct date:

MediaEncoder Metadata panel

However, none of the five methods I tried can programmatically retrieve the correct date: January 6, 2019. It always returns January 7!

Is there anyone out there capable of explaining why/how?

Here is the code:

import os
import re
import struct
import subprocess as sub
from datetime import datetime
from hachoir.parser import createParser
from hachoir.metadata import extractMetadata
from win32com.propsys import propsys, pscon

# Download ExifTool from https://exiftool.org/
EXIF_TOOL = "exiftool.exe"

def method_1(fpath):
    # On Windows ctime means "Creation time", on Linux "Changed time"
    ctime = os.path.getctime(fpath)
    dtime = datetime.fromtimestamp(ctime)
    return dtime.strftime("%Y:%m:%d %H:%M:%S")

def method_2(fpath):
    '''
    This requires Hachoir installed. For Python3 run:
        >> pip3 install hachoir==3.1.1
        >> pip3 install hachoir-parser
    The property key 'Creation date' is a filesystem metadata, but
    it was the only one returned by Hachoir. What I really need is
    the Quicktime key 'Media_DateEncoded', which Windows Explorer
    calls "Media Created"
    '''
    parser = createParser(fpath)
    with parser:
        metadata = extractMetadata(parser)
    exif_dict = metadata.exportDictionary()['Metadata']
    return exif_dict['Creation date']

def method_3(fpath):
    '''
    This executes this shell comand:
        >> exiftool.exe -h VID_20190106_162804.mp4
    ...which returns an HTML like string from stdout.
    So using regular expression, we look for this section:
        "Media Create Date</td><td>2019:01:07 00:28:08</td></tr>"
    '''
    p = sub.Popen(
        [EXIF_TOOL, '-h',fpath],
        stdout=sub.PIPE,
        encoding='utf8')
    res, err = p.communicate()
    pattern = re.compile(
        r'Media Create Date\</td\>\<td\>(\d{4}:\d{2}:\d{2}\s\d{2}:\d{2}:\d{2})'
    )
    match = re.findall(pattern, res)
    if match:
        return match[0]

def method_4(fpath):
    '''
    Here we look for the Quicktime property key: Media_DateEncoded, which
    Windows Explorer calls "Media Created"
    '''
    fpath = fpath.replace('/', '\\') # Windows api does not work with posix paths
    properties = propsys.SHGetPropertyStoreFromParsingName(fpath)
    dtime = properties.GetValue(pscon.PKEY_Media_DateEncoded).GetValue()
    return dtime.strftime("%Y:%m:%d %H:%M:%S")

def method_5(fpath):
    ATOM_HEADER_SIZE = 8

    # Difference between Unix epoch and QuickTime epoch, in seconds
    EPOCH_ADJUSTER = 2082844800

    # open file and search for moov item
    f = open(fpath, "rb")
    while 1:
        atom_header = f.read(ATOM_HEADER_SIZE)
        if atom_header[4:8] == b'moov':
            break
        atom_size = struct.unpack(">I", atom_header[0:4])[0]
        f.seek(atom_size - 8, 1)

    # found 'moov', look for 'mvhd' and timestamps
    atom_header = f.read(ATOM_HEADER_SIZE)
    if atom_header[4:8] == b'cmov':
        raise Exception("moov atom is compressed")
    elif atom_header[4:8] != b'mvhd':
        raise Exception("expected to find 'mvhd' header")
    else:
        f.seek(4, 1)
        creation_date = struct.unpack(">I", f.read(4))[0]
        dtime = datetime.utcfromtimestamp(creation_date - EPOCH_ADJUSTER)
        return dtime.strftime("%Y:%m:%d %H:%M:%S")

Python 3.7.3 (v3.7.3:ef4ec6ed12) [MSC v.1916 64 bit (AMD64)] on win32

OS: Windows 10 Version 10.0.18362 Build 18362


Solution

  • The thing you aren't taking into account is the fact that Quicktime metadata time stamps are saved as UTC, not as local time. You need to adjust the stored time by your time zone. Windows understands this and displays the corrected time accordingly, though not all programs do.

    When extracting the timestamp with exiftool, you can add the -api QuickTimeUTC option and exiftool will make the adjustment to the current time zone of the computer automatically.