1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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)
|