【发布时间】:2021-03-29 01:28:06
【问题描述】:
当执行“+dl https://youtube......”时,我尝试使用 discord.py 获取视频的一些信息,程序以 mp3 格式下载 youtube 链接并发送:视频名称, duration 和 id 但在执行过程中出现错误:
ydl_opts = {
'outtmpl': './SomethingMore/dl.mp3',
'format': 'bestaudio/best',
'noplaylist': True,
'default_search' : 'ytsearch',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}]}
Link = ctx.message.content
Link = Link.strip('+dl ')
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
playlist_dict = ydl.extract_info(Link, download=False)
for video in playlist_dict['entries']:
if not video:
print('ERROR: Unable to get info. Continuing...')
continue
video_title = video.get("title")
video_duration = video.get("duration")
video_id = video.get("id")
await ctx.channel.send('Download of '+ video_title +' is starting, please wait a minute')
try:
ydl.download([Link])
await ctx.channel.send('Download ended')
这是错误:
Ignoring exception in command dl:
Traceback (most recent call last):
File "C:\Users\Zarcross\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "h:\Users\Zarcross\Desktop\Discord\main.py", line 267, in dl
for video in playlist_dict['entries']:
KeyError: 'entries'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Zarcross\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Zarcross\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Zarcross\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\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: 'entries'
【问题讨论】:
-
" 但我在执行过程中遇到错误:" 请显示complete error message。
-
另外,请尝试制作一个正确包含的minimal example。您是否能够使该过程在您的 Discord 机器人的上下文之外工作?如果不是,那么问题与您的 Discord 机器人无关,因此与
discord.py无关;所以你不应该向我们展示那部分(你应该只向我们展示你的最小示例),并且你不应该那样标记它。如果是,那么问题在于您的机器人如何与之交互,我们需要更多上下文。 -
您还应该阅读ericlippert.com/2014/03/05/how-to-debug-small-programs。例如,您是否尝试验证
Link是否符合您的预期?playlist_dict怎么样?如果存在差异,您是否尝试找到原因? -
我的链接是好的链接,我得到youtu.be......
-
没有discord.py的相同问题
标签: python discord.py youtube-dl keyerror