From 4045549eb6103f7e595f69350efebfbe68022807 Mon Sep 17 00:00:00 2001 From: jbannon Date: Fri, 12 Aug 2022 07:28:03 +0000 Subject: [PATCH] updated tests and file metadata --- src/ytdl_sub/plugins/subtitles.py | 38 ++++++++------- .../subscription_ytdl_options.py | 25 +++++----- tests/e2e/plugins/test_subtitles.py | 48 +++++++++++++++---- .../plugins/test_subtitles_embedded.json | 5 ++ ... => test_subtitles_embedded_and_file.json} | 2 +- .../plugins/test_subtitles_embedded.txt | 12 +++++ ...t => test_subtitles_embedded_and_file.txt} | 1 + 7 files changed, 92 insertions(+), 39 deletions(-) create mode 100644 tests/e2e/resources/expected_downloads_summaries/plugins/test_subtitles_embedded.json rename tests/e2e/resources/expected_downloads_summaries/plugins/{test_subtitles_video.json => test_subtitles_embedded_and_file.json} (92%) create mode 100644 tests/e2e/resources/transaction_log_summaries/plugins/test_subtitles_embedded.txt rename tests/e2e/resources/transaction_log_summaries/plugins/{test_subtitles_video.txt => test_subtitles_embedded_and_file.txt} (93%) diff --git a/src/ytdl_sub/plugins/subtitles.py b/src/ytdl_sub/plugins/subtitles.py index d4f14fc4..7bfb252e 100644 --- a/src/ytdl_sub/plugins/subtitles.py +++ b/src/ytdl_sub/plugins/subtitles.py @@ -8,6 +8,7 @@ from ytdl_sub.downloaders.ytdl_options_builder import YTDLOptionsBuilder from ytdl_sub.entries.entry import Entry from ytdl_sub.plugins.plugin import Plugin from ytdl_sub.plugins.plugin import PluginOptions +from ytdl_sub.utils.file_handler import FileMetadata from ytdl_sub.validators.string_formatter_validators import StringFormatterValidator from ytdl_sub.validators.string_select_validator import StringSelectValidator from ytdl_sub.validators.validators import BoolValidator @@ -173,7 +174,7 @@ class SubtitlesPlugin(Plugin[SubtitleOptions]): return entry - def post_process_entry(self, entry: Entry) -> None: + def post_process_entry(self, entry: Entry) -> Optional[FileMetadata]: """ Creates an entry's NFO file using values defined in the metadata options @@ -182,20 +183,25 @@ class SubtitlesPlugin(Plugin[SubtitleOptions]): entry: Entry to create subtitles for """ - if not self.plugin_options.subtitles_name: - return - requested_subtitles = entry.kwargs("requested_subtitles") - for lang in requested_subtitles.keys(): - subtitle_file_name = f"{entry.uid}.{lang}.{self.plugin_options.subtitles_type}" - output_subtitle_file_name = self.overrides.apply_formatter( - formatter=self.plugin_options.subtitles_name, - entry=entry, - function_overrides={"lang": lang}, - ) + file_metadata: Optional[FileMetadata] = None + langs = list(requested_subtitles.keys()) - self.save_file( - file_name=subtitle_file_name, - output_file_name=output_subtitle_file_name, - entry=entry, - ) + if self.plugin_options.embed_subtitles: + file_metadata = FileMetadata(f"Embedded subtitles with lang(s) {', '.join(langs)}") + if self.plugin_options.subtitles_name: + for lang in langs: + subtitle_file_name = f"{entry.uid}.{lang}.{self.plugin_options.subtitles_type}" + output_subtitle_file_name = self.overrides.apply_formatter( + formatter=self.plugin_options.subtitles_name, + entry=entry, + function_overrides={"lang": lang}, + ) + + self.save_file( + file_name=subtitle_file_name, + output_file_name=output_subtitle_file_name, + entry=entry, + ) + + return file_metadata diff --git a/src/ytdl_sub/subscriptions/subscription_ytdl_options.py b/src/ytdl_sub/subscriptions/subscription_ytdl_options.py index a7040f11..1e91fc19 100644 --- a/src/ytdl_sub/subscriptions/subscription_ytdl_options.py +++ b/src/ytdl_sub/subscriptions/subscription_ytdl_options.py @@ -90,14 +90,21 @@ class SubscriptionYTDLOptions: # TODO: warn here return {} - builder = YTDLOptionsBuilder() + builder = YTDLOptionsBuilder().add({"writesubtitles": True}) subtitle_options: SubtitleOptions = subtitle_plugin.plugin_options - write_subtitle_file: bool = subtitle_options.subtitles_name is not None - if write_subtitle_file: + if subtitle_options.embed_subtitles: + builder.add( + { + "postprocessors": [ + {"key": "FFmpegEmbedSubtitle", "already_have_subtitle": True} + ] + } + ) + + if subtitle_options.subtitles_name: builder.add( { - "writesubtitles": True, "postprocessors": [ { "key": "FFmpegSubtitlesConvertor", @@ -107,16 +114,6 @@ class SubscriptionYTDLOptions: } ) - if subtitle_options.embed_subtitles: - builder.add( - { - "postprocessors": [ - # already_have_subtitle=True means we downloaded the subtitle files. - {"key": "FFmpegEmbedSubtitle", "already_have_subtitle": write_subtitle_file} - ] - } - ) - # If neither subtitles_name or embed_subtitles is set, do not set any other flags if not builder.to_dict(): return {} diff --git a/tests/e2e/plugins/test_subtitles.py b/tests/e2e/plugins/test_subtitles.py index 563ee63d..948bc068 100644 --- a/tests/e2e/plugins/test_subtitles.py +++ b/tests/e2e/plugins/test_subtitles.py @@ -6,14 +6,13 @@ from ytdl_sub.subscriptions.subscription import Subscription @pytest.fixture -def single_video_subs_preset_dict(output_directory): +def single_video_subs_embed_preset_dict(output_directory): return { "preset": "yt_music_video", "youtube": {"video_url": "https://www.youtube.com/watch?v=2lAe1cqCOXo"}, # override the output directory with our fixture-generated dir "output_options": {"output_directory": output_directory}, "subtitles": { - "subtitles_name": "{music_video_name}.{lang}.{subtitles_ext}", "embed_subtitles": True, "languages": ["en", "de"], "allow_auto_generated_subtitles": True, @@ -25,30 +24,63 @@ def single_video_subs_preset_dict(output_directory): "overrides": {"artist": "JMC"}, } +@pytest.fixture +def test_single_video_subs_embed_and_file_preset_dict(single_video_subs_embed_preset_dict): + single_video_subs_embed_preset_dict['subtitles']['subtitles_name'] = ( + "{music_video_name}.{lang}.{subtitles_ext}" + ) + return single_video_subs_embed_preset_dict + class TestSubtitles: @pytest.mark.parametrize("dry_run", [True, False]) - def test_single_video_download_with_subtitles( + def test_subtitles_embedded( self, music_video_config, - single_video_subs_preset_dict, + single_video_subs_embed_preset_dict, output_directory, dry_run, ): subscription = Subscription.from_dict( config=music_video_config, - preset_name="subtitles_video_test", - preset_dict=single_video_subs_preset_dict, + preset_name="subtitles_embedded_test", + preset_dict=single_video_subs_embed_preset_dict, ) transaction_log = subscription.download(dry_run=dry_run) assert_transaction_log_matches( output_directory=output_directory, transaction_log=transaction_log, - transaction_log_summary_file_name="plugins/test_subtitles_video.txt", + transaction_log_summary_file_name="plugins/test_subtitles_embedded.txt", ) assert_expected_downloads( output_directory=output_directory, dry_run=dry_run, - expected_download_summary_file_name="plugins/test_subtitles_video.json", + expected_download_summary_file_name="plugins/test_subtitles_embedded.json", ) + + @pytest.mark.parametrize("dry_run", [True, False]) + def test_subtitles_embedded_and_file( + self, + music_video_config, + test_single_video_subs_embed_and_file_preset_dict, + output_directory, + dry_run, + ): + subscription = Subscription.from_dict( + config=music_video_config, + preset_name="subtitles_embedded_and_file_test", + preset_dict=test_single_video_subs_embed_and_file_preset_dict, + ) + + transaction_log = subscription.download(dry_run=dry_run) + assert_transaction_log_matches( + output_directory=output_directory, + transaction_log=transaction_log, + transaction_log_summary_file_name="plugins/test_subtitles_embedded_and_file.txt", + ) + assert_expected_downloads( + output_directory=output_directory, + dry_run=dry_run, + expected_download_summary_file_name="plugins/test_subtitles_embedded_and_file.json", + ) \ No newline at end of file diff --git a/tests/e2e/resources/expected_downloads_summaries/plugins/test_subtitles_embedded.json b/tests/e2e/resources/expected_downloads_summaries/plugins/test_subtitles_embedded.json new file mode 100644 index 00000000..d39fc75f --- /dev/null +++ b/tests/e2e/resources/expected_downloads_summaries/plugins/test_subtitles_embedded.json @@ -0,0 +1,5 @@ +{ + "JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind-thumb.jpg": "704246dd78074e8a0ec001dd8d03fd60", + "JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.mp4": "90ae944811ca3312fcb3175ea32a0aa5", + "JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.nfo": "ffa10f1cbc098ace7b1c7a8fbe3097a8" +} \ No newline at end of file diff --git a/tests/e2e/resources/expected_downloads_summaries/plugins/test_subtitles_video.json b/tests/e2e/resources/expected_downloads_summaries/plugins/test_subtitles_embedded_and_file.json similarity index 92% rename from tests/e2e/resources/expected_downloads_summaries/plugins/test_subtitles_video.json rename to tests/e2e/resources/expected_downloads_summaries/plugins/test_subtitles_embedded_and_file.json index 8c38ba09..1c1c6901 100644 --- a/tests/e2e/resources/expected_downloads_summaries/plugins/test_subtitles_video.json +++ b/tests/e2e/resources/expected_downloads_summaries/plugins/test_subtitles_embedded_and_file.json @@ -2,6 +2,6 @@ "JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind-thumb.jpg": "704246dd78074e8a0ec001dd8d03fd60", "JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.de.srt": "b343c3bb9257b7ee7ba38f570a115b37", "JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.en.srt": "fe8c6ee92cae6e059fd80fd61691adbe", - "JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.mp4": "383213d2809cdc2e86e3a2c4c8deb685", + "JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.mp4": "90ae944811ca3312fcb3175ea32a0aa5", "JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.nfo": "ffa10f1cbc098ace7b1c7a8fbe3097a8" } \ No newline at end of file diff --git a/tests/e2e/resources/transaction_log_summaries/plugins/test_subtitles_embedded.txt b/tests/e2e/resources/transaction_log_summaries/plugins/test_subtitles_embedded.txt new file mode 100644 index 00000000..03daf6ee --- /dev/null +++ b/tests/e2e/resources/transaction_log_summaries/plugins/test_subtitles_embedded.txt @@ -0,0 +1,12 @@ +Files created in '{output_directory}' +---------------------------------------- +JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind-thumb.jpg +JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.mp4 + Embedded subtitles with lang(s) en, de +JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.nfo + NFO tags: + musicvideo: + album: Music Videos + artist: JMC + title: YouTube Rewind 2019: For the Record | #YouTubeRewind + year: 2019 \ No newline at end of file diff --git a/tests/e2e/resources/transaction_log_summaries/plugins/test_subtitles_video.txt b/tests/e2e/resources/transaction_log_summaries/plugins/test_subtitles_embedded_and_file.txt similarity index 93% rename from tests/e2e/resources/transaction_log_summaries/plugins/test_subtitles_video.txt rename to tests/e2e/resources/transaction_log_summaries/plugins/test_subtitles_embedded_and_file.txt index 4d6bda7c..d47900c3 100644 --- a/tests/e2e/resources/transaction_log_summaries/plugins/test_subtitles_video.txt +++ b/tests/e2e/resources/transaction_log_summaries/plugins/test_subtitles_embedded_and_file.txt @@ -4,6 +4,7 @@ JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind-thumb.jpg JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.de.srt JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.en.srt JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.mp4 + Embedded subtitles with lang(s) en, de JMC - YouTube Rewind 2019: For the Record | #YouTubeRewind.nfo NFO tags: musicvideo: