rewrite subtitle search in folders
This commit is contained in:
105
subgen.py
105
subgen.py
@@ -1,4 +1,4 @@
|
|||||||
subgen_version = '2025.02.89'
|
subgen_version = '2025.02.90'
|
||||||
|
|
||||||
from language_code import LanguageCode
|
from language_code import LanguageCode
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
@@ -1253,66 +1253,69 @@ def has_subtitle_language_in_file(video_file: str, target_language: Union[Langua
|
|||||||
logging.error(f"An error occurred while checking the file with pyav: {type(e).__name__}: {e}")
|
logging.error(f"An error occurred while checking the file with pyav: {type(e).__name__}: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def has_subtitle_of_language_in_folder(video_file, target_language: LanguageCode, recursion = True):
|
def has_subtitle_of_language_in_folder(video_file: str, target_language: LanguageCode, recursion: bool = True, only_skip_if_subgen_subtitle: bool = False) -> bool:
|
||||||
"""Checks if the given folder has a subtitle file with the given language.
|
"""Checks if the given folder has a subtitle file with the given language.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
video_file: The path of the video file.
|
video_file (str): The path of the video file.
|
||||||
target_language: The language of the subtitle file that we are looking for.
|
target_language (LanguageCode): The language of the subtitle file to search for.
|
||||||
recursion: If True, search in subfolders of the given folder. If False,
|
recursion (bool): If True, search subfolders. If False, only the current folder.
|
||||||
only search in the given folder.
|
only_skip_if_subgen_subtitle (bool): If True, only skip if subtitles are auto-generated ("subgen").
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
True if a subtitle file with the given language is found in the folder,
|
bool: True if a matching subtitle file is found, False otherwise.
|
||||||
False otherwise.
|
|
||||||
"""
|
"""
|
||||||
subtitle_extensions = ['.srt', '.vtt', '.sub', '.ass', '.ssa', '.idx', '.sbv', '.pgs', '.ttml', '.lrc']
|
subtitle_extensions = {'.srt', '.vtt', '.sub', '.ass', '.ssa', '.idx', '.sbv', '.pgs', '.ttml', '.lrc'}
|
||||||
|
|
||||||
# just get the name of the movie e.g. movie.2025.remastered
|
video_folder = os.path.dirname(video_file)
|
||||||
video_file_stripped = os.path.splitext(os.path.split(video_file)[1])[0]
|
video_name = os.path.splitext(os.path.basename(video_file))[0]
|
||||||
folder_path = os.path.dirname(video_file)
|
|
||||||
for file_name in os.listdir(folder_path):
|
logging.debug(f"Searching for subtitles in: {video_folder}")
|
||||||
file_path = os.path.join(folder_path, file_name)
|
|
||||||
|
for file_name in os.listdir(video_folder):
|
||||||
if os.path.isfile(file_path):
|
file_path = os.path.join(video_folder, file_name)
|
||||||
root, ext = os.path.splitext(file_name)
|
|
||||||
if root.startswith(video_file_stripped) and ext.lower() in subtitle_extensions:
|
# If it's a file and has a subtitle extension
|
||||||
parts = root[len(video_file_stripped):].lstrip(".").split(".")
|
if os.path.isfile(file_path) and file_path.endswith(tuple(subtitle_extensions)):
|
||||||
|
subtitle_name, ext = os.path.splitext(file_name)
|
||||||
has_subgen = "subgen" in parts # Checks if "subgen" is in parts
|
|
||||||
|
# Ensure the subtitle name starts with the video name
|
||||||
#checking this first because e.g LanguageCode.from_string("subgen") == LanguageCode.NONE is equal to True. Maybe handle this better with a check with a function like is language code. To check if part is a valid language before comparing it to target_language
|
if not subtitle_name.startswith(video_name):
|
||||||
|
continue
|
||||||
if target_language == LanguageCode.NONE:
|
|
||||||
if only_skip_if_subgen_subtitle:
|
# Extract parts after video filename
|
||||||
if has_subgen:
|
subtitle_parts = subtitle_name[len(video_name):].lstrip(".").split(".")
|
||||||
logger.debug("Subtitles from subgen found in the folder. ")
|
|
||||||
return skip_if_language_is_not_set_but_subtitles_exist
|
# Check for "subgen"
|
||||||
else:
|
has_subgen = "subgen" in subtitle_parts
|
||||||
#might be other subtitles that have subgen in the name
|
|
||||||
continue
|
# Special handling if only skipping for subgen subtitles
|
||||||
logger.debug("Subtitles exist in the folder. and only_skip_if_subgen_subtitle is False.")
|
if target_language == LanguageCode.NONE:
|
||||||
return skip_if_language_is_not_set_but_subtitles_exist
|
if only_skip_if_subgen_subtitle:
|
||||||
|
if has_subgen:
|
||||||
if any(LanguageCode.from_string(part) == target_language for part in parts):
|
logging.debug("Skipping subtitles because they are auto-generated ('subgen').")
|
||||||
# If the subtitle is found, return True
|
return False
|
||||||
if only_skip_if_subgen_subtitle:
|
logging.debug("Skipping subtitles because language is NONE.")
|
||||||
if has_subgen:
|
return True # Default behavior if subtitles exist
|
||||||
logger.debug(f"Subtitles from subgen in '{target_language}' language found in the folder.")
|
|
||||||
return True
|
# Check if the subtitle file matches the target language
|
||||||
else:
|
if is_valid_subtitle_language(subtitle_parts, target_language):
|
||||||
#might be other subtitles that have subgen in the name
|
if only_skip_if_subgen_subtitle and not has_subgen:
|
||||||
continue
|
continue # Ignore non-subgen subtitles if flag is set
|
||||||
logger.debug(f"Subtitles in '{target_language}' language found in the folder.")
|
logging.debug(f"Found matching subtitle: {file_name} for language {target_language.name} (subgen={has_subgen})")
|
||||||
return True
|
|
||||||
elif os.path.isdir(file_path) and recursion:
|
|
||||||
# Looking in the subfolders of the video for subtitles
|
|
||||||
if has_subtitle_of_language_in_folder(os.path.join(file_path, os.path.split(video_file)[1]) , target_language, False):
|
|
||||||
# If the language is found in the subfolders, return True
|
|
||||||
return True
|
return True
|
||||||
# If the language is not found, return False
|
|
||||||
|
# Recursively search subfolders
|
||||||
|
elif os.path.isdir(file_path) and recursion:
|
||||||
|
if has_subtitle_of_language_in_folder(os.path.join(file_path, os.path.basename(video_file)), target_language, False, only_skip_if_subgen_subtitle):
|
||||||
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def is_valid_subtitle_language(subtitle_parts: List[str], target_language: LanguageCode) -> bool:
|
||||||
|
"""Checks if any part of the subtitle name matches the target language."""
|
||||||
|
return any(LanguageCode.from_string(part) == target_language for part in subtitle_parts)
|
||||||
|
|
||||||
def get_next_plex_episode(current_episode_rating_key, stay_in_season: bool = False):
|
def get_next_plex_episode(current_episode_rating_key, stay_in_season: bool = False):
|
||||||
"""
|
"""
|
||||||
Get the next episode's ratingKey based on the current episode in Plex.
|
Get the next episode's ratingKey based on the current episode in Plex.
|
||||||
|
|||||||
Reference in New Issue
Block a user