When I try and run this code with any given youtube URL, it will usually say: "Downloading video 1 of 25", and then obviously times out. Downloading the videos as an mp3 file does work, but obviously, I don't need 25 mp3 files whenever I'm only trying to play one video as audio. Anyone got any ideas as to why this happens?
@bot.command()
async def play(ctx, url : str):
song_there = os.path.isfile("song.mp3")
try:
if not os.path.exists("song.mp3"):
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the current music to end / use stop command")
return
voiceChannel = ctx.message.author.voice.channel
await voiceChannel.connect()
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
ydl_opts = {
'format' : 'bestaudio/best',
'postprocessors': [{
'key' : 'FFmpegExtractAudio',
'preferredcodec' : 'mp3',
'preferredquality' : '192',
}]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir('./'):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegAudio("song.mp3"))
@bot.command()
async def leave(ctx):
voice = discord.utils.get(bot.voice_clients, guild= ctx.guild)
if voice.is_connected():
await voice.disconnect()
else:
await ctx.send("The bot is not connected to a voice channel")
@bot.command()
async def pause(ctx):
voice = discord.utils.get(bot.voice_clients, guild= ctx.guild)
if voice.is_playing():
voice.pause()
else:
await ctx.send("Currently no audio is playing")
@bot.command()
async def resume(ctx):
voice = discord.utils.get(bot.voice_clients, guild= ctx.guild)
if voice.is_paused():
voice.resume()
else:
await ctx.send("The audio is not currently paused")
@bot.command()
async def stop(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
voice.stop()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…