forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCliYoutubeDownloader.py
75 lines (61 loc) · 2.39 KB
/
CliYoutubeDownloader.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
from pytube import *
import sys
class YouTubeDownloder:
def __init__(self):
self.url = str(input("Enter the url of video : "))
self.youtube = YouTube(
self.url, on_progress_callback=YouTubeDownloder.onProgress)
self.showTitle()
def showTitle(self):
print("title : {0}\n".format(self.youtube.title))
self.showStreams()
def showStreams(self):
self.streamNo = 1
for stream in self.youtube.streams:
print("{0} => resolation:{1}/fps:{2}/type:{3}".format(self.streamNo,
stream.resolution, stream.fps, stream.type))
self.streamNo += 1
self.chooseStream()
def chooseStream(self):
self.choose = int(input("please select one : "))
self.validateChooseValue()
def validateChooseValue(self):
if self.choose in range(1, self.streamNo):
self.getStream()
else:
print("please enter a currect option on the list.")
self.chooseStream()
def getStream(self):
self.stream = self.youtube.streams[self.choose-1]
self.getFileSize()
def getFileSize(self):
global file_size
file_size = self.stream.filesize / 1000000
self.getPermisionToContinue()
def getPermisionToContinue(self):
print("\n title : {0} \n author : {1} \n size : {2:.2f}MB \n resolution : {3} \n fps : {4} \n ".format(
self.youtube.title, self.youtube.author, file_size, self.stream.resolution, self.stream.fps))
if input("do you want it ?(defualt = (y)es) or (n)o ") == "n":
self.showStreams()
else:
self.main()
def download(self):
self.stream.download()
@staticmethod
def onProgress(stream=None, chunk=None, remaining=None):
file_downloaded = (file_size-(remaining/1000000))
print(
f"downloading ... {file_downloaded/file_size*100:0.2f} % [{file_downloaded:.1f}MB of {file_size:.1f}MB]", end="\r")
def main(self):
try:
self.download()
except KeyboardInterrupt:
print("Canceled. ")
sys.exit(0)
if __name__ == "__main__":
try:
YouTubeDownloder()
except KeyboardInterrupt:
pass
except Exception as e:
print(e)