File size: 1,924 Bytes
889f1d1
cd88d93
 
 
 
889f1d1
bf857ff
 
 
 
91572a7
 
 
 
 
 
 
 
4426a8d
 
 
 
 
86abf34
4426a8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf857ff
889f1d1
 
 
 
 
 
 
 
 
 
 
bf857ff
889f1d1
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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)