Spaces:
Sleeping
Sleeping
File size: 1,273 Bytes
68f8502 | 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 | 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) |