I am trying to put together a Python Discord music bot as a fun little project. Outside of the required discord library I’m currently using the YouTube API to search for videos and parse the URL (not shown in code), yt-dlp which is a fork of yt_download that is still maintained to get the info from the YT URL, and FFMPEG to play the song obtained from yt-dlp through the bot. My play command seems to work as the 1st YT video result will start to play, but roughly 30-90 seconds into the audio, it stops playing. I get this message in the console:
2023-02-23 14:54:44 IN discord.player ffmpeg process 4848 successfully terminated with return code of 0.
So there is no error for me to go off of. I’ve included the full output from the console below…
-----------------------------------
groovy-jr#6741 is up and running
-----------------------------------
2023-02-23 14:53:23 INFO discord.voice_client Connecting to voice...
2023-02-23 14:53:23 INFO discord.voice_client Starting voice handshake... (connection attempt 1)
2023-02-23 14:53:24 INFO discord.voice_client Voice handshake complete. Endpoint found us-south1655.discord.media
2023-02-23 14:54:44 INFO discord.player ffmpeg process 4848 successfully terminated with return code of 0. <= AUDIO STOPS
I’m currently developing this project on a Windows 11 machine, but I’ve had the issue running it on my Ubuntu machine as well. I am just hosting the bot directly from the VSCode terminal for development.
I’ve been trying to do research on this problem, the problem is I can’t find many recent information for the issue. There was another post that talked about a similar problem and had an answer suggesting the following FFMPEG options be used which I tried to no avail.
FFMPEG_OPTIONS = {
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn',
}
I’ll include the problem file below:
import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
import responses
import youtubeSearch as YT
import yt_dlp
async def send_message(message, user_message, is_private = False):
try:
response = responses.handle_response(user_message)
await message.author.send(response) if is_private else await message.channel.send(response)
except Exception as e:
print(e)
def run_discord_bot():
intents = discord.Intents.default()
intents.message_content = True
TOKEN = 'xxxxxx'
client = commands.Bot(command_prefix = '-', intents=intents)
@client.event
async def on_ready():
print('-----------------------------------')
print(f'{client.user} is up and running')
print('-----------------------------------')
@client.command(name='play', aliases=['p'], pass_context = True)
async def play(ctx, *, search_term:str = None):
if ctx.author.voice:
voice = None
if search_term == None:
await ctx.send('No song specified.')
return
if not ctx.voice_client:
channel = ctx.message.author.voice.channel
voice = await channel.connect()
else:
voice = ctx.guild.voice_client
url = YT.singleSearch(search_term)
YTDLP_OPTIONS = {
'format': 'bestaudio/best',
'extractaudio': True,
'audioformat': 'mp3',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'ytsearch',
'source_address': '0.0.0.0',
}
=====> FFMPEG_OPTIONS = {
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn',
}
with yt_dlp.YoutubeDL(YTDLP_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
playUrl = info['url']
source = FFmpegPCMAudio(playUrl, options=FFMPEG_OPTIONS)
voice.play(source)
else:
await ctx.send('You must be in a voice channel to play a song!')
return
@client.command(pass_context = True)
async def leave(ctx):
if ctx.voice_client:
await ctx.guild.voice_client.disconnect()
else:
await ctx.send("I'm not in a voice channel!")
@client.command(pass_context = True)
async def pause(ctx):
voice = discord.utils.get(client.voice_clients, guild = ctx.guild)
if voice.is_playing():
voice.pause()
else:
await ctx.send('No audio playing...')
@client.command(pass_context = True)
async def resume(ctx):
voice = discord.utils.get(client.voice_clients, guild = ctx.guild)
if voice.is_paused():
voice.resume()
else:
await ctx.send('No audio paused...')
@client.command(pass_context = True)
async def stop(ctx):
voice = discord.utils.get(client.voice_clients, guild = ctx.guild)
voice.stop()
client.run(TOKEN)
I appreciate any guidance I can get!
2
Answers
It’s not running on Ubuntu properly might be issue with libraries installation there so some packages won’t be installed normally but by using wget, curl or brew. Can check here
One thing I noticed is that YouTube may be blocking the stream after a certain amount of time has elapsed. This is a common issue with YouTube streams, as they are not intended to be played continuously for long periods of time because of their premium feature availability I think.
Solution
for this is use different source for your music. There are many other sources for music, such as SoundCloud or Spotify, that may be more reliable for streaming long songs or can use different library like wavelink.Also it may be helpful for you to check the FFMPEG logs for any errors or warnings that may provide more detailed info on the issue. You can try increasing the verbosity of the logs to get more detailed information by adding the
-v
option to your FFMPEG command.Try this first if may fix, you may have to try updating your FFMPEG installation to the latest version to see if that resolves the issue.
I was also facing the same issue. Passing in the options like below worked for me