-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphoto_rename.py
More file actions
280 lines (239 loc) · 7.9 KB
/
Copy pathphoto_rename.py
File metadata and controls
280 lines (239 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import argparse
import datetime
import os
import re
import sys
from voussoirkit import betterhelp
from voussoirkit import imagetools
from voussoirkit import interactive
from voussoirkit import pathclass
from voussoirkit import pipeable
from voussoirkit import spinal
from voussoirkit import vlogging
log = vlogging.getLogger(__name__)
def makename(file, read_exif=False, read_mtime=False, minus_duration=False):
old = file.replace_extension('').basename
new = old
final_pattern = r'^(\d\d\d\d)-(\d\d)-(\d\d)_(\d\d)-(\d\d)-(\d\d)(?:x\d+)?$'
# Already optimized filenames need not apply
# This is also important when the filename and the exif disagree
if re.match(final_pattern, old) and not read_exif and not read_mtime:
# return file
pass
# Microsoft ICE
new = re.sub(
r'^(\d\d\d\d)-(\d\d)-(\d\d)_(\d\d)-(\d\d)-(\d\d)_stitch$',
r'\1-\2-\3_\4-\5-\6',
new,
)
new = re.sub(
r'^(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)_stitch$',
r'\1-\2-\3_\4-\5-\6',
new,
)
# Android screenshots
new = re.sub(
r'^Screenshot_(\d\d\d\d)(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d)(?:_cropped)?$',
r'\1-\2-\3_\4-\5-\6',
new,
)
new = re.sub(
r'^Recording_(\d\d\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)(?:_cropped)?$',
r'\1-\2-\3_\4-\5-\6',
new,
)
# Terraria screenshots
new = re.sub(
r'^Capture (\d\d\d\d)-(\d\d)-(\d\d) (\d\d)_(\d\d)_(\d\d)$',
r'\1-\2-\3_\4-\5-\6',
new,
)
# OBS screenshots
new = re.sub(
r'^Screenshot (\d\d\d\d)-(\d\d)-(\d\d)_(\d\d)-(\d\d)-(\d\d)$',
r'\1-\2-\3_\4-\5-\6',
new,
)
new = re.sub(
r'^(\d\d\d\d)(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d)$',
r'\1-\2-\3_\4-\5-\6',
new,
)
new = re.sub(
r'^(\d\d\d\d)-(\d\d)-(\d\d)[_-](\d\d)(\d\d)(\d\d)$',
r'\1-\2-\3_\4-\5-\6',
new,
)
new = re.sub(
r'^(\d\d\d\d)(\d\d)(\d\d)-(\d\d)_(\d\d)_(\d\d)$',
r'\1-\2-\3_\4-\5-\6',
new,
)
# LG Android camera
new = re.sub(
r'^(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)(?:_HDR)?$',
r'\1-\2-\3_\4-\5-\6',
new,
)
# Unihertz Jelly 2 camera
new = re.sub(
r'^IMG_(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)_\d+$',
r'\1-\2-\3_\4-\5-\6',
new,
)
# Jelly videos
new = re.sub(
r'^VID_(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)+$',
r'\1-\2-\3_\4-\5-\6',
new,
)
# Jelly screen recordings
new = re.sub(
r'^screen-(\d\d\d\d)(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d)$',
r'\1-\2-\3_\4-\5-\6',
new,
)
# Sony videos
new = re.sub(
r'^VideoPro_(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)+$',
r'\1-\2-\3_\4-\5-\6',
new,
)
new = re.sub(
r'^(\d\d\d\d)-(\d\d)-(\d\d)_(\d\d)-(\d\d)-(\d\d)[_-](\d+)$',
r'\1-\2-\3_\4-\5-\6x\7',
new,
)
new = re.sub(
r'^(\d\d\d\d)(\d\d)(\d\d)[_-](\d+)$',
r'\1-\2-\3x\4',
new,
)
new = re.sub(
r'^(\d\d\d\d)(\d\d)(\d\d) (\w+.*)$',
r'\1-\2-\3 \4',
new,
)
new = re.sub(
r'^(\d\d\d\d)(\d\d)(\d\d)$',
r'\1-\2-\3',
new,
)
# Kakaotalk downloaded photos
# Unix timestamps, followed by an index if downloaded as bundle
unix = re.match(r'^(?:kakaotalk_)?(\d{13})(?:[_-](\d))?', new)
if unix:
date = datetime.datetime.fromtimestamp(int(unix.group(1)) / 1000)
f = date.strftime('%Y-%m-%d_%H-%M-%S')
if unix.group(2):
f += 'x' + unix.group(2)
new = re.sub(unix.group(0), f, new)
# exif comes last because I feel the filename is most important.
# Especially in cases where the user has edited a photo with software that
# reset the exif but the filename refers to the original date.
# I'm sure cases could be made either way but I'm starting here.
if new == old and read_exif and file.extension in {'jpg', 'jpeg', 'dng'}:
new = makename_exif(file, old)
if new == old and read_mtime:
mtime = file.stat.st_mtime
if minus_duration:
mtime -= get_file_duration(file)
date = datetime.datetime.fromtimestamp(mtime)
new = date.strftime('%Y-%m-%d_%H-%M-%S')
if new == old:
return file
new = file.parent.with_child(new).add_extension(file.extension)
return new
def makename_exif(file, fallback):
dt = imagetools.get_exif_datetime(file)
if not dt:
return fallback
return dt.strftime('%Y-%m-%d_%H-%M-%S')
def get_file_duration(file):
import kkroening_ffmpeg
probe = kkroening_ffmpeg.probe(file.absolute_path)
duration = float(probe['format']['duration'])
return duration
def makename_ffmpeg(file, fallback):
import kkroening_ffmpeg
probe = kkroening_ffmpeg.probe(file.absolute_path)
duration = float(probe['format']['duration'])
zulu = probe['streams'][0]['tags']['creation_time']
def makenames(files, read_exif=False, read_mtime=False, minus_duration=False):
pairs = {}
new_duplicates = {}
for file in files:
newname = makename(file, read_exif=read_exif, read_mtime=read_mtime, minus_duration=minus_duration)
new_duplicates.setdefault(newname, []).append(file)
if file.basename == newname.basename:
continue
pairs[file] = newname
if not pairs:
return pairs
for (new, olds) in list(new_duplicates.items()):
count = len(olds)
if count > 1:
olds.sort()
new_duplicates.pop(new)
wanted = new
zeroes = len(str(len(olds)))
for (index, old) in enumerate(olds):
new_base = wanted.replace_extension('').basename + f'x{index+1:0{zeroes}d}'
new = wanted.parent.with_child(new_base).add_extension(wanted.extension)
if old.basename == new.basename:
del pairs[old]
continue
pairs[old] = new
return pairs
def photo_rename_argparse(args):
patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True)
if args.recurse:
files = spinal.walk('.', glob_filenames=patterns, yield_directories=False)
else:
files = pathclass.glob_many_files(patterns)
pairs = makenames(files, read_exif=args.read_exif, read_mtime=args.read_mtime, minus_duration=args.minus_duration)
if not pairs:
return 0
for (old, new) in pairs.items():
print(f'{old.relative_path} -> {new.relative_path}')
if not (args.autoyes or interactive.getpermission('Okay?', must_pick=True)):
return 1
for (old, new) in pairs.items():
os.rename(old.absolute_path, new.absolute_path)
@vlogging.main_decorator
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('patterns', nargs='+')
parser.add_argument('--recurse', action='store_true')
parser.add_argument(
'--exif',
dest='read_exif',
action='store_true',
help='''
Program will look for EXIF metadata and use ImageDateTime, if available.
''',
)
parser.add_argument(
'--mtime',
dest='read_mtime',
action='store_true',
help='''
Program will use the file's mtime as a last resort.
''',
)
parser.add_argument(
'--minus_duration',
'--minus-duration',
dest='minus_duration',
action='store_true',
help='''
When used with --mtime, the file's duration will be subtracted.
Use for cameras that set the mtime to the end of the video and you want
the name to be the start.
''',
)
parser.add_argument('--yes', dest='autoyes', action='store_true')
parser.set_defaults(func=photo_rename_argparse)
return betterhelp.go(parser, argv)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))