diff options
author | Vitiko <[email protected]> | 2022-05-05 00:02:34 -0400 |
---|---|---|
committer | Vitiko <[email protected]> | 2022-05-05 00:04:37 -0400 |
commit | 134613711ac1ede510cfc023072f0712a175b858 (patch) | |
tree | ad7446c8521787948cb472f5beac35622f51b19d | |
parent | e186bd1a0f754738873eec995271522dc1df2263 (diff) | |
download | bazarr-134613711ac1ede510cfc023072f0712a175b858.tar.gz bazarr-134613711ac1ede510cfc023072f0712a175b858.zip |
Embedded Subtitles provider: improve detection of potentially incomplete subtitlesv1.0.5-beta.3
-rw-r--r-- | libs/subliminal_patch/providers/embeddedsubtitles.py | 35 | ||||
-rw-r--r-- | tests/subliminal_patch/test_embeddedsubtitles.py | 36 |
2 files changed, 67 insertions, 4 deletions
diff --git a/libs/subliminal_patch/providers/embeddedsubtitles.py b/libs/subliminal_patch/providers/embeddedsubtitles.py index 324438a1c..63e7c8fde 100644 --- a/libs/subliminal_patch/providers/embeddedsubtitles.py +++ b/libs/subliminal_patch/providers/embeddedsubtitles.py @@ -117,6 +117,8 @@ class EmbeddedSubtitlesProvider(Provider): self._blacklist.add(path) streams = [] + streams = _discard_possible_incomplete_subtitles(list(streams)) + if not streams: logger.debug("No subtitles found for container: %s", video) @@ -260,6 +262,39 @@ def _check_hi_fallback(streams, languages): logger.debug("HI fallback not needed: %s", compatible_streams) +def _discard_possible_incomplete_subtitles(streams): + """Check number_of_frames attributes from subtitle streams in order to find + supposedly incomplete subtitles""" + try: + max_frames = max(stream.number_of_frames for stream in streams) + except ValueError: + return [] + + # Blatantly assume there's nothing to discard as some ffprobe streams don't + # have number_of_frames tags + if not max_frames: + return streams + + logger.debug("Checking possible incomplete subtitles (max frames: %d)", max_frames) + + valid_streams = [] + + for stream in streams: + # 500 < 1200 + if stream.number_of_frames < max_frames // 2: + logger.debug( + "Possible bad subtitle found: %s (%s frames - %s frames)", + stream, + stream.number_of_frames, + max_frames, + ) + continue + + valid_streams.append(stream) + + return valid_streams + + def _is_fuse_rclone_mount(path: str): # Experimental! diff --git a/tests/subliminal_patch/test_embeddedsubtitles.py b/tests/subliminal_patch/test_embeddedsubtitles.py index 942c5a56a..0bda12072 100644 --- a/tests/subliminal_patch/test_embeddedsubtitles.py +++ b/tests/subliminal_patch/test_embeddedsubtitles.py @@ -9,10 +9,11 @@ import subliminal_patch from subliminal_patch.core import Episode from subliminal_patch.core import Movie from subliminal_patch.exceptions import MustGetBlacklisted -from subliminal_patch.providers.embeddedsubtitles import \ - _MemoizedFFprobeVideoContainer -from subliminal_patch.providers.embeddedsubtitles import \ - EmbeddedSubtitlesProvider +from subliminal_patch.providers.embeddedsubtitles import _MemoizedFFprobeVideoContainer +from subliminal_patch.providers.embeddedsubtitles import EmbeddedSubtitlesProvider +from subliminal_patch.providers.embeddedsubtitles import ( + _discard_possible_incomplete_subtitles, +) from subzero.language import Language @@ -249,3 +250,30 @@ def test_memoized(video_single_language, mocker): ] is not None ) + + + "number_of_frames,expected_len", + [((34, 811), 1), ((0, 0), 2), ((811, 34), 1), ((900, 1000), 2), ((0, 900), 1)], +) +def test_discard_possible_incomplete_subtitles(number_of_frames, expected_len): + subtitle_1 = FFprobeSubtitleStream( + { + "index": 1, + "codec_name": "subrip", + "codec_long_name": "SubRip subtitle", + "disposition": {}, + "tags": {"language": "eng", "NUMBER_OF_FRAMES": number_of_frames[0]}, + } + ) + subtitle_2 = FFprobeSubtitleStream( + { + "index": 2, + "codec_name": "subrip", + "codec_long_name": "SubRip subtitle", + "disposition": {}, + "tags": {"language": "eng", "NUMBER_OF_FRAMES": number_of_frames[1]}, + } + ) + new_list = _discard_possible_incomplete_subtitles([subtitle_1, subtitle_2]) + assert len(new_list) == expected_len |