From 7ec1e417ca28a7c76ee116138e80e5744806ac14 Mon Sep 17 00:00:00 2001 From: Alexandre L Date: Sun, 5 Nov 2017 01:58:03 +0100 Subject: [PATCH 1/6] 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 From d5bb4d18fc31efe9b41260a3209bf09bf8e276d3 Mon Sep 17 00:00:00 2001 From: Alexandre L Date: Mon, 29 Jan 2018 17:24:59 +0100 Subject: [PATCH 2/6] Remove duplicated code --- youtube_dl/postprocessor/embedthumbnail.py | 23 +++++++++------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 1a1ca1ae6..f83d15041 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -40,6 +40,9 @@ class EmbedThumbnailPP(FFmpegPostProcessor): 'Skipping embedding the thumbnail because the file is missing.') return [], info + if info['ext'] not in ['mp3', 'mkv', 'm4a', 'mp4']: + raise EmbedThumbnailPPError('Only mp3, m4a/mp4 and mkv are supported for thumbnail embedding for now.') + if info['ext'] == 'mkv': if thumbnail_filename.endswith(('.jpe', '.jpeg', '.jpg', '.jfif')): mimetype = 'image/jpeg' @@ -59,35 +62,29 @@ class EmbedThumbnailPP(FFmpegPostProcessor): # 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), + '-metadata:s:t', 'mimetype=%s' % 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)) + input_paths = [filename] elif info['ext'] == 'mp3': options = [ '-c', 'copy', '-map', '0', '-map', '1', '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"'] + input_paths = [filename, thumbnail_filename] + if info['ext'] in ['mkv', 'mp3']: self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename) - self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options) + self.run_ffmpeg_multiple_files(input_paths, 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'] in ['m4a', 'mp4']: + if info['ext'] in ['m4a', 'mp4']: if not check_executable('AtomicParsley', ['-v']): raise EmbedThumbnailPPError('AtomicParsley was not found. Please install.') @@ -119,7 +116,5 @@ class EmbedThumbnailPP(FFmpegPostProcessor): else: os.remove(encodeFilename(filename)) os.rename(encodeFilename(temp_filename), encodeFilename(filename)) - else: - raise EmbedThumbnailPPError('Only mp3, m4a/mp4 and mkv are supported for thumbnail embedding for now.') return [], info From fbb4e6a746a47cae14e738d3dd8eb229f04fc313 Mon Sep 17 00:00:00 2001 From: Alexandre L Date: Mon, 29 Jan 2018 17:28:47 +0100 Subject: [PATCH 3/6] Put back mp3 first --- 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 f83d15041..3115a4fb0 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -43,7 +43,13 @@ class EmbedThumbnailPP(FFmpegPostProcessor): if info['ext'] not in ['mp3', 'mkv', 'm4a', 'mp4']: raise EmbedThumbnailPPError('Only mp3, m4a/mp4 and mkv are supported for thumbnail embedding for now.') - if info['ext'] == 'mkv': + if info['ext'] == 'mp3': + options = [ + '-c', 'copy', '-map', '0', '-map', '1', + '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"'] + input_paths = [filename, thumbnail_filename] + + elif info['ext'] == 'mkv': if thumbnail_filename.endswith(('.jpe', '.jpeg', '.jpg', '.jfif')): mimetype = 'image/jpeg' extension = 'jpg' @@ -68,12 +74,6 @@ class EmbedThumbnailPP(FFmpegPostProcessor): '-metadata:s:t', 'title=Thumbnail'] input_paths = [filename] - elif info['ext'] == 'mp3': - options = [ - '-c', 'copy', '-map', '0', '-map', '1', - '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"'] - input_paths = [filename, thumbnail_filename] - if info['ext'] in ['mkv', 'mp3']: self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename) From 99aa6a20e3fcfd58a5a039c9bdd7b34a034ba1e9 Mon Sep 17 00:00:00 2001 From: Alexandre L Date: Mon, 29 Jan 2018 18:30:04 +0100 Subject: [PATCH 4/6] Forgotten {} --- youtube_dl/postprocessor/embedthumbnail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 3115a4fb0..aab7f1ffa 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -66,7 +66,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): '-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), + '-metadata:s:t', 'filename=cover_land.%s' % 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=%s' % mimetype, # Use metadata "title" so it is set as MATROSKA_ID_FILEDESC - optional From 263b629121555cc4c183158292abd0e7e5bc321a Mon Sep 17 00:00:00 2001 From: Alexandre L Date: Mon, 29 Jan 2018 18:51:19 +0100 Subject: [PATCH 5/6] Use the same order everywhere :) --- youtube_dl/postprocessor/embedthumbnail.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index aab7f1ffa..6446116ff 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -41,7 +41,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): return [], info if info['ext'] not in ['mp3', 'mkv', 'm4a', 'mp4']: - raise EmbedThumbnailPPError('Only mp3, m4a/mp4 and mkv are supported for thumbnail embedding for now.') + raise EmbedThumbnailPPError('Only mp3, mkv and m4a/mp4 are supported for thumbnail embedding for now.') if info['ext'] == 'mp3': options = [ @@ -74,7 +74,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): '-metadata:s:t', 'title=Thumbnail'] input_paths = [filename] - if info['ext'] in ['mkv', 'mp3']: + if info['ext'] in ['mp3', 'mkv']: self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename) self.run_ffmpeg_multiple_files(input_paths, temp_filename, options) From 7006dabd4c5f48d55459b3d8d9b16068b44fcb0c Mon Sep 17 00:00:00 2001 From: 0x9fff00 <0x9fff00+git@protonmail.ch> Date: Fri, 22 Feb 2019 20:25:42 +0100 Subject: [PATCH 6/6] From @0x9fff00: `-map 0` is needed to copy all streams from the source file (untested) Co-Authored-By: Alex131089 --- youtube_dl/postprocessor/embedthumbnail.py | 1 + 1 file changed, 1 insertion(+) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 6446116ff..6481a87fe 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -63,6 +63,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): options = [ '-c', 'copy', + '-map', '0', '-attach', thumbnail_filename, # https://matroska.org/technical/cover_art/index.html as pointed in #6046 # No orientation detection nor dimensions checking/convertion