Repository navigation
Expand file tree
/
Copy pathexifer.py
More file actions
119 lines (94 loc) · 3.92 KB
/
Copy pathexifer.py
File metadata and controls
119 lines (94 loc) · 3.92 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
import sys, os, json
from lib.tools import *
tools.checkRequirements(["pymediainfo"])
from pymediainfo import MediaInfo
class program():
def __init__(self,args):
self.tool = tools(args)
self.setup(args)#process the arguments
def setup(self,args):
#Help
if self.tool.argExist("-h") or self.tool.argExist("-help") or self.tool.argExist("-?"):
self.help()
exit(0)
#the folder for source files
if self.tool.argHasValue("-f"):
val = self.tool.argValue("-f")
self.sourceFolderPath = val.replace("\\","/")
else:
print("Error, -f is missing !")
exit(1)
#the destination folder for renamed files
if self.tool.argHasValue("-df"):
val = self.tool.argValue("-df")
self.destinationFolderPath = val.replace("\\","/")
else:
self.destinationFolderPath = self.sourceFolderPath
#keep the architecture or not ?
self.flat = self.tool.argExist("-flat")#flat = no subfolders
self.keepName = self.tool.argExist("-kn")
self.extensionAllowed=[".jpg",".cr2",".mp4",".mts",".mov",".tif"]
def run(self):
for root, dirs, files in os.walk(self.sourceFolderPath):
for filename in files:
self.rename(root,filename)
def rename(self, root, file):
extension="." + file.split(".")[-1]
extension=extension.lower()
if extension in self.extensionAllowed:
try:
oldPath= (root + "/" + file).replace("\\","/")
newName=self.getNewName(root,file) + extension
if self.flat:
newPath = self.destinationFolderPath
else:
subfolders = root[len(self.sourceFolderPath):]
newPath = self.destinationFolderPath + "/" + subfolders
os.makedirs(newPath, 0o777, exist_ok = True)
newPath = newPath + "/" + newName
print("Renaming {} -> {}".format(oldPath, newPath))
os.rename(oldPath, newPath)
except:
try:
print("Error with",file)
newPath = self.destinationFolderPath + "/invalid"
os.makedirs(newPath, 0o777, exist_ok = True)
newPath = newPath + "/" + newName
print("Renaming {} -> {}".format(oldPath, newPath))
os.rename(oldPath, newPath)
except:
print("Giving up with", oldPath)
else:
print("Unallowed :", file)
def getNewName(self, root, file):
data=self.getDate(root+"/"+file)
year=data[:4]
month=data[5:7]
day=data[8:10]
hour=data[11:13]
minute=data[14:16]
second=data[17:19]
newName=year+month+day+"_"+hour+minute+second
if self.keepName :
if "." in file:
newName += "_" + file[:file.rfind(".")] #suppress the extension
else:
newName += "_" + file
return newName
def getDate(self,file):
media_info = MediaInfo.parse(file, output="JSON")
return(json.loads(media_info)["media"]["track"][0]["File_Modified_Date_Local"])
def help(self):
print("")
print("Usage: python exifer.py -f folderPath [-kn]")
print(" [[-h] | [-help] | [-?]]")
print("")
print("Options:")
print(" -f folderPath The program will rename all files in this forder (and all subfolders).")
print(" Only files with the following extensions will be renamed:")
print(" .jpg, .cr2, .mp4, .mts, .mov and .tif")
print(" -kn (Optional) Keep the original name.")
exit(0)
if __name__ == '__main__':
prog = program(sys.argv)
prog.run()