trace-avatar-render 0.1.7 → 0.2.1
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 +34 -1
- package/README.md +55 -0
- package/dist/{chunk-PPVNEPXF.js → chunk-MC2ZHX5Z.js} +1997 -599
- package/dist/chunk-MC2ZHX5Z.js.map +1 -0
- package/dist/cli.js +6 -448
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +437 -11
- package/dist/index.js +55 -1
- package/package.json +13 -1
- package/dist/chunk-PPVNEPXF.js.map +0 -1
|
@@ -1,86 +1,389 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
// src/cli.ts
|
|
2
|
+
import { existsSync as existsSync6, readFileSync as readFileSync9, writeFileSync as writeFileSync5 } from "fs";
|
|
3
|
+
import { apply as applySuggestions, suggest } from "trace-avatar/assist";
|
|
4
|
+
|
|
5
|
+
// src/ai.ts
|
|
6
|
+
import { resolve } from "path";
|
|
7
|
+
import { pathToFileURL } from "url";
|
|
8
|
+
import { gateway, openAiCompatible } from "trace-avatar/assist";
|
|
9
|
+
async function providerOf(spec, env = process.env, o = {}) {
|
|
10
|
+
const vision = o.vision ? { visionModel: o.vision } : {};
|
|
11
|
+
const g = /^gateway(?::([\w.-]+)(?:\/([^,]+))?((?:,[\w.-]+)*))?$/.exec(spec);
|
|
12
|
+
if (g) {
|
|
13
|
+
return gateway({
|
|
14
|
+
...g[1] ? { provider: [g[1], ...(g[3] ?? "").split(",").filter(Boolean)] } : {},
|
|
15
|
+
...g[2] ? { model: g[2] } : {},
|
|
16
|
+
...vision,
|
|
17
|
+
url: env.AI_GATEWAY_URL,
|
|
18
|
+
key: env.AI_GATEWAY_KEY
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
const m = /^openai:(.+)$/.exec(spec);
|
|
22
|
+
if (m) {
|
|
23
|
+
if (!env.OPENAI_API_KEY && !env.OPENAI_BASE_URL) throw new Error("--ai openai:\u2026 needs OPENAI_API_KEY (and OPENAI_BASE_URL for another OpenAI-compatible server)");
|
|
24
|
+
return openAiCompatible({ baseUrl: env.OPENAI_BASE_URL || "https://api.openai.com/v1", ...env.OPENAI_API_KEY ? { key: env.OPENAI_API_KEY } : {}, model: m[1], ...vision });
|
|
25
|
+
}
|
|
26
|
+
if (/\.(m?js|cjs)$/.test(spec)) {
|
|
27
|
+
const mod = await import(pathToFileURL(resolve(spec)).href);
|
|
28
|
+
const fn = mod.default ?? mod.ai;
|
|
29
|
+
if (typeof fn !== "function") throw new Error(`${spec} must export a default function (task, input, schema, { instructions, images }) \u2192 JSON`);
|
|
30
|
+
return fn;
|
|
31
|
+
}
|
|
32
|
+
throw new Error(`--ai is gateway, gateway:<provider>[/<model>][,<provider>\u2026], openai:<model>, or a .js file (got "${spec}")`);
|
|
33
|
+
}
|
|
34
|
+
var QA_KINDS = ["error", "empty", "stalled", "personal-data", "wrong-page", "other"];
|
|
35
|
+
var QA_SCHEMA = {
|
|
36
|
+
type: "object",
|
|
37
|
+
required: ["flags"],
|
|
38
|
+
properties: { flags: { type: "array", items: { type: "object", required: ["frame", "kind", "note"], properties: {
|
|
39
|
+
frame: { type: "integer" },
|
|
40
|
+
kind: { enum: [...QA_KINDS] },
|
|
41
|
+
note: { type: "string", maxLength: 200 }
|
|
42
|
+
} } } }
|
|
43
|
+
};
|
|
44
|
+
var QA_INSTRUCTIONS = `You check frames of a GIF/video of an on-screen guide before it is published: a cursor with a name badge walks through a web app. For each numbered frame, flag only real problems a viewer would notice: an error message or toast ("error"), an empty state or blank page where content is expected ("empty"), a "Looking for \u2026" caption meaning the guide got stuck ("stalled"), personal data such as real email addresses, phone numbers, card or account numbers ("personal-data"), or a page that does not fit the step ("wrong-page"). The cursor, the name badge, highlights and captions are part of the guide, not problems. Reply with JSON only: {"flags":[{"frame":1,"kind":"error","note":"\u2026"}]}, an empty list when all is well.`;
|
|
45
|
+
async function runQa(ai, frames, context) {
|
|
46
|
+
if (!frames.length) return [];
|
|
47
|
+
const input = { title: context.title, steps: context.steps, frames: frames.map((f, k) => ({ frame: k + 1, at: Math.round(f.at / 100) / 10, shows: f.label })) };
|
|
48
|
+
const answer = await ai("qa", input, QA_SCHEMA, { instructions: QA_INSTRUCTIONS, images: frames.map((f, k) => ({ png: f.png, label: `Frame ${k + 1}: ${f.label}` })) });
|
|
49
|
+
const list = answer && typeof answer === "object" && Array.isArray(answer.flags) ? answer.flags : null;
|
|
50
|
+
if (!list) throw new Error('the QA answer has no "flags" list');
|
|
51
|
+
const out = [];
|
|
52
|
+
for (const item of list) {
|
|
53
|
+
const r = item;
|
|
54
|
+
if (!Number.isInteger(r?.frame) || r.frame < 1 || r.frame > frames.length) continue;
|
|
55
|
+
const kind = QA_KINDS.includes(r.kind) ? r.kind : "other";
|
|
56
|
+
const note = typeof r.note === "string" ? r.note.replace(/\s+/g, " ").trim().slice(0, 200) : "";
|
|
57
|
+
out.push({ at: frames[r.frame - 1].at, kind, note });
|
|
58
|
+
}
|
|
59
|
+
return out.sort((a, b) => a.at - b.at);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/narration.ts
|
|
63
|
+
import { preview } from "trace-avatar/assist";
|
|
64
|
+
function linesOf(steps, script) {
|
|
65
|
+
const out = /* @__PURE__ */ new Map();
|
|
66
|
+
steps.forEach((s, i) => {
|
|
67
|
+
if (s.say?.trim()) out.set(i, s.say.trim());
|
|
68
|
+
});
|
|
69
|
+
for (const l of script?.lines ?? []) {
|
|
70
|
+
const i = l.n - 1;
|
|
71
|
+
if (!Number.isInteger(i) || i < 0 || i >= steps.length) throw new Error(`the script has a line for step ${l.n}, but the trace has ${steps.length} steps`);
|
|
72
|
+
if (l.text.trim()) out.set(i, l.text.trim());
|
|
73
|
+
else out.delete(i);
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
function parseScript(json) {
|
|
78
|
+
const lines = json?.lines;
|
|
79
|
+
if (!Array.isArray(lines) || lines.some((l) => !l || typeof l.n !== "number" || typeof l.text !== "string")) {
|
|
80
|
+
throw new Error('a narration script is { "lines": [{ "n": 1, "text": "\u2026" }] }');
|
|
81
|
+
}
|
|
82
|
+
return { lines: lines.map((l) => ({ n: l.n, text: l.text })) };
|
|
83
|
+
}
|
|
84
|
+
function planNarration(acts, lines, endMs, o = {}) {
|
|
85
|
+
const first = o.firstMs ?? 300;
|
|
86
|
+
const after = o.afterActMs ?? 200;
|
|
87
|
+
const before = o.beforeActMs ?? 250;
|
|
88
|
+
const gap = o.gapMs ?? 150;
|
|
89
|
+
const maxHold = o.maxHoldMs ?? 8e3;
|
|
90
|
+
const plan = { lines: [], holds: [], long: [] };
|
|
91
|
+
let extra = 0;
|
|
92
|
+
let prevCut = null;
|
|
93
|
+
let prevEnd = -Infinity;
|
|
94
|
+
for (const a of acts.slice().sort((x, y) => x.t - y.t)) {
|
|
95
|
+
const line = lines.get(a.index);
|
|
96
|
+
const prevHeld = prevCut === null ? null : prevCut + extra;
|
|
97
|
+
if (line && line.ms > 0) {
|
|
98
|
+
const start = Math.max(prevHeld === null ? first : prevHeld + after, prevEnd + gap);
|
|
99
|
+
const end = start + line.ms;
|
|
100
|
+
const need = Math.ceil(end - (a.t + extra - before));
|
|
101
|
+
if (need > 0) {
|
|
102
|
+
plan.holds.push({ at: Math.max(0, a.t - before, prevCut ?? 0), ms: need });
|
|
103
|
+
extra += need;
|
|
104
|
+
if (need > maxHold) plan.long.push({ index: a.index, ms: need });
|
|
105
|
+
}
|
|
106
|
+
plan.lines.push({ index: a.index, text: line.text, start: Math.round(start), ms: Math.round(line.ms) });
|
|
107
|
+
prevEnd = end;
|
|
19
108
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
109
|
+
prevCut = a.t;
|
|
110
|
+
}
|
|
111
|
+
const endHoldMs = Math.max(0, Math.ceil(prevEnd + (o.tailMs ?? 400) - (endMs + extra)));
|
|
112
|
+
if (endHoldMs > 0) plan.holds.push({ at: endMs, ms: endHoldMs });
|
|
113
|
+
return { ...plan, endHoldMs };
|
|
114
|
+
}
|
|
115
|
+
function narrationText(lines, offsetMs = 0) {
|
|
116
|
+
const at = (ms) => {
|
|
117
|
+
const s = Math.round((ms + offsetMs) / 1e3);
|
|
118
|
+
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
|
|
119
|
+
};
|
|
120
|
+
return lines.map((l) => `[${at(l.start)}] ${l.text}`).join("\n") + (lines.length ? "\n" : "");
|
|
121
|
+
}
|
|
122
|
+
var SCRIPT_SCHEMA = { type: "object", properties: { lines: { type: "array", items: { type: "object", properties: { n: { type: "integer" }, text: { type: "string" } }, required: ["n", "text"] } } }, required: ["lines"] };
|
|
123
|
+
var SCRIPT_INSTRUCTIONS = `You write the voice-over for a short screen guide. For each step, write one line to be spoken while the cursor moves to it: a full, natural sentence in plain English, second person ("Now click Save"), at most 25 words. Spell out symbols and numbers as spoken ("twenty percent", not "20%"). Keep the meaning of the step's caption when it has one. Skip steps that need no words (return no line for them). Never invent data values. Answer with JSON: { "lines": [{ "n": <step number>, "text": "\u2026" }] }.`;
|
|
124
|
+
async function suggestScript(ai, trace) {
|
|
125
|
+
const p = preview("captions", trace);
|
|
126
|
+
const answer = await ai("narration", p.input, SCRIPT_SCHEMA, { instructions: SCRIPT_INSTRUCTIONS });
|
|
127
|
+
const steps = trace.steps?.length ?? 0;
|
|
128
|
+
const lines = [];
|
|
129
|
+
const dropped = [];
|
|
130
|
+
const seen = /* @__PURE__ */ new Set();
|
|
131
|
+
for (const l of answer?.lines ?? []) {
|
|
132
|
+
const n = l?.n;
|
|
133
|
+
const text = typeof l?.text === "string" ? l.text.trim() : "";
|
|
134
|
+
if (typeof n !== "number" || !Number.isInteger(n) || n < 1 || n > steps) dropped.push({ n, why: "no such step" });
|
|
135
|
+
else if (seen.has(n)) dropped.push({ n, why: "a second line for the step" });
|
|
136
|
+
else if (!text) dropped.push({ n, why: "empty" });
|
|
137
|
+
else if (text.length > 300) dropped.push({ n, why: "longer than 300 characters" });
|
|
138
|
+
else {
|
|
139
|
+
seen.add(n);
|
|
140
|
+
lines.push({ n, text });
|
|
24
141
|
}
|
|
25
|
-
cur.push(a);
|
|
26
142
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
143
|
+
return { script: { lines: lines.sort((a, b) => a.n - b.n) }, dropped };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// src/voice.ts
|
|
147
|
+
import { spawnSync } from "child_process";
|
|
148
|
+
import { createHash } from "crypto";
|
|
149
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs";
|
|
150
|
+
import { tmpdir } from "os";
|
|
151
|
+
import { join, resolve as resolve2 } from "path";
|
|
152
|
+
import { pathToFileURL as pathToFileURL2 } from "url";
|
|
153
|
+
|
|
154
|
+
// src/audio.ts
|
|
155
|
+
var TRACK_RATE = 48e3;
|
|
156
|
+
function toWav(pcm, sampleRate) {
|
|
157
|
+
const data = pcm.length * 2;
|
|
158
|
+
const buf = new ArrayBuffer(44 + data);
|
|
159
|
+
const v = new DataView(buf);
|
|
160
|
+
const str = (at, s) => {
|
|
161
|
+
for (let i = 0; i < s.length; i += 1) v.setUint8(at + i, s.charCodeAt(i));
|
|
162
|
+
};
|
|
163
|
+
str(0, "RIFF");
|
|
164
|
+
v.setUint32(4, 36 + data, true);
|
|
165
|
+
str(8, "WAVE");
|
|
166
|
+
str(12, "fmt ");
|
|
167
|
+
v.setUint32(16, 16, true);
|
|
168
|
+
v.setUint16(20, 1, true);
|
|
169
|
+
v.setUint16(22, 1, true);
|
|
170
|
+
v.setUint32(24, sampleRate, true);
|
|
171
|
+
v.setUint32(28, sampleRate * 2, true);
|
|
172
|
+
v.setUint16(32, 2, true);
|
|
173
|
+
v.setUint16(34, 16, true);
|
|
174
|
+
str(36, "data");
|
|
175
|
+
v.setUint32(40, data, true);
|
|
176
|
+
for (let i = 0; i < pcm.length; i += 1) v.setInt16(44 + i * 2, pcm[i], true);
|
|
177
|
+
return new Uint8Array(buf);
|
|
178
|
+
}
|
|
179
|
+
function fromWav(bytes) {
|
|
180
|
+
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
181
|
+
const tag = (at2) => String.fromCharCode(...bytes.subarray(at2, at2 + 4));
|
|
182
|
+
if (bytes.length < 12 || tag(0) !== "RIFF" || tag(8) !== "WAVE") throw new Error("not a WAV file");
|
|
183
|
+
let at = 12;
|
|
184
|
+
let rate = 0;
|
|
185
|
+
let channels = 1;
|
|
186
|
+
while (at + 8 <= bytes.length) {
|
|
187
|
+
const id = tag(at);
|
|
188
|
+
const size = v.getUint32(at + 4, true);
|
|
189
|
+
if (id === "fmt ") {
|
|
190
|
+
const format = v.getUint16(at + 8, true);
|
|
191
|
+
channels = v.getUint16(at + 10, true);
|
|
192
|
+
rate = v.getUint32(at + 12, true);
|
|
193
|
+
const bits = v.getUint16(at + 22, true);
|
|
194
|
+
if (format !== 1 && format !== 65534 || bits !== 16) throw new Error(`only 16-bit PCM WAV is read (format ${format}, ${bits} bits)`);
|
|
195
|
+
} else if (id === "data") {
|
|
196
|
+
if (!rate) throw new Error("a WAV data chunk before its format");
|
|
197
|
+
const len = size === 0 || size === 4294967295 || at + 8 + size > bytes.length ? bytes.length - at - 8 : size;
|
|
198
|
+
const n = Math.floor(len / 2 / channels);
|
|
199
|
+
const pcm = new Int16Array(n);
|
|
200
|
+
for (let i = 0; i < n; i += 1) {
|
|
201
|
+
let sum = 0;
|
|
202
|
+
for (let c = 0; c < channels; c += 1) sum += v.getInt16(at + 8 + (i * channels + c) * 2, true);
|
|
203
|
+
pcm[i] = Math.round(sum / channels);
|
|
38
204
|
}
|
|
205
|
+
return { pcm, sampleRate: rate };
|
|
39
206
|
}
|
|
40
|
-
|
|
207
|
+
at += 8 + size + size % 2;
|
|
41
208
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
209
|
+
throw new Error("a WAV file without data");
|
|
210
|
+
}
|
|
211
|
+
function fromS16le(bytes) {
|
|
212
|
+
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
213
|
+
const out = new Int16Array(Math.floor(bytes.length / 2));
|
|
214
|
+
for (let i = 0; i < out.length; i += 1) out[i] = v.getInt16(i * 2, true);
|
|
215
|
+
return out;
|
|
216
|
+
}
|
|
217
|
+
function resample(pcm, from, to) {
|
|
218
|
+
if (from === to || !pcm.length) return pcm.slice();
|
|
219
|
+
const n = Math.max(1, Math.round(pcm.length * to / from));
|
|
220
|
+
const out = new Int16Array(n);
|
|
221
|
+
const step = from / to;
|
|
222
|
+
for (let i = 0; i < n; i += 1) {
|
|
223
|
+
const x = i * step;
|
|
224
|
+
const a = Math.min(pcm.length - 1, Math.floor(x));
|
|
225
|
+
const b = Math.min(pcm.length - 1, a + 1);
|
|
226
|
+
out[i] = Math.round(pcm[a] + (pcm[b] - pcm[a]) * (x - a));
|
|
227
|
+
}
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
function trimSilence(p, o = {}) {
|
|
231
|
+
const level = o.level ?? 400;
|
|
232
|
+
const pad = Math.round((o.padMs ?? 40) * p.sampleRate / 1e3);
|
|
233
|
+
let a = 0;
|
|
234
|
+
while (a < p.pcm.length && Math.abs(p.pcm[a]) < level) a += 1;
|
|
235
|
+
if (a === p.pcm.length) return { pcm: new Int16Array(0), sampleRate: p.sampleRate };
|
|
236
|
+
let b = p.pcm.length - 1;
|
|
237
|
+
while (b > a && Math.abs(p.pcm[b]) < level) b -= 1;
|
|
238
|
+
return { pcm: p.pcm.slice(Math.max(0, a - pad), Math.min(p.pcm.length, b + 1 + pad)), sampleRate: p.sampleRate };
|
|
239
|
+
}
|
|
240
|
+
var msOf = (p) => p.pcm.length * 1e3 / p.sampleRate;
|
|
241
|
+
function mix(lines, totalMs, o = {}) {
|
|
242
|
+
const out = new Float64Array(Math.max(0, Math.round(totalMs * TRACK_RATE / 1e3)));
|
|
243
|
+
const fade = Math.round((o.fadeMs ?? 15) * TRACK_RATE / 1e3);
|
|
244
|
+
for (const l of lines) {
|
|
245
|
+
const s = resample(l.audio.pcm, l.audio.sampleRate, TRACK_RATE);
|
|
246
|
+
const at = Math.round(l.start * TRACK_RATE / 1e3);
|
|
247
|
+
const f = Math.min(fade, Math.floor(s.length / 2));
|
|
248
|
+
for (let i = 0; i < s.length; i += 1) {
|
|
249
|
+
const k = at + i;
|
|
250
|
+
if (k < 0) continue;
|
|
251
|
+
if (k >= out.length) break;
|
|
252
|
+
const g = f ? Math.min(1, i / f, (s.length - 1 - i) / f) : 1;
|
|
253
|
+
out[k] += s[i] * g;
|
|
52
254
|
}
|
|
53
|
-
const last = keys[keys.length - 1];
|
|
54
|
-
const out = Math.max(s[s.length - 1].t + hold, last.t);
|
|
55
|
-
keys.push({ t: out, z: Z, x: last.x, y: last.y }, { t: out + ease, z: 1, x: last.x, y: last.y });
|
|
56
255
|
}
|
|
57
|
-
|
|
256
|
+
const pcm = new Int16Array(out.length);
|
|
257
|
+
for (let i = 0; i < out.length; i += 1) pcm[i] = Math.max(-32768, Math.min(32767, Math.round(out[i])));
|
|
258
|
+
return pcm;
|
|
58
259
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
260
|
+
|
|
261
|
+
// src/voice.ts
|
|
262
|
+
var GEMINI_TTS = { model: "gemini-2.5-flash-preview-tts", voice: "Kore" };
|
|
263
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
264
|
+
async function post(f, url, headers, body) {
|
|
265
|
+
for (let n = 1; ; n += 1) {
|
|
266
|
+
let res = null;
|
|
267
|
+
try {
|
|
268
|
+
res = await f(url, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body) });
|
|
269
|
+
} catch (e) {
|
|
270
|
+
if (n >= 3) throw new Error(`could not reach the voice (${e.message})`);
|
|
271
|
+
}
|
|
272
|
+
if (res?.ok) return res;
|
|
273
|
+
if (res) {
|
|
274
|
+
const text = await res.text();
|
|
275
|
+
let gw = null;
|
|
276
|
+
try {
|
|
277
|
+
const j = JSON.parse(text);
|
|
278
|
+
if (j?.error?.source === "ai-interface") gw = j.error;
|
|
279
|
+
} catch {
|
|
280
|
+
}
|
|
281
|
+
if (gw) throw new Error(`the AI gateway refused the voice call: ${gw.message}`);
|
|
282
|
+
if (!(res.status === 429 || res.status >= 500) || n >= 3) throw new Error(`the voice call failed (${res.status}): ${text.slice(0, 300)}`);
|
|
283
|
+
}
|
|
284
|
+
await sleep(Math.min(400 * 2 ** (n - 1) * (0.5 + Math.random() / 2), 4e3));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function geminiVoice(o) {
|
|
288
|
+
const model = o.model ?? GEMINI_TTS.model;
|
|
289
|
+
return async (text) => {
|
|
290
|
+
const res = await post(
|
|
291
|
+
o.fetch ?? fetch,
|
|
292
|
+
`${o.url.replace(/\/+$/, "")}/models/${model}:generateContent`,
|
|
293
|
+
{ ...o.bearer ? { authorization: `Bearer ${o.key}` } : { "x-goog-api-key": o.key }, ...o.headers },
|
|
294
|
+
{
|
|
295
|
+
contents: [{ role: "user", parts: [{ text }] }],
|
|
296
|
+
generationConfig: { responseModalities: ["AUDIO"], speechConfig: { voiceConfig: { prebuiltVoiceConfig: { voiceName: o.voice ?? GEMINI_TTS.voice } } } }
|
|
297
|
+
}
|
|
298
|
+
);
|
|
299
|
+
const json = await res.json();
|
|
300
|
+
const part = json.candidates?.[0]?.content?.parts?.find((p) => p.inlineData?.data || p.inline_data?.data);
|
|
301
|
+
const data = part?.inlineData?.data ?? part?.inline_data?.data;
|
|
302
|
+
if (!data) throw new Error(`${model} answered without audio`);
|
|
303
|
+
const mime = part?.inlineData?.mimeType ?? part?.inline_data?.mime_type ?? "";
|
|
304
|
+
const rate = Number(/rate=(\d+)/.exec(mime)?.[1] ?? 24e3);
|
|
305
|
+
return { pcm: fromS16le(new Uint8Array(Buffer.from(data, "base64"))), sampleRate: rate };
|
|
67
306
|
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
307
|
+
}
|
|
308
|
+
function openAiVoice(o) {
|
|
309
|
+
return async (text) => {
|
|
310
|
+
const res = await post(
|
|
311
|
+
o.fetch ?? fetch,
|
|
312
|
+
`${o.baseUrl.replace(/\/+$/, "")}/audio/speech`,
|
|
313
|
+
o.key ? { authorization: `Bearer ${o.key}` } : {},
|
|
314
|
+
{ model: o.model, voice: o.voice ?? "alloy", input: text, response_format: "pcm" }
|
|
315
|
+
);
|
|
316
|
+
return { pcm: fromS16le(new Uint8Array(await res.arrayBuffer())), sampleRate: 24e3 };
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
function sayVoice(o = {}) {
|
|
320
|
+
return async (text) => {
|
|
321
|
+
const dir = mkdtempSync(join(tmpdir(), "tar-say-"));
|
|
322
|
+
try {
|
|
323
|
+
const file = join(dir, "line.wav");
|
|
324
|
+
const r = spawnSync("say", [...o.voice ? ["-v", o.voice] : [], "-o", file, "--file-format=WAVE", "--data-format=LEI16@24000", text], { encoding: "utf8" });
|
|
325
|
+
if (r.error || r.status !== 0) throw new Error(`say failed${r.error ? ` (${r.error.message})` : `: ${r.stderr}`}; --voice say needs macOS`);
|
|
326
|
+
return fromWav(new Uint8Array(readFileSync(file)));
|
|
327
|
+
} finally {
|
|
328
|
+
rmSync(dir, { recursive: true, force: true });
|
|
329
|
+
}
|
|
79
330
|
};
|
|
80
331
|
}
|
|
332
|
+
function cached(voice, id, dir) {
|
|
333
|
+
const v = (async (text) => {
|
|
334
|
+
const file = join(dir, `${createHash("sha256").update(`${id}
|
|
335
|
+
${text}`).digest("hex").slice(0, 32)}.wav`);
|
|
336
|
+
if (existsSync(file)) {
|
|
337
|
+
v.hits += 1;
|
|
338
|
+
return fromWav(new Uint8Array(readFileSync(file)));
|
|
339
|
+
}
|
|
340
|
+
const p = await voice(text);
|
|
341
|
+
v.misses += 1;
|
|
342
|
+
mkdirSync(dir, { recursive: true });
|
|
343
|
+
writeFileSync(file, toWav(p.pcm, p.sampleRate));
|
|
344
|
+
return p;
|
|
345
|
+
});
|
|
346
|
+
v.hits = 0;
|
|
347
|
+
v.misses = 0;
|
|
348
|
+
return v;
|
|
349
|
+
}
|
|
350
|
+
async function voiceOf(spec, env = process.env, o = {}) {
|
|
351
|
+
const cacheDir = o.cacheDir === void 0 ? resolve2(".trace-avatar-cache", "voice") : o.cacheDir;
|
|
352
|
+
const wrap = (v) => cacheDir ? cached(v, spec, cacheDir) : v;
|
|
353
|
+
const g = /^gateway(?::gemini(?:\/([^:]+))?(?::(.+))?)?$/.exec(spec);
|
|
354
|
+
if (g) {
|
|
355
|
+
if (!env.AI_GATEWAY_URL || !env.AI_GATEWAY_KEY) throw new Error("--voice gateway needs AI_GATEWAY_URL and AI_GATEWAY_KEY");
|
|
356
|
+
return wrap(geminiVoice({
|
|
357
|
+
url: `${env.AI_GATEWAY_URL.replace(/\/+$/, "")}/v1/gemini`,
|
|
358
|
+
key: env.AI_GATEWAY_KEY,
|
|
359
|
+
bearer: true,
|
|
360
|
+
...g[1] ? { model: g[1] } : {},
|
|
361
|
+
...g[2] ? { voice: g[2] } : {},
|
|
362
|
+
headers: { "x-ai-feature": "trace-avatar-voice" },
|
|
363
|
+
...o.fetch ? { fetch: o.fetch } : {}
|
|
364
|
+
}));
|
|
365
|
+
}
|
|
366
|
+
const m = /^openai:([^:]+)(?::(.+))?$/.exec(spec);
|
|
367
|
+
if (m) {
|
|
368
|
+
if (!env.OPENAI_API_KEY && !env.OPENAI_BASE_URL) throw new Error("--voice openai:\u2026 needs OPENAI_API_KEY (and OPENAI_BASE_URL for another OpenAI-compatible server)");
|
|
369
|
+
return wrap(openAiVoice({ baseUrl: env.OPENAI_BASE_URL || "https://api.openai.com/v1", ...env.OPENAI_API_KEY ? { key: env.OPENAI_API_KEY } : {}, model: m[1], ...m[2] ? { voice: m[2] } : {}, ...o.fetch ? { fetch: o.fetch } : {} }));
|
|
370
|
+
}
|
|
371
|
+
const s = /^say(?::(.+))?$/.exec(spec);
|
|
372
|
+
if (s) return wrap(sayVoice(s[1] ? { voice: s[1] } : {}));
|
|
373
|
+
if (/\.(m?js|cjs)$/.test(spec)) {
|
|
374
|
+
const mod = await import(pathToFileURL2(resolve2(spec)).href);
|
|
375
|
+
const fn = mod.default ?? mod.voice;
|
|
376
|
+
if (typeof fn !== "function") throw new Error(`${spec} must export a default function (text) \u2192 { pcm: Int16Array, sampleRate }`);
|
|
377
|
+
return wrap(fn);
|
|
378
|
+
}
|
|
379
|
+
throw new Error(`--voice is gateway[:gemini[/<model>][:<voice>]], openai:<model>[:<voice>], say[:<voice>], or a .js file (got "${spec}")`);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// src/cli.ts
|
|
383
|
+
import { parseArgs } from "util";
|
|
81
384
|
|
|
82
385
|
// src/capture.ts
|
|
83
|
-
import { readFileSync } from "fs";
|
|
386
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
84
387
|
import { chromium } from "playwright";
|
|
85
388
|
var RenderError = class extends Error {
|
|
86
389
|
constructor(message, screenshot) {
|
|
@@ -176,7 +479,7 @@ function bootstrap(cfg) {
|
|
|
176
479
|
if (document.readyState === "loading") addEventListener("DOMContentLoaded", go, { once: true });
|
|
177
480
|
else go();
|
|
178
481
|
}
|
|
179
|
-
var
|
|
482
|
+
var sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
180
483
|
async function capture(o) {
|
|
181
484
|
const timeout = o.timeoutMs ?? 12e4;
|
|
182
485
|
const cfg = {
|
|
@@ -192,7 +495,7 @@ async function capture(o) {
|
|
|
192
495
|
...o.security !== void 0 ? { security: o.security } : {},
|
|
193
496
|
...o.values ? { values: o.values } : {}
|
|
194
497
|
};
|
|
195
|
-
const content = `${
|
|
498
|
+
const content = `${readFileSync2(o.playerFile, "utf8")}
|
|
196
499
|
;window.__taPlayer = TraceAvatar;
|
|
197
500
|
(${bootstrap.toString()})(${JSON.stringify(cfg)});`;
|
|
198
501
|
const browser = await chromium.launch({ headless: o.headless !== false });
|
|
@@ -250,7 +553,7 @@ async function capture(o) {
|
|
|
250
553
|
if (first.type !== "ready") throw new RenderError(`the guide could not start: ${first.message ?? first.type}`);
|
|
251
554
|
capturing = true;
|
|
252
555
|
await startCast();
|
|
253
|
-
await
|
|
556
|
+
await sleep2(o.leadMs ?? 500);
|
|
254
557
|
const playAt = Date.now();
|
|
255
558
|
await page.evaluate(() => window.__taPlay());
|
|
256
559
|
const done = await until(() => events.find((e) => e.type === "end" || e.type === "stalled") ?? failure(), "the guide to end");
|
|
@@ -264,8 +567,8 @@ async function capture(o) {
|
|
|
264
567
|
err.stall = { ...stall, ...data !== void 0 ? { data } : {} };
|
|
265
568
|
throw err;
|
|
266
569
|
}
|
|
267
|
-
await
|
|
268
|
-
} else await
|
|
570
|
+
await sleep2(3e3);
|
|
571
|
+
} else await sleep2(o.holdEndMs ?? 1500);
|
|
269
572
|
const stopAt = Date.now();
|
|
270
573
|
capturing = false;
|
|
271
574
|
await cdp.send("Page.stopScreencast").catch(() => {
|
|
@@ -279,13 +582,101 @@ async function capture(o) {
|
|
|
279
582
|
await browser.close();
|
|
280
583
|
}
|
|
281
584
|
}
|
|
282
|
-
async function findPlayer(explicit, env = process.env) {
|
|
283
|
-
if (explicit) return explicit;
|
|
284
|
-
if (env.TRACE_AVATAR_PLAYER) return env.TRACE_AVATAR_PLAYER;
|
|
285
|
-
const { createRequire } = await import("module");
|
|
286
|
-
const { dirname:
|
|
287
|
-
const req =
|
|
288
|
-
return
|
|
585
|
+
async function findPlayer(explicit, env = process.env) {
|
|
586
|
+
if (explicit) return explicit;
|
|
587
|
+
if (env.TRACE_AVATAR_PLAYER) return env.TRACE_AVATAR_PLAYER;
|
|
588
|
+
const { createRequire: createRequire2 } = await import("module");
|
|
589
|
+
const { dirname: dirname6, join: join5 } = await import("path");
|
|
590
|
+
const req = createRequire2(import.meta.url);
|
|
591
|
+
return join5(dirname6(req.resolve("trace-avatar/package.json")), "dist", "trace-perform.min.js");
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// src/render.ts
|
|
595
|
+
import { readFileSync as readFileSync6, writeFileSync as writeFileSync2, mkdirSync as mkdirSync2, mkdtempSync as mkdtempSync2, rmSync as rmSync2 } from "fs";
|
|
596
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
597
|
+
import { dirname as dirname3, extname as extname2, join as join3 } from "path";
|
|
598
|
+
import { chromium as chromium2 } from "playwright";
|
|
599
|
+
import { PNG as PNG2 } from "pngjs";
|
|
600
|
+
|
|
601
|
+
// src/camera.ts
|
|
602
|
+
var ZOOM_KINDS = /* @__PURE__ */ new Set(["click", "dblclick", "type", "key", "hover"]);
|
|
603
|
+
var smooth = (u) => u * u * (3 - 2 * u);
|
|
604
|
+
function cameraKeys(actions, o) {
|
|
605
|
+
const Z = o.zoom ?? 1.6;
|
|
606
|
+
const lead = o.leadMs ?? 400;
|
|
607
|
+
const ease = o.easeMs ?? 400;
|
|
608
|
+
const join5 = o.joinMs ?? 2500;
|
|
609
|
+
const hold = o.holdMs ?? 900;
|
|
610
|
+
const minChange = o.minChangeMs ?? 1500;
|
|
611
|
+
const sorted = actions.slice().sort((a, b) => a.t - b.t);
|
|
612
|
+
const shots = [];
|
|
613
|
+
let cur = [];
|
|
614
|
+
for (const a of sorted) {
|
|
615
|
+
if (!ZOOM_KINDS.has(a.kind)) {
|
|
616
|
+
if (cur.length) shots.push(cur);
|
|
617
|
+
cur = [];
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
const last = cur[cur.length - 1];
|
|
621
|
+
if (last && a.t - last.t > join5) {
|
|
622
|
+
shots.push(cur);
|
|
623
|
+
cur = [];
|
|
624
|
+
}
|
|
625
|
+
cur.push(a);
|
|
626
|
+
}
|
|
627
|
+
if (cur.length) shots.push(cur);
|
|
628
|
+
const merged = [];
|
|
629
|
+
for (const s of shots) {
|
|
630
|
+
const prev = merged[merged.length - 1];
|
|
631
|
+
if (prev) {
|
|
632
|
+
const outAt = prev[prev.length - 1].t + hold;
|
|
633
|
+
const inAt = s[0].t - lead - ease;
|
|
634
|
+
const scrollBetween = sorted.some((a) => !ZOOM_KINDS.has(a.kind) && a.t > prev[prev.length - 1].t && a.t < s[0].t);
|
|
635
|
+
if (!scrollBetween && inAt - (outAt + ease) < minChange) {
|
|
636
|
+
prev.push(...s);
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
merged.push(s.slice());
|
|
641
|
+
}
|
|
642
|
+
const keys = [];
|
|
643
|
+
for (const s of merged) {
|
|
644
|
+
const first = s[0];
|
|
645
|
+
const inEnd = Math.max(first.t - lead, (keys[keys.length - 1]?.t ?? -Infinity) + ease);
|
|
646
|
+
keys.push({ t: inEnd - ease, z: 1, x: first.x, y: first.y }, { t: inEnd, z: Z, x: first.x, y: first.y });
|
|
647
|
+
for (let k = 1; k < s.length; k += 1) {
|
|
648
|
+
const a = s[k];
|
|
649
|
+
const prev = keys[keys.length - 1];
|
|
650
|
+
const arrive = Math.max(a.t - lead, prev.t + ease);
|
|
651
|
+
keys.push({ t: arrive - ease, z: Z, x: prev.x, y: prev.y }, { t: arrive, z: Z, x: a.x, y: a.y });
|
|
652
|
+
}
|
|
653
|
+
const last = keys[keys.length - 1];
|
|
654
|
+
const out = Math.max(s[s.length - 1].t + hold, last.t);
|
|
655
|
+
keys.push({ t: out, z: Z, x: last.x, y: last.y }, { t: out + ease, z: 1, x: last.x, y: last.y });
|
|
656
|
+
}
|
|
657
|
+
return keys;
|
|
658
|
+
}
|
|
659
|
+
function cameraPath(actions, o) {
|
|
660
|
+
const keys = cameraKeys(actions, o);
|
|
661
|
+
const full = { x: 0, y: 0, w: o.width, h: o.height };
|
|
662
|
+
const rectOf = (z, x, y) => {
|
|
663
|
+
if (z <= 1.0001) return full;
|
|
664
|
+
const w = o.width / z;
|
|
665
|
+
const h = o.height / z;
|
|
666
|
+
return { x: Math.min(Math.max(0, x - w / 2), o.width - w), y: Math.min(Math.max(0, y - h / 2), o.height - h), w, h };
|
|
667
|
+
};
|
|
668
|
+
return (t) => {
|
|
669
|
+
if (!keys.length || t <= keys[0].t || t >= keys[keys.length - 1].t) return full;
|
|
670
|
+
let k = 0;
|
|
671
|
+
while (k < keys.length - 2 && keys[k + 1].t <= t) k += 1;
|
|
672
|
+
const a = keys[k];
|
|
673
|
+
const b = keys[k + 1];
|
|
674
|
+
const u = b.t > a.t ? smooth(Math.min(1, Math.max(0, (t - a.t) / (b.t - a.t)))) : 1;
|
|
675
|
+
const za = 1 / a.z;
|
|
676
|
+
const zb = 1 / b.z;
|
|
677
|
+
const z = 1 / (za + (zb - za) * u);
|
|
678
|
+
return rectOf(z, a.x + (b.x - a.x) * u, a.y + (b.y - a.y) * u);
|
|
679
|
+
};
|
|
289
680
|
}
|
|
290
681
|
|
|
291
682
|
// src/compose.ts
|
|
@@ -467,7 +858,7 @@ function same(a, b) {
|
|
|
467
858
|
}
|
|
468
859
|
|
|
469
860
|
// src/layers.ts
|
|
470
|
-
import { readFileSync as
|
|
861
|
+
import { readFileSync as readFileSync3 } from "fs";
|
|
471
862
|
import { extname } from "path";
|
|
472
863
|
import { PNG } from "pngjs";
|
|
473
864
|
var MIME = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".svg": "image/svg+xml", ".webp": "image/webp", ".gif": "image/gif" };
|
|
@@ -475,7 +866,7 @@ function dataUri(file) {
|
|
|
475
866
|
if (/^data:/.test(file)) return file;
|
|
476
867
|
const type = MIME[extname(file).toLowerCase()];
|
|
477
868
|
if (!type) throw new Error(`a logo is a PNG, JPEG, SVG, WebP or GIF file (got ${file})`);
|
|
478
|
-
return `data:${type};base64,${
|
|
869
|
+
return `data:${type};base64,${readFileSync3(file).toString("base64")}`;
|
|
479
870
|
}
|
|
480
871
|
var DEFAULT_BACKGROUND = "linear-gradient(135deg, #dfe7fb 0%, #eef0f6 50%, #f6e9f1 100%)";
|
|
481
872
|
var esc = (s) => s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
|
@@ -595,8 +986,8 @@ function pixel(img, x, y) {
|
|
|
595
986
|
|
|
596
987
|
// src/layout.ts
|
|
597
988
|
var even = (n) => Math.max(2, Math.round(n / 2) * 2);
|
|
598
|
-
function layoutOf(srcW, srcH, width, frame = "none", canvas) {
|
|
599
|
-
if (canvas) return onCanvas(srcW, srcH, width, frame, canvas);
|
|
989
|
+
function layoutOf(srcW, srcH, width, frame = "none", canvas, fill = false) {
|
|
990
|
+
if (canvas) return onCanvas(srcW, srcH, width, fill ? "none" : frame, canvas, fill);
|
|
600
991
|
if (frame === "none") {
|
|
601
992
|
const { width: W2, height: H2 } = fitWidth(srcW, srcH, width);
|
|
602
993
|
const r = { x: 0, y: 0, w: W2, h: H2 };
|
|
@@ -617,13 +1008,13 @@ function layoutOf(srcW, srcH, width, frame = "none", canvas) {
|
|
|
617
1008
|
window: { x: pad, y: top, w: cw, h: barH + ch }
|
|
618
1009
|
};
|
|
619
1010
|
}
|
|
620
|
-
function onCanvas(srcW, srcH, width, frame, canvas) {
|
|
1011
|
+
function onCanvas(srcW, srcH, width, frame, canvas, fill = false) {
|
|
621
1012
|
const W = even(width ? Math.min(width, canvas.width) : canvas.width);
|
|
622
1013
|
const H = even(canvas.height * W / canvas.width);
|
|
623
1014
|
const tall = H / W > 1.2;
|
|
624
|
-
const padX = Math.round(W * 0.05);
|
|
625
|
-
const top = Math.round(H * (tall ? 0.1 : 0.07));
|
|
626
|
-
const bottom = Math.round(H * (tall ? 0.16 : 0.09));
|
|
1015
|
+
const padX = fill ? 0 : Math.round(W * 0.05);
|
|
1016
|
+
const top = fill ? 0 : Math.round(H * (tall ? 0.1 : 0.07));
|
|
1017
|
+
const bottom = fill ? 0 : Math.round(H * (tall ? 0.16 : 0.09));
|
|
627
1018
|
const boxW = W - 2 * padX;
|
|
628
1019
|
const boxH = H - top - bottom;
|
|
629
1020
|
const barK = frame === "browser" ? 0.039 : 0;
|
|
@@ -643,8 +1034,8 @@ function onCanvas(srcW, srcH, width, frame, canvas) {
|
|
|
643
1034
|
}
|
|
644
1035
|
|
|
645
1036
|
// src/brand.ts
|
|
646
|
-
import { readFileSync as
|
|
647
|
-
import { dirname, isAbsolute, resolve } from "path";
|
|
1037
|
+
import { readFileSync as readFileSync4 } from "fs";
|
|
1038
|
+
import { dirname, isAbsolute, resolve as resolve3 } from "path";
|
|
648
1039
|
var CANVASES = {
|
|
649
1040
|
"9:16": { width: 1080, height: 1920 },
|
|
650
1041
|
"4:5": { width: 1080, height: 1350 },
|
|
@@ -669,14 +1060,14 @@ function watermarkOf(v) {
|
|
|
669
1060
|
if (w.opacity !== void 0 && !(w.opacity > 0 && w.opacity <= 1)) throw new Error("a watermark's opacity is above 0, up to 1");
|
|
670
1061
|
return { ...w, text: w.text.trim() };
|
|
671
1062
|
}
|
|
672
|
-
var KEYS = ["$comment", "name", "canvas", "watermark", "frame", "background", "camera", "captions", "captionsAt", "titleCard", "endCard"];
|
|
1063
|
+
var KEYS = ["$comment", "name", "canvas", "fill", "watermark", "frame", "background", "camera", "captions", "captionsAt", "titleCard", "endCard"];
|
|
673
1064
|
function brandOf(json, dir = ".") {
|
|
674
1065
|
if (!json || typeof json !== "object" || Array.isArray(json)) throw new Error("a brand is a JSON object");
|
|
675
1066
|
const b = json;
|
|
676
1067
|
const problems = [];
|
|
677
1068
|
for (const k of Object.keys(b)) if (!KEYS.includes(k)) problems.push(`unknown key ${k}`);
|
|
678
1069
|
const out = {};
|
|
679
|
-
const at = (p) => p && !isAbsolute(p) && !/^data:/.test(p) ?
|
|
1070
|
+
const at = (p) => p && !isAbsolute(p) && !/^data:/.test(p) ? resolve3(dir, p) : p;
|
|
680
1071
|
const card = (v, name) => {
|
|
681
1072
|
if (typeof v === "boolean" || typeof v === "string") return v;
|
|
682
1073
|
if (v && typeof v === "object" && typeof v.title === "string") {
|
|
@@ -697,6 +1088,13 @@ function brandOf(json, dir = ".") {
|
|
|
697
1088
|
canvasOf(b.canvas);
|
|
698
1089
|
out.canvas = b.canvas;
|
|
699
1090
|
});
|
|
1091
|
+
if (b.fill !== void 0) {
|
|
1092
|
+
if (Array.isArray(b.fill)) tryIt(() => {
|
|
1093
|
+
for (const f of b.fill) canvasOf(f);
|
|
1094
|
+
out.fill = b.fill;
|
|
1095
|
+
});
|
|
1096
|
+
else problems.push('fill is a list of shapes, e.g. ["16:9"]');
|
|
1097
|
+
}
|
|
700
1098
|
if (b.watermark !== void 0) tryIt(() => {
|
|
701
1099
|
const w = watermarkOf(b.watermark);
|
|
702
1100
|
out.watermark = { ...w, ...w.logo ? { logo: at(w.logo) } : {} };
|
|
@@ -736,24 +1134,39 @@ function brandOf(json, dir = ".") {
|
|
|
736
1134
|
function brandFile(file) {
|
|
737
1135
|
let json;
|
|
738
1136
|
try {
|
|
739
|
-
json = JSON.parse(
|
|
1137
|
+
json = JSON.parse(readFileSync4(file, "utf8"));
|
|
740
1138
|
} catch (e) {
|
|
741
1139
|
throw new Error(`brand ${file}: ${e.message}`);
|
|
742
1140
|
}
|
|
743
|
-
return brandOf(json, dirname(
|
|
1141
|
+
return brandOf(json, dirname(resolve3(file)));
|
|
744
1142
|
}
|
|
745
1143
|
|
|
746
1144
|
// src/mp4.ts
|
|
747
|
-
import { spawn, spawnSync } from "child_process";
|
|
748
|
-
|
|
749
|
-
|
|
1145
|
+
import { spawn, spawnSync as spawnSync2 } from "child_process";
|
|
1146
|
+
import { existsSync as existsSync2 } from "fs";
|
|
1147
|
+
import { createRequire } from "module";
|
|
1148
|
+
var works = (bin) => {
|
|
1149
|
+
try {
|
|
1150
|
+
return spawnSync2(bin, ["-version"], { stdio: "ignore" }).status === 0;
|
|
1151
|
+
} catch {
|
|
1152
|
+
return false;
|
|
1153
|
+
}
|
|
1154
|
+
};
|
|
1155
|
+
function bundled() {
|
|
750
1156
|
try {
|
|
751
|
-
const
|
|
752
|
-
return
|
|
1157
|
+
const p = createRequire(import.meta.url)("ffmpeg-static");
|
|
1158
|
+
return p && existsSync2(p) ? p : null;
|
|
753
1159
|
} catch {
|
|
754
1160
|
return null;
|
|
755
1161
|
}
|
|
756
1162
|
}
|
|
1163
|
+
function findFfmpeg(env = process.env, o = {}) {
|
|
1164
|
+
if (env.FFMPEG) return works(env.FFMPEG) ? env.FFMPEG : null;
|
|
1165
|
+
if (works("ffmpeg")) return "ffmpeg";
|
|
1166
|
+
const b = (o.bundled ?? bundled)();
|
|
1167
|
+
return b && works(b) ? b : null;
|
|
1168
|
+
}
|
|
1169
|
+
var FFMPEG_MISSING = "MP4 needs ffmpeg: install ffmpeg-static (npm install ffmpeg-static), install ffmpeg (brew install ffmpeg, apt install ffmpeg), or set FFMPEG to its path. GIF works without it.";
|
|
757
1170
|
function mp4Args(file, fps, size, audio) {
|
|
758
1171
|
return [
|
|
759
1172
|
"-y",
|
|
@@ -788,16 +1201,16 @@ function mp4Args(file, fps, size, audio) {
|
|
|
788
1201
|
}
|
|
789
1202
|
async function encodeMp4(frames, o) {
|
|
790
1203
|
const bin = o.ffmpeg ?? findFfmpeg();
|
|
791
|
-
if (!bin) throw new Error(
|
|
1204
|
+
if (!bin) throw new Error(FFMPEG_MISSING);
|
|
792
1205
|
if (o.width % 2 || o.height % 2) throw new Error(`MP4 needs an even size (got ${o.width}\xD7${o.height})`);
|
|
793
1206
|
const p = spawn(bin, mp4Args(o.file, o.fps, o, o.audio), { stdio: ["pipe", "ignore", "pipe"] });
|
|
794
1207
|
let err = "";
|
|
795
1208
|
p.stderr.on("data", (d) => {
|
|
796
1209
|
err += String(d);
|
|
797
1210
|
});
|
|
798
|
-
const done = new Promise((
|
|
1211
|
+
const done = new Promise((resolve6, reject) => {
|
|
799
1212
|
p.on("error", reject);
|
|
800
|
-
p.on("close", (code) => code === 0 ?
|
|
1213
|
+
p.on("close", (code) => code === 0 ? resolve6() : reject(new Error(`ffmpeg exited with ${code}: ${err.trim()}`)));
|
|
801
1214
|
});
|
|
802
1215
|
p.stdin.on("error", () => {
|
|
803
1216
|
});
|
|
@@ -963,263 +1376,72 @@ function pickFrames(captured, want) {
|
|
|
963
1376
|
return best;
|
|
964
1377
|
});
|
|
965
1378
|
}
|
|
966
|
-
|
|
967
|
-
// src/plan.ts
|
|
968
|
-
var stepsOf = (events) => events.filter((e) => e.type === "step").slice().sort((a, b) => a.t - b.t);
|
|
969
|
-
function actionsOf(events, segs, scale = 1) {
|
|
970
|
-
const out = [];
|
|
971
|
-
for (const e of events) {
|
|
972
|
-
const t = outputAt(segs, e.t);
|
|
973
|
-
if (t === null) continue;
|
|
974
|
-
if (e.type === "unload") out.push({ t, x: 0, y: 0, kind: "page" });
|
|
975
|
-
else if (e.type === "step" && e.rect && e.stepType) {
|
|
976
|
-
out.push({ t, x: (e.rect.x + e.rect.w / 2) * scale, y: (e.rect.y + e.rect.h / 2) * scale, kind: e.stepType });
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
return out.sort((a, b) => a.t - b.t);
|
|
980
|
-
}
|
|
981
|
-
function stepCues(events, segs, endMs, o = {}) {
|
|
982
|
-
const after = o.afterMs ?? 600;
|
|
983
|
-
const min = o.minMs ?? 1200;
|
|
984
|
-
const cues = [];
|
|
985
|
-
let prev = 0;
|
|
986
|
-
for (const e of stepsOf(events)) {
|
|
987
|
-
const t = outputAt(segs, e.t);
|
|
988
|
-
if (t === null) continue;
|
|
989
|
-
if (e.say) {
|
|
990
|
-
const end = Math.min(t + after, endMs);
|
|
991
|
-
cues.push({ start: Math.max(0, Math.min(prev, end - min)), end, text: e.say });
|
|
992
|
-
}
|
|
993
|
-
prev = t;
|
|
994
|
-
}
|
|
995
|
-
for (let k = 1; k < cues.length; k += 1) {
|
|
996
|
-
if (cues[k].start < cues[k - 1].end) cues[k].start = cues[k - 1].end;
|
|
997
|
-
}
|
|
998
|
-
return cues.filter((c) => c.end > c.start);
|
|
999
|
-
}
|
|
1000
|
-
function pageAt(events) {
|
|
1001
|
-
const marks = events.filter((e) => e.url && e.type !== "unload").slice().sort((a, b) => a.t - b.t);
|
|
1002
|
-
return (src) => {
|
|
1003
|
-
let url = marks[0]?.url ?? "/";
|
|
1004
|
-
for (const m of marks) {
|
|
1005
|
-
if (m.t <= src) url = m.url;
|
|
1006
|
-
else break;
|
|
1007
|
-
}
|
|
1008
|
-
return url;
|
|
1009
|
-
};
|
|
1010
|
-
}
|
|
1011
|
-
function cueAt(cues, t, fadeMs = 150) {
|
|
1012
|
-
for (const c of cues) {
|
|
1013
|
-
if (t >= c.start && t < c.end) {
|
|
1014
|
-
const alpha = Math.min(1, (t - c.start) / fadeMs, (c.end - t) / fadeMs);
|
|
1015
|
-
return { cue: c, alpha: Math.max(0, alpha) };
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
return null;
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
// src/audio.ts
|
|
1022
|
-
var TRACK_RATE = 48e3;
|
|
1023
|
-
function toWav(pcm, sampleRate) {
|
|
1024
|
-
const data = pcm.length * 2;
|
|
1025
|
-
const buf = new ArrayBuffer(44 + data);
|
|
1026
|
-
const v = new DataView(buf);
|
|
1027
|
-
const str = (at, s) => {
|
|
1028
|
-
for (let i = 0; i < s.length; i += 1) v.setUint8(at + i, s.charCodeAt(i));
|
|
1029
|
-
};
|
|
1030
|
-
str(0, "RIFF");
|
|
1031
|
-
v.setUint32(4, 36 + data, true);
|
|
1032
|
-
str(8, "WAVE");
|
|
1033
|
-
str(12, "fmt ");
|
|
1034
|
-
v.setUint32(16, 16, true);
|
|
1035
|
-
v.setUint16(20, 1, true);
|
|
1036
|
-
v.setUint16(22, 1, true);
|
|
1037
|
-
v.setUint32(24, sampleRate, true);
|
|
1038
|
-
v.setUint32(28, sampleRate * 2, true);
|
|
1039
|
-
v.setUint16(32, 2, true);
|
|
1040
|
-
v.setUint16(34, 16, true);
|
|
1041
|
-
str(36, "data");
|
|
1042
|
-
v.setUint32(40, data, true);
|
|
1043
|
-
for (let i = 0; i < pcm.length; i += 1) v.setInt16(44 + i * 2, pcm[i], true);
|
|
1044
|
-
return new Uint8Array(buf);
|
|
1045
|
-
}
|
|
1046
|
-
function fromWav(bytes) {
|
|
1047
|
-
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1048
|
-
const tag = (at2) => String.fromCharCode(...bytes.subarray(at2, at2 + 4));
|
|
1049
|
-
if (bytes.length < 12 || tag(0) !== "RIFF" || tag(8) !== "WAVE") throw new Error("not a WAV file");
|
|
1050
|
-
let at = 12;
|
|
1051
|
-
let rate = 0;
|
|
1052
|
-
let channels = 1;
|
|
1053
|
-
while (at + 8 <= bytes.length) {
|
|
1054
|
-
const id = tag(at);
|
|
1055
|
-
const size = v.getUint32(at + 4, true);
|
|
1056
|
-
if (id === "fmt ") {
|
|
1057
|
-
const format = v.getUint16(at + 8, true);
|
|
1058
|
-
channels = v.getUint16(at + 10, true);
|
|
1059
|
-
rate = v.getUint32(at + 12, true);
|
|
1060
|
-
const bits = v.getUint16(at + 22, true);
|
|
1061
|
-
if (format !== 1 && format !== 65534 || bits !== 16) throw new Error(`only 16-bit PCM WAV is read (format ${format}, ${bits} bits)`);
|
|
1062
|
-
} else if (id === "data") {
|
|
1063
|
-
if (!rate) throw new Error("a WAV data chunk before its format");
|
|
1064
|
-
const len = size === 0 || size === 4294967295 || at + 8 + size > bytes.length ? bytes.length - at - 8 : size;
|
|
1065
|
-
const n = Math.floor(len / 2 / channels);
|
|
1066
|
-
const pcm = new Int16Array(n);
|
|
1067
|
-
for (let i = 0; i < n; i += 1) {
|
|
1068
|
-
let sum = 0;
|
|
1069
|
-
for (let c = 0; c < channels; c += 1) sum += v.getInt16(at + 8 + (i * channels + c) * 2, true);
|
|
1070
|
-
pcm[i] = Math.round(sum / channels);
|
|
1071
|
-
}
|
|
1072
|
-
return { pcm, sampleRate: rate };
|
|
1073
|
-
}
|
|
1074
|
-
at += 8 + size + size % 2;
|
|
1075
|
-
}
|
|
1076
|
-
throw new Error("a WAV file without data");
|
|
1077
|
-
}
|
|
1078
|
-
function fromS16le(bytes) {
|
|
1079
|
-
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1080
|
-
const out = new Int16Array(Math.floor(bytes.length / 2));
|
|
1081
|
-
for (let i = 0; i < out.length; i += 1) out[i] = v.getInt16(i * 2, true);
|
|
1082
|
-
return out;
|
|
1083
|
-
}
|
|
1084
|
-
function resample(pcm, from, to) {
|
|
1085
|
-
if (from === to || !pcm.length) return pcm.slice();
|
|
1086
|
-
const n = Math.max(1, Math.round(pcm.length * to / from));
|
|
1087
|
-
const out = new Int16Array(n);
|
|
1088
|
-
const step = from / to;
|
|
1089
|
-
for (let i = 0; i < n; i += 1) {
|
|
1090
|
-
const x = i * step;
|
|
1091
|
-
const a = Math.min(pcm.length - 1, Math.floor(x));
|
|
1092
|
-
const b = Math.min(pcm.length - 1, a + 1);
|
|
1093
|
-
out[i] = Math.round(pcm[a] + (pcm[b] - pcm[a]) * (x - a));
|
|
1094
|
-
}
|
|
1095
|
-
return out;
|
|
1096
|
-
}
|
|
1097
|
-
function trimSilence(p, o = {}) {
|
|
1098
|
-
const level = o.level ?? 400;
|
|
1099
|
-
const pad = Math.round((o.padMs ?? 40) * p.sampleRate / 1e3);
|
|
1100
|
-
let a = 0;
|
|
1101
|
-
while (a < p.pcm.length && Math.abs(p.pcm[a]) < level) a += 1;
|
|
1102
|
-
if (a === p.pcm.length) return { pcm: new Int16Array(0), sampleRate: p.sampleRate };
|
|
1103
|
-
let b = p.pcm.length - 1;
|
|
1104
|
-
while (b > a && Math.abs(p.pcm[b]) < level) b -= 1;
|
|
1105
|
-
return { pcm: p.pcm.slice(Math.max(0, a - pad), Math.min(p.pcm.length, b + 1 + pad)), sampleRate: p.sampleRate };
|
|
1106
|
-
}
|
|
1107
|
-
var msOf = (p) => p.pcm.length * 1e3 / p.sampleRate;
|
|
1108
|
-
function mix(lines, totalMs, o = {}) {
|
|
1109
|
-
const out = new Float64Array(Math.max(0, Math.round(totalMs * TRACK_RATE / 1e3)));
|
|
1110
|
-
const fade = Math.round((o.fadeMs ?? 15) * TRACK_RATE / 1e3);
|
|
1111
|
-
for (const l of lines) {
|
|
1112
|
-
const s = resample(l.audio.pcm, l.audio.sampleRate, TRACK_RATE);
|
|
1113
|
-
const at = Math.round(l.start * TRACK_RATE / 1e3);
|
|
1114
|
-
const f = Math.min(fade, Math.floor(s.length / 2));
|
|
1115
|
-
for (let i = 0; i < s.length; i += 1) {
|
|
1116
|
-
const k = at + i;
|
|
1117
|
-
if (k < 0) continue;
|
|
1118
|
-
if (k >= out.length) break;
|
|
1119
|
-
const g = f ? Math.min(1, i / f, (s.length - 1 - i) / f) : 1;
|
|
1120
|
-
out[k] += s[i] * g;
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
const pcm = new Int16Array(out.length);
|
|
1124
|
-
for (let i = 0; i < out.length; i += 1) pcm[i] = Math.max(-32768, Math.min(32767, Math.round(out[i])));
|
|
1125
|
-
return pcm;
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
// src/narration.ts
|
|
1129
|
-
import { preview } from "trace-avatar/assist";
|
|
1130
|
-
function linesOf(steps, script) {
|
|
1131
|
-
const out = /* @__PURE__ */ new Map();
|
|
1132
|
-
steps.forEach((s, i) => {
|
|
1133
|
-
if (s.say?.trim()) out.set(i, s.say.trim());
|
|
1134
|
-
});
|
|
1135
|
-
for (const l of script?.lines ?? []) {
|
|
1136
|
-
const i = l.n - 1;
|
|
1137
|
-
if (!Number.isInteger(i) || i < 0 || i >= steps.length) throw new Error(`the script has a line for step ${l.n}, but the trace has ${steps.length} steps`);
|
|
1138
|
-
if (l.text.trim()) out.set(i, l.text.trim());
|
|
1139
|
-
else out.delete(i);
|
|
1140
|
-
}
|
|
1141
|
-
return out;
|
|
1142
|
-
}
|
|
1143
|
-
function parseScript(json) {
|
|
1144
|
-
const lines = json?.lines;
|
|
1145
|
-
if (!Array.isArray(lines) || lines.some((l) => !l || typeof l.n !== "number" || typeof l.text !== "string")) {
|
|
1146
|
-
throw new Error('a narration script is { "lines": [{ "n": 1, "text": "\u2026" }] }');
|
|
1147
|
-
}
|
|
1148
|
-
return { lines: lines.map((l) => ({ n: l.n, text: l.text })) };
|
|
1149
|
-
}
|
|
1150
|
-
function planNarration(acts, lines, endMs, o = {}) {
|
|
1151
|
-
const first = o.firstMs ?? 300;
|
|
1152
|
-
const after = o.afterActMs ?? 200;
|
|
1153
|
-
const before = o.beforeActMs ?? 250;
|
|
1154
|
-
const gap = o.gapMs ?? 150;
|
|
1155
|
-
const maxHold = o.maxHoldMs ?? 8e3;
|
|
1156
|
-
const plan = { lines: [], holds: [], long: [] };
|
|
1157
|
-
let extra = 0;
|
|
1158
|
-
let prevCut = null;
|
|
1159
|
-
let prevEnd = -Infinity;
|
|
1160
|
-
for (const a of acts.slice().sort((x, y) => x.t - y.t)) {
|
|
1161
|
-
const line = lines.get(a.index);
|
|
1162
|
-
const prevHeld = prevCut === null ? null : prevCut + extra;
|
|
1163
|
-
if (line && line.ms > 0) {
|
|
1164
|
-
const start = Math.max(prevHeld === null ? first : prevHeld + after, prevEnd + gap);
|
|
1165
|
-
const end = start + line.ms;
|
|
1166
|
-
const need = Math.ceil(end - (a.t + extra - before));
|
|
1167
|
-
if (need > 0) {
|
|
1168
|
-
plan.holds.push({ at: Math.max(0, a.t - before, prevCut ?? 0), ms: need });
|
|
1169
|
-
extra += need;
|
|
1170
|
-
if (need > maxHold) plan.long.push({ index: a.index, ms: need });
|
|
1171
|
-
}
|
|
1172
|
-
plan.lines.push({ index: a.index, text: line.text, start: Math.round(start), ms: Math.round(line.ms) });
|
|
1173
|
-
prevEnd = end;
|
|
1379
|
+
|
|
1380
|
+
// src/plan.ts
|
|
1381
|
+
var stepsOf = (events) => events.filter((e) => e.type === "step").slice().sort((a, b) => a.t - b.t);
|
|
1382
|
+
function actionsOf(events, segs, scale = 1) {
|
|
1383
|
+
const out = [];
|
|
1384
|
+
for (const e of events) {
|
|
1385
|
+
const t = outputAt(segs, e.t);
|
|
1386
|
+
if (t === null) continue;
|
|
1387
|
+
if (e.type === "unload") out.push({ t, x: 0, y: 0, kind: "page" });
|
|
1388
|
+
else if (e.type === "step" && e.rect && e.stepType) {
|
|
1389
|
+
out.push({ t, x: (e.rect.x + e.rect.w / 2) * scale, y: (e.rect.y + e.rect.h / 2) * scale, kind: e.stepType });
|
|
1174
1390
|
}
|
|
1175
|
-
prevCut = a.t;
|
|
1176
1391
|
}
|
|
1177
|
-
|
|
1178
|
-
if (endHoldMs > 0) plan.holds.push({ at: endMs, ms: endHoldMs });
|
|
1179
|
-
return { ...plan, endHoldMs };
|
|
1392
|
+
return out.sort((a, b) => a.t - b.t);
|
|
1180
1393
|
}
|
|
1181
|
-
function
|
|
1182
|
-
const
|
|
1183
|
-
|
|
1184
|
-
|
|
1394
|
+
function stepCues(events, segs, endMs, o = {}) {
|
|
1395
|
+
const after = o.afterMs ?? 600;
|
|
1396
|
+
const min = o.minMs ?? 1200;
|
|
1397
|
+
const cues = [];
|
|
1398
|
+
let prev = 0;
|
|
1399
|
+
for (const e of stepsOf(events)) {
|
|
1400
|
+
const t = outputAt(segs, e.t);
|
|
1401
|
+
if (t === null) continue;
|
|
1402
|
+
if (e.say) {
|
|
1403
|
+
const end = Math.min(t + after, endMs);
|
|
1404
|
+
cues.push({ start: Math.max(0, Math.min(prev, end - min)), end, text: e.say });
|
|
1405
|
+
}
|
|
1406
|
+
prev = t;
|
|
1407
|
+
}
|
|
1408
|
+
for (let k = 1; k < cues.length; k += 1) {
|
|
1409
|
+
if (cues[k].start < cues[k - 1].end) cues[k].start = cues[k - 1].end;
|
|
1410
|
+
}
|
|
1411
|
+
return cues.filter((c) => c.end > c.start);
|
|
1412
|
+
}
|
|
1413
|
+
function pageAt(events) {
|
|
1414
|
+
const marks = events.filter((e) => e.url && e.type !== "unload").slice().sort((a, b) => a.t - b.t);
|
|
1415
|
+
return (src) => {
|
|
1416
|
+
let url = marks[0]?.url ?? "/";
|
|
1417
|
+
for (const m of marks) {
|
|
1418
|
+
if (m.t <= src) url = m.url;
|
|
1419
|
+
else break;
|
|
1420
|
+
}
|
|
1421
|
+
return url;
|
|
1185
1422
|
};
|
|
1186
|
-
return lines.map((l) => `[${at(l.start)}] ${l.text}`).join("\n") + (lines.length ? "\n" : "");
|
|
1187
1423
|
}
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
const steps = trace.steps?.length ?? 0;
|
|
1194
|
-
const lines = [];
|
|
1195
|
-
const dropped = [];
|
|
1196
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1197
|
-
for (const l of answer?.lines ?? []) {
|
|
1198
|
-
const n = l?.n;
|
|
1199
|
-
const text = typeof l?.text === "string" ? l.text.trim() : "";
|
|
1200
|
-
if (typeof n !== "number" || !Number.isInteger(n) || n < 1 || n > steps) dropped.push({ n, why: "no such step" });
|
|
1201
|
-
else if (seen.has(n)) dropped.push({ n, why: "a second line for the step" });
|
|
1202
|
-
else if (!text) dropped.push({ n, why: "empty" });
|
|
1203
|
-
else if (text.length > 300) dropped.push({ n, why: "longer than 300 characters" });
|
|
1204
|
-
else {
|
|
1205
|
-
seen.add(n);
|
|
1206
|
-
lines.push({ n, text });
|
|
1424
|
+
function cueAt(cues, t, fadeMs = 150) {
|
|
1425
|
+
for (const c of cues) {
|
|
1426
|
+
if (t >= c.start && t < c.end) {
|
|
1427
|
+
const alpha = Math.min(1, (t - c.start) / fadeMs, (c.end - t) / fadeMs);
|
|
1428
|
+
return { cue: c, alpha: Math.max(0, alpha) };
|
|
1207
1429
|
}
|
|
1208
1430
|
}
|
|
1209
|
-
return
|
|
1431
|
+
return null;
|
|
1210
1432
|
}
|
|
1211
1433
|
|
|
1212
1434
|
// src/repair.ts
|
|
1213
|
-
import { existsSync, readFileSync as
|
|
1214
|
-
import { dirname as dirname2, join } from "path";
|
|
1435
|
+
import { existsSync as existsSync3, readFileSync as readFileSync5 } from "fs";
|
|
1436
|
+
import { dirname as dirname2, join as join2 } from "path";
|
|
1215
1437
|
import { applyRaw, chooseRepair, chooseWait, guessWait, repairSuggestion, typedOf, waitSuggestion } from "trace-avatar/assist";
|
|
1216
1438
|
function repairScriptOf(playerFile) {
|
|
1217
|
-
const file =
|
|
1218
|
-
if (!
|
|
1439
|
+
const file = join2(dirname2(playerFile), "trace-repair.min.js");
|
|
1440
|
+
if (!existsSync3(file)) throw new RenderError(`--repair needs trace-avatar 0.1.7 or later (no ${file})`);
|
|
1219
1441
|
return file;
|
|
1220
1442
|
}
|
|
1221
1443
|
function stallInspector(o) {
|
|
1222
|
-
const code = `${
|
|
1444
|
+
const code = `${readFileSync5(o.script, "utf8")}
|
|
1223
1445
|
;window.__taRepair = TraceAvatarRepair;`;
|
|
1224
1446
|
return async (page, stall) => {
|
|
1225
1447
|
if (stall.reason === "until-timeout") return waitInspect(page, stall.index);
|
|
@@ -1342,69 +1564,7 @@ function chapterCues(starts, endMs) {
|
|
|
1342
1564
|
});
|
|
1343
1565
|
}
|
|
1344
1566
|
|
|
1345
|
-
// src/ai.ts
|
|
1346
|
-
import { resolve as resolve2 } from "path";
|
|
1347
|
-
import { pathToFileURL } from "url";
|
|
1348
|
-
import { gateway, openAiCompatible } from "trace-avatar/assist";
|
|
1349
|
-
async function providerOf(spec, env = process.env, o = {}) {
|
|
1350
|
-
const vision = o.vision ? { visionModel: o.vision } : {};
|
|
1351
|
-
const g = /^gateway(?::([\w.-]+)(?:\/([^,]+))?((?:,[\w.-]+)*))?$/.exec(spec);
|
|
1352
|
-
if (g) {
|
|
1353
|
-
return gateway({
|
|
1354
|
-
...g[1] ? { provider: [g[1], ...(g[3] ?? "").split(",").filter(Boolean)] } : {},
|
|
1355
|
-
...g[2] ? { model: g[2] } : {},
|
|
1356
|
-
...vision,
|
|
1357
|
-
url: env.AI_GATEWAY_URL,
|
|
1358
|
-
key: env.AI_GATEWAY_KEY
|
|
1359
|
-
});
|
|
1360
|
-
}
|
|
1361
|
-
const m = /^openai:(.+)$/.exec(spec);
|
|
1362
|
-
if (m) {
|
|
1363
|
-
if (!env.OPENAI_API_KEY && !env.OPENAI_BASE_URL) throw new Error("--ai openai:\u2026 needs OPENAI_API_KEY (and OPENAI_BASE_URL for another OpenAI-compatible server)");
|
|
1364
|
-
return openAiCompatible({ baseUrl: env.OPENAI_BASE_URL || "https://api.openai.com/v1", ...env.OPENAI_API_KEY ? { key: env.OPENAI_API_KEY } : {}, model: m[1], ...vision });
|
|
1365
|
-
}
|
|
1366
|
-
if (/\.(m?js|cjs)$/.test(spec)) {
|
|
1367
|
-
const mod = await import(pathToFileURL(resolve2(spec)).href);
|
|
1368
|
-
const fn = mod.default ?? mod.ai;
|
|
1369
|
-
if (typeof fn !== "function") throw new Error(`${spec} must export a default function (task, input, schema, { instructions, images }) \u2192 JSON`);
|
|
1370
|
-
return fn;
|
|
1371
|
-
}
|
|
1372
|
-
throw new Error(`--ai is gateway, gateway:<provider>[/<model>][,<provider>\u2026], openai:<model>, or a .js file (got "${spec}")`);
|
|
1373
|
-
}
|
|
1374
|
-
var QA_KINDS = ["error", "empty", "stalled", "personal-data", "wrong-page", "other"];
|
|
1375
|
-
var QA_SCHEMA = {
|
|
1376
|
-
type: "object",
|
|
1377
|
-
required: ["flags"],
|
|
1378
|
-
properties: { flags: { type: "array", items: { type: "object", required: ["frame", "kind", "note"], properties: {
|
|
1379
|
-
frame: { type: "integer" },
|
|
1380
|
-
kind: { enum: [...QA_KINDS] },
|
|
1381
|
-
note: { type: "string", maxLength: 200 }
|
|
1382
|
-
} } } }
|
|
1383
|
-
};
|
|
1384
|
-
var QA_INSTRUCTIONS = `You check frames of a GIF/video of an on-screen guide before it is published: a cursor with a name badge walks through a web app. For each numbered frame, flag only real problems a viewer would notice: an error message or toast ("error"), an empty state or blank page where content is expected ("empty"), a "Looking for \u2026" caption meaning the guide got stuck ("stalled"), personal data such as real email addresses, phone numbers, card or account numbers ("personal-data"), or a page that does not fit the step ("wrong-page"). The cursor, the name badge, highlights and captions are part of the guide, not problems. Reply with JSON only: {"flags":[{"frame":1,"kind":"error","note":"\u2026"}]}, an empty list when all is well.`;
|
|
1385
|
-
async function runQa(ai, frames, context) {
|
|
1386
|
-
if (!frames.length) return [];
|
|
1387
|
-
const input = { title: context.title, steps: context.steps, frames: frames.map((f, k) => ({ frame: k + 1, at: Math.round(f.at / 100) / 10, shows: f.label })) };
|
|
1388
|
-
const answer = await ai("qa", input, QA_SCHEMA, { instructions: QA_INSTRUCTIONS, images: frames.map((f, k) => ({ png: f.png, label: `Frame ${k + 1}: ${f.label}` })) });
|
|
1389
|
-
const list = answer && typeof answer === "object" && Array.isArray(answer.flags) ? answer.flags : null;
|
|
1390
|
-
if (!list) throw new Error('the QA answer has no "flags" list');
|
|
1391
|
-
const out = [];
|
|
1392
|
-
for (const item of list) {
|
|
1393
|
-
const r = item;
|
|
1394
|
-
if (!Number.isInteger(r?.frame) || r.frame < 1 || r.frame > frames.length) continue;
|
|
1395
|
-
const kind = QA_KINDS.includes(r.kind) ? r.kind : "other";
|
|
1396
|
-
const note = typeof r.note === "string" ? r.note.replace(/\s+/g, " ").trim().slice(0, 200) : "";
|
|
1397
|
-
out.push({ at: frames[r.frame - 1].at, kind, note });
|
|
1398
|
-
}
|
|
1399
|
-
return out.sort((a, b) => a.at - b.at);
|
|
1400
|
-
}
|
|
1401
|
-
|
|
1402
1567
|
// src/render.ts
|
|
1403
|
-
import { readFileSync as readFileSync5, writeFileSync, mkdirSync, mkdtempSync, rmSync } from "fs";
|
|
1404
|
-
import { tmpdir } from "os";
|
|
1405
|
-
import { dirname as dirname3, extname as extname2, join as join2 } from "path";
|
|
1406
|
-
import { chromium as chromium2 } from "playwright";
|
|
1407
|
-
import { PNG as PNG2 } from "pngjs";
|
|
1408
1568
|
import { describe as describeGuide } from "trace-avatar/assist";
|
|
1409
1569
|
var formatOf = (file) => {
|
|
1410
1570
|
const ext = extname2(file).toLowerCase();
|
|
@@ -1425,7 +1585,7 @@ function cardOf(v, fallback) {
|
|
|
1425
1585
|
return { ...fallback, ...v };
|
|
1426
1586
|
}
|
|
1427
1587
|
async function render(o) {
|
|
1428
|
-
|
|
1588
|
+
let trace = typeof o.trace === "string" ? JSON.parse(readFileSync6(o.trace, "utf8")) : o.trace;
|
|
1429
1589
|
if (!trace || typeof trace !== "object") throw new RenderError("the trace must be an object or a .trace.json path");
|
|
1430
1590
|
const mode = o.mode ?? "overlay";
|
|
1431
1591
|
if (mode === "perform" && !o.allowPerform) {
|
|
@@ -1434,14 +1594,18 @@ async function render(o) {
|
|
|
1434
1594
|
const outs = o.out.map((x) => typeof x === "string" ? { file: x } : x).map((x) => ({ ...x, format: formatOf(x.file) }));
|
|
1435
1595
|
if (!outs.length) throw new RenderError("nothing to write: give at least one output (.gif or .mp4)");
|
|
1436
1596
|
const ffmpeg = outs.some((x) => x.format === "mp4") ? findFfmpeg() : null;
|
|
1437
|
-
if (outs.some((x) => x.format === "mp4") && !ffmpeg) throw new RenderError(
|
|
1597
|
+
if (outs.some((x) => x.format === "mp4") && !ffmpeg) throw new RenderError(FFMPEG_MISSING);
|
|
1438
1598
|
const t = trace;
|
|
1439
1599
|
const viewport = o.viewport ?? t.meta?.viewport ?? { w: 1280, h: 800 };
|
|
1440
1600
|
const burn = o.captions === "burn" || o.captions === "both";
|
|
1441
1601
|
if ((o.qa || o.alt) && !o.ai) throw new RenderError("qa and alt need an AI: pass ai (--ai)");
|
|
1442
1602
|
if (o.voice && !outs.some((x) => x.format === "mp4")) throw new RenderError("narration is for video: add an .mp4 output (a GIF has no sound)");
|
|
1443
|
-
const spoken = o.voice ? linesOf(trace.steps ?? [], o.script === void 0 ? void 0 : parseScript(typeof o.script === "string" ? JSON.parse(
|
|
1603
|
+
const spoken = o.voice ? linesOf(trace.steps ?? [], o.script === void 0 ? void 0 : parseScript(typeof o.script === "string" ? JSON.parse(readFileSync6(o.script, "utf8")) : o.script)) : null;
|
|
1444
1604
|
if (spoken && !spoken.size) throw new RenderError("nothing to say: no step has a caption (say), and no script was given");
|
|
1605
|
+
if (spoken && o.script !== void 0) {
|
|
1606
|
+
const steps2 = trace.steps ?? [];
|
|
1607
|
+
trace = { ...trace, steps: steps2.map((st, i) => spoken.has(i) && spoken.get(i) !== st.say ? { ...st, say: spoken.get(i) } : st) };
|
|
1608
|
+
}
|
|
1445
1609
|
if (o.repairVision && !(o.repair && o.ai)) throw new RenderError("repairVision needs repair and an AI");
|
|
1446
1610
|
const capOpts = {
|
|
1447
1611
|
url: o.url,
|
|
@@ -1474,8 +1638,8 @@ async function render(o) {
|
|
|
1474
1638
|
if (!(o.repair && e instanceof RenderError && e.stall)) throw e;
|
|
1475
1639
|
const outcome = await repairGuide({ trace, capture: capOpts, ...o.ai ? { ai: o.ai } : {}, ...o.repairVision ? { vision: true } : {}, first: e });
|
|
1476
1640
|
const file = `${base(outs[0].file)}.repair.json`;
|
|
1477
|
-
|
|
1478
|
-
|
|
1641
|
+
mkdirSync2(dirname3(file) || ".", { recursive: true });
|
|
1642
|
+
writeFileSync2(file, `${JSON.stringify({ task: "repair", suggestions: outcome.suggestions, unresolved: outcome.unresolved, passed: outcome.passed }, null, 2)}
|
|
1479
1643
|
`);
|
|
1480
1644
|
const n = outcome.suggestions.length;
|
|
1481
1645
|
const ok = outcome.suggestions.filter((x) => x.verified).length;
|
|
@@ -1514,7 +1678,7 @@ repair: ${n ? `${n} fix${n > 1 ? "es" : ""} (${ok} verified${outcome.passed ? ",
|
|
|
1514
1678
|
});
|
|
1515
1679
|
if (described?.alt) {
|
|
1516
1680
|
const file = `${base(outs[0].file)}.alt.txt`;
|
|
1517
|
-
|
|
1681
|
+
writeFileSync2(file, `${described.alt}
|
|
1518
1682
|
`);
|
|
1519
1683
|
r.alt = file;
|
|
1520
1684
|
}
|
|
@@ -1574,6 +1738,8 @@ async function finish(cap, outs, o = {}) {
|
|
|
1574
1738
|
const frameStyle = o.frame ?? "none";
|
|
1575
1739
|
const canvas = o.canvas !== void 0 ? canvasOf(o.canvas) : void 0;
|
|
1576
1740
|
const canvases = outs.map((x) => x.canvas !== void 0 ? canvasOf(x.canvas) : canvas);
|
|
1741
|
+
const fillShapes = (o.fill ?? []).map((f) => canvasOf(f));
|
|
1742
|
+
const fills = canvases.map((c) => !!c && fillShapes.some((f) => f.width * c.height === f.height * c.width));
|
|
1577
1743
|
const mark = o.watermark !== void 0 ? watermarkOf(o.watermark) : void 0;
|
|
1578
1744
|
const background = o.background ?? DEFAULT_BACKGROUND;
|
|
1579
1745
|
const burn = o.captions === "burn" || o.captions === "both";
|
|
@@ -1587,9 +1753,9 @@ async function finish(cap, outs, o = {}) {
|
|
|
1587
1753
|
cues: o.captions ? stepCues(cap.events.map((e) => e.stepType === "card" ? { ...e, say: void 0 } : e), sg, ms) : []
|
|
1588
1754
|
};
|
|
1589
1755
|
};
|
|
1590
|
-
const
|
|
1591
|
-
const plain = narr ? variant(cut) :
|
|
1592
|
-
const cues =
|
|
1756
|
+
const main2 = variant(segs);
|
|
1757
|
+
const plain = narr ? variant(cut) : main2;
|
|
1758
|
+
const cues = main2.cues;
|
|
1593
1759
|
const page = pageAt(cap.events);
|
|
1594
1760
|
const titleMs = o.cards?.title ? TITLE_MS : 0;
|
|
1595
1761
|
const endMs = o.cards?.end ? END_MS : 0;
|
|
@@ -1601,14 +1767,14 @@ async function finish(cap, outs, o = {}) {
|
|
|
1601
1767
|
const qaFrames = [];
|
|
1602
1768
|
let poster;
|
|
1603
1769
|
let wav;
|
|
1604
|
-
const tmp = narr ?
|
|
1770
|
+
const tmp = narr ? mkdtempSync2(join3(tmpdir2(), "tar-voice-")) : null;
|
|
1605
1771
|
try {
|
|
1606
1772
|
layers = browser ? await createLayers(browser) : null;
|
|
1607
1773
|
for (const [n, out] of outs.entries()) {
|
|
1608
|
-
|
|
1774
|
+
mkdirSync2(dirname3(out.file) || ".", { recursive: true });
|
|
1609
1775
|
const fps = out.fps ?? (out.format === "gif" ? 12 : 30);
|
|
1610
|
-
const layout = layoutOf(srcW, srcH, out.width ?? (out.format === "gif" ? 800 : void 0), frameStyle, canvases[n]);
|
|
1611
|
-
const v = out.format === "gif" ? plain :
|
|
1776
|
+
const layout = layoutOf(srcW, srcH, out.width ?? (out.format === "gif" ? 800 : void 0), frameStyle, canvases[n], fills[n]);
|
|
1777
|
+
const v = out.format === "gif" ? plain : main2;
|
|
1612
1778
|
const { cam, segs: segs2, contentMs: contentMs2 } = v;
|
|
1613
1779
|
const cues2 = v.cues;
|
|
1614
1780
|
const times = frameTimes(segs2, fps);
|
|
@@ -1681,7 +1847,7 @@ async function finish(cap, outs, o = {}) {
|
|
|
1681
1847
|
const w = createGifWriter({ width: layout.width, height: layout.height, fps, palette: paletteOf(samples, samples.length) });
|
|
1682
1848
|
for (let m = 0; m < total; m += 1) w.add(frameAt(m));
|
|
1683
1849
|
const gif = w.finish();
|
|
1684
|
-
|
|
1850
|
+
writeFileSync2(out.file, gif.bytes);
|
|
1685
1851
|
outputs.push({ file: out.file, format: "gif", width: layout.width, height: layout.height, fps, frames: total, bytes: gif.bytes.length, durationMs: gif.durationMs });
|
|
1686
1852
|
} else {
|
|
1687
1853
|
const all = function* () {
|
|
@@ -1690,16 +1856,16 @@ async function finish(cap, outs, o = {}) {
|
|
|
1690
1856
|
let audio;
|
|
1691
1857
|
if (narr && said && tmp) {
|
|
1692
1858
|
const track = mix(said.lines.map((l) => ({ start: l.start + titleMs, audio: narr.lines.get(l.index).audio })), total * 1e3 / fps);
|
|
1693
|
-
audio =
|
|
1694
|
-
|
|
1859
|
+
audio = join3(tmp, `${n}.wav`);
|
|
1860
|
+
writeFileSync2(audio, toWav(track, TRACK_RATE));
|
|
1695
1861
|
if (narr.out && !wav) {
|
|
1696
|
-
|
|
1697
|
-
|
|
1862
|
+
mkdirSync2(dirname3(narr.out) || ".", { recursive: true });
|
|
1863
|
+
writeFileSync2(narr.out, toWav(track, TRACK_RATE));
|
|
1698
1864
|
wav = narr.out;
|
|
1699
1865
|
}
|
|
1700
1866
|
}
|
|
1701
1867
|
await encodeMp4(all(), { file: out.file, fps, width: layout.width, height: layout.height, ...audio ? { audio } : {} });
|
|
1702
|
-
outputs.push({ file: out.file, format: "mp4", width: layout.width, height: layout.height, fps, frames: total, bytes:
|
|
1868
|
+
outputs.push({ file: out.file, format: "mp4", width: layout.width, height: layout.height, fps, frames: total, bytes: readFileSync6(out.file).length, durationMs: Math.round(total * 1e3 / fps) });
|
|
1703
1869
|
}
|
|
1704
1870
|
if (n === 0 && o.poster) {
|
|
1705
1871
|
const click = cap.events.find((e) => e.type === "step" && (e.stepType === "click" || e.stepType === "dblclick"));
|
|
@@ -1710,8 +1876,8 @@ async function finish(cap, outs, o = {}) {
|
|
|
1710
1876
|
const png = new PNG2({ width: img.width, height: img.height });
|
|
1711
1877
|
png.data.set(img.data);
|
|
1712
1878
|
poster = typeof o.poster === "string" ? o.poster : `${base(out.file)}.png`;
|
|
1713
|
-
|
|
1714
|
-
|
|
1879
|
+
mkdirSync2(dirname3(poster) || ".", { recursive: true });
|
|
1880
|
+
writeFileSync2(poster, PNG2.sync.write(png));
|
|
1715
1881
|
}
|
|
1716
1882
|
if (n === 0 && o.qa) {
|
|
1717
1883
|
const steps = cap.events.filter((e) => e.type === "step" && e.index !== void 0);
|
|
@@ -1735,7 +1901,7 @@ async function finish(cap, outs, o = {}) {
|
|
|
1735
1901
|
await layers?.close().catch(() => {
|
|
1736
1902
|
});
|
|
1737
1903
|
await browser?.close();
|
|
1738
|
-
if (tmp)
|
|
1904
|
+
if (tmp) rmSync2(tmp, { recursive: true, force: true });
|
|
1739
1905
|
}
|
|
1740
1906
|
let vtt;
|
|
1741
1907
|
let chapters;
|
|
@@ -1743,15 +1909,15 @@ async function finish(cap, outs, o = {}) {
|
|
|
1743
1909
|
const shift = (c) => ({ ...c, start: c.start + titleMs, end: c.end + titleMs });
|
|
1744
1910
|
vtt = `${base(outs[0].file)}.vtt`;
|
|
1745
1911
|
chapters = `${base(outs[0].file)}.chapters.vtt`;
|
|
1746
|
-
|
|
1747
|
-
|
|
1912
|
+
writeFileSync2(vtt, toVtt(cues.map(shift)));
|
|
1913
|
+
writeFileSync2(chapters, toVtt(o.chapterNames?.length ? namedChapters(o.chapterNames, cap.events, segs, contentMs).map(shift) : chapterCues(cues.map((c) => ({ t: c.start, text: c.text })), contentMs).map(shift)));
|
|
1748
1914
|
}
|
|
1749
1915
|
const qa = o.qa ? await runQa(o.qa.ai, qaFrames, { title: o.qa.title, steps: o.qa.steps }) : void 0;
|
|
1750
1916
|
let narration;
|
|
1751
1917
|
if (said) {
|
|
1752
1918
|
const mp4 = outs.find((x) => x.format === "mp4");
|
|
1753
1919
|
const text = `${base(mp4.file)}.narration.txt`;
|
|
1754
|
-
|
|
1920
|
+
writeFileSync2(text, narrationText(said.lines, titleMs));
|
|
1755
1921
|
narration = { lines: said.lines.map((l) => ({ ...l, start: l.start + titleMs })), holdMs: said.holds.reduce((a, h) => a + h.ms, 0), text, ...wav ? { wav } : {} };
|
|
1756
1922
|
}
|
|
1757
1923
|
const rel = (t) => Math.round(t - start);
|
|
@@ -1769,8 +1935,8 @@ async function finish(cap, outs, o = {}) {
|
|
|
1769
1935
|
...warnings.length ? { warnings } : {}
|
|
1770
1936
|
};
|
|
1771
1937
|
if (o.timeline) {
|
|
1772
|
-
|
|
1773
|
-
|
|
1938
|
+
mkdirSync2(dirname3(o.timeline) || ".", { recursive: true });
|
|
1939
|
+
writeFileSync2(o.timeline, JSON.stringify(result, null, 2));
|
|
1774
1940
|
}
|
|
1775
1941
|
return result;
|
|
1776
1942
|
}
|
|
@@ -1780,173 +1946,46 @@ function namedChapters(names, events, segs, endMs) {
|
|
|
1780
1946
|
const t = e ? outputAt(segs, e.t) : null;
|
|
1781
1947
|
return t === null ? [] : [{ t: c.index === names[0].index ? 0 : t, text: c.name }];
|
|
1782
1948
|
}).sort((a, b) => a.t - b.t);
|
|
1783
|
-
return starts.map((c, k) => ({ start: c.t, end: starts[k + 1]?.t ?? endMs, text: c.text }));
|
|
1784
|
-
}
|
|
1785
|
-
function badgeAt(W, H, w, h, at = "bottom-right") {
|
|
1786
|
-
const m = Math.round(Math.min(W, H) * 0.025);
|
|
1787
|
-
return { x: at.endsWith("left") ? m : W - w - m, y: at.startsWith("top") ? m : H - h - m };
|
|
1788
|
-
}
|
|
1789
|
-
async function artFor(layers, layout, o) {
|
|
1790
|
-
const art = { base: null, captions: /* @__PURE__ */ new Map(), mid: [] };
|
|
1791
|
-
if (!layers) return art;
|
|
1792
|
-
if (o.frameStyle !== "none" || layout.canvas) {
|
|
1793
|
-
art.base = /* @__PURE__ */ new Map();
|
|
1794
|
-
for (const url of o.urls.length ? o.urls : ["/"]) art.base.set(url, await layers.base(layout, { background: o.background, path: url, title: o.titles[url] }));
|
|
1795
|
-
}
|
|
1796
|
-
const fontPx = layout.canvas ? Math.max(12, Math.round(layout.width * 0.034)) : Math.max(12, Math.round(layout.content.w * 0.026));
|
|
1797
|
-
const maxWidth = Math.round(layout.canvas ? layout.width * 0.9 : layout.content.w * 0.8);
|
|
1798
|
-
for (const c of o.cues) {
|
|
1799
|
-
if (!art.captions.has(c.text)) art.captions.set(c.text, await layers.caption(c.text, { maxWidth, fontPx }));
|
|
1800
|
-
}
|
|
1801
|
-
if (o.cards?.title) art.title = await layers.card(layout, { ...o.cards.title, background: o.background });
|
|
1802
|
-
if (o.cards?.end) art.end = await layers.card(layout, { ...o.cards.end, background: o.background });
|
|
1803
|
-
for (const c of o.mid ?? []) art.mid.push(await layers.card(layout, { ...c, background: o.background }));
|
|
1804
|
-
if (o.mark) {
|
|
1805
|
-
const img = await layers.badge(o.mark.text, { fontPx: Math.max(11, Math.round(Math.min(layout.width, layout.height) * 0.024)), ...o.mark.logo ? { logo: o.mark.logo } : {} });
|
|
1806
|
-
const { x, y } = badgeAt(layout.width, layout.height, img.width, img.height, o.mark.at);
|
|
1807
|
-
art.badge = { img, x, y, alpha: o.mark.opacity ?? 0.9 };
|
|
1808
|
-
for (const k of ["title", "end"]) {
|
|
1809
|
-
const c = art[k];
|
|
1810
|
-
if (c) {
|
|
1811
|
-
const d = copy(c);
|
|
1812
|
-
overlay(d, img, x, y, art.badge.alpha);
|
|
1813
|
-
art[k] = d;
|
|
1814
|
-
}
|
|
1815
|
-
}
|
|
1816
|
-
art.mid = art.mid.map((c) => {
|
|
1817
|
-
const d = copy(c);
|
|
1818
|
-
overlay(d, img, x, y, art.badge.alpha);
|
|
1819
|
-
return d;
|
|
1820
|
-
});
|
|
1821
|
-
}
|
|
1822
|
-
return art;
|
|
1823
|
-
}
|
|
1824
|
-
|
|
1825
|
-
// src/voice.ts
|
|
1826
|
-
import { spawnSync as spawnSync2 } from "child_process";
|
|
1827
|
-
import { createHash } from "crypto";
|
|
1828
|
-
import { existsSync as existsSync2, mkdirSync as mkdirSync2, mkdtempSync as mkdtempSync2, readFileSync as readFileSync6, rmSync as rmSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
1829
|
-
import { tmpdir as tmpdir2 } from "os";
|
|
1830
|
-
import { join as join3, resolve as resolve3 } from "path";
|
|
1831
|
-
import { pathToFileURL as pathToFileURL2 } from "url";
|
|
1832
|
-
var GEMINI_TTS = { model: "gemini-2.5-flash-preview-tts", voice: "Kore" };
|
|
1833
|
-
var sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
1834
|
-
async function post(f, url, headers, body) {
|
|
1835
|
-
for (let n = 1; ; n += 1) {
|
|
1836
|
-
let res = null;
|
|
1837
|
-
try {
|
|
1838
|
-
res = await f(url, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body) });
|
|
1839
|
-
} catch (e) {
|
|
1840
|
-
if (n >= 3) throw new Error(`could not reach the voice (${e.message})`);
|
|
1841
|
-
}
|
|
1842
|
-
if (res?.ok) return res;
|
|
1843
|
-
if (res) {
|
|
1844
|
-
const text = await res.text();
|
|
1845
|
-
let gw = null;
|
|
1846
|
-
try {
|
|
1847
|
-
const j = JSON.parse(text);
|
|
1848
|
-
if (j?.error?.source === "ai-interface") gw = j.error;
|
|
1849
|
-
} catch {
|
|
1850
|
-
}
|
|
1851
|
-
if (gw) throw new Error(`the AI gateway refused the voice call: ${gw.message}`);
|
|
1852
|
-
if (!(res.status === 429 || res.status >= 500) || n >= 3) throw new Error(`the voice call failed (${res.status}): ${text.slice(0, 300)}`);
|
|
1853
|
-
}
|
|
1854
|
-
await sleep2(Math.min(400 * 2 ** (n - 1) * (0.5 + Math.random() / 2), 4e3));
|
|
1855
|
-
}
|
|
1856
|
-
}
|
|
1857
|
-
function geminiVoice(o) {
|
|
1858
|
-
const model = o.model ?? GEMINI_TTS.model;
|
|
1859
|
-
return async (text) => {
|
|
1860
|
-
const res = await post(
|
|
1861
|
-
o.fetch ?? fetch,
|
|
1862
|
-
`${o.url.replace(/\/+$/, "")}/models/${model}:generateContent`,
|
|
1863
|
-
{ ...o.bearer ? { authorization: `Bearer ${o.key}` } : { "x-goog-api-key": o.key }, ...o.headers },
|
|
1864
|
-
{
|
|
1865
|
-
contents: [{ role: "user", parts: [{ text }] }],
|
|
1866
|
-
generationConfig: { responseModalities: ["AUDIO"], speechConfig: { voiceConfig: { prebuiltVoiceConfig: { voiceName: o.voice ?? GEMINI_TTS.voice } } } }
|
|
1867
|
-
}
|
|
1868
|
-
);
|
|
1869
|
-
const json = await res.json();
|
|
1870
|
-
const part = json.candidates?.[0]?.content?.parts?.find((p) => p.inlineData?.data || p.inline_data?.data);
|
|
1871
|
-
const data = part?.inlineData?.data ?? part?.inline_data?.data;
|
|
1872
|
-
if (!data) throw new Error(`${model} answered without audio`);
|
|
1873
|
-
const mime = part?.inlineData?.mimeType ?? part?.inline_data?.mime_type ?? "";
|
|
1874
|
-
const rate = Number(/rate=(\d+)/.exec(mime)?.[1] ?? 24e3);
|
|
1875
|
-
return { pcm: fromS16le(new Uint8Array(Buffer.from(data, "base64"))), sampleRate: rate };
|
|
1876
|
-
};
|
|
1877
|
-
}
|
|
1878
|
-
function openAiVoice(o) {
|
|
1879
|
-
return async (text) => {
|
|
1880
|
-
const res = await post(
|
|
1881
|
-
o.fetch ?? fetch,
|
|
1882
|
-
`${o.baseUrl.replace(/\/+$/, "")}/audio/speech`,
|
|
1883
|
-
o.key ? { authorization: `Bearer ${o.key}` } : {},
|
|
1884
|
-
{ model: o.model, voice: o.voice ?? "alloy", input: text, response_format: "pcm" }
|
|
1885
|
-
);
|
|
1886
|
-
return { pcm: fromS16le(new Uint8Array(await res.arrayBuffer())), sampleRate: 24e3 };
|
|
1887
|
-
};
|
|
1888
|
-
}
|
|
1889
|
-
function sayVoice(o = {}) {
|
|
1890
|
-
return async (text) => {
|
|
1891
|
-
const dir = mkdtempSync2(join3(tmpdir2(), "tar-say-"));
|
|
1892
|
-
try {
|
|
1893
|
-
const file = join3(dir, "line.wav");
|
|
1894
|
-
const r = spawnSync2("say", [...o.voice ? ["-v", o.voice] : [], "-o", file, "--file-format=WAVE", "--data-format=LEI16@24000", text], { encoding: "utf8" });
|
|
1895
|
-
if (r.error || r.status !== 0) throw new Error(`say failed${r.error ? ` (${r.error.message})` : `: ${r.stderr}`}; --voice say needs macOS`);
|
|
1896
|
-
return fromWav(new Uint8Array(readFileSync6(file)));
|
|
1897
|
-
} finally {
|
|
1898
|
-
rmSync2(dir, { recursive: true, force: true });
|
|
1899
|
-
}
|
|
1900
|
-
};
|
|
1901
|
-
}
|
|
1902
|
-
function cached(voice, id, dir) {
|
|
1903
|
-
const v = (async (text) => {
|
|
1904
|
-
const file = join3(dir, `${createHash("sha256").update(`${id}
|
|
1905
|
-
${text}`).digest("hex").slice(0, 32)}.wav`);
|
|
1906
|
-
if (existsSync2(file)) {
|
|
1907
|
-
v.hits += 1;
|
|
1908
|
-
return fromWav(new Uint8Array(readFileSync6(file)));
|
|
1909
|
-
}
|
|
1910
|
-
const p = await voice(text);
|
|
1911
|
-
v.misses += 1;
|
|
1912
|
-
mkdirSync2(dir, { recursive: true });
|
|
1913
|
-
writeFileSync2(file, toWav(p.pcm, p.sampleRate));
|
|
1914
|
-
return p;
|
|
1915
|
-
});
|
|
1916
|
-
v.hits = 0;
|
|
1917
|
-
v.misses = 0;
|
|
1918
|
-
return v;
|
|
1949
|
+
return starts.map((c, k) => ({ start: c.t, end: starts[k + 1]?.t ?? endMs, text: c.text }));
|
|
1919
1950
|
}
|
|
1920
|
-
|
|
1921
|
-
const
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
...g[1] ? { model: g[1] } : {},
|
|
1931
|
-
...g[2] ? { voice: g[2] } : {},
|
|
1932
|
-
headers: { "x-ai-feature": "trace-avatar-voice" },
|
|
1933
|
-
...o.fetch ? { fetch: o.fetch } : {}
|
|
1934
|
-
}));
|
|
1951
|
+
function badgeAt(W, H, w, h, at = "bottom-right") {
|
|
1952
|
+
const m = Math.round(Math.min(W, H) * 0.025);
|
|
1953
|
+
return { x: at.endsWith("left") ? m : W - w - m, y: at.startsWith("top") ? m : H - h - m };
|
|
1954
|
+
}
|
|
1955
|
+
async function artFor(layers, layout, o) {
|
|
1956
|
+
const art = { base: null, captions: /* @__PURE__ */ new Map(), mid: [] };
|
|
1957
|
+
if (!layers) return art;
|
|
1958
|
+
if (o.frameStyle !== "none" || layout.canvas) {
|
|
1959
|
+
art.base = /* @__PURE__ */ new Map();
|
|
1960
|
+
for (const url of o.urls.length ? o.urls : ["/"]) art.base.set(url, await layers.base(layout, { background: o.background, path: url, title: o.titles[url] }));
|
|
1935
1961
|
}
|
|
1936
|
-
const
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1962
|
+
const fontPx = layout.canvas ? Math.max(12, Math.round(layout.width * 0.034)) : Math.max(12, Math.round(layout.content.w * 0.026));
|
|
1963
|
+
const maxWidth = Math.round(layout.canvas ? layout.width * 0.9 : layout.content.w * 0.8);
|
|
1964
|
+
for (const c of o.cues) {
|
|
1965
|
+
if (!art.captions.has(c.text)) art.captions.set(c.text, await layers.caption(c.text, { maxWidth, fontPx }));
|
|
1940
1966
|
}
|
|
1941
|
-
|
|
1942
|
-
if (
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
const
|
|
1946
|
-
|
|
1947
|
-
|
|
1967
|
+
if (o.cards?.title) art.title = await layers.card(layout, { ...o.cards.title, background: o.background });
|
|
1968
|
+
if (o.cards?.end) art.end = await layers.card(layout, { ...o.cards.end, background: o.background });
|
|
1969
|
+
for (const c of o.mid ?? []) art.mid.push(await layers.card(layout, { ...c, background: o.background }));
|
|
1970
|
+
if (o.mark) {
|
|
1971
|
+
const img = await layers.badge(o.mark.text, { fontPx: Math.max(11, Math.round(Math.min(layout.width, layout.height) * 0.024)), ...o.mark.logo ? { logo: o.mark.logo } : {} });
|
|
1972
|
+
const { x, y } = badgeAt(layout.width, layout.height, img.width, img.height, o.mark.at);
|
|
1973
|
+
art.badge = { img, x, y, alpha: o.mark.opacity ?? 0.9 };
|
|
1974
|
+
for (const k of ["title", "end"]) {
|
|
1975
|
+
const c = art[k];
|
|
1976
|
+
if (c) {
|
|
1977
|
+
const d = copy(c);
|
|
1978
|
+
overlay(d, img, x, y, art.badge.alpha);
|
|
1979
|
+
art[k] = d;
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
art.mid = art.mid.map((c) => {
|
|
1983
|
+
const d = copy(c);
|
|
1984
|
+
overlay(d, img, x, y, art.badge.alpha);
|
|
1985
|
+
return d;
|
|
1986
|
+
});
|
|
1948
1987
|
}
|
|
1949
|
-
|
|
1988
|
+
return art;
|
|
1950
1989
|
}
|
|
1951
1990
|
|
|
1952
1991
|
// src/catalogue.ts
|
|
@@ -2100,6 +2139,1333 @@ async function checkCatalogue(doc, o = {}) {
|
|
|
2100
2139
|
return { ok: !problems.length, problems, warnings, videos: entries.length, fetched };
|
|
2101
2140
|
}
|
|
2102
2141
|
|
|
2142
|
+
// src/campaign.ts
|
|
2143
|
+
import { spawn as spawn2 } from "child_process";
|
|
2144
|
+
import { existsSync as existsSync5, mkdirSync as mkdirSync4, readFileSync as readFileSync8, rmSync as rmSync3, writeFileSync as writeFileSync4 } from "fs";
|
|
2145
|
+
import { dirname as dirname5, join as join4, resolve as resolve5 } from "path";
|
|
2146
|
+
import { pathToFileURL as pathToFileURL4 } from "url";
|
|
2147
|
+
|
|
2148
|
+
// src/director.ts
|
|
2149
|
+
import { existsSync as existsSync4, mkdirSync as mkdirSync3, readFileSync as readFileSync7, writeFileSync as writeFileSync3 } from "fs";
|
|
2150
|
+
import { dirname as dirname4, extname as extname3, resolve as resolve4 } from "path";
|
|
2151
|
+
import { pathToFileURL as pathToFileURL3 } from "url";
|
|
2152
|
+
import { chromium as chromium3 } from "playwright";
|
|
2153
|
+
import { PNG as PNG3 } from "pngjs";
|
|
2154
|
+
var DIRECTOR_VIEWPORT = { width: 1280, height: 800 };
|
|
2155
|
+
var FPS = 30;
|
|
2156
|
+
var RING_MS = 420;
|
|
2157
|
+
var SKIP_KEEP_MS = 1600;
|
|
2158
|
+
var CURSOR_SIZE = 1.5;
|
|
2159
|
+
var ARROW = [[0, 0], [0, 17], [4.2, 13.1], [7.1, 19.6], [9.8, 18.4], [7, 12.1], [12.6, 12.1]];
|
|
2160
|
+
function inside(poly, x, y) {
|
|
2161
|
+
let c = false;
|
|
2162
|
+
for (let i = 0, j = poly.length - 1; i < poly.length; j = i, i += 1) {
|
|
2163
|
+
const [xi, yi] = poly[i];
|
|
2164
|
+
const [xj, yj] = poly[j];
|
|
2165
|
+
if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) c = !c;
|
|
2166
|
+
}
|
|
2167
|
+
return c;
|
|
2168
|
+
}
|
|
2169
|
+
function arrowSprite(scale = 1) {
|
|
2170
|
+
const pad = 2;
|
|
2171
|
+
const w = Math.ceil((13 + pad * 2) * scale);
|
|
2172
|
+
const h = Math.ceil((20 + pad * 2) * scale);
|
|
2173
|
+
const data = new Uint8Array(w * h * 4);
|
|
2174
|
+
const n = 4;
|
|
2175
|
+
const inner = ARROW.map(([x, y]) => [x * 0.8 + 1.3, y * 0.8 + 2.4]);
|
|
2176
|
+
for (let y = 0; y < h; y += 1) {
|
|
2177
|
+
for (let x = 0; x < w; x += 1) {
|
|
2178
|
+
let edge = 0;
|
|
2179
|
+
let body = 0;
|
|
2180
|
+
for (let sy = 0; sy < n; sy += 1) {
|
|
2181
|
+
for (let sx = 0; sx < n; sx += 1) {
|
|
2182
|
+
const px = (x + (sx + 0.5) / n) / scale - pad;
|
|
2183
|
+
const py = (y + (sy + 0.5) / n) / scale - pad;
|
|
2184
|
+
if (inside(ARROW, px, py)) {
|
|
2185
|
+
edge += 1;
|
|
2186
|
+
if (inside(inner, px, py)) body += 1;
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
const k = (y * w + x) * 4;
|
|
2191
|
+
const a = edge / (n * n);
|
|
2192
|
+
const b = edge ? body / edge : 0;
|
|
2193
|
+
const v = Math.round(255 * b + 24 * (1 - b));
|
|
2194
|
+
data[k] = v;
|
|
2195
|
+
data[k + 1] = v;
|
|
2196
|
+
data[k + 2] = v;
|
|
2197
|
+
data[k + 3] = Math.round(a * 255);
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
return { width: w, height: h, data, ox: Math.round(pad * scale), oy: Math.round(pad * scale) };
|
|
2201
|
+
}
|
|
2202
|
+
function blend(img, x, y, r, g, b, a) {
|
|
2203
|
+
if (x < 0 || y < 0 || x >= img.width || y >= img.height || a <= 0) return;
|
|
2204
|
+
const k = (y * img.width + x) * 4;
|
|
2205
|
+
img.data[k] = Math.round(img.data[k] * (1 - a) + r * a);
|
|
2206
|
+
img.data[k + 1] = Math.round(img.data[k + 1] * (1 - a) + g * a);
|
|
2207
|
+
img.data[k + 2] = Math.round(img.data[k + 2] * (1 - a) + b * a);
|
|
2208
|
+
}
|
|
2209
|
+
function drawCursor(img, sprite, x, y, ring = null) {
|
|
2210
|
+
if (ring) {
|
|
2211
|
+
const p = ring.p;
|
|
2212
|
+
const rad = 8 + 18 * p;
|
|
2213
|
+
const alpha = 0.55 * (1 - p);
|
|
2214
|
+
const x0 = Math.floor(ring.x - rad - 3);
|
|
2215
|
+
const y0 = Math.floor(ring.y - rad - 3);
|
|
2216
|
+
for (let py = y0; py <= ring.y + rad + 3; py += 1) {
|
|
2217
|
+
for (let px = x0; px <= ring.x + rad + 3; px += 1) {
|
|
2218
|
+
const d = Math.abs(Math.hypot(px - ring.x, py - ring.y) - rad);
|
|
2219
|
+
if (d < 2.5) blend(img, px, py, 37, 99, 235, alpha * Math.min(1, 2.5 - d));
|
|
2220
|
+
}
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
const left = Math.round(x) - sprite.ox;
|
|
2224
|
+
const top = Math.round(y) - sprite.oy;
|
|
2225
|
+
for (let sy = 0; sy < sprite.height; sy += 1) {
|
|
2226
|
+
for (let sx = 0; sx < sprite.width; sx += 1) {
|
|
2227
|
+
const k = (sy * sprite.width + sx) * 4;
|
|
2228
|
+
const a = sprite.data[k + 3] / 255;
|
|
2229
|
+
if (a) blend(img, left + sx, top + sy, sprite.data[k], sprite.data[k + 1], sprite.data[k + 2], a);
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
function cursorAt(moves, t) {
|
|
2234
|
+
if (!moves.length) return null;
|
|
2235
|
+
if (t <= moves[0].t) return moves[0];
|
|
2236
|
+
for (let i = 1; i < moves.length; i += 1) {
|
|
2237
|
+
if (moves[i].t >= t) {
|
|
2238
|
+
const a = moves[i - 1];
|
|
2239
|
+
const b = moves[i];
|
|
2240
|
+
const f = b.t === a.t ? 1 : (t - a.t) / (b.t - a.t);
|
|
2241
|
+
return { x: a.x + (b.x - a.x) * f, y: a.y + (b.y - a.y) * f };
|
|
2242
|
+
}
|
|
2243
|
+
}
|
|
2244
|
+
return moves[moves.length - 1];
|
|
2245
|
+
}
|
|
2246
|
+
function withCursor(shots, moves, clicks, o = {}) {
|
|
2247
|
+
if (!shots.length) return [];
|
|
2248
|
+
const sprite = o.sprite ?? arrowSprite((o.scale ?? 1) * CURSOR_SIZE);
|
|
2249
|
+
const times = new Set(shots.map((s2) => s2.t));
|
|
2250
|
+
const step = 1e3 / FPS;
|
|
2251
|
+
for (let i = 1; i < moves.length; i += 1) {
|
|
2252
|
+
if (moves[i].x === moves[i - 1].x && moves[i].y === moves[i - 1].y) continue;
|
|
2253
|
+
for (let t = moves[i - 1].t; t < moves[i].t; t += step) times.add(Math.round(t));
|
|
2254
|
+
times.add(moves[i].t);
|
|
2255
|
+
}
|
|
2256
|
+
for (const c of clicks) for (let t = c.t; t <= c.t + RING_MS + step; t += step) times.add(Math.round(t));
|
|
2257
|
+
const end = shots[shots.length - 1].t;
|
|
2258
|
+
const last = Math.max(end, moves.at(-1)?.t ?? end, (clicks.at(-1)?.t ?? 0) + RING_MS);
|
|
2259
|
+
const sorted = [...times].filter((t) => t >= shots[0].t && t <= last).sort((a, b) => a - b);
|
|
2260
|
+
const out = [];
|
|
2261
|
+
let k = 0;
|
|
2262
|
+
let decoded = null;
|
|
2263
|
+
const s = o.scale ?? 1;
|
|
2264
|
+
for (const t of sorted) {
|
|
2265
|
+
while (k + 1 < shots.length && shots[k + 1].t <= t) k += 1;
|
|
2266
|
+
if (!decoded || decoded.k !== k) {
|
|
2267
|
+
const p = PNG3.sync.read(shots[k].png);
|
|
2268
|
+
decoded = { k, width: p.width, height: p.height, data: p.data };
|
|
2269
|
+
}
|
|
2270
|
+
const img = { width: decoded.width, height: decoded.height, data: Buffer.from(decoded.data) };
|
|
2271
|
+
const at = cursorAt(moves, t);
|
|
2272
|
+
const c = [...clicks].reverse().find((x) => x.t <= t && t - x.t <= RING_MS);
|
|
2273
|
+
if (at) drawCursor(img, sprite, at.x * s, at.y * s, c ? { x: c.x * s, y: c.y * s, p: (t - c.t) / RING_MS } : null);
|
|
2274
|
+
const png = new PNG3({ width: img.width, height: img.height });
|
|
2275
|
+
img.data.copy(png.data);
|
|
2276
|
+
out.push({ t, png: PNG3.sync.write(png, { deflateLevel: 1 }) });
|
|
2277
|
+
}
|
|
2278
|
+
return out;
|
|
2279
|
+
}
|
|
2280
|
+
var sleep3 = (ms) => new Promise((r) => {
|
|
2281
|
+
setTimeout(r, ms);
|
|
2282
|
+
});
|
|
2283
|
+
function pathOf(a, b, ms) {
|
|
2284
|
+
const n = Math.max(2, Math.round(ms / 16));
|
|
2285
|
+
const dx = b.x - a.x;
|
|
2286
|
+
const dy = b.y - a.y;
|
|
2287
|
+
const bend = Math.min(60, Math.hypot(dx, dy) * 0.12);
|
|
2288
|
+
const nx = -dy / (Math.hypot(dx, dy) || 1);
|
|
2289
|
+
const ny = dx / (Math.hypot(dx, dy) || 1);
|
|
2290
|
+
const pts = [];
|
|
2291
|
+
for (let i = 1; i <= n; i += 1) {
|
|
2292
|
+
const f = i / n;
|
|
2293
|
+
const e = f < 0.5 ? 4 * f * f * f : 1 - (-2 * f + 2) ** 3 / 2;
|
|
2294
|
+
const arc = Math.sin(Math.PI * e) * bend;
|
|
2295
|
+
pts.push({ x: a.x + dx * e + nx * arc, y: a.y + dy * e + ny * arc });
|
|
2296
|
+
}
|
|
2297
|
+
return pts;
|
|
2298
|
+
}
|
|
2299
|
+
var travelMs = (d) => Math.round(Math.min(1100, 380 + d * 0.9));
|
|
2300
|
+
function sayFrom(script, count, id = "video") {
|
|
2301
|
+
const out = /* @__PURE__ */ new Map();
|
|
2302
|
+
for (const l of script?.lines ?? []) {
|
|
2303
|
+
if (!(Number.isInteger(l.n) && l.n >= 1 && l.n <= count)) throw new Error(`${id}: the narration script has a line ${l.n}, but the video has ${count} scene(s)`);
|
|
2304
|
+
if (typeof l.text === "string" && l.text.trim()) out.set(l.n, l.text.trim());
|
|
2305
|
+
}
|
|
2306
|
+
return out;
|
|
2307
|
+
}
|
|
2308
|
+
function lookOptions(video, brand, appName = "") {
|
|
2309
|
+
const p = video.polish ?? [];
|
|
2310
|
+
const at = (flag) => p.indexOf(flag);
|
|
2311
|
+
const b = typeof brand === "string" ? brandFile(brand) : brand ?? {};
|
|
2312
|
+
const { titleCard: bTitle, endCard: bEnd, ...look } = b;
|
|
2313
|
+
const o = { ...look };
|
|
2314
|
+
if (at("--canvas") >= 0) o.canvas = p[at("--canvas") + 1];
|
|
2315
|
+
if (at("--watermark") >= 0) o.watermark = p[at("--watermark") + 1];
|
|
2316
|
+
if (at("--frame") >= 0) o.frame = p[at("--frame") + 1];
|
|
2317
|
+
if (at("--captions") >= 0) o.captions = p[at("--captions") + 1];
|
|
2318
|
+
if (at("--captions-at") >= 0) o.captionsAt = p[at("--captions-at") + 1];
|
|
2319
|
+
if (at("--background") >= 0) o.background = p[at("--background") + 1];
|
|
2320
|
+
if (p.includes("--camera")) o.camera = true;
|
|
2321
|
+
if (p.includes("--poster")) o.poster = true;
|
|
2322
|
+
const sub = { sub: appName };
|
|
2323
|
+
const wantTitle = p.includes("--title-card") || bTitle !== void 0;
|
|
2324
|
+
const wantEnd = p.includes("--end-card") || bEnd !== void 0;
|
|
2325
|
+
const cardFrom = (brandCard, own, fallback) => {
|
|
2326
|
+
const base2 = brandCard && typeof brandCard === "object" ? brandCard : typeof brandCard === "string" ? { title: brandCard } : true;
|
|
2327
|
+
const v = own !== void 0 ? base2 === true ? own : { ...base2, title: own } : base2;
|
|
2328
|
+
return cardOf(v, { title: own ?? fallback, ...sub });
|
|
2329
|
+
};
|
|
2330
|
+
const title = wantTitle ? cardFrom(bTitle, video.title, "How to") : void 0;
|
|
2331
|
+
const end = wantEnd ? cardFrom(bEnd, video.endTitle, "Done") : void 0;
|
|
2332
|
+
if (title || end) o.cards = { ...title ? { title } : {}, ...end ? { end } : {} };
|
|
2333
|
+
return o;
|
|
2334
|
+
}
|
|
2335
|
+
function cardsOf(cards, finishCards = {}, appName = "") {
|
|
2336
|
+
const { title: _t, url, ...look } = finishCards?.end ?? finishCards?.title ?? { sub: appName };
|
|
2337
|
+
return cards.map((c) => ({ t: c.t, ms: c.ms, step: c.step, card: { ...look, title: c.title, ...c.url && url ? { url } : {} } }));
|
|
2338
|
+
}
|
|
2339
|
+
var TYPES2 = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".json": "application/json", ".svg": "image/svg+xml", ".png": "image/png" };
|
|
2340
|
+
function aliasOf(url, alias) {
|
|
2341
|
+
const a = typeof alias === "string" ? { host: alias } : alias;
|
|
2342
|
+
if (!a?.host) return { url, local: null, rewrite: [] };
|
|
2343
|
+
return { url: `https://${a.host}`, local: url.replace(/\/$/, ""), ...a.api ? { api: `https://api.${a.host}`, localApi: a.api.replace(/\/$/, "") } : {}, rewrite: a.rewrite ?? [] };
|
|
2344
|
+
}
|
|
2345
|
+
async function routeAlias(context, alias) {
|
|
2346
|
+
if (!alias.local) return;
|
|
2347
|
+
const forward = (to) => async (route) => {
|
|
2348
|
+
const u = new URL(route.request().url());
|
|
2349
|
+
const response = await route.fetch({ url: `${to}${u.pathname}${u.search}` });
|
|
2350
|
+
if (to === alias.local && alias.localApi && alias.rewrite.includes(u.pathname)) {
|
|
2351
|
+
const body = (await response.text()).split(alias.localApi).join(alias.api);
|
|
2352
|
+
await route.fulfill({ response, body });
|
|
2353
|
+
return;
|
|
2354
|
+
}
|
|
2355
|
+
await route.fulfill({ response });
|
|
2356
|
+
};
|
|
2357
|
+
await context.route(`${alias.url}/**`, forward(alias.local));
|
|
2358
|
+
if (alias.localApi) await context.route(`${alias.api}/**`, forward(alias.localApi));
|
|
2359
|
+
}
|
|
2360
|
+
async function routeFolders(context, base2, routes) {
|
|
2361
|
+
for (const [prefix, dir] of Object.entries(routes)) {
|
|
2362
|
+
const root = resolve4(dir);
|
|
2363
|
+
const at = `/${prefix.replace(/^\/+|\/+$/g, "")}/`;
|
|
2364
|
+
await context.route(`${base2}${at}**`, async (route) => {
|
|
2365
|
+
const rel = decodeURIComponent(new URL(route.request().url()).pathname.slice(at.length));
|
|
2366
|
+
const file = resolve4(root, rel);
|
|
2367
|
+
if (!file.startsWith(`${root}/`) || !existsSync4(file)) return route.fulfill({ status: 404, body: "not found" });
|
|
2368
|
+
return route.fulfill({ status: 200, contentType: TYPES2[extname3(file)] ?? "application/octet-stream", body: readFileSync7(file), headers: { "cache-control": "no-store" } });
|
|
2369
|
+
});
|
|
2370
|
+
}
|
|
2371
|
+
}
|
|
2372
|
+
var pathOfUrl = (u) => {
|
|
2373
|
+
try {
|
|
2374
|
+
return new URL(u).pathname;
|
|
2375
|
+
} catch {
|
|
2376
|
+
return "/";
|
|
2377
|
+
}
|
|
2378
|
+
};
|
|
2379
|
+
async function expectFn() {
|
|
2380
|
+
try {
|
|
2381
|
+
const m = await import("@playwright/test");
|
|
2382
|
+
return m.expect;
|
|
2383
|
+
} catch {
|
|
2384
|
+
return () => {
|
|
2385
|
+
throw new Error("d.expect needs @playwright/test: npm install -D @playwright/test");
|
|
2386
|
+
};
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
async function direct(o) {
|
|
2390
|
+
const id = o.id ?? "video";
|
|
2391
|
+
const log = o.log ?? (() => {
|
|
2392
|
+
});
|
|
2393
|
+
const VP = o.viewport ?? DIRECTOR_VIEWPORT;
|
|
2394
|
+
const DSF = o.scale ?? (VP.width === DIRECTOR_VIEWPORT.width && VP.height === DIRECTOR_VIEWPORT.height ? 1 : 2);
|
|
2395
|
+
const alias = aliasOf(o.url, o.alias);
|
|
2396
|
+
const url = alias.url;
|
|
2397
|
+
const script = typeof o.script === "function" ? o.script : (await import(pathToFileURL3(resolve4(o.script)).href)).default;
|
|
2398
|
+
if (typeof script !== "function") throw new Error(`${id}: ${o.script} has no default export (the script)`);
|
|
2399
|
+
const expect = await expectFn();
|
|
2400
|
+
const stateOf = async (opts) => {
|
|
2401
|
+
if (!o.session) return void 0;
|
|
2402
|
+
if (typeof o.session === "string") return o.session;
|
|
2403
|
+
return o.session(url, opts);
|
|
2404
|
+
};
|
|
2405
|
+
const browser = await chromium3.launch({ headless: o.headless ?? true, ...o.launchArgs ? { args: o.launchArgs } : {} });
|
|
2406
|
+
const shots = [];
|
|
2407
|
+
const moves = [];
|
|
2408
|
+
const clicks = [];
|
|
2409
|
+
const events = [];
|
|
2410
|
+
const titles = {};
|
|
2411
|
+
const scenes = [];
|
|
2412
|
+
const cards = [];
|
|
2413
|
+
let playAt = 0;
|
|
2414
|
+
let skipped = false;
|
|
2415
|
+
try {
|
|
2416
|
+
const windowOf = async (opts) => {
|
|
2417
|
+
const signedIn = opts.signedIn !== false;
|
|
2418
|
+
const state = signedIn ? await stateOf(opts) : void 0;
|
|
2419
|
+
const context2 = await browser.newContext({
|
|
2420
|
+
viewport: VP,
|
|
2421
|
+
deviceScaleFactor: DSF,
|
|
2422
|
+
permissions: ["clipboard-read", "clipboard-write"],
|
|
2423
|
+
...state ? { storageState: state } : {}
|
|
2424
|
+
});
|
|
2425
|
+
await routeAlias(context2, alias);
|
|
2426
|
+
if (o.routes) await routeFolders(context2, url, o.routes);
|
|
2427
|
+
const page2 = await context2.newPage();
|
|
2428
|
+
const cdp2 = await context2.newCDPSession(page2);
|
|
2429
|
+
cdp2.on("Page.screencastFrame", (f2) => {
|
|
2430
|
+
shots.push({ t: f2.metadata?.timestamp ? Math.round(f2.metadata.timestamp * 1e3) : Date.now(), png: Buffer.from(f2.data, "base64") });
|
|
2431
|
+
cdp2.send("Page.screencastFrameAck", { sessionId: f2.sessionId }).catch(() => {
|
|
2432
|
+
});
|
|
2433
|
+
});
|
|
2434
|
+
page2.on("framenavigated", (fr) => {
|
|
2435
|
+
if (fr !== page2.mainFrame()) return;
|
|
2436
|
+
const p = pathOfUrl(page2.url());
|
|
2437
|
+
events.push({ t: Date.now(), type: "page", url: p });
|
|
2438
|
+
page2.title().then((t) => {
|
|
2439
|
+
titles[p] = t;
|
|
2440
|
+
}).catch(() => {
|
|
2441
|
+
});
|
|
2442
|
+
});
|
|
2443
|
+
return { context: context2, page: page2, cdp: cdp2 };
|
|
2444
|
+
};
|
|
2445
|
+
let { context, page, cdp } = await windowOf({ ...o.sessionOptions ?? {}, ...o.signedOut ? { signedIn: false } : {} });
|
|
2446
|
+
const path = () => pathOfUrl(page.url());
|
|
2447
|
+
const screencast = () => cdp.send("Page.startScreencast", { format: "png", everyNthFrame: 1, maxWidth: VP.width * DSF, maxHeight: VP.height * DSF });
|
|
2448
|
+
let at = { x: VP.width * 0.62, y: VP.height * 0.55 };
|
|
2449
|
+
const moveTo = async (to, ms) => {
|
|
2450
|
+
for (const p of pathOf(at, to, ms ?? travelMs(Math.hypot(to.x - at.x, to.y - at.y)))) {
|
|
2451
|
+
await page.mouse.move(p.x, p.y);
|
|
2452
|
+
moves.push({ t: Date.now(), x: p.x, y: p.y });
|
|
2453
|
+
await sleep3(16);
|
|
2454
|
+
}
|
|
2455
|
+
at = to;
|
|
2456
|
+
};
|
|
2457
|
+
const pointOf = async (locator, opts = {}) => {
|
|
2458
|
+
await locator.waitFor({ state: "visible", timeout: opts.timeout ?? 15e3 });
|
|
2459
|
+
await locator.scrollIntoViewIfNeeded();
|
|
2460
|
+
const box = await locator.boundingBox();
|
|
2461
|
+
if (!box) throw new Error(`director: ${locator} has no box`);
|
|
2462
|
+
const fx = box.width > 160 ? 0.35 : 0.5;
|
|
2463
|
+
return { x: box.x + box.width * fx, y: box.y + box.height * 0.5 };
|
|
2464
|
+
};
|
|
2465
|
+
const d = {
|
|
2466
|
+
...o.props ?? {},
|
|
2467
|
+
page,
|
|
2468
|
+
expect,
|
|
2469
|
+
url,
|
|
2470
|
+
local: alias.local ?? url,
|
|
2471
|
+
async window(p, opts = {}) {
|
|
2472
|
+
await cdp.send("Page.stopScreencast").catch(() => {
|
|
2473
|
+
});
|
|
2474
|
+
await context.close();
|
|
2475
|
+
({ context, page, cdp } = await windowOf({ ...o.sessionOptions ?? {}, ...opts }));
|
|
2476
|
+
d.page = page;
|
|
2477
|
+
await screencast();
|
|
2478
|
+
await page.goto(`${url}${p}`, { waitUntil: "networkidle" });
|
|
2479
|
+
await sleep3(400);
|
|
2480
|
+
},
|
|
2481
|
+
async open(p) {
|
|
2482
|
+
await page.goto(`${url}${p}`, { waitUntil: "networkidle" });
|
|
2483
|
+
await sleep3(300);
|
|
2484
|
+
},
|
|
2485
|
+
read: (ms = 900) => sleep3(ms),
|
|
2486
|
+
async hover(locator, opts = {}) {
|
|
2487
|
+
const p = await pointOf(locator, opts);
|
|
2488
|
+
await moveTo(p, opts.ms);
|
|
2489
|
+
await sleep3(opts.rest ?? 250);
|
|
2490
|
+
},
|
|
2491
|
+
async click(locator, opts = {}) {
|
|
2492
|
+
const p = await pointOf(locator, opts);
|
|
2493
|
+
await moveTo(p, opts.ms);
|
|
2494
|
+
await sleep3(opts.aim ?? 180);
|
|
2495
|
+
await page.mouse.down();
|
|
2496
|
+
clicks.push({ t: Date.now(), x: p.x, y: p.y });
|
|
2497
|
+
await sleep3(70);
|
|
2498
|
+
await page.mouse.up();
|
|
2499
|
+
await sleep3(opts.rest ?? 350);
|
|
2500
|
+
},
|
|
2501
|
+
async type(locator, text, opts = {}) {
|
|
2502
|
+
await d.click(locator, { rest: 150, ...opts });
|
|
2503
|
+
if (opts.clear) {
|
|
2504
|
+
await page.keyboard.press("ControlOrMeta+A");
|
|
2505
|
+
await page.keyboard.press("Backspace");
|
|
2506
|
+
}
|
|
2507
|
+
await page.keyboard.type(text, { delay: opts.delay ?? 55 });
|
|
2508
|
+
await sleep3(opts.rest ?? 350);
|
|
2509
|
+
},
|
|
2510
|
+
async press(key, opts = {}) {
|
|
2511
|
+
await page.keyboard.press(key);
|
|
2512
|
+
await sleep3(opts.rest ?? 350);
|
|
2513
|
+
},
|
|
2514
|
+
async skip(fn) {
|
|
2515
|
+
events.push({ t: Date.now(), type: "state", phase: "waiting" });
|
|
2516
|
+
await fn();
|
|
2517
|
+
events.push({ t: Date.now(), type: "state", phase: "playing" });
|
|
2518
|
+
skipped = true;
|
|
2519
|
+
},
|
|
2520
|
+
async card(title, opts = {}) {
|
|
2521
|
+
await sleep3(5);
|
|
2522
|
+
const t = Date.now();
|
|
2523
|
+
const index = scenes.length;
|
|
2524
|
+
const ms = opts.ms ?? 2200;
|
|
2525
|
+
scenes.push({ index, caption: title, say: opts.say ?? title, from: t, to: t + ms, card: true });
|
|
2526
|
+
cards.push({ t, ms, title, url: !!opts.url, step: index });
|
|
2527
|
+
events.push({ t: t + 1, type: "step", index, stepType: "card", say: title, url: path() });
|
|
2528
|
+
await sleep3(5);
|
|
2529
|
+
},
|
|
2530
|
+
async scene(caption, fn, opts = {}) {
|
|
2531
|
+
const from = Date.now();
|
|
2532
|
+
await fn();
|
|
2533
|
+
await sleep3(opts.hold ?? 500);
|
|
2534
|
+
const index = scenes.length;
|
|
2535
|
+
scenes.push({ index, caption, say: opts.say ?? caption, from, to: Date.now() });
|
|
2536
|
+
events.push({ t: Date.now(), type: "step", index, stepType: "scene", say: caption, url: path() });
|
|
2537
|
+
}
|
|
2538
|
+
};
|
|
2539
|
+
await screencast();
|
|
2540
|
+
await page.goto(`${url}${o.page ?? "/"}`, { waitUntil: "networkidle" });
|
|
2541
|
+
await sleep3(600);
|
|
2542
|
+
moves.push({ t: Date.now(), ...at });
|
|
2543
|
+
playAt = Date.now();
|
|
2544
|
+
events.push({ t: playAt, type: "play", url: path() });
|
|
2545
|
+
log(`${id}: playing \u2026`);
|
|
2546
|
+
await script(d);
|
|
2547
|
+
await sleep3(o.tailMs ?? 800);
|
|
2548
|
+
moves.push({ t: Date.now(), ...at });
|
|
2549
|
+
await cdp.send("Page.stopScreencast").catch(() => {
|
|
2550
|
+
});
|
|
2551
|
+
await sleep3(100);
|
|
2552
|
+
if (shots.length) shots.push({ t: Date.now(), png: shots[shots.length - 1].png });
|
|
2553
|
+
await context.close();
|
|
2554
|
+
} finally {
|
|
2555
|
+
await browser.close();
|
|
2556
|
+
}
|
|
2557
|
+
if (!shots.length) throw new Error("director: the screencast sent no frames");
|
|
2558
|
+
const stopAt = Date.now();
|
|
2559
|
+
events.push({ t: stopAt, type: "end" });
|
|
2560
|
+
shots.sort((a, b) => a.t - b.t);
|
|
2561
|
+
log(`${id}: ${scenes.length} scenes, ${shots.length} screencast frames; drawing the cursor \u2026`);
|
|
2562
|
+
const frames = withCursor(shots.filter((s) => s.t >= playAt - 400), moves, clicks, { scale: DSF });
|
|
2563
|
+
const cap = { frames, viewport: { w: VP.width, h: VP.height, scale: DSF }, titles, events, playAt, stopAt, endAt: stopAt };
|
|
2564
|
+
const f = { ...o.look ?? {}, cuts: { maxWaitMs: skipped ? SKIP_KEEP_MS : 0 } };
|
|
2565
|
+
if (cards.length) cap.cards = cardsOf(cards, f.cards, o.appName);
|
|
2566
|
+
let narrated = false;
|
|
2567
|
+
if (o.voice) {
|
|
2568
|
+
const lines = /* @__PURE__ */ new Map();
|
|
2569
|
+
if (o.voiceScript) {
|
|
2570
|
+
const s = sayFrom(typeof o.voiceScript === "string" ? JSON.parse(readFileSync7(o.voiceScript, "utf8")) : o.voiceScript, scenes.length, id);
|
|
2571
|
+
for (const sc of scenes) sc.say = s.get(sc.index + 1) ?? sc.say;
|
|
2572
|
+
}
|
|
2573
|
+
for (const sc of scenes) {
|
|
2574
|
+
if (sc.card || !sc.say || sc.say === sc.caption) continue;
|
|
2575
|
+
sc.caption = sc.say;
|
|
2576
|
+
for (const e of events) if (e.type === "step" && e.index === sc.index) e.say = sc.say;
|
|
2577
|
+
}
|
|
2578
|
+
for (const sc of scenes) {
|
|
2579
|
+
if (!sc.say) continue;
|
|
2580
|
+
const audio = trimSilence(await o.voice(sc.say));
|
|
2581
|
+
if (audio.pcm.length) lines.set(sc.index, { text: sc.say, audio });
|
|
2582
|
+
}
|
|
2583
|
+
f.narration = { lines };
|
|
2584
|
+
narrated = lines.size > 0;
|
|
2585
|
+
}
|
|
2586
|
+
for (const x of o.out) mkdirSync3(dirname4(x.file), { recursive: true });
|
|
2587
|
+
if (f.poster === true) f.poster = o.out[0].file.replace(/\.[^./]+$/, ".png");
|
|
2588
|
+
const r = await finish(cap, o.out, f);
|
|
2589
|
+
if (o.scenesFile) writeFileSync3(o.scenesFile, `${JSON.stringify(scenes.map(({ index, caption, say, from, to }) => ({ n: index + 1, caption, say, ms: to - from })), null, 2)}
|
|
2590
|
+
`);
|
|
2591
|
+
return { ...r, narrated, scenes };
|
|
2592
|
+
}
|
|
2593
|
+
|
|
2594
|
+
// src/campaign.ts
|
|
2595
|
+
var CAMPAIGN_KINDS = CATALOGUE_KINDS;
|
|
2596
|
+
var CAMPAIGN_SHAPES = CATALOGUE_SHAPES;
|
|
2597
|
+
var DESKTOP = ["16:9", "1:1", "4:5"];
|
|
2598
|
+
var PHONE = "390x844";
|
|
2599
|
+
var TALL = { width: 414, height: 736 };
|
|
2600
|
+
var WIDE = { width: 1280, height: 720 };
|
|
2601
|
+
var abs = (base2, p) => p === void 0 ? void 0 : resolve5(base2, p);
|
|
2602
|
+
function loadCampaign(file = "campaign.json") {
|
|
2603
|
+
const f = resolve5(file);
|
|
2604
|
+
if (!existsSync5(f)) throw new Error(`no campaign file ${file}`);
|
|
2605
|
+
let c;
|
|
2606
|
+
try {
|
|
2607
|
+
c = JSON.parse(readFileSync8(f, "utf8"));
|
|
2608
|
+
} catch (e) {
|
|
2609
|
+
throw new Error(`${file}: ${e.message}`);
|
|
2610
|
+
}
|
|
2611
|
+
const base2 = dirname5(f);
|
|
2612
|
+
const app = { ...c.app ?? {} };
|
|
2613
|
+
app.dir = process.env.TRACE_AVATAR_APP_DIR ? resolve5(process.env.TRACE_AVATAR_APP_DIR) : resolve5(base2, app.dir ?? ".");
|
|
2614
|
+
for (const k of ["guides", "storageState", "session"]) app[k] = abs(app.dir, app[k]);
|
|
2615
|
+
if (app.routes) app.routes = Object.fromEntries(Object.entries(app.routes).map(([k, v]) => [k, resolve5(base2, v)]));
|
|
2616
|
+
const looks = Object.fromEntries(Object.entries(c.looks ?? {}).map(([k, l]) => [k, { ...l, brand: resolve5(base2, l?.brand ?? "") }]));
|
|
2617
|
+
const videos = (c.videos ?? []).map((v) => ({
|
|
2618
|
+
...v,
|
|
2619
|
+
...v.director ? { director: resolve5(base2, v.director) } : {},
|
|
2620
|
+
...v.guide && /\.json$/i.test(v.guide) ? { guide: resolve5(base2, v.guide) } : {},
|
|
2621
|
+
...v.voice ? { voice: Object.fromEntries(Object.entries(v.voice).map(([k, s]) => [k, resolve5(base2, s)])) } : {}
|
|
2622
|
+
}));
|
|
2623
|
+
return { ...c, app, looks, videos, file: f, base: base2, out: resolve5(base2, c.out ?? join4("out", c.product ?? "product")) };
|
|
2624
|
+
}
|
|
2625
|
+
var guidesOf = (c) => c.app.guides && existsSync5(c.app.guides) ? JSON.parse(readFileSync8(c.app.guides, "utf8")) : null;
|
|
2626
|
+
function guideOf(c, v, set = guidesOf(c)) {
|
|
2627
|
+
if (/\.json$/i.test(v.guide)) {
|
|
2628
|
+
if (!existsSync5(v.guide)) return null;
|
|
2629
|
+
const trace = JSON.parse(readFileSync8(v.guide, "utf8"));
|
|
2630
|
+
return { trace, page: v.page ?? "/" };
|
|
2631
|
+
}
|
|
2632
|
+
const g = set?.guides.find((x) => x.id === v.guide);
|
|
2633
|
+
if (!g) return null;
|
|
2634
|
+
return { trace: g.trace, page: v.page ?? (g.pages ?? []).find((p) => !p.includes("*")) ?? "/" };
|
|
2635
|
+
}
|
|
2636
|
+
function checkCampaign(c, o = {}) {
|
|
2637
|
+
const files = o.files ?? true;
|
|
2638
|
+
const problems = [];
|
|
2639
|
+
if (!/^[a-z0-9-]+$/.test(c.product ?? "")) problems.push("product is a word of a-z, 0-9 and -, e.g. acme");
|
|
2640
|
+
if (!/^https:\/\/[^\s/]+/.test(c.site ?? "")) problems.push("site is the product's https address, e.g. https://acme.com");
|
|
2641
|
+
if (c.language !== void 0 && !/^[a-z]{2}$/.test(c.language)) problems.push("language is two letters, e.g. en");
|
|
2642
|
+
if (!/^https?:\/\//.test(c.app?.url ?? "")) problems.push("app.url is where the app serves, e.g. http://127.0.0.1:3000");
|
|
2643
|
+
if (c.app.reset && !("post" in c.app.reset && /^https?:\/\//.test(c.app.reset.post)) && !("run" in c.app.reset && Array.isArray(c.app.reset.run) && c.app.reset.run.length)) problems.push('app.reset is { "post": url } or { "run": [command\u2026] }');
|
|
2644
|
+
if (c.app.start && !(Array.isArray(c.app.start) && c.app.start.length)) problems.push('app.start is a command, e.g. ["npm", "run", "demo"]');
|
|
2645
|
+
if (files) {
|
|
2646
|
+
if (c.app.guides && !existsSync5(c.app.guides)) problems.push(`no guides file ${c.app.guides}`);
|
|
2647
|
+
if (c.app.storageState && !existsSync5(c.app.storageState)) problems.push(`no storageState ${c.app.storageState}`);
|
|
2648
|
+
if (c.app.session && !existsSync5(c.app.session)) problems.push(`no session module ${c.app.session}`);
|
|
2649
|
+
}
|
|
2650
|
+
for (const [d, dir] of Object.entries(c.app.routes ?? {})) if (!existsSync5(dir)) problems.push(`app.routes ${d}: no folder ${dir}`);
|
|
2651
|
+
const lookNames = Object.keys(c.looks ?? {});
|
|
2652
|
+
if (!lookNames.length) problems.push('no looks: { "product": { "brand": "brands/acme.json" } }');
|
|
2653
|
+
for (const [k, l] of Object.entries(c.looks ?? {})) {
|
|
2654
|
+
if (!/^[a-z0-9-]+$/.test(k)) problems.push(`look ${k}: a name of a-z, 0-9 and -`);
|
|
2655
|
+
if (!existsSync5(l.brand)) problems.push(`look ${k}: no brand file ${l.brand}`);
|
|
2656
|
+
}
|
|
2657
|
+
const set = files ? guidesOf(c) : null;
|
|
2658
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2659
|
+
for (const v of c.videos ?? []) {
|
|
2660
|
+
const at = `${v.id}:`;
|
|
2661
|
+
if (!/^[a-z0-9-]+$/.test(v.id ?? "")) problems.push(`bad video id ${v.id} (a-z, 0-9 and -)`);
|
|
2662
|
+
if (ids.has(v.id)) problems.push(`${at} id used twice`);
|
|
2663
|
+
ids.add(v.id);
|
|
2664
|
+
if (!CAMPAIGN_KINDS.includes(v.kind)) problems.push(`${at} kind is one of ${CAMPAIGN_KINDS.join(", ")}`);
|
|
2665
|
+
if (!v.guide === !v.director) problems.push(`${at} a guide or a director script (one of them)`);
|
|
2666
|
+
if (v.director && !existsSync5(v.director)) problems.push(`${at} no director script ${v.director}`);
|
|
2667
|
+
if (v.guide && files) {
|
|
2668
|
+
const g = guideOf(c, v, set);
|
|
2669
|
+
if (!g) problems.push(`${at} no guide ${v.guide}${/\.json$/i.test(v.guide) ? "" : c.app.guides ? ` in ${c.app.guides}` : " (no app.guides)"}`);
|
|
2670
|
+
else for (const [look, s] of Object.entries(v.voice ?? {})) {
|
|
2671
|
+
if (!existsSync5(s)) continue;
|
|
2672
|
+
const steps = g.trace.steps?.length ?? 0;
|
|
2673
|
+
const over = (JSON.parse(readFileSync8(s, "utf8")).lines ?? []).filter((l) => !(l.n >= 1 && l.n <= steps));
|
|
2674
|
+
if (over.length) problems.push(`${at} the ${look} script has line ${over.map((l) => l.n).join(", ")}, but the guide has ${steps} step(s)`);
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
if (v.mode !== void 0 && v.mode !== "overlay" && v.mode !== "perform") problems.push(`${at} mode is overlay or perform`);
|
|
2678
|
+
if (v.director && v.mode) problems.push(`${at} a director video has no mode`);
|
|
2679
|
+
if (!v.alt?.trim()) problems.push(`${at} no alt text (what it shows)`);
|
|
2680
|
+
const looks = v.looks ?? lookNames.slice(0, 1);
|
|
2681
|
+
if (!looks.length || looks.some((l) => !lookNames.includes(l))) problems.push(`${at} looks are some of ${lookNames.join(", ") || "(none defined)"}`);
|
|
2682
|
+
if (v.kind === "docs-gif") {
|
|
2683
|
+
if (v.shapes) problems.push(`${at} a docs-gif has no shapes`);
|
|
2684
|
+
if (v.voice) problems.push(`${at} a GIF has no sound`);
|
|
2685
|
+
} else if (!v.shapes?.length || v.shapes.some((s) => !CAMPAIGN_SHAPES.includes(s))) problems.push(`${at} shapes are some of ${CAMPAIGN_SHAPES.join(", ")}`);
|
|
2686
|
+
for (const l of looks) {
|
|
2687
|
+
const s = v.voice?.[l];
|
|
2688
|
+
if (v.voice && !s) problems.push(`${at} a narrated video needs a script for each look (voice.${l})`);
|
|
2689
|
+
if (s && !existsSync5(s)) problems.push(`${at} no script ${s}`);
|
|
2690
|
+
}
|
|
2691
|
+
if (v.polish && !Array.isArray(v.polish)) problems.push(`${at} polish is a list of renderer flags`);
|
|
2692
|
+
}
|
|
2693
|
+
if (!ids.size) problems.push("no videos");
|
|
2694
|
+
return problems;
|
|
2695
|
+
}
|
|
2696
|
+
function readCampaign(file, o = {}) {
|
|
2697
|
+
const c = loadCampaign(file);
|
|
2698
|
+
const problems = checkCampaign(c, o);
|
|
2699
|
+
if (problems.length) throw new Error(`campaign ${c.product ?? c.file}: ${problems.join("; ")}`);
|
|
2700
|
+
return c;
|
|
2701
|
+
}
|
|
2702
|
+
function rendersOf(c) {
|
|
2703
|
+
const out = [];
|
|
2704
|
+
const first = Object.keys(c.looks)[0];
|
|
2705
|
+
for (const v of c.videos) {
|
|
2706
|
+
for (const look of v.looks ?? [first]) {
|
|
2707
|
+
if (v.kind === "docs-gif") {
|
|
2708
|
+
out.push({ id: `${v.id}--${look}`, video: v, look, phone: false, shapes: [], format: "gif", polish: [...v.polish ?? []] });
|
|
2709
|
+
continue;
|
|
2710
|
+
}
|
|
2711
|
+
const desk = (v.shapes ?? []).filter((s) => DESKTOP.includes(s));
|
|
2712
|
+
if (desk.length) out.push({ id: `${v.id}--${look}`, video: v, look, phone: false, shapes: desk, format: "mp4", polish: ["--scale", "2", "--captions", "vtt", "--poster", ...v.polish ?? []] });
|
|
2713
|
+
if (v.shapes?.includes("9:16")) out.push({ id: `${v.id}--${look}--phone`, video: v, look, phone: true, shapes: ["9:16"], format: "mp4", polish: ["--scale", "2", "--frame", "none", "--captions", "vtt", "--poster", ...v.polish ?? []] });
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
return out;
|
|
2717
|
+
}
|
|
2718
|
+
var shapeTag = (shape) => shape.replace(":", "x");
|
|
2719
|
+
function filesOf(r) {
|
|
2720
|
+
if (!r.shapes.length) return [{ key: r.format, name: `${r.id}.${r.format}`, format: r.format }];
|
|
2721
|
+
return r.shapes.map((s) => ({ key: `${r.format}@${s}`, name: `${r.id}-${shapeTag(s)}.${r.format}`, format: r.format, canvas: s }));
|
|
2722
|
+
}
|
|
2723
|
+
function endsWithOf(look, endTitle) {
|
|
2724
|
+
if (look.endsWith) return look.endsWith;
|
|
2725
|
+
const b = existsSync5(look.brand) ? JSON.parse(readFileSync8(look.brand, "utf8")) : {};
|
|
2726
|
+
const end = b.endCard;
|
|
2727
|
+
if (end && typeof end === "object") return [endTitle ?? end.title, end.url].filter(Boolean).join(" ") || null;
|
|
2728
|
+
if (typeof end === "string") return endTitle ?? end;
|
|
2729
|
+
const w = typeof b.watermark === "string" ? b.watermark : b.watermark?.text;
|
|
2730
|
+
return w ?? null;
|
|
2731
|
+
}
|
|
2732
|
+
function progressOf(out, ids, o = {}) {
|
|
2733
|
+
const file = join4(out, "progress.json");
|
|
2734
|
+
const was = !o.fresh && existsSync5(file) ? JSON.parse(readFileSync8(file, "utf8")) : null;
|
|
2735
|
+
const same2 = !!was && !was.finished && JSON.stringify([...was.ids].sort()) === JSON.stringify([...ids].sort());
|
|
2736
|
+
const p = same2 ? { ...was, failed: {}, current: null, resumed: (/* @__PURE__ */ new Date()).toISOString() } : { ids, started: (/* @__PURE__ */ new Date()).toISOString(), done: {}, failed: {}, current: null, finished: null };
|
|
2737
|
+
mkdirSync4(out, { recursive: true });
|
|
2738
|
+
const save = () => writeFileSync4(file, `${JSON.stringify(p, null, 2)}
|
|
2739
|
+
`);
|
|
2740
|
+
save();
|
|
2741
|
+
return {
|
|
2742
|
+
file,
|
|
2743
|
+
resumed: same2,
|
|
2744
|
+
isDone: (id) => Boolean(p.done[id]),
|
|
2745
|
+
start(id, n, of) {
|
|
2746
|
+
p.current = { id, n, of, since: (/* @__PURE__ */ new Date()).toISOString() };
|
|
2747
|
+
save();
|
|
2748
|
+
},
|
|
2749
|
+
done(id) {
|
|
2750
|
+
p.done[id] = (/* @__PURE__ */ new Date()).toISOString();
|
|
2751
|
+
delete p.failed[id];
|
|
2752
|
+
p.current = null;
|
|
2753
|
+
save();
|
|
2754
|
+
},
|
|
2755
|
+
fail(id, why) {
|
|
2756
|
+
p.failed[id] = why;
|
|
2757
|
+
p.current = null;
|
|
2758
|
+
save();
|
|
2759
|
+
},
|
|
2760
|
+
finish() {
|
|
2761
|
+
p.finished = (/* @__PURE__ */ new Date()).toISOString();
|
|
2762
|
+
p.current = null;
|
|
2763
|
+
save();
|
|
2764
|
+
},
|
|
2765
|
+
get state() {
|
|
2766
|
+
return p;
|
|
2767
|
+
}
|
|
2768
|
+
};
|
|
2769
|
+
}
|
|
2770
|
+
function statusOf(c, now = Date.now()) {
|
|
2771
|
+
const file = join4(c.out, "progress.json");
|
|
2772
|
+
if (!existsSync5(file)) return `${c.product}: no make run yet`;
|
|
2773
|
+
const p = JSON.parse(readFileSync8(file, "utf8"));
|
|
2774
|
+
const done = p.ids.filter((id) => p.done[id]);
|
|
2775
|
+
const mins = (t) => `${Math.round((now - Date.parse(t)) / 6e4)} min`;
|
|
2776
|
+
return [
|
|
2777
|
+
`${c.product}: ${done.length} of ${p.ids.length} made${p.finished ? ` \u2014 finished ${mins(p.finished)} ago` : ""} (started ${mins(p.started)} ago${p.resumed ? `, carried on ${mins(p.resumed)} ago` : ""})`,
|
|
2778
|
+
...p.current ? [` now: [${p.current.n}/${p.current.of}] ${p.current.id}, for ${mins(p.current.since)}`] : [],
|
|
2779
|
+
...p.ids.map((id) => ` ${p.done[id] ? "\u2713" : p.failed[id] ? "\u2717" : p.current?.id === id ? "\u2026" : "\xB7"} ${id}${p.failed[id] ? ` \u2014 ${p.failed[id]}` : ""}`)
|
|
2780
|
+
].join("\n");
|
|
2781
|
+
}
|
|
2782
|
+
var reachable = async (url) => {
|
|
2783
|
+
try {
|
|
2784
|
+
return (await fetch(url)).ok;
|
|
2785
|
+
} catch {
|
|
2786
|
+
return false;
|
|
2787
|
+
}
|
|
2788
|
+
};
|
|
2789
|
+
var sleep4 = (ms) => new Promise((r) => {
|
|
2790
|
+
setTimeout(r, ms);
|
|
2791
|
+
});
|
|
2792
|
+
async function ensureApp(app, start, log = () => {
|
|
2793
|
+
}) {
|
|
2794
|
+
const ready = `${app.url.replace(/\/$/, "")}${app.ready ?? "/"}`;
|
|
2795
|
+
if (await reachable(ready)) return () => {
|
|
2796
|
+
};
|
|
2797
|
+
if (!start || !app.start?.length) throw new Error(`the app is not serving at ${ready}${app.start?.length ? `: run \`${app.start.join(" ")}\` in ${app.dir}, or pass --start` : ""}`);
|
|
2798
|
+
log(`starting the app: ${app.start.join(" ")} (in ${app.dir}) \u2026`);
|
|
2799
|
+
const child = spawn2(app.start[0], app.start.slice(1), { cwd: app.dir, stdio: ["ignore", "pipe", "inherit"] });
|
|
2800
|
+
let exited = null;
|
|
2801
|
+
let said = false;
|
|
2802
|
+
child.on("exit", (code) => {
|
|
2803
|
+
exited = code ?? -1;
|
|
2804
|
+
});
|
|
2805
|
+
child.stdout.on("data", (d) => {
|
|
2806
|
+
process.stdout.write(d);
|
|
2807
|
+
if (app.readyText && String(d).includes(app.readyText)) said = true;
|
|
2808
|
+
});
|
|
2809
|
+
const until = Date.now() + 6e5;
|
|
2810
|
+
for (; ; ) {
|
|
2811
|
+
if (exited !== null) throw new Error(`the app exited (${exited}) before it was ready`);
|
|
2812
|
+
if (app.readyText ? said : await reachable(ready)) break;
|
|
2813
|
+
if (Date.now() > until) {
|
|
2814
|
+
child.kill("SIGTERM");
|
|
2815
|
+
throw new Error("the app did not start in 10 minutes");
|
|
2816
|
+
}
|
|
2817
|
+
await sleep4(500);
|
|
2818
|
+
}
|
|
2819
|
+
return () => {
|
|
2820
|
+
child.kill("SIGTERM");
|
|
2821
|
+
};
|
|
2822
|
+
}
|
|
2823
|
+
async function resetFor(app, body) {
|
|
2824
|
+
if (!app.reset) return;
|
|
2825
|
+
if ("post" in app.reset) {
|
|
2826
|
+
const r = await fetch(app.reset.post, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ ...app.reset.body ?? {}, ...body }) }).catch((e) => e);
|
|
2827
|
+
if (r instanceof Error || !r.ok) throw new Error(`the app did not reset (${app.reset.post}: ${r instanceof Error ? r.message : r.status})`);
|
|
2828
|
+
return;
|
|
2829
|
+
}
|
|
2830
|
+
const run = app.reset.run;
|
|
2831
|
+
const code = await new Promise((done) => {
|
|
2832
|
+
const p = spawn2(run[0], run.slice(1), { cwd: app.dir, stdio: "inherit", env: { ...process.env, TRACE_AVATAR_RESET: JSON.stringify(body) } });
|
|
2833
|
+
p.on("exit", (c) => done(c ?? -1));
|
|
2834
|
+
p.on("error", () => done(-1));
|
|
2835
|
+
});
|
|
2836
|
+
if (code !== 0) throw new Error(`the app did not reset (${run.join(" ")} exited ${code})`);
|
|
2837
|
+
}
|
|
2838
|
+
async function sessionFn(app) {
|
|
2839
|
+
if (!app.session) return null;
|
|
2840
|
+
const m = await import(pathToFileURL4(app.session).href);
|
|
2841
|
+
if (typeof m.storageStateFor !== "function") throw new Error(`${app.session} has no storageStateFor(url, options)`);
|
|
2842
|
+
return m.storageStateFor;
|
|
2843
|
+
}
|
|
2844
|
+
async function voiceFor(spec, log) {
|
|
2845
|
+
if (!spec) return null;
|
|
2846
|
+
const v = typeof spec === "function" ? spec : await voiceOf(spec);
|
|
2847
|
+
try {
|
|
2848
|
+
await v("This is a short check that the voice can be reached.");
|
|
2849
|
+
} catch (e) {
|
|
2850
|
+
throw new Error(`the voice is not reachable (${e.message}); nothing was filmed`);
|
|
2851
|
+
}
|
|
2852
|
+
log(`voice: ${typeof spec === "string" ? spec : "given"}`);
|
|
2853
|
+
return v;
|
|
2854
|
+
}
|
|
2855
|
+
async function makeCampaign(c, o = {}) {
|
|
2856
|
+
const log = o.log ?? console.log;
|
|
2857
|
+
const only = o.only ?? [];
|
|
2858
|
+
const all = rendersOf(c);
|
|
2859
|
+
const hit = (r, id) => r.video.id === id || r.id === id;
|
|
2860
|
+
const unknown = only.filter((id) => !all.some((r) => hit(r, id)));
|
|
2861
|
+
if (unknown.length) throw new Error(`no such video: ${unknown.join(", ")}`);
|
|
2862
|
+
const renders = only.length ? all.filter((r) => only.some((id) => hit(r, id))) : all;
|
|
2863
|
+
mkdirSync4(join4(c.out, "work"), { recursive: true });
|
|
2864
|
+
if (renders.some((r) => r.format === "mp4")) {
|
|
2865
|
+
const ff = findFfmpeg();
|
|
2866
|
+
if (!ff) throw new Error(`${FFMPEG_MISSING} Nothing was captured.`);
|
|
2867
|
+
process.env.FFMPEG = ff;
|
|
2868
|
+
}
|
|
2869
|
+
const narrated = renders.some((r) => r.video.voice);
|
|
2870
|
+
const spec = o.voice ?? process.env.TRACE_AVATAR_VOICE ?? c.narrator;
|
|
2871
|
+
const voice = narrated ? await voiceFor(spec, log) : null;
|
|
2872
|
+
if (narrated && !voice) log("no voice (--voice, TRACE_AVATAR_VOICE or the campaign's narrator): narrated videos are made without narration");
|
|
2873
|
+
const manifest = join4(c.out, "made.json");
|
|
2874
|
+
const made = existsSync5(manifest) ? JSON.parse(readFileSync8(manifest, "utf8")) : {};
|
|
2875
|
+
const failed = [];
|
|
2876
|
+
const progress = progressOf(c.out, renders.map((r) => r.id), { fresh: o.fresh });
|
|
2877
|
+
if (progress.resumed) log(`carrying on a run that stopped: ${renders.filter((r) => progress.isDone(r.id)).length} of ${renders.length} already made (--fresh to start over) \u2014 progress in ${progress.file}`);
|
|
2878
|
+
else log(`${renders.length} render(s) \u2014 progress in ${progress.file}`);
|
|
2879
|
+
const fail = (id, why) => {
|
|
2880
|
+
failed.push(`${id}: ${why}`);
|
|
2881
|
+
progress.fail(id, why);
|
|
2882
|
+
log(`${id}: \u2717 ${why}`);
|
|
2883
|
+
};
|
|
2884
|
+
let stop = null;
|
|
2885
|
+
const session = await sessionFn(c.app);
|
|
2886
|
+
try {
|
|
2887
|
+
for (const [i, r] of renders.entries()) {
|
|
2888
|
+
const tag = `[${i + 1}/${renders.length}]`;
|
|
2889
|
+
if (progress.isDone(r.id)) {
|
|
2890
|
+
log(`${tag} ${r.id}: made earlier in this run \u2014 kept`);
|
|
2891
|
+
continue;
|
|
2892
|
+
}
|
|
2893
|
+
log(`${tag} ${r.id} \u2026`);
|
|
2894
|
+
progress.start(r.id, i + 1, renders.length);
|
|
2895
|
+
stop ??= await ensureApp(c.app, !!o.start, log);
|
|
2896
|
+
const v = r.video;
|
|
2897
|
+
const outs = filesOf(r);
|
|
2898
|
+
const outBase = join4(c.out, r.id);
|
|
2899
|
+
const firstBase = join4(c.out, outs[0].name.replace(/\.[^.]+$/, ""));
|
|
2900
|
+
for (const b of [outBase, firstBase]) for (const ext of ["vtt", "chapters.vtt", "png", "scenes.json"]) rmSync3(`${b}.${ext}`, { force: true });
|
|
2901
|
+
const look = c.looks[r.look];
|
|
2902
|
+
const script = v.voice?.[r.look];
|
|
2903
|
+
const fill = (existsSync5(look.brand) ? brandFile(look.brand).fill : void 0) ?? [];
|
|
2904
|
+
const wide = !r.phone && r.shapes.includes("16:9") && fill.some((f) => String(f) === "16:9");
|
|
2905
|
+
const tall = r.phone && fill.some((f) => String(f) === "9:16");
|
|
2906
|
+
const speak = !!(v.voice && voice);
|
|
2907
|
+
let traceFile = "";
|
|
2908
|
+
try {
|
|
2909
|
+
await resetFor(c.app, v.reset ?? {});
|
|
2910
|
+
let width;
|
|
2911
|
+
let height;
|
|
2912
|
+
let seconds;
|
|
2913
|
+
let spoke = false;
|
|
2914
|
+
if (v.director) {
|
|
2915
|
+
const res = await direct({
|
|
2916
|
+
id: r.id,
|
|
2917
|
+
script: v.director,
|
|
2918
|
+
url: c.app.url,
|
|
2919
|
+
page: v.page ?? "/",
|
|
2920
|
+
appName: c.app.name ?? "",
|
|
2921
|
+
out: outs.map((x) => ({ file: join4(c.out, x.name), format: x.format, ...x.canvas ? { canvas: x.canvas } : {}, ...x.format === "gif" ? { width: v.width ?? 800 } : {} })),
|
|
2922
|
+
look: lookOptions({ title: v.title, endTitle: v.endTitle, polish: r.polish }, look.brand, c.app.name ?? ""),
|
|
2923
|
+
...r.phone ? { viewport: tall ? TALL : { width: 390, height: 844 } } : wide ? { viewport: WIDE } : {},
|
|
2924
|
+
...session ? { session } : c.app.storageState ? { session: c.app.storageState } : {},
|
|
2925
|
+
...v.session ? { sessionOptions: v.session } : {},
|
|
2926
|
+
...v.signedIn === false ? { signedOut: true } : {},
|
|
2927
|
+
...c.app.alias ? { alias: c.app.alias } : {},
|
|
2928
|
+
...c.app.routes ? { routes: c.app.routes } : {},
|
|
2929
|
+
// `d.app` in the script: the campaign's app, at the address the video shows (its alias, if it has one).
|
|
2930
|
+
props: { app: { ...c.app, url: c.app.alias ? `https://${typeof c.app.alias === "string" ? c.app.alias : c.app.alias.host}` : c.app.url } },
|
|
2931
|
+
...speak ? { voice, ...script ? { voiceScript: script } : {} } : {},
|
|
2932
|
+
scenesFile: `${outBase}.scenes.json`,
|
|
2933
|
+
headless: !v.headed,
|
|
2934
|
+
...v.launchArgs ? { launchArgs: v.launchArgs } : {},
|
|
2935
|
+
...v.tailMs !== void 0 ? { tailMs: v.tailMs } : {},
|
|
2936
|
+
log
|
|
2937
|
+
});
|
|
2938
|
+
({ width, height } = res.outputs[0]);
|
|
2939
|
+
seconds = Math.round(res.durationMs / 100) / 10;
|
|
2940
|
+
spoke = res.narrated;
|
|
2941
|
+
for (const w of res.warnings ?? []) log(`${r.id}: ${w}`);
|
|
2942
|
+
} else {
|
|
2943
|
+
const g = guideOf(c, v);
|
|
2944
|
+
if (!g) throw new Error(`no guide ${v.guide}`);
|
|
2945
|
+
const trace = join4(c.out, "work", `${r.id}.trace.json`);
|
|
2946
|
+
writeFileSync4(trace, JSON.stringify(g.trace, null, 2));
|
|
2947
|
+
traceFile = trace;
|
|
2948
|
+
let storageState = c.app.storageState;
|
|
2949
|
+
if (!storageState && session) {
|
|
2950
|
+
storageState = join4(c.out, "work", `${r.id}.session.json`);
|
|
2951
|
+
writeFileSync4(storageState, JSON.stringify(await session(c.app.url, v.session ?? {})));
|
|
2952
|
+
}
|
|
2953
|
+
const args = [trace, "--url", `${c.app.url.replace(/\/$/, "")}${g.page}`, "--navigate", "--brand", look.brand];
|
|
2954
|
+
if (storageState) args.push("--storage-state", storageState);
|
|
2955
|
+
if (r.phone) args.push("--viewport", tall ? `${TALL.width}x${TALL.height}` : PHONE);
|
|
2956
|
+
else if (wide) args.push("--viewport", `${WIDE.width}x${WIDE.height}`);
|
|
2957
|
+
if (r.format === "gif") args.push("--width", String(v.width ?? 800));
|
|
2958
|
+
if (v.mode === "perform") args.push("--mode", "perform", "--allow-perform");
|
|
2959
|
+
for (const [name, text] of Object.entries(v.values ?? {})) args.push("--value", `${name}=${text}`);
|
|
2960
|
+
args.push(...r.polish);
|
|
2961
|
+
if (speak && script) args.push("--voice", "given", "--script", script);
|
|
2962
|
+
for (const x of outs) args.push("--out", `${join4(c.out, x.name)}${x.canvas ? `@${x.canvas}` : ""}`);
|
|
2963
|
+
const p = parse(args);
|
|
2964
|
+
const res = await render({ ...p.options, ...speak ? { voice } : {} });
|
|
2965
|
+
({ width, height } = res.outputs[0]);
|
|
2966
|
+
seconds = Math.round(res.durationMs / 100) / 10;
|
|
2967
|
+
spoke = !!res.narration?.lines.length;
|
|
2968
|
+
for (const w of res.warnings ?? []) log(`${r.id}: ${w}`);
|
|
2969
|
+
}
|
|
2970
|
+
const files = {};
|
|
2971
|
+
for (const x of outs) files[x.key] = x.name;
|
|
2972
|
+
if (existsSync5(`${firstBase}.png`)) files.poster = `${outs[0].name.replace(/\.[^.]+$/, "")}.png`;
|
|
2973
|
+
if (existsSync5(`${firstBase}.vtt`)) files.vtt = `${outs[0].name.replace(/\.[^.]+$/, "")}.vtt`;
|
|
2974
|
+
made[r.id] = { files, width, height, seconds, narrated: spoke, video: v.id, look: r.look, at: (/* @__PURE__ */ new Date()).toISOString() };
|
|
2975
|
+
writeFileSync4(manifest, `${JSON.stringify(made, null, 2)}
|
|
2976
|
+
`);
|
|
2977
|
+
log(`${r.id}: \u2713 ${Object.values(files).join(", ")}`);
|
|
2978
|
+
progress.done(r.id);
|
|
2979
|
+
} catch (e) {
|
|
2980
|
+
if (e instanceof RenderError) {
|
|
2981
|
+
const rep = reportOf(e, traceFile);
|
|
2982
|
+
fail(r.id, `${rep.outcome}${rep.step !== null ? ` at step ${rep.step + 1}` : ""}: ${String(rep.message).split("\n")[0]}`);
|
|
2983
|
+
} else fail(r.id, String(e.message).split("\n")[0]);
|
|
2984
|
+
}
|
|
2985
|
+
}
|
|
2986
|
+
if (!failed.length) progress.finish();
|
|
2987
|
+
} finally {
|
|
2988
|
+
stop?.();
|
|
2989
|
+
}
|
|
2990
|
+
return { made, failed };
|
|
2991
|
+
}
|
|
2992
|
+
function saysOf(c, renderId) {
|
|
2993
|
+
const file = join4(c.out, `${renderId}.scenes.json`);
|
|
2994
|
+
if (!existsSync5(file)) return null;
|
|
2995
|
+
const lines = JSON.parse(readFileSync8(file, "utf8")).map((x) => String(x.caption ?? "").trim()).filter(Boolean);
|
|
2996
|
+
return lines.length ? lines : null;
|
|
2997
|
+
}
|
|
2998
|
+
|
|
2999
|
+
// src/cli.ts
|
|
3000
|
+
var USAGE = `Usage: trace-avatar-render <trace.json> --url <page> --out <file.gif|file.mp4> [options]
|
|
3001
|
+
|
|
3002
|
+
--url <url> the page the guide starts on (required)
|
|
3003
|
+
--out <file> .gif or .mp4; repeat for several (required). <file>@<shape> gives that output its own
|
|
3004
|
+
canvas (e.g. --out wide.mp4@16:9 --out square.mp4@1:1): several shapes from one capture
|
|
3005
|
+
--mode <overlay|perform> Show me (default) or Do it for me
|
|
3006
|
+
--allow-perform needed for perform: it clicks and types for real (use a sandbox account)
|
|
3007
|
+
--width <px> output width (GIF default 800, MP4 the capture's)
|
|
3008
|
+
--fps <n> frames per second (GIF default 12, MP4 30)
|
|
3009
|
+
--viewport <WxH> default: the trace's meta.viewport, else 1280x800
|
|
3010
|
+
--scale <n> device pixel ratio of the capture (2 for crisp video)
|
|
3011
|
+
--storage-state <file> a signed-in Playwright session
|
|
3012
|
+
--max-wait <ms> waits longer than this are cut to it (default 800, 0 keeps them)
|
|
3013
|
+
--hide <selector> hidden before capture; repeatable
|
|
3014
|
+
--blur <selector> blurred before capture (as well as [data-trace-blur]); repeatable
|
|
3015
|
+
--allow-stall render a stalled guide anyway instead of failing
|
|
3016
|
+
--navigate a step on another page: load that page, as the app's router would (without it, a
|
|
3017
|
+
Show me guide that crosses pages waits on "Open \u2026 to continue")
|
|
3018
|
+
--security [guards] perform: leave these to the viewer (a hand-off): all, or secrets,money,files
|
|
3019
|
+
--value <name=text> perform: a value to type, by name or field label (e.g. password=demo-pass); repeatable
|
|
3020
|
+
--player <file> the trace-perform.min.js to use (default: the installed trace-avatar)
|
|
3021
|
+
--timeline <file> write the timeline and the cuts as JSON
|
|
3022
|
+
--timeout <ms> give up after this long (default 120000)
|
|
3023
|
+
--report <file> write what happened as JSON: { outcome: passed|stalled|failed, step, reason, message }
|
|
3024
|
+
(for CI: one result per guide, whatever the exit code)
|
|
3025
|
+
--headed show the browser
|
|
3026
|
+
|
|
3027
|
+
Polish (all off by default):
|
|
3028
|
+
--frame <none|browser> a browser window around the app, on a background
|
|
3029
|
+
--background <css> the background behind the window and on the cards
|
|
3030
|
+
--camera zoom in on each action and pan between close ones
|
|
3031
|
+
--zoom <n> how far the camera goes in (default 1.6; implies --camera)
|
|
3032
|
+
--captions <burn|vtt|both> burn the steps' words in, write <out>.vtt + <out>.chapters.vtt, or both
|
|
3033
|
+
--captions-at <top|bottom> where burned-in captions sit (default bottom; top when the app's own UI is low)
|
|
3034
|
+
--poster [file] a PNG at the first click (default <out>.png)
|
|
3035
|
+
--title-card [title] a 1.5 s card first (default: the trace's title)
|
|
3036
|
+
--end-card [title] a 2 s card last (default: "Done")
|
|
3037
|
+
--canvas <shape> compose onto 9:16 (1080x1920), 4:5, 1:1, 16:9 or WxH: the app between safe margins on the
|
|
3038
|
+
background, for Reels, Stories and feeds (--width scales the whole canvas)
|
|
3039
|
+
--fill <shape> outputs of this shape fill the picture: no window, no margins (repeat; e.g. --fill 16:9
|
|
3040
|
+
with --viewport 1280x720 and --out wide.mp4@16:9). The brand's "fill" sets it too
|
|
3041
|
+
--watermark <text> a small badge on every frame, cards too, e.g. "Created with TraceAvatar \xB7 traceavatar.com"
|
|
3042
|
+
--watermark-at <corner> top-left, top-right, bottom-left or bottom-right (default)
|
|
3043
|
+
--brand <file.json> the look in one file: canvas, watermark, frame, background, camera, captions, captionsAt,
|
|
3044
|
+
titleCard, endCard ({ title, url, logo, color }); flags given here win over it
|
|
3045
|
+
|
|
3046
|
+
AI (nothing calls a model without --ai):
|
|
3047
|
+
--ai <provider> gateway: our AI gateway (AI_GATEWAY_URL, AI_GATEWAY_KEY), DeepSeek first, then Gemini,
|
|
3048
|
+
then OpenAI; or gateway:<provider>[/<model>][,<provider>\u2026]; openai:<model> (OPENAI_API_KEY,
|
|
3049
|
+
OPENAI_BASE_URL for any OpenAI-compatible server); or ./provider.js
|
|
3050
|
+
--ai-vision <model> the model for --qa's frames (gateway's DeepSeek default: deepseek-flash)
|
|
3051
|
+
--qa [strict] send a frame per step to the AI to look for errors, empty states, a stall, personal
|
|
3052
|
+
data; strict exits 1 when anything is flagged (needs a vision model)
|
|
3053
|
+
--alt write <out>.alt.txt; with --captions vtt, AI names the chapters
|
|
3054
|
+
--repair when a step can't find its element, or a wait's text never shows, propose a fix in
|
|
3055
|
+
<out>.repair.json (verified by playing again); still exits 1. With --ai, unsure
|
|
3056
|
+
cases are asked (DeepSeek first)
|
|
3057
|
+
--repair-vision with --repair and --ai: send a screenshot of the stalled page too (elements only)
|
|
3058
|
+
|
|
3059
|
+
Narration (MP4 only; nothing speaks without --voice):
|
|
3060
|
+
--voice <voice> gateway (our AI gateway: Gemini speech, AI_GATEWAY_URL/KEY), gateway:gemini/<model>:<voice>,
|
|
3061
|
+
openai:<model>[:<voice>] (OPENAI_API_KEY / OPENAI_BASE_URL), say[:<voice>] (macOS, offline),
|
|
3062
|
+
or ./voice.js. Each step's caption is spoken; the video holds where a line runs long
|
|
3063
|
+
--script <file.json> the spoken lines instead: { "lines": [{ "n": 1, "text": "\u2026" }] }
|
|
3064
|
+
--voice-out <file.wav> also write the narration track
|
|
3065
|
+
--voice-strict fail when a line needs a hold longer than 8 s
|
|
3066
|
+
--no-voice-cache don't keep spoken lines in .trace-avatar-cache/voice
|
|
3067
|
+
|
|
3068
|
+
Suggestions for a person to accept (the renderer never changes a trace):
|
|
3069
|
+
trace-avatar-render suggest <trace.json> --task captions|title|waits|narration --ai <provider> [--tone friendly|terse|formal]
|
|
3070
|
+
[--hints hints.json] [--out suggestions.json]
|
|
3071
|
+
trace-avatar-render apply <trace.json> <suggestions.json> --accept 1,3|all --out <new.trace.json>
|
|
3072
|
+
|
|
3073
|
+
A product's marketing videos from one campaign.json (every look and shape; progress kept, a stopped run carries on):
|
|
3074
|
+
trace-avatar-render campaign check [campaign.json] every problem at once
|
|
3075
|
+
trace-avatar-render campaign make [campaign.json] [video ids\u2026] [--start] [--fresh] [--voice <voice>]
|
|
3076
|
+
trace-avatar-render campaign status [campaign.json] done, failed, and what it's on now
|
|
3077
|
+
|
|
3078
|
+
A clip catalogue (trace-avatar-catalogue/1, schemas/catalogue-1.json):
|
|
3079
|
+
trace-avatar-render catalogue check <file.json|https://\u2026> [--fetch] [--json]
|
|
3080
|
+
--fetch: fetch every link too (reachable, its type, captions within the video, mp4 head)
|
|
3081
|
+
--allow-http: accept http:// links (a local test server)
|
|
3082
|
+
`;
|
|
3083
|
+
function parse(argv) {
|
|
3084
|
+
const extra = {};
|
|
3085
|
+
const flags = { "--poster": "poster", "--title-card": "title", "--end-card": "end", "--qa": "qa" };
|
|
3086
|
+
const rest = [];
|
|
3087
|
+
for (let k = 0; k < argv.length; k += 1) {
|
|
3088
|
+
const a = argv[k];
|
|
3089
|
+
const eq = a.indexOf("=");
|
|
3090
|
+
const name = eq > 0 ? a.slice(0, eq) : a;
|
|
3091
|
+
if (flags[name]) {
|
|
3092
|
+
if (eq > 0) extra[flags[name]] = a.slice(eq + 1);
|
|
3093
|
+
else if (argv[k + 1] !== void 0 && !argv[k + 1].startsWith("-") && !/\.json$/i.test(argv[k + 1]) && (flags[name] !== "qa" || argv[k + 1] === "strict")) {
|
|
3094
|
+
extra[flags[name]] = argv[k + 1];
|
|
3095
|
+
k += 1;
|
|
3096
|
+
} else extra[flags[name]] = true;
|
|
3097
|
+
continue;
|
|
3098
|
+
}
|
|
3099
|
+
rest.push(a);
|
|
3100
|
+
}
|
|
3101
|
+
const { values, positionals } = parseArgs({
|
|
3102
|
+
args: rest,
|
|
3103
|
+
allowPositionals: true,
|
|
3104
|
+
options: {
|
|
3105
|
+
url: { type: "string" },
|
|
3106
|
+
out: { type: "string", multiple: true },
|
|
3107
|
+
mode: { type: "string" },
|
|
3108
|
+
"allow-perform": { type: "boolean" },
|
|
3109
|
+
width: { type: "string" },
|
|
3110
|
+
fps: { type: "string" },
|
|
3111
|
+
viewport: { type: "string" },
|
|
3112
|
+
scale: { type: "string" },
|
|
3113
|
+
"storage-state": { type: "string" },
|
|
3114
|
+
"max-wait": { type: "string" },
|
|
3115
|
+
hide: { type: "string", multiple: true },
|
|
3116
|
+
blur: { type: "string", multiple: true },
|
|
3117
|
+
"allow-stall": { type: "boolean" },
|
|
3118
|
+
navigate: { type: "boolean" },
|
|
3119
|
+
security: { type: "string" },
|
|
3120
|
+
value: { type: "string", multiple: true },
|
|
3121
|
+
player: { type: "string" },
|
|
3122
|
+
timeline: { type: "string" },
|
|
3123
|
+
timeout: { type: "string" },
|
|
3124
|
+
headed: { type: "boolean" },
|
|
3125
|
+
help: { type: "boolean", short: "h" },
|
|
3126
|
+
frame: { type: "string" },
|
|
3127
|
+
background: { type: "string" },
|
|
3128
|
+
camera: { type: "boolean" },
|
|
3129
|
+
zoom: { type: "string" },
|
|
3130
|
+
captions: { type: "string" },
|
|
3131
|
+
"captions-at": { type: "string" },
|
|
3132
|
+
ai: { type: "string" },
|
|
3133
|
+
"ai-vision": { type: "string" },
|
|
3134
|
+
alt: { type: "boolean" },
|
|
3135
|
+
voice: { type: "string" },
|
|
3136
|
+
script: { type: "string" },
|
|
3137
|
+
"voice-out": { type: "string" },
|
|
3138
|
+
"voice-strict": { type: "boolean" },
|
|
3139
|
+
"no-voice-cache": { type: "boolean" },
|
|
3140
|
+
repair: { type: "boolean" },
|
|
3141
|
+
"repair-vision": { type: "boolean" },
|
|
3142
|
+
report: { type: "string" },
|
|
3143
|
+
canvas: { type: "string" },
|
|
3144
|
+
fill: { type: "string", multiple: true },
|
|
3145
|
+
watermark: { type: "string" },
|
|
3146
|
+
"watermark-at": { type: "string" },
|
|
3147
|
+
brand: { type: "string" }
|
|
3148
|
+
}
|
|
3149
|
+
});
|
|
3150
|
+
if (values.help) throw new UsageError("");
|
|
3151
|
+
const trace = positionals[0];
|
|
3152
|
+
if (!trace) throw new UsageError("a trace file is needed");
|
|
3153
|
+
if (positionals.length > 1) throw new UsageError(`one trace at a time (got ${positionals.join(", ")})`);
|
|
3154
|
+
if (!values.url) throw new UsageError("--url is needed");
|
|
3155
|
+
if (!values.out?.length) throw new UsageError("--out is needed");
|
|
3156
|
+
const num = (name, v, min = 0) => {
|
|
3157
|
+
if (v === void 0) return void 0;
|
|
3158
|
+
const n = Number(v);
|
|
3159
|
+
if (!Number.isFinite(n) || n < min) throw new UsageError(`--${name} must be a number \u2265 ${min}`);
|
|
3160
|
+
return n;
|
|
3161
|
+
};
|
|
3162
|
+
const mode = values.mode ?? "overlay";
|
|
3163
|
+
if (mode !== "overlay" && mode !== "perform") throw new UsageError("--mode is overlay or perform");
|
|
3164
|
+
let viewport;
|
|
3165
|
+
if (values.viewport) {
|
|
3166
|
+
const m = /^(\d+)x(\d+)$/i.exec(values.viewport);
|
|
3167
|
+
if (!m) throw new UsageError("--viewport is WIDTHxHEIGHT, e.g. 1280x800");
|
|
3168
|
+
viewport = { w: Number(m[1]), h: Number(m[2]) };
|
|
3169
|
+
}
|
|
3170
|
+
if (values.frame && values.frame !== "none" && values.frame !== "browser") throw new UsageError("--frame is none or browser");
|
|
3171
|
+
const captions = values.captions;
|
|
3172
|
+
if (captions !== void 0 && captions !== "burn" && captions !== "vtt" && captions !== "both") throw new UsageError("--captions is burn, vtt or both");
|
|
3173
|
+
const captionsAt = values["captions-at"];
|
|
3174
|
+
if (captionsAt !== void 0 && captionsAt !== "top" && captionsAt !== "bottom") throw new UsageError("--captions-at is top or bottom");
|
|
3175
|
+
const zoom = num("zoom", values.zoom, 1);
|
|
3176
|
+
if (extra.qa !== void 0 && extra.qa !== true && extra.qa !== "strict") throw new UsageError("--qa takes nothing or strict");
|
|
3177
|
+
if ((extra.qa !== void 0 || values.alt || values["ai-vision"]) && !values.ai) throw new UsageError("--qa, --alt and --ai-vision need --ai");
|
|
3178
|
+
if (values["repair-vision"] && !(values.repair && values.ai)) throw new UsageError("--repair-vision needs --repair and --ai");
|
|
3179
|
+
if ((values.script || values["voice-out"] || values["voice-strict"] || values["no-voice-cache"]) && !values.voice) throw new UsageError("--script, --voice-out, --voice-strict and --no-voice-cache need --voice");
|
|
3180
|
+
if (values.voice && !values.out.some((f) => /\.mp4(@.*)?$/i.test(f))) throw new UsageError("--voice needs an .mp4 output (a GIF has no sound)");
|
|
3181
|
+
let security;
|
|
3182
|
+
if (values.security !== void 0) {
|
|
3183
|
+
const s = values.security.trim().toLowerCase();
|
|
3184
|
+
const guards = s === "" || s === "all" || s === "true" ? true : s.split(/[\s,]+/).filter(Boolean);
|
|
3185
|
+
if (Array.isArray(guards) && guards.some((g) => !["secrets", "money", "files"].includes(g))) throw new UsageError("--security is all, or a list of secrets, money, files");
|
|
3186
|
+
security = guards;
|
|
3187
|
+
}
|
|
3188
|
+
const typed = {};
|
|
3189
|
+
for (const v of values.value ?? []) {
|
|
3190
|
+
const k = v.indexOf("=");
|
|
3191
|
+
if (k < 1) throw new UsageError("--value is name=text, e.g. password=demo-pass");
|
|
3192
|
+
typed[v.slice(0, k)] = v.slice(k + 1);
|
|
3193
|
+
}
|
|
3194
|
+
if ((security !== void 0 || values.value?.length) && mode !== "perform") throw new UsageError("--security and --value are for --mode perform");
|
|
3195
|
+
const tryUsage = (f) => {
|
|
3196
|
+
try {
|
|
3197
|
+
return f();
|
|
3198
|
+
} catch (e) {
|
|
3199
|
+
throw new UsageError(e.message);
|
|
3200
|
+
}
|
|
3201
|
+
};
|
|
3202
|
+
if (values.canvas !== void 0) tryUsage(() => canvasOf(values.canvas));
|
|
3203
|
+
for (const f of values.fill ?? []) tryUsage(() => canvasOf(f));
|
|
3204
|
+
for (const o of values.out ?? []) {
|
|
3205
|
+
const m = /^.+\.(?:gif|mp4)@(.+)$/i.exec(o);
|
|
3206
|
+
if (m) tryUsage(() => canvasOf(m[1]));
|
|
3207
|
+
}
|
|
3208
|
+
const at = values["watermark-at"];
|
|
3209
|
+
if (at !== void 0 && !CORNERS.includes(at)) throw new UsageError(`--watermark-at is ${CORNERS.join(", ")}`);
|
|
3210
|
+
const brand = values.brand !== void 0 ? tryUsage(() => brandFile(values.brand)) : {};
|
|
3211
|
+
let watermark = values.watermark !== void 0 ? tryUsage(() => watermarkOf(values.watermark)) : brand.watermark;
|
|
3212
|
+
if (at !== void 0) {
|
|
3213
|
+
if (watermark === void 0) throw new UsageError("--watermark-at needs a watermark (--watermark, or one in --brand)");
|
|
3214
|
+
watermark = { ...typeof watermark === "string" ? { text: watermark } : watermark, at };
|
|
3215
|
+
}
|
|
3216
|
+
const width = num("width", values.width, 16);
|
|
3217
|
+
const fps = num("fps", values.fps, 1);
|
|
3218
|
+
const maxWait = num("max-wait", values["max-wait"]);
|
|
3219
|
+
return {
|
|
3220
|
+
trace,
|
|
3221
|
+
options: {
|
|
3222
|
+
// The brand first: the flags below win over it.
|
|
3223
|
+
...brand,
|
|
3224
|
+
trace,
|
|
3225
|
+
url: values.url,
|
|
3226
|
+
mode,
|
|
3227
|
+
allowPerform: !!values["allow-perform"],
|
|
3228
|
+
out: values.out.map((spec) => {
|
|
3229
|
+
const m = /^(.+\.(?:gif|mp4))@(.+)$/i.exec(spec);
|
|
3230
|
+
const file = m ? m[1] : spec;
|
|
3231
|
+
return { file, ...width ? { width } : {}, ...fps ? { fps } : {}, ...m ? { canvas: m[2] } : {} };
|
|
3232
|
+
}),
|
|
3233
|
+
...viewport ? { viewport } : {},
|
|
3234
|
+
...values.scale ? { scale: num("scale", values.scale, 1) } : {},
|
|
3235
|
+
...values["storage-state"] ? { storageState: values["storage-state"] } : {},
|
|
3236
|
+
...maxWait !== void 0 ? { cuts: { maxWaitMs: maxWait } } : {},
|
|
3237
|
+
...values.hide ? { hide: values.hide } : {},
|
|
3238
|
+
...values.blur ? { blur: values.blur } : {},
|
|
3239
|
+
allowStall: !!values["allow-stall"],
|
|
3240
|
+
...values.navigate ? { navigate: true } : {},
|
|
3241
|
+
...security !== void 0 ? { security } : {},
|
|
3242
|
+
...values.value?.length ? { values: typed } : {},
|
|
3243
|
+
...values.player ? { player: values.player } : {},
|
|
3244
|
+
...values.timeline ? { timeline: values.timeline } : {},
|
|
3245
|
+
...values.timeout ? { timeoutMs: num("timeout", values.timeout, 1) } : {},
|
|
3246
|
+
headless: !values.headed,
|
|
3247
|
+
...values.frame ? { frame: values.frame } : {},
|
|
3248
|
+
...values.background ? { background: values.background } : {},
|
|
3249
|
+
...zoom ? { camera: { zoom } } : values.camera ? { camera: true } : {},
|
|
3250
|
+
...captions ? { captions } : {},
|
|
3251
|
+
...captionsAt ? { captionsAt } : {},
|
|
3252
|
+
...extra.poster !== void 0 ? { poster: extra.poster } : {},
|
|
3253
|
+
...extra.title !== void 0 ? { titleCard: extra.title } : {},
|
|
3254
|
+
...extra.end !== void 0 ? { endCard: extra.end } : {},
|
|
3255
|
+
...values.canvas !== void 0 ? { canvas: values.canvas } : {},
|
|
3256
|
+
...values.fill?.length ? { fill: values.fill } : {},
|
|
3257
|
+
...watermark !== void 0 ? { watermark } : {},
|
|
3258
|
+
...extra.qa !== void 0 ? { qa: true } : {},
|
|
3259
|
+
...values.alt ? { alt: true } : {},
|
|
3260
|
+
...values.script ? { script: values.script } : {},
|
|
3261
|
+
...values["voice-out"] ? { voiceOut: values["voice-out"] } : {},
|
|
3262
|
+
...values["voice-strict"] ? { voiceStrict: true } : {},
|
|
3263
|
+
...values.repair ? { repair: true } : {},
|
|
3264
|
+
...values["repair-vision"] ? { repairVision: true } : {}
|
|
3265
|
+
},
|
|
3266
|
+
...values.ai ? { ai: values.ai } : {},
|
|
3267
|
+
...values["ai-vision"] ? { aiVision: values["ai-vision"] } : {},
|
|
3268
|
+
...values.voice ? { voice: values.voice } : {},
|
|
3269
|
+
...values["no-voice-cache"] ? { noVoiceCache: true } : {},
|
|
3270
|
+
...extra.qa === "strict" ? { strict: true } : {},
|
|
3271
|
+
...values.report ? { report: values.report } : {}
|
|
3272
|
+
};
|
|
3273
|
+
}
|
|
3274
|
+
function reportOf(err, traceFile, repairFile) {
|
|
3275
|
+
if (!err) return { outcome: "passed", step: null, reason: null, message: null };
|
|
3276
|
+
const stall = err instanceof RenderError ? err.stall : void 0;
|
|
3277
|
+
if (stall && Number.isInteger(stall.index)) {
|
|
3278
|
+
let say = null;
|
|
3279
|
+
try {
|
|
3280
|
+
say = JSON.parse(readFileSync9(traceFile, "utf8")).steps?.[stall.index]?.say ?? null;
|
|
3281
|
+
} catch {
|
|
3282
|
+
}
|
|
3283
|
+
return {
|
|
3284
|
+
outcome: "stalled",
|
|
3285
|
+
step: stall.index,
|
|
3286
|
+
reason: stall.reason === "until-timeout" ? "until-timeout" : "not-found",
|
|
3287
|
+
message: say ?? err.message,
|
|
3288
|
+
...repairFile && existsSync6(repairFile) ? { repair: repairFile } : {}
|
|
3289
|
+
};
|
|
3290
|
+
}
|
|
3291
|
+
return { outcome: "failed", step: null, reason: null, message: err?.message ?? String(err) };
|
|
3292
|
+
}
|
|
3293
|
+
var UsageError = class extends Error {
|
|
3294
|
+
};
|
|
3295
|
+
async function main(argv, io = { log: console.log, error: console.error }) {
|
|
3296
|
+
if (argv[0] === "suggest" || argv[0] === "apply" || argv[0] === "catalogue" || argv[0] === "campaign") {
|
|
3297
|
+
const cmds = { suggest: suggestCmd, apply: applyCmd, catalogue: catalogueCmd, campaign: campaignCmd };
|
|
3298
|
+
try {
|
|
3299
|
+
return await cmds[argv[0]](argv.slice(1), io);
|
|
3300
|
+
} catch (e) {
|
|
3301
|
+
io.error(`trace-avatar-render ${argv[0]}: ${e.message}`);
|
|
3302
|
+
if (e instanceof UsageError) io.error(USAGE);
|
|
3303
|
+
return e instanceof UsageError ? 2 : 1;
|
|
3304
|
+
}
|
|
3305
|
+
}
|
|
3306
|
+
let p;
|
|
3307
|
+
try {
|
|
3308
|
+
p = parse(argv);
|
|
3309
|
+
} catch (e) {
|
|
3310
|
+
const msg = e.message;
|
|
3311
|
+
if (msg) io.error(`trace-avatar-render: ${msg}
|
|
3312
|
+
`);
|
|
3313
|
+
io.error(USAGE);
|
|
3314
|
+
return e instanceof UsageError && !msg ? 0 : 2;
|
|
3315
|
+
}
|
|
3316
|
+
try {
|
|
3317
|
+
if (p.ai) p.options.ai = await providerOf(p.ai, process.env, p.aiVision ? { vision: p.aiVision } : {});
|
|
3318
|
+
if (p.voice) p.options.voice = await voiceOf(p.voice, process.env, p.noVoiceCache ? { cacheDir: null } : {});
|
|
3319
|
+
const r = await render(p.options);
|
|
3320
|
+
for (const o of r.outputs) io.log(`${o.file} ${o.width}\xD7${o.height} ${o.fps} fps ${(r.durationMs / 1e3).toFixed(1)} s ${(o.bytes / 1024).toFixed(0)} KB`);
|
|
3321
|
+
if (r.rawMs > r.durationMs + 50) io.log(`cut ${((r.rawMs - r.durationMs) / 1e3).toFixed(1)} s of waiting`);
|
|
3322
|
+
for (const f of [r.poster, r.vtt, r.chapters, r.alt]) if (f) io.log(f);
|
|
3323
|
+
if (r.narration) {
|
|
3324
|
+
io.log(`narration: ${r.narration.lines.length} line${r.narration.lines.length === 1 ? "" : "s"}${r.narration.holdMs ? `, the video holds ${(r.narration.holdMs / 1e3).toFixed(1)} s for them` : ""} ${r.narration.text}`);
|
|
3325
|
+
if (r.narration.wav) io.log(r.narration.wav);
|
|
3326
|
+
}
|
|
3327
|
+
for (const w of r.warnings ?? []) io.error(`warning: ${w}`);
|
|
3328
|
+
if (p.report) writeFileSync5(p.report, `${JSON.stringify(reportOf(null, p.trace), null, 2)}
|
|
3329
|
+
`);
|
|
3330
|
+
if (r.qa) {
|
|
3331
|
+
if (!r.qa.length) io.log("qa: nothing flagged");
|
|
3332
|
+
for (const f of r.qa) io.log(`qa: ${(f.at / 1e3).toFixed(1)} s ${f.kind} ${f.note}`);
|
|
3333
|
+
if (p.strict && r.qa.length) {
|
|
3334
|
+
io.error(`trace-avatar-render: qa flagged ${r.qa.length} problem${r.qa.length > 1 ? "s" : ""} (--qa strict)`);
|
|
3335
|
+
return 1;
|
|
3336
|
+
}
|
|
3337
|
+
}
|
|
3338
|
+
return 0;
|
|
3339
|
+
} catch (e) {
|
|
3340
|
+
io.error(`trace-avatar-render: ${e.message}`);
|
|
3341
|
+
if (p.report) {
|
|
3342
|
+
const first = p.options.out[0];
|
|
3343
|
+
const base2 = (typeof first === "string" ? first : first.file).replace(/\.[^./]+$/, "");
|
|
3344
|
+
writeFileSync5(p.report, `${JSON.stringify(reportOf(e, p.trace, `${base2}.repair.json`), null, 2)}
|
|
3345
|
+
`);
|
|
3346
|
+
}
|
|
3347
|
+
if (e instanceof RenderError && e.screenshot) {
|
|
3348
|
+
const first = p.options.out[0];
|
|
3349
|
+
const shot = `${(typeof first === "string" ? first : first.file).replace(/\.[^./]+$/, "")}.failure.png`;
|
|
3350
|
+
writeFileSync5(shot, e.screenshot);
|
|
3351
|
+
io.error(`the page at that moment: ${shot}`);
|
|
3352
|
+
}
|
|
3353
|
+
return 1;
|
|
3354
|
+
}
|
|
3355
|
+
}
|
|
3356
|
+
var readJson = (file) => {
|
|
3357
|
+
try {
|
|
3358
|
+
return JSON.parse(readFileSync9(file, "utf8"));
|
|
3359
|
+
} catch (e) {
|
|
3360
|
+
throw new Error(`could not read ${file}: ${e.message}`);
|
|
3361
|
+
}
|
|
3362
|
+
};
|
|
3363
|
+
async function suggestCmd(argv, io) {
|
|
3364
|
+
const { values, positionals } = parseArgs({ args: argv, allowPositionals: true, options: {
|
|
3365
|
+
task: { type: "string" },
|
|
3366
|
+
ai: { type: "string" },
|
|
3367
|
+
tone: { type: "string" },
|
|
3368
|
+
hints: { type: "string" },
|
|
3369
|
+
out: { type: "string" },
|
|
3370
|
+
"send-typed": { type: "boolean" }
|
|
3371
|
+
} });
|
|
3372
|
+
const [file] = positionals;
|
|
3373
|
+
if (!file) throw new UsageError("suggest needs a trace file");
|
|
3374
|
+
const task = values.task;
|
|
3375
|
+
if (task !== "captions" && task !== "title" && task !== "waits" && task !== "narration") throw new UsageError("--task is captions, title, waits or narration");
|
|
3376
|
+
if (!values.ai) throw new UsageError("suggest needs --ai");
|
|
3377
|
+
if (task === "narration") {
|
|
3378
|
+
const r2 = await suggestScript(await providerOf(values.ai), readJson(file));
|
|
3379
|
+
const out2 = JSON.stringify({ ...r2.script, ...r2.dropped.length ? { dropped: r2.dropped } : {} }, null, 2);
|
|
3380
|
+
if (values.out) {
|
|
3381
|
+
writeFileSync5(values.out, `${out2}
|
|
3382
|
+
`);
|
|
3383
|
+
io.log(`${r2.script.lines.length} line${r2.script.lines.length === 1 ? "" : "s"} in ${values.out}: edit them, then --script ${values.out}`);
|
|
3384
|
+
} else io.log(out2);
|
|
3385
|
+
return 0;
|
|
3386
|
+
}
|
|
3387
|
+
const tone = values.tone;
|
|
3388
|
+
if (tone !== void 0 && tone !== "friendly" && tone !== "terse" && tone !== "formal") throw new UsageError("--tone is friendly, terse or formal");
|
|
3389
|
+
const trace = readJson(file);
|
|
3390
|
+
const hints = values.hints ? readJson(values.hints) : void 0;
|
|
3391
|
+
const r = await suggest(task, trace, { ai: await providerOf(values.ai), ...hints ? { hints } : {}, ...tone ? { tone } : {}, ...values["send-typed"] ? { sendTyped: true } : {} });
|
|
3392
|
+
const out = JSON.stringify({ task, suggestions: r.suggestions.map((sg, k) => ({ id: k + 1, ...sg })), dropped: r.dropped }, null, 2);
|
|
3393
|
+
if (values.out) {
|
|
3394
|
+
writeFileSync5(values.out, `${out}
|
|
3395
|
+
`);
|
|
3396
|
+
io.log(`${r.suggestions.length} suggestion${r.suggestions.length === 1 ? "" : "s"} in ${values.out}`);
|
|
3397
|
+
} else io.log(out);
|
|
3398
|
+
return 0;
|
|
3399
|
+
}
|
|
3400
|
+
async function applyCmd(argv, io) {
|
|
3401
|
+
const { values, positionals } = parseArgs({ args: argv, allowPositionals: true, options: { accept: { type: "string" }, out: { type: "string" } } });
|
|
3402
|
+
const [file, sugFile] = positionals;
|
|
3403
|
+
if (!file || !sugFile) throw new UsageError("apply needs a trace file and a suggestions file");
|
|
3404
|
+
if (!values.accept) throw new UsageError("apply needs --accept 1,3 (or all): a person picks what goes in");
|
|
3405
|
+
if (!values.out) throw new UsageError("apply needs --out for the new trace");
|
|
3406
|
+
const all = readJson(sugFile).suggestions ?? [];
|
|
3407
|
+
const ids = values.accept === "all" ? all.map((x) => x.id) : values.accept.split(",").map((x) => Number(x.trim()));
|
|
3408
|
+
const missing = ids.filter((id) => !all.some((x) => x.id === id));
|
|
3409
|
+
if (missing.length) throw new UsageError(`no suggestion ${missing.join(", ")} in ${sugFile}`);
|
|
3410
|
+
const next = applySuggestions(readJson(file), all.filter((x) => ids.includes(x.id)));
|
|
3411
|
+
writeFileSync5(values.out, `${JSON.stringify(next, null, 2)}
|
|
3412
|
+
`);
|
|
3413
|
+
io.log(`applied ${ids.length} of ${all.length} to ${values.out}`);
|
|
3414
|
+
return 0;
|
|
3415
|
+
}
|
|
3416
|
+
async function campaignCmd(argv, io) {
|
|
3417
|
+
const { values, positionals } = parseArgs({ args: argv, allowPositionals: true, options: { start: { type: "boolean" }, fresh: { type: "boolean" }, voice: { type: "string" } } });
|
|
3418
|
+
const [sub, ...rest] = positionals;
|
|
3419
|
+
const file = rest[0] && /\.json$/i.test(rest[0]) ? rest.shift() : "campaign.json";
|
|
3420
|
+
if (sub !== "check" && sub !== "make" && sub !== "status") throw new UsageError("campaign check, make or status");
|
|
3421
|
+
if (!existsSync6(file)) throw new UsageError(`no campaign file ${file}`);
|
|
3422
|
+
if (sub === "status") {
|
|
3423
|
+
io.log(statusOf(readCampaign(file, { files: false })));
|
|
3424
|
+
return 0;
|
|
3425
|
+
}
|
|
3426
|
+
if (sub === "check") {
|
|
3427
|
+
const c2 = readCampaign(file);
|
|
3428
|
+
io.log(`${file}: ${c2.product}, ${c2.videos.length} video${c2.videos.length === 1 ? "" : "s"} in ${Object.keys(c2.looks).length} look${Object.keys(c2.looks).length === 1 ? "" : "s"} \u2014 ready to make`);
|
|
3429
|
+
return 0;
|
|
3430
|
+
}
|
|
3431
|
+
const c = readCampaign(file);
|
|
3432
|
+
const r = await makeCampaign(c, { only: rest, start: !!values.start, fresh: !!values.fresh, ...values.voice !== void 0 ? { voice: values.voice } : {}, log: io.log });
|
|
3433
|
+
if (r.failed.length) {
|
|
3434
|
+
io.error(`
|
|
3435
|
+
${r.failed.length} failed:
|
|
3436
|
+
${r.failed.join("\n ")}`);
|
|
3437
|
+
return 1;
|
|
3438
|
+
}
|
|
3439
|
+
return 0;
|
|
3440
|
+
}
|
|
3441
|
+
async function catalogueCmd(argv, io) {
|
|
3442
|
+
const { values, positionals } = parseArgs({ args: argv, allowPositionals: true, options: { fetch: { type: "boolean" }, json: { type: "boolean" }, "allow-http": { type: "boolean" } } });
|
|
3443
|
+
const [sub, where] = positionals;
|
|
3444
|
+
if (sub !== "check" || !where) throw new UsageError("catalogue check needs a file or a link");
|
|
3445
|
+
let doc;
|
|
3446
|
+
if (/^https?:\/\//.test(where)) {
|
|
3447
|
+
const res = await fetch(where);
|
|
3448
|
+
if (!res.ok) throw new Error(`${where} answered ${res.status}`);
|
|
3449
|
+
doc = await res.json();
|
|
3450
|
+
} else {
|
|
3451
|
+
if (!existsSync6(where)) throw new UsageError(`no file ${where}`);
|
|
3452
|
+
doc = readJson(where);
|
|
3453
|
+
}
|
|
3454
|
+
const r = await checkCatalogue(doc, { fetch: !!values.fetch, allowHttp: !!values["allow-http"] });
|
|
3455
|
+
if (values.json) {
|
|
3456
|
+
io.log(JSON.stringify(r, null, 2));
|
|
3457
|
+
return r.ok ? 0 : 1;
|
|
3458
|
+
}
|
|
3459
|
+
for (const p of r.problems) io.error(`\u2717 ${p}`);
|
|
3460
|
+
for (const w of r.warnings) io.log(`warning: ${w}`);
|
|
3461
|
+
io.log(`${where}: ${r.videos} video${r.videos === 1 ? "" : "s"}${values.fetch ? `, ${r.fetched} link${r.fetched === 1 ? "" : "s"} fetched` : ""} \u2014 ${r.ok ? "keeps trace-avatar-catalogue/1" : `${r.problems.length} problem${r.problems.length === 1 ? "" : "s"}`}${r.warnings.length ? `, ${r.warnings.length} warning${r.warnings.length === 1 ? "" : "s"}` : ""}`);
|
|
3462
|
+
return r.ok ? 0 : 1;
|
|
3463
|
+
}
|
|
3464
|
+
var isMain = import.meta.url === `file://${process.argv[1]}` || process.argv[1]?.endsWith("/trace-avatar-render") || process.argv[1]?.endsWith("/cli.js");
|
|
3465
|
+
if (isMain) void main(process.argv.slice(2)).then((code) => {
|
|
3466
|
+
process.exitCode = code;
|
|
3467
|
+
});
|
|
3468
|
+
|
|
2103
3469
|
export {
|
|
2104
3470
|
ZOOM_KINDS,
|
|
2105
3471
|
cameraKeys,
|
|
@@ -2186,6 +3552,38 @@ export {
|
|
|
2186
3552
|
catalogueId,
|
|
2187
3553
|
vttEnds,
|
|
2188
3554
|
mp4Head,
|
|
2189
|
-
checkCatalogue
|
|
3555
|
+
checkCatalogue,
|
|
3556
|
+
DIRECTOR_VIEWPORT,
|
|
3557
|
+
SKIP_KEEP_MS,
|
|
3558
|
+
arrowSprite,
|
|
3559
|
+
drawCursor,
|
|
3560
|
+
cursorAt,
|
|
3561
|
+
withCursor,
|
|
3562
|
+
pathOf,
|
|
3563
|
+
travelMs,
|
|
3564
|
+
sayFrom,
|
|
3565
|
+
lookOptions,
|
|
3566
|
+
cardsOf,
|
|
3567
|
+
aliasOf,
|
|
3568
|
+
direct,
|
|
3569
|
+
USAGE,
|
|
3570
|
+
parse,
|
|
3571
|
+
reportOf,
|
|
3572
|
+
UsageError,
|
|
3573
|
+
main,
|
|
3574
|
+
CAMPAIGN_KINDS,
|
|
3575
|
+
CAMPAIGN_SHAPES,
|
|
3576
|
+
loadCampaign,
|
|
3577
|
+
checkCampaign,
|
|
3578
|
+
readCampaign,
|
|
3579
|
+
rendersOf,
|
|
3580
|
+
shapeTag,
|
|
3581
|
+
filesOf,
|
|
3582
|
+
endsWithOf,
|
|
3583
|
+
progressOf,
|
|
3584
|
+
statusOf,
|
|
3585
|
+
ensureApp,
|
|
3586
|
+
makeCampaign,
|
|
3587
|
+
saysOf
|
|
2190
3588
|
};
|
|
2191
|
-
//# sourceMappingURL=chunk-
|
|
3589
|
+
//# sourceMappingURL=chunk-MC2ZHX5Z.js.map
|