From 87b94f55e6f5696ae7474d7e1c2f7416d2c91b16 Mon Sep 17 00:00:00 2001 From: Tryamid Date: Mon, 9 Mar 2020 22:49:06 +0530 Subject: [PATCH 1/6] [embedthumbnail] Add support for OGGVorbis, OGGOpus and FLAC using mutagen. --- youtube_dl/postprocessor/embedthumbnail.py | 39 +++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 56be914b8..90002460c 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import os +import shutil import subprocess from .ffmpeg import FFmpegPostProcessor @@ -41,6 +42,8 @@ class EmbedThumbnailPP(FFmpegPostProcessor): 'Skipping embedding the thumbnail because the file is missing.') return [], info + self._downloader.to_screen("used format" + info['ext']) + if info['ext'] == 'mp3': options = [ '-c', 'copy', '-map', '0', '-map', '1', @@ -87,7 +90,41 @@ 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 + from base64 import b64encode + + # 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 #< use as front cover. + + if info['ext'] == 'flac': + aufile.add_picture(covart) + else: + # VorbisComments don't allow raw-bytes so it's encoded + # in base64 and converted to a string. + aufile['metadata_block_picture'] = \ + b64encode(covart.write()).decode('ascii') + + aufile.save() + + if not self._already_have_thumbnail: + os.remove(encodeFilename(thumbnail_filename)) + os.remove(encodeFilename(filename)) + os.rename(encodeFilename(temp_filename), encodeFilename(filename)) + except ImportError: + raise EmbedThumbnailPPError('mutagen was not found. Please install.') 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 01d84b7db5ed0ec53e4b60d5c55a229e049b991e Mon Sep 17 00:00:00 2001 From: Tryamid Date: Tue, 10 Mar 2020 07:00:35 +0530 Subject: [PATCH 2/6] [embedthumbnail] Only wrapped import statements with try..except. Some comments are added previous ones are improvised. --- youtube_dl/postprocessor/embedthumbnail.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 90002460c..c5c3cbf24 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -97,33 +97,33 @@ class EmbedThumbnailPP(FFmpegPostProcessor): from mutagen.oggopus import OggOpus from mutagen.flac import Picture, FLAC from base64 import b64encode + 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 #< use as front cover. + covart.type = 3 # Cover (front) + # Since, OGGOpus and OGGVorbis doesn't natively support + # (coverart / thumbnail)s, it's wrapped in if..else if info['ext'] == 'flac': aufile.add_picture(covart) else: - # VorbisComments don't allow raw-bytes so it's encoded - # in base64 and converted to a string. aufile['metadata_block_picture'] = \ b64encode(covart.write()).decode('ascii') + # Save changes to temporary file, it'd be overlapped as the + # original one. aufile.save() if not self._already_have_thumbnail: os.remove(encodeFilename(thumbnail_filename)) os.remove(encodeFilename(filename)) os.rename(encodeFilename(temp_filename), encodeFilename(filename)) - except ImportError: - raise EmbedThumbnailPPError('mutagen was not found. Please install.') else: raise EmbedThumbnailPPError('Only mp3, m4a/mp4, ogg, opus and flac are supported for thumbnail embedding for now.') From 5e10411546187761d90b266f62457c7da2be5502 Mon Sep 17 00:00:00 2001 From: Tryamid Date: Tue, 10 Mar 2020 07:01:36 +0530 Subject: [PATCH 3/6] [embedthumbnail] Fixed a bug causing the rest of the code to be unreachable. --- youtube_dl/postprocessor/embedthumbnail.py | 48 +++++++++++----------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index c5c3cbf24..48456d37f 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -100,30 +100,30 @@ class EmbedThumbnailPP(FFmpegPostProcessor): except ImportError: raise EmbedThumbnailPPError('mutagen was not found. Please install.') - 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 # Cover (front) - - # Since, OGGOpus and OGGVorbis doesn't natively support - # (coverart / thumbnail)s, it's wrapped in if..else - if info['ext'] == 'flac': - aufile.add_picture(covart) - else: - aufile['metadata_block_picture'] = \ - b64encode(covart.write()).decode('ascii') - - # Save changes to temporary file, it'd be overlapped as the - # original one. - aufile.save() - - if not self._already_have_thumbnail: - os.remove(encodeFilename(thumbnail_filename)) - os.remove(encodeFilename(filename)) - os.rename(encodeFilename(temp_filename), encodeFilename(filename)) + 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 # Cover (front) + + # Since, OGGOpus and OGGVorbis doesn't natively support + # (coverart / thumbnail)s, it's wrapped in if..else + if info['ext'] == 'flac': + aufile.add_picture(covart) + else: + aufile['metadata_block_picture'] = \ + b64encode(covart.write()).decode('ascii') + + # Save changes to temporary file, it'd be overlapped as the + # original one. + 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, m4a/mp4, ogg, opus and flac are supported for thumbnail embedding for now.') From 2715865cd0ef64dcf90250b4625aefc34297b27d Mon Sep 17 00:00:00 2001 From: Tryamid Date: Tue, 10 Mar 2020 07:07:11 +0530 Subject: [PATCH 4/6] [embedthumbnail] Use the imported base64 module from the 'utils' --- youtube_dl/postprocessor/embedthumbnail.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 48456d37f..0c0613408 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -14,7 +14,8 @@ from ..utils import ( encodeFilename, PostProcessingError, prepend_extension, - shell_quote + shell_quote, + base64 ) @@ -96,7 +97,6 @@ class EmbedThumbnailPP(FFmpegPostProcessor): from mutagen.oggvorbis import OggVorbis from mutagen.oggopus import OggOpus from mutagen.flac import Picture, FLAC - from base64 import b64encode except ImportError: raise EmbedThumbnailPPError('mutagen was not found. Please install.') @@ -114,7 +114,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): aufile.add_picture(covart) else: aufile['metadata_block_picture'] = \ - b64encode(covart.write()).decode('ascii') + base64.b64encode(covart.write()).decode('ascii') # Save changes to temporary file, it'd be overlapped as the # original one. From 982f386fdf97a9934c154550a3ad9c479f9d2fd7 Mon Sep 17 00:00:00 2001 From: Tryamid Date: Tue, 10 Mar 2020 08:04:15 +0530 Subject: [PATCH 5/6] [embedthumbnai] Ran flake8 over the code and fixed according to its standard. --- youtube_dl/postprocessor/embedthumbnail.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 0c0613408..6f5301a2b 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -91,22 +91,22 @@ 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 + from mutagen.oggopus import OggOpus + from mutagen.flac import Picture, FLAC except ImportError: raise EmbedThumbnailPPError('mutagen was not found. Please install.') shutil.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() - covart.type = 3 # Cover (front) + covart.type = 3 # Cover (front) # Since, OGGOpus and OGGVorbis doesn't natively support # (coverart / thumbnail)s, it's wrapped in if..else @@ -115,7 +115,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): else: aufile['metadata_block_picture'] = \ base64.b64encode(covart.write()).decode('ascii') - + # Save changes to temporary file, it'd be overlapped as the # original one. aufile.save() From 7f2f14fcb8d2d89c4a9a40e57e2d949290228e52 Mon Sep 17 00:00:00 2001 From: Tryamid Date: Tue, 10 Mar 2020 13:55:12 +0530 Subject: [PATCH 6/6] [embedthumbnail] a little 'debug' statement got out of eyes. --- youtube_dl/postprocessor/embedthumbnail.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 6f5301a2b..e34589126 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -43,8 +43,6 @@ class EmbedThumbnailPP(FFmpegPostProcessor): 'Skipping embedding the thumbnail because the file is missing.') return [], info - self._downloader.to_screen("used format" + info['ext']) - if info['ext'] == 'mp3': options = [ '-c', 'copy', '-map', '0', '-map', '1',