首页 > 解决方案 > 不和谐机器人在哪里可以找到 ffmpeg buildpack heroku

问题描述

我正在制作一个机器人,它将一个 mp3 文件输出到与 discord.py 的语音聊天中,它通过以下方式在本地工作:

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

但是我现在将它托管在 Heroku 上,我已经安装了 buildpack,但是我的代码如何访问它来替换上面的代码

标签: pythonherokuffmpegdiscord.py

解决方案


您没有包含您的代码,所以我不知道您是否希望它在您在 discord 中编写命令或机器人准备好时播放,所以我决定让机器人在准备好时播放(如果您希望它加入在命令上你可以修改我的代码)。另外,我认为如果您为此使用youtube_dl会更容易。

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

还要记住在 Heroku 上包含 4 个构建包,以确保一切正常(Your APP > Settings > Buildpacks):

并检查您的requirements.txt中是否包含所有这些:

  • 点子
  • youtube_dl
  • discord.py[语音]
  • ffmpeg
  • 氯化钠

推荐阅读