-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube_downloader.py
90 lines (69 loc) · 2.28 KB
/
youtube_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from pytube.cli import on_progress
from pytube import YouTube
from pytube import Playlist
from pytube.extract import playlist_id
# from pytube.extract import video_id
import os
def path_creator():
#location where file will be downloaded
des_path = os.path.join(os.environ["USERPROFILE"], "Desktop", "Youtube")
if os.path.exists(des_path):
return des_path
else:
#Create Youtube folder on desktop if it does not exist
os.makedirs(des_path, exist_ok=True)
return des_path
def is_playlist(url):
"""
url: str
Return: True(Bolean) if url is of a playlist
"""
try:
pl_id = playlist_id(url)
return True
except KeyError:
return False
def youtube_url(url):
"""
url: str
Return: A list of video urls in a playlist
"""
playlist = Playlist(url)
return playlist.video_urls
def downloader(url):
"""
url: str
Return: None
Download video of resolution 720p or highest available resolution
"""
try:
yt_obj = YouTube(url, on_progress_callback=on_progress)
title = yt_obj.title
print(f"\n{title} is downloading...")
stream_obj = yt_obj.streams
try:
selected_stream = stream_obj.filter(progressive=True, resolution="720p")[0]
except:
selected_stream = stream_obj.get_highest_resolution()
finally:
selected_stream.download(output_path=path_creator())
except Exception as err:
return err
else:
return f"{title} downloaded\n"
# main driver
if __name__ == '__main__':
print("Initilizing the script...\n")
url = input("Enter the url of the youtube playlist or video:\n")
while True:
if is_playlist(url):
for vid_url in youtube_url(url):
print(downloader(vid_url))
else:
print(downloader(url))
print(r"NOTE: You can find your downloads in C:\Users\DRB\Desktop\Youtube.\n")
command = input("Press any key to close program and 'N' to continue more downloading: ")
if "N" != command.upper():
break
url = input("\nEnter the url of the youtube playlist or video:\n")
# print("\n====== Done - Check Download Folder =======")