【问题标题】:ytdl python "KeyError: formats"ytdl python“KeyError:格式”
【发布时间】:2021-11-13 04:03:24
【问题描述】:

我正在尝试制作一个供个人使用的不和谐音乐机器人,因为 groovy 和 rythm 被关闭了。 我猜它工作正常,但我遇到了 ytdl 的问题。 输入“-play”和一个 url 就像预期的那样工作,但我不能输入“-play 'song name'”。 输入“-play example”会给我这个:

[download] Downloading playlist: example
[youtube:search] query "example": Downloading page 1
[youtube:search] playlist example: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] CLXt3yh2g0s: Downloading webpage
Ignoring exception in command play:
[download] Finished downloading playlist: example
Traceback (most recent call last):
  File "C:\Users\Dennis\PycharmProjects\groovy's true successor\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Dennis\PycharmProjects\groovy's true successor\voice.py", line 53, in play
    url2 = info['formats'][0]['url']
KeyError: 'formats'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Dennis\PycharmProjects\groovy's true successor\venv\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Dennis\PycharmProjects\groovy's true successor\venv\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Dennis\PycharmProjects\groovy's true successor\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'formats'

我对编码很陌生,如果有什么奇怪的地方我很抱歉。

好的,所以:用 url 输入 -play 可以正常工作,但用歌曲名称输入 -play 则不行。它只搜索第一个单词,下载第一个搜索结果然后“崩溃”。

所以“-play Rick Astley - Never Gonna Give You Up”例如只搜索“Rick”,然后它会说一些关于 KeyError: 'formats' 这是我的代码:

@client.command()
async def play(ctx, url):
    channel = ctx.author.voice.channel
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if voice and voice.is_connected():
        pass
    else:
        await channel.connect()

    ffmpeg_opts = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
    ydl_opts = {'format': "bestaudio/best", 'default_search': 'auto'}
    vc = ctx.voice_client

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=False)
        url2 = info['formats'][0]['url']
        source = await discord.FFmpegOpusAudio.from_probe(url2, **ffmpeg_opts)
        vc.play(source)

【问题讨论】:

  • 我认为错误与键名有关。你能试试info['format']
  • 我已经试过了。一样。删除该部分并留下“[0]['url]”会给我同样的错误,但例如“KeyError:'0'”。我认为问题在于它试图从“ydl.extract_info(url, download=False) 行中取出东西,但它无法提取东西,因为它没有开头的 url,它通过 yt 搜索结果下载,并且是唯一的我有接近链接的东西是“yhS9LnDoo_w”,“youtube.com/watch?v=”之后的部分。输入“-play test”给我“[youtube] yhS9LnDoo_w:下载网页”作为输出。
  • 抱歉英语不好。英语是我的第三语言,我有点累。

标签: python ffmpeg discord keyerror ytdl


【解决方案1】:
#takes the entire text instead of just the first word
async def play(ctx, *,url):


#i would remove 'default_search': 'auto' and do this
ydl_opts = {'format': "bestaudio/best"}
vc = ctx.voice_client

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    url = ydl.extract_info("ytsearch:%s" % name, download = False)['entries'][0]

【讨论】:

    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 2011-04-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多