-
Notifications
You must be signed in to change notification settings - Fork 2
/
rename_multimedia_files.py
executable file
·472 lines (427 loc) · 18 KB
/
rename_multimedia_files.py
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
批量修改照片文件名称的Python脚本程序。
遍历指定目录(含子目录)的照片文件,并
根据拍照时间将照片文件名修改为以下格式:
IMG_20140315_091230.JPG
如果存在重名文件,则尝试如下文件名:
IMG_20140315_091230_01.JPG
...
IMG_20140315_091230_99.jpg
本程序需要安装下列模块:
$ sudo apt install python3-pip
$ pip3 --version
$ sudo pip3 install exifread
$ pip3 show exifread
Name: ExifRead
Version: 2.3.2
Summary: Read Exif metadata from tiff and jpeg files.
Home-page: https://github.com/ianare/exif-py
Author: Ianaré Sévi
Author-email: [email protected]
License: BSD
Location: /usr/local/lib/python3.8/dist-packages
Requires:
Required-by:
$ pip3 install python-dateutil
$ pip3 show python-dateutil
Name: python-dateutil
Version: 2.8.1
Summary: Extensions to the standard Python datetime module
Home-page: https://dateutil.readthedocs.io
Author: Gustavo Niemeyer
Author-email: [email protected]
License: Dual License
Location: /home/chenwx/.local/lib/python3.8/site-packages
Requires: six
Required-by:
$ sudo apt install exiftool
'''
import os
import stat
import time
import exifread
import argparse
import subprocess
from datetime import datetime
from dateutil import tz
from pathlib import Path
# FILE_TYPE
IMG_FILE_TYPE = "IMG_"
VID_FILE_TYPE = "VID_"
AUD_FILE_TYPE = "AUD_"
# Extension Names
IMG_SUFFIX_FILTER = [ '.JPG', '.PNG', '.BMP', '.JPEG' ]
VID_SUFFIX_FILTER = [ '.MP4', '.MPG', '.MOV', '.AVI' ]
AUD_SUFFIX_FILTER = [ '.M4A', '.WAV' ]
# Global Variables
isRecursive = False
isExecute = False
aFile = ""
useModifiedDate = False
handlePhoto = False
handleVedio = False
handleAudio = False
useAbsPath = False
isQuiet = True
# China Time Zone
CN_TIME_ZONE = "+08:00"
def parseArguments():
# Parse arguments
parser = argparse.ArgumentParser(description='')
parser.add_argument("-d", "--directory", help="Specify directory.")
parser.add_argument("-r", "--recursive", action="store_true", help="Recursive all sub-directories.")
parser.add_argument("-e", "--execute", action="store_true", help="Execute the rename action.")
parser.add_argument("-f", "--file", help="Specify a file.")
parser.add_argument("-m", "--modified", action="store_true", help="Use modified date if EXIF data missing.")
parser.add_argument("-a", "--all", action="store_true", help="Process photos and vedios.")
parser.add_argument("-p", "--photo", action="store_true", help="Process photos.")
parser.add_argument("-v", "--vedio", action="store_true", help="Process vedios.")
parser.add_argument("-b", "--abspath", action="store_true", help="Print abspath.")
parser.add_argument("-n", "--notquiet", action="store_true", help="Print all rename info.")
args = parser.parse_args()
# -d, --directory
path = "."
if args.directory and Path(args.directory).exists():
path = args.directory
# -r, --recursive
global isRecursive
if args.recursive:
isRecursive = True
# -e, --execute
global isExecute
if args.execute:
isExecute = True
# -f, --file
global aFile
if args.file:
aFile = args.file
# -m, --modified
global useModifiedDate
if args.modified:
useModifiedDate = True
# -a, --all
global handlePhoto
global handleVedio
global handleAudio
if args.all:
handlePhoto = True
handleVedio = True
handleAudio = True
# -p, --photo
if args.photo:
handlePhoto = True
# -v, --vedio
if args.vedio:
handleVedio = True
# -b, --abspath
global useAbsPath
if args.abspath:
useAbsPath = True
# -n, --notquiet
global isQuiet
if args.notquiet:
isQuiet = False
print("Path :", os.path.abspath(path))
print("Recursive :", isRecursive)
print("Execute :", isExecute)
print("aFile :", aFile)
print("Use Modified Date :", useModifiedDate)
print("Handle Photo :", handlePhoto)
print("Handle Vedio :", handleVedio)
print("Handle Audio :", handleAudio)
print("Use AbsPath :", useAbsPath)
print("Quiet :", isQuiet)
print('\n')
return path
def isTargetFileType(fileName):
# 根据文件扩展名,判断是否是需要处理的文件类型
fileNameNoPath = os.path.basename(fileName)
f, e = os.path.splitext(fileNameNoPath)
if e.upper() in IMG_SUFFIX_FILTER:
return True, IMG_FILE_TYPE
elif e.upper() in VID_SUFFIX_FILTER:
return True, VID_FILE_TYPE
elif e.upper() in AUD_SUFFIX_FILTER:
return True, AUD_FILE_TYPE
else:
return False, ""
def isSameFileName(fileName, newFileName):
retVal = False
lenExt = 4
if len(newFileName) <= lenExt:
return False
lenName = len(newFileName) - lenExt
if fileName[:lenName] == newFileName[:lenName] and fileName[-lenExt:] == newFileName[-lenExt:]:
retVal = True
return retVal
def generateNewFileName(fileName):
# 根据照片的拍照时间生成新的文件名。如果获取不到拍照时间,则直接跳过
try:
if os.path.isfile(fileName):
fd = open(fileName, 'rb')
else:
raise "[%s] is not a file!\n" % fileName
except:
raise "unopen file[%s]\n" % fileName
# Default value
retVal = False
newFileName = ""
# 原文件信息
absPathFile = os.path.abspath(fileName)
dirname = os.path.dirname(absPathFile)
fileNameNoPath = os.path.basename(absPathFile)
f, e = os.path.splitext(fileNameNoPath)
# Check if the file can be handled or not
isSupported, fileType = isTargetFileType(fileName)
if isSupported == False:
return retVal, newFileName
myDataFormat = fileType + '%Y%m%d_%H%M%S'
if fileType == IMG_FILE_TYPE and handlePhoto == True:
# 如果取得Exif信息,则根据照片的拍摄日期重命名文件
exifTags = exifread.process_file(fd)
if exifTags:
try:
# 取得照片的拍摄日期,并转换成 yyyymmdd_hhmmss 格式
t = exifTags [ 'EXIF DateTimeOriginal' ]
dateStr = fileType + str(t).replace(":", "")[:8] + "_" + str(t)[11:].replace(":", "")
# 生成新文件名
newFileName = os.path.join(dateStr + e).upper()
retVal = True
except:
pass
elif fileType == VID_FILE_TYPE and handleVedio == True:
exiftoolCmd = "exiftool " + fileName
exiftoolVal = subprocess.run(exiftoolCmd, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
metaData = str(exiftoolVal.stdout).split("\\n")
src_zone = tz.tzutc()
dst_zone = tz.tzlocal()
# For ".MP4":
# File Type : MP4
# File Type Extension : mp4
# MIME Type : video/mp4
# Major Brand : MP4 v2 [ISO 14496-14]
if e.upper() == ".MP4":
for element in metaData:
if "Media Create Date" in element:
try:
mediaCreateDate = (element.split(" : ")[1])
utcDateTime = datetime.strptime(mediaCreateDate, '%Y:%m:%d %H:%M:%S')
utcDateTime = utcDateTime.replace(tzinfo=src_zone)
locDateTime = utcDateTime.astimezone(dst_zone)
dateStr = locDateTime.strftime(myDataFormat)
newFileName = os.path.join(dateStr + e).upper()
retVal = True
break
except:
retVal = False
break
# For ".MPG":
# File Type : MPEG
# File Type Extension : mpg
# MIME Type : video/mpeg
elif e.upper() == ".MPG":
for element in metaData:
if "File Modification Date/Time" in element:
try:
fileModificationDateTime = (element.split(" : ")[1])
modificationDateTime = fileModificationDateTime[:19]
timeZone = fileModificationDateTime[19:]
if timeZone == CN_TIME_ZONE:
locDateTime = datetime.strptime(modificationDateTime, '%Y:%m:%d %H:%M:%S')
dateStr = locDateTime.strftime(myDataFormat)
newFileName = os.path.join(dateStr + e).upper()
retVal = True
break
else:
utcDateTime = datetime.strptime(modificationDateTime, '%Y:%m:%d %H:%M:%S')
utcDateTime = utcDateTime.replace(tzinfo=src_zone)
locDateTime = utcDateTime.astimezone(dst_zone)
dateStr = locDateTime.strftime(myDataFormat)
newFileName = os.path.join(dateStr + e).upper()
retVal = True
break
except:
retVal = False
break
# For ".MOV":
# File Type : MOV
# File Type Extension : mov
# MIME Type : video/quicktime
# Major Brand : Apple QuickTime (.MOV/QT)
elif e.upper() == ".MOV":
for element in metaData:
if "Creation Date" in element:
try:
creationDate = (element.split(" : ")[1])
creationDateTime = creationDate[:19]
timeZone = creationDate[19:]
if timeZone == CN_TIME_ZONE:
locDateTime = datetime.strptime(creationDateTime, '%Y:%m:%d %H:%M:%S')
dateStr = locDateTime.strftime(myDataFormat)
newFileName = os.path.join(dateStr + e).upper()
retVal = True
break
else:
utcDateTime = datetime.strptime(creationDateTime, '%Y:%m:%d %H:%M:%S')
utcDateTime = utcDateTime.replace(tzinfo=src_zone)
locDateTime = utcDateTime.astimezone(dst_zone)
dateStr = locDateTime.strftime(myDataFormat)
newFileName = os.path.join(dateStr + e).upper()
retVal = True
break
except:
retVal = False
break
elif fileType == AUD_FILE_TYPE and handleAudio == True:
exiftoolCmd = "exiftool " + fileName
exiftoolVal = subprocess.run(exiftoolCmd, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
metaData = str(exiftoolVal.stdout).split("\\n")
src_zone = tz.tzutc()
dst_zone = tz.tzlocal()
# For ".M4A":
# File Type : M4A
# File Type Extension : m4a
# MIME Type : audio/mp4
# Major Brand : Apple iTunes AAC-LC (.M4A) Audio
if e.upper() == ".M4A":
for element in metaData:
if "Create Date" in element:
try:
creationDate = (element.split(" : ")[1])
creationDateTime = creationDate[:19]
timeZone = creationDate[19:]
if timeZone == CN_TIME_ZONE:
locDateTime = datetime.strptime(creationDateTime, '%Y:%m:%d %H:%M:%S')
dateStr = locDateTime.strftime(myDataFormat)
newFileName = os.path.join(dateStr + e).upper()
retVal = True
break
else:
utcDateTime = datetime.strptime(creationDateTime, '%Y:%m:%d %H:%M:%S')
utcDateTime = utcDateTime.replace(tzinfo=src_zone)
locDateTime = utcDateTime.astimezone(dst_zone)
dateStr = locDateTime.strftime(myDataFormat)
newFileName = os.path.join(dateStr + e).upper()
retVal = True
break
except:
retVal = False
break
# For ".WAV":
# File Type : WAV
# File Type Extension : wav
# MIME Type : audio/x-wav
# Encoding : Microsoft PCM
elif e.upper() == ".WAV":
for element in metaData:
if "File Modification Date/Time" in element:
try:
creationDate = (element.split(" : ")[1])
creationDateTime = creationDate[:19]
timeZone = creationDate[19:]
if timeZone == CN_TIME_ZONE:
locDateTime = datetime.strptime(creationDateTime, '%Y:%m:%d %H:%M:%S')
dateStr = locDateTime.strftime(myDataFormat)
newFileName = os.path.join(dateStr + e).upper()
retVal = True
break
else:
utcDateTime = datetime.strptime(creationDateTime, '%Y:%m:%d %H:%M:%S')
utcDateTime = utcDateTime.replace(tzinfo=src_zone)
locDateTime = utcDateTime.astimezone(dst_zone)
dateStr = locDateTime.strftime(myDataFormat)
newFileName = os.path.join(dateStr + e).upper()
retVal = True
break
except:
retVal = False
break
# 如果获取Exif信息失败,则采用该照片的创建日期重命名文件
if retVal == False and useModifiedDate == True:
state = os.stat(fileName)
dateStr = time.strftime(myDataFormat, time.localtime(state[-2]))
newFileName = os.path.join(dateStr + e).upper()
retVal = True
# 检查新文件名是否合法
if retVal == True:
# 如果新文件名与原文件名相同,则无需改名
if isSameFileName(fileName, newFileName):
retVal = False
# 如果新文件名与其他文件重名,则在新文件明后加后缀 _01, _02, .., _99
elif newFileName in os.listdir(dirname):
for i in range(1, 100):
tmpDateStr = dateStr + "_" + str(i).zfill(2)
newFileName = os.path.join(tmpDateStr + e).upper()
if isSameFileName(fileName, newFileName):
retVal = False
break
elif newFileName not in os.listdir(dirname):
retVal = True
break
else:
retVal = True
return retVal, newFileName
def scanDir(startdir):
# 只检查指定的一个文件
if aFile != "":
if os.path.isfile(aFile):
obj = aFile
# 对满足过滤条件的文件进行改名处理
retVal, newFileName = generateNewFileName(obj)
if retVal:
try:
if isExecute == True:
os.rename(obj, newFileName)
if useAbsPath == True:
print(os.path.abspath(obj), "\t=>\t", newFileName)
else:
print(obj, "\t=>\t", newFileName)
except:
if isQuiet == False:
if useAbsPath == True:
print(os.path.abspath(obj), "\t=>\tcannot rename to ", newFileName)
else:
print(obj, "\t=>\tcannot rename to ", newFileName)
else:
if isQuiet == False:
if useAbsPath == True:
print(os.path.abspath(obj), "\t=>\tNo change")
else:
print(obj, "\t=>\tNo change")
else:
print(obj, " is not a vaild file!")
return
# 遍历指定目录以及子目录,对满足条件的文件进行改名
os.chdir(startdir)
for obj in os.listdir(os.curdir):
if os.path.isfile(obj):
# 对满足过滤条件的文件进行改名处理
retVal, newFileName = generateNewFileName(obj)
if retVal:
try:
if isExecute == True:
os.rename(obj, newFileName)
if useAbsPath == True:
print(os.path.abspath(obj), "\t=>\t", newFileName)
else:
print(obj, "\t=>\t", newFileName)
except:
if isQuiet == False:
if useAbsPath == True:
print(os.path.abspath(obj), "\t=>\tcannot rename to ", newFileName)
else:
print(obj, "\t=>\tcannot rename to ", newFileName)
else:
if isQuiet == False:
if useAbsPath == True:
print(os.path.abspath(obj), "\t=>\tNo change")
else:
print(obj, "\t=>\tNo change")
elif os.path.isdir(obj) and isRecursive == True:
scanDir(obj)
os.chdir(os.pardir)
if __name__ == "__main__":
path = parseArguments()
scanDir(path)