Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
import subprocess
|
| 4 |
+
import uuid
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
@app.get("/")
|
| 10 |
+
def health():
|
| 11 |
+
return {"status": "ok", "ffmpeg": True}
|
| 12 |
+
|
| 13 |
+
@app.post("/trim")
|
| 14 |
+
async def trim(
|
| 15 |
+
file: UploadFile = File(...),
|
| 16 |
+
start: float = 0,
|
| 17 |
+
duration: float = 5
|
| 18 |
+
):
|
| 19 |
+
input_path = f"/tmp/{uuid.uuid4()}_in.mp4"
|
| 20 |
+
output_path = f"/tmp/{uuid.uuid4()}_out.mp4"
|
| 21 |
+
|
| 22 |
+
with open(input_path, "wb") as f:
|
| 23 |
+
f.write(await file.read())
|
| 24 |
+
|
| 25 |
+
cmd = [
|
| 26 |
+
"ffmpeg", "-y",
|
| 27 |
+
"-ss", str(start),
|
| 28 |
+
"-i", input_path,
|
| 29 |
+
"-t", str(duration),
|
| 30 |
+
"-c", "copy",
|
| 31 |
+
output_path
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
subprocess.run(cmd, check=True)
|
| 35 |
+
|
| 36 |
+
return FileResponse(
|
| 37 |
+
output_path,
|
| 38 |
+
media_type="video/mp4",
|
| 39 |
+
filename="trimmed.mp4"
|
| 40 |
+
)
|