diff --git a/bazarr/database.py b/bazarr/database.py index 992189e84..9b3dd8ccc 100644 --- a/bazarr/database.py +++ b/bazarr/database.py @@ -108,6 +108,7 @@ def db_upgrade(): ['table_episodes', 'audio_codec', 'text'], ['table_episodes', 'episode_file_id', 'integer'], ['table_episodes', 'audio_language', 'text'], + ['table_episodes', 'file_size', 'integer', '0'], ['table_movies', 'sortTitle', 'text'], ['table_movies', 'year', 'text'], ['table_movies', 'alternativeTitles', 'text'], @@ -120,6 +121,7 @@ def db_upgrade(): ['table_movies', 'movie_file_id', 'integer'], ['table_movies', 'tags', 'text', '[]'], ['table_movies', 'profileId', 'integer'], + ['table_movies', 'file_size', 'integer', '0'], ['table_history', 'video_path', 'text'], ['table_history', 'language', 'text'], ['table_history', 'provider', 'text'], diff --git a/bazarr/embedded_subs_reader.py b/bazarr/embedded_subs_reader.py index fda402d4a..767445b11 100644 --- a/bazarr/embedded_subs_reader.py +++ b/bazarr/embedded_subs_reader.py @@ -4,14 +4,20 @@ import enzyme from enzyme.exceptions import MalformedMKVError import logging import os +import datetime from knowit import api +from subliminal.cache import region + +FFPROBE_CACHE_EXPIRATION_TIME = datetime.timedelta(weeks=2).total_seconds() class EmbeddedSubsReader: def __init__(self): self.ffprobe = None - - def list_languages(self, file): + + @region.cache_on_arguments(expiration_time=FFPROBE_CACHE_EXPIRATION_TIME) + # file_size, episode_file_id and movie_file_id are used for cache identification. DO NOT REMOVE! + def list_languages(self, file, file_size, episode_file_id=None, movie_file_id=None): from utils import get_binary self.ffprobe = get_binary("ffprobe") diff --git a/bazarr/get_episodes.py b/bazarr/get_episodes.py index 0840715a1..582d0e32d 100644 --- a/bazarr/get_episodes.py +++ b/bazarr/get_episodes.py @@ -117,7 +117,8 @@ def sync_episodes(): 'video_codec': videoCodec, 'audio_codec': audioCodec, 'episode_file_id': episode['episodeFile']['id'], - 'audio_language': str(audio_language)}) + 'audio_language': str(audio_language), + 'file_size': episode['episodeFile']['size']}) else: episodes_to_add.append({'sonarrSeriesId': episode['seriesId'], 'sonarrEpisodeId': episode['id'], @@ -132,7 +133,8 @@ def sync_episodes(): 'video_codec': videoCodec, 'audio_codec': audioCodec, 'episode_file_id': episode['episodeFile']['id'], - 'audio_language': str(audio_language)}) + 'audio_language': str(audio_language), + 'file_size': episode['episodeFile']['size']}) # Remove old episodes from DB removed_episodes = list(set(current_episodes_db_list) - set(current_episodes_sonarr)) @@ -148,7 +150,7 @@ def sync_episodes(): episode_in_db_list = [] episodes_in_db = database.execute("SELECT sonarrSeriesId, sonarrEpisodeId, title, path, season, episode, " "scene_name, monitored, format, resolution, video_codec, audio_codec, " - "episode_file_id, audio_language FROM table_episodes") + "episode_file_id, audio_language, file_size FROM table_episodes") for item in episodes_in_db: episode_in_db_list.append(item) diff --git a/bazarr/get_movies.py b/bazarr/get_movies.py index 9ab7fd7d0..8441ac7e4 100644 --- a/bazarr/get_movies.py +++ b/bazarr/get_movies.py @@ -188,7 +188,8 @@ def update_movies(): 'overview': overview, 'imdbId': imdbId, 'movie_file_id': int(movie['movieFile']['id']), - 'tags': str(tags)}) + 'tags': str(tags), + 'file_size': movie['movieFile']['size']}) else: movies_to_add.append({'radarrId': int(movie["id"]), 'title': movie["title"], @@ -211,7 +212,8 @@ def update_movies(): 'imdbId': imdbId, 'movie_file_id': int(movie['movieFile']['id']), 'tags': str(tags), - 'profileId': movie_default_profile}) + 'profileId': movie_default_profile, + 'file_size': movie['movieFile']['size']}) else: logging.error( 'BAZARR Radarr returned a movie without a file path: ' + movie["path"] + separator + @@ -228,7 +230,7 @@ def update_movies(): movies_in_db = database.execute("SELECT radarrId, title, path, tmdbId, overview, poster, fanart, " "audio_language, sceneName, monitored, sortTitle, year, " "alternativeTitles, format, resolution, video_codec, audio_codec, imdbId," - "movie_file_id, tags FROM table_movies") + "movie_file_id, tags, file_size FROM table_movies") for item in movies_in_db: movies_in_db_list.append(item) diff --git a/bazarr/list_subtitles.py b/bazarr/list_subtitles.py index ff0793173..bdc9bebe7 100644 --- a/bazarr/list_subtitles.py +++ b/bazarr/list_subtitles.py @@ -5,6 +5,7 @@ import os import logging import ast import re +import subliminal from guess_language import guess_language from subliminal_patch import core, search_external_subtitles from subzero.language import Language @@ -31,7 +32,13 @@ def store_subtitles(original_path, reversed_path): if settings.general.getboolean('use_embedded_subs'): logging.debug("BAZARR is trying to index embedded subtitles.") try: - subtitle_languages = embedded_subs_reader.list_languages(reversed_path) + item = database.execute('SELECT file_size, episode_file_id FROM table_episodes ' + 'WHERE path = ?', (original_path,), only_one=True) + subtitle_languages = embedded_subs_reader.list_languages(reversed_path, + file_size=item['file_size'], + episode_file_id=item['episode_file_id']) + subliminal.region.backend.sync() + for subtitle_language, subtitle_forced, subtitle_hi, subtitle_codec in subtitle_languages: try: if (settings.general.getboolean("ignore_pgs_subs") and subtitle_codec.lower() == "pgs") or \ @@ -145,7 +152,13 @@ def store_subtitles_movie(original_path, reversed_path): if settings.general.getboolean('use_embedded_subs'): logging.debug("BAZARR is trying to index embedded subtitles.") try: - subtitle_languages = embedded_subs_reader.list_languages(reversed_path) + item = database.execute('SELECT file_size, movie_file_id FROM table_movies ' + 'WHERE path = ?', (original_path,), only_one=True) + subtitle_languages = embedded_subs_reader.list_languages(reversed_path, + file_size=item['file_size'], + movie_file_id=item['movie_file_id']) + subliminal.region.backend.sync() + for subtitle_language, subtitle_forced, subtitle_hi, subtitle_codec in subtitle_languages: try: if (settings.general.getboolean("ignore_pgs_subs") and subtitle_codec.lower() == "pgs") or \