使用yt_dlp在discord.py中播放一首歌。

huangapple go评论66阅读模式
英文:

Using yt_dlp in discord.py to play a song

问题

Youtube-dl突然停止工作,并出现了上传者ID错误,所以在阅读了一些文章后,我决定使用yt-dlp作为替代,但当我尝试运行下面的命令时,它只给我一个"输出文件#0不包含任何流"的错误。这是我的代码:

@commands.command()
async def rick(self, ctx):
    voice_channel = ctx.author.voice.channel
    if ctx.voice_client is None:
        await voice_channel.connect()

    if ctx.voice_client.is_playing():
        await ctx.send("something is currently playing...")
        return

    FFMPEG_OPTIONS = {
        'before_options':
        '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -probesize 200M',
        'options': '-vn'
    }
    YDL_OPTIONS = { 'format': 'm4a/bestaudio/best',

    'postprocessors': [{
    'key': 'FFmpegExtractAudio',
    'preferredcodec': 'm4a',}]
                  }
    with yt_dlp.YoutubeDL(YDL_OPTIONS) as ydl:
        secret = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
        info = ydl.extract_info(secret, download=False)
        url2 = ydl.sanitize_info(info)
        url2 = info['formats'][0]['url']
        source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
        vc = ctx.voice_client
        vc.play(source)

我尝试更改了代码,但没有获得任何结果。阅读yt-dlp文档后,我尝试添加了"sanitize_info"模块,但仍然收到相同的错误。感谢帮助!

英文:

Youtube-dl suddenly stopped working and was giving me uploader id errors so after reading a few articles I decided to use yt-dlp as an alternative but as I try to run the command below it just gives me an "Output file #0 does not contain any stream" error. Here's my code:

    @commands.command()
    async def rick(self, ctx):
        voice_channel = ctx.author.voice.channel
        if ctx.voice_client is None:
            await voice_channel.connect()
    
        if ctx.voice_client.is_playing():
            await ctx.send("something is currently playing...")
            return
    
        FFMPEG_OPTIONS = {
            'before_options':
            '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -probesize 200M',
            'options': '-vn'
        }
        YDL_OPTIONS = { 'format': 'm4a/bestaudio/best',

        'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'm4a',}]
                      }
        with yt_dlp.YoutubeDL(YDL_OPTIONS) as ydl:
            secret = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
            info = ydl.extract_info(secret, download=False)
            url2 = ydl.sanitize_info(info)
            url2= info['formats'][0]['url']
            source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
            vc = ctx.voice_client
            vc.play(source)

I've tried changing the code but I wasn't getting any results. Reading the yt-dlp documentation I tried adding the sanitize info module but i was still getting the same error. Thanks for the help!

答案1

得分: 1

已解决!只需更改我的ydl选项。

@commands.command()
async def rick(self, ctx):
    voice_channel = ctx.author.voice.channel
    if ctx.voice_client is None:
        await voice_channel.connect()

    if ctx.voice_client.is_playing():
        await ctx.send("something is currently playing...")
        return

    FFMPEG_OPTIONS = {
        'before_options':
        '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -probesize 200M',
        'options': '-vn'
    }
    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
        'quiet': True,
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        secret = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
        info = ydl.extract_info(secret, download=False)
        url2 = info['url']
        print(url2)
        source = discord.FFmpegPCMAudio(url2)
        vc = ctx.voice_client
        vc.play(source)
英文:

Solved it!
just had to change my ydl options

    @commands.command()
async def rick(self, ctx):
    voice_channel = ctx.author.voice.channel
    if ctx.voice_client is None:
        await voice_channel.connect()

    if ctx.voice_client.is_playing():
        await ctx.send("something is currently playing...")
        return

    FFMPEG_OPTIONS = {
        'before_options':
        '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -probesize 200M',
        'options': '-vn'
    }
    ydl_opts = {
    'format': 'bestaudio/best',
    'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
    'quiet': True,
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        secret = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
        info = ydl.extract_info(secret, download=False)
        url2 = info['url']
        print(url2)
        source = discord.FFmpegPCMAudio(url2)
        vc = ctx.voice_client
        vc.play(source)

huangapple
  • 本文由 发表于 2023年3月9日 14:08:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定