feat(media): live fragmented MP4 (H.264/HEVC/AV1 + AAC/Opus), shade live video

Fmp4Splitter cuts ftyp+moov into the sticky init and moof+mdat into frames,
reading sync samples from trun/tfhd/trex flags so fragments shorter than a
GOP still mark where late joiners may start. Codec strings come from the
sample entries. MediaSplitter sniffs WebM vs fMP4; the CLI's new
`live video` uses it (`live webm` stays as the pinned form).

Verified: 9 new tests on real ffmpeg output; H.264/AAC live in headless
Chromium (49/49 fragments appended, late join, 3.2 s latency) and a sealed
H.264 film seeking to 24 s.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 21:27:50 +02:00
parent c5b897b924
commit 75dbf530ce
12 changed files with 710 additions and 26 deletions

View File

@@ -35,12 +35,22 @@ shade media get '<link>' -o Film.mkv
tail -f /var/log/app.log | shade live text --server https://relay.example --name "api logs" --channel ~/.config/shade/api-logs.json
shade live watch '<link>' # in a terminal — or open the link in a browser
# Live video (and audio) from anything ffmpeg can read.
# Live video (and audio) from anything ffmpeg can read: WebM (VP8/VP9/AV1 + Opus) …
ffmpeg -re -i camera.mp4 -c:v libvpx -deadline realtime -g 30 -c:a libopus \
-f webm -live 1 -cluster_time_limit 1000 - \
| shade live webm --server https://relay.example --name "Kamera 1"
| shade live video --server https://relay.example --name "Kamera 1"
# … or fragmented MP4 (H.264/HEVC/AV1 + AAC/Opus), what cameras, OBS and Safari speak.
ffmpeg -re -i camera.mp4 -c:v libx264 -tune zerolatency -g 30 -c:a aac \
-f mp4 -movflags frag_keyframe+empty_moov+default_base_moof -frag_duration 250000 - \
| shade live video --server https://relay.example --name "Kamera 1"
```
`shade live video` recognises the container from the first bytes (`--format webm|fmp4`
pins it). A fragment is a starting point for late joiners when the first sample of its
video track is a sync sample, so fragments shorter than a GOP keep latency down without
breaking late joining.
`--channel <file>` keeps a live channel's identity across restarts: the link keeps
working, and each run starts a new epoch.
@@ -97,7 +107,8 @@ new LiveWatchClient(grant, { onFrame: (f) => console.log(f.content) });
In a page, `installMediaWorker('/sw.js')` registers the decrypting worker, and
`keyring.add(grant)` returns a URL to put in `<video src>`. `new LivePlayer(video, grant)`
plays a live WebM channel. The relay serves a ready-made page at `/watch/` that does
plays a live WebM or fragmented-MP4 channel; `MediaSplitter` cuts either container into
the frames a publisher sends (init once, sticky; then one frame per cluster or fragment). The relay serves a ready-made page at `/watch/` that does
both from a link.
On Android/JVM, `no.zyon.shade.media`: `Grants.fromCapabilityUrl(link)`,
@@ -247,7 +258,8 @@ done in v1. A deployment that needs it can pad above this layer.
(flipped bytes, swapped segments, forged table, truncation), live chains (late
join, reordering, replay, forged frames from a key holder, rotation lock-out, link
mode across restarts), the HTTP routes and relay over real sockets, and the WebM
splitter on real ffmpeg output.
and fMP4 splitters on real ffmpeg output (keyframes inside a GOP, codec strings
for H.264, HEVC, AV1, VP9, AAC and Opus, CMAF `styp`/`prft` prefixes).
- `test-vectors/media.json` (`bun run scripts/generate-vectors.ts media.json`): the
Kotlin port reads every vector and reproduces every sealed file and signed frame
byte for byte (`MediaVectorTest`).
@@ -273,6 +285,12 @@ done in v1. A deployment that needs it can pad above this layer.
interleaved video frame. The splitter sorts blocks within each cluster (stable, so
each track keeps its order). The cluster keeps its size and every block keeps its
bytes.
- **fMP4 keyframes.** The splitter reads sync-ness the way MSE does:
`trun` first-sample flags, else per-sample flags, else `tfhd` defaults, else the
`trex` defaults from the init. The codec string comes from the sample entry
(`avcC`, `hvcC`, `av1C`, `vpcC`, `esds`), so the browser is asked for exactly what
the encoder wrote. CENC-encrypted tracks (`encv`/`enca`) are refused; Shade already
encrypts, and a second DRM layer would leave the viewer without keys.
- **Bun 1.3.5.** `new Response(Bun.file(p).slice(a, b))` sent the whole file in
answer to a Range request when the file was large. The store therefore reads the
range itself.