Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,595 @@
|
|
1 |
import os
|
2 |
import uuid
|
3 |
-
import
|
4 |
import logging
|
5 |
-
|
6 |
-
from
|
7 |
-
from spleeter.separator import Separator
|
8 |
-
from pydub import AudioSegment
|
9 |
-
from starlette.middleware.cors import CORSMiddleware
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
TEMP_DIR =
|
16 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
17 |
|
18 |
-
#
|
19 |
logging.basicConfig(level=logging.INFO)
|
20 |
-
logger = logging.getLogger(
|
21 |
-
|
22 |
-
#
|
23 |
-
app
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
allow_headers=["*"],
|
28 |
)
|
29 |
|
30 |
-
# Helper
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
def cleanup_file(path: str):
|
39 |
try:
|
40 |
-
|
41 |
-
|
|
|
42 |
except Exception as e:
|
43 |
-
logger.error(f"
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
output_path = os.path.join(TEMP_DIR, f"{uuid.uuid4().hex}.{output_format}")
|
47 |
-
audio.export(output_path, format=output_format)
|
48 |
-
return output_path
|
49 |
|
50 |
-
#
|
51 |
-
@app.get("/", tags=["
|
52 |
def read_root():
|
53 |
-
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
async def remove_vocals(
|
58 |
background_tasks: BackgroundTasks,
|
59 |
-
file: UploadFile = File(..., description="Audio file
|
60 |
-
output_format: str = Form("mp3", description="Output format (mp3, wav, etc.)")
|
61 |
):
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
background_tasks.add_task(cleanup_file, input_path)
|
66 |
|
67 |
try:
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
71 |
|
72 |
-
# Use spleeter
|
73 |
-
separator = Separator("spleeter:2stems")
|
74 |
-
separator.separate_to_file(input_path, out_dir)
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
instrumental_path = os.path.join(out_dir, base_name, "accompaniment.wav")
|
79 |
|
80 |
-
|
81 |
-
|
82 |
|
83 |
-
|
84 |
-
instrumental_audio = AudioSegment.from_file(instrumental_path)
|
85 |
-
output_path = export_audio(instrumental_audio, output_format)
|
86 |
background_tasks.add_task(cleanup_file, output_path)
|
87 |
|
88 |
-
return FileResponse(
|
89 |
-
|
|
|
|
|
|
|
90 |
except Exception as e:
|
91 |
-
logger.error(f"Error: {e}", exc_info=True)
|
92 |
-
|
|
|
|
|
|
|
93 |
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import uuid
|
3 |
+
import tempfile
|
4 |
import logging
|
5 |
+
import math
|
6 |
+
from typing import List, Optional
|
|
|
|
|
|
|
7 |
|
8 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException, BackgroundTasks, Query
|
9 |
+
from fastapi.responses import FileResponse, JSONResponse
|
10 |
+
from pydub import AudioSegment
|
11 |
+
from pydub.exceptions import CouldntDecodeError
|
12 |
|
13 |
+
# --- Configuration & Setup ---
|
14 |
+
TEMP_DIR = tempfile.gettempdir()
|
15 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
16 |
|
17 |
+
# Configure logging
|
18 |
logging.basicConfig(level=logging.INFO)
|
19 |
+
logger = logging.getLogger(__name__)
|
20 |
+
|
21 |
+
# --- FastAPI App Initialization ---
|
22 |
+
app = FastAPI(
|
23 |
+
title="Enhanced Audio Editor API",
|
24 |
+
description="API for various audio editing tasks including trim, concat, volume, convert, fade, reverse, normalize, overlay, info, silence, speed. Requires FFmpeg.",
|
25 |
+
version="2.0.0",
|
|
|
26 |
)
|
27 |
|
28 |
+
# --- Helper Functions (Slightly Enhanced) ---
|
29 |
+
|
30 |
+
def cleanup_file(file_path: str):
|
31 |
+
"""Safely remove a file."""
|
32 |
+
try:
|
33 |
+
if os.path.exists(file_path):
|
34 |
+
os.remove(file_path)
|
35 |
+
logger.info(f"Cleaned up temporary file: {file_path}")
|
36 |
+
except Exception as e:
|
37 |
+
logger.error(f"Error cleaning up file {file_path}: {e}", exc_info=True)
|
38 |
+
|
39 |
+
async def save_upload_file(upload_file: UploadFile) -> str:
|
40 |
+
"""Saves an uploaded file to a temporary location and returns the path."""
|
41 |
+
file_extension = os.path.splitext(upload_file.filename)[1].lower() or '.tmp'
|
42 |
+
temp_file_path = os.path.join(TEMP_DIR, f"{uuid.uuid4().hex}{file_extension}")
|
43 |
+
try:
|
44 |
+
with open(temp_file_path, "wb") as buffer:
|
45 |
+
while content := await upload_file.read(1024 * 1024):
|
46 |
+
buffer.write(content)
|
47 |
+
logger.info(f"Saved uploaded file '{upload_file.filename}' to temp path: {temp_file_path}")
|
48 |
+
return temp_file_path
|
49 |
+
except Exception as e:
|
50 |
+
logger.error(f"Failed to save uploaded file {upload_file.filename}: {e}", exc_info=True)
|
51 |
+
cleanup_file(temp_file_path)
|
52 |
+
raise HTTPException(status_code=500, detail=f"Could not save uploaded file: {upload_file.filename}")
|
53 |
+
finally:
|
54 |
+
await upload_file.close()
|
55 |
+
|
56 |
+
def load_audio(file_path: str) -> AudioSegment:
|
57 |
+
"""Loads an audio file using pydub."""
|
58 |
+
try:
|
59 |
+
audio = AudioSegment.from_file(file_path)
|
60 |
+
logger.info(f"Loaded audio from: {file_path} ({len(audio)}ms)")
|
61 |
+
return audio
|
62 |
+
except CouldntDecodeError:
|
63 |
+
logger.warning(f"pydub couldn't decode file: {file_path}. Check format/corruption/FFmpeg.")
|
64 |
+
raise HTTPException(status_code=415, detail=f"Unsupported audio format or corrupted file: {os.path.basename(file_path)}. Ensure FFmpeg is correctly installed and supports the format.")
|
65 |
+
except FileNotFoundError:
|
66 |
+
logger.error(f"Audio file not found after saving: {file_path}")
|
67 |
+
raise HTTPException(status_code=500, detail="Internal error: Temporary audio file missing.")
|
68 |
+
except Exception as e:
|
69 |
+
logger.error(f"Error loading audio file {file_path}: {e}", exc_info=True)
|
70 |
+
raise HTTPException(status_code=500, detail=f"Error processing audio file: {os.path.basename(file_path)}")
|
71 |
+
|
72 |
+
def export_audio(audio: AudioSegment, format: str, bitrate: Optional[str] = None) -> str:
|
73 |
+
"""Exports an AudioSegment to a temporary file and returns the path. Supports bitrate."""
|
74 |
+
output_filename = f"edited_{uuid.uuid4().hex}.{format}"
|
75 |
+
output_path = os.path.join(TEMP_DIR, output_filename)
|
76 |
+
export_params = {}
|
77 |
+
if bitrate and format in ['mp3', 'ogg', 'aac', 'm4a']: # Add other formats if they support bitrate param in pydub/ffmpeg
|
78 |
+
export_params['bitrate'] = bitrate
|
79 |
+
logger.info(f"Using bitrate: {bitrate} for export.")
|
80 |
|
|
|
81 |
try:
|
82 |
+
logger.info(f"Exporting audio to format '{format}' at {output_path}")
|
83 |
+
audio.export(output_path, format=format, **export_params)
|
84 |
+
return output_path
|
85 |
except Exception as e:
|
86 |
+
logger.error(f"Error exporting audio to format {format} (bitrate: {bitrate}): {e}", exc_info=True)
|
87 |
+
cleanup_file(output_path)
|
88 |
+
raise HTTPException(status_code=500, detail=f"Failed to export audio to format '{format}'. Check format support and parameters.")
|
89 |
|
90 |
+
# --- API Endpoints ---
|
|
|
|
|
|
|
91 |
|
92 |
+
# --- General ---
|
93 |
+
@app.get("/", tags=["General"])
|
94 |
def read_root():
|
95 |
+
"""Root endpoint providing a welcome message."""
|
96 |
+
return {"message": "Welcome to the Enhanced Audio Editor API. Use POST requests to the specific editing endpoints."}
|
97 |
|
98 |
+
@app.post("/info", tags=["General"])
|
99 |
+
async def get_audio_info(
|
|
|
100 |
background_tasks: BackgroundTasks,
|
101 |
+
file: UploadFile = File(..., description="Audio file to analyze.")
|
|
|
102 |
):
|
103 |
+
"""Retrieves basic information about the uploaded audio file."""
|
104 |
+
logger.info(f"Info request: file='{file.filename}'")
|
105 |
+
input_path = await save_upload_file(file)
|
106 |
+
background_tasks.add_task(cleanup_file, input_path) # Schedule cleanup
|
107 |
+
|
108 |
+
try:
|
109 |
+
audio = load_audio(input_path)
|
110 |
+
info = {
|
111 |
+
"filename": file.filename,
|
112 |
+
"duration_ms": len(audio),
|
113 |
+
"duration_seconds": len(audio) / 1000.0,
|
114 |
+
"channels": audio.channels,
|
115 |
+
"sample_width_bytes": audio.sample_width,
|
116 |
+
"frame_rate_hz": audio.frame_rate,
|
117 |
+
"frame_count": audio.frame_count(),
|
118 |
+
"max_amplitude": audio.max, # Max sample value (peak)
|
119 |
+
"rms_amplitude": audio.rms, # Root Mean Square amplitude (average loudness)
|
120 |
+
"dBFS": audio.dBFS, # Peak amplitude in dBFS
|
121 |
+
}
|
122 |
+
logger.info(f"Audio info retrieved for '{file.filename}': {info}")
|
123 |
+
return JSONResponse(content=info)
|
124 |
+
except Exception as e:
|
125 |
+
logger.error(f"Error during info operation: {e}", exc_info=True)
|
126 |
+
if isinstance(e, HTTPException): raise e
|
127 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred while getting audio info: {str(e)}")
|
128 |
+
|
129 |
|
130 |
+
# --- Basic Editing ---
|
131 |
+
@app.post("/trim", tags=["Basic Editing"])
|
132 |
+
async def trim_audio(
|
133 |
+
background_tasks: BackgroundTasks,
|
134 |
+
file: UploadFile = File(..., description="Audio file to trim."),
|
135 |
+
start_ms: int = Form(..., description="Start time in milliseconds."),
|
136 |
+
end_ms: int = Form(..., description="End time in milliseconds.")
|
137 |
+
):
|
138 |
+
"""Trims an audio file to the specified start and end times (in milliseconds)."""
|
139 |
+
if start_ms < 0 or end_ms <= start_ms:
|
140 |
+
raise HTTPException(status_code=422, detail="Invalid start/end times. Ensure start_ms >= 0 and end_ms > start_ms.")
|
141 |
+
|
142 |
+
logger.info(f"Trim request: file='{file.filename}', start={start_ms}ms, end={end_ms}ms")
|
143 |
+
input_path = await save_upload_file(file)
|
144 |
background_tasks.add_task(cleanup_file, input_path)
|
145 |
|
146 |
try:
|
147 |
+
audio = load_audio(input_path)
|
148 |
+
if end_ms > len(audio):
|
149 |
+
logger.warning(f"End time ({end_ms}ms) exceeds audio duration ({len(audio)}ms). Trimming to end.")
|
150 |
+
end_ms = len(audio)
|
151 |
+
if start_ms >= len(audio):
|
152 |
+
raise HTTPException(status_code=422, detail=f"Start time ({start_ms}ms) is beyond audio duration ({len(audio)}ms).")
|
153 |
|
|
|
|
|
|
|
154 |
|
155 |
+
trimmed_audio = audio[start_ms:end_ms]
|
156 |
+
logger.info(f"Audio trimmed to {len(trimmed_audio)}ms")
|
|
|
157 |
|
158 |
+
original_format = os.path.splitext(file.filename)[1][1:].lower() or "mp3"
|
159 |
+
if not original_format or original_format == "tmp": original_format = "mp3"
|
160 |
|
161 |
+
output_path = export_audio(trimmed_audio, original_format)
|
|
|
|
|
162 |
background_tasks.add_task(cleanup_file, output_path)
|
163 |
|
164 |
+
return FileResponse(
|
165 |
+
path=output_path,
|
166 |
+
media_type=f"audio/{original_format}",
|
167 |
+
filename=f"trimmed_{start_ms}-{end_ms}ms_{file.filename}"
|
168 |
+
)
|
169 |
except Exception as e:
|
170 |
+
logger.error(f"Error during trim operation: {e}", exc_info=True)
|
171 |
+
if 'output_path' in locals() and os.path.exists(output_path): cleanup_file(output_path)
|
172 |
+
# Input cleanup is handled by background task unless saving failed earlier
|
173 |
+
if isinstance(e, HTTPException): raise e
|
174 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred during trimming: {str(e)}")
|
175 |
|
176 |
+
@app.post("/concat", tags=["Basic Editing"])
|
177 |
+
async def concatenate_audio(
|
178 |
+
background_tasks: BackgroundTasks,
|
179 |
+
files: List[UploadFile] = File(..., description="Two or more audio files to join in order."),
|
180 |
+
output_format: str = Form("mp3", description="Desired output format (e.g., 'mp3', 'wav', 'ogg')."),
|
181 |
+
crossfade_ms: int = Form(0, description="Duration of crossfade between segments in milliseconds (0 for no crossfade).")
|
182 |
+
):
|
183 |
+
"""Concatenates two or more audio files sequentially, optionally with crossfade."""
|
184 |
+
if len(files) < 2:
|
185 |
+
raise HTTPException(status_code=422, detail="Please upload at least two files to concatenate.")
|
186 |
+
if crossfade_ms < 0:
|
187 |
+
raise HTTPException(status_code=422, detail="Crossfade duration cannot be negative.")
|
188 |
+
|
189 |
+
logger.info(f"Concatenate request: {len(files)} files, format='{output_format}', crossfade={crossfade_ms}ms")
|
190 |
+
input_paths = []
|
191 |
+
loaded_audios = []
|
192 |
+
output_path = None # Define to allow cleanup in finally
|
193 |
+
|
194 |
+
try:
|
195 |
+
for file in files:
|
196 |
+
input_path = await save_upload_file(file)
|
197 |
+
input_paths.append(input_path)
|
198 |
+
background_tasks.add_task(cleanup_file, input_path)
|
199 |
+
audio = load_audio(input_path)
|
200 |
+
loaded_audios.append(audio)
|
201 |
+
|
202 |
+
if not loaded_audios:
|
203 |
+
raise HTTPException(status_code=500, detail="No audio segments were loaded successfully.")
|
204 |
+
|
205 |
+
combined_audio = loaded_audios[0]
|
206 |
+
logger.info(f"Starting concatenation with first segment ({len(combined_audio)}ms)")
|
207 |
+
for i in range(1, len(loaded_audios)):
|
208 |
+
logger.info(f"Adding segment {i+1} ({len(loaded_audios[i])}ms)")
|
209 |
+
# Use crossfade parameter if provided
|
210 |
+
combined_audio = combined_audio.append(loaded_audios[i], crossfade=crossfade_ms)
|
211 |
+
|
212 |
+
logger.info(f"Concatenated audio length: {len(combined_audio)}ms")
|
213 |
+
|
214 |
+
output_path = export_audio(combined_audio, output_format)
|
215 |
+
background_tasks.add_task(cleanup_file, output_path)
|
216 |
+
|
217 |
+
first_filename_base = os.path.splitext(files[0].filename)[0]
|
218 |
+
output_filename = f"concat_{first_filename_base}_and_{len(files)-1}_others.{output_format}"
|
219 |
+
|
220 |
+
return FileResponse(path=output_path, media_type=f"audio/{output_format}", filename=output_filename)
|
221 |
+
except Exception as e:
|
222 |
+
logger.error(f"Error during concat operation: {e}", exc_info=True)
|
223 |
+
# Cleanup output if it exists and error happened after export
|
224 |
+
if output_path and os.path.exists(output_path): cleanup_file(output_path)
|
225 |
+
# Input cleanup is handled by background tasks
|
226 |
+
if isinstance(e, HTTPException): raise e
|
227 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred during concatenation: {str(e)}")
|
228 |
+
|
229 |
+
@app.post("/volume", tags=["Basic Editing"])
|
230 |
+
async def change_volume(
|
231 |
+
background_tasks: BackgroundTasks,
|
232 |
+
file: UploadFile = File(..., description="Audio file to adjust volume for."),
|
233 |
+
change_db: float = Form(..., description="Volume change in decibels (dB). Positive increases, negative decreases.")
|
234 |
+
):
|
235 |
+
"""Adjusts the volume of an audio file by a specified decibel amount."""
|
236 |
+
logger.info(f"Volume request: file='{file.filename}', change_db={change_db}dB")
|
237 |
+
input_path = await save_upload_file(file)
|
238 |
+
background_tasks.add_task(cleanup_file, input_path)
|
239 |
+
output_path = None
|
240 |
+
|
241 |
+
try:
|
242 |
+
audio = load_audio(input_path)
|
243 |
+
adjusted_audio = audio + change_db
|
244 |
+
logger.info(f"Volume adjusted by {change_db}dB.")
|
245 |
+
|
246 |
+
original_format = os.path.splitext(file.filename)[1][1:].lower() or "mp3"
|
247 |
+
if not original_format or original_format == "tmp": original_format = "mp3"
|
248 |
+
|
249 |
+
output_path = export_audio(adjusted_audio, original_format)
|
250 |
+
background_tasks.add_task(cleanup_file, output_path)
|
251 |
+
|
252 |
+
return FileResponse(path=output_path, media_type=f"audio/{original_format}", filename=f"volume_{change_db}dB_{file.filename}")
|
253 |
+
except Exception as e:
|
254 |
+
logger.error(f"Error during volume operation: {e}", exc_info=True)
|
255 |
+
if output_path and os.path.exists(output_path): cleanup_file(output_path)
|
256 |
+
if isinstance(e, HTTPException): raise e
|
257 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred during volume adjustment: {str(e)}")
|
258 |
+
|
259 |
+
@app.post("/convert", tags=["Basic Editing"])
|
260 |
+
async def convert_format(
|
261 |
+
background_tasks: BackgroundTasks,
|
262 |
+
file: UploadFile = File(..., description="Audio file to convert."),
|
263 |
+
output_format: str = Form(..., description="Target audio format (e.g., 'mp3', 'wav', 'ogg', 'flac', 'aac')."),
|
264 |
+
bitrate: Optional[str] = Form(None, description="Target bitrate (e.g., '192k', '320k'). Only applicable for certain formats like MP3, OGG, AAC.")
|
265 |
+
):
|
266 |
+
"""Converts an audio file to a different format, optionally specifying bitrate."""
|
267 |
+
allowed_formats = {'mp3', 'wav', 'ogg', 'flac', 'aac', 'm4a', 'opus'} # Common formats
|
268 |
+
output_format = output_format.lower()
|
269 |
+
if output_format not in allowed_formats:
|
270 |
+
raise HTTPException(status_code=422, detail=f"Invalid output format '{output_format}'. Allowed: {', '.join(allowed_formats)}")
|
271 |
+
|
272 |
+
logger.info(f"Convert request: file='{file.filename}', format='{output_format}', bitrate='{bitrate}'")
|
273 |
+
input_path = await save_upload_file(file)
|
274 |
+
background_tasks.add_task(cleanup_file, input_path)
|
275 |
+
output_path = None
|
276 |
+
|
277 |
+
try:
|
278 |
+
audio = load_audio(input_path)
|
279 |
+
output_path = export_audio(audio, output_format, bitrate=bitrate)
|
280 |
+
background_tasks.add_task(cleanup_file, output_path)
|
281 |
+
|
282 |
+
filename_base = os.path.splitext(file.filename)[0]
|
283 |
+
output_filename = f"{filename_base}_converted.{output_format}"
|
284 |
+
|
285 |
+
return FileResponse(path=output_path, media_type=f"audio/{output_format}", filename=output_filename)
|
286 |
+
except Exception as e:
|
287 |
+
logger.error(f"Error during convert operation: {e}", exc_info=True)
|
288 |
+
if output_path and os.path.exists(output_path): cleanup_file(output_path)
|
289 |
+
if isinstance(e, HTTPException): raise e
|
290 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred during format conversion: {str(e)}")
|
291 |
+
|
292 |
+
# --- Effects & Advanced Editing ---
|
293 |
+
@app.post("/fade", tags=["Effects & Advanced"])
|
294 |
+
async def apply_fade(
|
295 |
+
background_tasks: BackgroundTasks,
|
296 |
+
file: UploadFile = File(..., description="Audio file to apply fade."),
|
297 |
+
fade_type: str = Form(..., description="Type of fade: 'in' or 'out'."),
|
298 |
+
duration_ms: int = Form(..., description="Duration of the fade in milliseconds.")
|
299 |
+
):
|
300 |
+
"""Applies a fade-in or fade-out effect to the audio."""
|
301 |
+
if fade_type not in ['in', 'out']:
|
302 |
+
raise HTTPException(status_code=422, detail="Invalid fade_type. Must be 'in' or 'out'.")
|
303 |
+
if duration_ms <= 0:
|
304 |
+
raise HTTPException(status_code=422, detail="Fade duration must be positive.")
|
305 |
+
|
306 |
+
logger.info(f"Fade request: file='{file.filename}', type='{fade_type}', duration={duration_ms}ms")
|
307 |
+
input_path = await save_upload_file(file)
|
308 |
+
background_tasks.add_task(cleanup_file, input_path)
|
309 |
+
output_path = None
|
310 |
+
|
311 |
+
try:
|
312 |
+
audio = load_audio(input_path)
|
313 |
+
if duration_ms > len(audio):
|
314 |
+
logger.warning(f"Fade duration ({duration_ms}ms) exceeds audio length ({len(audio)}ms). Clamping.")
|
315 |
+
duration_ms = len(audio)
|
316 |
+
|
317 |
+
if fade_type == 'in':
|
318 |
+
faded_audio = audio.fade_in(duration_ms)
|
319 |
+
else: # fade_type == 'out'
|
320 |
+
faded_audio = audio.fade_out(duration_ms)
|
321 |
+
logger.info(f"Fade-{fade_type} applied successfully.")
|
322 |
+
|
323 |
+
original_format = os.path.splitext(file.filename)[1][1:].lower() or "mp3"
|
324 |
+
if not original_format or original_format == "tmp": original_format = "mp3"
|
325 |
+
|
326 |
+
output_path = export_audio(faded_audio, original_format)
|
327 |
+
background_tasks.add_task(cleanup_file, output_path)
|
328 |
+
|
329 |
+
return FileResponse(path=output_path, media_type=f"audio/{original_format}", filename=f"fade_{fade_type}_{duration_ms}ms_{file.filename}")
|
330 |
+
except Exception as e:
|
331 |
+
logger.error(f"Error during fade operation: {e}", exc_info=True)
|
332 |
+
if output_path and os.path.exists(output_path): cleanup_file(output_path)
|
333 |
+
if isinstance(e, HTTPException): raise e
|
334 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred during fade: {str(e)}")
|
335 |
+
|
336 |
+
@app.post("/reverse", tags=["Effects & Advanced"])
|
337 |
+
async def reverse_audio(
|
338 |
+
background_tasks: BackgroundTasks,
|
339 |
+
file: UploadFile = File(..., description="Audio file to reverse.")
|
340 |
+
):
|
341 |
+
"""Reverses the audio playback."""
|
342 |
+
logger.info(f"Reverse request: file='{file.filename}'")
|
343 |
+
input_path = await save_upload_file(file)
|
344 |
+
background_tasks.add_task(cleanup_file, input_path)
|
345 |
+
output_path = None
|
346 |
+
|
347 |
+
try:
|
348 |
+
audio = load_audio(input_path)
|
349 |
+
reversed_audio = audio.reverse()
|
350 |
+
logger.info("Audio reversed successfully.")
|
351 |
+
|
352 |
+
original_format = os.path.splitext(file.filename)[1][1:].lower() or "mp3"
|
353 |
+
if not original_format or original_format == "tmp": original_format = "mp3"
|
354 |
+
|
355 |
+
output_path = export_audio(reversed_audio, original_format)
|
356 |
+
background_tasks.add_task(cleanup_file, output_path)
|
357 |
+
|
358 |
+
return FileResponse(path=output_path, media_type=f"audio/{original_format}", filename=f"reversed_{file.filename}")
|
359 |
+
except Exception as e:
|
360 |
+
logger.error(f"Error during reverse operation: {e}", exc_info=True)
|
361 |
+
if output_path and os.path.exists(output_path): cleanup_file(output_path)
|
362 |
+
if isinstance(e, HTTPException): raise e
|
363 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred during reverse: {str(e)}")
|
364 |
+
|
365 |
+
@app.post("/normalize", tags=["Effects & Advanced"])
|
366 |
+
async def normalize_audio(
|
367 |
+
background_tasks: BackgroundTasks,
|
368 |
+
file: UploadFile = File(..., description="Audio file to normalize."),
|
369 |
+
headroom_db: float = Form(0.1, description="Target peak amplitude headroom in dB below 0 dBFS. Default is 0.1dB.")
|
370 |
+
):
|
371 |
+
"""Normalizes the audio volume so the peak is at -headroom_db dBFS."""
|
372 |
+
if headroom_db < 0:
|
373 |
+
raise HTTPException(status_code=422, detail="Headroom must be non-negative.")
|
374 |
+
|
375 |
+
logger.info(f"Normalize request: file='{file.filename}', headroom={headroom_db}dB")
|
376 |
+
input_path = await save_upload_file(file)
|
377 |
+
background_tasks.add_task(cleanup_file, input_path)
|
378 |
+
output_path = None
|
379 |
+
|
380 |
+
try:
|
381 |
+
audio = load_audio(input_path)
|
382 |
+
normalized_audio = audio.normalize(headroom=headroom_db)
|
383 |
+
logger.info(f"Audio normalized with {headroom_db}dB headroom.")
|
384 |
+
|
385 |
+
original_format = os.path.splitext(file.filename)[1][1:].lower() or "mp3"
|
386 |
+
if not original_format or original_format == "tmp": original_format = "mp3"
|
387 |
+
|
388 |
+
output_path = export_audio(normalized_audio, original_format)
|
389 |
+
background_tasks.add_task(cleanup_file, output_path)
|
390 |
+
|
391 |
+
return FileResponse(path=output_path, media_type=f"audio/{original_format}", filename=f"normalized_{headroom_db}dB_{file.filename}")
|
392 |
+
except Exception as e:
|
393 |
+
logger.error(f"Error during normalize operation: {e}", exc_info=True)
|
394 |
+
if output_path and os.path.exists(output_path): cleanup_file(output_path)
|
395 |
+
if isinstance(e, HTTPException): raise e
|
396 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred during normalization: {str(e)}")
|
397 |
+
|
398 |
+
@app.post("/overlay", tags=["Effects & Advanced"])
|
399 |
+
async def overlay_audio(
|
400 |
+
background_tasks: BackgroundTasks,
|
401 |
+
file_base: UploadFile = File(..., description="The base audio track."),
|
402 |
+
file_overlay: UploadFile = File(..., description="The audio track to overlay."),
|
403 |
+
position_ms: int = Form(0, description="Position (in ms) in the base track where the overlay should start."),
|
404 |
+
# loop: bool = Form(False, description="Whether to loop the overlay track if it's shorter than needed."), # Pydub overlay doesn't directly support count-limited loop, only infinite or no loop
|
405 |
+
# times: int = Form(1, description="How many times to loop the overlay (if loop=True). 'inf' for infinite.") # See above note
|
406 |
+
gain_during_overlay: Optional[float] = Form(None, description="Volume change (dB) applied to the base track *during* the overlay. E.g., -6 to lower base volume.")
|
407 |
+
):
|
408 |
+
"""Overlays (mixes) one audio file onto another at a specific position."""
|
409 |
+
if position_ms < 0:
|
410 |
+
raise HTTPException(status_code=422, detail="Overlay position cannot be negative.")
|
411 |
+
|
412 |
+
logger.info(f"Overlay request: base='{file_base.filename}', overlay='{file_overlay.filename}', position={position_ms}ms, gain_during={gain_during_overlay}dB")
|
413 |
+
input_path_base = None
|
414 |
+
input_path_overlay = None
|
415 |
+
output_path = None
|
416 |
+
|
417 |
+
try:
|
418 |
+
input_path_base = await save_upload_file(file_base)
|
419 |
+
background_tasks.add_task(cleanup_file, input_path_base)
|
420 |
+
input_path_overlay = await save_upload_file(file_overlay)
|
421 |
+
background_tasks.add_task(cleanup_file, input_path_overlay)
|
422 |
+
|
423 |
+
audio_base = load_audio(input_path_base)
|
424 |
+
audio_overlay = load_audio(input_path_overlay)
|
425 |
+
|
426 |
+
# Note: pydub's gain_during_overlay is relative to the segment's *current* volume, not absolute dBFS.
|
427 |
+
# It applies the gain change only to the portion of the base track that overlaps with the overlay.
|
428 |
+
overlaid_audio = audio_base.overlay(
|
429 |
+
audio_overlay,
|
430 |
+
position=position_ms,
|
431 |
+
gain_during_overlay=gain_during_overlay if gain_during_overlay is not None else 0 # pydub needs a numerical value
|
432 |
+
)
|
433 |
+
logger.info("Overlay applied successfully.")
|
434 |
+
|
435 |
+
original_format = os.path.splitext(file_base.filename)[1][1:].lower() or "mp3"
|
436 |
+
if not original_format or original_format == "tmp": original_format = "mp3"
|
437 |
+
|
438 |
+
output_path = export_audio(overlaid_audio, original_format)
|
439 |
+
background_tasks.add_task(cleanup_file, output_path)
|
440 |
+
|
441 |
+
base_name = os.path.splitext(file_base.filename)[0]
|
442 |
+
overlay_name = os.path.splitext(file_overlay.filename)[0]
|
443 |
+
|
444 |
+
return FileResponse(path=output_path, media_type=f"audio/{original_format}", filename=f"overlay_{base_name}_with_{overlay_name}.{original_format}")
|
445 |
+
except Exception as e:
|
446 |
+
logger.error(f"Error during overlay operation: {e}", exc_info=True)
|
447 |
+
if output_path and os.path.exists(output_path): cleanup_file(output_path)
|
448 |
+
# Input cleanups handled by background tasks
|
449 |
+
if isinstance(e, HTTPException): raise e
|
450 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred during overlay: {str(e)}")
|
451 |
+
|
452 |
+
|
453 |
+
@app.post("/add-silence", tags=["Effects & Advanced"])
|
454 |
+
async def add_silence(
|
455 |
+
background_tasks: BackgroundTasks,
|
456 |
+
file: UploadFile = File(..., description="Audio file to add silence to."),
|
457 |
+
duration_ms: int = Form(..., description="Duration of silence in milliseconds."),
|
458 |
+
position: str = Form("end", description="Position to add silence: 'start' or 'end'.")
|
459 |
+
):
|
460 |
+
"""Adds a period of silence to the beginning or end of an audio file."""
|
461 |
+
if duration_ms <= 0:
|
462 |
+
raise HTTPException(status_code=422, detail="Silence duration must be positive.")
|
463 |
+
if position not in ['start', 'end']:
|
464 |
+
raise HTTPException(status_code=422, detail="Position must be 'start' or 'end'.")
|
465 |
+
|
466 |
+
logger.info(f"Add silence request: file='{file.filename}', duration={duration_ms}ms, position='{position}'")
|
467 |
+
input_path = await save_upload_file(file)
|
468 |
+
background_tasks.add_task(cleanup_file, input_path)
|
469 |
+
output_path = None
|
470 |
+
|
471 |
+
try:
|
472 |
+
audio = load_audio(input_path)
|
473 |
+
silence = AudioSegment.silent(duration=duration_ms, frame_rate=audio.frame_rate) # Match frame rate
|
474 |
+
|
475 |
+
if position == 'start':
|
476 |
+
modified_audio = silence + audio
|
477 |
+
else: # position == 'end'
|
478 |
+
modified_audio = audio + silence
|
479 |
+
logger.info(f"Silence added successfully to {position}.")
|
480 |
+
|
481 |
+
original_format = os.path.splitext(file.filename)[1][1:].lower() or "mp3"
|
482 |
+
if not original_format or original_format == "tmp": original_format = "mp3"
|
483 |
+
|
484 |
+
output_path = export_audio(modified_audio, original_format)
|
485 |
+
background_tasks.add_task(cleanup_file, output_path)
|
486 |
+
|
487 |
+
return FileResponse(path=output_path, media_type=f"audio/{original_format}", filename=f"silence_{position}_{duration_ms}ms_{file.filename}")
|
488 |
+
except Exception as e:
|
489 |
+
logger.error(f"Error during add silence operation: {e}", exc_info=True)
|
490 |
+
if output_path and os.path.exists(output_path): cleanup_file(output_path)
|
491 |
+
if isinstance(e, HTTPException): raise e
|
492 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred while adding silence: {str(e)}")
|
493 |
+
|
494 |
+
@app.post("/speedup", tags=["Effects & Advanced"])
|
495 |
+
async def change_speed(
|
496 |
+
background_tasks: BackgroundTasks,
|
497 |
+
file: UploadFile = File(..., description="Audio file to change speed of."),
|
498 |
+
playback_speed: float = Form(..., gt=0, description="Playback speed multiplier (e.g., 1.5 for 50% faster, 0.8 for 20% slower). Note: Affects pitch.")
|
499 |
+
):
|
500 |
+
"""Changes the playback speed of the audio. WARNING: This basic method also changes the pitch."""
|
501 |
+
if playback_speed <= 0:
|
502 |
+
raise HTTPException(status_code=422, detail="Playback speed must be positive.")
|
503 |
+
|
504 |
+
logger.info(f"Speed change request: file='{file.filename}', speed={playback_speed}x")
|
505 |
+
input_path = await save_upload_file(file)
|
506 |
+
background_tasks.add_task(cleanup_file, input_path)
|
507 |
+
output_path = None
|
508 |
+
|
509 |
+
try:
|
510 |
+
audio = load_audio(input_path)
|
511 |
+
# Pydub's speedup changes pitch. More complex methods exist for time-stretching without pitch shift (e.g., using rubberband via ffmpeg), but not directly in pydub.
|
512 |
+
# We need to manipulate the frame rate directly for speed changes *with* pitch shift
|
513 |
+
new_frame_rate = int(audio.frame_rate * playback_speed)
|
514 |
+
logger.info(f"Original frame rate: {audio.frame_rate}, New frame rate: {new_frame_rate}")
|
515 |
+
speed_changed_audio = audio._spawn(audio.raw_data, overrides={'frame_rate': new_frame_rate})
|
516 |
+
|
517 |
+
# Recalculate duration based on speed change
|
518 |
+
new_duration = len(audio) / playback_speed
|
519 |
+
logger.info(f"Speed changed by {playback_speed}x. New duration approx {new_duration:.2f}ms (pitch also changed).")
|
520 |
+
|
521 |
+
|
522 |
+
original_format = os.path.splitext(file.filename)[1][1:].lower() or "mp3"
|
523 |
+
if not original_format or original_format == "tmp": original_format = "mp3"
|
524 |
+
|
525 |
+
# Set frame rate back to original for export compatibility? Or keep changed rate?
|
526 |
+
# Keeping the changed rate reflects the speed change. Some players might handle it; others might play at the wrong speed/pitch if they ignore the rate.
|
527 |
+
# Let's try keeping the modified rate first.
|
528 |
+
output_path = export_audio(speed_changed_audio.set_frame_rate(new_frame_rate), original_format)
|
529 |
+
background_tasks.add_task(cleanup_file, output_path)
|
530 |
+
|
531 |
+
return FileResponse(path=output_path, media_type=f"audio/{original_format}", filename=f"speed_{playback_speed}x_{file.filename}")
|
532 |
+
except Exception as e:
|
533 |
+
logger.error(f"Error during speed change operation: {e}", exc_info=True)
|
534 |
+
if output_path and os.path.exists(output_path): cleanup_file(output_path)
|
535 |
+
if isinstance(e, HTTPException): raise e
|
536 |
+
else: raise HTTPException(status_code=500, detail=f"An unexpected error occurred during speed change: {str(e)}")
|
537 |
+
|
538 |
+
|
539 |
+
# --- How to Run ---
|
540 |
+
# 1. Ensure FFmpeg is installed and in PATH.
|
541 |
+
# 2. Save as `app.py`, create/update `requirements.txt`.
|
542 |
+
# 3. `pip install -r requirements.txt`
|
543 |
+
# 4. `uvicorn app:app --reload`
|
544 |
+
#
|
545 |
+
# --- Example Usage (New Endpoints with curl) ---
|
546 |
+
#
|
547 |
+
# **Fade In:** (Fade in input.wav over 500ms)
|
548 |
+
# curl -X POST "http://127.0.0.1:8000/fade" \
|
549 |
+
# -F "[email protected]" \
|
550 |
+
# -F "fade_type=in" \
|
551 |
+
# -F "duration_ms=500" \
|
552 |
+
# --output faded_in_output.wav
|
553 |
+
#
|
554 |
+
# **Reverse:** (Reverse input.mp3)
|
555 |
+
# curl -X POST "http://127.0.0.1:8000/reverse" \
|
556 |
+
# -F "[email protected]" \
|
557 |
+
# --output reversed_output.mp3
|
558 |
+
#
|
559 |
+
# **Normalize:** (Normalize input.ogg to peak at -0.5 dBFS)
|
560 |
+
# curl -X POST "http://127.0.0.1:8000/normalize" \
|
561 |
+
# -F "[email protected]" \
|
562 |
+
# -F "headroom_db=0.5" \
|
563 |
+
# --output normalized_output.ogg
|
564 |
+
#
|
565 |
+
# **Overlay:** (Overlay effect.wav onto base.mp3 starting at 2000ms)
|
566 |
+
# curl -X POST "http://127.0.0.1:8000/overlay" \
|
567 |
+
# -F "[email protected]" \
|
568 |
+
# -F "[email protected]" \
|
569 |
+
# -F "position_ms=2000" \
|
570 |
+
# --output overlay_output.mp3
|
571 |
+
#
|
572 |
+
# **Get Info:** (Get info about input.flac - returns JSON, not a file)
|
573 |
+
# curl -X POST "http://127.0.0.1:8000/info" \
|
574 |
+
# -F "[email protected]"
|
575 |
+
#
|
576 |
+
# **Add Silence:** (Add 1500ms silence to the start of input.m4a)
|
577 |
+
# curl -X POST "http://127.0.0.1:8000/add-silence" \
|
578 |
+
# -F "[email protected]" \
|
579 |
+
# -F "duration_ms=1500" \
|
580 |
+
# -F "position=start" \
|
581 |
+
# --output silence_start_output.m4a
|
582 |
+
#
|
583 |
+
# **Change Speed:** (Make input.wav play 50% faster - pitch will increase)
|
584 |
+
# curl -X POST "http://127.0.0.1:8000/speedup" \
|
585 |
+
# -F "[email protected]" \
|
586 |
+
# -F "playback_speed=1.5" \
|
587 |
+
# --output speed_1.5x_output.wav
|
588 |
+
#
|
589 |
+
# **Convert with Bitrate:** (Convert input.wav to MP3 at 192kbps)
|
590 |
+
# curl -X POST "http://127.0.0.1:8000/convert" \
|
591 |
+
# -F "[email protected]" \
|
592 |
+
# -F "output_format=mp3" \
|
593 |
+
# -F "bitrate=192k" \
|
594 |
+
# --output converted_192k_output.mp3
|
595 |
+
#
|