-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
381 lines (292 loc) · 13.4 KB
/
solution.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# importing the necessary modules
# Importing the necessary modules
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
from PIL import Image, ImageTk
import threading
import urllib.request
from io import BytesIO
import yt_dlp as youtube_dl
import re
import os
class YoutubeDownloadWindow(tk.Tk):
def get_unique_resolutions(self, inf_dict):
resolutions = {}
for format in inf_dict['formats']:
if re.match(r'^\d+p', format['format_note']) :
resolution_id = format['format_id']
resolution = format['format_note']
if 'HDR' in resolution:
resolution = re.search(r'\d+p\d* HDR', resolution)[0]
resolutions[resolution ] =resolution_id
resolutions = [(v, k) for k, v in resolutions.items()]
return sorted(resolutions, key=lambda k: [int(k[1].split('p')[0]), k[1].split('p')[-1]])
def download_info_dict(self):
ydl_opts = {
'format': 'bestvideo[ext=mp4]+bestaudio/best',
'forcejson': True,
'dump_single_json': True,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(self.video_url.get(), download=False)
return info_dict
def download_thumbnail(self, url):
with urllib.request.urlopen(url) as url:
img_data = url.read()
return img_data
def create_pil_image(self, img_data):
img = Image.open(BytesIO(img_data))
img.thumbnail((120, 120))
return img
def create_photo_image(self, img):
return ImageTk.PhotoImage(img)
def display_image_and_title(self, info_dict, photo2):
label = tk.Label(self.image_frame, image=photo2)
label.grid(row = 0 , padx = 5, pady = 5)
label.configure(image=photo2)
label.image = photo2
text_label = tk.Label(
self.image_frame,
text=info_dict['title'],
font=("Arial", 8, "bold"),
bg="#FFFFFF",
fg="#000000"
)
text_label.grid(row=1, padx=5, pady=5)
def create_resolutions_label(self):
resolutions_label = tk.Label(
self.entry_frame,
text="Resolutions:",
font=("verdana", "10"),
bg="#FEE4E3",
fg="#000000",
anchor=tk.SE
)
resolutions_label.grid(row=2, column=0, padx=5, pady=5, sticky=tk.E)
def create_resolutions_dropdown(self, info_dict):
resolutions = self.get_unique_resolutions(info_dict)
self.resolutions_fields['values'] = [res[1] for res in resolutions]
self.ids = {res[1]: res[0] for res in resolutions}
self.resolutions_fields.current(0)
self.resolutions_fields.grid(row=2, column=1, pady=5)
def set_image(self):
info_dict = self.download_info_dict()
img_data = self.download_thumbnail(info_dict['thumbnail'])
img = self.create_pil_image(img_data)
photo2 = self.create_photo_image(img)
self.display_image_and_title(info_dict, photo2)
self.create_resolutions_label()
self.create_resolutions_dropdown(info_dict)
def progress_hook(self , data):
if data['status'] == 'downloading':
downloaded = data['downloaded_bytes']
total = data['total_bytes'] if data.get('total_bytes' ,None) else data['total_bytes_estimate']
percentage = downloaded / total * 100
percentage = round(percentage, 2)
self.progress_bar["value"] = percentage
self.progress_bar.update()
self.style.configure('text.Horizontal.TProgressbar', text='{:g} %'.format(percentage))
def setup_ydl_opts(self):
# Retrieve the string from the entry fields
format = self.ids[self.resolutions_fields.get()]
download_folder = self.download_dir.get()
return {
'format': f"{format}+bestaudio",
'merge_output_format': 'mkv' ,
'progress_hooks': [self.progress_hook],
'outtmpl': os.path.join(
download_folder, '%(title)s-%(format)s.%(ext)s'
),
}
def download_video(self):
# Retrieve the string from the entry fields
youtube_url = self.video_url.get()
download_folder = self.download_dir.get()
# Check if the entry fields are not empty
if youtube_url and download_folder:
# Display progress bar
self.progress_bar.grid(column=0, row=0, columnspan=2, padx=10, pady=20)
# Set up options for youtube-dl
ydl_opts = self.setup_ydl_opts()
# Download the video
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([youtube_url])
# Hide progress bar and show download complete message
self.progress_bar.grid_remove()
messagebox.showinfo(title='Download Complete', message='Video has been downloaded successfully.')
else:
# Display error message for empty fields
messagebox.showerror("Empty Fields", "Fields are empty!")
def browse_folder(self):
# using the askdirectory() method of the filedialog module to select the directory
download_path = filedialog.askdirectory(initialdir = "D:\Downloads", title = "Select the folder to save the video")
# using the set() method to set the directory path in the entry field
self.download_dir.set(download_path)
if self.video_url.get()=="" :
return
download_thread = threading.Thread(target=self.set_image)
download_thread.start()
def download_video_thread(self):
download_thread = threading.Thread(target=self.download_video)
download_thread.start()
def reset(self):
self.video_url.set("")
self.download_dir.set("")
self.url_field.focus_set()
def exit(self):
self.destroy()
def __init__(self):
super().__init__()
# setting the title of the window
self.title("YouTube Downloader By WAHIB M.")
# setting the size and position of the window
self.geometry("580x380+700+250")
# disabling the resizable option for better UI
self.resizable(0, 0)
# configuring the background color of the window
self.config(bg = "#FEE4E3")
# configuring the icon of the window
##self.iconbitmap("download1.ico")
# adding frames to the window using the Frame() widget
header_frame = tk.Frame(self, bg = "#FEE4E3")
self.image_frame = tk.Frame(self, bg = "#FEE4E3")
self.entry_frame = tk.Frame(self, bg = "#FEE4E3")
button_frame = tk.Frame(self, bg = "#FEE4E3")
progress_frame = tk.Frame(self, bg = "#FEE4E3")
# using the pack() method to set the positions of the frames
header_frame.pack()
self.image_frame.pack()
self.entry_frame.pack()
button_frame.pack()
progress_frame.pack()
# ------------------------- the header_frame frame -------------------------
header_label = tk.Label(
header_frame,
text = "YouTube Video Downloader",
font = ("verdana", "14", "bold"),
bg = "#FEE4E3",
anchor = tk.SE
)
# using the grid() method to set the position of the labels in the grid format
header_label.grid(row = 0, column = 1, padx = 10, pady = 10)
# ------------------------- the self.entry_frame frame -------------------------
# adding the labels to the self.entry_frame frame using the Label() widget
url_label = tk.Label(
self.entry_frame,
text = "Video URL:",
font = ("verdana", "10"),
bg = "#FEE4E3",
fg = "#000000",
anchor = tk.SE
)
des_label = tk.Label(
self.entry_frame,
text = "Destination:",
font = ("verdana", "10"),
bg = "#FEE4E3",
fg = "#000000",
anchor = tk.SE
)
# using the grid() method to set the position of the labels in the grid format
url_label.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = tk.E)
des_label.grid(row = 1, column = 0, padx = 5, pady = 5, sticky = tk.E)
# creating the objects of the StringVar() class
self.video_url = tk.StringVar()
self.download_dir = tk.StringVar()
# adding the entry fields to the self.entry_frame frame using the Entry() widget
self.url_field = tk.Entry(
self.entry_frame,
textvariable = self.video_url,
width = 35,
font = ("verdana", "10"),
bg = "#FFFFFF",
fg = "#000000",
relief = tk.GROOVE
)
des_field = tk.Entry(
self.entry_frame,
textvariable = self.download_dir,
width = 26,
font = ("verdana", "10"),
bg = "#FFFFFF",
fg = "#000000",
relief = tk.GROOVE
)
# using the grid() method to set the position of the entry fields in the grid format
self.url_field.grid(row = 0, column = 1, padx = 5, pady = 5, columnspan = 2)
des_field.grid(row = 1, column = 1, padx = 5, pady = 5)
resolution = tk.StringVar()
self.resolutions_fields = ttk.Combobox(self.entry_frame, state= "readonly", width = 20, font = ("verdana", "8"))
# adding a button to the self.entry_frame frame using the Button() widget
browse_button = tk.Button(
self.entry_frame,
text = "Browse",
width = 7,
font = ("verdana", "10"),
bg = "#FF9200",
fg = "#FFFFFF",
activebackground = "#FFE0B7",
activeforeground = "#000000",
relief = tk.GROOVE,
command = self.browse_folder
)
# using the grid() method to set the position of the button in the grid format
browse_button.grid(row = 1, column = 2, padx = 5, pady = 5)
self.style = ttk.Style(self)
self.style.layout('text.Horizontal.TProgressbar',
[('Horizontal.Progressbar.trough',
{'children': [('Horizontal.Progressbar.pbar',
{'side': 'left', 'sticky': 'ns'})],
'sticky': 'nswe'}),
('Horizontal.Progressbar.label', {'sticky': ''})])
self.style.configure('text.Horizontal.TProgressbar', text='0 %')
self.progress_bar = ttk.Progressbar(progress_frame, orient = tk.HORIZONTAL, style='text.Horizontal.TProgressbar',
length = 250, mode = 'determinate')
self.progress_bar.grid(column=0, row=0, columnspan=2, padx=10, pady=20)
#hide the progress bar
self.progress_bar.grid_remove()
# ------------------------- the button_frame frame -------------------------
# adding the buttons to the button_frame frame using the Button() widget
download_button = tk.Button(
button_frame,
text = "Download",
width = 12,
font = ("verdana", "10"),
bg = "#15EF5F",
fg = "#FFFFFF",
activebackground = "#97F9B8",
activeforeground = "#000000",
relief = tk.GROOVE,
command = self.download_video_thread
)
reset_button = tk.Button(
button_frame,
text = "Clear",
width = 12,
font = ("verdana", "10"),
bg = "#23B1E6",
fg = "#FFFFFF",
activebackground = "#C3E6EF",
activeforeground = "#000000",
relief = tk.GROOVE,
command = self.reset
)
close_button = tk.Button(
button_frame,
text = "Exit",
width = 12,
font = ("verdana", "10"),
bg = "#F64247",
fg = "#FFFFFF",
activebackground = "#F7A2A5",
activeforeground = "#000000",
relief = tk.GROOVE,
command = self.exit
)
# using the grid() method to set the position of the buttons in the grid format
download_button.grid(row = 0, column = 0, padx = 5, pady = 10)
reset_button.grid(row = 0, column = 1, padx = 5, pady = 10)
close_button.grid(row = 0, column = 2, padx = 5, pady = 10)
if __name__ == "__main__":
app = YoutubeDownloadWindow()
app.mainloop()