api: /v1/rbx Roblox transcode sessions (mpeg1video + mp2 MPEG-TS segments)

open/<n>.ts/status/close: one ffmpeg per session reads an API playback path
over loopback and writes 2 s closed-GOP segments (no B-frames, PAT/PMT +
sequence header per segment, pts = source time, X-Rbx-Pts). Paced with
SIGSTOP/SIGCONT 30 segments ahead of the highest request; old segments,
idle sessions and exits clean up. cfg.rbx; docs/api.md; node --test test/.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 07:42:22 -05:00
1 parent ee0e36d2c7
commit 68d4924a77
9 files changed
+877 -5

No files matched your search

+52 -1
View File
@@ -13,7 +13,7 @@ deviate, keep the deviation backwards compatible and note it at the bottom of th
## Server wiring (already written — `src/server.js`)
- Each `src/routes/<name>.js` exports `{ routes: [{ method, path, handler(req, res, ctx) }], init?(cfg) }`.
`path` is an exact string or a RegExp; capture groups → `ctx.params` (URI-decoded).
`ctx = { cfg, url, params }`. Loaded modules: `health providers streams proxy torrent remux subs openapi`.
`ctx = { cfg, url, params }`. Loaded modules: `health providers streams proxy torrent remux subs rbx openapi`.
- Helpers in `src/http.js`: `send(req,res,status,body,type,extraHeaders)` (auto br/gzip for text >1KB),
`json(req,res,status,obj)`, `error(req,res,status,msg,extra)`, `sse(req,res)` → `{event(name,data), close(), closed}`, `CORS` headers object.
- `src/sign.js`: `sign(obj) → token`, `verify(token) → obj|null` (HMAC, URL-safe, no `/`).
@@ -22,6 +22,7 @@ deviate, keep the deviation backwards compatible and note it at the bottom of th
{ host, port, tmdbKey, enabledProviders: [..] ([] = all), disabledProviders: [..] (default ["torrentio"]), providerSettings: {..} (→ global.SCRAPER_SETTINGS),
timeoutMs, workers (0 = auto, min(cpus,4)), cacheTtlSec, secret,
torrent: { enable, cacheDir, maxSizeGB }, remux: { enable, ffmpeg, ffprobe },
rbx: { enable, maxSessions, segment, idleSec, tmpDir },
filters: { minQuality, audioCodec, subtitles, subtitleLang } }
```
Config file path: `$BUG_TV_CONFIG` or `./config.json`.
@@ -97,6 +98,56 @@ Starts the scrape in the background to fill the cache (used for next episode).
### `GET /healthz` → `{ ok: true }`
### `GET /` Swagger UI, `GET /api.json` OpenAPI spec (`src/routes/openapi.js`).
## Roblox transcode (`/v1/rbx`)
For clients that can't play browser media: Roblox only gets whole HTTP bodies (server-side
HttpService) and decodes video in Luau. A session runs one ffmpeg that turns a playback path into
numbered MPEG-TS segments, **MPEG-1 video + MP2 audio** (the jsmpeg recipe), in a temp dir.
Code: `src/routes/rbx.js`, `src/media/rbx.js`. Tests: `node --test test/`.
### `GET /v1/rbx/open?src=&start=&w=&h=&fps=&vb=&ab=&audio=`
- `src` (required): an API-relative playback path — `/v1/p/<token>/…`, `/v1/remux/<token>` or
`/v1/torrent/<token>/stream` (a Stream's `playback.fallback` if present, else `playback.url` when relative).
ffmpeg reads it from this server over loopback, so upstream fetches keep the token + SSRF rules.
Absolute URLs or other paths → 400; a bad token signature → 403.
- `/v1/remux/…`: `start` and `audio` are passed to the remux (`?start=&audio=`). The remux starts at
the keyframe at/before `start`; that keyframe is looked up first and reported as `start`.
- `/v1/p/…`: ffmpeg input-seeks (`-ss` before `-i`); transcoding makes it frame accurate.
- `/v1/torrent/<token>/stream`: ffmpeg reads `/v1/torrent/<token>/raw` (range-served), seeking as above.
- Optional, clamped: `w` 32–640 (default 320), `h` 32–360 (180), both rounded to even; `fps` snapped to
the nearest MPEG-1 rate ≤ 30 (23.976, 24, 25, 29.97, 30; default 24); `vb` 64k–1500k (450k);
`ab` 32k–192k (96k), rounded down to an MP2 bitrate; `audio` = absolute ffprobe stream index.
- The source is probed first (≈1–8 s): a dead source fails here with 502 (403 for a blocked target).
`start` ≥ duration → 400. More than `cfg.rbx.maxSessions` sessions → 503 `{error, max}`.
- Reply: `{ id, segment, start, startExact, duration, width, height, fps, sampleRate: 44100, channels: 2,
videoBitrate, audioBitrate, audioTracks: [probe audio[]] }`. `start` is where segment 0 really begins
(source seconds); `startExact` is false only when a remux keyframe lookup failed and `start` is the
requested value. `duration` is the source duration or null.
### `GET /v1/rbx/<id>/<n>.ts`
Segment n (0-based), `video/mp2t`. Waits (up to 25 s) until ffmpeg has finished it.
- Header `X-Rbx-Pts`: the segment's first video frame in source seconds. The PES timestamps inside
the TS are source time too (`-output_ts_offset start`), so demuxed pts and `X-Rbx-Pts` agree.
- Each segment is independently decodable: PAT/PMT (after an SDT), then a sequence header + GOP header
+ I-frame, then P-frames only (no B-frames), `segment × fps` frames; a closed GOP per segment.
- 204 once ffmpeg finished and n is past the last segment; 502 `{error}` if ffmpeg failed instead;
504 if not ready after 25 s; 410 if n was discarded (more than 10 behind the highest n requested) or
the session closed meanwhile; 404 for an unknown session.
### `GET /v1/rbx/<id>/status`
`{ produced, ended, error, speed, paused, requested, start, duration, segment }` — `produced` = complete
segments, `speed` = media seconds encoded per wall second over the last ~1 s, `paused` = ffmpeg is
SIGSTOPped, `requested` = highest n requested (-1 = none).
### `GET /v1/rbx/<id>/close`
Kills ffmpeg and deletes the temp dir → `{ ok: true }` (404 if unknown).
Lifecycle: any request to a session keeps it alive; after `idleSec` (60) without one it is closed.
ffmpeg encodes as fast as it can but is paused (SIGSTOP) once 30 segments are ready beyond the highest
requested n and resumed (SIGCONT) at 24. Seeking = open a new session with `start=` (close the old one).
Server exit (SIGINT/SIGTERM/SIGHUP/normal exit) kills every session and removes its dir; stale dirs
are cleared at boot. Config: `rbx: { enable: true, maxSessions: 4, segment: 2, idleSec: 60, tmpDir: "" }`
(`tmpDir` "" = `<os tmp>/bug-tv-rbx`). ffmpeg (`cfg.remux.ffmpeg`) needs the `mpeg1video` and `mp2`
encoders and the `segment` muxer (all in standard builds, including nixpkgs `ffmpeg-headless`).
A remux source also counts against `cfg.remux.maxProcs`.
## Scrape internals (scrape workstream)
- Worker pool (`worker_threads`), providers `require()`d once per worker at boot.
- `global.SCRAPER_SETTINGS = cfg.providerSettings` in every worker.