-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
90 lines (63 loc) · 1.99 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from pytube import YouTube
from tkinter.filedialog import *
from tkinter.messagebox import *
from tkinter import *
from threading import *
font=('verdana',20)
file_size = 0
# on complete call back function
def completeDownload(stream=None,file_path=None):
print("download completed")
showinfo("Message", "File has been downloaded...")
downloadBtn['text'] = "Download Video"
downloadBtn['state'] = "active"
urlField.delete(0, END)
#on progress callback function
def progressDownload(stream=None,chunck=None,bytes_remaining=None):
percent=(100*((file_size-bytes_remaining)/file_size))
downloadBtn['text']="{:00.0f} % downloaded ".format(percent)
# download function
def startDownload(url):
global file_size
path_to_save=askdirectory()
if path_to_save is None:
return
try:
yt=YouTube(url)
st = yt.streams.first()
yt.register_on_complete_callback(completeDownload)
yt.register_on_progress_callback(progressDownload)
file_size = st.filesize
st.download(output_path=path_to_save)
except Exception as e:
print(e)
print("something went wrong")
def btnClicked():
try:
downloadBtn['text'] = "Please wait..."
downloadBtn['state']='disabled'
url=urlField.get()
if url=='':
return
print(url)
thread=Thread(target=startDownload, args=(url,))
thread.start()
except Exception as e:
print(e)
# gui coding
root = Tk()
root.title("My YouTube Downloader")
root.iconbitmap("img/icon.ico")
root.geometry("500x600")
# main icon section
file = PhotoImage(file="img/icon.png")
headingIcon=Label(root,image=file)
headingIcon.pack(side=TOP, pady= 3)
# making url field
urlField=Entry(root,font=font,justify = CENTER)
urlField.pack(side=TOP, fill=X, padx=10)
urlField.focus()
# download button
downloadBtn = Button(root,text="Download Video",font=font,relief='ridge',command= btnClicked)
downloadBtn.pack(side=TOP,pady=20)
root.mainloop()