-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathyoutube.py
76 lines (56 loc) · 2.45 KB
/
youtube.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
from pytube import YouTube
from pytube import Playlist
#Gives download progress
def on_progress(stream, chunk, bytes_remaining):
progress = f"{round(100 - (bytes_remaining/stream.filesize * 100),2)}%"
print(f"progress: {progress}")
#Do this on download completion
def on_complete(stream, file_path):
print("Download Completed")
print(f"Download path: {file_path}")
#get playlist url from user
pl_url = input("Please enter Playlist URL:\n")
#Create Playlist obj
pl = Playlist(pl_url)
#Num of videos in playlist
video_count = pl.length
remaining_video_count = 0
print(f"Number of videos in the playlist: {video_count}")
print("Downloading started...")
#for every video in the playlist
for vids in pl.videos:
vid_url = vids.watch_url
yt = YouTube(url = vid_url, on_progress_callback=on_progress, on_complete_callback=on_complete) #create Youtube obj
res_480p = yt.streams.get_by_resolution('480p')
res_720p = yt.streams.get_by_resolution('720p')
res_360p = yt.streams.get_by_resolution('360p')
if(res_480p and yt.streams.filter(file_extension='mp4', progressive=True)): #if the resolution match 480p
title = yt.title
print(f"Video title: {title}")
print("Video download quality: 480p")
file_size = round((res_480p.filesize / 1000000), 2)
print(f"File Size: {file_size} MB")
res_480p.download()
remaining_video_count += 1
print("\n")
elif(res_720p and yt.streams.filter(file_extension='mp4', progressive=True)): #if the resolution match 720p
title = yt.title
print(f"Video title: {title}")
print("Video download quality: 720p")
file_size = round((res_720p.filesize / 1000000), 2)
print(f"File Size: {file_size} MB")
res_720p.download()
remaining_video_count += 1
print("\n")
elif(res_360p and yt.streams.filter(file_extension='mp4', progressive=True)): #if the resolution match 360p
title = yt.title
print(f"Video title: {title}")
print("Video download quality: 360p")
file_size = round((res_360p.filesize / 1000000), 2)
print(f"File Size: {file_size} MB")
res_360p.download()
remaining_video_count += 1
print("\n")
else:
print("Low quality video. No video will be downloaded")
print(f"Remaining: {remaining_video_count} out of {video_count}")