saq1b commited on
Commit
66dcb35
·
verified ·
1 Parent(s): 275d0aa

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +44 -36
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("/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"
@@ -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
- 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,
@@ -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 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,
@@ -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 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
 
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