EmbeddedSubtitles provider: improve cache management (Fix #2241)

pull/2264/head v1.2.5-beta.26
Vitiko 8 months ago
parent ceb947dac1
commit 906c2e9cb9

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

@ -81,6 +81,7 @@ class FFprobeVideoContainer:
overwrite=True,
timeout=600,
convert_format=None,
basename_callback=None,
):
"""Extracts a list of subtitles converting them. Returns a dictionary of the
extracted filenames by index.
@ -95,6 +96,8 @@ class FFprobeVideoContainer:
:param timeout: subprocess timeout in seconds (default: 600)
:param convert_format: format to convert selected subtitles. Defaults to
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
"""
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}"
)
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:
sub_path = f"{os.path.splitext(sub_path)[0]}.{len(collected_paths):02}.{extension_to_use}"
@ -156,6 +160,7 @@ class FFprobeVideoContainer:
overwrite=True,
timeout=600,
fallback_to_convert=True,
basename_callback=None,
):
"""Extracts a list of subtitles with ffmpeg's copy method. Returns a dictionary
of the extracted filenames by index.
@ -167,6 +172,8 @@ class FFprobeVideoContainer:
:param timeout: subprocess timeout in seconds (default: 600)
:param fallback_to_convert: fallback to stream's default convert format if it is
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
"""
extract_command = [FFMPEG_PATH, "-v", FF_LOG_LEVEL]
@ -184,7 +191,8 @@ class FFprobeVideoContainer:
for subtitle in subtitles:
sub_path = f"{os.path.splitext(self.path)[0]}.{subtitle.suffix}.{subtitle.extension}"
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:
sub_path = f"{os.path.splitext(sub_path)[0]}.{len(collected_paths):02}.{subtitle.extension}"

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

Loading…
Cancel
Save