-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoutubeMP4Playlist.py
49 lines (44 loc) · 1.35 KB
/
YoutubeMP4Playlist.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
import re, os, time, urllib.request, urllib.error, pytube
video_file = "video_list.txt"
path = "Video"
url = "https://www.youtube.com/playlist?list=myplaylist"
if not os.path.exists(path):
os.mkdir(path)
def get_playlist(url):
amp = 0
final_url = []
if 'list=' in url:
eq = url.rfind('=') + 1
cPL = url[eq:]
else:
print('Incorrect Playlist.')
exit(1)
try:
sTUBE = str(urllib.request.urlopen(url).read())
except urllib.error.URLError as e:
print(e.reason)
tmp_mat = re.compile(r'watch\?v=\S+?list=' + cPL)
mat = re.findall(tmp_mat, sTUBE)
if mat:
for PL in mat:
yPL = str(PL)
if '&' in yPL:
yPL_amp = yPL.index('&')
final_url.append('http://www.youtube.com/' + yPL[:yPL_amp])
all_url = list(set(final_url))
i = 0
file = open(video_file, "w", encoding="utf-8")
while i < len(all_url):
file.write(all_url[i].strip() + '\n')
time.sleep(0.04)
i = i + 1
file.close()
else:
print('No videos found.')
exit(1)
if not 'http' in url:
url = 'http://' + url
get_playlist(url)
with open(video_file) as file:
for line in file:
pytube.YouTube(line).streams.filter(subtype="mp4").filter(progressive=True).first().download(path)