Serve original uploaded file instead of WAV for audio playback
All checks were successful
Build and Push / build (push) Successful in 12m35s
All checks were successful
Build and Push / build (push) Successful in 12m35s
The audio endpoint was serving the extracted WAV (16kHz mono PCM) which balloons to ~1GB for a 3-hour session. Browsers cannot seek efficiently in such a large uncompressed file. Now the endpoint serves the original uploaded file (m4a, mp3, opus, etc.) — a compressed format the browser can seek via byte-range requests. Falls back to the WAV if the original is missing.
This commit is contained in:
@@ -60,15 +60,49 @@ app.include_router(files_router.router)
|
||||
app.include_router(campaigns_router.router)
|
||||
|
||||
|
||||
MEDIA_TYPES = {
|
||||
".mp3": "audio/mpeg",
|
||||
".mp4": "audio/mp4",
|
||||
".m4a": "audio/mp4",
|
||||
".m4b": "audio/mp4",
|
||||
".ogg": "audio/ogg",
|
||||
".opus": "audio/ogg",
|
||||
".wav": "audio/wav",
|
||||
".flac": "audio/flac",
|
||||
".aac": "audio/aac",
|
||||
".webm": "audio/webm",
|
||||
}
|
||||
|
||||
AUDIO_EXTS = frozenset(MEDIA_TYPES.keys())
|
||||
|
||||
|
||||
@app.get("/api/sessions/{session_id}/audio")
|
||||
def get_audio(session_id: str):
|
||||
row = db.get_conn().execute("SELECT audio_path FROM sessions WHERE id = ?", (session_id,)).fetchone()
|
||||
if not row or not row["audio_path"]:
|
||||
raise HTTPException(404, "Session not found or audio not available")
|
||||
audio_path = Path(row["audio_path"])
|
||||
if not audio_path.exists():
|
||||
raise HTTPException(404, "Audio not available for this session")
|
||||
return FileResponse(audio_path, media_type="audio/wav")
|
||||
row = db.get_conn().execute(
|
||||
"SELECT video_path, audio_path FROM sessions WHERE id = ?", (session_id,)
|
||||
).fetchone()
|
||||
if not row:
|
||||
raise HTTPException(404, "Session not found")
|
||||
|
||||
# Prefer the original uploaded file — it's a compressed format the browser
|
||||
# can seek in efficiently. Fall back to the extracted WAV.
|
||||
candidates = []
|
||||
if row["video_path"]:
|
||||
p = Path(row["video_path"])
|
||||
if p.exists():
|
||||
candidates.append(p)
|
||||
if row["audio_path"]:
|
||||
p = Path(row["audio_path"])
|
||||
if p.exists():
|
||||
candidates.append(p)
|
||||
|
||||
if not candidates:
|
||||
raise HTTPException(404, "No audio file available for this session")
|
||||
|
||||
chosen = candidates[0]
|
||||
ext = chosen.suffix.lower()
|
||||
media_type = MEDIA_TYPES.get(ext, "application/octet-stream")
|
||||
return FileResponse(chosen, media_type=media_type)
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
|
||||
Reference in New Issue
Block a user