From 777d5a45be81fb1f274c8c558ba1eb24855c66fc Mon Sep 17 00:00:00 2001
From: Alex Merkel <mail@alexmerkel.com>
Date: Thu, 18 Jun 2020 22:36:44 +0200
Subject: [PATCH 1/6] [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 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
                 '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 <mail@alexmerkel.com>
Date: Sun, 21 Jun 2020 11:53:22 +0200
Subject: [PATCH 2/6] [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 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
                 '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 <mail@alexmerkel.com>
Date: Sun, 21 Jun 2020 12:06:01 +0200
Subject: [PATCH 3/6] [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 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
         #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 <mail@alexmerkel.com>
Date: Sun, 21 Jun 2020 12:16:45 +0200
Subject: [PATCH 4/6] [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 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
                 '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 <mail@alexmerkel.com>
Date: Tue, 23 Jun 2020 10:25:04 +0200
Subject: [PATCH 5/6] [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 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
             [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':

From ba39289d79452650263cc6e7950d41fba8dcef8e Mon Sep 17 00:00:00 2001
From: tfvlrue <35318734+tfvlrue@users.noreply.github.com>
Date: Fri, 28 Aug 2020 12:00:30 -0400
Subject: [PATCH 6/6] Resolve audio/x-wav to "wav" extension, rather than
 "x-wav" (which ffmpeg does not recognize)

This was causing ffmpeg errors when trying to downloading certain audio files from Soundcloud with the --add-metadata option set.
---
 youtube_dl/utils.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index d1eca3760..c73f5e0ca 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -4173,6 +4173,7 @@ def mimetype2ext(mt):
         # Per RFC 3003, audio/mpeg can be .mp1, .mp2 or .mp3. Here use .mp3 as
         # it's the most popular one
         'audio/mpeg': 'mp3',
+        'audio/x-wav': 'wav',
     }.get(mt)
     if ext is not None:
         return ext