Subtitle support WIP

This commit is contained in:
jbannon 2022-08-08 06:10:09 +00:00
parent 3e60f7d1da
commit 96c9c4b152
6 changed files with 63 additions and 2 deletions

View file

@ -110,6 +110,7 @@ class OutputOptions(StrictDictValidator):
_required_keys = {"output_directory", "file_name"} _required_keys = {"output_directory", "file_name"}
_optional_keys = { _optional_keys = {
"thumbnail_name", "thumbnail_name",
"subtitles_name",
"maintain_download_archive", "maintain_download_archive",
"keep_files_before", "keep_files_before",
"keep_files_after", "keep_files_after",
@ -131,6 +132,9 @@ class OutputOptions(StrictDictValidator):
self._thumbnail_name = self._validate_key_if_present( self._thumbnail_name = self._validate_key_if_present(
key="thumbnail_name", validator=StringFormatterValidator key="thumbnail_name", validator=StringFormatterValidator
) )
self._subtitles_name = self._validate_key_if_present(
key="subtitles_name", validator=StringFormatterValidator
)
self._maintain_download_archive = self._validate_key_if_present( self._maintain_download_archive = self._validate_key_if_present(
key="maintain_download_archive", validator=BoolValidator, default=False key="maintain_download_archive", validator=BoolValidator, default=False
@ -174,6 +178,15 @@ class OutputOptions(StrictDictValidator):
""" """
return self._thumbnail_name return self._thumbnail_name
@property
def subtitles_name(self) -> Optional[StringFormatterValidator]:
"""
Optional. The file name for the media's subtitles if they are present. This can include
directories such as ``"Season {upload_year}/{title}.{subtitle_ext}"``, and will be placed
in the output directory.
"""
return self._subtitles_name
@property @property
def maintain_download_archive(self) -> bool: def maintain_download_archive(self) -> bool:
""" """

View file

@ -319,6 +319,7 @@ class Downloader(DownloadArchiver, Generic[DownloaderOptionsT, DownloaderEntryT]
if only_info_json: if only_info_json:
extract_info_ytdl_options["skip_download"] = True extract_info_ytdl_options["skip_download"] = True
extract_info_ytdl_options["writethumbnail"] = False extract_info_ytdl_options["writethumbnail"] = False
extract_info_ytdl_options["writesubtitles"] = False
ytdl_options_overrides = dict(ytdl_options_overrides, **extract_info_ytdl_options) ytdl_options_overrides = dict(ytdl_options_overrides, **extract_info_ytdl_options)

View file

@ -58,6 +58,18 @@ class Entry(EntryVariables, BaseEntry):
return None return None
def get_download_subtitles_name(self) -> str:
"""
Returns
-------
The download subtitle's file name
"""
return f"{self.uid}.{self.subtitles_ext}"
def get_download_subtitles_path(self) -> str:
"""Returns the entry's thumbnail's file path to where it was downloaded"""
return str(Path(self.working_directory()) / self.get_download_subtitles_name())
@final @final
def is_downloaded(self) -> bool: def is_downloaded(self) -> bool:
""" """

View file

@ -112,6 +112,17 @@ class EntryVariables(SourceVariables):
""" """
return "jpg" return "jpg"
@property
def subtitles_ext(self: BaseEntry) -> str:
"""
Returns
-------
str
The download entry's thumbnail extension. Will always return 'str'. Until there is a
need to support other subtitle types, we always use srt.
"""
return "srt"
@property @property
def upload_date(self: BaseEntry) -> str: def upload_date(self: BaseEntry) -> str:
""" """

View file

@ -178,6 +178,7 @@ class Subscription:
entry=entry, entry=entry,
) )
# TODO: see if entry even has a thumbnail
if self.output_options.thumbnail_name: if self.output_options.thumbnail_name:
output_thumbnail_name = self.overrides.apply_formatter( output_thumbnail_name = self.overrides.apply_formatter(
formatter=self.output_options.thumbnail_name, entry=entry formatter=self.output_options.thumbnail_name, entry=entry
@ -193,6 +194,18 @@ class Subscription:
entry=entry, entry=entry,
) )
# TODO: see if entry even has subtitles
if self.output_options.subtitles_name:
output_subtitles_name = self.overrides.apply_formatter(
formatter=self.output_options.subtitles_name, entry=entry
)
self._enhanced_download_archive.save_file_to_output_directory(
file_name=entry.get_download_subtitles_name(),
output_file_name=output_subtitles_name,
entry=entry,
)
@contextlib.contextmanager @contextlib.contextmanager
def _prepare_working_directory(self): def _prepare_working_directory(self):
""" """
@ -266,10 +279,21 @@ class Subscription:
# TODO: Move this logic to separate function # TODO: Move this logic to separate function
# TODO: set id here as well # TODO: set id here as well
ytdl_options = copy.deepcopy(self.ytdl_options.dict) ytdl_options = copy.deepcopy(self.ytdl_options.dict)
if self.output_options.thumbnail_name:
ytdl_options["writethumbnail"] = True ytdl_options["writethumbnail"] = True
if self.output_options.subtitles_name:
ytdl_options["writesubtitles"] = True
ytdl_options["subtitleslangs"] = ["en"]
ytdl_options["postprocessers"] = [
{"key": "FFmpegSubtitlesConvertorPP", "format": "srt"}
]
if dry_run: if dry_run:
ytdl_options["skip_download"] = True ytdl_options["skip_download"] = True
ytdl_options["writethumbnail"] = False ytdl_options["writethumbnail"] = False
ytdl_options["writesubtitles"] = False
if self.downloader_class.supports_download_archive and self.maintain_download_archive: if self.downloader_class.supports_download_archive and self.maintain_download_archive:
ytdl_options["download_archive"] = str( ytdl_options["download_archive"] = str(
Path(self.working_directory) / self._enhanced_download_archive.archive_file_name Path(self.working_directory) / self._enhanced_download_archive.archive_file_name

View file

@ -74,7 +74,7 @@ class TestYoutubeVideo:
preset_dict=single_video_preset_dict, preset_dict=single_video_preset_dict,
) )
transaction_log = single_video_subscription.download() transaction_log = single_video_subscription.download(dry_run=dry_run)
assert_transaction_log_matches( assert_transaction_log_matches(
output_directory=output_directory, output_directory=output_directory,
transaction_log=transaction_log, transaction_log=transaction_log,