pythondiscord.pyspotipy

How can I fix the 'ffmpeg was not found' error when playing music in a Discord bot with Python and Spotipy?


I have a problem with my code. I set myself a goal to write a Discord bot that would perform basic actions. I got stuck on the play() function when I enter the command ">play [track_url]". It gives me an error:

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.

Despite searching half the internet, I still can't find a solution.

My code:

@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))

Of course, I imported the libraries below, but ffmpeg still doesn't want to cooperate:

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

I'm in the initial phase of the project, so for now, my main focus is to play any song from Spotify. I would be very grateful for any help or advice on where I can find a solution.


Solution

  • Based on the stacktrace and the documentaion of FFmpegPCMAudio, ffmpeg is not in your PATH and therefore cannot be found by the library.

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

    Install ffmpeg on your machine will fix the exception.