gpt4 book ai didi

python - FFmpegPCMaudio 在我的服务器上不起作用,但它在我的计算机上工作

转载 作者:行者123 更新时间:2023-12-04 23:33:08 27 4
gpt4 key购买 nike

我正在用 python 开发一个可以播放音乐的 Discord 机器人。当我在我的计算机上进行测试时,所有工作都可以找到,但是当我将所有文件放到我的服务器中时,它就会停止工作。所以我尝试在另一台计算机上运行它,不,它仍然无法正常工作。
首先我以为是电源问题,但另一台电脑几乎一样。我尝试将计算机上的所有软件包安装到第二个,但音乐部分仍然无法正常工作。这不是 Discord API 问题,因为我的其他命令可以正常工作。

import discord
from discord.ext import commands
from youtube_dl import YoutubeDL


class music_cog(commands.Cog):
def __init__(self, bot):
self.bot = bot

# all the music related stuff
self.is_playing = False

# 2d array containing [song, channel]
self.music_queue = []
self.YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'True'}
self.FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn'}

self.vc = ""

# searching the item on youtube
def search_yt(self, item):
with YoutubeDL(self.YDL_OPTIONS) as ydl:
try:
info = ydl.extract_info("ytsearch:%s" % item, download=False)['entries'][0]
except Exception:
return False

return {'source': info['formats'][0]['url'], 'title': info['title']}

def play_next(self):
if len(self.music_queue) > 0:
self.is_playing = True

# get the first url
m_url = self.music_queue[0][0]['source']

# remove the first element as you are currently playing it
self.music_queue.pop(0)

self.vc.play(discord.FFmpegPCMAudio(m_url, **self.FFMPEG_OPTIONS), after=lambda e: self.play_next())
else:
self.is_playing = False

# infinite loop checking
async def play_music(self):
if len(self.music_queue) > 0:
self.is_playing = True

m_url = self.music_queue[0][0]['source']

# try to connect to voice channel if you are not already connected

if self.vc == "" or not self.vc.is_connected() or self.vc == None:
self.vc = await self.music_queue[0][1].connect()
else:
await self.vc.move_to(self.music_queue[0][1])

print(self.music_queue)
# remove the first element as you are currently playing it
self.music_queue.pop(0)

self.vc.play(discord.FFmpegPCMAudio(m_url, **self.FFMPEG_OPTIONS), after=lambda e: self.play_next())
else:
self.is_playing = False

@commands.command(name="play", help="Plays a selected song from youtube")
async def p(self, ctx, *args):
query = " ".join(args)

voice_channel = discord.utils.get(ctx.guild.voice_channels, name='General')
if voice_channel is None:
# you need to be connected so that the bot knows where to go
await ctx.send("Connect to a voice channel!")
else:
song = self.search_yt(query)
if type(song) == type(True):
await ctx.send(
"Could not download the song. Incorrect format try another keyword. This could be due to playlist or a livestream format.")
else:
await ctx.send("Song added to the queue")
self.music_queue.append([song, voice_channel])

if self.is_playing == False:
await self.play_music()

@commands.command(name="queue", help="Displays the current songs in queue")
async def q(self, ctx):
retval = ""
for i in range(0, len(self.music_queue)):
retval += self.music_queue[i][0]['title'] + "\n"

print(retval)
if retval != "":
await ctx.send(retval)
else:
await ctx.send("No music in queue")

@commands.command(name="skip", help="Skips the current song being played")
async def skip(self, ctx):
if self.vc != "" and self.vc:
self.vc.stop()
# try to play next in the queue if it exists
await self.play_music()

@commands.command(name="disconnect", help="Disconnecting bot from VC")
async def dc(self, ctx):
await self.vc.disconnect()

@commands.command(name="pause")
async def pause(self, ctx):
voice = discord.utils.get(self.bot.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.pause()
else:
await ctx.send("Currently no audio is playing.")

@commands.command(name="resume")
async def resume(self, ctx):
voice = discord.utils.get(self.bot.voice_clients, guild=ctx.guild)
if voice.is_paused():
voice.resume()
else:
await ctx.send("The audio is not paused.")
没有任何错误消息,机器人只是加入语音聊天,什么也不做。
如果有人能告诉我它只在我的电脑上工作,我将非常感激。
我的文件:
my files

最佳答案

我终于找到了问题,在环境变量中我设置的 ffmpeg 一般不在路径中。当我将 ffmpeg 放入 cmd 时它什么也没做,所以我在好地方重新安装了 ffmpeg,它完成了所有工作,感谢您的帮助😁

关于python - FFmpegPCMaudio 在我的服务器上不起作用,但它在我的计算机上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72204048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com