feat(media): live video --input — a film, a webcam or an IP camera without writing the ffmpeg line

`shade live video` needed a hand-written ffmpeg pipeline. --input <file |
/dev/video0 | rtsp://…> probes the source and runs ffmpeg itself:
H.264 and AAC are copied (a camera's own encoder, no CPU), anything else
becomes H.264 with a keyframe every 2 s whatever the frame rate (where
late joiners start) and AAC (>6 channels downmixed to 5.1), out as
low-latency fragmented MP4. A file is paced (-re) as if live; RTSP goes
over TCP; /dev/videoN opens as v4l2. ffmpeg's failure is reported with
its own last lines.

Verified: liveEncodePlan unit tests (copy, encode, pacing, RTSP/v4l2,
audio-only, nothing-to-send); Chromium through the relay: VP9/Opus WebM
encoded → live H.264/AAC, keyframe every 4th 0.5 s fragment, 0.96 s
latency; H.264/AAC MKV copied, 1.8 s latency. Media suite 97/0.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 22:56:19 +02:00
parent d87db5f4dc
commit 0bbeb5682b
8 changed files with 140 additions and 7 deletions

View File

@@ -2,7 +2,8 @@ import { describe, test, expect, afterAll } from 'bun:test';
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { prepareForWeb, probe } from '../src/web-prep.js';
import { liveEncodePlan, prepareForWeb, probe } from '../src/web-prep.js';
import type { ProbeStream } from '../src/web-prep.js';
const hasFfmpeg = Bun.which('ffmpeg') !== null && Bun.which('ffprobe') !== null;
const dirs: string[] = [];
@@ -70,3 +71,37 @@ describe.skipIf(!hasFfmpeg)('prepareForWeb (media put --web)', () => {
expect((await probe(prep.file)).map((s) => s.codec_name)).toEqual(['h264', 'aac']);
}, 30_000);
});
describe('liveEncodePlan (live video --input)', () => {
const v = (codec: string): ProbeStream => ({ index: 0, codec_type: 'video', codec_name: codec });
const a = (codec: string, channels = 2): ProbeStream => ({ index: 1, codec_type: 'audio', codec_name: codec, channels });
const after = (args: string[], flag: string) => args[args.indexOf(flag) + 1];
test("a camera's H.264/AAC is copied; RTSP goes over TCP; not paced", () => {
const p = liveEncodePlan('rtsp://cam.local/stream1', [v('h264'), a('aac')], { realtime: false });
expect(after(p.args, '-c:v')).toBe('copy');
expect(after(p.args, '-c:a')).toBe('copy');
expect(after(p.args, '-rtsp_transport')).toBe('tcp');
expect(p.args).not.toContain('-re');
expect(p.args.slice(-1)).toEqual(['pipe:1']);
expect(after(p.args, '-movflags')).toBe('frag_keyframe+empty_moov+default_base_moof');
});
test('a film in HEVC with 7.1 DTS is encoded, paced, with a keyframe every 2 s', () => {
const p = liveEncodePlan('/films/x.mkv', [v('hevc'), a('dts', 8), { index: 2, codec_type: 'subtitle', codec_name: 'subrip' }], { realtime: true });
expect(p.args).toContain('-re');
expect(after(p.args, '-c:v')).toBe('libx264');
expect(after(p.args, '-force_key_frames')).toBe('expr:gte(t,n_forced*2)');
expect(after(p.args, '-c:a')).toBe('aac');
expect(after(p.args, '-ac')).toBe('6');
expect(p.args).toContain('-sn');
expect(p.notes).toEqual(['video hevc → H.264 (a keyframe every 2 s)', 'audio dts → AAC']);
});
test('a webcam device is opened as v4l2; sound alone is fine; nothing at all is refused', () => {
expect(after(liveEncodePlan('/dev/video0', [v('mjpeg')], { realtime: false }).args, '-f')).toBe('v4l2');
const radio = liveEncodePlan('http://radio.example/stream.mp3', [a('mp3')], { realtime: false });
expect(radio.args).not.toContain('-c:v');
expect(() => liveEncodePlan('x', [], { realtime: false })).toThrow(/no audio or video/);
});
});