From 5df836529b30abee9d0d1ebf9c0d973a3618e27c Mon Sep 17 00:00:00 2001 From: Andreas Balogh Date: Mon, 28 Aug 2023 21:19:13 +0200 Subject: [PATCH] corrected GoPro mp4 with exiftool --- correct_date_gopro.py | 73 ++++++++++++++++++++++++++++++++++++++ exiftool_scratchpad.cmd | 15 ++++++++ ren_triage.py | 2 +- set_mtime_from_filename.py | 12 +++++-- 4 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 correct_date_gopro.py create mode 100644 exiftool_scratchpad.cmd diff --git a/correct_date_gopro.py b/correct_date_gopro.py new file mode 100644 index 0000000..ed38a0d --- /dev/null +++ b/correct_date_gopro.py @@ -0,0 +1,73 @@ +#!python + +# https://github.com/hMatoba/Piexif +# http://ce3wiki.theturninggate.net/doku.php?id=file_name_convention +# destination: // /ab---.jpg + +from PIL import Image +from pathlib import Path +import datetime as dt +import piexif +from collections import defaultdict +import re +import os + +SRC = r'E:\RawTriage\GoPro Hero\20230819' +DRY_RUN = True + +# def constant_factory(value): +# return lambda: value +# fileidx = defaultdict(constant_factory(0)) + +fileidx = defaultdict(int) + +real_dt = dt.datetime(2023,7,28,15,0,0) +gopro_dt = dt.datetime(2015,1,3,19,23,25) +dt_diff = real_dt - gopro_dt + +# print(dt_diff) +# print(gopro_dt + dt_diff) + +for raw in Path(SRC).iterdir(): + fn = raw.name + # print(fn) + if not raw.is_file(): + print(f"skipped {fn}") + continue + if raw.suffix == ".MP4": + atime = raw.stat().st_atime + mtime = raw.stat().st_mtime + if dt.datetime.fromtimestamp(mtime) < dt.datetime(2020,1,1,0,0,0): + ctime = mtime + dt_diff.total_seconds() + print(f"{raw.name}: cur {dt.datetime.fromtimestamp(mtime)}, new {dt.datetime.fromtimestamp(ctime)}") + os.utime(raw, (atime, ctime)) + else: + orig = raw.with_suffix(".MP4_original") + atime = orig.stat().st_atime + mtime = orig.stat().st_mtime + os.utime(raw, (atime, mtime)) + # use exiftool for MP4 metadata timestamps 3127 days, 19:36:35 + # exiftool "-AllDates+=3127 19:36:35" "E:\RawTriage\GoPro Hero\20230819" + if raw.suffix == ".JPG": + atime = raw.stat().st_atime + mtime = raw.stat().st_mtime + ctime = mtime + dt_diff.total_seconds() + print(f"{raw.name}: cur {dt.datetime.fromtimestamp(mtime)}, new {dt.datetime.fromtimestamp(ctime)}") + if not DRY_RUN: + os.utime(raw, (atime, ctime)) + # EXIF update + im = Image.open(raw) + exif_dict = piexif.load(im.info["exif"]) + try: + model = exif_dict["0th"][piexif.ImageIFD.Model].decode("ascii") + idto = exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal].decode("ascii") + except: + continue + rawdt = dt.datetime.strptime(idto,"%Y:%m:%d %H:%M:%S") + print(model, rawdt) + crawstr = dt.datetime.fromtimestamp(ctime).strftime("%Y:%m:%d %H:%M:%S") + exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal] = crawstr.encode("ascii") + exif_bytes = piexif.dump(exif_dict) + fn2 = raw.with_name(raw.stem + "_corrected" + raw.suffix) + im.save(fn2, "jpeg", exif=exif_bytes, quality=95) + os.utime(fn2, (atime, ctime)) diff --git a/exiftool_scratchpad.cmd b/exiftool_scratchpad.cmd new file mode 100644 index 0000000..cb27693 --- /dev/null +++ b/exiftool_scratchpad.cmd @@ -0,0 +1,15 @@ +exiftool -G -args "ab-20230726-183944-DJI Mini 2.MP4" | findstr Date + +for %F in ("*DJI Mini 2.mp4") DO exiftool -P -tagsFromFile "E:\RawTriage\DJI Mini 2\20230819 Videos\%F" -DateTimeOriginal^[^-]*)-(?P
\S*)-(?P[^-]*)(-\S+)?.jpg", raw.name) +for raw in src.glob("*.mp4"): + mo = re.match("(?P[^-]*)-(?P
\S{8}-\S{6})-(?P[^-]*)(-\S+)?.mp4", raw.name) if mo is None: print(f"skipped {raw.name}, no match") continue @@ -34,7 +35,12 @@ for raw in src.glob("*pano.jpg"): mtime = rawdt.timestamp() atime = raw.stat().st_atime print(f"setting {raw.name} to {rawdt}") + exifdt = (rawdt+dt.timedelta(hours=2)).strftime('%Y:%m:%d %H:%M:%S') + print(f'exiftool "-AllDates={exifdt}" "{raw}"') + subprocess.run(["exiftool", f"-AllDates={exifdt}", f"{raw}"]) if not DRY_RUN: os.utime(raw, (atime, mtime)) # fn = f"{md['author'].lower()}-{md['dt']}-{md['model']}{raw.suffix}" # rename(raw, raw.parent / fn) + # use exiftool for MP4 metadata timestamps 3127 days, 19:36:35 + # exiftool "-AllDates+=3127 19:36:35" "E:\RawTriage\GoPro Hero\20230819"