Update main.py
Browse files55 unmodified lines565758596061625960646162636465666967686970747172737410 unmodified lines85868791928889909192939495969798991001011021039495969798991001011021031041051061051061071081091101071081091101111121131141131151161171181191201211221235 unmodified lines1291301311271281321331341351361371 unmodified line13914014113613714214314414514655 unmodified lines 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""" # Create temporary file list for ffmpeg concat concat_file = "temp_concat.txt" """Generate slideshow from images and audio using ffmpeg asynchronously, with optional zoom effect""" if not zoom: # Original implementation without zoom effect # 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 # Run ffmpeg command to create slideshow with audio (no zoom) total_duration = len(image_paths) * duration cmd = [ "ffmpeg",10 unmodified lines output_path ] else: # Build separate inputs looping each image with a fixed display time and apply zoom effect cmd_inputs = [] # 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_inputs += ["-loop", "1", "-t", str(duration), "-i", img] # Add the audio input at the end cmd_inputs += ["-i", audio_path]
# Create filter_complex applying zoompan on each image (assuming 25 fps) num_images = len(image_paths) filter_parts = [] for i in range(num_images): filter_parts.append( f"[{i}:v]zoompan=z='min(zoom+{zoom_ratio},1.5)':d={duration * 25}:s=hd720,setsar=1[v{i}]" 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). filter_complex += ( f"[{i}:v]zoompan=z='if(eq(on,0),1,zoom+{zoom_ratio})':" f"d={duration*fps}:s=hd720, setpts=PTS-STARTPTS[v{i}];" ) # Concatenate all processed clips concat_inputs = "".join(f"[v{i}]" for i in range(num_images)) filter_complex = "; ".join(filter_parts) + f"; {concat_inputs}concat=n={num_images}:v=1:a=0,format=yuv420p[v]"
# Build the ffmpeg command with proper mapping for video and audio (audio is after the images) cmd = ["ffmpeg", "-y"] + cmd_inputs + [ # 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"{num_images}:a", "-map", f"{len(image_paths)}:a", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest", "-y", output_path ] 5 unmodified lines ) stdout, stderr = await process.communicate() if os.path.exists(concat_file): os.remove(concat_file) # 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()}")1 unmodified line return True except Exception as e: print(f"FFmpeg error: {str(e)}") if os.path.exists(concat_file): os.remove(concat_file) if not zoom and os.path.exists("temp_concat.txt"): os.remove("temp_concat.txt") return False
@app.post("/make_slideshow")main.py+36-30