Skip to content

Commit

Permalink
get_exif_date_time_offset: Prioritize QuickTime:ContentCreateDate ove…
Browse files Browse the repository at this point in the history
…r EXIF:DateTimeOriginal for videos.
  • Loading branch information
oPromessa committed Sep 22, 2024
1 parent a6e4d5e commit eae7427
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion osxphotos/exifutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,28 @@ def get_exif_date_time_offset(
# set to True if no date/time in EXIF and the FileModifyDate is used
used_file_modify_date = False

# determine file type
mime_type = exif.get('File:MIMEType', 'image')
isphoto = mime_type.startswith("image")
ismovie = mime_type.startswith("video")

# search these fields in this order for date/time/timezone
time_fields = [
# Prioritize QuickTime:ContentCreateDate over EXIF:DateTimeOriginal for videos
start_time_fields = [
"Composite:DateTimeCreated",
"Composite:SubSecDateTimeOriginal",
"Composite:SubSecCreateDate",
]
photo_time_fields = [
"EXIF:DateTimeOriginal",
"EXIF:CreateDate",
]
movie_time_fields = [
"QuickTime:ContentCreateDate",
"QuickTime:CreationDate",
"QuickTime:CreateDate",
]
end_time_fields = [
"IPTC:DateCreated",
"XMP-exif:DateTimeOriginal",
"XMP-xmp:DateCreated",
Expand All @@ -75,11 +87,21 @@ def get_exif_date_time_offset(
"ContentCreateDate",
"CreationDate",
]
if isphoto:
time_fields = start_time_fields + photo_time_fields + movie_time_fields + end_time_fields
elif ismovie:
time_fields = start_time_fields + movie_time_fields + photo_time_fields + end_time_fields
else:
time_fields = start_time_fields + photo_time_fields + movie_time_fields + end_time_fields

if use_file_modify_date:
time_fields.extend(["File:FileModifyDate", "FileModifyDate"])

for dt_str in time_fields:
dt = exif.get(dt_str)
# Some old mp4 may return ContentCreationDate as YYYY (eg. 2014) which
# is converted to int causing re.match(pattern, dt) to fail.
dt = str(dt) if isinstance(dt, int) else dt
if dt and dt_str in {"IPTC:DateCreated", "DateCreated"}:
# also need time
time_ = exif.get("IPTC:TimeCreated") or exif.get("TimeCreated")
Expand Down

0 comments on commit eae7427

Please sign in to comment.