From c2cb5087337b9d78bdb967b98f3cfb10547976da Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Mon, 24 Jul 2023 10:11:39 -0700 Subject: [PATCH] tests --- src/ytdl_sub/plugins/embed_thumbnail.py | 74 ++++++++++++------- tests/e2e/youtube/test_video.py | 2 + .../plugins/test_audio_extract_single.json | 2 +- .../youtube/test_video.json | 4 +- .../youtube/test_video_cli.json | 4 +- .../plugins/test_audio_extract_single.txt | 1 + .../youtube/test_video.txt | 1 + .../youtube/test_video_cli.txt | 1 + 8 files changed, 56 insertions(+), 33 deletions(-) diff --git a/src/ytdl_sub/plugins/embed_thumbnail.py b/src/ytdl_sub/plugins/embed_thumbnail.py index 2c448d78..6310569d 100644 --- a/src/ytdl_sub/plugins/embed_thumbnail.py +++ b/src/ytdl_sub/plugins/embed_thumbnail.py @@ -1,5 +1,7 @@ from typing import Optional +import mediafile + from ytdl_sub.config.preset_options import OptionsValidator from ytdl_sub.entries.entry import Entry from ytdl_sub.plugins.plugin import Plugin @@ -8,6 +10,7 @@ from ytdl_sub.utils.file_handler import FileHandler from ytdl_sub.utils.file_handler import FileMetadata from ytdl_sub.utils.logger import Logger from ytdl_sub.utils.thumbnail import convert_download_thumbnail +from ytdl_sub.validators.audo_codec_validator import AUDIO_CODEC_EXTS from ytdl_sub.validators.validators import BoolValidator logger = Logger.get("embed_thumbnail") @@ -34,6 +37,45 @@ class EmbedThumbnailPlugin(Plugin[EmbedThumbnailOptions]): def _embed_thumbnail(self) -> bool: return self.plugin_options.value + @classmethod + def _embed_video_thumbnail(cls, entry: Entry) -> None: + file_path = entry.get_download_file_path() + thumbnail_path = entry.get_download_thumbnail_path() + tmp_file_path = FFMPEG.tmp_file_path(file_path) + try: + cmd = [ + "-i", + file_path, + "-i", + thumbnail_path, + "-map", + "1", + "-map", + "0", + "-dn", # ignore data streams + "-c", + "copy", + "-bitexact", # for reproducibility + "-disposition:0", + "attached_pic", + tmp_file_path, + ] + FFMPEG.run(cmd) + FileHandler.move(tmp_file_path, file_path) + finally: + FileHandler.delete(tmp_file_path) + + @classmethod + def _embed_audio_file(cls, entry: Entry) -> None: + audio_file = mediafile.MediaFile(entry.get_download_file_path()) + with open(entry.get_download_thumbnail_path(), "rb") as thumb: + mediafile_img = mediafile.Image( + data=thumb.read(), desc="cover", type=mediafile.ImageType.front + ) + + audio_file.images = [mediafile_img] + audio_file.save() + def post_process_entry(self, entry: Entry) -> Optional[FileMetadata]: """ Maybe embed the thumbnail @@ -49,33 +91,9 @@ class EmbedThumbnailPlugin(Plugin[EmbedThumbnailOptions]): # convert the entry thumbnail so it is embedded as jpg convert_download_thumbnail(entry=entry) - file_path = entry.get_download_file_path() - thumbnail_path = entry.get_download_thumbnail_path() - tmp_file_path = FFMPEG.tmp_file_path(file_path) - try: - FFMPEG.run( - [ - "-i", - file_path, - "-i", - thumbnail_path, - "-map", - "0", - "-map", - "1", - "-dn", # ignore data streams - "-c", - "-copy", - "-c:v:1", - entry.thumbnail_ext, - "-disposition:v:1", - "attached_pic", - "-bitexact", # for reproducibility - tmp_file_path, - ] - ) - FileHandler.move(tmp_file_path, file_path) - finally: - FileHandler.delete(tmp_file_path) + if entry.ext in AUDIO_CODEC_EXTS: + self._embed_audio_file(entry) + else: + self._embed_video_thumbnail(entry) return FileMetadata("Embedded thumbnail") diff --git a/tests/e2e/youtube/test_video.py b/tests/e2e/youtube/test_video.py index e7a1f2f6..3219692d 100644 --- a/tests/e2e/youtube/test_video.py +++ b/tests/e2e/youtube/test_video.py @@ -17,6 +17,8 @@ def single_video_preset_dict(output_directory): "output_directory": output_directory, "maintain_download_archive": False, }, + # embed thumb into the video + "embed_thumbnail": True, # download the worst format so it is fast "ytdl_options": { "format": "worst[ext=mp4]", diff --git a/tests/resources/expected_downloads_summaries/plugins/test_audio_extract_single.json b/tests/resources/expected_downloads_summaries/plugins/test_audio_extract_single.json index f4aa6b67..c8b056a1 100644 --- a/tests/resources/expected_downloads_summaries/plugins/test_audio_extract_single.json +++ b/tests/resources/expected_downloads_summaries/plugins/test_audio_extract_single.json @@ -1,5 +1,5 @@ { ".ytdl-sub-single_song_test-download-archive.json": "c8ff22ec3304c9f8dab18cedaed4e8b4", - "YouTube/[2019] YouTube Rewind 2019: For the Record | #YouTubeRewind/01 - YouTube Rewind 2019: For the Record | #YouTubeRewind.mp3": "37b38834eda1293ad503e00dcff7c4dc", + "YouTube/[2019] YouTube Rewind 2019: For the Record | #YouTubeRewind/01 - YouTube Rewind 2019: For the Record | #YouTubeRewind.mp3": "4726b6be4f92716adf18dffbb4d78899", "YouTube/[2019] YouTube Rewind 2019: For the Record | #YouTubeRewind/folder.jpg": "50ee47c80f679029f5d3503bb91b045a" } \ No newline at end of file diff --git a/tests/resources/expected_downloads_summaries/youtube/test_video.json b/tests/resources/expected_downloads_summaries/youtube/test_video.json index f7d75ddc..2bf540a6 100644 --- a/tests/resources/expected_downloads_summaries/youtube/test_video.json +++ b/tests/resources/expected_downloads_summaries/youtube/test_video.json @@ -1,6 +1,6 @@ { "JMC/JMC - Oblivion Mod "Falcor" p.1-thumb.jpg": "fb95b510681676e81c321171fc23143e", - "JMC/JMC - Oblivion Mod "Falcor" p.1.info.json": "73dd7eb872fe150364581de40a66777d", - "JMC/JMC - Oblivion Mod "Falcor" p.1.mp4": "3744c49f2e447bd7712a5aad5ed36be2", + "JMC/JMC - Oblivion Mod "Falcor" p.1.info.json": "8954ea35e5b974dfc37a147d6d3b374f", + "JMC/JMC - Oblivion Mod "Falcor" p.1.mp4": "aeed76c53ec458626edbede0825dc34b", "JMC/JMC - Oblivion Mod "Falcor" p.1.nfo": "24cc4e17d2bebc89b2759ce5471d403e" } \ No newline at end of file diff --git a/tests/resources/expected_downloads_summaries/youtube/test_video_cli.json b/tests/resources/expected_downloads_summaries/youtube/test_video_cli.json index 27e58215..069244f1 100644 --- a/tests/resources/expected_downloads_summaries/youtube/test_video_cli.json +++ b/tests/resources/expected_downloads_summaries/youtube/test_video_cli.json @@ -1,6 +1,6 @@ { "JMC/JMC - Oblivion Mod "Falcor" p.1-thumb.jpg": "fb95b510681676e81c321171fc23143e", - "JMC/JMC - Oblivion Mod "Falcor" p.1.info.json": "7ffe51eae87c1d6fd10151a366c9d207", - "JMC/JMC - Oblivion Mod "Falcor" p.1.mp4": "3744c49f2e447bd7712a5aad5ed36be2", + "JMC/JMC - Oblivion Mod "Falcor" p.1.info.json": "1841e2ffd6f0609fedc5475b919528e2", + "JMC/JMC - Oblivion Mod "Falcor" p.1.mp4": "aeed76c53ec458626edbede0825dc34b", "JMC/JMC - Oblivion Mod "Falcor" p.1.nfo": "24cc4e17d2bebc89b2759ce5471d403e" } \ No newline at end of file diff --git a/tests/resources/transaction_log_summaries/plugins/test_audio_extract_single.txt b/tests/resources/transaction_log_summaries/plugins/test_audio_extract_single.txt index 6327e106..cb489739 100644 --- a/tests/resources/transaction_log_summaries/plugins/test_audio_extract_single.txt +++ b/tests/resources/transaction_log_summaries/plugins/test_audio_extract_single.txt @@ -4,6 +4,7 @@ Files created: .ytdl-sub-single_song_test-download-archive.json {output_directory}/YouTube/[2019] YouTube Rewind 2019: For the Record | #YouTubeRewind 01 - YouTube Rewind 2019: For the Record | #YouTubeRewind.mp3 + Embedded thumbnail Embedded Thumbnail, Music Tags: album: YouTube Rewind 2019: For the Record | #YouTubeRewind albumartist: YouTube diff --git a/tests/resources/transaction_log_summaries/youtube/test_video.txt b/tests/resources/transaction_log_summaries/youtube/test_video.txt index 8162ad7f..7b632030 100644 --- a/tests/resources/transaction_log_summaries/youtube/test_video.txt +++ b/tests/resources/transaction_log_summaries/youtube/test_video.txt @@ -4,6 +4,7 @@ Files created: JMC - Oblivion Mod "Falcor" p.1-thumb.jpg JMC - Oblivion Mod "Falcor" p.1.info.json JMC - Oblivion Mod "Falcor" p.1.mp4 + Embedded thumbnail Video Tags: title: Oblivion Mod "Falcor" p.1 JMC - Oblivion Mod "Falcor" p.1.nfo diff --git a/tests/resources/transaction_log_summaries/youtube/test_video_cli.txt b/tests/resources/transaction_log_summaries/youtube/test_video_cli.txt index 8162ad7f..7b632030 100644 --- a/tests/resources/transaction_log_summaries/youtube/test_video_cli.txt +++ b/tests/resources/transaction_log_summaries/youtube/test_video_cli.txt @@ -4,6 +4,7 @@ Files created: JMC - Oblivion Mod "Falcor" p.1-thumb.jpg JMC - Oblivion Mod "Falcor" p.1.info.json JMC - Oblivion Mod "Falcor" p.1.mp4 + Embedded thumbnail Video Tags: title: Oblivion Mod "Falcor" p.1 JMC - Oblivion Mod "Falcor" p.1.nfo