-
Notifications
You must be signed in to change notification settings - Fork 0
/
yt_dl.py
194 lines (131 loc) · 7.23 KB
/
yt_dl.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#Getting/downloading Youtube-video, Youtube-playlist, Video info, Thumbnails, Audio of the video, etc. using Pytube.
#===================================================Youtube-Video===================================================#
# from pytube import YouTube
# from pytube.cli import on_progress
#Enter the 'video link'
# yt=YouTube('https://www.youtube.com/watch?v=yTWnTAUiY4I', on_progress_callback = on_progress)
# yt=YouTube(input('Enter the video link: '), on_progress_callback = on_progress)
##'Download' any youtube video.
# print(f'Downloading.....\n\nTitle: {yt.title}.')
# yt.streams.get_highest_resolution().download()
# yt.streams.filter(mime_type='video/mp4', res='720p', progressive=True).first().download()
# yt=YouTube('https://youtu.be/kM3G35yhyDw')
# print(f'{round((yt.streams.get_highest_resolution().filesize)/10**6, 2)} MB')
# from pytube.cli import on_progress
# yt=YouTube('https://youtu.be/kM3G35yhyDw', on_progress_callback=on_progress)
# Quality_lst=['144p','360p','480p','720p','1080p']
# yt.streams.filter(res= Quality_lst[0]).first().download()
#Video-File Size Calculation.
# yt=YouTube(input('Enter the video link: '))
# print('\nCalculating.... ')
# # vid_size=(yt.streams.get_highest_resolution().filesize)/1024**2
# vid_size=(yt.streams.filter(res='1080p').first().filesize)/1024**2
# print(f'\nVideo File Size of "{yt.title}" is: \n{vid_size} MB')
#=================================================Youtube-playlist=================================================#
# from pytube import Playlist
# from pytube import YouTube
# from pytube.cli import on_progress
#Getting the playlist link.
# P_lst = Playlist('https://www.youtube.com/playlist?list=PLu0W_9lII9ah7DDtYtflgwMwpT3xmjXY9')
# P_lst = Playlist(input('Enter the playlist link: '))
# print(P_lst.title)
#Printing all video-tiles of the playlist
# for index , video in enumerate(P_lst.videos):
# print(f'{index+1}.) {video.title}')
#Youtube-Playlist-Duration (Alternative-link: https://ytplaylist-len.herokuapp.com/)
# import datetime
# P_lst_time=0
# for video in P_lst.videos:
# P_lst_time+=video.length
# Formated_time= str(datetime.timedelta(seconds = P_lst_time)).split(":")
# print(f"{Formated_time[0]}hr {Formated_time[1]}min {Formated_time[2]}sec")
#Download any part of playlist
# Required_vid_index=[1, 2, '5-10', '15-30', 32, 34]
# for index in Required_vid_index:
# if str(index).isnumeric():
# P_lst.videos[index-1].streams.get_highest_resolution().download(filename_prefix=f"{index}.) ")
# else:
# range_index=int(index.split('-')[0])
# for vid in P_lst.videos[int(index.split('-')[0])-1 : int(index.split('-')[1])]:
# vid.streams.get_highest_resolution().download(filename_prefix=f"{range_index}.) ")
# range_index+=1
#Download Full playlist
# for index , video in enumerate(P_lst.videos):
# video.streams.get_highest_resolution().download(filename_prefix=f"{index+1}.) ")
#Download full-playlist with progress-bar.
#Type1:
# Quality_lst=['144p','360p','480p','720p','1080p']
# for index , video_url in enumerate(P_lst.video_urls):
# yt=YouTube(video_url, on_progress_callback=on_progress)
# print(f'{index+1}.) {yt.title}')
# yt.streams.filter(res= Quality_lst[4]).first().download(filename_prefix=f"{index+1}.) ")
#Type2:
# print('➡Downloading...\n')
# for index , video_url in enumerate(P_lst.video_urls):
# yt=YouTube(video_url, on_progress_callback=on_progress)
# print(f'{index+1}.) {yt.title}')
# yt.streams.get_highest_resolution().download(filename_prefix=f"{index+1}.) ")
# print(f'\n☑Downloading Completed!')
#Download any 'part of playlist' with 'progress bar'
# from pytube import YouTube
# from pytube.cli import on_progress
# print('➡Downloading...\n')
# Required_vid_index=[1, 2, '5-10', '15-30', 32, 34]
# for index in Required_vid_index:
# if str(index).isnumeric():
# yt=YouTube(P_lst.video_urls[index-1], on_progress_callback= on_progress)
# print(f'{index}.) {yt.title}')
# yt.streams.get_highest_resolution().download(filename_prefix=f"{index}.) ")
# else:
# range_index=int(index.split('-')[0])
# for vid_url in P_lst.video_urls[int(index.split('-')[0])-1 : int(index.split('-')[1])]:
# yt=YouTube(vid_url, on_progress_callback=on_progress)
# print(f'{range_index}.) {yt.title}')
# yt.streams.get_highest_resolution().download(filename_prefix=f"{range_index}.) ")
# range_index+=1
# print(f'\n☑Downloading Completed!')
#===============================================-(FINALL PROG)-===============================================#
from pytube import Playlist
from pytube import YouTube
from pytube.cli import on_progress
import datetime
import os
user_choice_Plist_or_Video=int(input(f'➡Select you choice from the bellow options! \n 1.)Video. \n 2.)Playlist. \n 3.)Cancel Process. \n\n➡Enter the number here: '))
if user_choice_Plist_or_Video==1:
vid_url = YouTube(input("\n⏩Enter the video link here: "), on_progress_callback=on_progress)
print(f'\n▶Title: {vid_url.title} \n▶Size: {round(vid_url.streams.get_highest_resolution().filesize/1024**2, 2)} MB \n▶Duration: {datetime.timedelta(seconds = vid_url.length)}')
user_choice_down=input(f'\n\nDo you wish to download "{vid_url.title}" video? [y/n]: ').lower()
if user_choice_down=='y':
vid_url.streams.get_highest_resolution().download()
print(f'\n\n☑Downloading Completed!')
else:
print('\n🟥Downloading Stopped❗')
elif user_choice_Plist_or_Video==2:
P_lst = Playlist(input('\n⏩Enter the playlist link: '))
print(f'\n▶Title: {P_lst.title} \n▶The total number of videos: {len(P_lst.videos)}')
user_choice_Duration_Size = input(f'\n\n⏬Do you wish to calculate Duration & Size of "{P_lst.title}" playlist? [y/n]: ').lower()
if user_choice_Duration_Size == 'y':
P_lst_time=0
P_lst_size=0
print('➡Calculating....')
for video in P_lst.videos:
P_lst_time+=video.length
P_lst_size+=(video.streams.get_highest_resolution().filesize)/1024**2
Formated_time= str(datetime.timedelta(seconds = P_lst_time)).split(":")
print(f'\nPlaylist-Duration: {Formated_time[0]}hr {Formated_time[1]}min {Formated_time[2]}sec')
print(f'\nPlaylist-Size: {round(P_lst_size, 2)} MB')
else:
print("\n🟥Calculation Stopped❗")
user_choice_down=input(f'\n\n⏬Do you wish to download "{P_lst.title}" playlist? [y/n]: ').lower()
if user_choice_down=='y':
print('\n➡Downloading...\n')
for index , video_url in enumerate(P_lst.video_urls):
yt=YouTube(video_url, on_progress_callback=on_progress)
print(f'{index+1}.) {yt.title}')
yt.streams.get_highest_resolution().download(filename_prefix=f"{index+1}.) ", output_path=P_lst.title)
print(f'\n☑Downloading Completed!')
else:
print("\n🟥Downloading Stopped❗")
else:
print('🟥Process Cancelled❗')
#============================================================================================================#