Получение кода ошибки 1 при использовании youtube_dl для воспроизведения музыки на диске discord.py - PullRequest
2 голосов
/ 22 сентября 2019

Контекст: Я пытаюсь создать команду ;play <youtubeURL> для моего бота disord.py

Проблема: Я не могу заставить музыку играть

Код:

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.

Буду признателен за любую помощь / решения, которые вы можете дать.

Ответы [ 2 ]

1 голос
/ 29 сентября 2019

Я обнаружил проблему с моим кодом.Это правильный способ воспроизведения музыки через 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)
0 голосов
/ 22 сентября 2019

Файл, который вы хотите использовать, в данный момент не существует, потому что вы не загрузили его из-за параметра download=False в extract_info.

...