From ad8f116c78688ab038e834a8ad3a3b4c7bfee0c6 Mon Sep 17 00:00:00 2001 From: MrHereAndThere <38396158+boydaihungst@users.noreply.github.com> Date: Tue, 20 Feb 2024 09:10:27 +0700 Subject: [PATCH] Updated fallback language for embeddedsubtitles provider to be able to specify the one to use --- bazarr/app/config.py | 9 ++++++++- bazarr/app/get_providers.py | 3 ++- frontend/src/pages/Settings/Providers/list.ts | 10 ++++++++-- libs/subliminal_patch/providers/embeddedsubtitles.py | 8 +++++--- tests/bazarr/test_app_get_providers.py | 3 ++- tests/subliminal_patch/test_embeddedsubtitles.py | 12 ++++++------ 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/bazarr/app/config.py b/bazarr/app/config.py index f4ce412fe..3920969e2 100644 --- a/bazarr/app/config.py +++ b/bazarr/app/config.py @@ -291,7 +291,8 @@ validators = [ Validator('embeddedsubtitles.included_codecs', must_exist=True, default=[], is_type_of=list), Validator('embeddedsubtitles.hi_fallback', must_exist=True, default=False, is_type_of=bool), Validator('embeddedsubtitles.timeout', must_exist=True, default=600, is_type_of=int, gte=1), - Validator('embeddedsubtitles.unknown_as_english', must_exist=True, default=False, is_type_of=bool), + Validator('embeddedsubtitles.unknown_as_fallback', must_exist=True, default=False, is_type_of=bool), + Validator('embeddedsubtitles.fallback_lang', must_exist=True, default='en', is_type_of=str, cast=str), # karagarga section Validator('karagarga.username', must_exist=True, default='', is_type_of=str, cast=str), @@ -448,6 +449,12 @@ if settings.general.wanted_search_frequency == 3: if settings.general.wanted_search_frequency_movie == 3: settings.general.wanted_search_frequency_movie = 6 +# backward compatibility embeddedsubtitles provider +if hasattr(settings.embeddedsubtitles, 'unknown_as_english'): + if settings.embeddedsubtitles.unknown_as_english: + settings.embeddedsubtitles.unknown_as_fallback = True + settings.embeddedsubtitles.fallback_lang = 'en' + del settings.embeddedsubtitles.unknown_as_english # save updated settings to file write_config() diff --git a/bazarr/app/get_providers.py b/bazarr/app/get_providers.py index bba6cd990..7516c38b9 100644 --- a/bazarr/app/get_providers.py +++ b/bazarr/app/get_providers.py @@ -301,7 +301,8 @@ def get_providers_auth(): 'ffprobe_path': _FFPROBE_BINARY, 'ffmpeg_path': _FFMPEG_BINARY, 'timeout': settings.embeddedsubtitles.timeout, - 'unknown_as_english': settings.embeddedsubtitles.unknown_as_english, + 'unknown_as_fallback': settings.embeddedsubtitles.unknown_as_fallback, + 'fallback_lang': settings.embeddedsubtitles.fallback_lang, }, 'karagarga': { 'username': settings.karagarga.username, diff --git a/frontend/src/pages/Settings/Providers/list.ts b/frontend/src/pages/Settings/Providers/list.ts index 6beb38551..faaa9b992 100644 --- a/frontend/src/pages/Settings/Providers/list.ts +++ b/frontend/src/pages/Settings/Providers/list.ts @@ -156,8 +156,14 @@ export const ProviderList: Readonly = [ }, { type: "switch", - key: "unknown_as_english", - name: "Use subtitles with unknown info/language as english", + key: "unknown_as_fallback", + name: "Use subtitles with unknown info/language as fallback language", + }, + { + type: "text", + key: "fallback_lang", + name: "Fallback language", + defaultValue: "en", }, ], message: diff --git a/libs/subliminal_patch/providers/embeddedsubtitles.py b/libs/subliminal_patch/providers/embeddedsubtitles.py index 045347563..7fbf1bef8 100644 --- a/libs/subliminal_patch/providers/embeddedsubtitles.py +++ b/libs/subliminal_patch/providers/embeddedsubtitles.py @@ -85,7 +85,8 @@ class EmbeddedSubtitlesProvider(Provider): ffmpeg_path=None, hi_fallback=False, timeout=600, - unknown_as_english=False, + unknown_as_fallback=False, + fallback_lang="en", ): self._included_codecs = set(included_codecs or _ALLOWED_CODECS) @@ -97,7 +98,8 @@ class EmbeddedSubtitlesProvider(Provider): cache_dir or tempfile.gettempdir(), self.__class__.__name__.lower() ) self._hi_fallback = hi_fallback - self._unknown_as_english = unknown_as_english + self._unknown_as_fallback = unknown_as_fallback + self._fallback_lang = fallback_lang self._cached_paths = {} self._timeout = int(timeout) @@ -110,7 +112,7 @@ class EmbeddedSubtitlesProvider(Provider): # Default is True container.FFMPEG_STATS = False - tags.LANGUAGE_FALLBACK = "en" if self._unknown_as_english else None + tags.LANGUAGE_FALLBACK = self._fallback_lang if self._unknown_as_fallback and self._fallback_lang else None logger.debug("Language fallback set: %s", tags.LANGUAGE_FALLBACK) def initialize(self): diff --git a/tests/bazarr/test_app_get_providers.py b/tests/bazarr/test_app_get_providers.py index e8f344ef2..af00cb02d 100644 --- a/tests/bazarr/test_app_get_providers.py +++ b/tests/bazarr/test_app_get_providers.py @@ -35,7 +35,8 @@ def test_get_providers_auth_embeddedsubtitles(): assert isinstance(item["ffprobe_path"], str) assert isinstance(item["ffmpeg_path"], str) assert isinstance(item["timeout"], str) - assert isinstance(item["unknown_as_english"], bool) + assert isinstance(item["unknown_as_fallback"], bool) + assert isinstance(item["fallback_lang"], str) def test_get_providers_auth_karagarga(): diff --git a/tests/subliminal_patch/test_embeddedsubtitles.py b/tests/subliminal_patch/test_embeddedsubtitles.py index 47e322287..d7f157de1 100644 --- a/tests/subliminal_patch/test_embeddedsubtitles.py +++ b/tests/subliminal_patch/test_embeddedsubtitles.py @@ -126,8 +126,8 @@ def fake_streams(): @pytest.mark.parametrize("tags_", [{}, {"language": "und", "title": "Unknown"}]) -def test_list_subtitles_unknown_as_english(mocker, tags_, video_single_language): - with EmbeddedSubtitlesProvider(unknown_as_english=True) as provider: +def test_list_subtitles_unknown_as_fallback(mocker, tags_, video_single_language): + with EmbeddedSubtitlesProvider(unknown_as_fallback=True, fallback_lang="en") as provider: fake = FFprobeSubtitleStream( {"index": 3, "codec_name": "subrip", "tags": tags_} ) @@ -141,10 +141,10 @@ def test_list_subtitles_unknown_as_english(mocker, tags_, video_single_language) assert len(result) == 1 -def test_list_subtitles_unknown_as_english_w_real_english_subtitles( +def test_list_subtitles_unknown_as_fallback_w_real_english_subtitles( video_single_language, mocker ): - with EmbeddedSubtitlesProvider(unknown_as_english=True) as provider: + with EmbeddedSubtitlesProvider(unknown_as_fallback=True, fallback_lang="en") as provider: fakes = [ FFprobeSubtitleStream( {"index": 3, "codec_name": "subrip", "tags": {"language": "und"}} @@ -164,8 +164,8 @@ def test_list_subtitles_unknown_as_english_w_real_english_subtitles( @pytest.mark.parametrize("tags_", [{}, {"language": "und", "title": "Unknown"}]) -def test_list_subtitles_unknown_as_english_disabled(tags_): - with EmbeddedSubtitlesProvider(unknown_as_english=False): +def test_list_subtitles_unknown_as_fallback_disabled(tags_): + with EmbeddedSubtitlesProvider(unknown_as_fallback=False,fallback_lang="en"): with pytest.raises(LanguageNotFound): assert FFprobeSubtitleStream( {"index": 3, "codec_name": "subrip", "tags": tags_}