Update main.py
Browse files87 unmodified lines8889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631644 unmodified lines1691701711721731741751761771783 unmodified lines18218318418518618718818987 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(*cmd,4 unmodified linesif 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()}")3 unmodified linesprint(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")87 unmodified lines8889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471484 unmodified lines1531541551561571583 unmodified lines16216316416516616787 unmodified linesoutput_path]else:# ffmpeg -loop 1 -framerate 60 -i image.jpg -vf "scale=8000:-1,zoompan=z='zoom+0.001':x=iw/2-(iw/zoom/2):y=ih/2-(ih/zoom/2):d=5*60:s=1920x1280:fps=60" -t 5 -c:v libx264 -pix_fmt yuv420p output.mp4# For zoom effect, process each image separately and then concatenatetemp_videos = []zoom_filter = f"zoompan=z='min(zoom+{zoom_ratio},1.5)':x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':d={duration*60}:s=1920x1080:fps=24"# First create individual video clips with zoom effect for each imagefor i, img_path in enumerate(image_paths):temp_video = os.path.join(os.path.dirname(output_path), f"temp_{i:03d}.mp4")temp_videos.append(temp_video)img_cmd = ["ffmpeg","-loop", "1","-i", img_path,"-vf", zoom_filter,"-t", str(duration),"-c:v", "libx264","-pix_fmt", "yuv420p","-y",temp_video]img_process = await asyncio.create_subprocess_exec(*img_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)img_stdout, img_stderr = await img_process.communicate()if img_process.returncode != 0:print(f"FFmpeg error processing image {i}: {img_stderr.decode()}")# Clean up already created temp videosfor vid in temp_videos:if os.path.exists(vid):os.remove(vid)return False# Create a concat file for the temporary videosasync with aiofiles.open(concat_file, "w") as f:for video in temp_videos:await f.write(f"file '{video}'\n")# Combine videos with audiocmd = ["ffmpeg","-f", "concat","-safe", "0","-i", concat_file,"-i", audio_path,"-c:v", "libx264","-c:a", "aac","-shortest","-y",output_path]try:process = await asyncio.create_subprocess_exec(*cmd,4 unmodified linesif os.path.exists(concat_file):os.remove(concat_file)if process.returncode != 0:print(f"FFmpeg error: {stderr.decode()}")3 unmodified linesprint(f"FFmpeg error: {str(e)}")if os.path.exists(concat_file):os.remove(concat_file)return False@app.post("/make_slideshow")
main.py+51-73