8

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?

miguelmorin
  • 5,025
  • 4
  • 29
  • 64
  • 2
    EXIF is a metadata format for **images**. Videos have the metadata stored in the container format. This question might be helpful: https://stackoverflow.com/questions/21355316/getting-metadata-for-mov-video – Klaus D. Mar 07 '20 at 11:12
  • 1
    @KlausD. OK, so I had better stick with the `exiftool` subprocess call, since I deal with different file types (`.MOV`, `.avi`, and `.mp4`). Do you want to write an answer? Or shall I mark this as a duplicate of the other thread and post the code for the subprocess call as an answer there? – miguelmorin Mar 10 '20 at 10:25

1 Answers1

0

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

VdGR
  • 77
  • 7