import yt_dlp import os DOWNLOAD_TYPES = ["best/mp4", "worst/mp4", "mp3"] def get_playlist_name(url: str) -> str: ydl_opts = { 'format': 'worstaudio/worst', "quiet": True, "simulate": True, "extract_flat": True, "force_generic_extractor": True, } with yt_dlp.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(url, download=False) if "title" in info: return info["title"] else: return None def playlist_hook(d): print(d) def download_playlist(url: str, download_type: str, directory: str, numbers: bool) -> None: # Get name and set download directory. playlist_name = get_playlist_name(url) if playlist_name is None: playlist_name = "playlist" download_directory = os.path.join(directory, playlist_name) os.mkdir(download_directory) # Set options. ydl_opts = {} format_mask = "%(title)s.%(ext)s" if numbers: format_mask = "%(playlist_index)s %(title)s.%(ext)s" if download_type == "mp3": ydl_opts = { "paths": {"home": download_directory}, "outtmpl": {"default": format_mask}, "progress_hooks": [playlist_hook], "postprocessors": [{ "key": "FFmpegExtractAudio", "preferredcodec": "mp3", }] } else: ydl_opts = { "format": download_type, "paths": {"home": download_directory}, "outtmpl": {"default": format_mask}, "progress_hooks": [playlist_hook] } with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download(url)