Saqib
commited on
Update modules/app.py
Browse files- modules/app.py +40 -30
modules/app.py
CHANGED
@@ -72,48 +72,58 @@ async def download_file(url: str, suffix: str):
|
|
72 |
|
73 |
@app.post("/add_audio_to_image")
|
74 |
async def add_audio_to_image(input_data: AudioImageInput):
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
94 |
|
95 |
@app.post("/concatenate_videos")
|
96 |
async def concatenate_videos(input_data: VideosInput):
|
97 |
-
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
117 |
|
118 |
@app.get("/get_audio")
|
119 |
async def get_audio(url: str):
|
|
|
72 |
|
73 |
@app.post("/add_audio_to_image")
|
74 |
async def add_audio_to_image(input_data: AudioImageInput):
|
75 |
+
try:
|
76 |
+
# Download image and audio files
|
77 |
+
temp_image_path = await download_file(str(input_data.image_url), ".png")
|
78 |
+
temp_audio_path = await download_file(str(input_data.audio_url), ".mp3")
|
79 |
|
80 |
+
# Generate a unique filename
|
81 |
+
output_filename = f"{uuid.uuid4()}.mp4"
|
82 |
+
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
83 |
|
84 |
+
# Use ffmpeg to combine image and audio into a video
|
85 |
+
input_image = ffmpeg.input(temp_image_path, loop=1, t=5) # 5 seconds duration
|
86 |
+
input_audio = ffmpeg.input(temp_audio_path)
|
87 |
+
ffmpeg.concat(input_image, input_audio, v=1, a=1).output(output_path, vcodec='libx264', acodec='aac').run()
|
88 |
|
89 |
+
# Clean up temporary files
|
90 |
+
os.unlink(temp_image_path)
|
91 |
+
os.unlink(temp_audio_path)
|
92 |
|
93 |
+
# Return the URL path to the output file
|
94 |
+
return f"/output/{output_filename}"
|
95 |
+
except Exception as e:
|
96 |
+
print(f"An error occurred: {str(e)}")
|
97 |
+
print(traceback.format_exc())
|
98 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
99 |
|
100 |
@app.post("/concatenate_videos")
|
101 |
async def concatenate_videos(input_data: VideosInput):
|
102 |
+
try:
|
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(await download_file(str(video_url), ".mp4"))
|
108 |
|
109 |
+
# Generate a unique filename
|
110 |
+
output_filename = f"{uuid.uuid4()}.mp4"
|
111 |
+
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
112 |
|
113 |
+
# Concatenate video clips using ffmpeg
|
114 |
+
inputs = [ffmpeg.input(path) for path in temp_video_paths]
|
115 |
+
ffmpeg.concat(*inputs, v=1, a=1).output(output_path, vcodec='libx264', acodec='aac').run()
|
116 |
|
117 |
+
# Clean up temporary files
|
118 |
+
for path in temp_video_paths:
|
119 |
+
os.unlink(path)
|
120 |
|
121 |
+
# Return the URL path to the output file
|
122 |
+
return f"/output/{output_filename}"
|
123 |
+
except Exception as e:
|
124 |
+
print(f"An error occurred: {str(e)}")
|
125 |
+
print(traceback.format_exc())
|
126 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
127 |
|
128 |
@app.get("/get_audio")
|
129 |
async def get_audio(url: str):
|