首页 > 解决方案 > How with youtube_dl i can play playlist in discord.py

问题描述

Here is my code of play command

@client.command(pass_context=True)
async def play(ctx, url):   
    try:
        voice_channel = ctx.message.author.voice.channel
        vc = await voice_channel.connect()
    except:
        print('already connected')

    voice = get(client.voice_clients, guild=ctx.guild)
    with YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=False)
    if 'entries' in info:
        video = info['entries'][0]
        if(voice.is_playing()):
            voice.stop()
            voice.play(FFmpegPCMAudio(video,executable = "C:/Users/Gleb/Desktop/ffmpeg/bin/ffmpeg.exe", **FFMPEG_OPTIONS))
        else:
            voice.play(FFmpegPCMAudio(video,executable = "C:/Users/Gleb/Desktop/ffmpeg/bin/ffmpeg.exe", **FFMPEG_OPTIONS))
    else:
        URL = info['formats'][0]['url']
        if(voice.is_playing()):
            voice.stop()
            voice.play(FFmpegPCMAudio(UE,executable = "C:/Users/Gleb/Desktop/ffmpeg/bin/ffmpeg.exe", **FFMPEG_OPTIONS))
        else:
            voice.play(FFmpegPCMAudio(video,executable = "C:/Users/Gleb/Desktop/ffmpeg/bin/ffmpeg.exe", **FFMPEG_OPTIONS))

enter image description here

i get an error when i play playlist that the 'video' is a dictionary but it only takes string

标签: pythondiscorddiscord.py

解决方案


你的程序不会像这样工作。声明为的文档状态FFmpegPCMAudio

class discord.FFmpegPCMAudio(source, *, executable='ffmpeg', pipe=False, stderr=None, before_options=None, options=None)

您将字典video作为source. 但是,FFmpegPCMAudio()不能缓冲视频流。它只能播放本地保存的文件,这些文件通过FFmpeg. source必须是文件的链接,就像你在参数中一样executable。既然你有download = False争论

info = ydl.extract_info(url, download=False)

您只是提取视频信息,而不是实际下载它。如果没有文件,则无法播放。


要解决您的问题,您需要下载 youtube 视频的音频,并将文件FFmpegPCMAudio()路径指定为source


推荐阅读