Update main.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517from fastapi import FastAPI, HTTPException, Requestfrom fastapi.staticfiles import StaticFilesfrom concurrent.futures import ThreadPoolExecutorimport asyncioimport aiohttpimport tempfileimport uuidimport shutilimport osimport randomimport tracebackimport stringapp = FastAPI()def generate_hash(length=12):# Characters that can appear in the hashcharacters = string.ascii_lowercase + string.digits# Generate a random string of the specified lengthhash_string = ''.join(random.choice(characters) for _ in range(length))return hash_string@app.get("/")async def read_root():return {"message": "Saqib's API"}# Create a directory to store MP3 files if it doesn't existAUDIO_DIR = "audio_files"os.makedirs(AUDIO_DIR, exist_ok=True)# Create a directory for storing output filesOUTPUT_DIR = "output"os.makedirs(OUTPUT_DIR, exist_ok=True)# Mount the audio directoryapp.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")# Mount the output directoryapp.mount("/output", StaticFiles(directory=OUTPUT_DIR), name="output")thread_pool = ThreadPoolExecutor(max_workers=2)async def run_ffmpeg_async(ffmpeg_command):loop = asyncio.get_running_loop()await loop.run_in_executor(thread_pool, ffmpeg_command)async def download_file(url: str, suffix: str):async with aiohttp.ClientSession() as session:async with session.get(url) as response:if response.status != 200:raise HTTPException(status_code=400, detail=f"Failed to download file from {url}")with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:temp_file.write(await response.read())return temp_file.name@app.post("/add_audio_to_image")async def add_audio_to_image(request: Request):try:# Generate a unique filenameoutput_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()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 command with improved audio detection parametersffmpeg_cmd = f"ffmpeg -loop 1 -i {image_file} -analyzeduration 10000000 -probesize 10000000 -i {audio_file} -c:v libx264 -tune stillimage -c:a aac -b:a 192k -strict experimental -shortest -pix_fmt yuv420p {output_path}"process = await asyncio.create_subprocess_shell(ffmpeg_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, 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())raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")@app.post("/add_audio_to_video")async def add_audio_to_video(request: Request):try:# Generate a unique filenameoutput_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()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 command with improved audio detection parametersffmpeg_cmd = f"ffmpeg -analyzeduration 10000000 -probesize 10000000 -i {video_file} -analyzeduration 10000000 -probesize 10000000 -i {audio_file} -c:v copy -c:a aac -strict experimental -shortest {output_path}"process = await asyncio.create_subprocess_shell(ffmpeg_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, 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())raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")@app.post("/concatenate_videos")async def concatenate_videos(request: Request):try:# Generate a unique filename for the outputoutput_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()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)_, 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)}")print(traceback.format_exc())raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")@app.post("/concatenate_audio")async def concatenate_audio(request: Request):try:# Generate a unique filename for the outputoutput_filename = f"{uuid.uuid4()}.mp3"output_path = os.path.join(AUDIO_DIR, output_filename)# 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 command with improved audio parametersffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c:a aac -b:a 192k -strict experimental {output_path}"process = await asyncio.create_subprocess_shell(ffmpeg_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, 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)}")print(traceback.format_exc())raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")@app.post("/make_video")async def make_video(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()assets = data.get("assets", {})clips = assets.get("clips", [])music_url = assets.get("music_url")volume_adjustment = data.get("volume_adjustment", 1.0) # Default to normal volume if not specifiedif not clips or not isinstance(clips, list):raise HTTPException(status_code=400, detail="Invalid clips in request.")# Create a list to hold clip video files that we'll concatenate laterclip_videos = []segment_list_path = os.path.join(temp_dir, "segment_list.txt")with open(segment_list_path, "w") as segment_file:# Process each clip: combine image with its audiofor i, clip in enumerate(clips):image_url = clip.get("image_url")audio_url = clip.get("audio_url")if not image_url or not audio_url:raise HTTPException(status_code=400, detail=f"Missing image_url or audio_url in clip {i}")# Download filesimage_file = await download_file(image_url, ".jpg")audio_file = await download_file(audio_url, ".mp3")# Create segment videosegment_output = os.path.join(temp_dir, f"segment_{i}.mp4")# Run ffmpeg command to create video segmentsegment_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 {segment_output}'proc = await asyncio.create_subprocess_shell(segment_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, stderr = await proc.communicate()if proc.returncode != 0:print(f"FFmpeg error for segment {i}: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg failed for segment {i}: {stderr.decode()}")# Add to list for concatenationclip_videos.append(segment_output)segment_file.write(f"file '{segment_output}'\n")# Clean up individual filesos.remove(image_file)os.remove(audio_file)# Concatenate all segment videosconcat_output = os.path.join(temp_dir, "concat_output.mp4")concat_cmd = f'ffmpeg -f concat -safe 0 -i {segment_list_path} -c copy {concat_output}'proc = await asyncio.create_subprocess_shell(concat_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, stderr = await proc.communicate()if proc.returncode != 0:print(f"FFmpeg concat error: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg concat failed: {stderr.decode()}")# If there's a music URL, download it and mix with the videoif music_url:music_file = await download_file(music_url, ".wav")# Final command to mix music with video at specified volumefinal_cmd = (f'ffmpeg -i {concat_output} -i {music_file} -filter_complex 'f'"[1:a]volume={volume_adjustment}[music];[0:a][music]amix=inputs=2:duration=longest" 'f'-c:v copy {output_path}')proc = await asyncio.create_subprocess_shell(final_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, stderr = await proc.communicate()if proc.returncode != 0:print(f"FFmpeg final mix error: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg music mixing failed: {stderr.decode()}")os.remove(music_file)else:# If no music, just copy the concatenated outputshutil.copy(concat_output, output_path)# Clean up temporary files and directoryfor video_file in clip_videos:os.remove(video_file)os.remove(segment_list_path)os.remove(concat_output)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)}")@app.post("/create_slideshow")async def create_slideshow(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()image_urls = data.get("image_urls")audio_url = data.get("audio_url")duration = data.get("duration", 4) # Default to 4 seconds per image if not specifiedif not image_urls or not isinstance(image_urls, list):raise HTTPException(status_code=400, detail="Invalid image_urls in request. Must be a list of URLs.")if not audio_url:raise HTTPException(status_code=400, detail="Missing audio_url in request.")# Download the imagesimage_files = []for i, url in enumerate(image_urls):image_file = await download_file(url, f"_{i}.jpg")image_files.append(image_file)# Download audio fileaudio_file = await download_file(audio_url, ".mp3")# Pre-process audio file to ensure it's validnormalized_audio = os.path.join(temp_dir, "normalized_audio.wav")audio_check_cmd = (f'ffmpeg -i {audio_file} -af "aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo" 'f'-y {normalized_audio}')audio_process = await asyncio.create_subprocess_shell(audio_check_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, audio_stderr = await audio_process.communicate()# If audio normalization fails, create a silent audio trackif audio_process.returncode != 0:print(f"Audio normalization failed: {audio_stderr.decode()}")print("Creating silent audio track instead...")# Calculate total video duration based on number of images and duration per imagetotal_duration = len(image_urls) * durationsilent_cmd = (f'ffmpeg -f lavfi -i anullsrc=r=44100:cl=stereo -t {total_duration} 'f'-y {normalized_audio}')silent_process = await asyncio.create_subprocess_shell(silent_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, silent_stderr = await silent_process.communicate()if silent_process.returncode != 0:print(f"Silent audio creation failed: {silent_stderr.decode()}")# Fall back to no audio if even silent audio failsnormalized_audio = None# Create a text file with the list of images and their durationimages_list_path = os.path.join(temp_dir, "images_list.txt")with open(images_list_path, "w") as f:for file in image_files:f.write(f"file '{file}'\n")f.write(f"duration {duration}\n")# Add the last image again with a small duration to prevent ffmpeg from cutting it offf.write(f"file '{image_files[-1]}'\n")f.write(f"duration 0.1\n")# Create intermediate video without audiointermediate_video = os.path.join(temp_dir, "intermediate.mp4")# Use the complex concat demuxer for images to create a slideshowconcat_cmd = (f'ffmpeg -f concat -safe 0 -i {images_list_path} -vsync vfr 'f'-pix_fmt yuv420p -c:v libx264 -r 30 {intermediate_video}')process = await asyncio.create_subprocess_shell(concat_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, stderr = await process.communicate()if process.returncode != 0:print(f"FFmpeg slideshow creation error: {stderr.decode()}")raise HTTPException(status_code=500, detail=f"FFmpeg slideshow creation failed: {stderr.decode()}")# Add audio to the slideshow only if normalized_audio is availableif normalized_audio:final_cmd = (f'ffmpeg -i {intermediate_video} -i {normalized_audio} 'f'-c:v copy -c:a aac -b:a 192k -shortest {output_path}')process = await asyncio.create_subprocess_shell(final_cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)_, stderr = await process.communicate()if process.returncode != 0:print(f"FFmpeg audio addition error: {stderr.decode()}")# If adding audio fails, use just the videoshutil.copy(intermediate_video, output_path)else:# If no normalized audio available, use just the videoshutil.copy(intermediate_video, output_path)# Clean up temporary filesfor file in image_files:if os.path.exists(file):os.remove(file)if os.path.exists(images_list_path):os.remove(images_list_path)if os.path.exists(intermediate_video):os.remove(intermediate_video)if os.path.exists(audio_file):os.remove(audio_file)if normalized_audio and os.path.exists(normalized_audio):os.remove(normalized_audio)try:os.rmdir(temp_dir)except OSError:# Directory might not be empty, try to clean up remaining filesfor remaining_file in os.listdir(temp_dir):try:os.remove(os.path.join(temp_dir, remaining_file))except:passtry:os.rmdir(temp_dir)except:pass# 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)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137from fastapi import FastAPI, HTTPExceptionfrom fastapi.staticfiles import StaticFilesfrom pydantic import BaseModel, HttpUrlfrom typing import Listimport osimport subprocessimport uuidimport requestsimport refrom urllib.parse import urlparseimport shutil# Create FastAPI appapp = FastAPI()# Create and mount staticfiles directoryos.makedirs("staticfiles", exist_ok=True)app.mount("/static", StaticFiles(directory="staticfiles"), name="static")# Define input model for the requestclass SlideshowRequest(BaseModel):image_urls: List[HttpUrl]audio_url: HttpUrlduration: intdef extract_google_drive_id(url):"""Extract file ID from a Google Drive URL"""pattern = r'(?:/file/d/|id=|/open\?id=)([^/&]+)'match = re.search(pattern, str(url))return match.group(1) if match else Nonedef download_file(url, local_path):"""Download a file from URL to local path"""try:# Handle Google Drive URLsif "drive.google.com" in str(url):file_id = extract_google_drive_id(url)if file_id:url = f"https://drive.google.com/uc?export=download&id={file_id}"response = requests.get(str(url), stream=True)response.raise_for_status()with open(local_path, 'wb') as f:for chunk in response.iter_content(chunk_size=8192):f.write(chunk)return Trueexcept Exception as e:print(f"Error downloading {url}: {str(e)}")return Falsedef create_slideshow(image_paths, audio_path, output_path, duration):"""Generate slideshow from images and audio using ffmpeg"""# Create temporary file list for ffmpeg concatconcat_file = "temp_concat.txt"with open(concat_file, "w") as f:for img in image_paths:f.write(f"file '{img}'\n")f.write(f"duration {duration}\n")# Add the last image again without duration (required by ffmpeg)if image_paths:f.write(f"file '{image_paths[-1]}'\n")# Run ffmpeg command to create slideshow with audiocmd = ["ffmpeg","-f", "concat","-safe", "0","-i", concat_file,"-i", audio_path,"-c:v", "libx264","-pix_fmt", "yuv420p","-c:a", "aac","-shortest","-y",output_path]try:subprocess.run(cmd, check=True, capture_output=True)os.remove(concat_file)return Trueexcept subprocess.CalledProcessError as e:print(f"FFmpeg error: {e.stderr.decode()}")if os.path.exists(concat_file):os.remove(concat_file)return False@app.post("/make_slideshow")async def make_slideshow(request: SlideshowRequest):"""Create a slideshow from images and audio with specified duration per image.Returns the URL of the generated video."""# Create unique directory for this requestrequest_id = str(uuid.uuid4())request_dir = os.path.join("staticfiles", request_id)os.makedirs(request_dir, exist_ok=True)try:# Download imagesimage_paths = []for i, url in enumerate(request.image_urls):image_path = os.path.join(request_dir, f"image_{i:03d}.png")if download_file(url, image_path):image_paths.append(image_path)else:raise HTTPException(status_code=400, detail=f"Failed to download image: {url}")# Download audioaudio_path = os.path.join(request_dir, "audio.mp3")if not download_file(request.audio_url, audio_path):raise HTTPException(status_code=400, detail=f"Failed to download audio: {request.audio_url}")# Output video pathoutput_path = os.path.join(request_dir, "slideshow.mp4")# Generate slideshowif not create_slideshow(image_paths, audio_path, output_path, request.duration):raise HTTPException(status_code=500, detail="Failed to create slideshow")# Return URL to the videobase_url = "/static"video_url = f"{base_url}/{request_id}/slideshow.mp4"return {"url": video_url}except Exception as e:# Clean up on errorif os.path.exists(request_dir):shutil.rmtree(request_dir)raise HTTPException(status_code=500, detail=f"Error: {str(e)}")if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=8000)
main.py+114-494