Nextcord机器人play命令报错:FFmpeg未找到及FFmpegPCMAudio属性缺失
解决Nextcord机器人play命令的FFmpeg相关错误
运行基于Nextcord的Discord机器人play命令时,会触发两类错误:
- 首先抛出
nextcord.errors.ClientException,提示ffmpeg未找到 - 随后引发
AttributeError,提示**'FFmpegPCMAudio'对象不存在'_process'属性**
相关代码
@bot.command() async def play(ctx, url): global is_playing if not ctx.author.voice: await ctx.send("You are not in a voice channel.") return voice_channel = ctx.author.voice.channel if not voice_channel: await ctx.send("You are not in a voice channel.") return voice_client = nextcord.utils.get(bot.voice_clients, guild=ctx.guild) if not voice_client: voice_client = await voice_channel.connect() if is_playing: await ctx.send("The bot is already playing audio.") return url_youtube = url ydl_opts = { 'format': 'bestaudio/best', 'extractaudio': True, 'audioformat': 'mp3', 'outtmpl': 'C:\\Users\\alshh\\Videos\\%(title)s.%(ext)s', 'yes-playlist': True, 'playlist-random': True, 'embed-thumbnail': True, } with youtube_dl.YoutubeDL(ydl_opts) as ydl: info_dict = ydl.extract_info(url_youtube, download=True) if 'entries' in info_dict: for entry in info_dict['entries']: print(f"Downloaded: {entry['title']}.{entry['ext']}") else: print(f"Downloaded: {info_dict['title']}.{info_dict['ext']}") is_playing = True player = nextcord.FFmpegPCMAudio(f"C:\\Users\\alshh\\Videos\\{info_dict['title']}.{info_dict['ext']}") voice_client.play(player, after=lambda e: play_next(ctx) if e else None)
完整报错堆栈
Ignoring exception in command play: Traceback (most recent call last): File "C:\Users\alshh\AppData\Local\Programs\Python\Python38\lib\site-packages\nextcord\ext\commands\core.py", line 143, in wrapped ret = await coro(*args, **kwargs) File "c:/Users/alshh/.vscode/discord/Recording/1.py", line 63, in play player = nextcord.FFmpegPCMAudio(f"C:\\Users\\alshh\\Videos\\{info_dict['title']}.{info_dict['ext']}") File "C:\Users\alshh\AppData\Local\Programs\Python\Python38\lib\site-packages\nextcord\player.py", line 281, in __init__ super().__init__(source, executable=executable, args=args, **subprocess_kwargs) File "C:\Users\alshh\AppData\Local\Programs\Python\Python38\lib\site-packages\nextcord\player.py", line 144, in __init__ self._process: subprocess.Popen = self._spawn_process(args, **kwargs) File "C:\Users\alshh\AppData\Local\Programs\Python\Python38\lib\site-packages\nextcord\player.py", line 163, in _spawn_process raise ClientException(executable + " was not found.") from None nextcord.errors.ClientException: ffmpeg was not found. The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\alshh\AppData\Local\Programs\Python\Python38\lib\site-packages\nextcord\ext\commands\bot.py", line 1355, in invoke await ctx.command.invoke(ctx) File "C:\Users\alshh\AppData\Local\Programs\Python\Python38\lib\site-packages\nextcord\ext\commands\core.py", line 1003, in invoke await injected(*ctx.args, **ctx.kwargs) File "C:\Users\alshh\AppData\Local\Programs\Python\Python38\lib\site-packages\nextcord\ext\commands\core.py", line 152, in wrapped raise CommandInvokeError(exc) from exc nextcord.ext.commands.errors.CommandInvokeError: Command raised an exception: ClientException: ffmpeg was not found. Exception ignored in: <function AudioSource.__del__ at 0x000002881ADFF0D0> Traceback (most recent call last): File "C:\Users\alshh\AppData\Local\Programs\Python\Python38\lib\site-packages\nextcord\player.py", line 95, in __del__ self.cleanup() File "C:\Users\alshh\AppData\Local\Programs\Python\Python38\lib\site-packages\nextcord\player.py", line 214, in cleanup self._kill_process() File "C:\Users\alshh\AppData\Local\Programs\Python\Python38\lib\site-packages\nextcord\player.py", line 170, in _kill_process proc = self._process AttributeError: 'FFmpegPCMAudio' object has no attribute '_process'
解决方案
1. 安装并配置FFmpeg
这是根本问题,所有后续错误都是FFmpeg未找到导致的:
- 下载对应系统的FFmpeg安装包,解压到本地路径(比如
C:\ffmpeg) - 方法一:配置系统环境变量
找到系统PATH变量,添加FFmpeg的bin目录路径(比如C:\ffmpeg\bin),配置完成后重启命令行和Python环境 - 方法二:代码中指定FFmpeg路径
如果不想配置环境变量,直接在创建FFmpegPCMAudio时指定executable参数:player = nextcord.FFmpegPCMAudio( f"C:\\Users\\alshh\\Videos\\{info_dict['title']}.{info_dict['ext']}", executable="C:\\ffmpeg\\bin\\ffmpeg.exe" # 替换成你的FFmpeg实际路径 ) - 验证配置:打开命令行输入
ffmpeg -version,能正常输出版本信息即为配置成功
2. 关于_process属性缺失的错误
这个错误是FFmpeg初始化失败的衍生问题:当FFmpeg未找到时,FFmpegPCMAudio对象的_process属性还没被创建,但对象销毁时的cleanup()方法仍尝试访问该属性,从而抛出异常。只要解决了FFmpeg的问题,这个错误会自动消失。
3. 额外优化建议
- 替换全局变量
is_playing:直接使用voice_client.is_playing()方法判断机器人是否正在播放,避免全局变量的潜在问题:if voice_client.is_playing(): await ctx.send("The bot is already playing audio.") return - 修复播放列表处理逻辑:当前代码下载了播放列表的所有条目,但只播放了第一个条目。建议维护一个播放队列,在
play_next函数中依次取出队列中的音频文件进行播放 - 替换
youtube_dl为yt-dlp:youtube_dl已停止维护,yt-dlp是其活跃分支,支持更多站点和最新特性,安装命令:pip install yt-dlp,代码中只需将youtube_dl替换为yt_dlp即可
内容的提问来源于stack exchange,提问作者Faisal
相关产品推荐
相关产品推荐

