首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ytdl python "KeyError: formats“

ytdl python "KeyError: formats“
EN

Stack Overflow用户
提问于 2021-09-18 18:56:06
回答 2查看 405关注 0票数 0

我试图制造一个不和谐的个人音乐机器人,因为groovy和rythm被关闭了。我想还不错,但我对ytdl有问题。输入"-play“和一个url就像预期的那样工作,但我不能输入"-play‘歌曲名称’”。输入"-play示例“将给出以下内容:

代码语言:javascript
复制
[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 -永不放弃“,只搜索"Rick”,然后它说了一些关于KeyError的内容:'formats‘,这是我的代码:

代码语言:javascript
复制
@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)
EN

回答 2

Stack Overflow用户

发布于 2022-01-09 11:03:10

代码语言:javascript
复制
#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]
票数 0
EN

Stack Overflow用户

发布于 2022-07-07 00:59:41

我遇到了这个问题,在做了几个小时的工作后,我发现"extract_info“在从搜索中提取而不是从url中提取时返回不同的字典。

通过使用一个简单的if检查来区分url和search,我解决了这个问题:

代码语言:javascript
复制
#check if it is a valid link
if ("youtube.com" in url or "youtu.be" in url):
#run the normal code after
    info = ydl.extract_info(url, download=False)
    url2 = info['formats'][0]['url']
    source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)

#if it's not a valid link but a search
else:
    info = ydl.extract_info(url, download=False)
    url2 = info['entries'][0]['webpage_url']

这个新值的"url2“在搜索部分将是对视频的链接,你应该能够把它分回与正常链接相同的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69237607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档