Skip to content

Instantly share code, notes, and snippets.

@SimonTheCoder
Created November 16, 2023 03:27
Show Gist options
  • Save SimonTheCoder/789bcae7ec61b22698077aae496e362d to your computer and use it in GitHub Desktop.
Save SimonTheCoder/789bcae7ec61b22698077aae496e362d to your computer and use it in GitHub Desktop.
A script concats videos together into a single video.
import tkinter as tk
from tkinter import filedialog, messagebox, ttk, simpledialog
from moviepy.editor import VideoFileClip, concatenate_videoclips
from threading import Thread
import sys
def adjust_resolution(clip, target_resolution):
# Function to adjust video resolution while keeping the aspect ratio
target_width, target_height = target_resolution
clip_aspect_ratio = clip.w / clip.h
target_aspect_ratio = target_width / target_height
if clip_aspect_ratio > target_aspect_ratio:
# Video is wider than the target aspect ratio
return clip.resize(width=target_width)
else:
# Video is taller than the target aspect ratio
return clip.resize(height=target_height)
def concatenate_videos(video_files, output_path, target_resolution=(1920, 1080), bitrate='5000k', on_complete=None):
try:
clips = [adjust_resolution(VideoFileClip(path), target_resolution) for path in video_files]
final_clip = concatenate_videoclips(clips, method="compose")
final_clip.write_videofile(output_path, bitrate=bitrate, codec='libx264', audio_codec='aac', fps=24)
final_clip.close()
except Exception as e:
messagebox.showerror("Error", str(e))
finally:
if on_complete:
on_complete()
def main():
def remove_video():
selected = listbox.curselection()
if selected:
listbox.delete(selected[0])
def start_concatenation():
video_files = listbox.get(0, tk.END)
bitrate = bitrate_entry.get()
output_file = filedialog.asksaveasfilename(defaultextension=".mp4",
filetypes=[("MP4 files", "*.mp4")])
if output_file:
start_button.config(text="Connecting...", state='disabled')
Thread(target=concatenate_videos, args=(video_files, output_file, (1920, 1080), bitrate, on_concatenation_complete)).start()
def on_concatenation_complete():
start_button.config(text="Start", state='normal')
messagebox.showinfo("Success", "The videos have been successfully concatenated!")
def add_videos():
files = filedialog.askopenfilenames(filetypes=[("MP4 files", "*.mp4")])
for file in files:
listbox.insert(tk.END, file)
root = tk.Tk()
root.title("Video Concatenator")
listbox = tk.Listbox(root, width=50, height=15)
listbox.pack(padx=10, pady=10)
add_button = tk.Button(root, text="Add Videos", command=add_videos)
add_button.pack(padx=10, pady=5)
remove_button = tk.Button(root, text="Remove Video", command=remove_video)
remove_button.pack(padx=10, pady=5)
bitrate_label = tk.Label(root, text="Bitrate (e.g., '5000k'):")
bitrate_label.pack(padx=10, pady=5)
bitrate_entry = tk.Entry(root)
bitrate_entry.pack(padx=10, pady=5)
bitrate_entry.insert(0, "5000k")
start_button = tk.Button(root, text="Start", command=start_concatenation)
start_button.pack(padx=10, pady=10)
root.mainloop()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment