首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.py在嵌入消息中发送youtube缩略图

Discord.py在嵌入消息中发送youtube缩略图
EN

Stack Overflow用户
提问于 2021-07-06 04:11:22
回答 1查看 398关注 0票数 0

我试着学习如何制作一个音乐机器人,播放命令很好,但是youtube的缩略图没有显示当url是在嵌入消息中给出的时候。Idk有什么功能让机器人将youtube视频缩略图显示到嵌入消息中。这是代码:

代码语言:javascript
复制
@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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-06 18:18:29

不管怎样,我想你是在找.set_image()方法。

您可以通过以下方式获得youtube视频的缩略图链接

代码语言:javascript
复制
https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg

就像解释的here

检索youtube视频的id

代码语言:javascript
复制
videoID = url.split("watch?v=")[1].split("&")[0]

然后您可以将其设置为嵌入图像或缩略图。在你的例子中,应该是这样的

代码语言:javascript
复制
@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)将其设置为缩略图。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68264434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档