#!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') for raw in src.glob("*.m4v"): mo = re.match("(?P[^-]*)-(?P
\S*)-(?P[^-]*)(-\S+)?.m4v", 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}") 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)