【问题标题】:Getting error code 1, when using youtube_dl to play music on discord.py bot使用 youtube_dl 在 discord.py bot 上播放音乐时出现错误代码 1
【发布时间】:2020-01-22 18:02:24
【问题描述】:

上下文:我正在尝试为我的 disord.py 机器人创建一个 ;play <youtubeURL> 命令

问题:我无法播放音乐

代码:

ytdl_format_options = {
    'format': 'bestaudio/best',
    'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
    'restrictfilenames': True,
    'noplaylist': True,
    'nocheckcertificate': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'quiet': True,
    'no_warnings': True,
    'default_search': 'auto',
    'source_address': '0.0.0.0'
}

ytdl = youtube_dl.YoutubeDL(ytdl_format_options)

@client.command()
async def play(ctx, url):

    #I have code here that makes sure the bot is in the correct VC

    guild = ctx.message.guild
    voice_client = guild.voice_client

    song_info = ytdl.extract_info(url, download=False)
    filename = ytdl.prepare_filename(song_info)
    song = discord.FFmpegPCMAudio(filename)
    player = voice_client.play(song)

错误:使用标准的 python IDE,我没有收到任何错误。但是,使用日志记录模块。我收到错误代码 1:

INFO:discord.player:Preparing to terminate ffmpeg process 21972.
INFO:discord.player:ffmpeg process 21972 successfully terminated with return code of 1.

如果您能提供任何帮助/解决方案,我们将不胜感激。

【问题讨论】:

    标签: python discord.py youtube-dl


    【解决方案1】:

    您要使用的文件暂时不存在,因为extract_info中的download=False参数,您没有下载它。

    【讨论】:

    • 这不是唯一的问题,看我的回答。
    【解决方案2】:

    我发现我的代码存在问题。这是通过ffmpeg播放音乐的正确方法:

    import os
    import youtube_dl
    
    async def joinMusicChannel(ctx):
        try:
            channel = ctx.author.voice.channel
        except:
            await ctx.send(ctx.author.mention + " Please join the music voice channel.")
            return False
    
        vc = ctx.voice_client
        if vc == None:
            await channel.connect()
        return True
    
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    
    def endSong(guild, path):
        os.remove(path)
    
    
    @client.command()
    async def play(ctx, url):
        data = await joinMusicChannel(ctx)
        if data == True:
            with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                file = ydl.extract_info(url, download=True)
            guild = ctx.message.guild
            voice_client = guild.voice_client
            path = str(file['title']) + "-" + str(file['id'] + ".mp3")
    
            voice_client.play(discord.FFmpegPCMAudio(path), after=lambda x: endSong(guild, path))
            voice_client.source = discord.PCMVolumeTransformer(voice_client.source, 1)
    

    【讨论】:

      猜你喜欢
      • 2021-02-06
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 2019-05-20
      • 2020-06-25
      • 2021-04-09
      相关资源
      最近更新 更多