@hypersocial/decks-cli 0.9.3 → 0.9.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypersocial/decks-cli",
3
- "version": "0.9.3",
3
+ "version": "0.9.4",
4
4
  "description": "Command-line client for Hypersocial Decks live analytics and share management.",
5
5
  "type": "module",
6
6
  "main": "./src/commands.mjs",
@@ -144,7 +144,7 @@ Hyperdecks server; use the CLI where you have a shell and the tool otherwise.
144
144
  | Ship | `hyperdecks release decks/<slug> --to "<name>" --email <e>` | `release_deck` |
145
145
  | Share-link poster | `hyperdecks poster <slug>` | `generate_deck_poster` |
146
146
  | PDF for a data room or attachment | `hyperdecks export <slug> --pdf [--notes] --out deck.pdf`, `hyperdecks export list` | `export_deck`, `list_exports` |
147
- | Video (MP4) for YouTube, LinkedIn or an attachment | `hyperdecks export <slug\|deck-dir> --video [--size 1080p\|720p\|4k] [--shape 16:9\|9:16\|1:1] [--title-card] [--end-card] [--no-captions] [--no-avatar] [--upload] --out deck.mp4`: records the narrated player in real time (a 4-minute deck takes about 5), avatar, voice and captions, no player chrome; a slug records the released copy, a deck folder its working copy; `--upload` lists it with the deck's exports | `export_deck { kind: "mp4" }` answers with this command (CLI only until the render worker ships) |
147
+ | Video (MP4) for YouTube, LinkedIn or an attachment | `hyperdecks export <slug\|deck-dir> --video [--size 1080p\|720p\|4k] [--shape 16:9\|9:16\|1:1] [--title-card] [--end-card] [--no-captions] [--no-avatar] [--upload] --out deck.mp4`: records the narrated player in real time (a 4-minute deck takes about 5), avatar, voice and captions, no player chrome; a slug records the released copy, a deck folder its working copy; `--upload` lists it with the deck's exports | `export_deck { kind: "mp4" }` renders it on our render worker (the dashboard's Export → Video); poll `check_export`, or `hyperdecks export status <exportId>` |
148
148
  | Import only | `hyperdecks import <file> --slug <slug>` | `import_deck_from_url` |
149
149
  | One more link | `hyperdecks share new <slug> "<name>" [--no-forward]` | `create_share` (`allowForward`) |
150
150
  | Forwarding, one link | `hyperdecks share forward on\|off\|default <token>` | `set_share_forwarding` |
@@ -103,9 +103,14 @@ makes the hello deck; continue from step 5 below.
103
103
  days; `notes: true` adds the slide's script after each slide.
104
104
  `list_exports` shows what was exported. Release first: it prints the live
105
105
  deck, not the draft.
106
- A video (MP4) is recorded by the CLI for now: `export_deck { kind: "mp4" }`
107
- answers with the `hyperdecks export <slug> --video` command; give it to the
108
- user to run (it records in real time, so a 4-minute deck takes about 5).
106
+ A video (MP4): `export_deck { slug, kind: "mp4", size?, shape?, titleCard?,
107
+ endCard? }` queues it on our render worker and returns an `exportId` at once.
108
+ It records in real time (a 4-minute deck takes about 5), so tell the user it
109
+ is on its way, then poll `check_export { exportId }` every 20-30 s: it shows
110
+ the slide it is on, and once `ready` a `url` plus a `title` and `description`
111
+ for YouTube. The owner is emailed the link too. On a server with no render
112
+ worker it answers with the `hyperdecks export <slug> --video` command
113
+ instead; give that to the user to run.
109
114
 
110
115
  ## Rules that matter
111
116
 
package/src/commands.mjs CHANGED
@@ -416,12 +416,15 @@ commands:
416
416
  with --notes), a footer with the deck's tracked link; kept 30 days
417
417
  export <slug|deck-dir> --video [--size 1080p|720p|4k] [--shape 16:9|9:16|1:1] [--no-captions]
418
418
  [--no-avatar] [--title-card] [--end-card [--link <url>]] [--upload] [--out <file.mp4>] [--json]
419
- an MP4 of the narrated deck, recorded in real time (a 4-minute deck takes about
420
- 5): the avatar, voice and captions, none of the player's chrome. A slug records
419
+ an MP4 of the narrated deck, drawn frame by frame so the avatar is smooth (slower
420
+ than real time: a 4-minute deck takes about 25 minutes with the live head on a
421
+ laptop): the avatar, voice and captions, none of the player's chrome. A slug records
421
422
  the released copy, a deck folder its working copy. --title-card adds the title
422
423
  and presenter (2s), --end-card the deck's "Video viewers" link (3s); --upload
423
424
  keeps it with the deck's exports for 30 days
424
425
  export list [<slug>] [--json] your exports and their download links
426
+ export status <exportId> [--json] one export: a hosted video's progress, its link when done
427
+ export cancel <exportId> stop a hosted video export (Export → Video, export_deck mp4)
425
428
  export rm <exportId> delete an export now
426
429
  poster <slug> [--force] [--json]
427
430
  regenerate the share-link poster (release makes one) and print its URL
@@ -712,6 +715,18 @@ async function exportCmd(pos, opts) {
712
715
  console.log(` download (30 days): ${out.url}`);
713
716
  }
714
717
 
718
+ // One export, a line: "pitch mp4 rendering: slide 3/12" / "… ready, 4:31, 1920×1080, 48.2 MB".
719
+ export function exportStatusLine(r) {
720
+ const p = r.progress;
721
+ const what = r.status === "rendering" && p
722
+ ? (p.slides ? `slide ${p.slide}/${p.slides}` : "starting") + (p.phase && !["recording", "starting"].includes(p.phase) ? ` (${p.phase})` : "")
723
+ : r.status === "queued" ? `${r.ahead ?? 0} ahead in line`
724
+ : r.status === "failed" ? r.error ?? "failed"
725
+ : r.status === "ready" ? [r.seconds ? `${Math.floor(r.seconds / 60)}:${String(Math.round(r.seconds % 60)).padStart(2, "0")}` : null, r.width ? `${r.width}×${r.height}` : null, r.pages ? `${r.pages} pages` : null, r.bytes ? `${(r.bytes / 1e6).toFixed(1)} MB` : null].filter(Boolean).join(", ")
726
+ : "";
727
+ return `${r.exportId} ${r.slug} ${r.kind} ${r.status}${what ? `: ${what}` : ""}`;
728
+ }
729
+
715
730
  async function exportList(pos, opts) {
716
731
  const rows = await ops.listExports(opts, { slug: pos[0] });
717
732
  if (opts.json) return void console.log(JSON.stringify(rows, null, 2));
@@ -993,6 +1008,18 @@ function commandTable(pos, rest, opts) {
993
1008
  poster: () => poster(pos.slice(1), opts),
994
1009
  export: () => exportCmd(pos.slice(1), opts),
995
1010
  "export list": () => exportList(pos.slice(2), opts),
1011
+ "export status": async () => {
1012
+ if (!pos[2]) throw new Error("usage: export status <exportId>");
1013
+ const r = await ops.checkExport(opts, pos[2]);
1014
+ if (opts.json) return void console.log(JSON.stringify(r, null, 2));
1015
+ console.log(exportStatusLine(r));
1016
+ if (r.url) console.log(` ${r.url}`);
1017
+ },
1018
+ "export cancel": async () => {
1019
+ if (!pos[2]) throw new Error("usage: export cancel <exportId>");
1020
+ const out = await ops.cancelExport(opts, pos[2]);
1021
+ console.log(`✓ cancelled ${out.exportId}`);
1022
+ },
996
1023
  "export rm": async () => {
997
1024
  if (!pos[2]) throw new Error("usage: export rm <exportId>");
998
1025
  const out = await ops.removeExport(opts, { exportId: pos[2] });
@@ -1,12 +1,16 @@
1
1
  // hyperdecks export <slug|deck-dir> --video: a narrated deck as an MP4.
2
2
  //
3
- // Real time, as the email GIF preview records (preview-capture.mjs): Chromium
4
- // opens the deck in the player's export mode (?export=1: no chrome, autoplay,
5
- // no tracking; packages/player/export-mode.js) and the compositor's screencast
6
- // is written to disk at 30 fps until the tour ends. The screencast has no
7
- // sound, so the audio track is built from the clips themselves, each placed at
8
- // the moment the player really started it (window.__hdExport), so the voice
9
- // lands under the frames that show its captions and cues. An optional title
3
+ // Frame by frame: Chromium opens the deck in the player's export mode
4
+ // (?export=1: no chrome, autoplay, no tracking; packages/player/export-mode.js)
5
+ // on a page clock the recorder controls (frame-clock.mjs). Once the deck is
6
+ // ready the clock is frozen; then, per video frame, it is advanced 1/30 s and
7
+ // a screenshot taken, so every frame is drawn, however slowly the live head
8
+ // draws in headless Chromium (a real-time screencast got about 5 of 30 a
9
+ // second). It runs slower than real time. The page plays no sound (its
10
+ // narration runs on the same clock), so the audio track is built from the
11
+ // clips themselves, each placed at the page-clock moment the player started it
12
+ // (window.__hdExport), so the voice lands under the frames that show its
13
+ // captions, cues and lip-sync. An optional title
10
14
  // card (2 s) and end card (3 s) are plain pages rendered the same size.
11
15
  // ffmpeg (the CLI's portable @ffmpeg-installer build) mixes and encodes:
12
16
  // H.264, AAC, +faststart.
@@ -15,7 +19,8 @@ import { tmpdir } from 'node:os';
15
19
  import { dirname, extname, join } from 'node:path';
16
20
  import { execFile } from 'node:child_process';
17
21
  import { promisify } from 'node:util';
18
- import { allowCaptureDestination, routeCaptureRequest } from './preview-capture.mjs';
22
+ import { allowCaptureDestination, installChromium, isMissingBrowser, routeCaptureRequest } from './preview-capture.mjs';
23
+ import { installFrameClock } from './frame-clock.mjs';
19
24
 
20
25
  const run = promisify(execFile);
21
26
 
@@ -24,9 +29,11 @@ export const VIDEO_SIZES = Object.freeze({ '720p': 720, '1080p': 1080, '4k': 216
24
29
  export const VIDEO_SHAPES = Object.freeze(['16:9', '9:16', '1:1']);
25
30
  export const TITLE_CARD_SECONDS = 2;
26
31
  export const END_CARD_SECONDS = 3;
27
- // What the player's clock may run past the manifest's total before the export
28
- // is called stuck: slow clip loads and transitions add a little per slide.
32
+ // Page-clock seconds a slide may run past its planned clip and hold before the
33
+ // export is called stuck: slow clip loads and transitions add a little.
29
34
  const STUCK_GRACE_SECONDS = 60;
35
+ // Real seconds one frame (a clock step and a screenshot) may take.
36
+ const FRAME_TIMEOUT_MS = 60000;
30
37
  const MAX_CLIP_BYTES = 50 * 1024 * 1024;
31
38
 
32
39
  // Output pixels, and the viewport that draws them: 4K is the 1080p layout at
@@ -186,6 +193,19 @@ async function ffmpeg(path, args, timeoutMs) {
186
193
  }
187
194
  }
188
195
 
196
+ // A clip's real length in seconds, from its decoded samples (an MP3's header
197
+ // can be off): the page's narration ends each clip there, as <audio> would.
198
+ const LENGTH_RATE = 8000;
199
+ export async function clipSeconds(path, file) {
200
+ let stdout;
201
+ try {
202
+ ({ stdout } = await run(path, ['-hide_banner', '-loglevel', 'error', '-nostdin', '-i', file, '-vn', '-ac', '1', '-ar', String(LENGTH_RATE), '-f', 's16le', '-'], { encoding: 'buffer', timeout: 60000, maxBuffer: 256 * 1024 * 1024, env: { PATH: dirname(path), ...(process.platform === 'win32' ? { SystemRoot: process.env.SystemRoot } : {}) } }));
203
+ } catch (e) { throw new Error(`could not read ${file}: ${String(e.stderr || e.message).trim().split('\n').at(-1)}`); }
204
+ const seconds = stdout.length / 2 / LENGTH_RATE;
205
+ if (!(seconds > 0)) throw new Error(`could not read ${file}: no audio in it`);
206
+ return seconds;
207
+ }
208
+
189
209
  // Bounds a page.evaluate on the Node side (a busy main thread can hold it).
190
210
  async function within(promise, ms, fallback) {
191
211
  promise.catch(() => {});
@@ -194,54 +214,54 @@ async function within(promise, ms, fallback) {
194
214
  finally { clearTimeout(timer); }
195
215
  }
196
216
 
197
- // Screencast frames into numbered slots from `first`, one per 1/fps from
198
- // `mark` (epoch seconds): each slot holds the newest frame up to half a slot
199
- // past its time. `stop(seconds)` fills the slots up to that many seconds after `mark`
200
- // with the last frame and returns how many it wrote.
201
- function startFrameSlots(session, { dir, fps, first }) {
202
- let mark = Infinity, last = null, next = 0, writes = Promise.resolve(), received = 0, stopped = false, failed = null;
203
- // The first failed write (a full disk, say) is kept and thrown by check()/stop(), never left unhandled.
204
- const write = (data) => {
205
- const path = join(dir, frameName(first + next++));
206
- writes = writes.then(() => (failed ? null : writeFile(path, data))).catch((e) => { failed ??= e; });
207
- };
208
- session.on('Page.screencastFrame', ({ data, metadata, sessionId }) => {
209
- session.send('Page.screencastFrameAck', { sessionId }).catch(() => {});
210
- const ts = metadata.timestamp ?? Date.now() / 1000;
211
- if (stopped || ts < mark) return;
212
- // A slot takes the frame nearest its time: one stamped up to half a slot after it beats the one before.
213
- while (last && mark + (next + 0.5) / fps < ts) write(last);
214
- last = Buffer.from(data, 'base64');
215
- received++;
216
- });
217
- return {
218
- get received() { return received; },
219
- check() { if (failed) throw new Error(`could not write a frame: ${failed.message}`); },
220
- get latest() { return mark + next / fps; },
221
- begin(poster, at) { last = poster; mark = at; },
222
- async stop(seconds) {
223
- stopped = true;
224
- const total = Math.max(1, Math.round(seconds * fps));
225
- while (next < total) write(last);
226
- await writes;
227
- if (failed) throw new Error(`could not write a frame: ${failed.message}`);
228
- return total;
229
- },
230
- settled: () => writes,
231
- };
232
- }
233
-
234
217
  // The player's export log, read without trusting the page to answer quickly.
235
- const readLog = (page) => within(page.evaluate(() => {
218
+ // undefined when it did not answer within `ms`.
219
+ const readLog = (page, ms = 5000) => within(page.evaluate(() => {
236
220
  const l = window.__hdExport;
237
221
  return l ? JSON.parse(JSON.stringify(l)) : null;
238
- }), 5000, undefined);
222
+ }), ms, undefined);
223
+
224
+ // What the page's network did before a ready timeout, for the error message.
225
+ // `blocked` holds the URLs the capture rules refused; `failed` is the last
226
+ // request that failed for another reason ({ url, error }).
227
+ export function readySaw({ blocked = [], failed = null } = {}) {
228
+ const cut = (u) => (u.length > 160 ? `${u.slice(0, 157)}…` : u);
229
+ const parts = [];
230
+ if (blocked.length) parts.push(`${blocked.length} request${blocked.length === 1 ? '' : 's'} blocked, last ${cut(blocked.at(-1))}`);
231
+ // requestfailed can fire before the route handler records the block.
232
+ if (failed && !blocked.includes(failed.url)) parts.push(`last failed request ${cut(failed.url)} (${failed.error})`);
233
+ return parts.length ? ` (${parts.join('; ')})` : '';
234
+ }
235
+
236
+ // Why a child process failed: the last line it printed, or its timeout.
237
+ const reasonOf = (e) => (e?.killed ? 'timed out' : String(e?.stderr || '').trim().split('\n').filter(Boolean).at(-1) || String(e?.message ?? e).split('\n')[0]);
238
+
239
+ // Launches the recorder's Chromium. On a first run the browser is not on disk
240
+ // yet: install it (as `preview setup` does) with one line of notice and retry
241
+ // once, instead of stopping at "run preview setup".
242
+ export async function launchRecorder(chromium, options, { log = () => {}, install = installChromium } = {}) {
243
+ const first = (e) => String(e?.message ?? e).split('\n')[0];
244
+ const failed = (e) => new Error(`Could not launch isolated Chromium (${first(e)}). Run \`hyperdecks preview setup\` to install it.`);
245
+ try { return await chromium.launch(options); }
246
+ catch (e) {
247
+ if (!isMissingBrowser(e)) throw failed(e);
248
+ log('first run: installing the Chromium build video export records with (one time, about a minute)…');
249
+ try { await install(); }
250
+ catch (err) { throw new Error(`Could not install isolated Chromium (${reasonOf(err)}). Run \`hyperdecks preview setup\` to retry.`); }
251
+ try { return await chromium.launch(options); }
252
+ catch (err) {
253
+ // Installed, yet still not where this Playwright looks: another install will not help.
254
+ if (isMissingBrowser(err)) throw new Error(`Installed Chromium, but this CLI's Playwright still cannot find it (${first(err)}). Check PLAYWRIGHT_BROWSERS_PATH.`);
255
+ throw failed(err);
256
+ }
257
+ }
258
+ }
239
259
 
240
260
  // Records `config.url` (a page that loads player/v1) and writes the MP4.
241
261
  // config: { url, geometry, captions, avatar, titleCard, endCard, title,
242
262
  // presenter, link, out, firstPartyOrigins, playerDir }. `log` gets progress
243
263
  // lines. Returns { file, seconds, width, height, bytes, slides, clips, warnings }.
244
- export async function recordVideo(config, { log = () => {}, chromium: injected } = {}) {
264
+ export async function recordVideo(config, { log = () => {}, chromium: injected, install } = {}) {
245
265
  const { geometry, out } = config;
246
266
  const ff = await ffmpegPath();
247
267
  const chromium = injected ?? (await import('playwright')).chromium;
@@ -249,20 +269,19 @@ export async function recordVideo(config, { log = () => {}, chromium: injected }
249
269
  let browser, temp, phase = 'launch';
250
270
  try {
251
271
  temp = await mkdtemp(join(tmpdir(), 'hyperdecks-video-'));
252
- try {
253
- browser = await chromium.launch({
254
- // config.sandbox: false only for CI runners whose AppArmor blocks Chromium's sandbox (the smoke suite).
255
- headless: true, timeout: 30000, chromiumSandbox: config.sandbox ?? process.platform === 'linux',
256
- args: ['--autoplay-policy=no-user-gesture-required', '--mute-audio', '--enable-unsafe-swiftshader'],
257
- env: { ...(process.platform === 'win32' ? { SystemRoot: process.env.SystemRoot } : {}), PATH: process.env.PATH, TMPDIR: temp, HOME: temp },
258
- });
259
- } catch (e) {
260
- throw new Error(`Could not launch isolated Chromium (${String(e?.message ?? e).split('\n')[0]}). Run \`hyperdecks preview setup\` to install it.`);
261
- }
272
+ browser = await launchRecorder(chromium, {
273
+ // config.sandbox: false only for CI runners whose AppArmor blocks Chromium's sandbox (the smoke suite).
274
+ headless: true, timeout: 30000, chromiumSandbox: config.sandbox ?? process.platform === 'linux',
275
+ args: ['--autoplay-policy=no-user-gesture-required', '--mute-audio', '--enable-unsafe-swiftshader'],
276
+ env: { ...(process.platform === 'win32' ? { SystemRoot: process.env.SystemRoot } : {}), PATH: process.env.PATH, TMPDIR: temp, HOME: temp },
277
+ }, { log, ...(install ? { install } : {}) });
262
278
  const pageUrl = exportPageUrl(config.url, config);
263
279
  const context = await browser.newContext({ viewport: geometry.viewport, deviceScaleFactor: geometry.scale, serviceWorkers: 'block', acceptDownloads: false });
264
- let blocked = 0;
265
- await context.route('**/*', async (route) => { if (!await routeCaptureRequest(route, pageUrl, config.firstPartyOrigins)) blocked++; });
280
+ const blockedUrls = [];
281
+ let failed = null;
282
+ await context.route('**/*', async (route) => {
283
+ if (!await routeCaptureRequest(route, pageUrl, config.firstPartyOrigins)) blockedUrls.push(route.request().url());
284
+ });
266
285
  // A development checkout plays its own player/v1 (HYPERDECKS_PLAYER_DIR), not the released one.
267
286
  if (config.playerDir) {
268
287
  await context.route(/\/player\/v1\/[^?#]+/, async (route) => {
@@ -274,6 +293,8 @@ export async function recordVideo(config, { log = () => {}, chromium: injected }
274
293
  });
275
294
  }
276
295
  await context.routeWebSocket('**/*', (ws) => ws.close());
296
+ // The page clock goes in before any of the deck's scripts (frame-clock.mjs).
297
+ await context.addInitScript({ content: `(${installFrameClock.toString()})(window);` });
277
298
  await context.addInitScript(() => {
278
299
  navigator.sendBeacon = () => false;
279
300
  window.open = () => null;
@@ -284,18 +305,25 @@ export async function recordVideo(config, { log = () => {}, chromium: injected }
284
305
  page.setDefaultTimeout(30000);
285
306
  const pageErrors = [];
286
307
  page.on('pageerror', (e) => pageErrors.push(String(e?.message ?? e)));
308
+ // A refused request also fails in the page; only the others count here.
309
+ page.on('requestfailed', (r) => { if (!blockedUrls.includes(r.url())) failed = { url: r.url(), error: r.failure()?.errorText ?? 'failed' }; });
287
310
  phase = 'page load';
288
311
  await page.goto(pageUrl, { waitUntil: 'domcontentloaded', timeout: 60000 });
289
312
  phase = 'player ready';
290
313
  const pageSaid = () => (pageErrors.length ? ` (page errors: ${pageErrors.slice(0, 3).join('; ').slice(0, 400)})` : '');
291
314
  await page.waitForSelector('body[data-hd-ready="true"]', { timeout: 60000 }).catch((e) => {
292
- throw new Error(`the deck never became ready (${e.message.split('\n')[0]})${pageSaid()}; is it on player/v1 (https://decks.hypersocial.ai/player/v1/player.js)?`);
315
+ throw new Error(`the deck never became ready (${e.message.split('\n')[0]})${pageSaid()}${readySaw({ blocked: blockedUrls, failed })}; is it on player/v1 (https://decks.hypersocial.ai/player/v1/player.js)?`);
293
316
  });
294
- let state = await readLog(page);
317
+ let state = await readLog(page, 30000);
318
+ if (state === undefined) throw new Error(`the deck's player did not return its export log within 30s${pageSaid()}`);
295
319
  if (!state) throw new Error(`the deck's player has no export mode: it must load https://decks.hypersocial.ai/player/v1/player.js (an older copy, or its own runtime, cannot be recorded)${pageSaid()}`);
296
320
  if (state.manifestError) throw new Error(`the deck's narration manifest could not load (${state.manifestError.url}: ${state.manifestError.message}); nothing was recorded`);
297
321
  if (state.silent) throw new Error('the deck has no narration to record: run `hyperdecks narrate` first');
322
+ // An older player plays its narration on wall time, which a frame-by-frame recording cannot follow.
323
+ if (!(state.version >= 2 && state.clock === 'page')) throw new Error('the deck\'s player is older than this CLI (its narration cannot follow a frame-by-frame recording); reload https://decks.hypersocial.ai/player/v1/player.js, or update the deck\'s copy');
298
324
  const plan = planTimeline(state.plan ?? []);
325
+ const face = state.presenter;
326
+ if (config.avatar !== false && face?.wanted === 'head' && face.shown !== 'head') warnings.push(`the deck's live head did not load${face.why ? ` (${face.why})` : ''}; the video shows ${face.shown === 'static' ? 'its still image' : 'captions only'}`);
299
327
  // The cards wear the first slide's colours and its heading's font.
300
328
  const look = await within(page.evaluate(() => {
301
329
  const first = document.querySelector('section.slide');
@@ -313,7 +341,7 @@ export async function recordVideo(config, { log = () => {}, chromium: injected }
313
341
  }), 5000, {});
314
342
  // Every clip's audio is fetched before recording: a bad URL fails now, not after minutes of real time.
315
343
  phase = 'clip audio';
316
- const audioFiles = new Map();
344
+ const audioFiles = new Map(), clipLengths = {};
317
345
  for (const [n, url] of [...new Set((state.plan ?? []).map((s) => s.audio).filter(Boolean))].entries()) {
318
346
  const id = state.plan.find((s) => s.audio === url).id;
319
347
  if (!await allowCaptureDestination(url, pageUrl)) throw new Error(`clip ${id}'s audio is not on a public address: ${url}`);
@@ -326,8 +354,9 @@ export async function recordVideo(config, { log = () => {}, chromium: injected }
326
354
  const path = join(temp, `clip-${n}${extname(new URL(url).pathname).slice(0, 6) || '.audio'}`);
327
355
  await writeFile(path, bytes);
328
356
  audioFiles.set(url, path);
357
+ clipLengths[url] = await clipSeconds(ff, path).catch((e) => { throw new Error(`clip ${id}'s audio: ${e.message}`); });
329
358
  }
330
- log(`recording ${plan.ids.length} slides${plan.total ? `, about ${clock(plan.total)} in real time,` : ''} at ${geometry.width}×${geometry.height} (${geometry.shape})`);
359
+ log(`recording ${plan.ids.length} slides${plan.total ? `, about ${clock(plan.total)} of video,` : ''} at ${geometry.width}×${geometry.height} (${geometry.shape}), frame by frame`);
331
360
 
332
361
  // The title card's frames come first.
333
362
  const lead = config.titleCard ? TITLE_CARD_SECONDS : 0;
@@ -354,47 +383,67 @@ export async function recordVideo(config, { log = () => {}, chromium: injected }
354
383
 
355
384
  phase = 'recording';
356
385
  const session = await context.newCDPSession(page);
357
- const slots = startFrameSlots(session, { dir: temp, fps: VIDEO_FPS, first: frameCount });
358
- await session.send('Page.startScreencast', { format: 'jpeg', quality: 90, maxWidth: geometry.width, maxHeight: geometry.height, everyNthFrame: 1 });
359
- // A screenshot forces a fresh frame: it, and every compositor frame after it, show the deck as it starts.
360
- const poster = await page.screenshot({ type: 'jpeg', quality: 90 });
361
- const markMs = Date.now();
362
- slots.begin(poster, markMs / 1000);
363
- await page.evaluate(() => window.__hdExportGo());
364
- // Stuck: no slide landed and no clip started for longer than the current
365
- // slide's planned clip and hold (unknown for a data-clip deck) plus a grace.
386
+ const fps = VIDEO_FPS;
387
+ const startedAt = Date.now();
388
+ let written = Promise.resolve();
389
+ const capture = async () => {
390
+ const { data } = await within(session.send('Page.captureScreenshot', { format: 'jpeg', quality: 90, optimizeForSpeed: true }), FRAME_TIMEOUT_MS, { data: null });
391
+ if (!data) throw new Error(`a frame took over ${FRAME_TIMEOUT_MS / 1000}s to capture${pageSaid()}`);
392
+ const path = join(temp, frameName(frameCount++));
393
+ // One write in flight while the next frame is drawn; a failed one (a full disk) throws at the next await.
394
+ await written;
395
+ written = writeFile(path, Buffer.from(data, 'base64')).catch((e) => { throw new Error(`could not write a frame: ${e.message}`); });
396
+ written.catch(() => {});
397
+ };
398
+ // Frame 0 is the deck as it starts; frame n shows it n/fps seconds later on the page clock.
399
+ const markMs = await page.evaluate(() => performance.timeOrigin + window.__hdFrameClock.freeze());
400
+ // Each clip ends where its real audio does (the manifest's length can be off), as <audio> would end it.
401
+ await page.evaluate((lengths) => { window.__hdExportClipLengths = lengths; window.__hdExportGo(); return window.__hdFrameClock.settle(); }, clipLengths);
402
+ const firstFrame = frameCount;
403
+ await capture();
366
404
  const secondsOf = new Map(plan.ids.map((id, i) => [id, plan.seconds[i]]));
367
- let shown = 0, progress = 0, movedAt = Date.now();
368
- for (;;) {
369
- await new Promise((ok) => setTimeout(ok, 500));
370
- slots.check();
371
- const next = await readLog(page);
372
- if (next) state = next;
373
- const failedClip = state.errors?.[0];
374
- if (failedClip) throw new Error(`narration failed on ${failedClip.id} (${failedClip.message}); the tour cannot go on`);
375
- const moved = (state.slides?.length ?? 0) + (state.clips?.length ?? 0);
376
- if (moved !== progress) { progress = moved; movedAt = Date.now(); }
377
- for (; shown < (state.slides?.length ?? 0); shown++) {
378
- const s = state.slides[shown];
379
- const i = plan.ids.indexOf(s.id);
380
- log(` slide ${i + 1}/${plan.ids.length} ${s.id} · ${clock(Math.max(0, (s.at - markMs) / 1000))}${plan.total ? ` of ${clock(plan.total)}` : ''}`);
405
+ let shown = 0, progress = 0, movedAt = 0, end = Infinity;
406
+ // Stuck: no slide landed and no clip started for longer than the current
407
+ // slide's planned clip and hold (unknown for a data-clip deck) plus a grace, on the page clock.
408
+ for (let n = 1; n < end; n++) {
409
+ const step = await within(page.evaluate(async (ms) => {
410
+ const t = window.__hdFrameClock.step(ms);
411
+ await window.__hdFrameClock.settle();
412
+ const l = window.__hdExport;
413
+ return { t, moved: l.slides.length + l.clips.length, errors: l.errors.length, done: l.done };
414
+ }, 1000 / fps), FRAME_TIMEOUT_MS, null);
415
+ if (!step) throw new Error(`the page stopped answering (a frame took over ${FRAME_TIMEOUT_MS / 1000}s to draw)${pageSaid()}`);
416
+ await capture();
417
+ const at = n / fps;
418
+ if (step.moved !== progress || step.errors || (step.done && end === Infinity)) {
419
+ const next = await readLog(page, FRAME_TIMEOUT_MS);
420
+ if (!next) throw new Error(`the page did not return its export log within ${FRAME_TIMEOUT_MS / 1000}s${pageSaid()}`);
421
+ state = next;
422
+ const failedClip = state.errors?.[0];
423
+ if (failedClip) throw new Error(`narration failed on ${failedClip.id} (${failedClip.message}); the tour cannot go on`);
424
+ if (step.moved !== progress) { progress = step.moved; movedAt = at; }
425
+ for (; shown < (state.slides?.length ?? 0); shown++) {
426
+ const s = state.slides[shown];
427
+ const i = plan.ids.indexOf(s.id);
428
+ log(` slide ${i + 1}/${plan.ids.length} ${s.id} · ${clock(Math.max(0, (s.at - markMs) / 1000))}${plan.total ? ` of ${clock(plan.total)}` : ''}`);
429
+ }
430
+ // The video ends on the frame where the tour ended.
431
+ if (state.done) end = Math.max(n + 1, Math.round(((state.doneAt - markMs) / 1000) * fps));
381
432
  }
382
- if (state.done) break;
383
- const at = state.slides?.at(-1)?.id ?? plan.ids[0];
384
- const allowed = ((secondsOf.get(at) || 0) * 1.5 + STUCK_GRACE_SECONDS) * 1000;
385
- if (Date.now() - movedAt > allowed) throw new Error(`the tour stopped on slide ${at}: nothing moved for ${Math.round(allowed / 1000)}s${pageSaid()}`);
433
+ const on = state.slides?.at(-1)?.id ?? plan.ids[0];
434
+ const allowed = (secondsOf.get(on) || 0) * 1.5 + STUCK_GRACE_SECONDS;
435
+ if (end === Infinity && at - movedAt > allowed) throw new Error(`the tour stopped on slide ${on}: nothing moved for ${Math.round(allowed)}s of the deck${pageSaid()}`);
386
436
  }
387
- const seconds = Math.max(0.5, (state.doneAt - markMs) / 1000);
388
- // Let the compositor deliver the frames up to the end before stopping.
389
- const waitUntil = Date.now() + 1500;
390
- while (slots.latest < markMs / 1000 + seconds && Date.now() < waitUntil) await new Promise((ok) => setTimeout(ok, 50));
391
- await session.send('Page.stopScreencast').catch(() => {});
392
- frameCount += await slots.stop(seconds);
393
- if (!slots.received) throw new Error('the screencast delivered no frames after the first; the capture broke (nothing would move in the video)');
437
+ await written;
438
+ const frames = frameCount - firstFrame;
439
+ const seconds = frames / fps;
440
+ const wall = (Date.now() - startedAt) / 1000;
441
+ log(`rendered ${frames} frames (${clock(seconds)}) in ${clock(wall)}: ${(frames / Math.max(0.001, wall)).toFixed(1)} frames a second, ${(wall / Math.max(0.001, seconds)).toFixed(1)}× the video's length`);
394
442
  // Drift means something only when every clip's planned length is known (a data-clip deck's are not).
395
443
  const drift = (state.plan ?? []).every((s) => !s.audio || s.duration > 0) ? timelineDrift(state, markMs) : [];
396
444
  const worst = drift.reduce((a, d) => (Math.abs(d.drift) > Math.abs(a.drift) ? d : a), { drift: 0 });
397
445
  if (Math.abs(worst.drift) > 2) warnings.push(`the player ran up to ${worst.drift.toFixed(1)}s off the manifest's timeline (by ${worst.id}: clip loads, transitions); each clip's audio is placed where it really started, so the voice stays in sync`);
446
+ const render = { frames, seconds: wall };
398
447
 
399
448
  if (config.endCard) {
400
449
  phase = 'end card';
@@ -404,7 +453,7 @@ export async function recordVideo(config, { log = () => {}, chromium: injected }
404
453
  await context.close();
405
454
  await browser.close();
406
455
  browser = null;
407
- if (blocked) warnings.push(`${blocked} request(s) blocked (tracking, navigation or unsafe destinations); nothing was recorded as a view.`);
456
+ if (blockedUrls.length) warnings.push(`${blockedUrls.length} request(s) blocked (tracking, navigation or unsafe destinations); nothing was recorded as a view.`);
408
457
 
409
458
  phase = 'audio';
410
459
  const clips = audioTimeline(state, markMs, lead);
@@ -430,7 +479,7 @@ export async function recordVideo(config, { log = () => {}, chromium: injected }
430
479
  });
431
480
  const bytes = (await stat(out)).size;
432
481
  if (pageErrors.length) warnings.push(`${pageErrors.length} page error(s) while recording: ${pageErrors.slice(0, 2).join('; ').slice(0, 300)}`);
433
- return { file: out, seconds: total, width: geometry.width, height: geometry.height, bytes, slides: plan.ids.length, clips: clips.length, warnings };
482
+ return { file: out, seconds: total, width: geometry.width, height: geometry.height, bytes, slides: plan.ids.length, clips: clips.length, render, warnings };
434
483
  } catch (e) {
435
484
  throw new Error(`Video export failed during ${phase}: ${e.message}`);
436
485
  } finally {
@@ -0,0 +1,26 @@
1
+ // What both MCP servers (app/api/mcp/route.ts, mcp/server.mjs) say about video
2
+ // exports, so the two never drift.
3
+
4
+ export const VIDEO_EXPORT_HELP =
5
+ "kind \"mp4\" records the narrated deck as a video on our render worker (drawn frame by frame, " +
6
+ "so it takes several times the deck's length: a 4-minute deck about half an hour), with the options size (720p, 1080p, 4k), shape (16:9, 9:16, 1:1), captions, avatar, " +
7
+ "titleCard and endCard (the deck's tracked link). It returns { exportId, status: \"queued\" } at once: poll " +
8
+ "check_export(exportId) every 20-30 seconds until status is ready (url is the MP4, kept 30 days; title and description " +
9
+ "are ready for YouTube) or failed (error says why). The owner is also emailed the link. One video per deck at a time, " +
10
+ "10 a day. Same as the dashboard's Export → Video; `hyperdecks export <slug> --video` records one locally instead.";
11
+
12
+ export const CHECK_EXPORT_HELP =
13
+ "Check an export started by export_deck: status (queued | rendering | ready | failed | cancelled); for a video, " +
14
+ "progress { slide, slides, phase } and ahead (jobs before it in line); once ready, url (the file, 30 days), " +
15
+ "filename, bytes, seconds, width, height, and title and description for a YouTube upload (the description " +
16
+ "carries the deck's tracked link); error when it failed. Poll every 20-30 seconds.";
17
+
18
+ // The video options as zod fields (each server passes its own zod).
19
+ export const videoExportInputs = (z) => ({
20
+ size: z.enum(["720p", "1080p", "4k"]).optional().describe("mp4: frame size, default 1080p"),
21
+ shape: z.enum(["16:9", "9:16", "1:1"]).optional().describe("mp4: default 16:9"),
22
+ captions: z.boolean().optional().describe("mp4: burn in captions (default true)"),
23
+ avatar: z.boolean().optional().describe("mp4: show the presenter (default true)"),
24
+ titleCard: z.boolean().optional().describe("mp4: a 2 s title card first"),
25
+ endCard: z.boolean().optional().describe("mp4: a 3 s end card with the deck's tracked link"),
26
+ });
@@ -0,0 +1,268 @@
1
+ // The page clock video export records on (export-video.mjs).
2
+ //
3
+ // Recording in real time loses frames: in headless Chromium the live head
4
+ // (WebGL) draws a few frames a second, so a screencast gets a few. Instead the
5
+ // recorder installs this clock before any of the page's scripts run, freezes
6
+ // it once the deck is ready, and then, per video frame, advances it by exactly
7
+ // 1/fps and takes a screenshot. While frozen:
8
+ // - performance.now() and Date.now() read the page clock;
9
+ // - setTimeout/setInterval fire when the clock passes their time, in order;
10
+ // - requestAnimationFrame callbacks run once per step, with the clock's time;
11
+ // - Web Animations (CSS animations and transitions, element.animate()) are
12
+ // paused and seeked to the clock, and finished when it passes their end
13
+ // (so `animation.finished` and transitionend still come).
14
+ // So a frame that takes the page 300 ms to draw is still one frame 33 ms after
15
+ // the last, and nothing is skipped or repeated. Before freeze() everything runs
16
+ // on real time, so loading (fonts, the head, clip downloads) is unaffected.
17
+ //
18
+ // Chromium's own virtual time (Emulation.setVirtualTimePolicy) was tried
19
+ // first: it moves timers and performance.now(), but not requestAnimationFrame
20
+ // (its callbacks keep their real-time stamps and run many times per step) or
21
+ // CSS animations, so the head and transitions would still run on wall time.
22
+ //
23
+ // installFrameClock is serialised into the page (addInitScript): it must not
24
+ // close over anything outside itself.
25
+ export function installFrameClock(win) {
26
+ if (win.__hdFrameClock) return win.__hdFrameClock;
27
+ const perf = win.performance;
28
+ const native = {
29
+ now: perf.now.bind(perf),
30
+ dateNow: win.Date.now.bind(win.Date),
31
+ setTimeout: win.setTimeout.bind(win),
32
+ clearTimeout: win.clearTimeout.bind(win),
33
+ raf: win.requestAnimationFrame?.bind(win),
34
+ caf: win.cancelAnimationFrame?.bind(win),
35
+ };
36
+ let frozen = false, t = 0, seq = 0;
37
+ const timers = new Map(); // id → { due, fn, args, every, native }
38
+ const frames = new Map(); // id → { cb, native }
39
+ const tracked = new WeakMap(); // Animation → { origin: clock ms at its time 0, last: its time when last seen, ours }
40
+ let clock_takeVideos = () => {};
41
+ const now = () => (frozen ? t : native.now());
42
+ // A callback that throws must not stop the step: report it as the page would (a pageerror).
43
+ const report = (e) => native.setTimeout(() => { throw e; }, 0);
44
+ const call = (fn, args) => {
45
+ try { fn(...args); } catch (e) { report(e); }
46
+ };
47
+ const fire = (id) => {
48
+ const x = timers.get(id);
49
+ if (!x) return;
50
+ if (x.every == null) timers.delete(id);
51
+ else {
52
+ x.due += x.every;
53
+ if (!frozen) x.native = native.setTimeout(() => fire(id), x.every);
54
+ }
55
+ call(x.fn, x.args);
56
+ };
57
+ const schedule = (fn, ms, args, repeat) => {
58
+ // A string body is not run (the player uses none); a deck that does is told, as a page error.
59
+ if (typeof fn !== 'function') { report(new Error('the video recorder does not run a setTimeout/setInterval given a string')); return 0; }
60
+ const delay = Math.max(0, Number(ms) || 0);
61
+ const id = ++seq;
62
+ const x = { due: now() + delay, fn, args, every: repeat ? Math.max(1, delay) : null, native: null };
63
+ if (!frozen) x.native = native.setTimeout(() => fire(id), delay);
64
+ timers.set(id, x);
65
+ return id;
66
+ };
67
+ const unschedule = (id) => {
68
+ const x = timers.get(id);
69
+ if (!x) return;
70
+ if (x.native != null) native.clearTimeout(x.native);
71
+ timers.delete(id);
72
+ };
73
+ perf.now = now;
74
+ const dateOffset = native.dateNow() - native.now();
75
+ win.Date.now = () => (frozen ? Math.round(dateOffset + t) : native.dateNow());
76
+ win.setTimeout = (fn, ms, ...args) => schedule(fn, ms, args, false);
77
+ win.setInterval = (fn, ms, ...args) => schedule(fn, ms, args, true);
78
+ win.clearTimeout = unschedule;
79
+ win.clearInterval = unschedule;
80
+ win.requestAnimationFrame = (cb) => {
81
+ const id = ++seq;
82
+ const x = { cb, native: null };
83
+ if (!frozen && native.raf) x.native = native.raf((ts) => { frames.delete(id); call(cb, [ts]); });
84
+ frames.set(id, x);
85
+ return id;
86
+ };
87
+ win.cancelAnimationFrame = (id) => {
88
+ const x = frames.get(id);
89
+ if (!x) return;
90
+ if (x.native != null) native.caf?.(x.native);
91
+ frames.delete(id);
92
+ };
93
+
94
+ // Every running animation, paused and placed at the clock's time. One
95
+ // running when the clock froze keeps its place. One seen running later
96
+ // starts from where the clock last saw it (0 for a new or replayed one, its
97
+ // paused time for one the page paused and resumed), whatever real time
98
+ // passed before it was seen. One the page paused itself is left alone.
99
+ const animate = (atFreeze = false) => {
100
+ const doc = win.document;
101
+ if (typeof doc?.getAnimations !== 'function') return;
102
+ // getAnimations() also brings styles up to date, so a transition a class change just started is in it.
103
+ for (const a of doc.getAnimations()) {
104
+ const rate = a.playbackRate || 1;
105
+ let s = tracked.get(a);
106
+ if (a.playState === 'running') {
107
+ const from = atFreeze ? Number(a.currentTime) || 0 : s?.last ?? 0;
108
+ s = { origin: t - from / rate, last: from, ours: true };
109
+ tracked.set(a, s);
110
+ if (nativeAnim) nativeAnim.pause.call(a); else a.pause();
111
+ } else if (!s?.ours || a.playState !== 'paused') {
112
+ tracked.set(a, { ...s, ours: false, last: a.playState === 'finished' ? 0 : Number(a.currentTime) || 0 });
113
+ continue;
114
+ }
115
+ const at = (t - s.origin) * rate;
116
+ const end = a.effect?.getComputedTiming?.().endTime;
117
+ if (a.playbackRate > 0 && Number.isFinite(end) && at >= end) { a.finish(); s.ours = false; s.last = 0; }
118
+ else { a.currentTime = Math.max(0, at); s.last = Math.max(0, at); }
119
+ }
120
+ };
121
+
122
+ // The page pausing or cancelling an animation the clock has taken gives it
123
+ // back to the page (the clock pauses with the native method, not these).
124
+ const Anim = win.Animation?.prototype;
125
+ const nativeAnim = Anim && { pause: Anim.pause, cancel: Anim.cancel };
126
+ if (Anim) {
127
+ Anim.pause = function pause() { const s = tracked.get(this); if (s) s.ours = false; return nativeAnim.pause.call(this); };
128
+ Anim.cancel = function cancel() { const s = tracked.get(this); if (s) { s.ours = false; s.last = 0; } return nativeAnim.cancel.call(this); };
129
+ }
130
+
131
+ // Slide <video>s: while frozen, play() and pause() only say whether the page
132
+ // wants one playing, and settle() seeks each playing one to the clock (loop
133
+ // wraps; a clip that reaches its end stops and sends `ended`).
134
+ const Media = win.HTMLMediaElement?.prototype;
135
+ const isVideo = (m) => !!win.HTMLVideoElement && m instanceof win.HTMLVideoElement;
136
+ const videos = new Map(); // HTMLVideoElement → { playing, origin, base }
137
+ let nativeMedia = null;
138
+ if (Media) {
139
+ nativeMedia = { play: Media.play, pause: Media.pause, paused: Object.getOwnPropertyDescriptor(Media, 'paused')?.get };
140
+ const nativelyPlaying = (v) => nativeMedia.paused && !nativeMedia.paused.call(v);
141
+ const send = (v, type) => v.dispatchEvent(new Event(type));
142
+ const take = (v) => {
143
+ const s = { playing: true, origin: t, base: v.currentTime || 0 };
144
+ videos.set(v, s);
145
+ if (nativelyPlaying(v)) nativeMedia.pause.call(v);
146
+ return s;
147
+ };
148
+ Media.play = function play() {
149
+ if (!frozen || !isVideo(this)) return nativeMedia.play.call(this);
150
+ const s = videos.get(this);
151
+ if (!s?.playing) {
152
+ if (s?.ended) this.currentTime = 0;
153
+ // As a real play() does, start loading a video that has not (preload="none").
154
+ if (this.readyState === 0 && this.networkState !== 2) { this.preload = 'auto'; this.load(); }
155
+ take(this);
156
+ send(this, 'play');
157
+ send(this, 'playing');
158
+ }
159
+ return Promise.resolve();
160
+ };
161
+ Media.pause = function pause() {
162
+ if (!frozen || !isVideo(this)) return nativeMedia.pause.call(this);
163
+ const s = videos.get(this);
164
+ if (nativelyPlaying(this)) nativeMedia.pause.call(this);
165
+ else if (s?.playing) send(this, 'pause');
166
+ if (s) s.playing = false;
167
+ };
168
+ if (nativeMedia.paused) {
169
+ Object.defineProperty(Media, 'paused', {
170
+ configurable: true,
171
+ get() { const s = frozen && videos.get(this); return s ? !s.playing : nativeMedia.paused.call(this); },
172
+ });
173
+ }
174
+ clock_takeVideos = () => {
175
+ for (const v of win.document?.querySelectorAll?.('video') ?? []) {
176
+ if (nativelyPlaying(v)) { const s = videos.get(v); if (!s?.playing) take(v); else nativeMedia.pause.call(v); }
177
+ }
178
+ };
179
+ }
180
+ // Seeks every playing video to the clock; resolves when they show it. A
181
+ // video whose seek does not finish within 5 s (a host that stopped answering,
182
+ // a file that cannot seek) is left where it is from then on and reported, so
183
+ // one bad clip costs one wait, not one per frame.
184
+ const seekVideos = () => {
185
+ const waits = [];
186
+ for (const [v, s] of videos) {
187
+ if (!s.playing || s.stuck || !v.isConnected || v.readyState < 1 || v.error) continue;
188
+ let at = s.base + ((t - s.origin) / 1000) * (v.playbackRate || 1);
189
+ const d = v.duration;
190
+ let ended = false;
191
+ if (Number.isFinite(d) && d > 0) {
192
+ if (v.loop) at %= d;
193
+ else if (at >= d) { at = d; ended = true; }
194
+ }
195
+ // A hair past the frame's time, so rounding never lands it on the frame before.
196
+ if (!ended) at = Number.isFinite(d) ? Math.min(at + 0.001, d) : at + 0.001;
197
+ if (Math.abs(v.currentTime - at) > 0.0005) {
198
+ waits.push(new Promise((ok) => {
199
+ const done = () => { native.clearTimeout(timer); v.removeEventListener('seeked', done); v.removeEventListener('error', done); ok(); };
200
+ const timer = native.setTimeout(() => {
201
+ done();
202
+ s.stuck = true;
203
+ report(new Error(`a slide video stopped seeking (${String(v.currentSrc || v.src).slice(0, 120)}); it holds its frame for the rest of the video`));
204
+ }, 5000);
205
+ v.addEventListener('seeked', done);
206
+ v.addEventListener('error', done);
207
+ }));
208
+ v.currentTime = at;
209
+ }
210
+ if (ended) {
211
+ s.playing = false;
212
+ s.ended = true;
213
+ v.dispatchEvent(new Event('pause'));
214
+ v.dispatchEvent(new Event('ended'));
215
+ }
216
+ }
217
+ return Promise.all(waits);
218
+ };
219
+
220
+ const clock = {
221
+ get frozen() { return frozen; },
222
+ // From now on the page's time moves only with step().
223
+ freeze() {
224
+ if (frozen) return t;
225
+ t = native.now();
226
+ frozen = true;
227
+ for (const x of timers.values()) if (x.native != null) { native.clearTimeout(x.native); x.native = null; }
228
+ for (const x of frames.values()) if (x.native != null) { native.caf?.(x.native); x.native = null; }
229
+ clock_takeVideos();
230
+ animate(true);
231
+ return t;
232
+ },
233
+ // Advances the clock `ms`: due timers fire in time order (each at its own
234
+ // time), then this frame's requestAnimationFrame callbacks run. Returns the
235
+ // new time. settle() then finishes the frame.
236
+ step(ms) {
237
+ if (!frozen) throw new Error('the frame clock is not frozen');
238
+ const target = t + Math.max(0, Number(ms) || 0);
239
+ // A timer that keeps re-arming itself at 0 ms must not hang the step: it is reported, as a page error.
240
+ for (let guard = 0; ; guard++) {
241
+ let next = null, nextId = 0;
242
+ for (const [id, x] of timers) if (x.due <= target && (!next || x.due < next.due || (x.due === next.due && id < nextId))) { next = x; nextId = id; }
243
+ if (!next) break;
244
+ if (guard === 100000) { report(new Error('a timer re-armed itself 100000 times in one video frame; the rest wait for the next frame')); break; }
245
+ t = Math.max(t, next.due);
246
+ fire(nextId);
247
+ }
248
+ t = target;
249
+ const due = [...frames.values()];
250
+ frames.clear();
251
+ for (const x of due) call(x.cb, [t]);
252
+ return t;
253
+ },
254
+ // Lets what the step set off finish (promise continuations: a clip's
255
+ // `playing`, a class it adds), then places every animation and video at the
256
+ // clock's time and waits for the videos to show it. Call after each step,
257
+ // before the screenshot.
258
+ async settle() {
259
+ await new Promise((ok) => native.setTimeout(ok, 0));
260
+ clock_takeVideos();
261
+ animate();
262
+ await seekVideos();
263
+ },
264
+ now,
265
+ };
266
+ win.__hdFrameClock = clock;
267
+ return clock;
268
+ }
package/src/ops.mjs CHANGED
@@ -206,11 +206,13 @@ export async function generatePoster(opts, fields = {}) {
206
206
  // page after each with notes=true, a footer with the deck's tracked link) and
207
207
  // keep it 30 days. Returns { exportId, url, filename, bytes, pages, slides,
208
208
  // notes, link, expiresAt }. Waits for the file (about a second per slide).
209
+ // kind "mp4" queues a video on the render worker (convex/videoExports.ts) and
210
+ // returns { exportId, status: "queued" } at once: follow it with checkExport.
209
211
  export async function exportDeck(opts, fields = {}) {
210
212
  if (!fields.slug) throw new Error("slug is required");
211
213
  const kind = fields.kind ?? "pdf";
212
- if (kind === "mp4") throw new Error(videoFromCli(String(fields.slug)));
213
- if (kind !== "pdf") throw new Error(`kind "${kind}" is not supported: "pdf", or "mp4" from the CLI`);
214
+ if (kind === "mp4") return requestVideoExport(opts, fields);
215
+ if (kind !== "pdf") throw new Error(`kind "${kind}" is not supported: "pdf" or "mp4"`);
214
216
  const args = { slug: String(fields.slug), kind: "pdf" };
215
217
  if (fields.notes) args.notes = true;
216
218
  if (fields.script) args.script = String(fields.script);
@@ -218,10 +220,40 @@ export async function exportDeck(opts, fields = {}) {
218
220
  return (await client(opts)).action(api.exports.create, args);
219
221
  }
220
222
 
221
- // What export_deck { kind: "mp4" } answers (the same words as convex/exports.ts videoFromCli).
223
+ const VIDEO_OPTIONS = ["size", "shape", "captions", "avatar", "titleCard", "endCard"];
224
+ export async function requestVideoExport(opts, fields = {}) {
225
+ const slug = String(fields.slug);
226
+ for (const k of ["notes", "script", "shareToken"]) if (fields[k]) throw new Error(`${k} is a PDF option; a video has none`);
227
+ const options = {};
228
+ for (const k of VIDEO_OPTIONS) if (fields[k] !== undefined) options[k] = fields[k];
229
+ const c = await client(opts);
230
+ try {
231
+ const out = await c.mutation(api.videoExports.requestVideo, { slug, options });
232
+ return { ...out, next: `poll check_export { exportId: "${out.exportId}" } (or \`hyperdecks export status ${out.exportId}\`) every 20-30 s; a deck renders in about its own length` };
233
+ } catch (e) {
234
+ // A backend from before the render worker has no requestVideo.
235
+ if (/Could not find public function/i.test(errorText(e))) throw new Error(videoFromCli(slug));
236
+ throw e;
237
+ }
238
+ }
239
+
240
+ // One export by id (check_export / `export status`): status, a rendering
241
+ // video's progress { slide, slides, phase }, its place in line, and the
242
+ // download URL once ready.
243
+ export async function checkExport(opts, exportId) {
244
+ if (!exportId) throw new Error("exportId is required");
245
+ return (await client(opts)).query(api.exports.get, { exportId: String(exportId) });
246
+ }
247
+
248
+ export async function cancelExport(opts, exportId) {
249
+ if (!exportId) throw new Error("exportId is required");
250
+ return (await client(opts)).mutation(api.videoExports.cancel, { exportId: String(exportId) });
251
+ }
252
+
253
+ // What a video request answers where no render worker is set up (the same words as convex/exports.ts videoFromCli).
222
254
  export const videoFromCli = (slug) =>
223
- `video exports run from the CLI until the render worker ships: \`hyperdecks export ${slug} --video --out ${slug}.mp4\` ` +
224
- "records the narrated deck in real time (add --upload to keep the MP4 with the deck's exports, --shape 9:16 or 1:1, --size 720p or 4k).";
255
+ `video exports from the dashboard and chat are not set up on this server yet; record it with the CLI: \`hyperdecks export ${slug} --video --out ${slug}.mp4\` ` +
256
+ "records the narrated deck frame by frame, slower than real time (add --upload to keep the MP4 with the deck's exports, --shape 9:16 or 1:1, --size 720p or 4k).";
225
257
 
226
258
  // The released copy of a deck you own, for `export --video <slug>`: { slug, title, html }.
227
259
  // Owner only, as export_deck is: a video is the owner's to make.
@@ -12,12 +12,35 @@ const run = promisify(execFile);
12
12
  const require = createRequire(import.meta.url);
13
13
  const MIME = { '.html':'text/html', '.htm':'text/html', '.js':'text/javascript', '.mjs':'text/javascript', '.css':'text/css', '.json':'application/json', '.png':'image/png', '.jpg':'image/jpeg', '.jpeg':'image/jpeg', '.gif':'image/gif', '.webp':'image/webp', '.svg':'image/svg+xml', '.woff':'font/woff', '.woff2':'font/woff2', '.ttf':'font/ttf', '.otf':'font/otf', '.mp4':'video/mp4', '.webm':'video/webm', '.mp3':'audio/mpeg', '.wav':'audio/wav', '.wasm':'application/wasm', '.glb':'model/gltf-binary', '.gltf':'model/gltf+json', '.bin':'application/octet-stream' };
14
14
  const SETUP = 'Run `hyperdecks preview setup` (or `decks preview setup`) to install the matching isolated Chromium. On Linux install system libraries with `npx playwright install-deps chromium` using the Playwright version in your installed CLI.';
15
+ // Installs the Chromium this CLI's Playwright launches, into Playwright's shared
16
+ // cache. PLAYWRIGHT_SKIP_BROWSER_GC keeps the installer from pruning builds that
17
+ // other Playwright installs (a repo's own test runner) still use.
18
+ export async function installChromium({ exec = run } = {}) {
19
+ await exec(process.execPath, [join(dirname(require.resolve('playwright/package.json')), 'cli.js'), 'install', 'chromium'], {
20
+ timeout: 240000, maxBuffer: 4 * 1024 * 1024, env: { ...process.env, PLAYWRIGHT_SKIP_BROWSER_GC: '1' },
21
+ });
22
+ }
23
+ // Playwright's launch error when the browser build is not on disk.
24
+ export const isMissingBrowser = (e) => /Executable doesn't exist/.test(String(e?.message ?? e));
15
25
  export async function setupCapture(pos, opts) {
16
26
  if (pos.length || Object.keys(opts).some(k => !['json', 'dev', 'convex', 'prod'].includes(k))) throw new Error('usage: preview setup');
17
- await run(process.execPath, [join(dirname(require.resolve('playwright/package.json')), 'cli.js'), 'install', 'chromium'], { timeout: 240000, maxBuffer: 4 * 1024 * 1024 });
27
+ await installChromium();
18
28
  console.log(opts.json ? JSON.stringify({ installed: 'chromium' }) : 'Chromium installed. Preview capture is ready.');
19
29
  }
20
30
 
31
+ // One `bytes=a-b`, `bytes=a-` or `bytes=-n` range of a `size`-byte file:
32
+ // { start, end } (inclusive), undefined when there is no (usable) Range header
33
+ // (send it whole), null when it asks past the end (416).
34
+ export function byteRange(header, size) {
35
+ const m = /^bytes=(\d*)-(\d*)$/.exec(String(header ?? '').trim());
36
+ if (!m || (m[1] === '' && m[2] === '')) return undefined;
37
+ let start, end;
38
+ if (m[1] === '') { start = Math.max(0, size - Number(m[2])); end = size - 1; }
39
+ else { start = Number(m[1]); end = m[2] === '' ? size - 1 : Math.min(Number(m[2]), size - 1); }
40
+ if (start >= size || start > end) return null;
41
+ return { start, end };
42
+ }
43
+
21
44
  // Capability URL + realpath containment; captured code gets no general file server.
22
45
  // `html` alone serves that page; with `file` it replaces the page and the file's
23
46
  // folder still serves the rest (play's in-memory copy with the face on it).
@@ -34,8 +57,14 @@ export async function serveSource({ file, html, port = 0 }) {
34
57
  if (!root || rel.split('/').some(s => s.startsWith('.') || s.includes('\\')) || !MIME[extname(rel).toLowerCase()]) { res.writeHead(404).end(); return; }
35
58
  const full = await realpath(resolve(root, rel));
36
59
  if (!full.startsWith(root + sep) || !(await stat(full)).isFile()) { res.writeHead(404).end(); return; }
37
- res.writeHead(200, { 'Content-Type': MIME[extname(full).toLowerCase()] + (MIME[extname(full).toLowerCase()].startsWith('text/') ? '; charset=utf-8' : ''), 'Cache-Control':'no-store' });
38
- res.end(await readFile(full));
60
+ const type = MIME[extname(full).toLowerCase()] + (MIME[extname(full).toLowerCase()].startsWith('text/') ? '; charset=utf-8' : '');
61
+ const body = await readFile(full);
62
+ // A byte range, as any host answers one: a <video> can only seek with it (a frame-by-frame export seeks every frame).
63
+ const range = byteRange(req.headers.range, body.length);
64
+ if (range === null) { res.writeHead(416, { 'Content-Range': `bytes */${body.length}` }).end(); return; }
65
+ if (range) { res.writeHead(206, { 'Content-Type': type, 'Cache-Control':'no-store', 'Accept-Ranges':'bytes', 'Content-Range': `bytes ${range.start}-${range.end}/${body.length}` }).end(body.subarray(range.start, range.end + 1)); return; }
66
+ res.writeHead(200, { 'Content-Type': type, 'Cache-Control':'no-store', 'Accept-Ranges':'bytes' });
67
+ res.end(body);
39
68
  } catch { res.writeHead(404).end(); }
40
69
  });
41
70
  await new Promise((ok, fail) => { server.once('error', fail); server.listen(port, '127.0.0.1', ok); });
@@ -50,7 +79,9 @@ export function allowCaptureRequest(url, method, type, entryUrl, firstPartyOrigi
50
79
  if (type === 'document') return url === entryUrl;
51
80
  if (['websocket', 'eventsource', 'ping', 'other'].includes(type)) return false;
52
81
  if (firstPartyOrigins.some(v => new URL(v).origin === u.origin) || /\.convex\.(cloud|site)$/.test(u.hostname)) {
53
- return /^\/api\/storage\//.test(path);
82
+ // Stored assets, and the hosted player a player/v1 deck loads (player.js, its
83
+ // modules, css, emote bursts). Views (/v/) and telemetry stay refused above.
84
+ return /^\/(api\/storage|player)\//.test(path);
54
85
  }
55
86
  // Prevent page code from reaching local services or cloud metadata. Only the
56
87
  // capability file server is available on loopback; redirects are checked too.
package/src/runtime.mjs CHANGED
@@ -8,7 +8,7 @@ import { fileURLToPath } from "node:url";
8
8
  import { ConvexHttpClient } from "convex/browser";
9
9
  import { makeFunctionReference } from "convex/server";
10
10
 
11
- export const VERSION = "0.9.3";
11
+ export const VERSION = "0.9.4";
12
12
  const UPLOAD_TIMEOUT_MS = 125_000;
13
13
 
14
14
  export const PROD = {
@@ -135,6 +135,11 @@ export const api = {
135
135
  startVideoUpload: makeFunctionReference("exports:startVideoUpload"),
136
136
  finishVideoUpload: makeFunctionReference("exports:finishVideoUpload"),
137
137
  failVideoUpload: makeFunctionReference("exports:failVideoUpload"),
138
+ get: makeFunctionReference("exports:get"),
139
+ },
140
+ videoExports: {
141
+ requestVideo: makeFunctionReference("videoExports:requestVideo"),
142
+ cancel: makeFunctionReference("videoExports:cancel"),
138
143
  },
139
144
  review: {
140
145
  reviewDeck: makeFunctionReference("review:reviewDeck"),