Skip to content

Commit

Permalink
Updated fallback language for embeddedsubtitles provider to be able t…
Browse files Browse the repository at this point in the history
…o specify the one to use
  • Loading branch information
boydaihungst authored Feb 20, 2024
1 parent 5dd0013 commit ad8f116
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
9 changes: 8 additions & 1 deletion bazarr/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ class Validator(OriginalValidator):
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),
Expand Down Expand Up @@ -448,6 +449,12 @@ def write_config():
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()

Expand Down
3 changes: 2 additions & 1 deletion bazarr/app/get_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/pages/Settings/Providers/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,14 @@ export const ProviderList: Readonly<ProviderInfo[]> = [
},
{
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:
Expand Down
8 changes: 5 additions & 3 deletions libs/subliminal_patch/providers/embeddedsubtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def __init__(
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)

Expand All @@ -97,7 +98,8 @@ def __init__(
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)

Expand All @@ -110,7 +112,7 @@ def __init__(
# 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):
Expand Down
3 changes: 2 additions & 1 deletion tests/bazarr/test_app_get_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
12 changes: 6 additions & 6 deletions tests/subliminal_patch/test_embeddedsubtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_}
)
Expand All @@ -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"}}
Expand All @@ -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_}
Expand Down

0 comments on commit ad8f116

Please sign in to comment.