Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
import tempfile
|
| 4 |
import shutil
|
|
@@ -77,7 +77,6 @@ async def merge_endpoint(
|
|
| 77 |
files: list[UploadFile] = File(...),
|
| 78 |
orig_vol: float = Form(1.0),
|
| 79 |
music_vol: float = Form(0.5),
|
| 80 |
-
background_tasks: BackgroundTasks,
|
| 81 |
):
|
| 82 |
temp_dir = tempfile.mkdtemp()
|
| 83 |
try:
|
|
@@ -94,28 +93,31 @@ async def merge_endpoint(
|
|
| 94 |
# Separate video and audio files
|
| 95 |
video_files = [f for f in saved_files if f.lower().endswith(".mp4")]
|
| 96 |
audio_files = [
|
| 97 |
-
f
|
|
|
|
|
|
|
| 98 |
]
|
| 99 |
|
| 100 |
if len(saved_files) < 2:
|
| 101 |
-
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 102 |
return {"error": "Please upload at least 2 files (videos or audios)."}
|
| 103 |
|
|
|
|
| 104 |
output_path = os.path.join(temp_dir, "merged_output.mp4")
|
| 105 |
|
|
|
|
| 106 |
merge_videos_and_audios(video_files, audio_files, output_path, temp_dir)
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
background_tasks.add_task(shutil.rmtree, temp_dir, ignore_errors=True)
|
| 110 |
-
|
| 111 |
return FileResponse(output_path, media_type="video/mp4", filename="merged_output.mp4")
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
-
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 115 |
error_msg = f"Error: {str(e)}\n\n{traceback.format_exc()}"
|
| 116 |
logger.error(error_msg)
|
| 117 |
return {"error": error_msg}
|
| 118 |
|
|
|
|
|
|
|
|
|
|
| 119 |
# Optional: log public URL
|
| 120 |
def log_api_url():
|
| 121 |
url = os.getenv("SPACE_PUBLIC_URL")
|
|
@@ -124,4 +126,6 @@ def log_api_url():
|
|
| 124 |
else:
|
| 125 |
logger.info("SPACE_PUBLIC_URL environment variable not found")
|
| 126 |
|
| 127 |
-
log_api_url()
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
import tempfile
|
| 4 |
import shutil
|
|
|
|
| 77 |
files: list[UploadFile] = File(...),
|
| 78 |
orig_vol: float = Form(1.0),
|
| 79 |
music_vol: float = Form(0.5),
|
|
|
|
| 80 |
):
|
| 81 |
temp_dir = tempfile.mkdtemp()
|
| 82 |
try:
|
|
|
|
| 93 |
# Separate video and audio files
|
| 94 |
video_files = [f for f in saved_files if f.lower().endswith(".mp4")]
|
| 95 |
audio_files = [
|
| 96 |
+
f
|
| 97 |
+
for f in saved_files
|
| 98 |
+
if f.lower().endswith((".mp3", ".wav", ".aac", ".m4a", ".ogg"))
|
| 99 |
]
|
| 100 |
|
| 101 |
if len(saved_files) < 2:
|
|
|
|
| 102 |
return {"error": "Please upload at least 2 files (videos or audios)."}
|
| 103 |
|
| 104 |
+
# Prepare output path
|
| 105 |
output_path = os.path.join(temp_dir, "merged_output.mp4")
|
| 106 |
|
| 107 |
+
# Merge videos and audios
|
| 108 |
merge_videos_and_audios(video_files, audio_files, output_path, temp_dir)
|
| 109 |
|
| 110 |
+
# Return merged file
|
|
|
|
|
|
|
| 111 |
return FileResponse(output_path, media_type="video/mp4", filename="merged_output.mp4")
|
| 112 |
|
| 113 |
except Exception as e:
|
|
|
|
| 114 |
error_msg = f"Error: {str(e)}\n\n{traceback.format_exc()}"
|
| 115 |
logger.error(error_msg)
|
| 116 |
return {"error": error_msg}
|
| 117 |
|
| 118 |
+
finally:
|
| 119 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 120 |
+
|
| 121 |
# Optional: log public URL
|
| 122 |
def log_api_url():
|
| 123 |
url = os.getenv("SPACE_PUBLIC_URL")
|
|
|
|
| 126 |
else:
|
| 127 |
logger.info("SPACE_PUBLIC_URL environment variable not found")
|
| 128 |
|
| 129 |
+
log_api_url() fix it
|
| 130 |
+
|
| 131 |
+
|