Update main.py
Browse files
main.py
CHANGED
|
@@ -72,14 +72,14 @@ async def add_audio_to_image(request: Request):
|
|
| 72 |
image_file = await download_file(image_url, ".jpg")
|
| 73 |
audio_file = await download_file(audio_url, ".mp3")
|
| 74 |
|
| 75 |
-
# Run ffmpeg command
|
| 76 |
-
ffmpeg_cmd = f"ffmpeg -loop 1 -i {image_file} -i {audio_file} -c:v libx264 -tune stillimage -c:a aac -b:a 192k -shortest -pix_fmt yuv420p {output_path}"
|
| 77 |
process = await asyncio.create_subprocess_shell(
|
| 78 |
ffmpeg_cmd,
|
| 79 |
stdout=asyncio.subprocess.PIPE,
|
| 80 |
stderr=asyncio.subprocess.PIPE
|
| 81 |
)
|
| 82 |
-
|
| 83 |
|
| 84 |
if process.returncode != 0:
|
| 85 |
print(f"FFmpeg error: {stderr.decode()}")
|
|
@@ -114,14 +114,14 @@ async def add_audio_to_video(request: Request):
|
|
| 114 |
video_file = await download_file(video_url, ".mp4")
|
| 115 |
audio_file = await download_file(audio_url, ".mp3")
|
| 116 |
|
| 117 |
-
# Run ffmpeg command
|
| 118 |
-
ffmpeg_cmd = f"ffmpeg -i {video_file} -i {audio_file} -c:v copy -c:a aac -shortest {output_path}"
|
| 119 |
process = await asyncio.create_subprocess_shell(
|
| 120 |
ffmpeg_cmd,
|
| 121 |
stdout=asyncio.subprocess.PIPE,
|
| 122 |
stderr=asyncio.subprocess.PIPE
|
| 123 |
)
|
| 124 |
-
|
| 125 |
|
| 126 |
if process.returncode != 0:
|
| 127 |
print(f"FFmpeg error: {stderr.decode()}")
|
|
@@ -171,7 +171,7 @@ async def concatenate_videos(request: Request):
|
|
| 171 |
stdout=asyncio.subprocess.PIPE,
|
| 172 |
stderr=asyncio.subprocess.PIPE
|
| 173 |
)
|
| 174 |
-
|
| 175 |
|
| 176 |
if process.returncode != 0:
|
| 177 |
print(f"FFmpeg error: {stderr.decode()}")
|
|
@@ -216,14 +216,14 @@ async def concatenate_audio(request: Request):
|
|
| 216 |
for file in audio_files:
|
| 217 |
f.write(f"file '{file}'\n")
|
| 218 |
|
| 219 |
-
# Run ffmpeg command
|
| 220 |
-
ffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c
|
| 221 |
process = await asyncio.create_subprocess_shell(
|
| 222 |
ffmpeg_cmd,
|
| 223 |
stdout=asyncio.subprocess.PIPE,
|
| 224 |
stderr=asyncio.subprocess.PIPE
|
| 225 |
)
|
| 226 |
-
|
| 227 |
|
| 228 |
if process.returncode != 0:
|
| 229 |
print(f"FFmpeg error: {stderr.decode()}")
|
|
@@ -409,23 +409,25 @@ async def create_slideshow(request: Request):
|
|
| 409 |
stdout=asyncio.subprocess.PIPE,
|
| 410 |
stderr=asyncio.subprocess.PIPE
|
| 411 |
)
|
| 412 |
-
|
| 413 |
|
| 414 |
if process.returncode != 0:
|
| 415 |
print(f"FFmpeg slideshow creation error: {stderr.decode()}")
|
| 416 |
raise HTTPException(status_code=500, detail=f"FFmpeg slideshow creation failed: {stderr.decode()}")
|
| 417 |
|
| 418 |
# Add audio to the slideshow
|
|
|
|
| 419 |
final_cmd = (
|
| 420 |
-
f'ffmpeg -
|
| 421 |
-
f'-
|
|
|
|
| 422 |
)
|
| 423 |
process = await asyncio.create_subprocess_shell(
|
| 424 |
final_cmd,
|
| 425 |
stdout=asyncio.subprocess.PIPE,
|
| 426 |
stderr=asyncio.subprocess.PIPE
|
| 427 |
)
|
| 428 |
-
|
| 429 |
|
| 430 |
if process.returncode != 0:
|
| 431 |
print(f"FFmpeg audio addition error: {stderr.decode()}")
|
|
|
|
| 72 |
image_file = await download_file(image_url, ".jpg")
|
| 73 |
audio_file = await download_file(audio_url, ".mp3")
|
| 74 |
|
| 75 |
+
# Run ffmpeg command with improved audio detection parameters
|
| 76 |
+
ffmpeg_cmd = f"ffmpeg -loop 1 -i {image_file} -analyzeduration 10000000 -probesize 10000000 -i {audio_file} -c:v libx264 -tune stillimage -c:a aac -b:a 192k -strict experimental -shortest -pix_fmt yuv420p {output_path}"
|
| 77 |
process = await asyncio.create_subprocess_shell(
|
| 78 |
ffmpeg_cmd,
|
| 79 |
stdout=asyncio.subprocess.PIPE,
|
| 80 |
stderr=asyncio.subprocess.PIPE
|
| 81 |
)
|
| 82 |
+
_, stderr = await process.communicate()
|
| 83 |
|
| 84 |
if process.returncode != 0:
|
| 85 |
print(f"FFmpeg error: {stderr.decode()}")
|
|
|
|
| 114 |
video_file = await download_file(video_url, ".mp4")
|
| 115 |
audio_file = await download_file(audio_url, ".mp3")
|
| 116 |
|
| 117 |
+
# Run ffmpeg command with improved audio detection parameters
|
| 118 |
+
ffmpeg_cmd = f"ffmpeg -analyzeduration 10000000 -probesize 10000000 -i {video_file} -analyzeduration 10000000 -probesize 10000000 -i {audio_file} -c:v copy -c:a aac -strict experimental -shortest {output_path}"
|
| 119 |
process = await asyncio.create_subprocess_shell(
|
| 120 |
ffmpeg_cmd,
|
| 121 |
stdout=asyncio.subprocess.PIPE,
|
| 122 |
stderr=asyncio.subprocess.PIPE
|
| 123 |
)
|
| 124 |
+
_, stderr = await process.communicate()
|
| 125 |
|
| 126 |
if process.returncode != 0:
|
| 127 |
print(f"FFmpeg error: {stderr.decode()}")
|
|
|
|
| 171 |
stdout=asyncio.subprocess.PIPE,
|
| 172 |
stderr=asyncio.subprocess.PIPE
|
| 173 |
)
|
| 174 |
+
_, stderr = await process.communicate()
|
| 175 |
|
| 176 |
if process.returncode != 0:
|
| 177 |
print(f"FFmpeg error: {stderr.decode()}")
|
|
|
|
| 216 |
for file in audio_files:
|
| 217 |
f.write(f"file '{file}'\n")
|
| 218 |
|
| 219 |
+
# Run ffmpeg command with improved audio parameters
|
| 220 |
+
ffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c:a aac -b:a 192k -strict experimental {output_path}"
|
| 221 |
process = await asyncio.create_subprocess_shell(
|
| 222 |
ffmpeg_cmd,
|
| 223 |
stdout=asyncio.subprocess.PIPE,
|
| 224 |
stderr=asyncio.subprocess.PIPE
|
| 225 |
)
|
| 226 |
+
_, stderr = await process.communicate()
|
| 227 |
|
| 228 |
if process.returncode != 0:
|
| 229 |
print(f"FFmpeg error: {stderr.decode()}")
|
|
|
|
| 409 |
stdout=asyncio.subprocess.PIPE,
|
| 410 |
stderr=asyncio.subprocess.PIPE
|
| 411 |
)
|
| 412 |
+
_, stderr = await process.communicate()
|
| 413 |
|
| 414 |
if process.returncode != 0:
|
| 415 |
print(f"FFmpeg slideshow creation error: {stderr.decode()}")
|
| 416 |
raise HTTPException(status_code=500, detail=f"FFmpeg slideshow creation failed: {stderr.decode()}")
|
| 417 |
|
| 418 |
# Add audio to the slideshow
|
| 419 |
+
# Increase analyzeduration and probesize for better audio detection
|
| 420 |
final_cmd = (
|
| 421 |
+
f'ffmpeg -analyzeduration 10000000 -probesize 10000000 -i {intermediate_video} '
|
| 422 |
+
f'-analyzeduration 10000000 -probesize 10000000 -i {audio_file} '
|
| 423 |
+
f'-c:v copy -c:a aac -strict experimental -shortest {output_path}'
|
| 424 |
)
|
| 425 |
process = await asyncio.create_subprocess_shell(
|
| 426 |
final_cmd,
|
| 427 |
stdout=asyncio.subprocess.PIPE,
|
| 428 |
stderr=asyncio.subprocess.PIPE
|
| 429 |
)
|
| 430 |
+
_, stderr = await process.communicate()
|
| 431 |
|
| 432 |
if process.returncode != 0:
|
| 433 |
print(f"FFmpeg audio addition error: {stderr.decode()}")
|