-
Notifications
You must be signed in to change notification settings - Fork 175
/
youtube_downloader.py
44 lines (34 loc) · 1.09 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
import pytube
def download_video(url, resolution):
itag = choose_resolution(resolution)
video = pytube.YouTube(url)
stream = video.streams.get_by_itag(itag)
stream.download()
return stream.default_filename
def download_videos(urls, resolution):
for url in urls:
download_video(url, resolution)
def download_playlist(url, resolution):
playlist = pytube.Playlist(url)
download_videos(playlist.video_urls, resolution)
def choose_resolution(resolution):
if resolution in ["low", "360", "360p"]:
itag = 18
elif resolution in ["medium", "720", "720p", "hd"]:
itag = 22
elif resolution in ["high", "1080", "1080p", "fullhd", "full_hd", "full hd"]:
itag = 137
elif resolution in ["very high", "2160", "2160p", "4K", "4k"]:
itag = 313
else:
itag = 18
return itag
def input_links():
print("Enter the links of the videos (end by entering 'STOP'):")
links = []
link = ""
while link != "STOP" and link != "stop":
link = input()
links.append(link)
links.pop()
return links