1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2025-01-03 13:35:18 +00:00

EmbeddedSubtitles provider: improve cache management (Fix #2241)

This commit is contained in:
Vitiko 2023-09-14 15:58:18 -04:00
parent ceb947dac1
commit 906c2e9cb9
3 changed files with 18 additions and 3 deletions

View file

@ -4,4 +4,4 @@
from .container import FFprobeVideoContainer from .container import FFprobeVideoContainer
from .stream import FFprobeSubtitleStream from .stream import FFprobeSubtitleStream
__version__ = "0.2.8" __version__ = "0.2.9"

View file

@ -81,6 +81,7 @@ class FFprobeVideoContainer:
overwrite=True, overwrite=True,
timeout=600, timeout=600,
convert_format=None, convert_format=None,
basename_callback=None,
): ):
"""Extracts a list of subtitles converting them. Returns a dictionary of the """Extracts a list of subtitles converting them. Returns a dictionary of the
extracted filenames by index. extracted filenames by index.
@ -95,6 +96,8 @@ class FFprobeVideoContainer:
:param timeout: subprocess timeout in seconds (default: 600) :param timeout: subprocess timeout in seconds (default: 600)
:param convert_format: format to convert selected subtitles. Defaults to :param convert_format: format to convert selected subtitles. Defaults to
srt srt
:param basename_callback: a callback that takes the filename path. Only used if
custom_dir is set. Defaults to `os.path.basename`
:raises: ExtractionError, UnsupportedCodec, OSError :raises: ExtractionError, UnsupportedCodec, OSError
""" """
extract_command = [FFMPEG_PATH, "-v", FF_LOG_LEVEL] extract_command = [FFMPEG_PATH, "-v", FF_LOG_LEVEL]
@ -116,7 +119,8 @@ class FFprobeVideoContainer:
f"{os.path.splitext(self.path)[0]}.{subtitle.suffix}.{extension_to_use}" f"{os.path.splitext(self.path)[0]}.{subtitle.suffix}.{extension_to_use}"
) )
if custom_dir is not None: if custom_dir is not None:
sub_path = os.path.join(custom_dir, os.path.basename(sub_path)) basename_callback = basename_callback or os.path.basename
sub_path = os.path.join(custom_dir, basename_callback(sub_path))
if not overwrite and sub_path in collected_paths: if not overwrite and sub_path in collected_paths:
sub_path = f"{os.path.splitext(sub_path)[0]}.{len(collected_paths):02}.{extension_to_use}" sub_path = f"{os.path.splitext(sub_path)[0]}.{len(collected_paths):02}.{extension_to_use}"
@ -156,6 +160,7 @@ class FFprobeVideoContainer:
overwrite=True, overwrite=True,
timeout=600, timeout=600,
fallback_to_convert=True, fallback_to_convert=True,
basename_callback=None,
): ):
"""Extracts a list of subtitles with ffmpeg's copy method. Returns a dictionary """Extracts a list of subtitles with ffmpeg's copy method. Returns a dictionary
of the extracted filenames by index. of the extracted filenames by index.
@ -167,6 +172,8 @@ class FFprobeVideoContainer:
:param timeout: subprocess timeout in seconds (default: 600) :param timeout: subprocess timeout in seconds (default: 600)
:param fallback_to_convert: fallback to stream's default convert format if it is :param fallback_to_convert: fallback to stream's default convert format if it is
incompatible with copy incompatible with copy
:param basename_callback: a callback that takes the filename path. Only used if
custom_dir is set. Defaults to `os.path.basename`
:raises: ExtractionError, UnsupportedCodec, OSError :raises: ExtractionError, UnsupportedCodec, OSError
""" """
extract_command = [FFMPEG_PATH, "-v", FF_LOG_LEVEL] extract_command = [FFMPEG_PATH, "-v", FF_LOG_LEVEL]
@ -184,7 +191,8 @@ class FFprobeVideoContainer:
for subtitle in subtitles: for subtitle in subtitles:
sub_path = f"{os.path.splitext(self.path)[0]}.{subtitle.suffix}.{subtitle.extension}" sub_path = f"{os.path.splitext(self.path)[0]}.{subtitle.suffix}.{subtitle.extension}"
if custom_dir is not None: if custom_dir is not None:
sub_path = os.path.join(custom_dir, os.path.basename(sub_path)) basename_callback = basename_callback or os.path.basename
sub_path = os.path.join(custom_dir, basename_callback(sub_path))
if not overwrite and sub_path in collected_paths: if not overwrite and sub_path in collected_paths:
sub_path = f"{os.path.splitext(sub_path)[0]}.{len(collected_paths):02}.{subtitle.extension}" sub_path = f"{os.path.splitext(sub_path)[0]}.{len(collected_paths):02}.{subtitle.extension}"

View file

@ -2,6 +2,7 @@
import functools import functools
import logging import logging
import hashlib
import os import os
import re import re
import shutil import shutil
@ -214,6 +215,7 @@ class EmbeddedSubtitlesProvider(Provider):
self._cache_dir, self._cache_dir,
timeout=self._timeout, timeout=self._timeout,
fallback_to_convert=True, fallback_to_convert=True,
basename_callback=_basename_callback,
) )
# Add the extracted paths to the containter path key # Add the extracted paths to the containter path key
self._cached_paths[container.path] = extracted self._cached_paths[container.path] = extracted
@ -345,6 +347,11 @@ def _get_pretty_release_name(stream, container):
return f"{os.path.splitext(bname)[0]}.{stream.suffix}" return f"{os.path.splitext(bname)[0]}.{stream.suffix}"
def _basename_callback(path: str):
path, ext = os.path.splitext(path)
return hashlib.md5(path.encode()).hexdigest() + ext
# TODO: improve this # TODO: improve this
_SIGNS_LINE_RE = re.compile(r",([\w|_]{,15}(sign|fx|karaoke))", flags=re.IGNORECASE) _SIGNS_LINE_RE = re.compile(r",([\w|_]{,15}(sign|fx|karaoke))", flags=re.IGNORECASE)