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

Discord Bot调用spotdl报FFmpeg未找到故障解决方案

问题现象

通过sudo apt-get install安装spotdl、FFmpeg后,直接在终端运行spotdl可正常工作,但在Discord机器人代码中调用spotdl时,弹出FFmpeg was not found, spotdl can't continue报错,流程中断。

初始故障代码

@bot.command()
async def getsong(ctx, *, songname):
    "get song in mp3 from song name"
    os.popen(f'spotdl {songname} -f "/home/pi/discordbot/output/{songname}.mp3"')
    await ctx.send(file=discord.File(fr'/home/pi/discordbot/output/{songname}.mp3'))

前置校验:FFmpeg已通过apt源正常安装,终端环境下执行spotdl、ffmpeg命令均无异常。

修复方案

问题根源是Discord机器人的运行环境PATH变量和终端交互环境不一致,子进程调用spotdl时无法自动检索到FFmpeg可执行文件,同时原代码使用的-f参数不符合新版本spotdl的参数规范。

修复后可正常运行的代码如下:

@bot.command()
async def getsong(ctx, *, songname):
    "get song in mp3 from song name"
    subprocess.Popen(f'spotdl -o "/home/pi/discordbot/output" --ffmpeg "/usr/bin/ffmpeg" {songname} --path-template "{songname}.mp3"', shell=True)
    await ctx.send(file=discord.File(fr'/home/pi/discordbot/output/{songname}.mp3'))

关键调整点:

  • 显式通过--ffmpeg参数传入FFmpeg的绝对安装路径/usr/bin/ffmpeg,跳过环境变量查找逻辑,直接指定依赖位置,避免路径检索失败
  • 替换os.popen为subprocess.Popen发起子进程调用,兼容性和稳定性更好
  • 适配新版spotdl参数规则,用-o指定输出根目录、--path-template指定生成文件的命名规则,替换旧版已废弃的-f参数

如果你的设备上FFmpeg不是安装在默认路径,可在终端执行which ffmpeg查询实际绝对路径,替换代码中对应参数值即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 18:09:28