如果我通过我的PC启动它,我的heroku音乐机器人可以正常工作,但是当我通过heroku运行它时,它却无法正常工作

时间:2020-03-05 21:06:13

标签: python heroku ffmpeg bots discord

我安装了ffmpeg,安装了youtube-dl,安装了所有要求,下载了重命名的歌曲,但是什么也没发生!

@client.command()
async def play(ctx, *url: str):
    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
            print("Removed old song file")
    except PermissionError:
        print("Trying to delete song file, but it's being played")
        await ctx.send("ERROR: Music playing")
        return

    await ctx.send("Getting everything ready now")

    voice =  get(client.voice_clients,  guild=ctx.guild)

    ydl_opts = {
        'format': 'bestaudio/best',
        'quiet': True,
        'outtmpl':"./song.mp3",
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }

    song_search = " ".join(url)

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        print("Downloading audio now\n")
        ydl.download([f"ytsearch1:{song_search}"])

    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            name = file
            print(f"Renamed File: {file}\n")
            os.rename(file, "song.mp3")

    voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: print(f"{name} has finished playing"))
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.10

    nname = name.rsplit("-", 2)
    await ctx.send(f"Currently playing: {nname}")
    print("playing\n")

1 个答案:

答案 0 :(得分:0)

Heroku支持将ffmpeg作为您应用程序的构建包。只需在https://dashboard.heroku.com/apps/[app-name]/settings上打开您的应用设置,然后向下滚动到Buildpacks,即可在其中添加https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git

或者,您也可以运行CLI命令:

$ heroku buildpacks:add https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git

来源:

https://elements.heroku.com/buildpacks/jonathanong/heroku-buildpack-ffmpeg-latest