From de3865fec0cb27004873e15864edd8672313a411 Mon Sep 17 00:00:00 2001 From: tripulse Date: Tue, 27 Apr 2021 11:44:58 +0530 Subject: [PATCH 1/3] OggVorbis, OggOpus, FLAC thumbnail embed support. --- youtube_dl/postprocessor/embedthumbnail.py | 34 +++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 3990908b6..c854cb541 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -17,6 +17,7 @@ from ..utils import ( shell_quote ) +from base64 import b64encode class EmbedThumbnailPPError(PostProcessingError): pass @@ -124,7 +125,38 @@ class EmbedThumbnailPP(FFmpegPostProcessor): else: os.remove(encodeFilename(filename)) os.rename(encodeFilename(temp_filename), encodeFilename(filename)) + + elif info['ext'] in ['opus', 'ogg', 'flac']: + try: + from mutagen.oggvorbis import OggVorbis + from mutagen.oggopus import OggOpus + from mutagen.flac import Picture, FLAC + except ImportError: + raise EmbedThumbnailPPError('mutagen was not found. Please install.') + + # to prevent the behaviour of in-place modification of Mutagen + shutil.copyfile(filename, temp_filename) + + aufile = {'opus': OggOpus, 'flac': FLAC, 'ogg', OggVorbis}[info['ext']](temp_filename) + + covart = Picture() + covart.data = open(thumbnail_filename, 'rb').read() + covart.type = 3 # as the front cover + + if info['ext'] == 'flac': + aufile.add_picture(covart) + else: + # https://wiki.xiph.org/VorbisComment#METADATA_BLOCK_PICTURE + aufile['metadata_block_picture'] = b64encode(covart.write()).decode('ascii') + + self._downloader.to_screen('[mutagen] Adding thumbnail to "%s"' % temp_filename) + aufile.save() + + if not self._already_have_thumbnail: + os.remove(encodeFilename(thumbnail_filename)) + os.remove(encodeFilename(filename)) + os.rename(encodeFilename(temp_filename), encodeFilename(filename)) else: - raise EmbedThumbnailPPError('Only mp3 and m4a/mp4 are supported for thumbnail embedding for now.') + raise EmbedThumbnailPPError('Only mp3, m4a/mp4, ogg, opus and flac are supported for thumbnail embedding for now.') return [], info From 9ca71e76baf958a22b7685cd8cf27aa1bbb2e9b4 Mon Sep 17 00:00:00 2001 From: tripulse Date: Tue, 27 Apr 2021 12:00:50 +0530 Subject: [PATCH 2/3] Fixed missing imports, ran Flake8 over the fille. --- youtube_dl/postprocessor/embedthumbnail.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index c854cb541..69613ffaf 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -18,6 +18,8 @@ from ..utils import ( ) from base64 import b64encode +from shutil import copyfile + class EmbedThumbnailPPError(PostProcessingError): pass @@ -129,15 +131,15 @@ class EmbedThumbnailPP(FFmpegPostProcessor): elif info['ext'] in ['opus', 'ogg', 'flac']: try: from mutagen.oggvorbis import OggVorbis - from mutagen.oggopus import OggOpus - from mutagen.flac import Picture, FLAC + from mutagen.oggopus import OggOpus + from mutagen.flac import Picture, FLAC except ImportError: raise EmbedThumbnailPPError('mutagen was not found. Please install.') # to prevent the behaviour of in-place modification of Mutagen - shutil.copyfile(filename, temp_filename) + copyfile(filename, temp_filename) - aufile = {'opus': OggOpus, 'flac': FLAC, 'ogg', OggVorbis}[info['ext']](temp_filename) + aufile = {'opus': OggOpus, 'flac': FLAC, 'ogg': OggVorbis}[info['ext']](temp_filename) covart = Picture() covart.data = open(thumbnail_filename, 'rb').read() @@ -151,7 +153,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): self._downloader.to_screen('[mutagen] Adding thumbnail to "%s"' % temp_filename) aufile.save() - + if not self._already_have_thumbnail: os.remove(encodeFilename(thumbnail_filename)) os.remove(encodeFilename(filename)) From 80a88a15ab7e3fcdc76d40aa733ab8f8114df478 Mon Sep 17 00:00:00 2001 From: tripulse Date: Tue, 27 Apr 2021 18:46:34 +0530 Subject: [PATCH 3/3] Transform in place. Tag in mimetype. --- youtube_dl/postprocessor/embedthumbnail.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 69613ffaf..8c454b245 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -4,6 +4,7 @@ from __future__ import unicode_literals import os import subprocess +import imghdr from .ffmpeg import FFmpegPostProcessor @@ -18,7 +19,6 @@ from ..utils import ( ) from base64 import b64encode -from shutil import copyfile class EmbedThumbnailPPError(PostProcessingError): @@ -136,13 +136,12 @@ class EmbedThumbnailPP(FFmpegPostProcessor): except ImportError: raise EmbedThumbnailPPError('mutagen was not found. Please install.') - # to prevent the behaviour of in-place modification of Mutagen - copyfile(filename, temp_filename) - - aufile = {'opus': OggOpus, 'flac': FLAC, 'ogg': OggVorbis}[info['ext']](temp_filename) + aufile = {'opus': OggOpus, 'flac': FLAC, 'ogg': OggVorbis}[info['ext']](filename) + thumbfile = open(thumbnail_filename, 'rb') covart = Picture() - covart.data = open(thumbnail_filename, 'rb').read() + covart.mime = 'image/' + imghdr.what(thumbfile) + covart.data = thumbfile.read() covart.type = 3 # as the front cover if info['ext'] == 'flac': @@ -156,8 +155,6 @@ class EmbedThumbnailPP(FFmpegPostProcessor): if not self._already_have_thumbnail: os.remove(encodeFilename(thumbnail_filename)) - os.remove(encodeFilename(filename)) - os.rename(encodeFilename(temp_filename), encodeFilename(filename)) else: raise EmbedThumbnailPPError('Only mp3, m4a/mp4, ogg, opus and flac are supported for thumbnail embedding for now.')