Spaces:
Sleeping
Sleeping
Saqib commited on
Update modules/app.py
Browse files- modules/app.py +14 -21
modules/app.py
CHANGED
|
@@ -97,24 +97,6 @@ async def add_audio_to_image(input_data: AudioImageInput):
|
|
| 97 |
print(traceback.format_exc())
|
| 98 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 99 |
|
| 100 |
-
def concatenate_video_audio_clips(clip_names, output_filename):
|
| 101 |
-
inputs = []
|
| 102 |
-
filter_complex = []
|
| 103 |
-
|
| 104 |
-
for i, name in enumerate(clip_names):
|
| 105 |
-
# For each clip, create separate inputs for video and audio
|
| 106 |
-
inputs.append(ffmpeg.input(name))
|
| 107 |
-
# Create filter_complex entries for concatenating without losing sync
|
| 108 |
-
filter_complex.append(f"[{i}:v][{i}:a]")
|
| 109 |
-
|
| 110 |
-
# Generate the filter_complex string for concatenating all clips together
|
| 111 |
-
filter_complex_string = "".join(f"{x}concat=n={len(clip_names)}:v=1:a=1[v][a];" for x in filter_complex)[:-1]
|
| 112 |
-
|
| 113 |
-
# Execute ffmpeg command with complex filter
|
| 114 |
-
ffmpeg.output(inputs, output_filename, vcodec='libx264', acodec='aac', **{'filter_complex': filter_complex_string}).run()
|
| 115 |
-
|
| 116 |
-
return output_filename
|
| 117 |
-
|
| 118 |
@app.post("/concatenate_videos")
|
| 119 |
async def concatenate_videos(input_data: VideosInput):
|
| 120 |
try:
|
|
@@ -124,12 +106,23 @@ async def concatenate_videos(input_data: VideosInput):
|
|
| 124 |
for video_url in input_data.video_urls:
|
| 125 |
temp_video_paths.append(await download_file(str(video_url), ".mp4"))
|
| 126 |
|
| 127 |
-
# Generate a unique filename for the output
|
| 128 |
output_filename = f"{uuid.uuid4()}.mp4"
|
| 129 |
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
# Clean up temporary files
|
| 135 |
for path in temp_video_paths:
|
|
|
|
| 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:
|
|
|
|
| 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 for the output
|
| 110 |
output_filename = f"{uuid.uuid4()}.mp4"
|
| 111 |
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 112 |
|
| 113 |
+
# Separate video and audio streams
|
| 114 |
+
video_and_audio_streams = []
|
| 115 |
+
for path in temp_video_paths:
|
| 116 |
+
video = ffmpeg.input(path).video
|
| 117 |
+
audio = ffmpeg.input(path).audio
|
| 118 |
+
video_and_audio_streams.append(video)
|
| 119 |
+
video_and_audio_streams.append(audio)
|
| 120 |
+
|
| 121 |
+
# Concatenate video and audio streams
|
| 122 |
+
joined = ffmpeg.concat(*video_and_audio_streams, v=1, a=1).node
|
| 123 |
+
|
| 124 |
+
# Merge video and audio
|
| 125 |
+
ffmpeg.output(joined[0], joined[1], output_path, vcodec='libx264', acodec='aac').run()
|
| 126 |
|
| 127 |
# Clean up temporary files
|
| 128 |
for path in temp_video_paths:
|