How to read exif data of movies in Python?

Viewed 3790

I use exifread (installed with python3 -m pip install exifread) to read EXIF tags from photos. The same camera takes videos with extension .MOV and a Create Date EXIF field that I am able to view with exiftool (installed with brew install exiftool):

$ exiftool DSC_0002.MOV | grep Date
File Modification Date/Time     : 2020:02:20 18:13:14+00:00
File Access Date/Time           : 2020:03:07 08:11:57+00:00
File Inode Change Date/Time     : 2020:03:04 11:24:51+00:00
Modify Date                     : 2020:02:20 18:13:21
Track Create Date               : 2020:02:20 18:13:21
Track Modify Date               : 2020:02:20 18:13:21
Media Create Date               : 2020:02:20 18:13:21
Media Modify Date               : 2020:02:20 18:13:21
Create Date                     : 2020:02:20 18:13:15
Date/Time Original              : 2020:02:20 18:13:15
Date Display Format             : Y/M/D

I suppose exifread was built for photos, as the code below shows an empty list of tags for this video:

import exifread

f = open("/path/to/file", "rb")
tags = exifread.process_file(f)
print(tags)

One solution is to make a subprocess call to exiftool and parse the result:

EXIFTOOL_DATE_TAG_VIDEOS = "Create Date"
EXIF_DATE_FORMAT = "%Y:%m:%d %H:%M:%S"

cmd = "exiftool '%s'" % filepath
output = subprocess.check_output(cmd, shell=True)
lines = output.decode("ascii").split("\n")
for l in lines:
    if EXIFTOOL_DATE_TAG_VIDEOS in l:
            datetime_str = l.split(" : ")[1]
            print(datetime.datetime.strptime(datetime_str,
                                             EXIF_DATE_FORMAT))

How can I access the list of EXIF tags without a subprocess call?

1 Answers

I edited your own answer a little bit.

def get_exif_creation_dates_video(path):
    EXIFTOOL_DATE_TAG_VIDEOS = "Create Date"
    EXIF_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
    absolute_path = os.path.join( os.getcwd(), path )

    process = subprocess.Popen(["exiftool", absolute_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = process.communicate()
    lines = out.decode("utf-8").split("\n")
    for l in lines:
        if EXIFTOOL_DATE_TAG_VIDEOS in str(l):
                datetime_str = str(l.split(" : ")[1].strip())
                dt = datetime.strptime(datetime_str, EXIF_DATE_FORMAT)
                print(dt) #you will get 3 dates: Create Date, Track Create Date and Media Create Date 

This works on Python 3.9.7

Related