【发布时间】:2021-02-10 20:46:15
【问题描述】:
当我在我的机器人上使用流命令时,它会在切断之前流式传输大约 1 分 30 秒的音频并且不播放音频,并且它不会在开始或切断时给出错误消息。当我改用下载和播放命令时,它会播放整首歌曲而不会出现错误或中断。 流命令:
async def play(ctx, *, search):
voiceChannel=ctx.message.author.voice.channel
await voiceChannel.connect()
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
query_string = urllib.parse.urlencode({
'search_query': search
})
htm_content = urllib.request.urlopen(
'http://www.youtube.com/results?' + query_string
)
search_results = re.findall(r"watch\?v=(\S{11})", htm_content.read().decode())
url = 'http://www.youtube.com/watch?v=' + search_results[0]
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
song_info = ydl.extract_info(url, download=False)
voice.play(discord.FFmpegPCMAudio(song_info["formats"][0]["url"]))
下载命令:
async def download(ctx, *, search):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the current playing music to end or use the 'stop' command")
return
voiceChannel=ctx.message.author.voice.channel
await voiceChannel.connect()
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
query_string = urllib.parse.urlencode({
'search_query': search
})
htm_content = urllib.request.urlopen(
'http://www.youtube.com/results?' + query_string
)
search_results = re.findall(r"watch\?v=(\S{11})", htm_content.read().decode())
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['http://www.youtube.com/watch?v=' + search_results[0]])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"))
【问题讨论】:
-
这似乎是一个 youtube 问题。
标签: discord.py