Cleaned up checks

This commit is contained in:
McCloudS
2024-04-20 08:17:08 -06:00
committed by GitHub
parent bec60ea49a
commit 72f264870f

View File

@@ -83,8 +83,8 @@ def update_env_variables():
path_mapping_from = os.getenv('PATH_MAPPING_FROM', r'/tv') path_mapping_from = os.getenv('PATH_MAPPING_FROM', r'/tv')
path_mapping_to = os.getenv('PATH_MAPPING_TO', r'/Volumes/TV') path_mapping_to = os.getenv('PATH_MAPPING_TO', r'/Volumes/TV')
model_location = os.getenv('MODEL_PATH', './models') model_location = os.getenv('MODEL_PATH', './models')
monitor = convert_to_bool(os.getenv('MONITOR', False)) monitor = convert_to_bool(os.getenv('MONITOR', True))
transcribe_folders = os.getenv('TRANSCRIBE_FOLDERS', '') transcribe_folders = os.getenv('TRANSCRIBE_FOLDERS', './test')
transcribe_or_translate = os.getenv('TRANSCRIBE_OR_TRANSLATE', 'transcribe') transcribe_or_translate = os.getenv('TRANSCRIBE_OR_TRANSLATE', 'transcribe')
force_detected_language_to = os.getenv('FORCE_DETECTED_LANGUAGE_TO', '').lower() force_detected_language_to = os.getenv('FORCE_DETECTED_LANGUAGE_TO', '').lower()
clear_vram_on_complete = convert_to_bool(os.getenv('CLEAR_VRAM_ON_COMPLETE', True)) clear_vram_on_complete = convert_to_bool(os.getenv('CLEAR_VRAM_ON_COMPLETE', True))
@@ -310,6 +310,7 @@ def receive_tautulli_webhook(
logging.debug("Path of file: " + fullpath) logging.debug("Path of file: " + fullpath)
# gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True) # gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True)
if gen_subtitles_check(path_mapping(fullpath)):
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True) gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
else: else:
return { return {
@@ -338,6 +339,7 @@ def receive_plex_webhook(
logging.debug("Path of file: " + fullpath) logging.debug("Path of file: " + fullpath)
# gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True) # gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True)
if gen_subtitles_check(path_mapping(fullpath)):
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True) gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
refresh_plex_metadata(plex_json['Metadata']['ratingKey'], plexserver, plextoken) refresh_plex_metadata(plex_json['Metadata']['ratingKey'], plexserver, plextoken)
logging.info(f"Metadata for item {plex_json['Metadata']['ratingKey']} refreshed successfully.") logging.info(f"Metadata for item {plex_json['Metadata']['ratingKey']} refreshed successfully.")
@@ -364,6 +366,7 @@ def receive_jellyfin_webhook(
logging.debug(f"Path of file: {fullpath}") logging.debug(f"Path of file: {fullpath}")
# gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True) # gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True)
if gen_subtitles_check(path_mapping(fullpath)):
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True) gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
try: try:
refresh_jellyfin_metadata(ItemId, jellyfinserver, jellyfintoken) refresh_jellyfin_metadata(ItemId, jellyfinserver, jellyfintoken)
@@ -398,6 +401,7 @@ def receive_emby_webhook(
if event == "library.new" and procaddedmedia or event == "playback.start" and procmediaonplay: if event == "library.new" and procaddedmedia or event == "playback.start" and procmediaonplay:
logging.debug("Path of file: " + fullpath) logging.debug("Path of file: " + fullpath)
# gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True) # gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True)
if gen_subtitles_check(path_mapping(fullpath)):
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True) gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
return "" return ""
@@ -509,21 +513,10 @@ def write_lrc(result, file_path):
fraction = int((segment.start - int(segment.start)) * 100) fraction = int((segment.start - int(segment.start)) * 100)
file.write(f"[{minutes:02d}:{seconds:02d}.{fraction:02d}] {segment.text}\n") file.write(f"[{minutes:02d}:{seconds:02d}.{fraction:02d}] {segment.text}\n")
def gen_subtitles_check(file_path: str):
def gen_subtitles(file_path: str, transcription_type: str, add_to_front=True, force_language=None) -> None:
"""Generates subtitles for a video file.
Args:
file_path: str - The path to the video file.
transcription_type: str - The type of transcription or translation to perform.
add_to_front: bool - Whether to add the file to the front of the transcription queue. Default is True.
force_language: str - The language to force for transcription or translation. Default is None.
"""
try:
if not has_audio(file_path): if not has_audio(file_path):
logging.debug(f"{file_path} doesn't have any audio to transcribe!") logging.debug(f"{file_path} doesn't have any audio to transcribe!")
return None return False
if file_path in files_to_transcribe: if file_path in files_to_transcribe:
logging.info(f"File {os.path.basename(file_path)} is already in the transcription list. Skipping.") logging.info(f"File {os.path.basename(file_path)} is already in the transcription list. Skipping.")
@@ -538,14 +531,29 @@ def gen_subtitles(file_path: str, transcription_type: str, add_to_front=True, fo
message = f"{file_path} already has a SDH subtitle created for this, skipping it" message = f"{file_path} already has a SDH subtitle created for this, skipping it"
if message: if message:
logging.info(message) logging.info(message)
return message return False
return True
def gen_subtitles(file_path: str, transcription_type: str, add_to_front=True, force_language=None) -> None:
"""Generates subtitles for a video file.
Args:
file_path: str - The path to the video file.
transcription_type: str - The type of transcription or translation to perform.
add_to_front: bool - Whether to add the file to the front of the transcription queue. Default is True.
force_language: str - The language to force for transcription or translation. Default is None.
"""
try:
if add_to_front: if add_to_front:
files_to_transcribe.insert(0, file_path) files_to_transcribe.insert(0, file_path)
else: else:
files_to_transcribe.append(file_path) files_to_transcribe.append(file_path)
logging.info(f"Added {os.path.basename(file_path)} for transcription.") logging.info(f"Added {os.path.basename(file_path)} for transcription.")
logging.info(f"{len(files_to_transcribe)} files in the queue for transcription") #logging.info(f"{len(files_to_transcribe)} files in the queue for transcription")
logging.info(f"Transcribing file: {os.path.basename(file_path)}") logging.info(f"Transcribing file: {os.path.basename(file_path)}")
start_time = time.time() start_time = time.time()
@@ -765,6 +773,7 @@ if monitor:
# Call the gen_subtitles function # Call the gen_subtitles function
logging.info(f"File: {path_mapping(file_path)} was added") logging.info(f"File: {path_mapping(file_path)} was added")
# gen_subtitles(path_mapping(file_path), transcribe_or_translate, False) # gen_subtitles(path_mapping(file_path), transcribe_or_translate, False)
if gen_subtitles_check(path_mapping(fullpath)):
gen_subtitles_queue(path_mapping(file_path), transcribe_or_translate, False) gen_subtitles_queue(path_mapping(file_path), transcribe_or_translate, False)
def on_created(self, event): def on_created(self, event):
self.create_subtitle(event) self.create_subtitle(event)
@@ -781,11 +790,13 @@ def transcribe_existing(transcribe_folders, forceLanguage=None):
for file in files: for file in files:
file_path = os.path.join(root, file) file_path = os.path.join(root, file)
# gen_subtitles(path_mapping(file_path), transcribe_or_translate, False, forceLanguage) # gen_subtitles(path_mapping(file_path), transcribe_or_translate, False, forceLanguage)
if gen_subtitles_check(path_mapping(file_path)):
gen_subtitles_queue(path_mapping(file_path), transcribe_or_translate, False, forceLanguage) gen_subtitles_queue(path_mapping(file_path), transcribe_or_translate, False, forceLanguage)
# if the path specified was actually a single file and not a folder, process it # if the path specified was actually a single file and not a folder, process it
if os.path.isfile(path): if os.path.isfile(path):
if has_audio(path): if has_audio(path):
# gen_subtitles(path_mapping(path), transcribe_or_translate, False, forceLanguage) # gen_subtitles(path_mapping(path), transcribe_or_translate, False, forceLanguage)
if gen_subtitles_check(path_mapping(path)):
gen_subtitles_queue(path_mapping(path), transcribe_or_translate, False, forceLanguage) gen_subtitles_queue(path_mapping(path), transcribe_or_translate, False, forceLanguage)
# Set up the observer to watch for new files # Set up the observer to watch for new files
if monitor: if monitor: