Skip to content

Commit

Permalink
Embedded Subtitles provider: add support for unknown language tags
Browse files Browse the repository at this point in the history
  • Loading branch information
vitiko98 committed Jul 20, 2022
1 parent 27d7920 commit 5624ae4
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions bazarr/app/config.py
Expand Up @@ -206,6 +206,7 @@ def get(self, section, option, raw=False, vars=None):
'included_codecs': '[]',
'hi_fallback': 'False',
'timeout': '600',
'unknown_as_english': 'False',
},
'karagarga': {
'username': '',
Expand Down
1 change: 1 addition & 0 deletions bazarr/app/get_providers.py
Expand Up @@ -232,6 +232,7 @@ def get_providers_auth():
'ffprobe_path': _FFPROBE_BINARY,
'ffmpeg_path': _FFMPEG_BINARY,
'timeout': settings.embeddedsubtitles.timeout,
'unknown_as_english': settings.embeddedsubtitles.getboolean('unknown_as_english'),
},
'karagarga': {
'username': settings.karagarga.username,
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/pages/Settings/Providers/list.ts
Expand Up @@ -103,6 +103,11 @@ export const ProviderList: Readonly<ProviderInfo[]> = [
key: "hi_fallback",
name: "Use HI subtitles as a fallback (don't enable it if you have a HI language profile)",
},
{
type: "switch",
key: "unknown_as_english",
name: "Use subtitles with unknown info/language as english",
},
],
message:
"Warning for cloud users: this provider needs to read the entire file in order to extract subtitles.",
Expand Down
2 changes: 1 addition & 1 deletion libs/fese/__init__.py
Expand Up @@ -4,4 +4,4 @@
from .container import FFprobeVideoContainer
from .stream import FFprobeSubtitleStream

__version__ = "0.2"
__version__ = "0.2.2"
11 changes: 10 additions & 1 deletion libs/fese/tags.py
Expand Up @@ -8,12 +8,21 @@

logger = logging.getLogger(__name__)

LANGUAGE_FALLBACK = None


class FFprobeGenericSubtitleTags:
_DETECTABLE_TAGS = None

def __init__(self, data: dict):
self.language = _get_language(data)
try:
self.language = _get_language(data)
except LanguageNotFound:
if LANGUAGE_FALLBACK is not None:
self.language = Language.fromietf(LANGUAGE_FALLBACK)
else:
raise

self._data = data

@classmethod
Expand Down
8 changes: 5 additions & 3 deletions libs/subliminal_patch/providers/embeddedsubtitles.py
Expand Up @@ -79,9 +79,7 @@ def __init__(
ffmpeg_path=None,
hi_fallback=False,
timeout=600,
include_ass=None,
include_srt=None,
mergerfs_mode=None,
unknown_as_english=False,
):
self._included_codecs = set(included_codecs or _ALLOWED_CODECS)

Expand All @@ -93,6 +91,7 @@ def __init__(
cache_dir or tempfile.gettempdir(), self.__class__.__name__.lower()
)
self._hi_fallback = hi_fallback
self._unknown_as_english = unknown_as_english
self._cached_paths = {}
self._timeout = int(timeout)

Expand All @@ -105,6 +104,9 @@ def __init__(
# Default is True
container.FFMPEG_STATS = False

tags.LANGUAGE_FALLBACK = "en" if self._unknown_as_english else None
logger.debug("Language fallback set: %s", tags.LANGUAGE_FALLBACK)

def initialize(self):
os.makedirs(self._cache_dir, exist_ok=True)

Expand Down
1 change: 1 addition & 0 deletions tests/bazarr/app/test_get_providers.py
Expand Up @@ -34,6 +34,7 @@ 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)


def test_get_providers_auth_karagarga():
Expand Down
26 changes: 25 additions & 1 deletion tests/subliminal_patch/test_embeddedsubtitles.py
Expand Up @@ -4,6 +4,7 @@
from fese import FFprobeSubtitleStream
from fese import FFprobeVideoContainer
from fese import tags
from fese.exceptions import LanguageNotFound
import pytest
from subliminal_patch.core import Episode
from subliminal_patch.core import Movie
Expand Down Expand Up @@ -123,13 +124,36 @@ def fake_streams():
}


@pytest.mark.parametrize("tags_", [{}, {"language": "und", "title": "Unknown"}])
def test_list_subtitles_unknown_as_english(mocker, tags_):
with EmbeddedSubtitlesProvider(unknown_as_english=True):
fake = FFprobeSubtitleStream(
{"index": 3, "codec_name": "subrip", "tags": tags_}
)
mocker.patch(
"subliminal_patch.providers.embeddedsubtitles._MemoizedFFprobeVideoContainer.get_subtitles",
return_value=[fake],
)
streams = _MemoizedFFprobeVideoContainer.get_subtitles("")
assert len(streams) == 1
assert streams[0].language == Language.fromietf("en")


@pytest.mark.parametrize("tags_", [{}, {"language": "und", "title": "Unknown"}])
def test_list_subtitles_unknown_as_english_disabled(tags_):
with EmbeddedSubtitlesProvider(unknown_as_english=False):
with pytest.raises(LanguageNotFound):
assert FFprobeSubtitleStream(
{"index": 3, "codec_name": "subrip", "tags": tags_}
)


def test_list_subtitles_hi_fallback_one_stream(
video_single_language, fake_streams, mocker
):
with EmbeddedSubtitlesProvider(hi_fallback=True) as provider:
language = Language.fromalpha2("en")
mocker.patch(
# "fese.FFprobeVideoContainer.get_subtitles",
"subliminal_patch.providers.embeddedsubtitles._MemoizedFFprobeVideoContainer.get_subtitles",
return_value=[fake_streams["en_hi"]],
)
Expand Down

0 comments on commit 5624ae4

Please sign in to comment.