video-ffmpeg / app.py
Tim13ekd's picture
Update app.py
77921b4 verified
raw
history blame
1.59 kB
import gradio as gr
import subprocess
from pathlib import Path
import tempfile
import shutil
import uuid
allowed_medias = [".png", ".jpg", ".jpeg", ".bmp", ".gif", ".tiff"]
def execute_ffmpeg_image_to_video(image_file):
"""Konvertiert ein Bild in ein 5-Sekunden-Video"""
if image_file is None:
return None, "❌ Keine Datei ausgewählt"
temp_dir = tempfile.mkdtemp()
file_path = Path(image_file.name)
sanitized_name = file_path.name.replace(" ", "_")
dest_path = Path(temp_dir) / sanitized_name
shutil.copy(file_path, dest_path)
output_file = Path(temp_dir) / f"output_{uuid.uuid4().hex}.mp4"
cmd = [
"ffmpeg",
"-y",
"-loop", "1",
"-framerate", "25",
"-i", str(dest_path),
"-t", "5",
"-c:v", "mpeg4",
"-pix_fmt", "yuv420p",
str(output_file)
]
try:
subprocess.run(cmd, check=True, text=True, capture_output=True)
except subprocess.CalledProcessError as e:
return None, f"❌ FFmpeg Fehler:\n{e.stderr}"
return str(output_file), "✅ Video erfolgreich erstellt"
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# Bild → Video Konverter")
img_input = gr.File(file_types=allowed_medias, label="Bild auswählen")
out_video = gr.Video(interactive=False, label="Generiertes Video")
status = gr.Textbox(interactive=False, label="Status")
btn = gr.Button("Video erstellen")
btn.click(
fn=execute_ffmpeg_image_to_video,
inputs=[img_input],
outputs=[out_video, status],
)
demo.launch()