@clipy/cli 0.17.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-APACHE +202 -0
- package/README.md +265 -12
- package/dist/chromeForClipy.js +1 -1
- package/dist/context/importCmd.js +4 -1
- package/dist/context/readCmd.js +33 -21
- package/dist/context/ytdlp.js +108 -28
- package/dist/context-core/arec.js +66 -6
- package/dist/index.js +1180 -98
- package/dist/library.js +7 -3
- package/dist/local/arec.js +186 -0
- package/dist/local/commands.js +1103 -0
- package/dist/local/deps.js +42 -0
- package/dist/local/media.js +154 -0
- package/dist/local/pipeline.js +530 -0
- package/dist/local/ports.js +1 -0
- package/dist/local/speechEngine.js +260 -0
- package/dist/local/transcriptFile.js +54 -0
- package/dist/local/vendor/capture/capture.js +442 -0
- package/dist/local/vendor/capture/check.js +103 -0
- package/dist/local/vendor/capture/children.js +69 -0
- package/dist/local/vendor/capture/ffmpegArgs.js +108 -0
- package/dist/local/vendor/capture/index.js +36 -0
- package/dist/local/vendor/capture/proc.js +71 -0
- package/dist/local/vendor/capture/scripts.js +107 -0
- package/dist/local/vendor/capture/sidecar.js +199 -0
- package/dist/local/vendor/capture/sources.js +272 -0
- package/dist/local/vendor/capture/types.js +1 -0
- package/dist/local/vendor/capture/virtualDisplay.js +170 -0
- package/dist/local/vendor/capture/xauth.js +68 -0
- package/dist/local/vendor/config/errors.js +12 -0
- package/dist/local/vendor/config/index.js +241 -0
- package/dist/local/vendor/config/paths.js +37 -0
- package/dist/local/vendor/config/presets.js +61 -0
- package/dist/local/vendor/config/schema.js +66 -0
- package/dist/local/vendor/moments/generated/drawStrokes.js +127 -0
- package/dist/local/vendor/moments/generated/keyMomentsCursorContext.js +568 -0
- package/dist/local/vendor/moments/generated/keyMomentsFusion.js +406 -0
- package/dist/local/vendor/moments/generated/localMomentPlan.js +178 -0
- package/dist/local/vendor/moments/generated/types.js +1 -0
- package/dist/local/vendor/moments/index.js +147 -0
- package/dist/local/vendor/moments/llm.js +158 -0
- package/dist/local/vendor/transcribe/audio.js +251 -0
- package/dist/local/vendor/transcribe/chunking.js +250 -0
- package/dist/local/vendor/transcribe/index.js +89 -0
- package/dist/local/vendor/transcribe/local.js +221 -0
- package/dist/local/vendor/transcribe/localWorker.js +79 -0
- package/dist/local/vendor/transcribe/models.js +369 -0
- package/dist/local/vendor/transcribe/providers/assemblyai.js +61 -0
- package/dist/local/vendor/transcribe/providers/common.js +105 -0
- package/dist/local/vendor/transcribe/providers/deepgram.js +59 -0
- package/dist/local/vendor/transcribe/providers/elevenlabs.js +43 -0
- package/dist/local/vendor/transcribe/providers/gemini.js +102 -0
- package/dist/local/vendor/transcribe/providers/http.js +214 -0
- package/dist/local/vendor/transcribe/providers/openai.js +48 -0
- package/dist/local/vendor/transcribe/tar.js +53 -0
- package/dist/local/vendor/transcribe/types.js +1 -0
- package/dist/macBridge.js +78 -9
- package/dist/recheck.js +934 -0
- package/dist/recheckCmd.js +279 -0
- package/dist/skill.js +94 -3
- package/package.json +22 -11
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// GENERATED from oss/packages/capture/src/capture.ts by cli/scripts/sync-local.mjs. Do not edit here.
|
|
3
|
+
/**
|
|
4
|
+
* startCapture: one ffmpeg process recording a display (or, on Linux, a
|
|
5
|
+
* window) into a fragmented MP4, plus the pointer sidecar next to it.
|
|
6
|
+
*
|
|
7
|
+
* Stopping writes "q" to ffmpeg's stdin so the file finalizes; if ffmpeg does
|
|
8
|
+
* not exit, SIGINT and then SIGKILL follow. Because the MP4 is fragmented
|
|
9
|
+
* (moov first, a fragment per keyframe), even a killed ffmpeg leaves a
|
|
10
|
+
* playable file up to its last fragment.
|
|
11
|
+
*/
|
|
12
|
+
import { spawn } from "node:child_process";
|
|
13
|
+
import { mkdirSync, rmSync, statSync } from "node:fs";
|
|
14
|
+
import { dirname, resolve } from "node:path";
|
|
15
|
+
import { isWaylandSession, hasDevice, SCREEN_RECORDING_HINT } from "./check.js";
|
|
16
|
+
import { trackChild } from "./children.js";
|
|
17
|
+
import { xEnv } from "./xauth.js";
|
|
18
|
+
import { buildFfmpegArgs, pickH264Encoder } from "./ffmpegArgs.js";
|
|
19
|
+
import { currentPlatform, ffmpegPath, ffprobePath, realRunner, sleep } from "./proc.js";
|
|
20
|
+
import { samplesToEvents, startPointerSampler } from "./sidecar.js";
|
|
21
|
+
import { avfoundationScreens, linuxScreens, listAvfoundation, listDshowAudio, parseSourceId, parseWindowGeometry, primaryFirst, windowsScreens, } from "./sources.js";
|
|
22
|
+
/** How long ffmpeg may take to open its inputs (a macOS permission prompt counts). */
|
|
23
|
+
const START_TIMEOUT_MS = 20_000;
|
|
24
|
+
/** After it starts, how long to watch for an immediate failure. */
|
|
25
|
+
const SETTLE_MS = 600;
|
|
26
|
+
const BANNER = /Press \[q\] to stop/;
|
|
27
|
+
/**
|
|
28
|
+
* Patterns ffmpeg prints when macOS has not granted Screen Recording or
|
|
29
|
+
* Microphone. Without the Screen Recording grant, recent macOS lists no
|
|
30
|
+
* "Capture screen" devices at all, so opening one by name fails as "Video
|
|
31
|
+
* device not found".
|
|
32
|
+
*/
|
|
33
|
+
export function looksLikeMacPermissionError(stderr) {
|
|
34
|
+
if (/Video device not found[\s\S]*Capture screen \d/i.test(stderr))
|
|
35
|
+
return true;
|
|
36
|
+
return /not (?:been )?(?:authorized|permitted)|Operation not permitted|Failed to create AV capture input device|Could not create (?:video|audio) capture|kTCC|privacy/i.test(stderr);
|
|
37
|
+
}
|
|
38
|
+
function tail(text, lines = 8) {
|
|
39
|
+
return text.trim().split(/\r?\n/).filter((l) => !/^\s*(?:Input|Output|Stream|Metadata|Duration|encoder|handler_name|vendor_id|major_brand|minor_version|compatible_brands|Side data|cpb:|Press \[q\])/.test(l)).slice(-lines).join("\n");
|
|
40
|
+
}
|
|
41
|
+
class StartError extends Error {
|
|
42
|
+
stderr;
|
|
43
|
+
constructor(message, stderr) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.stderr = stderr;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Spawn ffmpeg and wait until it is recording (or has failed). */
|
|
49
|
+
async function launch(ffmpeg, args, env) {
|
|
50
|
+
const child = trackChild(spawn(ffmpeg, args, { env, stdio: ["pipe", "ignore", "pipe"], windowsHide: true }));
|
|
51
|
+
let stderr = "";
|
|
52
|
+
let bannerAt = null;
|
|
53
|
+
let onBanner = () => { };
|
|
54
|
+
const banner = new Promise((r) => {
|
|
55
|
+
onBanner = r;
|
|
56
|
+
});
|
|
57
|
+
child.stderr.on("data", (chunk) => {
|
|
58
|
+
stderr += chunk.toString("utf8");
|
|
59
|
+
if (stderr.length > 32_768)
|
|
60
|
+
stderr = stderr.slice(-16_384);
|
|
61
|
+
if (bannerAt === null && BANNER.test(stderr)) {
|
|
62
|
+
bannerAt = Date.now();
|
|
63
|
+
onBanner();
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
const exited = new Promise((r) => {
|
|
67
|
+
child.on("exit", (code, signal) => r({ code, signal, at: Date.now() }));
|
|
68
|
+
child.on("error", () => r({ code: null, signal: null, at: Date.now() }));
|
|
69
|
+
});
|
|
70
|
+
// ffmpeg reads "q" from stdin; a broken pipe after it exits is harmless.
|
|
71
|
+
child.stdin.on("error", () => { });
|
|
72
|
+
// A cleared timer, not sleep(): a pending 20 s sleep would keep the process
|
|
73
|
+
// alive that long after a short recording has finished.
|
|
74
|
+
let startTimer;
|
|
75
|
+
const timeout = new Promise((r) => {
|
|
76
|
+
startTimer = setTimeout(() => r("timeout"), START_TIMEOUT_MS);
|
|
77
|
+
});
|
|
78
|
+
const first = await Promise.race([banner.then(() => "banner"), exited.then(() => "exit"), timeout]);
|
|
79
|
+
clearTimeout(startTimer);
|
|
80
|
+
if (first === "exit")
|
|
81
|
+
throw new StartError("ffmpeg stopped before recording started", stderr);
|
|
82
|
+
if (first === "timeout") {
|
|
83
|
+
child.kill("SIGKILL");
|
|
84
|
+
throw new StartError(`ffmpeg did not start recording within ${START_TIMEOUT_MS / 1000} s`, stderr);
|
|
85
|
+
}
|
|
86
|
+
const settled = await Promise.race([exited.then(() => "exit"), sleep(SETTLE_MS).then(() => "ok")]);
|
|
87
|
+
if (settled === "exit")
|
|
88
|
+
throw new StartError("ffmpeg stopped right after it started", stderr);
|
|
89
|
+
return { child, startedAt: bannerAt ?? Date.now(), exited, stderr: () => stderr };
|
|
90
|
+
}
|
|
91
|
+
/** Ask ffmpeg to finish its file ("q"), then insist. */
|
|
92
|
+
async function stopFfmpeg(l, graceMs = 5000) {
|
|
93
|
+
if (l.child.exitCode !== null || l.child.signalCode !== null)
|
|
94
|
+
return;
|
|
95
|
+
try {
|
|
96
|
+
l.child.stdin.write("q");
|
|
97
|
+
l.child.stdin.end();
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
// stdin already closed
|
|
101
|
+
}
|
|
102
|
+
const int = setTimeout(() => l.child.kill("SIGINT"), graceMs);
|
|
103
|
+
const kill = setTimeout(() => l.child.kill("SIGKILL"), graceMs * 2);
|
|
104
|
+
await l.exited;
|
|
105
|
+
clearTimeout(int);
|
|
106
|
+
clearTimeout(kill);
|
|
107
|
+
}
|
|
108
|
+
export function parseProbeJson(json) {
|
|
109
|
+
try {
|
|
110
|
+
const data = JSON.parse(json);
|
|
111
|
+
const video = data.streams?.find((s) => s.codec_type === "video");
|
|
112
|
+
const seconds = data.format?.duration !== undefined ? Number(data.format.duration) : NaN;
|
|
113
|
+
return {
|
|
114
|
+
durationMs: Number.isFinite(seconds) ? Math.round(seconds * 1000) : null,
|
|
115
|
+
width: video?.width ?? null,
|
|
116
|
+
height: video?.height ?? null,
|
|
117
|
+
codec: video?.codec_name ?? null,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return { durationMs: null, width: null, height: null, codec: null };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
export async function probe(path, run = realRunner, env = process.env) {
|
|
125
|
+
const ffprobe = ffprobePath(currentPlatform(), env);
|
|
126
|
+
if (!ffprobe)
|
|
127
|
+
return null;
|
|
128
|
+
// file: keeps a name such as take2:final.mp4 from being read as a protocol.
|
|
129
|
+
const input = `file:${resolve(path)}`;
|
|
130
|
+
const r = await run(ffprobe, ["-v", "error", "-show_entries", "format=duration:stream=codec_type,codec_name,width,height", "-of", "json", input], env);
|
|
131
|
+
return r.status === 0 ? parseProbeJson(r.stdout) : null;
|
|
132
|
+
}
|
|
133
|
+
const encoderCache = new Map();
|
|
134
|
+
async function ffmpegFeatures(ffmpeg, platform, env) {
|
|
135
|
+
const cached = encoderCache.get(ffmpeg);
|
|
136
|
+
if (cached)
|
|
137
|
+
return cached;
|
|
138
|
+
const [encoders, devices] = await Promise.all([
|
|
139
|
+
realRunner(ffmpeg, ["-hide_banner", "-encoders"], env),
|
|
140
|
+
realRunner(ffmpeg, ["-hide_banner", "-devices"], env),
|
|
141
|
+
]);
|
|
142
|
+
const features = { encoder: pickH264Encoder(encoders.stdout + encoders.stderr, platform), devices: devices.stdout + devices.stderr };
|
|
143
|
+
encoderCache.set(ffmpeg, features);
|
|
144
|
+
return features;
|
|
145
|
+
}
|
|
146
|
+
async function resolveLinux(options, base, devices, sampler, env) {
|
|
147
|
+
const warnings = [];
|
|
148
|
+
const xDisplay = options.xDisplay ?? env.DISPLAY;
|
|
149
|
+
if (!xDisplay) {
|
|
150
|
+
throw new Error(isWaylandSession(env)
|
|
151
|
+
? "this is a Wayland session without XWayland (DISPLAY is unset): ffmpeg cannot record it. Log in to an X11 session, enable XWayland, or record a virtual display."
|
|
152
|
+
: "no X display to record: DISPLAY is unset. Pass xDisplay or start a virtual display.");
|
|
153
|
+
}
|
|
154
|
+
if (isWaylandSession(env) && !options.xDisplay)
|
|
155
|
+
warnings.push("Wayland session: native Wayland windows record as black; only XWayland apps appear.");
|
|
156
|
+
const source = parseSourceId(options.source);
|
|
157
|
+
let video;
|
|
158
|
+
let geometry = null;
|
|
159
|
+
if (source.kind === "window") {
|
|
160
|
+
video = { backend: "x11grab", xDisplay, windowId: source.id };
|
|
161
|
+
const info = await realRunner("xwininfo", ["-id", `0x${source.id.toString(16)}`], xEnv(env, xDisplay));
|
|
162
|
+
const g = info.status === 0 ? parseWindowGeometry(info.stdout) : null;
|
|
163
|
+
if (g)
|
|
164
|
+
geometry = { x: g.x, y: g.y, width: g.width, height: g.height };
|
|
165
|
+
else
|
|
166
|
+
warnings.push("window geometry is unknown (install x11-utils for xwininfo), so pointer positions cannot be placed on the video");
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
const screens = await linuxScreens(xDisplay, realRunner, env);
|
|
170
|
+
if (source.index >= Math.max(1, screens.length)) {
|
|
171
|
+
throw new Error(`display:${source.index} does not exist on ${xDisplay} (${screens.length || 1} display${screens.length === 1 ? "" : "s"})`);
|
|
172
|
+
}
|
|
173
|
+
const screen = screens[source.index];
|
|
174
|
+
// A single display that is the whole X screen records without a crop.
|
|
175
|
+
const whole = screens.length <= 1 && screen && screen.x === 0 && screen.y === 0;
|
|
176
|
+
video = { backend: "x11grab", xDisplay, rect: whole || !screen ? undefined : screen };
|
|
177
|
+
geometry = screen ? { x: screen.x, y: screen.y, width: screen.width, height: screen.height } : null;
|
|
178
|
+
if (!geometry) {
|
|
179
|
+
await sampler.ready;
|
|
180
|
+
const s = sampler.screens()[0];
|
|
181
|
+
if (s)
|
|
182
|
+
geometry = { x: 0, y: 0, width: s.width, height: s.height };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
let audio;
|
|
186
|
+
if (options.microphone) {
|
|
187
|
+
const device = typeof options.microphone === "string" ? options.microphone : "default";
|
|
188
|
+
if (hasDevice(devices, "pulse"))
|
|
189
|
+
audio = { backend: "pulse", device };
|
|
190
|
+
else if (hasDevice(devices, "alsa"))
|
|
191
|
+
audio = { backend: "alsa", device };
|
|
192
|
+
else
|
|
193
|
+
warnings.push("this ffmpeg has neither pulse nor alsa input; recording without the microphone");
|
|
194
|
+
}
|
|
195
|
+
return { plans: [{ plan: { ...base, video, audio }, label: "x11grab" }], geometry, warnings, windowCapture: source.kind === "window" };
|
|
196
|
+
}
|
|
197
|
+
async function resolveMac(options, base, sampler, env) {
|
|
198
|
+
const warnings = [];
|
|
199
|
+
const source = parseSourceId(options.source);
|
|
200
|
+
if (source.kind === "window")
|
|
201
|
+
throw new Error("macOS capture records whole displays only; pick a display:N source");
|
|
202
|
+
const devices = await listAvfoundation(realRunner, env);
|
|
203
|
+
const screens = devices ? avfoundationScreens(devices) : [];
|
|
204
|
+
let device;
|
|
205
|
+
if (screens.length > 0) {
|
|
206
|
+
const screen = screens[source.index];
|
|
207
|
+
if (!screen)
|
|
208
|
+
throw new Error(`display:${source.index} does not exist (${screens.length} screen${screens.length === 1 ? "" : "s"})`);
|
|
209
|
+
device = String(screen.index);
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
device = `Capture screen ${source.index}`;
|
|
213
|
+
warnings.push("ffmpeg listed no screens; trying the screen by name. " + SCREEN_RECORDING_HINT);
|
|
214
|
+
}
|
|
215
|
+
let audio;
|
|
216
|
+
if (options.microphone) {
|
|
217
|
+
if (typeof options.microphone === "string") {
|
|
218
|
+
const want = options.microphone.toLowerCase();
|
|
219
|
+
const match = devices?.audio.find((d) => d.name.toLowerCase() === want) ?? devices?.audio.find((d) => d.name.toLowerCase().includes(want));
|
|
220
|
+
if (match)
|
|
221
|
+
audio = { backend: "avfoundation", device: String(match.index) };
|
|
222
|
+
else if (/^\d+$/.test(options.microphone))
|
|
223
|
+
audio = { backend: "avfoundation", device: options.microphone };
|
|
224
|
+
else {
|
|
225
|
+
const names = devices?.audio.map((d) => `"${d.name}"`).join(", ") || "none listed";
|
|
226
|
+
throw new Error(`no microphone named "${options.microphone}" (available: ${names})`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
audio = { backend: "avfoundation", device: "default" };
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
await sampler.ready;
|
|
234
|
+
const s = sampler.screens()[source.index];
|
|
235
|
+
const geometry = s ? { x: s.x, y: s.y, width: s.width, height: s.height } : null;
|
|
236
|
+
return { plans: [{ plan: { ...base, video: { backend: "avfoundation", device }, audio }, label: "avfoundation" }], geometry, warnings, windowCapture: false };
|
|
237
|
+
}
|
|
238
|
+
async function resolveWindows(options, base, devices, sampler, env) {
|
|
239
|
+
const warnings = [];
|
|
240
|
+
const source = parseSourceId(options.source);
|
|
241
|
+
if (source.kind === "window")
|
|
242
|
+
throw new Error("Windows capture records whole displays only; pick a display:N source");
|
|
243
|
+
await sampler.ready;
|
|
244
|
+
let screens = primaryFirst(sampler.screens());
|
|
245
|
+
if (screens.length === 0)
|
|
246
|
+
screens = await windowsScreens(realRunner, env);
|
|
247
|
+
if (screens.length > 0 && !screens[source.index]) {
|
|
248
|
+
throw new Error(`display:${source.index} does not exist (${screens.length} display${screens.length === 1 ? "" : "s"})`);
|
|
249
|
+
}
|
|
250
|
+
const screen = screens[source.index] ?? null;
|
|
251
|
+
let audio;
|
|
252
|
+
if (options.microphone) {
|
|
253
|
+
if (!hasDevice(devices, "dshow")) {
|
|
254
|
+
warnings.push("this ffmpeg has no dshow input; recording without the microphone");
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
const names = await listDshowAudio(realRunner, env);
|
|
258
|
+
if (typeof options.microphone === "string") {
|
|
259
|
+
const want = options.microphone.toLowerCase();
|
|
260
|
+
const match = names.find((n) => n.toLowerCase() === want) ?? names.find((n) => n.toLowerCase().includes(want));
|
|
261
|
+
if (!match)
|
|
262
|
+
throw new Error(`no microphone named "${options.microphone}" (available: ${names.map((n) => `"${n}"`).join(", ") || "none listed"})`);
|
|
263
|
+
audio = { backend: "dshow", device: match };
|
|
264
|
+
}
|
|
265
|
+
else if (names[0]) {
|
|
266
|
+
audio = { backend: "dshow", device: names[0] };
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
warnings.push("no microphone was found; recording without it");
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const geometry = screen ? { x: screen.x, y: screen.y, width: screen.width, height: screen.height } : null;
|
|
274
|
+
return {
|
|
275
|
+
plans: [
|
|
276
|
+
{ plan: { ...base, video: { backend: "ddagrab", outputIndex: source.index }, audio }, label: "ddagrab" },
|
|
277
|
+
{ plan: { ...base, video: { backend: "gdigrab", rect: screen ?? undefined }, audio }, label: "gdigrab" },
|
|
278
|
+
],
|
|
279
|
+
geometry,
|
|
280
|
+
warnings,
|
|
281
|
+
windowCapture: false,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
function statOrNull(path) {
|
|
285
|
+
try {
|
|
286
|
+
return statSync(path);
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* After a capture that never started: remove the output only if this capture
|
|
294
|
+
* wrote it (absent before, or changed since). A file the user already had
|
|
295
|
+
* there and ffmpeg never opened is left alone.
|
|
296
|
+
*/
|
|
297
|
+
export function removeFailedOutput(path, before) {
|
|
298
|
+
const after = statOrNull(path);
|
|
299
|
+
if (!after)
|
|
300
|
+
return;
|
|
301
|
+
if (before && after.size === before.size && after.mtimeMs === before.mtimeMs && after.ino === before.ino)
|
|
302
|
+
return;
|
|
303
|
+
rmSync(path, { force: true });
|
|
304
|
+
}
|
|
305
|
+
export async function startCapture(options, internal = {}) {
|
|
306
|
+
const platform = currentPlatform();
|
|
307
|
+
const env = internal.env ?? process.env;
|
|
308
|
+
const ffmpeg = ffmpegPath(platform, env);
|
|
309
|
+
if (!ffmpeg)
|
|
310
|
+
throw new Error("ffmpeg was not found on PATH (run checkCapture for install steps)");
|
|
311
|
+
const fps = options.fps ?? 30;
|
|
312
|
+
if (!Number.isFinite(fps) || fps < 1 || fps > 120)
|
|
313
|
+
throw new Error(`fps must be between 1 and 120 (got ${fps})`);
|
|
314
|
+
if (options.maxSeconds !== undefined && !(options.maxSeconds > 0))
|
|
315
|
+
throw new Error("maxSeconds must be positive");
|
|
316
|
+
const outPath = resolve(options.outPath);
|
|
317
|
+
mkdirSync(dirname(outPath), { recursive: true });
|
|
318
|
+
const features = await ffmpegFeatures(ffmpeg, platform, env);
|
|
319
|
+
if (!features.encoder)
|
|
320
|
+
throw new Error("this ffmpeg has no H.264 encoder (libx264); run checkCapture for install steps");
|
|
321
|
+
const base = { fps, encoder: features.encoder, maxSeconds: options.maxSeconds, output: outPath };
|
|
322
|
+
const xDisplay = options.xDisplay ?? env.DISPLAY;
|
|
323
|
+
const sampler = startPointerSampler(platform, { xDisplay, env });
|
|
324
|
+
let resolved;
|
|
325
|
+
try {
|
|
326
|
+
resolved =
|
|
327
|
+
platform === "linux"
|
|
328
|
+
? await resolveLinux(options, base, features.devices, sampler, env)
|
|
329
|
+
: platform === "darwin"
|
|
330
|
+
? await resolveMac(options, base, sampler, env)
|
|
331
|
+
: await resolveWindows(options, base, features.devices, sampler, env);
|
|
332
|
+
await sampler.ready;
|
|
333
|
+
}
|
|
334
|
+
catch (err) {
|
|
335
|
+
await sampler.stop();
|
|
336
|
+
throw err;
|
|
337
|
+
}
|
|
338
|
+
const warnings = [...resolved.warnings];
|
|
339
|
+
const existingOutput = statOrNull(outPath);
|
|
340
|
+
let launched = null;
|
|
341
|
+
let lastError = null;
|
|
342
|
+
for (const { plan, label } of resolved.plans) {
|
|
343
|
+
try {
|
|
344
|
+
launched = await launch(ffmpeg, buildFfmpegArgs(plan), platform === "linux" ? xEnv(env, xDisplay) : env);
|
|
345
|
+
if (lastError)
|
|
346
|
+
warnings.push(`${resolved.plans[0].label} could not start, recorded with ${label} instead: ${tail(lastError.stderr, 2)}`);
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
catch (err) {
|
|
350
|
+
if (!(err instanceof StartError))
|
|
351
|
+
throw err;
|
|
352
|
+
lastError = err;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (!launched) {
|
|
356
|
+
await sampler.stop();
|
|
357
|
+
removeFailedOutput(outPath, existingOutput);
|
|
358
|
+
const detail = tail(lastError?.stderr ?? "");
|
|
359
|
+
const hint = platform === "darwin" && looksLikeMacPermissionError(lastError?.stderr ?? "") ? `\n${SCREEN_RECORDING_HINT}` : "";
|
|
360
|
+
throw new Error(`${lastError?.message ?? "ffmpeg did not start"}${detail ? `:\n${detail}` : ""}${hint}`);
|
|
361
|
+
}
|
|
362
|
+
const l = launched;
|
|
363
|
+
let stopRequestedAt = null;
|
|
364
|
+
let finished = null;
|
|
365
|
+
let aborted = null;
|
|
366
|
+
const ended = l.exited.then(() => undefined);
|
|
367
|
+
const finish = async () => {
|
|
368
|
+
if (stopRequestedAt === null)
|
|
369
|
+
stopRequestedAt = Date.now();
|
|
370
|
+
await stopFfmpeg(l);
|
|
371
|
+
const exit = await l.exited;
|
|
372
|
+
const { samples, warnings: sidecarWarnings } = await sampler.stop();
|
|
373
|
+
warnings.push(...sidecarWarnings);
|
|
374
|
+
let size = 0;
|
|
375
|
+
try {
|
|
376
|
+
size = statSync(outPath).size;
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
// handled below
|
|
380
|
+
}
|
|
381
|
+
if (size === 0)
|
|
382
|
+
throw new Error(`ffmpeg wrote no video${tail(l.stderr()) ? `:\n${tail(l.stderr())}` : ""}`);
|
|
383
|
+
const selfStopped = exit.at < stopRequestedAt;
|
|
384
|
+
if (selfStopped) {
|
|
385
|
+
const hitLimit = options.maxSeconds !== undefined && exit.at - l.startedAt >= options.maxSeconds * 1000 - 1500 && exit.code === 0;
|
|
386
|
+
if (!hitLimit) {
|
|
387
|
+
warnings.push(resolved.windowCapture
|
|
388
|
+
? "the window changed size or closed, so the recording stopped early"
|
|
389
|
+
: `ffmpeg stopped early (exit ${exit.code ?? exit.signal}): ${tail(l.stderr(), 3)}`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
else if (exit.code !== 0 && exit.code !== 255) {
|
|
393
|
+
warnings.push(`ffmpeg exited with ${exit.code ?? exit.signal}; the file was kept up to its last complete fragment`);
|
|
394
|
+
}
|
|
395
|
+
const info = await probe(outPath, realRunner, env);
|
|
396
|
+
const endedAt = Math.min(exit.at, stopRequestedAt);
|
|
397
|
+
let durationMs = info?.durationMs ?? null;
|
|
398
|
+
if (durationMs === null) {
|
|
399
|
+
durationMs = Math.max(0, endedAt - l.startedAt);
|
|
400
|
+
warnings.push("ffprobe was unavailable; the duration comes from the wall clock");
|
|
401
|
+
}
|
|
402
|
+
const interactions = samplesToEvents(samples, l.startedAt, l.startedAt + durationMs);
|
|
403
|
+
let geometry = resolved.geometry ? { ...resolved.geometry } : null;
|
|
404
|
+
if (!geometry && platform !== "darwin" && info?.width && info.height) {
|
|
405
|
+
geometry = { x: 0, y: 0, width: info.width, height: info.height };
|
|
406
|
+
}
|
|
407
|
+
if (geometry && info?.width && info.height) {
|
|
408
|
+
geometry.output_width = info.width;
|
|
409
|
+
geometry.output_height = info.height;
|
|
410
|
+
}
|
|
411
|
+
return { path: outPath, durationMs, interactions, geometry, warnings };
|
|
412
|
+
};
|
|
413
|
+
return {
|
|
414
|
+
startedAt: l.startedAt,
|
|
415
|
+
ended,
|
|
416
|
+
// Both are idempotent: a second stop() returns the first one's result,
|
|
417
|
+
// a second abort() waits for the first. stop() after abort() rejects
|
|
418
|
+
// (there is no file any more); abort() after stop() leaves the finished
|
|
419
|
+
// file alone and only waits for stop() to settle.
|
|
420
|
+
stop() {
|
|
421
|
+
if (aborted)
|
|
422
|
+
return aborted.then(() => Promise.reject(new Error("the capture was aborted")));
|
|
423
|
+
finished ??= finish();
|
|
424
|
+
return finished;
|
|
425
|
+
},
|
|
426
|
+
abort() {
|
|
427
|
+
if (finished)
|
|
428
|
+
return finished.then(() => undefined, () => undefined);
|
|
429
|
+
aborted ??= (async () => {
|
|
430
|
+
if (stopRequestedAt === null)
|
|
431
|
+
stopRequestedAt = Date.now();
|
|
432
|
+
if (l.child.exitCode === null && l.child.signalCode === null) {
|
|
433
|
+
l.child.kill("SIGKILL");
|
|
434
|
+
await l.exited;
|
|
435
|
+
}
|
|
436
|
+
await sampler.stop();
|
|
437
|
+
rmSync(outPath, { force: true });
|
|
438
|
+
})();
|
|
439
|
+
return aborted;
|
|
440
|
+
},
|
|
441
|
+
};
|
|
442
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// GENERATED from oss/packages/capture/src/check.ts by cli/scripts/sync-local.mjs. Do not edit here.
|
|
3
|
+
import { pickH264Encoder } from "./ffmpegArgs.js";
|
|
4
|
+
import { currentPlatform, ffmpegPath, ffprobePath, findExecutable, realRunner } from "./proc.js";
|
|
5
|
+
import { avfoundationScreens, parseAvfoundationDevices } from "./sources.js";
|
|
6
|
+
export const SCREEN_RECORDING_HINT = "macOS: allow your terminal app in System Settings > Privacy & Security > Screen Recording " +
|
|
7
|
+
"(and Microphone for --microphone), then quit and reopen the terminal.";
|
|
8
|
+
const INSTALL_FFMPEG = {
|
|
9
|
+
darwin: "Install ffmpeg: `brew install ffmpeg`.",
|
|
10
|
+
win32: "Install ffmpeg 6 or newer: `winget install Gyan.FFmpeg` (or download from ffmpeg.org) and make sure ffmpeg.exe is on PATH.",
|
|
11
|
+
linux: "Install ffmpeg: `sudo apt install ffmpeg` (Debian/Ubuntu) or `sudo dnf install ffmpeg` (Fedora).",
|
|
12
|
+
};
|
|
13
|
+
/** True when the session is Wayland, where x11grab sees only XWayland. */
|
|
14
|
+
export function isWaylandSession(env) {
|
|
15
|
+
return env.XDG_SESSION_TYPE === "wayland" || !!env.WAYLAND_DISPLAY;
|
|
16
|
+
}
|
|
17
|
+
/** Whether `ffmpeg -devices` lists a device (input side). */
|
|
18
|
+
export function hasDevice(devicesText, name) {
|
|
19
|
+
return new RegExp(`^\\s*D\\S*\\s+${name}\\b`, "m").test(devicesText);
|
|
20
|
+
}
|
|
21
|
+
/** Whether `ffmpeg -filters` lists a filter. */
|
|
22
|
+
export function hasFilter(filtersText, name) {
|
|
23
|
+
return new RegExp(`^\\s*\\S+\\s+${name}\\s`, "m").test(filtersText);
|
|
24
|
+
}
|
|
25
|
+
export async function checkCapture(platform = currentPlatform(), opts = {}) {
|
|
26
|
+
const run = opts.run ?? realRunner;
|
|
27
|
+
const env = opts.env ?? process.env;
|
|
28
|
+
const problems = [];
|
|
29
|
+
const hints = [];
|
|
30
|
+
const ffmpeg = ffmpegPath(platform, env);
|
|
31
|
+
if (!ffmpeg) {
|
|
32
|
+
return { ok: false, ffmpeg: null, problems: ["ffmpeg was not found on PATH"], hints: [INSTALL_FFMPEG[platform]] };
|
|
33
|
+
}
|
|
34
|
+
const devices = await run(ffmpeg, ["-hide_banner", "-devices"], env);
|
|
35
|
+
if (devices.status === null) {
|
|
36
|
+
return { ok: false, ffmpeg, problems: [`ffmpeg at ${ffmpeg} could not be run`], hints: [INSTALL_FFMPEG[platform]] };
|
|
37
|
+
}
|
|
38
|
+
const devicesText = devices.stdout + devices.stderr;
|
|
39
|
+
const encoders = await run(ffmpeg, ["-hide_banner", "-encoders"], env);
|
|
40
|
+
if (!pickH264Encoder(encoders.stdout + encoders.stderr, platform)) {
|
|
41
|
+
problems.push("this ffmpeg has no H.264 encoder (libx264)");
|
|
42
|
+
hints.push(INSTALL_FFMPEG[platform]);
|
|
43
|
+
}
|
|
44
|
+
if (!ffprobePath(platform, env))
|
|
45
|
+
hints.push("ffprobe was not found; durations will come from the wall clock (it ships with ffmpeg).");
|
|
46
|
+
if (platform === "linux") {
|
|
47
|
+
if (!hasDevice(devicesText, "x11grab")) {
|
|
48
|
+
problems.push("this ffmpeg was built without x11grab, so it cannot record an X display");
|
|
49
|
+
hints.push(INSTALL_FFMPEG.linux);
|
|
50
|
+
}
|
|
51
|
+
const xvfb = !!findExecutable("Xvfb", platform, env);
|
|
52
|
+
if (!env.DISPLAY) {
|
|
53
|
+
if (isWaylandSession(env)) {
|
|
54
|
+
problems.push("this is a Wayland session without XWayland (DISPLAY is unset): ffmpeg cannot read a Wayland screen without the desktop portal");
|
|
55
|
+
hints.push("Log in to an X11 session, or enable XWayland, to record your screen.");
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
problems.push("no X display: DISPLAY is unset");
|
|
59
|
+
}
|
|
60
|
+
hints.push(xvfb
|
|
61
|
+
? "A private virtual display (Xvfb) still works for agent runs."
|
|
62
|
+
: "Install Xvfb (`sudo apt install xvfb`) to record a private virtual display instead.");
|
|
63
|
+
}
|
|
64
|
+
else if (isWaylandSession(env)) {
|
|
65
|
+
hints.push("Wayland session: only X11 apps running under XWayland appear; native Wayland windows record as black.");
|
|
66
|
+
}
|
|
67
|
+
if (!hasDevice(devicesText, "pulse") && !hasDevice(devicesText, "alsa"))
|
|
68
|
+
hints.push("this ffmpeg has neither pulse nor alsa input, so the microphone cannot be recorded");
|
|
69
|
+
if (!findExecutable("python3", platform, env))
|
|
70
|
+
hints.push("install python3 to record pointer positions and clicks next to the video");
|
|
71
|
+
}
|
|
72
|
+
else if (platform === "darwin") {
|
|
73
|
+
if (!hasDevice(devicesText, "avfoundation")) {
|
|
74
|
+
problems.push("this ffmpeg was built without avfoundation, so it cannot record the screen");
|
|
75
|
+
hints.push(INSTALL_FFMPEG.darwin);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
const list = await run(ffmpeg, ["-hide_banner", "-f", "avfoundation", "-list_devices", "true", "-i", ""], env);
|
|
79
|
+
if (avfoundationScreens(parseAvfoundationDevices(list.stderr + list.stdout)).length === 0) {
|
|
80
|
+
problems.push("ffmpeg lists no screens to capture");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
hints.push(SCREEN_RECORDING_HINT);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
const filters = await run(ffmpeg, ["-hide_banner", "-filters"], env);
|
|
87
|
+
const ddagrab = hasFilter(filters.stdout + filters.stderr, "ddagrab");
|
|
88
|
+
const gdigrab = hasDevice(devicesText, "gdigrab");
|
|
89
|
+
if (!ddagrab && !gdigrab) {
|
|
90
|
+
problems.push("this ffmpeg has neither ddagrab nor gdigrab, so it cannot record the screen");
|
|
91
|
+
hints.push(INSTALL_FFMPEG.win32);
|
|
92
|
+
}
|
|
93
|
+
else if (!ddagrab) {
|
|
94
|
+
hints.push("this ffmpeg has no ddagrab (FFmpeg 6+ with Direct3D 11); recording uses the slower gdigrab");
|
|
95
|
+
}
|
|
96
|
+
if (!hasDevice(devicesText, "dshow"))
|
|
97
|
+
hints.push("this ffmpeg has no dshow input, so the microphone cannot be recorded");
|
|
98
|
+
if (!findExecutable("powershell.exe", platform, env) && !findExecutable("pwsh.exe", platform, env)) {
|
|
99
|
+
hints.push("PowerShell was not found; recordings will have no pointer positions");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { ok: problems.length === 0, ffmpeg, problems, hints };
|
|
103
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const live = new Map();
|
|
2
|
+
const cleanups = new Set();
|
|
3
|
+
let hooked = false;
|
|
4
|
+
function gone(child) {
|
|
5
|
+
return child.exitCode !== null || child.signalCode !== null;
|
|
6
|
+
}
|
|
7
|
+
function hook() {
|
|
8
|
+
if (hooked)
|
|
9
|
+
return;
|
|
10
|
+
hooked = true;
|
|
11
|
+
process.on("exit", () => {
|
|
12
|
+
stopAllChildren();
|
|
13
|
+
for (const fn of cleanups) {
|
|
14
|
+
try {
|
|
15
|
+
fn();
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// best effort at exit
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
cleanups.clear();
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/** Keep track of `child` until it exits. `signal` is what process exit sends it. */
|
|
25
|
+
export function trackChild(child, signal = "SIGTERM") {
|
|
26
|
+
if (gone(child))
|
|
27
|
+
return child;
|
|
28
|
+
live.set(child, signal);
|
|
29
|
+
const forget = () => live.delete(child);
|
|
30
|
+
child.once("exit", forget);
|
|
31
|
+
child.once("error", forget);
|
|
32
|
+
hook();
|
|
33
|
+
return child;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Run `fn` (synchronous) when the process exits, unless the returned function
|
|
37
|
+
* is called first. For temporary files that belong to a tracked child.
|
|
38
|
+
*/
|
|
39
|
+
export function atExit(fn) {
|
|
40
|
+
cleanups.add(fn);
|
|
41
|
+
hook();
|
|
42
|
+
return () => cleanups.delete(fn);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Signal every tracked child that is still running. Synchronous, so it is
|
|
46
|
+
* safe in an `exit` handler; an application can also call it from its own
|
|
47
|
+
* signal handler before exiting.
|
|
48
|
+
*/
|
|
49
|
+
export function stopAllChildren() {
|
|
50
|
+
for (const [child, signal] of live) {
|
|
51
|
+
if (!gone(child)) {
|
|
52
|
+
try {
|
|
53
|
+
child.kill(signal);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// already gone
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
live.clear();
|
|
61
|
+
}
|
|
62
|
+
/** How many tracked children are still running (for tests). */
|
|
63
|
+
export function runningChildren() {
|
|
64
|
+
let n = 0;
|
|
65
|
+
for (const child of live.keys())
|
|
66
|
+
if (!gone(child))
|
|
67
|
+
n++;
|
|
68
|
+
return n;
|
|
69
|
+
}
|