pythondiscorddiscord.pyytdlyt-dlp

Using yt_dlp in discord.py to play a song


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!


Solution

  • 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)