【问题标题】:Discord.py bot audio streaming cuts offDiscord.py 机器人音频流中断
【发布时间】: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


【解决方案1】:

好的,所以我在 When playing audio, the last part is cut off. How can this be fixed? (discord.py) 的 stackoverflow dot com 上找到了答案。我引用,“这是一个已知问题,当您尝试制作一个不下载它正在播放的歌曲的机器人时。这里解释:https://support.discord.com/hc/en-us/articles/360035010351--Known-Issue-Music-Bots-Not-Playing-Music-From-Certain-Sources" 原来您只需要提供 FFMPEG 选项即可重新连接并播放歌曲,如下面的代码所示

FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
@client.command(
    name='play',
    description='Streams audio from Youtube. Syntax: w!play [keyword(s)].')
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"], **FFMPEG_OPTIONS))

不管什么原因,有些

【讨论】:

    猜你喜欢
    • 2018-06-16
    • 1970-01-01
    • 2019-04-08
    • 2021-11-19
    • 1970-01-01
    • 2020-08-30
    • 2021-05-05
    • 2021-05-24
    • 2020-08-31
    相关资源
    最近更新 更多