Update main.py
Browse files
main.py
CHANGED
|
@@ -358,8 +358,8 @@ async def make_video(request: Request):
|
|
| 358 |
print(traceback.format_exc())
|
| 359 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 360 |
|
| 361 |
-
@app.post("/
|
| 362 |
-
async def
|
| 363 |
try:
|
| 364 |
# Generate a unique filename for the output
|
| 365 |
output_filename = f"{uuid.uuid4()}.mp4"
|
|
@@ -368,31 +368,42 @@ async def concatenate_videos_with_music(request: Request):
|
|
| 368 |
os.makedirs(temp_dir, exist_ok=True)
|
| 369 |
|
| 370 |
data = await request.json()
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
|
| 375 |
-
if not
|
| 376 |
-
raise HTTPException(status_code=400, detail="Invalid
|
| 377 |
|
| 378 |
-
if not
|
| 379 |
-
raise HTTPException(status_code=400, detail="Missing
|
| 380 |
|
| 381 |
-
# Download the
|
| 382 |
-
|
| 383 |
-
for i, url in enumerate(
|
| 384 |
-
|
| 385 |
-
|
| 386 |
|
| 387 |
-
#
|
| 388 |
-
|
| 389 |
-
with open(concat_list_path, "w") as f:
|
| 390 |
-
for file in video_files:
|
| 391 |
-
f.write(f"file '{file}'\n")
|
| 392 |
|
| 393 |
-
#
|
| 394 |
-
|
| 395 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
process = await asyncio.create_subprocess_shell(
|
| 397 |
concat_cmd,
|
| 398 |
stdout=asyncio.subprocess.PIPE,
|
|
@@ -401,16 +412,13 @@ async def concatenate_videos_with_music(request: Request):
|
|
| 401 |
stdout, stderr = await process.communicate()
|
| 402 |
|
| 403 |
if process.returncode != 0:
|
| 404 |
-
print(f"FFmpeg
|
| 405 |
-
raise HTTPException(status_code=500, detail=f"FFmpeg
|
| 406 |
-
|
| 407 |
-
# Download music file
|
| 408 |
-
music_file = await download_file(music_url, ".mp3")
|
| 409 |
|
| 410 |
-
# Add
|
| 411 |
final_cmd = (
|
| 412 |
-
f'ffmpeg -i {
|
| 413 |
-
f'-c:v copy -c:a aac -shortest
|
| 414 |
)
|
| 415 |
process = await asyncio.create_subprocess_shell(
|
| 416 |
final_cmd,
|
|
@@ -420,15 +428,15 @@ async def concatenate_videos_with_music(request: Request):
|
|
| 420 |
stdout, stderr = await process.communicate()
|
| 421 |
|
| 422 |
if process.returncode != 0:
|
| 423 |
-
print(f"FFmpeg
|
| 424 |
-
raise HTTPException(status_code=500, detail=f"FFmpeg
|
| 425 |
|
| 426 |
# Clean up temporary files
|
| 427 |
-
for file in
|
| 428 |
os.remove(file)
|
| 429 |
-
os.remove(
|
| 430 |
-
os.remove(
|
| 431 |
-
os.remove(
|
| 432 |
os.rmdir(temp_dir)
|
| 433 |
|
| 434 |
# Return the URL path to the output file
|
|
|
|
| 358 |
print(traceback.format_exc())
|
| 359 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 360 |
|
| 361 |
+
@app.post("/create_slideshow")
|
| 362 |
+
async def create_slideshow(request: Request):
|
| 363 |
try:
|
| 364 |
# Generate a unique filename for the output
|
| 365 |
output_filename = f"{uuid.uuid4()}.mp4"
|
|
|
|
| 368 |
os.makedirs(temp_dir, exist_ok=True)
|
| 369 |
|
| 370 |
data = await request.json()
|
| 371 |
+
image_urls = data.get("image_urls")
|
| 372 |
+
audio_url = data.get("audio_url")
|
| 373 |
+
duration = data.get("duration", 4) # Default to 4 seconds per image if not specified
|
| 374 |
|
| 375 |
+
if not image_urls or not isinstance(image_urls, list):
|
| 376 |
+
raise HTTPException(status_code=400, detail="Invalid image_urls in request. Must be a list of URLs.")
|
| 377 |
|
| 378 |
+
if not audio_url:
|
| 379 |
+
raise HTTPException(status_code=400, detail="Missing audio_url in request.")
|
| 380 |
|
| 381 |
+
# Download the images
|
| 382 |
+
image_files = []
|
| 383 |
+
for i, url in enumerate(image_urls):
|
| 384 |
+
image_file = await download_file(url, f"_{i}.jpg")
|
| 385 |
+
image_files.append(image_file)
|
| 386 |
|
| 387 |
+
# Download audio file
|
| 388 |
+
audio_file = await download_file(audio_url, ".mp3")
|
|
|
|
|
|
|
|
|
|
| 389 |
|
| 390 |
+
# Create a text file with the list of images and their duration
|
| 391 |
+
images_list_path = os.path.join(temp_dir, "images_list.txt")
|
| 392 |
+
with open(images_list_path, "w") as f:
|
| 393 |
+
for file in image_files:
|
| 394 |
+
f.write(f"file '{file}'\n")
|
| 395 |
+
f.write(f"duration {duration}\n")
|
| 396 |
+
# Add the last image again with a small duration to prevent ffmpeg from cutting it off
|
| 397 |
+
f.write(f"file '{image_files[-1]}'\n")
|
| 398 |
+
f.write(f"duration 0.1\n")
|
| 399 |
+
|
| 400 |
+
# Create intermediate video without audio
|
| 401 |
+
intermediate_video = os.path.join(temp_dir, "intermediate.mp4")
|
| 402 |
+
# Use the complex concat demuxer for images to create a slideshow
|
| 403 |
+
concat_cmd = (
|
| 404 |
+
f'ffmpeg -f concat -safe 0 -i {images_list_path} -vsync vfr '
|
| 405 |
+
f'-pix_fmt yuv420p -c:v libx264 -r 30 {intermediate_video}'
|
| 406 |
+
)
|
| 407 |
process = await asyncio.create_subprocess_shell(
|
| 408 |
concat_cmd,
|
| 409 |
stdout=asyncio.subprocess.PIPE,
|
|
|
|
| 412 |
stdout, 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 |
final_cmd = (
|
| 420 |
+
f'ffmpeg -i {intermediate_video} -i {audio_file} -map 0:v -map 1:a '
|
| 421 |
+
f'-c:v copy -c:a aac -shortest {output_path}'
|
| 422 |
)
|
| 423 |
process = await asyncio.create_subprocess_shell(
|
| 424 |
final_cmd,
|
|
|
|
| 428 |
stdout, stderr = await process.communicate()
|
| 429 |
|
| 430 |
if process.returncode != 0:
|
| 431 |
+
print(f"FFmpeg audio addition error: {stderr.decode()}")
|
| 432 |
+
raise HTTPException(status_code=500, detail=f"FFmpeg audio addition failed: {stderr.decode()}")
|
| 433 |
|
| 434 |
# Clean up temporary files
|
| 435 |
+
for file in image_files:
|
| 436 |
os.remove(file)
|
| 437 |
+
os.remove(images_list_path)
|
| 438 |
+
os.remove(intermediate_video)
|
| 439 |
+
os.remove(audio_file)
|
| 440 |
os.rmdir(temp_dir)
|
| 441 |
|
| 442 |
# Return the URL path to the output file
|