Compare commits

..

No commits in common. "master" and "no_audio" have entirely different histories.

2 changed files with 14 additions and 54 deletions

55
main.py
View File

@ -2,9 +2,6 @@ import glob
import os import os
import hashlib import hashlib
import time import time
import traceback
from multiprocessing import Pool
import queue
from PIL import Image, UnidentifiedImageError from PIL import Image, UnidentifiedImageError
from imagehash import average_hash from imagehash import average_hash
@ -21,7 +18,7 @@ print('ENTER to continue, ctrl+c to cancel.')
try: try:
input() input()
except KeyboardInterrupt: except KeyboardInterrupt:
print('Cancelled.') print('\nCancelled.')
os._exit(0) os._exit(0)
print('Sorting file list by smallest size...') print('Sorting file list by smallest size...')
@ -39,75 +36,51 @@ for filename in filenames:
hasher.update(buf) hasher.update(buf)
digest = hasher.hexdigest() digest = hasher.hexdigest()
if digest in hashes: if digest in hashes:
print('Found digest', digest, 'collision for', filename)
delete.add(filename) delete.add(filename)
else: else:
hashes.add(digest) hashes.add(digest)
print('Found', len(delete), 'duplicates by md5 hash.') print('Found', len(delete), 'duplicates by md5 hash.')
time.sleep(4)
print('Deduplicating by media fingerprint...') print('Deduplicating by media fingerprint...')
def get_image_hash(filename): def get_image_hash(filename):
basename = os.path.basename(os.path.dirname(filename))
try: try:
image = Image.open(filename) image = Image.open(filename)
return basename + str(average_hash(image)) + '_image' return str(average_hash(image)) + '_image'
except UnidentifiedImageError: except UnidentifiedImageError:
return None return None
def get_video_hash(filename): def get_video_hash(filename):
basename = os.path.basename(os.path.dirname(filename))
try: try:
v = VideoHash(path=filename) return str(VideoHash(path=filename).hash_hex) + '_video'
digest = str(v.hash_hex)
v.delete_storage_path()
return basename + digest + '_video'
except FFmpegFailedToExtractFrames: except FFmpegFailedToExtractFrames:
return None return None
count = 0 count = 0
total = len(filenames) total = len(filenames)
def hasher(filename): for filename in filenames:
if filename in delete: return None count += 1
print('Hashing file', count, '/', total, end='\r')
print('Hashing file:', filename) if filename in delete: continue
try: try:
digest = get_image_hash(filename)# or get_video_hash(filename) digest = get_image_hash(filename) or get_video_hash(filename)
except KeyboardInterrupt: except KeyboardInterrupt:
print('Cancelled.') print('\nCancelled.')
os._exit(0) os._exit(0)
except BaseException as e: except BaseException as e:
print() print()
print('Exception', e.__class__.__name__, str(e), 'while hashing:') print('Exception', e.__class__.__name__, str(e), 'while hashing:')
print(filename) print(filename)
print(traceback.format_exc()) continue
return None
if not digest: return None time.sleep(5)
return (filename, digest) if not digest: continue
with Pool() as pool:
results = pool.map(hasher, filenames)
print('Finished hashing.')
print()
print('Checking digests:')
print()
for result in results:
if not result: continue
print(result)
filename, digest = result
if digest in hashes: if digest in hashes:
print('Found digest', digest, 'collision for', filename)
delete.add(filename) delete.add(filename)
else: else:
hashes.add(digest) hashes.add(digest)
@ -115,6 +88,6 @@ for result in results:
print() print()
print('Found', len(delete), 'total duplicate files.') print('Found', len(delete), 'total duplicate files.')
for dupe in sorted(list(delete)): print('Deleting...')
print('Deleting:', dupe) for dupe in delete:
os.remove(dupe) os.remove(dupe)

View File

@ -1,13 +0,0 @@
Brotli==1.1.0
certifi==2023.7.22
imagedominantcolor==1.0.1
ImageHash==4.3.1
mutagen==1.47.0
numpy==1.26.1
Pillow==10.1.0
pycryptodomex==3.19.0
PyWavelets==1.4.1
scipy==1.11.3
videohash==3.0.1
websockets==11.0.3
yt-dlp==2023.10.13