|
| 1 | +from pytube import * |
| 2 | +import sys |
| 3 | + |
| 4 | + |
| 5 | +class YouTubeDownloder: |
| 6 | + def __init__(self): |
| 7 | + self.url = str(input("Enter the url of video : ")) |
| 8 | + self.youtube = YouTube( |
| 9 | + self.url, on_progress_callback=YouTubeDownloder.onProgress) |
| 10 | + self.showTitle() |
| 11 | + |
| 12 | + def showTitle(self): |
| 13 | + print("title : {0}\n".format(self.youtube.title)) |
| 14 | + self.showStreams() |
| 15 | + |
| 16 | + def showStreams(self): |
| 17 | + self.streamNo = 1 |
| 18 | + for stream in self.youtube.streams: |
| 19 | + print("{0} => resolation:{1}/fps:{2}/type:{3}".format(self.streamNo, |
| 20 | + stream.resolution, stream.fps, stream.type)) |
| 21 | + self.streamNo += 1 |
| 22 | + self.chooseStream() |
| 23 | + |
| 24 | + def chooseStream(self): |
| 25 | + self.choose = int(input("please select one : ")) |
| 26 | + self.validateChooseValue() |
| 27 | + |
| 28 | + def validateChooseValue(self): |
| 29 | + if self.choose in range(1, self.streamNo): |
| 30 | + self.getStream() |
| 31 | + else: |
| 32 | + print("please enter a currect option on the list.") |
| 33 | + self.chooseStream() |
| 34 | + |
| 35 | + def getStream(self): |
| 36 | + self.stream = self.youtube.streams[self.choose-1] |
| 37 | + self.getFileSize() |
| 38 | + |
| 39 | + def getFileSize(self): |
| 40 | + global file_size |
| 41 | + file_size = self.stream.filesize / 1000000 |
| 42 | + self.getPermisionToContinue() |
| 43 | + |
| 44 | + def getPermisionToContinue(self): |
| 45 | + print("\n title : {0} \n author : {1} \n size : {2:.2f}MB \n resolution : {3} \n fps : {4} \n ".format( |
| 46 | + self.youtube.title, self.youtube.author, file_size, self.stream.resolution, self.stream.fps)) |
| 47 | + if input("do you want it ?(defualt = (y)es) or (n)o ") == "n": |
| 48 | + self.showStreams() |
| 49 | + else: |
| 50 | + self.main() |
| 51 | + |
| 52 | + def download(self): |
| 53 | + self.stream.download() |
| 54 | + |
| 55 | + @staticmethod |
| 56 | + def onProgress(stream=None, chunk=None, remaining=None): |
| 57 | + file_downloaded = (file_size-(remaining/1000000)) |
| 58 | + print( |
| 59 | + f"downloading ... {file_downloaded/file_size*100:0.2f} % [{file_downloaded:.1f}MB of {file_size:.1f}MB]", end="\r") |
| 60 | + |
| 61 | + def main(self): |
| 62 | + try: |
| 63 | + self.download() |
| 64 | + except KeyboardInterrupt: |
| 65 | + print("Canceled. ") |
| 66 | + sys.exit(0) |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + try: |
| 71 | + YouTubeDownloder() |
| 72 | + except KeyboardInterrupt: |
| 73 | + pass |
| 74 | + except Exception as e: |
| 75 | + print(e) |
0 commit comments