-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutubeDownload.pyw
146 lines (132 loc) · 5.08 KB
/
youtubeDownload.pyw
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
#Youtube Downloader GUI
import tkinter as tk
from tkinter import messagebox
import sys
import pytube
from tkinter import ttk
from tkinter.filedialog import asksaveasfile
from traceback import format_tb
import os
import threading
def toplevelerror(message,title='Error'):
title = str(title)
message = str(message)
root = tk.Tk()
root.wm_attributes("-topmost",True)
root.withdraw()
messagebox.showerror(title,message)
def handle_exception(type,value,traceback):
if issubclass(type, KeyboardInterrupt):
pass
else:
toplevelerror('A fatal exception occured. ERROR:\n'+str(type)+'\n'+str(value)+'\n'+str(format_tb(traceback)[0]))
sys.exit()
sys.excepthook = handle_exception
def download_video():
global ent
global btn0
global btn1
btn1['state'] = 'disabled'
btn0['state'] = 'disabled'
if os.system('ping 8.8.8.8 -n 1') == 0:
try:
yt = pytube.YouTube(ent.get())
yt.check_availability()
except:
tk.Tk().withdraw()
messagebox.showerror('Error','Invalid Video URL!')
else:
x = yt.streams.get_highest_resolution()
files = [('mp4 video',"*.mp4")]
p = asksaveasfile(filetypes = files, defaultextension = files,title='Please choose save file')
if p == None:
pass
else:
x.download(filename=str(p.name))
file_size = os.path.getsize(p.name)
tk.Tk().withdraw()
messagebox.showinfo('Info','Finished downloading video '+str(yt.watch_url)+' to\n'+str(p.name)+'\nfilesize: '+str(file_size)+'Bytes')
else:
tk.Tk().withdraw()
messagebox.showerror('Error','Could not connect to Youtube. Check your internet connection?')
btn0['state'] = 'normal'
btn1['state'] = 'normal'
def download_video_l():
global ent
global btn3
global btn1
btn1['state'] = 'disabled'
btn3['state'] = 'disabled'
if os.system('ping 8.8.8.8 -n 1') == 0:
try:
yt = pytube.YouTube(ent.get())
yt.check_availability()
except:
tk.Tk().withdraw()
messagebox.showerror('Error','Invalid Video URL!')
else:
x = yt.streams.get_lowest_resolution()
files = [('mp4 video',"*.mp4")]
p = asksaveasfile(filetypes = files, defaultextension = files,title='Please choose save file')
if p == None:
pass
else:
x.download(filename=str(p.name))
file_size = os.path.getsize(p.name)
tk.Tk().withdraw()
messagebox.showinfo('Info','Finished downloading video '+str(yt.watch_url)+' to\n'+str(p.name)+'\nfilesize: '+str(file_size)+'Bytes')
else:
tk.Tk().withdraw()
messagebox.showerror('Error','Could not connect to Youtube. Check your internet connection?')
btn3['state'] = 'normal'
btn1['state'] = 'normal'
def download_audio():
global btn2
global ent
global btn1
btn1['state'] = 'disabled'
btn2['state'] = 'disabled'
if os.system('ping 8.8.8.8 -n 1') == 0:
try:
yt = pytube.YouTube(ent.get())
yt.check_availability()
except:
tk.Tk().withdraw()
messagebox.showerror('Error','Invalid Video URL!')
else:
x = yt.streams.get_audio_only()
files = [('mp3 audio',"*.mp3")]
p = asksaveasfile(filetypes = files, defaultextension = files,title='Please choose save file')
if p == None:
pass
else:
x.download(filename=str(p.name))
file_size = os.path.getsize(p.name)
tk.Tk().withdraw()
messagebox.showinfo('Info','Finished downloading audio for video'+str(yt.watch_url)+' to\n'+str(p.name)+'\nfilesize: '+str(file_size)+'Bytes')
else:
tk.Tk().withdraw()
messagebox.showerror('Error','Could not connect to Youtube. Check your internet connection?')
btn2['state'] = 'normal'
btn1['state'] = 'normal'
def expas():
pass
root = tk.Tk()
root.title('Youtube Downloader')
root.geometry('400x160')
style = ttk.Style(root)
style.theme_use('vista')
lbl = ttk.Label(root,text='Please input the full URL to the video you want to download here.')
lbl.pack()
ent = ttk.Entry(root,width=50)
ent.pack()
btn0 = ttk.Button(root,text='Download Video (high resolution)',command=lambda: threading.Thread(target=download_video).start())
btn0.pack()
btn3 = ttk.Button(root,text='Download Video (low resolution)',command=lambda: threading.Thread(target=download_video_l).start())
btn3.pack()
btn2 = ttk.Button(root,text='Download Audio',command=lambda: threading.Thread(target=download_audio).start())
btn2.pack()
btn1 = ttk.Button(root,text='Exit',command=sys.exit)
btn1.pack()
root.protocol("WM_DELETE_WINDOW",expas)
root.mainloop()