corrected GoPro mp4 with exiftool

This commit is contained in:
Andreas Balogh
2023-08-28 21:19:13 +02:00
parent 0e4999b6f3
commit 5df836529b
4 changed files with 98 additions and 4 deletions

73
correct_date_gopro.py Normal file
View File

@@ -0,0 +1,73 @@
#!python
# https://github.com/hMatoba/Piexif
# http://ce3wiki.theturninggate.net/doku.php?id=file_name_convention
# destination: <BASE>/<year>/<YYYYMMDD> <Event>/ab-<YYYYMMDD>-<Event>-<nnnn>.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))