Compare commits

..

2 Commits

Author SHA1 Message Date
965197cd6a Add debug log level 2025-01-18 18:50:10 +00:00
860417488f Generate encoder arguments in separate method 2025-01-18 18:13:18 +00:00
4 changed files with 36 additions and 17 deletions

View File

@ -1,6 +1,7 @@
INFO=0 DEBUG=0
WARNING=5 INFO=5
ERROR=10 WARNING=10
ERROR=20
from .log import Log from .log import Log
from .logcat import LogCat from .logcat import LogCat

View File

@ -2,11 +2,11 @@ from datetime import datetime
from multiprocessing import Lock from multiprocessing import Lock
from threading import Thread from threading import Thread
from queue import Queue, Empty from queue import Queue, Empty
from . import INFO, WARNING, ERROR from . import DEBUG, INFO, WARNING, ERROR
class Log(object): class Log(object):
def __init__(self, path, queue=None, print_output=True, timeout=1, level=WARNING): def __init__(self, path, queue=None, print_output=True, timeout=1, level=DEBUG):
self.__terminated = False self.__terminated = False
self.level = level self.level = level
self.file_lock = Lock() self.file_lock = Lock()
@ -78,4 +78,9 @@ class Log(object):
self.set_log_file() self.set_log_file()
with self.file_lock: with self.file_lock:
with open(self.log_file, mode='a') as log_file: with open(self.log_file, mode='a') as log_file:
log_file.writelines(log_msg_list) for line in log_msg_list:
try:
log_file.write(line)
except Exception as e:
print(f"Error writing line{line}")
print(e)

View File

@ -1,6 +1,6 @@
from datetime import datetime from datetime import datetime
from multiprocessing import Queue from multiprocessing import Queue
from . import INFO, WARNING, ERROR from . import DEBUG, INFO, WARNING, ERROR
class LogCat(object): class LogCat(object):
@ -13,6 +13,9 @@ class LogCat(object):
time = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") time = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
self.queue.put((level, f"{self.level_string(level)} | {time} - {self.category.upper()}.{function.upper()}: {message}")) self.queue.put((level, f"{self.level_string(level)} | {time} - {self.category.upper()}.{function.upper()}: {message}"))
def debug(self, function: str, message: str) -> None:
self._write(DEBUG, function, message)
def info(self, function: str, message: str) -> None: def info(self, function: str, message: str) -> None:
self._write(INFO, function, message) self._write(INFO, function, message)
@ -24,7 +27,9 @@ class LogCat(object):
@staticmethod @staticmethod
def level_string(level: int) -> str: def level_string(level: int) -> str:
if level == INFO: if level == DEBUG:
return "DEBG"
elif level == INFO:
return "INFO" return "INFO"
elif level == WARNING: elif level == WARNING:
return "WARN" return "WARN"

View File

@ -27,29 +27,37 @@ class Worker:
shutil.copy(file.path, output_path) shutil.copy(file.path, output_path)
log.info("ART", f"Successfully copied {output_path}") log.info("ART", f"Successfully copied {output_path}")
except FileExistsError: except FileExistsError:
pass log.warning("ART", f"File exists: {output_path}")
except Exception: except Exception:
log.error("ART", f"{file.path} failed to copy") log.error("ART", f"{file.path} failed to copy")
def transcode_audio(self, track, encoder, log): def transcode_audio(self, track, encoder, log):
enc_filename = encoder.parts[-1]
track_output = track.output_file(self.output_root, self.extension) track_output = track.output_file(self.output_root, self.extension)
if track_output.exists(): if track_output.exists():
log.info("AUD", f"Skipped {track_output}") log.info("AUD", f"Skipped {track_output}")
return return
self.create_directories(track) self.create_directories(track)
if enc_filename == "opusenc.exe": encoder_args = self.encoder_args(encoder, track, log)
additional_args = ('--bitrate', '128', '--music')
subprocess_args = (str(encoder),) + additional_args + (str(track.path), str(track_output))
elif enc_filename == "qaac64.exe":
additional_args = (str(track), '-o', str(track_output), '--threading')
subprocess_args = (str(encoder),) + additional_args
try: try:
subprocess.run(subprocess_args, capture_output=True, text=True, check=True) subprocess.run(encoder_args, capture_output=True, text=True, check=True)
log.info("AUD", f"Transcoded '{track}' successfully.") log.info("AUD", f"Transcoded '{track}' successfully.")
except Exception: except Exception:
log.error("AUD", f"ERROR: Transcoding of '{track}' failed.") log.error("AUD", f"ERROR: Transcoding of '{track}' failed.")
def encoder_args(self, encoder, track, log):
track_output = track.output_file(self.output_root, self.extension)
enc_filename = encoder.parts[-1]
encoder_args = ""
if enc_filename == "opusenc.exe":
additional_args = ('--bitrate', '128', '--music')
encoder_args = (str(encoder),) + additional_args + (str(track.path), str(track_output))
elif enc_filename == "qaac64.exe":
additional_args = (str(track), '-o', str(track_output), '--threading')
encoder_args = (str(encoder),) + additional_args
else:
log.error("AUD", f"Encoder {encoder} not recognised.")
return encoder_args
def create_directories(self, track: File): def create_directories(self, track: File):
if not track.artist_out(self.output_root).exists(): if not track.artist_out(self.output_root).exists():
try: try: