| import os |
| import uuid |
| import requests |
| import subprocess |
| from fastapi import FastAPI |
| from pydantic import BaseModel |
| from fastapi.responses import FileResponse |
|
|
| FILES_DIR = "/app/files" |
| os.makedirs(FILES_DIR, exist_ok=True) |
|
|
| app = FastAPI() |
|
|
|
|
| class EditRequest(BaseModel): |
| video_url: str |
| overlay_url: str |
| ffmpeg_cmd: str |
|
|
|
|
| def download_file(url, dest_path): |
| try: |
| r = requests.get(url, timeout=20) |
| r.raise_for_status() |
| with open(dest_path, 'wb') as f: |
| f.write(r.content) |
| return True |
| except Exception as e: |
| print(f"Failed to download {url}: {e}") |
| return False |
|
|
|
|
| @app.post("/edit/") |
| async def edit_video(req: EditRequest): |
| video_file = os.path.join(FILES_DIR, f"{uuid.uuid4()}.mp4") |
| overlay_file = os.path.join(FILES_DIR, f"{uuid.uuid4()}.png") |
| output_file = os.path.join(FILES_DIR, f"{uuid.uuid4()}.mp4") |
|
|
| if not download_file(req.video_url, video_file): |
| return {"error": f"Failed to download {req.video_url}"} |
| if not download_file(req.overlay_url, overlay_file): |
| return {"error": f"Failed to download {req.overlay_url}"} |
|
|
| |
| cmd = f"ffmpeg -y -i {video_file} -i {overlay_file} {req.ffmpeg_cmd} {output_file}" |
| print(f"Running: {cmd}") |
| try: |
| subprocess.run(cmd, shell=True, check=True) |
| except subprocess.CalledProcessError as e: |
| return {"error": f"FFmpeg failed: {e}"} |
|
|
| download_url = f"https://hivecorp-video-gen.hf.space/files/{os.path.basename(output_file)}" |
| return {"status": "success", "download_url": download_url} |
|
|
|
|
| @app.get("/files/{filename}") |
| async def get_file(filename: str): |
| file_path = os.path.join(FILES_DIR, filename) |
| if os.path.exists(file_path): |
| return FileResponse(file_path, media_type='video/mp4') |
| return {"error": "File not found"} |
|
|