YTDL Python "KeyError: formats"



我想做一个个人使用的不和谐音乐机器人,因为groovy和rhythm被关闭了。我想它工作得还可以,但是我用ytdl有问题。输入"-play"和一个url工作就像预期的,但我不能键入"播放'歌曲名称'"。输入"-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:UsersDennisPycharmProjectsgroovy's true successorvenvlibsite-packagesdiscordextcommandscore.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:UsersDennisPycharmProjectsgroovy's true successorvoice.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:UsersDennisPycharmProjectsgroovy's true successorvenvlibsite-packagesdiscordextcommandsbot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:UsersDennisPycharmProjectsgroovy's true successorvenvlibsite-packagesdiscordextcommandscore.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:UsersDennisPycharmProjectsgroovy's true successorvenvlibsite-packagesdiscordextcommandscore.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'formats'

我对编码相当陌生,所以如果有些奇怪的东西难以理解,我很抱歉。

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

so "-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)
#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]

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

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

#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']

新值" ur12 "在搜索部分将是视频的链接,你应该能够把它和普通链接一样的东西。

最新更新