-
Notifications
You must be signed in to change notification settings - Fork 0
/
YouTube-Downloader.py
169 lines (143 loc) · 7.02 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
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
# importing Libraries
from pytube import YouTube, Playlist
import time
import string
import unicodedata as ud
import pyarabic.araby as araby
import re
from pytube.cli import on_progress
# Simple function to be used to show that video dowloaded
def finish():
print('Video Downloaded')
while True:
# getting input from the user
inp = input("Link: ")
# exitting when pressing enter or writing done
if inp.lower() == 'done' or len(inp) < 1 : break
# different input types
type_link = input('Enter:\n 1 for Video\n 2 for Playlist\n 3 for Audio\n 4 for Audio Playlist\n')
# Getting output path from the user if pressing enter putting it in the default directory
output_path = input('Enter Your Directory Path: ').strip()
# Default directory you can chamge it if you like
if len(output_path) < 1: output_path = 'D:\Courses'
# downloading a video
if int(type_link) == 1:
yt = YouTube(inp, on_progress_callback = on_progress)
# printing video tiltle & thumnail url
print(yt.title)
print(yt.thumbnail_url)
# trying to download the video first, with 720p
#if not getting the highest resolution
try:
#'Default resolution is 720p you can chamge it if you like'
yt.streams.filter(progressive=True, res='720p').first().download(output_path=output_path)
# printing video downloaded when finished
yt.register_on_complete_callback(finish())
except:
# trying to get the highest resolution
yt.streams.get_highest_resolution().download(output_path=output_path)
yt.register_on_complete_callback(finish())
# downloading a playlist
elif int(type_link) == 2:
p = Playlist(inp)
# printing play list name
print(p.title)
# Allowing the user to chose a video to start downloading at by entering its index
num_playlist_start = input('Enter the number of the video to start at or press Enter for All \n')
# if the user pressed enter, start downloading from the first one
if len(num_playlist_start) < 1:
x = 0
else:
x = int(num_playlist_start) - 1
'''
- Downloading each video in the default quality 720p if not grtting the highest one.
- All the playlist videos will be added to a new folder that has the playlist name.
- Each downloaded video wil be named with the index of video followed by its name.
which make the downloaded videos in the right order.
- After each video is downloaded, a message print out on the screen with the video name and its index.
- Adding a sleep for 5 sec after each video, to avoid connection problems.
'''
# looping through the playlist
for video in list(p.videos)[x:]:
# Cleaning names in Arabic and English
p_name = p.title.translate(str.maketrans('', '', string.punctuation))
p_name = ''.join(c for c in p_name if not ud.category(c).startswith('P'))
p_name = araby.strip_diacritics(p_name)
p_name = re.sub(r'\s+', ' ', p_name)
video_name = video.title.translate(str.maketrans('', '', string.punctuation))
video_name = ''.join(c for c in video_name if not ud.category(c).startswith('P'))
video_name = araby.strip_diacritics(video_name)
video_name = re.sub(r'\s+', ' ', video_name)
try:
x+=1
# downloading the file
print(x)
print(video_name)
video.register_on_progress_callback(on_progress)
video.streams.filter(progressive=True, res='720p').first().download(
output_path= f"{output_path}\\{p_name}",
filename='{} {}.mp4'.format(x, video_name))
# finished message
video.register_on_complete_callback(finish())
# sleeping for connection problems
time.sleep(5)
except:
try:
# downloading the file
print(x)
print(video_name)
video.register_on_progress_callback(on_progress)
video.streams.get_highest_resolution().download(
output_path= f"{output_path}\\{p_name}",
filename='{} {}'.format(x, video_name))
# finished message
video.register_on_complete_callback(finish())
# sleeping for connection problems
time.sleep(5)
except:
# if failed, continue with the next video
continue
# downloading a Video as an Audio
elif int(type_link) == 3:
yt = YouTube(inp, on_progress_callback = on_progress)
# printing video tiltle
print(yt.title)
# downloading as mp4 128kbs
yt.streams.filter(only_audio=True, mime_type="audio/mp4", abr="128kbps").first().download(
output_path=output_path)
# printing video downloaded when finished
yt.register_on_complete_callback(finish())
# downloading a playlist as audio
elif int(type_link) == 4:
p = Playlist(inp)
#printing play list name
print(p.title)
'''
- Downloading each video as an audio (mp4 128kbs).
- All the playlist videos will be added to a new folder that has the playlist name.
- Each downloaded video wil be named with the index of video followed by its name.
which make the downloaded videos in the right order.
- After each video is downloaded, a message print out on the screen with the video name and its index.
- Adding a sleep for 5 sec after each video, to avoid connection problems.
'''
# Allowing the user to chose a video to start downloading at by entering its index
num_playlist_start = input('Enter the number of the video to start at or press Enter for All \n')
# if the user pressed enter, start downloading from the first one
if len(num_playlist_start) < 1:
x = 0
else:
x = int(num_playlist_start) - 1
# looping through the playlist
for video in list(p.videos)[x:]:
x+=1
# downloading the file
print(x)
print(video.title)
video.register_on_progress_callback(on_progress)
video.streams.filter(only_audio=True, mime_type="audio/mp4", abr="128kbps").first().download(
output_path=f"{output_path}\\{p.title}", filename='{} {}'.format(x, video.title))
# finished message
video.register_on_complete_callback(finish())
# sleeping for connection problems
time.sleep(5)
break