Update main.py
Browse files357 unmodified lines358359360361362363357 unmodified linesprint(traceback.format_exc())raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=7860)357 unmodified lines358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444357 unmodified linesprint(traceback.format_exc())raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")@app.post("/concatenate_videos_with_music")async def concatenate_videos_with_music(request: Request):try:# Generate a unique filename for the outputoutput_filename = f"{uuid.uuid4()}.mp4"output_path = os.path.join(OUTPUT_DIR, output_filename)temp_dir = os.path.join(tempfile.gettempdir(), generate_hash())os.makedirs(temp_dir, exist_ok=True)data = await request.json()video_urls = data.get("video_urls")music_url = data.get("music_url")volume_adjustment = data.get("volume_adjustment", 1.0) # Default volume if not specifiedif not video_urls or not isinstance(video_urls, list):raise HTTPException(status_code=400, detail="Invalid video_urls in request. Must be a list of URLs.")if not music_url:raise HTTPException(status_code=400, detail="Missing music_url in request.")# Download the video filesvideo_files = []for i, url in enumerate(video_urls):video_file = await download_file(url, f"_{i}.mp4")video_files.append(video_file)# Create a temporary file with the list of input filesconcat_list_path = os.path.join(temp_dir, "concat_list.txt")with open(concat_list_path, "w") as f:for file in video_files:f.write(f"file '{file}'\n")# First concatenate all videosconcat_output = os.path.join(temp_dir, "concat_output.mp4")concat_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c:v copy -an {concat_output}"process = await asyncio.create_subprocess_shell(concat_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:print(f"FFmpeg concat error: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg concat failed: {stderr.decode()}")# Download music filemusic_file = await download_file(music_url, ".mp3")# Add music to the concatenated videofinal_cmd = (f'ffmpeg -i {concat_output} -i {music_file} -map 0:v -map 1:a 'f'-c:v copy -c:a aac -shortest -af "volume={volume_adjustment}" {output_path}')process = await asyncio.create_subprocess_shell(final_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:print(f"FFmpeg music mix error: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg music mixing failed: {stderr.decode()}")# Clean up temporary filesfor file in video_files:os.remove(file)os.remove(concat_list_path)os.remove(concat_output)os.remove(music_file)os.rmdir(temp_dir)# Return the URL path to the output filereturn f"/output/{output_filename}"except Exception as e:print(f"An error occurred: {str(e)}")print(traceback.format_exc())raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=7860)
main.py+81