Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,10 +1,15 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
import tempfile
|
| 4 |
import shutil
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
|
|
| 8 |
|
| 9 |
@app.post("/merge")
|
| 10 |
async def merge_endpoint(
|
|
@@ -15,24 +20,34 @@ async def merge_endpoint(
|
|
| 15 |
temp_dir = tempfile.mkdtemp()
|
| 16 |
try:
|
| 17 |
saved_files = []
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
with open(
|
| 22 |
-
content = await
|
| 23 |
out_file.write(content)
|
| 24 |
-
saved_files.append(
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
if not saved_files:
|
| 29 |
-
return {"error": "No files uploaded"}
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
finally:
|
| 37 |
-
#
|
| 38 |
shutil.rmtree(temp_dir)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
import tempfile
|
| 4 |
import shutil
|
| 5 |
import os
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
# Import your existing merge functions here
|
| 9 |
+
# from your_module import merge_videos_and_audios # if separated, or define inline
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
+
logger = logging.getLogger("uvicorn.error")
|
| 13 |
|
| 14 |
@app.post("/merge")
|
| 15 |
async def merge_endpoint(
|
|
|
|
| 20 |
temp_dir = tempfile.mkdtemp()
|
| 21 |
try:
|
| 22 |
saved_files = []
|
| 23 |
+
for uploaded_file in files:
|
| 24 |
+
# Save each uploaded file to disk
|
| 25 |
+
file_location = os.path.join(temp_dir, uploaded_file.filename)
|
| 26 |
+
with open(file_location, "wb") as out_file:
|
| 27 |
+
content = await uploaded_file.read()
|
| 28 |
out_file.write(content)
|
| 29 |
+
saved_files.append(file_location)
|
| 30 |
|
| 31 |
+
if len(saved_files) < 2:
|
| 32 |
+
return {"error": "Please upload at least 2 video/audio files."}
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Call your existing merge function
|
| 35 |
+
output_path = merge_videos_and_audios(
|
| 36 |
+
video_files=[f for f in saved_files if f.lower().endswith(".mp4")],
|
| 37 |
+
audio_files=[f for f in saved_files if f.lower().endswith((".mp3", ".wav"))],
|
| 38 |
+
orig_vol=orig_vol,
|
| 39 |
+
music_vol=music_vol,
|
| 40 |
+
temp_dir=temp_dir,
|
| 41 |
+
)
|
| 42 |
|
| 43 |
+
if isinstance(output_path, str) and output_path.startswith("Error"):
|
| 44 |
+
return {"error": output_path}
|
| 45 |
+
|
| 46 |
+
# Return the merged file with appropriate media type
|
| 47 |
+
media_type = "video/mp4" if output_path.lower().endswith(".mp4") else "audio/mpeg"
|
| 48 |
+
file_name = os.path.basename(output_path)
|
| 49 |
+
return FileResponse(output_path, media_type=media_type, filename=file_name)
|
| 50 |
|
| 51 |
finally:
|
| 52 |
+
# Clean up uploaded files after response is sent
|
| 53 |
shutil.rmtree(temp_dir)
|