forked from ShiftedClock/youtube-playlist-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist-downloader.py
executable file
·56 lines (41 loc) · 2.1 KB
/
playlist-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
#!/usr/bin/env python
import os, sys, time
import argparse
from urllib2 import urlopen
import pytube # pip install pytube
CS294_playlist_url = "https://www.youtube.com/playlist?list=PLkFD6_40KJIwTmSbCv9OVJB3YaO4sFwkX"
def get_playlist_links(playlist_url):
page_elements = urlopen(playlist_url).readlines()
video_elements = [el for el in page_elements if 'pl-video-title-link' in el] # Filter out unnecessary lines
video_urls = [v.split('href="',1)[1].split('" ',1)[0] for v in video_elements] # Grab the video urls from the elements
return ['http://www.youtube.com' + v for v in video_urls]
start_time = time.time()
def print_dot(bytes_received, file_size, start):
global start_time
if time.time() - start_time > 1.0:
sys.stdout.write('.')
sys.stdout.flush()
start_time = time.time()
parser = argparse.ArgumentParser(usage='%(prog)s [-h] [-p PLAYLISTURL] [-d DESTINATION]')
parser.add_argument('-p', '--playlisturl', help='url of the playlist to be downloaded', default=CS294_playlist_url, metavar='')
parser.add_argument('-d', '--destination', help='path of directory to save videos to', default=os.path.curdir, metavar='')
args = parser.parse_args()
if os.path.exists(args.destination):
directory_contents = [f.split('.mp4',1)[0] for f in os.listdir(args.destination) if f.endswith('.mp4')]
else:
print('Destination directory does not exist')
sys.exit(1)
video_urls = get_playlist_links(args.playlisturl)
confirmation = raw_input('You are about to download {} videos to {}\nWould you like to continue? [Y/n] '.format(
len(video_urls), os.path.abspath(args.destination)))
if confirmation.lower() in ['y', '']:
for u in video_urls:
yt = pytube.YouTube(u)
vid = yt.streams.filter(file_extension='mp4').order_by('res').last() # grab the highest resolution mp4 file
if vid.default_filename in directory_contents:
print('Skipping {}'.format(vid.default_filename))
continue
else:
print('Downloading {}'.format(vid.default_filename))
vid.download(args.destination)
print('Done')