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

Discord音乐bot使用youtube-dl执行/play命令卡在下载网页问题排查

问题根因

代码执行到discord.FFmpegOpusAudio.from_probe步骤时卡住,核心是传参错误、依赖缺失或库兼容性问题,按以下步骤排查修复:

修复步骤

  • 1. 修正FFmpeg参数传参错误
    你当前直接把FFMPEG_OPTIONS字典作为位置参数传给from_probe,不符合方法的参数要求,正确写法是拆分before_options和options分别传参:
# 把原来的source = await discord.FFmpegOpusAudio.from_probe(url2,FFMPEG_OPTIONS)替换成下面的
source = await discord.FFmpegOpusAudio.from_probe(
    url2,
    before_options=FFMPEG_OPTIONS['before_options'],
    options=FFMPEG_OPTIONS['options']
)
  • 2. 检查FFmpeg依赖安装
    在系统终端执行ffmpeg -version,如果无版本信息输出,需要先安装FFmpeg并将其路径添加到系统环境变量,重启VSCode后再测试。
  • 3. 替换停止维护的youtube_dl
    youtube_dl早已停止更新,对YouTube的新接口适配存在大量问题,直接替换为兼容其接口的yt-dlp即可:
    1. 执行pip uninstall youtube_dl卸载旧库
    2. 执行pip install yt-dlp安装新库
    3. 将代码中所有import youtube_dl替换为import yt_dlp as youtube_dl
  • 4. 添加异常捕获定位问题
    给play方法添加异常捕获,方便后续定位其他潜在问题:
@commands.command()
async def play(self,ctx, url:str = None):
    if(url == None):
        await ctx.reply("*Check your arguments!*\n```/play VIDEO_URL```")
        return
    try:
        ctx.voice_client.stop() # stop current song
        
        # FFMPEG handle streaming in discord, and has some standard options we need to include
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
        YTDL_OPTIONS = {"format":"bestaudio"}
        vc = ctx.voice_client
        
        # Create stream to play audio and then stream directly into vc
        with youtube_dl.YoutubeDL(YTDL_OPTIONS) as ydl:
            info = ydl.extract_info(url, download=False)
            print("1")
            url2 = info["formats"][0]["url"]
            print("2")
            source = await discord.FFmpegOpusAudio.from_probe(
                url2,
                before_options=FFMPEG_OPTIONS['before_options'],
                options=FFMPEG_OPTIONS['options']
            )
            print("3")
            vc.play(source) # play the audio
            await ctx.send(f"*Playing {info['title']} -* 🎵")
    except Exception as e:
        print(f"播放错误:{e}")
        await ctx.reply(f"播放失败:{str(e)}")

内容的提问来源于stack exchange,提问作者patricebailey1998

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 07:48:03