Youtube Dll can't extract video metadata

333 Views Asked by At

Hi I am having problem with youtube-dl I have check and have the latest version. I am usinig python and trying to make discord music bot but when i enter a title of video or a url youtube dl has an issue with extracting it's metadata

This is my code

@bot.command()
async def play(ctx, *, query):
    try:
        # Use youtube-dl to extract video information
        ydl_opts = {
            'format': 'bestaudio/best',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }],'default_search': 'ytsearch',  
            # Use YouTube search
        }

        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(query, download=False)
            if 'entries' in info:
                video = info['entries'][0]
            else:
                video = info

            # Print all the metadata to the console
            print(f'Title: {video["title"]}')
            print(f'Duration: {video["duration"]} seconds')
            print(f'URL: {video["url"]}')

        # Extract the audio stream URL
        audio_url = video['url']

        # Call the play_audio function to play the audio
        await play_audio(ctx, audio_url)

    except youtube_dl.DownloadError as e:
        # Handle errors when video is not found or URL is invalid
        print(f"Error: {e}")
        await ctx.send("Sorry, I couldn't find that video or the URL is invalid.")

I belive problem lies somewhere here i tried reading documentation but can't find a solution also if the problem isnt here this is a play audio function

async def play_audio(ctx, audio_url):
    # Call the join_voice_channel function to ensure the bot is in the voice channel
    await join_voice_channel(ctx)

    # Play the audio in the voice channel
    voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    if voice_client:
        voice_client.play(discord.FFmpegPCMAudio(audio_url))
        # Wait for the audio to finish playing before disconnecting (optional)
        while voice_client.is_playing():
            await asyncio.sleep(1)
        await voice_client.disconnect()
    else:
        print('Bot is not in a voice channel.')

Bot is complex and i dont want to pulbish enitre code as it's well over 1000lines

This is en error message i get when trying to play audio the warniing part is repeated the same like 15 times and i am gettinig the same meassage when instead of title i give it an actual url

[download] Downloading playlist: rock you

[youtube:search] query "rock you": Downloading page 1

[youtube:search] playlist rock you: Downloading 1 videos

[download] Downloading video 1 of 1

[youtube] -tJYN-eG1zk: Downloading webpage

[youtube] -tJYN-eG1zk: Downloading API JSON

WARNING: unable to extract player URL; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see https://yt-dl.org/update on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.

If someone is able to find an error and explain why it's an error and possibly find a fix i Would be very welcome

0

There are 0 best solutions below