Embedded Subtitles provider: add support for unknown language tags

pull/1912/head v1.1.1-beta.6
vitiko98 2 years ago
parent 27d792041c
commit 5624ae431e

@ -206,6 +206,7 @@ defaults = {
'included_codecs': '[]',
'hi_fallback': 'False',
'timeout': '600',
'unknown_as_english': 'False',
},
'karagarga': {
'username': '',

@ -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,

@ -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.",

@ -4,4 +4,4 @@
from .container import FFprobeVideoContainer
from .stream import FFprobeSubtitleStream
__version__ = "0.2"
__version__ = "0.2.2"

@ -8,12 +8,21 @@ from .exceptions import LanguageNotFound
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

@ -79,9 +79,7 @@ class EmbeddedSubtitlesProvider(Provider):
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)
@ -93,6 +91,7 @@ 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._cached_paths = {}
self._timeout = int(timeout)
@ -105,6 +104,9 @@ class EmbeddedSubtitlesProvider(Provider):
# 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)

@ -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():

@ -4,6 +4,7 @@ import os
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
@ -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"]],
)

Loading…
Cancel
Save