Youtube-dl KeyError: 'entries'
Youtube-dl KeyError: 'entries'
当执行“+dl https://youtube ......”时,我试图通过 discord.py 获取视频的一些信息,程序以 mp3 格式下载到 youtube link 并发送:视频名称、持续时间和 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()
) 代码混合在一起是一种不好的做法。
考虑安排来自单独线程的阻塞调用 (asyncio.to_thread()
)
UPD:据我所知,现在 YoutubeDL.extract_info()
只是 returns 个字典列表,因此您可以删除 ['entries']
部分并迭代返回的列表。
In [73]: import youtube_dl as ydl
In [74]: with ydl.YoutubeDL() as ydl:
...: ydl.extract_info??
...:
Signature:
ydl.extract_info(
url,
download=True,
ie_key=None,
extra_info={},
process=True,
force_generic_extractor=False,
)
Source:
def extract_info(self, url, download=True, ie_key=None, extra_info={},
process=True, force_generic_extractor=False):
'''
Returns a list with a dictionary for each video we find.
If 'download', also downloads the videos.
extra_info is a dict containing the extra values to add to each result
'''
if not ie_key and force_generic_extractor:
ie_key = 'Generic'
if ie_key:
ies = [self.get_info_extractor(ie_key)]
else:
ies = self._ies
for ie in ies:
if not ie.suitable(url):
continue
ie = self.get_info_extractor(ie.ie_key())
if not ie.working():
self.report_warning('The program functionality for this site has been marked as broken, '
'and will probably not work.')
return self.__extract_info(url, ie, download, extra_info, process)
else:
self.report_error('no suitable InfoExtractor for URL %s' % url)
File: ~/.local/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py
Type: method
当执行“+dl https://youtube ......”时,我试图通过 discord.py 获取视频的一些信息,程序以 mp3 格式下载到 youtube link 并发送:视频名称、持续时间和 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()
) 代码混合在一起是一种不好的做法。
考虑安排来自单独线程的阻塞调用 (asyncio.to_thread()
)
UPD:据我所知,现在 YoutubeDL.extract_info()
只是 returns 个字典列表,因此您可以删除 ['entries']
部分并迭代返回的列表。
In [73]: import youtube_dl as ydl
In [74]: with ydl.YoutubeDL() as ydl:
...: ydl.extract_info??
...:
Signature:
ydl.extract_info(
url,
download=True,
ie_key=None,
extra_info={},
process=True,
force_generic_extractor=False,
)
Source:
def extract_info(self, url, download=True, ie_key=None, extra_info={},
process=True, force_generic_extractor=False):
'''
Returns a list with a dictionary for each video we find.
If 'download', also downloads the videos.
extra_info is a dict containing the extra values to add to each result
'''
if not ie_key and force_generic_extractor:
ie_key = 'Generic'
if ie_key:
ies = [self.get_info_extractor(ie_key)]
else:
ies = self._ies
for ie in ies:
if not ie.suitable(url):
continue
ie = self.get_info_extractor(ie.ie_key())
if not ie.working():
self.report_warning('The program functionality for this site has been marked as broken, '
'and will probably not work.')
return self.__extract_info(url, ie, download, extra_info, process)
else:
self.report_error('no suitable InfoExtractor for URL %s' % url)
File: ~/.local/lib/python3.9/site-packages/youtube_dl/YoutubeDL.py
Type: method