Saqib
commited on
Commit
•
b675688
1
Parent(s):
be0c8aa
Update modules/app.py
Browse files- modules/app.py +20 -10
modules/app.py
CHANGED
@@ -2,9 +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 ffmpeg
|
6 |
from pydantic import BaseModel, HttpUrl
|
7 |
from typing import List
|
|
|
8 |
import requests
|
9 |
import tempfile
|
10 |
import time
|
@@ -40,9 +42,6 @@ def generate_hash(length=12):
|
|
40 |
async def read_root():
|
41 |
return {"message": "Saqib's API"}
|
42 |
|
43 |
-
# API URL
|
44 |
-
API_URL = "https://sxqib-api.hf.space"
|
45 |
-
|
46 |
# Create a directory to store MP3 files if it doesn't exist
|
47 |
AUDIO_DIR = "audio_files"
|
48 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
@@ -64,6 +63,12 @@ class AudioImageInput(BaseModel):
|
|
64 |
class VideosInput(BaseModel):
|
65 |
video_urls: List[HttpUrl]
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
async def download_file(url: str, suffix: str):
|
68 |
async with aiohttp.ClientSession() as session:
|
69 |
async with session.get(url) as response:
|
@@ -87,14 +92,17 @@ async def add_audio_to_image(input_data: AudioImageInput):
|
|
87 |
# Use ffmpeg to combine image and audio into a video
|
88 |
input_image = ffmpeg.input(temp_image_path, loop=1, t=5) # 5 seconds duration
|
89 |
input_audio = ffmpeg.input(temp_audio_path)
|
90 |
-
ffmpeg.concat(input_image, input_audio, v=1, a=1).output(output_path, vcodec='libx264', acodec='aac')
|
|
|
|
|
|
|
91 |
|
92 |
# Clean up temporary files
|
93 |
os.unlink(temp_image_path)
|
94 |
os.unlink(temp_audio_path)
|
95 |
|
96 |
# Return the URL path to the output file
|
97 |
-
return f"
|
98 |
except Exception as e:
|
99 |
print(f"An error occurred: {str(e)}")
|
100 |
print(traceback.format_exc())
|
@@ -104,7 +112,6 @@ async def add_audio_to_image(input_data: AudioImageInput):
|
|
104 |
async def concatenate_videos(input_data: VideosInput):
|
105 |
try:
|
106 |
temp_video_paths = []
|
107 |
-
|
108 |
# Download videos to temporary files
|
109 |
for video_url in input_data.video_urls:
|
110 |
temp_video_paths.append(await download_file(str(video_url), ".mp4"))
|
@@ -125,14 +132,17 @@ async def concatenate_videos(input_data: VideosInput):
|
|
125 |
joined = ffmpeg.concat(*video_and_audio_streams, v=1, a=1).node
|
126 |
|
127 |
# Merge video and audio
|
128 |
-
ffmpeg.output(joined[0], joined[1], output_path, vcodec='libx264', acodec='aac')
|
|
|
|
|
|
|
129 |
|
130 |
# Clean up temporary files
|
131 |
for path in temp_video_paths:
|
132 |
os.remove(path)
|
133 |
|
134 |
# Return the URL path to the output file
|
135 |
-
return f"
|
136 |
except Exception as e:
|
137 |
print(f"An error occurred: {str(e)}")
|
138 |
print(traceback.format_exc())
|
@@ -159,14 +169,14 @@ async def get_audio(url: str):
|
|
159 |
print((f"Downloading audio to: {out_file}"))
|
160 |
|
161 |
# Download the audio
|
162 |
-
video.download(output_path=AUDIO_DIR, filename=unique_filename)
|
163 |
|
164 |
file_stats = os.stat(out_file)
|
165 |
print(f'Size of audio file in Bytes: {file_stats.st_size}')
|
166 |
|
167 |
if file_stats.st_size <= 30000000:
|
168 |
# Construct the URL for the MP3 file
|
169 |
-
mp3_url = f"
|
170 |
return mp3_url
|
171 |
else:
|
172 |
os.remove(out_file)
|
|
|
2 |
from fastapi.staticfiles import StaticFiles
|
3 |
from pytubefix import YouTube
|
4 |
from pytubefix.exceptions import PytubeFixError
|
5 |
+
from concurrent.futures import ThreadPoolExecutor
|
6 |
import ffmpeg
|
7 |
from pydantic import BaseModel, HttpUrl
|
8 |
from typing import List
|
9 |
+
import asyncio
|
10 |
import requests
|
11 |
import tempfile
|
12 |
import time
|
|
|
42 |
async def read_root():
|
43 |
return {"message": "Saqib's API"}
|
44 |
|
|
|
|
|
|
|
45 |
# Create a directory to store MP3 files if it doesn't exist
|
46 |
AUDIO_DIR = "audio_files"
|
47 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
|
|
63 |
class VideosInput(BaseModel):
|
64 |
video_urls: List[HttpUrl]
|
65 |
|
66 |
+
thread_pool = ThreadPoolExecutor(max_workers=2)
|
67 |
+
|
68 |
+
async def run_ffmpeg_async(ffmpeg_command):
|
69 |
+
loop = asyncio.get_running_loop()
|
70 |
+
await loop.run_in_executor(thread_pool, ffmpeg_command)
|
71 |
+
|
72 |
async def download_file(url: str, suffix: str):
|
73 |
async with aiohttp.ClientSession() as session:
|
74 |
async with session.get(url) as response:
|
|
|
92 |
# Use ffmpeg to combine image and audio into a video
|
93 |
input_image = ffmpeg.input(temp_image_path, loop=1, t=5) # 5 seconds duration
|
94 |
input_audio = ffmpeg.input(temp_audio_path)
|
95 |
+
ffmpeg_command = ffmpeg.concat(input_image, input_audio, v=1, a=1).output(output_path, vcodec='libx264', acodec='aac')
|
96 |
+
|
97 |
+
# Run ffmpeg asynchronously
|
98 |
+
await run_ffmpeg_async(ffmpeg_command.run)
|
99 |
|
100 |
# Clean up temporary files
|
101 |
os.unlink(temp_image_path)
|
102 |
os.unlink(temp_audio_path)
|
103 |
|
104 |
# Return the URL path to the output file
|
105 |
+
return f"https://sxqib-api.hf.space/output/{output_filename}"
|
106 |
except Exception as e:
|
107 |
print(f"An error occurred: {str(e)}")
|
108 |
print(traceback.format_exc())
|
|
|
112 |
async def concatenate_videos(input_data: VideosInput):
|
113 |
try:
|
114 |
temp_video_paths = []
|
|
|
115 |
# Download videos to temporary files
|
116 |
for video_url in input_data.video_urls:
|
117 |
temp_video_paths.append(await download_file(str(video_url), ".mp4"))
|
|
|
132 |
joined = ffmpeg.concat(*video_and_audio_streams, v=1, a=1).node
|
133 |
|
134 |
# Merge video and audio
|
135 |
+
ffmpeg_command = ffmpeg.output(joined[0], joined[1], output_path, vcodec='libx264', acodec='aac')
|
136 |
+
|
137 |
+
# Run ffmpeg asynchronously
|
138 |
+
await run_ffmpeg_async(ffmpeg_command.run)
|
139 |
|
140 |
# Clean up temporary files
|
141 |
for path in temp_video_paths:
|
142 |
os.remove(path)
|
143 |
|
144 |
# Return the URL path to the output file
|
145 |
+
return f"https://sxqib-api.hf.space/output/{output_filename}"
|
146 |
except Exception as e:
|
147 |
print(f"An error occurred: {str(e)}")
|
148 |
print(traceback.format_exc())
|
|
|
169 |
print((f"Downloading audio to: {out_file}"))
|
170 |
|
171 |
# Download the audio
|
172 |
+
video.download(output_path=AUDIO_DIR, filename=unique_filename, timeout=30)
|
173 |
|
174 |
file_stats = os.stat(out_file)
|
175 |
print(f'Size of audio file in Bytes: {file_stats.st_size}')
|
176 |
|
177 |
if file_stats.st_size <= 30000000:
|
178 |
# Construct the URL for the MP3 file
|
179 |
+
mp3_url = f"https://sxqib-api.hf.space/audio/{unique_filename}"
|
180 |
return mp3_url
|
181 |
else:
|
182 |
os.remove(out_file)
|