File size: 7,620 Bytes
4401c86
730c60f
4401c86
3ead8be
4401c86
5b457c6
730c60f
3ead8be
4401c86
3ead8be
a1d44af
5b457c6
730c60f
4401c86
730c60f
 
4401c86
 
 
730c60f
4401c86
 
 
 
 
3ead8be
730c60f
4401c86
 
 
 
 
730c60f
5b457c6
 
730c60f
4401c86
 
 
 
 
 
5b457c6
 
 
 
 
 
 
 
 
 
4401c86
730c60f
4401c86
 
5f98112
36489c3
6f61fa6
3ead8be
6f61fa6
 
3ead8be
 
 
 
 
 
 
4401c86
6f61fa6
3ead8be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f61fa6
 
 
 
 
9b13a7b
6f61fa6
 
 
 
 
 
 
 
8941369
6f61fa6
8941369
6f61fa6
9b13a7b
6f61fa6
 
 
 
 
 
9b13a7b
 
6f61fa6
 
 
36489c3
 
6f61fa6
36489c3
3ead8be
730c60f
 
5b457c6
 
 
 
 
8941369
5b457c6
6f61fa6
 
 
5b457c6
 
 
3ead8be
 
 
 
6f61fa6
 
4401c86
 
 
 
 
 
 
 
 
 
 
 
 
730c60f
4401c86
 
5b457c6
 
4401c86
 
5b457c6
 
a1d44af
4401c86
 
5b457c6
 
 
 
 
 
 
 
 
 
4401c86
a1d44af
4401c86
 
a1d44af
4401c86
2420d5c
4401c86
a1d44af
4401c86
9bb1f0b
4401c86
 
 
275d0aa
4401c86
 
 
 
275d0aa
ba83473
 
82d4647
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, HttpUrl
from typing import List
import os
import asyncio
import uuid
import aiohttp
import re
from urllib.parse import urlparse
import shutil
import aiofiles

# Create FastAPI app
app = FastAPI()

# Create and mount staticfiles directory
os.makedirs("staticfiles", exist_ok=True)
app.mount("/static", StaticFiles(directory="staticfiles"), name="static")

# Define input model for the request
class SlideshowRequest(BaseModel):
    image_urls: List[HttpUrl]
    audio_url: HttpUrl
    duration: int
    zoom: bool = False

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

async def download_file(url, local_path):
    """Download a file from URL to local path asynchronously"""
    try:
        # Handle Google Drive URLs
        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}"
        
        async with aiohttp.ClientSession() as session:
            async with session.get(str(url)) as response:
                response.raise_for_status()
                
                async with aiofiles.open(local_path, 'wb') as f:
                    while True:
                        chunk = await response.content.read(8192)
                        if not chunk:
                            break
                        await f.write(chunk)
        return True
    except Exception as e:
        print(f"Error downloading {url}: {str(e)}")
        return False

async def create_slideshow(image_paths, audio_path, output_path, duration, zoom=False, zoom_ratio=0.04):
    """Generate slideshow from images and audio using ffmpeg asynchronously, with optional zoom effect"""
    if not zoom:
        # Create temporary file list for ffmpeg concat
        concat_file = "temp_concat.txt"
        async with aiofiles.open(concat_file, "w") as f:
            for img in image_paths:
                await f.write(f"file '{img}'\n")
                await f.write(f"duration {duration}\n")
            # Add the last image again without duration (required by ffmpeg)
            if image_paths:
                await f.write(f"file '{image_paths[-1]}'\n")
        
        # Run ffmpeg command to create slideshow with audio (no zoom)
        total_duration = len(image_paths) * duration
        cmd = [
            "ffmpeg",
            "-f", "concat",
            "-safe", "0",
            "-i", concat_file,
            "-i", audio_path,
            "-c:v", "libx264",
            "-pix_fmt", "yuv420p",
            "-c:a", "aac",
            "-shortest",
            "-y",
            "-t", str(total_duration),
            output_path
        ]
    else:
        # Generate slideshow with zoom effect using ffmpeg zoompan filter.
        # For each image, loop it for the specified duration and apply zoompan.
        fps = 25  # frames per second
        cmd = ["ffmpeg"]
        # Add each image as an input that loops for 'duration' seconds.
        for img in image_paths:
            cmd.extend(["-loop", "1", "-t", str(duration), "-i", img])
        # Append the audio input. Its index will be len(image_paths)
        cmd.extend(["-i", audio_path])
        
        # Build filter_complex for zoompan on each image.
        filter_complex = ""
        for i in range(len(image_paths)):
            # Each input's video stream is processed with zoompan.
            # The zoom increases by zoom_ratio per frame (starting at 1.0) and centers the image.
            filter_complex += (
                f"[{i}:v]zoompan=z='if(eq(on,0),1,zoom+{zoom_ratio})':x='iw/2-(iw/zoom)/2':y='ih/2-(ih/zoom)/2':"
                f"d={duration*fps}:s=hd720, setpts=PTS-STARTPTS[v{i}];"
            )
        # Concatenate all processed video segments.
        inputs = "".join(f"[v{i}]" for i in range(len(image_paths)))
        filter_complex += f"{inputs}concat=n={len(image_paths)}:v=1:a=0,format=yuv420p[v]"
        
        # Map the concatenated video and the audio stream (audio input is at index len(image_paths))
        cmd = cmd + [
            "-filter_complex", filter_complex,
            "-map", "[v]",
            "-map", f"{len(image_paths)}:a",
            "-c:v", "libx264",
            "-pix_fmt", "yuv420p",
            "-c:a", "aac",
            "-shortest",
            "-y",
            output_path
        ]
    
    try:
        process = await asyncio.create_subprocess_exec(
            *cmd, 
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )
        _, stderr = await process.communicate()
        
        # Remove temporary concat file if it exists
        if not zoom and os.path.exists("temp_concat.txt"):
            os.remove("temp_concat.txt")
            
        if process.returncode != 0:
            print(f"FFmpeg error: {stderr.decode()}")
            return False
        return True
    except Exception as e:
        print(f"FFmpeg error: {str(e)}")
        if not zoom and os.path.exists("temp_concat.txt"):
            os.remove("temp_concat.txt")
        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.
    """
    # Create unique directory for this request
    request_id = str(uuid.uuid4())
    request_dir = os.path.join("staticfiles", request_id)
    os.makedirs(request_dir, exist_ok=True)
    
    try:
        # Download images
        image_paths = []
        download_tasks = []
        
        for i, url in enumerate(request.image_urls):
            image_path = os.path.join(request_dir, f"image_{i:03d}.png")
            image_paths.append(image_path)
            download_tasks.append(download_file(url, image_path))
        
        # Download audio
        audio_path = os.path.join(request_dir, "audio.mp3")
        download_tasks.append(download_file(request.audio_url, audio_path))
        
        # Wait for all downloads to complete
        results = await asyncio.gather(*download_tasks)
        
        # Check if all downloads were successful
        if not all(results[:-1]):  # All image downloads
            raise HTTPException(status_code=400, detail="Failed to download one or more images")
        
        if not results[-1]:  # Audio download
            raise HTTPException(status_code=400, detail=f"Failed to download audio: {request.audio_url}")
        
        # Output video path
        output_path = os.path.join(request_dir, "slideshow.mp4")
        
        # Generate slideshow
        if not await create_slideshow(image_paths, audio_path, output_path, request.duration, request.zoom):
            raise HTTPException(status_code=500, detail="Failed to create slideshow")
        
        # Return URL to the video
        base_url = "https://saq1b-api.hf.space/static"
        video_url = f"{base_url}/{request_id}/slideshow.mp4"
        return {"url": video_url}
    
    except Exception as e:
        # Clean up on error
        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=7860)