Update main.py
Browse files
main.py
CHANGED
|
@@ -358,6 +358,87 @@ 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 |
if __name__ == "__main__":
|
| 362 |
import uvicorn
|
| 363 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 358 |
print(traceback.format_exc())
|
| 359 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 360 |
|
| 361 |
+
@app.post("/concatenate_videos_with_music")
|
| 362 |
+
async def concatenate_videos_with_music(request: Request):
|
| 363 |
+
try:
|
| 364 |
+
# Generate a unique filename for the output
|
| 365 |
+
output_filename = f"{uuid.uuid4()}.mp4"
|
| 366 |
+
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 367 |
+
temp_dir = os.path.join(tempfile.gettempdir(), generate_hash())
|
| 368 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 369 |
+
|
| 370 |
+
data = await request.json()
|
| 371 |
+
video_urls = data.get("video_urls")
|
| 372 |
+
music_url = data.get("music_url")
|
| 373 |
+
volume_adjustment = data.get("volume_adjustment", 1.0) # Default volume if not specified
|
| 374 |
+
|
| 375 |
+
if not video_urls or not isinstance(video_urls, list):
|
| 376 |
+
raise HTTPException(status_code=400, detail="Invalid video_urls in request. Must be a list of URLs.")
|
| 377 |
+
|
| 378 |
+
if not music_url:
|
| 379 |
+
raise HTTPException(status_code=400, detail="Missing music_url in request.")
|
| 380 |
+
|
| 381 |
+
# Download the video files
|
| 382 |
+
video_files = []
|
| 383 |
+
for i, url in enumerate(video_urls):
|
| 384 |
+
video_file = await download_file(url, f"_{i}.mp4")
|
| 385 |
+
video_files.append(video_file)
|
| 386 |
+
|
| 387 |
+
# Create a temporary file with the list of input files
|
| 388 |
+
concat_list_path = os.path.join(temp_dir, "concat_list.txt")
|
| 389 |
+
with open(concat_list_path, "w") as f:
|
| 390 |
+
for file in video_files:
|
| 391 |
+
f.write(f"file '{file}'\n")
|
| 392 |
+
|
| 393 |
+
# First concatenate all videos
|
| 394 |
+
concat_output = os.path.join(temp_dir, "concat_output.mp4")
|
| 395 |
+
concat_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c:v copy -an {concat_output}"
|
| 396 |
+
process = await asyncio.create_subprocess_shell(
|
| 397 |
+
concat_cmd,
|
| 398 |
+
stdout=asyncio.subprocess.PIPE,
|
| 399 |
+
stderr=asyncio.subprocess.PIPE
|
| 400 |
+
)
|
| 401 |
+
stdout, stderr = await process.communicate()
|
| 402 |
+
|
| 403 |
+
if process.returncode != 0:
|
| 404 |
+
print(f"FFmpeg concat error: {stderr.decode()}")
|
| 405 |
+
raise HTTPException(status_code=500, detail=f"FFmpeg concat failed: {stderr.decode()}")
|
| 406 |
+
|
| 407 |
+
# Download music file
|
| 408 |
+
music_file = await download_file(music_url, ".mp3")
|
| 409 |
+
|
| 410 |
+
# Add music to the concatenated video
|
| 411 |
+
final_cmd = (
|
| 412 |
+
f'ffmpeg -i {concat_output} -i {music_file} -map 0:v -map 1:a '
|
| 413 |
+
f'-c:v copy -c:a aac -shortest -af "volume={volume_adjustment}" {output_path}'
|
| 414 |
+
)
|
| 415 |
+
process = await asyncio.create_subprocess_shell(
|
| 416 |
+
final_cmd,
|
| 417 |
+
stdout=asyncio.subprocess.PIPE,
|
| 418 |
+
stderr=asyncio.subprocess.PIPE
|
| 419 |
+
)
|
| 420 |
+
stdout, stderr = await process.communicate()
|
| 421 |
+
|
| 422 |
+
if process.returncode != 0:
|
| 423 |
+
print(f"FFmpeg music mix error: {stderr.decode()}")
|
| 424 |
+
raise HTTPException(status_code=500, detail=f"FFmpeg music mixing failed: {stderr.decode()}")
|
| 425 |
+
|
| 426 |
+
# Clean up temporary files
|
| 427 |
+
for file in video_files:
|
| 428 |
+
os.remove(file)
|
| 429 |
+
os.remove(concat_list_path)
|
| 430 |
+
os.remove(concat_output)
|
| 431 |
+
os.remove(music_file)
|
| 432 |
+
os.rmdir(temp_dir)
|
| 433 |
+
|
| 434 |
+
# Return the URL path to the output file
|
| 435 |
+
return f"/output/{output_filename}"
|
| 436 |
+
|
| 437 |
+
except Exception as e:
|
| 438 |
+
print(f"An error occurred: {str(e)}")
|
| 439 |
+
print(traceback.format_exc())
|
| 440 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 441 |
+
|
| 442 |
if __name__ == "__main__":
|
| 443 |
import uvicorn
|
| 444 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|