-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvertMP4toGIF.py
More file actions
109 lines (93 loc) · 3.95 KB
/
ConvertMP4toGIF.py
File metadata and controls
109 lines (93 loc) · 3.95 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
import tkinter as tk
from tkinter import filedialog, messagebox
import subprocess
import os
import tempfile
def browse_input():
file_path = filedialog.askopenfilename(filetypes=[("MP4 files", "*.mp4")])
if file_path:
entry_input.delete(0, tk.END)
entry_input.insert(0, file_path)
def browse_output():
file_path = filedialog.asksaveasfilename(defaultextension=".gif", filetypes=[("GIF files", "*.gif")])
if file_path:
entry_output.delete(0, tk.END)
entry_output.insert(0, file_path)
def convert_video():
input_file = entry_input.get()
output_file = entry_output.get()
fps = entry_fps.get()
width = entry_width.get()
height = entry_height.get()
if not os.path.exists(input_file):
messagebox.showerror("Error", "Input file does not exist.")
return
if not output_file:
messagebox.showerror("Error", "Please specify an output file.")
return
if not fps:
messagebox.showerror("Error", "Please enter FPS value.")
return
if not width or not height:
messagebox.showerror("Error", "Please enter both width and height.")
return
try:
# Create a temporary file for the palette
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
palette_file = tmp.name
# Step 1: Generate the palette
palette_cmd = [
"ffmpeg",
"-y",
"-i", input_file,
"-vf", f"fps={fps},scale={width}:{height}:flags=lanczos,palettegen",
palette_file
]
subprocess.run(palette_cmd, check=True)
# Step 2: Use the palette to create the GIF
gif_cmd = [
"ffmpeg",
"-i", input_file,
"-i", palette_file,
"-filter_complex", f"fps={fps},scale={width}:{height}:flags=lanczos[x];[x][1:v]paletteuse",
output_file
]
subprocess.run(gif_cmd, check=True)
# Remove the temporary palette file
os.remove(palette_file)
messagebox.showinfo("Success", "Conversion completed successfully!")
except subprocess.CalledProcessError as e:
messagebox.showerror("Error", f"Conversion failed:\n{e}")
except Exception as e:
messagebox.showerror("Error", f"An error occurred:\n{e}")
# Set up the GUI
root = tk.Tk()
root.title("MP4 to GIF Converter")
root.wm_attributes('-toolwindow', 'True') # Set as a tool window
# Input file selection
tk.Label(root, text="Input MP4 File:").grid(row=0, column=0, padx=5, pady=5, sticky="e")
entry_input = tk.Entry(root, width=50)
entry_input.grid(row=0, column=1, padx=5, pady=5)
tk.Button(root, text="Browse", command=browse_input).grid(row=0, column=2, padx=5, pady=5)
# Output file selection
tk.Label(root, text="Output GIF File:").grid(row=1, column=0, padx=5, pady=5, sticky="e")
entry_output = tk.Entry(root, width=50)
entry_output.grid(row=1, column=1, padx=5, pady=5)
tk.Button(root, text="Browse", command=browse_output).grid(row=1, column=2, padx=5, pady=5)
# FPS input with pre-filled value "30"
tk.Label(root, text="FPS:").grid(row=2, column=0, padx=5, pady=5, sticky="e")
entry_fps = tk.Entry(root)
entry_fps.grid(row=2, column=1, padx=5, pady=5, sticky="w")
entry_fps.insert(0, "30")
# Output size input with pre-filled values 640x480
tk.Label(root, text="Output Size (width x height):").grid(row=3, column=0, padx=5, pady=5, sticky="e")
entry_width = tk.Entry(root, width=10)
entry_width.grid(row=3, column=1, padx=(5,0), pady=5, sticky="w")
entry_width.insert(0, "640")
tk.Label(root, text="x").grid(row=3, column=1, padx=(80,0), pady=5, sticky="w")
entry_height = tk.Entry(root, width=10)
entry_height.grid(row=3, column=1, padx=(100,0), pady=5, sticky="w")
entry_height.insert(0, "480")
# Convert button
tk.Button(root, text="Convert", command=convert_video).grid(row=4, column=1, padx=5, pady=15)
root.mainloop()