shots record: gameplay clips from a web build (Chromium screencast), iOS Simulator, Android emulator or a folder

This commit is contained in:
CRTkafa 2026-10-10 14:22:52 +00:00
parent 630e933427
commit 37a32033c1
4 changed files with 319 additions and 4 deletions

View file

@ -6,7 +6,7 @@
// android-emu adb: demo-mode status bar, per-app language (Android 13+), am start with deep link / extras,
// exec-out screencap. Emulator or a USB device.
// folder bring your own frames: <frames>/<lang>/<phone|tablet>/<name>.png, checked for size.
import type { ShotsCapture, ShotSpec } from '../lib/config';
import type { ShotsCapture, ShotSpec, VideoRecord } from '../lib/config';
export type Source = 'web' | 'ios-sim' | 'android-emu' | 'folder';
export const SOURCES: Source[] = ['web', 'ios-sim', 'android-emu', 'folder'];
@ -50,6 +50,24 @@ export function validateCapture(c: ShotsCapture | undefined): string[] {
return e;
}
/** Problems with shots.video.record (pure). The web / simulator / emulator settings come from shots.capture. */
export function validateRecord(r: VideoRecord, cap: ShotsCapture | undefined): string[] {
const e: string[] = [];
const src = r.source ?? cap?.source ?? 'web';
if (!SOURCES.includes(src)) return [`shots.video.record.source: one of ${SOURCES.join(', ')}`];
if (r.device && r.device !== 'phone' && r.device !== 'tablet') e.push('shots.video.record.device: phone | tablet');
if (src === 'web' && !/^https?:\/\//.test(cap?.baseUrl ?? '')) e.push('shots.video.record: source web needs shots.capture.baseUrl');
if (src === 'ios-sim' && !cap?.ios?.bundleId) e.push('shots.video.record: source ios-sim needs shots.capture.ios (bundleId, device)');
if (src === 'android-emu' && !cap?.android?.package) e.push('shots.video.record: source android-emu needs shots.capture.android.package');
if (src !== 'folder' && !Object.keys(r.clips ?? {}).length) e.push('shots.video.record.clips: at least one clip');
for (const [name, c] of Object.entries(r.clips ?? {})) {
if (!/^[A-Za-z0-9._-]+$/.test(name)) e.push(`shots.video.record.clips.${name}: name must be a file name (letters, digits, . _ -)`);
if (!(typeof c.seconds === 'number' && c.seconds >= 1 && c.seconds <= 60)) e.push(`shots.video.record.clips.${name}.seconds: 1-60`);
if (src === 'web' && c.path === undefined) e.push(`shots.video.record.clips.${name}.path: URL path (source web)`);
}
return e;
}
/** Install hints for the external tools each source needs (shown when the tool is not on PATH). */
export const TOOL_HINTS: Record<string, string> = {
xcrun: 'xcrun not found: the iOS Simulator needs macOS with Xcode (App Store) and its command line tools (xcode-select --install)',
@ -165,6 +183,50 @@ export const adbScreencapArgs = (serial?: string): string[] => adb(serial, 'exec
/** screenrecord stops by itself after `seconds` (Android caps it at 180). */
export const adbRecordArgs = (remote: string, seconds: number, serial?: string): string[] => adb(serial, 'shell', 'screenrecord', '--time-limit', String(Math.min(180, Math.max(1, Math.round(seconds)))), remote);
export const ADB_REMOTE_CLIP = '/sdcard/storebound-clip.mp4';
export const adbPullArgs = (remote: string, local: string, serial?: string): string[] => adb(serial, 'pull', remote, local);
export const adbRmArgs = (remote: string, serial?: string): string[] => adb(serial, 'shell', 'rm', '-f', remote);
// ---- clips ----
/** Clip file name: config order first, so `shots video` joins them in that order: 01-intro.mp4. */
export const clipName = (i: number, name: string): string => `${String(i + 1).padStart(2, '0')}-${name}.mp4`;
/** ffmpeg argv: cut [start, start + seconds) out of a raw recording, constant 30 fps H.264 (yuv420p, even size), no
* audio (`shots video` adds music or a silent track). simctl recordVideo writes a frame only when the screen changes,
* so a still ending is padded with the last frame up to `seconds`. Pure. */
export const trimArgs = (inp: string, out: string, start: number, seconds: number, fps = 30): string[] =>
['-v', 'error', '-y', ...(start > 0 ? ['-ss', start.toFixed(3)] : []), '-i', inp, '-t', seconds.toFixed(3),
'-vf', `tpad=stop_mode=clone:stop_duration=${seconds.toFixed(3)},fps=${fps},scale=trunc(iw/2)*2:trunc(ih/2)*2,format=yuv420p`, '-c:v', 'libx264', '-preset', 'veryfast', '-crf', '16', '-an', '-movflags', '+faststart', out];
export interface ScreencastFrame { file: string; t: number }
/** ffmpeg concat-demuxer list for screencast frames (Chromium sends a frame only when the page changes): each frame lasts
* until the next one; the last one fills up to `seconds`. Pure. */
export function concatList(frames: ScreencastFrame[], seconds: number): string {
const t0 = frames[0]?.t ?? 0;
const q = (f: string) => `'${f.replaceAll("'", "'\\''")}'`;
const lines: string[] = [];
frames.forEach((f, k) => {
const next = frames[k + 1]?.t ?? t0 + Math.max(seconds, f.t - t0 + 1 / 30);
lines.push(`file ${q(f.file)}`, `duration ${Math.max(0.001, next - f.t).toFixed(4)}`);
});
if (frames.length) lines.push(`file ${q(frames.at(-1)!.file)}`);
return lines.join('\n') + '\n';
}
/** ffmpeg argv: a concat list of frames -> constant 30 fps H.264 clip of `seconds`. Pure. */
export const concatArgs = (list: string, out: string, seconds: number, fps = 30): string[] =>
['-v', 'error', '-y', '-f', 'concat', '-safe', '0', '-i', list, '-t', seconds.toFixed(3),
'-vf', `fps=${fps},scale=trunc(iw/2)*2:trunc(ih/2)*2,format=yuv420p`, '-c:v', 'libx264', '-preset', 'veryfast', '-crf', '16', '-an', '-movflags', '+faststart', out];
/** Screencast size for a frame: the frame at 2x (capped at 1600 px on the long side), even numbers. */
export function recordSize(d: { w: number; h: number }): { width: number; height: number } {
const k = Math.min(2, 1600 / Math.max(d.w, d.h));
const even = (n: number) => Math.max(2, Math.round(n * k / 2) * 2);
return { width: even(d.w), height: even(d.h) };
}
// ---- frames ----
/** Status bar rows to cut from a full-screen capture so the frame matches the template (which draws its own bar).