initial commit
This commit is contained in:
81
ren_title.py
Normal file
81
ren_title.py
Normal file
@@ -0,0 +1,81 @@
|
||||
#!python3
|
||||
|
||||
# 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 piexif
|
||||
from collections import defaultdict
|
||||
import re
|
||||
|
||||
SRC = r'E:\MediaArchive\Andreas\RawByDate\2004'
|
||||
# camera = "Minolta"
|
||||
author = 'ab'
|
||||
dry_run = False
|
||||
|
||||
# def constant_factory(value):
|
||||
# return lambda: value
|
||||
# fileidx = defaultdict(constant_factory(0))
|
||||
|
||||
fileidx = defaultdict(int)
|
||||
|
||||
for daydir in Path(SRC).iterdir():
|
||||
mo = re.match("(?P<dt>\S*)\s(?P<title>.*)", daydir.name)
|
||||
md = mo.groupdict()
|
||||
title = md['title']
|
||||
for raw in daydir.rglob(f"*.jpg"):
|
||||
# check whether exif works
|
||||
try:
|
||||
exif_dict = piexif.load(str(raw))
|
||||
model = exif_dict["0th"][piexif.ImageIFD.Model].decode("ascii")
|
||||
idto = exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal].decode("ascii")
|
||||
rawdt = dt.datetime.strptime(idto,"%Y:%m:%d %H:%M:%S")
|
||||
except:
|
||||
model = None
|
||||
print(f'exif failed {raw.name}, using file date')
|
||||
mtime = raw.stat().st_mtime
|
||||
rawdt = dt.datetime.fromtimestamp(mtime)
|
||||
# digitized negative photo?
|
||||
if model == 'Canon EOS 5D':
|
||||
rawdt = dt.datetime.strptime(md['dt'],"%Y%m%d")
|
||||
use_ts = False
|
||||
else:
|
||||
use_ts = True
|
||||
if model == 'FinePix S602 ZOOM':
|
||||
author = "nn"
|
||||
else:
|
||||
author = "ab"
|
||||
year = rawdt.year
|
||||
day = f"{rawdt:%Y%m%d}"
|
||||
ts = f"{rawdt:%H%M%S}"
|
||||
if use_ts:
|
||||
destfn = raw.parent / f"{author}-{day}-{title}-{ts}{raw.suffix}"
|
||||
else:
|
||||
index = fileidx[day] + 1
|
||||
fileidx[day] = index
|
||||
destfn = raw.parent / f"{author}-{day}-{title}-{index:04}{raw.suffix}"
|
||||
# destfn = raw.parent / f"{author}-{day}-{title}-{index:04}-{camera}{raw.suffix}"
|
||||
print(raw.name, "->", destfn.name)
|
||||
if not dry_run:
|
||||
if not destfn.exists():
|
||||
raw.rename(destfn)
|
||||
else:
|
||||
print(f"skipped {raw.name}, {destfn.name}, already exists")
|
||||
camera = "Sony TRV_25E"
|
||||
for raw in daydir.rglob(f"*.avi"):
|
||||
mtime = raw.stat().st_mtime
|
||||
rawdt = dt.datetime.fromtimestamp(mtime)
|
||||
year = rawdt.year
|
||||
day = f"{rawdt:%Y%m%d}"
|
||||
ts = f"{rawdt:%H%M%S}"
|
||||
index = fileidx[day] + 1
|
||||
fileidx[day] = index
|
||||
destfn = raw.parent / f"{author}-{day}-{title}-{ts}-{camera}{raw.suffix}"
|
||||
print(raw.name, "->", destfn.name)
|
||||
if not dry_run:
|
||||
if not destfn.exists():
|
||||
raw.rename(destfn)
|
||||
else:
|
||||
print(f"skipped {raw.name}, {destfn.name}, already exists")
|
||||
Reference in New Issue
Block a user