Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,14 +15,16 @@ def trim_video(file, start, duration):
|
|
| 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", "
|
|
|
|
|
|
|
| 26 |
output_path
|
| 27 |
]
|
| 28 |
subprocess.run(cmd, check=True, capture_output=True)
|
|
@@ -34,9 +36,71 @@ def trim_video(file, start, duration):
|
|
| 34 |
return output_path
|
| 35 |
|
| 36 |
except subprocess.CalledProcessError as e:
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return f"Allgemeiner Fehler:\n{str(e)}"
|
| 41 |
|
| 42 |
# -------- Gradio Interface --------
|
|
|
|
| 15 |
with open(input_path, "wb") as f:
|
| 16 |
f.write(file_bytes)
|
| 17 |
|
| 18 |
+
# --- FFmpeg Befehl mit Rekodierung für korrektes Trimming ---
|
| 19 |
cmd = [
|
| 20 |
"ffmpeg",
|
| 21 |
"-y",
|
| 22 |
"-ss", str(start),
|
| 23 |
"-i", input_path,
|
| 24 |
"-t", str(duration),
|
| 25 |
+
"-c:v", "libx264",
|
| 26 |
+
"-c:a", "aac",
|
| 27 |
+
"-strict", "experimental",
|
| 28 |
output_path
|
| 29 |
]
|
| 30 |
subprocess.run(cmd, check=True, capture_output=True)
|
|
|
|
| 36 |
return output_path
|
| 37 |
|
| 38 |
except subprocess.CalledProcessError as e:
|
| 39 |
+
# FFmpeg Fehler ausgeben
|
| 40 |
+
return f"FFmpeg-Fehler:\n{e.stderr.decode(errors='ignore')}"
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
+
# Allgemeiner Fehler
|
| 44 |
+
return f"Allgemeiner Fehler:\n{str(e)}"
|
| 45 |
+
|
| 46 |
+
# -------- Gradio Interface --------
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=trim_video,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.File(label="Video hochladen", file_types=[".mp4"]),
|
| 51 |
+
gr.Number(label="Startzeit (Sekunden)", value=0),
|
| 52 |
+
gr.Number(label="Dauer (Sekunden)", value=5)
|
| 53 |
+
],
|
| 54 |
+
outputs=gr.Video(label="Getrimmtes Video"),
|
| 55 |
+
title="FFmpeg Video Editor",
|
| 56 |
+
description="Trimme Videos direkt im Hugging Face Space. Fehlermeldungen werden angezeigt, falls etwas schiefgeht."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# -------- Server starten --------
|
| 60 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
| 61 |
+
import gradio as gr
|
| 62 |
+
import subprocess
|
| 63 |
+
import uuid
|
| 64 |
+
import os
|
| 65 |
+
|
| 66 |
+
# -------- Video Trim Funktion --------
|
| 67 |
+
def trim_video(file, start, duration):
|
| 68 |
+
try:
|
| 69 |
+
# --- Pfade für temporäre Dateien ---
|
| 70 |
+
input_path = f"/tmp/{uuid.uuid4()}_in.mp4"
|
| 71 |
+
output_path = f"/tmp/{uuid.uuid4()}_out.mp4"
|
| 72 |
+
|
| 73 |
+
# --- Datei speichern ---
|
| 74 |
+
file_bytes = file.read() # Gradio liefert SpooledTemporaryFile
|
| 75 |
+
with open(input_path, "wb") as f:
|
| 76 |
+
f.write(file_bytes)
|
| 77 |
+
|
| 78 |
+
# --- FFmpeg Befehl mit Rekodierung für korrektes Trimming ---
|
| 79 |
+
cmd = [
|
| 80 |
+
"ffmpeg",
|
| 81 |
+
"-y",
|
| 82 |
+
"-ss", str(start),
|
| 83 |
+
"-i", input_path,
|
| 84 |
+
"-t", str(duration),
|
| 85 |
+
"-c:v", "libx264",
|
| 86 |
+
"-c:a", "aac",
|
| 87 |
+
"-strict", "experimental",
|
| 88 |
+
output_path
|
| 89 |
+
]
|
| 90 |
+
subprocess.run(cmd, check=True, capture_output=True)
|
| 91 |
+
|
| 92 |
+
# --- Prüfen, ob Output existiert ---
|
| 93 |
+
if not os.path.exists(output_path):
|
| 94 |
+
return "Fehler: Output-Video wurde nicht erstellt."
|
| 95 |
+
|
| 96 |
+
return output_path
|
| 97 |
+
|
| 98 |
+
except subprocess.CalledProcessError as e:
|
| 99 |
+
# FFmpeg Fehler ausgeben
|
| 100 |
+
return f"FFmpeg-Fehler:\n{e.stderr.decode(errors='ignore')}"
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
# Allgemeiner Fehler
|
| 104 |
return f"Allgemeiner Fehler:\n{str(e)}"
|
| 105 |
|
| 106 |
# -------- Gradio Interface --------
|