from fastapi import FastAPI, UploadFile, File, Form, HTTPException from fastapi.responses import FileResponse import subprocess import shutil import os import uuid app = FastAPI() @app.post("/edit") async def edit_video( file: UploadFile = File(...), ffmpeg_cmd: str = Form(...), # לדוגמה: "-ss 00:10 -t 00:15 -vf drawtext=..." ): input_path = f"/tmp/input_{uuid.uuid4()}.mp4" output_path = f"/tmp/output_{uuid.uuid4()}.mp4" with open(input_path, "wb") as f: shutil.copyfileobj(file.file, f) # הגנה בסיסית מאוד (אפשר לשפר) if any(x in ffmpeg_cmd for x in [";", "|", "&", "`", "$(", "rm ", "wget "]): raise HTTPException(400, "פקודה לא בטוחה") full_cmd = ["ffmpeg", "-y", "-i", input_path] + ffmpeg_cmd.split() + [output_path] try: result = subprocess.run(full_cmd, capture_output=True, text=True, timeout=180) if result.returncode != 0: return {"error": result.stderr} return FileResponse(output_path, media_type="video/mp4", filename="edited.mp4") except Exception as e: raise HTTPException(500, str(e)) finally: for p in [input_path, output_path]: if os.path.exists(p): os.remove(p)