@gnawbit/tabretake 0.0.0-stage → 0.4.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/CHANGELOG.md +83 -0
- package/LICENSE +21 -0
- package/README.md +534 -2
- package/README.zh-CN.md +493 -0
- package/SKILL.md +157 -0
- package/agents/openai.yaml +4 -0
- package/bin/demo-reel.mjs +2 -0
- package/bin/tabretake.mjs +12 -0
- package/examples/site/app.js +87 -0
- package/examples/site/index.html +168 -0
- package/examples/site/style.css +705 -0
- package/examples/stories/readme.json +34 -0
- package/examples/stories/release.json +48 -0
- package/examples/story-video.json +32 -0
- package/examples/story.json +46 -0
- package/package.json +61 -4
- package/references/rendering.md +72 -0
- package/references/story.md +80 -0
- package/schemas/story.schema.json +610 -0
- package/scripts/capabilities.mjs +22 -0
- package/scripts/cli.mjs +106 -0
- package/scripts/demo.mjs +39 -0
- package/scripts/doctor.mjs +83 -0
- package/scripts/init.mjs +46 -0
- package/scripts/lib/browser.mjs +208 -0
- package/scripts/lib/camera.mjs +63 -0
- package/scripts/lib/captions.mjs +39 -0
- package/scripts/lib/config.mjs +76 -0
- package/scripts/lib/ffmpeg.mjs +60 -0
- package/scripts/lib/fonts.mjs +36 -0
- package/scripts/lib/metadata.mjs +14 -0
- package/scripts/lib/navigation.mjs +26 -0
- package/scripts/lib/options.mjs +65 -0
- package/scripts/lib/report.mjs +42 -0
- package/scripts/lib/tty.mjs +18 -0
- package/scripts/plan.mjs +214 -0
- package/scripts/record.mjs +352 -0
- package/scripts/render.mjs +563 -0
- package/scripts/serve.mjs +74 -0
- package/scripts/setup.mjs +23 -0
- package/scripts/validate.mjs +18 -0
package/scripts/cli.mjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { version } from "./lib/metadata.mjs";
|
|
2
|
+
|
|
3
|
+
const HELP = `
|
|
4
|
+
TabRetake — turn any URL into a polished demo video and GIF.
|
|
5
|
+
|
|
6
|
+
Usage:
|
|
7
|
+
tabretake setup [--with-deps]
|
|
8
|
+
tabretake --version
|
|
9
|
+
tabretake capabilities [--json]
|
|
10
|
+
tabretake doctor [--json]
|
|
11
|
+
tabretake validate --config demo.json [--json]
|
|
12
|
+
tabretake init --url <url> [--out demo.json]
|
|
13
|
+
tabretake plan --url <url> [--out plan.json] [--login] [--storage-state auth.json]
|
|
14
|
+
tabretake record --config demo.json [--out demo] [--login] [--storage-state auth.json]
|
|
15
|
+
tabretake preview --config demo.json [--headless] [--json]
|
|
16
|
+
tabretake render --config demo.json [--out demo] [--video recording.mp4] [--vertical] [--no-gif]
|
|
17
|
+
tabretake serve [--port 4173] [--dir examples/site]
|
|
18
|
+
tabretake demo [--story readme|onboarding|release] [--out demo]
|
|
19
|
+
|
|
20
|
+
Examples:
|
|
21
|
+
tabretake init --url https://your-app.com
|
|
22
|
+
tabretake plan --url https://your-app.com
|
|
23
|
+
tabretake record --config demo.json
|
|
24
|
+
tabretake render --config demo.json
|
|
25
|
+
tabretake serve
|
|
26
|
+
tabretake record --config examples/story.json --out demo
|
|
27
|
+
|
|
28
|
+
Options:
|
|
29
|
+
--json Machine-readable results for init, plan, record and render.
|
|
30
|
+
--login Open a visible browser so you can log in once.
|
|
31
|
+
--storage-state Reuse a saved Playwright storage state (cookies/localStorage).
|
|
32
|
+
--proxy Proxy server, e.g. http://127.0.0.1:10808
|
|
33
|
+
--viewport Viewport, e.g. 1440x810 (default from config)
|
|
34
|
+
--headed Watch the recording happen (default false)
|
|
35
|
+
--vertical Also render a 9:16 vertical video + GIF.
|
|
36
|
+
--video Use an existing screen recording; the config needs "marks".
|
|
37
|
+
--no-gif Skip GIF export.
|
|
38
|
+
--poster-at Choose the poster time in output seconds, e.g. 4.5.
|
|
39
|
+
--zoom-duration Ease between click targets in seconds (default 0.35; 0 for cuts).
|
|
40
|
+
`;
|
|
41
|
+
|
|
42
|
+
export async function main(argv) {
|
|
43
|
+
const [command, ...rest] = argv;
|
|
44
|
+
|
|
45
|
+
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
46
|
+
console.log(HELP);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
switch (command) {
|
|
51
|
+
case "preview": {
|
|
52
|
+
const { record } = await import("./record.mjs");
|
|
53
|
+
return record(rest, "preview");
|
|
54
|
+
}
|
|
55
|
+
case "validate": {
|
|
56
|
+
const { validate } = await import("./validate.mjs");
|
|
57
|
+
return validate(rest);
|
|
58
|
+
}
|
|
59
|
+
case "--version":
|
|
60
|
+
case "-v": {
|
|
61
|
+
if (rest.length) throw new Error("--version does not accept arguments");
|
|
62
|
+
console.log(version);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
case "capabilities": {
|
|
66
|
+
const { capabilities } = await import("./capabilities.mjs");
|
|
67
|
+
return capabilities(rest);
|
|
68
|
+
}
|
|
69
|
+
case "setup": {
|
|
70
|
+
const { setup } = await import("./setup.mjs");
|
|
71
|
+
return setup(rest);
|
|
72
|
+
}
|
|
73
|
+
case "init": {
|
|
74
|
+
const { init } = await import("./init.mjs");
|
|
75
|
+
return init(rest);
|
|
76
|
+
}
|
|
77
|
+
case "doctor": {
|
|
78
|
+
const { doctor } = await import("./doctor.mjs");
|
|
79
|
+
return doctor(rest);
|
|
80
|
+
}
|
|
81
|
+
case "plan": {
|
|
82
|
+
const { plan } = await import("./plan.mjs");
|
|
83
|
+
return plan(rest);
|
|
84
|
+
}
|
|
85
|
+
case "record": {
|
|
86
|
+
const { record } = await import("./record.mjs");
|
|
87
|
+
return record(rest);
|
|
88
|
+
}
|
|
89
|
+
case "render": {
|
|
90
|
+
const { render } = await import("./render.mjs");
|
|
91
|
+
return render(rest);
|
|
92
|
+
}
|
|
93
|
+
case "serve": {
|
|
94
|
+
const { serve } = await import("./serve.mjs");
|
|
95
|
+
return serve(rest);
|
|
96
|
+
}
|
|
97
|
+
case "demo": {
|
|
98
|
+
const { demo } = await import("./demo.mjs");
|
|
99
|
+
return demo(rest);
|
|
100
|
+
}
|
|
101
|
+
default:
|
|
102
|
+
console.error(`Unknown command: ${command}\n`);
|
|
103
|
+
console.log(HELP);
|
|
104
|
+
process.exitCode = 1;
|
|
105
|
+
}
|
|
106
|
+
}
|
package/scripts/demo.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { parseCommand } from "./lib/options.mjs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { record } from "./record.mjs";
|
|
5
|
+
import { render } from "./render.mjs";
|
|
6
|
+
import { startServer } from "./serve.mjs";
|
|
7
|
+
|
|
8
|
+
export async function demo(argv) {
|
|
9
|
+
const { values } = parseCommand("demo", argv);
|
|
10
|
+
|
|
11
|
+
const stories = new Map([
|
|
12
|
+
["onboarding", "../examples/story.json"],
|
|
13
|
+
["readme", "../examples/stories/readme.json"],
|
|
14
|
+
["release", "../examples/stories/release.json"],
|
|
15
|
+
]);
|
|
16
|
+
if (!stories.has(values.story)) {
|
|
17
|
+
throw new Error("Unknown demo story. Choose readme, onboarding or release.");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const server = await startServer({
|
|
21
|
+
port: Number(values.port),
|
|
22
|
+
dir: "examples/site",
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
console.log(`Demo site: ${server.url}\n`);
|
|
27
|
+
await record([
|
|
28
|
+
"--config",
|
|
29
|
+
fileURLToPath(new URL(stories.get(values.story), import.meta.url)),
|
|
30
|
+
"--url",
|
|
31
|
+
server.url,
|
|
32
|
+
"--out",
|
|
33
|
+
values.out,
|
|
34
|
+
]);
|
|
35
|
+
await render(["--config", path.join(values.out, "record.json"), "--vertical"]);
|
|
36
|
+
} finally {
|
|
37
|
+
await server.close();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
import { findCjkFont, findFont } from "./lib/fonts.mjs";
|
|
5
|
+
import { parseCommand } from "./lib/options.mjs";
|
|
6
|
+
import { nodeRange, schemaVersion, supportsNodeVersion, version } from "./lib/metadata.mjs";
|
|
7
|
+
|
|
8
|
+
const exec = promisify(execFile);
|
|
9
|
+
const reinstall = "Reinstall CLI dependencies (from source: npm ci).";
|
|
10
|
+
|
|
11
|
+
// Load native dependencies separately so broken installs still produce a report.
|
|
12
|
+
export async function diagnose({
|
|
13
|
+
nodeVersion = process.versions.node,
|
|
14
|
+
load = (name) => import(name),
|
|
15
|
+
exists = existsSync,
|
|
16
|
+
run = (file) => exec(file, ["-version"], { windowsHide: true, timeout: 10000 }),
|
|
17
|
+
fonts = { latin: findFont, cjk: findCjkFont },
|
|
18
|
+
} = {}) {
|
|
19
|
+
const checks = [];
|
|
20
|
+
async function check(id, required, hint, probe) {
|
|
21
|
+
try {
|
|
22
|
+
checks.push({ id, required, status: "ok", detail: await probe() });
|
|
23
|
+
} catch (error) {
|
|
24
|
+
checks.push({ id, required, status: required ? "error" : "warning", detail: error.message, hint });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
await check("node", true, `Install Node.js ${nodeRange}.`, () => {
|
|
28
|
+
if (!supportsNodeVersion(nodeVersion)) throw new Error(`Unsupported Node.js ${nodeVersion}`);
|
|
29
|
+
return nodeVersion;
|
|
30
|
+
});
|
|
31
|
+
for (const id of ["ffmpeg", "ffprobe"]) {
|
|
32
|
+
await check(id, true, reinstall, async () => {
|
|
33
|
+
const module = await load(`@${id}-installer/${id}`);
|
|
34
|
+
const file = module.default.path;
|
|
35
|
+
if (!file || !exists(file)) throw new Error("Executable not found");
|
|
36
|
+
const { stdout, stderr } = await run(file);
|
|
37
|
+
return (stdout || stderr).split("\n")[0].trim();
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
await check("chromium", true, "Run tabretake setup (from source: node bin/tabretake.mjs setup).", async () => {
|
|
41
|
+
const { chromium } = await load("playwright");
|
|
42
|
+
const file = chromium.executablePath();
|
|
43
|
+
if (!exists(file)) throw new Error("Chromium executable not found");
|
|
44
|
+
return "Executable found; browser launch and system libraries are not tested.";
|
|
45
|
+
});
|
|
46
|
+
await check("sharp", true, reinstall, async () => {
|
|
47
|
+
const { default: sharp } = await load("sharp");
|
|
48
|
+
await sharp({ create: { width: 1, height: 1, channels: 4, background: "white" } }).png().toBuffer();
|
|
49
|
+
return sharp.versions?.sharp || "Image encoding passed";
|
|
50
|
+
});
|
|
51
|
+
await check("config-validator", true, reinstall, async () => {
|
|
52
|
+
const { inspectConfig } = await load("./lib/config.mjs");
|
|
53
|
+
if (!inspectConfig({ marks: [] }).ok) throw new Error("Config schema could not be validated");
|
|
54
|
+
return "Config schema loaded";
|
|
55
|
+
});
|
|
56
|
+
await check("font.latin", false, "Install Arial, Segoe UI or DejaVu Sans; captions may otherwise be skipped.", () => {
|
|
57
|
+
if (!fonts.latin()) throw new Error("Caption font not found");
|
|
58
|
+
return "Caption font found";
|
|
59
|
+
});
|
|
60
|
+
await check("font.cjk", false, "For Chinese captions, install Microsoft YaHei, PingFang or Noto Sans CJK.", () => {
|
|
61
|
+
if (!fonts.cjk()) throw new Error("CJK font not found");
|
|
62
|
+
return "CJK font found";
|
|
63
|
+
});
|
|
64
|
+
const errors = checks.filter((check) => check.status === "error").length;
|
|
65
|
+
const warnings = checks.filter((check) => check.status === "warning").length;
|
|
66
|
+
return { schemaVersion, version, ok: errors === 0, checks, summary: { errors, warnings } };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function doctor(argv = []) {
|
|
70
|
+
const { values } = parseCommand("doctor", argv);
|
|
71
|
+
const report = await diagnose();
|
|
72
|
+
if (values.json) console.log(JSON.stringify(report));
|
|
73
|
+
else {
|
|
74
|
+
console.log(`\ntabretake doctor (${version})\n`);
|
|
75
|
+
for (const check of report.checks) {
|
|
76
|
+
console.log(`${check.status.toUpperCase().padEnd(7)} ${check.id.padEnd(14)} ${check.detail}`);
|
|
77
|
+
if (check.hint) console.log(` -> ${check.hint}`);
|
|
78
|
+
}
|
|
79
|
+
console.log(`\n${report.summary.errors} error(s), ${report.summary.warnings} warning(s).\n`);
|
|
80
|
+
}
|
|
81
|
+
if (!report.ok) process.exitCode = 1;
|
|
82
|
+
return report;
|
|
83
|
+
}
|
package/scripts/init.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { parseCommand } from "./lib/options.mjs";
|
|
4
|
+
import { createReporter } from "./lib/report.mjs";
|
|
5
|
+
|
|
6
|
+
export async function init(argv) {
|
|
7
|
+
const { values } = parseCommand("init", argv);
|
|
8
|
+
const reporter = createReporter("init", values.json);
|
|
9
|
+
|
|
10
|
+
if (!values.url) throw new Error("Provide --url https://your-app.com");
|
|
11
|
+
let url;
|
|
12
|
+
try {
|
|
13
|
+
url = new URL(values.url);
|
|
14
|
+
} catch {
|
|
15
|
+
throw new Error("Use a complete http:// or https:// URL");
|
|
16
|
+
}
|
|
17
|
+
if (!["http:", "https:"].includes(url.protocol)) {
|
|
18
|
+
throw new Error("Use a complete http:// or https:// URL");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const story = {
|
|
22
|
+
url: url.href,
|
|
23
|
+
displayUrl: url.host,
|
|
24
|
+
viewport: { width: 1440, height: 810 },
|
|
25
|
+
initialWait: 700,
|
|
26
|
+
tail: 1200,
|
|
27
|
+
brand: { background: "#070a12", accent: "#6c5ce7" },
|
|
28
|
+
steps: [{ action: "wait", ms: 1500 }],
|
|
29
|
+
};
|
|
30
|
+
const outPath = path.resolve(values.out);
|
|
31
|
+
await mkdir(path.dirname(outPath), { recursive: true });
|
|
32
|
+
try {
|
|
33
|
+
await writeFile(outPath, JSON.stringify(story, null, 2) + "\n", { flag: "wx" });
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (error.code === "EEXIST") {
|
|
36
|
+
throw new Error("Story file already exists; edit it or choose a different --out path");
|
|
37
|
+
}
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
reporter.log(`Created story: ${outPath}`);
|
|
42
|
+
reporter.log("The starter records a static page. Use plan to find controls, then add click/type steps.");
|
|
43
|
+
reporter.log("Run record with this config, then render the resulting record.json.");
|
|
44
|
+
await reporter.success({ artifacts: [{ kind: "story", path: outPath }] });
|
|
45
|
+
return outPath;
|
|
46
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { chromium, errors } from "playwright";
|
|
2
|
+
|
|
3
|
+
export async function launchBrowser({ headless = true, proxy } = {}) {
|
|
4
|
+
return chromium.launch({
|
|
5
|
+
headless,
|
|
6
|
+
proxy: proxy ? { server: proxy } : undefined,
|
|
7
|
+
args: ["--disable-blink-features=AutomationControlled"],
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function newContext(browser, options) {
|
|
12
|
+
const {
|
|
13
|
+
viewport = { width: 1440, height: 810 },
|
|
14
|
+
videoDir,
|
|
15
|
+
storageState,
|
|
16
|
+
} = options;
|
|
17
|
+
|
|
18
|
+
const context = await browser.newContext({
|
|
19
|
+
viewport,
|
|
20
|
+
storageState: storageState || undefined,
|
|
21
|
+
recordVideo: videoDir
|
|
22
|
+
? {
|
|
23
|
+
dir: videoDir,
|
|
24
|
+
size: { width: viewport.width, height: viewport.height },
|
|
25
|
+
}
|
|
26
|
+
: undefined,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return context;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Draws a fake cursor + click ripple on top of the page, so the recording
|
|
34
|
+
* looks like a human is using it.
|
|
35
|
+
*/
|
|
36
|
+
export async function installCursor(page) {
|
|
37
|
+
await page.addInitScript(() => {
|
|
38
|
+
if (window.__tabretakeCursorInstalled) return;
|
|
39
|
+
window.__tabretakeCursorInstalled = true;
|
|
40
|
+
|
|
41
|
+
const CSS = `
|
|
42
|
+
#__tabretake_cursor {
|
|
43
|
+
position: fixed; left: 0; top: 0; width: 22px; height: 22px;
|
|
44
|
+
z-index: 2147483647; pointer-events: none; will-change: transform;
|
|
45
|
+
filter: drop-shadow(0 2px 4px rgba(0,0,0,.35));
|
|
46
|
+
}
|
|
47
|
+
#__tabretake_ripple {
|
|
48
|
+
position: fixed; width: 14px; height: 14px; border-radius: 50%;
|
|
49
|
+
background: rgba(108,92,231,.35); border: 2px solid rgba(255,255,255,.9);
|
|
50
|
+
z-index: 2147483646; pointer-events: none;
|
|
51
|
+
transform: translate(-50%,-50%) scale(.2); opacity: 0;
|
|
52
|
+
}
|
|
53
|
+
`;
|
|
54
|
+
|
|
55
|
+
const style = document.createElement("style");
|
|
56
|
+
style.textContent = CSS;
|
|
57
|
+
document.documentElement.appendChild(style);
|
|
58
|
+
|
|
59
|
+
const cursor = document.createElement("div");
|
|
60
|
+
cursor.id = "__tabretake_cursor";
|
|
61
|
+
cursor.innerHTML = `
|
|
62
|
+
<svg width="22" height="22" viewBox="0 0 24 24" fill="none">
|
|
63
|
+
<path d="M4 2L20.5 12.2L12.6 13.7L9.1 21.2L4 2Z"
|
|
64
|
+
fill="#fff" stroke="#111" stroke-width="1.4" stroke-linejoin="round"/>
|
|
65
|
+
</svg>`;
|
|
66
|
+
document.documentElement.appendChild(cursor);
|
|
67
|
+
|
|
68
|
+
const move = (x, y) => {
|
|
69
|
+
cursor.style.transform = `translate(${x - 2}px, ${y - 2}px)`;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const ripple = (x, y) => {
|
|
73
|
+
const r = document.createElement("div");
|
|
74
|
+
r.id = "__tabretake_ripple";
|
|
75
|
+
r.style.left = `${x}px`;
|
|
76
|
+
r.style.top = `${y}px`;
|
|
77
|
+
document.documentElement.appendChild(r);
|
|
78
|
+
r.animate(
|
|
79
|
+
[
|
|
80
|
+
{ transform: "translate(-50%,-50%) scale(.2)", opacity: 1 },
|
|
81
|
+
{ transform: "translate(-50%,-50%) scale(2.8)", opacity: 0 },
|
|
82
|
+
],
|
|
83
|
+
{ duration: 520, easing: "cubic-bezier(.2,.8,.2,1)" },
|
|
84
|
+
);
|
|
85
|
+
setTimeout(() => r.remove(), 560);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
window.addEventListener("mousemove", (e) => move(e.clientX, e.clientY), true);
|
|
89
|
+
window.addEventListener("mousedown", (e) => ripple(e.clientX, e.clientY), true);
|
|
90
|
+
move(window.innerWidth / 2, window.innerHeight / 2);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function resolveLocator(page, step) {
|
|
95
|
+
const candidates = [];
|
|
96
|
+
|
|
97
|
+
if (step.selector) candidates.push(page.locator(step.selector));
|
|
98
|
+
|
|
99
|
+
if (step.role) {
|
|
100
|
+
for (const exact of [true, false]) {
|
|
101
|
+
candidates.push(page.getByRole(step.role, { name: step.name || step.text, exact }));
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
const names = [...new Set([step.name, step.text].filter(Boolean))];
|
|
105
|
+
// Prefer exact controls, then partial controls, before plain-text fallback.
|
|
106
|
+
for (const exact of [true, false]) {
|
|
107
|
+
for (const name of names) {
|
|
108
|
+
candidates.push(page.getByRole("button", { name, exact }));
|
|
109
|
+
candidates.push(page.getByRole("link", { name, exact }));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
for (const exact of [true, false]) {
|
|
113
|
+
for (const name of names) candidates.push(page.getByText(name, { exact }));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
for (const locator of candidates) {
|
|
118
|
+
try {
|
|
119
|
+
const count = await locator.count();
|
|
120
|
+
for (let i = 0; i < count; i += 1) {
|
|
121
|
+
const item = locator.nth(i);
|
|
122
|
+
if (await item.isVisible().catch(() => false)) return item;
|
|
123
|
+
}
|
|
124
|
+
} catch {
|
|
125
|
+
// try the next candidate
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
throw Object.assign(new Error("Could not find a visible element for this step."), { code: "TARGET_NOT_FOUND" });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export async function moveAndClick(page, locator, pause = 700, timeout = 20000) {
|
|
133
|
+
const deadline = Date.now() + timeout;
|
|
134
|
+
const remaining = () => {
|
|
135
|
+
const ms = deadline - Date.now();
|
|
136
|
+
if (ms <= 0) throw new errors.TimeoutError("Timed out waiting for an actionable click target.");
|
|
137
|
+
return ms;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// Wait before measuring: loading overlays or disabled controls may also move.
|
|
141
|
+
await locator.click({ trial: true, timeout: remaining() });
|
|
142
|
+
const box = await locator.boundingBox({ timeout: remaining() });
|
|
143
|
+
if (!box) throw new Error("Element has no bounding box");
|
|
144
|
+
|
|
145
|
+
const x = box.x + box.width / 2;
|
|
146
|
+
const y = box.y + box.height / 2;
|
|
147
|
+
|
|
148
|
+
await page.mouse.move(x, y, { steps: 18 });
|
|
149
|
+
await page.waitForTimeout(120);
|
|
150
|
+
// Recheck actionability at the actual click; never click an overlay by position.
|
|
151
|
+
await locator.click({ delay: 70, timeout: remaining() });
|
|
152
|
+
await page.waitForTimeout(pause);
|
|
153
|
+
|
|
154
|
+
return { x, y };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export async function clickAndType(page, locator, text, pause = 500) {
|
|
158
|
+
await locator.scrollIntoViewIfNeeded();
|
|
159
|
+
const box = await locator.boundingBox();
|
|
160
|
+
if (!box) throw new Error("Element has no bounding box");
|
|
161
|
+
|
|
162
|
+
const x = box.x + box.width / 2;
|
|
163
|
+
const y = box.y + box.height / 2;
|
|
164
|
+
|
|
165
|
+
await page.mouse.move(x, y, { steps: 16 });
|
|
166
|
+
await page.mouse.down();
|
|
167
|
+
await page.mouse.up();
|
|
168
|
+
await page.waitForTimeout(150);
|
|
169
|
+
|
|
170
|
+
await locator.fill("");
|
|
171
|
+
await page.keyboard.type(text, { delay: 45 });
|
|
172
|
+
await page.waitForTimeout(pause);
|
|
173
|
+
|
|
174
|
+
return { x, y };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export async function dragBy(page, locator, dx, dy = 0, steps = 24, pause = 600) {
|
|
178
|
+
await locator.scrollIntoViewIfNeeded();
|
|
179
|
+
const box = await locator.boundingBox();
|
|
180
|
+
if (!box) throw new Error("Element has no bounding box");
|
|
181
|
+
|
|
182
|
+
const x = box.x + box.width / 2;
|
|
183
|
+
const y = box.y + box.height / 2;
|
|
184
|
+
const endX = x + Number(dx || 0);
|
|
185
|
+
const endY = y + Number(dy || 0);
|
|
186
|
+
|
|
187
|
+
await page.mouse.move(x, y, { steps: 12 });
|
|
188
|
+
await page.waitForTimeout(100);
|
|
189
|
+
await page.mouse.down();
|
|
190
|
+
await page.waitForTimeout(80);
|
|
191
|
+
await page.mouse.move(endX, endY, { steps: Number(steps || 24) });
|
|
192
|
+
await page.waitForTimeout(120);
|
|
193
|
+
await page.mouse.up();
|
|
194
|
+
await page.waitForTimeout(pause);
|
|
195
|
+
|
|
196
|
+
return { x: endX, y: endY };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export async function hover(page, locator, pause = 500) {
|
|
200
|
+
await locator.scrollIntoViewIfNeeded();
|
|
201
|
+
const box = await locator.boundingBox();
|
|
202
|
+
if (!box) throw new Error("Element has no bounding box");
|
|
203
|
+
const x = box.x + box.width / 2;
|
|
204
|
+
const y = box.y + box.height / 2;
|
|
205
|
+
await page.mouse.move(x, y, { steps: 16 });
|
|
206
|
+
await page.waitForTimeout(pause);
|
|
207
|
+
return { x, y };
|
|
208
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
|
2
|
+
const ease = (progress) => (1 - Math.cos(Math.PI * clamp(progress, 0, 1))) / 2;
|
|
3
|
+
const number = (value) => Number(value.toFixed(8)).toString();
|
|
4
|
+
|
|
5
|
+
export function buildCameraFilter({
|
|
6
|
+
events, durationSec, videoW, videoH, contentW, contentH,
|
|
7
|
+
scale = 1, zoom = 1.28, transition = 0.35, videoOffset = 0,
|
|
8
|
+
}) {
|
|
9
|
+
if (transition > 0) transition = Math.max(transition, 1 / 30);
|
|
10
|
+
// Work at twice the output size so integer crop positions move in half pixels.
|
|
11
|
+
const width = contentW * 2;
|
|
12
|
+
const height = contentH * 2;
|
|
13
|
+
const fit = Math.min(width / videoW, height / videoH);
|
|
14
|
+
const sourceW = Math.max(1, Math.round(videoW * fit));
|
|
15
|
+
const sourceH = Math.max(1, Math.round(videoH * fit));
|
|
16
|
+
const left = Math.floor((width - sourceW) / 2);
|
|
17
|
+
const top = Math.floor((height - sourceH) / 2);
|
|
18
|
+
const baseZoom = Math.max(width / sourceW, height / sourceH);
|
|
19
|
+
if (baseZoom * zoom > 10) {
|
|
20
|
+
throw new Error("Zoom is too large for this video aspect ratio; reduce --zoom");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const overview = { z: baseZoom, x: left + sourceW / 2, y: top + sourceH / 2 };
|
|
24
|
+
const moves = [];
|
|
25
|
+
for (const event of events.filter((event) => (
|
|
26
|
+
Number.isFinite(event.t) && event.t >= 0 && event.t < durationSec * 1000 &&
|
|
27
|
+
(event.zoom === false || (Number.isFinite(event.x) && Number.isFinite(event.y)))
|
|
28
|
+
)).sort((a, b) => a.t - b.t)) {
|
|
29
|
+
const start = event.t / 1000;
|
|
30
|
+
const previous = moves.at(-1);
|
|
31
|
+
const from = previous ? Object.fromEntries(["z", "x", "y"].map((key) => [
|
|
32
|
+
key, previous.from[key] + (previous.to[key] - previous.from[key]) *
|
|
33
|
+
(transition === 0 ? 1 : ease((start - previous.start) / transition)),
|
|
34
|
+
])) : overview;
|
|
35
|
+
const z = baseZoom * zoom;
|
|
36
|
+
const to = event.zoom === false ? overview : {
|
|
37
|
+
z,
|
|
38
|
+
x: clamp(left + event.x * scale * sourceW / videoW, left + width / z / 2, left + sourceW - width / z / 2),
|
|
39
|
+
y: clamp(top + event.y * scale * sourceH / videoH, top + height / z / 2, top + sourceH - height / z / 2),
|
|
40
|
+
};
|
|
41
|
+
moves.push({ start, from, to });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const expression = (key) => moves.reduce((prior, move) => {
|
|
45
|
+
const progress = `(1-cos(PI*clip((on/30-${number(move.start)})/${number(transition)},0,1)))/2`;
|
|
46
|
+
const value = transition === 0 ? number(move.to[key]) :
|
|
47
|
+
`${number(move.from[key])}+(${number(move.to[key] - move.from[key])})*(${progress})`;
|
|
48
|
+
return `if(gte(on/30,${number(move.start)}),${value},${prior})`;
|
|
49
|
+
}, number(overview[key]));
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
moves,
|
|
53
|
+
filter: [
|
|
54
|
+
`trim=start=${number(videoOffset)}:end=${number(videoOffset + durationSec)}`,
|
|
55
|
+
"setpts=PTS-STARTPTS", "fps=30", "format=yuv444p",
|
|
56
|
+
`scale=${sourceW}:${sourceH}:flags=lanczos`,
|
|
57
|
+
`pad=${width}:${height}:${left}:${top}`,
|
|
58
|
+
`zoompan=z='${expression("z")}':x='(${expression("x")})-iw/zoom/2':` +
|
|
59
|
+
`y='(${expression("y")})-ih/zoom/2':d=1:s=${contentW}x${contentH}:fps=30`,
|
|
60
|
+
"setsar=1",
|
|
61
|
+
].join(","),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import sharp from "sharp";
|
|
3
|
+
import { hasCjk } from "./fonts.mjs";
|
|
4
|
+
|
|
5
|
+
const escapeMarkup = (text) => String(text).replace(/&/g, "&")
|
|
6
|
+
.replace(/</g, "<").replace(/>/g, ">");
|
|
7
|
+
|
|
8
|
+
export async function createCaptionLayers({ events, durationSec, font, cjkFont, layout, assetsDir }) {
|
|
9
|
+
if (!font) return [];
|
|
10
|
+
const captions = events.map((event) => ({ text: String(event.caption || ""), start: (event.tEnd ?? event.t) / 1000 }))
|
|
11
|
+
.filter(({ text, start }) => text.trim() && Number.isFinite(start) && start >= 0 && start < durationSec)
|
|
12
|
+
.sort((a, b) => a.start - b.start);
|
|
13
|
+
const { y, size, box, width, height } = layout.caption;
|
|
14
|
+
const layers = [];
|
|
15
|
+
for (let i = 0; i < captions.length; i++) {
|
|
16
|
+
const { text, start } = captions[i];
|
|
17
|
+
const end = Math.min(captions[i + 1]?.start ?? durationSec, start + 2.8, durationSec);
|
|
18
|
+
if (end <= start) continue;
|
|
19
|
+
const options = {
|
|
20
|
+
text: `<span foreground="white">${escapeMarkup(text)}</span>`,
|
|
21
|
+
font: `sans-serif ${size}`, fontfile: hasCjk(text) ? cjkFont || font : font,
|
|
22
|
+
width: width - box * 2, align: "centre", rgba: true, wrap: "word-char",
|
|
23
|
+
};
|
|
24
|
+
let rendered = await sharp({ text: { ...options, dpi: 72 } }).png().toBuffer({ resolveWithObject: true });
|
|
25
|
+
if (rendered.info.height > height - box * 2) {
|
|
26
|
+
rendered = await sharp({ text: { ...options, height: height - box * 2 } }).png().toBuffer({ resolveWithObject: true });
|
|
27
|
+
if (rendered.info.textAutofitDpi < 48) {
|
|
28
|
+
throw new Error(`Caption at ${start.toFixed(2)}s is too long for ${layout.key}; shorten it or split it across steps`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const file = path.join(assetsDir, `caption-${layout.key}-${i}.png`);
|
|
32
|
+
const info = await sharp({ create: {
|
|
33
|
+
width: rendered.info.width + box * 2, height: rendered.info.height + box * 2,
|
|
34
|
+
channels: 4, background: { r: 11, g: 15, b: 25, alpha: 0.74 },
|
|
35
|
+
} }).composite([{ input: rendered.data, top: box, left: box }]).png().toFile(file);
|
|
36
|
+
layers.push({ file, start, end, x: Math.floor((layout.width - info.width) / 2), y });
|
|
37
|
+
}
|
|
38
|
+
return layers;
|
|
39
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import Ajv from "ajv";
|
|
4
|
+
|
|
5
|
+
export const configSchema = JSON.parse(readFileSync(new URL("../../schemas/story.schema.json", import.meta.url), "utf8"));
|
|
6
|
+
const ajv = new Ajv({ allErrors: true, strictRequired: false });
|
|
7
|
+
ajv.addFormat("uri", (value) => {
|
|
8
|
+
try { new URL(value); return true; } catch { return false; }
|
|
9
|
+
});
|
|
10
|
+
ajv.addSchema(configSchema);
|
|
11
|
+
const validators = Object.fromEntries(["story", "marks", "record"].map((kind) => [
|
|
12
|
+
kind, ajv.getSchema(`${configSchema.$id}#/definitions/${kind}`),
|
|
13
|
+
]));
|
|
14
|
+
const pointer = (part) => String(part).replace(/~/g, "~0").replace(/\//g, "~1");
|
|
15
|
+
|
|
16
|
+
export function inspectConfig(config, { kind } = {}) {
|
|
17
|
+
const detected = config && typeof config === "object" && !Array.isArray(config)
|
|
18
|
+
? ("marks" in config ? "marks" : "events" in config ? "record" : "story") : "story";
|
|
19
|
+
kind ||= detected;
|
|
20
|
+
const validate = validators[kind];
|
|
21
|
+
if (!validate) throw new Error(`Unknown config kind: ${kind}`);
|
|
22
|
+
const ok = validate(config);
|
|
23
|
+
const errors = ok ? [] : validate.errors.filter((error) => error.keyword !== "if").map((error) => {
|
|
24
|
+
const property = error.params.missingProperty ?? error.params.additionalProperty;
|
|
25
|
+
return {
|
|
26
|
+
code: "INVALID_CONFIG",
|
|
27
|
+
path: (error.instancePath + (property === undefined ? "" : `/${pointer(property)}`)) || "/",
|
|
28
|
+
message: error.message,
|
|
29
|
+
hint: "Correct this field using schemas/story.schema.json, then run validate again.",
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
if (ok) {
|
|
33
|
+
const duration = config.duration;
|
|
34
|
+
const settings = kind === "record" ? config.config : config;
|
|
35
|
+
const poster = settings?.posterAt;
|
|
36
|
+
if (duration !== undefined && poster !== undefined && poster >= duration) {
|
|
37
|
+
errors.push({ code: "INVALID_CONFIG", path: kind === "record" ? "/config/posterAt" : "/posterAt",
|
|
38
|
+
message: "must be less than duration", hint: "Choose a poster time inside the output timeline." });
|
|
39
|
+
}
|
|
40
|
+
if (kind === "record") {
|
|
41
|
+
config.events.forEach((event, index) => {
|
|
42
|
+
if (event.tEnd !== undefined && event.tEnd < event.t) errors.push({
|
|
43
|
+
code: "INVALID_CONFIG", path: `/events/${index}/tEnd`, message: "must be at least t",
|
|
44
|
+
hint: "Use milliseconds for both event timestamps.",
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { ok: errors.length === 0, kind, errors };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function assertConfig(config, options) {
|
|
53
|
+
const result = inspectConfig(config, options);
|
|
54
|
+
if (!result.ok) {
|
|
55
|
+
const error = new Error(`Invalid config: ${result.errors.map((e) => `${e.path} ${e.message}`).join("; ")}`);
|
|
56
|
+
error.code = "INVALID_CONFIG";
|
|
57
|
+
error.issues = result.errors;
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
return config;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function readConfig(file) {
|
|
64
|
+
let source;
|
|
65
|
+
try { source = await readFile(file, "utf8"); } catch {
|
|
66
|
+
const error = new Error("Could not read config file; check --config and file permissions.");
|
|
67
|
+
error.code = "CONFIG_READ_FAILED";
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
try { return JSON.parse(source); } catch {
|
|
71
|
+
// Do not echo source snippets, which may include typed values or credentials.
|
|
72
|
+
const error = new Error("Config is not valid JSON; check quotes, commas and brackets.");
|
|
73
|
+
error.code = "INVALID_JSON";
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|