Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[postprocessor/embedthumbnail] Fix Youtube's WebP thumbnails by converting them to JPG #25717

Closed
wants to merge 5 commits into from

Conversation

alexmerkel
Copy link
Contributor

Before submitting a pull request make sure you have:

In order to be accepted and merged into youtube-dl each piece of code must be in public domain or released under Unlicense. Check one of the following options:

  • I am the original author of this code and I am willing to release it under Unlicense
  • I am not the original author of this code but it is in public domain or released under Unlicense (provide reliable evidence)

What is the purpose of your pull request?

  • Bug fix
  • Improvement
  • New extractor
  • New feature

Description of your pull request and other information

This change should fix #25687:
Youtube started using the WebP image format for thumbnail images. However, both ffmpeg and AtomicParsley only support the embedding of JPEG and PNG files. Therefore, as @noname8753 suggested, a conversion step was added during which the thumbnail is converted to JPEG using ffmpeg.

@empyrical
Copy link

This fix works for me! One thing that might need to be tweaked is to have it strip the original thumbnail extension when converting - right now it leaves a file thumbnail.webp.jpg, and it could instead name it thumbnail.jpg

@alexmerkel
Copy link
Contributor Author

Thanks @empyrical, I changed the file name.
Additionally I added a detection for WebP files that are mislabeled as JPG by Youtube (see @koloshor comment here).

@CrypticSignal
Copy link

CrypticSignal commented Jun 22, 2020

I cloned the repo, added your "fix" and then ran youtube-dl with python3 -m youtube_dl but the same issue persists. Here's an example of the terminal output:

pi@raspberrypi:~/website $ python3 -m youtube_dl -x --embed-thumbnail --audio-format mp3 https://www.youtube.com/watch?v=gi9Bvj0JIrw
[youtube] gi9Bvj0JIrw: Downloading webpage
[youtube] gi9Bvj0JIrw: Downloading thumbnail ...
[youtube] gi9Bvj0JIrw: Writing thumbnail to: Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webp
[download] Destination: Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webm
[download] 100% of 3.71MiB in 00:01
[ffmpeg] Destination: Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.mp3
Deleting original file Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webm (pass -k to keep)
[ffmpeg] Adding thumbnail to "Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.mp3"
ERROR: Conversion failed!

@alexmerkel
Copy link
Contributor Author

I cloned the repo, added your "fix" and then ran youtube-dl with python3 -m youtube_dl but the same issue persists.

Are you sure that you applied the patch correctly? I added a log message when a thumbnail conversion takes place and it is missing from your output.
This is the terminal output I get when running the same command as you with the patch applied:

$ python3 -m youtube_dl -x --embed-thumbnail --audio-format mp3 https://www.youtube.com/watch?v=gi9Bvj0JIrw
[youtube] gi9Bvj0JIrw: Downloading webpage
[youtube] gi9Bvj0JIrw: Downloading js player 1d33781a
[youtube] gi9Bvj0JIrw: Downloading js player 1d33781a
[youtube] gi9Bvj0JIrw: Downloading thumbnail ...
[youtube] gi9Bvj0JIrw: Writing thumbnail to: Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webp
[download] Destination: Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webm
[download] 100% of 3.71MiB in 00:01
[ffmpeg] Destination: Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.mp3
Deleting original file Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webm (pass -k to keep)
[ffmpeg] Converting thumbnail "Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webp" to JPEG
[ffmpeg] Adding thumbnail to "Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.mp3"

As you can see, there is an additional message ffmpeg] Converting thumbnail "Mikebøi - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webp" to JPEG that is missing from your log.

@CrypticSignal
Copy link

@alexmerkel I applied the fix correctly, the issue was that it wasn't running the version with the fix added, it was running the version I had previously installed using pip3. All is good now.

@njoynj
Copy link

njoynj commented Jun 23, 2020

An error occurs when there is "%" in the file title

@alexmerkel
Copy link
Contributor Author

Oh, good catch @njoynj. ffmpeg interpreted % as an image sequence and got confused. I now replace % with _ in the thumbnail file name, this fixes the issue.

@OzzyHelix
Copy link

I'd like to compile a binary of of Alex's patch but there is no instruction on how to compile it to a Windows exe binary it would be handy if someone wrote a wiki on how to do that but that is a side tangent

@CrypticSignal
Copy link

For those who want to easily install youtube-dl with the fix implemented, you can do:
pip install git+https://github.com/ytdl-org/youtube-dl.git@refs/pull/25717/head

@du-song du-song mentioned this pull request Jul 12, 2020
6 tasks
@CoWinkKeyDinkInc
Copy link

I used the youtube-dl version that @BassThatHertz suggested and it works perfectly, after I cut out all the affected videos from my music playlist from archive.txt, it was able to overwrite all the old content and the thumbnails work! 👍

@sist1998
Copy link

sist1998 commented Jul 14, 2020

Hi,
Here is my output with your patch.
Any ideas ? Thank you !

StefSuse:/home/stefan # python3 -m youtube_dl --verbose --extract-audio --no-mtime --audio-quality 4 --embed-thumbnail --audio-format mp3 -o test.mp3 https://www.youtube.com/watch?v=Phsr8tC0R6w
[debug] System config: []
[debug] User config: []
[debug] Custom config: []
[debug] Command-line args: ['--verbose', '--extract-audio', '--no-mtime', '--audio-quality', '4', '--embed-thumbnail', '--audio-format', 'mp3', '-o', 'test.mp3', 'https://www.youtube.com/watch?v=Phsr8tC0R6w']
[debug] Encodings: locale UTF-8, fs utf-8, out utf-8, pref UTF-8
[debug] youtube-dl version 2020.06.16.1
[debug] Python version 3.8.3 (CPython) - Linux-5.7.5-1-default-x86_64-with-glibc2.2.5
[debug] exe versions: ffmpeg 4.2.3, ffprobe 4.2.3
[debug] Proxy map: {}
[youtube] Phsr8tC0R6w: Downloading webpage
[youtube] {18} signature length 102, html5 player 3662280c
[youtube] {137} signature length 106, html5 player 3662280c
[youtube] {248} signature length 106, html5 player 3662280c
[youtube] {399} signature length 102, html5 player 3662280c
[youtube] {136} signature length 102, html5 player 3662280c
[youtube] {247} signature length 106, html5 player 3662280c
[youtube] {398} signature length 102, html5 player 3662280c
[youtube] {135} signature length 102, html5 player 3662280c
[youtube] {244} signature length 102, html5 player 3662280c
[youtube] {397} signature length 102, html5 player 3662280c
[youtube] {134} signature length 106, html5 player 3662280c
[youtube] {243} signature length 106, html5 player 3662280c
[youtube] {396} signature length 106, html5 player 3662280c
[youtube] {133} signature length 102, html5 player 3662280c
[youtube] {242} signature length 106, html5 player 3662280c
[youtube] {395} signature length 106, html5 player 3662280c
[youtube] {160} signature length 106, html5 player 3662280c
[youtube] {278} signature length 102, html5 player 3662280c
[youtube] {394} signature length 106, html5 player 3662280c
[youtube] {140} signature length 102, html5 player 3662280c
[youtube] {249} signature length 106, html5 player 3662280c
[youtube] {250} signature length 106, html5 player 3662280c
[youtube] {251} signature length 106, html5 player 3662280c
[youtube] Phsr8tC0R6w: Downloading thumbnail ...
[youtube] Phsr8tC0R6w: Writing thumbnail to: test.webp
[debug] Invoking downloader on 'https://r2---sn-1gi7znes.googlevideo.com/videoplayback?expire=1594708795&ei=2_4MX6-1D9XqgAeFqJTIAw&ip=84.74.236.91&id=o-AJekJH4GgIM7PnQSZ6hNb1XZj4YSe48shhQz_o81wfdH&itag=251&source=youtub e&requiressl=yes&mh=0N&mm=31%2C26&mn=sn-1gi7znes%2Csn-4g5ednld&ms=au%2Conr&mv=m&mvi=2&pl=19&initcwndbps=1981250&vprv=1&mime=audio%2Fwebm&gir=yes&clen=2888440&dur=159.181&lmt=1593246369017324&mt=1594687114&fvip=2&keepali ve=yes&c=WEB&txp=5531432&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIgaQhX5jy3ADFJhGo3NRHcLm vQxMRCiEBIkAV0zI6OWR4CIQCpvlYBP0khzbQV3N-flbeg2DJOyTP_JWHUZvP6-ZBkfw%3D%3D&sig=AOq0QJ8wRQIgTPfKR0PlX_fsL4fF7lZvT7lGdVc3HKYozBCD2o9ieoUCIQDqyz-kaoP2miNF5IDW6uKLD09tJvROvTxTaoqDtVXVxw==&ratebypass=yes'
[download] test.mp3 has already been downloaded
[download] 100% of 2.93MiB
[debug] ffmpeg command line: ffprobe -show_streams file:test.mp3
[ffmpeg] Post-process file test.mp3 exists, skipping
[ffmpeg] Converting thumbnail "test.webp" to JPEG
[debug] ffmpeg command line: ffmpeg -y -loglevel repeat+info -i file:test.webp -bsf:v mjpeg2jpeg file:test.jpg
[ffmpeg] Adding thumbnail to "test.mp3"
[debug] ffmpeg command line: ffmpeg -y -loglevel repeat+info -i file:test.mp3 -i file:test.jpg -c copy -map 0 -map 1 -metadata:s:v 'title="Album cover"' -metadata:s:v 'comment="Cover (Front)"' file:test.temp.mp3
ERROR: Stream #1:0 -> #0:1 (copy)
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 2065, in post_process
files_to_delete, info = pp.run(info)
File "/usr/local/lib/python3.8/site-packages/youtube_dl/postprocessor/embedthumbnail.py", line 73, in run
self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options)
File "/usr/local/lib/python3.8/site-packages/youtube_dl/postprocessor/ffmpeg.py", line 235, in run_ffmpeg_multiple_files
raise FFmpegPostProcessorError(msg)
youtube_dl.postprocessor.ffmpeg.FFmpegPostProcessorError: Stream #1:0 -> #0:1 (copy)

@adithya-s-sekhar
Copy link

adithya-s-sekhar commented Jul 14, 2020

Can confirm @Labud98 's problem happens when you specify the output filename.

Using output templates and not specifying a filename works fine.

@adithya-s-sekhar
Copy link

More about the bug @Labud98 found

From original comment

[download] 100% of 2.93MiB
[debug] ffmpeg command line: ffprobe -show_streams file:test.mp3
[ffmpeg] Post-process file test.mp3 exists, skipping

youtube-dl seems to save the downloaded opus file as test.mp3 and skips converting the audio to actual mp3 format. This breaks ffmpeg when it tries to look for mp3 streams in an opus file.

Input #0, matroska,webm, from 'file:test.mp3':
Metadata:
encoder : google/video-file
Duration: 00:02:39.18, start: -0.007000, bitrate: 145 kb/s
Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
Input #1, image2, from 'file:test.jpg':
Duration: 00:00:00.04, start: 0.000000, bitrate: 18816 kb/s
Stream #1:0: Video: mjpeg (Baseline), yuvj420p(pc, bt470bg/unknown/unknown), 1280x720, 25 tbr, 25 tbn, 25 tbc
[mp3 @ 0000024506284dc0] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

I'm not that good at python to fix this but hope this helps.

@michaelb
Copy link

michaelb commented Jul 15, 2020

StefSuse:/home/stefan # python3 -m youtube_dl --verbose --extract-audio --no-mtime --audio-quality 4 --embed-thumbnail --audio-format mp3 -o test.mp3 https://www.youtube.com/watch?v=Phsr8tC0R6w

Using the unpatched (unrelated to @alexmerkel PR) youtube-dl version, I can confirm this ^ is a different bug.
Another issue should be opened.

(my output:
$ youtube-dl --verbose --extract-audio --no-mtime --audio-quality 4 --embed-thumbnail --audio-format mp3 -o test.mp3 https://www.youtube.com/watch\?v\=Phsr8tC0R6w

[debug] System config: []
[debug] User config: []
[debug] Custom config: []
[debug] Command-line args: ['--verbose', '--extract-audio', '--no-mtime', '--audio-quality', '4', '--embed-thumbnail', '--audio-format', 'mp3', '-o', 'test.mp3', 'https://www.youtube.com/watch?v=Phsr8tC0R6w']
[debug] Encodings: locale UTF-8, fs utf-8, out utf-8, pref UTF-8
[debug] youtube-dl version 2020.06.16.1
[debug] Python version 3.8.3 (CPython) - Linux-5.7.7-arch1-1-x86_64-with-glibc2.2.5
[debug] exe versions: ffmpeg 4.2.3, ffprobe 4.2.3, rtmpdump 2.4
[debug] Proxy map: {}
[youtube] Phsr8tC0R6w: Downloading webpage
[youtube] {18} signature length 107, html5 player 5253ac4d
[youtube] {137} signature length 107, html5 player 5253ac4d
[youtube] {248} signature length 107, html5 player 5253ac4d
[youtube] {399} signature length 103, html5 player 5253ac4d
[youtube] {136} signature length 107, html5 player 5253ac4d
[youtube] {247} signature length 107, html5 player 5253ac4d
[youtube] {398} signature length 107, html5 player 5253ac4d
[youtube] {135} signature length 107, html5 player 5253ac4d
[youtube] {244} signature length 107, html5 player 5253ac4d
[youtube] {397} signature length 107, html5 player 5253ac4d
[youtube] {134} signature length 107, html5 player 5253ac4d
[youtube] {243} signature length 107, html5 player 5253ac4d
[youtube] {396} signature length 107, html5 player 5253ac4d
[youtube] {133} signature length 107, html5 player 5253ac4d
[youtube] {242} signature length 107, html5 player 5253ac4d
[youtube] {395} signature length 103, html5 player 5253ac4d
[youtube] {160} signature length 107, html5 player 5253ac4d
[youtube] {278} signature length 107, html5 player 5253ac4d
[youtube] {394} signature length 103, html5 player 5253ac4d
[youtube] {140} signature length 107, html5 player 5253ac4d
[youtube] {249} signature length 107, html5 player 5253ac4d
[youtube] {250} signature length 107, html5 player 5253ac4d
[youtube] {251} signature length 107, html5 player 5253ac4d
[youtube] Phsr8tC0R6w: Downloading thumbnail ...
[youtube] Phsr8tC0R6w: Writing thumbnail to: test.webp
[debug] Invoking downloader on 'https://r2---sn-hgn7yn7e.googlevideo.com/videoplayback?expire=1594835573&ei=Fe4OX4i5C4mAxN8Pu9OM8A4&ip=2a01%3Acb1d%3A189%3A6600%3A9ead%3A97ff%3Afea3%3Ab285&id=o-ANEU-lylBuNe4Vf3lZ2agoxTtwatlCRmPiEOkzFdNRZy&itag=251&source=youtube&requiressl=yes&mh=0N&mm=31%2C26&mn=sn-hgn7yn7e%2Csn-4g5e6nze&ms=au%2Conr&mv=m&mvi=2&pl=30&initcwndbps=910000&vprv=1&mime=audio%2Fwebm&gir=yes&clen=2888440&dur=159.181&lmt=1593246369017324&mt=1594813833&fvip=2&keepalive=yes&beids=9466585&c=WEB&txp=5531432&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgSHVN0o5i4Gd267PtwR9JB43GuvytvK0txIgOqNoe-scCIBdtIfJT091FyozfDeUN2fjS89qEHi7-OI55KacG7OKT&sig=AOq0QJ8wRQIhAJnbED9VqaKixsvwjYVHRv2NgInaYiByjzOaFvqoBEUQAiAZqR_oy-I5S1c-8QZ7lx_1UfdHzCGD_ExnzdRVtgk1cQ==&ratebypass=yes'
[download] Destination: test.mp3
[download] 100% of 2.75MiB in 00:03
[debug] ffmpeg command line: ffprobe -show_streams file:test.mp3
[ffmpeg] Post-process file test.mp3 exists, skipping
[ffmpeg] Adding thumbnail to "test.mp3"
[debug] ffmpeg command line: ffmpeg -y -loglevel repeat+info -i file:test.mp3 -i file:test.webp -c copy -map 0 -map 1 -metadata:s:v 'title="Album cover"' -metadata:s:v 'comment="Cover (Front)"' file:test.temp.mp3
ERROR: Stream #1:0 -> #0:1 (copy)
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 2065, in post_process
files_to_delete, info = pp.run(info)
File "/usr/lib/python3.8/site-packages/youtube_dl/postprocessor/embedthumbnail.py", line 51, in run
self.run_ffmpeg_multiple_files([filename, thumbnail_filename], temp_filename, options)
File "/usr/lib/python3.8/site-packages/youtube_dl/postprocessor/ffmpeg.py", line 235, in run_ffmpeg_multiple_files
raise FFmpegPostProcessorError(msg)
youtube_dl.postprocessor.ffmpeg.FFmpegPostProcessorError: Stream #1:0 -> #0:1 (copy)

)

The PR looks clean enough to me.
Given how bad it breaks --embed-thumbnail for a lot of youtube videos (every new one, if not all), can it be considered for merge?

@Haafingar
Copy link

Youtube-dl is broken for a very large number of use cases because of this bug. No new issues with the PR have been found in 2 weeks, and it's pretty small and straightforward. I hate to be pushy, but why hasn't this been merged?

@reallyuniquename
Copy link

reallyuniquename commented Jul 16, 2020

Somewhat related: #24882

ffmpeg now supports thumbnail embedding for MP4 video files.

@Mhowser
Copy link

Mhowser commented Jul 17, 2020

Could we get a merge please @dstftw 😀

AvinashReddy3108 pushed a commit to AvinashReddy3108/PaperplaneRemix that referenced this pull request Jul 21, 2020
NOTE: The thumbnail embedding part is the cause of many conversion failures, and [this](ytdl-org/youtube-dl#25717) PR can probably fix it.
@The-neophyte The-neophyte mentioned this pull request Jul 22, 2020
6 tasks
@silariel
Copy link

I just downloaded youtube-dl after youtube-dlg stopped working so I might be doing this wrong, but here's the output that I got with the same command:

youtube-dl -x --embed-thumbnail --audio-format mp3 https://www.youtube.com/watch?v=gi9Bvj0JIrw
[youtube] gi9Bvj0JIrw: Downloading webpage
[youtube] gi9Bvj0JIrw: Downloading thumbnail ...
[youtube] gi9Bvj0JIrw: Writing thumbnail to: Mikeb°i - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webp
[download] Destination: Mikeb°i - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webm
[download] 100% of 3.71MiB in 00:00
[ffmpeg] Destination: Mikeb°i - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.mp3
Deleting original file Mikeb°i - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webm (pass -k to keep)
[ffmpeg] Converting thumbnail "Mikeb°i - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.webp" to JPEG
[ffmpeg] Adding thumbnail to "Mikeb°i - Heavy Lungs (ft. SRU)-gi9Bvj0JIrw.mp3"

It looks like it should work but the mp3 file still doesn't have the thumbnail embedded?

@alexmerkel
Copy link
Contributor Author

@silariel Unfortunately I can't reproduce your issue. When I run your command, the resulting file has the embedded thumbnail. Have you tried opening the MP3 with a different program to see if the thumbnail appears there?

@CrypticSignal
Copy link

CrypticSignal commented Jul 24, 2020

What is different about the way thumbnails are added with youtube-dl to, say, official cover art of a song purchased from iTunes? Because I have noticed that the cover art added with youtube-dl does not show up in Groove Music and Windows Media Player but cover art of music that I have purchased does.

@silariel If you're on Windows, try foobar2000. It shows the cover art. I also like the fact that it shows the bitrate in realtime (at the bottom-right) for VBR files. See the image below. As you can see, it shows the cover art.

image

@alexmerkel
Copy link
Contributor Author

According to this stackoverflow answer, it might be because the ffmpeg command uses comment="Cover (Front)" instead of comment="Cover (front)" and the -id3v2_version 3 flag is missing. But I'm unable to test this as I don't have a Win machine handy atm.

@CrypticSignal
Copy link

CrypticSignal commented Jul 24, 2020

@alexmerkel I'm trying to test this myself by adding -id3v2_version 3. I'm guessing you simply need to change:
self.run_ffmpeg(thumbnail_filename, jpg_thumbnail_filename, ['-bsf:v', 'mjpeg2jpeg'])
to
self.run_ffmpeg(thumbnail_filename, jpg_thumbnail_filename, ['-bsf:v', 'mjpeg2jpeg', 'id3v2_version', '3'])?

I'm getting the following error when running youtube-dl with python -m though:

C:\Users\H\Desktop\youtube-dl>python -m youtube_dl -x --embed-thumbnail --audio-format mp3 --audio-quality 0 https://www.youtube.com/watch?v=Y25awNvNB4A -o C:/Users/H/Desktop/Test.mp3
[youtube] Y25awNvNB4A: Downloading webpage
[youtube] Y25awNvNB4A: Downloading MPD manifest
[youtube] Y25awNvNB4A: Downloading thumbnail ...
[youtube] Y25awNvNB4A: Writing thumbnail to: C:\Users\H\Desktop\Test.jpg
[dashsegments] Total fragments: 17
[download] Destination: C:\Users\H\Desktop\Test.mp3
[download] 100% of 2.56MiB in 00:02
[ffmpeg] Post-process file C:\Users\H\Desktop\Test.mp3 exists, skipping
[ffmpeg] Converting thumbnail "C:\Users\H\Desktop\Test.webp" to JPEG
[ffmpeg] Adding thumbnail to "C:\Users\H\Desktop\Test.mp3"
ERROR:   Stream #1:0 -> #0:1 (copy)

ERROR: Stream #1:0 -> #0:1 (copy)

I get this error even without adding the -id3v2_version 3, the only change I made to embedthumbnail.py was changing it to your version of the file with the fix.

@alexmerkel
Copy link
Contributor Author

@BassThatHertz no, the flag has to be added to the ffmpeg command that adds the thumbnail to the MP3 file

options = [
    '-c', 'copy', '-map', '0', '-map', '1',
     '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"']

to

options = [
    '-c', 'copy', '-map', '0', '-map', '1', '-id3v2_version', '3',
     '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (front)"']

The other error should be the unrelated bug that occurs when specifying the output filename that was discussed above.

@CrypticSignal
Copy link

@alexmerkel Just did some testing. Cover (Front) does not need to be changed to a lowercase f, and adding -id3v2_version 3 sorts the issue on Windows.

With regards to the output filename issue, I can see that someone else has brought up the same issue but nobody has explained what's causing that issue.

Copy link

@CrypticSignal CrypticSignal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add '-id3v2_version', '3' to the options list to fix the issue of no cover art being seen when using Groove Music on Windows or Windows Media Player, and perhaps other applications.

@alexmerkel
Copy link
Contributor Author

I'm not sure this should be added to this pull request.
While it certainly needs to be changed, it has nothing to do with the webp files Youtube introduced that this pull request addresses.
Therefore, I would propose creating a new pull request for the addition of the -id3v2_version 3 flag.

@CrypticSignal
Copy link

CrypticSignal commented Jul 25, 2020

@alexmerkel Makes sense. I would issue a new pull request but the issue is that your fix hasn't been merged with the master branch yet, and adding a fix for cover art visibility on some Windows applications doesn't make sense because right now the thumbnails don't even get added to the file (due to being WebP). In other words, with the current youtube-dl code, adding -id3v2_version 3 has no benefit.

I guess I need to wait for your fix to be merged with the master branch, which is taking a while.

@alexmerkel
Copy link
Contributor Author

@Labud98, @adithya-s-sekhar, @michaelb, @BassThatHertz I found the reason for the ERROR: Stream #1:0 -> #0:1 (copy) bug, but it has nothing to do directly with the thumbnails.
When specifying an output file with a .mp3 extension, the conversion step from WEBM or M4A to MP3 is skipped because ffmpeg assumes the audio format is already correct.
In the logs this can be observed by the [ffmpeg] Post-process file test.mp3 exists, skipping message.
Additionally ffprobe shows the webm file format

Input #0, matroska,webm, from 'file:test.mp3':
  Metadata:
    encoder         : google/video-file
  Duration: 00:02:39.22, start: -0.007000, bitrate: 134 kb/s
    Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp (default)

So the file has the extension .mp3 but it isn't really an MP3 file.
The thumbnail-embedding process results in an exception because there ffmpeg is expecting an MP3 stream but is not receiving one and panics.

I think a new issue/pull request should be created to address this problem.

@adithya-s-sekhar
Copy link

adithya-s-sekhar commented Jul 25, 2020

When specifying an output file with a .mp3 extension, the conversion step from WEBM or M4A to MP3 is skipped because ffmpeg assumes the audio format is already correct.
In the logs this can be observed by the [ffmpeg] Post-process file test.mp3 exists, skipping message.
Additionally ffprobe shows the webm file format
...

I've noticed it too. This issue has nothing to do with this thumbnail fix.

@alexmerkel @Labud98 @BassThatHertz
This issue was raised and answered before here .

Basically you shouldn't give .mp3 as an extension and use --audio-format mp3 -o "test.%(ext)s" as the former confuses youtube-dl, no one has figured out a way to fix this unfortunately.

@CrypticSignal
Copy link

@Labud98, @adithya-s-sekhar, @michaelb, @alexmerkel I've submitted a fix for ERROR: Stream #1:0 -> #0:1 (copy) when specifying the mp3 extension for the output filename. Here's the pull request.

@CrypticSignal
Copy link

If possible, you should remove/decline my suggested changes. I don't want anything delaying this fix being merged to the master branch. If/when this fix gets merged, I may submit a pull request which passes -id3v2_version 3 to FFmpeg so that the cover art can be seen when using Groove Music or Windows Media Player.

@silariel
Copy link

@BassThatHertz no, the flag has to be added to the ffmpeg command that adds the thumbnail to the MP3 file

options = [
    '-c', 'copy', '-map', '0', '-map', '1',
     '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (Front)"']

to

options = [
    '-c', 'copy', '-map', '0', '-map', '1', '-id3v2_version', '3',
     '-metadata:s:v', 'title="Album cover"', '-metadata:s:v', 'comment="Cover (front)"']

The other error should be the unrelated bug that occurs when specifying the output filename that was discussed above.

Oops sorry, wasn't keeping track of this thread but this worked like a charm! :) ^ Thanks for the suggestions and help, y'all!

@AvinashReddy3108
Copy link

Add '-id3v2_version', '3' to the options list to fix the issue of no cover art being seen when using Groove Music on Windows or Windows Media Player, and perhaps other applications.

Some older(?) Android devices/apps also needs the '-write_id3v1', '1' to be able to show the album art in their music players.

@CrypticSignal
Copy link

@AvinashReddy3108 FFmpeg only supports ID3v2 versions 3 and 4.

@TeguhTeknisi
Copy link

somehow, 'ffmpeg' in my Kubuntu 18.04 cannot convert webp.
but 'convert' from 'image magick' can convert it ( i need to 'apt install webp' first ), i hope you can make convert as option to convert the webp to jpg.

thanks.

@arytek
Copy link

arytek commented Jul 29, 2020

I'm still having this issue as well when downloading mp4 videos on YouTube. Thumbnail downloads but doesn't embed.
./youtube-dl https://youtu.be/WltJPKFo_J4 -f 137+140 -ciw --add-metadata --embed-subs --embed-thumbnail --write-sub --ffmpeg-location "C:\Program Files\ffmpeg\bin\ffmpeg.exe"

@ytdl-org ytdl-org locked and limited conversation to collaborators Jul 29, 2020
@dstftw dstftw force-pushed the master branch 2 times, most recently from 5e26784 to da2069f Compare September 13, 2020 13:50
Comment on lines +45 to +47
with open(encodeFilename(thumbnail_filename), "rb") as f:
b = f.read(16)
if b'\x57\x45\x42\x50' in b: # Binary for WEBP
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes no sense if extension is not webp, extension must be checked first.

@@ -41,6 +41,28 @@ def run(self, info):
'Skipping embedding the thumbnail because the file is missing.')
return [], info

# Check for mislabeled webp file
with open(encodeFilename(thumbnail_filename), "rb") as f:
b = f.read(16)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebP header occupies 12 bytes, there is no point reading more.

# 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebP header is clearly defined as 0-3 bytes == ASCII RIFF, 8-11 bytes == ASCII WEBP. This is what should be checked exactly and not for WEBP somewhere is the first 16 bytes.

# 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use b'WEBP`.

# 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
Copy link
Collaborator

@dstftw dstftw Sep 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%d in path still does not work:

[debug] System config: []
[debug] User config: []
[debug] Custom config: []
[debug] Command-line args: ['--embed-thu', 'https://www.youtube.com/watch?v=bSBTcQNPNqc', '-f', '18', '-v']
[debug] Encodings: locale cp1251, fs utf-8, out utf-8, pref cp1251
[debug] youtube-dl version 2020.09.06
[debug] Git HEAD: bf6c312
[debug] Python version 3.7.0 (CPython) - Windows-10-10.0.10240-SP0
[debug] exe versions: ffmpeg N-85653-gb4330a0, ffprobe N-85653-gb4330a0, phantomjs 2.1.1, rtmpdump 2.4
[debug] Proxy map: {}
[youtube] bSBTcQNPNqc: Downloading webpage
[youtube] bSBTcQNPNqc: Downloading thumbnail ...
[youtube] bSBTcQNPNqc: Writing thumbnail to: How to print %d using printf in C language-bSBTcQNPNqc.jpg
[debug] Invoking downloader on '...'
[download] How to print %d using printf in C language-bSBTcQNPNqc.mp4 has already been downloaded
[download] 100% of 8.39MiB
[ffmpeg] Converting thumbnail "How to print %d using printf in C language-bSBTcQNPNqc.webp" to JPEG
[debug] ffmpeg command line: ffmpeg -y -loglevel "repeat+info" -i "file:How to print %d using printf in C language-bSBTcQNPNqc.webp" "-bsf:v" mjpeg2jpeg "file:How to print _d using printf in C language-bSBTcQNPNqc.jpg"
ERROR: file:How to print %d using printf in C language-bSBTcQNPNqc.webp: No such file or directory
Traceback (most recent call last):
  File "C:\Dev\youtube-dl\master\youtube_dl\YoutubeDL.py", line 2065, in post_process
    files_to_delete, info = pp.run(info)
  File "C:\Dev\youtube-dl\master\youtube_dl\postprocessor\embedthumbnail.py", line 61, in run
    self.run_ffmpeg(thumbnail_filename, jpg_thumbnail_filename, ['-bsf:v', 'mjpeg2jpeg'])
  File "C:\Dev\youtube-dl\master\youtube_dl\postprocessor\ffmpeg.py", line 239, in run_ffmpeg
    self.run_ffmpeg_multiple_files([path], out_path, opts)
  File "C:\Dev\youtube-dl\master\youtube_dl\postprocessor\ffmpeg.py", line 235, in run_ffmpeg_multiple_files
    raise FFmpegPostProcessorError(msg)
youtube_dl.postprocessor.ffmpeg.FFmpegPostProcessorError: file:How to print %d using printf in C language-bSBTcQNPNqc.webp: No such file or directory


# 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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utils.replace_extension.

@dstftw dstftw closed this in bff857a Sep 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--embed-thumbnail conversion failed, ffmpeg and .webp images.