From f50bcf34ac3c246d21a69dc3cc2f2eba63314c21 Mon Sep 17 00:00:00 2001 From: pannal <1359593+pannal@users.noreply.github.com> Date: Thu, 10 Oct 2024 19:19:08 +0200 Subject: [PATCH 1/2] Fixed bad non-HI detection with embedded provider when forced subtitles available --- custom_libs/subliminal_patch/providers/embeddedsubtitles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_libs/subliminal_patch/providers/embeddedsubtitles.py b/custom_libs/subliminal_patch/providers/embeddedsubtitles.py index 2d8a492c7..943607735 100644 --- a/custom_libs/subliminal_patch/providers/embeddedsubtitles.py +++ b/custom_libs/subliminal_patch/providers/embeddedsubtitles.py @@ -287,7 +287,7 @@ def _check_hi_fallback(streams, languages): logger.debug("Checking HI fallback for '%r' language", language) streams_ = [ - stream for stream in streams if stream.language.alpha3 == language.alpha3 + stream for stream in streams if stream.language.alpha3 == language.alpha3 and stream.language.forced == language.forced ] if len(streams_) == 1 and streams_[0].disposition.hearing_impaired: stream_ = streams_[0] From 43563cdcbd9524803856228352a3ef0d1131717c Mon Sep 17 00:00:00 2001 From: morpheus65535 Date: Sat, 12 Oct 2024 11:35:27 -0400 Subject: [PATCH 2/2] Fixed subtitles conversion when use original format is enabled and prevented hearing-impaired detection for non srt format subtitles. #2693 --- bazarr/subtitles/download.py | 13 ++++++++----- bazarr/subtitles/indexer/utils.py | 8 +++++--- bazarr/subtitles/manual.py | 2 +- bazarr/subtitles/mass_download/movies.py | 4 +++- bazarr/subtitles/mass_download/series.py | 8 ++++++-- bazarr/subtitles/upgrade.py | 2 ++ bazarr/subtitles/wanted/movies.py | 4 +++- bazarr/subtitles/wanted/series.py | 4 +++- custom_libs/subliminal_patch/core.py | 13 ++++++++----- custom_libs/subliminal_patch/core_persistent.py | 2 ++ custom_libs/subliminal_patch/subtitle.py | 11 ++++------- 11 files changed, 45 insertions(+), 26 deletions(-) diff --git a/bazarr/subtitles/download.py b/bazarr/subtitles/download.py index 5f588fbf6..440e3e6f5 100644 --- a/bazarr/subtitles/download.py +++ b/bazarr/subtitles/download.py @@ -13,7 +13,7 @@ from subliminal_patch.score import ComputeScore from app.config import settings, get_scores, get_array_from -from app.database import TableEpisodes, TableMovies, database, select +from app.database import TableEpisodes, TableMovies, database, select, get_profiles_list from utilities.path_mappings import path_mappings from utilities.helper import get_target_folder, force_unicode from languages.get_languages import alpha3_from_alpha2 @@ -24,8 +24,8 @@ @update_pools -def generate_subtitles(path, languages, audio_language, sceneName, title, media_type, forced_minimum_score=None, - is_upgrade=False, profile_id=None, check_if_still_required=False, +def generate_subtitles(path, languages, audio_language, sceneName, title, media_type, profile_id, + forced_minimum_score=None, is_upgrade=False, check_if_still_required=False, previous_subtitles_to_delete=None): if not languages: return None @@ -41,6 +41,8 @@ def generate_subtitles(path, languages, audio_language, sceneName, title, media_ providers = pool.providers language_set = _get_language_obj(languages=languages) + profile = get_profiles_list(profile_id=profile_id) + original_format = profile['originalFormat'] hi_required = "force HI" if any([x.hi for x in language_set]) else False also_forced = any([x.forced for x in language_set]) forced_required = all([x.forced for x in language_set]) @@ -72,7 +74,8 @@ def generate_subtitles(path, languages, audio_language, sceneName, title, media_ pool_instance=pool, min_score=int(min_score), hearing_impaired=hi_required, - compute_score=ComputeScore(get_scores())) + compute_score=ComputeScore(get_scores()), + use_original_format=original_format in (1, "1", "True", True)) if downloaded_subtitles: for video, subtitles in downloaded_subtitles.items(): @@ -100,7 +103,7 @@ def generate_subtitles(path, languages, audio_language, sceneName, title, media_ tags=None, # fixme directory=fld, chmod=chmod, - formats=tuple(subtitle_formats), + formats=subtitle_formats, path_decoder=force_unicode ) except Exception as e: diff --git a/bazarr/subtitles/indexer/utils.py b/bazarr/subtitles/indexer/utils.py index 63b9dee6d..4e7c339a6 100644 --- a/bazarr/subtitles/indexer/utils.py +++ b/bazarr/subtitles/indexer/utils.py @@ -135,7 +135,9 @@ def guess_external_subtitles(dest_folder, subtitles, media_type, previously_inde continue text = text.decode(encoding) - if core.parse_for_hi_regex(subtitle_text=text, - alpha3_language=language.alpha3 if hasattr(language, 'alpha3') else None): - subtitles[subtitle] = Language.rebuild(subtitles[subtitle], forced=False, hi=True) + if os.path.splitext(subtitle_path)[1] == 'srt': + if core.parse_for_hi_regex(subtitle_text=text, + alpha3_language=language.alpha3 if hasattr(language, 'alpha3') else + None): + subtitles[subtitle] = Language.rebuild(subtitles[subtitle], forced=False, hi=True) return subtitles diff --git a/bazarr/subtitles/manual.py b/bazarr/subtitles/manual.py index 5d642b577..3ddd59c74 100644 --- a/bazarr/subtitles/manual.py +++ b/bazarr/subtitles/manual.py @@ -158,7 +158,7 @@ def manual_download_subtitle(path, audio_language, hi, forced, subtitle, provide subtitle.language.forced = True else: subtitle.language.forced = False - if use_original_format in ("1", "True"): + if use_original_format in (1, "1", "True", True): subtitle.use_original_format = True subtitle.mods = get_array_from(settings.general.subzero_mods) diff --git a/bazarr/subtitles/mass_download/movies.py b/bazarr/subtitles/mass_download/movies.py index 25fa04364..5698db178 100644 --- a/bazarr/subtitles/mass_download/movies.py +++ b/bazarr/subtitles/mass_download/movies.py @@ -30,7 +30,8 @@ def movies_download_subtitles(no): TableMovies.sceneName, TableMovies.title, TableMovies.tags, - TableMovies.monitored) + TableMovies.monitored, + TableMovies.profileId) .where(reduce(operator.and_, conditions))) \ .first() if not movie: @@ -79,6 +80,7 @@ def movies_download_subtitles(no): str(movie.sceneName), movie.title, 'movie', + movie.profileId, check_if_still_required=True): if result: diff --git a/bazarr/subtitles/mass_download/series.py b/bazarr/subtitles/mass_download/series.py index 3a9d998ca..8fafe583a 100644 --- a/bazarr/subtitles/mass_download/series.py +++ b/bazarr/subtitles/mass_download/series.py @@ -43,7 +43,8 @@ def series_download_subtitles(no): TableShows.title, TableEpisodes.season, TableEpisodes.episode, - TableEpisodes.title.label('episodeTitle')) + TableEpisodes.title.label('episodeTitle'), + TableShows.profileId) .select_from(TableEpisodes) .join(TableShows) .where(reduce(operator.and_, conditions))) \ @@ -87,6 +88,7 @@ def series_download_subtitles(no): str(episode.sceneName), episode.title, 'series', + episode.profileId, check_if_still_required=True): if result: if isinstance(result, tuple) and len(result): @@ -117,7 +119,8 @@ def episode_download_subtitles(no, send_progress=False): TableShows.seriesType, TableEpisodes.title.label('episodeTitle'), TableEpisodes.season, - TableEpisodes.episode) + TableEpisodes.episode, + TableShows.profileId) .select_from(TableEpisodes) .join(TableShows) .where(reduce(operator.and_, conditions))) \ @@ -159,6 +162,7 @@ def episode_download_subtitles(no, send_progress=False): str(episode.sceneName), episode.title, 'series', + episode.profileId, check_if_still_required=True): if result: if isinstance(result, tuple) and len(result): diff --git a/bazarr/subtitles/upgrade.py b/bazarr/subtitles/upgrade.py index 1c565bd0d..a78ca950d 100644 --- a/bazarr/subtitles/upgrade.py +++ b/bazarr/subtitles/upgrade.py @@ -107,6 +107,7 @@ def upgrade_subtitles(): str(episode['sceneName']), episode['seriesTitle'], 'series', + episode['profileId'], forced_minimum_score=int(episode['score']), is_upgrade=True, previous_subtitles_to_delete=path_mappings.path_replace( @@ -192,6 +193,7 @@ def upgrade_subtitles(): str(movie['sceneName']), movie['title'], 'movie', + movie['profileId'], forced_minimum_score=int(movie['score']), is_upgrade=True, previous_subtitles_to_delete=path_mappings.path_replace_movie( diff --git a/bazarr/subtitles/wanted/movies.py b/bazarr/subtitles/wanted/movies.py index 16c363386..9d20f5140 100644 --- a/bazarr/subtitles/wanted/movies.py +++ b/bazarr/subtitles/wanted/movies.py @@ -50,6 +50,7 @@ def _wanted_movie(movie): str(movie.sceneName), movie.title, 'movie', + movie.profileId, check_if_still_required=True): if result: @@ -69,7 +70,8 @@ def wanted_download_subtitles_movie(radarr_id): TableMovies.audio_language, TableMovies.sceneName, TableMovies.failedAttempts, - TableMovies.title) + TableMovies.title, + TableMovies.profileId) .where(TableMovies.radarrId == radarr_id)) \ .all() diff --git a/bazarr/subtitles/wanted/series.py b/bazarr/subtitles/wanted/series.py index 4bc687415..dc5d19d8b 100644 --- a/bazarr/subtitles/wanted/series.py +++ b/bazarr/subtitles/wanted/series.py @@ -51,6 +51,7 @@ def _wanted_episode(episode): str(episode.sceneName), episode.title, 'series', + episode.profileId, check_if_still_required=True): if result: if isinstance(result, tuple) and len(result): @@ -71,7 +72,8 @@ def wanted_download_subtitles(sonarr_episode_id): TableEpisodes.audio_language, TableEpisodes.sceneName, TableEpisodes.failedAttempts, - TableShows.title) + TableShows.title, + TableShows.profileId) .select_from(TableEpisodes) .join(TableShows) .where((TableEpisodes.sonarrEpisodeId == sonarr_episode_id))) \ diff --git a/custom_libs/subliminal_patch/core.py b/custom_libs/subliminal_patch/core.py index 0fc2ac0a7..708cbd58b 100644 --- a/custom_libs/subliminal_patch/core.py +++ b/custom_libs/subliminal_patch/core.py @@ -524,7 +524,7 @@ def download_subtitle(self, subtitle): return True def download_best_subtitles(self, subtitles, video, languages, min_score=0, hearing_impaired=False, only_one=False, - compute_score=None): + compute_score=None, use_original_format=False): """Download the best matching subtitles. patch: @@ -543,6 +543,7 @@ def download_best_subtitles(self, subtitles, video, languages, min_score=0, hear :param bool only_one: download only one subtitle, not one per language. :param compute_score: function that takes `subtitle` and `video` as positional arguments, `hearing_impaired` as keyword argument and returns the score. + :param bool use_original_format: preserve original subtitles format :return: downloaded subtitles. :rtype: list of :class:`~subliminal.subtitle.Subtitle` @@ -620,6 +621,9 @@ def download_best_subtitles(self, subtitles, video, languages, min_score=0, hear subtitle, score) continue + # make sure to preserve original subtitles format if requested + subtitle.use_original_format = use_original_format + # download logger.debug("%r: Trying to download subtitle with matches %s, score: %s; release(s): %s", subtitle, matches, score, subtitle.release_info) @@ -1213,10 +1217,9 @@ def save_subtitles(file_path, subtitles, single=False, directory=None, chmod=Non continue # create subtitle path - if subtitle.text and parse_for_hi_regex(subtitle_text=subtitle.text, - alpha3_language=subtitle.language.alpha3 if - (hasattr(subtitle, 'language') and hasattr(subtitle.language, 'alpha3')) - else None): + if (subtitle.text and subtitle.format == 'srt' and + parse_for_hi_regex(subtitle_text=subtitle.text, alpha3_language=subtitle.language.alpha3 if + (hasattr(subtitle, 'language') and hasattr(subtitle.language, 'alpha3')) else None)): subtitle.language.hi = True subtitle_path = get_subtitle_path(file_path, None if single else subtitle.language, forced_tag=subtitle.language.forced, diff --git a/custom_libs/subliminal_patch/core_persistent.py b/custom_libs/subliminal_patch/core_persistent.py index e98914901..31ec61273 100644 --- a/custom_libs/subliminal_patch/core_persistent.py +++ b/custom_libs/subliminal_patch/core_persistent.py @@ -50,6 +50,7 @@ def download_best_subtitles( hearing_impaired=False, only_one=False, compute_score=None, + use_original_format=False, **kwargs ): downloaded_subtitles = defaultdict(list) @@ -77,6 +78,7 @@ def download_best_subtitles( hearing_impaired=hearing_impaired, only_one=only_one, compute_score=compute_score, + use_original_format=use_original_format, ) logger.info("Downloaded %d subtitle(s)", len(subtitles)) downloaded_subtitles[video].extend(subtitles) diff --git a/custom_libs/subliminal_patch/subtitle.py b/custom_libs/subliminal_patch/subtitle.py index c65f8cdd2..82d5a6895 100644 --- a/custom_libs/subliminal_patch/subtitle.py +++ b/custom_libs/subliminal_patch/subtitle.py @@ -313,13 +313,10 @@ def is_valid(self): logger.info("Got FPS from MicroDVD subtitle: %s", subs.fps) else: logger.info("Got format: %s", subs.format) - self._og_format = subs.format - self._is_valid = True - # if self.use_original_format: - # self.format = subs.format - # self._is_valid = True - # logger.debug("Using original format") - return True + if self.use_original_format: + self._og_format = subs.format + self._is_valid = True + return True except pysubs2.UnknownFPSError: # if parsing failed, use frame rate from provider