vnrecode: improve duplications check for mt

This commit is contained in:
OleSTEEP 2024-09-04 01:21:59 +03:00
parent 03647d4b84
commit 92474b4aa4
3 changed files with 27 additions and 12 deletions

View file

@ -42,6 +42,7 @@ class Application:
for future in as_completed(futures): for future in as_completed(futures):
future.result() future.result()
self.utils.print_duplicates()
self.utils.get_compression_status(source) self.utils.get_compression_status(source)
self.utils.sys_pause() self.utils.sys_pause()
print(f"Time taken: {datetime.now() - start_time}") print(f"Time taken: {datetime.now() - start_time}")

View file

@ -46,7 +46,7 @@ class Compress:
def audio(self, in_dir, file, out_dir, extension): def audio(self, in_dir, file, out_dir, extension):
bit_rate = self.params.audio_bitrate bit_rate = self.params.audio_bitrate
out_file = self.utils.check_duplicates(f'{out_dir}/{os.path.splitext(file)[0]}.{extension}') out_file = self.utils.check_duplicates(in_dir, out_dir, f'{os.path.splitext(file)[0]}.{extension}')
try: try:
(FFmpeg() (FFmpeg()
.input(f'{in_dir}/{file}') .input(f'{in_dir}/{file}')
@ -65,7 +65,7 @@ class Compress:
def video(self, in_dir, file, out_dir, extension): def video(self, in_dir, file, out_dir, extension):
if not self.params.video_skip: if not self.params.video_skip:
out_file = self.utils.check_duplicates(f'{out_dir}/{os.path.splitext(file)[0]}.{extension}') out_file = self.utils.check_duplicates(in_dir, out_dir, f'{os.path.splitext(file)[0]}.{extension}')
codec = self.params.video_codec codec = self.params.video_codec
crf = self.params.video_crf crf = self.params.video_crf
@ -91,7 +91,7 @@ class Compress:
def image(self, in_dir, file, out_dir, extension): def image(self, in_dir, file, out_dir, extension):
quality = self.params.image_quality quality = self.params.image_quality
out_file = self.utils.check_duplicates(f"{out_dir}/{os.path.splitext(file)[0]}.{extension}") out_file = self.utils.check_duplicates(in_dir, out_dir, f"{os.path.splitext(file)[0]}.{extension}")
try: try:
image = Image.open(f'{in_dir}/{file}') image = Image.open(f'{in_dir}/{file}')
@ -127,7 +127,7 @@ class Compress:
def unknown(self, in_dir, filename, out_dir): def unknown(self, in_dir, filename, out_dir):
if self.params.force_compress: if self.params.force_compress:
self.printer.unknown_file(filename) self.printer.unknown_file(filename)
out_file = self.utils.check_duplicates(f'{out_dir}/{filename}') out_file = self.utils.check_duplicates(in_dir, out_dir, filename)
try: try:
(FFmpeg() (FFmpeg()
.input(f'{in_dir}/{filename}') .input(f'{in_dir}/{filename}')
@ -144,7 +144,6 @@ class Compress:
self.utils.add_unprocessed_file(f'{in_dir}/{filename}', f'{out_dir}/{filename}') self.utils.add_unprocessed_file(f'{in_dir}/{filename}', f'{out_dir}/{filename}')
return f'{out_dir}/{filename}' return f'{out_dir}/{filename}'
def compress(self, _dir, filename, source, output): def compress(self, _dir, filename, source, output):
match File.get_type(filename): match File.get_type(filename):
case "audio": case "audio":
@ -157,7 +156,7 @@ class Compress:
out_file = self.unknown(_dir, filename, output) out_file = self.unknown(_dir, filename, output)
if self.params.mimic_mode: if self.params.mimic_mode:
os.rename(out_file, f'{_dir}/{filename}'.replace(source, f"{source}_compressed")) self.utils.mimic_rename(out_file, f'{_dir}/{filename}', source)
self.printer.bar.update() self.printer.bar.update()
self.printer.bar.next() self.printer.bar.next()

View file

@ -1,4 +1,5 @@
from shutil import copyfile from shutil import copyfile
from glob import glob
import sys import sys
import os import os
@ -8,6 +9,7 @@ class Utils:
self.errors = 0 self.errors = 0
self.params = params self.params = params
self.printer = printer self.printer = printer
self.duplicates = []
@staticmethod @staticmethod
def sys_pause(): def sys_pause():
@ -61,11 +63,24 @@ class Utils:
copyfile(orig_folder, new_folder) copyfile(orig_folder, new_folder)
self.printer.info(f"File {filename} copied to compressed folder.") self.printer.info(f"File {filename} copied to compressed folder.")
def check_duplicates(self, new_folder): def check_duplicates(self, in_dir, out_dir, filename):
filename = new_folder.split().pop() duplicates = glob(f"{in_dir}/{os.path.splitext(filename)[0]}.*")
if os.path.exists(new_folder): if len(duplicates) > 1:
if filename in self.duplicates:
new_name = os.path.splitext(filename)[0] + "(vncopy)" + os.path.splitext(filename)[1]
return f"{out_dir}/{new_name}"
self.duplicates.append(filename)
return f"{out_dir}/{filename}"
def print_duplicates(self):
for filename in self.duplicates:
self.printer.warning( self.printer.warning(
f'Duplicate file has been found! Check manually this files - "{filename}", ' f'Duplicate file has been found! Check manually this files - "{filename}", '
f'"{os.path.splitext(filename)[0] + "(copy)" + os.path.splitext(filename)[1]}"') f'"{os.path.splitext(filename)[0] + "(vncopy)" + os.path.splitext(filename)[1]}"')
return os.path.splitext(new_folder)[0] + "(copy)" + os.path.splitext(new_folder)[1]
return new_folder @staticmethod
def mimic_rename(filename, target, source):
if filename.count("(vncopy)"):
target = os.path.splitext(target)[0] + "(vncopy)" + os.path.splitext(target)[1]
os.rename(filename, target.replace(source, f"{source}_compressed"))