self.splitter = VideoSplitter()
self._create_widgets()
def _create_widgets(self):
# Main frame
main_frame = ttk.Frame(self.root, padding=10)
main_frame.pack(fill=tk.BOTH, expand=True)
# Input file selection
ttk.Label(main_frame, text="Input Video:").grid(row=0, column=0, sticky=tk.W, pady=5)
self.input_path_var = tk.StringVar()
ttk.Entry(main_frame, textvariable=self.input_path_var, width=50).grid(row=0, column=1, pady=5, padx=5)
ttk.Button(main_frame, text="Browse", command=self._select_input_file).grid(row=0, column=2, pady=5)
# Output folder selection
ttk.Label(main_frame, text="Output Folder:").grid(row=1, column=0, sticky=tk.W, pady=5)
self.output_path_var = tk.StringVar()
ttk.Entry(main_frame, textvariable=self.output_path_var, width=50).grid(row=1, column=1, pady=5, padx=5)
ttk.Button(main_frame, text="Browse", command=self._select_output_folder).grid(row=1, column=2, pady=5)
# Video information frame
info_frame = ttk.LabelFrame(main_frame, text="Video Information", padding=10)
info_frame.grid(row=2, column=0, columnspan=3, sticky=tk.EW, pady=10)
self.duration_var = tk.StringVar(value="Duration: Not available")
ttk.Label(info_frame, textvariable=self.duration_var).grid(row=0, column=0, sticky=tk.W)
self.gpu_var = tk.StringVar(value="GPU: Checking...")
ttk.Label(info_frame, textvariable=self.gpu_var).grid(row=1, column=0, sticky=tk.W)
# Splitting options frame
options_frame = ttk.LabelFrame(main_frame, text="Splitting Options", padding=10)
options_frame.grid(row=3, column=0, columnspan=3, sticky=tk.EW, pady=10)
# Split by duration
self.split_method = tk.StringVar(value="duration")
ttk.Radiobutton(options_frame, text="Split by Duration (seconds)",
variable=self.split_method, value="duration").grid(row=0, column=0, sticky=tk.W)
self.duration_input = ttk.Entry(options_frame, width=10)
self.duration_input.grid(row=0, column=1, padx=5)
self.duration_input.insert(0, "300") # Default 5 minutes
# Split by number of segments
ttk.Radiobutton(options_frame, text="Split into Equal Segments",
variable=self.split_method, value="segments").grid(row=1, column=0, sticky=tk.W)
self.segments_input = ttk.Entry(options_frame, width=10)
self.segments_input.grid(row=1, column=1, padx=5)
self.segments_input.insert(0, "3") # Default 3 segments
# Progress frame
progress_frame = ttk.Frame(main_frame)
progress_frame.grid(row=4, column=0, columnspan=3, sticky=tk.EW, pady=10)
self.progress_var = tk.StringVar(value="Ready")
ttk.Label(progress_frame, textvariable=self.progress_var).grid(row=0, column=0, sticky=tk.W)
self.progress_bar = ttk.Progressbar(progress_frame, mode="indeterminate")
self.progress_bar.grid(row=1, column=0, columnspan=3, sticky=tk.EW, pady=5)
# Action buttons
button_frame = ttk.Frame(main_frame)
button_frame.grid(row=5, column=0, columnspan=3, pady=10)
ttk.Button(button_frame, text="Split Video", command=self._start_splitting).pack(side=tk.LEFT, padx=5)
ttk.Button(button_frame, text="Exit", command=self.root.destroy).pack(side=tk.LEFT, padx=5)
# Update GPU status
self._update_gpu_status()
def _update_gpu_status(self):
if self.splitter.cuda_available:
self.gpu_var.set("GPU Acceleration: Available (NVENC)")
else:
self.gpu_var.set("GPU Acceleration: Not available (using CPU)")
def _select_input_file(self):
file_path = filedialog.askopenfilename(
title="Select Video File",
filetypes=[
("Video files", "*.mp4 *.avi *.mkv *.mov *.wmv"),
("All files", "*.*")
]
)
if file_path:
self.input_path_var.set(file_path)
self.splitter.input_file = file_path
self._update_video_info()
def _select_output_folder(self):
folder_path = filedialog.askdirectory(title="Select Output Folder")
if folder_path:
self.output_path_var.set(folder_path)
self.splitter.output_folder = folder_path
def _update_video_info(self):
if self.splitter.input_file:
duration = self.splitter.get_video_duration()
if duration > 0:
mins, secs = divmod(duration, 60)
hours, mins = divmod(mins, 60)
self.duration_var.set(f"Duration: {int(hours):02d}:{int(mins):02d}:{int(secs):02d}")
else:
self.duration_var.set("Duration: Could not determine")
def _start_splitting(self):
if not self.splitter.input_file:
messagebox.showerror("Error", "Please select an input video file")
return
if not self.splitter.output_folder:
messagebox.showerror("Error", "Please select an output folder")
return
# Start progress bar
self.progress_bar.start()
self.progress_var.set("Processing...")
# Run in a thread to keep GUI responsive
threading_method = self.split_method.get()
if threading_method == "duration":
try:
duration = float(self.duration_input.get())
if duration <= 0:
messagebox.showerror("Error", "Duration must be a positive number")
self.progress_bar.stop()
self.progress_var.set("Ready")
return
threading.Thread(target=self._run_split_by_duration, args=(duration,), daemon=True).start()
except ValueError:
messagebox.showerror("Error", "Invalid duration value")
self.progress_bar.stop()
self.progress_var.set("Ready")
else: # segments
try:
segments = int(self.segments_input.get())
if segments <= 0:
messagebox.showerror("Error", "Number of segments must be a positive integer")
self.progress_bar.stop()
self.progress_var.set("Ready")
return
threading.Thread(target=self._run_split_by_segments, args=(segments,), daemon=True).start()
except ValueError:
messagebox.showerror("Error", "Invalid number of segments")
self.progress_bar.stop()
self.progress_var.set("Ready")
def _run_split_by_duration(self, duration):
success, message = self.splitter.split_by_duration(duration)
self.root.after(0, self._processing_complete, success, message)
def _run_split_by_segments(self, segments):
success, message = self.splitter.split_by_segments(segments)
self.root.after(0, self._processing_complete, success, message)
def _processing_complete(self, success, message):
self.progress_bar.stop()
if success:
self.progress_var.set("Complete")
messagebox.showinfo("Success", message)
else:
self.progress_var.set("Failed")
messagebox.showerror("Error", message)
import os, math, subprocess, threading, platform
from datetime import timedelta
import customtkinter as ctk
from tkinter import filedialog, messagebox, StringVar, END
# ---------- CORE -------------------------------------------------
class VideoSplitter:
def __init__(self):
self.input_file = ""
self.output_folder = ""
self.ffmpeg_path = "ffmpeg"
self.duration = 0
self.cuda_available = self._check_cuda()
def _check_cuda(self):
try:
result = subprocess.run(
[self.ffmpeg_path, "-encoders"],
capture_output=True, text=True
)
return "nvenc" in result.stdout.lower()
except Exception:
return False
def get_video_duration(self):
if not self.input_file:
return 0
try:
result = subprocess.run(
[self.ffmpeg_path, "-i", self.input_file],
stderr=subprocess.PIPE, text=True
)
for line in result.stderr.splitlines():
if "Duration" in line:
time_str = line.split("Duration:")[1].split(",")[0].strip()
h, m, s = time_str.split(':')
s, ms = s.split('.')
self.duration = int(h)*3600 + int(m)*60 + int(s) + float(f"0.{ms}")
return self.duration
return 0
except Exception:
return 0
# ---------- splitting helpers ----------
def _run_ffmpeg(self, start_time, segment_duration, output_file):
if self.cuda_available:
cmd = [self.ffmpeg_path, "-hide_banner", "-y",
"-ss", str(timedelta(seconds=start_time)),
"-i", self.input_file, "-t", str(segment_duration),
"-c:v", "h264_nvenc", "-preset", "p4", "-tune", "hq",
"-b:v", "5M", "-maxrate", "8M", "-bufsize", "10M",
"-c:a", "aac", "-b:a", "192k", output_file]
else:
cmd = [self.ffmpeg_path, "-hide_banner", "-y",
"-ss", str(timedelta(seconds=start_time)),
"-i", self.input_file, "-t", str(segment_duration),
"-c:v", "libx264", "-preset", "fast", "-crf", "22",
"-c:a", "aac", "-b:a", "192k", output_file]
subprocess.run(cmd, check=True)
def split_by_duration(self, segment_duration, progress_cb=None, log_cb=None):
if not self.get_video_duration():
raise RuntimeError("Could not read video duration.")
num_segments = math.ceil(self.duration / segment_duration)
base = os.path.splitext(os.path.basename(self.input_file))[0]
for i in range(num_segments):
start = i * segment_duration
out_file = os.path.join(self.output_folder,
f"{base}_part{i+1:02d}.mp4")
if log_cb:
log_cb(f"[{i+1}/{num_segments}] → {os.path.basename(out_file)}")
self._run_ffmpeg(start, segment_duration, out_file)
if progress_cb:
progress_cb(i+1, num_segments)
def split_by_segments(self, num_segments, progress_cb=None, log_cb=None):
if not self.get_video_duration():
raise RuntimeError("Could not read video duration.")
seg_dur = self.duration / num_segments
self.split_by_duration(seg_dur, progress_cb, log_cb)
# ---------- UI ---------------------------------------------------
class VideoSplitterApp(ctk.CTk):
def __init__(self):
super().__init__()
# Window basics
self.title("10-Minute Video Splitter")
self.geometry("600x450")
ctk.set_appearance_mode("system") # auto dark/light
ctk.set_default_color_theme("blue") # macOS-ish accent
self.splitter = VideoSplitter()
# State
self.adv_on = False
self.split_mode = StringVar(value="duration")
# Layout ----------------------------------------------------
self._build_ui()
# -------------- UI helpers ------------------------------------
def _build_ui(self):
pad = 10
# Header
self.header = ctk.CTkLabel(self, text="🎬 10-Minute Video Splitter",
font=("SF Pro Display", 24, "bold"))
self.header.pack(pady=(20, 5))
self.subheader = ctk.CTkLabel(self,
text="Sleek. Simple. Powered by FFmpeg.",
font=("SF Pro Text", 13))
self.subheader.pack(pady=(0, 15))
# File section ----------
file_frame = ctk.CTkFrame(self, corner_radius=15)
file_frame.pack(fill="x", padx=pad, pady=(0, pad))
# Input file
self.in_path = StringVar()
ctk.CTkButton(file_frame, text="Choose Video",
command=self.select_file, width=140).grid(row=0, column=0, padx=pad, pady=pad)
self.in_entry = ctk.CTkEntry(file_frame, textvariable=self.in_path,
placeholder_text="No file selected")
self.in_entry.grid(row=0, column=1, sticky="ew", padx=(0, pad), pady=pad)
# Output folder
self.out_path = StringVar()
ctk.CTkButton(file_frame, text="Output Folder",
command=self.select_folder, width=140).grid(row=1, column=0, padx=pad, pady=(0, pad))
self.out_entry = ctk.CTkEntry(file_frame, textvariable=self.out_path,
placeholder_text="No folder selected")
self.out_entry.grid(row=1, column=1, sticky="ew", padx=(0, pad), pady=(0, pad))
file_frame.columnconfigure(1, weight=1)
# Info line
info_frame = ctk.CTkFrame(self, fg_color="transparent")
info_frame.pack(fill="x", padx=pad)
self.duration_lbl = ctk.CTkLabel(info_frame, text="Duration: —")
self.duration_lbl.pack(side="left")
self.gpu_lbl = ctk.CTkLabel(info_frame, text="GPU: checking…")
self.gpu_lbl.pack(side="right", padx=(0, 4))
self.after(100, self._update_gpu_status)
# Advanced toggle
self.adv_switch = ctk.CTkSwitch(self, text="Advanced options",
command=self.toggle_advanced)
self.adv_switch.pack(pady=(pad, 0))
# Placeholder advanced frame
self.adv_frame = ctk.CTkFrame(self, corner_radius=12)
self._build_advanced(self.adv_frame)
self.adv_frame.pack_forget()
# Progress bar & log -------------
self.progress = ctk.CTkProgressBar(self, height=6)
self.progress.set(0)
self.progress.pack(fill="x", padx=pad, pady=(pad, 0))
self.logbox = ctk.CTkTextbox(self, height=100)
self.logbox.configure(state="disabled")
self.logbox.pack(fill="both", expand=True, padx=pad, pady=(pad, pad))
# Buttons
btn_frame = ctk.CTkFrame(self, fg_color="transparent")
btn_frame.pack(pady=(0, pad))
self.split_btn = ctk.CTkButton(btn_frame, text="Split",
command=self.start_split, width=120)
self.split_btn.grid(row=0, column=0, padx=6)
ctk.CTkButton(btn_frame, text="Quit", width=80,
command=self.destroy).grid(row=0, column=1, padx=6)
def _build_advanced(self, parent):
pad = 8
# Split by duration
dur_radio = ctk.CTkRadioButton(parent, text="Split every (sec):",
variable=self.split_mode, value="duration")
dur_radio.grid(row=0, column=0, sticky="w", padx=pad, pady=pad)
self.dur_entry = ctk.CTkEntry(parent, width=80)
self.dur_entry.insert(0, "600") # 10 min default
self.dur_entry.grid(row=0, column=1, padx=pad, pady=pad)
# Split by segments
seg_radio = ctk.CTkRadioButton(parent, text="Split into segments:",
variable=self.split_mode, value="segments")
seg_radio.grid(row=1, column=0, sticky="w", padx=pad, pady=(0, pad))
self.seg_entry = ctk.CTkEntry(parent, width=80)
self.seg_entry.insert(0, "3")
self.seg_entry.grid(row=1, column=1, padx=pad, pady=(0, pad))
parent.columnconfigure(2, weight=1)
# -------------- callbacks -------------------------------------
def select_file(self):
file = filedialog.askopenfilename(
title="Select video",
filetypes=[("Video files", "*.mp4 *.mkv *.mov *.avi *.wmv"), ("All files", "*.*")]
)
if file:
self.in_path.set(file)
self.splitter.input_file = file
self._show_duration()
def select_folder(self):
folder = filedialog.askdirectory(title="Select output folder")
if folder:
self.out_path.set(folder)
self.splitter.output_folder = folder
def toggle_advanced(self):
self.adv_on = not self.adv_on
if self.adv_on:
self.adv_frame.pack(fill="x", padx=10, pady=10)
else:
self.adv_frame.pack_forget()
def _update_gpu_status(self):
txt = "GPU: NVENC ✅" if self.splitter.cuda_available else "GPU: CPU only"
self.gpu_lbl.configure(text=txt)
def _show_duration(self):
dur = self.splitter.get_video_duration()
if dur:
h, rem = divmod(int(dur), 3600)
m, s = divmod(rem, 60)
self.duration_lbl.configure(text=f"Duration: {h:02d}:{m:02d}:{s:02d}")
# -------------- splitting thread ------------------------------
def start_split(self):
if not self.in_path.get():
messagebox.showerror("Error", "Please select a video.")
return
if not self.out_path.get():
messagebox.showerror("Error", "Please pick an output folder.")
return
# Prep UI
self.progress.set(0)
self.logbox.configure(state="normal")
self.logbox.delete("1.0", END)
self.log("Starting…")
self.split_btn.configure(state="disabled")
# Parameters
try:
if not self.adv_on or self.split_mode.get() == "duration":
seg = float(self.dur_entry.get()) if self.adv_on else 600
if seg <= 0:
raise ValueError
args = ("duration", seg)
else:
num = int(self.seg_entry.get())
if num <= 0:
raise ValueError
args = ("segments", num)
except ValueError:
messagebox.showerror("Error", "Invalid advanced option.")
self.split_btn.configure(state="normal")
return
# Thread & GO
threading.Thread(target=self._split_worker, args=args, daemon=True).start()
def _split_worker(self, mode, val):
try:
if mode == "duration":
self.splitter.split_by_duration(val,
progress_cb=self._progress_cb, log_cb=self.log)
else:
self.splitter.split_by_segments(val,
progress_cb=self._progress_cb, log_cb=self.log)
self.after(0, lambda: messagebox.showinfo("Done", "Splitting completed 🎉"))
except Exception as e:
self.after(0, lambda: messagebox.showerror("Error", str(e)))
finally:
self.after(0, lambda: self.split_btn.configure(state="normal"))
# UI helpers inside worker thread must be marshalled with after()
def _progress_cb(self, done, total):
self.after(0, lambda: self.progress.set(done/total))
def log(self, text):
self.after(0, lambda: self._append_log(text))
def _append_log(self, text):
self.logbox.insert(END, text + "\n")
self.logbox.see(END)
self.logbox.configure(state="disabled")
# ---------- RUN --------------------------------------------------
if __name__ == "__main__":
app = VideoSplitterApp()
app.mainloop()