From 322a523dad897726e353cf86075ea62b31cc0835 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Mon, 21 Sep 2020 20:49:50 +0530 Subject: [PATCH] Added embedding of thumbnails in mkv files (#10) * MKV Thumbnails with FFMpeg * Now has the correct file name in MKV container * Change os.rename to shutil.move Co-authored-by: MrDoritos Co-authored-by: ian Co-authored-by: pukkandan --- youtube_dl/postprocessor/embedthumbnail.py | 32 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 5a3359588..1e9bcd077 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 @@ -55,7 +56,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): self._downloader.to_screen( '[ffmpeg] Correcting extension to webp and escaping path for thumbnail "%s"' % thumbnail_filename) thumbnail_webp_filename = replace_extension(thumbnail_filename, 'webp') - os.rename(encodeFilename(thumbnail_filename), encodeFilename(thumbnail_webp_filename)) + shutil.move(encodeFilename(thumbnail_filename), encodeFilename(thumbnail_webp_filename)) thumbnail_filename = thumbnail_webp_filename thumbnail_ext = 'webp' @@ -64,15 +65,15 @@ class EmbedThumbnailPP(FFmpegPostProcessor): # NB: % is supposed to be escaped with %% but this does not work # for input files so working around with standard substitution escaped_thumbnail_filename = thumbnail_filename.replace('%', '#') - os.rename(encodeFilename(thumbnail_filename), encodeFilename(escaped_thumbnail_filename)) + shutil.move(encodeFilename(thumbnail_filename), encodeFilename(escaped_thumbnail_filename)) escaped_thumbnail_jpg_filename = replace_extension(escaped_thumbnail_filename, 'jpg') self._downloader.to_screen('[ffmpeg] Converting thumbnail "%s" to JPEG' % escaped_thumbnail_filename) self.run_ffmpeg(escaped_thumbnail_filename, escaped_thumbnail_jpg_filename, ['-bsf:v', 'mjpeg2jpeg']) - os.remove(encodeFilename(escaped_thumbnail_filename)) thumbnail_jpg_filename = replace_extension(thumbnail_filename, 'jpg') # Rename back to unescaped for further processing - os.rename(encodeFilename(escaped_thumbnail_jpg_filename), encodeFilename(thumbnail_jpg_filename)) + shutil.move(encodeFilename(escaped_thumbnail_jpg_filename), encodeFilename(thumbnail_jpg_filename)) thumbnail_filename = thumbnail_jpg_filename + os.remove(encodeFilename(escaped_thumbnail_filename)) if info['ext'] == 'mp3': options = [ @@ -86,7 +87,26 @@ 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)) + shutil.move(encodeFilename(temp_filename), encodeFilename(filename)) + + elif info['ext'] == 'mkv': + shutil.move(encodeFilename(thumbnail_filename), encodeFilename('cover.jpg')) + old_thumbnail_filename = thumbnail_filename + thumbnail_filename = 'cover.jpg' + + options = [ + '-c', 'copy', '-attach', thumbnail_filename, '-metadata:s:t', 'mimetype=image/jpeg'] + + self._downloader.to_screen('[ffmpeg] Adding thumbnail to "%s"' % filename) + + self.run_ffmpeg_multiple_files([filename], temp_filename, options) + + if not self._already_have_thumbnail: + os.remove(encodeFilename(thumbnail_filename)) + else: + shutil.move(encodeFilename(thumbnail_filename), encodeFilename(old_thumbnail_filename)) + os.remove(encodeFilename(filename)) + shutil.move(encodeFilename(temp_filename), encodeFilename(filename)) elif info['ext'] in ['m4a', 'mp4']: if not check_executable('AtomicParsley', ['-v']): @@ -119,7 +139,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail') else: os.remove(encodeFilename(filename)) - os.rename(encodeFilename(temp_filename), encodeFilename(filename)) + shutil.move(encodeFilename(temp_filename), encodeFilename(filename)) else: raise EmbedThumbnailPPError('Only mp3 and m4a/mp4 are supported for thumbnail embedding for now.')