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.
  • Loading branch information
morpheus65535 committed Oct 11, 2024
1 parent 5c8abac commit 561904b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
5 changes: 2 additions & 3 deletions bazarr/subtitles/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,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 All @@ -84,8 +85,6 @@ def generate_subtitles(path, languages, audio_language, sceneName, title, media_
subtitle_formats = set()
for s in subtitles:
s.mods = subz_mods
if original_format in (1, "1", "True", True):
s.use_original_format = True
subtitle_formats.add(s.format)

try:
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
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
15 changes: 8 additions & 7 deletions custom_libs/subliminal_patch/subtitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,14 @@ 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
# if self.use_original_format:
# self.format = subs.format
# self._is_valid = True
# logger.debug("Using original format")
return True

except pysubs2.UnknownFPSError:
# if parsing failed, use frame rate from provider
Expand Down

0 comments on commit 561904b

Please sign in to comment.