Skip to content

Commit

Permalink
Fixed subtitles conversion when use original format is enabled and pr…
Browse files Browse the repository at this point in the history
…evented hearing-impaired detection for non srt format subtitles. #2693
  • Loading branch information
morpheus65535 authored Oct 12, 2024
1 parent f50bcf3 commit 43563cd
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 26 deletions.
13 changes: 8 additions & 5 deletions bazarr/subtitles/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions bazarr/subtitles/indexer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion bazarr/subtitles/manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion bazarr/subtitles/mass_download/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -79,6 +80,7 @@ def movies_download_subtitles(no):
str(movie.sceneName),
movie.title,
'movie',
movie.profileId,
check_if_still_required=True):

if result:
Expand Down
8 changes: 6 additions & 2 deletions bazarr/subtitles/mass_download/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))) \
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))) \
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions bazarr/subtitles/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion bazarr/subtitles/wanted/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def _wanted_movie(movie):
str(movie.sceneName),
movie.title,
'movie',
movie.profileId,
check_if_still_required=True):

if result:
Expand All @@ -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()

Expand Down
4 changes: 3 additions & 1 deletion bazarr/subtitles/wanted/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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))) \
Expand Down
13 changes: 8 additions & 5 deletions custom_libs/subliminal_patch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions custom_libs/subliminal_patch/core_persistent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions custom_libs/subliminal_patch/subtitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 43563cd

Please sign in to comment.