Tim13ekd commited on
Commit
d593d36
·
verified ·
1 Parent(s): 7897a95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -38
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
- # Dateien ins Temp-Verzeichnis kopieren
 
 
17
  for file in files:
18
  file_path = Path(file.name)
19
- sanitized_name = file_path.name.replace(" ", "_")
20
- dest_path = Path(temp_dir) / sanitized_name
21
- shutil.copy(file.name, dest_path)
22
- uploaded_files.append(dest_path)
 
 
 
 
 
 
 
23
 
24
- # Nehmen wir nur die erste Datei als Beispiel
25
- input_file = uploaded_files[0]
26
- output_file = Path(temp_dir) / "output.mp4"
27
 
28
- # FFmpeg Command
29
  cmd = [
30
  "ffmpeg",
31
- "-y", # Überschreiben, falls existiert
32
- "-loop", "1", # Bild wiederholen
33
- "-i", str(input_file),
34
- "-t", "5", # Video 5 Sekunden
35
- "-c:v", "libx264",
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 None, f"❌ FFmpeg Fehler:\n{e}"
 
 
47
 
48
- # Gradio Interface
49
  with gr.Blocks() as demo:
50
- gr.Markdown("# 📸 Bild → Video Converter")
51
- with gr.Row():
52
- with gr.Column():
53
- user_files = gr.File(file_types=allowed_medias, file_count="multiple", label="Bild-Dateien")
54
- btn = gr.Button("Video erstellen")
55
- with gr.Column():
56
- video_output = gr.Video(label="Ausgabe Video")
57
- command_output = gr.Markdown()
58
-
59
- btn.click(fn=image_to_video, inputs=user_files, outputs=[video_output, command_output])
 
 
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()