#!python3
# set mtime for handbrake mp4 files from filename
# https://github.com/hMatoba/Piexif
# http://ce3wiki.theturninggate.net/doku.php?id=file_name_convention
# destination: // /ab---.jpg
from pathlib import Path
import datetime as dt
import piexif
from collections import defaultdict
import re
import os
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')
tim = Path(r'E:\MediaArchive\Andreas\RawByCamera\Panasonic HDC-SD99\20150412-20161209 Card\PRIVATE\AVCHD\BDMV\STREAM')
for raw in src.glob("*.m4v"):
mo = re.match("(?P\d+)-\d+.m4v", raw.name)
if mo is None:
print(f"skipped {raw.name}, no match")
continue
md = mo.groupdict()
mtsn = f"{md['seq']}.MTS"
mts_ = list(tim.glob(mtsn))
if len(mts_) != 1:
print(f"skipped {mtsn}, no match")
continue
mts=mts_[0]
stat = mts.stat()
mtime = stat.st_mtime
atime = stat.st_atime
rawdt = dt.datetime.fromtimestamp(mtime)
print(f"{raw.name} -> {rawdt}")
if not DRY_RUN:
os.utime(raw, (atime, mtime))
fn = f"ab-{rawdt:%Y%m%d-%H%M%S}-Panasonic HDC_SD99{raw.suffix}"
rename(raw, raw.parent / fn)