1

I'm making a discord bot for playing music (ik very unoriginal) and everytime I try to play music, it gives this error:

2023-02-18 12:31:54 INFO     discord.player ffmpeg process 4024 has not terminated. Waiting to terminate...
2023-02-18 12:31:54 INFO     discord.player ffmpeg process 4024 should have terminated with a return code of -9.

It is in the voice chat and didn't play anything. Here's my code if you need it:

print("defining functions")
import discord, pytube, os, threading
from moviepy import editor
from discord.ext.commands import Bot
intents=discord.Intents.default()
intents.message_content = True
intents.voice_states = True
bot = Bot("b!",intents=intents)
check = False
token = "redacted"
@bot.command()
async def play(ctx, arg):
            await ctx.send("Downloading...")
            try:
                yt = pytube.YouTube(arg)
                print("STREAM: "+str(yt.streams.filter(only_audio=True,mime_type="audio/webm",type="audio",abr="160kbps")))
                def download(yt):
                    print("DOWNLOADING STREAM TO AUDIO.WEBM")
                    yt.streams.filter(only_audio=True,mime_type="audio/webm",type="audio",abr="160kbps").order_by("abr").first().download(filename="audio.webm")
                    print("EXPORTING TO MP3")
                    editor.AudioFileClip("audio.webm").write_audiofile("audio.webm"[:-5] + ".mp3")
                    os.remove("audio.webm")
                    print("DONE")
                thread = threading.Thread(target=download, args=(yt,))
                thread.start()
                thread.join()
                try:
                  channel = ctx.author.voice.channel
                  try:
                    vc = await channel.connect()
                  except Exception:
                    ctx.send("already in vc, one sec")
                except Exception:
                    pass
                vc.play(discord.FFmpegPCMAudio(source="audio.mp3", executable="./ffmpeg"))
            except Exception:
              pass
bot.run(token)

It worked on my computer in VS Code, but I wanted to test it on Replit and only there I got the error. I tried:

  1. Reinstalling discord, pynacl, even pytube and moviepy.
  2. Using a local copy of ffmpeg on replit, see line 36.
  3. Adding the intents that were described in a different thread.

One thing that always happens is the exit code being -9 but the process number changing (ofc). I saw a GitHub Issue about this too which is not showing a fix so how do I fix that?

Bummbumm6
  • 31
  • 4
  • The computer in VS Code, is it windows? You may need to specify ffmpeg options. Also, rather than downloading, I reccomend playing the stream directly, it's faster. I'll send you my code in a bit once my class finishes. – AzlanCoding Feb 23 '23 at 05:42

1 Answers1

0

Idk what's the main cause of your issue, but I have a better code that is faster, works for my bot and is how most music bots work:

stream = pytube.YouTube.from_id(video_id=Id).streams
itag_list = [141,140,139,251,171,250,249]#These are the lists of itags that can be played by ffmpeg.
for itag in itag_list:
    try:
        audio = pytube.YouTube(YTvideo_url).streams.get_by_itag(itag).url#get stream url
        print("itag: " + str(itag))
        break
    except AttributeError:#cannot find stream by current itag, as itag not avaliable
        continue
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5','options': '-vn -filter:a "volume=0.25"'}#optimised settings for ffmpeg for streaming
source = FFmpegPCMAudio(audio, **FFMPEG_OPTIONS)  # converts the youtube audio source into a source discord can use
print(audio) #prints stream url for debugging
if voice_client.is_playing():
    await self.stop(ctx)#My own function to stop the bot from playing the music if music is already playing.
voice_client.play(source)# play the source
AzlanCoding
  • 209
  • 2
  • 11