-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
59 lines (52 loc) · 2.49 KB
/
test.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
import pytube
import ffmpeg
import time
import ssl
def clean_filename(name):
"""Ensures each file name does not contain forbidden characters and is within the character limit"""
# For some reason the file system (Windows at least) is having trouble saving files that are over 180ish
# characters. I'm not sure why this is, as the file name limit should be around 240. But either way, this
# method has been adapted to work with the results that I am consistently getting.
forbidden_chars = '"*\\/\'.|?:<>'
filename = (''.join([x if x not in forbidden_chars else '#' for x in name])).replace(' ', ' ').strip()
if len(filename) >= 176:
filename = filename[:170] + '...'
return filename
ssl._create_default_https_context = ssl._create_unverified_context
link = 'https://www.youtube.com/watch?v=h3fUgOKFMNU&t=16s'
ti = time.time()
yt = pytube.YouTube(link, use_oauth=True, allow_oauth_cache=True)
print('Title:', yt.title)
print('Author:', yt.author)
print('Published date:', yt.publish_date.strftime("%Y-%m-%d"))
print('Number of views:', yt.views)
print('Length of video:', yt.length, 'sec')
vid = ''
try:
yt.streams.filter(res='2160p', progressive=False).first().download(filename='video.mkv')
yt.streams.filter(abr='160kbps', progressive=False).first().download(filename='audio.mp3')
res = '2160p'
vid = 'video.mkv'
except:
try:
yt.streams.filter(res='1440p', progressive=False).first().download(filename='video.mkv')
yt.streams.filter(abr='160kbps', progressive=False).first().download(filename='audio.mp3')
res = '1440p'
vid = 'video.mkv'
except:
try:
yt.streams.filter(res='1080p', progressive=False).first().download(filename='video.mp4')
yt.streams.filter(abr='160kbps', progressive=False).first().download(filename='audio.mp3')
res = '1080p'
vid = 'video.mp4'
except:
yt.streams.filter(res='720p', progressive=False).first().download(filename='video.mp4')
yt.streams.filter(abr='128kbps', progressive=False).first().download(filename='audio.mp3')
res = '720p'
vid = 'video.mp4'
audio = ffmpeg.input('audio.mp3')
video = ffmpeg.input(vid)
filename = clean_filename(yt.title) + '.mkv' #for Windows output to folder C:\Users\Admin\Downloads\
ffmpeg.output(audio, video, filename).run(overwrite_output=True)
print(res, 'video successfully downloaded from', link)
print('Time taken: {:.0f} sec'.format(time.time() - ti))