-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrameHarvestPro.py
293 lines (233 loc) · 12 KB
/
FrameHarvestPro.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
# PRODUCT OF TRAKEXCEL AGENCY 2024. COPYRIGHT TERMS APPLIES
import cv2
import os
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import subprocess
import sys
class VideoToFramesConverter:
def __init__(self, root):
self.root = root
self.root.title("Video to Frames Pictures Converter")
# Set custom icon
icon_path = 'vid.ico' # Replace with the path to your .ico file
if getattr(sys, 'frozen', False): # Check if running as a PyInstaller executable
icon_path = os.path.join(sys._MEIPASS, "vid.ico")
if os.path.exists(icon_path):
self.root.iconbitmap(icon_path)
# Icon definitions
self.browse_icon = tk.PhotoImage(file="folder.png" if not getattr(
sys, 'frozen', False) else os.path.join(sys._MEIPASS, "folder.png"))
self.cancel_icon = tk.PhotoImage(file="cancel.png" if not getattr(
sys, 'frozen', False) else os.path.join(sys._MEIPASS, "cancel.png"))
self.convert_icon = tk.PhotoImage(file="convert.png" if not getattr(
sys, 'frozen', False) else os.path.join(sys._MEIPASS, "convert.png"))
self.open_folder_icon = tk.PhotoImage(file="open-folder.png" if not getattr(
sys, 'frozen', False) else os.path.join(sys._MEIPASS, "open-folder.png"))
self.create_widgets()
self.cancel_conversion = False
def create_widgets(self):
self.notebook = ttk.Notebook(self.root)
self.notebook.grid(row=0, column=0, sticky="nsew", padx=10, pady=10)
# Page 1: Conversion functionality
conversion_page = ttk.Frame(self.notebook)
self.notebook.add(conversion_page, text="Convert Video")
self.create_conversion_widgets(conversion_page)
# Page 2: About the program
about_page = ttk.Frame(self.notebook)
self.notebook.add(about_page, text="About")
about_label = ttk.Label(
about_page, text="Video to Frames Converter\nVersion 1.0\n© 2024 Trakexcel Agency-@Uzitrake")
about_label.pack(padx=20, pady=14)
explanation_text = (
"This program allows you to convert a video file into a sequence of frames.\n\n"
"Usage:\n"
"1. Select a video file using the 'Browse' button.\n"
"2. Choose an output directory where the frames will be saved.\n"
"3. Select the output format (WebP, JPEG, PNG) from the dropdown menu.\n"
"4. Set the quality of the images (0-100).\n"
"5. Click 'Convert' to start the conversion process.\n"
"6. Optionally, you can cancel the conversion using the 'Cancel' button.\n"
"7. Once the conversion is done, use 'Open in Explorer'."
)
explanation_label = ttk.Label(
about_page, text=explanation_text, anchor="w", justify="left")
explanation_label.pack(padx=20, pady=20, fill="both")
# Allow window expansion
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
def create_conversion_widgets(self, parent):
# Video file selection
self.file_frame = ttk.Frame(parent, padding="10 5 10 5")
self.file_frame.grid(row=0, column=0, sticky="ew")
self.file_path_label = ttk.Label(
self.file_frame, text="Video To Convert:")
self.file_path_label.grid(row=0, column=0, sticky="w")
self.file_path_entry = ttk.Entry(
self.file_frame, state="disabled", width=40, font=("Helvetica", 10))
self.file_path_entry.grid(row=0, column=1, padx=(0, 10), sticky="ew")
self.browse_button = ttk.Button(
self.file_frame, text="Browse", command=self.browse_file)
self.browse_button.grid(row=0, column=2)
# Output directory selection
self.output_frame = ttk.Frame(parent, padding="10 5 10 5")
self.output_frame.grid(row=1, column=0, sticky="ew")
self.output_path_label = ttk.Label(
self.output_frame, text="Output Location:", font=("Helvetica", 10))
self.output_path_label.grid(row=0, column=0, sticky="w")
self.output_path_entry = ttk.Entry(
self.output_frame, state="disabled", width=40, font=("Helvetica", 10))
self.output_path_entry.grid(row=0, column=1, padx=(0, 10), sticky="ew")
self.output_browse_button = ttk.Button(
self.output_frame, text="Browse", command=self.browse_output_directory)
self.output_browse_button.grid(row=0, column=2)
# Output format selection
self.format_frame = ttk.Frame(parent, padding="10 5 10 5")
self.format_frame.grid(row=2, column=0, sticky="ew")
self.format_label = ttk.Label(
self.format_frame, text="Output Format:", font=("Helvetica", 10))
self.format_label.grid(row=0, column=0, sticky="w")
self.format_var = tk.StringVar()
self.format_var.set("WebP") # Default format
self.format_dropdown = ttk.Combobox(self.format_frame, textvariable=self.format_var,
values=["WebP", "JPEG", "PNG"])
self.format_dropdown.grid(row=0, column=1, padx=(0, 10), sticky="ew")
# Quality selection
self.quality_frame = ttk.Frame(parent, padding="10 5 10 5")
self.quality_frame.grid(row=3, column=0, sticky="ew")
self.quality_label = ttk.Label(
self.quality_frame, text="Quality (0-100):", font=("Helvetica", 10))
self.quality_label.grid(row=0, column=0, sticky="w")
self.quality_var = tk.StringVar()
self.quality_var.set("100")
self.quality_spinbox = ttk.Spinbox(
self.quality_frame, from_=0, to=100, textvariable=self.quality_var, width=7)
self.quality_spinbox.grid(row=0, column=1, padx=(0, 10), sticky="ew")
# Button frame
self.button_frame = ttk.Frame(parent, padding="10 5 10 5")
self.button_frame.grid(row=4, column=0, sticky="ew")
# Convert button
self.convert_button = ttk.Button(
self.button_frame, text="Convert", command=self.start_conversion, image=self.convert_icon, compound="left")
self.convert_button.grid(row=0, column=0, padx=5)
# Cancel button
self.cancel_button = ttk.Button(
self.button_frame, text="Cancel", command=self.cancel_conversion_process, image=self.cancel_icon, compound="left")
self.cancel_button.grid(row=0, column=1, padx=5)
# Open in Explorer button
self.open_folder_button = ttk.Button(self.button_frame, text="Open in Explorer",
command=self.open_output_folder, image=self.open_folder_icon, compound="left")
self.open_folder_button.grid(row=0, column=2, padx=5)
# Info label
info_label = ttk.Label(
parent, text="ℹ️ Check chosen directory for images", font="Helvetica 8 italic")
info_label.grid(row=5, column=0, pady=5)
# Conversion status labels
self.status_label = ttk.Label(parent, text="")
self.status_label.grid(row=6, column=0, pady=5)
# Progress bar style
style = ttk.Style(self.root)
style.theme_use("clam")
style.configure("green.Horizontal.TProgressbar",
troughcolor='black', barcolor='green')
# Progress bar
self.progress_var = tk.DoubleVar()
self.progress_bar = ttk.Progressbar(
parent, variable=self.progress_var, mode="determinate", style="green.Horizontal.TProgressbar")
self.progress_bar.grid(row=7, column=0, sticky="ew", pady=5)
def browse_file(self):
file_path = filedialog.askopenfilename(title="Select a video file", filetypes=[
("Video Files", "*.mp4;*.avi")])
self.file_path_entry.config(state="normal")
self.file_path_entry.delete(0, tk.END)
self.file_path_entry.insert(tk.END, file_path)
self.file_path_entry.config(state="disabled")
def browse_output_directory(self):
output_directory = filedialog.askdirectory(
title="Select an output directory")
self.output_path_entry.config(state="normal")
self.output_path_entry.delete(0, tk.END)
self.output_path_entry.insert(tk.END, output_directory)
self.output_path_entry.config(state="disabled")
# Call open_output_folder to update the output_directory variable
# self.open_output_folder()
# CODE BY UZITRAKE
def start_conversion(self):
self.cancel_conversion = False
self.status_label.config(text="Converting...")
self.progress_var.set(0) # Reset progress bar
# Delayed call to start conversion
self.root.after(100, self.convert_video_to_frames)
def cancel_conversion_process(self):
self.cancel_conversion = True
self.status_label.config(text="Conversion canceled.")
self.progress_bar.stop()
def convert_video_to_frames(self):
video_path = self.file_path_entry.get()
output_directory = self.output_path_entry.get()
quality = int(self.quality_var.get())
output_format = self.format_var.get()
if not video_path:
messagebox.showerror("Error", "Please select a video file.")
self.reset_conversion_status()
return
if not output_directory:
messagebox.showerror("Error", "Please select an output directory.")
self.reset_conversion_status()
return
if not os.path.exists(output_directory):
os.makedirs(output_directory)
video_capture = cv2.VideoCapture(video_path)
total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
frame_count = 0
def process_frame():
nonlocal frame_count
if not self.cancel_conversion:
ret, frame = video_capture.read()
if ret:
frame_filename = os.path.join(output_directory, f'frame_{
frame_count:04d}.{output_format.lower()}')
if output_format == "AVIF":
imageio.imwrite(frame_filename, frame,
format='AVIF', codec='av1')
elif output_format == "PNG":
cv2.imwrite(frame_filename, frame, [
cv2.IMWRITE_PNG_COMPRESSION, quality])
else:
cv2.imwrite(frame_filename, frame, [
cv2.IMWRITE_WEBP_QUALITY, quality] if output_format == "WebP" else [])
frame_count += 1
# Update progress bar
progress_value = (frame_count + 1) / total_frames * 100
self.progress_var.set(progress_value)
self.root.update_idletasks() # Force update of the GUI
# Schedule the next frame processing
self.root.after(10, process_frame)
else:
# Release resources and finalize
video_capture.release()
cv2.destroyAllWindows()
if self.cancel_conversion:
self.status_label.config(text="Conversion canceled.")
else:
self.status_label.config(text="Conversion completed.")
self.progress_bar.stop()
self.reset_conversion_status()
process_frame()
def open_output_folder(self):
output_directory = self.output_path_entry.get()
if output_directory and os.path.exists(output_directory):
os.startfile(output_directory)
else:
messagebox.showwarning(
"Warning", "Output directory does not exist.")
def reset_conversion_status(self):
self.cancel_conversion = False
self.status_label.config(text="")
self.progress_bar.stop()
if __name__ == "__main__":
root = tk.Tk()
style = ttk.Style(root)
style.theme_use("clam")
converter = VideoToFramesConverter(root)
root.mainloop()