我试着学习如何制作一个音乐机器人,播放命令很好,但是youtube的缩略图没有显示当url是在嵌入消息中给出的时候。Idk有什么功能让机器人将youtube视频缩略图显示到嵌入消息中。这是代码:
@client.command()
async def play(ctx, url : str):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the current playing music to end or use %leave <:_Paimon6:827074349450133524>")
return
voiceChannel = discord.utils.get(ctx.guild.voice_channels, name='Private')
await voiceChannel.connect()
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
em6 = discord.Embed(title = "Downloading Music", description = f'{url}\n\nPlease wait for paimon to setup the music you provide.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color)
await ctx.send(embed = em6, delete_after = 2)
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"))
em1 = discord.Embed(title = "Now Listening", description = f'{url}\n\nPlease use %leave first to change music.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color)
await ctx.send(embed = em1)发布于 2021-07-06 18:18:29
不管怎样,我想你是在找.set_image()方法。
您可以通过以下方式获得youtube视频的缩略图链接
https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg就像解释的here。
检索youtube视频的id
videoID = url.split("watch?v=")[1].split("&")[0]然后您可以将其设置为嵌入图像或缩略图。在你的例子中,应该是这样的
@client.command()
async def play(ctx, url : str):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the current playing music to end or use %leave <:_Paimon6:827074349450133524>")
return
voiceChannel = discord.utils.get(ctx.guild.voice_channels, name='Private')
await voiceChannel.connect()
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
em6 = discord.Embed(title = "Downloading Music", description = f'{url}\n\nPlease wait for paimon to setup the music you provide.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color)
await ctx.send(embed = em6, delete_after = 2)
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"))
em1 = discord.Embed(title = "Now Listening", description = f'{url}\n\nPlease use %leave first to change music.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color)
# get id
videoID = url.split("watch?v=")[1].split("&")[0]
em1.set_image(url = "https://img.youtube.com/vi/{videoID}/0.jpg".format(videoID = videoID))
await ctx.send(embed = em1)如果您发现这个图像太大,也可以通过embed.set_thumbnail(url)将其设置为缩略图。
https://stackoverflow.com/questions/68264434
复制相似问题