Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,76 +6,20 @@ import os
|
|
| 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
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 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)
|
| 31 |
-
|
| 32 |
-
# --- Prüfen, ob Output existiert ---
|
| 33 |
-
if not os.path.exists(output_path):
|
| 34 |
-
return "Fehler: Output-Video wurde nicht erstellt."
|
| 35 |
-
|
| 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
|
| 79 |
cmd = [
|
| 80 |
"ffmpeg",
|
| 81 |
"-y",
|
|
@@ -96,13 +40,12 @@ def trim_video(file, start, duration):
|
|
| 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 --------
|
| 107 |
iface = gr.Interface(
|
| 108 |
fn=trim_video,
|
|
|
|
| 6 |
# -------- Video Trim Funktion --------
|
| 7 |
def trim_video(file, start, duration):
|
| 8 |
try:
|
|
|
|
| 9 |
input_path = f"/tmp/{uuid.uuid4()}_in.mp4"
|
| 10 |
output_path = f"/tmp/{uuid.uuid4()}_out.mp4"
|
| 11 |
|
| 12 |
+
# --- Datei korrekt lesen ---
|
| 13 |
+
if hasattr(file, "read"): # SpooledTemporaryFile oder Dateiobjekt
|
| 14 |
+
file_bytes = file.read()
|
| 15 |
+
else: # NamedString oder direkt Pfad
|
| 16 |
+
file_bytes = file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# --- Datei speichern ---
|
|
|
|
| 19 |
with open(input_path, "wb") as f:
|
| 20 |
f.write(file_bytes)
|
| 21 |
|
| 22 |
+
# --- FFmpeg Befehl mit Rekodierung ---
|
| 23 |
cmd = [
|
| 24 |
"ffmpeg",
|
| 25 |
"-y",
|
|
|
|
| 40 |
return output_path
|
| 41 |
|
| 42 |
except subprocess.CalledProcessError as e:
|
|
|
|
| 43 |
return f"FFmpeg-Fehler:\n{e.stderr.decode(errors='ignore')}"
|
| 44 |
|
| 45 |
except Exception as e:
|
|
|
|
| 46 |
return f"Allgemeiner Fehler:\n{str(e)}"
|
| 47 |
|
| 48 |
+
|
| 49 |
# -------- Gradio Interface --------
|
| 50 |
iface = gr.Interface(
|
| 51 |
fn=trim_video,
|