@openvidstudio/capture 0.0.0-stage → 0.1.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 +81 -2
- package/dist/browser.d.ts +145 -0
- package/dist/browser.js +285 -0
- package/dist/cursor.d.ts +53 -0
- package/dist/cursor.js +54 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +33 -0
- package/dist/native.d.ts +126 -0
- package/dist/native.js +240 -0
- package/dist/process.d.ts +20 -0
- package/dist/process.js +43 -0
- package/dist/settle.d.ts +69 -0
- package/dist/settle.js +124 -0
- package/dist/terminal.d.ts +99 -0
- package/dist/terminal.js +190 -0
- package/package.json +41 -3
package/README.md
CHANGED
|
@@ -1,3 +1,82 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @openvidstudio/capture
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Getting a true frame out of a running product. No Remotion, no React, no video pipeline.
|
|
4
|
+
|
|
5
|
+
This is the half of [openvidstudio](https://github.com/AnayDhawan/openvidstudio) that is
|
|
6
|
+
hard, and it is useful on its own. If you want reliable captures of your app and you are
|
|
7
|
+
not making a video, take this package and ignore the rest.
|
|
8
|
+
|
|
9
|
+
## What is in it
|
|
10
|
+
|
|
11
|
+
**Browser capture that measures the zoom instead of assuming it.** Chrome keeps zoom
|
|
12
|
+
per origin, so `setViewportSize(1600x1000)` can leave you with an effective viewport that
|
|
13
|
+
is not 1600x1000, and every crop rect computed against the requested size is then wrong.
|
|
14
|
+
Nothing errors. `detectAndCompensateZoom` measures `window.innerWidth` live, re-requests a
|
|
15
|
+
compensated size, verifies convergence, and throws rather than proceed with a crop measured
|
|
16
|
+
against the wrong viewport. The ratio is never hardcoded: it differs per machine and per
|
|
17
|
+
profile.
|
|
18
|
+
|
|
19
|
+
**Interaction replay.** A small declarative `Interaction[]` (click, fill, select, hover,
|
|
20
|
+
scroll, wait) with a zod schema, replayed in array order, so two capture callers cannot
|
|
21
|
+
drift apart in how they drive the page.
|
|
22
|
+
|
|
23
|
+
**Backends for what a browser cannot reach.** Desktop windows and regions through ffmpeg
|
|
24
|
+
(`gdigrab`, `avfoundation`, `x11grab`, `pipewiregrab`), Android through `adb screenrecord`,
|
|
25
|
+
the iOS Simulator through `simctl io recordVideo`, and terminals through a recorded cast.
|
|
26
|
+
These are pure argv builders plus a thin spawn wrapper, so they are testable without a
|
|
27
|
+
screen attached and safe with argument values containing spaces.
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install @openvidstudio/capture
|
|
33
|
+
npx playwright install chromium # browser capture only
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`ffmpeg` on PATH is required for the native backends, `adb` for Android, Xcode command
|
|
37
|
+
line tools for the iOS Simulator.
|
|
38
|
+
|
|
39
|
+
Terminal recording works out of the box through pipes. For a faithful recording, with the
|
|
40
|
+
program's colour and cursor addressing intact, also install `node-pty`:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm install node-pty
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
It is deliberately not declared as a dependency of any kind here, not even an optional
|
|
47
|
+
peer: package managers install optional peers anyway, and `node-pty` compiles a native
|
|
48
|
+
addon. Requiring everyone to build one in order to record `npm test` is a bad trade. The
|
|
49
|
+
module is loaded at runtime if it is present, and `recordTerminal` reports which mode it
|
|
50
|
+
actually used.
|
|
51
|
+
|
|
52
|
+
## Use
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { launchChromium, detectAndCompensateZoom, replayInteractions } from "@openvidstudio/capture";
|
|
56
|
+
|
|
57
|
+
const browser = await launchChromium();
|
|
58
|
+
const page = await browser.newPage();
|
|
59
|
+
await page.setViewportSize({ width: 1600, height: 1000 });
|
|
60
|
+
await page.goto("http://localhost:3000");
|
|
61
|
+
|
|
62
|
+
const { zoom } = await detectAndCompensateZoom(page, { width: 1600, height: 1000 });
|
|
63
|
+
await replayInteractions(page, [{ type: "click", selector: "[data-tour=start]" }]);
|
|
64
|
+
await page.screenshot({ path: "shot.png" });
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { buildDesktopCaptureArgs } from "@openvidstudio/capture";
|
|
69
|
+
|
|
70
|
+
const args = buildDesktopCaptureArgs({
|
|
71
|
+
platform: "win32",
|
|
72
|
+
window: "My App",
|
|
73
|
+
framerate: 30,
|
|
74
|
+
durationSeconds: 8,
|
|
75
|
+
outPath: "out.mp4",
|
|
76
|
+
});
|
|
77
|
+
// spawn("ffmpeg", args)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
Apache-2.0
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { Browser, Page } from "playwright";
|
|
2
|
+
import type { CursorPoint } from "./cursor";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
/**
|
|
5
|
+
* Shared building blocks for capture_screenshot and capture_screen_recording:
|
|
6
|
+
* the Interaction type both replay identically, and CAPTURE.md's zoom-desync
|
|
7
|
+
* detection/compensation math, extracted once so the two tools' protocols
|
|
8
|
+
* cannot silently drift apart from each other.
|
|
9
|
+
*/
|
|
10
|
+
export interface Viewport {
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
}
|
|
14
|
+
/** CAPTURE.md's worked example. */
|
|
15
|
+
export declare const DEFAULT_VIEWPORT: Viewport;
|
|
16
|
+
/**
|
|
17
|
+
* Capture at twice the CSS resolution by default.
|
|
18
|
+
*
|
|
19
|
+
* This is the single biggest quality lever in the pipeline and it used to be missing.
|
|
20
|
+
* A capture taken at 1x is exactly as many pixels as the viewport, and the video then
|
|
21
|
+
* puts it on a 1920x1080 stage and pushes a camera into it. Every one of those steps
|
|
22
|
+
* is an upscale, so a 1440x900 capture shown at a 1.33 camera scale is being asked for
|
|
23
|
+
* roughly 1.8x the pixels it has, and the result is the soft, slightly smeared text
|
|
24
|
+
* that makes a demo look cheap.
|
|
25
|
+
*
|
|
26
|
+
* At 2x there are pixels to spare for both the stage and the push-in, so the frame stays
|
|
27
|
+
* sharp all the way through. The cost is memory and file size during capture, which is
|
|
28
|
+
* the right trade for an asset that gets rendered once and watched many times.
|
|
29
|
+
*/
|
|
30
|
+
export declare const DEFAULT_DEVICE_SCALE = 2;
|
|
31
|
+
export declare const viewportSchema: z.ZodObject<{
|
|
32
|
+
width: z.ZodNumber;
|
|
33
|
+
height: z.ZodNumber;
|
|
34
|
+
}, z.core.$strip>;
|
|
35
|
+
export type Interaction = {
|
|
36
|
+
type: "click";
|
|
37
|
+
selector: string;
|
|
38
|
+
} | {
|
|
39
|
+
type: "fill";
|
|
40
|
+
selector: string;
|
|
41
|
+
value: string;
|
|
42
|
+
} | {
|
|
43
|
+
type: "select";
|
|
44
|
+
selector: string;
|
|
45
|
+
value: string;
|
|
46
|
+
} | {
|
|
47
|
+
type: "hover";
|
|
48
|
+
selector: string;
|
|
49
|
+
} | {
|
|
50
|
+
type: "scroll";
|
|
51
|
+
selector?: string;
|
|
52
|
+
x?: number;
|
|
53
|
+
y?: number;
|
|
54
|
+
durationMs?: number;
|
|
55
|
+
} | {
|
|
56
|
+
type: "wait";
|
|
57
|
+
ms?: number;
|
|
58
|
+
selector?: string;
|
|
59
|
+
} | {
|
|
60
|
+
type: "settle";
|
|
61
|
+
ms?: number;
|
|
62
|
+
};
|
|
63
|
+
export declare const interactionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
64
|
+
type: z.ZodLiteral<"click">;
|
|
65
|
+
selector: z.ZodString;
|
|
66
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
67
|
+
type: z.ZodLiteral<"fill">;
|
|
68
|
+
selector: z.ZodString;
|
|
69
|
+
value: z.ZodString;
|
|
70
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
71
|
+
type: z.ZodLiteral<"select">;
|
|
72
|
+
selector: z.ZodString;
|
|
73
|
+
value: z.ZodString;
|
|
74
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
75
|
+
type: z.ZodLiteral<"hover">;
|
|
76
|
+
selector: z.ZodString;
|
|
77
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
78
|
+
type: z.ZodLiteral<"scroll">;
|
|
79
|
+
selector: z.ZodOptional<z.ZodString>;
|
|
80
|
+
x: z.ZodOptional<z.ZodNumber>;
|
|
81
|
+
y: z.ZodOptional<z.ZodNumber>;
|
|
82
|
+
durationMs: z.ZodOptional<z.ZodNumber>;
|
|
83
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
84
|
+
type: z.ZodLiteral<"wait">;
|
|
85
|
+
ms: z.ZodOptional<z.ZodNumber>;
|
|
86
|
+
selector: z.ZodOptional<z.ZodString>;
|
|
87
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
88
|
+
type: z.ZodLiteral<"settle">;
|
|
89
|
+
ms: z.ZodOptional<z.ZodNumber>;
|
|
90
|
+
}, z.core.$strip>], "type">;
|
|
91
|
+
/**
|
|
92
|
+
* Replays a beat's interactions in array order. Shared verbatim by both capture
|
|
93
|
+
* tools so their replay semantics never drift apart -- called after navigation
|
|
94
|
+
* and (for both tools) after the zoom-desync compensation below, in the same
|
|
95
|
+
* effective CSS-pixel viewport CAPTURE.md measured.
|
|
96
|
+
*
|
|
97
|
+
* Returns every click/hover/fill/select target's real measured center, in replay order,
|
|
98
|
+
* for capture_screenshot/capture_screen_recording to persist as a cursor sidecar (see
|
|
99
|
+
* cursor.ts) -- this is what lets scaffold_scene's cursor overlay land on real coordinates
|
|
100
|
+
* instead of a hand-authored guess.
|
|
101
|
+
*/
|
|
102
|
+
export declare function replayInteractions(page: Page, interactions: Interaction[] | undefined): Promise<CursorPoint[]>;
|
|
103
|
+
export declare const ZOOM_EPSILON = 0.01;
|
|
104
|
+
export interface ZoomCompensation {
|
|
105
|
+
/** The viewport actually in effect for capture: `target` unchanged, or the compensated size. */
|
|
106
|
+
viewport: Viewport;
|
|
107
|
+
/** Measured zoom = target.width / (effective width before compensation). 1 means no desync. */
|
|
108
|
+
zoom: number;
|
|
109
|
+
/** The last-measured `[window.innerWidth, window.innerHeight]`. */
|
|
110
|
+
effective: Viewport;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Thrown when a single compensation attempt doesn't converge -- CAPTURE.md
|
|
114
|
+
* doesn't explicitly cover this case (it assumes the desync ratio is
|
|
115
|
+
* consistent), so this is surfaced as a real error rather than silently
|
|
116
|
+
* proceeding with a crop rect measured against the wrong effective viewport.
|
|
117
|
+
*/
|
|
118
|
+
export declare class ZoomCompensationError extends Error {
|
|
119
|
+
readonly target: Viewport;
|
|
120
|
+
readonly zoom: number;
|
|
121
|
+
readonly effective: Viewport;
|
|
122
|
+
constructor(target: Viewport, zoom: number, effective: Viewport);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* CAPTURE.md's zoom-desync detection + compensation (protocol steps 1-2).
|
|
126
|
+
* Assumes the caller already called `page.setViewportSize(target)` and
|
|
127
|
+
* `page.goto(...)` -- this only measures and, if needed, re-requests a
|
|
128
|
+
* compensated viewport and re-verifies once.
|
|
129
|
+
*
|
|
130
|
+
* Never hardcodes a zoom constant (CAPTURE.md is explicit the ratio "can
|
|
131
|
+
* differ per machine/profile") -- it is measured live on every call. If the
|
|
132
|
+
* one compensation attempt doesn't bring the effective viewport back to
|
|
133
|
+
* `target` within `ZOOM_EPSILON`, throws `ZoomCompensationError` rather than
|
|
134
|
+
* silently proceeding.
|
|
135
|
+
*/
|
|
136
|
+
export declare function detectAndCompensateZoom(page: Page, target: Viewport): Promise<ZoomCompensation>;
|
|
137
|
+
/**
|
|
138
|
+
* Playwright's bundled browser binary is not installed by a plain `npm
|
|
139
|
+
* install`/`pnpm install` (it needs a separate `npx playwright install
|
|
140
|
+
* chromium`, deliberately not wired as a postinstall script -- see this
|
|
141
|
+
* package's README/tool descriptions). Rather than let that surface as a
|
|
142
|
+
* cryptic native-binary launch error, catch it here and return a clear,
|
|
143
|
+
* actionable message.
|
|
144
|
+
*/
|
|
145
|
+
export declare function launchChromium(): Promise<Browser>;
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ZoomCompensationError = exports.ZOOM_EPSILON = exports.interactionSchema = exports.viewportSchema = exports.DEFAULT_DEVICE_SCALE = exports.DEFAULT_VIEWPORT = void 0;
|
|
4
|
+
exports.replayInteractions = replayInteractions;
|
|
5
|
+
exports.detectAndCompensateZoom = detectAndCompensateZoom;
|
|
6
|
+
exports.launchChromium = launchChromium;
|
|
7
|
+
const playwright_1 = require("playwright");
|
|
8
|
+
const settle_1 = require("./settle");
|
|
9
|
+
const zod_1 = require("zod");
|
|
10
|
+
/** CAPTURE.md's worked example. */
|
|
11
|
+
exports.DEFAULT_VIEWPORT = { width: 1600, height: 1000 };
|
|
12
|
+
/**
|
|
13
|
+
* Capture at twice the CSS resolution by default.
|
|
14
|
+
*
|
|
15
|
+
* This is the single biggest quality lever in the pipeline and it used to be missing.
|
|
16
|
+
* A capture taken at 1x is exactly as many pixels as the viewport, and the video then
|
|
17
|
+
* puts it on a 1920x1080 stage and pushes a camera into it. Every one of those steps
|
|
18
|
+
* is an upscale, so a 1440x900 capture shown at a 1.33 camera scale is being asked for
|
|
19
|
+
* roughly 1.8x the pixels it has, and the result is the soft, slightly smeared text
|
|
20
|
+
* that makes a demo look cheap.
|
|
21
|
+
*
|
|
22
|
+
* At 2x there are pixels to spare for both the stage and the push-in, so the frame stays
|
|
23
|
+
* sharp all the way through. The cost is memory and file size during capture, which is
|
|
24
|
+
* the right trade for an asset that gets rendered once and watched many times.
|
|
25
|
+
*/
|
|
26
|
+
exports.DEFAULT_DEVICE_SCALE = 2;
|
|
27
|
+
exports.viewportSchema = zod_1.z.object({
|
|
28
|
+
width: zod_1.z.number().int().positive(),
|
|
29
|
+
height: zod_1.z.number().int().positive(),
|
|
30
|
+
});
|
|
31
|
+
exports.interactionSchema = zod_1.z.discriminatedUnion("type", [
|
|
32
|
+
zod_1.z.object({ type: zod_1.z.literal("click"), selector: zod_1.z.string().min(1) }),
|
|
33
|
+
zod_1.z.object({ type: zod_1.z.literal("fill"), selector: zod_1.z.string().min(1), value: zod_1.z.string() }),
|
|
34
|
+
zod_1.z.object({ type: zod_1.z.literal("select"), selector: zod_1.z.string().min(1), value: zod_1.z.string() }),
|
|
35
|
+
zod_1.z.object({ type: zod_1.z.literal("hover"), selector: zod_1.z.string().min(1) }),
|
|
36
|
+
zod_1.z.object({
|
|
37
|
+
type: zod_1.z.literal("scroll"),
|
|
38
|
+
selector: zod_1.z.string().optional(),
|
|
39
|
+
x: zod_1.z.number().optional(),
|
|
40
|
+
y: zod_1.z.number().optional(),
|
|
41
|
+
// Undefined/0 keeps the old instant scrollTo -- an unrequested behaviour change would
|
|
42
|
+
// silently retime every beat that already has a scroll interaction. Positive is an
|
|
43
|
+
// eased scroll (see easedScrollInPage below) driven by requestAnimationFrame, since
|
|
44
|
+
// CSS's `behavior: "smooth"` gives no control over duration or curve and browsers are
|
|
45
|
+
// free to shorten or skip it, neither of which is acceptable for video timing.
|
|
46
|
+
durationMs: zod_1.z.number().positive().optional(),
|
|
47
|
+
}),
|
|
48
|
+
zod_1.z.object({ type: zod_1.z.literal("wait"), ms: zod_1.z.number().optional(), selector: zod_1.z.string().optional() }),
|
|
49
|
+
// Not a timed guess. "settle" drives whatever the previous step started (a modal
|
|
50
|
+
// transition, a route change, a lazily loaded image) to completion before the next step
|
|
51
|
+
// runs, which is what stops a capture landing halfway through a fade.
|
|
52
|
+
zod_1.z.object({ type: zod_1.z.literal("settle"), ms: zod_1.z.number().optional() }),
|
|
53
|
+
]);
|
|
54
|
+
/**
|
|
55
|
+
* Runs in the page against a scrollable element. `behavior: "smooth"` was rejected on
|
|
56
|
+
* purpose: it has no duration/easing parameters, a page can override its own
|
|
57
|
+
* scroll-behavior CSS out from under it, and browsers are free to shorten or skip a smooth
|
|
58
|
+
* scroll under reduced-motion or heavy load -- none of which is acceptable when the
|
|
59
|
+
* duration has to match the video's timeline exactly. Driven by requestAnimationFrame
|
|
60
|
+
* instead, resolving its own promise so replayInteractions can await the real end of the
|
|
61
|
+
* animation rather than a fixed sleep.
|
|
62
|
+
*/
|
|
63
|
+
/* c8 ignore start -- executes in the browser, covered by the real-browser E2E */
|
|
64
|
+
function easedScrollElementInPage(el, args) {
|
|
65
|
+
const target = el;
|
|
66
|
+
const startX = target.scrollLeft;
|
|
67
|
+
const startY = target.scrollTop;
|
|
68
|
+
const endX = args.x ?? startX;
|
|
69
|
+
const endY = args.y ?? startY;
|
|
70
|
+
const duration = args.durationMs;
|
|
71
|
+
return new Promise((resolve) => {
|
|
72
|
+
const start = performance.now();
|
|
73
|
+
const step = (now) => {
|
|
74
|
+
const t = Math.min(1, (now - start) / duration);
|
|
75
|
+
// Cubic ease-in-out: slow away from the start, fast through the middle, slow into
|
|
76
|
+
// the landing point -- a linear scroll reads as mechanical on camera in a way an
|
|
77
|
+
// eased one does not.
|
|
78
|
+
const eased = t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
|
|
79
|
+
target.scrollTo(startX + (endX - startX) * eased, startY + (endY - startY) * eased);
|
|
80
|
+
if (t < 1)
|
|
81
|
+
requestAnimationFrame(step);
|
|
82
|
+
else
|
|
83
|
+
resolve();
|
|
84
|
+
};
|
|
85
|
+
requestAnimationFrame(step);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/* c8 ignore stop */
|
|
89
|
+
/** Same easing as easedScrollElementInPage, against the window instead of an element. */
|
|
90
|
+
/* c8 ignore start -- executes in the browser, covered by the real-browser E2E */
|
|
91
|
+
function easedScrollWindowInPage(args) {
|
|
92
|
+
const startX = window.scrollX;
|
|
93
|
+
const startY = window.scrollY;
|
|
94
|
+
const endX = args.x ?? startX;
|
|
95
|
+
const endY = args.y ?? startY;
|
|
96
|
+
const duration = args.durationMs;
|
|
97
|
+
return new Promise((resolve) => {
|
|
98
|
+
const start = performance.now();
|
|
99
|
+
const step = (now) => {
|
|
100
|
+
const t = Math.min(1, (now - start) / duration);
|
|
101
|
+
const eased = t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
|
|
102
|
+
window.scrollTo(startX + (endX - startX) * eased, startY + (endY - startY) * eased);
|
|
103
|
+
if (t < 1)
|
|
104
|
+
requestAnimationFrame(step);
|
|
105
|
+
else
|
|
106
|
+
resolve();
|
|
107
|
+
};
|
|
108
|
+
requestAnimationFrame(step);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/* c8 ignore stop */
|
|
112
|
+
/**
|
|
113
|
+
* Measures a click/hover/fill/select target's real on-screen center right before the
|
|
114
|
+
* action runs -- same rect-measurement style as captureScreenshot.ts's cropSelector code.
|
|
115
|
+
* Scrolls the target into view first, matching the auto-scroll every one of these
|
|
116
|
+
* Playwright actions already does internally, so the coordinate lands in the same
|
|
117
|
+
* effective viewport space the final screenshot/recording is captured in rather than
|
|
118
|
+
* wherever the element happened to sit before the action's own scroll would have moved it.
|
|
119
|
+
*
|
|
120
|
+
* Best-effort and silent on failure: a selector that doesn't resolve here is about to make
|
|
121
|
+
* the action itself throw a real, clearer error a moment later, and this is not the call
|
|
122
|
+
* that should report it first.
|
|
123
|
+
*/
|
|
124
|
+
/* c8 ignore start -- executes in the browser, covered by the real-browser E2E */
|
|
125
|
+
async function recordCursorPoint(page, points, index, type, selector, replayStartedAt) {
|
|
126
|
+
try {
|
|
127
|
+
const center = await page.$eval(selector, (el) => {
|
|
128
|
+
el.scrollIntoView({ block: "center", inline: "center" });
|
|
129
|
+
const r = el.getBoundingClientRect();
|
|
130
|
+
return { x: r.x + r.width / 2, y: r.y + r.height / 2 };
|
|
131
|
+
});
|
|
132
|
+
points.push({ index, type, selector, x: center.x, y: center.y, atMs: Date.now() - replayStartedAt });
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
// Selector didn't resolve or wasn't measurable -- not this function's job to report.
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/* c8 ignore stop */
|
|
139
|
+
/**
|
|
140
|
+
* Replays a beat's interactions in array order. Shared verbatim by both capture
|
|
141
|
+
* tools so their replay semantics never drift apart -- called after navigation
|
|
142
|
+
* and (for both tools) after the zoom-desync compensation below, in the same
|
|
143
|
+
* effective CSS-pixel viewport CAPTURE.md measured.
|
|
144
|
+
*
|
|
145
|
+
* Returns every click/hover/fill/select target's real measured center, in replay order,
|
|
146
|
+
* for capture_screenshot/capture_screen_recording to persist as a cursor sidecar (see
|
|
147
|
+
* cursor.ts) -- this is what lets scaffold_scene's cursor overlay land on real coordinates
|
|
148
|
+
* instead of a hand-authored guess.
|
|
149
|
+
*/
|
|
150
|
+
async function replayInteractions(page, interactions) {
|
|
151
|
+
const points = [];
|
|
152
|
+
const replayStartedAt = Date.now();
|
|
153
|
+
let index = 0;
|
|
154
|
+
for (const interaction of interactions ?? []) {
|
|
155
|
+
switch (interaction.type) {
|
|
156
|
+
case "click":
|
|
157
|
+
await recordCursorPoint(page, points, index, "click", interaction.selector, replayStartedAt);
|
|
158
|
+
await page.click(interaction.selector);
|
|
159
|
+
break;
|
|
160
|
+
case "fill":
|
|
161
|
+
await recordCursorPoint(page, points, index, "fill", interaction.selector, replayStartedAt);
|
|
162
|
+
await page.fill(interaction.selector, interaction.value);
|
|
163
|
+
break;
|
|
164
|
+
case "select":
|
|
165
|
+
await recordCursorPoint(page, points, index, "select", interaction.selector, replayStartedAt);
|
|
166
|
+
await page.selectOption(interaction.selector, interaction.value);
|
|
167
|
+
break;
|
|
168
|
+
case "hover":
|
|
169
|
+
await recordCursorPoint(page, points, index, "hover", interaction.selector, replayStartedAt);
|
|
170
|
+
await page.hover(interaction.selector);
|
|
171
|
+
break;
|
|
172
|
+
case "scroll":
|
|
173
|
+
if (interaction.durationMs && interaction.durationMs > 0) {
|
|
174
|
+
const args = { x: interaction.x, y: interaction.y, durationMs: interaction.durationMs };
|
|
175
|
+
if (interaction.selector) {
|
|
176
|
+
await page.$eval(interaction.selector, easedScrollElementInPage, args);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
await page.evaluate(easedScrollWindowInPage, args);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else if (interaction.selector) {
|
|
183
|
+
await page.$eval(interaction.selector, (el, coords) => el.scrollTo(coords.x ?? 0, coords.y ?? 0), { x: interaction.x, y: interaction.y });
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
await page.evaluate((coords) => window.scrollTo(coords.x ?? 0, coords.y ?? 0), { x: interaction.x, y: interaction.y });
|
|
187
|
+
}
|
|
188
|
+
break;
|
|
189
|
+
case "wait":
|
|
190
|
+
if (interaction.selector) {
|
|
191
|
+
await page.waitForSelector(interaction.selector, { timeout: interaction.ms });
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
await page.waitForTimeout(interaction.ms ?? 500);
|
|
195
|
+
}
|
|
196
|
+
break;
|
|
197
|
+
case "settle":
|
|
198
|
+
await (0, settle_1.settlePage)(page, { timeoutMs: interaction.ms });
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
index++;
|
|
202
|
+
}
|
|
203
|
+
return points;
|
|
204
|
+
}
|
|
205
|
+
exports.ZOOM_EPSILON = 0.01;
|
|
206
|
+
/**
|
|
207
|
+
* Thrown when a single compensation attempt doesn't converge -- CAPTURE.md
|
|
208
|
+
* doesn't explicitly cover this case (it assumes the desync ratio is
|
|
209
|
+
* consistent), so this is surfaced as a real error rather than silently
|
|
210
|
+
* proceeding with a crop rect measured against the wrong effective viewport.
|
|
211
|
+
*/
|
|
212
|
+
class ZoomCompensationError extends Error {
|
|
213
|
+
constructor(target, zoom, effective) {
|
|
214
|
+
super(`Zoom-desync compensation did not converge: requested ${target.width}x${target.height}, measured zoom ` +
|
|
215
|
+
`${zoom.toFixed(4)} (per CAPTURE.md, this ratio is not hardcoded, it can differ per machine/profile), ` +
|
|
216
|
+
`but after one compensation attempt the effective viewport was ${effective.width}x${effective.height}, ` +
|
|
217
|
+
`not ${target.width}x${target.height} within ${(exports.ZOOM_EPSILON * 100).toFixed(0)}%. Refusing to proceed ` +
|
|
218
|
+
`with a screenshot/crop measured against the wrong effective viewport -- see packages/docs/CAPTURE.md.`);
|
|
219
|
+
this.target = target;
|
|
220
|
+
this.zoom = zoom;
|
|
221
|
+
this.effective = effective;
|
|
222
|
+
this.name = "ZoomCompensationError";
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
exports.ZoomCompensationError = ZoomCompensationError;
|
|
226
|
+
async function measureEffectiveViewport(page) {
|
|
227
|
+
const [width, height] = await page.evaluate(() => [window.innerWidth, window.innerHeight]);
|
|
228
|
+
return { width, height };
|
|
229
|
+
}
|
|
230
|
+
function relativeDiff(actual, target) {
|
|
231
|
+
return Math.abs(actual - target) / target;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* CAPTURE.md's zoom-desync detection + compensation (protocol steps 1-2).
|
|
235
|
+
* Assumes the caller already called `page.setViewportSize(target)` and
|
|
236
|
+
* `page.goto(...)` -- this only measures and, if needed, re-requests a
|
|
237
|
+
* compensated viewport and re-verifies once.
|
|
238
|
+
*
|
|
239
|
+
* Never hardcodes a zoom constant (CAPTURE.md is explicit the ratio "can
|
|
240
|
+
* differ per machine/profile") -- it is measured live on every call. If the
|
|
241
|
+
* one compensation attempt doesn't bring the effective viewport back to
|
|
242
|
+
* `target` within `ZOOM_EPSILON`, throws `ZoomCompensationError` rather than
|
|
243
|
+
* silently proceeding.
|
|
244
|
+
*/
|
|
245
|
+
async function detectAndCompensateZoom(page, target) {
|
|
246
|
+
const firstMeasurement = await measureEffectiveViewport(page);
|
|
247
|
+
const zoom = target.width / firstMeasurement.width;
|
|
248
|
+
if (Math.abs(zoom - 1) <= exports.ZOOM_EPSILON) {
|
|
249
|
+
return { viewport: target, zoom, effective: firstMeasurement };
|
|
250
|
+
}
|
|
251
|
+
const compensated = {
|
|
252
|
+
width: Math.round(target.width * zoom),
|
|
253
|
+
height: Math.round(target.height * zoom),
|
|
254
|
+
};
|
|
255
|
+
await page.setViewportSize(compensated);
|
|
256
|
+
const secondMeasurement = await measureEffectiveViewport(page);
|
|
257
|
+
const converged = relativeDiff(secondMeasurement.width, target.width) <= exports.ZOOM_EPSILON &&
|
|
258
|
+
relativeDiff(secondMeasurement.height, target.height) <= exports.ZOOM_EPSILON;
|
|
259
|
+
if (!converged) {
|
|
260
|
+
throw new ZoomCompensationError(target, zoom, secondMeasurement);
|
|
261
|
+
}
|
|
262
|
+
return { viewport: compensated, zoom, effective: secondMeasurement };
|
|
263
|
+
}
|
|
264
|
+
const BROWSER_NOT_INSTALLED_RE = /Executable doesn't exist at/;
|
|
265
|
+
/**
|
|
266
|
+
* Playwright's bundled browser binary is not installed by a plain `npm
|
|
267
|
+
* install`/`pnpm install` (it needs a separate `npx playwright install
|
|
268
|
+
* chromium`, deliberately not wired as a postinstall script -- see this
|
|
269
|
+
* package's README/tool descriptions). Rather than let that surface as a
|
|
270
|
+
* cryptic native-binary launch error, catch it here and return a clear,
|
|
271
|
+
* actionable message.
|
|
272
|
+
*/
|
|
273
|
+
async function launchChromium() {
|
|
274
|
+
try {
|
|
275
|
+
return await playwright_1.chromium.launch({ headless: true });
|
|
276
|
+
}
|
|
277
|
+
catch (err) {
|
|
278
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
279
|
+
if (BROWSER_NOT_INSTALLED_RE.test(message)) {
|
|
280
|
+
throw new Error(`Playwright's Chromium browser is not installed. Run "npx playwright install chromium" (in the ` +
|
|
281
|
+
`project, or wherever @openvidstudio/mcp-server is installed) and retry. Original error: ${message}`);
|
|
282
|
+
}
|
|
283
|
+
throw err;
|
|
284
|
+
}
|
|
285
|
+
}
|
package/dist/cursor.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Viewport } from "./browser";
|
|
2
|
+
/**
|
|
3
|
+
* Real cursor coordinates recorded during replayInteractions, so scaffold_scene's cursor
|
|
4
|
+
* overlay can show the actor landing where the product was actually clicked instead of a
|
|
5
|
+
* hand-drawn guess.
|
|
6
|
+
*
|
|
7
|
+
* The old way: a scene author who wanted a cursor built one from primitives, picking x/y
|
|
8
|
+
* keyframes by eye against a static screenshot. Nothing tied those numbers to where the
|
|
9
|
+
* real click during capture happened, so a redesign that moved a button left the cursor
|
|
10
|
+
* pointing at empty space with no error anywhere -- the same silent-wrongness failure mode
|
|
11
|
+
* validate_scenes exists to catch for framing, just uncaught for this.
|
|
12
|
+
*/
|
|
13
|
+
export type CursorPointType = "click" | "hover" | "fill" | "select";
|
|
14
|
+
export interface CursorPoint {
|
|
15
|
+
/** Index into the interactions array this point came from, in replay order. */
|
|
16
|
+
index: number;
|
|
17
|
+
type: CursorPointType;
|
|
18
|
+
selector: string;
|
|
19
|
+
/**
|
|
20
|
+
* The target's bounding-rect center, in the CSS-pixel viewport space the capture ran in
|
|
21
|
+
* (the same effective, zoom-compensated space captureWidth/captureHeight describe) --
|
|
22
|
+
* NOT multiplied by deviceScaleFactor. Measured after scrolling the target into view,
|
|
23
|
+
* matching what a real click/hover action does before it acts.
|
|
24
|
+
*/
|
|
25
|
+
x: number;
|
|
26
|
+
y: number;
|
|
27
|
+
/**
|
|
28
|
+
* Milliseconds since replayInteractions started measuring this beat's interactions.
|
|
29
|
+
* capture_screen_recording rewrites this to be relative to the RECORDING's own start
|
|
30
|
+
* (adding back the navigation+settle time that ran before replay began) before writing
|
|
31
|
+
* the sidecar, so a consumer can turn it into a frame via fps directly. capture_screenshot
|
|
32
|
+
* leaves it replay-relative: a still has no video timeline for it to be relative to.
|
|
33
|
+
*/
|
|
34
|
+
atMs: number;
|
|
35
|
+
}
|
|
36
|
+
export interface CursorSidecar {
|
|
37
|
+
/**
|
|
38
|
+
* The CSS-pixel viewport the points were measured in. Carried here explicitly rather
|
|
39
|
+
* than assumed, so a consumer can convert a point into its own stage/frame geometry
|
|
40
|
+
* without having to reprobe or guess the capture's own viewport.
|
|
41
|
+
*/
|
|
42
|
+
viewport: Viewport;
|
|
43
|
+
points: CursorPoint[];
|
|
44
|
+
}
|
|
45
|
+
export declare function cursorSidecarPath(outPathAbs: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Persists recorded cursor points next to the artifact they describe, mirroring settle's
|
|
48
|
+
* `<outPath>.settle.json` sidecar convention. Writes nothing when there are no points, so a
|
|
49
|
+
* beat with no click/hover/fill/select interactions (most `dom-demo`-adjacent capture
|
|
50
|
+
* beats, or one that only scrolls/waits) produces no sidecar rather than an empty,
|
|
51
|
+
* meaningless file that scaffold_scene would otherwise have to special-case around.
|
|
52
|
+
*/
|
|
53
|
+
export declare function writeCursorSidecar(outPathAbs: string, viewport: Viewport, points: CursorPoint[]): void;
|
package/dist/cursor.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.cursorSidecarPath = cursorSidecarPath;
|
|
37
|
+
exports.writeCursorSidecar = writeCursorSidecar;
|
|
38
|
+
const fs = __importStar(require("node:fs"));
|
|
39
|
+
function cursorSidecarPath(outPathAbs) {
|
|
40
|
+
return `${outPathAbs}.cursor.json`;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Persists recorded cursor points next to the artifact they describe, mirroring settle's
|
|
44
|
+
* `<outPath>.settle.json` sidecar convention. Writes nothing when there are no points, so a
|
|
45
|
+
* beat with no click/hover/fill/select interactions (most `dom-demo`-adjacent capture
|
|
46
|
+
* beats, or one that only scrolls/waits) produces no sidecar rather than an empty,
|
|
47
|
+
* meaningless file that scaffold_scene would otherwise have to special-case around.
|
|
48
|
+
*/
|
|
49
|
+
function writeCursorSidecar(outPathAbs, viewport, points) {
|
|
50
|
+
if (points.length === 0)
|
|
51
|
+
return;
|
|
52
|
+
const sidecar = { viewport, points };
|
|
53
|
+
fs.writeFileSync(cursorSidecarPath(outPathAbs), JSON.stringify(sidecar, null, 2) + "\n", "utf8");
|
|
54
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @openvidstudio/capture
|
|
3
|
+
*
|
|
4
|
+
* The half of openvidstudio that has nothing to do with video: getting a true frame out
|
|
5
|
+
* of a running product. A real browser with the per-origin zoom desync measured and
|
|
6
|
+
* compensated rather than assumed, interaction replay, and ffmpeg/adb/simctl backends for
|
|
7
|
+
* the desktop, mobile, and terminal surfaces a browser cannot reach.
|
|
8
|
+
*
|
|
9
|
+
* Deliberately free of Remotion, React, and @openvidstudio/core: a team that wants
|
|
10
|
+
* trustworthy capture and does not want to make videos should be able to take this alone.
|
|
11
|
+
*/
|
|
12
|
+
export * from "./process";
|
|
13
|
+
export * from "./browser";
|
|
14
|
+
export * from "./settle";
|
|
15
|
+
export * from "./cursor";
|
|
16
|
+
export * from "./native";
|
|
17
|
+
export * from "./terminal";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
/**
|
|
18
|
+
* @openvidstudio/capture
|
|
19
|
+
*
|
|
20
|
+
* The half of openvidstudio that has nothing to do with video: getting a true frame out
|
|
21
|
+
* of a running product. A real browser with the per-origin zoom desync measured and
|
|
22
|
+
* compensated rather than assumed, interaction replay, and ffmpeg/adb/simctl backends for
|
|
23
|
+
* the desktop, mobile, and terminal surfaces a browser cannot reach.
|
|
24
|
+
*
|
|
25
|
+
* Deliberately free of Remotion, React, and @openvidstudio/core: a team that wants
|
|
26
|
+
* trustworthy capture and does not want to make videos should be able to take this alone.
|
|
27
|
+
*/
|
|
28
|
+
__exportStar(require("./process"), exports);
|
|
29
|
+
__exportStar(require("./browser"), exports);
|
|
30
|
+
__exportStar(require("./settle"), exports);
|
|
31
|
+
__exportStar(require("./cursor"), exports);
|
|
32
|
+
__exportStar(require("./native"), exports);
|
|
33
|
+
__exportStar(require("./terminal"), exports);
|