Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
from pathlib import Path
|
| 4 |
-
import shutil
|
| 5 |
import tempfile
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
allowed_medias = [".png", ".jpg", ".jpeg", ".bmp", ".gif"]
|
| 8 |
-
|
| 9 |
-
def image_to_video(files):
|
| 10 |
-
if not files:
|
| 11 |
-
return None, "❌ Keine Datei hochgeladen"
|
| 12 |
-
|
| 13 |
-
temp_dir = tempfile.mkdtemp()
|
| 14 |
-
uploaded_files = []
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
for file in files:
|
| 18 |
file_path = Path(file.name)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
input_file = uploaded_files[0]
|
| 26 |
-
output_file = Path(temp_dir) / "output.mp4"
|
| 27 |
|
| 28 |
-
# FFmpeg Command
|
| 29 |
cmd = [
|
| 30 |
"ffmpeg",
|
| 31 |
-
"-y",
|
| 32 |
-
"-loop", "1",
|
| 33 |
-
"-i", str(
|
| 34 |
-
"-t", "5",
|
| 35 |
-
"-c:v", "
|
| 36 |
"-pix_fmt", "yuv420p",
|
| 37 |
str(output_file)
|
| 38 |
]
|
| 39 |
|
| 40 |
# FFmpeg ausführen
|
| 41 |
try:
|
| 42 |
-
subprocess.run(cmd, check=True)
|
| 43 |
-
command_str = " ".join(cmd)
|
| 44 |
-
return str(output_file), f"✅ Video erstellt!\n\nFFmpeg Command:\n`{command_str}`"
|
| 45 |
except subprocess.CalledProcessError as e:
|
| 46 |
-
return
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
# Gradio
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
-
gr.Markdown("#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
from pathlib import Path
|
|
|
|
| 4 |
import tempfile
|
| 5 |
+
import shutil
|
| 6 |
+
import uuid
|
| 7 |
|
| 8 |
+
allowed_medias = [".png", ".jpg", ".jpeg", ".bmp", ".gif", ".tiff"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def get_files_infos(files):
|
| 11 |
+
"""Gibt einfache Dateiinformationen zurück"""
|
| 12 |
+
results = []
|
| 13 |
for file in files:
|
| 14 |
file_path = Path(file.name)
|
| 15 |
+
info = {"name": file_path.name.replace(" ", "_"), "size": file_path.stat().st_size}
|
| 16 |
+
results.append(info)
|
| 17 |
+
return results
|
| 18 |
+
|
| 19 |
+
def execute_ffmpeg_image_to_video(image_file):
|
| 20 |
+
"""Konvertiert ein Bild in ein 5-Sekunden-Video"""
|
| 21 |
+
temp_dir = tempfile.mkdtemp()
|
| 22 |
+
file_path = Path(image_file.name)
|
| 23 |
+
sanitized_name = file_path.name.replace(" ", "_")
|
| 24 |
+
dest_path = Path(temp_dir) / sanitized_name
|
| 25 |
+
shutil.copy(file_path, dest_path)
|
| 26 |
|
| 27 |
+
output_file = Path(temp_dir) / f"output_{uuid.uuid4().hex}.mp4"
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
cmd = [
|
| 30 |
"ffmpeg",
|
| 31 |
+
"-y",
|
| 32 |
+
"-loop", "1",
|
| 33 |
+
"-i", str(dest_path),
|
| 34 |
+
"-t", "5",
|
| 35 |
+
"-c:v", "mpeg4", # funktioniert im HF-Container besser als libx264
|
| 36 |
"-pix_fmt", "yuv420p",
|
| 37 |
str(output_file)
|
| 38 |
]
|
| 39 |
|
| 40 |
# FFmpeg ausführen
|
| 41 |
try:
|
| 42 |
+
subprocess.run(cmd, check=True, text=True, capture_output=True)
|
|
|
|
|
|
|
| 43 |
except subprocess.CalledProcessError as e:
|
| 44 |
+
return f"❌ FFmpeg Fehler:\n{e.stderr}", None
|
| 45 |
+
|
| 46 |
+
return str(output_file), "✅ Video erfolgreich erstellt"
|
| 47 |
|
| 48 |
+
# Gradio UI
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# Bild → Video Konverter")
|
| 51 |
+
|
| 52 |
+
img_input = gr.File(file_types=allowed_medias, label="Bild auswählen")
|
| 53 |
+
out_video = gr.Video(interactive=False, label="Generiertes Video")
|
| 54 |
+
status = gr.Textbox(interactive=False, label="Status")
|
| 55 |
+
|
| 56 |
+
btn = gr.Button("Video erstellen")
|
| 57 |
+
btn.click(
|
| 58 |
+
fn=execute_ffmpeg_image_to_video,
|
| 59 |
+
inputs=[img_input],
|
| 60 |
+
outputs=[out_video, status],
|
| 61 |
+
)
|
| 62 |
|
| 63 |
demo.launch()
|