Avoid live streams with Youtube-dl search

Viewed 582

I am having a problem with a program that does a keyword search on Youtube-DL and displays the results. (In this case, Amout is 5, so it searches 5 times.)

This code was created from this tutorial: https://www.youtube.com/watch?v=46ZHJcNnPJ8

The problem is that when the search results include a live stream, Youtube-DL returns "ERROR: requested format not available" and the contents of the function (info) remain empty. (It doesn't seem to contain any other results either.)

By using the "ignoreerrors":True option, you will not be stopped by errors, but errors will still be visible and the "info" will still be empty.

Is there an option in Youtube_dl to bypass the search for live streams? Or is there another solution? Any help would be much appreciated.

My code

async def search_song(self, amount, song, get_url=False):
     info = await self.bot.loop.run_in_executor(None, lambda: youtube_dl.YoutubeDL({"format" : "bestaudio",'default_search': 'auto',"ignoreerrors":True,}).extract_info(f"ytsearch{amount}:{song}", download=False, ie_key="YoutubeSearch"))
     if len(info["entries"]) == 0: return None

     return [entry["webpage_url"] for entry in info["entries"]] if get_url else info

log(The first video would be the live stream.)

[download] Downloading video 1 of 5
[youtube] q8A_TvjvMjo: Downloading webpage
[youtube] q8A_TvjvMjo: Downloading m3u8 information
[youtube] q8A_TvjvMjo: Downloading MPD manifest
ERROR: requested format not available
[download] Downloading video 2 of 5
[youtube] B1go9DPd_xg: Downloading webpage
[download] Downloading video 3 of 5
[youtube] XGSSmQiqBl8: Downloading webpage
[download] Downloading video 4 of 5
[youtube] u962IzRYPIE: Downloading webpage
ERROR: Premieres in 18 hours
[download] Downloading video 5 of 5
[youtube] WYYj2DUPHGI: Downloading webpage
[download] Finished downloading playlist: sleep
Ignoring exception in command search:
Traceback (most recent call last):
  File "C:\Users\kogetu\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(args, **kwargs)
  File "C:\Bot\PiyoMusicBot\music.py", line 147, in search
    embed.description += (f"[{entry['title']}]({entry['webpage_url']})\n")
TypeError: 'NoneType' object is not subscriptable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\kogetu\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\kogetu\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(ctx.args, **ctx.kwargs)
  File "C:\Users\kogetu\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'NoneType' object is not subscriptable
0 Answers
Related