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_to = os.getenv('PATH_MAPPING_TO', r'/Volumes/TV')
model_location = os.getenv('MODEL_PATH', './models')
monitor = convert_to_bool(os.getenv('MONITOR', False))
transcribe_folders = os.getenv('TRANSCRIBE_FOLDERS', '')
monitor = convert_to_bool(os.getenv('MONITOR', True))
transcribe_folders = os.getenv('TRANSCRIBE_FOLDERS', './test')
transcribe_or_translate = os.getenv('TRANSCRIBE_OR_TRANSLATE', 'transcribe')
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))
@@ -310,7 +310,8 @@ def receive_tautulli_webhook(
logging.debug("Path of file: " + fullpath)
# gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True)
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
if gen_subtitles_check(path_mapping(fullpath)):
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
else:
return {
"message": "This doesn't appear to be a properly configured Tautulli webhook, please review the instructions again!"}
@@ -338,7 +339,8 @@ def receive_plex_webhook(
logging.debug("Path of file: " + fullpath)
# gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True)
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
if gen_subtitles_check(path_mapping(fullpath)):
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
refresh_plex_metadata(plex_json['Metadata']['ratingKey'], plexserver, plextoken)
logging.info(f"Metadata for item {plex_json['Metadata']['ratingKey']} refreshed successfully.")
except Exception as e:
@@ -364,7 +366,8 @@ def receive_jellyfin_webhook(
logging.debug(f"Path of file: {fullpath}")
# gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True)
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
if gen_subtitles_check(path_mapping(fullpath)):
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
try:
refresh_jellyfin_metadata(ItemId, jellyfinserver, jellyfintoken)
logging.info(f"Metadata for item {ItemId} refreshed successfully.")
@@ -398,7 +401,8 @@ def receive_emby_webhook(
if event == "library.new" and procaddedmedia or event == "playback.start" and procmediaonplay:
logging.debug("Path of file: " + fullpath)
# gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True)
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
if gen_subtitles_check(path_mapping(fullpath)):
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate, True)
return ""
@@ -509,6 +513,27 @@ def write_lrc(result, file_path):
fraction = int((segment.start - int(segment.start)) * 100)
file.write(f"[{minutes:02d}:{seconds:02d}.{fraction:02d}] {segment.text}\n")
def gen_subtitles_check(file_path: str):
if not has_audio(file_path):
logging.debug(f"{file_path} doesn't have any audio to transcribe!")
return False
if file_path in files_to_transcribe:
logging.info(f"File {os.path.basename(file_path)} is already in the transcription list. Skipping.")
return
message = None
if has_subtitle_language(file_path, skipifinternalsublang):
message = f"{file_path} already has an internal subtitle we want, skipping generation"
elif os.path.exists(file_path.rsplit('.', 1)[0] + subextension):
message = f"{file_path} already has a subtitle created for this, skipping it"
elif os.path.exists(file_path.rsplit('.', 1)[0] + subextensionSDH):
message = f"{file_path} already has a SDH subtitle created for this, skipping it"
if message:
logging.info(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.
@@ -521,31 +546,14 @@ def gen_subtitles(file_path: str, transcription_type: str, add_to_front=True, fo
"""
try:
if not has_audio(file_path):
logging.debug(f"{file_path} doesn't have any audio to transcribe!")
return None
if file_path in files_to_transcribe:
logging.info(f"File {os.path.basename(file_path)} is already in the transcription list. Skipping.")
return
message = None
if has_subtitle_language(file_path, skipifinternalsublang):
message = f"{file_path} already has an internal subtitle we want, skipping generation"
elif os.path.exists(file_path.rsplit('.', 1)[0] + subextension):
message = f"{file_path} already has a subtitle created for this, skipping it"
elif os.path.exists(file_path.rsplit('.', 1)[0] + subextensionSDH):
message = f"{file_path} already has a SDH subtitle created for this, skipping it"
if message:
logging.info(message)
return message
if add_to_front:
files_to_transcribe.insert(0, file_path)
else:
files_to_transcribe.append(file_path)
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)}")
start_time = time.time()
@@ -765,7 +773,8 @@ if monitor:
# Call the gen_subtitles function
logging.info(f"File: {path_mapping(file_path)} was added")
# gen_subtitles(path_mapping(file_path), transcribe_or_translate, False)
gen_subtitles_queue(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)
def on_created(self, event):
self.create_subtitle(event)
def on_modified(self, event):
@@ -781,12 +790,14 @@ def transcribe_existing(transcribe_folders, forceLanguage=None):
for file in files:
file_path = os.path.join(root, file)
# gen_subtitles(path_mapping(file_path), transcribe_or_translate, False, forceLanguage)
gen_subtitles_queue(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)
# if the path specified was actually a single file and not a folder, process it
if os.path.isfile(path):
if has_audio(path):
# gen_subtitles(path_mapping(path), transcribe_or_translate, False, forceLanguage)
gen_subtitles_queue(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)
# Set up the observer to watch for new files
if monitor:
observer = Observer()