Update main.py
Browse files55 unmodified lines5657585960616263646566676869707172737410 unmodified lines8586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251 unmodified line1271281291301311321331341351361371 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) 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(1 unmodified linestdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, 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")55 unmodified lines5657585960616263646566676869707172737475767710 unmodified lines8889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631 unmodified line1651661671681691701711721731741751761771781 unmodified line18018118218318418518618718818955 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:# Implementation with zoom effecttemp_dir = os.path.dirname(output_path) + "/temp"os.makedirs(temp_dir, exist_ok=True)# Generate a video clip for each image with zoom effectvideo_parts = []try:for i, img_path in enumerate(image_paths):out_clip = f"{temp_dir}/clip_{i:03d}.mp4"video_parts.append(out_clip)# Calculate zoompan parametersfps = 30frames = duration * fps# Start at zoom=1 and gradually increase over timezoom_expr = f"'1+{zoom_ratio}*t/{duration}'"# Center the zoom effectx_expr = "'iw/2-(iw/zoom/2)'"y_expr = "'ih/2-(ih/zoom/2)'"cmd = ["ffmpeg","-loop", "1","-i", img_path,"-t", str(duration),"-filter_complex", f"zoompan=z={zoom_expr}:x={x_expr}:y={y_expr}:d={frames}:s='iw:ih':fps={fps}","-c:v", "libx264","-pix_fmt", "yuv420p","-y",out_clip]process = await asyncio.create_subprocess_exec(*cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, stderr = await process.communicate()if process.returncode != 0:print(f"FFmpeg error on clip {i}: {stderr.decode()}")raise Exception(f"Failed to create zoomed clip {i}")# Create a concat file for all the video partsasync with aiofiles.open(concat_file, "w") as f:for video in video_parts:await f.write(f"file '{video}'\n")# Combine all clips with audiocmd = ["ffmpeg","-f", "concat","-safe", "0","-i", concat_file,"-i", audio_path,"-c:v", "copy","-c:a", "aac","-shortest","-y",output_path]except Exception as e:# Clean up on errorif os.path.exists(temp_dir):shutil.rmtree(temp_dir)if os.path.exists(concat_file):os.remove(concat_file)raise etry:process = await asyncio.create_subprocess_exec(1 unmodified linestdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if os.path.exists(concat_file):os.remove(concat_file)# Clean up temporary directory if zoom was usedif zoom and os.path.exists(temp_dir):shutil.rmtree(temp_dir)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)if zoom and os.path.exists(temp_dir):shutil.rmtree(temp_dir)return False@app.post("/make_slideshow")
main.py+87-44