Tim13ekd commited on
Commit
bf857ff
·
verified ·
1 Parent(s): 3bdfbb3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -5,26 +5,39 @@ import os
5
 
6
  # -------- Video Trim Funktion --------
7
  def trim_video(file, start, duration):
8
- input_path = f"/tmp/{uuid.uuid4()}_in.mp4"
9
- output_path = f"/tmp/{uuid.uuid4()}_out.mp4"
10
-
11
- # Video speichern
12
- with open(input_path, "wb") as f:
13
- f.write(file.read())
14
-
15
- # FFmpeg Befehl ausführen
16
- cmd = [
17
- "ffmpeg",
18
- "-y",
19
- "-ss", str(start),
20
- "-i", input_path,
21
- "-t", str(duration),
22
- "-c", "copy",
23
- output_path
24
- ]
25
- subprocess.run(cmd, check=True)
26
-
27
- return output_path
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  # -------- Gradio Interface --------
30
  iface = gr.Interface(
@@ -36,7 +49,7 @@ iface = gr.Interface(
36
  ],
37
  outputs=gr.Video(label="Getrimmtes Video"),
38
  title="FFmpeg Video Editor",
39
- description="Trimme Videos direkt im Hugging Face Space"
40
  )
41
 
42
  # -------- Server starten --------
 
5
 
6
  # -------- Video Trim Funktion --------
7
  def trim_video(file, start, duration):
8
+ try:
9
+ # --- Pfade für temporäre Dateien ---
10
+ input_path = f"/tmp/{uuid.uuid4()}_in.mp4"
11
+ output_path = f"/tmp/{uuid.uuid4()}_out.mp4"
12
+
13
+ # --- Datei speichern ---
14
+ file_bytes = file.read() # Gradio liefert SpooledTemporaryFile
15
+ with open(input_path, "wb") as f:
16
+ f.write(file_bytes)
17
+
18
+ # --- FFmpeg Befehl ---
19
+ cmd = [
20
+ "ffmpeg",
21
+ "-y",
22
+ "-ss", str(start),
23
+ "-i", input_path,
24
+ "-t", str(duration),
25
+ "-c", "copy",
26
+ output_path
27
+ ]
28
+ subprocess.run(cmd, check=True, capture_output=True)
29
+
30
+ # --- Prüfen, ob Output existiert ---
31
+ if not os.path.exists(output_path):
32
+ return "Fehler: Output-Video wurde nicht erstellt."
33
+
34
+ return output_path
35
+
36
+ except subprocess.CalledProcessError as e:
37
+ return f"FFmpeg-Fehler:\n{e.stderr.decode()}"
38
+
39
+ except Exception as e:
40
+ return f"Allgemeiner Fehler:\n{str(e)}"
41
 
42
  # -------- Gradio Interface --------
43
  iface = gr.Interface(
 
49
  ],
50
  outputs=gr.Video(label="Getrimmtes Video"),
51
  title="FFmpeg Video Editor",
52
+ description="Trimme Videos direkt im Hugging Face Space. Fehlermeldungen werden angezeigt, falls etwas schiefgeht."
53
  )
54
 
55
  # -------- Server starten --------