Update main.py
Browse files61 unmodified lines626364656667686970717273747576778 unmodified lines86878889909192939495969798991001018 unmodified lines1101111121131141151161171181191201211221231241259 unmodified lines1351361371381391401411421431441451461471481491507 unmodified lines15815916016116216316416516616716816917017117217317417561 unmodified lines# Call the modal API with the request data and download the output filedata = await request.json()async with aiohttp.ClientSession() as session:async with session.post(f"{MODAL_BASE_URL}/add_audio_to_image", json=data) as response:if response.status != 200:raise HTTPException(status_code=500, detail="Failed to process request")output_data = await response.read()async with aiofiles.open(output_path, "wb") as f:await f.write(output_data)# Return the URL path to the output filereturn f"https://sxqib-api.hf.space/output/{output_filename}"except Exception as e:print(f"An error occurred: {str(e)}")print(traceback.format_exc())8 unmodified lines# Call the modal API with the request data and download the output filedata = await request.json()async with aiohttp.ClientSession() as session:async with session.post(f"{MODAL_BASE_URL}/add_audio_to_video", json=data) as response:if response.status != 200:raise HTTPException(status_code=500, detail="Failed to process request")output_data = await response.read()async with aiofiles.open(output_path, "wb") as f:await f.write(output_data)# Return the URL path to the output filereturn f"https://sxqib-api.hf.space/output/{output_filename}"except Exception as e:print(f"An error occurred: {str(e)}")print(traceback.format_exc())8 unmodified lines# Call the modal API with the request data and download the output filedata = await request.json()async with aiohttp.ClientSession() as session:async with session.post(f"{MODAL_BASE_URL}/concatenate_videos", json=data) as response:if response.status != 200:raise HTTPException(status_code=500, detail="Failed to process request")output_data = await response.read()async with aiofiles.open(output_path, "wb") as f:await f.write(output_data)# Return the URL path to the output filereturn f"https://sxqib-api.hf.space/output/{output_filename}"except Exception as e:print(f"An error occurred: {str(e)}")9 unmodified lines# Call the modal API with the request data and download the output filedata = await request.json()async with aiohttp.ClientSession() as session:async with session.post(f"{MODAL_BASE_URL}/concatenate_audio", json=data) as response:if response.status != 200:raise HTTPException(status_code=500, detail="Failed to process request")output_data = await response.read()async with aiofiles.open(output_path, "wb") as f:await f.write(output_data)# Return the URL path to the output filereturn f"https://sxqib-api.hf.space/audio_files/{output_filename}"except Exception as e:print(f"An error occurred: {str(e)}")7 unmodified linesoutput_filename = f"{uuid.uuid4()}.mp4"output_path = os.path.join(OUTPUT_DIR, output_filename)# Call the modal API with the request data and download the output filedata = await request.json()async with aiohttp.ClientSession() as session:async with session.post(f"{MODAL_BASE_URL}/make_video", json=data) as response:if response.status != 200:raise HTTPException(status_code=500, detail="Failed to process request")output_data = await response.read()async with aiofiles.open(output_path, "wb") as f:await f.write(output_data)# Return the URL path to the output filereturn f"https://sxqib-api.hf.space/output/{output_filename}"except Exception as e:print(f"An error occurred: {str(e)}")61 unmodified lines626364656667686970717273747576777879808182838485868788899091929394958 unmodified lines1041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361378 unmodified lines1461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871889 unmodified lines1981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392407 unmodified lines24824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629761 unmodified lines# Call the modal API with the request data and download the output filedata = await request.json()image_url = data.get("image_url")audio_url = data.get("audio_url")if not image_url or not audio_url:raise HTTPException(status_code=400, detail="Missing image_url or audio_url in request")image_file = await download_file(image_url, ".jpg")audio_file = await download_file(audio_url, ".mp3")# Run ffmpeg commandffmpeg_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}"process = await asyncio.create_subprocess_shell(ffmpeg_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:print(f"FFmpeg error: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")# Clean up temporary filesos.remove(image_file)os.remove(audio_file)# 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())8 unmodified lines# Call the modal API with the request data and download the output filedata = await request.json()video_url = data.get("video_url")audio_url = data.get("audio_url")if not video_url or not audio_url:raise HTTPException(status_code=400, detail="Missing video_url or audio_url in request")video_file = await download_file(video_url, ".mp4")audio_file = await download_file(audio_url, ".mp3")# Run ffmpeg commandffmpeg_cmd = f"ffmpeg -i {video_file} -i {audio_file} -c:v copy -c:a aac -shortest {output_path}"process = await asyncio.create_subprocess_shell(ffmpeg_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:print(f"FFmpeg error: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")# Clean up temporary filesos.remove(video_file)os.remove(audio_file)# 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())8 unmodified lines# Call the modal API with the request data and download the output filedata = await request.json()video_urls = data.get("video_urls")if 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.")# 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(OUTPUT_DIR, "concat_list.txt")with open(concat_list_path, "w") as f:for file in video_files:f.write(f"file '{file}'\n")# Run ffmpeg commandffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c copy {output_path}"process = await asyncio.create_subprocess_shell(ffmpeg_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:print(f"FFmpeg error: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")# Clean up temporary filesfor file in video_files:os.remove(file)os.remove(concat_list_path)# Return the URL path to the output filereturn f"/output/{output_filename}"except Exception as e:print(f"An error occurred: {str(e)}")9 unmodified lines# Call the modal API with the request data and download the output filedata = await request.json()audio_urls = data.get("audio_urls")if not audio_urls or not isinstance(audio_urls, list):raise HTTPException(status_code=400, detail="Invalid audio_urls in request. Must be a list of URLs.")# Download the audio filesaudio_files = []for i, url in enumerate(audio_urls):audio_file = await download_file(url, f"_{i}.mp3")audio_files.append(audio_file)# Create a temporary file with the list of input filesconcat_list_path = os.path.join(AUDIO_DIR, "concat_list.txt")with open(concat_list_path, "w") as f:for file in audio_files:f.write(f"file '{file}'\n")# Run ffmpeg commandffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c copy {output_path}"process = await asyncio.create_subprocess_shell(ffmpeg_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:print(f"FFmpeg error: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")# Clean up temporary filesfor file in audio_files:os.remove(file)os.remove(concat_list_path)# Return the URL path to the output filereturn f"/audio/{output_filename}"except Exception as e:print(f"An error occurred: {str(e)}")7 unmodified linesoutput_filename = f"{uuid.uuid4()}.mp4"output_path = os.path.join(OUTPUT_DIR, output_filename)data = await request.json()image_urls = data.get("image_urls")audio_url = data.get("audio_url")duration = data.get("duration", 5) # Default duration of 5 seconds per imageif not image_urls or not isinstance(image_urls, list) or not audio_url:raise HTTPException(status_code=400, detail="Invalid image_urls or audio_url in request.")# Download audio fileaudio_file = await download_file(audio_url, ".mp3")# Download image files and create a temporary file with the list of input filesimage_files = []concat_list_path = os.path.join(OUTPUT_DIR, "concat_list.txt")with open(concat_list_path, "w") as f:for i, url in enumerate(image_urls):image_file = await download_file(url, f"_{i}.jpg")image_files.append(image_file)f.write(f"file '{image_file}'\nduration {duration}\n") # Set duration for each image# Add the last image again to ensure the audio doesn't cut off abruptlyf.write(f"file '{image_files[-1]}'\n")# Run ffmpeg command to create video from images and add audioffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -i {audio_file} -c:v libx264 -pix_fmt yuv420p -c:a aac -shortest {output_path}"process = await asyncio.create_subprocess_shell(ffmpeg_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:print(f"FFmpeg error: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")# Clean up temporary filesfor file in image_files:os.remove(file)os.remove(audio_file)os.remove(concat_list_path)# Return the URL path to the output filereturn f"/output/{output_filename}"except Exception as e:print(f"An error occurred: {str(e)}")
main.py+163-41