Files
Shade/scripts/make-live-fixtures.ts

59 lines
2.4 KiB
TypeScript
Raw Normal View History

/**
* Live-stream fixtures for the Android tests (`:shade-android-media`).
*
* Encodes a short H.264/AAC fragmented MP4 and a VP8/Opus WebM with ffmpeg,
* cuts both with the TS `MediaSplitter` — the same cut `shade live video`
* sends — and writes the pieces as `[u8 kind][u32 length][bytes]…`, kind 0 =
* init, 1 = keyframe piece, 2 = other piece. The Kotlin tests seal them into
* live frames and play them through ExoPlayer.
*
* Run: bun scripts/make-live-fixtures.ts
*/
import { writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { MediaSplitter } from '../packages/shade-media/src/splitter.js';
const OUT = join(import.meta.dir, '../android/shade-android-media/src/test/resources');
const SRC = [
'-f', 'lavfi', '-i', 'testsrc2=size=160x120:rate=15,format=yuv420p',
'-f', 'lavfi', '-i', 'sine=frequency=440:sample_rate=48000', '-t', '5',
];
async function ffmpeg(args: string[]): Promise<Uint8Array> {
const p = Bun.spawn(['ffmpeg', '-hide_banner', '-loglevel', 'error', ...SRC, ...args, 'pipe:1'], { stdout: 'pipe', stderr: 'inherit' });
const out = new Uint8Array(await new Response(p.stdout).arrayBuffer());
if ((await p.exited) !== 0) throw new Error('ffmpeg failed');
return out;
}
async function write(name: string, stream: Uint8Array): Promise<void> {
const s = new MediaSplitter();
const pieces = [...s.push(stream), ...s.finish()];
const parts: Uint8Array[] = [];
for (const p of pieces) {
const head = new Uint8Array(5);
head[0] = p.type === 'init' ? 0 : p.keyframe ? 1 : 2;
new DataView(head.buffer).setUint32(1, p.bytes.length);
parts.push(head, p.bytes);
}
await writeFile(join(OUT, name), Buffer.concat(parts));
const keys = pieces.filter((p) => p.type === 'media' && p.keyframe).length;
console.log(`${name}: ${pieces[0]?.type === 'init' ? pieces[0].mime : '?'}, ${pieces.length - 1} pieces, ${keys} keyframes`);
}
await write(
'live-h264.pieces',
await ffmpeg([
'-c:v', 'libx264', '-preset', 'veryfast', '-g', '15', '-keyint_min', '15', '-sc_threshold', '0', '-crf', '35',
'-c:a', 'aac', '-b:a', '32k',
'-f', 'mp4', '-movflags', 'frag_keyframe+empty_moov+default_base_moof', '-frag_duration', '250000',
]),
);
await write(
'live-vp8.pieces',
await ffmpeg([
'-c:v', 'libvpx', '-deadline', 'realtime', '-g', '15', '-b:v', '100k', '-c:a', 'libopus', '-b:a', '32k',
'-f', 'webm', '-live', '1', '-cluster_time_limit', '250',
]),
);