-
Notifications
You must be signed in to change notification settings - Fork 2
/
toolDeleteInDifferentFolder.py
326 lines (288 loc) · 12.1 KB
/
toolDeleteInDifferentFolder.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# Created by Mario Chen, 04.01.2021, Shenzhen
# My Github site: https://github.com/Mario-Hero
import sys
import os
import hashlib
# import datefinder
try:
import cv2
except ImportError:
os.system('pip install opencv-python')
import cv2
import numpy as np
from shutil import move
# you can edit this >>>>>>
MAX_HASH_SIZE = 100 # MB. Set it to 0 to disable MAX_HASH_SIZE.
DELETE_VIDEO_WITH_SAME_CONTENT = True
# <<<<<< you can edit this
MAX_HASH_TIME = MAX_HASH_SIZE * 128
SHOULD_NOT_DELETE = ['.ini', '.dll', '.exe']
VIDEO_SUPPORT = ['mp4', 'webm', 'avi', 'mov', '.mkv', '.wmv', '.m3u8', '.3g2', '.3gp', '.3gp2', '.3gpp', '.amv', '.asf',
'.bik', '.divx', '.drc', '.dv', '.f4v', '.flv', '.gvi', '.gxf', '.iso', '.m1v', '.m2v', '.m2t',
'.m2ts',
'.m4v', '.mp2', '.mp4v', '.mpe', '.mpeg', '.mpeg1', '.mpeg2', '.mpeg4', '.mpg', '.mpv2', '.mts',
'.mxf', '.mxg', '.nsv', '.nuv', '.ogg', '.ogm', '.ogv', '.ps', '.rec', '.rm', '.rmvb', '.rpl', '.thp',
'.tod', '.ts', '.tts', '.txd', '.vob', '.vro', '.wm', '.wtv', '.xesc']
VIDEO_CUTTIME = 3
def getVidLength(filename):
cap = cv2.VideoCapture(filename)
if cap.isOpened():
rate = cap.get(5)
frame_num = cap.get(7)
duration = frame_num / rate
return duration
return -1
def captureFrame(vidPath):
vidCapture = cv2.VideoCapture(vidPath)
if vidCapture.isOpened():
frameNumber = vidCapture.get(7)
else:
return False
frames = []
for i in range(1, VIDEO_CUTTIME + 1):
vidCapture.set(cv2.CAP_PROP_POS_MSEC, int((frameNumber / VIDEO_CUTTIME + 1) * i))
# print("f: "+str(int((frameNumber/cutTime)*i)))
success, image = vidCapture.read()
if success:
resized = cv2.resize(image, (150, 150), interpolation=cv2.INTER_NEAREST)
# resized = cv2.cvtColor(resized, cv2.COLOR_BGR2HSV)
frames.append(resized)
else:
return ''
return frames
def create_rgb_hist(image):
h, w, c = image.shape
# 创建一个(16*16*16,1)的初始矩阵,作为直方图矩阵
# 16*16*16的意思为三通道每通道有16个bins
rgbhist = np.zeros([16 * 16 * 16, 1], np.float32)
bsize = 256 / 16
for row in range(h):
for col in range(w):
b = image[row, col, 0]
g = image[row, col, 1]
r = image[row, col, 2]
index = int(b / bsize) * 16 * 16 + int(g / bsize) * 16 + int(r / bsize)
rgbhist[int(index), 0] += 1
return rgbhist
class fileStruct:
def __init__(self, fileName='', path='', size=0):
self.name = fileName # name with extension
self.path = path # complete path
self.size = size
self.md5: str = ''
def updateSize(self):
self.size = os.path.getsize(self.path)
class videoStruct:
def __init__(self, fileName='', path=''):
self.name = fileName # name without extension
self.path = path # complete path
self.date = None # date in the name of file
self.size = 0
self.md5: str = ''
self.length: float = 0.0
self.frames = None
def updateNormal(self):
'''
self.size = os.path.getsize(self.path)
try:
dateMatches = list(datefinder.find_dates(self.name))
if len(dateMatches) > 0:
self.date = dateMatches[0]
except:
pass
'''
self.length = getVidLength(self.path)
def printInfo(self):
print('Name: ' + self.name)
print('Date: ' + str(self.date))
print('Size: ' + str(self.size))
print('Length: ' + str(self.length))
def isVid(fileName: str):
for ext in VIDEO_SUPPORT:
if fileName.endswith(ext):
return True
return False
def videoFrameCompare(vid1: videoStruct, vid2: videoStruct) -> float:
if not vid1.frames:
vid1.frames = captureFrame(vid1.path)
if not vid2.frames:
vid2.frames = captureFrame(vid2.path)
result: float = 0.0
if vid1.frames and vid2.frames:
for i in range(len(vid1.frames)):
hist1 = create_rgb_hist(vid1.frames[i])
hist2 = create_rgb_hist(vid2.frames[i])
oneResult = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
if oneResult < 0.9:
return oneResult
# print(oneResult)
result += oneResult
result = result / VIDEO_CUTTIME
return result
def GetFileMd5(filename):
if not os.path.isfile(filename):
return
myhash = hashlib.md5()
f = open(filename, 'rb')
hashI = 0
while hashI < MAX_HASH_TIME or MAX_HASH_TIME <= 0:
b = f.read(8192)
if not b:
break
myhash.update(b)
hashI += 1
f.close()
return myhash.hexdigest()
def lengthClose(a, b):
return abs(a - b) < 0.4
def sizeShow(size):
SIZE_STR = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
i = 0
while size > 1024:
size /= 1024
i += 1
return '(' + str(round(size, 1)) + SIZE_STR[i] + ')'
def canDelete(fileName):
for keepStr in SHOULD_NOT_DELETE:
if fileName.endswith(keepStr):
return False
return True
def deleteInDifferentFolder(folders: list[str], numList: list[int]):
fileList: list[fileStruct] = []
for folder in folders:
if os.path.isdir(folder):
for file in os.listdir(folder):
fileList.append(
fileStruct(file, os.path.join(folder, file), os.path.getsize(os.path.join(folder, file))))
print('Size testing...')
fileListLength = len(fileList)
keepFolder = []
for num in numList:
if not folders[num - 1] in keepFolder:
keepFolder.append(folders[num - 1])
i = 0
while i < fileListLength - 1:
j = i + 1
while j < fileListLength:
if fileList[i].size == fileList[j].size:
if canDelete(fileList[i].name) and canDelete(fileList[j].name):
if GetFileMd5(fileList[i].path) == GetFileMd5(fileList[j].path):
parenti = os.path.split(fileList[i].path)[0]
parentj = os.path.split(fileList[j].path)[0]
if parenti in keepFolder:
if not parentj in keepFolder:
removeJ = True
else:
name1 = os.path.splitext(fileList[i].name)[0]
name2 = os.path.splitext(fileList[j].name)[0]
if name1.startswith(name2):
removeJ = False
elif name2.startswith(name1):
removeJ = True
else:
removeJ = len(name1) < len(name2)
else:
removeJ = False
fileListLength -= 1
if removeJ:
os.remove(fileList[j].path)
print("Keep: " + fileList[i].path)
print("Remove same: " + fileList[j].path + '\n')
fileList.pop(j)
continue
else:
os.remove(fileList[i].path)
print("Keep: " + fileList[j].path)
print("Remove same: " + fileList[i].path + '\n')
fileList.pop(i)
i -= 1
break
j += 1
i += 1
if DELETE_VIDEO_WITH_SAME_CONTENT:
videoList: list[videoStruct] = []
for file in fileList:
if isVid(file.name):
videoList.append(videoStruct(os.path.splitext(file.name)[0], file.path))
for struct in videoList:
struct.updateNormal()
# struct.printInfo()
print('Video Content testing...')
fileListLength = len(videoList)
i = 0
while i < fileListLength - 1:
j = i + 1
while j < fileListLength:
if lengthClose(videoList[i].length, videoList[j].length):
# print('\nCompare:')
# print(videoList[i].name)
# print(videoList[j].name)
result = videoFrameCompare(videoList[i], videoList[j])
if 0.5 < result < 0.99:
print('Warning:')
print(videoList[i].name + '\n' + videoList[j].name)
print('Similarity: ' + str(result) + '\n')
elif result > 0.99:
if videoList[i].size == 0:
videoList[i].size = os.path.getsize(videoList[i].path)
if videoList[j].size == 0:
videoList[j].size = os.path.getsize(videoList[j].path)
removeJ = videoList[i].size > videoList[j].size # Keep the bigger video.
fileListLength = fileListLength - 1
if removeJ:
os.remove(videoList[j].path)
parentj = os.path.split(videoList[j].path)[0]
if parentj in keepFolder:
newPath = os.path.join(parentj, os.path.split(videoList[i].path)[1])
if not os.path.exists(newPath):
move(videoList[i].path, parentj)
videoList[i].path = newPath
print('Keep ' + sizeShow(videoList[i].size) + ': ' + videoList[i].name)
print('Remove ' + sizeShow(videoList[j].size) + ': ' + videoList[j].name + '\n')
videoList.pop(j)
continue
else:
os.remove(videoList[i].path)
parenti = os.path.split(videoList[i].path)[0]
if parenti in keepFolder:
newPath = os.path.join(parenti, os.path.split(videoList[j].path)[1])
if not os.path.exists(newPath):
move(videoList[j].path, parenti)
videoList[j].path = newPath
print('Keep ' + sizeShow(videoList[j].size) + ': ' + videoList[j].name)
print('Remove ' + sizeShow(videoList[i].size) + ': ' + videoList[i].name + '\n')
videoList.pop(i)
i -= 1
break
j += 1
i += 1
if __name__ == '__main__':
if len(sys.argv) >= 3:
i = 1
for inputFolder in sys.argv[1:]:
print(str(i) + ': ' + inputFolder)
i += 1
while True:
num = input('Input the Folder Number you want to keep:\n').strip()
numList: list[int] = []
numberOK = True
if ' ' in num:
for number in num.split():
if 1 <= int(number) <= len(sys.argv) - 1:
numList.append(int(number))
else:
numberOK = False
break
else:
if 1 <= int(num) <= len(sys.argv) - 1:
numList.append(int(num))
else:
numberOK = False
if numberOK:
deleteInDifferentFolder(sys.argv[1:], numList)
break
else:
continue
os.system("pause")