I'm trying to make a Discord bot in Python that plays Youtube videos as audios in a voice channel. Right now, my bot uses a combination using yt_dlp
and youtube_search
. It firstly uses youtube_search
to get the URL of the video searched by keyword. Then, yt_dlp
gets the direct audio link given to discord.FFmpegPCMAudio
. However, this takes multiple seconds and I feel like it could be made shorter.
I found this post : Searching Youtube videos using youtube-dl that suggests to use this :
from requests import get
from youtube_dl import YoutubeDL
YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist':'True'}
def search(arg):
with YoutubeDL(YDL_OPTIONS) as ydl:
try:
get(arg)
except:
video = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
else:
video = ydl.extract_info(arg, download=False)
return video
This looks great to me, however I'm trying to get the top 5 results. I found the equivalent of that command from the same module, but on my terminal, which is, for the keyword hello
yt-dlp ytsearch5:hello --get-url --get-title --print uploader
Can anyone help me turning that into a Python command? Thanks