Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 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(
|
| 11 |
+
files: list[UploadFile] = File(...),
|
| 12 |
+
orig_vol: float = Form(1.0),
|
| 13 |
+
music_vol: float = Form(0.5),
|
| 14 |
+
):
|
| 15 |
+
temp_dir = tempfile.mkdtemp()
|
| 16 |
+
try:
|
| 17 |
+
saved_files = []
|
| 18 |
+
# Save uploaded files to disk
|
| 19 |
+
for upload_file in files:
|
| 20 |
+
file_path = os.path.join(temp_dir, upload_file.filename)
|
| 21 |
+
with open(file_path, "wb") as out_file:
|
| 22 |
+
content = await upload_file.read()
|
| 23 |
+
out_file.write(content)
|
| 24 |
+
saved_files.append(file_path)
|
| 25 |
+
|
| 26 |
+
# Your merge logic here
|
| 27 |
+
# For demo: just return first uploaded file as "merged" output
|
| 28 |
+
if not saved_files:
|
| 29 |
+
return {"error": "No files uploaded"}
|
| 30 |
+
|
| 31 |
+
output_path = saved_files[0]
|
| 32 |
+
|
| 33 |
+
# Return the merged file (video or audio)
|
| 34 |
+
return FileResponse(output_path, media_type="application/octet-stream", filename="merged_output")
|
| 35 |
+
|
| 36 |
+
finally:
|
| 37 |
+
# Cleanup temp_dir after response is sent
|
| 38 |
+
shutil.rmtree(temp_dir)
|