如何修复使用Python和Spotipy在Discord bot中播放音乐时出现的“ffmpeg was not found”错误?

pbpqsu0x  于 2023-06-04  发布在  Python
关注(0)|答案(2)|浏览(182)

我的代码有问题。我给自己设定了一个目标,写一个能执行基本动作的Discord机器人。当我输入命令“>play [track_url]"时,我被play()函数卡住了。它给了我一个错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/ext/commands/core.py", line 229, in wrapped
    ret = await coro(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/zala/Documents/GitHub/Joi-discord-bot/bot.py", line 91, in play
    voice_client.play(discord.FFmpegPCMAudio(track_preview_url))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/player.py", line 289, in __init__
    super().__init__(source, executable=executable, args=args, **subprocess_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/player.py", line 166, in __init__
    self._process = self._spawn_process(args, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/player.py", line 183, in _spawn_process
    raise ClientException(executable + ' was not found.') from None
discord.errors.ClientException: ffmpeg was not found.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/ext/commands/bot.py", line 1350, in invoke
    await ctx.command.invoke(ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/ext/commands/core.py", line 1023, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/ext/commands/core.py", line 238, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ClientException: ffmpeg was not found.

尽管在网上找了一半,我还是找不到解决办法。
我的代码:

@client.command()
async def play(ctx, prompt):
    voice_channel = ctx.author.voice.channel
    voice_client = await voice_channel.connect()
    track_url = prompt
    track_info = spotify.track(track_url)
    if track_info:
        track_preview_url = track_info['album']['external_urls']['spotify']
        if track_preview_url:
            voice_client.play(discord.FFmpegPCMAudio(track_preview_url))

当然,我导入了下面的库,但ffmpeg仍然不想合作:

import discord
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from dotenv import dotenv_values
from discord.ext import commands

我正处于项目的初始阶段,所以现在,我的主要重点是播放Spotify上的任何歌曲。我将非常感谢任何帮助或建议,我可以找到一个解决方案。

xwbd5t1u

xwbd5t1u1#

您需要单独安装ffmpeg并将其放在PATH上。您可以通过在终端中键入ffmpeg来检查是否正确安装了它。更多信息:阅读文档

daolsyd0

daolsyd02#

根据堆栈跟踪和FFmpegPCMAudio的文档,ffmpeg不在您的PATH中,因此库无法找到。

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ClientException: ffmpeg was not found.

在您的机器上安装ffmpeg将修复该异常。

相关问题