Skip to content

Instantly share code, notes, and snippets.

@twobob
Created April 16, 2024 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twobob/b9b9ebcb9081ccdd85a8119cafc3f9af to your computer and use it in GitHub Desktop.
Save twobob/b9b9ebcb9081ccdd85a8119cafc3f9af to your computer and use it in GitHub Desktop.
merge gif and wav into mp4 with a ui via python and ffmpeg
import tkinter as tk
from tkinter import filedialog, messagebox
import subprocess
import os
def browse_file(entry, file_type):
if file_type == 'audio':
filename = filedialog.askopenfilename(filetypes=[("Audio files", "*.wav")])
elif file_type == 'gif':
filename = filedialog.askopenfilename(filetypes=[("GIF files", "*.gif")])
elif file_type == 'output':
filename = filedialog.asksaveasfilename(filetypes=[("MP4 files", "*.mp4")], defaultextension=[("MP4 files", "*.mp4")])
entry.delete(0, tk.END)
entry.insert(0, filename)
def merge_files(audio_path, gif_path, output_path):
command = [
"ffmpeg",
"-i", gif_path,
"-i", audio_path,
"-c:v", "libx264",
"-pix_fmt", "yuv420p",
"-c:a", "aac",
"-shortest",
output_path
]
try:
subprocess.run(command, check=True)
messagebox.showinfo("Success", "The video file has been successfully created.")
except subprocess.CalledProcessError as e:
messagebox.showerror("Error", f"An error occurred during file merging: {e}")
def main():
root = tk.Tk()
root.title("Audio-GIF Merger")
tk.Label(root, text="Select Audio File:").grid(row=0, column=0)
audio_entry = tk.Entry(root, width=50)
audio_entry.grid(row=0, column=1)
tk.Button(root, text="Browse", command=lambda: browse_file(audio_entry, 'audio')).grid(row=0, column=2)
tk.Label(root, text="Select GIF File:").grid(row=1, column=0)
gif_entry = tk.Entry(root, width=50)
gif_entry.grid(row=1, column=1)
tk.Button(root, text="Browse", command=lambda: browse_file(gif_entry, 'gif')).grid(row=1, column=2)
tk.Label(root, text="Select Output File:").grid(row=2, column=0)
output_entry = tk.Entry(root, width=50)
output_entry.grid(row=2, column=1)
tk.Button(root, text="Browse", command=lambda: browse_file(output_entry, 'output')).grid(row=2, column=2)
tk.Button(root, text="Merge Files", command=lambda: merge_files(audio_entry.get(), gif_entry.get(), output_entry.get())).grid(row=3, column=1)
root.mainloop()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment