python - How with youtube_dl i can play playlist in discord.py
问题描述
Here is my code of play command
@client.command(pass_context=True)
async def play(ctx, url):
try:
voice_channel = ctx.message.author.voice.channel
vc = await voice_channel.connect()
except:
print('already connected')
voice = get(client.voice_clients, guild=ctx.guild)
with YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
if 'entries' in info:
video = info['entries'][0]
if(voice.is_playing()):
voice.stop()
voice.play(FFmpegPCMAudio(video,executable = "C:/Users/Gleb/Desktop/ffmpeg/bin/ffmpeg.exe", **FFMPEG_OPTIONS))
else:
voice.play(FFmpegPCMAudio(video,executable = "C:/Users/Gleb/Desktop/ffmpeg/bin/ffmpeg.exe", **FFMPEG_OPTIONS))
else:
URL = info['formats'][0]['url']
if(voice.is_playing()):
voice.stop()
voice.play(FFmpegPCMAudio(UE,executable = "C:/Users/Gleb/Desktop/ffmpeg/bin/ffmpeg.exe", **FFMPEG_OPTIONS))
else:
voice.play(FFmpegPCMAudio(video,executable = "C:/Users/Gleb/Desktop/ffmpeg/bin/ffmpeg.exe", **FFMPEG_OPTIONS))
i get an error when i play playlist that the 'video' is a dictionary but it only takes string
解决方案
你的程序不会像这样工作。声明为的文档状态FFmpegPCMAudio
class discord.FFmpegPCMAudio(source, *, executable='ffmpeg', pipe=False, stderr=None, before_options=None, options=None)
您将字典video
作为source
. 但是,FFmpegPCMAudio()
不能缓冲视频流。它只能播放本地保存的文件,这些文件通过FFmpeg
. source
必须是文件的链接,就像你在参数中一样executable
。既然你有download = False
争论
info = ydl.extract_info(url, download=False)
您只是提取视频信息,而不是实际下载它。如果没有文件,则无法播放。
要解决您的问题,您需要下载 youtube 视频的音频,并将文件FFmpegPCMAudio()
路径指定为source
推荐阅读
- python - 在 Django 中使用查询的问题
- kubernetes - 在 Kubernetes 中运行 presto CLI 的问题
- unity3d - 在 Unity 中隐藏部分游戏对象
- graphql - 突变后更新缓存时是否需要使用“writeFragment”?
- unit-testing - Pytest:为什么我的模拟函数没有被调用?
- kubernetes - 如何在 Kubernetes 中访问主机 http 服务器
- progressive-web-apps - ChromeOS PWA 调试:“无法获取应用数据,仍然尝试运行应用”
- c++ - 如何在 DevCpp 项目中使用“.dll”和“.lib”文件?
- c++ - 如何在 Arduino-ESP8266 上从 C/C++ 中的字符串解析 time_t?
- android - Ionic 始终允许定位 API 级别 29