Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,18 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
from fastapi.responses import FileResponse, JSONResponse
|
| 3 |
-
import
|
| 4 |
-
from threading import Thread
|
| 5 |
-
import logging
|
| 6 |
-
|
| 7 |
-
logging.basicConfig(level=logging.INFO)
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
JOBS_DIR = "/tmp/jobs"
|
| 12 |
os.makedirs(JOBS_DIR, exist_ok=True)
|
| 13 |
-
jobs = {} # job_id: {"status": "processing/completed/failed", "output_file": ..., "error": ...}
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
try:
|
| 18 |
-
logging.info(f"Downloading file from: {url}")
|
| 19 |
-
headers = {"User-Agent": "Mozilla/5.0"} # Pretend to be a browser
|
| 20 |
-
r = requests.get(url, headers=headers, stream=True)
|
| 21 |
-
r.raise_for_status()
|
| 22 |
-
with open(filename, 'wb') as f:
|
| 23 |
-
for chunk in r.iter_content(chunk_size=8192):
|
| 24 |
-
f.write(chunk)
|
| 25 |
-
logging.info(f"Downloaded: {filename}")
|
| 26 |
-
except Exception as e:
|
| 27 |
-
logging.error(f"Failed to download: {url} - {e}")
|
| 28 |
-
raise
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def run_ffmpeg(base_file, overlay_file, output_file):
|
|
@@ -42,72 +28,47 @@ def run_ffmpeg(base_file, overlay_file, output_file):
|
|
| 42 |
return result.returncode == 0
|
| 43 |
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
if success and os.path.exists(output_file):
|
| 57 |
-
|
| 58 |
-
jobs[job_id]["output_file"] = output_file
|
| 59 |
-
logging.info(f"Job {job_id} completed successfully. Output: {output_file}")
|
| 60 |
else:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
logging.error(f"Job {job_id} failed. No output file created.")
|
| 64 |
except Exception as e:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
logging.error(f"Job {job_id} failed with exception: {e}")
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
@app.post("/generate-video/")
|
| 71 |
-
async def generate_video(request: Request):
|
| 72 |
-
data = await request.json()
|
| 73 |
-
base_url = data.get("video_url")
|
| 74 |
-
overlay_url = data.get("overlay_url")
|
| 75 |
-
|
| 76 |
-
if not base_url or not overlay_url:
|
| 77 |
-
return JSONResponse({"error": "Missing video_url or overlay_url"}, status_code=400)
|
| 78 |
-
|
| 79 |
-
job_id = str(uuid.uuid4())
|
| 80 |
-
jobs[job_id] = {"status": "processing"}
|
| 81 |
-
|
| 82 |
-
Thread(target=background_process, args=(job_id, base_url, overlay_url)).start()
|
| 83 |
-
|
| 84 |
-
return JSONResponse({"job_id": job_id, "status": "processing"})
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
@app.get("/status/{job_id}")
|
| 88 |
-
async def check_status(job_id: str):
|
| 89 |
-
job_info = jobs.get(job_id)
|
| 90 |
-
if not job_info:
|
| 91 |
-
return JSONResponse({"error": "Invalid job_id"}, status_code=404)
|
| 92 |
-
|
| 93 |
-
response = {"status": job_info["status"]}
|
| 94 |
-
if "error" in job_info:
|
| 95 |
-
response["error"] = job_info["error"]
|
| 96 |
-
if job_info["status"] == "completed":
|
| 97 |
-
response["download_url"] = f"/download/{job_id}"
|
| 98 |
-
return JSONResponse(response)
|
| 99 |
|
|
|
|
| 100 |
|
| 101 |
-
@app.get("/download/{job_id}")
|
| 102 |
-
async def download_video(job_id: str):
|
| 103 |
-
job_info = jobs.get(job_id)
|
| 104 |
-
if not job_info or job_info["status"] != "completed":
|
| 105 |
-
return JSONResponse({"error": "Invalid job_id or not completed"}, status_code=404)
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
if not os.path.exists(output_file):
|
| 110 |
-
logging.error(f"Download failed: output file for job {job_id} not found.")
|
| 111 |
-
return JSONResponse({"error": "Output file not found."}, status_code=404)
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from fastapi.responses import FileResponse, JSONResponse
|
| 3 |
+
import os, subprocess, logging
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
JOBS_DIR = "/tmp/jobs"
|
| 8 |
os.makedirs(JOBS_DIR, exist_ok=True)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
job_status = {
|
| 13 |
+
"status": "idle",
|
| 14 |
+
"error": None,
|
| 15 |
+
}
|
| 16 |
|
| 17 |
|
| 18 |
def run_ffmpeg(base_file, overlay_file, output_file):
|
|
|
|
| 28 |
return result.returncode == 0
|
| 29 |
|
| 30 |
|
| 31 |
+
@app.post("/generate-video/")
|
| 32 |
+
async def generate_video(video: UploadFile = File(...), overlay: UploadFile = File(...)):
|
| 33 |
+
base_file = os.path.join(JOBS_DIR, "current_base.mp4")
|
| 34 |
+
overlay_file = os.path.join(JOBS_DIR, "current_overlay.mp4")
|
| 35 |
+
output_file = os.path.join(JOBS_DIR, "current_output.mp4")
|
| 36 |
|
| 37 |
+
# Clear old files
|
| 38 |
+
for f in [base_file, overlay_file, output_file]:
|
| 39 |
+
if os.path.exists(f):
|
| 40 |
+
os.remove(f)
|
| 41 |
|
| 42 |
+
with open(base_file, "wb") as f:
|
| 43 |
+
f.write(await video.read())
|
| 44 |
+
with open(overlay_file, "wb") as f:
|
| 45 |
+
f.write(await overlay.read())
|
| 46 |
|
| 47 |
+
job_status["status"] = "processing"
|
| 48 |
+
job_status["error"] = None
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
success = run_ffmpeg(base_file, overlay_file, output_file)
|
| 52 |
if success and os.path.exists(output_file):
|
| 53 |
+
job_status["status"] = "completed"
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
+
job_status["status"] = "failed"
|
| 56 |
+
job_status["error"] = "FFmpeg failed or output missing."
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
+
job_status["status"] = "failed"
|
| 59 |
+
job_status["error"] = str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
return {"status": job_status["status"]}
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
@app.get("/status/")
|
| 65 |
+
async def check_status():
|
| 66 |
+
return JSONResponse(job_status)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
@app.get("/download/")
|
| 70 |
+
async def download_video():
|
| 71 |
+
output_file = os.path.join(JOBS_DIR, "current_output.mp4")
|
| 72 |
+
if job_status["status"] != "completed" or not os.path.exists(output_file):
|
| 73 |
+
return JSONResponse({"error": "No completed video to download."}, status_code=404)
|
| 74 |
+
return FileResponse(output_file, media_type="video/mp4", filename="output.mp4")
|