-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathplaylist_downloader.py
42 lines (33 loc) · 1.4 KB
/
playlist_downloader.py
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
import os
import yt_dlp
def make_alpha_numeric(string):
return ''.join(char for char in string if char.isalnum())
link = input("Enter YouTube Playlist URL: ✨")
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'merge_output_format': 'mp4',
'outtmpl': '%(title)s.%(ext)s',
'noplaylist': False,
'quiet': False,
'postprocessors': [
{'key': 'FFmpegVideoConvertor', 'preferedformat': 'mp4'}
]
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
playlist_info = ydl.extract_info(link, download=False)
playlist_title = make_alpha_numeric(playlist_info['title'])
if not os.path.exists(playlist_title):
os.mkdir(playlist_title)
totalVideoCount = len(playlist_info['entries'])
print("Total videos in playlist: 🎦", totalVideoCount)
ydl_opts['outtmpl'] = os.path.join(playlist_title, '%(title)s.%(ext)s')
with yt_dlp.YoutubeDL(ydl_opts) as ydl2:
for index, video in enumerate(playlist_info['entries'], start=1):
try:
print(f"\nDownloading: {video['title']}")
ydl2.download([video['webpage_url']]) # Use 'webpage_url' here
print(f"Downloaded: {video['title']} ✨ successfully!")
print("Remaining Videos:", totalVideoCount - index)
except Exception as e:
print(f"Error downloading {video['title']}: {e}")
print("\nAll videos downloaded successfully! 🎉")