python - 我无法在我的 Discord Bot 上播放带有 Youtube URL 的歌曲
问题描述
因此,由于 Rhythm 和 Groovy 不可用,我尝试使用 Python 为我自己的服务器创建自己的音乐机器人,但我无法使用 YouTube URL 播放它。有没有人有解决方案?
@commands.command()
async def play(self,ctx,*,url):
ctx.voice_client.stop()
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
if 'entries' in info:
url2 = info["entries"][0]["formats"][0]['url']
elif 'formats' in info:
url2 = info["entries"][0]["formats"][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
vc.play(source)
解决方案
@commands.command(name='play')
async def _play(self, ctx: commands.Context, *, search: str):
"""Plays a song.
If there are songs in the queue, this will be queued until the
other songs finished playing.
This command automatically searches from various sites if no URL is provided.
"""
if not ctx.voice_state.voice:
await ctx.invoke(self._join)
async with ctx.typing():
try:
source = await YTDLSource.create_source(ctx, search, loop=self.bot.loop)
except YTDLError as e:
await ctx.send('An error occurred while processing this request: {}'.format(str(e)))
else:
song = Song(source)
await ctx.voice_state.songs.put(song)
await ctx.send('Enqueued {}'.format(str(source)))
上面的代码取自这里。由于这是一个常见问题,我认为使用预先测试的代码将是创建选项。
推荐阅读
- node.js - 使用 ffmpeg 和 nodejs (fluent-ffmpeg) 在不重新编码的情况下剪切视频
- react-native - 如何将 simple-peer 与 react-native-webrtc 一起使用
- python - 从 pyspark 数据帧向量列中查找最大值索引的错误
- r - 使用 runif() 和 rbern() 的 R 脚本
- c# - 在 Xamarin 表单中隐藏/显示文本
- javascript - 两个 Material-UI 单选按钮都被选中
- nlp - 规范化 Fasttext 预训练的 Fasttext 词嵌入
- python - 根据术语将带有 URL 的列分类为类别
- prestashop - Prestashop 1.7 仅在添加国家地址后显示运费
- python - 我如何检查来自 python 的用户输入是否已经存在于数据库/表中?