diff --git a/backend/app/main.py b/backend/app/main.py index 0fc1040..56e8e74 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -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")