From 337e0c62f894722e9c268b14d02a85b84c96024d Mon Sep 17 00:00:00 2001 From: pukkandan Date: Sat, 29 May 2021 01:39:07 +0530 Subject: [PATCH] [embedthumbnail] Correctly escape filename Closes #352 The approach in [1] is faulty as can be seen in the test cases 1. https://github.com/ytdl-org/youtube-dl/commit/bff857a8af696e701482208617bf0b7564951326 --- .gitignore | 85 +++++++++--------- test/test_postprocessors.py | 27 +++++- .../thumbnails/foo %d bar/foo_%d.webp | Bin 0 -> 3928 bytes yt_dlp/postprocessor/embedthumbnail.py | 2 +- yt_dlp/postprocessor/ffmpeg.py | 17 ++-- 5 files changed, 75 insertions(+), 56 deletions(-) create mode 100644 test/testdata/thumbnails/foo %d bar/foo_%d.webp diff --git a/.gitignore b/.gitignore index a2484b752..b6431b766 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,46 @@ +# Config +*.conf +*.spec +cookies +cookies.txt + +# Downloaded +*.srt +*.ttml +*.sbv +*.vtt +*.flv +*.mp4 +*.m4a +*.m4v +*.mp3 +*.3gp +*.webm +*.wav +*.ape +*.mkv +*.swf +*.part +*.part-* +*.ytdl +*.dump +*.frag +*.frag.urls +*.aria2 +*.swp +*.ogg +*.opus +*.info.json +*.live_chat.json +*.jpg +*.png +*.webp +*.annotations.xml +*.description + +# Allow config/media files in testdata +!test/testdata/** + # Python *.pyc *.pyo @@ -43,48 +86,6 @@ README.txt yt-dlp.zip *.exe -# Downloaded -*.srt -*.ttml -*.sbv -*.vtt -*.flv -*.mp4 -*.m4a -*.m4v -*.mp3 -*.3gp -*.webm -*.wav -*.ape -*.mkv -*.swf -*.part -*.part-* -*.ytdl -*.dump -*.frag -*.frag.urls -*.aria2 -*.swp -*.ogg -*.opus -*.info.json -*.live_chat.json -*.jpg -*.png -*.webp -*.annotations.xml -*.description - -# Config -*.conf -*.spec -cookies -cookies.txt - - - # Text Editor / IDE .idea *.iml diff --git a/test/test_postprocessors.py b/test/test_postprocessors.py index 7574a0b95..868bb25f9 100644 --- a/test/test_postprocessors.py +++ b/test/test_postprocessors.py @@ -8,7 +8,11 @@ import sys import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from yt_dlp.postprocessor import MetadataFromFieldPP, MetadataFromTitlePP +from yt_dlp.postprocessor import ( + FFmpegThumbnailsConvertorPP, + MetadataFromFieldPP, + MetadataFromTitlePP, +) class TestMetadataFromField(unittest.TestCase): @@ -30,3 +34,24 @@ class TestMetadataFromTitle(unittest.TestCase): def test_format_to_regex(self): pp = MetadataFromTitlePP(None, '%(title)s - %(artist)s') self.assertEqual(pp._titleregex, r'(?P.+)\ \-\ (?P<artist>.+)') + + +class TestConvertThumbnail(unittest.TestCase): + def test_escaping(self): + pp = FFmpegThumbnailsConvertorPP() + if not pp.available: + print('Skipping: ffmpeg not found') + return + + file = 'test/testdata/thumbnails/foo %d bar/foo_%d.{}' + tests = (('webp', 'png'), ('png', 'jpg')) + + for inp, out in tests: + out_file = file.format(out) + if os.path.exists(out_file): + os.remove(out_file) + pp.convert_thumbnail(file.format(inp), out) + assert os.path.exists(out_file) + + for _, out in tests: + os.remove(file.format(out)) diff --git a/test/testdata/thumbnails/foo %d bar/foo_%d.webp b/test/testdata/thumbnails/foo %d bar/foo_%d.webp new file mode 100644 index 0000000000000000000000000000000000000000..d64d0839f054071849aa12f194b8b20b19e6bb59 GIT binary patch literal 3928 zcmb`~_dgVX;|B0EvWr7mA)$~Jm7J}!$vn<Fd#`L~T_VZ8%XUaE>+C%bAtz*&5!t#N zM><8u`F>xo&+GFae4byP=lv%<W_sG%CaeH}m6oQ7rOD%4;Q!xKPJkjRiB+nHRH{hb zw}zq^;oCyL^%xr189n^h!>E7)ILKgM{C&GlLaE?Cfjmla3>)I?=d&Zkv^~-GLWfe{ zK>u^3wjFzU%IaXRpvHx8A_<<Y{@M5G^Mb>BxfTx=;c=8V{e2M1TL%=v@KU&^381D# z$)%j_5oUzXGr*LID8MsK6PkZ&CmB!FDx)bE3YYB{I{g9U08+2VWr#`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^cQzI1Eb2+rjgEe*9TZB|CF66SI_09)tqv95Sz%Hya^-egh zTm18|Rdqv`aVvB5PsJT`QiXdd6UHQjz#I`{TgD&C4?Q;>J!3-Wr$=Q!NgEAsYMrsZ zj*DFL`U7%Dyo+@b6#L#{e(1bdcy#o<Ak!%ZvRS2$@4Bi#P*oW-k|yK}FMfxfN#N1o z%gQvOTaG^?MpSU8am<|u$nluVdBR}G7p6Sq=lL01O1`EO=vUj(n<b5=MuN-)g|R~L z!r(^GWTxTOS1`k`&_-$@4Ti{ni1y6kfEVSUfvqxW&#n%>b=VU*)PUvC)t7R`*mkFq zjJs{iz-Rc<idRLQ8Of8EwXqdKy&uenc&d({;$GR$e;3m4Q%W`>CNt23{KZ!Tf;Kat bLpo5mJGW#CT`nX2$#Kl+y#GFf|MdR>k&2#! literal 0 HcmV?d00001 diff --git a/yt_dlp/postprocessor/embedthumbnail.py b/yt_dlp/postprocessor/embedthumbnail.py index f3eb7d96d..278a45eb6 100644 --- a/yt_dlp/postprocessor/embedthumbnail.py +++ b/yt_dlp/postprocessor/embedthumbnail.py @@ -70,7 +70,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): self.to_screen('There aren\'t any thumbnails to embed') return [], info - idx = next((-(i+1) for i, t in enumerate(info['thumbnails'][::-1]) if t.get('filepath')), None) + idx = next((-i for i, t in enumerate(info['thumbnails'][::-1], 1) if t.get('filepath')), None) if idx is None: self.to_screen('There are no thumbnails on disk') return [], info diff --git a/yt_dlp/postprocessor/ffmpeg.py b/yt_dlp/postprocessor/ffmpeg.py index ea728be37..d9f816b04 100644 --- a/yt_dlp/postprocessor/ffmpeg.py +++ b/yt_dlp/postprocessor/ffmpeg.py @@ -853,19 +853,12 @@ class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor): return [] def convert_thumbnail(self, thumbnail_filename, target_ext): - # 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)) - escaped_thumbnail_conv_filename = replace_extension(escaped_thumbnail_filename, target_ext) - - self.to_screen('Converting thumbnail "%s" to %s' % (escaped_thumbnail_filename, target_ext)) - self.run_ffmpeg(escaped_thumbnail_filename, escaped_thumbnail_conv_filename, self._options(target_ext)) - - # Rename back to unescaped thumbnail_conv_filename = replace_extension(thumbnail_filename, target_ext) - os.rename(encodeFilename(escaped_thumbnail_filename), encodeFilename(thumbnail_filename)) - os.rename(encodeFilename(escaped_thumbnail_conv_filename), encodeFilename(thumbnail_conv_filename)) + + self.to_screen('Converting thumbnail "%s" to %s' % (thumbnail_filename, target_ext)) + self.real_run_ffmpeg( + [(thumbnail_filename, ['-f', 'image2', '-pattern_type', 'none'])], + [(thumbnail_conv_filename.replace('%', '%%'), self._options(target_ext))]) return thumbnail_conv_filename def run(self, info):