-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
First of all love this program!
I'm in a slight bit of a rush and might make a proper PR later, but here is some updated code thats a bit faster
import os
import subprocess
import tkinter as tk
from tkinter import filedialog, messagebox, scrolledtext
import re
def select_file(filetypes, title):
return filedialog.askopenfilename(filetypes=filetypes, title=title)
def get_audio_duration(audio_path):
cmd = [
"ffprobe", "-v", "error",
"-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1",
audio_path
]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
return float(result.stdout.strip())
def hms_to_seconds(hms):
h, m, s = hms.split(":")
return int(h) * 3600 + int(m) * 60 + float(s)
def create_video():
image_path = select_file([("Image Files", "*.jpg;*.jpeg;*.png")], "Select an Image")
if not image_path:
return
audio_path = select_file([("Audio Files", "*.mp3;*.wav")], "Select an Audio File")
if not audio_path:
return
output_folder = os.path.dirname(audio_path)
output_name = os.path.splitext(os.path.basename(audio_path))[0] + ".mp4"
output_path = os.path.join(output_folder, output_name)
duration = get_audio_duration(audio_path)
command = [
"ffmpeg",
"-y",
"-loop", "1",
"-i", image_path,
"-i", audio_path,
"-c:v", "libx264",
"-preset", "ultrafast",
"-tune", "stillimage",
"-pix_fmt", "yuv420p",
"-r", "30",
"-c:a", "aac",
"-b:a", "384k",
"-shortest",
"-movflags", "+faststart",
output_path
]
console_output.delete("1.0", tk.END)
console_output.insert(tk.END, "Starting conversion...\n")
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
time_pattern = re.compile(r"time=(\d+:\d+:\d+\.\d+)")
for line in process.stdout:
match = time_pattern.search(line)
if match:
current_time = hms_to_seconds(match.group(1))
percent = min((current_time / duration) * 100, 100)
console_output.insert(
tk.END,
f"\rProgress: {percent:.1f}% ({current_time:.1f}s / {duration:.1f}s)\n"
)
else:
console_output.insert(tk.END, line)
console_output.see(tk.END)
console_output.update_idletasks()
process.wait()
if process.returncode == 0:
messagebox.showinfo("Success", f"MP4 created successfully:\n{output_path}")
else:
messagebox.showerror("Error", "FFmpeg failed. Ensure FFmpeg is installed.")
# Create GUI
root = tk.Tk()
root.title("Music Video Maker")
root.geometry("600x350")
tk.Label(root, text="Convert an image and audio file into an MP4 video").pack(pady=5)
tk.Label(root, text="Supported file types: JPG, PNG, MP3, WAV").pack(pady=5)
convert_button = tk.Button(root, text="Select Files & Convert", command=create_video)
convert_button.pack(pady=10)
console_output = scrolledtext.ScrolledText(root, height=12, width=70)
console_output.pack(pady=10)
root.mainloop()
MAIN CHANGES
- Uses bitrate of 384k which is max for youtube, because audio quality should be max no matter what (a little unrelated to speed)
- adds -preset ultrafast -pix_fmt yuv420p -r 30 in the ffmpeg argument string array thing to speed up encoding
- adds get_audio_duration so users can see what percent of encoding is done. mabye a new feature after this could be estimated time?
- Thats it 👍
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels