Tim13ekd commited on
Commit
e5b658f
·
verified ·
1 Parent(s): 44470f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -86
app.py CHANGED
@@ -1,103 +1,61 @@
1
  import gradio as gr
2
  import subprocess
3
- import tempfile
4
- import shutil
5
  from pathlib import Path
6
- import shlex
7
- import uuid
8
- import os
9
-
10
- ALLOWED_MEDIA = [
11
- ".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif",
12
- ".mp3", ".wav", ".ogg",
13
- ".mp4", ".mov", ".mkv", ".avi", ".webm"
14
- ]
15
 
 
16
 
17
- def run_ffmpeg(files, command):
18
  if not files:
19
- raise gr.Error("Bitte lade mindestens eine Datei hoch.")
20
-
21
- if not command.strip():
22
- raise gr.Error("Bitte gib einen FFmpeg Command ein.")
23
 
24
  temp_dir = tempfile.mkdtemp()
25
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  try:
27
- # Dateien ins Temp-Verzeichnis kopieren
28
- for file in files:
29
- src = Path(file.name)
30
- dst = Path(temp_dir) / src.name.replace(" ", "_")
31
- shutil.copy(src, dst)
32
-
33
- output_name = f"output_{uuid.uuid4()}.mp4"
34
- output_path = Path(temp_dir) / output_name
35
-
36
- # Command vorbereiten
37
- cmd = command.strip()
38
-
39
- if not cmd.startswith("ffmpeg"):
40
- cmd = "ffmpeg " + cmd
41
-
42
- args = shlex.split(cmd)
43
-
44
- # Falls output.mp4 verwendet wird → ersetzen
45
- args = [str(output_path) if a == "output.mp4" else a for a in args]
46
-
47
- # Falls kein Output definiert → anhängen
48
- if not any(a.endswith(".mp4") for a in args):
49
- args.append(str(output_path))
50
-
51
- # Ausführen
52
- process = subprocess.run(
53
- args,
54
- cwd=temp_dir,
55
- stdout=subprocess.PIPE,
56
- stderr=subprocess.PIPE,
57
- text=True,
58
- )
59
-
60
- if process.returncode != 0:
61
- raise gr.Error(f"FFmpeg Fehler:\n{process.stderr}")
62
-
63
- return str(output_path), f"```bash\n{' '.join(args)}\n```"
64
-
65
- finally:
66
- pass
67
-
68
 
 
69
  with gr.Blocks() as demo:
70
- gr.Markdown("# 🎬 FFmpeg Command Tool")
71
- gr.Markdown(
72
- "Lade Dateien hoch und führe **manuell eingegebene FFmpeg-Commands** aus. "
73
- "Kein KI-Processing – 100 % direktes FFmpeg."
74
- )
75
-
76
  with gr.Row():
77
  with gr.Column():
78
- files = gr.File(
79
- file_count="multiple",
80
- label="Media Dateien",
81
- file_types=ALLOWED_MEDIA,
82
- )
83
-
84
- command = gr.Textbox(
85
- label="FFmpeg Command",
86
- placeholder="z.B.: -i input.mp4 -ss 00:00:05 -t 10 -c copy output.mp4",
87
- lines=3,
88
- )
89
-
90
- run_btn = gr.Button("Run FFmpeg")
91
-
92
  with gr.Column():
93
- video_out = gr.Video(label="Output Video")
94
- command_out = gr.Markdown(label="Ausgeführter Command")
95
 
96
- run_btn.click(
97
- fn=run_ffmpeg,
98
- inputs=[files, command],
99
- outputs=[video_out, command_out],
100
- )
101
 
102
- demo.queue()
103
  demo.launch()
 
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()