Update main.py
Browse files55 unmodified lines5657585960616263646566676869707172737475767710 unmodified lines8889909192939495969798991001011021031041051061071081091101111121131141151161171185 unmodified lines1241251261271281291301311 unmodified line13313413513613713813914055 unmodified linesreturn Falseasync 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 concatconcat_file = "temp_concat.txt"if not zoom:# Original implementation without zoom effectasync 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 audiototal_duration = len(image_paths) * durationcmd = ["ffmpeg",10 unmodified linesoutput_path]else:# Build separate inputs looping each image with a fixed display time and apply zoom effectcmd_inputs = []for img in image_paths:cmd_inputs += ["-loop", "1", "-t", str(duration), "-i", img]# Add the audio input at the endcmd_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}]")# Concatenate all processed clipsconcat_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 + ["-filter_complex", filter_complex,"-map", "[v]","-map", f"{num_images}:a","-c:a", "aac","-shortest",output_path]5 unmodified lines)stdout, stderr = await process.communicate()if os.path.exists(concat_file):os.remove(concat_file)if process.returncode != 0:print(f"FFmpeg error: {stderr.decode()}")1 unmodified linereturn Trueexcept Exception as e:print(f"FFmpeg error: {str(e)}")if os.path.exists(concat_file):os.remove(concat_file)return False@app.post("/make_slideshow")55 unmodified lines5657585960616263646566676869707172737410 unmodified lines8586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221235 unmodified lines1291301311321331341351361371 unmodified line13914014114214314414514655 unmodified linesreturn Falseasync 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 concatconcat_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) * durationcmd = ["ffmpeg",10 unmodified linesoutput_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 secondcmd = ["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).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 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]5 unmodified lines)stdout, stderr = await process.communicate()# Remove temporary concat file if it existsif 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 linereturn Trueexcept 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")
main.py+36-30