youtube_dl KeyError 'key'

Viewed 310

Hello I am trying to download youtube videos with the following code

import youtube_dl
import tempfile

youtube_links = ['https://www.youtube.com/watch?v=668nUCeBHyY']
with tempfile.TemporaryDirectory() as tempdir:
    opts = {
        'format': 'best',
        'outtmpl': f'{tempdir}/%(id)s.%(ext)s',
        'noplaylist': True,
        'postprocessors': [{
            'preferredcodec': 'mp4'
        }]
    }
    ydl = youtube_dl.YoutubeDL(opts)
    try:
        meta = ydl.extract_info(
            youtube_links,
            download=True
        )
    except Exception as e:
        raise e
    else:
        print(f"Downloaded to {tempdir}/{meta['id']}.{meta['ext']}")

However this raises an error:

<path>>py -3.8 test.py
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    ydl = youtube_dl.YoutubeDL(opts)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\youtube_dl\YoutubeDL.py", line 429, in __init__
    pp_class = get_postprocessor(pp_def_raw['key'])
KeyError: 'key'

And none of the examples told me anything about a key I have to pass, nor can I find anything about it, what am I doing wrong?

1 Answers

In a nutshell, try this:

'postprocessors': [{
            'key':'FFmpegMetadata',
            'preferredcodec': 'mp4'
        }]

There are lots of keys, so if it's not working, check out this: https://github.com/ytdl-org/youtube-dl/blob/master/youtube_dl/postprocessor/__init__.py

The trick is that the keys listed inside the file are not usable because you gotta drop the "PP" part!!

FYI:

It seems you are trying to get video? Then you should try the documented style: https://github.com/ytdl-org/youtube-dl/

You have to look at the "Functions" yourself and guess what parameters you should put here: https://github.com/ytdl-org/youtube-dl/blob/master/youtube_dl/YoutubeDL.py In fact, somewhere in this link says, "check out the init.py for key information."

Related