Spaces:
Runtime error
Runtime error
cut audio into segments
Browse files
main.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from fastapi import FastAPI, UploadFile, HTTPException
|
2 |
from ffmpeg import input, output, run
|
3 |
import ffmpeg
|
@@ -11,14 +12,15 @@ aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID']
|
|
11 |
aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY']
|
12 |
s3_bucket_name = os.environ['S3_BUCKET_NAME']
|
13 |
aws_region = 'eu-central-1'
|
14 |
-
|
15 |
# Initialize an S3 client
|
16 |
s3_client = boto3.client('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=aws_region)
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
# Temporary directory to store uploaded audio files
|
21 |
-
temp_dir = '/tmp/'
|
22 |
|
23 |
# Local paths for the fixed input audio files
|
24 |
gen_base_file = temp_dir + 'genBase.mp3'
|
@@ -38,6 +40,27 @@ async def merge_audio(output_key: str):
|
|
38 |
except Exception as e:
|
39 |
raise HTTPException(status_code=500, detail=f"Failed to download files from S3: {e}")
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
# Output file name
|
42 |
output_file = temp_dir + 'output.mp3'
|
43 |
|
|
|
1 |
+
from pydantic import BaseModel
|
2 |
from fastapi import FastAPI, UploadFile, HTTPException
|
3 |
from ffmpeg import input, output, run
|
4 |
import ffmpeg
|
|
|
12 |
aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY']
|
13 |
s3_bucket_name = os.environ['S3_BUCKET_NAME']
|
14 |
aws_region = 'eu-central-1'
|
15 |
+
temp_dir = '/tmp/'
|
16 |
# Initialize an S3 client
|
17 |
s3_client = boto3.client('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=aws_region)
|
18 |
|
19 |
+
class CutRequest(BaseModel):
|
20 |
+
segments: list[tuple[float, float]]
|
21 |
+
|
22 |
+
def download_files(output_key: str):
|
23 |
# Temporary directory to store uploaded audio files
|
|
|
24 |
|
25 |
# Local paths for the fixed input audio files
|
26 |
gen_base_file = temp_dir + 'genBase.mp3'
|
|
|
40 |
except Exception as e:
|
41 |
raise HTTPException(status_code=500, detail=f"Failed to download files from S3: {e}")
|
42 |
|
43 |
+
@app.post("/cut-audio/{output_key}")
|
44 |
+
def cut_audio(request: CutRequest, output_key: str):
|
45 |
+
download_files(output_key)
|
46 |
+
for i, (start, end) in enumerate(request.segments):
|
47 |
+
output_file = f"/tmp/genBase_segment_{i}.mp3"
|
48 |
+
try:
|
49 |
+
(
|
50 |
+
ffmpeg
|
51 |
+
.input('/tmp/genBase.mp3', ss=start, to=end)
|
52 |
+
.output(output_file)
|
53 |
+
.run()
|
54 |
+
)
|
55 |
+
except ffmpeg.Error as e:
|
56 |
+
raise HTTPException(status_code=500, detail=str(e))
|
57 |
+
return {"message": "Audio cut successfully"}
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
@app.post("/merge_audio/{output_key}")
|
62 |
+
async def merge_audio(output_key: str):
|
63 |
+
s3_directory = output_key
|
64 |
# Output file name
|
65 |
output_file = temp_dir + 'output.mp3'
|
66 |
|