From 777d5a45be81fb1f274c8c558ba1eb24855c66fc Mon Sep 17 00:00:00 2001 From: Alex Merkel Date: Thu, 18 Jun 2020 22:36:44 +0200 Subject: [PATCH 1/5] [postprocessor/embedthumbnail] Add conversion for non JPG/PNG images --- youtube_dl/postprocessor/embedthumbnail.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 56be914b8..a5939a7d3 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -41,6 +41,16 @@ def run(self, info): 'Skipping embedding the thumbnail because the file is missing.') return [], info + if not os.path.splitext(encodeFilename(thumbnail_filename))[1].lower() in ['.jpg', '.png']: + jpg_thumbnail_filename = thumbnail_filename + ".jpg" + + self._downloader.to_screen('[ffmpeg] Converting thumbnail "%s" to JPEG' % thumbnail_filename) + + self.run_ffmpeg(thumbnail_filename, jpg_thumbnail_filename, ['-bsf:v', 'mjpeg2jpeg']) + + os.remove(thumbnail_filename) + thumbnail_filename = jpg_thumbnail_filename + if info['ext'] == 'mp3': options = [ '-c', 'copy', '-map', '0', '-map', '1', From e987deb504565102bb6dc271b074781434a75e5c Mon Sep 17 00:00:00 2001 From: Alex Merkel Date: Sun, 21 Jun 2020 11:53:22 +0200 Subject: [PATCH 2/5] [postprocessor/embedthumbnail] Add detection for mislabeled WebP files --- youtube_dl/postprocessor/embedthumbnail.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index a5939a7d3..74928be55 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -41,8 +41,19 @@ def run(self, info): 'Skipping embedding the thumbnail because the file is missing.') return [], info - if not os.path.splitext(encodeFilename(thumbnail_filename))[1].lower() in ['.jpg', '.png']: - jpg_thumbnail_filename = thumbnail_filename + ".jpg" + #Check for mislabeled webp file + with open(encodeFilename(thumbnail_filename), "rb") as f: + b = f.read(16) + if b'\x57\x45\x42\x50' in b: #Binary for WEBP + [thumbnail_filename_path, thumbnail_filename_extension] = os.path.splitext(thumbnail_filename) + if not thumbnail_filename_extension == ".webp": + webp_thumbnail_filename = thumbnail_filename_path + ".webp" + os.rename(thumbnail_filename, webp_thumbnail_filename) + thumbnail_filename = webp_thumbnail_filename + + #If not a jpg or png thumbnail, convert it to jpg using ffmpeg + if not os.path.splitext(thumbnail_filename)[1].lower() in ['.jpg', '.png']: + jpg_thumbnail_filename = os.path.splitext(thumbnail_filename)[0] + ".jpg" self._downloader.to_screen('[ffmpeg] Converting thumbnail "%s" to JPEG' % thumbnail_filename) From ac0ad4f91dbf4e82c22a8fd059891f0b5c138f0d Mon Sep 17 00:00:00 2001 From: Alex Merkel Date: Sun, 21 Jun 2020 12:06:01 +0200 Subject: [PATCH 3/5] [postprocessor/embedthumbnail] Close file before possible renaming --- youtube_dl/postprocessor/embedthumbnail.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index 74928be55..7673b4fd1 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -44,12 +44,12 @@ def run(self, info): #Check for mislabeled webp file with open(encodeFilename(thumbnail_filename), "rb") as f: b = f.read(16) - if b'\x57\x45\x42\x50' in b: #Binary for WEBP - [thumbnail_filename_path, thumbnail_filename_extension] = os.path.splitext(thumbnail_filename) - if not thumbnail_filename_extension == ".webp": - webp_thumbnail_filename = thumbnail_filename_path + ".webp" - os.rename(thumbnail_filename, webp_thumbnail_filename) - thumbnail_filename = webp_thumbnail_filename + if b'\x57\x45\x42\x50' in b: #Binary for WEBP + [thumbnail_filename_path, thumbnail_filename_extension] = os.path.splitext(thumbnail_filename) + if not thumbnail_filename_extension == ".webp": + webp_thumbnail_filename = thumbnail_filename_path + ".webp" + os.rename(thumbnail_filename, webp_thumbnail_filename) + thumbnail_filename = webp_thumbnail_filename #If not a jpg or png thumbnail, convert it to jpg using ffmpeg if not os.path.splitext(thumbnail_filename)[1].lower() in ['.jpg', '.png']: From 6011dd9539eae03d78246db5c320f29607871d43 Mon Sep 17 00:00:00 2001 From: Alex Merkel Date: Sun, 21 Jun 2020 12:16:45 +0200 Subject: [PATCH 4/5] [postprocessor/embedthumbnail] Fix comments to make flake8 happy --- 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 7673b4fd1..ebf7ea27b 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -41,17 +41,17 @@ def run(self, info): 'Skipping embedding the thumbnail because the file is missing.') return [], info - #Check for mislabeled webp file + # Check for mislabeled webp file with open(encodeFilename(thumbnail_filename), "rb") as f: b = f.read(16) - if b'\x57\x45\x42\x50' in b: #Binary for WEBP + if b'\x57\x45\x42\x50' in b: # Binary for WEBP [thumbnail_filename_path, thumbnail_filename_extension] = os.path.splitext(thumbnail_filename) if not thumbnail_filename_extension == ".webp": webp_thumbnail_filename = thumbnail_filename_path + ".webp" os.rename(thumbnail_filename, webp_thumbnail_filename) thumbnail_filename = webp_thumbnail_filename - #If not a jpg or png thumbnail, convert it to jpg using ffmpeg + # If not a jpg or png thumbnail, convert it to jpg using ffmpeg if not os.path.splitext(thumbnail_filename)[1].lower() in ['.jpg', '.png']: jpg_thumbnail_filename = os.path.splitext(thumbnail_filename)[0] + ".jpg" From f6513e1a9302ab601460133804e0c06f1595279a Mon Sep 17 00:00:00 2001 From: Alex Merkel Date: Tue, 23 Jun 2020 10:25:04 +0200 Subject: [PATCH 5/5] [postprocessor/embedthumbnail] Replace % with _ in ffmpeg image output path --- youtube_dl/postprocessor/embedthumbnail.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/youtube_dl/postprocessor/embedthumbnail.py b/youtube_dl/postprocessor/embedthumbnail.py index ebf7ea27b..e2002ab0b 100644 --- a/youtube_dl/postprocessor/embedthumbnail.py +++ b/youtube_dl/postprocessor/embedthumbnail.py @@ -48,18 +48,19 @@ def run(self, info): [thumbnail_filename_path, thumbnail_filename_extension] = os.path.splitext(thumbnail_filename) if not thumbnail_filename_extension == ".webp": webp_thumbnail_filename = thumbnail_filename_path + ".webp" - os.rename(thumbnail_filename, webp_thumbnail_filename) + os.rename(encodeFilename(thumbnail_filename), encodeFilename(webp_thumbnail_filename)) thumbnail_filename = webp_thumbnail_filename # If not a jpg or png thumbnail, convert it to jpg using ffmpeg if not os.path.splitext(thumbnail_filename)[1].lower() in ['.jpg', '.png']: jpg_thumbnail_filename = os.path.splitext(thumbnail_filename)[0] + ".jpg" + jpg_thumbnail_filename = os.path.join(os.path.dirname(jpg_thumbnail_filename), os.path.basename(jpg_thumbnail_filename).replace('%', '_')) # ffmpeg interprets % as image sequence self._downloader.to_screen('[ffmpeg] Converting thumbnail "%s" to JPEG' % thumbnail_filename) self.run_ffmpeg(thumbnail_filename, jpg_thumbnail_filename, ['-bsf:v', 'mjpeg2jpeg']) - os.remove(thumbnail_filename) + os.remove(encodeFilename(thumbnail_filename)) thumbnail_filename = jpg_thumbnail_filename if info['ext'] == 'mp3':