-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvideo_frame.py
357 lines (316 loc) · 12.3 KB
/
video_frame.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
import os
import platform
import subprocess
from io import BytesIO
from pathlib import Path
from threading import Thread
from tkinter import Menu, messagebox
from tkinter import filedialog
import customtkinter as ctk
import requests
from PIL import Image
from check_connection import Connection
from youtube import Downloader
OUTPUT_PATH = Path(__file__).parent
def convert_img(thumbnail):
response = requests.get(thumbnail)
img_data = response.content
img = Image.open(BytesIO(img_data))
img.thumbnail((130, 120))
conv_img = ctk.CTkImage(light_image=img, dark_image=img, size=(125, 110))
return conv_img
class Video(ctk.CTkFrame):
def __init__(self, master, switch_frame, thumbnail, default_thumb, title):
super().__init__(master, corner_radius=0, fg_color="#2B2933", width=514, height=400)
self.thumbnail_label_img = thumbnail
self.default_thumb = default_thumb
self.title = title
# Create Frame 1 Button
self.frame_1_btn = ctk.CTkButton(
self,
text="Video",
fg_color="#62606B",
hover_color="#62606B",
corner_radius=20
)
self.frame_1_btn.grid(column=0, row=0, pady=15, padx=10)
# Button to switch to Frame 2
self.switch_to_frame2_btn = ctk.CTkButton(
self,
text="Audio",
command=switch_frame,
fg_color="#706E7A",
hover_color="#62606B",
corner_radius=20
)
self.switch_to_frame2_btn.grid(column=0, row=0, columnspan=2)
# Load WiFi status images
self.wifi_on = ctk.CTkImage(
light_image=Image.open("resources/images/wifi.png"),
dark_image=Image.open("resources/images/wifi.png"),
size=(24, 24)
)
self.wifi_off = ctk.CTkImage(
light_image=Image.open("resources/images/no-wifi.png"),
dark_image=Image.open("resources/images/no-wifi.png"),
size=(29, 29)
)
# WiFi status label
self.wifi_label = ctk.CTkLabel(
self,
text="",
fg_color="#2B2933",
bg_color="#2B2933"
)
self.wifi_label.grid(row=0, column=1, sticky="e", padx=(0, 120))
# WiFi status text
self.wifi_statue_text = ctk.CTkLabel(
self,
text="",
fg_color="#2B2933",
bg_color="#2B2933",
font=ctk.CTkFont("ubuntu", 14, weight="bold")
)
self.wifi_statue_text.grid(row=0, column=1, columnspan=2, padx=(0, 10), sticky="e")
self.conn_status(Connection().check())
# URL entry label
self.url_text = ctk.CTkLabel(
self,
text="U R L :",
font=ctk.CTkFont("intern", 16, weight="normal", underline=True)
)
self.url_text.grid(row=1, column=0, padx=(50, 0), pady=10)
# URL entry field
self.url_entry = ctk.CTkEntry(
self,
width=296,
height=41,
fg_color="#3B3944",
corner_radius=20,
border_width=0,
placeholder_text="Enter Url"
)
self.url_entry.grid(row=1, column=1, padx=20, pady=10)
self.menu = Menu(self, tearoff=False)
self.menu.add_command(label="Paste", command=lambda: self.paste(self.url_entry))
# Bind focus events to URL entry field
self.url_entry.bind("<FocusIn>", self.on_focus_in)
self.url_entry.bind("<FocusOut>", self.on_focus_out)
# Bind right-click event to URL entry field
self.url_entry.bind("<Button-3>", self.show_right_click_menu)
# Resolution label
self.resolution_text = ctk.CTkLabel(
self,
text="R e s o l u t i o n :",
font=ctk.CTkFont("intern", 16, weight="normal", underline=True)
)
self.resolution_text.grid(row=2, column=0, padx=(50, 0), sticky="w", pady=10)
# Resolution dropdown menu
self.resolution = ctk.CTkOptionMenu(
self,
values=[],
fg_color="#3B3944",
width=257,
height=41,
corner_radius=20,
dropdown_fg_color="#3B3944"
)
self.resolution.grid(row=2, column=1, pady=10)
# Directory label
self.directory_text = ctk.CTkLabel(
self,
text="D i r e c t o r y :",
font=ctk.CTkFont("intern", 16, weight="normal", underline=True)
)
self.directory_text.grid(row=3, column=0, padx=(50, 0), pady=10)
# Directory button
self.directory_btn = ctk.CTkButton(
self,
text="Choose directory",
font=ctk.CTkFont("intern", 20, weight="normal"),
width=257,
height=41,
fg_color="#3B3944",
hover_color="#383641",
corner_radius=20,
command=self.get_directory
)
self.directory_btn.grid(row=3, column=1, pady=10)
# Save in label
self.save_in_text = ctk.CTkLabel(
self,
text="S a v e i n :",
font=ctk.CTkFont("intern", 16, weight="normal", underline=True)
)
self.save_in_text.grid(row=4, column=0, padx=(70, 0), sticky="w", pady=10)
# Chosen directory label
self.chosen_directory_text = ctk.CTkLabel(
self,
text="",
font=ctk.CTkFont("intern", 16, weight="normal", underline=True)
)
self.chosen_directory_text.grid(row=4, column=1, pady=10)
# folder button
self.folder_img = ctk.CTkImage(
light_image=Image.open("resources/images/folder.png"),
dark_image=Image.open("resources/images/folder.png"),
)
self.folder_btn = ctk.CTkButton(
self,
image=self.folder_img,
width=32,
height=32,
text="",
corner_radius=20,
fg_color="#2B2933",
hover_color="#37353F",
command=lambda: self.open_folder(self.chosen_directory_text.cget("text"))
)
self.folder_btn.grid(row=4, column=1, columnspan=2, pady=10, sticky="se")
# Download button
self.download_btn = ctk.CTkButton(
self,
text="Download",
font=ctk.CTkFont("intern", 20, weight="normal"),
width=128,
height=41,
fg_color="#3B3944",
hover_color="#383641",
corner_radius=20,
command=self.download,
)
self.download_btn.grid(row=5, column=1, sticky="w", padx=12, pady=10)
# Search button
self.search_btn = ctk.CTkButton(
self,
text="Search",
font=ctk.CTkFont("intern", 20, weight="normal"),
width=128,
height=41,
fg_color="#3B3944",
hover_color="#383641",
corner_radius=20,
command=self.start_search
)
self.search_btn.grid(row=5, column=1, sticky="e", padx=12, pady=10)
# Progress bar
self.progress_bar = ctk.CTkProgressBar(self)
self.progress_bar.grid(column=1, row=6)
self.progress_bar.grid_forget()
self.progress_bar.configure(mode="determinate")
# Status label
self.status = ctk.CTkLabel(
self,
text="",
font=ctk.CTkFont("intern", 16, weight="normal")
)
self.status.grid(row=7, column=1, columnspan=2, rowspan=2, pady=(0, 10), sticky="s")
def on_focus_in(self, event):
self.url_entry.configure(border_color="#7D6C6C", border_width=2)
def on_focus_out(self, event):
self.url_entry.configure(border_color="#7D6C6C", border_width=0)
def get_directory(self):
directory = filedialog.askdirectory()
if directory:
self.chosen_directory_text.configure(text=directory)
self.save_in_text.configure(text="S a v e i n :")
def conn_status(self, status):
if status:
self.wifi_label.configure(image=self.wifi_on)
self.wifi_label.grid(padx=(0, 100))
self.wifi_statue_text.configure(text="Connected")
self.update()
return True
else:
self.wifi_label.configure(image=self.wifi_off)
self.wifi_label.grid(padx=(0, 120))
self.wifi_statue_text.configure(text="Disconnected")
self.update()
return False
def start_search(self):
if self.conn_status(Connection().check()):
url = self.url_entry.get()
self.yt = Downloader(url)
title = self.yt.title
resolution = self.yt.resolutions
thumbnail = self.yt.thumbnail
thumbnail = convert_img(thumbnail)
self.resolution.configure(values=resolution)
self.title.configure(text=title, fg_color="#3C313F")
self.thumbnail_label_img.configure(image=thumbnail)
# print(OUTPUT_PATH)
self.update()
else:
self.status.configure(text="Please Check connection", text_color="red")
self.update()
self.after(7000, self.clear_resolution_label)
def download(self):
if self.conn_status(Connection().check()):
path = self.chosen_directory_text.cget("text")
if path == "":
self.status.configure(text="Select downloading folder", text_color="red")
return None
res = self.resolution.get()
self.progress_bar.grid(column=1, row=6)
self.update()
def call():
message = messagebox.askquestion(title="File exist",message="File already exist \nDo you want to overwrite?")
return message
def perform_download():
self.status.configure(text="Downloading...", text_color="green")
data = self.yt.download(res, path,call)
if data:
self.progress_bar.stop()
self.progress_bar.configure(mode="determinate")
self.progress_bar.set(1)
self.status.configure(text="Downloaded", text_color="green")
self.after(7000, self.clear_resolution_label)
elif not data:
self.status.configure(text="downloading stopped", text_color="red")
self.after(7000, self.clear_resolution_label)
else:
self.status.configure(text=data, text_color="red")
self.after(7000, self.clear_resolution_label)
self.update()
download_thread = Thread(target=perform_download)
download_thread.start()
def update_progress_bar():
if download_thread.is_alive():
self.progress_bar.configure(mode="indeterminate")
self.progress_bar.start()
else:
self.progress_bar.set(1)
update_progress_bar()
else:
self.status.configure(text="Please Check connection", text_color="red")
self.update()
self.after(7000, self.clear_resolution_label)
def clear_resolution_label(self):
self.status.configure(text="")
self.url_entry.delete(0, ctk.END)
self.title.configure(text="", fg_color="#3B3944")
self.resolution.configure(values=[])
self.thumbnail_label_img.configure(image=self.default_thumb)
self.progress_bar.grid_forget()
self.progress_bar.configure(mode="indeterminate")
self.progress_bar.start()
def show_right_click_menu(self, e):
self.menu.tk_popup(e.x_root, e.y_root)
def paste(self, widget):
try:
clipboard_text = self.clipboard_get()
widget.insert("insert", clipboard_text)
except:
pass
def open_folder(self, path):
if path:
current_path = Path(__file__).resolve().parent.parent
if path == "output/videos":
path = current_path / path
# Open the selected folder
if platform.system() == "Windows":
os.startfile(path)
elif platform.system() == "Darwin": # macOS
subprocess.Popen(["open", path])
else: # Linux and other OS
subprocess.Popen(["xdg-open", path])