-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
66 lines (54 loc) · 1.84 KB
/
main.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
import sys
import pytube
from pytube import YouTube
from pytube.cli import on_progress
import urllib
def getRequest(url: str):
try:
yt = YouTube(url, on_progress_callback=on_progress)
print("\nTitle of Video:", yt.title)
except urllib.error.URLError:
print(
"\nNo Internet. Please make sure you have an active internet connection."
)
exit()
except pytube.exceptions.RegexMatchError:
print("\nInvalid address for youtube video. ")
exit()
return yt
def AvailableRes(yt):
AvailableResolutions = set()
for i in yt.streams.filter(progressive=True):
if isinstance(i.resolution, str):
AvailableResolutions.add(i.resolution)
print("\nAvailable resolutions:", *AvailableResolutions)
def main(url="", res=None):
if url == "":
url = input("Enter the URL of the youtube video: ")
yt = getRequest(url)
AvailableRes(yt)
if res is None:
res = str(input("Specify the resolution: "))
else:
res = res[1:]
stream = yt.streams.filter(progressive=True,
file_extension='mp4',
res=str(res)).first()
if (stream is None):
print("\nResolution Not Found")
exit()
print("\nStream connected. Stream:\n", stream)
print("\nStarting Download...")
stream = stream.download()
print("\nDone Downloading\nFile Location:", stream)
if __name__ == "__main__":
if len(sys.argv) == 3:
main(sys.argv[2], sys.argv[1])
elif len(sys.argv) == 2:
main(sys.argv[1])
elif len(sys.argv) == 1:
main()
else:
print(
"You have entered the wrong format for arguments. Please input in the form:\npython main.py -resolution url"
)