You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人播放YouTube音频时FFmpeg返回码3436169992且无声音输出的问题求助

Discord机器人播放YouTube音频时FFmpeg返回码3436169992且无声音输出的问题求助

我正在开发一个能把YouTube视频转成音频播放的Discord机器人,之前遇到403 Forbidden错误后,通过添加YDL_OPTIONS参数解决了,但现在又碰到了错误码3436169992——机器人会在服务器里发送「now playing: xxxx」的提示,但完全没有声音输出。有没有大佬能给点排查方向或者解决思路?

以下是我的完整代码:

FFMPEG_OPTIONS = {
    "before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5",
    "options": "-vn -filter:a \"volume=0.25\""
}

# yt-dlp options
YDL_OPTIONS = {
    "format": "bestaudio[ext=webm][acodec=opus]/bestaudio",
    "noplaylist": True,
    "quiet": True,
    "extract_flat": False,
    "no_warnings": True,
    "source_address": "0.0.0.0"  # Prevents YouTube throttling
}

voice_clients = {}

@bot.event
async def on_message(message):
    if message.content.startswith("-play"):
        try:
            # Ensure the user is in a voice channel
            if not message.author.voice:
                await message.channel.send("You need to be in a voice channel to use this command.")
                return

            # Connect to the user's voice channel if not already connected
            if message.guild.id not in voice_clients or not voice_clients[message.guild.id].is_connected():
                voice_client = await message.author.voice.channel.connect()
                voice_clients[message.guild.id] = voice_client
            else:
                voice_client = voice_clients[message.guild.id]

            # Extract the YouTube URL from the command
            url = message.content.split()[1]

            # Download and extract audio using yt-dlp
            with YoutubeDL(YDL_OPTIONS) as ydl:
                info = ydl.extract_info(url, download=False)
                audio_url = info["url"]
                title = info.get("title", "Unknown Title")

            # Debug: Print the extracted audio URL
            print(f"Audio URL: {audio_url}")

            # Play the audio using FFmpeg
            source = FFmpegPCMAudio(audio_url, **FFMPEG_OPTIONS)
            voice_client.play(source, after=lambda e: print("Playback finished."))

            # Notify the user
            await message.channel.send(f"Now playing: {title}")

        except Exception as e:
            print(f"Error: {e}")
            await message.channel.send("An error occurred while trying to play the audio.")

可能的排查和解决方向:

  • 排查FFmpeg参数兼容性:错误码3436169992通常和FFmpeg处理音频流异常有关,怀疑是当前的音量过滤参数导致的问题。可以先暂时去掉音量过滤,只保留-vn(剥离视频流)测试播放:

    FFMPEG_OPTIONS = {
        "before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 10 -reconnect_on_network_error 1",
        "options": "-vn"
    }
    
  • 放宽yt-dlp的格式限制:当前的格式过滤条件太严格,可能部分视频没有匹配的音频流。可以调整格式选项,让yt-dlp自动选择最优兼容的音频流:

    YDL_OPTIONS = {
        "format": "bestaudio/best",
        "noplaylist": True,
        "quiet": True,
        "extract_flat": False,
        "no_warnings": True,
        # 暂时注释source_address,排除IP绑定导致的连接问题
        # "source_address": "0.0.0.0"
    }
    
  • 直接测试音频流有效性:把打印出来的audio_url复制到命令行,用FFmpeg直接测试播放,比如运行:

    ffplay <你的audio_url>
    

    如果命令行也无法播放,说明是音频流本身的问题,需要调整yt-dlp参数;如果能播放,问题可能出在Discord语音客户端的配置上。

  • 完善错误回调信息:修改play方法的after回调,打印详细的播放错误,精准定位问题:

    voice_client.play(source, after=lambda e: print(f"Playback error: {e}" if e else "Playback finished."))
    
  • 更新依赖版本:确保yt-dlp、discord.py和FFmpeg都是最新版本——YouTube反爬机制经常更新,旧版yt-dlp可能无法正确提取音频流;discord.py语音模块的更新也可能修复兼容性问题。

备注:内容来源于stack exchange,提问作者Deeroy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 03:42:59