From 7ec1e417ca28a7c76ee116138e80e5744806ac14 Mon Sep 17 00:00:00 2001 From: Alexandre L Date: Sun, 5 Nov 2017 01:58:03 +0100 Subject: [PATCH] Allow embedding thumbnail in MKV as attachment To fix rg3#10359, use `-attach` to embed the thumbnail as attachment. In this version, the attached filename follow the https://matroska.org/technical/cover_art/index.html convention, as pointed in #6046. The image dimensions are probably not going to respect the convention though, and we assume the thumbnail is in landscape orientation. As only 2 images type seems to be recognized by the convention, they are hardcoded (mimetypes module would match on extension too). --- youtube_dl/postprocessor/embedthumbnail.py | 37 ++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index e606a58de..1a1ca1ae6 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -40,7 +40,40 @@ class EmbedThumbnailPP(FFmpegPostProcessor): 'Skipping embedding the thumbnail because the file is missing.') return [], info - if info['ext'] == 'mp3': + if info['ext'] == 'mkv': + if thumbnail_filename.endswith(('.jpe', '.jpeg', '.jpg', '.jfif')): + mimetype = 'image/jpeg' + extension = 'jpg' + elif thumbnail_filename.endswith('.png'): + mimetype = 'image/png' + extension = 'png' + else: + self._downloader.report_warning( + 'Skipping embedding the thumbnail because the thumbnail extension is unknown.') + return [], info + + options = [ + '-c', 'copy', + '-attach', thumbnail_filename, + # https://matroska.org/technical/cover_art/index.html as pointed in #6046 + # No orientation detection nor dimensions checking/convertion + '-metadata:s:t', 'filename=cover_land.{}'.format(extension), + # If not given : "[matroska @ 000001458de38840] Attachment stream 2 has no mimetype tag and it cannot be deduced from the codec id." + '-metadata:s:t', 'mimetype={}'.format(mimetype), + # Use metadata "title" so it is set as MATROSKA_ID_FILEDESC - optional + # https://github.com/FFmpeg/FFmpeg/blob/9cfdf0e3322b9a451277cf36406ac4a8e4e3da74/libavformat/matroskaenc.c#L1762 + '-metadata:s:t', 'title=Thumbnail'] + + self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename) + + self.run_ffmpeg(filename, temp_filename, options) + + if not self._already_have_thumbnail: + os.remove(encodeFilename(thumbnail_filename)) + os.remove(encodeFilename(filename)) + os.rename(encodeFilename(temp_filename), encodeFilename(filename)) + + elif info['ext'] == 'mp3': options = [ '-c', 'copy', '-map', '0', '-map', '1', '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"'] @@ -87,6 +120,6 @@ class EmbedThumbnailPP(FFmpegPostProcessor): 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 and mkv are supported for thumbnail embedding for now.') return [], info