Search code examples
pythonherokuffmpegdiscord.py

Where can discord bots find the ffmpeg buildpack heroku


I'm making a bot which outputs a mp3 file to the voice chat with discord.py, It works localy by using:

vc.play(discord.FFmpegPCMAudio(executable="ffmpeg/bin/ffmpeg.exe", source=noise.mp3))

However I'm now hosting it on Heroku, i have the buildpack installed but how can my code access it in replacement of the code above


Solution

  • You didn't include your code, so I didn't know if you want it to play when you write command in discord or when the bot is ready, so I decided to make bot play when it's ready (if you want it to join on the command you can modify my code). Also, I think it will be easier if you use youtube_dl for this.

    import discord
    from discord.ext import commands
    import youtube_dl
    
    Token = "XXXXXX" #your token         
    client = commands.Bot(command_prefix = ":") 
    
    
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }   
    
    def endSong(guild, path):
        os.remove(path)
    
    url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" #link to your song on YouTube
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        file = ydl.extract_info(url, download=True)
        guild = "1234567890" #id of your server which you can get by right clicking on server name and clicking "Copy ID" (developer mode must be on)
        path = str(file['title']) + "-" + str(file['id'] + ".mp3")
    
    channel = client.get_channel(1234567890) #id of your channel (you get it like server id, but by right clicking on channel)                         
    voice_client = await channel.connect()                                           
    
    voice_client.play(discord.FFmpegPCMAudio(path), after=lambda x: endSong(guild, path))
    voice_client.source = discord.PCMVolumeTransformer(voice_client.source, 1)
        
    while voice_client.is_playing(): #waits until song ends
        await asyncio.sleep(1)
    else:
        await voice_client.disconnect() #and disconnects
        print("Disconnected")
    

    Also remember to include 4 buildpacks on Heroku to make sure everything will work (Your APP > Settings > Buildpacks):

    And check if you have all of these in your requirements.txt:

    • pip
    • youtube_dl
    • discord.py[voice]
    • ffmpeg
    • PyNaCl