saq1b commited on
Commit
5f98112
·
verified ·
1 Parent(s): 111a01b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +163 -41
main.py CHANGED
@@ -62,16 +62,34 @@ async def add_audio_to_image(request: Request):
62
 
63
  # Call the modal API with the request data and download the output file
64
  data = await request.json()
65
- async with aiohttp.ClientSession() as session:
66
- async with session.post(f"{MODAL_BASE_URL}/add_audio_to_image", json=data) as response:
67
- if response.status != 200:
68
- raise HTTPException(status_code=500, detail="Failed to process request")
69
- output_data = await response.read()
70
- async with aiofiles.open(output_path, "wb") as f:
71
- await f.write(output_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  # Return the URL path to the output file
74
- return f"https://sxqib-api.hf.space/output/{output_filename}"
75
  except Exception as e:
76
  print(f"An error occurred: {str(e)}")
77
  print(traceback.format_exc())
@@ -86,16 +104,34 @@ async def add_audio_to_video(request: Request):
86
 
87
  # Call the modal API with the request data and download the output file
88
  data = await request.json()
89
- async with aiohttp.ClientSession() as session:
90
- async with session.post(f"{MODAL_BASE_URL}/add_audio_to_video", json=data) as response:
91
- if response.status != 200:
92
- raise HTTPException(status_code=500, detail="Failed to process request")
93
- output_data = await response.read()
94
- async with aiofiles.open(output_path, "wb") as f:
95
- await f.write(output_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
  # Return the URL path to the output file
98
- return f"https://sxqib-api.hf.space/output/{output_filename}"
99
  except Exception as e:
100
  print(f"An error occurred: {str(e)}")
101
  print(traceback.format_exc())
@@ -110,16 +146,43 @@ async def concatenate_videos(request: Request):
110
 
111
  # Call the modal API with the request data and download the output file
112
  data = await request.json()
113
- async with aiohttp.ClientSession() as session:
114
- async with session.post(f"{MODAL_BASE_URL}/concatenate_videos", json=data) as response:
115
- if response.status != 200:
116
- raise HTTPException(status_code=500, detail="Failed to process request")
117
- output_data = await response.read()
118
- async with aiofiles.open(output_path, "wb") as f:
119
- await f.write(output_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  # Return the URL path to the output file
122
- return f"https://sxqib-api.hf.space/output/{output_filename}"
123
 
124
  except Exception as e:
125
  print(f"An error occurred: {str(e)}")
@@ -135,16 +198,43 @@ async def concatenate_audio(request: Request):
135
 
136
  # Call the modal API with the request data and download the output file
137
  data = await request.json()
138
- async with aiohttp.ClientSession() as session:
139
- async with session.post(f"{MODAL_BASE_URL}/concatenate_audio", json=data) as response:
140
- if response.status != 200:
141
- raise HTTPException(status_code=500, detail="Failed to process request")
142
- output_data = await response.read()
143
- async with aiofiles.open(output_path, "wb") as f:
144
- await f.write(output_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  # Return the URL path to the output file
147
- return f"https://sxqib-api.hf.space/audio_files/{output_filename}"
148
 
149
  except Exception as e:
150
  print(f"An error occurred: {str(e)}")
@@ -158,18 +248,50 @@ async def make_video(request: Request):
158
  output_filename = f"{uuid.uuid4()}.mp4"
159
  output_path = os.path.join(OUTPUT_DIR, output_filename)
160
 
161
- # Call the modal API with the request data and download the output file
162
  data = await request.json()
163
- async with aiohttp.ClientSession() as session:
164
- async with session.post(f"{MODAL_BASE_URL}/make_video", json=data) as response:
165
- if response.status != 200:
166
- raise HTTPException(status_code=500, detail="Failed to process request")
167
- output_data = await response.read()
168
- async with aiofiles.open(output_path, "wb") as f:
169
- await f.write(output_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
  # Return the URL path to the output file
172
- return f"https://sxqib-api.hf.space/output/{output_filename}"
173
 
174
  except Exception as e:
175
  print(f"An error occurred: {str(e)}")
 
62
 
63
  # Call the modal API with the request data and download the output file
64
  data = await request.json()
65
+ image_url = data.get("image_url")
66
+ audio_url = data.get("audio_url")
67
+
68
+ if not image_url or not audio_url:
69
+ raise HTTPException(status_code=400, detail="Missing image_url or audio_url in request")
70
+
71
+ image_file = await download_file(image_url, ".jpg")
72
+ audio_file = await download_file(audio_url, ".mp3")
73
+
74
+ # Run ffmpeg command
75
+ ffmpeg_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 {output_path}"
76
+ process = await asyncio.create_subprocess_shell(
77
+ ffmpeg_cmd,
78
+ stdout=asyncio.subprocess.PIPE,
79
+ stderr=asyncio.subprocess.PIPE
80
+ )
81
+ stdout, stderr = await process.communicate()
82
+
83
+ if process.returncode != 0:
84
+ print(f"FFmpeg error: {stderr.decode()}")
85
+ raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")
86
+
87
+ # Clean up temporary files
88
+ os.remove(image_file)
89
+ os.remove(audio_file)
90
 
91
  # Return the URL path to the output file
92
+ return f"/output/{output_filename}"
93
  except Exception as e:
94
  print(f"An error occurred: {str(e)}")
95
  print(traceback.format_exc())
 
104
 
105
  # Call the modal API with the request data and download the output file
106
  data = await request.json()
107
+ video_url = data.get("video_url")
108
+ audio_url = data.get("audio_url")
109
+
110
+ if not video_url or not audio_url:
111
+ raise HTTPException(status_code=400, detail="Missing video_url or audio_url in request")
112
+
113
+ video_file = await download_file(video_url, ".mp4")
114
+ audio_file = await download_file(audio_url, ".mp3")
115
+
116
+ # Run ffmpeg command
117
+ ffmpeg_cmd = f"ffmpeg -i {video_file} -i {audio_file} -c:v copy -c:a aac -shortest {output_path}"
118
+ process = await asyncio.create_subprocess_shell(
119
+ ffmpeg_cmd,
120
+ stdout=asyncio.subprocess.PIPE,
121
+ stderr=asyncio.subprocess.PIPE
122
+ )
123
+ stdout, stderr = await process.communicate()
124
+
125
+ if process.returncode != 0:
126
+ print(f"FFmpeg error: {stderr.decode()}")
127
+ raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")
128
+
129
+ # Clean up temporary files
130
+ os.remove(video_file)
131
+ os.remove(audio_file)
132
 
133
  # Return the URL path to the output file
134
+ return f"/output/{output_filename}"
135
  except Exception as e:
136
  print(f"An error occurred: {str(e)}")
137
  print(traceback.format_exc())
 
146
 
147
  # Call the modal API with the request data and download the output file
148
  data = await request.json()
149
+ video_urls = data.get("video_urls")
150
+
151
+ if not video_urls or not isinstance(video_urls, list):
152
+ raise HTTPException(status_code=400, detail="Invalid video_urls in request. Must be a list of URLs.")
153
+
154
+ # Download the video files
155
+ video_files = []
156
+ for i, url in enumerate(video_urls):
157
+ video_file = await download_file(url, f"_{i}.mp4")
158
+ video_files.append(video_file)
159
+
160
+ # Create a temporary file with the list of input files
161
+ concat_list_path = os.path.join(OUTPUT_DIR, "concat_list.txt")
162
+ with open(concat_list_path, "w") as f:
163
+ for file in video_files:
164
+ f.write(f"file '{file}'\n")
165
+
166
+ # Run ffmpeg command
167
+ ffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c copy {output_path}"
168
+ process = await asyncio.create_subprocess_shell(
169
+ ffmpeg_cmd,
170
+ stdout=asyncio.subprocess.PIPE,
171
+ stderr=asyncio.subprocess.PIPE
172
+ )
173
+ stdout, stderr = await process.communicate()
174
+
175
+ if process.returncode != 0:
176
+ print(f"FFmpeg error: {stderr.decode()}")
177
+ raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")
178
+
179
+ # Clean up temporary files
180
+ for file in video_files:
181
+ os.remove(file)
182
+ os.remove(concat_list_path)
183
 
184
  # Return the URL path to the output file
185
+ return f"/output/{output_filename}"
186
 
187
  except Exception as e:
188
  print(f"An error occurred: {str(e)}")
 
198
 
199
  # Call the modal API with the request data and download the output file
200
  data = await request.json()
201
+ audio_urls = data.get("audio_urls")
202
+
203
+ if not audio_urls or not isinstance(audio_urls, list):
204
+ raise HTTPException(status_code=400, detail="Invalid audio_urls in request. Must be a list of URLs.")
205
+
206
+ # Download the audio files
207
+ audio_files = []
208
+ for i, url in enumerate(audio_urls):
209
+ audio_file = await download_file(url, f"_{i}.mp3")
210
+ audio_files.append(audio_file)
211
+
212
+ # Create a temporary file with the list of input files
213
+ concat_list_path = os.path.join(AUDIO_DIR, "concat_list.txt")
214
+ with open(concat_list_path, "w") as f:
215
+ for file in audio_files:
216
+ f.write(f"file '{file}'\n")
217
+
218
+ # Run ffmpeg command
219
+ ffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c copy {output_path}"
220
+ process = await asyncio.create_subprocess_shell(
221
+ ffmpeg_cmd,
222
+ stdout=asyncio.subprocess.PIPE,
223
+ stderr=asyncio.subprocess.PIPE
224
+ )
225
+ stdout, stderr = await process.communicate()
226
+
227
+ if process.returncode != 0:
228
+ print(f"FFmpeg error: {stderr.decode()}")
229
+ raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")
230
+
231
+ # Clean up temporary files
232
+ for file in audio_files:
233
+ os.remove(file)
234
+ os.remove(concat_list_path)
235
 
236
  # Return the URL path to the output file
237
+ return f"/audio/{output_filename}"
238
 
239
  except Exception as e:
240
  print(f"An error occurred: {str(e)}")
 
248
  output_filename = f"{uuid.uuid4()}.mp4"
249
  output_path = os.path.join(OUTPUT_DIR, output_filename)
250
 
 
251
  data = await request.json()
252
+ image_urls = data.get("image_urls")
253
+ audio_url = data.get("audio_url")
254
+ duration = data.get("duration", 5) # Default duration of 5 seconds per image
255
+
256
+ if not image_urls or not isinstance(image_urls, list) or not audio_url:
257
+ raise HTTPException(status_code=400, detail="Invalid image_urls or audio_url in request.")
258
+
259
+ # Download audio file
260
+ audio_file = await download_file(audio_url, ".mp3")
261
+
262
+ # Download image files and create a temporary file with the list of input files
263
+ image_files = []
264
+ concat_list_path = os.path.join(OUTPUT_DIR, "concat_list.txt")
265
+ with open(concat_list_path, "w") as f:
266
+ for i, url in enumerate(image_urls):
267
+ image_file = await download_file(url, f"_{i}.jpg")
268
+ image_files.append(image_file)
269
+ f.write(f"file '{image_file}'\nduration {duration}\n") # Set duration for each image
270
+
271
+ # Add the last image again to ensure the audio doesn't cut off abruptly
272
+ f.write(f"file '{image_files[-1]}'\n")
273
+
274
+ # Run ffmpeg command to create video from images and add audio
275
+ ffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -i {audio_file} -c:v libx264 -pix_fmt yuv420p -c:a aac -shortest {output_path}"
276
+ process = await asyncio.create_subprocess_shell(
277
+ ffmpeg_cmd,
278
+ stdout=asyncio.subprocess.PIPE,
279
+ stderr=asyncio.subprocess.PIPE
280
+ )
281
+ stdout, stderr = await process.communicate()
282
+
283
+ if process.returncode != 0:
284
+ print(f"FFmpeg error: {stderr.decode()}")
285
+ raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")
286
+
287
+ # Clean up temporary files
288
+ for file in image_files:
289
+ os.remove(file)
290
+ os.remove(audio_file)
291
+ os.remove(concat_list_path)
292
 
293
  # Return the URL path to the output file
294
+ return f"/output/{output_filename}"
295
 
296
  except Exception as e:
297
  print(f"An error occurred: {str(e)}")