gpt4 book ai didi

python - youtubedl 在 python 音乐机器人中停留在 'downloading webpage'

转载 作者:行者123 更新时间:2023-12-04 23:18:48 28 4
gpt4 key购买 nike

我已经设置好了所有东西,并且所有东西都正确安装了。当我尝试使用 !play 命令运行机器人时,回溯只是在下载网页上。完全没有错误。我怀疑这可能是源错误,但我不确定。

@commands.command()
async def play(self, ctx, url):
if ctx.voice_client is None:
if ctx.author.voice:
await ctx.author.voice.channel.connect()
else:
await ctx.send("You're not connected to a voice channel.")
raise commands.CommandError("Author not connected to voice channel.")
elif ctx.voice_client.is_playing():
ctx.voice_client.stop()
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnected_delay_max 5', 'options': '-vn'}
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client

with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
url2 = info["formats"][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
vc.play(source)

最佳答案

我稍微改变了你的代码。
您将需要 YoutubeSearch模块pip install youtube-search

from youtube_search import YoutubeSearch

@client.command()
async def play(ctx,*, song):
voice_state = ctx.member.voice

if voice_state == None:
await ctx.send("You are not in a voice channel!")
return

if voice_state != None:
await ctx.author.voice.channel.connect()

results = YoutubeSearch(song, max_results=1).to_dict()
print(results)
song_name = results[0]['title']
url_suffix = results[0]['url_suffix']
url = "https://youtube.com"+url_suffix

playinge = discord.Embed(
title = "Now Playing",
description = f"[{song_name}]({url})",
color = discord.Colour.green()
)

ydl_opts = {'format': 'bestaudio'}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
URL = info['formats'][0]['url']

vc = ctx.voice_client
vc.play(discord.FFmpegPCMAudio(URL))

await ctx.send(embed=playinge)
尝试这个。

关于python - youtubedl 在 python 音乐机器人中停留在 'downloading webpage',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71110750/

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