cdp-toolkit 3.1.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,7 +54,7 @@ import { spawn } from "node:child_process";
54
54
  import { mkdir, rm, stat, writeFile } from "node:fs/promises";
55
55
  import { join } from "node:path";
56
56
  import { CdpError, openPage } from "../client.ts";
57
- import type { CdpConnection } from "../client.ts";
57
+ import type { PageConnection } from "../client.ts";
58
58
  import type { Target, TargetSelector } from "../types.ts";
59
59
 
60
60
  const ARTIFACT_DIR = process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
@@ -235,6 +235,8 @@ export interface EncoderChoice {
235
235
  codec: "hevc" | "h264";
236
236
  /** -tag:v value, set on HEVC so QuickTime will play the result. */
237
237
  tag?: "hvc1";
238
+ /** Use `-fps_mode vfr` (ffmpeg >=5.1) instead of `-vsync vfr`; set by the probe. */
239
+ fpsMode?: boolean;
238
240
  }
239
241
 
240
242
  /** Preference order: hardware HEVC, hardware H.264, software HEVC, software H.264. */
@@ -278,8 +280,38 @@ export function selectEncoder(encodersOutput: string): EncoderChoice {
278
280
  }
279
281
 
280
282
  /**
281
- * The exact ffmpeg argv. `-vsync vfr` (not `-fps_mode`, which needs ffmpeg
282
- * >=5.1 and would break Ubuntu 22.04's 4.4) passes the concat demuxer's
283
+ * Whether this ffmpeg takes `-fps_mode` (added in 5.1). `-vsync` was deprecated
284
+ * then and later removed, so a new ffmpeg fails with "Unrecognized option 'vsync'",
285
+ * while 4.4 (Ubuntu 22.04) only knows `-vsync`. Parses `ffmpeg -version`;
286
+ * a git snapshot ("ffmpeg version N-1234-g...") is treated as new. Returns
287
+ * `undefined` when the banner has no version it can read, so the caller can
288
+ * look further instead of guessing.
289
+ */
290
+ export function supportsFpsMode(versionOutput: string): boolean | undefined {
291
+ const m = /ffmpeg version n?(\d+)\.(\d+)/.exec(versionOutput);
292
+ if (!m) return /ffmpeg version N-/.test(versionOutput) ? true : undefined;
293
+ const major = Number(m[1]);
294
+ const minor = Number(m[2]);
295
+ return major > 5 || (major === 5 && minor >= 1);
296
+ }
297
+
298
+ /**
299
+ * Pick `-fps_mode` vs `-vsync` from what the probe could read. The version
300
+ * banner decides when it parses; otherwise the option list from `-h full`
301
+ * decides (it names `-fps_mode` only on ffmpeg that has it). When neither could
302
+ * be read, choose `-fps_mode`: it is the current option, while `-vsync` is gone
303
+ * from new ffmpeg, so an unreadable probe must not silently pick the removed one.
304
+ */
305
+ export function chooseFpsMode(versionOutput: string | undefined, helpOutput: string | undefined): boolean {
306
+ const byVersion = versionOutput === undefined ? undefined : supportsFpsMode(versionOutput);
307
+ if (byVersion !== undefined) return byVersion;
308
+ if (helpOutput !== undefined && /-vsync\b|-fps_mode\b/.test(helpOutput)) return /-fps_mode\b/.test(helpOutput);
309
+ return true;
310
+ }
311
+
312
+ /**
313
+ * The exact ffmpeg argv. `-fps_mode vfr` (or `-vsync vfr` on ffmpeg <5.1, see
314
+ * supportsFpsMode) passes the concat demuxer's
283
315
  * per-frame timestamps through instead of resampling them to a constant rate.
284
316
  * `-t` is deliberately NOT used to pin the duration: it trims at the LAST
285
317
  * frame's PTS, which throws away exactly the long final hold the ledger exists
@@ -293,7 +325,7 @@ export function buildFfmpegArgs(manifestPath: string, outPath: string, choice: E
293
325
  "-f", "concat",
294
326
  "-safe", "0",
295
327
  "-i", manifestPath,
296
- "-vsync", "vfr",
328
+ ...(choice.fpsMode ? ["-fps_mode", "vfr"] : ["-vsync", "vfr"]),
297
329
  "-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2",
298
330
  "-c:v", choice.encoder,
299
331
  ...(choice.tag ? ["-tag:v", choice.tag] : []),
@@ -352,7 +384,15 @@ async function probeEncoder(): Promise<EncoderChoice> {
352
384
  throw new CdpError(`${FFMPEG_MISSING} (${(err as Error).message})`);
353
385
  }
354
386
  if (res.code !== 0) throw new CdpError(`${FFMPEG_MISSING} ('-encoders' exited ${res.code}: ${res.stderr.trim().slice(0, 300)})`);
355
- return selectEncoder(`${res.stdout}\n${res.stderr}`);
387
+ const choice = selectEncoder(`${res.stdout}\n${res.stderr}`);
388
+ const output = async (args: string[]): Promise<string | undefined> => {
389
+ const r = await run(FFMPEG_BIN, args).catch(() => undefined);
390
+ return r && r.code === 0 ? `${r.stdout}\n${r.stderr}` : undefined;
391
+ };
392
+ const version = await output(["-version"]);
393
+ const byVersion = version === undefined ? undefined : supportsFpsMode(version);
394
+ const help = byVersion === undefined ? await output(["-hide_banner", "-h", "full"]) : undefined;
395
+ return { ...choice, fpsMode: chooseFpsMode(version, help) };
356
396
  })();
357
397
  // A failed probe must not poison every later call: forget it so a caller who
358
398
  // installs ffmpeg and retries is not told it is still missing.
@@ -367,7 +407,7 @@ async function probeEncoder(): Promise<EncoderChoice> {
367
407
 
368
408
  /** A live screencast held in-process between start and stop, keyed by targetId. */
369
409
  interface LiveRecording {
370
- conn: CdpConnection;
410
+ conn: PageConnection;
371
411
  target: Target;
372
412
  spoolDir: string;
373
413
  ledger: FrameLedgerEntry[];
@@ -8,7 +8,7 @@
8
8
  * `DOM.resolveNode({ backendNodeId: uid })`. There is no server-side ref
9
9
  * table to drift or expire.
10
10
  */
11
- import type { CdpConnection } from "../client.ts";
11
+ import type { PageConnection } from "../client.ts";
12
12
  import { withPage } from "../client.ts";
13
13
  import type { Target, TargetSelector, Uid } from "../types.ts";
14
14
 
@@ -175,7 +175,7 @@ export async function takeSnapshot(args: TakeSnapshotArgs = {}): Promise<TakeSna
175
175
  * Resolve a Uid (backendDOMNodeId) to a live JS object handle. Shared by every
176
176
  * interaction tool in input.ts. Throws if the node no longer exists.
177
177
  */
178
- export async function resolveUid(conn: CdpConnection, uid: Uid): Promise<{ objectId: string }> {
178
+ export async function resolveUid(conn: PageConnection, uid: Uid): Promise<{ objectId: string }> {
179
179
  const { object } = await conn.send<{ object: { objectId?: string } }>("DOM.resolveNode", {
180
180
  backendNodeId: uid,
181
181
  });
package/src/version.ts CHANGED
@@ -6,4 +6,4 @@
6
6
  * ready line; keeping it in its own module means a release bump touches one
7
7
  * line here plus package.json, not a const buried in the server.
8
8
  */
9
- export const VERSION = "3.1.0";
9
+ export const VERSION = "3.3.0";