| from fastapi import FastAPI, HTTPException |
| from fastapi.staticfiles import StaticFiles |
| from pydantic import BaseModel, HttpUrl |
| from typing import List |
| import os |
| import subprocess |
| import uuid |
| import requests |
| import re |
| from urllib.parse import urlparse |
| import shutil |
|
|
| |
| app = FastAPI() |
|
|
| |
| os.makedirs("staticfiles", exist_ok=True) |
| app.mount("/static", StaticFiles(directory="staticfiles"), name="static") |
|
|
| |
| class SlideshowRequest(BaseModel): |
| image_urls: List[HttpUrl] |
| audio_url: HttpUrl |
| duration: int |
|
|
| def extract_google_drive_id(url): |
| """Extract file ID from a Google Drive URL""" |
| pattern = r'(?:/file/d/|id=|/open\?id=)([^/&]+)' |
| match = re.search(pattern, str(url)) |
| return match.group(1) if match else None |
|
|
| def download_file(url, local_path): |
| """Download a file from URL to local path""" |
| try: |
| |
| if "drive.google.com" in str(url): |
| file_id = extract_google_drive_id(url) |
| if file_id: |
| url = f"https://drive.google.com/uc?export=download&id={file_id}" |
| |
| response = requests.get(str(url), stream=True) |
| response.raise_for_status() |
| |
| with open(local_path, 'wb') as f: |
| for chunk in response.iter_content(chunk_size=8192): |
| f.write(chunk) |
| return True |
| except Exception as e: |
| print(f"Error downloading {url}: {str(e)}") |
| return False |
|
|
| def create_slideshow(image_paths, audio_path, output_path, duration): |
| """Generate slideshow from images and audio using ffmpeg""" |
| |
| concat_file = "temp_concat.txt" |
| |
| with open(concat_file, "w") as f: |
| for img in image_paths: |
| f.write(f"file '{img}'\n") |
| f.write(f"duration {duration}\n") |
| |
| |
| if image_paths: |
| f.write(f"file '{image_paths[-1]}'\n") |
| |
| |
| cmd = [ |
| "ffmpeg", |
| "-f", "concat", |
| "-safe", "0", |
| "-i", concat_file, |
| "-i", audio_path, |
| "-c:v", "libx264", |
| "-pix_fmt", "yuv420p", |
| "-c:a", "aac", |
| "-shortest", |
| "-y", |
| output_path |
| ] |
| |
| try: |
| subprocess.run(cmd, check=True, capture_output=True) |
| os.remove(concat_file) |
| return True |
| except subprocess.CalledProcessError as e: |
| print(f"FFmpeg error: {e.stderr.decode()}") |
| if os.path.exists(concat_file): |
| os.remove(concat_file) |
| return False |
|
|
| @app.post("/make_slideshow") |
| async def make_slideshow(request: SlideshowRequest): |
| """ |
| Create a slideshow from images and audio with specified duration per image. |
| Returns the URL of the generated video. |
| """ |
| |
| request_id = str(uuid.uuid4()) |
| request_dir = os.path.join("staticfiles", request_id) |
| os.makedirs(request_dir, exist_ok=True) |
| |
| try: |
| |
| image_paths = [] |
| for i, url in enumerate(request.image_urls): |
| image_path = os.path.join(request_dir, f"image_{i:03d}.png") |
| if download_file(url, image_path): |
| image_paths.append(image_path) |
| else: |
| raise HTTPException(status_code=400, detail=f"Failed to download image: {url}") |
| |
| |
| audio_path = os.path.join(request_dir, "audio.mp3") |
| if not download_file(request.audio_url, audio_path): |
| raise HTTPException(status_code=400, detail=f"Failed to download audio: {request.audio_url}") |
| |
| |
| output_path = os.path.join(request_dir, "slideshow.mp4") |
| |
| |
| if not create_slideshow(image_paths, audio_path, output_path, request.duration): |
| raise HTTPException(status_code=500, detail="Failed to create slideshow") |
| |
| |
| base_url = "/static" |
| video_url = f"{base_url}/{request_id}/slideshow.mp4" |
| return {"url": video_url} |
| |
| except Exception as e: |
| |
| if os.path.exists(request_dir): |
| shutil.rmtree(request_dir) |
| raise HTTPException(status_code=500, detail=f"Error: {str(e)}") |
|
|
| if __name__ == "__main__": |
| import uvicorn |
| uvicorn.run(app, host="0.0.0.0", port=8000) |