diff --git a/bazarr/api/subtitles/subtitles.py b/bazarr/api/subtitles/subtitles.py index 805d74a17..01f486204 100644 --- a/bazarr/api/subtitles/subtitles.py +++ b/bazarr/api/subtitles/subtitles.py @@ -94,7 +94,8 @@ class Subtitles(Resource): radarr_id=id) else: use_original_format = True if args.get('original_format') == 'true' else False - subtitles_apply_mods(language, subtitles_path, [action], use_original_format) + subtitles_apply_mods(language=language, subtitle_path=subtitles_path, mods=[action], + use_original_format=use_original_format, video_path=video_path) # apply chmod if required chmod = int(settings.general.chmod, 8) if not sys.platform.startswith( diff --git a/bazarr/subtitles/tools/mods.py b/bazarr/subtitles/tools/mods.py index a29b715c3..126050b1b 100644 --- a/bazarr/subtitles/tools/mods.py +++ b/bazarr/subtitles/tools/mods.py @@ -4,19 +4,22 @@ import os import logging from subliminal_patch.subtitle import Subtitle +from subliminal_patch.core import get_subtitle_path from subzero.language import Language +from app.config import settings from languages.custom_lang import CustomLanguage from languages.get_languages import alpha3_from_alpha2 -def subtitles_apply_mods(language, subtitle_path, mods, use_original_format): +def subtitles_apply_mods(language, subtitle_path, mods, use_original_format, video_path): language = alpha3_from_alpha2(language) custom = CustomLanguage.from_value(language, "alpha3") if custom is None: lang_obj = Language(language) else: lang_obj = custom.subzero_language() + single = settings.general.getboolean('single_language') sub = Subtitle(lang_obj, mods=mods, original_format=use_original_format) with open(subtitle_path, 'rb') as f: @@ -31,8 +34,17 @@ def subtitles_apply_mods(language, subtitle_path, mods, use_original_format): content = sub.get_modified_content() if content: + if hasattr(sub, 'mods') and isinstance(sub.mods, list) and 'remove_HI' in sub.mods: + modded_subtitles_path = get_subtitle_path(video_path, None if single else sub.language, + forced_tag=sub.language.forced, hi_tag=False, tags=[]) + else: + modded_subtitles_path = subtitle_path + if os.path.exists(subtitle_path): os.remove(subtitle_path) - with open(subtitle_path, 'wb') as f: + if os.path.exists(modded_subtitles_path): + os.remove(modded_subtitles_path) + + with open(modded_subtitles_path, 'wb') as f: f.write(content) diff --git a/libs/subliminal_patch/core.py b/libs/subliminal_patch/core.py index 9c8f6159d..2950467a9 100644 --- a/libs/subliminal_patch/core.py +++ b/libs/subliminal_patch/core.py @@ -1118,6 +1118,9 @@ def save_subtitles(file_path, subtitles, single=False, directory=None, chmod=Non saved_subtitles = [] for subtitle in subtitles: + # check if HI mods will be used to get the proper name for the subtitles file + must_remove_hi = 'remove_HI' in subtitle.mods + # check content if subtitle.content is None: logger.error('Skipping subtitle %r: no content', subtitle) @@ -1130,7 +1133,8 @@ def save_subtitles(file_path, subtitles, single=False, directory=None, chmod=Non # create subtitle path subtitle_path = get_subtitle_path(file_path, None if single else subtitle.language, - forced_tag=subtitle.language.forced, hi_tag=subtitle.language.hi, tags=tags) + forced_tag=subtitle.language.forced, + hi_tag=False if must_remove_hi else subtitle.language.hi, tags=tags) if directory is not None: subtitle_path = os.path.join(directory, os.path.split(subtitle_path)[1])