Spaces:
Sleeping
Sleeping
Saqib commited on
Update modules/app.py
Browse files- modules/app.py +85 -1
modules/app.py
CHANGED
|
@@ -2,6 +2,11 @@ from fastapi import FastAPI, HTTPException, Request, Depends
|
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
from pytubefix import YouTube
|
| 4 |
from pytubefix.exceptions import PytubeFixError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import time
|
| 6 |
import uuid
|
| 7 |
import aiohttp
|
|
@@ -39,9 +44,88 @@ async def read_root():
|
|
| 39 |
AUDIO_DIR = "audio_files"
|
| 40 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# Mount the audio directory
|
| 43 |
app.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
@app.get("/get_audio")
|
| 46 |
async def get_audio(url: str):
|
| 47 |
if not url:
|
|
@@ -75,7 +159,7 @@ async def get_audio(url: str):
|
|
| 75 |
else:
|
| 76 |
os.remove(out_file)
|
| 77 |
raise HTTPException(status_code=413, detail="Audio file is too large. Limited to about 1.5 hours.")
|
| 78 |
-
except
|
| 79 |
print(f"PytubeError occurred: {str(e)}")
|
| 80 |
raise HTTPException(status_code=400, detail=f"Error processing YouTube video: {str(e)}")
|
| 81 |
except HTTPException as he:
|
|
|
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
from pytubefix import YouTube
|
| 4 |
from pytubefix.exceptions import PytubeFixError
|
| 5 |
+
from moviepy.editor import VideoFileClip, concatenate_videoclips, CompositeVideoClip, AudioFileClip, ImageClip
|
| 6 |
+
from pydantic import BaseModel, HttpUrl
|
| 7 |
+
from typing import List
|
| 8 |
+
import requests
|
| 9 |
+
import tempfile
|
| 10 |
import time
|
| 11 |
import uuid
|
| 12 |
import aiohttp
|
|
|
|
| 44 |
AUDIO_DIR = "audio_files"
|
| 45 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
| 46 |
|
| 47 |
+
# Create a directory for storing output files
|
| 48 |
+
OUTPUT_DIR = "output"
|
| 49 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 50 |
+
|
| 51 |
# Mount the audio directory
|
| 52 |
app.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")
|
| 53 |
|
| 54 |
+
# Mount the output directory
|
| 55 |
+
app.mount("/output", StaticFiles(directory=OUTPUT_DIR), name="output")
|
| 56 |
+
|
| 57 |
+
class AudioImageInput(BaseModel):
|
| 58 |
+
image_url: HttpUrl
|
| 59 |
+
audio_url: HttpUrl
|
| 60 |
+
|
| 61 |
+
class VideosInput(BaseModel):
|
| 62 |
+
video_urls: List[HttpUrl]
|
| 63 |
+
|
| 64 |
+
def download_file(url: str, suffix: str):
|
| 65 |
+
response = requests.get(url)
|
| 66 |
+
if response.status_code != 200:
|
| 67 |
+
raise HTTPException(status_code=400, detail=f"Failed to download file from {url}")
|
| 68 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
| 69 |
+
temp_file.write(response.content)
|
| 70 |
+
return temp_file.name
|
| 71 |
+
|
| 72 |
+
@app.post("/add_audio_to_image/")
|
| 73 |
+
async def add_audio_to_image(input_data: AudioImageInput):
|
| 74 |
+
# Download image and audio files
|
| 75 |
+
temp_image_path = download_file(str(input_data.image_url), ".png")
|
| 76 |
+
temp_audio_path = download_file(str(input_data.audio_url), ".mp3")
|
| 77 |
+
|
| 78 |
+
# Create a video from the image
|
| 79 |
+
image_clip = ImageClip(temp_image_path).set_duration(5) # 5 seconds duration
|
| 80 |
+
|
| 81 |
+
# Load the audio
|
| 82 |
+
audio_clip = AudioFileClip(temp_audio_path)
|
| 83 |
+
|
| 84 |
+
# Set the audio of the video clip
|
| 85 |
+
video_with_audio = image_clip.set_audio(audio_clip)
|
| 86 |
+
|
| 87 |
+
# Generate a unique filename
|
| 88 |
+
output_filename = f"{uuid.uuid4()}.mp4"
|
| 89 |
+
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 90 |
+
|
| 91 |
+
# Write the result to a file
|
| 92 |
+
video_with_audio.write_videofile(output_path, codec="libx264", audio_codec="aac")
|
| 93 |
+
|
| 94 |
+
# Clean up temporary files
|
| 95 |
+
os.unlink(temp_image_path)
|
| 96 |
+
os.unlink(temp_audio_path)
|
| 97 |
+
|
| 98 |
+
# Return the URL path to the output file
|
| 99 |
+
return f"/output/{output_filename}"
|
| 100 |
+
|
| 101 |
+
@app.post("/concatenate_videos/")
|
| 102 |
+
async def concatenate_videos(input_data: VideosInput):
|
| 103 |
+
temp_video_paths = []
|
| 104 |
+
|
| 105 |
+
# Download videos to temporary files
|
| 106 |
+
for video_url in input_data.video_urls:
|
| 107 |
+
temp_video_paths.append(download_file(str(video_url), ".mp4"))
|
| 108 |
+
|
| 109 |
+
# Load video clips
|
| 110 |
+
video_clips = [VideoFileClip(path) for path in temp_video_paths]
|
| 111 |
+
|
| 112 |
+
# Concatenate video clips
|
| 113 |
+
final_clip = concatenate_videoclips(video_clips)
|
| 114 |
+
|
| 115 |
+
# Generate a unique filename
|
| 116 |
+
output_filename = f"{uuid.uuid4()}.mp4"
|
| 117 |
+
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 118 |
+
|
| 119 |
+
# Write the result to a file
|
| 120 |
+
final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac")
|
| 121 |
+
|
| 122 |
+
# Clean up temporary files
|
| 123 |
+
for path in temp_video_paths:
|
| 124 |
+
os.unlink(path)
|
| 125 |
+
|
| 126 |
+
# Return the URL path to the output file
|
| 127 |
+
return f"/output/{output_filename}"
|
| 128 |
+
|
| 129 |
@app.get("/get_audio")
|
| 130 |
async def get_audio(url: str):
|
| 131 |
if not url:
|
|
|
|
| 159 |
else:
|
| 160 |
os.remove(out_file)
|
| 161 |
raise HTTPException(status_code=413, detail="Audio file is too large. Limited to about 1.5 hours.")
|
| 162 |
+
except PytubeFixError as e:
|
| 163 |
print(f"PytubeError occurred: {str(e)}")
|
| 164 |
raise HTTPException(status_code=400, detail=f"Error processing YouTube video: {str(e)}")
|
| 165 |
except HTTPException as he:
|