不和谐音乐机器人下载网页上的所有视频,而不仅仅是一个

时间:2021-01-11 09:49:43

标签: ffmpeg discord discord.py

当我尝试使用任何给定的 youtube URL 运行此代码时,它通常会说:“正在下载 25 个视频中的第 1 个”,然后显然超时。将视频作为 mp3 文件下载确实有效,但很明显,每当我只想将一个视频作为音频播放时,我都不需要 25 个 mp3 文件。任何人都知道为什么会发生这种情况?

@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()

0 个答案:

没有答案