Files
media-scripts/set_mtime_from_filename.py
2023-08-28 21:19:13 +02:00

47 lines
1.5 KiB
Python

#!python
# set mtime for handbrake mp4 files from filename
# 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 pathlib import Path
import datetime as dt
import re
import os
import subprocess
DRY_RUN = False
def rename(old, new):
print(old.name, "->", new.name)
if not DRY_RUN:
if not new.exists():
old.rename(new)
else:
print(f"skipped {old.name}, {new.name}, already exists")
# src = Path(r'D:\Videos')
src = Path(r"D:\MediaLibrary\Pictures\2023\20230819 Urlaub Nordkapp")
for raw in src.glob("*.mp4"):
mo = re.match("(?P<author>[^-]*)-(?P<dt>\S{8}-\S{6})-(?P<model>[^-]*)(-\S+)?.mp4", raw.name)
if mo is None:
print(f"skipped {raw.name}, no match")
continue
md = mo.groupdict()
rawdt = dt.datetime.strptime(md['dt'],"%Y%m%d-%H%M%S")
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"