import gradio as gr import subprocess import uuid import os def trim_video(file, start, duration): try: input_path = f"/tmp/{uuid.uuid4()}_in.mp4" output_path = f"/tmp/{uuid.uuid4()}_out.mp4" # --- Datei in Bytes konvertieren --- if hasattr(file, "read"): file_bytes = file.read() # File-Objekt elif isinstance(file, str): # NamedString liefert String → in Bytes umwandeln file_bytes = file.encode("utf-8") else: return f"Unbekannter Dateityp: {type(file)}" # --- Datei speichern --- with open(input_path, "wb") as f: f.write(file_bytes) # --- FFmpeg Befehl mit Rekodierung --- cmd = [ "ffmpeg", "-y", "-ss", str(start), "-i", input_path, "-t", str(duration), "-c:v", "libx264", "-c:a", "aac", "-strict", "experimental", output_path ] subprocess.run(cmd, check=True, capture_output=True) if not os.path.exists(output_path): return "Fehler: Output-Video wurde nicht erstellt." return output_path except subprocess.CalledProcessError as e: return f"FFmpeg-Fehler:\n{e.stderr.decode(errors='ignore')}" except Exception as e: return f"Allgemeiner Fehler:\n{str(e)}" # -------- Gradio Interface -------- iface = gr.Interface( fn=trim_video, inputs=[ gr.File(label="Video hochladen", file_types=[".mp4"]), gr.Number(label="Startzeit (Sekunden)", value=0), gr.Number(label="Dauer (Sekunden)", value=5) ], outputs=gr.Video(label="Getrimmtes Video"), title="FFmpeg Video Editor", description="Trimme Videos direkt im Hugging Face Space. Fehlermeldungen werden angezeigt, falls etwas schiefgeht." ) iface.launch(server_name="0.0.0.0", server_port=7860)