From ef55effa58ee01089bffb441a65e932393d9facb Mon Sep 17 00:00:00 2001 From: nathansmith117 Date: Wed, 27 Mar 2024 16:11:09 -0600 Subject: Working on yt-dlp --- src/youload_downloader.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/youload_downloader.py (limited to 'src/youload_downloader.py') diff --git a/src/youload_downloader.py b/src/youload_downloader.py new file mode 100644 index 0000000..5800ed2 --- /dev/null +++ b/src/youload_downloader.py @@ -0,0 +1,63 @@ +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) -- cgit v1.2.3