Embedding thumbnail to mp3 with Youtube-dl raise exception
Asked Answered
C

2

7

I am trying to use youtube-dl to download some youtube video sound as mp3 and embed the thumbnail as well. But i get the following error every time i try:

thumbnail_filename = info['thumbnails'][-1]['filename'] KeyError: 'filename'

Here is my youtube-dl options

    ydl_opts = {
        'key':'IgnoreErrors',
        'format': 'bestaudio/best',
        'download_archive': self.songs_data,
        'outtmpl': '/'+download_path+'/'+'%(title)s.%(ext)s',
        'progress_hooks': [self.my_hook],
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192'},
            {'key': 'EmbedThumbnail'},]}

Any ideas why? embed thumbnail does not have any arguments.

Thank you

Canaliculus answered 28/7, 2016 at 21:15 Comment(2)
can you print info['thumbnails'][-1].keys() ?Flavor
Its a python module, i don't have the source code to add lines to.Canaliculus
C
9

So I figured it out on on my own although its not documented on youtube-dl api. You need to add 'writethumbnail':True to options, and change the order on the post processors so 'key': 'FFmpegExtractAudio' is before 'key': 'EmbedThumbnail'

    ydl_opts = {
        'writethumbnail': True,
        'format': 'bestaudio/best',
        'download_archive': self.songs_data,
        'outtmpl': '/'+download_path+'/'+'%(title)s.%(ext)s',
        'progress_hooks': [self.my_hook],
        'postprocessors': [
            {'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192'},
            {'key': 'EmbedThumbnail',},]}
Canaliculus answered 29/7, 2016 at 20:37 Comment(1)
to add to 'Gigalala' answer. Please note that you must download the file as ".webm", this is why ".%(ext)s" is used. If you will change it to be static ".mp3" in the 'outtmpl' you will have some long hours of debugging, like I did.Housecoat
V
1

I had to solve this problem independently, but I used Gigalala to get the final step.

Here is a working Youtube-DL / yt_dlp options variable for downloading mp3's with thumbnails and metadata.

ydl_opts = {
                'format': 'bestaudio[ext=mp3]/best',
                'writethumbnail': True,
                'postprocessors': [
                    {'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3','preferredquality': '192'},
                    {'key': 'FFmpegMetadata', 'add_metadata': 'True'},
                    {'key': 'EmbedThumbnail','already_have_thumbnail': False,}
                
                ],
            }

I found that the order of the postprocessors is very important. I had to embed metadata first, as it wipes the embed thumbnail. This is the fix for Youtube-DL not embedding thumbnails through the python version.

Vitebsk answered 21/3, 2022 at 14:13 Comment(1)
Thanks! The order actually matters. You saved me lots of frustrations.Almaalmaata

© 2022 - 2024 — McMap. All rights reserved.