gpt4 book ai didi

python - ytdl python "KeyError: formats"

转载 作者:行者123 更新时间:2023-12-04 23:19:37 25 4
gpt4 key购买 nike

我试图制作一个供个人使用的不和谐音乐机器人,因为 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)

最佳答案

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

关于python - ytdl python "KeyError: formats",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69237607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com