我制作了一个不和谐的音乐机器人,它工作得很好,直到 youtube_dl 发生了一些事情,它无法传输视频并出现错误“无法提取上传者 ID”。我决定改用 yt-dlp。但现在,当我尝试播放视频时,它连接到语音通道,但不播放任何内容,并立即在控制台中显示错误“[out#0/s16le @ 000001ad295ae800] 输出文件不包含任何流”。任何人都可以分享一个解决方案,使其与 youtube_dl 或 yt-dlp 一起使用,如果它们有效的话,对我来说都可以。顺便说一句,抱歉,我是新人,我不太会提出干净的问题。这是我的机器人的代码:
import discord
from discord.ext import commands
import time
import yt_dlp
import asyncio
from requests import get
import datetime
start_time = time.time()
default_intents = discord.Intents.all()
activity = discord.Activity(type=discord.ActivityType.listening, name="@philipou")
bot = commands.Bot(command_prefix=".", intents=default_intents, activity=activity, status=discord.Status.online)
musics = {}
ytdl = yt_dlp.YoutubeDL()
queue = []
@bot.event
async def on_ready():
print("The bot is ready !")
class Video:
def __init__(self, arg):
try:
get(arg)
except:
video = ytdl.extract_info(f"ytsearch:{arg}", download=False)["entries"][0]
else:
video = ytdl.extract_info(arg, download=False)
video_format = video["requested_formats"][0]
self.url = video["webpage_url"]
self.stream_url = video_format["url"]
@bot.command(name="stop", help="Stops the current track.", )
async def stop(ctx):
global queue
client = ctx.guild.voice_client
musics[ctx.guild] = []
if client:
await client.disconnect()
await ctx.send("Disconnected from the voice channel.")
else:
await ctx.send("I'm not in a voice channel !")
@bot.command(name="resume", help="Resume the current paused track.")
async def resume(ctx):
client = ctx.guild.voice_client
if client.is_paused():
client.resume()
await ctx.send("Resuming the current track.")
else:
await ctx.send("I'm not paused !")
@bot.command(name="pause", help="Pause the current playing track.")
async def pause(ctx):
client = ctx.guild.voice_client
if not client.is_paused():
client.pause()
await ctx.send("Pausing the current playing track.")
else:
await ctx.send("I'm already paused !")
async def play_song(client, queue, song):
source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(song.stream_url,
before_options="-reconnect 1 -reconnect_streamed 1 "
"-reconnect_delay_max 5 "))
# noinspection PyTypeChecker
def next(_):
global queue
if "loop" in queue:
asyncio.run_coroutine_threadsafe(play_song(client, queue, song), bot.loop)
elif len(queue) > 0:
new_song = queue[0]
del queue
queue = [0]
asyncio.run_coroutine_threadsafe(play_song(client, queue, new_song), bot.loop)
else:
asyncio.run_coroutine_threadsafe(client.disconnect(), bot.loop)
client.play(source, after=next)
@bot.command(name="play", help="Play a song from a search query or url.")
async def play(ctx, *, url):
client = ctx.guild.voice_client
video = Video(url)
musics[ctx.guild] = []
channel = ctx.author.voice.channel
if client and client.channel:
await client.disconnect()
time.sleep(1)
client = await channel.connect()
await ctx.send(f"Playing : {video.url}")
await play_song(client, musics[ctx.guild], video)
else:
await ctx.send(f"Playing : {video.url}")
client = await channel.connect()
await play_song(client, musics[ctx.guild], video)
@bot.command(name="ping", help="Check the bot's ping/latency.")
async def bot_ping(ctx):
await ctx.channel.send(f"Bot have {round(bot.latency * 1000)} ms of ping")
@bot.command(name="uptime", help="Check the bot's uptime")
async def bot_uptime(ctx):
current_time = time.time()
difference = int(round(current_time - start_time))
text = str(datetime.timedelta(seconds=difference))
await ctx.channel.send("Bot have " + text + " of uptime")
@bot.command(name="about", help="Give information about the bot.")
async def about(ctx):
await ctx.send("Hey ! I'm a nice bot made to listen to music from youtube (and i also have some other "
"funtionalities), for help type `.help`, for support contact `Philipou#6977`")
@bot.command(name="loop", help="Toggle loop for the current track.")
async def loop(ctx):
client = ctx.guild.voice_client
try:
if not client.is_playing():
return
except AttributeError:
await ctx.send("I'm not playing anything !")
return
if "loop" in queue:
queue.remove("loop")
await ctx.send("Looping disabled for the current track.")
else:
queue.append("loop")
await ctx.send("Looping enabled for the current track.")
bot.run("token :)")
我尝试了this以及youtube_dl和yt-dlp
代替:
self.stream_url = video_format["url"]
尝试一下:
self.stream_url = video["url"]
并删除
video_format
变量。