# Live capture

Capture a talk **while it happens**: Audiobly transcribes the audio in real time, lets an operator name each voice as it first speaks, keeps a recording, and — once the talk ends — replaces the live transcript with a more accurate full-audio pass. This guide is for an integrator building an operator tool or wiring a venue's AV feed into Audiobly.

- **Base URL:** `https://api.audiobly.com` — all paths are under `/v1`.
- **Auth:** `Authorization: Bearer <API_KEY>`. Reading a capture needs any valid key; every action that changes one needs the `event_edit` permission (unscoped keys have it).
- One capture belongs to one programme **session** (one talk in one room). Parallel rooms run one capture each.
- Live captions and translation for the audience are a separate feature: see *Live translation*.

---

## How it fits together

1. **Provision** a capture for the session.
2. **Send audio** in one of two ways:
   - Let Audiobly **pull** a stream address you give it (RTMP, RTMPS, SRT, HLS or an https media URL).
   - Build your **own capture client** that streams audio and posts transcript segments (advanced).
3. **Operate** while it runs: watch the transcript, name the voices, pause and resume, flag lines for the editors.
4. **End** the capture. The live transcript becomes the session's transcript straight away, and the recording's full-audio transcription replaces it when ready.

### Capture status

| `status` | Meaning |
|---|---|
| `awaiting_stream` | provisioned, no audio yet |
| `live` | audio arriving, transcript updating |
| `paused` | the operator paused (an off-the-record passage, walk-on music) |
| `reconnecting` | nothing has arrived for about 45 seconds without a pause (the laptop dropped or the feed stopped). The talk stays live and attendees see "the room is reconnecting"; it goes back to `live` by itself when lines arrive again |
| `ended` | stopped; the live transcript has been saved |
| `recorded` | the recording has been stored |
| `failed` | the capture could not run |

---

## Step 1 — Provision a capture

`POST /v1/events/{EVENT_ID}/live`

```bash
curl -sX POST https://api.audiobly.com/v1/events/$EVENT_ID/live \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"session_id": "'$SESSION_ID'"}'
# → 201 { "id": "…", "ingest_url": "rtmp://…/<stream_key>", "stream_key": "…", "status": "awaiting_stream" }
```

- **Always pass `session_id`.** Omitting it creates an event-level capture (one per event, and only before the event has any audio).
- The response also carries `ingest_url` and `stream_key`, reserved for pushing a stream to Audiobly. Push is not available yet; use pull (Step 2a) or your own client (Step 2b).
- `409 CONFLICT` — a capture already exists for this session (or, without `session_id`, the event already has audio).
- `402 LIVE_MINUTES_EXCEEDED` — only if your contract sets a monthly live-minutes cap and it is reached.

Check a capture at any time with `GET /v1/events/{EVENT_ID}/live?session_id={SESSION_ID}` → `id`, `session_id`, `status`, `ingest_url`, `started_at`, `ended_at`, `minutes_used`, `minutes_cap` (`0` = no cap). Without `session_id`, the event's most recent capture is returned. `404` if there is no capture.

---

## Step 2 — Send audio

### 2a. Let Audiobly pull a stream

```bash
curl -sX PUT "https://api.audiobly.com/v1/events/$EVENT_ID/live/stream-source?session_id=$SESSION_ID" \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"url": "srt://encoder.venue.example:9000"}'
# → { "url": "srt://…", "state": "connecting", "seconds": null, "error": null }
```

The address must start with `rtmp://`, `rtmps://`, `srt://`, `http://` or `https://` (HLS or a media URL) and resolve to a **public** host; otherwise `422 INVALID_STREAM`. `409 CONFLICT` if the capture has already ended.

`GET /v1/events/{EVENT_ID}/live/stream-source?session_id=…` reports the stream's `state` (`connecting`, `live`, `ended`, `error`), how many `seconds` have been taken, and any `error`. `DELETE` on the same path stops taking audio from the stream; the capture ends and the recording is transcribed.

**With pull, Audiobly records the stream itself.** When the stream ends, the recording is stored on the session and the full-audio transcription starts automatically — no upload needed.

### 2b. Your own capture client (advanced)

If you capture audio yourself (for example from a soundboard laptop), you stream it to the speech service and post the recognised text to Audiobly:

1. `POST /v1/events/{EVENT_ID}/live/token?session_id=…&audio=pcm16k` → `{access_token, expires_in, ws_url, params, keepalive_interval_seconds}`. Open a WebSocket to `ws_url` with `params` as query parameters and `access_token` as the `access_token` query parameter; send raw 16-bit mono PCM at 16 kHz (`audio=pcm16k`). The token is short-lived (seconds) — it only has to be valid when the socket opens. `params` already carries the event's language and keyterms. Send a keep-alive at the given interval during silences. `502 TOKEN_MINT_FAILED` — retry.
2. **Go live** with `POST /v1/events/{EVENT_ID}/live/claim?session_id=…`. It makes this device the capture's only writer and returns `{"writer": "…", "next_seq": 41, "took_over": false}`. Send the token in the `X-Capture-Writer` header on every later call. A second Go live (or a reload) continues numbering from `next_seq` instead of overwriting the first device's lines, and the earlier writer is switched off: its next write gets `409 CAPTURE_TAKEN_OVER`. `took_over` says whether that earlier writer was still sending.
3. Send `POST /v1/events/{EVENT_ID}/live/heartbeat?session_id=…` every 15 seconds while live, even during a quiet minute. Segments count as a heartbeat too. A capture with neither for 45 seconds shows as `reconnecting`; a paused capture keeps its status.
4. Post each result to `POST /v1/events/{EVENT_ID}/live/segments?session_id=…` (**`session_id` is required**):

```json
{ "segments": [
  { "seq": 41, "speaker": "Speaker 0", "text": "Welcome to day two.", "start": 312.4, "end": 313.9, "is_final": true }
] }
```

| Field | Notes |
|---|---|
| `seq` | **required** — your running sequence number for the capture. A segment with the same `seq` replaces the earlier one, so a final replaces its interim. After a pause, continue from the `next_seq` that resume returns. |
| `text` | **required** |
| `speaker` | the diarization label, e.g. `Speaker 0`. After a dropped streaming connection the next connection's voices are stored in their own block of labels (`Speaker 100`, `Speaker 200`…), so names given before the drop are not applied to new voices. Name the new labels with `PUT …/live/speakers`. |
| `start`, `end` | seconds from the start of the capture |
| `is_final` | `false` for a still-forming line |
| `gap_before` | `true` if audio was lost just before this segment |
| `channel` | input channel, `0` for a mixed venue feed |

Returns `202 {"status": "accepted", "count": n}`. `409 CONFLICT` once the capture has ended.

5. At the end, upload the recording (Step 4b).

---

## Step 3 — Operate the capture

All of these take `?session_id=` — required on the actions, optional on the reads (which then use the event's most recent capture).

### Watch the transcript

`GET /v1/events/{EVENT_ID}/live/transcript?session_id=…`

```json
{
  "is_live": true, "status": "live",
  "segments": [ { "seq": 40, "speaker": "Speaker 0", "speaker_name": "Ada Lovelace",
                  "text": "…", "start_time": 305.1, "end_time": 311.8, "gap_before": false } ],
  "interim": { "seq": 41, "speaker": "Speaker 0", "speaker_name": "Ada Lovelace", "text": "Welcome to", … }
}
```

`segments` are the final lines in order (spans marked as non-speech are left out); `interim` is the line still forming. Poll every one to two seconds.

### Name the voices

The venue feed is a single mixed signal, so speaker identity comes from the operator: name a voice the first time it speaks, and the name is applied **backwards** to everything that voice already said and **forwards** for the rest of the talk.

- `GET /v1/events/{EVENT_ID}/live/speakers?session_id=…` → `entries[]` (`label`, `speaker_id`, `display_name`, `merged_into`) and `heard[]` (labels heard so far, in order of first appearance).
- `PUT /v1/events/{EVENT_ID}/live/speakers?session_id=…` with **either** `{"label": "Speaker 1", "speaker_id": "<roster speaker>"}` to name a voice, **or** `{"label": "Speaker 3", "merged_into": "Speaker 1"}` when two labels are the same person. Returns the map plus `segments_updated`. The speaker must be on the event's roster (`404` otherwise); `400` if you send both fields or merge a label into itself; `409` once the capture has ended (edit the transcript instead).

### Mark music or video playout

`POST /v1/events/{EVENT_ID}/live/non-speech?session_id=…` with `{"from_seq": 120, "to_seq": 134}` excludes that span from the transcript (send `"is_non_speech": false` to undo). The span is kept in the record, not deleted. Returns `{"segments_updated": n}`.

### Pause and resume

- `POST /v1/events/{EVENT_ID}/live/pause?session_id=…` → `{"status": "paused"}`. Viewers see "paused" rather than a fault. Idempotent.
- `POST /v1/events/{EVENT_ID}/live/resume?session_id=…` → `{"status": "live", "next_seq": 135}`. A custom client must continue numbering from `next_seq`.
- Both return `409 CONFLICT` once the capture has ended — an ended capture cannot be reopened.

### Flag lines for the editors

Anyone watching can flag a line for the people who will review the transcript later. Notes are anchored to the transcript's clock, so they survive the full-audio replacement.

| Action | Endpoint |
|---|---|
| List flags (all authors, in transcript order) | `GET /v1/events/{EVENT_ID}/live/notes?session_id=…` |
| Flag a line | `POST /v1/events/{EVENT_ID}/live/notes` with `{"session_id": "…", "at_seconds": 312.4, "body": "Name is Nextida, not Next Ida", "quote": "next ida", "speaker": "Speaker 0", "category": "spelling"}` (`at_seconds` and `body` required) |
| Edit or resolve | `PATCH /v1/events/{EVENT_ID}/live/notes/{NOTE_ID}` with `body`, `category` and/or `"resolved": true` |
| Undo an automatic correction | `POST /v1/events/{EVENT_ID}/live/notes/{NOTE_ID}/undo` (`409 NOT_APPLIED` if the flag had none) |
| Delete a flag raised by mistake | `DELETE /v1/events/{EVENT_ID}/live/notes/{NOTE_ID}` (204) |

Each note returns `id`, `at_seconds`, `quote`, `speaker`, `body`, `category`, `created_by_name`, `resolved_at`, and — when a correction was applied automatically — `applied_at`, `applied_before`, `applied_after`.

---

## Step 4 — End the capture

### 4a. Stop

`POST /v1/events/{EVENT_ID}/live/end?session_id=…` (**`session_id` required**)

```bash
curl -sX POST "https://api.audiobly.com/v1/events/$EVENT_ID/live/end?session_id=$SESSION_ID" \
  -H "Authorization: Bearer $KEY"
# → { "status": "ended", "already_ended": false }
```

The live transcript is saved as the session's transcript, so editors can open it in Review straight away. Idempotent — a second call returns `already_ended: true`. Optional `reason` (`operator` or `left-page`) and `hidden_seconds` are logged for troubleshooting.

For a **pulled** stream, `DELETE …/live/stream-source` ends it too.

### 4b. Hand over the recording (custom clients)

A pulled stream records itself. A custom client uploads its own recording to the session with the normal upload flow, forcing a multipart upload so it can upload while recording:

1. `POST /v1/uploads` with `session_id`, `filename`, `content_type` (e.g. `audio/webm`), the expected `file_size` and `part_bytes` → a multipart plan.
2. Upload the parts and call `POST /v1/uploads/multipart/{upload_session_id}/complete` (see *Create a complete event via the API*, Step 7).
3. **Then call `POST /v1/events/{EVENT_ID}/live/recording?session_id=…`** — not the ordinary upload-complete call. It keeps the recording and starts the full-audio transcription without first discarding the live transcript:

```json
{ "status": "stored", "transcribing": true }
```

`transcribing` is `false` when the session has already been approved or is being edited — the recording is kept, and the live transcript an editor has worked on is not replaced. `409 NO_RECORDING` if no recording has been stored for the session yet. Idempotent.

When the full-audio transcription lands it replaces the live transcript; the speaker names and editor flags carry across.

---

## Errors

| Status | Code | When |
|---|---|---|
| `400` | `INVALID_REQUEST` | conflicting speaker assignment |
| `402` | `LIVE_MINUTES_EXCEEDED` | contractual live-minutes cap reached |
| `404` | `NOT_FOUND` | no capture for this event/session, or session/speaker/note not found |
| `409` | `CONFLICT` | capture already provisioned, or already ended |
| `409` | `CAPTURE_TAKEN_OVER` | another device went live on the same session and is now its writer |
| `409` | `NO_RECORDING` | `/live/recording` before any recording was stored |
| `422` | `INVALID_STREAM` | stream address has the wrong scheme or isn't public |
| `502` | `TOKEN_MINT_FAILED` | streaming credential could not be issued — retry |
