@tmhs/screencast-mcp 0.49.1 → 0.51.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/README.md +15 -9
- package/dist/index.js +551 -106
- package/dist/wgcRecorder.js +15 -139
- package/package.json +9 -5
- package/assets/.gitkeep +0 -0
- package/assets/icon.png +0 -0
- package/dist/cli.js +0 -516
- package/dist/context.js +0 -50
- package/dist/prompts.js +0 -95
- package/dist/resources.js +0 -93
- package/dist/server.js +0 -174
- package/dist/setup.js +0 -438
- package/dist/tools/addMarker.js +0 -93
- package/dist/tools/assembleHighlights.js +0 -69
- package/dist/tools/backdrop.js +0 -79
- package/dist/tools/bugReport.js +0 -243
- package/dist/tools/burnSubtitles.js +0 -56
- package/dist/tools/callout.js +0 -62
- package/dist/tools/chapters.js +0 -156
- package/dist/tools/clip.js +0 -52
- package/dist/tools/compress.js +0 -51
- package/dist/tools/concat.js +0 -67
- package/dist/tools/contactSheet.js +0 -113
- package/dist/tools/convert.js +0 -46
- package/dist/tools/crop.js +0 -43
- package/dist/tools/cursor.js +0 -67
- package/dist/tools/cutByTranscript.js +0 -86
- package/dist/tools/cutIdle.js +0 -84
- package/dist/tools/deleteRecording.js +0 -119
- package/dist/tools/denoise.js +0 -60
- package/dist/tools/doctor.js +0 -146
- package/dist/tools/exportGif.js +0 -57
- package/dist/tools/exportPreset.js +0 -48
- package/dist/tools/extractAudio.js +0 -65
- package/dist/tools/findText.js +0 -194
- package/dist/tools/getMediaInfo.js +0 -36
- package/dist/tools/getSession.js +0 -40
- package/dist/tools/indexRecording.js +0 -195
- package/dist/tools/keystrokeOverlay.js +0 -90
- package/dist/tools/listAudioDevices.js +0 -35
- package/dist/tools/listSessions.js +0 -28
- package/dist/tools/listTargets.js +0 -76
- package/dist/tools/listVideoDevices.js +0 -26
- package/dist/tools/musicBed.js +0 -55
- package/dist/tools/narrate.js +0 -86
- package/dist/tools/normalizeAudio.js +0 -59
- package/dist/tools/overlay.js +0 -62
- package/dist/tools/pauseRecording.js +0 -44
- package/dist/tools/pipeline.js +0 -397
- package/dist/tools/readEvents.js +0 -98
- package/dist/tools/redactRegion.js +0 -62
- package/dist/tools/reframe.js +0 -46
- package/dist/tools/resumeRecording.js +0 -46
- package/dist/tools/reviewRecording.js +0 -163
- package/dist/tools/sampleFrames.js +0 -200
- package/dist/tools/saveReplay.js +0 -90
- package/dist/tools/scale.js +0 -44
- package/dist/tools/screenDiff.js +0 -86
- package/dist/tools/screenshot.js +0 -76
- package/dist/tools/searchRecording.js +0 -79
- package/dist/tools/speed.js +0 -46
- package/dist/tools/startRecording.js +0 -398
- package/dist/tools/stepGuide.js +0 -249
- package/dist/tools/stopRecording.js +0 -132
- package/dist/tools/titleCard.js +0 -67
- package/dist/tools/transcribe.js +0 -66
- package/dist/tools/trim.js +0 -57
- package/dist/tools/waitForChange.js +0 -142
- package/dist/tools/xfadeTransition.js +0 -58
- package/dist/tools/zoom.js +0 -134
- package/dist/utils/anim.js +0 -54
- package/dist/utils/animExport.js +0 -56
- package/dist/utils/argPaths.js +0 -39
- package/dist/utils/audio.js +0 -117
- package/dist/utils/audioDevices.js +0 -107
- package/dist/utils/backdrop.js +0 -162
- package/dist/utils/bugReport.js +0 -195
- package/dist/utils/callout.js +0 -132
- package/dist/utils/calloutAssets.js +0 -42
- package/dist/utils/capabilities.js +0 -96
- package/dist/utils/captions.js +0 -104
- package/dist/utils/chapters.js +0 -227
- package/dist/utils/consent.js +0 -29
- package/dist/utils/cursor.js +0 -338
- package/dist/utils/diff.js +0 -186
- package/dist/utils/errors.js +0 -29
- package/dist/utils/events.js +0 -403
- package/dist/utils/expr.js +0 -11
- package/dist/utils/ffmpeg.js +0 -285
- package/dist/utils/fontCoverage.js +0 -177
- package/dist/utils/fonts.js +0 -154
- package/dist/utils/frameQueue.js +0 -50
- package/dist/utils/gpu.js +0 -219
- package/dist/utils/idle.js +0 -91
- package/dist/utils/images.js +0 -38
- package/dist/utils/jobs.js +0 -129
- package/dist/utils/keystrokes.js +0 -198
- package/dist/utils/lifecycle.js +0 -233
- package/dist/utils/logging.js +0 -75
- package/dist/utils/media.js +0 -584
- package/dist/utils/monitors.js +0 -124
- package/dist/utils/narrate.js +0 -233
- package/dist/utils/ocr.js +0 -222
- package/dist/utils/ocrScan.js +0 -93
- package/dist/utils/paths.js +0 -155
- package/dist/utils/pause.js +0 -190
- package/dist/utils/pipeline.js +0 -361
- package/dist/utils/player.js +0 -85
- package/dist/utils/pool.js +0 -29
- package/dist/utils/produce.js +0 -300
- package/dist/utils/recipes.js +0 -71
- package/dist/utils/recorder.js +0 -173
- package/dist/utils/recordingFiles.js +0 -32
- package/dist/utils/recordingIndex.js +0 -312
- package/dist/utils/replay.js +0 -283
- package/dist/utils/resolveTarget.js +0 -25
- package/dist/utils/retention.js +0 -48
- package/dist/utils/roots.js +0 -43
- package/dist/utils/sessions.js +0 -499
- package/dist/utils/sheet.js +0 -82
- package/dist/utils/stepGuide.js +0 -231
- package/dist/utils/steps.js +0 -88
- package/dist/utils/still.js +0 -38
- package/dist/utils/targetList.js +0 -64
- package/dist/utils/targets.js +0 -265
- package/dist/utils/timeline.js +0 -85
- package/dist/utils/toolSpec.js +0 -31
- package/dist/utils/toolsets.js +0 -134
- package/dist/utils/transcriptCut.js +0 -149
- package/dist/utils/validate.js +0 -23
- package/dist/utils/waitLoop.js +0 -50
- package/dist/utils/wgc.js +0 -84
- package/dist/utils/windows.js +0 -192
- package/dist/utils/zoom.js +0 -252
package/dist/utils/chapters.js
DELETED
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { ScreencastError } from "./errors.js";
|
|
3
|
-
import { requireFfmpeg, runCapture } from "./ffmpeg.js";
|
|
4
|
-
import { cutShift } from "./timeline.js";
|
|
5
|
-
export const MAX_MARKERS = 500;
|
|
6
|
-
export const MAX_LABEL = 200;
|
|
7
|
-
export const YOUTUBE_MIN_CHAPTERS = 3;
|
|
8
|
-
export const YOUTUBE_MIN_CHAPTER_SEC = 10;
|
|
9
|
-
const r3 = (v) => Math.round(v * 1000) / 1000;
|
|
10
|
-
export function markersPath(video) {
|
|
11
|
-
return `${video.replace(/\.[^./\\]+$/, "")}.markers.json`;
|
|
12
|
-
}
|
|
13
|
-
export function markerTimeSec(videoStartMs, nowMs) {
|
|
14
|
-
return Math.max(0, r3((nowMs - videoStartMs) / 1000));
|
|
15
|
-
}
|
|
16
|
-
export function cleanLabel(label, max = MAX_LABEL) {
|
|
17
|
-
const s = label.replace(/\s+/g, " ").trim();
|
|
18
|
-
return s.length > max ? `${s.slice(0, max - 1).trimEnd()}…` : s;
|
|
19
|
-
}
|
|
20
|
-
export function insertMarker(list, marker) {
|
|
21
|
-
if (list.length >= MAX_MARKERS) {
|
|
22
|
-
throw new ScreencastError(`A recording holds at most ${MAX_MARKERS} markers.`);
|
|
23
|
-
}
|
|
24
|
-
const label = cleanLabel(marker.label);
|
|
25
|
-
if (!label)
|
|
26
|
-
throw new ScreencastError("A marker needs a non-empty label.");
|
|
27
|
-
if (!Number.isFinite(marker.t) || marker.t < 0)
|
|
28
|
-
throw new ScreencastError("A marker time must be a non-negative number of seconds.");
|
|
29
|
-
const m = { ...marker, t: r3(marker.t), label };
|
|
30
|
-
const i = list.findIndex((x) => x.t > m.t);
|
|
31
|
-
return i === -1 ? [...list, m] : [...list.slice(0, i), m, ...list.slice(i)];
|
|
32
|
-
}
|
|
33
|
-
function isMarker(v) {
|
|
34
|
-
const m = v;
|
|
35
|
-
return !!m && typeof m.t === "number" && Number.isFinite(m.t) && m.t >= 0 && typeof m.label === "string";
|
|
36
|
-
}
|
|
37
|
-
export function parseMarkerSidecar(text) {
|
|
38
|
-
let data;
|
|
39
|
-
try {
|
|
40
|
-
data = JSON.parse(text);
|
|
41
|
-
}
|
|
42
|
-
catch {
|
|
43
|
-
return [];
|
|
44
|
-
}
|
|
45
|
-
const list = Array.isArray(data) ? data : data?.markers;
|
|
46
|
-
return Array.isArray(list) ? list.filter(isMarker).sort((a, b) => a.t - b.t) : [];
|
|
47
|
-
}
|
|
48
|
-
export function readMarkers(video) {
|
|
49
|
-
const path = markersPath(video);
|
|
50
|
-
if (!existsSync(path))
|
|
51
|
-
return [];
|
|
52
|
-
try {
|
|
53
|
-
return parseMarkerSidecar(readFileSync(path, "utf8"));
|
|
54
|
-
}
|
|
55
|
-
catch {
|
|
56
|
-
return [];
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
export function writeMarkers(video, markers, sessionId) {
|
|
60
|
-
const path = markersPath(video);
|
|
61
|
-
const tmp = `${path}.${process.pid}.tmp`;
|
|
62
|
-
writeFileSync(tmp, JSON.stringify({ ...(sessionId ? { sessionId } : {}), markers }, null, 2));
|
|
63
|
-
renameSync(tmp, path);
|
|
64
|
-
return path;
|
|
65
|
-
}
|
|
66
|
-
export function retimeMarkers(markers, ops) {
|
|
67
|
-
let cur = [...markers].sort((a, b) => a.t - b.t);
|
|
68
|
-
for (const op of ops) {
|
|
69
|
-
if (op.op === "speed") {
|
|
70
|
-
cur = cur.map((m) => ({ ...m, t: r3(m.t / op.factor) }));
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
if (op.op === "cut") {
|
|
74
|
-
cur = cur.map((m) => ({ ...m, t: r3(m.t - cutShift(m.t, op.spans)) }));
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
const inside = cur.filter((m) => m.t >= op.start && m.t < op.end).map((m) => ({ ...m, t: r3(m.t - op.start) }));
|
|
78
|
-
const carried = cur.filter((m) => m.t < op.start).at(-1);
|
|
79
|
-
cur = carried && !inside.some((m) => m.t < 0.001) ? [{ ...carried, t: 0 }, ...inside] : inside;
|
|
80
|
-
}
|
|
81
|
-
return cur;
|
|
82
|
-
}
|
|
83
|
-
export function buildChapters(points, durationSec, opts = {}) {
|
|
84
|
-
const min = Math.max(0.001, opts.minChapterSec ?? 1);
|
|
85
|
-
const sorted = points
|
|
86
|
-
.filter((p) => Number.isFinite(p.start) && p.start >= 0 && p.start < durationSec)
|
|
87
|
-
.map((p, i) => ({ start: r3(p.start), title: cleanLabel(p.title, 100) || `Chapter ${i + 1}` }))
|
|
88
|
-
.sort((a, b) => a.start - b.start);
|
|
89
|
-
const kept = [];
|
|
90
|
-
for (const p of sorted) {
|
|
91
|
-
const last = kept.at(-1);
|
|
92
|
-
if (!last) {
|
|
93
|
-
if (p.start < min)
|
|
94
|
-
kept.push({ ...p, start: 0 });
|
|
95
|
-
else
|
|
96
|
-
kept.push({ start: 0, title: opts.introTitle ?? "Intro" }, p);
|
|
97
|
-
}
|
|
98
|
-
else if (p.start - last.start >= min) {
|
|
99
|
-
kept.push(p);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const lastKept = kept.at(-1);
|
|
103
|
-
if (kept.length > 1 && lastKept && durationSec - lastKept.start < min)
|
|
104
|
-
kept.pop();
|
|
105
|
-
return kept.map((p, i) => ({ start: p.start, end: r3(kept[i + 1]?.start ?? durationSec), title: p.title }));
|
|
106
|
-
}
|
|
107
|
-
export function youtubeTimestamp(sec, long) {
|
|
108
|
-
const s = Math.max(0, Math.floor(sec));
|
|
109
|
-
const h = Math.floor(s / 3600);
|
|
110
|
-
const m = Math.floor((s % 3600) / 60);
|
|
111
|
-
const ss = String(s % 60).padStart(2, "0");
|
|
112
|
-
return long ? `${h}:${String(m).padStart(2, "0")}:${ss}` : `${Math.floor(s / 60)}:${ss}`;
|
|
113
|
-
}
|
|
114
|
-
export function youtubeChapters(chapters, durationSec) {
|
|
115
|
-
const long = durationSec >= 3600;
|
|
116
|
-
const text = chapters.map((c) => `${youtubeTimestamp(c.start, long)} ${c.title}`).join("\n");
|
|
117
|
-
const issues = [];
|
|
118
|
-
if (chapters.length < YOUTUBE_MIN_CHAPTERS) {
|
|
119
|
-
issues.push(`YouTube needs at least ${YOUTUBE_MIN_CHAPTERS} chapters; there are ${chapters.length}.`);
|
|
120
|
-
}
|
|
121
|
-
if (chapters[0] && chapters[0].start !== 0)
|
|
122
|
-
issues.push("YouTube needs the first chapter at 0:00.");
|
|
123
|
-
const short = chapters.filter((c) => c.end - c.start < YOUTUBE_MIN_CHAPTER_SEC);
|
|
124
|
-
if (short.length > 0) {
|
|
125
|
-
issues.push(`YouTube needs every chapter to be at least ${YOUTUBE_MIN_CHAPTER_SEC} s long; ${short.length} ` +
|
|
126
|
-
`${short.length === 1 ? "is" : "are"} shorter (${short.map((c) => `"${c.title}"`).join(", ")}). ` +
|
|
127
|
-
`Raise minChapterSec to ${YOUTUBE_MIN_CHAPTER_SEC} to merge them.`);
|
|
128
|
-
}
|
|
129
|
-
return { text, issues };
|
|
130
|
-
}
|
|
131
|
-
export function escapeFfmetadata(value) {
|
|
132
|
-
return value.replace(/[=;#\\\n]/g, (c) => `\\${c}`);
|
|
133
|
-
}
|
|
134
|
-
export function ffmetadata(chapters) {
|
|
135
|
-
const lines = [";FFMETADATA1"];
|
|
136
|
-
for (const c of chapters) {
|
|
137
|
-
lines.push("[CHAPTER]", "TIMEBASE=1/1000", `START=${Math.round(c.start * 1000)}`, `END=${Math.round(c.end * 1000)}`, `title=${escapeFfmetadata(c.title)}`);
|
|
138
|
-
}
|
|
139
|
-
return `${lines.join("\n")}\n`;
|
|
140
|
-
}
|
|
141
|
-
export function markerPoints(markers) {
|
|
142
|
-
return markers.map((m) => ({ start: m.t, title: m.label }));
|
|
143
|
-
}
|
|
144
|
-
export function scenePoints(scenes, minChapterSec, maxChapters) {
|
|
145
|
-
const picked = [0];
|
|
146
|
-
for (const s of [...scenes].sort((a, b) => b.score - a.score)) {
|
|
147
|
-
if (picked.length >= maxChapters)
|
|
148
|
-
break;
|
|
149
|
-
if (picked.every((t) => Math.abs(t - s.timeSec) >= minChapterSec))
|
|
150
|
-
picked.push(s.timeSec);
|
|
151
|
-
}
|
|
152
|
-
return picked.sort((a, b) => a - b).map((t, i) => ({ start: t, title: `Scene ${i + 1}` }));
|
|
153
|
-
}
|
|
154
|
-
function cueTitle(text, max = 60) {
|
|
155
|
-
const s = text.replace(/<[^>]+>/g, "").replace(/\s+/g, " ").trim();
|
|
156
|
-
if (s.length <= max)
|
|
157
|
-
return s;
|
|
158
|
-
const cut = s.slice(0, max);
|
|
159
|
-
const space = cut.lastIndexOf(" ");
|
|
160
|
-
return `${(space > max / 2 ? cut.slice(0, space) : cut).trimEnd()}…`;
|
|
161
|
-
}
|
|
162
|
-
export function transcriptPoints(cues, opts = {}) {
|
|
163
|
-
const gap = opts.gapSec ?? 2;
|
|
164
|
-
const min = opts.minChapterSec ?? 30;
|
|
165
|
-
const sorted = [...cues].sort((a, b) => a.start - b.start);
|
|
166
|
-
const out = [];
|
|
167
|
-
let prevEnd = -Infinity;
|
|
168
|
-
for (const c of sorted) {
|
|
169
|
-
const last = out.at(-1);
|
|
170
|
-
if (!last || (c.start - prevEnd >= gap && c.start - last.start >= min)) {
|
|
171
|
-
out.push({ start: c.start, title: cueTitle(c.text) });
|
|
172
|
-
}
|
|
173
|
-
prevEnd = Math.max(prevEnd, c.end);
|
|
174
|
-
}
|
|
175
|
-
return out;
|
|
176
|
-
}
|
|
177
|
-
export function buildChaptersMuxArgs(input, metaFile, output, opts) {
|
|
178
|
-
const subs = opts.subtitles;
|
|
179
|
-
return [
|
|
180
|
-
"-y",
|
|
181
|
-
"-i", input,
|
|
182
|
-
"-f", "ffmetadata", "-i", metaFile,
|
|
183
|
-
...(subs ? ["-i", subs] : []),
|
|
184
|
-
"-map", "0:v", "-map", "0:a?",
|
|
185
|
-
...(subs ? ["-map", "2:s:0"] : ["-map", "0:s?"]),
|
|
186
|
-
"-map_metadata", "0",
|
|
187
|
-
"-map_chapters", "1",
|
|
188
|
-
"-c", "copy",
|
|
189
|
-
...(subs ? ["-c:s", opts.container === ".mkv" ? "copy" : "mov_text"] : []),
|
|
190
|
-
...(subs && opts.language ? ["-metadata:s:s:0", `language=${opts.language}`] : []),
|
|
191
|
-
...(opts.container === ".mkv" ? [] : ["-movflags", "+faststart"]),
|
|
192
|
-
output,
|
|
193
|
-
];
|
|
194
|
-
}
|
|
195
|
-
export function buildChaptersProbeArgs(input) {
|
|
196
|
-
return ["-v", "quiet", "-print_format", "json", "-show_chapters", input];
|
|
197
|
-
}
|
|
198
|
-
export function parseChaptersProbe(stdout) {
|
|
199
|
-
let data;
|
|
200
|
-
try {
|
|
201
|
-
data = JSON.parse(stdout);
|
|
202
|
-
}
|
|
203
|
-
catch {
|
|
204
|
-
return [];
|
|
205
|
-
}
|
|
206
|
-
return (data.chapters ?? []).flatMap((c, i) => {
|
|
207
|
-
const start = Number(c.start_time);
|
|
208
|
-
const end = Number(c.end_time);
|
|
209
|
-
if (!Number.isFinite(start) || !Number.isFinite(end))
|
|
210
|
-
return [];
|
|
211
|
-
return [{ start: r3(start), end: r3(end), title: c.tags?.title ?? `Chapter ${i + 1}` }];
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
export async function probeChapters(input, signal) {
|
|
215
|
-
const { ffprobe } = requireFfmpeg();
|
|
216
|
-
const res = await runCapture(ffprobe, buildChaptersProbeArgs(input), 30_000, { signal });
|
|
217
|
-
return res.code === 0 ? parseChaptersProbe(res.stdout) : [];
|
|
218
|
-
}
|
|
219
|
-
export async function markersOrChapters(video, signal) {
|
|
220
|
-
const sidecar = readMarkers(video);
|
|
221
|
-
if (sidecar.length > 0)
|
|
222
|
-
return { markers: sidecar, from: "sidecar" };
|
|
223
|
-
const embedded = await probeChapters(video, signal);
|
|
224
|
-
if (embedded.length > 0)
|
|
225
|
-
return { markers: embedded.map((c) => ({ t: c.start, label: c.title })), from: "chapters" };
|
|
226
|
-
return { markers: [], from: null };
|
|
227
|
-
}
|
package/dist/utils/consent.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { errorMessage, ScreencastError } from "./errors.js";
|
|
2
|
-
export const CONFIRM_CAPTURE_ENV = "SCREENCAST_CONFIRM_CAPTURE";
|
|
3
|
-
export const CONFIRM_TIMEOUT_MS = 5 * 60 * 1000;
|
|
4
|
-
export function confirmCaptureEnabled(env = process.env) {
|
|
5
|
-
return ["1", "true", "yes", "on"].includes((env[CONFIRM_CAPTURE_ENV] ?? "").trim().toLowerCase());
|
|
6
|
-
}
|
|
7
|
-
export function describeTarget(target, window) {
|
|
8
|
-
if (window)
|
|
9
|
-
return `window "${window.matchedTitle}"`;
|
|
10
|
-
return target === "full" ? "the full screen (all monitors)" : target;
|
|
11
|
-
}
|
|
12
|
-
export async function confirmCapture(server, request, signal) {
|
|
13
|
-
if (!confirmCaptureEnabled())
|
|
14
|
-
return;
|
|
15
|
-
if (!server.server.getClientCapabilities()?.elicitation?.form) {
|
|
16
|
-
throw new ScreencastError(`Capture blocked: ${CONFIRM_CAPTURE_ENV} requires the user to confirm each capture, ` +
|
|
17
|
-
"but this client does not support MCP elicitation. Nothing was captured.", `Use a client that supports elicitation, or unset ${CONFIRM_CAPTURE_ENV} to capture without confirmation.`);
|
|
18
|
-
}
|
|
19
|
-
let action;
|
|
20
|
-
try {
|
|
21
|
-
({ action } = await server.server.elicitInput({ message: request, requestedSchema: { type: "object", properties: {} } }, { signal, timeout: CONFIRM_TIMEOUT_MS }));
|
|
22
|
-
}
|
|
23
|
-
catch (err) {
|
|
24
|
-
throw new ScreencastError(`Capture blocked: the confirmation request failed (${errorMessage(err)}). Nothing was captured.`);
|
|
25
|
-
}
|
|
26
|
-
if (action !== "accept") {
|
|
27
|
-
throw new ScreencastError(`Capture ${action === "decline" ? "declined" : "cancelled"} by the user. Nothing was captured.`);
|
|
28
|
-
}
|
|
29
|
-
}
|
package/dist/utils/cursor.js
DELETED
|
@@ -1,338 +0,0 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url";
|
|
2
|
-
import { ScreencastError } from "./errors.js";
|
|
3
|
-
import { escapeFilterPath } from "./fonts.js";
|
|
4
|
-
import { MOVE_SAMPLE_MS, eventPaths, parseEventsJsonl } from "./events.js";
|
|
5
|
-
import { mapTime, mapWindow } from "./timeline.js";
|
|
6
|
-
import { resolveQuality } from "./targets.js";
|
|
7
|
-
import { z } from "zod";
|
|
8
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
9
|
-
export const CURSOR_SPRITE = { w: 114, h: 168, hotX: 9, hotY: 9, arrowH: 135 };
|
|
10
|
-
export function cursorSpritePath() {
|
|
11
|
-
return fileURLToPath(new URL("../../assets/cursor/arrow.png", import.meta.url));
|
|
12
|
-
}
|
|
13
|
-
export const DEFAULT_SMOOTHING = 0.5;
|
|
14
|
-
export const DEFAULT_HIDE_AFTER_SEC = 2;
|
|
15
|
-
export const DEFAULT_CURSOR_SIZE = 1.5;
|
|
16
|
-
export const RIPPLE_SEC = 0.4;
|
|
17
|
-
const SIM_HZ = 240;
|
|
18
|
-
const PATH_EVERY = 4;
|
|
19
|
-
const SIMPLIFY_PX = 0.35;
|
|
20
|
-
const POSITION_TYPES = new Set(["move", "down", "up", "wheel"]);
|
|
21
|
-
const r3 = (n) => Math.round(n * 1000) / 1000;
|
|
22
|
-
export function springFor(smoothing) {
|
|
23
|
-
const omega = 6 + (1 - smoothing) * 34;
|
|
24
|
-
return { omega, lead: Math.min(2 / omega, 0.2) };
|
|
25
|
-
}
|
|
26
|
-
export function targetPath(samples) {
|
|
27
|
-
const out = [];
|
|
28
|
-
const step = MOVE_SAMPLE_MS / 1000;
|
|
29
|
-
for (const s of samples) {
|
|
30
|
-
const prev = out.at(-1);
|
|
31
|
-
if (prev && s.t - prev.t > step * 2 && (prev.x !== s.x || prev.y !== s.y)) {
|
|
32
|
-
out.push({ t: s.t - step, x: prev.x, y: prev.y });
|
|
33
|
-
}
|
|
34
|
-
if (prev && s.t === prev.t)
|
|
35
|
-
out[out.length - 1] = s;
|
|
36
|
-
else
|
|
37
|
-
out.push(s);
|
|
38
|
-
}
|
|
39
|
-
return out;
|
|
40
|
-
}
|
|
41
|
-
export function pathAt(path, t, from = 0) {
|
|
42
|
-
if (path.length === 0)
|
|
43
|
-
return { x: 0, y: 0, i: 0 };
|
|
44
|
-
let i = Math.max(0, Math.min(from, path.length - 1));
|
|
45
|
-
while (i > 0 && path[i].t > t)
|
|
46
|
-
i--;
|
|
47
|
-
while (i < path.length - 1 && path[i + 1].t <= t)
|
|
48
|
-
i++;
|
|
49
|
-
const a = path[i];
|
|
50
|
-
const b = path[i + 1];
|
|
51
|
-
if (!b || t <= a.t)
|
|
52
|
-
return { x: a.x, y: a.y, i };
|
|
53
|
-
const f = (t - a.t) / (b.t - a.t);
|
|
54
|
-
return { x: a.x + (b.x - a.x) * f, y: a.y + (b.y - a.y) * f, i };
|
|
55
|
-
}
|
|
56
|
-
export function springPath(target, omega, lead, from, to) {
|
|
57
|
-
if (target.length === 0)
|
|
58
|
-
return [];
|
|
59
|
-
const dt = 1 / SIM_HZ;
|
|
60
|
-
let hint = 0;
|
|
61
|
-
const at = (t) => {
|
|
62
|
-
const p = pathAt(target, t + lead, hint);
|
|
63
|
-
hint = p.i;
|
|
64
|
-
return p;
|
|
65
|
-
};
|
|
66
|
-
const start = at(from);
|
|
67
|
-
let x = start.x;
|
|
68
|
-
let y = start.y;
|
|
69
|
-
let vx = 0;
|
|
70
|
-
let vy = 0;
|
|
71
|
-
const out = [{ t: r3(from), x, y }];
|
|
72
|
-
const e = Math.exp(-omega * dt);
|
|
73
|
-
const steps = Math.max(0, Math.ceil((to - from) * SIM_HZ));
|
|
74
|
-
for (let n = 1; n <= steps; n++) {
|
|
75
|
-
const t = Math.min(to, from + n * dt);
|
|
76
|
-
const p = at(t);
|
|
77
|
-
const cx = x - p.x;
|
|
78
|
-
const cy = y - p.y;
|
|
79
|
-
const tx = (vx + omega * cx) * dt;
|
|
80
|
-
const ty = (vy + omega * cy) * dt;
|
|
81
|
-
vx = (vx - omega * tx) * e;
|
|
82
|
-
vy = (vy - omega * ty) * e;
|
|
83
|
-
x = p.x + (cx + tx) * e;
|
|
84
|
-
y = p.y + (cy + ty) * e;
|
|
85
|
-
if (n % PATH_EVERY === 0 || n === steps)
|
|
86
|
-
out.push({ t: r3(t), x, y });
|
|
87
|
-
}
|
|
88
|
-
return out;
|
|
89
|
-
}
|
|
90
|
-
export function simplifyPath(path, tol = SIMPLIFY_PX) {
|
|
91
|
-
if (path.length <= 2)
|
|
92
|
-
return [...path];
|
|
93
|
-
const keep = new Uint8Array(path.length);
|
|
94
|
-
keep[0] = 1;
|
|
95
|
-
keep[path.length - 1] = 1;
|
|
96
|
-
const stack = [[0, path.length - 1]];
|
|
97
|
-
while (stack.length > 0) {
|
|
98
|
-
const [a, b] = stack.pop();
|
|
99
|
-
const A = path[a];
|
|
100
|
-
const B = path[b];
|
|
101
|
-
let worst = -1;
|
|
102
|
-
let at = -1;
|
|
103
|
-
for (let i = a + 1; i < b; i++) {
|
|
104
|
-
const P = path[i];
|
|
105
|
-
const f = B.t > A.t ? (P.t - A.t) / (B.t - A.t) : 0;
|
|
106
|
-
const d = Math.hypot(P.x - (A.x + (B.x - A.x) * f), P.y - (A.y + (B.y - A.y) * f));
|
|
107
|
-
if (d > worst) {
|
|
108
|
-
worst = d;
|
|
109
|
-
at = i;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (worst > tol) {
|
|
113
|
-
keep[at] = 1;
|
|
114
|
-
stack.push([a, at], [at, b]);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return path.filter((_, i) => keep[i] === 1);
|
|
118
|
-
}
|
|
119
|
-
function mergeWindows(ws) {
|
|
120
|
-
const out = [];
|
|
121
|
-
for (const w of [...ws].sort((a, b) => a[0] - b[0])) {
|
|
122
|
-
const last = out.at(-1);
|
|
123
|
-
if (last && w[0] <= last[1])
|
|
124
|
-
last[1] = Math.max(last[1], w[1]);
|
|
125
|
-
else
|
|
126
|
-
out.push([w[0], w[1]]);
|
|
127
|
-
}
|
|
128
|
-
return out;
|
|
129
|
-
}
|
|
130
|
-
export function planCursor(events, opts = {}, endSec = null) {
|
|
131
|
-
const smoothing = opts.smoothing ?? DEFAULT_SMOOTHING;
|
|
132
|
-
if (!(smoothing >= 0 && smoothing <= 1))
|
|
133
|
-
throw new ScreencastError("smoothing must be between 0 and 1.");
|
|
134
|
-
const hideAfter = opts.hideAfterSec ?? DEFAULT_HIDE_AFTER_SEC;
|
|
135
|
-
if (!(hideAfter >= 0))
|
|
136
|
-
throw new ScreencastError("hideAfterSec must be 0 or more.");
|
|
137
|
-
const samples = events
|
|
138
|
-
.filter((e) => POSITION_TYPES.has(e.type) && Number.isFinite(e.t) && e.t >= 0 && Number.isFinite(e.x) && Number.isFinite(e.y))
|
|
139
|
-
.filter((e) => endSec === null || e.t <= endSec)
|
|
140
|
-
.sort((a, b) => a.t - b.t);
|
|
141
|
-
if (samples.length === 0)
|
|
142
|
-
return { path: [], visible: [], clicks: [] };
|
|
143
|
-
const target = targetPath(samples.map((e) => ({ t: e.t, x: e.x, y: e.y })));
|
|
144
|
-
const first = target[0].t;
|
|
145
|
-
const last = target.at(-1).t;
|
|
146
|
-
let path;
|
|
147
|
-
let lead = 0;
|
|
148
|
-
if (smoothing === 0) {
|
|
149
|
-
path = target.map((p) => ({ ...p }));
|
|
150
|
-
if (endSec !== null && endSec > last)
|
|
151
|
-
path.push({ ...target.at(-1), t: endSec });
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
const spring = springFor(smoothing);
|
|
155
|
-
lead = spring.lead;
|
|
156
|
-
const from = Math.max(0, first - lead);
|
|
157
|
-
const to = Math.max(from, endSec ?? last + 8 / spring.omega);
|
|
158
|
-
path = springPath(target, spring.omega, lead, from, to);
|
|
159
|
-
}
|
|
160
|
-
const start = Math.max(0, first - lead);
|
|
161
|
-
const visible = hideAfter === 0
|
|
162
|
-
? [[start, endSec ?? Infinity]]
|
|
163
|
-
: mergeWindows(samples.map((e) => [Math.max(0, e.t - lead), e.t + hideAfter]));
|
|
164
|
-
const clicks = samples.filter((e) => e.type === "down").map((e) => ({ t: e.t, x: e.x, y: e.y }));
|
|
165
|
-
return {
|
|
166
|
-
path,
|
|
167
|
-
visible: visible.map(([a, b]) => [r3(a), endSec !== null ? Math.min(b, endSec) : b]).filter(([a, b]) => b > a),
|
|
168
|
-
clicks,
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
const IDENTITY = { sx: 1, sy: 1, ox: 0, oy: 0 };
|
|
172
|
-
export function transformCursor(plan, ops = [], geo = IDENTITY) {
|
|
173
|
-
const move = (p) => {
|
|
174
|
-
const t = mapTime(p.t, ops);
|
|
175
|
-
return t === null ? null : { t: r3(t), x: p.x * geo.sx + geo.ox, y: p.y * geo.sy + geo.oy };
|
|
176
|
-
};
|
|
177
|
-
const path = [];
|
|
178
|
-
for (const p of plan.path) {
|
|
179
|
-
const m = move(p);
|
|
180
|
-
if (!m)
|
|
181
|
-
continue;
|
|
182
|
-
if (path.length > 0 && path.at(-1).t >= m.t)
|
|
183
|
-
path[path.length - 1] = { ...m, t: path.at(-1).t };
|
|
184
|
-
else
|
|
185
|
-
path.push(m);
|
|
186
|
-
}
|
|
187
|
-
const visible = plan.visible.flatMap(([a, b]) => {
|
|
188
|
-
const w = mapWindow(a, b, ops);
|
|
189
|
-
return w && w[1] - w[0] >= 0.05 ? [[r3(w[0]), Number.isFinite(w[1]) ? r3(w[1]) : w[1]]] : [];
|
|
190
|
-
});
|
|
191
|
-
const clicks = plan.clicks.flatMap((c) => {
|
|
192
|
-
const m = move(c);
|
|
193
|
-
return m ? [m] : [];
|
|
194
|
-
});
|
|
195
|
-
return { path: simplifyPath(path), visible, clicks };
|
|
196
|
-
}
|
|
197
|
-
const num = (n, d = 2) => {
|
|
198
|
-
const v = Number(n.toFixed(d));
|
|
199
|
-
return Object.is(v, -0) ? "0" : String(v);
|
|
200
|
-
};
|
|
201
|
-
export function linearExpr(v0, t0, k) {
|
|
202
|
-
const kk = Number(k.toFixed(3));
|
|
203
|
-
if (kk === 0)
|
|
204
|
-
return num(v0);
|
|
205
|
-
return `${num(v0)}${kk < 0 ? "-" : "+"}(t-${num(t0, 3)})*${num(Math.abs(kk), 3)}`;
|
|
206
|
-
}
|
|
207
|
-
export function cursorLayout(frameH, size = DEFAULT_CURSOR_SIZE) {
|
|
208
|
-
if (!(size >= 0.25 && size <= 8))
|
|
209
|
-
throw new ScreencastError("size must be between 0.25 and 8.");
|
|
210
|
-
const arrowH = Math.max(8, frameH * 0.025 * size);
|
|
211
|
-
const k = arrowH / CURSOR_SPRITE.arrowH;
|
|
212
|
-
const d = Math.max(12, Math.round(arrowH * 2.2));
|
|
213
|
-
return {
|
|
214
|
-
spriteW: Math.max(2, Math.round(CURSOR_SPRITE.w * k)),
|
|
215
|
-
spriteH: Math.max(2, Math.round(CURSOR_SPRITE.h * k)),
|
|
216
|
-
hotX: CURSOR_SPRITE.hotX * k,
|
|
217
|
-
hotY: CURSOR_SPRITE.hotY * k,
|
|
218
|
-
ripples: [0.45, 0.72, 1].map((f) => Math.max(6, Math.round((d * f) / 2) * 2)),
|
|
219
|
-
spotRadius: Math.max(24, Math.round(frameH * 0.12)),
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
export function formatCommands(cmds) {
|
|
223
|
-
const byTime = new Map();
|
|
224
|
-
for (const c of [...cmds].sort((a, b) => a.t - b.t)) {
|
|
225
|
-
const key = num(Math.max(0, c.t), 3);
|
|
226
|
-
const list = byTime.get(key) ?? [];
|
|
227
|
-
list.push(c.text);
|
|
228
|
-
byTime.set(key, list);
|
|
229
|
-
}
|
|
230
|
-
return [...byTime.entries()].map(([t, list]) => `${t} ${list.join(", ")};`).join("\n") + "\n";
|
|
231
|
-
}
|
|
232
|
-
function pathCommands(path, target, dx, dy) {
|
|
233
|
-
return path.map((p, i) => {
|
|
234
|
-
const n = path[i + 1];
|
|
235
|
-
const span = n ? n.t - p.t : 0;
|
|
236
|
-
const kx = n && span > 0 ? (n.x - p.x) / span : 0;
|
|
237
|
-
const ky = n && span > 0 ? (n.y - p.y) / span : 0;
|
|
238
|
-
return {
|
|
239
|
-
t: p.t,
|
|
240
|
-
text: `${target} x ${linearExpr(p.x + dx, p.t, kx)}, ${target} y ${linearExpr(p.y + dy, p.t, ky)}`,
|
|
241
|
-
};
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
function windowCommands(windows, target) {
|
|
245
|
-
return windows.flatMap(([a, b]) => [
|
|
246
|
-
{ t: a, text: `${target} enable 1` },
|
|
247
|
-
...(Number.isFinite(b) ? [{ t: b, text: `${target} enable 0` }] : []),
|
|
248
|
-
]);
|
|
249
|
-
}
|
|
250
|
-
export function cursorSteps(plan, style, frame, input, firstInput, tag, sprite, commandsFile) {
|
|
251
|
-
if (plan.path.length === 0)
|
|
252
|
-
throw new ScreencastError("No cursor positions to draw.");
|
|
253
|
-
const L = cursorLayout(frame.height, style.size);
|
|
254
|
-
const { width: W, height: H } = frame;
|
|
255
|
-
const name = (s) => `overlay@${tag}c${s}`;
|
|
256
|
-
const steps = [];
|
|
257
|
-
const cmds = [];
|
|
258
|
-
const p0 = plan.path[0];
|
|
259
|
-
let cur = `[${tag}cm]`;
|
|
260
|
-
steps.push(`${input}sendcmd=f=${escapeFilterPath(commandsFile)}${cur}`);
|
|
261
|
-
const layer = (src, id, dx, dy, windows) => {
|
|
262
|
-
const out = `[${tag}c${id}o]`;
|
|
263
|
-
steps.push(`${cur}${src}${name(id)}=x=${num(p0.x + dx)}:y=${num(p0.y + dy)}:enable=0${out}`);
|
|
264
|
-
cmds.push(...windowCommands(windows, name(id)));
|
|
265
|
-
cur = out;
|
|
266
|
-
};
|
|
267
|
-
if (style.spotlight) {
|
|
268
|
-
const q = 4;
|
|
269
|
-
const mw = Math.ceil((2 * W) / q);
|
|
270
|
-
const mh = Math.ceil((2 * H) / q);
|
|
271
|
-
const r = L.spotRadius / q;
|
|
272
|
-
const feather = r * 0.8;
|
|
273
|
-
const a = `255*0.55*clip((hypot(X-${num(mw / 2)},Y-${num(mh / 2)})-${num(r)})/${num(feather)},0,1)`;
|
|
274
|
-
steps.push(`color=c=black:s=${mw}x${mh}:r=1:d=1,format=rgba,geq=r=0:g=0:b=0:a='${a}',scale=${2 * W}:${2 * H}[${tag}csm]`);
|
|
275
|
-
const cx = L.spriteW * 0.25 - W;
|
|
276
|
-
const cy = L.spriteH * 0.3 - H;
|
|
277
|
-
layer(`[${tag}csm]`, "s", cx, cy, plan.visible);
|
|
278
|
-
cmds.push(...pathCommands(plan.path, name("s"), cx, cy));
|
|
279
|
-
}
|
|
280
|
-
if (style.clicks !== false && plan.clicks.length > 0) {
|
|
281
|
-
const third = RIPPLE_SEC / 3;
|
|
282
|
-
L.ripples.forEach((d, k) => {
|
|
283
|
-
const c = d / 2;
|
|
284
|
-
const th = Math.max(1.5, d * 0.05);
|
|
285
|
-
const dist = `abs(hypot(X-${num(c - 0.5)},Y-${num(c - 0.5)})-${num(c - th - 1)})`;
|
|
286
|
-
const alpha = [0.9, 0.65, 0.4][k];
|
|
287
|
-
const white = `255*lt(${dist},${num(th * 0.45)})`;
|
|
288
|
-
steps.push(`color=c=black:s=${d}x${d}:r=1:d=1,format=rgba,geq=r='${white}':g='${white}':b='${white}':` +
|
|
289
|
-
`a='255*${alpha}*clip(${num(th)}-${dist},0,1)'[${tag}cr${k}]`);
|
|
290
|
-
const windows = plan.clicks.map((p) => [p.t + k * third, p.t + (k + 1) * third]);
|
|
291
|
-
layer(`[${tag}cr${k}]`, `r${k}`, -c, -c, []);
|
|
292
|
-
for (const [i, p] of plan.clicks.entries()) {
|
|
293
|
-
const [a, b] = windows[i];
|
|
294
|
-
const next = windows[i + 1];
|
|
295
|
-
cmds.push({ t: a, text: `${name(`r${k}`)} x ${num(p.x - c)}, ${name(`r${k}`)} y ${num(p.y - c)}, ${name(`r${k}`)} enable 1` });
|
|
296
|
-
if (!next || next[0] >= b)
|
|
297
|
-
cmds.push({ t: b, text: `${name(`r${k}`)} enable 0` });
|
|
298
|
-
}
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
steps.push(`[${firstInput}:v]scale=${L.spriteW}:${L.spriteH}:flags=lanczos,format=rgba[${tag}csp]`);
|
|
302
|
-
layer(`[${tag}csp]`, "p", -L.hotX, -L.hotY, plan.visible);
|
|
303
|
-
cmds.push(...pathCommands(plan.path, name("p"), -L.hotX, -L.hotY));
|
|
304
|
-
return { steps, out: cur, extraInputs: ["-i", sprite], commands: formatCommands(cmds) };
|
|
305
|
-
}
|
|
306
|
-
export function buildCursorArgs(input, output, plan, style, frame, sprite, commandsFile, quality = "standard") {
|
|
307
|
-
const { steps, out, extraInputs, commands } = cursorSteps(plan, style, frame, "[0:v]", 1, "", sprite, commandsFile);
|
|
308
|
-
return {
|
|
309
|
-
args: [
|
|
310
|
-
"-y", "-i", input, ...extraInputs,
|
|
311
|
-
"-filter_complex", steps.join(";"),
|
|
312
|
-
"-map", out, "-map", "0:a?",
|
|
313
|
-
...resolveQuality(quality),
|
|
314
|
-
"-c:a", "copy",
|
|
315
|
-
output,
|
|
316
|
-
],
|
|
317
|
-
commands,
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
export const cursorOptionsSchema = {
|
|
321
|
-
events: z.string().optional(),
|
|
322
|
-
size: z.number().min(0.25).max(8).optional(),
|
|
323
|
-
smoothing: z.number().min(0).max(1).optional(),
|
|
324
|
-
hideAfterSec: z.number().min(0).max(600).optional(),
|
|
325
|
-
clicks: z.boolean().optional(),
|
|
326
|
-
spotlight: z.boolean().optional(),
|
|
327
|
-
};
|
|
328
|
-
export function loadCursor(input, events, opts, endSec) {
|
|
329
|
-
const path = events ?? eventPaths(input).jsonl;
|
|
330
|
-
if (!existsSync(path)) {
|
|
331
|
-
throw new ScreencastError(`No event log for ${input} (looked for ${path}). Record with start_recording cursor: "events" ` +
|
|
332
|
-
"(no OS cursor in the video, events logged), or pass events: <path to .events.jsonl>.");
|
|
333
|
-
}
|
|
334
|
-
const plan = planCursor(parseEventsJsonl(readFileSync(path, "utf8")), opts, endSec);
|
|
335
|
-
if (plan.path.length === 0)
|
|
336
|
-
throw new ScreencastError(`The event log ${path} has no cursor positions to draw.`);
|
|
337
|
-
return plan;
|
|
338
|
-
}
|