This commit is contained in:
Jesse Bannon 2023-07-24 10:11:39 -07:00
parent aa6728abca
commit c2cb508733
8 changed files with 56 additions and 33 deletions

View file

@ -1,5 +1,7 @@
from typing import Optional from typing import Optional
import mediafile
from ytdl_sub.config.preset_options import OptionsValidator from ytdl_sub.config.preset_options import OptionsValidator
from ytdl_sub.entries.entry import Entry from ytdl_sub.entries.entry import Entry
from ytdl_sub.plugins.plugin import Plugin 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.file_handler import FileMetadata
from ytdl_sub.utils.logger import Logger from ytdl_sub.utils.logger import Logger
from ytdl_sub.utils.thumbnail import convert_download_thumbnail 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 from ytdl_sub.validators.validators import BoolValidator
logger = Logger.get("embed_thumbnail") logger = Logger.get("embed_thumbnail")
@ -34,6 +37,45 @@ class EmbedThumbnailPlugin(Plugin[EmbedThumbnailOptions]):
def _embed_thumbnail(self) -> bool: def _embed_thumbnail(self) -> bool:
return self.plugin_options.value 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]: def post_process_entry(self, entry: Entry) -> Optional[FileMetadata]:
""" """
Maybe embed the thumbnail Maybe embed the thumbnail
@ -49,33 +91,9 @@ class EmbedThumbnailPlugin(Plugin[EmbedThumbnailOptions]):
# convert the entry thumbnail so it is embedded as jpg # convert the entry thumbnail so it is embedded as jpg
convert_download_thumbnail(entry=entry) convert_download_thumbnail(entry=entry)
file_path = entry.get_download_file_path() if entry.ext in AUDIO_CODEC_EXTS:
thumbnail_path = entry.get_download_thumbnail_path() self._embed_audio_file(entry)
tmp_file_path = FFMPEG.tmp_file_path(file_path) else:
try: self._embed_video_thumbnail(entry)
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)
return FileMetadata("Embedded thumbnail") return FileMetadata("Embedded thumbnail")

View file

@ -17,6 +17,8 @@ def single_video_preset_dict(output_directory):
"output_directory": output_directory, "output_directory": output_directory,
"maintain_download_archive": False, "maintain_download_archive": False,
}, },
# embed thumb into the video
"embed_thumbnail": True,
# download the worst format so it is fast # download the worst format so it is fast
"ytdl_options": { "ytdl_options": {
"format": "worst[ext=mp4]", "format": "worst[ext=mp4]",

View file

@ -1,5 +1,5 @@
{ {
".ytdl-sub-single_song_test-download-archive.json": "c8ff22ec3304c9f8dab18cedaed4e8b4", ".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" "YouTube/[2019] YouTube Rewind 2019 For the Record #YouTubeRewind/folder.jpg": "50ee47c80f679029f5d3503bb91b045a"
} }

View file

@ -1,6 +1,6 @@
{ {
"JMC/JMC - Oblivion Mod Falcor p.1-thumb.jpg": "fb95b510681676e81c321171fc23143e", "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.info.json": "8954ea35e5b974dfc37a147d6d3b374f",
"JMC/JMC - Oblivion Mod Falcor p.1.mp4": "3744c49f2e447bd7712a5aad5ed36be2", "JMC/JMC - Oblivion Mod Falcor p.1.mp4": "aeed76c53ec458626edbede0825dc34b",
"JMC/JMC - Oblivion Mod Falcor p.1.nfo": "24cc4e17d2bebc89b2759ce5471d403e" "JMC/JMC - Oblivion Mod Falcor p.1.nfo": "24cc4e17d2bebc89b2759ce5471d403e"
} }

View file

@ -1,6 +1,6 @@
{ {
"JMC/JMC - Oblivion Mod Falcor p.1-thumb.jpg": "fb95b510681676e81c321171fc23143e", "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.info.json": "1841e2ffd6f0609fedc5475b919528e2",
"JMC/JMC - Oblivion Mod Falcor p.1.mp4": "3744c49f2e447bd7712a5aad5ed36be2", "JMC/JMC - Oblivion Mod Falcor p.1.mp4": "aeed76c53ec458626edbede0825dc34b",
"JMC/JMC - Oblivion Mod Falcor p.1.nfo": "24cc4e17d2bebc89b2759ce5471d403e" "JMC/JMC - Oblivion Mod Falcor p.1.nfo": "24cc4e17d2bebc89b2759ce5471d403e"
} }

View file

@ -4,6 +4,7 @@ Files created:
.ytdl-sub-single_song_test-download-archive.json .ytdl-sub-single_song_test-download-archive.json
{output_directory}/YouTube/[2019] YouTube Rewind 2019 For the Record #YouTubeRewind {output_directory}/YouTube/[2019] YouTube Rewind 2019 For the Record #YouTubeRewind
01 - YouTube Rewind 2019 For the Record #YouTubeRewind.mp3 01 - YouTube Rewind 2019 For the Record #YouTubeRewind.mp3
Embedded thumbnail
Embedded Thumbnail, Music Tags: Embedded Thumbnail, Music Tags:
album: YouTube Rewind 2019: For the Record | #YouTubeRewind album: YouTube Rewind 2019: For the Record | #YouTubeRewind
albumartist: YouTube albumartist: YouTube

View file

@ -4,6 +4,7 @@ Files created:
JMC - Oblivion Mod Falcor p.1-thumb.jpg JMC - Oblivion Mod Falcor p.1-thumb.jpg
JMC - Oblivion Mod Falcor p.1.info.json JMC - Oblivion Mod Falcor p.1.info.json
JMC - Oblivion Mod Falcor p.1.mp4 JMC - Oblivion Mod Falcor p.1.mp4
Embedded thumbnail
Video Tags: Video Tags:
title: Oblivion Mod "Falcor" p.1 title: Oblivion Mod "Falcor" p.1
JMC - Oblivion Mod Falcor p.1.nfo JMC - Oblivion Mod Falcor p.1.nfo

View file

@ -4,6 +4,7 @@ Files created:
JMC - Oblivion Mod Falcor p.1-thumb.jpg JMC - Oblivion Mod Falcor p.1-thumb.jpg
JMC - Oblivion Mod Falcor p.1.info.json JMC - Oblivion Mod Falcor p.1.info.json
JMC - Oblivion Mod Falcor p.1.mp4 JMC - Oblivion Mod Falcor p.1.mp4
Embedded thumbnail
Video Tags: Video Tags:
title: Oblivion Mod "Falcor" p.1 title: Oblivion Mod "Falcor" p.1
JMC - Oblivion Mod Falcor p.1.nfo JMC - Oblivion Mod Falcor p.1.nfo