当“+dl”时,我尝试使用discord.py获取一些视频信息https://youtube......“执行时,程序将在mp3中下载youtube链接并发送:视频名称、持续时间和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'
显然,您的
playlist_dict
没有键'entries'
很难说为什么。尝试检查
playlist_dict
中还有什么除此之外,将阻塞(
youtube-dl
)和异步(ctx.channel.send()
)代码混合在一起是一种糟糕的做法考虑从一个单独的线程(^ {< CD6>})
调度阻塞调用UPD:据我所知,现在
YoutubeDL.extract_info()
只返回dict的列表,所以您可以删除['entries']
部分,然后在返回的列表上迭代相关问题 更多 >
编程相关推荐