Update main.py
Browse files
main.py
CHANGED
|
@@ -55,7 +55,7 @@ async def download_file(url, local_path):
|
|
| 55 |
print(f"Error downloading {url}: {str(e)}")
|
| 56 |
return False
|
| 57 |
|
| 58 |
-
async def create_slideshow(image_paths, audio_path, output_path, duration, zoom=False):
|
| 59 |
"""Generate slideshow from images and audio using ffmpeg asynchronously"""
|
| 60 |
# Create temporary file list for ffmpeg concat
|
| 61 |
concat_file = "temp_concat.txt"
|
|
@@ -88,31 +88,34 @@ async def create_slideshow(image_paths, audio_path, output_path, duration, zoom=
|
|
| 88 |
output_path
|
| 89 |
]
|
| 90 |
else:
|
| 91 |
-
# With zoom effect
|
| 92 |
total_duration = len(image_paths) * duration
|
| 93 |
filter_complex = ""
|
| 94 |
inputs = []
|
| 95 |
|
| 96 |
-
# Add each image with zoompan effect
|
| 97 |
for i, img in enumerate(image_paths):
|
| 98 |
inputs.extend(["-loop", "1", "-t", str(duration), "-i", img])
|
| 99 |
-
# Apply zoom effect with smoother formula - slower zoom factor and better stabilization
|
| 100 |
-
# Adjust zoom rate (0.02 instead of 0.04) for smoother effect
|
| 101 |
-
fps = 25
|
| 102 |
-
zoom_factor = 0.02
|
| 103 |
-
zoompan_params = f"z='if(lte(zoom,1.0),1.0,zoom-{zoom_factor}/{fps})':d={duration*fps}:s=1920x1080:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)'"
|
| 104 |
-
filter_complex += f"[{i}:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1,zoompan={zoompan_params}[v{i}];"
|
| 105 |
-
|
| 106 |
-
# Concatenate all video segments with crossfade transitions
|
| 107 |
-
if len(image_paths) > 1:
|
| 108 |
-
for i in range(len(image_paths) - 1):
|
| 109 |
-
filter_complex += f"[v{i}][v{i+1}]xfade=duration=0.5:offset={duration-0.5}[v{i+1}_xf];"
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
#
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
cmd = [
|
| 118 |
"ffmpeg",
|
|
@@ -122,12 +125,12 @@ async def create_slideshow(image_paths, audio_path, output_path, duration, zoom=
|
|
| 122 |
"-map", "[outv]",
|
| 123 |
"-map", f"{len(image_paths)}:a",
|
| 124 |
"-c:v", "libx264",
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
]
|
| 132 |
|
| 133 |
try:
|
|
|
|
| 55 |
print(f"Error downloading {url}: {str(e)}")
|
| 56 |
return False
|
| 57 |
|
| 58 |
+
async def create_slideshow(image_paths, audio_path, output_path, duration, zoom=False, zoom_ratio=0.04):
|
| 59 |
"""Generate slideshow from images and audio using ffmpeg asynchronously"""
|
| 60 |
# Create temporary file list for ffmpeg concat
|
| 61 |
concat_file = "temp_concat.txt"
|
|
|
|
| 88 |
output_path
|
| 89 |
]
|
| 90 |
else:
|
| 91 |
+
# With improved zoom effect inspired by the zoom_in_effect function
|
| 92 |
total_duration = len(image_paths) * duration
|
| 93 |
filter_complex = ""
|
| 94 |
inputs = []
|
| 95 |
|
| 96 |
+
# Add each image with enhanced zoompan effect
|
| 97 |
for i, img in enumerate(image_paths):
|
| 98 |
inputs.extend(["-loop", "1", "-t", str(duration), "-i", img])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# Calculate frames based on duration (assuming 25fps)
|
| 101 |
+
frames = duration * 25
|
| 102 |
+
|
| 103 |
+
# Create zoompan filter that mimics the provided zoom_in_effect function
|
| 104 |
+
# z: zoom factor increases over time based on zoom_ratio
|
| 105 |
+
# x/y: center the zoom effect by adjusting the position based on zoom
|
| 106 |
+
filter_complex += (
|
| 107 |
+
f"[{i}:v]scale=1920:1080:force_original_aspect_ratio=decrease,setsar=1,"
|
| 108 |
+
f"pad=1920:1080:(ow-iw)/2:(oh-ih)/2,"
|
| 109 |
+
f"zoompan=z='1+(on/{frames})*{zoom_ratio}*{frames}':"
|
| 110 |
+
f"x='iw/2-(iw/zoom/2)':"
|
| 111 |
+
f"y='ih/2-(ih/zoom/2)':"
|
| 112 |
+
f"d={frames}:s=1920x1080[v{i}];"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# Concatenate all video segments
|
| 116 |
+
for i in range(len(image_paths)):
|
| 117 |
+
filter_complex += f"[v{i}]"
|
| 118 |
+
filter_complex += f"concat=n={len(image_paths)}:v=1:a=0[outv]"
|
| 119 |
|
| 120 |
cmd = [
|
| 121 |
"ffmpeg",
|
|
|
|
| 125 |
"-map", "[outv]",
|
| 126 |
"-map", f"{len(image_paths)}:a",
|
| 127 |
"-c:v", "libx264",
|
| 128 |
+
"-pix_fmt", "yuv420p",
|
| 129 |
+
"-c:a", "aac",
|
| 130 |
+
"-shortest",
|
| 131 |
+
"-y",
|
| 132 |
+
"-t", str(total_duration),
|
| 133 |
+
output_path
|
| 134 |
]
|
| 135 |
|
| 136 |
try:
|