subtitle fix for playlists

This commit is contained in:
jbannon 2022-08-13 22:18:45 +00:00
parent 9a7a34f3d0
commit 0548730e06
4 changed files with 35 additions and 15 deletions

View file

@ -132,7 +132,7 @@ class Downloader(DownloadArchiver, Generic[DownloaderOptionsT, DownloaderEntryT]
def extract_info_with_retry( def extract_info_with_retry(
self, self,
is_downloaded_fn: Callable[[], bool], is_downloaded_fn: Optional[Callable[[], bool]],
ytdl_options_overrides: Optional[Dict] = None, ytdl_options_overrides: Optional[Dict] = None,
**kwargs, **kwargs,
) -> Dict: ) -> Dict:
@ -146,7 +146,7 @@ class Downloader(DownloadArchiver, Generic[DownloaderOptionsT, DownloaderEntryT]
Parameters Parameters
---------- ----------
is_downloaded_fn is_downloaded_fn
Function to check if the entry is downloaded Optional. Function to check if the entry is downloaded
ytdl_options_overrides ytdl_options_overrides
Optional. Dict containing ytdl args to override other predefined ytdl args Optional. Dict containing ytdl args to override other predefined ytdl args
**kwargs **kwargs
@ -162,7 +162,7 @@ class Downloader(DownloadArchiver, Generic[DownloaderOptionsT, DownloaderEntryT]
while not entry_files_exist and num_tries < self._extract_entry_num_retries: while not entry_files_exist and num_tries < self._extract_entry_num_retries:
entry_dict = self.extract_info(ytdl_options_overrides=ytdl_options_overrides, **kwargs) entry_dict = self.extract_info(ytdl_options_overrides=ytdl_options_overrides, **kwargs)
if is_downloaded_fn(): if is_downloaded_fn is None or is_downloaded_fn():
return entry_dict return entry_dict
time.sleep(self._extract_entry_retry_wait_sec) time.sleep(self._extract_entry_retry_wait_sec)
@ -298,7 +298,6 @@ class Downloader(DownloadArchiver, Generic[DownloaderOptionsT, DownloaderEntryT]
{ {
"skip_download": True, "skip_download": True,
"writethumbnail": False, "writethumbnail": False,
"writesubtitles": False,
} }
) )

View file

@ -89,15 +89,17 @@ class YoutubePlaylistDownloader(
) )
download_logger.info("Downloading %d/%d %s", idx, len(entry_dicts), video.title) download_logger.info("Downloading %d/%d %s", idx, len(entry_dicts), video.title)
# Only do the individual download if it is not dry-run and downloading individually # Re-download the contents even if it's a dry-run as a single video. At this time,
if not self.is_dry_run: # playlists do not download subtitles or subtitle metadata
_ = self.extract_info_with_retry( as_single_video_dict = self.extract_info_with_retry(
is_downloaded_fn=video.is_downloaded, is_downloaded_fn=None if self.is_dry_run else video.is_downloaded,
ytdl_options_overrides={ ytdl_options_overrides={"writeinfojson": False, "skip_download": self.is_dry_run},
"playlist_items": str(entry_dict.get("playlist_index")), url=video.kwargs("webpage_url"),
"writeinfojson": False,
},
url=self.download_options.playlist_url,
) )
# Workaround for the ytdlp issue
# pylint: disable=protected-access
video._kwargs["requested_subtitles"] = as_single_video_dict.get("requested_subtitles")
# pylint: enable=protected-access
yield video yield video

View file

@ -9,6 +9,7 @@ from ytdl_sub.entries.entry import Entry
from ytdl_sub.plugins.plugin import Plugin from ytdl_sub.plugins.plugin import Plugin
from ytdl_sub.plugins.plugin import PluginOptions from ytdl_sub.plugins.plugin import PluginOptions
from ytdl_sub.utils.file_handler import FileMetadata from ytdl_sub.utils.file_handler import FileMetadata
from ytdl_sub.utils.logger import Logger
from ytdl_sub.validators.string_formatter_validators import StringFormatterValidator from ytdl_sub.validators.string_formatter_validators import StringFormatterValidator
from ytdl_sub.validators.string_select_validator import StringSelectValidator from ytdl_sub.validators.string_select_validator import StringSelectValidator
from ytdl_sub.validators.validators import BoolValidator from ytdl_sub.validators.validators import BoolValidator
@ -17,6 +18,9 @@ from ytdl_sub.validators.validators import StringListValidator
SUBTITLE_EXTENSIONS: Set[str] = {"srt", "vtt", "ass", "lrc"} SUBTITLE_EXTENSIONS: Set[str] = {"srt", "vtt", "ass", "lrc"}
logger = Logger.get(name="subtitles")
def _is_entry_subtitle_file(path: Path, entry: Entry) -> bool: def _is_entry_subtitle_file(path: Path, entry: Entry) -> bool:
if path.is_file() and path.name.startswith(entry.uid): if path.is_file() and path.name.startswith(entry.uid):
for ext in SUBTITLE_EXTENSIONS: for ext in SUBTITLE_EXTENSIONS:
@ -67,7 +71,9 @@ class SubtitleOptions(PluginOptions):
key="subtitles_type", validator=SubtitlesTypeValidator, default="srt" key="subtitles_type", validator=SubtitlesTypeValidator, default="srt"
).value ).value
self._embed_subtitles = self._validate_key_if_present( self._embed_subtitles = self._validate_key_if_present(
key="embed_subtitles", validator=BoolValidator key="embed_subtitles",
validator=BoolValidator,
default=False,
).value ).value
self._languages = self._validate_key_if_present( self._languages = self._validate_key_if_present(
key="languages", validator=StringListValidator, default=["en"] key="languages", validator=StringListValidator, default=["en"]
@ -166,6 +172,9 @@ class SubtitlesPlugin(Plugin[SubtitleOptions]):
def modify_entry(self, entry: Entry) -> Optional[Entry]: def modify_entry(self, entry: Entry) -> Optional[Entry]:
requested_subtitles = entry.kwargs("requested_subtitles") requested_subtitles = entry.kwargs("requested_subtitles")
if not requested_subtitles:
return entry
languages = sorted(requested_subtitles.keys()) languages = sorted(requested_subtitles.keys())
entry.add_variables( entry.add_variables(
variables_to_add={ variables_to_add={
@ -186,6 +195,10 @@ class SubtitlesPlugin(Plugin[SubtitleOptions]):
Entry to create subtitles for Entry to create subtitles for
""" """
requested_subtitles = entry.kwargs("requested_subtitles") requested_subtitles = entry.kwargs("requested_subtitles")
if not requested_subtitles:
logger.info("subtitles not found for %s", entry.title)
return None
file_metadata: Optional[FileMetadata] = None file_metadata: Optional[FileMetadata] = None
langs = list(requested_subtitles.keys()) langs = list(requested_subtitles.keys())

View file

@ -19,6 +19,10 @@ def playlist_preset_dict(output_directory):
"ytdl_options": { "ytdl_options": {
"format": "worst[ext=mp4]", "format": "worst[ext=mp4]",
}, },
"subtitles": {
"subtitles_name": "{music_video_name}.{lang}.{subtitles_ext}",
"allow_auto_generated_subtitles": True,
},
"overrides": {"artist": "JMC"}, "overrides": {"artist": "JMC"},
} }
@ -94,11 +98,13 @@ class TestPlaylistAsKodiMusicVideo:
output_directory=output_directory, output_directory=output_directory,
transaction_log=transaction_log, transaction_log=transaction_log,
transaction_log_summary_file_name="youtube/test_playlist.txt", transaction_log_summary_file_name="youtube/test_playlist.txt",
regenerate_transaction_log=True,
) )
assert_expected_downloads( assert_expected_downloads(
output_directory=output_directory, output_directory=output_directory,
dry_run=dry_run, dry_run=dry_run,
expected_download_summary_file_name="youtube/test_playlist.json", expected_download_summary_file_name="youtube/test_playlist.json",
regenerate_expected_download_summary=True,
) )
if not dry_run: if not dry_run: