From 87b94f55e6f5696ae7474d7e1c2f7416d2c91b16 Mon Sep 17 00:00:00 2001 From: Tryamid Date: Mon, 9 Mar 2020 22:49:06 +0530 Subject: [PATCH] [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