这是我的Discord机器人的播放功能的代码:
@bot.command()
async def play(ctx, url:str = None): # DOESN'T WORK, DOWNLOADS BUT DOESN'T PLAY
...
ytdl_options = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
ytdl = youtube_dl.YoutubeDL(ytdl_options)
loop = asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url))
if 'entries' in data:
data = data['entries'][0] # taking top item from a YT playlist
source = ytdl.prepare_filename(data)
vc.play(discord.FFmpegPCMAudio(source)) #play audio
print(vc.is_playing())
await ctx.send("Now playing: " + data.get('title')) #now playing
asyncio.sleep(3)
print(vc.is_playing())
当我调用该函数时,它运行良好,并从youtube-dl下载了视频,然后发送消息“正在播放:X”,但不播放任何音频。
我添加了两行print(vc.is_playing())
,shell中返回的内容是:
True
False
很显然,它尝试播放歌曲,但立即失败。
没有抛出异常,所以我不明白为什么FFmpeg不想播放音频。