@bendyline/squisq-cli 1.2.2 → 2.0.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/dist/api.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/api.ts","../src/util/detectFfmpeg.ts","../src/util/audioMix.ts","../src/registry.ts","../src/util/domPolyfill.ts","../src/util/readInput.ts"],"sourcesContent":["/**\n * Programmatic Video API\n *\n * Provides a library-style entry point for rendering Squisq documents to MP4\n * from Node.js callers (e.g., Qualla's pipeline). This avoids the need to shell\n * out to the `squisq video` CLI and gives callers full control over the Doc,\n * MemoryContentContainer, and encoding options.\n *\n * Orchestrates the full pipeline: Doc → render HTML → Playwright frame capture → FFmpeg encode.\n *\n * Usage:\n * import { renderDocToMp4 } from '@bendyline/squisq-cli/api';\n *\n * await renderDocToMp4(doc, container, {\n * outputPath: '/tmp/output.mp4',\n * fps: 30,\n * quality: 'normal',\n * orientation: 'landscape',\n * });\n */\n\nimport type { Doc } from '@bendyline/squisq/schemas';\nimport { resolveMediaSchedule } from '@bendyline/squisq/schemas';\nimport { flattenBlocks } from '@bendyline/squisq/doc';\nimport type { ContentContainer } from '@bendyline/squisq/storage';\nimport type { VideoQuality, VideoOrientation } from '@bendyline/squisq-video';\nimport { generateRenderHtml } from '@bendyline/squisq-video';\nimport { resolveDimensions } from '@bendyline/squisq-video';\nimport { convert as formatsConvert } from '@bendyline/squisq-formats';\nimport type {\n ConvertSource,\n ConvertOptions,\n ConversionResult,\n FormatId,\n} from '@bendyline/squisq-formats';\nimport { detectFfmpeg } from './util/detectFfmpeg.js';\nimport { buildMixedAudioTrack } from './util/audioMix.js';\nimport { createCliRegistry } from './registry.js';\n\n// Re-export utility types and functions callers may need\nexport type { VideoQuality, VideoOrientation } from '@bendyline/squisq-video';\nexport { MemoryContentContainer } from '@bendyline/squisq/storage';\nexport { readInput } from './util/readInput.js';\nexport type { ReadInputResult } from './util/readInput.js';\n\n// ── Format registry / convert() surface ───────────────────────────\n// Re-export the CLI's format registry factory plus the registry types and the\n// structured error, so `@bendyline/squisq-cli/api` is a one-stop programmatic\n// front door for both video rendering and document conversion.\nexport { createCliRegistry } from './registry.js';\nexport { ConversionError } from '@bendyline/squisq-formats';\nexport type {\n ConvertSource,\n ConvertOptions,\n ConversionResult,\n FormatId,\n FormatRegistry,\n FormatDefinition,\n NormalizedInput,\n ConversionErrorCode,\n ConversionErrorOptions,\n} from '@bendyline/squisq-formats';\n\n/**\n * Convert a document to a target format using the CLI's format registry.\n *\n * This is a thin, pre-bound wrapper over `convert()` from\n * `@bendyline/squisq-formats`: it injects the CLI registry (which adds the\n * `mp4` format on top of every built-in exporter) and a default\n * `resolvePlayerScript` that lazily loads the standalone player IIFE bundle\n * (required for HTML/EPUB-style exports). Callers may override either via\n * `options`.\n *\n * @param source - A bytes / markdown / doc {@link ConvertSource}.\n * @param to - Target format id (`docx`, `pdf`, `pptx`, `html`, `mp4`, …).\n * @param options - Conversion options; `registry` and `resolvePlayerScript`\n * default to the CLI's values but can be overridden.\n * @returns The encoded bytes plus mime type, suggested filename, and warnings.\n * @throws {@link ConversionError} on any failure, with a stable `code`.\n */\nexport async function convert(\n source: ConvertSource,\n to: FormatId,\n options: ConvertOptions = {},\n): Promise<ConversionResult> {\n return formatsConvert(source, to, {\n registry: createCliRegistry(),\n resolvePlayerScript: () =>\n import('@bendyline/squisq-react/standalone-source').then((m) => m.PLAYER_BUNDLE),\n ...options,\n });\n}\n\n/** Options for renderDocToMp4. */\nexport interface RenderDocToMp4Options {\n /** Output file path for the MP4. */\n outputPath: string;\n\n /** Frames per second (default: 30). */\n fps?: number;\n\n /** Encoding quality preset (default: 'normal'). */\n quality?: VideoQuality;\n\n /** Video orientation (default: 'landscape'). */\n orientation?: VideoOrientation;\n\n /** Override video width in pixels. */\n width?: number;\n\n /** Override video height in pixels. */\n height?: number;\n\n /** Caption style to bake into the video (default: none). */\n captionStyle?: 'standard' | 'social';\n\n /**\n * Seconds of cover-slide pre-roll before the story starts (default: 0).\n *\n * Note: the `squisq video` CLI defaults its `--cover-preroll` flag to 2\n * seconds; this programmatic API deliberately defaults to 0 so library\n * callers get exactly the duration they ask for.\n */\n coverPreRoll?: number;\n\n /**\n * Progress callback. Called with a phase name and 0-100 percentage.\n */\n onProgress?: (phase: string, percent: number) => void;\n}\n\n/** Result returned by renderDocToMp4. */\nexport interface RenderDocToMp4Result {\n /** Duration of the rendered video in seconds (including pre-roll). */\n duration: number;\n\n /** Number of frames captured. */\n frameCount: number;\n\n /** Output file path. */\n outputPath: string;\n}\n\n/**\n * Render a Doc + media container to an MP4 video file.\n *\n * The container should contain audio and image files referenced by the Doc's\n * audio.segments[].src and block image paths. Files are embedded as base64\n * data URIs in a self-contained render HTML page.\n *\n * Requires:\n * - Playwright (chromium) — for headless frame capture\n * - FFmpeg — for video encoding (resolved from SQUISQ_FFMPEG, PATH, or an\n * optionally installed `ffmpeg-static` package — see detectFfmpeg)\n *\n * @param doc - The Doc structure to render\n * @param container - MemoryContentContainer with audio/image files\n * @param options - Rendering and encoding options\n * @returns Result with duration and frame count\n */\nexport async function renderDocToMp4(\n doc: Doc,\n container: ContentContainer,\n options: RenderDocToMp4Options,\n): Promise<RenderDocToMp4Result> {\n const {\n outputPath,\n fps = 30,\n quality = 'normal',\n orientation = 'landscape',\n captionStyle,\n coverPreRoll = 0,\n onProgress,\n } = options;\n\n const dimensions = resolveDimensions({\n orientation,\n width: options.width,\n height: options.height,\n });\n\n // Detect ffmpeg early — needed for audio concat and video encoding\n const ffmpegPath = await detectFfmpeg();\n if (!ffmpegPath) {\n throw new Error(\n 'ffmpeg is required but not found in PATH.\\n' +\n 'Install it with:\\n' +\n ' macOS: brew install ffmpeg\\n' +\n ' Ubuntu: sudo apt install ffmpeg\\n' +\n ' Windows: winget install ffmpeg\\n' +\n 'Or: npm install ffmpeg-static, or set SQUISQ_FFMPEG to an ffmpeg binary.',\n );\n }\n\n onProgress?.('collecting media', 0);\n\n // ── Collect images from container ───────────────────────────────\n const { collectImagePaths } = await import('@bendyline/squisq-formats/html');\n const imagePaths = collectImagePaths(doc);\n const images = new Map<string, ArrayBuffer>();\n for (const imgPath of imagePaths) {\n const data = await container.readFile(imgPath);\n if (data) {\n images.set(imgPath, data);\n }\n }\n\n // ── Collect audio segments for the render HTML ──────────────────\n // The player page loads narration so the doc's duration/timing resolves; the\n // captured frames themselves are silent (audio is reconstructed offline from\n // the timeline below). Keyed by both src and name so the inline provider\n // resolves either reference.\n const audio = new Map<string, ArrayBuffer>();\n if (doc.audio?.segments?.length) {\n for (const seg of doc.audio.segments) {\n const data = await container.readFile(seg.src);\n if (data) {\n audio.set(seg.src, data);\n audio.set(seg.name, data);\n }\n }\n }\n\n // ── Collect timed-media clips + block video sources ─────────────\n // Every scheduled clip (block.media + doc.documentMedia) and every\n // template-produced VideoLayer needs its bytes embedded so the headless\n // page can load them with no network. They resolve through the inline\n // media provider's `images` map (which infers mp4/mp3 MIME by extension).\n const mediaSrcs = new Set<string>(resolveMediaSchedule(doc).map((c) => c.src));\n for (const block of flattenBlocks(doc.blocks)) {\n for (const layer of block.layers ?? []) {\n if (layer.type === 'video') mediaSrcs.add(layer.content.src);\n }\n }\n for (const src of mediaSrcs) {\n if (images.has(src)) continue;\n const data = await container.readFile(src);\n if (data) images.set(src, data);\n }\n\n onProgress?.('generating render HTML', 10);\n\n // ── Generate self-contained render HTML ─────────────────────────\n const { PLAYER_BUNDLE } = await import('@bendyline/squisq-react/standalone-source');\n const renderHtml = generateRenderHtml(doc, {\n playerScript: PLAYER_BUNDLE,\n images,\n audio: audio.size > 0 ? audio : undefined,\n width: dimensions.width,\n height: dimensions.height,\n captionStyle,\n });\n\n onProgress?.('launching browser', 15);\n\n // ── Playwright frame capture ────────────────────────────────────\n const { chromium } = await import('playwright-core');\n let browser: import('playwright-core').Browser;\n try {\n browser = await chromium.launch({ headless: true });\n } catch (err: unknown) {\n const detail = err instanceof Error ? err.message.split('\\n')[0] : String(err);\n throw new Error(\n 'Playwright Chromium is not installed. Run: npx playwright install chromium\\n' +\n `(launch failed: ${detail})`,\n );\n }\n const page = await browser.newPage({\n viewport: { width: dimensions.width, height: dimensions.height },\n });\n\n const pageErrors: string[] = [];\n page.on('pageerror', (err) => pageErrors.push(err.message));\n\n await page.setContent(renderHtml, { waitUntil: 'load' });\n await page.waitForTimeout(500);\n\n try {\n await page.waitForFunction(\n () => typeof (window as unknown as Record<string, unknown>).getDuration === 'function',\n { timeout: 15000 },\n );\n } catch {\n await browser.close();\n const errorDetail = pageErrors.length\n ? `\\nPage errors:\\n ${pageErrors.join('\\n ')}`\n : '\\nNo page errors captured — the player may have failed to mount.';\n throw new Error(\n `The standalone player failed to boot in headless Chromium. ` +\n `Render API did not initialize within 15 seconds.${errorDetail}`,\n );\n }\n\n const docDuration: number = await page.evaluate(() => {\n return (window as unknown as { getDuration: () => number }).getDuration();\n });\n\n if (docDuration <= 0) {\n await browser.close();\n throw new Error('Document has zero duration — nothing to render');\n }\n\n const storyFrameCount = Math.ceil(docDuration * fps);\n const preRollFrameCount = Math.ceil(coverPreRoll * fps);\n const totalFrames = preRollFrameCount + storyFrameCount;\n const frames: Uint8Array[] = [];\n\n onProgress?.('capturing frames', 20);\n\n // Cover slide pre-roll (if requested)\n if (preRollFrameCount > 0) {\n const hasCover: boolean = await page.evaluate(() => {\n const w = window as unknown as { hasCoverBlock?: () => boolean };\n return typeof w.hasCoverBlock === 'function' ? w.hasCoverBlock() : false;\n });\n\n if (hasCover) {\n await page.evaluate(() => {\n (window as unknown as { showCover: () => void }).showCover();\n });\n await page.waitForTimeout(100);\n const coverFrame = new Uint8Array(await page.screenshot({ type: 'png' }));\n for (let i = 0; i < preRollFrameCount; i++) {\n frames.push(coverFrame);\n }\n await page.evaluate(() => {\n (window as unknown as { hideCover: () => void }).hideCover();\n });\n }\n }\n\n // Story frames via seekTo\n const frameInterval = 1 / fps;\n for (let i = 0; i < storyFrameCount; i++) {\n const time = i * frameInterval;\n await page.evaluate((t: number) => {\n return (window as unknown as { seekTo: (t: number) => Promise<void> }).seekTo(t);\n }, time);\n\n const screenshot = await page.screenshot({ type: 'png' });\n frames.push(new Uint8Array(screenshot));\n\n // Report progress: frames phase is 20% to 80%\n if (i % Math.max(1, Math.floor(fps / 2)) === 0 || i === storyFrameCount - 1) {\n const pct = 20 + Math.round((frames.length / totalFrames) * 60);\n onProgress?.('capturing frames', pct);\n }\n }\n\n await browser.close();\n\n onProgress?.('encoding video', 80);\n\n // Build the final audio track from the single-source-of-truth timeline:\n // narration segments laid sequentially + timed media clips at their absolute\n // positions, every start shifted by the cover pre-roll. `buildMixedAudioTrack`\n // is a pure consumer of `computeAudioTimeline`, so the CLI's placement can no\n // longer drift from the browser export (zero-duration narration segments are\n // skipped consistently in both).\n const encodingAudio = await buildMixedAudioTrack(doc, container, ffmpegPath, coverPreRoll);\n\n const { framesToMp4Native } = await import('./util/nativeEncoder.js');\n await framesToMp4Native(ffmpegPath, frames, encodingAudio, outputPath, {\n fps,\n quality,\n orientation,\n width: dimensions.width,\n height: dimensions.height,\n onProgress: (percent, phase) => {\n onProgress?.(`encoding: ${phase}`, 80 + Math.round(percent * 0.2));\n },\n });\n\n onProgress?.('done', 100);\n\n const totalDuration = docDuration + coverPreRoll;\n return {\n duration: totalDuration,\n frameCount: frames.length,\n outputPath,\n };\n}\n\n// ── Thumbnail extraction ──────────────────────────────────────────\n\n/** A thumbnail size specification. */\nexport interface ThumbnailSpec {\n /** Label for the thumbnail (used in filename: `{slug}-{width}x{height}.jpg`). */\n name: string;\n /** Output width in pixels. */\n width: number;\n /** Output height in pixels. */\n height: number;\n /** FFmpeg video filter string (e.g., 'scale=1280:720'). */\n filter: string;\n}\n\n/** Options for extractThumbnails. */\nexport interface ExtractThumbnailsOptions {\n /** Path to the source MP4 video. */\n videoPath: string;\n /** Directory to write thumbnails into. */\n outputDir: string;\n /** Base slug for filenames (produces `{slug}-{width}x{height}.jpg`). */\n slug: string;\n /** Thumbnail sizes to generate. */\n sizes: ThumbnailSpec[];\n /** Overwrite existing thumbnails (default: false). */\n force?: boolean;\n}\n\n/**\n * Extract thumbnail images from the first frame of an MP4 video.\n * Produces JPEG files at each specified size using FFmpeg video filters.\n */\nexport async function extractThumbnails(options: ExtractThumbnailsOptions): Promise<void> {\n const { videoPath, outputDir, slug, sizes, force } = options;\n const { existsSync } = await import('node:fs');\n const { join } = await import('node:path');\n const { execFile } = await import('node:child_process');\n\n const ffmpegPath = await detectFfmpeg();\n if (!ffmpegPath) {\n throw new Error(\n 'ffmpeg is required for thumbnail extraction but not found in PATH.\\n' +\n 'Install it with:\\n' +\n ' macOS: brew install ffmpeg\\n' +\n ' Ubuntu: sudo apt install ffmpeg\\n' +\n ' Windows: winget install ffmpeg',\n );\n }\n\n for (const thumb of sizes) {\n const outputPath = join(outputDir, `${slug}-${thumb.width}x${thumb.height}.jpg`);\n if (!force && existsSync(outputPath)) continue;\n\n await new Promise<void>((resolve, reject) => {\n execFile(\n ffmpegPath,\n ['-y', '-i', videoPath, '-vf', thumb.filter, '-frames:v', '1', '-q:v', '2', outputPath],\n { timeout: 30_000 },\n (err) => {\n if (err) reject(new Error(`Thumbnail extraction failed (${thumb.name}): ${err.message}`));\n else resolve();\n },\n );\n });\n }\n}\n","/**\n * detectFfmpeg\n *\n * Locates a native ffmpeg binary. Resolution order:\n * 1. `SQUISQ_FFMPEG` environment variable — verified by running\n * `<path> -version`. A value that is set but broken is a hard error\n * (never silently ignored), so misconfiguration surfaces immediately.\n * 2. System PATH lookup (`which` on macOS/Linux, `where` on Windows).\n * 3. The optional `ffmpeg-static` npm package, when the user has installed\n * it themselves. It is deliberately NOT a dependency of the CLI — the\n * import is opportunistic and falls through cleanly when absent.\n */\n\nimport { execFile } from 'node:child_process';\n\n/** Where a detected ffmpeg binary came from. */\nexport type FfmpegSource = 'env' | 'path' | 'ffmpeg-static';\n\n/** A successfully detected ffmpeg binary. */\nexport interface FfmpegDetection {\n /** Absolute path to the ffmpeg binary. */\n path: string;\n /** Which resolution step found it. */\n source: FfmpegSource;\n}\n\n/** Run a command and resolve with trimmed stdout, or null on any failure. */\nfunction run(command: string, args: string[]): Promise<string | null> {\n return new Promise((resolve) => {\n execFile(command, args, { timeout: 5000 }, (err, stdout) => {\n if (err || !stdout.trim()) {\n resolve(null);\n return;\n }\n resolve(stdout.trim());\n });\n });\n}\n\n/**\n * Run `<path> -version` and return the first line of output\n * (e.g. \"ffmpeg version 7.1 ...\"), or null when the binary fails to run.\n */\nexport async function getFfmpegVersion(path: string): Promise<string | null> {\n const out = await run(path, ['-version']);\n return out ? out.split('\\n')[0].trim() : null;\n}\n\n/**\n * Detect ffmpeg and report which source found it (see module doc for the\n * resolution order). Returns null when no working binary is found.\n *\n * @throws Error when `SQUISQ_FFMPEG` is set but the binary does not run —\n * an explicit override must never be silently ignored.\n */\nexport async function detectFfmpegDetailed(): Promise<FfmpegDetection | null> {\n // 1. Explicit override\n const envPath = process.env.SQUISQ_FFMPEG;\n if (envPath) {\n const version = await getFfmpegVersion(envPath);\n if (!version) {\n throw new Error(\n `SQUISQ_FFMPEG is set to \"${envPath}\" but running \"${envPath} -version\" failed. ` +\n 'Fix the path or unset SQUISQ_FFMPEG.',\n );\n }\n return { path: envPath, source: 'env' };\n }\n\n // 2. System PATH\n const command = process.platform === 'win32' ? 'where' : 'which';\n const found = await run(command, ['ffmpeg']);\n if (found) {\n // `which` and `where` can return multiple lines; take the first\n return { path: found.split('\\n')[0].trim(), source: 'path' };\n }\n\n // 3. Optional ffmpeg-static package. The specifier is a variable so\n // bundlers/TypeScript don't try to resolve a package we don't depend on.\n try {\n const specifier = 'ffmpeg-static';\n const mod = (await import(specifier)) as { default?: unknown } | string;\n // CJS default export shape (`module.exports = path`) surfaces as\n // `mod.default`; some build setups expose the string directly.\n const candidate =\n typeof mod === 'string' ? mod : typeof mod.default === 'string' ? mod.default : null;\n if (candidate) {\n return { path: candidate, source: 'ffmpeg-static' };\n }\n } catch {\n // Not installed — fall through.\n }\n\n return null;\n}\n\n/**\n * Detect whether native ffmpeg is available (SQUISQ_FFMPEG, PATH, or an\n * installed `ffmpeg-static` package).\n *\n * @returns Absolute path to ffmpeg binary, or null if not found\n * @throws Error when `SQUISQ_FFMPEG` is set but broken\n */\nexport async function detectFfmpeg(): Promise<string | null> {\n const detection = await detectFfmpegDetailed();\n return detection?.path ?? null;\n}\n","/**\n * audioMix — Build the MP4's final audio track from a doc's audio timeline.\n *\n * The schedule is produced by ONE source of truth: `computeAudioTimeline`\n * (from `@bendyline/squisq-video`), which flattens a doc's narration segments\n * (laid sequentially) and its timed media clips (placed at absolute positions\n * via `resolveMediaSchedule`) into a flat list of absolute-timed\n * `AudioTimelineClip`s. This CLI-side helper is a pure *consumer* of that\n * schedule: it loads each clip's bytes and assembles an ffmpeg `amix`\n * filtergraph where every clip is trimmed to its source window and delayed to\n * its absolute start.\n *\n * Because the browser MP4 export and this CLI mix now read the same timeline,\n * their audio placement can no longer drift. In particular, zero-duration\n * narration segments are skipped here exactly as they are in the browser\n * (`computeAudioTimeline` never emits a clip with `durationSec <= 0`).\n *\n * Frame capture stays silent — this reconstructs the audio offline from the\n * same schedule the renderer uses, keeping audio and video aligned.\n */\n\nimport type { Doc } from '@bendyline/squisq/schemas';\nimport type { ContentContainer } from '@bendyline/squisq/storage';\nimport { computeAudioTimeline } from '@bendyline/squisq-video';\nimport type { AudioTimelineClip } from '@bendyline/squisq-video';\n\n/**\n * Build the final mixed MP3 audio track for a doc's MP4 export.\n *\n * Drives the ffmpeg filtergraph entirely from `computeAudioTimeline(doc,\n * coverPreRoll)`: each clip's `startSec` → `adelay`, `sourceInSec` → `atrim`\n * start, `durationSec` → `atrim` duration, keyed by `src`. All clips are summed\n * with `amix` (`normalize=0` so individual levels are preserved).\n *\n * @param doc - The document whose audio to schedule.\n * @param container - Media container to read each clip's `src` bytes from.\n * @param ffmpegPath - Path to the ffmpeg binary.\n * @param coverPreRoll - Seconds of cover-slide pre-roll; shifts every clip's\n * start (matching the silent leading frames).\n * @returns The mixed MP3 bytes, or `null` when the doc has no audio to place.\n */\nexport async function buildMixedAudioTrack(\n doc: Doc,\n container: ContentContainer,\n ffmpegPath: string,\n coverPreRoll: number,\n): Promise<Uint8Array | null> {\n const timeline = computeAudioTimeline(doc, coverPreRoll);\n if (timeline.length === 0) return null;\n\n // Load each clip's bytes, caching reads by `src` (a src may back several clips).\n const bytesBySrc = new Map<string, ArrayBuffer | null>();\n const readSrc = async (src: string): Promise<ArrayBuffer | null> => {\n if (bytesBySrc.has(src)) return bytesBySrc.get(src)!;\n const data = await container.readFile(src);\n bytesBySrc.set(src, data ?? null);\n return data ?? null;\n };\n\n const usable: Array<{ clip: AudioTimelineClip; buffer: ArrayBuffer }> = [];\n for (const clip of timeline) {\n const buffer = await readSrc(clip.src);\n if (buffer) usable.push({ clip, buffer });\n }\n if (usable.length === 0) return null;\n\n return mixTimelineClips(ffmpegPath, usable);\n}\n\n/** Assemble the ffmpeg `amix` filtergraph and run it, returning the mixed MP3. */\nasync function mixTimelineClips(\n ffmpegPath: string,\n usable: Array<{ clip: AudioTimelineClip; buffer: ArrayBuffer }>,\n): Promise<Uint8Array> {\n const { writeFile, readFile, mkdir, rm } = await import('node:fs/promises');\n const { join } = await import('node:path');\n const { tmpdir } = await import('node:os');\n const { randomBytes } = await import('node:crypto');\n const { execFile } = await import('node:child_process');\n\n const workDir = join(tmpdir(), `squisq-audio-mix-${randomBytes(8).toString('hex')}`);\n await mkdir(workDir, { recursive: true });\n\n const ms = (s: number) => Math.max(0, Math.round(s * 1000));\n\n try {\n const inputs: string[] = [];\n const filters: string[] = [];\n const labels: string[] = [];\n\n for (const { clip, buffer } of usable) {\n const p = join(workDir, `clip-${inputs.length}.mp3`);\n await writeFile(p, new Uint8Array(buffer));\n const i = inputs.push(p) - 1;\n const delayMs = ms(clip.startSec);\n const start = Math.max(0, clip.sourceInSec);\n const end = start + Math.max(0, clip.durationSec);\n filters.push(\n `[${i}:a]atrim=start=${start}:end=${end},asetpts=PTS-STARTPTS,adelay=${delayMs}|${delayMs}[a${i}]`,\n );\n labels.push(`[a${i}]`);\n }\n\n const graph =\n `${filters.join(';')};${labels.join('')}` +\n `amix=inputs=${labels.length}:normalize=0:dropout_transition=0[aout]`;\n\n const outputPath = join(workDir, 'mixed.mp3');\n const args = ['-y'];\n for (const p of inputs) args.push('-i', p);\n args.push(\n '-filter_complex',\n graph,\n '-map',\n '[aout]',\n '-c:a',\n 'libmp3lame',\n '-b:a',\n '192k',\n outputPath,\n );\n\n await new Promise<void>((resolve, reject) => {\n execFile(ffmpegPath, args, { timeout: 180_000 }, (err) => {\n if (err) reject(new Error(`ffmpeg audio mix failed: ${err.message}`));\n else resolve();\n });\n });\n\n const data = await readFile(outputPath);\n return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);\n } finally {\n await rm(workDir, { recursive: true, force: true });\n }\n}\n","/**\n * CLI format registry.\n *\n * The CLI extends the formats package's default registry with an `mp4` format\n * so `convert()` can produce video the same way it produces DOCX/PDF/HTML. MP4\n * export is Node-only (it needs Playwright + FFmpeg), which is why it lives\n * here in the CLI rather than in the browser-pure formats package.\n */\n\nimport { randomBytes } from 'node:crypto';\nimport { readFile, rm } from 'node:fs/promises';\nimport { tmpdir } from 'node:os';\nimport { join } from 'node:path';\nimport { defaultRegistry } from '@bendyline/squisq-formats';\nimport type {\n ConversionResult,\n ConvertOptions,\n FormatDefinition,\n FormatRegistry,\n NormalizedInput,\n} from '@bendyline/squisq-formats';\nimport type { VideoOrientation, VideoQuality } from '@bendyline/squisq-video';\nimport { renderDocToMp4 } from './api.js';\n\n/** Sensible defaults for `convert(..., 'mp4')` — a full-quality landscape clip. */\nconst MP4_DEFAULTS = {\n fps: 30,\n quality: 'normal' as VideoQuality,\n orientation: 'landscape' as VideoOrientation,\n coverPreRoll: 0,\n} as const;\n\n/**\n * The `mp4` {@link FormatDefinition}.\n *\n * Export-only: it renders the normalized Doc to a temporary MP4 on disk via\n * {@link renderDocToMp4}, reads the produced file back into a `Uint8Array`, and\n * always deletes the temp file (even on failure). Per-render knobs (fps,\n * quality, orientation, coverPreRoll) can be supplied through\n * `options.formatOptions.mp4`; otherwise {@link MP4_DEFAULTS} apply.\n */\nfunction mp4Format(): FormatDefinition {\n return {\n id: 'mp4',\n label: 'MP4 Video',\n mimeType: 'video/mp4',\n extensions: ['.mp4'],\n async exportDoc(input: NormalizedInput, options: ConvertOptions): Promise<ConversionResult> {\n // `renderDocToMp4` is imported statically from api.ts. api.ts imports\n // `createCliRegistry` from this module, forming an ES module cycle — but\n // both are hoisted function exports used only at call time, so the cycle\n // resolves cleanly.\n const mp4Opts = options.formatOptions?.mp4 ?? {};\n const fps = typeof mp4Opts.fps === 'number' ? mp4Opts.fps : MP4_DEFAULTS.fps;\n const quality =\n typeof mp4Opts.quality === 'string'\n ? (mp4Opts.quality as VideoQuality)\n : MP4_DEFAULTS.quality;\n const orientation =\n typeof mp4Opts.orientation === 'string'\n ? (mp4Opts.orientation as VideoOrientation)\n : MP4_DEFAULTS.orientation;\n const coverPreRoll =\n typeof mp4Opts.coverPreRoll === 'number' ? mp4Opts.coverPreRoll : MP4_DEFAULTS.coverPreRoll;\n\n const outputPath = join(tmpdir(), `squisq-mp4-${randomBytes(8).toString('hex')}.mp4`);\n try {\n await renderDocToMp4(input.doc, input.container, {\n outputPath,\n fps,\n quality,\n orientation,\n coverPreRoll,\n });\n const data = await readFile(outputPath);\n const bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);\n return { bytes, mimeType: 'video/mp4', suggestedFilename: '', warnings: [] };\n } finally {\n await rm(outputPath, { force: true });\n }\n },\n };\n}\n\n/**\n * Build the CLI's format registry: every built-in format from\n * `@bendyline/squisq-formats` plus the CLI-only `mp4` exporter.\n */\nexport function createCliRegistry(): FormatRegistry {\n const registry = defaultRegistry();\n registry.register(mp4Format());\n return registry;\n}\n","/**\n * DOMParser polyfill for Node.\n *\n * The formats package is browser-pure: its shared OOXML reader\n * (`ooxml/reader.ts`, used by DOCX/PPTX/XLSX import) parses XML with the global\n * `DOMParser`, which browsers and jsdom provide but bare Node does not. The CLI\n * runs in Node, so importing this module for its side effect installs a\n * `@xmldom/xmldom`-backed `DOMParser` on `globalThis` when one is missing.\n *\n * The install is idempotent and gated on absence, so it is a no-op in a browser\n * or once the formats package no longer needs a global parser.\n */\n\nimport { DOMParser as XmldomDOMParser } from '@xmldom/xmldom';\n\nconst globalScope = globalThis as { DOMParser?: unknown };\n\nif (typeof globalScope.DOMParser === 'undefined') {\n // @xmldom/xmldom's DOMParser implements the DOM Level 2 surface the OOXML\n // reader relies on (getElementsByTagName[NS], getAttribute, localName,\n // textContent). Its constructor signature is compatible with the browser's.\n globalScope.DOMParser = XmldomDOMParser as unknown as typeof DOMParser;\n}\n","/**\n * readInput\n *\n * Unified input reader for the CLI. Accepts a path to a markdown file, a JSON\n * Doc file, a `.zip`/`.dbk` container, a folder, or any importable binary\n * format (`.docx`/`.pptx`/`.pdf`/`.xlsx`/`.csv`/`.html`/`.htm`) and always\n * resolves it to a fully-populated {@link ReadInputResult}: a `Doc`, a\n * `ContentContainer` holding the document + any bundled media, the parsed\n * `MarkdownDocument` when the source was markdown-shaped, and the detected\n * `sourceFormat`.\n *\n * Binary formats are dispatched through the shared format registry's importer\n * (preferring `importContainer`, falling back to `importDoc`); every path\n * derives the `Doc` via `markdownToDoc()` when the source is markdown-shaped.\n */\n\nimport './domPolyfill.js';\nimport { readFile, readdir, stat } from 'node:fs/promises';\nimport { join, extname } from 'node:path';\nimport { parseMarkdown, stringifyMarkdown } from '@bendyline/squisq/markdown';\nimport type { MarkdownDocument } from '@bendyline/squisq/markdown';\nimport { markdownToDoc } from '@bendyline/squisq/doc';\nimport type { Doc } from '@bendyline/squisq/schemas';\nimport type { ContentContainer } from '@bendyline/squisq/storage';\nimport { MemoryContentContainer } from '@bendyline/squisq/storage';\nimport { zipToContainer } from '@bendyline/squisq-formats/container';\nimport { defaultRegistry } from '@bendyline/squisq-formats';\nimport type { FormatId } from '@bendyline/squisq-formats';\n\nexport interface ReadInputResult {\n /** The resolved document. Always present. */\n doc: Doc;\n /** Virtual file system holding the document plus any bundled media. */\n container: ContentContainer;\n /**\n * The parsed markdown document. Present when the source was markdown-shaped\n * (`.md`, container/folder markdown, or a binary format imported as\n * markdown); absent for JSON-Doc input.\n */\n markdownDoc?: MarkdownDocument;\n /** Detected source format id (registry id, or `md`/`json`/`dbk`/`folder`). */\n sourceFormat: FormatId;\n}\n\n/** MIME type lookup by extension (common content types) */\nconst MIME_TYPES: Record<string, string> = {\n '.md': 'text/markdown',\n '.txt': 'text/plain',\n '.json': 'application/json',\n '.jpg': 'image/jpeg',\n '.jpeg': 'image/jpeg',\n '.png': 'image/png',\n '.gif': 'image/gif',\n '.webp': 'image/webp',\n '.svg': 'image/svg+xml',\n '.mp3': 'audio/mpeg',\n '.wav': 'audio/wav',\n '.ogg': 'audio/ogg',\n '.mp4': 'video/mp4',\n '.webm': 'video/webm',\n};\n\n/**\n * Binary extensions dispatched through the registry importer. `.md`, `.zip`,\n * `.dbk`, and `.json` are intentionally excluded — they have dedicated paths\n * that also detect bundled media / doc.json.\n */\nconst IMPORTER_EXTS = ['.docx', '.pptx', '.pdf', '.xlsx', '.csv', '.html', '.htm'];\n\nfunction mimeFromExt(filePath: string): string {\n return MIME_TYPES[extname(filePath).toLowerCase()] ?? 'application/octet-stream';\n}\n\n/**\n * Recursively walk a directory and return all file paths (relative to root).\n */\nasync function walkDir(root: string, prefix = ''): Promise<string[]> {\n const entries = await readdir(root, { withFileTypes: true });\n const paths: string[] = [];\n\n for (const entry of entries) {\n const relPath = prefix ? `${prefix}/${entry.name}` : entry.name;\n if (entry.isDirectory()) {\n paths.push(...(await walkDir(join(root, entry.name), relPath)));\n } else if (entry.isFile()) {\n paths.push(relPath);\n }\n }\n\n return paths;\n}\n\n/**\n * Read input from a file path (markdown, JSON Doc, ZIP/DBK container, folder,\n * or an importable binary format) and resolve it to a populated\n * {@link ReadInputResult}.\n */\nexport async function readInput(inputPath: string): Promise<ReadInputResult> {\n const info = await stat(inputPath);\n\n if (info.isDirectory()) {\n return readFolder(inputPath);\n }\n\n const ext = extname(inputPath).toLowerCase();\n if (ext === '.zip' || ext === '.dbk') {\n return readContainer(inputPath);\n }\n\n if (ext === '.json') {\n return readDocJsonFile(inputPath);\n }\n\n if (IMPORTER_EXTS.includes(ext)) {\n const def = defaultRegistry().byExtension(ext);\n if (def && (def.importContainer || def.importDoc)) {\n return readViaImporter(inputPath, def);\n }\n }\n\n // Default: treat as a markdown file\n return readMarkdownFile(inputPath);\n}\n\n/** Read a file into an ArrayBuffer (a fresh copy, not a Buffer view). */\nasync function readArrayBuffer(filePath: string): Promise<ArrayBuffer> {\n const data = await readFile(filePath);\n return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength) as ArrayBuffer;\n}\n\nasync function readMarkdownFile(filePath: string): Promise<ReadInputResult> {\n const content = await readFile(filePath, 'utf-8');\n const container = new MemoryContentContainer();\n await container.writeDocument(content);\n const markdownDoc = parseMarkdown(content);\n return { doc: markdownToDoc(markdownDoc), container, markdownDoc, sourceFormat: 'md' };\n}\n\n/**\n * Read a standalone Doc JSON file. The container is empty (no media bundled);\n * callers should populate it or set basePath for media resolution.\n */\nasync function readDocJsonFile(filePath: string): Promise<ReadInputResult> {\n const content = await readFile(filePath, 'utf-8');\n const doc = JSON.parse(content) as Doc;\n const container = new MemoryContentContainer();\n return { doc, container, sourceFormat: 'json' };\n}\n\n/**\n * Dispatch an importable binary format (docx/pptx/pdf/…) through the registry\n * importer. Prefers `importContainer` (extracts media alongside the markdown),\n * falling back to `importDoc` wrapped in a fresh container.\n */\nasync function readViaImporter(\n filePath: string,\n def: import('@bendyline/squisq-formats').FormatDefinition,\n): Promise<ReadInputResult> {\n const buffer = await readArrayBuffer(filePath);\n\n let container: ContentContainer;\n let markdownDoc: MarkdownDocument;\n if (def.importContainer) {\n container = await def.importContainer(buffer, {});\n const text = await container.readDocument();\n markdownDoc = text ? parseMarkdown(text) : { type: 'document', children: [] };\n } else {\n // importDoc is guaranteed present by the caller's guard.\n markdownDoc = await def.importDoc!(buffer, {});\n const mem = new MemoryContentContainer();\n await mem.writeDocument(stringifyMarkdown(markdownDoc));\n container = mem;\n }\n\n return { doc: markdownToDoc(markdownDoc), container, markdownDoc, sourceFormat: def.id };\n}\n\n/**\n * Known filenames for Doc JSON inside containers and folders.\n * Checked in priority order before falling back to markdown discovery.\n */\nconst DOC_JSON_NAMES = ['doc.json', 'story.json'];\n\n/** Resolve a container that may hold either a doc.json or a markdown document. */\nasync function resolveContainer(\n container: ContentContainer,\n sourceFormat: FormatId,\n missingMessage: string,\n): Promise<ReadInputResult> {\n // Check for Doc JSON first.\n for (const name of DOC_JSON_NAMES) {\n const jsonData = await container.readFile(name);\n if (jsonData) {\n const doc = JSON.parse(new TextDecoder().decode(jsonData)) as Doc;\n return { doc, container, sourceFormat };\n }\n }\n\n const markdown = await container.readDocument();\n if (!markdown) {\n throw new Error(missingMessage);\n }\n\n const markdownDoc = parseMarkdown(markdown);\n return { doc: markdownToDoc(markdownDoc), container, markdownDoc, sourceFormat };\n}\n\nasync function readContainer(filePath: string): Promise<ReadInputResult> {\n const container = await zipToContainer(await readArrayBuffer(filePath));\n return resolveContainer(\n container,\n 'dbk',\n `No markdown document or doc.json found in container: ${filePath}`,\n );\n}\n\nasync function readFolder(dirPath: string): Promise<ReadInputResult> {\n const container = new MemoryContentContainer();\n const files = await walkDir(dirPath);\n\n for (const relPath of files) {\n const absPath = join(dirPath, relPath);\n const data = await readFile(absPath);\n await container.writeFile(\n relPath,\n new Uint8Array(data.buffer, data.byteOffset, data.byteLength),\n mimeFromExt(relPath),\n );\n }\n\n return resolveContainer(\n container,\n 'folder',\n `No markdown document or doc.json found in folder: ${dirPath}`,\n );\n}\n"],"mappings":";AAsBA,SAAS,4BAA4B;AACrC,SAAS,qBAAqB;AAG9B,SAAS,0BAA0B;AACnC,SAAS,yBAAyB;AAClC,SAAS,WAAW,sBAAsB;;;ACf1C,SAAS,gBAAg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AAAA,IACnE;AAAA,EACF,CAAC;AAED,eAAa,QAAQ,GAAG;AAExB,QAAM,gBAAgB,cAAc;AACpC,SAAO;AAAA,IACL,UAAU;AAAA,IACV,YAAY,OAAO;AAAA,IACnB;AAAA,EACF;AACF;AAkCA,eAAsB,kBAAkB,SAAkD;AACxF,QAAM,EAAE,WAAW,WAAW,MAAM,OAAO,MAAM,IAAI;AACrD,QAAM,EAAE,WAAW,IAAI,MAAM,OAAO,IAAS;AAC7C,QAAM,EAAE,MAAAG,MAAK,IAAI,MAAM,OAAO,MAAW;AACzC,QAAM,EAAE,UAAAC,UAAS,IAAI,MAAM,OAAO,eAAoB;AAEtD,QAAM,aAAa,MAAM,aAAa;AACtC,MAAI,CAAC,YAAY;AACf,UAAM,IAAI;AAAA,MACR;AAAA,IAKF;AAAA,EACF;AAEA,aAAW,SAAS,OAAO;AACzB,UAAM,aAAaD,MAAK,WAAW,GAAG,IAAI,IAAI,MAAM,KAAK,IAAI,MAAM,MAAM,MAAM;AAC/E,QAAI,CAAC,SAAS,WAAW,UAAU,EAAG;AAEtC,UAAM,IAAI,QAAc,CAAC,SAAS,WAAW;AAC3C,MAAAC;AAAA,QACE;AAAA,QACA,CAAC,MAAM,MAAM,WAAW,OAAO,MAAM,QAAQ,aAAa,KAAK,QAAQ,KAAK,UAAU;AAAA,QACtF,EAAE,SAAS,IAAO;AAAA,QAClB,CAAC,QAAQ;AACP,cAAI,IAAK,QAAO,IAAI,MAAM,gCAAgC,MAAM,IAAI,MAAM,IAAI,OAAO,EAAE,CAAC;AAAA,cACnF,SAAQ;AAAA,QACf;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":["readFile","rm","join","tmpdir","randomBytes","execFile","MemoryContentContainer","readFile","join","defaultRegistry","join","execFile"]}
1
+ {"version":3,"sources":["../src/api.ts","../src/util/detectFfmpeg.ts","../src/util/audioMix.ts","../src/util/capturedFrameBudget.ts","../src/util/coverPreRoll.ts","../src/registry.ts","../src/util/readInput.ts"],"sourcesContent":["/**\n * Programmatic Rendered-Media API\n *\n * Provides library-style entry points for rendering Squisq documents to MP4\n * or animated GIF from Node.js callers (e.g., Qualla's pipeline). This avoids\n * the need to shell\n * out to the `squisq video` CLI and gives callers full control over the Doc,\n * MemoryContentContainer, and encoding options.\n *\n * Orchestrates the full pipeline: Doc → render HTML → Playwright frame capture → FFmpeg encode.\n *\n * Usage:\n * import { renderDocToMp4 } from '@bendyline/squisq-cli/api';\n *\n * await renderDocToMp4(doc, container, {\n * outputPath: '/tmp/output.mp4',\n * fps: 30,\n * quality: 'normal',\n * orientation: 'landscape',\n * });\n *\n * await renderDocToGif(doc, container, {\n * outputPath: '/tmp/output.gif',\n * animationsEnabled: false,\n * });\n */\n\nimport type { Doc } from '@bendyline/squisq/schemas';\nimport { resolveMediaSchedule } from '@bendyline/squisq/schemas';\nimport { flattenBlocks } from '@bendyline/squisq/doc';\nimport type { ContentContainer } from '@bendyline/squisq/storage';\nimport type { GifDither, VideoQuality, VideoOrientation } from '@bendyline/squisq-video';\nimport { ffmpegGifOutputArgs, generateRenderHtml } from '@bendyline/squisq-video';\nimport { resolveDimensions } from '@bendyline/squisq-video';\nimport {\n convert as formatsConvert,\n prepareConversion as formatsPrepareConversion,\n} from '@bendyline/squisq-formats';\nimport type {\n ConvertSource,\n ConvertOptions,\n ConversionResult,\n FormatId,\n PreparedConversion,\n} from '@bendyline/squisq-formats';\nimport { detectFfmpegDetailed } from './util/detectFfmpeg.js';\nimport { buildMixedAudioTrack } from './util/audioMix.js';\nimport { CapturedFrameCollector } from './util/capturedFrameBudget.js';\nimport { resolveAppliedCoverPreRoll } from './util/coverPreRoll.js';\nimport { GIF_EXPORT_DEFAULTS } from './util/nativeEncoder.js';\nimport { runFfmpeg } from './util/runFfmpeg.js';\nimport { createCliRegistry } from './registry.js';\nimport type { GifFormatOptions, Mp4FormatOptions } from './registry.js';\n\n// Re-export utility types and functions callers may need\nexport type { GifDither, VideoQuality, VideoOrientation } from '@bendyline/squisq-video';\nexport { MemoryContentContainer } from '@bendyline/squisq/storage';\nexport { readInput } from './util/readInput.js';\nexport type { ReadInputResult } from './util/readInput.js';\nexport { CapturedFrameBudgetError, MAX_CAPTURED_FRAME_BYTES } from './util/capturedFrameBudget.js';\nexport {\n GIF_EXPORT_DEFAULTS,\n framesToGifNative,\n framesToGifNativeBytes,\n framesToMp4Native,\n framesToMp4NativeBytes,\n} from './util/nativeEncoder.js';\nexport type { GifExportOptions, NativeVideoExportOptions } from './util/nativeEncoder.js';\nexport type { GifFormatOptions, Mp4FormatOptions } from './registry.js';\n\n/** Convert options with the CLI-only rendered-media adapters strongly typed. */\nexport type CliConvertOptions = Omit<ConvertOptions, 'formatOptions'> & {\n formatOptions?: ConvertOptions['formatOptions'] & {\n mp4?: Mp4FormatOptions;\n gif?: GifFormatOptions;\n };\n};\n\n// ── Format registry / convert() surface ───────────────────────────\n// Re-export the CLI's format registry factory plus the registry types and the\n// structured error, so `@bendyline/squisq-cli/api` is a one-stop programmatic\n// front door for both video rendering and document conversion.\nexport { createCliRegistry } from './registry.js';\nexport { ConversionError } from '@bendyline/squisq-formats';\nexport type {\n ConvertSource,\n ConvertOptions,\n ConversionResult,\n FormatId,\n FormatRegistry,\n FormatDefinition,\n NormalizedInput,\n ConversionErrorCode,\n ConversionErrorOptions,\n PreparedConversion,\n PreparedExportOptions,\n} from '@bendyline/squisq-formats';\n\n/**\n * Convert a document to a target format using the CLI's format registry.\n *\n * This is a thin, pre-bound wrapper over `convert()` from\n * `@bendyline/squisq-formats`: it injects the CLI registry (which adds the\n * `mp4` and `gif` formats on top of every built-in exporter) and a default\n * `resolvePlayerScript` that lazily loads the standalone player IIFE bundle\n * (required for HTML/EPUB-style exports). Callers may override either via\n * `options`.\n *\n * @param source - A bytes / markdown / doc {@link ConvertSource}.\n * @param to - Target format id (`docx`, `pdf`, `pptx`, `html`, `mp4`, `gif`, …).\n * @param options - Conversion options; `registry` and `resolvePlayerScript`\n * default to the CLI's values but can be overridden.\n * @returns The encoded bytes plus mime type, suggested filename, and warnings.\n * @throws {@link ConversionError} on any failure, with a stable `code`.\n */\nexport async function convert(\n source: ConvertSource,\n to: FormatId,\n options: CliConvertOptions = {},\n): Promise<ConversionResult> {\n return formatsConvert(source, to, {\n registry: createCliRegistry(),\n resolvePlayerScript: () =>\n import('@bendyline/squisq-react/standalone-source').then((m) => m.PLAYER_BUNDLE),\n ...options,\n });\n}\n\n/** Normalize and transform once using the CLI registry, then export one or more targets. */\nexport async function prepareConversion(\n source: ConvertSource,\n options: CliConvertOptions = {},\n): Promise<PreparedConversion> {\n return formatsPrepareConversion(source, {\n registry: createCliRegistry(),\n resolvePlayerScript: () =>\n import('@bendyline/squisq-react/standalone-source').then((m) => m.PLAYER_BUNDLE),\n ...options,\n });\n}\n\n/** Options for renderDocToMp4. */\nexport interface RenderDocToMp4Options {\n /** Cancel capture/encoding and terminate browser and FFmpeg work. */\n signal?: AbortSignal;\n /** Output file path for the MP4. */\n outputPath: string;\n\n /** Frames per second (default: 30). */\n fps?: number;\n\n /** Encoding quality preset (default: 'normal'). */\n quality?: VideoQuality;\n\n /** Video orientation (default: 'landscape'). */\n orientation?: VideoOrientation;\n\n /** Override video width in pixels. */\n width?: number;\n\n /** Override video height in pixels. */\n height?: number;\n\n /** Caption style to bake into the video (default: none). */\n captionStyle?: 'standard' | 'social';\n\n /** Render layer animations and block transitions (default: true). */\n animationsEnabled?: boolean;\n\n /**\n * Seconds of cover-slide pre-roll before the story starts (default: 0).\n *\n * Note: the `squisq video` CLI defaults its `--cover-preroll` flag to 2\n * seconds; this programmatic API deliberately defaults to 0 so library\n * callers get exactly the duration they ask for.\n */\n coverPreRoll?: number;\n\n /**\n * Progress callback. Called with a phase name and 0-100 percentage.\n */\n onProgress?: (phase: string, percent: number) => void;\n}\n\n/** Options for rendering a document to an animated GIF. */\nexport interface RenderDocToGifOptions {\n /** Cancel capture/encoding and terminate browser and FFmpeg work. */\n signal?: AbortSignal;\n /** Output file path for the GIF. */\n outputPath: string;\n /** Frames per second (default: 10). */\n fps?: number;\n /** Viewport orientation (default: landscape). */\n orientation?: VideoOrientation;\n /** Override output width (default: 960 landscape / 540 portrait). */\n width?: number;\n /** Override output height (default: 540 landscape / 960 portrait). */\n height?: number;\n /** Caption style to bake into the GIF (default: none). */\n captionStyle?: 'standard' | 'social';\n /** Seconds of cover-slide pre-roll (default: 0). */\n coverPreRoll?: number;\n /** Render layer animations and block transitions (default: false). */\n animationsEnabled?: boolean;\n /** Number of repeats; 0 loops forever and -1 disables looping. */\n loop?: number;\n /** Palette size, from 2 through 256 (default: 256). */\n maxColors?: number;\n /** Palette dithering algorithm (default: sierra2_4a). */\n dither?: GifDither;\n /** Bayer strength when dither is bayer (0-5, default: 3). */\n bayerScale?: number;\n /** Progress callback. */\n onProgress?: (phase: string, percent: number) => void;\n}\n\n/** Result returned by renderDocToMp4. */\nexport interface RenderDocToMp4Result {\n /** Duration of the rendered video in seconds (including pre-roll). */\n duration: number;\n\n /** Number of frames captured. */\n frameCount: number;\n\n /** Output file path. */\n outputPath: string;\n}\n\n/** Result returned by renderDocToGif. */\nexport interface RenderDocToGifResult extends RenderDocToMp4Result {\n /** Non-fatal fidelity warnings, including GIF's lack of audio. */\n warnings: string[];\n}\n\n/** Minimal standalone-player contract used inside Playwright's page realm. */\ninterface BrowserRenderAPI {\n seekTo(time: number): Promise<void>;\n getDuration(): number;\n hasCoverBlock(): boolean;\n showCover(): Promise<void>;\n hideCover(): Promise<void>;\n}\n\ninterface BrowserPlayerHandle {\n getRenderAPI(): BrowserRenderAPI | null;\n}\n\ninterface BrowserStandalonePlayer {\n getHandle(element: Element): BrowserPlayerHandle | undefined;\n}\n\ninterface SquisqBrowserWindow {\n SquisqPlayer?: BrowserStandalonePlayer;\n}\n\ninterface CaptureDocFramesOptions {\n signal?: AbortSignal;\n fps: number;\n width: number;\n height: number;\n captionStyle?: 'standard' | 'social';\n coverPreRoll: number;\n animationsEnabled: boolean;\n onProgress?: (phase: string, percent: number) => void;\n}\n\ninterface CapturedDocFrames {\n frames: Uint8Array[];\n totalDuration: number;\n appliedCoverPreRoll: number;\n ffmpegPath: string;\n}\n\n/** Capture deterministic PNG frames once, independent of the output encoder. */\nasync function captureDocFrames(\n doc: Doc,\n container: ContentContainer,\n options: CaptureDocFramesOptions,\n): Promise<CapturedDocFrames> {\n const { fps, width, height, captionStyle, coverPreRoll, animationsEnabled, onProgress, signal } =\n options;\n\n signal?.throwIfAborted();\n resolveAppliedCoverPreRoll(coverPreRoll, true);\n const ffmpegPath = (await detectFfmpegDetailed(signal))?.path ?? null;\n signal?.throwIfAborted();\n if (!ffmpegPath) {\n throw new Error(\n 'ffmpeg is required but not found in PATH.\\n' +\n 'Install it with:\\n' +\n ' macOS: brew install ffmpeg\\n' +\n ' Ubuntu: sudo apt install ffmpeg\\n' +\n ' Windows: winget install ffmpeg\\n' +\n 'Or: npm install ffmpeg-static, or set SQUISQ_FFMPEG to an ffmpeg binary.',\n );\n }\n\n onProgress?.('collecting media', 0);\n signal?.throwIfAborted();\n const { collectImagePaths } = await import('@bendyline/squisq-formats/html');\n signal?.throwIfAborted();\n const images = new Map<string, ArrayBuffer>();\n for (const imgPath of collectImagePaths(doc)) {\n signal?.throwIfAborted();\n const data = await container.readFile(imgPath);\n signal?.throwIfAborted();\n if (data) images.set(imgPath, data);\n }\n\n // Audio remains available to the headless player even for silent GIF output:\n // narration metadata and media duration still define the visual timeline.\n const audio = new Map<string, ArrayBuffer>();\n for (const seg of doc.audio?.segments ?? []) {\n signal?.throwIfAborted();\n const data = await container.readFile(seg.src);\n signal?.throwIfAborted();\n if (data) {\n audio.set(seg.src, data);\n audio.set(seg.name, data);\n }\n }\n\n const mediaSrcs = new Set<string>(resolveMediaSchedule(doc).map((clip) => clip.src));\n for (const block of flattenBlocks(doc.blocks)) {\n for (const layer of block.layers ?? []) {\n if (layer.type === 'video') mediaSrcs.add(layer.content.src);\n }\n }\n for (const src of mediaSrcs) {\n signal?.throwIfAborted();\n if (images.has(src)) continue;\n const data = await container.readFile(src);\n signal?.throwIfAborted();\n if (data) images.set(src, data);\n }\n\n onProgress?.('generating render HTML', 10);\n signal?.throwIfAborted();\n const { PLAYER_BUNDLE } = await import('@bendyline/squisq-react/standalone-source');\n signal?.throwIfAborted();\n const renderHtml = generateRenderHtml(doc, {\n playerScript: PLAYER_BUNDLE,\n images,\n audio: audio.size > 0 ? audio : undefined,\n width,\n height,\n captionStyle,\n animationsEnabled,\n });\n\n onProgress?.('launching browser', 15);\n signal?.throwIfAborted();\n const { chromium } = await import('playwright-core');\n signal?.throwIfAborted();\n let browser: import('playwright-core').Browser;\n try {\n browser = await chromium.launch({ headless: true });\n } catch (err: unknown) {\n signal?.throwIfAborted();\n const detail = err instanceof Error ? err.message.split('\\n')[0] : String(err);\n throw new Error(\n 'Playwright Chromium is not installed. Run: npx playwright install chromium\\n' +\n `(launch failed: ${detail})`,\n );\n }\n\n const handleAbort = (): void => {\n void browser.close().catch(() => undefined);\n };\n if (signal?.aborted) {\n await browser.close().catch(() => undefined);\n signal.throwIfAborted();\n }\n signal?.addEventListener('abort', handleAbort, { once: true });\n let renderAPI: import('playwright-core').JSHandle<BrowserRenderAPI> | null = null;\n const capturedFrames = new CapturedFrameCollector();\n\n try {\n const page = await browser.newPage({ viewport: { width, height } });\n const pageErrors: string[] = [];\n page.on('pageerror', (err) => pageErrors.push(err.message));\n signal?.throwIfAborted();\n await page.setContent(renderHtml, { waitUntil: 'load' });\n signal?.throwIfAborted();\n await page.waitForTimeout(500);\n signal?.throwIfAborted();\n try {\n await page.waitForFunction(\n () => {\n const root = document.getElementById('squisq-root');\n const player = (window as unknown as SquisqBrowserWindow).SquisqPlayer;\n return root ? player?.getHandle(root)?.getRenderAPI() != null : false;\n },\n { timeout: 15000 },\n );\n } catch {\n signal?.throwIfAborted();\n const errorDetail = pageErrors.length\n ? `\\nPage errors:\\n ${pageErrors.join('\\n ')}`\n : '\\nNo page errors captured — the player may have failed to mount.';\n throw new Error(\n `The standalone player failed to boot in headless Chromium. ` +\n `Render API did not initialize within 15 seconds.${errorDetail}`,\n );\n }\n\n renderAPI = await page.evaluateHandle(() => {\n const root = document.getElementById('squisq-root');\n const player = (window as unknown as SquisqBrowserWindow).SquisqPlayer;\n const api = root ? player?.getHandle(root)?.getRenderAPI() : null;\n if (!api) throw new Error('Squisq render API disappeared after initialization.');\n return api;\n });\n signal?.throwIfAborted();\n const docDuration = await renderAPI.evaluate((api) => api.getDuration());\n signal?.throwIfAborted();\n if (docDuration <= 0) throw new Error('Document has zero duration — nothing to render');\n\n const hasCover =\n coverPreRoll > 0 ? await renderAPI.evaluate((api) => api.hasCoverBlock()) : false;\n const appliedCoverPreRoll = resolveAppliedCoverPreRoll(coverPreRoll, hasCover);\n const storyFrameCount = Math.ceil(docDuration * fps);\n const preRollFrameCount = Math.ceil(appliedCoverPreRoll * fps);\n const totalFrames = preRollFrameCount + storyFrameCount;\n onProgress?.('capturing frames', 20);\n capturedFrames.throwIfAborted(signal);\n\n if (preRollFrameCount > 0) {\n capturedFrames.throwIfAborted(signal);\n await renderAPI.evaluate((api) => api.showCover());\n await page.waitForTimeout(100);\n capturedFrames.throwIfAborted(signal);\n const coverFrame = await page.screenshot({ type: 'png' });\n capturedFrames.throwIfAborted(signal);\n capturedFrames.append(coverFrame, preRollFrameCount);\n await renderAPI.evaluate((api) => api.hideCover());\n }\n\n const frameInterval = 1 / fps;\n for (let i = 0; i < storyFrameCount; i++) {\n capturedFrames.throwIfAborted(signal);\n const time = i * frameInterval;\n await renderAPI.evaluate((api, t: number) => api.seekTo(t), time);\n const frame = await page.screenshot({ type: 'png' });\n capturedFrames.throwIfAborted(signal);\n capturedFrames.append(frame);\n if (i % Math.max(1, Math.floor(fps / 2)) === 0 || i === storyFrameCount - 1) {\n onProgress?.(\n 'capturing frames',\n 20 + Math.round((capturedFrames.frameCount / totalFrames) * 60),\n );\n capturedFrames.throwIfAborted(signal);\n }\n }\n\n return {\n frames: capturedFrames.release(),\n totalDuration: docDuration + appliedCoverPreRoll,\n appliedCoverPreRoll,\n ffmpegPath,\n };\n } catch (err: unknown) {\n capturedFrames.clear();\n signal?.throwIfAborted();\n throw err;\n } finally {\n signal?.removeEventListener('abort', handleAbort);\n await renderAPI?.dispose().catch(() => undefined);\n await browser.close().catch(() => undefined);\n }\n}\n\n/** Render a Doc + media container to an MP4 video file. */\nexport async function renderDocToMp4(\n doc: Doc,\n container: ContentContainer,\n options: RenderDocToMp4Options,\n): Promise<RenderDocToMp4Result> {\n options.signal?.throwIfAborted();\n const fps = options.fps ?? 30;\n const quality = options.quality ?? 'normal';\n const orientation = options.orientation ?? 'landscape';\n const dimensions = resolveDimensions({\n orientation,\n width: options.width,\n height: options.height,\n fps,\n quality,\n });\n const capture = await captureDocFrames(doc, container, {\n fps,\n width: dimensions.width,\n height: dimensions.height,\n captionStyle: options.captionStyle,\n coverPreRoll: options.coverPreRoll ?? 0,\n animationsEnabled: options.animationsEnabled ?? true,\n onProgress: options.onProgress,\n signal: options.signal,\n });\n const frameCount = capture.frames.length;\n try {\n options.onProgress?.('encoding video', 80);\n options.signal?.throwIfAborted();\n const encodingAudio = await buildMixedAudioTrack(\n doc,\n container,\n capture.ffmpegPath,\n capture.appliedCoverPreRoll,\n options.signal,\n );\n options.signal?.throwIfAborted();\n const { framesToMp4Native } = await import('./util/nativeEncoder.js');\n await framesToMp4Native(capture.ffmpegPath, capture.frames, encodingAudio, options.outputPath, {\n fps,\n quality,\n orientation,\n width: dimensions.width,\n height: dimensions.height,\n signal: options.signal,\n onProgress: (percent, phase) =>\n options.onProgress?.(`encoding: ${phase}`, 80 + Math.round(percent * 0.2)),\n });\n options.signal?.throwIfAborted();\n options.onProgress?.('done', 100);\n options.signal?.throwIfAborted();\n return {\n duration: capture.totalDuration,\n frameCount,\n outputPath: options.outputPath,\n };\n } finally {\n capture.frames.length = 0;\n }\n}\n\n/** Render a Doc + media container to a silent animated GIF. */\nexport async function renderDocToGif(\n doc: Doc,\n container: ContentContainer,\n options: RenderDocToGifOptions,\n): Promise<RenderDocToGifResult> {\n options.signal?.throwIfAborted();\n const fps = options.fps ?? GIF_EXPORT_DEFAULTS.fps;\n if (!Number.isFinite(fps) || fps <= 0 || fps > 100) {\n throw new RangeError('GIF FPS must be a finite number between 1 and 100.');\n }\n const orientation = options.orientation ?? 'landscape';\n const portrait = orientation === 'portrait';\n const width =\n options.width ?? (portrait ? GIF_EXPORT_DEFAULTS.height : GIF_EXPORT_DEFAULTS.width);\n const height =\n options.height ?? (portrait ? GIF_EXPORT_DEFAULTS.width : GIF_EXPORT_DEFAULTS.height);\n // Reuse shared dimension validation without inheriting MP4's 1080p defaults.\n resolveDimensions({ orientation, width, height, fps });\n // Validate palette/muxer options before the expensive browser capture.\n ffmpegGifOutputArgs({\n width,\n height,\n loop: options.loop ?? GIF_EXPORT_DEFAULTS.loop,\n maxColors: options.maxColors ?? GIF_EXPORT_DEFAULTS.maxColors,\n dither: options.dither ?? GIF_EXPORT_DEFAULTS.dither,\n bayerScale: options.bayerScale,\n });\n\n const capture = await captureDocFrames(doc, container, {\n fps,\n width,\n height,\n captionStyle: options.captionStyle,\n coverPreRoll: options.coverPreRoll ?? 0,\n animationsEnabled: options.animationsEnabled ?? false,\n onProgress: options.onProgress,\n signal: options.signal,\n });\n const frameCount = capture.frames.length;\n try {\n options.onProgress?.('encoding GIF', 80);\n options.signal?.throwIfAborted();\n const { framesToGifNative } = await import('./util/nativeEncoder.js');\n await framesToGifNative(capture.ffmpegPath, capture.frames, options.outputPath, {\n fps,\n orientation,\n width,\n height,\n loop: options.loop,\n maxColors: options.maxColors,\n dither: options.dither,\n bayerScale: options.bayerScale,\n signal: options.signal,\n onProgress: (percent, phase) =>\n options.onProgress?.(`encoding: ${phase}`, 80 + Math.round(percent * 0.2)),\n });\n options.signal?.throwIfAborted();\n options.onProgress?.('done', 100);\n options.signal?.throwIfAborted();\n\n const hasAudio =\n (doc.audio?.segments?.length ?? 0) > 0 ||\n resolveMediaSchedule(doc).some((clip) => clip.kind === 'audio');\n return {\n duration: capture.totalDuration,\n frameCount,\n outputPath: options.outputPath,\n warnings: hasAudio ? ['Animated GIF does not support audio; audio tracks were omitted.'] : [],\n };\n } finally {\n capture.frames.length = 0;\n }\n}\n\n// ── Thumbnail extraction ──────────────────────────────────────────\n\n/** A thumbnail size specification. */\nexport interface ThumbnailSpec {\n /** Label for the thumbnail (used in filename: `{slug}-{width}x{height}.jpg`). */\n name: string;\n /** Output width in pixels. */\n width: number;\n /** Output height in pixels. */\n height: number;\n /** FFmpeg video filter string (e.g., 'scale=1280:720'). */\n filter: string;\n}\n\n/** Options for extractThumbnails. */\nexport interface ExtractThumbnailsOptions {\n /** Path to the source MP4 or animated GIF. */\n videoPath: string;\n /** Directory to write thumbnails into. */\n outputDir: string;\n /** Base slug for filenames (produces `{slug}-{width}x{height}.jpg`). */\n slug: string;\n /** Thumbnail sizes to generate. */\n sizes: ThumbnailSpec[];\n /** Overwrite existing thumbnails (default: false). */\n force?: boolean;\n /** Cancels FFmpeg and rejects with the caller's exact abort reason. */\n signal?: AbortSignal;\n}\n\n/**\n * Extract thumbnail images from the first frame of an MP4 or animated GIF.\n * Produces JPEG files at each specified size using FFmpeg video filters.\n */\nexport async function extractThumbnails(options: ExtractThumbnailsOptions): Promise<void> {\n const { videoPath, outputDir, slug, sizes, force, signal } = options;\n const { existsSync } = await import('node:fs');\n const { rm } = await import('node:fs/promises');\n const { join } = await import('node:path');\n\n signal?.throwIfAborted();\n const ffmpegPath = (await detectFfmpegDetailed(signal))?.path ?? null;\n if (!ffmpegPath) {\n throw new Error(\n 'ffmpeg is required for thumbnail extraction but not found in PATH.\\n' +\n 'Install it with:\\n' +\n ' macOS: brew install ffmpeg\\n' +\n ' Ubuntu: sudo apt install ffmpeg\\n' +\n ' Windows: winget install ffmpeg',\n );\n }\n\n const generatedPaths: string[] = [];\n try {\n for (const thumb of sizes) {\n signal?.throwIfAborted();\n const outputPath = join(outputDir, `${slug}-${thumb.width}x${thumb.height}.jpg`);\n if (!force && existsSync(outputPath)) continue;\n\n try {\n await runFfmpeg(\n ffmpegPath,\n ['-y', '-i', videoPath, '-vf', thumb.filter, '-frames:v', '1', '-q:v', '2', outputPath],\n {\n timeoutMs: 30_000,\n failureMessage: `Thumbnail extraction failed (${thumb.name})`,\n signal,\n },\n );\n signal?.throwIfAborted();\n generatedPaths.push(outputPath);\n } catch (error: unknown) {\n await rm(outputPath, { force: true });\n throw error;\n }\n }\n signal?.throwIfAborted();\n } catch (error: unknown) {\n await Promise.allSettled(generatedPaths.map((path) => rm(path, { force: true })));\n throw error;\n }\n}\n","/**\n * detectFfmpeg\n *\n * Locates a native ffmpeg binary. Resolution order:\n * 1. `SQUISQ_FFMPEG` environment variable — verified by running\n * `<path> -version`. A value that is set but broken is a hard error\n * (never silently ignored), so misconfiguration surfaces immediately.\n * 2. System PATH lookup (`which` on macOS/Linux, `where` on Windows).\n * 3. The optional `ffmpeg-static` npm package, when the user has installed\n * it themselves. It is deliberately NOT a dependency of the CLI — the\n * import is opportunistic and falls through cleanly when absent.\n */\n\nimport { execFile } from 'node:child_process';\n\n/** Where a detected ffmpeg binary came from. */\nexport type FfmpegSource = 'env' | 'path' | 'ffmpeg-static';\n\n/** A successfully detected ffmpeg binary. */\nexport interface FfmpegDetection {\n /** Absolute path to the ffmpeg binary. */\n path: string;\n /** Which resolution step found it. */\n source: FfmpegSource;\n}\n\n/** Run a command and resolve with trimmed stdout, or null on any failure. */\nfunction run(command: string, args: string[], signal?: AbortSignal): Promise<string | null> {\n signal?.throwIfAborted();\n return new Promise((resolve, reject) => {\n execFile(command, args, { timeout: 5000, signal }, (err, stdout) => {\n if (signal?.aborted) {\n reject(signal.reason);\n return;\n }\n if (err || !stdout.trim()) {\n resolve(null);\n return;\n }\n resolve(stdout.trim());\n });\n });\n}\n\n/**\n * Run `<path> -version` and return the first line of output\n * (e.g. \"ffmpeg version 7.1 ...\"), or null when the binary fails to run.\n */\nexport async function getFfmpegVersion(path: string, signal?: AbortSignal): Promise<string | null> {\n const out = await run(path, ['-version'], signal);\n return out ? out.split('\\n')[0].trim() : null;\n}\n\n/**\n * Detect ffmpeg and report which source found it (see module doc for the\n * resolution order). Returns null when no working binary is found.\n *\n * @throws Error when `SQUISQ_FFMPEG` is set but the binary does not run —\n * an explicit override must never be silently ignored.\n */\nexport async function detectFfmpegDetailed(signal?: AbortSignal): Promise<FfmpegDetection | null> {\n signal?.throwIfAborted();\n // 1. Explicit override\n const envPath = process.env.SQUISQ_FFMPEG;\n if (envPath) {\n const version = await getFfmpegVersion(envPath, signal);\n if (!version) {\n throw new Error(\n `SQUISQ_FFMPEG is set to \"${envPath}\" but running \"${envPath} -version\" failed. ` +\n 'Fix the path or unset SQUISQ_FFMPEG.',\n );\n }\n return { path: envPath, source: 'env' };\n }\n\n // 2. System PATH\n const command = process.platform === 'win32' ? 'where' : 'which';\n const found = await run(command, ['ffmpeg'], signal);\n if (found) {\n // `which` and `where` can return multiple lines; take the first\n return { path: found.split('\\n')[0].trim(), source: 'path' };\n }\n\n // 3. Optional ffmpeg-static package. The specifier is a variable so\n // bundlers/TypeScript don't try to resolve a package we don't depend on.\n try {\n signal?.throwIfAborted();\n const specifier = 'ffmpeg-static';\n const mod = (await import(specifier)) as { default?: unknown } | string;\n // CJS default export shape (`module.exports = path`) surfaces as\n // `mod.default`; some build setups expose the string directly.\n const candidate =\n typeof mod === 'string' ? mod : typeof mod.default === 'string' ? mod.default : null;\n if (candidate) {\n return { path: candidate, source: 'ffmpeg-static' };\n }\n } catch {\n signal?.throwIfAborted();\n // Not installed — fall through.\n }\n\n signal?.throwIfAborted();\n return null;\n}\n","/**\n * audioMix — Build the MP4's final audio track from a doc's audio timeline.\n *\n * The schedule is produced by ONE source of truth: `computeAudioTimeline`\n * (from `@bendyline/squisq-video`), which flattens a doc's narration segments\n * (laid sequentially) and its timed media clips (placed at absolute positions\n * via `resolveMediaSchedule`) into a flat list of absolute-timed\n * `AudioTimelineClip`s. This CLI-side helper is a pure *consumer* of that\n * schedule: it loads each clip's bytes and assembles an ffmpeg `amix`\n * filtergraph where every clip is trimmed to its source window and delayed to\n * its absolute start.\n *\n * Because the browser MP4 export and this CLI mix now read the same timeline,\n * their audio placement can no longer drift. In particular, zero-duration\n * narration segments are skipped here exactly as they are in the browser\n * (`computeAudioTimeline` never emits a clip with `durationSec <= 0`).\n *\n * Frame capture stays silent — this reconstructs the audio offline from the\n * same schedule the renderer uses, keeping audio and video aligned.\n */\n\nimport type { Doc } from '@bendyline/squisq/schemas';\nimport type { ContentContainer } from '@bendyline/squisq/storage';\nimport { computeAudioTimeline } from '@bendyline/squisq-video';\nimport type { AudioTimelineClip } from '@bendyline/squisq-video';\nimport { runFfmpeg } from './runFfmpeg.js';\n\n/**\n * Build the final mixed MP3 audio track for a doc's MP4 export.\n *\n * Drives the ffmpeg filtergraph entirely from `computeAudioTimeline(doc,\n * coverPreRoll)`: each clip's `startSec` → `adelay`, `sourceInSec` → `atrim`\n * start, `durationSec` → `atrim` duration, keyed by `src`. All clips are summed\n * with `amix` (`normalize=0` so individual levels are preserved).\n *\n * @param doc - The document whose audio to schedule.\n * @param container - Media container to read each clip's `src` bytes from.\n * @param ffmpegPath - Path to the ffmpeg binary.\n * @param coverPreRoll - Seconds of cover-slide pre-roll; shifts every clip's\n * start (matching the silent leading frames).\n * @returns The mixed MP3 bytes, or `null` when the doc has no audio to place.\n */\nexport async function buildMixedAudioTrack(\n doc: Doc,\n container: ContentContainer,\n ffmpegPath: string,\n coverPreRoll: number,\n signal?: AbortSignal,\n): Promise<Uint8Array | null> {\n signal?.throwIfAborted();\n const timeline = computeAudioTimeline(doc, coverPreRoll);\n if (timeline.length === 0) return null;\n\n // Load each clip's bytes, caching reads by `src` (a src may back several clips).\n const bytesBySrc = new Map<string, ArrayBuffer | null>();\n const readSrc = async (src: string): Promise<ArrayBuffer | null> => {\n signal?.throwIfAborted();\n if (bytesBySrc.has(src)) return bytesBySrc.get(src)!;\n const data = await container.readFile(src);\n signal?.throwIfAborted();\n bytesBySrc.set(src, data ?? null);\n return data ?? null;\n };\n\n const usable: Array<{ clip: AudioTimelineClip; buffer: ArrayBuffer }> = [];\n for (const clip of timeline) {\n signal?.throwIfAborted();\n const buffer = await readSrc(clip.src);\n if (buffer) usable.push({ clip, buffer });\n }\n if (usable.length === 0) return null;\n\n return mixTimelineClips(ffmpegPath, usable, signal);\n}\n\n/** Assemble the ffmpeg `amix` filtergraph and run it, returning the mixed MP3. */\nasync function mixTimelineClips(\n ffmpegPath: string,\n usable: Array<{ clip: AudioTimelineClip; buffer: ArrayBuffer }>,\n signal?: AbortSignal,\n): Promise<Uint8Array> {\n const { writeFile, readFile, mkdir, rm } = await import('node:fs/promises');\n const { join } = await import('node:path');\n const { tmpdir } = await import('node:os');\n const { randomBytes } = await import('node:crypto');\n signal?.throwIfAborted();\n\n const workDir = join(tmpdir(), `squisq-audio-mix-${randomBytes(8).toString('hex')}`);\n await mkdir(workDir, { recursive: true });\n\n const ms = (s: number) => Math.max(0, Math.round(s * 1000));\n\n try {\n signal?.throwIfAborted();\n const inputs: string[] = [];\n const filters: string[] = [];\n const labels: string[] = [];\n\n for (const { clip, buffer } of usable) {\n signal?.throwIfAborted();\n const p = join(workDir, `clip-${inputs.length}.mp3`);\n await writeFile(p, new Uint8Array(buffer));\n signal?.throwIfAborted();\n const i = inputs.push(p) - 1;\n const delayMs = ms(clip.startSec);\n const start = Math.max(0, clip.sourceInSec);\n const end = start + Math.max(0, clip.durationSec);\n filters.push(\n `[${i}:a]atrim=start=${start}:end=${end},asetpts=PTS-STARTPTS,adelay=${delayMs}|${delayMs}[a${i}]`,\n );\n labels.push(`[a${i}]`);\n }\n\n const graph =\n `${filters.join(';')};${labels.join('')}` +\n `amix=inputs=${labels.length}:normalize=0:dropout_transition=0[aout]`;\n\n const outputPath = join(workDir, 'mixed.mp3');\n const args = ['-y'];\n for (const p of inputs) args.push('-i', p);\n args.push(\n '-filter_complex',\n graph,\n '-map',\n '[aout]',\n '-c:a',\n 'libmp3lame',\n '-b:a',\n '192k',\n outputPath,\n );\n\n await runFfmpeg(ffmpegPath, args, {\n timeoutMs: 180_000,\n failureMessage: 'ffmpeg audio mix failed',\n signal,\n });\n\n signal?.throwIfAborted();\n const data = await readFile(outputPath);\n signal?.throwIfAborted();\n return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);\n } finally {\n await rm(workDir, { recursive: true, force: true });\n }\n}\n","/**\n * Hard retained-memory ceiling for PNG frames captured before native encoding.\n *\n * The Node renderer currently keeps captured PNGs in memory while FFmpeg is\n * prepared. Bounding the actual encoded PNG bytes prevents visually complex\n * documents from exhausting the process even when their dimensions and frame\n * counts satisfy higher-level render budgets.\n */\nexport const MAX_CAPTURED_FRAME_BYTES = 256 * 1024 * 1024;\n\n/** Stable failure raised before another captured frame is retained. */\nexport class CapturedFrameBudgetError extends Error {\n public readonly code = 'captured-frame-budget-exceeded';\n\n public constructor(\n public readonly capturedBytes: number,\n public readonly attemptedFrameBytes: number,\n public readonly maximumBytes: number,\n ) {\n super(\n `Captured PNG frames would retain ${capturedBytes + attemptedFrameBytes} bytes, ` +\n `exceeding the ${maximumBytes}-byte rendered-media memory limit.`,\n );\n this.name = 'CapturedFrameBudgetError';\n }\n}\n\n/**\n * Owns captured-frame references until they are handed to an encoder.\n * Repeated references (cover pre-roll) count once toward retained bytes.\n */\nexport class CapturedFrameCollector {\n private values: Uint8Array[] = [];\n private seen = new WeakSet<Uint8Array>();\n private bytes = 0;\n\n public constructor(private readonly maximumBytes = MAX_CAPTURED_FRAME_BYTES) {\n if (\n !Number.isSafeInteger(maximumBytes) ||\n maximumBytes < 1 ||\n maximumBytes > MAX_CAPTURED_FRAME_BYTES\n ) {\n throw new Error(\n `Captured-frame byte limit must be between 1 and ${MAX_CAPTURED_FRAME_BYTES}`,\n );\n }\n }\n\n public get frameCount(): number {\n return this.values.length;\n }\n\n public get retainedBytes(): number {\n return this.bytes;\n }\n\n public append(frame: Uint8Array, repetitions = 1): void {\n if (!Number.isSafeInteger(repetitions) || repetitions < 1) {\n throw new Error('Captured-frame repetition count must be a positive integer');\n }\n const additionalBytes = this.seen.has(frame) ? 0 : frame.byteLength;\n if (this.bytes + additionalBytes > this.maximumBytes) {\n const capturedBytes = this.bytes;\n this.clear();\n throw new CapturedFrameBudgetError(capturedBytes, additionalBytes, this.maximumBytes);\n }\n if (additionalBytes > 0) {\n this.seen.add(frame);\n this.bytes += additionalBytes;\n }\n for (let index = 0; index < repetitions; index += 1) this.values.push(frame);\n }\n\n /** Clear retained frame references before preserving the caller's abort reason. */\n public throwIfAborted(signal?: AbortSignal): void {\n if (!signal?.aborted) return;\n this.clear();\n signal.throwIfAborted();\n }\n\n /** Transfer ownership of the captured frames to the encoder. */\n public release(): Uint8Array[] {\n const released = this.values;\n this.values = [];\n this.seen = new WeakSet<Uint8Array>();\n this.bytes = 0;\n return released;\n }\n\n /** Drop every retained reference so failed/cancelled captures can be collected promptly. */\n public clear(): void {\n this.values.length = 0;\n this.values = [];\n this.seen = new WeakSet<Uint8Array>();\n this.bytes = 0;\n }\n}\n","/**\n * Resolve the pre-roll that will actually be rendered.\n *\n * A requested pre-roll only has meaning when the document exposes a cover.\n * Keeping this decision explicit prevents frame counts, audio offsets, and\n * reported duration from drifting apart when a document has no cover block.\n */\nexport function resolveAppliedCoverPreRoll(requestedSeconds: number, hasCover: boolean): number {\n if (!Number.isFinite(requestedSeconds) || requestedSeconds < 0) {\n throw new Error('Cover pre-roll must be a finite number of seconds greater than or equal to 0');\n }\n return hasCover ? requestedSeconds : 0;\n}\n","/**\n * CLI format registry.\n *\n * The CLI extends the formats package's default registry with `mp4` and `gif`\n * formats so `convert()` can produce rendered media like DOCX/PDF/HTML. These\n * exports are Node-only (Playwright + FFmpeg), which is why they live here\n * rather than in the browser-pure formats package.\n */\n\nimport { randomBytes } from 'node:crypto';\nimport { readFile, rm } from 'node:fs/promises';\nimport { tmpdir } from 'node:os';\nimport { join } from 'node:path';\nimport { defaultRegistry } from '@bendyline/squisq-formats';\nimport type {\n ConversionResult,\n ConvertOptions,\n FormatDefinition,\n FormatRegistry,\n NormalizedInput,\n} from '@bendyline/squisq-formats';\nimport type { GifDither, VideoOrientation, VideoQuality } from '@bendyline/squisq-video';\n\nexport interface Mp4FormatOptions {\n onProgress?: (phase: string, percent: number) => void;\n fps?: number;\n quality?: VideoQuality;\n orientation?: VideoOrientation;\n width?: number;\n height?: number;\n captionStyle?: 'standard' | 'social';\n coverPreRoll?: number;\n animationsEnabled?: boolean;\n}\n\n/** Per-export options for `convert(..., 'gif')`. */\nexport interface GifFormatOptions {\n onProgress?: (phase: string, percent: number) => void;\n fps?: number;\n orientation?: VideoOrientation;\n width?: number;\n height?: number;\n captionStyle?: 'standard' | 'social';\n coverPreRoll?: number;\n animationsEnabled?: boolean;\n loop?: number;\n maxColors?: number;\n dither?: GifDither;\n bayerScale?: number;\n}\n\n/** Sensible defaults for `convert(..., 'mp4')` — a full-quality landscape clip. */\nconst MP4_DEFAULTS = {\n fps: 30,\n quality: 'normal' as VideoQuality,\n orientation: 'landscape' as VideoOrientation,\n coverPreRoll: 0,\n animationsEnabled: true,\n} as const;\n\n/** Compression-friendly defaults for `convert(..., 'gif')`. */\nconst GIF_DEFAULTS = {\n fps: 10,\n orientation: 'landscape' as VideoOrientation,\n width: 960,\n height: 540,\n coverPreRoll: 0,\n animationsEnabled: false,\n loop: 0,\n maxColors: 256,\n dither: 'sierra2_4a' as GifDither,\n} as const;\n\n/**\n * The `mp4` {@link FormatDefinition}.\n *\n * Export-only: it renders the normalized Doc to a temporary MP4 on disk via\n * {@link renderDocToMp4}, reads the produced file back into a `Uint8Array`, and\n * always deletes the temp file (even on failure). Per-render knobs (fps,\n * quality, orientation, coverPreRoll) can be supplied through\n * `options.formatOptions.mp4`; otherwise {@link MP4_DEFAULTS} apply.\n */\nfunction mp4Format(): FormatDefinition {\n return {\n id: 'mp4',\n label: 'MP4 Video',\n mimeType: 'video/mp4',\n extensions: ['.mp4'],\n async exportDoc(input: NormalizedInput, options: ConvertOptions): Promise<ConversionResult> {\n options.signal?.throwIfAborted();\n // The renderer is lazy-loaded below so api.ts can create this registry\n // without an eager ES-module initialization cycle.\n const mp4Opts = (options.formatOptions?.mp4 ?? {}) as Mp4FormatOptions;\n const fps = typeof mp4Opts.fps === 'number' ? mp4Opts.fps : MP4_DEFAULTS.fps;\n const quality =\n typeof mp4Opts.quality === 'string'\n ? (mp4Opts.quality as VideoQuality)\n : MP4_DEFAULTS.quality;\n const orientation =\n typeof mp4Opts.orientation === 'string'\n ? (mp4Opts.orientation as VideoOrientation)\n : MP4_DEFAULTS.orientation;\n const coverPreRoll =\n typeof mp4Opts.coverPreRoll === 'number' ? mp4Opts.coverPreRoll : MP4_DEFAULTS.coverPreRoll;\n const animationsEnabled =\n typeof mp4Opts.animationsEnabled === 'boolean'\n ? mp4Opts.animationsEnabled\n : MP4_DEFAULTS.animationsEnabled;\n\n const outputPath = join(tmpdir(), `squisq-mp4-${randomBytes(8).toString('hex')}.mp4`);\n try {\n const { renderDocToMp4 } = await import('./api.js');\n await renderDocToMp4(input.doc, input.container, {\n outputPath,\n fps,\n quality,\n orientation,\n width: mp4Opts.width,\n height: mp4Opts.height,\n captionStyle: mp4Opts.captionStyle,\n coverPreRoll,\n animationsEnabled,\n signal: options.signal,\n onProgress: mp4Opts.onProgress,\n });\n options.signal?.throwIfAborted();\n const data = await readFile(outputPath, { signal: options.signal });\n options.signal?.throwIfAborted();\n const bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);\n return { bytes, mimeType: 'video/mp4', suggestedFilename: '', warnings: [] };\n } finally {\n await rm(outputPath, { force: true });\n }\n },\n };\n}\n\n/** The CLI-only animated GIF format definition. */\nfunction gifFormat(): FormatDefinition {\n return {\n id: 'gif',\n label: 'Animated GIF',\n mimeType: 'image/gif',\n extensions: ['.gif'],\n async exportDoc(input: NormalizedInput, options: ConvertOptions): Promise<ConversionResult> {\n options.signal?.throwIfAborted();\n const gifOpts = (options.formatOptions?.gif ?? {}) as GifFormatOptions;\n const orientation =\n typeof gifOpts.orientation === 'string'\n ? (gifOpts.orientation as VideoOrientation)\n : GIF_DEFAULTS.orientation;\n const portrait = orientation === 'portrait';\n const defaultWidth = portrait ? GIF_DEFAULTS.height : GIF_DEFAULTS.width;\n const defaultHeight = portrait ? GIF_DEFAULTS.width : GIF_DEFAULTS.height;\n const outputPath = join(tmpdir(), `squisq-gif-${randomBytes(8).toString('hex')}.gif`);\n try {\n const { renderDocToGif } = await import('./api.js');\n const result = await renderDocToGif(input.doc, input.container, {\n outputPath,\n fps: typeof gifOpts.fps === 'number' ? gifOpts.fps : GIF_DEFAULTS.fps,\n orientation,\n width: typeof gifOpts.width === 'number' ? gifOpts.width : defaultWidth,\n height: typeof gifOpts.height === 'number' ? gifOpts.height : defaultHeight,\n captionStyle: gifOpts.captionStyle,\n coverPreRoll:\n typeof gifOpts.coverPreRoll === 'number'\n ? gifOpts.coverPreRoll\n : GIF_DEFAULTS.coverPreRoll,\n animationsEnabled:\n typeof gifOpts.animationsEnabled === 'boolean'\n ? gifOpts.animationsEnabled\n : GIF_DEFAULTS.animationsEnabled,\n loop: typeof gifOpts.loop === 'number' ? gifOpts.loop : GIF_DEFAULTS.loop,\n maxColors:\n typeof gifOpts.maxColors === 'number' ? gifOpts.maxColors : GIF_DEFAULTS.maxColors,\n dither: gifOpts.dither ?? GIF_DEFAULTS.dither,\n bayerScale: gifOpts.bayerScale,\n signal: options.signal,\n onProgress: gifOpts.onProgress,\n });\n options.signal?.throwIfAborted();\n const data = await readFile(outputPath, { signal: options.signal });\n options.signal?.throwIfAborted();\n const bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);\n return {\n bytes,\n mimeType: 'image/gif',\n suggestedFilename: '',\n warnings: result.warnings,\n };\n } finally {\n await rm(outputPath, { force: true });\n }\n },\n };\n}\n\n/**\n * Build the CLI's format registry: every built-in format from\n * `@bendyline/squisq-formats` plus the CLI-only rendered-media exporters.\n */\nexport function createCliRegistry(): FormatRegistry {\n const registry = defaultRegistry();\n registry.register(mp4Format());\n registry.register(gifFormat());\n return registry;\n}\n","/**\n * readInput\n *\n * Unified input reader for the CLI. Accepts a path to a markdown file, a JSON\n * Doc file, a `.zip`/`.dbk` container, a folder, or any importable binary\n * format (`.docx`/`.pptx`/`.pdf`/`.xlsx`/`.csv`/`.html`/`.htm`) and always\n * resolves it to a fully-populated {@link ReadInputResult}: a `Doc`, a\n * `ContentContainer` holding the document + any bundled media, the parsed\n * `MarkdownDocument` when the source was markdown-shaped, and the detected\n * `sourceFormat`.\n *\n * Binary formats are dispatched through the shared format registry's importer\n * (preferring `importContainer`, falling back to `importDoc`); every path\n * derives the `Doc` via `markdownToDoc()` when the source is markdown-shaped.\n */\n\nimport { readFile, readdir, stat } from 'node:fs/promises';\nimport { join, extname } from 'node:path';\nimport { parseMarkdown, stringifyMarkdown } from '@bendyline/squisq/markdown';\nimport type { MarkdownDocument } from '@bendyline/squisq/markdown';\nimport { markdownToDoc, resolveAudioMapping } from '@bendyline/squisq/doc';\nimport type { Doc } from '@bendyline/squisq/schemas';\nimport type { ContentContainer } from '@bendyline/squisq/storage';\nimport { MemoryContentContainer } from '@bendyline/squisq/storage';\nimport { zipToContainer } from '@bendyline/squisq-formats/container';\nimport { defaultRegistry } from '@bendyline/squisq-formats';\nimport type { FormatId } from '@bendyline/squisq-formats';\n\nexport interface ReadInputResult {\n /** The resolved document. Always present. */\n doc: Doc;\n /** Virtual file system holding the document plus any bundled media. */\n container: ContentContainer;\n /**\n * The parsed markdown document. Present when the source was markdown-shaped\n * (`.md`, container/folder markdown, or a binary format imported as\n * markdown); absent for JSON-Doc input.\n */\n markdownDoc?: MarkdownDocument;\n /** Detected source format id (registry id, or `md`/`json`/`dbk`/`folder`). */\n sourceFormat: FormatId;\n}\n\nexport interface ReadInputOptions {\n /** Cancel filesystem traversal, import, or audio resolution at a bounded boundary. */\n signal?: AbortSignal;\n /**\n * Infer a Squisq theme from an OOXML source's theme part (PPTX today).\n * Default true — the importer carries it as frontmatter.\n */\n inferTheme?: boolean;\n /** Derive custom layout templates from PPTX slide layouts. Default true. */\n inferLayouts?: boolean;\n}\n\n/** MIME type lookup by extension (common content types) */\nconst MIME_TYPES: Record<string, string> = {\n '.md': 'text/markdown',\n '.txt': 'text/plain',\n '.json': 'application/json',\n '.jpg': 'image/jpeg',\n '.jpeg': 'image/jpeg',\n '.png': 'image/png',\n '.gif': 'image/gif',\n '.webp': 'image/webp',\n '.svg': 'image/svg+xml',\n '.mp3': 'audio/mpeg',\n '.wav': 'audio/wav',\n '.ogg': 'audio/ogg',\n '.mp4': 'video/mp4',\n '.webm': 'video/webm',\n};\n\n/**\n * Binary extensions dispatched through the registry importer. `.md`, `.zip`,\n * `.dbk`, and `.json` are intentionally excluded — they have dedicated paths\n * that also detect bundled media / doc.json.\n */\nconst IMPORTER_EXTS = ['.docx', '.pptx', '.pdf', '.xlsx', '.csv', '.html', '.htm'];\n\nfunction mimeFromExt(filePath: string): string {\n return MIME_TYPES[extname(filePath).toLowerCase()] ?? 'application/octet-stream';\n}\n\n/**\n * Recursively walk a directory and return all file paths (relative to root).\n */\nasync function walkDir(root: string, prefix = '', signal?: AbortSignal): Promise<string[]> {\n throwIfAborted(signal);\n const entries = await readdir(root, { withFileTypes: true });\n throwIfAborted(signal);\n const paths: string[] = [];\n\n for (const entry of entries) {\n throwIfAborted(signal);\n const relPath = prefix ? `${prefix}/${entry.name}` : entry.name;\n if (entry.isDirectory()) {\n paths.push(...(await walkDir(join(root, entry.name), relPath, signal)));\n } else if (entry.isFile()) {\n paths.push(relPath);\n }\n }\n\n return paths;\n}\n\n/**\n * Read input from a file path (markdown, JSON Doc, ZIP/DBK container, folder,\n * or an importable binary format) and resolve it to a populated\n * {@link ReadInputResult}.\n */\nexport async function readInput(\n inputPath: string,\n options?: ReadInputOptions,\n): Promise<ReadInputResult> {\n throwIfAborted(options?.signal);\n const result = await readInputRaw(inputPath, options);\n throwIfAborted(options?.signal);\n // Audio rides in the container: a document-anchored narration take\n // (`{[audio src=… anchor=document]}` + timing sidecar) re-times the\n // block timeline, and per-block audio files map into segments — so\n // `squisq convert`/`video` exports pace exactly like the editor preview.\n const doc = await resolveAudioMapping(result.doc, result.container);\n throwIfAborted(options?.signal);\n return doc === result.doc ? result : { ...result, doc };\n}\n\nasync function readInputRaw(\n inputPath: string,\n options?: ReadInputOptions,\n): Promise<ReadInputResult> {\n throwIfAborted(options?.signal);\n const info = await stat(inputPath);\n throwIfAborted(options?.signal);\n\n if (info.isDirectory()) {\n return readFolder(inputPath, options?.signal);\n }\n\n const ext = extname(inputPath).toLowerCase();\n if (ext === '.zip' || ext === '.dbk') {\n return readContainer(inputPath, options?.signal);\n }\n\n if (ext === '.json') {\n return readDocJsonFile(inputPath, options?.signal);\n }\n\n if (IMPORTER_EXTS.includes(ext)) {\n const def = defaultRegistry().byExtension(ext);\n if (def && (def.importContainer || def.importDoc)) {\n return readViaImporter(inputPath, def, options);\n }\n }\n\n // Default: treat as a markdown file\n return readMarkdownFile(inputPath, options?.signal);\n}\n\n/** Read a file into an ArrayBuffer (a fresh copy, not a Buffer view). */\nasync function readArrayBuffer(filePath: string, signal?: AbortSignal): Promise<ArrayBuffer> {\n const data = await readBinaryFile(filePath, signal);\n return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength) as ArrayBuffer;\n}\n\nasync function readBinaryFile(filePath: string, signal?: AbortSignal): Promise<Uint8Array> {\n throwIfAborted(signal);\n try {\n const data = await readFile(filePath, { signal });\n throwIfAborted(signal);\n return data;\n } catch (error: unknown) {\n throwIfAborted(signal);\n throw error;\n }\n}\n\nasync function readUtf8File(filePath: string, signal?: AbortSignal): Promise<string> {\n throwIfAborted(signal);\n try {\n const content = await readFile(filePath, { encoding: 'utf-8', signal });\n throwIfAborted(signal);\n return content;\n } catch (error: unknown) {\n throwIfAborted(signal);\n throw error;\n }\n}\n\nasync function readMarkdownFile(filePath: string, signal?: AbortSignal): Promise<ReadInputResult> {\n const content = await readUtf8File(filePath, signal);\n const container = new MemoryContentContainer();\n await container.writeDocument(content);\n throwIfAborted(signal);\n const markdownDoc = parseMarkdown(content);\n return { doc: markdownToDoc(markdownDoc), container, markdownDoc, sourceFormat: 'md' };\n}\n\n/**\n * Read a standalone Doc JSON file. The container is empty (no media bundled);\n * callers should populate it or set basePath for media resolution.\n */\nasync function readDocJsonFile(filePath: string, signal?: AbortSignal): Promise<ReadInputResult> {\n const content = await readUtf8File(filePath, signal);\n const doc = JSON.parse(content) as Doc;\n const container = new MemoryContentContainer();\n return { doc, container, sourceFormat: 'json' };\n}\n\n/**\n * Dispatch an importable binary format (docx/pptx/pdf/…) through the registry\n * importer. Prefers `importContainer` (extracts media alongside the markdown),\n * falling back to `importDoc` wrapped in a fresh container.\n */\nasync function readViaImporter(\n filePath: string,\n def: import('@bendyline/squisq-formats').FormatDefinition,\n options?: ReadInputOptions,\n): Promise<ReadInputResult> {\n const signal = options?.signal;\n throwIfAborted(signal);\n const buffer = await readArrayBuffer(filePath, signal);\n const convertOptions = {\n signal,\n formatOptions: {\n [def.id]: {\n inferTheme: options?.inferTheme !== false,\n inferLayouts: options?.inferLayouts !== false,\n },\n },\n };\n\n let container: ContentContainer;\n let markdownDoc: MarkdownDocument;\n if (def.importContainer) {\n throwIfAborted(signal);\n container = await def.importContainer(buffer, convertOptions);\n throwIfAborted(signal);\n const text = await container.readDocument();\n throwIfAborted(signal);\n markdownDoc = text ? parseMarkdown(text) : { type: 'document', children: [] };\n } else {\n // importDoc is guaranteed present by the caller's guard.\n throwIfAborted(signal);\n markdownDoc = await def.importDoc!(buffer, convertOptions);\n throwIfAborted(signal);\n const mem = new MemoryContentContainer();\n await mem.writeDocument(stringifyMarkdown(markdownDoc));\n throwIfAborted(signal);\n container = mem;\n }\n\n return { doc: markdownToDoc(markdownDoc), container, markdownDoc, sourceFormat: def.id };\n}\n\n/**\n * Known filenames for Doc JSON inside containers and folders.\n * Checked in priority order before falling back to markdown discovery.\n */\nconst DOC_JSON_NAMES = ['doc.json', 'story.json'];\n\n/** Resolve a container that may hold either a doc.json or a markdown document. */\nasync function resolveContainer(\n container: ContentContainer,\n sourceFormat: FormatId,\n missingMessage: string,\n signal?: AbortSignal,\n): Promise<ReadInputResult> {\n // Check for Doc JSON first.\n for (const name of DOC_JSON_NAMES) {\n throwIfAborted(signal);\n const jsonData = await container.readFile(name);\n throwIfAborted(signal);\n if (jsonData) {\n const doc = JSON.parse(new TextDecoder().decode(jsonData)) as Doc;\n return { doc, container, sourceFormat };\n }\n }\n\n throwIfAborted(signal);\n const markdown = await container.readDocument();\n throwIfAborted(signal);\n if (!markdown) {\n throw new Error(missingMessage);\n }\n\n const markdownDoc = parseMarkdown(markdown);\n return { doc: markdownToDoc(markdownDoc), container, markdownDoc, sourceFormat };\n}\n\nasync function readContainer(filePath: string, signal?: AbortSignal): Promise<ReadInputResult> {\n throwIfAborted(signal);\n const container = await zipToContainer(await readArrayBuffer(filePath, signal));\n throwIfAborted(signal);\n return resolveContainer(\n container,\n 'dbk',\n `No markdown document or doc.json found in container: ${filePath}`,\n signal,\n );\n}\n\nasync function readFolder(dirPath: string, signal?: AbortSignal): Promise<ReadInputResult> {\n throwIfAborted(signal);\n const container = new MemoryContentContainer();\n const files = await walkDir(dirPath, '', signal);\n throwIfAborted(signal);\n\n for (const relPath of files) {\n throwIfAborted(signal);\n const absPath = join(dirPath, relPath);\n const data = await readBinaryFile(absPath, signal);\n throwIfAborted(signal);\n await container.writeFile(\n relPath,\n new Uint8Array(data.buffer, data.byteOffset, data.byteLength),\n mimeFromExt(relPath),\n );\n throwIfAborted(signal);\n }\n\n return resolveContainer(\n container,\n 'folder',\n `No markdown document or doc.json found in folder: ${dirPath}`,\n signal,\n );\n}\n\nfunction throwIfAborted(signal?: AbortSignal): void {\n if (!signal?.aborted) return;\n if (signal.reason !== undefined) throw signal.reason;\n const error = new Error('Input reading was cancelled');\n error.name = 'AbortError';\n throw error;\n}\n"],"mappings":";;;;;;;;;;AA4BA,SAAS,4BAA4B;AACrC,SAAS,qBAAqB;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AC;AAChF,UAAM;AAAA,EACR;AACF;","names":["readFile","rm","join","tmpdir","randomBytes","renderDocToMp4","renderDocToGif","MemoryContentContainer","readFile","join","defaultRegistry","framesToMp4Native","framesToGifNative","rm","join"]}