-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYT_downloader_v1.1.py
155 lines (90 loc) · 5.39 KB
/
YT_downloader_v1.1.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import tkinter as tk
from tkinter import ttk
#from pytube import YouTube
from pytubefix import YouTube
from pytubefix.cli import on_progress
class MainPage(tk.Tk):
def __init__(self, root):
self.root = root
root.title("YouTube Video Downloader v 1.0")
root.geometry('402x352')
self.openFirstWindow()
def openFirstWindow(self):
title = tk.Label(self.root, text="YT VIDEO DOWNLOADER", font=('Georgia',12))
title.grid(column=0,row=0, columnspan=2, ipadx=110)
prologue = tk.Label(self.root, text= 'Welcome! Enter the video URL:', font=('Perpetua',16))
prologue.grid(column= 0, row= 1, sticky='w', ipady=20)
self.obtainVideoLink()
def obtainVideoLink(self):
entry_box = tk.Entry(self.root)
entry_box.grid(column=0, row=2, ipadx=100)
enter_btn = tk.Button(self.root, text= "Enter", command= lambda: self.getVideo(videoLink= entry_box.get()))
enter_btn.grid(column=0, row=3, ipadx=60)
def getVideo(self,videoLink='*['):
if videoLink != '*[':
try:
self.yt = YouTube(videoLink, on_progress_callback = on_progress)
except:
error_lbl = tk.Label(self.root, text= "~ Error - Video Not Found ~ ", fg='white')
error_lbl.grid(column=0, row=4, ipadx=20, ipady=10, sticky='w')
self.flashErrorPrompt()
return None
success_lbl = tk.Label(self.root, text= "~ Getting The Video - Please Wait ~", fg="green")
success_lbl.grid(column=0, row=4, ipadx=20, ipady=10, sticky='w')
self.getResolutions()
def flashErrorPrompt(self):
self.root.after(50,self.showErrorPrompt)
def showErrorPrompt(self):
error_lbl = tk.Label(self.root, text= "~ Error - Video Not Found ~ ", fg='red')
error_lbl.grid(column=0, row=4, ipadx=20, ipady=10, sticky='w')
def getResolutions(self):
final_resolutions = []
allResolutions = set()
for availableStream in self.yt.streams:
if availableStream.is_progressive == True: #Filter Out The Progressive Streams
allResolutions.add(str(availableStream.resolution)[:-1])
for res in allResolutions:
final_resolutions.append(int(res)) # Sort & List the Resolutions
final_resolutions = sorted(final_resolutions)
for index in range(len(final_resolutions)):
final_resolutions[index] = str(final_resolutions[index]) + 'p' # e.g "144" + 'p' --> "144p"
self.openSecondWindow(final_resolutions)
def openSecondWindow(self, final_resolutions):
self.new_window = tk.Toplevel(self.root)
self.new_window.title('YouTube Video Downloader v 1.0')
self.new_window.geometry('402x352')
self.showTitle()
self.showResolutions(final_resolutions)
self.getDirectory()
self.startDownload()
self.downloadVideo()
def showTitle(self):
vid_title_lbl = tk.Label(self.new_window, text= "Video Title: ", font=('Perpetua',16))
vid_title_lbl.grid(column=0, row=1, sticky='w')
videoTitle = tk.Label(self.new_window, text=str(self.yt.title))
videoTitle.grid(column=0,row=2, sticky='w')
def showResolutions(self, final_resolutions):
select_res_prompt = tk.Label(self.new_window, text= "Video Resolution: ", font=('Perpetua', 16))
select_res_prompt.grid(column=0, row=3, sticky='w', ipady=10)
res_menu = tk.StringVar()
self.res_menu_drop = ttk.Combobox(self.new_window, width=12, values= final_resolutions, textvariable= res_menu)
self.res_menu_drop.grid(column=0, row=3)
def getDirectory(self):
saveDirectoryPrompt = tk.Label(self.new_window, text= "Save To: ", font=('Arabic Typesetting', 16))
saveDirectoryPrompt.grid(column=0, row=4, sticky='w')
self.directoryInput = tk.Entry(self.new_window)
self.directoryInput.grid(column=0, row=5, ipadx=100)
directoryExample = tk.Label(self.new_window, text= "e.g C:/User/MyFolder/Downloads", fg='grey', font=('Arabic Typesetting', 14))
directoryExample.grid(column=0, row=6, sticky='w', ipadx=65)
def startDownload(self):
beginDownload = tk.Button(self.new_window, text= "Download Video", command= lambda: self.downloadVideo(userResolution= self.res_menu_drop.get(), userDirectory= self.directoryInput.get()))
beginDownload.grid(column=0, row=8)
def downloadVideo(self, userResolution= "-", userDirectory= "-"):
if (userResolution != "-") and (userDirectory.strip() != ""):
video = self.yt.streams.filter(progressive=True, res= userResolution).first()
video.download(userDirectory)
downloadFinished = tk.Label(self.new_window, text= "Downloading Finished!", fg='green', font=('Arabic Typesetting',16))
downloadFinished.grid(column=0, row=9, sticky='w')
window = tk.Tk()
mm = MainPage(window)
window.mainloop()