Update main.py
Browse files
main.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
from pydantic import BaseModel, HttpUrl
|
| 4 |
-
from typing import List
|
| 5 |
import os
|
| 6 |
import asyncio
|
| 7 |
import uuid
|
|
@@ -23,6 +23,7 @@ class SlideshowRequest(BaseModel):
|
|
| 23 |
image_urls: List[HttpUrl]
|
| 24 |
audio_url: HttpUrl
|
| 25 |
duration: int
|
|
|
|
| 26 |
|
| 27 |
def extract_google_drive_id(url):
|
| 28 |
"""Extract file ID from a Google Drive URL"""
|
|
@@ -54,7 +55,7 @@ async def download_file(url, local_path):
|
|
| 54 |
print(f"Error downloading {url}: {str(e)}")
|
| 55 |
return False
|
| 56 |
|
| 57 |
-
async def create_slideshow(image_paths, audio_path, output_path, duration):
|
| 58 |
"""Generate slideshow from images and audio using ffmpeg asynchronously"""
|
| 59 |
# Create temporary file list for ffmpeg concat
|
| 60 |
concat_file = "temp_concat.txt"
|
|
@@ -70,12 +71,32 @@ async def create_slideshow(image_paths, audio_path, output_path, duration):
|
|
| 70 |
|
| 71 |
# Run ffmpeg command to create slideshow with audio
|
| 72 |
total_duration = len(image_paths) * duration
|
|
|
|
| 73 |
cmd = [
|
| 74 |
"ffmpeg",
|
| 75 |
"-f", "concat",
|
| 76 |
"-safe", "0",
|
| 77 |
"-i", concat_file,
|
| 78 |
"-i", audio_path,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
"-c:v", "libx264",
|
| 80 |
"-pix_fmt", "yuv420p",
|
| 81 |
"-c:a", "aac",
|
|
@@ -83,7 +104,7 @@ async def create_slideshow(image_paths, audio_path, output_path, duration):
|
|
| 83 |
"-y",
|
| 84 |
"-t", str(total_duration),
|
| 85 |
output_path
|
| 86 |
-
]
|
| 87 |
|
| 88 |
try:
|
| 89 |
process = await asyncio.create_subprocess_exec(
|
|
@@ -145,7 +166,7 @@ async def make_slideshow(request: SlideshowRequest):
|
|
| 145 |
output_path = os.path.join(request_dir, "slideshow.mp4")
|
| 146 |
|
| 147 |
# Generate slideshow
|
| 148 |
-
if not await create_slideshow(image_paths, audio_path, output_path, request.duration):
|
| 149 |
raise HTTPException(status_code=500, detail="Failed to create slideshow")
|
| 150 |
|
| 151 |
# Return URL to the video
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
from pydantic import BaseModel, HttpUrl
|
| 4 |
+
from typing import List, Optional
|
| 5 |
import os
|
| 6 |
import asyncio
|
| 7 |
import uuid
|
|
|
|
| 23 |
image_urls: List[HttpUrl]
|
| 24 |
audio_url: HttpUrl
|
| 25 |
duration: int
|
| 26 |
+
zoom: Optional[bool] = False # Optional zoom parameter with default value of False
|
| 27 |
|
| 28 |
def extract_google_drive_id(url):
|
| 29 |
"""Extract file ID from a Google Drive URL"""
|
|
|
|
| 55 |
print(f"Error downloading {url}: {str(e)}")
|
| 56 |
return False
|
| 57 |
|
| 58 |
+
async def create_slideshow(image_paths, audio_path, output_path, duration, zoom=False):
|
| 59 |
"""Generate slideshow from images and audio using ffmpeg asynchronously"""
|
| 60 |
# Create temporary file list for ffmpeg concat
|
| 61 |
concat_file = "temp_concat.txt"
|
|
|
|
| 71 |
|
| 72 |
# Run ffmpeg command to create slideshow with audio
|
| 73 |
total_duration = len(image_paths) * duration
|
| 74 |
+
|
| 75 |
cmd = [
|
| 76 |
"ffmpeg",
|
| 77 |
"-f", "concat",
|
| 78 |
"-safe", "0",
|
| 79 |
"-i", concat_file,
|
| 80 |
"-i", audio_path,
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
# Add zoom effect if requested
|
| 84 |
+
if zoom:
|
| 85 |
+
for i, img_path in enumerate(image_paths):
|
| 86 |
+
cmd.extend([
|
| 87 |
+
"-filter_complex",
|
| 88 |
+
f"[0:v]select=eq(n\\,{i}),setpts=PTS-STARTPTS,scale=1920:1080,zoompan=z='min(1.1,1+(in/out/8))':"
|
| 89 |
+
f"d={duration*25}:s=1920x1080:fps=25[v{i}]"
|
| 90 |
+
])
|
| 91 |
+
|
| 92 |
+
# Create video filter chain
|
| 93 |
+
filter_complex = ""
|
| 94 |
+
for i in range(len(image_paths)):
|
| 95 |
+
filter_complex += f"[v{i}]"
|
| 96 |
+
filter_complex += f"concat=n={len(image_paths)}:v=1:a=0[outv]"
|
| 97 |
+
cmd.extend(["-filter_complex", filter_complex, "-map", "[outv]", "-map", "1:a"])
|
| 98 |
+
|
| 99 |
+
cmd.extend([
|
| 100 |
"-c:v", "libx264",
|
| 101 |
"-pix_fmt", "yuv420p",
|
| 102 |
"-c:a", "aac",
|
|
|
|
| 104 |
"-y",
|
| 105 |
"-t", str(total_duration),
|
| 106 |
output_path
|
| 107 |
+
])
|
| 108 |
|
| 109 |
try:
|
| 110 |
process = await asyncio.create_subprocess_exec(
|
|
|
|
| 166 |
output_path = os.path.join(request_dir, "slideshow.mp4")
|
| 167 |
|
| 168 |
# Generate slideshow
|
| 169 |
+
if not await create_slideshow(image_paths, audio_path, output_path, request.duration, request.zoom):
|
| 170 |
raise HTTPException(status_code=500, detail="Failed to create slideshow")
|
| 171 |
|
| 172 |
# Return URL to the video
|