Saqib
commited on
Commit
•
455b2c2
1
Parent(s):
831f131
Update modules/app.py
Browse files- modules/app.py +50 -48
modules/app.py
CHANGED
@@ -2,7 +2,7 @@ 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 |
-
|
6 |
from pydantic import BaseModel, HttpUrl
|
7 |
from typing import List
|
8 |
import requests
|
@@ -61,73 +61,75 @@ class AudioImageInput(BaseModel):
|
|
61 |
class VideosInput(BaseModel):
|
62 |
video_urls: List[HttpUrl]
|
63 |
|
64 |
-
def download_file(url: str, suffix: str):
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
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 |
-
image_clip = image_clip.set_fps(24) # Set fps to 24
|
81 |
|
82 |
-
#
|
83 |
-
audio_clip = AudioFileClip(temp_audio_path)
|
84 |
-
|
85 |
-
# Set the audio of the video clip
|
86 |
-
video_with_audio = image_clip.set_audio(audio_clip)
|
87 |
-
|
88 |
-
# Generate a unique filename
|
89 |
output_filename = f"{uuid.uuid4()}.mp4"
|
90 |
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
# Return the URL path to the output file
|
100 |
return f"/output/{output_filename}"
|
101 |
|
102 |
-
@app.post("/concatenate_videos")
|
103 |
async def concatenate_videos(input_data: VideosInput):
|
104 |
temp_video_paths = []
|
105 |
|
106 |
# Download videos to temporary files
|
107 |
for video_url in input_data.video_urls:
|
108 |
-
temp_video_paths.append(download_file(str(video_url), ".mp4"))
|
109 |
-
|
110 |
-
# Load video clips
|
111 |
-
video_clips = []
|
112 |
-
for path in temp_video_paths:
|
113 |
-
clip = VideoFileClip(path)
|
114 |
-
if not clip.fps:
|
115 |
-
clip = clip.set_fps(24) # Set fps to 24 if not defined
|
116 |
-
video_clips.append(clip)
|
117 |
-
|
118 |
-
# Concatenate video clips
|
119 |
-
final_clip = concatenate_videoclips(video_clips)
|
120 |
|
121 |
-
# Generate a unique filename
|
122 |
output_filename = f"{uuid.uuid4()}.mp4"
|
123 |
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
# Return the URL path to the output file
|
133 |
return f"/output/{output_filename}"
|
|
|
2 |
from fastapi.staticfiles import StaticFiles
|
3 |
from pytubefix import YouTube
|
4 |
from pytubefix.exceptions import PytubeFixError
|
5 |
+
import ffmpeg
|
6 |
from pydantic import BaseModel, HttpUrl
|
7 |
from typing import List
|
8 |
import requests
|
|
|
61 |
class VideosInput(BaseModel):
|
62 |
video_urls: List[HttpUrl]
|
63 |
|
64 |
+
async def download_file(url: str, suffix: str):
|
65 |
+
async with aiohttp.ClientSession() as session:
|
66 |
+
async with session.get(url) as response:
|
67 |
+
if response.status != 200:
|
68 |
+
raise HTTPException(status_code=400, detail=f"Failed to download file from {url}")
|
69 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
70 |
+
temp_file.write(await response.read())
|
71 |
+
return temp_file.name
|
72 |
|
73 |
+
@app.post("/add_audio_to_image/")
|
74 |
async def add_audio_to_image(input_data: AudioImageInput):
|
75 |
# Download image and audio files
|
76 |
+
temp_image_path = await download_file(str(input_data.image_url), ".png")
|
77 |
+
temp_audio_path = await download_file(str(input_data.audio_url), ".mp3")
|
|
|
|
|
|
|
|
|
78 |
|
79 |
+
# Generate a unique filename for the output
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
output_filename = f"{uuid.uuid4()}.mp4"
|
81 |
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
82 |
|
83 |
+
try:
|
84 |
+
# Use ffmpeg to create a video from the image and add audio
|
85 |
+
(
|
86 |
+
ffmpeg
|
87 |
+
.input(temp_image_path, loop=1, t=5) # Loop the image for 5 seconds
|
88 |
+
.input(temp_audio_path)
|
89 |
+
.output(output_path, vcodec='libx264', acodec='aac', pix_fmt='yuv420p', shortest=None)
|
90 |
+
.overwrite_output()
|
91 |
+
.run(capture_stdout=True, capture_stderr=True)
|
92 |
+
)
|
93 |
+
except ffmpeg.Error as e:
|
94 |
+
raise HTTPException(status_code=500, detail=f"An error occurred while processing the files: {e.stderr.decode()}")
|
95 |
+
finally:
|
96 |
+
# Clean up temporary files
|
97 |
+
os.unlink(temp_image_path)
|
98 |
+
os.unlink(temp_audio_path)
|
99 |
|
100 |
# Return the URL path to the output file
|
101 |
return f"/output/{output_filename}"
|
102 |
|
103 |
+
@app.post("/concatenate_videos/")
|
104 |
async def concatenate_videos(input_data: VideosInput):
|
105 |
temp_video_paths = []
|
106 |
|
107 |
# Download videos to temporary files
|
108 |
for video_url in input_data.video_urls:
|
109 |
+
temp_video_paths.append(await download_file(str(video_url), ".mp4"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
+
# Generate a unique filename for the output
|
112 |
output_filename = f"{uuid.uuid4()}.mp4"
|
113 |
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
114 |
|
115 |
+
try:
|
116 |
+
# Prepare input files for concatenation
|
117 |
+
input_files = [ffmpeg.input(path) for path in temp_video_paths]
|
118 |
+
|
119 |
+
# Concatenate videos
|
120 |
+
(
|
121 |
+
ffmpeg
|
122 |
+
.concat(*input_files)
|
123 |
+
.output(output_path, vcodec='libx264', acodec='aac')
|
124 |
+
.overwrite_output()
|
125 |
+
.run(capture_stdout=True, capture_stderr=True)
|
126 |
+
)
|
127 |
+
except ffmpeg.Error as e:
|
128 |
+
raise HTTPException(status_code=500, detail=f"An error occurred while concatenating the videos: {e.stderr.decode()}")
|
129 |
+
finally:
|
130 |
+
# Clean up temporary files
|
131 |
+
for path in temp_video_paths:
|
132 |
+
os.unlink(path)
|
133 |
|
134 |
# Return the URL path to the output file
|
135 |
return f"/output/{output_filename}"
|