-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
360 lines (302 loc) · 15.4 KB
/
main.py
File metadata and controls
360 lines (302 loc) · 15.4 KB
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
import io
import os
import threading
import tkinter as tk
from tkinter import ttk
import customtkinter as ctk
from tkVideoPlayer import TkinterVideo
from CTkMessagebox import CTkMessagebox
from ffmpeg import FFmpeg
from ffprobe import FFProbe
from yt_dlp import YoutubeDL
from PIL import Image, ImageTk
class LoadVideoFrame(ctk.CTkFrame):
def __init__(self, master, file_path, next_button_enabled):
super().__init__(master)
self.file_path = file_path
self.next_button_enabled = next_button_enabled
self.label = ctk.CTkLabel(self, text="Load a video")
self.label.pack(pady=20, padx=20)
self.button = ctk.CTkButton(self, text="Select a video file", command=self.select_video)
self.button.pack(pady=20, padx=20)
ctk.CTkLabel(self, text="OR").pack(pady=20, padx=20)
self.youtube_link = tk.StringVar(self, "")
ctk.CTkLabel(self, text="Youtube video link:").pack(pady=20, padx=20)
self.youtube_link_label = ctk.CTkEntry(self, textvariable=self.youtube_link)
self.youtube_link_label.pack(pady=20, padx=20)
self.youtube_button = ctk.CTkButton(self, text="Download", command=self.download_youtube_video)
self.youtube_button.pack(pady=20, padx=20)
self.file_path_label = ctk.CTkLabel(self, textvariable=self.file_path)
self.file_path_label.pack(pady=20, padx=20)
def select_video(self):
filetypes = [("Video files", "*.mp4 *.mkv *.webm")]
file_path = ctk.filedialog.askopenfile(filetypes=filetypes)
if file_path:
self.file_path.set(file_path.name)
self.next_button_enabled.set(True)
def download_youtube_video(self):
thread = threading.Thread(target=self.download_youtube_video_target)
thread.start()
self.loading_label = ctk.CTkLabel(self, text="Downloading...")
self.loading_label.pack(pady=20, padx=20)
def download_youtube_video_target(self):
ytdl_opts = {
"format": "bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4",
"outtmpl": "%(title)s.%(ext)s"
}
with YoutubeDL(ytdl_opts) as ytdl:
info = ytdl.extract_info(self.youtube_link.get(), download=False)
if info["extractor"] == "youtube":
ytdl.download([self.youtube_link.get()])
print(info)
self.file_path.set(f"{info['title']}.{info['ext']}")
self.next_button_enabled.set(True)
self.loading_label.pack_forget()
else:
CTkMessagebox(title="Error", message="Invalid link", icon="cancel")
class TrimVideoFrame(ctk.CTkFrame):
def __init__(self, master, file_path, start_time, end_time, next_button_enabled):
super().__init__(master)
self.file_path = file_path
self.start_time = start_time
self.end_time = end_time
self.next_button_enabled = next_button_enabled
self.label = ctk.CTkLabel(self, text="Trim the video")
self.label.pack(pady=20, padx=20)
self.video_player = TkinterVideo(self, scaled=True, keep_aspect=True)
self.video_player.pack(fill="both", expand=True)
file_path.trace_add("write", lambda *args: self.load_video())
self.video_player.bind("<<Duration>>", self.update_duration)
self.video_player.bind("<<SecondChanged>>", self.update_scale)
self.curr_timestamp = tk.IntVar(self, 0)
self.scrub_slider = ttk.Scale(self, variable=self.curr_timestamp, from_=0, to=0, orient="horizontal", command=self.seek_video)
self.scrub_slider.pack(pady=20, padx=20, fill="x")
self.scrub_slider.bind("<ButtonRelease-1>", lambda evt: self.video_player.seek(int(float(self.curr_timestamp.get()))))
self.play_button = ctk.CTkButton(self, text="Play", command=self.toggle_video_state)
self.play_button.pack(pady=20, padx=20)
self.start_frame = ctk.CTkFrame(self)
ctk.CTkButton(self.start_frame, text="Set as start time", command=lambda: self.start_time.set(self.curr_timestamp.get())).pack(side="left")
self.start_time_label_text = tk.StringVar(self, "Start time: 00:00:00")
self.start_time_label = ctk.CTkLabel(self.start_frame, textvariable=self.start_time_label_text)
self.start_time_label.pack(side="left")
self.start_frame.pack(pady=20, padx=20)
self.end_frame = ctk.CTkFrame(self)
ctk.CTkButton(self.end_frame, text="Set as end time", command=lambda: self.end_time.set(self.curr_timestamp.get())).pack(side="left")
self.end_time_label_text = tk.StringVar(self, "End time: 00:00:00")
self.end_time_label = ctk.CTkLabel(self.end_frame, textvariable=self.end_time_label_text)
self.end_time_label.pack(side="left")
self.end_frame.pack(pady=20, padx=20)
self.start_time.trace_add("write", lambda *args: self.render_time())
self.end_time.trace_add("write", lambda *args: self.render_time())
def update_duration(self, event):
duration = self.video_player.video_info()["duration"]
self.scrub_slider["to"] = duration
def update_scale(self, event):
self.curr_timestamp.set(self.video_player.current_duration())
def seek_video(self, value):
self.video_player.seek(int(float(value)))
def render_time(self):
start_time = self.start_time.get()
end_time = self.end_time.get()
self.start_time_label_text.set(f"Start time: {start_time // 3600:02}:{(start_time // 60) % 60:02}:{start_time % 60:02}")
self.end_time_label_text.set(f"End time: {end_time // 3600:02}:{(end_time // 60) % 60:02}:{end_time % 60:02}")
if start_time and end_time:
self.next_button_enabled.set(True)
def load_video(self):
self.video_player.load(self.file_path.get())
def toggle_video_state(self):
if self.video_player.is_paused():
self.video_player.play()
self.play_button.configure(text="Pause")
else:
self.video_player.pause()
self.play_button.configure(text="Play")
class CropVideoFrame(ctk.CTkFrame):
def __init__(self, master, file_path, start_time, end_time, top_x, top_y, bottom_x, bottom_y, coords_scale, next_button_enabled):
super().__init__(master)
self.file_path = file_path
self.start_time = start_time
self.end_time = end_time
self.top_x = top_x
self.top_y = top_y
self.bottom_x = bottom_x
self.bottom_y = bottom_y
self.coords_scale = coords_scale
self.next_button_enabled = next_button_enabled
self.label = ctk.CTkLabel(self, text="Select crop area")
self.label.pack(padx=20)
outer_frame = ctk.CTkFrame(self)
self.generate_label(outer_frame, "Top X:", self.top_x)
self.generate_label(outer_frame, "Top Y:", self.top_y)
self.generate_label(outer_frame, "Bottom X:", self.bottom_x)
self.generate_label(outer_frame, "Bottom Y:", self.bottom_y)
# put to the right of the screen
outer_frame.pack(pady=20, padx=20, side="right")
self.start_time.trace_add("write", lambda *args: self.intialize_canvas())
def generate_label(self, outer_frame, text, variable):
frame = ctk.CTkFrame(outer_frame)
text_str = tk.StringVar(self, f"{text}: {variable.get() * self.coords_scale.get()}")
ctk.CTkLabel(frame, textvariable=text_str).pack(side="left")
variable.trace_add("write", lambda *args: text_str.set(f"{text}: {variable.get() * self.coords_scale.get()}"))
frame.pack(pady=20, padx=20)
def intialize_canvas(self):
if hasattr(self, "canvas"):
self.canvas.pack_forget()
if self.file_path.get() == "":
return
self.ffmpeg_output = FFmpeg()\
.input(self.file_path.get(), ss=self.start_time.get())\
.output("pipe:1", vframes=1, f="image2", vcodec="png")\
.execute()
self.image = Image.open(io.BytesIO(self.ffmpeg_output))
# set width to 1024 and height to maintain aspect ratio
WIDTH = int(0.7 * 1280)
w = WIDTH
h = WIDTH * self.image.height // self.image.width
if h > WIDTH:
h = WIDTH
w = WIDTH * self.image.width // self.image.height
self.coords_scale.set(self.image.width / w)
self.canvas = ctk.CTkCanvas(self, width=w, height=h)
# resize the image to fit the canvas
self.image = ImageTk.PhotoImage(self.image.resize((w, h)))
self.canvas.create_image(0, 0, image=self.image, anchor="nw")
self.canvas.pack(pady=20, padx=20)
# handle mouse click events
self.canvas.bind("<Button-1>", self.on_click)
def on_click(self, event):
if hasattr(self, "rect"):
self.canvas.delete(self.rect_id)
self.rect = [event.x, event.y, event.x, event.y]
self.rect_id = self.canvas.create_rectangle(self.rect, outline="red", width=2)
self.canvas.bind("<B1-Motion>", self.on_drag)
self.canvas.bind("<ButtonRelease-1>", self.on_release)
def on_drag(self, event):
self.rect[2], self.rect[3] = event.x, event.y
self.canvas.coords(self.rect_id, self.rect)
def on_release(self, event):
self.canvas.unbind("<B1-Motion>")
self.canvas.unbind("<ButtonRelease-1>")
self.top_x.set(self.rect[0])
self.top_y.set(self.rect[1])
self.bottom_x.set(self.rect[2])
self.bottom_y.set(self.rect[3])
self.next_button_enabled.set(True)
class ExportVideoFrame(ctk.CTkFrame):
def __init__(self, master, file_path, start_time, end_time, top_x, top_y, bottom_x, bottom_y, coords_scale):
super().__init__(master)
self.file_path = file_path
self.start_time = start_time
self.end_time = end_time
self.top_x = top_x
self.top_y = top_y
self.bottom_x = bottom_x
self.bottom_y = bottom_y
self.coords_scale = coords_scale
self.label = ctk.CTkLabel(self, text="Export the video")
self.label.pack(pady=20, padx=20)
self.export_button = ctk.CTkButton(self, text="Export", command=self.export_video)
self.export_button.pack(pady=20, padx=20)
def export_video(self):
save_as = ctk.filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("Video files", "*.mp4")])
if not save_as:
return
probe_res = FFProbe(self.file_path.get()).streams[0]
fps = probe_res.framerate
total_frames = (self.end_time.get() - self.start_time.get()) * fps
print("Exporting video"
f"\nFile path: {self.file_path.get()}"
f"\nSave as: {save_as}"
f"\nTotal frames: {total_frames}"
f"\nStart time: {self.start_time.get()}"
f"\nEnd time: {self.end_time.get()}"
f"\nTop left: ({self.top_x.get() * self.coords_scale.get()}, {self.top_y.get() * self.coords_scale.get()})"
f"\nBottom right: ({self.bottom_x.get() * self.coords_scale.get()}, {self.bottom_y.get() * self.coords_scale.get()})")
self.bar = ctk.CTkProgressBar(self)
self.bar.pack(pady=20, padx=20, fill="x")
res = FFmpeg()\
.input(self.file_path.get(), ss=self.start_time.get(), to=self.end_time.get())\
.output(save_as, vf=f"crop={self.coords_scale.get() * (self.bottom_x.get() - self.top_x.get())}:{self.coords_scale.get() * (self.bottom_y.get() - self.top_y.get())}:{self.coords_scale.get() * self.top_x.get()}:{self.coords_scale.get() * self.top_y.get()}", vcodec="libx264")\
@res.on("progress")
def on_progress(progress):
# print(progress)
self.bar.set(progress.frame / total_frames)
@res.on("completed")
def on_completed():
self.bar.pack_forget()
print("Video exported")
os.remove(self.file_path.get())
CTkMessagebox(title="Success", message="Video exported", icon="info")
thread = threading.Thread(target=res.execute)
thread.start()
class App(ctk.CTk):
def __init__(self):
super().__init__()
self.geometry("1280x720")
self.resizable(False, False)
self.title("Video Processor")
self.ttk_style = ttk.Style()
self.ttk_style.layout("TNotebook.Tab", [])
'''
0: Load a video
1: Trim the video
2: Select crop area
3: Export the video
'''
self.current_step = tk.IntVar(self, 0)
self.current_step.trace_add("write", lambda *args: self.update_progress())
self.progress_bar = ctk.CTkProgressBar(self)
self.progress_bar.pack(pady=20, padx=20, fill="x")
self.next_button_enabled = tk.BooleanVar(self, False)
self.file_path = tk.StringVar(self, "")
self.start_time = tk.IntVar(self, 0)
self.end_time = tk.IntVar(self, 0)
self.top_x = tk.IntVar(self, 0)
self.top_y = tk.IntVar(self, 0)
self.bottom_x = tk.IntVar(self, 0)
self.bottom_y = tk.IntVar(self, 0)
self.coords_scale = tk.DoubleVar(self, 0)
self.next_button = ctk.CTkButton(self, text="Next", command=self.next_step, state="disabled")
self.next_button.pack(side="bottom", padx=20, pady=20)
self.next_button_enabled.trace_add("write", lambda *args: self.next_button.configure(state="normal") if self.next_button_enabled.get() else self.next_button.configure(state="disabled"))
self.load_video_frame = LoadVideoFrame(self, self.file_path, self.next_button_enabled)
self.trim_video_frame = TrimVideoFrame(self, self.file_path, self.start_time, self.end_time, self.next_button_enabled)
self.crop_video_frame = CropVideoFrame(self, self.file_path, self.start_time, self.end_time, self.top_x, self.top_y, self.bottom_x, self.bottom_y, self.coords_scale, self.next_button_enabled)
self.export_video_frame = ExportVideoFrame(self, self.file_path, self.start_time, self.end_time, self.top_x, self.top_y, self.bottom_x, self.bottom_y, self.coords_scale)
self.update_progress()
def update_progress(self):
self.progress_bar.set(self.current_step.get() * 1/3)
self.load_video_frame.pack_forget()
self.trim_video_frame.pack_forget()
self.crop_video_frame.pack_forget()
self.export_video_frame.pack_forget()
if self.current_step.get() == 0:
self.load_video_frame.pack(fill="both", expand=True)
elif self.current_step.get() == 1:
self.trim_video_frame.pack(fill="both", expand=True)
elif self.current_step.get() == 2:
self.crop_video_frame.pack(fill="both", expand=True)
elif self.current_step.get() == 3:
self.export_video_frame.pack(fill="both", expand=True)
def next_step(self):
if self.current_step.get() == 2:
self.next_button.configure(text="Reset")
self.current_step.set(self.current_step.get() + 1)
return
if self.current_step.get() == 3:
self.current_step.set(0)
self.file_path.set("")
self.start_time.set(0)
self.end_time.set(0)
self.top_x.set(0)
self.top_y.set(0)
self.bottom_x.set(0)
self.bottom_y.set(0)
self.coords_scale.set(0)
self.next_button_enabled.set(False)
self.next_button.configure(text="Next")
return
self.current_step.set(self.current_step.get() + 1)
self.next_button_enabled.set(False)
app = App()
app.mainloop()