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:
@@ -6,7 +6,7 @@
|
||||
* shade media get <link> -o out.mkv download and decrypt
|
||||
* shade media ls|rm --server URL
|
||||
* tail -f app.log | shade live text --server URL --name "app logs"
|
||||
* ffmpeg … -f webm - | shade live webm --server URL --name "Camera"
|
||||
* ffmpeg … -f webm - | shade live video --server URL --name "Camera" (or fragmented MP4)
|
||||
* shade live watch <link> a live text channel in the terminal
|
||||
*
|
||||
* The owner key (who may upload and delete) is created on first use under
|
||||
@@ -26,7 +26,8 @@ import { LivePublishClient, LiveWatchClient } from './live/client.js';
|
||||
import { sealFile } from './node.js';
|
||||
import { OwnerKey } from './owner.js';
|
||||
import type { MediaMeta } from './sealed.js';
|
||||
import { WebmSplitter } from './webm.js';
|
||||
import { MediaSplitter } from './splitter.js';
|
||||
import type { LiveContainer } from './splitter.js';
|
||||
|
||||
type Flags = Record<string, string | true>;
|
||||
|
||||
@@ -297,25 +298,46 @@ async function liveText(flags: Flags): Promise<number> {
|
||||
return 0;
|
||||
}
|
||||
|
||||
async function liveWebm(flags: Flags): Promise<number> {
|
||||
const meta: MediaMeta = { name: str(flags, 'name') ?? 'Live', mime: str(flags, 'mime') ?? 'video/webm' };
|
||||
/**
|
||||
* A live WebM or fragmented-MP4 stream from stdin. `--format webm|fmp4` pins
|
||||
* the container (the `webm` subcommand pins WebM); otherwise the first bytes
|
||||
* decide.
|
||||
*/
|
||||
async function liveVideo(flags: Flags, pinned?: LiveContainer): Promise<number> {
|
||||
const format = pinned ?? str(flags, 'format');
|
||||
if (format !== undefined && format !== 'webm' && format !== 'fmp4') throw new UsageError('--format must be webm or fmp4');
|
||||
const splitter = new MediaSplitter(format);
|
||||
const stdin = Bun.stdin.stream().getReader();
|
||||
// Read until the container is known, so the meta can carry the right type.
|
||||
let first: ReturnType<MediaSplitter['push']> = [];
|
||||
while (splitter.container === null) {
|
||||
const { value, done } = await stdin.read();
|
||||
if (done) throw new UsageError('no input on stdin');
|
||||
first = first.concat(splitter.push(value));
|
||||
}
|
||||
const fallbackMime = splitter.container === 'webm' ? 'video/webm' : 'video/mp4';
|
||||
const meta: MediaMeta = { name: str(flags, 'name') ?? 'Live', mime: str(flags, 'mime') ?? fallbackMime };
|
||||
const { client } = await startPublishing(flags, meta);
|
||||
const splitter = new WebmSplitter();
|
||||
let clusters = 0;
|
||||
const send = async (pieces: ReturnType<WebmSplitter['push']>) => {
|
||||
for (const p of pieces) {
|
||||
let pieces = 0;
|
||||
const send = async (batch: ReturnType<MediaSplitter['push']>) => {
|
||||
for (const p of batch) {
|
||||
if (p.type === 'init') {
|
||||
await client.send({ type: 'init', mime: p.mime, bytes: p.bytes }, { sticky: true });
|
||||
err(`stream: ${p.mime}`);
|
||||
} else {
|
||||
await client.send({ type: 'chunk', bytes: p.bytes }, { keyframe: p.keyframe });
|
||||
clusters++;
|
||||
pieces++;
|
||||
}
|
||||
}
|
||||
};
|
||||
for await (const chunk of Bun.stdin.stream()) await send(splitter.push(chunk));
|
||||
await send(first);
|
||||
for (;;) {
|
||||
const { value, done } = await stdin.read();
|
||||
if (done) break;
|
||||
await send(splitter.push(value));
|
||||
}
|
||||
await send(splitter.finish());
|
||||
err(`${clusters} clusters sent`);
|
||||
err(`${pieces} ${splitter.container === 'webm' ? 'clusters' : 'fragments'} sent`);
|
||||
await client.end();
|
||||
return 0;
|
||||
}
|
||||
@@ -357,8 +379,12 @@ export const MEDIA_HELP = `
|
||||
media rm <link|id> --server URL
|
||||
live text --server URL [--name N] [--channel file.json]
|
||||
Publish stdin as a live log (tail -f app.log | shade live text …)
|
||||
live webm --server URL [--name N] [--channel file.json]
|
||||
Publish a WebM stream from stdin (ffmpeg … -f webm - | …)
|
||||
live video --server URL [--name N] [--channel file.json] [--format webm|fmp4]
|
||||
Publish a live WebM or fragmented MP4 from stdin:
|
||||
ffmpeg … -f webm - | …
|
||||
ffmpeg … -c:v libx264 -c:a aac -f mp4 \\
|
||||
-movflags frag_keyframe+empty_moov+default_base_moof - | …
|
||||
live webm … The same, pinned to WebM
|
||||
live watch <link> Follow a live text channel in the terminal
|
||||
`;
|
||||
|
||||
@@ -387,8 +413,10 @@ export async function runMediaCli(group: 'media' | 'live', argv: string[]): Prom
|
||||
switch (sub) {
|
||||
case 'text':
|
||||
return await liveText(flags);
|
||||
case 'video':
|
||||
return await liveVideo(flags);
|
||||
case 'webm':
|
||||
return await liveWebm(flags);
|
||||
return await liveVideo(flags, 'webm');
|
||||
case 'watch':
|
||||
return await liveWatch(pos);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user