sketchmark 2.1.8 → 2.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -83
- package/dist/src/browser-export.d.ts +1 -1
- package/dist/src/browser-export.js +6 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/mp4-muxer-source.d.ts +1 -0
- package/dist/src/mp4-muxer-source.js +5 -0
- package/dist/src/render/embed.d.ts +11 -0
- package/dist/src/render/embed.js +698 -0
- package/dist/tests/run.js +32 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -181,94 +181,74 @@ const visual = applyPresetFragments(
|
|
|
181
181
|
|
|
182
182
|
The output `.visual.json` still contains only kernel elements and timelines. Official namespaces include `shapes`, `characters`, `motions`, `effects`, `transitions`, and `scenes`.
|
|
183
183
|
|
|
184
|
-
## CLI
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
```bash
|
|
189
|
-
npm run build
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
Render:
|
|
193
|
-
|
|
194
|
-
```bash
|
|
195
|
-
node bin/sketchmark.cjs render
|
|
196
|
-
node bin/sketchmark.cjs render
|
|
197
|
-
node bin/sketchmark.cjs render
|
|
198
|
-
node bin/sketchmark.cjs render
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
```bash
|
|
204
|
-
node
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
```
|
|
218
|
-
node examples/make-presets-demo.cjs
|
|
219
|
-
node examples/make-preset-character-motion.cjs
|
|
220
|
-
node bin/sketchmark.cjs preview examples/presets-demo.visual.json
|
|
221
|
-
node bin/sketchmark.cjs preview examples/preset-character-motion.visual.json
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
Generate heavier real-world stress scenes:
|
|
225
|
-
|
|
226
|
-
```bash
|
|
227
|
-
node examples/make-stress-city-traffic.cjs
|
|
228
|
-
node examples/make-stress-ops-dashboard.cjs
|
|
229
|
-
node examples/make-stress-airport-radar.cjs
|
|
230
|
-
|
|
231
|
-
node bin/sketchmark.cjs preview examples/stress-city-traffic.visual.json
|
|
232
|
-
node bin/sketchmark.cjs preview examples/stress-ops-dashboard.visual.json
|
|
233
|
-
node bin/sketchmark.cjs preview examples/stress-airport-radar.visual.json
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
These are intentionally dense (many elements/keyframes) to pressure test preview responsiveness.
|
|
237
|
-
|
|
238
|
-
Preview animated timelines in the browser:
|
|
239
|
-
|
|
240
|
-
```bash
|
|
241
|
-
node bin/sketchmark.cjs preview examples/timeline.visual.json
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
Open the tiny editor:
|
|
245
|
-
|
|
246
|
-
```bash
|
|
247
|
-
node bin/sketchmark.cjs edit examples/keypose-walk.visual.json
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
Lint:
|
|
251
|
-
|
|
252
|
-
```bash
|
|
253
|
-
node bin/sketchmark.cjs lint examples/basic.visual.json
|
|
254
|
-
```
|
|
184
|
+
## CLI
|
|
185
|
+
|
|
186
|
+
If you're working from a source checkout, build first:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
npm run build
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Render:
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
node bin/sketchmark.cjs render path/to/diagram.visual.json out.svg
|
|
196
|
+
node bin/sketchmark.cjs render path/to/diagram.visual.json out.html --time 1
|
|
197
|
+
node bin/sketchmark.cjs render path/to/diagram.visual.json out.mp4
|
|
198
|
+
node bin/sketchmark.cjs render path/to/diagram.visual.json out.webm --fps 30
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Preview animated timelines in the browser:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
node bin/sketchmark.cjs preview path/to/diagram.visual.json
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Open the tiny editor:
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
node bin/sketchmark.cjs edit path/to/diagram.visual.json
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Lint:
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
node bin/sketchmark.cjs lint path/to/diagram.visual.json
|
|
217
|
+
```
|
|
255
218
|
|
|
256
219
|
Video export is an adapter, not a kernel feature. It samples the document timeline into SVG frames, rasterizes those frames, and hands them to `ffmpeg`. It requires `sharp` and `ffmpeg` to be available in the local environment.
|
|
257
220
|
|
|
258
221
|
## Public API
|
|
259
222
|
|
|
260
223
|
```ts
|
|
261
|
-
import {
|
|
262
|
-
validateVisualDocument,
|
|
263
|
-
compileKeyframeStates,
|
|
264
|
-
timelineCurvePreset,
|
|
265
|
-
resolveVisualFrame,
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
224
|
+
import {
|
|
225
|
+
validateVisualDocument,
|
|
226
|
+
compileKeyframeStates,
|
|
227
|
+
timelineCurvePreset,
|
|
228
|
+
resolveVisualFrame,
|
|
229
|
+
renderToEmbedHtml,
|
|
230
|
+
renderToSvg,
|
|
231
|
+
renderToHtml,
|
|
232
|
+
lintVisualDocument
|
|
233
|
+
} from "sketchmark";
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
For a reusable browser preview with play/pause and export controls, generate a self-contained HTML embed and mount it in an `iframe` via `srcDoc`:
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
import { renderToEmbedHtml } from "sketchmark";
|
|
240
|
+
|
|
241
|
+
const html = renderToEmbedHtml(doc, {
|
|
242
|
+
title: "Login Flow",
|
|
243
|
+
maxFrames: 180
|
|
244
|
+
});
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
`renderToEmbedHtml()` inlines sampled SVG frames so the preview can play without a server route. Use `maxFrames` to trade file size for smoother motion.
|
|
248
|
+
The embed chrome defaults to a transparent outer background and uses light/dark-aware translucent controls so it can sit inside either theme more naturally.
|
|
249
|
+
For broad embed-host compatibility, prefer self-contained HTML with inline scripts/styles, avoid CDN dependencies, avoid dynamic module imports from blob/object URLs, and avoid using blob URLs for runtime-loaded assets when a `data:` URL or other inline form will work.
|
|
250
|
+
The embed export menu includes MP4 when the browser supports WebCodecs, so Chrome and Edge are the safest targets for video export.
|
|
251
|
+
|
|
252
|
+
The root package intentionally exports no builders, player, project loader, deck/sequence helpers, 3D renderer, or preset compiler.
|
|
273
253
|
|
|
274
254
|
The official preset authoring layer is available from `sketchmark/presets`, not the root kernel entrypoint.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { VisualDocument } from "./types";
|
|
2
|
-
export type BrowserExportFormat = "svg" | "png" | "jpg" | "html" | "json" | "mp4" | "webm";
|
|
2
|
+
export type BrowserExportFormat = "svg" | "png" | "jpg" | "html" | "embed" | "json" | "mp4" | "webm";
|
|
3
3
|
export type BrowserExportOptions = {
|
|
4
4
|
format: BrowserExportFormat;
|
|
5
5
|
title: string;
|
|
@@ -35,6 +35,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.exportVisualInBrowser = exportVisualInBrowser;
|
|
37
37
|
const html_1 = require("./render/html");
|
|
38
|
+
const embed_1 = require("./render/embed");
|
|
38
39
|
const svg_1 = require("./render/svg");
|
|
39
40
|
async function exportVisualInBrowser(document, options) {
|
|
40
41
|
const format = options.format;
|
|
@@ -55,6 +56,11 @@ async function exportVisualInBrowser(document, options) {
|
|
|
55
56
|
options.onProgress?.(100);
|
|
56
57
|
return;
|
|
57
58
|
}
|
|
59
|
+
if (format === "embed") {
|
|
60
|
+
downloadBlob(new Blob([(0, embed_1.renderToEmbedHtml)(options.sourceDocument ?? document, { title: options.title, time })], { type: "text/html;charset=utf-8" }), `${title}.embed.html`);
|
|
61
|
+
options.onProgress?.(100);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
58
64
|
if (format === "png" || format === "jpg") {
|
|
59
65
|
await exportRasterFrame(document, { format, title, time });
|
|
60
66
|
options.onProgress?.(100);
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -29,3 +29,4 @@ Object.defineProperty(exports, "reorderElement", { enumerable: true, get: functi
|
|
|
29
29
|
__exportStar(require("./animatable"), exports);
|
|
30
30
|
__exportStar(require("./render/svg"), exports);
|
|
31
31
|
__exportStar(require("./render/html"), exports);
|
|
32
|
+
__exportStar(require("./render/embed"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const MP4_MUXER_SOURCE = "var __accessCheck = (obj, member, msg) => {\n if (!member.has(obj))\n throw TypeError(\"Cannot \" + msg);\n};\nvar __privateGet = (obj, member, getter) => {\n __accessCheck(obj, member, \"read from private field\");\n return getter ? getter.call(obj) : member.get(obj);\n};\nvar __privateAdd = (obj, member, value) => {\n if (member.has(obj))\n throw TypeError(\"Cannot add the same private member more than once\");\n member instanceof WeakSet ? member.add(obj) : member.set(obj, value);\n};\nvar __privateSet = (obj, member, value, setter) => {\n __accessCheck(obj, member, \"write to private field\");\n setter ? setter.call(obj, value) : member.set(obj, value);\n return value;\n};\nvar __privateWrapper = (obj, member, setter, getter) => ({\n set _(value) {\n __privateSet(obj, member, value, setter);\n },\n get _() {\n return __privateGet(obj, member, getter);\n }\n});\nvar __privateMethod = (obj, member, method) => {\n __accessCheck(obj, member, \"access private method\");\n return method;\n};\n\n// src/misc.ts\nvar bytes = new Uint8Array(8);\nvar view = new DataView(bytes.buffer);\nvar u8 = (value) => {\n return [(value % 256 + 256) % 256];\n};\nvar u16 = (value) => {\n view.setUint16(0, value, false);\n return [bytes[0], bytes[1]];\n};\nvar i16 = (value) => {\n view.setInt16(0, value, false);\n return [bytes[0], bytes[1]];\n};\nvar u24 = (value) => {\n view.setUint32(0, value, false);\n return [bytes[1], bytes[2], bytes[3]];\n};\nvar u32 = (value) => {\n view.setUint32(0, value, false);\n return [bytes[0], bytes[1], bytes[2], bytes[3]];\n};\nvar i32 = (value) => {\n view.setInt32(0, value, false);\n return [bytes[0], bytes[1], bytes[2], bytes[3]];\n};\nvar u64 = (value) => {\n view.setUint32(0, Math.floor(value / 2 ** 32), false);\n view.setUint32(4, value, false);\n return [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]];\n};\nvar fixed_8_8 = (value) => {\n view.setInt16(0, 2 ** 8 * value, false);\n return [bytes[0], bytes[1]];\n};\nvar fixed_16_16 = (value) => {\n view.setInt32(0, 2 ** 16 * value, false);\n return [bytes[0], bytes[1], bytes[2], bytes[3]];\n};\nvar fixed_2_30 = (value) => {\n view.setInt32(0, 2 ** 30 * value, false);\n return [bytes[0], bytes[1], bytes[2], bytes[3]];\n};\nvar ascii = (text, nullTerminated = false) => {\n let bytes2 = Array(text.length).fill(null).map((_, i) => text.charCodeAt(i));\n if (nullTerminated)\n bytes2.push(0);\n return bytes2;\n};\nvar last = (arr) => {\n return arr && arr[arr.length - 1];\n};\nvar lastPresentedSample = (samples) => {\n let result = void 0;\n for (let sample of samples) {\n if (!result || sample.presentationTimestamp > result.presentationTimestamp) {\n result = sample;\n }\n }\n return result;\n};\nvar intoTimescale = (timeInSeconds, timescale, round = true) => {\n let value = timeInSeconds * timescale;\n return round ? Math.round(value) : value;\n};\nvar rotationMatrix = (rotationInDegrees) => {\n let theta = rotationInDegrees * (Math.PI / 180);\n let cosTheta = Math.cos(theta);\n let sinTheta = Math.sin(theta);\n return [\n cosTheta,\n sinTheta,\n 0,\n -sinTheta,\n cosTheta,\n 0,\n 0,\n 0,\n 1\n ];\n};\nvar IDENTITY_MATRIX = rotationMatrix(0);\nvar matrixToBytes = (matrix) => {\n return [\n fixed_16_16(matrix[0]),\n fixed_16_16(matrix[1]),\n fixed_2_30(matrix[2]),\n fixed_16_16(matrix[3]),\n fixed_16_16(matrix[4]),\n fixed_2_30(matrix[5]),\n fixed_16_16(matrix[6]),\n fixed_16_16(matrix[7]),\n fixed_2_30(matrix[8])\n ];\n};\nvar deepClone = (x) => {\n if (!x)\n return x;\n if (typeof x !== \"object\")\n return x;\n if (Array.isArray(x))\n return x.map(deepClone);\n return Object.fromEntries(Object.entries(x).map(([key, value]) => [key, deepClone(value)]));\n};\nvar isU32 = (value) => {\n return value >= 0 && value < 2 ** 32;\n};\n\n// src/box.ts\nvar box = (type, contents, children) => ({\n type,\n contents: contents && new Uint8Array(contents.flat(10)),\n children\n});\nvar fullBox = (type, version, flags, contents, children) => box(\n type,\n [u8(version), u24(flags), contents ?? []],\n children\n);\nvar ftyp = (details) => {\n let minorVersion = 512;\n if (details.fragmented)\n return box(\"ftyp\", [\n ascii(\"iso5\"),\n // Major brand\n u32(minorVersion),\n // Minor version\n // Compatible brands\n ascii(\"iso5\"),\n ascii(\"iso6\"),\n ascii(\"mp41\")\n ]);\n return box(\"ftyp\", [\n ascii(\"isom\"),\n // Major brand\n u32(minorVersion),\n // Minor version\n // Compatible brands\n ascii(\"isom\"),\n details.holdsAvc ? ascii(\"avc1\") : [],\n ascii(\"mp41\")\n ]);\n};\nvar mdat = (reserveLargeSize) => ({ type: \"mdat\", largeSize: reserveLargeSize });\nvar free = (size) => ({ type: \"free\", size });\nvar moov = (tracks, creationTime, fragmented = false) => box(\"moov\", null, [\n mvhd(creationTime, tracks),\n ...tracks.map((x) => trak(x, creationTime)),\n fragmented ? mvex(tracks) : null\n]);\nvar mvhd = (creationTime, tracks) => {\n let duration = intoTimescale(Math.max(\n 0,\n ...tracks.filter((x) => x.samples.length > 0).map((x) => {\n const lastSample = lastPresentedSample(x.samples);\n return lastSample.presentationTimestamp + lastSample.duration;\n })\n ), GLOBAL_TIMESCALE);\n let nextTrackId = Math.max(...tracks.map((x) => x.id)) + 1;\n let needsU64 = !isU32(creationTime) || !isU32(duration);\n let u32OrU64 = needsU64 ? u64 : u32;\n return fullBox(\"mvhd\", +needsU64, 0, [\n u32OrU64(creationTime),\n // Creation time\n u32OrU64(creationTime),\n // Modification time\n u32(GLOBAL_TIMESCALE),\n // Timescale\n u32OrU64(duration),\n // Duration\n fixed_16_16(1),\n // Preferred rate\n fixed_8_8(1),\n // Preferred volume\n Array(10).fill(0),\n // Reserved\n matrixToBytes(IDENTITY_MATRIX),\n // Matrix\n Array(24).fill(0),\n // Pre-defined\n u32(nextTrackId)\n // Next track ID\n ]);\n};\nvar trak = (track, creationTime) => box(\"trak\", null, [\n tkhd(track, creationTime),\n mdia(track, creationTime)\n]);\nvar tkhd = (track, creationTime) => {\n let lastSample = lastPresentedSample(track.samples);\n let durationInGlobalTimescale = intoTimescale(\n lastSample ? lastSample.presentationTimestamp + lastSample.duration : 0,\n GLOBAL_TIMESCALE\n );\n let needsU64 = !isU32(creationTime) || !isU32(durationInGlobalTimescale);\n let u32OrU64 = needsU64 ? u64 : u32;\n let matrix;\n if (track.info.type === \"video\") {\n matrix = typeof track.info.rotation === \"number\" ? rotationMatrix(track.info.rotation) : track.info.rotation;\n } else {\n matrix = IDENTITY_MATRIX;\n }\n return fullBox(\"tkhd\", +needsU64, 3, [\n u32OrU64(creationTime),\n // Creation time\n u32OrU64(creationTime),\n // Modification time\n u32(track.id),\n // Track ID\n u32(0),\n // Reserved\n u32OrU64(durationInGlobalTimescale),\n // Duration\n Array(8).fill(0),\n // Reserved\n u16(0),\n // Layer\n u16(0),\n // Alternate group\n fixed_8_8(track.info.type === \"audio\" ? 1 : 0),\n // Volume\n u16(0),\n // Reserved\n matrixToBytes(matrix),\n // Matrix\n fixed_16_16(track.info.type === \"video\" ? track.info.width : 0),\n // Track width\n fixed_16_16(track.info.type === \"video\" ? track.info.height : 0)\n // Track height\n ]);\n};\nvar mdia = (track, creationTime) => box(\"mdia\", null, [\n mdhd(track, creationTime),\n hdlr(track.info.type === \"video\" ? \"vide\" : \"soun\"),\n minf(track)\n]);\nvar mdhd = (track, creationTime) => {\n let lastSample = lastPresentedSample(track.samples);\n let localDuration = intoTimescale(\n lastSample ? lastSample.presentationTimestamp + lastSample.duration : 0,\n track.timescale\n );\n let needsU64 = !isU32(creationTime) || !isU32(localDuration);\n let u32OrU64 = needsU64 ? u64 : u32;\n return fullBox(\"mdhd\", +needsU64, 0, [\n u32OrU64(creationTime),\n // Creation time\n u32OrU64(creationTime),\n // Modification time\n u32(track.timescale),\n // Timescale\n u32OrU64(localDuration),\n // Duration\n u16(21956),\n // Language (\"und\", undetermined)\n u16(0)\n // Quality\n ]);\n};\nvar hdlr = (componentSubtype) => fullBox(\"hdlr\", 0, 0, [\n ascii(\"mhlr\"),\n // Component type\n ascii(componentSubtype),\n // Component subtype\n u32(0),\n // Component manufacturer\n u32(0),\n // Component flags\n u32(0),\n // Component flags mask\n ascii(\"mp4-muxer-hdlr\", true)\n // Component name\n]);\nvar minf = (track) => box(\"minf\", null, [\n track.info.type === \"video\" ? vmhd() : smhd(),\n dinf(),\n stbl(track)\n]);\nvar vmhd = () => fullBox(\"vmhd\", 0, 1, [\n u16(0),\n // Graphics mode\n u16(0),\n // Opcolor R\n u16(0),\n // Opcolor G\n u16(0)\n // Opcolor B\n]);\nvar smhd = () => fullBox(\"smhd\", 0, 0, [\n u16(0),\n // Balance\n u16(0)\n // Reserved\n]);\nvar dinf = () => box(\"dinf\", null, [\n dref()\n]);\nvar dref = () => fullBox(\"dref\", 0, 0, [\n u32(1)\n // Entry count\n], [\n url()\n]);\nvar url = () => fullBox(\"url \", 0, 1);\nvar stbl = (track) => {\n const needsCtts = track.compositionTimeOffsetTable.length > 1 || track.compositionTimeOffsetTable.some((x) => x.sampleCompositionTimeOffset !== 0);\n return box(\"stbl\", null, [\n stsd(track),\n stts(track),\n stss(track),\n stsc(track),\n stsz(track),\n stco(track),\n needsCtts ? ctts(track) : null\n ]);\n};\nvar stsd = (track) => fullBox(\"stsd\", 0, 0, [\n u32(1)\n // Entry count\n], [\n track.info.type === \"video\" ? videoSampleDescription(\n VIDEO_CODEC_TO_BOX_NAME[track.info.codec],\n track\n ) : soundSampleDescription(\n AUDIO_CODEC_TO_BOX_NAME[track.info.codec],\n track\n )\n]);\nvar videoSampleDescription = (compressionType, track) => box(compressionType, [\n Array(6).fill(0),\n // Reserved\n u16(1),\n // Data reference index\n u16(0),\n // Pre-defined\n u16(0),\n // Reserved\n Array(12).fill(0),\n // Pre-defined\n u16(track.info.width),\n // Width\n u16(track.info.height),\n // Height\n u32(4718592),\n // Horizontal resolution\n u32(4718592),\n // Vertical resolution\n u32(0),\n // Reserved\n u16(1),\n // Frame count\n Array(32).fill(0),\n // Compressor name\n u16(24),\n // Depth\n i16(65535)\n // Pre-defined\n], [\n VIDEO_CODEC_TO_CONFIGURATION_BOX[track.info.codec](track),\n track.info.decoderConfig.colorSpace ? colr(track) : null\n]);\nvar COLOR_PRIMARIES_MAP = {\n \"bt709\": 1,\n // ITU-R BT.709\n \"bt470bg\": 5,\n // ITU-R BT.470BG\n \"smpte170m\": 6\n // ITU-R BT.601 525 - SMPTE 170M\n};\nvar TRANSFER_CHARACTERISTICS_MAP = {\n \"bt709\": 1,\n // ITU-R BT.709\n \"smpte170m\": 6,\n // SMPTE 170M\n \"iec61966-2-1\": 13\n // IEC 61966-2-1\n};\nvar MATRIX_COEFFICIENTS_MAP = {\n \"rgb\": 0,\n // Identity\n \"bt709\": 1,\n // ITU-R BT.709\n \"bt470bg\": 5,\n // ITU-R BT.470BG\n \"smpte170m\": 6\n // SMPTE 170M\n};\nvar colr = (track) => box(\"colr\", [\n ascii(\"nclx\"),\n // Colour type\n u16(COLOR_PRIMARIES_MAP[track.info.decoderConfig.colorSpace.primaries]),\n // Colour primaries\n u16(TRANSFER_CHARACTERISTICS_MAP[track.info.decoderConfig.colorSpace.transfer]),\n // Transfer characteristics\n u16(MATRIX_COEFFICIENTS_MAP[track.info.decoderConfig.colorSpace.matrix]),\n // Matrix coefficients\n u8((track.info.decoderConfig.colorSpace.fullRange ? 1 : 0) << 7)\n // Full range flag\n]);\nvar avcC = (track) => track.info.decoderConfig && box(\"avcC\", [\n // For AVC, description is an AVCDecoderConfigurationRecord, so nothing else to do here\n ...new Uint8Array(track.info.decoderConfig.description)\n]);\nvar hvcC = (track) => track.info.decoderConfig && box(\"hvcC\", [\n // For HEVC, description is a HEVCDecoderConfigurationRecord, so nothing else to do here\n ...new Uint8Array(track.info.decoderConfig.description)\n]);\nvar vpcC = (track) => {\n if (!track.info.decoderConfig) {\n return null;\n }\n let decoderConfig = track.info.decoderConfig;\n if (!decoderConfig.colorSpace) {\n throw new Error(`'colorSpace' is required in the decoder config for VP9.`);\n }\n let parts = decoderConfig.codec.split(\".\");\n let profile = Number(parts[1]);\n let level = Number(parts[2]);\n let bitDepth = Number(parts[3]);\n let chromaSubsampling = 0;\n let thirdByte = (bitDepth << 4) + (chromaSubsampling << 1) + Number(decoderConfig.colorSpace.fullRange);\n let colourPrimaries = 2;\n let transferCharacteristics = 2;\n let matrixCoefficients = 2;\n return fullBox(\"vpcC\", 1, 0, [\n u8(profile),\n // Profile\n u8(level),\n // Level\n u8(thirdByte),\n // Bit depth, chroma subsampling, full range\n u8(colourPrimaries),\n // Colour primaries\n u8(transferCharacteristics),\n // Transfer characteristics\n u8(matrixCoefficients),\n // Matrix coefficients\n u16(0)\n // Codec initialization data size\n ]);\n};\nvar av1C = () => {\n let marker = 1;\n let version = 1;\n let firstByte = (marker << 7) + version;\n return box(\"av1C\", [\n firstByte,\n 0,\n 0,\n 0\n ]);\n};\nvar soundSampleDescription = (compressionType, track) => box(compressionType, [\n Array(6).fill(0),\n // Reserved\n u16(1),\n // Data reference index\n u16(0),\n // Version\n u16(0),\n // Revision level\n u32(0),\n // Vendor\n u16(track.info.numberOfChannels),\n // Number of channels\n u16(16),\n // Sample size (bits)\n u16(0),\n // Compression ID\n u16(0),\n // Packet size\n fixed_16_16(track.info.sampleRate)\n // Sample rate\n], [\n AUDIO_CODEC_TO_CONFIGURATION_BOX[track.info.codec](track)\n]);\nvar esds = (track) => {\n let description = new Uint8Array(track.info.decoderConfig.description);\n return fullBox(\"esds\", 0, 0, [\n // https://stackoverflow.com/a/54803118\n u32(58753152),\n // TAG(3) = Object Descriptor ([2])\n u8(32 + description.byteLength),\n // length of this OD (which includes the next 2 tags)\n u16(1),\n // ES_ID = 1\n u8(0),\n // flags etc = 0\n u32(75530368),\n // TAG(4) = ES Descriptor ([2]) embedded in above OD\n u8(18 + description.byteLength),\n // length of this ESD\n u8(64),\n // MPEG-4 Audio\n u8(21),\n // stream type(6bits)=5 audio, flags(2bits)=1\n u24(0),\n // 24bit buffer size\n u32(130071),\n // max bitrate\n u32(130071),\n // avg bitrate\n u32(92307584),\n // TAG(5) = ASC ([2],[3]) embedded in above OD\n u8(description.byteLength),\n // length\n ...description,\n u32(109084800),\n // TAG(6)\n u8(1),\n // length\n u8(2)\n // data\n ]);\n};\nvar dOps = (track) => {\n let preskip = 3840;\n let gain = 0;\n const description = track.info.decoderConfig?.description;\n if (description) {\n if (description.byteLength < 18) {\n throw new TypeError(\"Invalid decoder description provided for Opus; must be at least 18 bytes long.\");\n }\n const view2 = ArrayBuffer.isView(description) ? new DataView(description.buffer, description.byteOffset, description.byteLength) : new DataView(description);\n preskip = view2.getUint16(10, true);\n gain = view2.getInt16(14, true);\n }\n return box(\"dOps\", [\n u8(0),\n // Version\n u8(track.info.numberOfChannels),\n // OutputChannelCount\n u16(preskip),\n u32(track.info.sampleRate),\n // InputSampleRate\n fixed_8_8(gain),\n // OutputGain\n u8(0)\n // ChannelMappingFamily\n ]);\n};\nvar stts = (track) => {\n return fullBox(\"stts\", 0, 0, [\n u32(track.timeToSampleTable.length),\n // Number of entries\n track.timeToSampleTable.map((x) => [\n // Time-to-sample table\n u32(x.sampleCount),\n // Sample count\n u32(x.sampleDelta)\n // Sample duration\n ])\n ]);\n};\nvar stss = (track) => {\n if (track.samples.every((x) => x.type === \"key\"))\n return null;\n let keySamples = [...track.samples.entries()].filter(([, sample]) => sample.type === \"key\");\n return fullBox(\"stss\", 0, 0, [\n u32(keySamples.length),\n // Number of entries\n keySamples.map(([index]) => u32(index + 1))\n // Sync sample table\n ]);\n};\nvar stsc = (track) => {\n return fullBox(\"stsc\", 0, 0, [\n u32(track.compactlyCodedChunkTable.length),\n // Number of entries\n track.compactlyCodedChunkTable.map((x) => [\n // Sample-to-chunk table\n u32(x.firstChunk),\n // First chunk\n u32(x.samplesPerChunk),\n // Samples per chunk\n u32(1)\n // Sample description index\n ])\n ]);\n};\nvar stsz = (track) => fullBox(\"stsz\", 0, 0, [\n u32(0),\n // Sample size (0 means non-constant size)\n u32(track.samples.length),\n // Number of entries\n track.samples.map((x) => u32(x.size))\n // Sample size table\n]);\nvar stco = (track) => {\n if (track.finalizedChunks.length > 0 && last(track.finalizedChunks).offset >= 2 ** 32) {\n return fullBox(\"co64\", 0, 0, [\n u32(track.finalizedChunks.length),\n // Number of entries\n track.finalizedChunks.map((x) => u64(x.offset))\n // Chunk offset table\n ]);\n }\n return fullBox(\"stco\", 0, 0, [\n u32(track.finalizedChunks.length),\n // Number of entries\n track.finalizedChunks.map((x) => u32(x.offset))\n // Chunk offset table\n ]);\n};\nvar ctts = (track) => {\n return fullBox(\"ctts\", 0, 0, [\n u32(track.compositionTimeOffsetTable.length),\n // Number of entries\n track.compositionTimeOffsetTable.map((x) => [\n // Time-to-sample table\n u32(x.sampleCount),\n // Sample count\n u32(x.sampleCompositionTimeOffset)\n // Sample offset\n ])\n ]);\n};\nvar mvex = (tracks) => {\n return box(\"mvex\", null, tracks.map(trex));\n};\nvar trex = (track) => {\n return fullBox(\"trex\", 0, 0, [\n u32(track.id),\n // Track ID\n u32(1),\n // Default sample description index\n u32(0),\n // Default sample duration\n u32(0),\n // Default sample size\n u32(0)\n // Default sample flags\n ]);\n};\nvar moof = (sequenceNumber, tracks) => {\n return box(\"moof\", null, [\n mfhd(sequenceNumber),\n ...tracks.map(traf)\n ]);\n};\nvar mfhd = (sequenceNumber) => {\n return fullBox(\"mfhd\", 0, 0, [\n u32(sequenceNumber)\n // Sequence number\n ]);\n};\nvar fragmentSampleFlags = (sample) => {\n let byte1 = 0;\n let byte2 = 0;\n let byte3 = 0;\n let byte4 = 0;\n let sampleIsDifferenceSample = sample.type === \"delta\";\n byte2 |= +sampleIsDifferenceSample;\n if (sampleIsDifferenceSample) {\n byte1 |= 1;\n } else {\n byte1 |= 2;\n }\n return byte1 << 24 | byte2 << 16 | byte3 << 8 | byte4;\n};\nvar traf = (track) => {\n return box(\"traf\", null, [\n tfhd(track),\n tfdt(track),\n trun(track)\n ]);\n};\nvar tfhd = (track) => {\n let tfFlags = 0;\n tfFlags |= 8;\n tfFlags |= 16;\n tfFlags |= 32;\n tfFlags |= 131072;\n let referenceSample = track.currentChunk.samples[1] ?? track.currentChunk.samples[0];\n let referenceSampleInfo = {\n duration: referenceSample.timescaleUnitsToNextSample,\n size: referenceSample.size,\n flags: fragmentSampleFlags(referenceSample)\n };\n return fullBox(\"tfhd\", 0, tfFlags, [\n u32(track.id),\n // Track ID\n u32(referenceSampleInfo.duration),\n // Default sample duration\n u32(referenceSampleInfo.size),\n // Default sample size\n u32(referenceSampleInfo.flags)\n // Default sample flags\n ]);\n};\nvar tfdt = (track) => {\n return fullBox(\"tfdt\", 1, 0, [\n u64(intoTimescale(track.currentChunk.startTimestamp, track.timescale))\n // Base Media Decode Time\n ]);\n};\nvar trun = (track) => {\n let allSampleDurations = track.currentChunk.samples.map((x) => x.timescaleUnitsToNextSample);\n let allSampleSizes = track.currentChunk.samples.map((x) => x.size);\n let allSampleFlags = track.currentChunk.samples.map(fragmentSampleFlags);\n let allSampleCompositionTimeOffsets = track.currentChunk.samples.map((x) => intoTimescale(x.presentationTimestamp - x.decodeTimestamp, track.timescale));\n let uniqueSampleDurations = new Set(allSampleDurations);\n let uniqueSampleSizes = new Set(allSampleSizes);\n let uniqueSampleFlags = new Set(allSampleFlags);\n let uniqueSampleCompositionTimeOffsets = new Set(allSampleCompositionTimeOffsets);\n let firstSampleFlagsPresent = uniqueSampleFlags.size === 2 && allSampleFlags[0] !== allSampleFlags[1];\n let sampleDurationPresent = uniqueSampleDurations.size > 1;\n let sampleSizePresent = uniqueSampleSizes.size > 1;\n let sampleFlagsPresent = !firstSampleFlagsPresent && uniqueSampleFlags.size > 1;\n let sampleCompositionTimeOffsetsPresent = uniqueSampleCompositionTimeOffsets.size > 1 || [...uniqueSampleCompositionTimeOffsets].some((x) => x !== 0);\n let flags = 0;\n flags |= 1;\n flags |= 4 * +firstSampleFlagsPresent;\n flags |= 256 * +sampleDurationPresent;\n flags |= 512 * +sampleSizePresent;\n flags |= 1024 * +sampleFlagsPresent;\n flags |= 2048 * +sampleCompositionTimeOffsetsPresent;\n return fullBox(\"trun\", 1, flags, [\n u32(track.currentChunk.samples.length),\n // Sample count\n u32(track.currentChunk.offset - track.currentChunk.moofOffset || 0),\n // Data offset\n firstSampleFlagsPresent ? u32(allSampleFlags[0]) : [],\n track.currentChunk.samples.map((_, i) => [\n sampleDurationPresent ? u32(allSampleDurations[i]) : [],\n // Sample duration\n sampleSizePresent ? u32(allSampleSizes[i]) : [],\n // Sample size\n sampleFlagsPresent ? u32(allSampleFlags[i]) : [],\n // Sample flags\n // Sample composition time offsets\n sampleCompositionTimeOffsetsPresent ? i32(allSampleCompositionTimeOffsets[i]) : []\n ])\n ]);\n};\nvar mfra = (tracks) => {\n return box(\"mfra\", null, [\n ...tracks.map(tfra),\n mfro()\n ]);\n};\nvar tfra = (track, trackIndex) => {\n let version = 1;\n return fullBox(\"tfra\", version, 0, [\n u32(track.id),\n // Track ID\n u32(63),\n // This specifies that traf number, trun number and sample number are 32-bit ints\n u32(track.finalizedChunks.length),\n // Number of entries\n track.finalizedChunks.map((chunk) => [\n u64(intoTimescale(chunk.startTimestamp, track.timescale)),\n // Time\n u64(chunk.moofOffset),\n // moof offset\n u32(trackIndex + 1),\n // traf number\n u32(1),\n // trun number\n u32(1)\n // Sample number\n ])\n ]);\n};\nvar mfro = () => {\n return fullBox(\"mfro\", 0, 0, [\n // This value needs to be overwritten manually from the outside, where the actual size of the enclosing mfra box\n // is known\n u32(0)\n // Size\n ]);\n};\nvar VIDEO_CODEC_TO_BOX_NAME = {\n \"avc\": \"avc1\",\n \"hevc\": \"hvc1\",\n \"vp9\": \"vp09\",\n \"av1\": \"av01\"\n};\nvar VIDEO_CODEC_TO_CONFIGURATION_BOX = {\n \"avc\": avcC,\n \"hevc\": hvcC,\n \"vp9\": vpcC,\n \"av1\": av1C\n};\nvar AUDIO_CODEC_TO_BOX_NAME = {\n \"aac\": \"mp4a\",\n \"opus\": \"Opus\"\n};\nvar AUDIO_CODEC_TO_CONFIGURATION_BOX = {\n \"aac\": esds,\n \"opus\": dOps\n};\n\n// src/target.ts\nvar isTarget = Symbol(\"isTarget\");\nvar Target = class {\n};\nisTarget;\nvar ArrayBufferTarget = class extends Target {\n constructor() {\n super(...arguments);\n this.buffer = null;\n }\n};\nvar StreamTarget = class extends Target {\n constructor(options) {\n super();\n this.options = options;\n if (typeof options !== \"object\") {\n throw new TypeError(\"StreamTarget requires an options object to be passed to its constructor.\");\n }\n if (options.onData) {\n if (typeof options.onData !== \"function\") {\n throw new TypeError(\"options.onData, when provided, must be a function.\");\n }\n if (options.onData.length < 2) {\n throw new TypeError(\n \"options.onData, when provided, must be a function that takes in at least two arguments (data and position). Ignoring the position argument, which specifies the byte offset at which the data is to be written, can lead to broken outputs.\"\n );\n }\n }\n if (options.chunked !== void 0 && typeof options.chunked !== \"boolean\") {\n throw new TypeError(\"options.chunked, when provided, must be a boolean.\");\n }\n if (options.chunkSize !== void 0 && (!Number.isInteger(options.chunkSize) || options.chunkSize < 1024)) {\n throw new TypeError(\"options.chunkSize, when provided, must be an integer and not smaller than 1024.\");\n }\n }\n};\nvar FileSystemWritableFileStreamTarget = class extends Target {\n constructor(stream, options) {\n super();\n this.stream = stream;\n this.options = options;\n if (!(stream instanceof FileSystemWritableFileStream)) {\n throw new TypeError(\"FileSystemWritableFileStreamTarget requires a FileSystemWritableFileStream instance.\");\n }\n if (options !== void 0 && typeof options !== \"object\") {\n throw new TypeError(\"FileSystemWritableFileStreamTarget's options, when provided, must be an object.\");\n }\n if (options) {\n if (options.chunkSize !== void 0 && (!Number.isInteger(options.chunkSize) || options.chunkSize <= 0)) {\n throw new TypeError(\"options.chunkSize, when provided, must be a positive integer\");\n }\n }\n }\n};\n\n// src/writer.ts\nvar _helper, _helperView;\nvar Writer = class {\n constructor() {\n this.pos = 0;\n __privateAdd(this, _helper, new Uint8Array(8));\n __privateAdd(this, _helperView, new DataView(__privateGet(this, _helper).buffer));\n /**\n * Stores the position from the start of the file to where boxes elements have been written. This is used to\n * rewrite/edit elements that were already added before, and to measure sizes of things.\n */\n this.offsets = /* @__PURE__ */ new WeakMap();\n }\n /** Sets the current position for future writes to a new one. */\n seek(newPos) {\n this.pos = newPos;\n }\n writeU32(value) {\n __privateGet(this, _helperView).setUint32(0, value, false);\n this.write(__privateGet(this, _helper).subarray(0, 4));\n }\n writeU64(value) {\n __privateGet(this, _helperView).setUint32(0, Math.floor(value / 2 ** 32), false);\n __privateGet(this, _helperView).setUint32(4, value, false);\n this.write(__privateGet(this, _helper).subarray(0, 8));\n }\n writeAscii(text) {\n for (let i = 0; i < text.length; i++) {\n __privateGet(this, _helperView).setUint8(i % 8, text.charCodeAt(i));\n if (i % 8 === 7)\n this.write(__privateGet(this, _helper));\n }\n if (text.length % 8 !== 0) {\n this.write(__privateGet(this, _helper).subarray(0, text.length % 8));\n }\n }\n writeBox(box2) {\n this.offsets.set(box2, this.pos);\n if (box2.contents && !box2.children) {\n this.writeBoxHeader(box2, box2.size ?? box2.contents.byteLength + 8);\n this.write(box2.contents);\n } else {\n let startPos = this.pos;\n this.writeBoxHeader(box2, 0);\n if (box2.contents)\n this.write(box2.contents);\n if (box2.children) {\n for (let child of box2.children)\n if (child)\n this.writeBox(child);\n }\n let endPos = this.pos;\n let size = box2.size ?? endPos - startPos;\n this.seek(startPos);\n this.writeBoxHeader(box2, size);\n this.seek(endPos);\n }\n }\n writeBoxHeader(box2, size) {\n this.writeU32(box2.largeSize ? 1 : size);\n this.writeAscii(box2.type);\n if (box2.largeSize)\n this.writeU64(size);\n }\n measureBoxHeader(box2) {\n return 8 + (box2.largeSize ? 8 : 0);\n }\n patchBox(box2) {\n let endPos = this.pos;\n this.seek(this.offsets.get(box2));\n this.writeBox(box2);\n this.seek(endPos);\n }\n measureBox(box2) {\n if (box2.contents && !box2.children) {\n let headerSize = this.measureBoxHeader(box2);\n return headerSize + box2.contents.byteLength;\n } else {\n let result = this.measureBoxHeader(box2);\n if (box2.contents)\n result += box2.contents.byteLength;\n if (box2.children) {\n for (let child of box2.children)\n if (child)\n result += this.measureBox(child);\n }\n return result;\n }\n }\n};\n_helper = new WeakMap();\n_helperView = new WeakMap();\nvar _target, _buffer, _bytes, _maxPos, _ensureSize, ensureSize_fn;\nvar ArrayBufferTargetWriter = class extends Writer {\n constructor(target) {\n super();\n __privateAdd(this, _ensureSize);\n __privateAdd(this, _target, void 0);\n __privateAdd(this, _buffer, new ArrayBuffer(2 ** 16));\n __privateAdd(this, _bytes, new Uint8Array(__privateGet(this, _buffer)));\n __privateAdd(this, _maxPos, 0);\n __privateSet(this, _target, target);\n }\n write(data) {\n __privateMethod(this, _ensureSize, ensureSize_fn).call(this, this.pos + data.byteLength);\n __privateGet(this, _bytes).set(data, this.pos);\n this.pos += data.byteLength;\n __privateSet(this, _maxPos, Math.max(__privateGet(this, _maxPos), this.pos));\n }\n finalize() {\n __privateMethod(this, _ensureSize, ensureSize_fn).call(this, this.pos);\n __privateGet(this, _target).buffer = __privateGet(this, _buffer).slice(0, Math.max(__privateGet(this, _maxPos), this.pos));\n }\n};\n_target = new WeakMap();\n_buffer = new WeakMap();\n_bytes = new WeakMap();\n_maxPos = new WeakMap();\n_ensureSize = new WeakSet();\nensureSize_fn = function(size) {\n let newLength = __privateGet(this, _buffer).byteLength;\n while (newLength < size)\n newLength *= 2;\n if (newLength === __privateGet(this, _buffer).byteLength)\n return;\n let newBuffer = new ArrayBuffer(newLength);\n let newBytes = new Uint8Array(newBuffer);\n newBytes.set(__privateGet(this, _bytes), 0);\n __privateSet(this, _buffer, newBuffer);\n __privateSet(this, _bytes, newBytes);\n};\nvar DEFAULT_CHUNK_SIZE = 2 ** 24;\nvar MAX_CHUNKS_AT_ONCE = 2;\nvar _target2, _sections, _chunked, _chunkSize, _chunks, _writeDataIntoChunks, writeDataIntoChunks_fn, _insertSectionIntoChunk, insertSectionIntoChunk_fn, _createChunk, createChunk_fn, _flushChunks, flushChunks_fn;\nvar StreamTargetWriter = class extends Writer {\n constructor(target) {\n super();\n __privateAdd(this, _writeDataIntoChunks);\n __privateAdd(this, _insertSectionIntoChunk);\n __privateAdd(this, _createChunk);\n __privateAdd(this, _flushChunks);\n __privateAdd(this, _target2, void 0);\n __privateAdd(this, _sections, []);\n __privateAdd(this, _chunked, void 0);\n __privateAdd(this, _chunkSize, void 0);\n /**\n * The data is divided up into fixed-size chunks, whose contents are first filled in RAM and then flushed out.\n * A chunk is flushed if all of its contents have been written.\n */\n __privateAdd(this, _chunks, []);\n __privateSet(this, _target2, target);\n __privateSet(this, _chunked, target.options?.chunked ?? false);\n __privateSet(this, _chunkSize, target.options?.chunkSize ?? DEFAULT_CHUNK_SIZE);\n }\n write(data) {\n __privateGet(this, _sections).push({\n data: data.slice(),\n start: this.pos\n });\n this.pos += data.byteLength;\n }\n flush() {\n if (__privateGet(this, _sections).length === 0)\n return;\n let chunks = [];\n let sorted = [...__privateGet(this, _sections)].sort((a, b) => a.start - b.start);\n chunks.push({\n start: sorted[0].start,\n size: sorted[0].data.byteLength\n });\n for (let i = 1; i < sorted.length; i++) {\n let lastChunk = chunks[chunks.length - 1];\n let section = sorted[i];\n if (section.start <= lastChunk.start + lastChunk.size) {\n lastChunk.size = Math.max(lastChunk.size, section.start + section.data.byteLength - lastChunk.start);\n } else {\n chunks.push({\n start: section.start,\n size: section.data.byteLength\n });\n }\n }\n for (let chunk of chunks) {\n chunk.data = new Uint8Array(chunk.size);\n for (let section of __privateGet(this, _sections)) {\n if (chunk.start <= section.start && section.start < chunk.start + chunk.size) {\n chunk.data.set(section.data, section.start - chunk.start);\n }\n }\n if (__privateGet(this, _chunked)) {\n __privateMethod(this, _writeDataIntoChunks, writeDataIntoChunks_fn).call(this, chunk.data, chunk.start);\n __privateMethod(this, _flushChunks, flushChunks_fn).call(this);\n } else {\n __privateGet(this, _target2).options.onData?.(chunk.data, chunk.start);\n }\n }\n __privateGet(this, _sections).length = 0;\n }\n finalize() {\n if (__privateGet(this, _chunked)) {\n __privateMethod(this, _flushChunks, flushChunks_fn).call(this, true);\n }\n }\n};\n_target2 = new WeakMap();\n_sections = new WeakMap();\n_chunked = new WeakMap();\n_chunkSize = new WeakMap();\n_chunks = new WeakMap();\n_writeDataIntoChunks = new WeakSet();\nwriteDataIntoChunks_fn = function(data, position) {\n let chunkIndex = __privateGet(this, _chunks).findIndex((x) => x.start <= position && position < x.start + __privateGet(this, _chunkSize));\n if (chunkIndex === -1)\n chunkIndex = __privateMethod(this, _createChunk, createChunk_fn).call(this, position);\n let chunk = __privateGet(this, _chunks)[chunkIndex];\n let relativePosition = position - chunk.start;\n let toWrite = data.subarray(0, Math.min(__privateGet(this, _chunkSize) - relativePosition, data.byteLength));\n chunk.data.set(toWrite, relativePosition);\n let section = {\n start: relativePosition,\n end: relativePosition + toWrite.byteLength\n };\n __privateMethod(this, _insertSectionIntoChunk, insertSectionIntoChunk_fn).call(this, chunk, section);\n if (chunk.written[0].start === 0 && chunk.written[0].end === __privateGet(this, _chunkSize)) {\n chunk.shouldFlush = true;\n }\n if (__privateGet(this, _chunks).length > MAX_CHUNKS_AT_ONCE) {\n for (let i = 0; i < __privateGet(this, _chunks).length - 1; i++) {\n __privateGet(this, _chunks)[i].shouldFlush = true;\n }\n __privateMethod(this, _flushChunks, flushChunks_fn).call(this);\n }\n if (toWrite.byteLength < data.byteLength) {\n __privateMethod(this, _writeDataIntoChunks, writeDataIntoChunks_fn).call(this, data.subarray(toWrite.byteLength), position + toWrite.byteLength);\n }\n};\n_insertSectionIntoChunk = new WeakSet();\ninsertSectionIntoChunk_fn = function(chunk, section) {\n let low = 0;\n let high = chunk.written.length - 1;\n let index = -1;\n while (low <= high) {\n let mid = Math.floor(low + (high - low + 1) / 2);\n if (chunk.written[mid].start <= section.start) {\n low = mid + 1;\n index = mid;\n } else {\n high = mid - 1;\n }\n }\n chunk.written.splice(index + 1, 0, section);\n if (index === -1 || chunk.written[index].end < section.start)\n index++;\n while (index < chunk.written.length - 1 && chunk.written[index].end >= chunk.written[index + 1].start) {\n chunk.written[index].end = Math.max(chunk.written[index].end, chunk.written[index + 1].end);\n chunk.written.splice(index + 1, 1);\n }\n};\n_createChunk = new WeakSet();\ncreateChunk_fn = function(includesPosition) {\n let start = Math.floor(includesPosition / __privateGet(this, _chunkSize)) * __privateGet(this, _chunkSize);\n let chunk = {\n start,\n data: new Uint8Array(__privateGet(this, _chunkSize)),\n written: [],\n shouldFlush: false\n };\n __privateGet(this, _chunks).push(chunk);\n __privateGet(this, _chunks).sort((a, b) => a.start - b.start);\n return __privateGet(this, _chunks).indexOf(chunk);\n};\n_flushChunks = new WeakSet();\nflushChunks_fn = function(force = false) {\n for (let i = 0; i < __privateGet(this, _chunks).length; i++) {\n let chunk = __privateGet(this, _chunks)[i];\n if (!chunk.shouldFlush && !force)\n continue;\n for (let section of chunk.written) {\n __privateGet(this, _target2).options.onData?.(\n chunk.data.subarray(section.start, section.end),\n chunk.start + section.start\n );\n }\n __privateGet(this, _chunks).splice(i--, 1);\n }\n};\nvar FileSystemWritableFileStreamTargetWriter = class extends StreamTargetWriter {\n constructor(target) {\n super(new StreamTarget({\n onData: (data, position) => target.stream.write({\n type: \"write\",\n data,\n position\n }),\n chunked: true,\n chunkSize: target.options?.chunkSize\n }));\n }\n};\n\n// src/muxer.ts\nvar GLOBAL_TIMESCALE = 1e3;\nvar SUPPORTED_VIDEO_CODECS = [\"avc\", \"hevc\", \"vp9\", \"av1\"];\nvar SUPPORTED_AUDIO_CODECS = [\"aac\", \"opus\"];\nvar TIMESTAMP_OFFSET = 2082844800;\nvar FIRST_TIMESTAMP_BEHAVIORS = [\"strict\", \"offset\", \"cross-track-offset\"];\nvar _options, _writer, _ftypSize, _mdat, _videoTrack, _audioTrack, _creationTime, _finalizedChunks, _nextFragmentNumber, _videoSampleQueue, _audioSampleQueue, _finalized, _validateOptions, validateOptions_fn, _writeHeader, writeHeader_fn, _computeMoovSizeUpperBound, computeMoovSizeUpperBound_fn, _prepareTracks, prepareTracks_fn, _generateMpeg4AudioSpecificConfig, generateMpeg4AudioSpecificConfig_fn, _createSampleForTrack, createSampleForTrack_fn, _addSampleToTrack, addSampleToTrack_fn, _validateTimestamp, validateTimestamp_fn, _finalizeCurrentChunk, finalizeCurrentChunk_fn, _finalizeFragment, finalizeFragment_fn, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn, _ensureNotFinalized, ensureNotFinalized_fn;\nvar Muxer = class {\n constructor(options) {\n __privateAdd(this, _validateOptions);\n __privateAdd(this, _writeHeader);\n __privateAdd(this, _computeMoovSizeUpperBound);\n __privateAdd(this, _prepareTracks);\n // https://wiki.multimedia.cx/index.php/MPEG-4_Audio\n __privateAdd(this, _generateMpeg4AudioSpecificConfig);\n __privateAdd(this, _createSampleForTrack);\n __privateAdd(this, _addSampleToTrack);\n __privateAdd(this, _validateTimestamp);\n __privateAdd(this, _finalizeCurrentChunk);\n __privateAdd(this, _finalizeFragment);\n __privateAdd(this, _maybeFlushStreamingTargetWriter);\n __privateAdd(this, _ensureNotFinalized);\n __privateAdd(this, _options, void 0);\n __privateAdd(this, _writer, void 0);\n __privateAdd(this, _ftypSize, void 0);\n __privateAdd(this, _mdat, void 0);\n __privateAdd(this, _videoTrack, null);\n __privateAdd(this, _audioTrack, null);\n __privateAdd(this, _creationTime, Math.floor(Date.now() / 1e3) + TIMESTAMP_OFFSET);\n __privateAdd(this, _finalizedChunks, []);\n // Fields for fragmented MP4:\n __privateAdd(this, _nextFragmentNumber, 1);\n __privateAdd(this, _videoSampleQueue, []);\n __privateAdd(this, _audioSampleQueue, []);\n __privateAdd(this, _finalized, false);\n __privateMethod(this, _validateOptions, validateOptions_fn).call(this, options);\n options.video = deepClone(options.video);\n options.audio = deepClone(options.audio);\n options.fastStart = deepClone(options.fastStart);\n this.target = options.target;\n __privateSet(this, _options, {\n firstTimestampBehavior: \"strict\",\n ...options\n });\n if (options.target instanceof ArrayBufferTarget) {\n __privateSet(this, _writer, new ArrayBufferTargetWriter(options.target));\n } else if (options.target instanceof StreamTarget) {\n __privateSet(this, _writer, new StreamTargetWriter(options.target));\n } else if (options.target instanceof FileSystemWritableFileStreamTarget) {\n __privateSet(this, _writer, new FileSystemWritableFileStreamTargetWriter(options.target));\n } else {\n throw new Error(`Invalid target: ${options.target}`);\n }\n __privateMethod(this, _prepareTracks, prepareTracks_fn).call(this);\n __privateMethod(this, _writeHeader, writeHeader_fn).call(this);\n }\n addVideoChunk(sample, meta, timestamp, compositionTimeOffset) {\n if (!(sample instanceof EncodedVideoChunk)) {\n throw new TypeError(\"addVideoChunk's first argument (sample) must be of type EncodedVideoChunk.\");\n }\n if (meta && typeof meta !== \"object\") {\n throw new TypeError(\"addVideoChunk's second argument (meta), when provided, must be an object.\");\n }\n if (timestamp !== void 0 && (!Number.isFinite(timestamp) || timestamp < 0)) {\n throw new TypeError(\n \"addVideoChunk's third argument (timestamp), when provided, must be a non-negative real number.\"\n );\n }\n if (compositionTimeOffset !== void 0 && !Number.isFinite(compositionTimeOffset)) {\n throw new TypeError(\n \"addVideoChunk's fourth argument (compositionTimeOffset), when provided, must be a real number.\"\n );\n }\n let data = new Uint8Array(sample.byteLength);\n sample.copyTo(data);\n this.addVideoChunkRaw(\n data,\n sample.type,\n timestamp ?? sample.timestamp,\n sample.duration,\n meta,\n compositionTimeOffset\n );\n }\n addVideoChunkRaw(data, type, timestamp, duration, meta, compositionTimeOffset) {\n if (!(data instanceof Uint8Array)) {\n throw new TypeError(\"addVideoChunkRaw's first argument (data) must be an instance of Uint8Array.\");\n }\n if (type !== \"key\" && type !== \"delta\") {\n throw new TypeError(\"addVideoChunkRaw's second argument (type) must be either 'key' or 'delta'.\");\n }\n if (!Number.isFinite(timestamp) || timestamp < 0) {\n throw new TypeError(\"addVideoChunkRaw's third argument (timestamp) must be a non-negative real number.\");\n }\n if (!Number.isFinite(duration) || duration < 0) {\n throw new TypeError(\"addVideoChunkRaw's fourth argument (duration) must be a non-negative real number.\");\n }\n if (meta && typeof meta !== \"object\") {\n throw new TypeError(\"addVideoChunkRaw's fifth argument (meta), when provided, must be an object.\");\n }\n if (compositionTimeOffset !== void 0 && !Number.isFinite(compositionTimeOffset)) {\n throw new TypeError(\n \"addVideoChunkRaw's sixth argument (compositionTimeOffset), when provided, must be a real number.\"\n );\n }\n __privateMethod(this, _ensureNotFinalized, ensureNotFinalized_fn).call(this);\n if (!__privateGet(this, _options).video)\n throw new Error(\"No video track declared.\");\n if (typeof __privateGet(this, _options).fastStart === \"object\" && __privateGet(this, _videoTrack).samples.length === __privateGet(this, _options).fastStart.expectedVideoChunks) {\n throw new Error(`Cannot add more video chunks than specified in 'fastStart' (${__privateGet(this, _options).fastStart.expectedVideoChunks}).`);\n }\n let videoSample = __privateMethod(this, _createSampleForTrack, createSampleForTrack_fn).call(this, __privateGet(this, _videoTrack), data, type, timestamp, duration, meta, compositionTimeOffset);\n if (__privateGet(this, _options).fastStart === \"fragmented\" && __privateGet(this, _audioTrack)) {\n while (__privateGet(this, _audioSampleQueue).length > 0 && __privateGet(this, _audioSampleQueue)[0].decodeTimestamp <= videoSample.decodeTimestamp) {\n let audioSample = __privateGet(this, _audioSampleQueue).shift();\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);\n }\n if (videoSample.decodeTimestamp <= __privateGet(this, _audioTrack).lastDecodeTimestamp) {\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);\n } else {\n __privateGet(this, _videoSampleQueue).push(videoSample);\n }\n } else {\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);\n }\n }\n addAudioChunk(sample, meta, timestamp) {\n if (!(sample instanceof EncodedAudioChunk)) {\n throw new TypeError(\"addAudioChunk's first argument (sample) must be of type EncodedAudioChunk.\");\n }\n if (meta && typeof meta !== \"object\") {\n throw new TypeError(\"addAudioChunk's second argument (meta), when provided, must be an object.\");\n }\n if (timestamp !== void 0 && (!Number.isFinite(timestamp) || timestamp < 0)) {\n throw new TypeError(\n \"addAudioChunk's third argument (timestamp), when provided, must be a non-negative real number.\"\n );\n }\n let data = new Uint8Array(sample.byteLength);\n sample.copyTo(data);\n this.addAudioChunkRaw(data, sample.type, timestamp ?? sample.timestamp, sample.duration, meta);\n }\n addAudioChunkRaw(data, type, timestamp, duration, meta) {\n if (!(data instanceof Uint8Array)) {\n throw new TypeError(\"addAudioChunkRaw's first argument (data) must be an instance of Uint8Array.\");\n }\n if (type !== \"key\" && type !== \"delta\") {\n throw new TypeError(\"addAudioChunkRaw's second argument (type) must be either 'key' or 'delta'.\");\n }\n if (!Number.isFinite(timestamp) || timestamp < 0) {\n throw new TypeError(\"addAudioChunkRaw's third argument (timestamp) must be a non-negative real number.\");\n }\n if (!Number.isFinite(duration) || duration < 0) {\n throw new TypeError(\"addAudioChunkRaw's fourth argument (duration) must be a non-negative real number.\");\n }\n if (meta && typeof meta !== \"object\") {\n throw new TypeError(\"addAudioChunkRaw's fifth argument (meta), when provided, must be an object.\");\n }\n __privateMethod(this, _ensureNotFinalized, ensureNotFinalized_fn).call(this);\n if (!__privateGet(this, _options).audio)\n throw new Error(\"No audio track declared.\");\n if (typeof __privateGet(this, _options).fastStart === \"object\" && __privateGet(this, _audioTrack).samples.length === __privateGet(this, _options).fastStart.expectedAudioChunks) {\n throw new Error(`Cannot add more audio chunks than specified in 'fastStart' (${__privateGet(this, _options).fastStart.expectedAudioChunks}).`);\n }\n let audioSample = __privateMethod(this, _createSampleForTrack, createSampleForTrack_fn).call(this, __privateGet(this, _audioTrack), data, type, timestamp, duration, meta);\n if (__privateGet(this, _options).fastStart === \"fragmented\" && __privateGet(this, _videoTrack)) {\n while (__privateGet(this, _videoSampleQueue).length > 0 && __privateGet(this, _videoSampleQueue)[0].decodeTimestamp <= audioSample.decodeTimestamp) {\n let videoSample = __privateGet(this, _videoSampleQueue).shift();\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);\n }\n if (audioSample.decodeTimestamp <= __privateGet(this, _videoTrack).lastDecodeTimestamp) {\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);\n } else {\n __privateGet(this, _audioSampleQueue).push(audioSample);\n }\n } else {\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);\n }\n }\n /** Finalizes the file, making it ready for use. Must be called after all video and audio chunks have been added. */\n finalize() {\n if (__privateGet(this, _finalized)) {\n throw new Error(\"Cannot finalize a muxer more than once.\");\n }\n if (__privateGet(this, _options).fastStart === \"fragmented\") {\n for (let videoSample of __privateGet(this, _videoSampleQueue))\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);\n for (let audioSample of __privateGet(this, _audioSampleQueue))\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);\n __privateMethod(this, _finalizeFragment, finalizeFragment_fn).call(this, false);\n } else {\n if (__privateGet(this, _videoTrack))\n __privateMethod(this, _finalizeCurrentChunk, finalizeCurrentChunk_fn).call(this, __privateGet(this, _videoTrack));\n if (__privateGet(this, _audioTrack))\n __privateMethod(this, _finalizeCurrentChunk, finalizeCurrentChunk_fn).call(this, __privateGet(this, _audioTrack));\n }\n let tracks = [__privateGet(this, _videoTrack), __privateGet(this, _audioTrack)].filter(Boolean);\n if (__privateGet(this, _options).fastStart === \"in-memory\") {\n let mdatSize;\n for (let i = 0; i < 2; i++) {\n let movieBox2 = moov(tracks, __privateGet(this, _creationTime));\n let movieBoxSize = __privateGet(this, _writer).measureBox(movieBox2);\n mdatSize = __privateGet(this, _writer).measureBox(__privateGet(this, _mdat));\n let currentChunkPos = __privateGet(this, _writer).pos + movieBoxSize + mdatSize;\n for (let chunk of __privateGet(this, _finalizedChunks)) {\n chunk.offset = currentChunkPos;\n for (let { data } of chunk.samples) {\n currentChunkPos += data.byteLength;\n mdatSize += data.byteLength;\n }\n }\n if (currentChunkPos < 2 ** 32)\n break;\n if (mdatSize >= 2 ** 32)\n __privateGet(this, _mdat).largeSize = true;\n }\n let movieBox = moov(tracks, __privateGet(this, _creationTime));\n __privateGet(this, _writer).writeBox(movieBox);\n __privateGet(this, _mdat).size = mdatSize;\n __privateGet(this, _writer).writeBox(__privateGet(this, _mdat));\n for (let chunk of __privateGet(this, _finalizedChunks)) {\n for (let sample of chunk.samples) {\n __privateGet(this, _writer).write(sample.data);\n sample.data = null;\n }\n }\n } else if (__privateGet(this, _options).fastStart === \"fragmented\") {\n let startPos = __privateGet(this, _writer).pos;\n let mfraBox = mfra(tracks);\n __privateGet(this, _writer).writeBox(mfraBox);\n let mfraBoxSize = __privateGet(this, _writer).pos - startPos;\n __privateGet(this, _writer).seek(__privateGet(this, _writer).pos - 4);\n __privateGet(this, _writer).writeU32(mfraBoxSize);\n } else {\n let mdatPos = __privateGet(this, _writer).offsets.get(__privateGet(this, _mdat));\n let mdatSize = __privateGet(this, _writer).pos - mdatPos;\n __privateGet(this, _mdat).size = mdatSize;\n __privateGet(this, _mdat).largeSize = mdatSize >= 2 ** 32;\n __privateGet(this, _writer).patchBox(__privateGet(this, _mdat));\n let movieBox = moov(tracks, __privateGet(this, _creationTime));\n if (typeof __privateGet(this, _options).fastStart === \"object\") {\n __privateGet(this, _writer).seek(__privateGet(this, _ftypSize));\n __privateGet(this, _writer).writeBox(movieBox);\n let remainingBytes = mdatPos - __privateGet(this, _writer).pos;\n __privateGet(this, _writer).writeBox(free(remainingBytes));\n } else {\n __privateGet(this, _writer).writeBox(movieBox);\n }\n }\n __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);\n __privateGet(this, _writer).finalize();\n __privateSet(this, _finalized, true);\n }\n};\n_options = new WeakMap();\n_writer = new WeakMap();\n_ftypSize = new WeakMap();\n_mdat = new WeakMap();\n_videoTrack = new WeakMap();\n_audioTrack = new WeakMap();\n_creationTime = new WeakMap();\n_finalizedChunks = new WeakMap();\n_nextFragmentNumber = new WeakMap();\n_videoSampleQueue = new WeakMap();\n_audioSampleQueue = new WeakMap();\n_finalized = new WeakMap();\n_validateOptions = new WeakSet();\nvalidateOptions_fn = function(options) {\n if (typeof options !== \"object\") {\n throw new TypeError(\"The muxer requires an options object to be passed to its constructor.\");\n }\n if (!(options.target instanceof Target)) {\n throw new TypeError(\"The target must be provided and an instance of Target.\");\n }\n if (options.video) {\n if (!SUPPORTED_VIDEO_CODECS.includes(options.video.codec)) {\n throw new TypeError(`Unsupported video codec: ${options.video.codec}`);\n }\n if (!Number.isInteger(options.video.width) || options.video.width <= 0) {\n throw new TypeError(`Invalid video width: ${options.video.width}. Must be a positive integer.`);\n }\n if (!Number.isInteger(options.video.height) || options.video.height <= 0) {\n throw new TypeError(`Invalid video height: ${options.video.height}. Must be a positive integer.`);\n }\n const videoRotation = options.video.rotation;\n if (typeof videoRotation === \"number\" && ![0, 90, 180, 270].includes(videoRotation)) {\n throw new TypeError(`Invalid video rotation: ${videoRotation}. Has to be 0, 90, 180 or 270.`);\n } else if (Array.isArray(videoRotation) && (videoRotation.length !== 9 || videoRotation.some((value) => typeof value !== \"number\"))) {\n throw new TypeError(`Invalid video transformation matrix: ${videoRotation.join()}`);\n }\n if (options.video.frameRate !== void 0 && (!Number.isInteger(options.video.frameRate) || options.video.frameRate <= 0)) {\n throw new TypeError(\n `Invalid video frame rate: ${options.video.frameRate}. Must be a positive integer.`\n );\n }\n }\n if (options.audio) {\n if (!SUPPORTED_AUDIO_CODECS.includes(options.audio.codec)) {\n throw new TypeError(`Unsupported audio codec: ${options.audio.codec}`);\n }\n if (!Number.isInteger(options.audio.numberOfChannels) || options.audio.numberOfChannels <= 0) {\n throw new TypeError(\n `Invalid number of audio channels: ${options.audio.numberOfChannels}. Must be a positive integer.`\n );\n }\n if (!Number.isInteger(options.audio.sampleRate) || options.audio.sampleRate <= 0) {\n throw new TypeError(\n `Invalid audio sample rate: ${options.audio.sampleRate}. Must be a positive integer.`\n );\n }\n }\n if (options.firstTimestampBehavior && !FIRST_TIMESTAMP_BEHAVIORS.includes(options.firstTimestampBehavior)) {\n throw new TypeError(`Invalid first timestamp behavior: ${options.firstTimestampBehavior}`);\n }\n if (typeof options.fastStart === \"object\") {\n if (options.video) {\n if (options.fastStart.expectedVideoChunks === void 0) {\n throw new TypeError(`'fastStart' is an object but is missing property 'expectedVideoChunks'.`);\n } else if (!Number.isInteger(options.fastStart.expectedVideoChunks) || options.fastStart.expectedVideoChunks < 0) {\n throw new TypeError(`'expectedVideoChunks' must be a non-negative integer.`);\n }\n }\n if (options.audio) {\n if (options.fastStart.expectedAudioChunks === void 0) {\n throw new TypeError(`'fastStart' is an object but is missing property 'expectedAudioChunks'.`);\n } else if (!Number.isInteger(options.fastStart.expectedAudioChunks) || options.fastStart.expectedAudioChunks < 0) {\n throw new TypeError(`'expectedAudioChunks' must be a non-negative integer.`);\n }\n }\n } else if (![false, \"in-memory\", \"fragmented\"].includes(options.fastStart)) {\n throw new TypeError(`'fastStart' option must be false, 'in-memory', 'fragmented' or an object.`);\n }\n if (options.minFragmentDuration !== void 0 && (!Number.isFinite(options.minFragmentDuration) || options.minFragmentDuration < 0)) {\n throw new TypeError(`'minFragmentDuration' must be a non-negative number.`);\n }\n};\n_writeHeader = new WeakSet();\nwriteHeader_fn = function() {\n __privateGet(this, _writer).writeBox(ftyp({\n holdsAvc: __privateGet(this, _options).video?.codec === \"avc\",\n fragmented: __privateGet(this, _options).fastStart === \"fragmented\"\n }));\n __privateSet(this, _ftypSize, __privateGet(this, _writer).pos);\n if (__privateGet(this, _options).fastStart === \"in-memory\") {\n __privateSet(this, _mdat, mdat(false));\n } else if (__privateGet(this, _options).fastStart === \"fragmented\") {\n } else {\n if (typeof __privateGet(this, _options).fastStart === \"object\") {\n let moovSizeUpperBound = __privateMethod(this, _computeMoovSizeUpperBound, computeMoovSizeUpperBound_fn).call(this);\n __privateGet(this, _writer).seek(__privateGet(this, _writer).pos + moovSizeUpperBound);\n }\n __privateSet(this, _mdat, mdat(true));\n __privateGet(this, _writer).writeBox(__privateGet(this, _mdat));\n }\n __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);\n};\n_computeMoovSizeUpperBound = new WeakSet();\ncomputeMoovSizeUpperBound_fn = function() {\n if (typeof __privateGet(this, _options).fastStart !== \"object\")\n return;\n let upperBound = 0;\n let sampleCounts = [\n __privateGet(this, _options).fastStart.expectedVideoChunks,\n __privateGet(this, _options).fastStart.expectedAudioChunks\n ];\n for (let n of sampleCounts) {\n if (!n)\n continue;\n upperBound += (4 + 4) * Math.ceil(2 / 3 * n);\n upperBound += 4 * n;\n upperBound += (4 + 4 + 4) * Math.ceil(2 / 3 * n);\n upperBound += 4 * n;\n upperBound += 8 * n;\n }\n upperBound += 4096;\n return upperBound;\n};\n_prepareTracks = new WeakSet();\nprepareTracks_fn = function() {\n if (__privateGet(this, _options).video) {\n __privateSet(this, _videoTrack, {\n id: 1,\n info: {\n type: \"video\",\n codec: __privateGet(this, _options).video.codec,\n width: __privateGet(this, _options).video.width,\n height: __privateGet(this, _options).video.height,\n rotation: __privateGet(this, _options).video.rotation ?? 0,\n decoderConfig: null\n },\n // The fallback contains many common frame rates as factors\n timescale: __privateGet(this, _options).video.frameRate ?? 57600,\n samples: [],\n finalizedChunks: [],\n currentChunk: null,\n firstDecodeTimestamp: void 0,\n lastDecodeTimestamp: -1,\n timeToSampleTable: [],\n compositionTimeOffsetTable: [],\n lastTimescaleUnits: null,\n lastSample: null,\n compactlyCodedChunkTable: []\n });\n }\n if (__privateGet(this, _options).audio) {\n __privateSet(this, _audioTrack, {\n id: __privateGet(this, _options).video ? 2 : 1,\n info: {\n type: \"audio\",\n codec: __privateGet(this, _options).audio.codec,\n numberOfChannels: __privateGet(this, _options).audio.numberOfChannels,\n sampleRate: __privateGet(this, _options).audio.sampleRate,\n decoderConfig: null\n },\n timescale: __privateGet(this, _options).audio.sampleRate,\n samples: [],\n finalizedChunks: [],\n currentChunk: null,\n firstDecodeTimestamp: void 0,\n lastDecodeTimestamp: -1,\n timeToSampleTable: [],\n compositionTimeOffsetTable: [],\n lastTimescaleUnits: null,\n lastSample: null,\n compactlyCodedChunkTable: []\n });\n if (__privateGet(this, _options).audio.codec === \"aac\") {\n let guessedCodecPrivate = __privateMethod(this, _generateMpeg4AudioSpecificConfig, generateMpeg4AudioSpecificConfig_fn).call(\n this,\n 2,\n // Object type for AAC-LC, since it's the most common\n __privateGet(this, _options).audio.sampleRate,\n __privateGet(this, _options).audio.numberOfChannels\n );\n __privateGet(this, _audioTrack).info.decoderConfig = {\n codec: __privateGet(this, _options).audio.codec,\n description: guessedCodecPrivate,\n numberOfChannels: __privateGet(this, _options).audio.numberOfChannels,\n sampleRate: __privateGet(this, _options).audio.sampleRate\n };\n }\n }\n};\n_generateMpeg4AudioSpecificConfig = new WeakSet();\ngenerateMpeg4AudioSpecificConfig_fn = function(objectType, sampleRate, numberOfChannels) {\n let frequencyIndices = [96e3, 88200, 64e3, 48e3, 44100, 32e3, 24e3, 22050, 16e3, 12e3, 11025, 8e3, 7350];\n let frequencyIndex = frequencyIndices.indexOf(sampleRate);\n let channelConfig = numberOfChannels;\n let configBits = \"\";\n configBits += objectType.toString(2).padStart(5, \"0\");\n configBits += frequencyIndex.toString(2).padStart(4, \"0\");\n if (frequencyIndex === 15)\n configBits += sampleRate.toString(2).padStart(24, \"0\");\n configBits += channelConfig.toString(2).padStart(4, \"0\");\n let paddingLength = Math.ceil(configBits.length / 8) * 8;\n configBits = configBits.padEnd(paddingLength, \"0\");\n let configBytes = new Uint8Array(configBits.length / 8);\n for (let i = 0; i < configBits.length; i += 8) {\n configBytes[i / 8] = parseInt(configBits.slice(i, i + 8), 2);\n }\n return configBytes;\n};\n_createSampleForTrack = new WeakSet();\ncreateSampleForTrack_fn = function(track, data, type, timestamp, duration, meta, compositionTimeOffset) {\n let presentationTimestampInSeconds = timestamp / 1e6;\n let decodeTimestampInSeconds = (timestamp - (compositionTimeOffset ?? 0)) / 1e6;\n let durationInSeconds = duration / 1e6;\n let adjusted = __privateMethod(this, _validateTimestamp, validateTimestamp_fn).call(this, presentationTimestampInSeconds, decodeTimestampInSeconds, track);\n presentationTimestampInSeconds = adjusted.presentationTimestamp;\n decodeTimestampInSeconds = adjusted.decodeTimestamp;\n if (meta?.decoderConfig) {\n if (track.info.decoderConfig === null) {\n track.info.decoderConfig = meta.decoderConfig;\n } else {\n Object.assign(track.info.decoderConfig, meta.decoderConfig);\n }\n }\n let sample = {\n presentationTimestamp: presentationTimestampInSeconds,\n decodeTimestamp: decodeTimestampInSeconds,\n duration: durationInSeconds,\n data,\n size: data.byteLength,\n type,\n // Will be refined once the next sample comes in\n timescaleUnitsToNextSample: intoTimescale(durationInSeconds, track.timescale)\n };\n return sample;\n};\n_addSampleToTrack = new WeakSet();\naddSampleToTrack_fn = function(track, sample) {\n if (__privateGet(this, _options).fastStart !== \"fragmented\") {\n track.samples.push(sample);\n }\n const sampleCompositionTimeOffset = intoTimescale(sample.presentationTimestamp - sample.decodeTimestamp, track.timescale);\n if (track.lastTimescaleUnits !== null) {\n let timescaleUnits = intoTimescale(sample.decodeTimestamp, track.timescale, false);\n let delta = Math.round(timescaleUnits - track.lastTimescaleUnits);\n track.lastTimescaleUnits += delta;\n track.lastSample.timescaleUnitsToNextSample = delta;\n if (__privateGet(this, _options).fastStart !== \"fragmented\") {\n let lastTableEntry = last(track.timeToSampleTable);\n if (lastTableEntry.sampleCount === 1) {\n lastTableEntry.sampleDelta = delta;\n lastTableEntry.sampleCount++;\n } else if (lastTableEntry.sampleDelta === delta) {\n lastTableEntry.sampleCount++;\n } else {\n lastTableEntry.sampleCount--;\n track.timeToSampleTable.push({\n sampleCount: 2,\n sampleDelta: delta\n });\n }\n const lastCompositionTimeOffsetTableEntry = last(track.compositionTimeOffsetTable);\n if (lastCompositionTimeOffsetTableEntry.sampleCompositionTimeOffset === sampleCompositionTimeOffset) {\n lastCompositionTimeOffsetTableEntry.sampleCount++;\n } else {\n track.compositionTimeOffsetTable.push({\n sampleCount: 1,\n sampleCompositionTimeOffset\n });\n }\n }\n } else {\n track.lastTimescaleUnits = 0;\n if (__privateGet(this, _options).fastStart !== \"fragmented\") {\n track.timeToSampleTable.push({\n sampleCount: 1,\n sampleDelta: intoTimescale(sample.duration, track.timescale)\n });\n track.compositionTimeOffsetTable.push({\n sampleCount: 1,\n sampleCompositionTimeOffset\n });\n }\n }\n track.lastSample = sample;\n let beginNewChunk = false;\n if (!track.currentChunk) {\n beginNewChunk = true;\n } else {\n let currentChunkDuration = sample.presentationTimestamp - track.currentChunk.startTimestamp;\n if (__privateGet(this, _options).fastStart === \"fragmented\") {\n let mostImportantTrack = __privateGet(this, _videoTrack) ?? __privateGet(this, _audioTrack);\n const chunkDuration = __privateGet(this, _options).minFragmentDuration ?? 1;\n if (track === mostImportantTrack && sample.type === \"key\" && currentChunkDuration >= chunkDuration) {\n beginNewChunk = true;\n __privateMethod(this, _finalizeFragment, finalizeFragment_fn).call(this);\n }\n } else {\n beginNewChunk = currentChunkDuration >= 0.5;\n }\n }\n if (beginNewChunk) {\n if (track.currentChunk) {\n __privateMethod(this, _finalizeCurrentChunk, finalizeCurrentChunk_fn).call(this, track);\n }\n track.currentChunk = {\n startTimestamp: sample.presentationTimestamp,\n samples: []\n };\n }\n track.currentChunk.samples.push(sample);\n};\n_validateTimestamp = new WeakSet();\nvalidateTimestamp_fn = function(presentationTimestamp, decodeTimestamp, track) {\n const strictTimestampBehavior = __privateGet(this, _options).firstTimestampBehavior === \"strict\";\n const noLastDecodeTimestamp = track.lastDecodeTimestamp === -1;\n const timestampNonZero = decodeTimestamp !== 0;\n if (strictTimestampBehavior && noLastDecodeTimestamp && timestampNonZero) {\n throw new Error(\n `The first chunk for your media track must have a timestamp of 0 (received DTS=${decodeTimestamp}).Non-zero first timestamps are often caused by directly piping frames or audio data from a MediaStreamTrack into the encoder. Their timestamps are typically relative to the age of thedocument, which is probably what you want.\n\nIf you want to offset all timestamps of a track such that the first one is zero, set firstTimestampBehavior: 'offset' in the options.\n`\n );\n } else if (__privateGet(this, _options).firstTimestampBehavior === \"offset\" || __privateGet(this, _options).firstTimestampBehavior === \"cross-track-offset\") {\n if (track.firstDecodeTimestamp === void 0) {\n track.firstDecodeTimestamp = decodeTimestamp;\n }\n let baseDecodeTimestamp;\n if (__privateGet(this, _options).firstTimestampBehavior === \"offset\") {\n baseDecodeTimestamp = track.firstDecodeTimestamp;\n } else {\n baseDecodeTimestamp = Math.min(\n __privateGet(this, _videoTrack)?.firstDecodeTimestamp ?? Infinity,\n __privateGet(this, _audioTrack)?.firstDecodeTimestamp ?? Infinity\n );\n }\n decodeTimestamp -= baseDecodeTimestamp;\n presentationTimestamp -= baseDecodeTimestamp;\n }\n if (decodeTimestamp < track.lastDecodeTimestamp) {\n throw new Error(\n `Timestamps must be monotonically increasing (DTS went from ${track.lastDecodeTimestamp * 1e6} to ${decodeTimestamp * 1e6}).`\n );\n }\n track.lastDecodeTimestamp = decodeTimestamp;\n return { presentationTimestamp, decodeTimestamp };\n};\n_finalizeCurrentChunk = new WeakSet();\nfinalizeCurrentChunk_fn = function(track) {\n if (__privateGet(this, _options).fastStart === \"fragmented\") {\n throw new Error(\"Can't finalize individual chunks if 'fastStart' is set to 'fragmented'.\");\n }\n if (!track.currentChunk)\n return;\n track.finalizedChunks.push(track.currentChunk);\n __privateGet(this, _finalizedChunks).push(track.currentChunk);\n if (track.compactlyCodedChunkTable.length === 0 || last(track.compactlyCodedChunkTable).samplesPerChunk !== track.currentChunk.samples.length) {\n track.compactlyCodedChunkTable.push({\n firstChunk: track.finalizedChunks.length,\n // 1-indexed\n samplesPerChunk: track.currentChunk.samples.length\n });\n }\n if (__privateGet(this, _options).fastStart === \"in-memory\") {\n track.currentChunk.offset = 0;\n return;\n }\n track.currentChunk.offset = __privateGet(this, _writer).pos;\n for (let sample of track.currentChunk.samples) {\n __privateGet(this, _writer).write(sample.data);\n sample.data = null;\n }\n __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);\n};\n_finalizeFragment = new WeakSet();\nfinalizeFragment_fn = function(flushStreamingWriter = true) {\n if (__privateGet(this, _options).fastStart !== \"fragmented\") {\n throw new Error(\"Can't finalize a fragment unless 'fastStart' is set to 'fragmented'.\");\n }\n let tracks = [__privateGet(this, _videoTrack), __privateGet(this, _audioTrack)].filter((track) => track && track.currentChunk);\n if (tracks.length === 0)\n return;\n let fragmentNumber = __privateWrapper(this, _nextFragmentNumber)._++;\n if (fragmentNumber === 1) {\n let movieBox = moov(tracks, __privateGet(this, _creationTime), true);\n __privateGet(this, _writer).writeBox(movieBox);\n }\n let moofOffset = __privateGet(this, _writer).pos;\n let moofBox = moof(fragmentNumber, tracks);\n __privateGet(this, _writer).writeBox(moofBox);\n {\n let mdatBox = mdat(false);\n let totalTrackSampleSize = 0;\n for (let track of tracks) {\n for (let sample of track.currentChunk.samples) {\n totalTrackSampleSize += sample.size;\n }\n }\n let mdatSize = __privateGet(this, _writer).measureBox(mdatBox) + totalTrackSampleSize;\n if (mdatSize >= 2 ** 32) {\n mdatBox.largeSize = true;\n mdatSize = __privateGet(this, _writer).measureBox(mdatBox) + totalTrackSampleSize;\n }\n mdatBox.size = mdatSize;\n __privateGet(this, _writer).writeBox(mdatBox);\n }\n for (let track of tracks) {\n track.currentChunk.offset = __privateGet(this, _writer).pos;\n track.currentChunk.moofOffset = moofOffset;\n for (let sample of track.currentChunk.samples) {\n __privateGet(this, _writer).write(sample.data);\n sample.data = null;\n }\n }\n let endPos = __privateGet(this, _writer).pos;\n __privateGet(this, _writer).seek(__privateGet(this, _writer).offsets.get(moofBox));\n let newMoofBox = moof(fragmentNumber, tracks);\n __privateGet(this, _writer).writeBox(newMoofBox);\n __privateGet(this, _writer).seek(endPos);\n for (let track of tracks) {\n track.finalizedChunks.push(track.currentChunk);\n __privateGet(this, _finalizedChunks).push(track.currentChunk);\n track.currentChunk = null;\n }\n if (flushStreamingWriter) {\n __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);\n }\n};\n_maybeFlushStreamingTargetWriter = new WeakSet();\nmaybeFlushStreamingTargetWriter_fn = function() {\n if (__privateGet(this, _writer) instanceof StreamTargetWriter) {\n __privateGet(this, _writer).flush();\n }\n};\n_ensureNotFinalized = new WeakSet();\nensureNotFinalized_fn = function() {\n if (__privateGet(this, _finalized)) {\n throw new Error(\"Cannot add new video or audio chunks after the file has been finalized.\");\n }\n};\nexport {\n ArrayBufferTarget,\n FileSystemWritableFileStreamTarget,\n Muxer,\n StreamTarget\n};\n";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MP4_MUXER_SOURCE = void 0;
|
|
4
|
+
// Generated from bin/vendor/mp4-muxer.mjs
|
|
5
|
+
exports.MP4_MUXER_SOURCE = "var __accessCheck = (obj, member, msg) => {\n if (!member.has(obj))\n throw TypeError(\"Cannot \" + msg);\n};\nvar __privateGet = (obj, member, getter) => {\n __accessCheck(obj, member, \"read from private field\");\n return getter ? getter.call(obj) : member.get(obj);\n};\nvar __privateAdd = (obj, member, value) => {\n if (member.has(obj))\n throw TypeError(\"Cannot add the same private member more than once\");\n member instanceof WeakSet ? member.add(obj) : member.set(obj, value);\n};\nvar __privateSet = (obj, member, value, setter) => {\n __accessCheck(obj, member, \"write to private field\");\n setter ? setter.call(obj, value) : member.set(obj, value);\n return value;\n};\nvar __privateWrapper = (obj, member, setter, getter) => ({\n set _(value) {\n __privateSet(obj, member, value, setter);\n },\n get _() {\n return __privateGet(obj, member, getter);\n }\n});\nvar __privateMethod = (obj, member, method) => {\n __accessCheck(obj, member, \"access private method\");\n return method;\n};\n\n// src/misc.ts\nvar bytes = new Uint8Array(8);\nvar view = new DataView(bytes.buffer);\nvar u8 = (value) => {\n return [(value % 256 + 256) % 256];\n};\nvar u16 = (value) => {\n view.setUint16(0, value, false);\n return [bytes[0], bytes[1]];\n};\nvar i16 = (value) => {\n view.setInt16(0, value, false);\n return [bytes[0], bytes[1]];\n};\nvar u24 = (value) => {\n view.setUint32(0, value, false);\n return [bytes[1], bytes[2], bytes[3]];\n};\nvar u32 = (value) => {\n view.setUint32(0, value, false);\n return [bytes[0], bytes[1], bytes[2], bytes[3]];\n};\nvar i32 = (value) => {\n view.setInt32(0, value, false);\n return [bytes[0], bytes[1], bytes[2], bytes[3]];\n};\nvar u64 = (value) => {\n view.setUint32(0, Math.floor(value / 2 ** 32), false);\n view.setUint32(4, value, false);\n return [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]];\n};\nvar fixed_8_8 = (value) => {\n view.setInt16(0, 2 ** 8 * value, false);\n return [bytes[0], bytes[1]];\n};\nvar fixed_16_16 = (value) => {\n view.setInt32(0, 2 ** 16 * value, false);\n return [bytes[0], bytes[1], bytes[2], bytes[3]];\n};\nvar fixed_2_30 = (value) => {\n view.setInt32(0, 2 ** 30 * value, false);\n return [bytes[0], bytes[1], bytes[2], bytes[3]];\n};\nvar ascii = (text, nullTerminated = false) => {\n let bytes2 = Array(text.length).fill(null).map((_, i) => text.charCodeAt(i));\n if (nullTerminated)\n bytes2.push(0);\n return bytes2;\n};\nvar last = (arr) => {\n return arr && arr[arr.length - 1];\n};\nvar lastPresentedSample = (samples) => {\n let result = void 0;\n for (let sample of samples) {\n if (!result || sample.presentationTimestamp > result.presentationTimestamp) {\n result = sample;\n }\n }\n return result;\n};\nvar intoTimescale = (timeInSeconds, timescale, round = true) => {\n let value = timeInSeconds * timescale;\n return round ? Math.round(value) : value;\n};\nvar rotationMatrix = (rotationInDegrees) => {\n let theta = rotationInDegrees * (Math.PI / 180);\n let cosTheta = Math.cos(theta);\n let sinTheta = Math.sin(theta);\n return [\n cosTheta,\n sinTheta,\n 0,\n -sinTheta,\n cosTheta,\n 0,\n 0,\n 0,\n 1\n ];\n};\nvar IDENTITY_MATRIX = rotationMatrix(0);\nvar matrixToBytes = (matrix) => {\n return [\n fixed_16_16(matrix[0]),\n fixed_16_16(matrix[1]),\n fixed_2_30(matrix[2]),\n fixed_16_16(matrix[3]),\n fixed_16_16(matrix[4]),\n fixed_2_30(matrix[5]),\n fixed_16_16(matrix[6]),\n fixed_16_16(matrix[7]),\n fixed_2_30(matrix[8])\n ];\n};\nvar deepClone = (x) => {\n if (!x)\n return x;\n if (typeof x !== \"object\")\n return x;\n if (Array.isArray(x))\n return x.map(deepClone);\n return Object.fromEntries(Object.entries(x).map(([key, value]) => [key, deepClone(value)]));\n};\nvar isU32 = (value) => {\n return value >= 0 && value < 2 ** 32;\n};\n\n// src/box.ts\nvar box = (type, contents, children) => ({\n type,\n contents: contents && new Uint8Array(contents.flat(10)),\n children\n});\nvar fullBox = (type, version, flags, contents, children) => box(\n type,\n [u8(version), u24(flags), contents ?? []],\n children\n);\nvar ftyp = (details) => {\n let minorVersion = 512;\n if (details.fragmented)\n return box(\"ftyp\", [\n ascii(\"iso5\"),\n // Major brand\n u32(minorVersion),\n // Minor version\n // Compatible brands\n ascii(\"iso5\"),\n ascii(\"iso6\"),\n ascii(\"mp41\")\n ]);\n return box(\"ftyp\", [\n ascii(\"isom\"),\n // Major brand\n u32(minorVersion),\n // Minor version\n // Compatible brands\n ascii(\"isom\"),\n details.holdsAvc ? ascii(\"avc1\") : [],\n ascii(\"mp41\")\n ]);\n};\nvar mdat = (reserveLargeSize) => ({ type: \"mdat\", largeSize: reserveLargeSize });\nvar free = (size) => ({ type: \"free\", size });\nvar moov = (tracks, creationTime, fragmented = false) => box(\"moov\", null, [\n mvhd(creationTime, tracks),\n ...tracks.map((x) => trak(x, creationTime)),\n fragmented ? mvex(tracks) : null\n]);\nvar mvhd = (creationTime, tracks) => {\n let duration = intoTimescale(Math.max(\n 0,\n ...tracks.filter((x) => x.samples.length > 0).map((x) => {\n const lastSample = lastPresentedSample(x.samples);\n return lastSample.presentationTimestamp + lastSample.duration;\n })\n ), GLOBAL_TIMESCALE);\n let nextTrackId = Math.max(...tracks.map((x) => x.id)) + 1;\n let needsU64 = !isU32(creationTime) || !isU32(duration);\n let u32OrU64 = needsU64 ? u64 : u32;\n return fullBox(\"mvhd\", +needsU64, 0, [\n u32OrU64(creationTime),\n // Creation time\n u32OrU64(creationTime),\n // Modification time\n u32(GLOBAL_TIMESCALE),\n // Timescale\n u32OrU64(duration),\n // Duration\n fixed_16_16(1),\n // Preferred rate\n fixed_8_8(1),\n // Preferred volume\n Array(10).fill(0),\n // Reserved\n matrixToBytes(IDENTITY_MATRIX),\n // Matrix\n Array(24).fill(0),\n // Pre-defined\n u32(nextTrackId)\n // Next track ID\n ]);\n};\nvar trak = (track, creationTime) => box(\"trak\", null, [\n tkhd(track, creationTime),\n mdia(track, creationTime)\n]);\nvar tkhd = (track, creationTime) => {\n let lastSample = lastPresentedSample(track.samples);\n let durationInGlobalTimescale = intoTimescale(\n lastSample ? lastSample.presentationTimestamp + lastSample.duration : 0,\n GLOBAL_TIMESCALE\n );\n let needsU64 = !isU32(creationTime) || !isU32(durationInGlobalTimescale);\n let u32OrU64 = needsU64 ? u64 : u32;\n let matrix;\n if (track.info.type === \"video\") {\n matrix = typeof track.info.rotation === \"number\" ? rotationMatrix(track.info.rotation) : track.info.rotation;\n } else {\n matrix = IDENTITY_MATRIX;\n }\n return fullBox(\"tkhd\", +needsU64, 3, [\n u32OrU64(creationTime),\n // Creation time\n u32OrU64(creationTime),\n // Modification time\n u32(track.id),\n // Track ID\n u32(0),\n // Reserved\n u32OrU64(durationInGlobalTimescale),\n // Duration\n Array(8).fill(0),\n // Reserved\n u16(0),\n // Layer\n u16(0),\n // Alternate group\n fixed_8_8(track.info.type === \"audio\" ? 1 : 0),\n // Volume\n u16(0),\n // Reserved\n matrixToBytes(matrix),\n // Matrix\n fixed_16_16(track.info.type === \"video\" ? track.info.width : 0),\n // Track width\n fixed_16_16(track.info.type === \"video\" ? track.info.height : 0)\n // Track height\n ]);\n};\nvar mdia = (track, creationTime) => box(\"mdia\", null, [\n mdhd(track, creationTime),\n hdlr(track.info.type === \"video\" ? \"vide\" : \"soun\"),\n minf(track)\n]);\nvar mdhd = (track, creationTime) => {\n let lastSample = lastPresentedSample(track.samples);\n let localDuration = intoTimescale(\n lastSample ? lastSample.presentationTimestamp + lastSample.duration : 0,\n track.timescale\n );\n let needsU64 = !isU32(creationTime) || !isU32(localDuration);\n let u32OrU64 = needsU64 ? u64 : u32;\n return fullBox(\"mdhd\", +needsU64, 0, [\n u32OrU64(creationTime),\n // Creation time\n u32OrU64(creationTime),\n // Modification time\n u32(track.timescale),\n // Timescale\n u32OrU64(localDuration),\n // Duration\n u16(21956),\n // Language (\"und\", undetermined)\n u16(0)\n // Quality\n ]);\n};\nvar hdlr = (componentSubtype) => fullBox(\"hdlr\", 0, 0, [\n ascii(\"mhlr\"),\n // Component type\n ascii(componentSubtype),\n // Component subtype\n u32(0),\n // Component manufacturer\n u32(0),\n // Component flags\n u32(0),\n // Component flags mask\n ascii(\"mp4-muxer-hdlr\", true)\n // Component name\n]);\nvar minf = (track) => box(\"minf\", null, [\n track.info.type === \"video\" ? vmhd() : smhd(),\n dinf(),\n stbl(track)\n]);\nvar vmhd = () => fullBox(\"vmhd\", 0, 1, [\n u16(0),\n // Graphics mode\n u16(0),\n // Opcolor R\n u16(0),\n // Opcolor G\n u16(0)\n // Opcolor B\n]);\nvar smhd = () => fullBox(\"smhd\", 0, 0, [\n u16(0),\n // Balance\n u16(0)\n // Reserved\n]);\nvar dinf = () => box(\"dinf\", null, [\n dref()\n]);\nvar dref = () => fullBox(\"dref\", 0, 0, [\n u32(1)\n // Entry count\n], [\n url()\n]);\nvar url = () => fullBox(\"url \", 0, 1);\nvar stbl = (track) => {\n const needsCtts = track.compositionTimeOffsetTable.length > 1 || track.compositionTimeOffsetTable.some((x) => x.sampleCompositionTimeOffset !== 0);\n return box(\"stbl\", null, [\n stsd(track),\n stts(track),\n stss(track),\n stsc(track),\n stsz(track),\n stco(track),\n needsCtts ? ctts(track) : null\n ]);\n};\nvar stsd = (track) => fullBox(\"stsd\", 0, 0, [\n u32(1)\n // Entry count\n], [\n track.info.type === \"video\" ? videoSampleDescription(\n VIDEO_CODEC_TO_BOX_NAME[track.info.codec],\n track\n ) : soundSampleDescription(\n AUDIO_CODEC_TO_BOX_NAME[track.info.codec],\n track\n )\n]);\nvar videoSampleDescription = (compressionType, track) => box(compressionType, [\n Array(6).fill(0),\n // Reserved\n u16(1),\n // Data reference index\n u16(0),\n // Pre-defined\n u16(0),\n // Reserved\n Array(12).fill(0),\n // Pre-defined\n u16(track.info.width),\n // Width\n u16(track.info.height),\n // Height\n u32(4718592),\n // Horizontal resolution\n u32(4718592),\n // Vertical resolution\n u32(0),\n // Reserved\n u16(1),\n // Frame count\n Array(32).fill(0),\n // Compressor name\n u16(24),\n // Depth\n i16(65535)\n // Pre-defined\n], [\n VIDEO_CODEC_TO_CONFIGURATION_BOX[track.info.codec](track),\n track.info.decoderConfig.colorSpace ? colr(track) : null\n]);\nvar COLOR_PRIMARIES_MAP = {\n \"bt709\": 1,\n // ITU-R BT.709\n \"bt470bg\": 5,\n // ITU-R BT.470BG\n \"smpte170m\": 6\n // ITU-R BT.601 525 - SMPTE 170M\n};\nvar TRANSFER_CHARACTERISTICS_MAP = {\n \"bt709\": 1,\n // ITU-R BT.709\n \"smpte170m\": 6,\n // SMPTE 170M\n \"iec61966-2-1\": 13\n // IEC 61966-2-1\n};\nvar MATRIX_COEFFICIENTS_MAP = {\n \"rgb\": 0,\n // Identity\n \"bt709\": 1,\n // ITU-R BT.709\n \"bt470bg\": 5,\n // ITU-R BT.470BG\n \"smpte170m\": 6\n // SMPTE 170M\n};\nvar colr = (track) => box(\"colr\", [\n ascii(\"nclx\"),\n // Colour type\n u16(COLOR_PRIMARIES_MAP[track.info.decoderConfig.colorSpace.primaries]),\n // Colour primaries\n u16(TRANSFER_CHARACTERISTICS_MAP[track.info.decoderConfig.colorSpace.transfer]),\n // Transfer characteristics\n u16(MATRIX_COEFFICIENTS_MAP[track.info.decoderConfig.colorSpace.matrix]),\n // Matrix coefficients\n u8((track.info.decoderConfig.colorSpace.fullRange ? 1 : 0) << 7)\n // Full range flag\n]);\nvar avcC = (track) => track.info.decoderConfig && box(\"avcC\", [\n // For AVC, description is an AVCDecoderConfigurationRecord, so nothing else to do here\n ...new Uint8Array(track.info.decoderConfig.description)\n]);\nvar hvcC = (track) => track.info.decoderConfig && box(\"hvcC\", [\n // For HEVC, description is a HEVCDecoderConfigurationRecord, so nothing else to do here\n ...new Uint8Array(track.info.decoderConfig.description)\n]);\nvar vpcC = (track) => {\n if (!track.info.decoderConfig) {\n return null;\n }\n let decoderConfig = track.info.decoderConfig;\n if (!decoderConfig.colorSpace) {\n throw new Error(`'colorSpace' is required in the decoder config for VP9.`);\n }\n let parts = decoderConfig.codec.split(\".\");\n let profile = Number(parts[1]);\n let level = Number(parts[2]);\n let bitDepth = Number(parts[3]);\n let chromaSubsampling = 0;\n let thirdByte = (bitDepth << 4) + (chromaSubsampling << 1) + Number(decoderConfig.colorSpace.fullRange);\n let colourPrimaries = 2;\n let transferCharacteristics = 2;\n let matrixCoefficients = 2;\n return fullBox(\"vpcC\", 1, 0, [\n u8(profile),\n // Profile\n u8(level),\n // Level\n u8(thirdByte),\n // Bit depth, chroma subsampling, full range\n u8(colourPrimaries),\n // Colour primaries\n u8(transferCharacteristics),\n // Transfer characteristics\n u8(matrixCoefficients),\n // Matrix coefficients\n u16(0)\n // Codec initialization data size\n ]);\n};\nvar av1C = () => {\n let marker = 1;\n let version = 1;\n let firstByte = (marker << 7) + version;\n return box(\"av1C\", [\n firstByte,\n 0,\n 0,\n 0\n ]);\n};\nvar soundSampleDescription = (compressionType, track) => box(compressionType, [\n Array(6).fill(0),\n // Reserved\n u16(1),\n // Data reference index\n u16(0),\n // Version\n u16(0),\n // Revision level\n u32(0),\n // Vendor\n u16(track.info.numberOfChannels),\n // Number of channels\n u16(16),\n // Sample size (bits)\n u16(0),\n // Compression ID\n u16(0),\n // Packet size\n fixed_16_16(track.info.sampleRate)\n // Sample rate\n], [\n AUDIO_CODEC_TO_CONFIGURATION_BOX[track.info.codec](track)\n]);\nvar esds = (track) => {\n let description = new Uint8Array(track.info.decoderConfig.description);\n return fullBox(\"esds\", 0, 0, [\n // https://stackoverflow.com/a/54803118\n u32(58753152),\n // TAG(3) = Object Descriptor ([2])\n u8(32 + description.byteLength),\n // length of this OD (which includes the next 2 tags)\n u16(1),\n // ES_ID = 1\n u8(0),\n // flags etc = 0\n u32(75530368),\n // TAG(4) = ES Descriptor ([2]) embedded in above OD\n u8(18 + description.byteLength),\n // length of this ESD\n u8(64),\n // MPEG-4 Audio\n u8(21),\n // stream type(6bits)=5 audio, flags(2bits)=1\n u24(0),\n // 24bit buffer size\n u32(130071),\n // max bitrate\n u32(130071),\n // avg bitrate\n u32(92307584),\n // TAG(5) = ASC ([2],[3]) embedded in above OD\n u8(description.byteLength),\n // length\n ...description,\n u32(109084800),\n // TAG(6)\n u8(1),\n // length\n u8(2)\n // data\n ]);\n};\nvar dOps = (track) => {\n let preskip = 3840;\n let gain = 0;\n const description = track.info.decoderConfig?.description;\n if (description) {\n if (description.byteLength < 18) {\n throw new TypeError(\"Invalid decoder description provided for Opus; must be at least 18 bytes long.\");\n }\n const view2 = ArrayBuffer.isView(description) ? new DataView(description.buffer, description.byteOffset, description.byteLength) : new DataView(description);\n preskip = view2.getUint16(10, true);\n gain = view2.getInt16(14, true);\n }\n return box(\"dOps\", [\n u8(0),\n // Version\n u8(track.info.numberOfChannels),\n // OutputChannelCount\n u16(preskip),\n u32(track.info.sampleRate),\n // InputSampleRate\n fixed_8_8(gain),\n // OutputGain\n u8(0)\n // ChannelMappingFamily\n ]);\n};\nvar stts = (track) => {\n return fullBox(\"stts\", 0, 0, [\n u32(track.timeToSampleTable.length),\n // Number of entries\n track.timeToSampleTable.map((x) => [\n // Time-to-sample table\n u32(x.sampleCount),\n // Sample count\n u32(x.sampleDelta)\n // Sample duration\n ])\n ]);\n};\nvar stss = (track) => {\n if (track.samples.every((x) => x.type === \"key\"))\n return null;\n let keySamples = [...track.samples.entries()].filter(([, sample]) => sample.type === \"key\");\n return fullBox(\"stss\", 0, 0, [\n u32(keySamples.length),\n // Number of entries\n keySamples.map(([index]) => u32(index + 1))\n // Sync sample table\n ]);\n};\nvar stsc = (track) => {\n return fullBox(\"stsc\", 0, 0, [\n u32(track.compactlyCodedChunkTable.length),\n // Number of entries\n track.compactlyCodedChunkTable.map((x) => [\n // Sample-to-chunk table\n u32(x.firstChunk),\n // First chunk\n u32(x.samplesPerChunk),\n // Samples per chunk\n u32(1)\n // Sample description index\n ])\n ]);\n};\nvar stsz = (track) => fullBox(\"stsz\", 0, 0, [\n u32(0),\n // Sample size (0 means non-constant size)\n u32(track.samples.length),\n // Number of entries\n track.samples.map((x) => u32(x.size))\n // Sample size table\n]);\nvar stco = (track) => {\n if (track.finalizedChunks.length > 0 && last(track.finalizedChunks).offset >= 2 ** 32) {\n return fullBox(\"co64\", 0, 0, [\n u32(track.finalizedChunks.length),\n // Number of entries\n track.finalizedChunks.map((x) => u64(x.offset))\n // Chunk offset table\n ]);\n }\n return fullBox(\"stco\", 0, 0, [\n u32(track.finalizedChunks.length),\n // Number of entries\n track.finalizedChunks.map((x) => u32(x.offset))\n // Chunk offset table\n ]);\n};\nvar ctts = (track) => {\n return fullBox(\"ctts\", 0, 0, [\n u32(track.compositionTimeOffsetTable.length),\n // Number of entries\n track.compositionTimeOffsetTable.map((x) => [\n // Time-to-sample table\n u32(x.sampleCount),\n // Sample count\n u32(x.sampleCompositionTimeOffset)\n // Sample offset\n ])\n ]);\n};\nvar mvex = (tracks) => {\n return box(\"mvex\", null, tracks.map(trex));\n};\nvar trex = (track) => {\n return fullBox(\"trex\", 0, 0, [\n u32(track.id),\n // Track ID\n u32(1),\n // Default sample description index\n u32(0),\n // Default sample duration\n u32(0),\n // Default sample size\n u32(0)\n // Default sample flags\n ]);\n};\nvar moof = (sequenceNumber, tracks) => {\n return box(\"moof\", null, [\n mfhd(sequenceNumber),\n ...tracks.map(traf)\n ]);\n};\nvar mfhd = (sequenceNumber) => {\n return fullBox(\"mfhd\", 0, 0, [\n u32(sequenceNumber)\n // Sequence number\n ]);\n};\nvar fragmentSampleFlags = (sample) => {\n let byte1 = 0;\n let byte2 = 0;\n let byte3 = 0;\n let byte4 = 0;\n let sampleIsDifferenceSample = sample.type === \"delta\";\n byte2 |= +sampleIsDifferenceSample;\n if (sampleIsDifferenceSample) {\n byte1 |= 1;\n } else {\n byte1 |= 2;\n }\n return byte1 << 24 | byte2 << 16 | byte3 << 8 | byte4;\n};\nvar traf = (track) => {\n return box(\"traf\", null, [\n tfhd(track),\n tfdt(track),\n trun(track)\n ]);\n};\nvar tfhd = (track) => {\n let tfFlags = 0;\n tfFlags |= 8;\n tfFlags |= 16;\n tfFlags |= 32;\n tfFlags |= 131072;\n let referenceSample = track.currentChunk.samples[1] ?? track.currentChunk.samples[0];\n let referenceSampleInfo = {\n duration: referenceSample.timescaleUnitsToNextSample,\n size: referenceSample.size,\n flags: fragmentSampleFlags(referenceSample)\n };\n return fullBox(\"tfhd\", 0, tfFlags, [\n u32(track.id),\n // Track ID\n u32(referenceSampleInfo.duration),\n // Default sample duration\n u32(referenceSampleInfo.size),\n // Default sample size\n u32(referenceSampleInfo.flags)\n // Default sample flags\n ]);\n};\nvar tfdt = (track) => {\n return fullBox(\"tfdt\", 1, 0, [\n u64(intoTimescale(track.currentChunk.startTimestamp, track.timescale))\n // Base Media Decode Time\n ]);\n};\nvar trun = (track) => {\n let allSampleDurations = track.currentChunk.samples.map((x) => x.timescaleUnitsToNextSample);\n let allSampleSizes = track.currentChunk.samples.map((x) => x.size);\n let allSampleFlags = track.currentChunk.samples.map(fragmentSampleFlags);\n let allSampleCompositionTimeOffsets = track.currentChunk.samples.map((x) => intoTimescale(x.presentationTimestamp - x.decodeTimestamp, track.timescale));\n let uniqueSampleDurations = new Set(allSampleDurations);\n let uniqueSampleSizes = new Set(allSampleSizes);\n let uniqueSampleFlags = new Set(allSampleFlags);\n let uniqueSampleCompositionTimeOffsets = new Set(allSampleCompositionTimeOffsets);\n let firstSampleFlagsPresent = uniqueSampleFlags.size === 2 && allSampleFlags[0] !== allSampleFlags[1];\n let sampleDurationPresent = uniqueSampleDurations.size > 1;\n let sampleSizePresent = uniqueSampleSizes.size > 1;\n let sampleFlagsPresent = !firstSampleFlagsPresent && uniqueSampleFlags.size > 1;\n let sampleCompositionTimeOffsetsPresent = uniqueSampleCompositionTimeOffsets.size > 1 || [...uniqueSampleCompositionTimeOffsets].some((x) => x !== 0);\n let flags = 0;\n flags |= 1;\n flags |= 4 * +firstSampleFlagsPresent;\n flags |= 256 * +sampleDurationPresent;\n flags |= 512 * +sampleSizePresent;\n flags |= 1024 * +sampleFlagsPresent;\n flags |= 2048 * +sampleCompositionTimeOffsetsPresent;\n return fullBox(\"trun\", 1, flags, [\n u32(track.currentChunk.samples.length),\n // Sample count\n u32(track.currentChunk.offset - track.currentChunk.moofOffset || 0),\n // Data offset\n firstSampleFlagsPresent ? u32(allSampleFlags[0]) : [],\n track.currentChunk.samples.map((_, i) => [\n sampleDurationPresent ? u32(allSampleDurations[i]) : [],\n // Sample duration\n sampleSizePresent ? u32(allSampleSizes[i]) : [],\n // Sample size\n sampleFlagsPresent ? u32(allSampleFlags[i]) : [],\n // Sample flags\n // Sample composition time offsets\n sampleCompositionTimeOffsetsPresent ? i32(allSampleCompositionTimeOffsets[i]) : []\n ])\n ]);\n};\nvar mfra = (tracks) => {\n return box(\"mfra\", null, [\n ...tracks.map(tfra),\n mfro()\n ]);\n};\nvar tfra = (track, trackIndex) => {\n let version = 1;\n return fullBox(\"tfra\", version, 0, [\n u32(track.id),\n // Track ID\n u32(63),\n // This specifies that traf number, trun number and sample number are 32-bit ints\n u32(track.finalizedChunks.length),\n // Number of entries\n track.finalizedChunks.map((chunk) => [\n u64(intoTimescale(chunk.startTimestamp, track.timescale)),\n // Time\n u64(chunk.moofOffset),\n // moof offset\n u32(trackIndex + 1),\n // traf number\n u32(1),\n // trun number\n u32(1)\n // Sample number\n ])\n ]);\n};\nvar mfro = () => {\n return fullBox(\"mfro\", 0, 0, [\n // This value needs to be overwritten manually from the outside, where the actual size of the enclosing mfra box\n // is known\n u32(0)\n // Size\n ]);\n};\nvar VIDEO_CODEC_TO_BOX_NAME = {\n \"avc\": \"avc1\",\n \"hevc\": \"hvc1\",\n \"vp9\": \"vp09\",\n \"av1\": \"av01\"\n};\nvar VIDEO_CODEC_TO_CONFIGURATION_BOX = {\n \"avc\": avcC,\n \"hevc\": hvcC,\n \"vp9\": vpcC,\n \"av1\": av1C\n};\nvar AUDIO_CODEC_TO_BOX_NAME = {\n \"aac\": \"mp4a\",\n \"opus\": \"Opus\"\n};\nvar AUDIO_CODEC_TO_CONFIGURATION_BOX = {\n \"aac\": esds,\n \"opus\": dOps\n};\n\n// src/target.ts\nvar isTarget = Symbol(\"isTarget\");\nvar Target = class {\n};\nisTarget;\nvar ArrayBufferTarget = class extends Target {\n constructor() {\n super(...arguments);\n this.buffer = null;\n }\n};\nvar StreamTarget = class extends Target {\n constructor(options) {\n super();\n this.options = options;\n if (typeof options !== \"object\") {\n throw new TypeError(\"StreamTarget requires an options object to be passed to its constructor.\");\n }\n if (options.onData) {\n if (typeof options.onData !== \"function\") {\n throw new TypeError(\"options.onData, when provided, must be a function.\");\n }\n if (options.onData.length < 2) {\n throw new TypeError(\n \"options.onData, when provided, must be a function that takes in at least two arguments (data and position). Ignoring the position argument, which specifies the byte offset at which the data is to be written, can lead to broken outputs.\"\n );\n }\n }\n if (options.chunked !== void 0 && typeof options.chunked !== \"boolean\") {\n throw new TypeError(\"options.chunked, when provided, must be a boolean.\");\n }\n if (options.chunkSize !== void 0 && (!Number.isInteger(options.chunkSize) || options.chunkSize < 1024)) {\n throw new TypeError(\"options.chunkSize, when provided, must be an integer and not smaller than 1024.\");\n }\n }\n};\nvar FileSystemWritableFileStreamTarget = class extends Target {\n constructor(stream, options) {\n super();\n this.stream = stream;\n this.options = options;\n if (!(stream instanceof FileSystemWritableFileStream)) {\n throw new TypeError(\"FileSystemWritableFileStreamTarget requires a FileSystemWritableFileStream instance.\");\n }\n if (options !== void 0 && typeof options !== \"object\") {\n throw new TypeError(\"FileSystemWritableFileStreamTarget's options, when provided, must be an object.\");\n }\n if (options) {\n if (options.chunkSize !== void 0 && (!Number.isInteger(options.chunkSize) || options.chunkSize <= 0)) {\n throw new TypeError(\"options.chunkSize, when provided, must be a positive integer\");\n }\n }\n }\n};\n\n// src/writer.ts\nvar _helper, _helperView;\nvar Writer = class {\n constructor() {\n this.pos = 0;\n __privateAdd(this, _helper, new Uint8Array(8));\n __privateAdd(this, _helperView, new DataView(__privateGet(this, _helper).buffer));\n /**\n * Stores the position from the start of the file to where boxes elements have been written. This is used to\n * rewrite/edit elements that were already added before, and to measure sizes of things.\n */\n this.offsets = /* @__PURE__ */ new WeakMap();\n }\n /** Sets the current position for future writes to a new one. */\n seek(newPos) {\n this.pos = newPos;\n }\n writeU32(value) {\n __privateGet(this, _helperView).setUint32(0, value, false);\n this.write(__privateGet(this, _helper).subarray(0, 4));\n }\n writeU64(value) {\n __privateGet(this, _helperView).setUint32(0, Math.floor(value / 2 ** 32), false);\n __privateGet(this, _helperView).setUint32(4, value, false);\n this.write(__privateGet(this, _helper).subarray(0, 8));\n }\n writeAscii(text) {\n for (let i = 0; i < text.length; i++) {\n __privateGet(this, _helperView).setUint8(i % 8, text.charCodeAt(i));\n if (i % 8 === 7)\n this.write(__privateGet(this, _helper));\n }\n if (text.length % 8 !== 0) {\n this.write(__privateGet(this, _helper).subarray(0, text.length % 8));\n }\n }\n writeBox(box2) {\n this.offsets.set(box2, this.pos);\n if (box2.contents && !box2.children) {\n this.writeBoxHeader(box2, box2.size ?? box2.contents.byteLength + 8);\n this.write(box2.contents);\n } else {\n let startPos = this.pos;\n this.writeBoxHeader(box2, 0);\n if (box2.contents)\n this.write(box2.contents);\n if (box2.children) {\n for (let child of box2.children)\n if (child)\n this.writeBox(child);\n }\n let endPos = this.pos;\n let size = box2.size ?? endPos - startPos;\n this.seek(startPos);\n this.writeBoxHeader(box2, size);\n this.seek(endPos);\n }\n }\n writeBoxHeader(box2, size) {\n this.writeU32(box2.largeSize ? 1 : size);\n this.writeAscii(box2.type);\n if (box2.largeSize)\n this.writeU64(size);\n }\n measureBoxHeader(box2) {\n return 8 + (box2.largeSize ? 8 : 0);\n }\n patchBox(box2) {\n let endPos = this.pos;\n this.seek(this.offsets.get(box2));\n this.writeBox(box2);\n this.seek(endPos);\n }\n measureBox(box2) {\n if (box2.contents && !box2.children) {\n let headerSize = this.measureBoxHeader(box2);\n return headerSize + box2.contents.byteLength;\n } else {\n let result = this.measureBoxHeader(box2);\n if (box2.contents)\n result += box2.contents.byteLength;\n if (box2.children) {\n for (let child of box2.children)\n if (child)\n result += this.measureBox(child);\n }\n return result;\n }\n }\n};\n_helper = new WeakMap();\n_helperView = new WeakMap();\nvar _target, _buffer, _bytes, _maxPos, _ensureSize, ensureSize_fn;\nvar ArrayBufferTargetWriter = class extends Writer {\n constructor(target) {\n super();\n __privateAdd(this, _ensureSize);\n __privateAdd(this, _target, void 0);\n __privateAdd(this, _buffer, new ArrayBuffer(2 ** 16));\n __privateAdd(this, _bytes, new Uint8Array(__privateGet(this, _buffer)));\n __privateAdd(this, _maxPos, 0);\n __privateSet(this, _target, target);\n }\n write(data) {\n __privateMethod(this, _ensureSize, ensureSize_fn).call(this, this.pos + data.byteLength);\n __privateGet(this, _bytes).set(data, this.pos);\n this.pos += data.byteLength;\n __privateSet(this, _maxPos, Math.max(__privateGet(this, _maxPos), this.pos));\n }\n finalize() {\n __privateMethod(this, _ensureSize, ensureSize_fn).call(this, this.pos);\n __privateGet(this, _target).buffer = __privateGet(this, _buffer).slice(0, Math.max(__privateGet(this, _maxPos), this.pos));\n }\n};\n_target = new WeakMap();\n_buffer = new WeakMap();\n_bytes = new WeakMap();\n_maxPos = new WeakMap();\n_ensureSize = new WeakSet();\nensureSize_fn = function(size) {\n let newLength = __privateGet(this, _buffer).byteLength;\n while (newLength < size)\n newLength *= 2;\n if (newLength === __privateGet(this, _buffer).byteLength)\n return;\n let newBuffer = new ArrayBuffer(newLength);\n let newBytes = new Uint8Array(newBuffer);\n newBytes.set(__privateGet(this, _bytes), 0);\n __privateSet(this, _buffer, newBuffer);\n __privateSet(this, _bytes, newBytes);\n};\nvar DEFAULT_CHUNK_SIZE = 2 ** 24;\nvar MAX_CHUNKS_AT_ONCE = 2;\nvar _target2, _sections, _chunked, _chunkSize, _chunks, _writeDataIntoChunks, writeDataIntoChunks_fn, _insertSectionIntoChunk, insertSectionIntoChunk_fn, _createChunk, createChunk_fn, _flushChunks, flushChunks_fn;\nvar StreamTargetWriter = class extends Writer {\n constructor(target) {\n super();\n __privateAdd(this, _writeDataIntoChunks);\n __privateAdd(this, _insertSectionIntoChunk);\n __privateAdd(this, _createChunk);\n __privateAdd(this, _flushChunks);\n __privateAdd(this, _target2, void 0);\n __privateAdd(this, _sections, []);\n __privateAdd(this, _chunked, void 0);\n __privateAdd(this, _chunkSize, void 0);\n /**\n * The data is divided up into fixed-size chunks, whose contents are first filled in RAM and then flushed out.\n * A chunk is flushed if all of its contents have been written.\n */\n __privateAdd(this, _chunks, []);\n __privateSet(this, _target2, target);\n __privateSet(this, _chunked, target.options?.chunked ?? false);\n __privateSet(this, _chunkSize, target.options?.chunkSize ?? DEFAULT_CHUNK_SIZE);\n }\n write(data) {\n __privateGet(this, _sections).push({\n data: data.slice(),\n start: this.pos\n });\n this.pos += data.byteLength;\n }\n flush() {\n if (__privateGet(this, _sections).length === 0)\n return;\n let chunks = [];\n let sorted = [...__privateGet(this, _sections)].sort((a, b) => a.start - b.start);\n chunks.push({\n start: sorted[0].start,\n size: sorted[0].data.byteLength\n });\n for (let i = 1; i < sorted.length; i++) {\n let lastChunk = chunks[chunks.length - 1];\n let section = sorted[i];\n if (section.start <= lastChunk.start + lastChunk.size) {\n lastChunk.size = Math.max(lastChunk.size, section.start + section.data.byteLength - lastChunk.start);\n } else {\n chunks.push({\n start: section.start,\n size: section.data.byteLength\n });\n }\n }\n for (let chunk of chunks) {\n chunk.data = new Uint8Array(chunk.size);\n for (let section of __privateGet(this, _sections)) {\n if (chunk.start <= section.start && section.start < chunk.start + chunk.size) {\n chunk.data.set(section.data, section.start - chunk.start);\n }\n }\n if (__privateGet(this, _chunked)) {\n __privateMethod(this, _writeDataIntoChunks, writeDataIntoChunks_fn).call(this, chunk.data, chunk.start);\n __privateMethod(this, _flushChunks, flushChunks_fn).call(this);\n } else {\n __privateGet(this, _target2).options.onData?.(chunk.data, chunk.start);\n }\n }\n __privateGet(this, _sections).length = 0;\n }\n finalize() {\n if (__privateGet(this, _chunked)) {\n __privateMethod(this, _flushChunks, flushChunks_fn).call(this, true);\n }\n }\n};\n_target2 = new WeakMap();\n_sections = new WeakMap();\n_chunked = new WeakMap();\n_chunkSize = new WeakMap();\n_chunks = new WeakMap();\n_writeDataIntoChunks = new WeakSet();\nwriteDataIntoChunks_fn = function(data, position) {\n let chunkIndex = __privateGet(this, _chunks).findIndex((x) => x.start <= position && position < x.start + __privateGet(this, _chunkSize));\n if (chunkIndex === -1)\n chunkIndex = __privateMethod(this, _createChunk, createChunk_fn).call(this, position);\n let chunk = __privateGet(this, _chunks)[chunkIndex];\n let relativePosition = position - chunk.start;\n let toWrite = data.subarray(0, Math.min(__privateGet(this, _chunkSize) - relativePosition, data.byteLength));\n chunk.data.set(toWrite, relativePosition);\n let section = {\n start: relativePosition,\n end: relativePosition + toWrite.byteLength\n };\n __privateMethod(this, _insertSectionIntoChunk, insertSectionIntoChunk_fn).call(this, chunk, section);\n if (chunk.written[0].start === 0 && chunk.written[0].end === __privateGet(this, _chunkSize)) {\n chunk.shouldFlush = true;\n }\n if (__privateGet(this, _chunks).length > MAX_CHUNKS_AT_ONCE) {\n for (let i = 0; i < __privateGet(this, _chunks).length - 1; i++) {\n __privateGet(this, _chunks)[i].shouldFlush = true;\n }\n __privateMethod(this, _flushChunks, flushChunks_fn).call(this);\n }\n if (toWrite.byteLength < data.byteLength) {\n __privateMethod(this, _writeDataIntoChunks, writeDataIntoChunks_fn).call(this, data.subarray(toWrite.byteLength), position + toWrite.byteLength);\n }\n};\n_insertSectionIntoChunk = new WeakSet();\ninsertSectionIntoChunk_fn = function(chunk, section) {\n let low = 0;\n let high = chunk.written.length - 1;\n let index = -1;\n while (low <= high) {\n let mid = Math.floor(low + (high - low + 1) / 2);\n if (chunk.written[mid].start <= section.start) {\n low = mid + 1;\n index = mid;\n } else {\n high = mid - 1;\n }\n }\n chunk.written.splice(index + 1, 0, section);\n if (index === -1 || chunk.written[index].end < section.start)\n index++;\n while (index < chunk.written.length - 1 && chunk.written[index].end >= chunk.written[index + 1].start) {\n chunk.written[index].end = Math.max(chunk.written[index].end, chunk.written[index + 1].end);\n chunk.written.splice(index + 1, 1);\n }\n};\n_createChunk = new WeakSet();\ncreateChunk_fn = function(includesPosition) {\n let start = Math.floor(includesPosition / __privateGet(this, _chunkSize)) * __privateGet(this, _chunkSize);\n let chunk = {\n start,\n data: new Uint8Array(__privateGet(this, _chunkSize)),\n written: [],\n shouldFlush: false\n };\n __privateGet(this, _chunks).push(chunk);\n __privateGet(this, _chunks).sort((a, b) => a.start - b.start);\n return __privateGet(this, _chunks).indexOf(chunk);\n};\n_flushChunks = new WeakSet();\nflushChunks_fn = function(force = false) {\n for (let i = 0; i < __privateGet(this, _chunks).length; i++) {\n let chunk = __privateGet(this, _chunks)[i];\n if (!chunk.shouldFlush && !force)\n continue;\n for (let section of chunk.written) {\n __privateGet(this, _target2).options.onData?.(\n chunk.data.subarray(section.start, section.end),\n chunk.start + section.start\n );\n }\n __privateGet(this, _chunks).splice(i--, 1);\n }\n};\nvar FileSystemWritableFileStreamTargetWriter = class extends StreamTargetWriter {\n constructor(target) {\n super(new StreamTarget({\n onData: (data, position) => target.stream.write({\n type: \"write\",\n data,\n position\n }),\n chunked: true,\n chunkSize: target.options?.chunkSize\n }));\n }\n};\n\n// src/muxer.ts\nvar GLOBAL_TIMESCALE = 1e3;\nvar SUPPORTED_VIDEO_CODECS = [\"avc\", \"hevc\", \"vp9\", \"av1\"];\nvar SUPPORTED_AUDIO_CODECS = [\"aac\", \"opus\"];\nvar TIMESTAMP_OFFSET = 2082844800;\nvar FIRST_TIMESTAMP_BEHAVIORS = [\"strict\", \"offset\", \"cross-track-offset\"];\nvar _options, _writer, _ftypSize, _mdat, _videoTrack, _audioTrack, _creationTime, _finalizedChunks, _nextFragmentNumber, _videoSampleQueue, _audioSampleQueue, _finalized, _validateOptions, validateOptions_fn, _writeHeader, writeHeader_fn, _computeMoovSizeUpperBound, computeMoovSizeUpperBound_fn, _prepareTracks, prepareTracks_fn, _generateMpeg4AudioSpecificConfig, generateMpeg4AudioSpecificConfig_fn, _createSampleForTrack, createSampleForTrack_fn, _addSampleToTrack, addSampleToTrack_fn, _validateTimestamp, validateTimestamp_fn, _finalizeCurrentChunk, finalizeCurrentChunk_fn, _finalizeFragment, finalizeFragment_fn, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn, _ensureNotFinalized, ensureNotFinalized_fn;\nvar Muxer = class {\n constructor(options) {\n __privateAdd(this, _validateOptions);\n __privateAdd(this, _writeHeader);\n __privateAdd(this, _computeMoovSizeUpperBound);\n __privateAdd(this, _prepareTracks);\n // https://wiki.multimedia.cx/index.php/MPEG-4_Audio\n __privateAdd(this, _generateMpeg4AudioSpecificConfig);\n __privateAdd(this, _createSampleForTrack);\n __privateAdd(this, _addSampleToTrack);\n __privateAdd(this, _validateTimestamp);\n __privateAdd(this, _finalizeCurrentChunk);\n __privateAdd(this, _finalizeFragment);\n __privateAdd(this, _maybeFlushStreamingTargetWriter);\n __privateAdd(this, _ensureNotFinalized);\n __privateAdd(this, _options, void 0);\n __privateAdd(this, _writer, void 0);\n __privateAdd(this, _ftypSize, void 0);\n __privateAdd(this, _mdat, void 0);\n __privateAdd(this, _videoTrack, null);\n __privateAdd(this, _audioTrack, null);\n __privateAdd(this, _creationTime, Math.floor(Date.now() / 1e3) + TIMESTAMP_OFFSET);\n __privateAdd(this, _finalizedChunks, []);\n // Fields for fragmented MP4:\n __privateAdd(this, _nextFragmentNumber, 1);\n __privateAdd(this, _videoSampleQueue, []);\n __privateAdd(this, _audioSampleQueue, []);\n __privateAdd(this, _finalized, false);\n __privateMethod(this, _validateOptions, validateOptions_fn).call(this, options);\n options.video = deepClone(options.video);\n options.audio = deepClone(options.audio);\n options.fastStart = deepClone(options.fastStart);\n this.target = options.target;\n __privateSet(this, _options, {\n firstTimestampBehavior: \"strict\",\n ...options\n });\n if (options.target instanceof ArrayBufferTarget) {\n __privateSet(this, _writer, new ArrayBufferTargetWriter(options.target));\n } else if (options.target instanceof StreamTarget) {\n __privateSet(this, _writer, new StreamTargetWriter(options.target));\n } else if (options.target instanceof FileSystemWritableFileStreamTarget) {\n __privateSet(this, _writer, new FileSystemWritableFileStreamTargetWriter(options.target));\n } else {\n throw new Error(`Invalid target: ${options.target}`);\n }\n __privateMethod(this, _prepareTracks, prepareTracks_fn).call(this);\n __privateMethod(this, _writeHeader, writeHeader_fn).call(this);\n }\n addVideoChunk(sample, meta, timestamp, compositionTimeOffset) {\n if (!(sample instanceof EncodedVideoChunk)) {\n throw new TypeError(\"addVideoChunk's first argument (sample) must be of type EncodedVideoChunk.\");\n }\n if (meta && typeof meta !== \"object\") {\n throw new TypeError(\"addVideoChunk's second argument (meta), when provided, must be an object.\");\n }\n if (timestamp !== void 0 && (!Number.isFinite(timestamp) || timestamp < 0)) {\n throw new TypeError(\n \"addVideoChunk's third argument (timestamp), when provided, must be a non-negative real number.\"\n );\n }\n if (compositionTimeOffset !== void 0 && !Number.isFinite(compositionTimeOffset)) {\n throw new TypeError(\n \"addVideoChunk's fourth argument (compositionTimeOffset), when provided, must be a real number.\"\n );\n }\n let data = new Uint8Array(sample.byteLength);\n sample.copyTo(data);\n this.addVideoChunkRaw(\n data,\n sample.type,\n timestamp ?? sample.timestamp,\n sample.duration,\n meta,\n compositionTimeOffset\n );\n }\n addVideoChunkRaw(data, type, timestamp, duration, meta, compositionTimeOffset) {\n if (!(data instanceof Uint8Array)) {\n throw new TypeError(\"addVideoChunkRaw's first argument (data) must be an instance of Uint8Array.\");\n }\n if (type !== \"key\" && type !== \"delta\") {\n throw new TypeError(\"addVideoChunkRaw's second argument (type) must be either 'key' or 'delta'.\");\n }\n if (!Number.isFinite(timestamp) || timestamp < 0) {\n throw new TypeError(\"addVideoChunkRaw's third argument (timestamp) must be a non-negative real number.\");\n }\n if (!Number.isFinite(duration) || duration < 0) {\n throw new TypeError(\"addVideoChunkRaw's fourth argument (duration) must be a non-negative real number.\");\n }\n if (meta && typeof meta !== \"object\") {\n throw new TypeError(\"addVideoChunkRaw's fifth argument (meta), when provided, must be an object.\");\n }\n if (compositionTimeOffset !== void 0 && !Number.isFinite(compositionTimeOffset)) {\n throw new TypeError(\n \"addVideoChunkRaw's sixth argument (compositionTimeOffset), when provided, must be a real number.\"\n );\n }\n __privateMethod(this, _ensureNotFinalized, ensureNotFinalized_fn).call(this);\n if (!__privateGet(this, _options).video)\n throw new Error(\"No video track declared.\");\n if (typeof __privateGet(this, _options).fastStart === \"object\" && __privateGet(this, _videoTrack).samples.length === __privateGet(this, _options).fastStart.expectedVideoChunks) {\n throw new Error(`Cannot add more video chunks than specified in 'fastStart' (${__privateGet(this, _options).fastStart.expectedVideoChunks}).`);\n }\n let videoSample = __privateMethod(this, _createSampleForTrack, createSampleForTrack_fn).call(this, __privateGet(this, _videoTrack), data, type, timestamp, duration, meta, compositionTimeOffset);\n if (__privateGet(this, _options).fastStart === \"fragmented\" && __privateGet(this, _audioTrack)) {\n while (__privateGet(this, _audioSampleQueue).length > 0 && __privateGet(this, _audioSampleQueue)[0].decodeTimestamp <= videoSample.decodeTimestamp) {\n let audioSample = __privateGet(this, _audioSampleQueue).shift();\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);\n }\n if (videoSample.decodeTimestamp <= __privateGet(this, _audioTrack).lastDecodeTimestamp) {\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);\n } else {\n __privateGet(this, _videoSampleQueue).push(videoSample);\n }\n } else {\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);\n }\n }\n addAudioChunk(sample, meta, timestamp) {\n if (!(sample instanceof EncodedAudioChunk)) {\n throw new TypeError(\"addAudioChunk's first argument (sample) must be of type EncodedAudioChunk.\");\n }\n if (meta && typeof meta !== \"object\") {\n throw new TypeError(\"addAudioChunk's second argument (meta), when provided, must be an object.\");\n }\n if (timestamp !== void 0 && (!Number.isFinite(timestamp) || timestamp < 0)) {\n throw new TypeError(\n \"addAudioChunk's third argument (timestamp), when provided, must be a non-negative real number.\"\n );\n }\n let data = new Uint8Array(sample.byteLength);\n sample.copyTo(data);\n this.addAudioChunkRaw(data, sample.type, timestamp ?? sample.timestamp, sample.duration, meta);\n }\n addAudioChunkRaw(data, type, timestamp, duration, meta) {\n if (!(data instanceof Uint8Array)) {\n throw new TypeError(\"addAudioChunkRaw's first argument (data) must be an instance of Uint8Array.\");\n }\n if (type !== \"key\" && type !== \"delta\") {\n throw new TypeError(\"addAudioChunkRaw's second argument (type) must be either 'key' or 'delta'.\");\n }\n if (!Number.isFinite(timestamp) || timestamp < 0) {\n throw new TypeError(\"addAudioChunkRaw's third argument (timestamp) must be a non-negative real number.\");\n }\n if (!Number.isFinite(duration) || duration < 0) {\n throw new TypeError(\"addAudioChunkRaw's fourth argument (duration) must be a non-negative real number.\");\n }\n if (meta && typeof meta !== \"object\") {\n throw new TypeError(\"addAudioChunkRaw's fifth argument (meta), when provided, must be an object.\");\n }\n __privateMethod(this, _ensureNotFinalized, ensureNotFinalized_fn).call(this);\n if (!__privateGet(this, _options).audio)\n throw new Error(\"No audio track declared.\");\n if (typeof __privateGet(this, _options).fastStart === \"object\" && __privateGet(this, _audioTrack).samples.length === __privateGet(this, _options).fastStart.expectedAudioChunks) {\n throw new Error(`Cannot add more audio chunks than specified in 'fastStart' (${__privateGet(this, _options).fastStart.expectedAudioChunks}).`);\n }\n let audioSample = __privateMethod(this, _createSampleForTrack, createSampleForTrack_fn).call(this, __privateGet(this, _audioTrack), data, type, timestamp, duration, meta);\n if (__privateGet(this, _options).fastStart === \"fragmented\" && __privateGet(this, _videoTrack)) {\n while (__privateGet(this, _videoSampleQueue).length > 0 && __privateGet(this, _videoSampleQueue)[0].decodeTimestamp <= audioSample.decodeTimestamp) {\n let videoSample = __privateGet(this, _videoSampleQueue).shift();\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);\n }\n if (audioSample.decodeTimestamp <= __privateGet(this, _videoTrack).lastDecodeTimestamp) {\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);\n } else {\n __privateGet(this, _audioSampleQueue).push(audioSample);\n }\n } else {\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);\n }\n }\n /** Finalizes the file, making it ready for use. Must be called after all video and audio chunks have been added. */\n finalize() {\n if (__privateGet(this, _finalized)) {\n throw new Error(\"Cannot finalize a muxer more than once.\");\n }\n if (__privateGet(this, _options).fastStart === \"fragmented\") {\n for (let videoSample of __privateGet(this, _videoSampleQueue))\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);\n for (let audioSample of __privateGet(this, _audioSampleQueue))\n __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);\n __privateMethod(this, _finalizeFragment, finalizeFragment_fn).call(this, false);\n } else {\n if (__privateGet(this, _videoTrack))\n __privateMethod(this, _finalizeCurrentChunk, finalizeCurrentChunk_fn).call(this, __privateGet(this, _videoTrack));\n if (__privateGet(this, _audioTrack))\n __privateMethod(this, _finalizeCurrentChunk, finalizeCurrentChunk_fn).call(this, __privateGet(this, _audioTrack));\n }\n let tracks = [__privateGet(this, _videoTrack), __privateGet(this, _audioTrack)].filter(Boolean);\n if (__privateGet(this, _options).fastStart === \"in-memory\") {\n let mdatSize;\n for (let i = 0; i < 2; i++) {\n let movieBox2 = moov(tracks, __privateGet(this, _creationTime));\n let movieBoxSize = __privateGet(this, _writer).measureBox(movieBox2);\n mdatSize = __privateGet(this, _writer).measureBox(__privateGet(this, _mdat));\n let currentChunkPos = __privateGet(this, _writer).pos + movieBoxSize + mdatSize;\n for (let chunk of __privateGet(this, _finalizedChunks)) {\n chunk.offset = currentChunkPos;\n for (let { data } of chunk.samples) {\n currentChunkPos += data.byteLength;\n mdatSize += data.byteLength;\n }\n }\n if (currentChunkPos < 2 ** 32)\n break;\n if (mdatSize >= 2 ** 32)\n __privateGet(this, _mdat).largeSize = true;\n }\n let movieBox = moov(tracks, __privateGet(this, _creationTime));\n __privateGet(this, _writer).writeBox(movieBox);\n __privateGet(this, _mdat).size = mdatSize;\n __privateGet(this, _writer).writeBox(__privateGet(this, _mdat));\n for (let chunk of __privateGet(this, _finalizedChunks)) {\n for (let sample of chunk.samples) {\n __privateGet(this, _writer).write(sample.data);\n sample.data = null;\n }\n }\n } else if (__privateGet(this, _options).fastStart === \"fragmented\") {\n let startPos = __privateGet(this, _writer).pos;\n let mfraBox = mfra(tracks);\n __privateGet(this, _writer).writeBox(mfraBox);\n let mfraBoxSize = __privateGet(this, _writer).pos - startPos;\n __privateGet(this, _writer).seek(__privateGet(this, _writer).pos - 4);\n __privateGet(this, _writer).writeU32(mfraBoxSize);\n } else {\n let mdatPos = __privateGet(this, _writer).offsets.get(__privateGet(this, _mdat));\n let mdatSize = __privateGet(this, _writer).pos - mdatPos;\n __privateGet(this, _mdat).size = mdatSize;\n __privateGet(this, _mdat).largeSize = mdatSize >= 2 ** 32;\n __privateGet(this, _writer).patchBox(__privateGet(this, _mdat));\n let movieBox = moov(tracks, __privateGet(this, _creationTime));\n if (typeof __privateGet(this, _options).fastStart === \"object\") {\n __privateGet(this, _writer).seek(__privateGet(this, _ftypSize));\n __privateGet(this, _writer).writeBox(movieBox);\n let remainingBytes = mdatPos - __privateGet(this, _writer).pos;\n __privateGet(this, _writer).writeBox(free(remainingBytes));\n } else {\n __privateGet(this, _writer).writeBox(movieBox);\n }\n }\n __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);\n __privateGet(this, _writer).finalize();\n __privateSet(this, _finalized, true);\n }\n};\n_options = new WeakMap();\n_writer = new WeakMap();\n_ftypSize = new WeakMap();\n_mdat = new WeakMap();\n_videoTrack = new WeakMap();\n_audioTrack = new WeakMap();\n_creationTime = new WeakMap();\n_finalizedChunks = new WeakMap();\n_nextFragmentNumber = new WeakMap();\n_videoSampleQueue = new WeakMap();\n_audioSampleQueue = new WeakMap();\n_finalized = new WeakMap();\n_validateOptions = new WeakSet();\nvalidateOptions_fn = function(options) {\n if (typeof options !== \"object\") {\n throw new TypeError(\"The muxer requires an options object to be passed to its constructor.\");\n }\n if (!(options.target instanceof Target)) {\n throw new TypeError(\"The target must be provided and an instance of Target.\");\n }\n if (options.video) {\n if (!SUPPORTED_VIDEO_CODECS.includes(options.video.codec)) {\n throw new TypeError(`Unsupported video codec: ${options.video.codec}`);\n }\n if (!Number.isInteger(options.video.width) || options.video.width <= 0) {\n throw new TypeError(`Invalid video width: ${options.video.width}. Must be a positive integer.`);\n }\n if (!Number.isInteger(options.video.height) || options.video.height <= 0) {\n throw new TypeError(`Invalid video height: ${options.video.height}. Must be a positive integer.`);\n }\n const videoRotation = options.video.rotation;\n if (typeof videoRotation === \"number\" && ![0, 90, 180, 270].includes(videoRotation)) {\n throw new TypeError(`Invalid video rotation: ${videoRotation}. Has to be 0, 90, 180 or 270.`);\n } else if (Array.isArray(videoRotation) && (videoRotation.length !== 9 || videoRotation.some((value) => typeof value !== \"number\"))) {\n throw new TypeError(`Invalid video transformation matrix: ${videoRotation.join()}`);\n }\n if (options.video.frameRate !== void 0 && (!Number.isInteger(options.video.frameRate) || options.video.frameRate <= 0)) {\n throw new TypeError(\n `Invalid video frame rate: ${options.video.frameRate}. Must be a positive integer.`\n );\n }\n }\n if (options.audio) {\n if (!SUPPORTED_AUDIO_CODECS.includes(options.audio.codec)) {\n throw new TypeError(`Unsupported audio codec: ${options.audio.codec}`);\n }\n if (!Number.isInteger(options.audio.numberOfChannels) || options.audio.numberOfChannels <= 0) {\n throw new TypeError(\n `Invalid number of audio channels: ${options.audio.numberOfChannels}. Must be a positive integer.`\n );\n }\n if (!Number.isInteger(options.audio.sampleRate) || options.audio.sampleRate <= 0) {\n throw new TypeError(\n `Invalid audio sample rate: ${options.audio.sampleRate}. Must be a positive integer.`\n );\n }\n }\n if (options.firstTimestampBehavior && !FIRST_TIMESTAMP_BEHAVIORS.includes(options.firstTimestampBehavior)) {\n throw new TypeError(`Invalid first timestamp behavior: ${options.firstTimestampBehavior}`);\n }\n if (typeof options.fastStart === \"object\") {\n if (options.video) {\n if (options.fastStart.expectedVideoChunks === void 0) {\n throw new TypeError(`'fastStart' is an object but is missing property 'expectedVideoChunks'.`);\n } else if (!Number.isInteger(options.fastStart.expectedVideoChunks) || options.fastStart.expectedVideoChunks < 0) {\n throw new TypeError(`'expectedVideoChunks' must be a non-negative integer.`);\n }\n }\n if (options.audio) {\n if (options.fastStart.expectedAudioChunks === void 0) {\n throw new TypeError(`'fastStart' is an object but is missing property 'expectedAudioChunks'.`);\n } else if (!Number.isInteger(options.fastStart.expectedAudioChunks) || options.fastStart.expectedAudioChunks < 0) {\n throw new TypeError(`'expectedAudioChunks' must be a non-negative integer.`);\n }\n }\n } else if (![false, \"in-memory\", \"fragmented\"].includes(options.fastStart)) {\n throw new TypeError(`'fastStart' option must be false, 'in-memory', 'fragmented' or an object.`);\n }\n if (options.minFragmentDuration !== void 0 && (!Number.isFinite(options.minFragmentDuration) || options.minFragmentDuration < 0)) {\n throw new TypeError(`'minFragmentDuration' must be a non-negative number.`);\n }\n};\n_writeHeader = new WeakSet();\nwriteHeader_fn = function() {\n __privateGet(this, _writer).writeBox(ftyp({\n holdsAvc: __privateGet(this, _options).video?.codec === \"avc\",\n fragmented: __privateGet(this, _options).fastStart === \"fragmented\"\n }));\n __privateSet(this, _ftypSize, __privateGet(this, _writer).pos);\n if (__privateGet(this, _options).fastStart === \"in-memory\") {\n __privateSet(this, _mdat, mdat(false));\n } else if (__privateGet(this, _options).fastStart === \"fragmented\") {\n } else {\n if (typeof __privateGet(this, _options).fastStart === \"object\") {\n let moovSizeUpperBound = __privateMethod(this, _computeMoovSizeUpperBound, computeMoovSizeUpperBound_fn).call(this);\n __privateGet(this, _writer).seek(__privateGet(this, _writer).pos + moovSizeUpperBound);\n }\n __privateSet(this, _mdat, mdat(true));\n __privateGet(this, _writer).writeBox(__privateGet(this, _mdat));\n }\n __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);\n};\n_computeMoovSizeUpperBound = new WeakSet();\ncomputeMoovSizeUpperBound_fn = function() {\n if (typeof __privateGet(this, _options).fastStart !== \"object\")\n return;\n let upperBound = 0;\n let sampleCounts = [\n __privateGet(this, _options).fastStart.expectedVideoChunks,\n __privateGet(this, _options).fastStart.expectedAudioChunks\n ];\n for (let n of sampleCounts) {\n if (!n)\n continue;\n upperBound += (4 + 4) * Math.ceil(2 / 3 * n);\n upperBound += 4 * n;\n upperBound += (4 + 4 + 4) * Math.ceil(2 / 3 * n);\n upperBound += 4 * n;\n upperBound += 8 * n;\n }\n upperBound += 4096;\n return upperBound;\n};\n_prepareTracks = new WeakSet();\nprepareTracks_fn = function() {\n if (__privateGet(this, _options).video) {\n __privateSet(this, _videoTrack, {\n id: 1,\n info: {\n type: \"video\",\n codec: __privateGet(this, _options).video.codec,\n width: __privateGet(this, _options).video.width,\n height: __privateGet(this, _options).video.height,\n rotation: __privateGet(this, _options).video.rotation ?? 0,\n decoderConfig: null\n },\n // The fallback contains many common frame rates as factors\n timescale: __privateGet(this, _options).video.frameRate ?? 57600,\n samples: [],\n finalizedChunks: [],\n currentChunk: null,\n firstDecodeTimestamp: void 0,\n lastDecodeTimestamp: -1,\n timeToSampleTable: [],\n compositionTimeOffsetTable: [],\n lastTimescaleUnits: null,\n lastSample: null,\n compactlyCodedChunkTable: []\n });\n }\n if (__privateGet(this, _options).audio) {\n __privateSet(this, _audioTrack, {\n id: __privateGet(this, _options).video ? 2 : 1,\n info: {\n type: \"audio\",\n codec: __privateGet(this, _options).audio.codec,\n numberOfChannels: __privateGet(this, _options).audio.numberOfChannels,\n sampleRate: __privateGet(this, _options).audio.sampleRate,\n decoderConfig: null\n },\n timescale: __privateGet(this, _options).audio.sampleRate,\n samples: [],\n finalizedChunks: [],\n currentChunk: null,\n firstDecodeTimestamp: void 0,\n lastDecodeTimestamp: -1,\n timeToSampleTable: [],\n compositionTimeOffsetTable: [],\n lastTimescaleUnits: null,\n lastSample: null,\n compactlyCodedChunkTable: []\n });\n if (__privateGet(this, _options).audio.codec === \"aac\") {\n let guessedCodecPrivate = __privateMethod(this, _generateMpeg4AudioSpecificConfig, generateMpeg4AudioSpecificConfig_fn).call(\n this,\n 2,\n // Object type for AAC-LC, since it's the most common\n __privateGet(this, _options).audio.sampleRate,\n __privateGet(this, _options).audio.numberOfChannels\n );\n __privateGet(this, _audioTrack).info.decoderConfig = {\n codec: __privateGet(this, _options).audio.codec,\n description: guessedCodecPrivate,\n numberOfChannels: __privateGet(this, _options).audio.numberOfChannels,\n sampleRate: __privateGet(this, _options).audio.sampleRate\n };\n }\n }\n};\n_generateMpeg4AudioSpecificConfig = new WeakSet();\ngenerateMpeg4AudioSpecificConfig_fn = function(objectType, sampleRate, numberOfChannels) {\n let frequencyIndices = [96e3, 88200, 64e3, 48e3, 44100, 32e3, 24e3, 22050, 16e3, 12e3, 11025, 8e3, 7350];\n let frequencyIndex = frequencyIndices.indexOf(sampleRate);\n let channelConfig = numberOfChannels;\n let configBits = \"\";\n configBits += objectType.toString(2).padStart(5, \"0\");\n configBits += frequencyIndex.toString(2).padStart(4, \"0\");\n if (frequencyIndex === 15)\n configBits += sampleRate.toString(2).padStart(24, \"0\");\n configBits += channelConfig.toString(2).padStart(4, \"0\");\n let paddingLength = Math.ceil(configBits.length / 8) * 8;\n configBits = configBits.padEnd(paddingLength, \"0\");\n let configBytes = new Uint8Array(configBits.length / 8);\n for (let i = 0; i < configBits.length; i += 8) {\n configBytes[i / 8] = parseInt(configBits.slice(i, i + 8), 2);\n }\n return configBytes;\n};\n_createSampleForTrack = new WeakSet();\ncreateSampleForTrack_fn = function(track, data, type, timestamp, duration, meta, compositionTimeOffset) {\n let presentationTimestampInSeconds = timestamp / 1e6;\n let decodeTimestampInSeconds = (timestamp - (compositionTimeOffset ?? 0)) / 1e6;\n let durationInSeconds = duration / 1e6;\n let adjusted = __privateMethod(this, _validateTimestamp, validateTimestamp_fn).call(this, presentationTimestampInSeconds, decodeTimestampInSeconds, track);\n presentationTimestampInSeconds = adjusted.presentationTimestamp;\n decodeTimestampInSeconds = adjusted.decodeTimestamp;\n if (meta?.decoderConfig) {\n if (track.info.decoderConfig === null) {\n track.info.decoderConfig = meta.decoderConfig;\n } else {\n Object.assign(track.info.decoderConfig, meta.decoderConfig);\n }\n }\n let sample = {\n presentationTimestamp: presentationTimestampInSeconds,\n decodeTimestamp: decodeTimestampInSeconds,\n duration: durationInSeconds,\n data,\n size: data.byteLength,\n type,\n // Will be refined once the next sample comes in\n timescaleUnitsToNextSample: intoTimescale(durationInSeconds, track.timescale)\n };\n return sample;\n};\n_addSampleToTrack = new WeakSet();\naddSampleToTrack_fn = function(track, sample) {\n if (__privateGet(this, _options).fastStart !== \"fragmented\") {\n track.samples.push(sample);\n }\n const sampleCompositionTimeOffset = intoTimescale(sample.presentationTimestamp - sample.decodeTimestamp, track.timescale);\n if (track.lastTimescaleUnits !== null) {\n let timescaleUnits = intoTimescale(sample.decodeTimestamp, track.timescale, false);\n let delta = Math.round(timescaleUnits - track.lastTimescaleUnits);\n track.lastTimescaleUnits += delta;\n track.lastSample.timescaleUnitsToNextSample = delta;\n if (__privateGet(this, _options).fastStart !== \"fragmented\") {\n let lastTableEntry = last(track.timeToSampleTable);\n if (lastTableEntry.sampleCount === 1) {\n lastTableEntry.sampleDelta = delta;\n lastTableEntry.sampleCount++;\n } else if (lastTableEntry.sampleDelta === delta) {\n lastTableEntry.sampleCount++;\n } else {\n lastTableEntry.sampleCount--;\n track.timeToSampleTable.push({\n sampleCount: 2,\n sampleDelta: delta\n });\n }\n const lastCompositionTimeOffsetTableEntry = last(track.compositionTimeOffsetTable);\n if (lastCompositionTimeOffsetTableEntry.sampleCompositionTimeOffset === sampleCompositionTimeOffset) {\n lastCompositionTimeOffsetTableEntry.sampleCount++;\n } else {\n track.compositionTimeOffsetTable.push({\n sampleCount: 1,\n sampleCompositionTimeOffset\n });\n }\n }\n } else {\n track.lastTimescaleUnits = 0;\n if (__privateGet(this, _options).fastStart !== \"fragmented\") {\n track.timeToSampleTable.push({\n sampleCount: 1,\n sampleDelta: intoTimescale(sample.duration, track.timescale)\n });\n track.compositionTimeOffsetTable.push({\n sampleCount: 1,\n sampleCompositionTimeOffset\n });\n }\n }\n track.lastSample = sample;\n let beginNewChunk = false;\n if (!track.currentChunk) {\n beginNewChunk = true;\n } else {\n let currentChunkDuration = sample.presentationTimestamp - track.currentChunk.startTimestamp;\n if (__privateGet(this, _options).fastStart === \"fragmented\") {\n let mostImportantTrack = __privateGet(this, _videoTrack) ?? __privateGet(this, _audioTrack);\n const chunkDuration = __privateGet(this, _options).minFragmentDuration ?? 1;\n if (track === mostImportantTrack && sample.type === \"key\" && currentChunkDuration >= chunkDuration) {\n beginNewChunk = true;\n __privateMethod(this, _finalizeFragment, finalizeFragment_fn).call(this);\n }\n } else {\n beginNewChunk = currentChunkDuration >= 0.5;\n }\n }\n if (beginNewChunk) {\n if (track.currentChunk) {\n __privateMethod(this, _finalizeCurrentChunk, finalizeCurrentChunk_fn).call(this, track);\n }\n track.currentChunk = {\n startTimestamp: sample.presentationTimestamp,\n samples: []\n };\n }\n track.currentChunk.samples.push(sample);\n};\n_validateTimestamp = new WeakSet();\nvalidateTimestamp_fn = function(presentationTimestamp, decodeTimestamp, track) {\n const strictTimestampBehavior = __privateGet(this, _options).firstTimestampBehavior === \"strict\";\n const noLastDecodeTimestamp = track.lastDecodeTimestamp === -1;\n const timestampNonZero = decodeTimestamp !== 0;\n if (strictTimestampBehavior && noLastDecodeTimestamp && timestampNonZero) {\n throw new Error(\n `The first chunk for your media track must have a timestamp of 0 (received DTS=${decodeTimestamp}).Non-zero first timestamps are often caused by directly piping frames or audio data from a MediaStreamTrack into the encoder. Their timestamps are typically relative to the age of thedocument, which is probably what you want.\n\nIf you want to offset all timestamps of a track such that the first one is zero, set firstTimestampBehavior: 'offset' in the options.\n`\n );\n } else if (__privateGet(this, _options).firstTimestampBehavior === \"offset\" || __privateGet(this, _options).firstTimestampBehavior === \"cross-track-offset\") {\n if (track.firstDecodeTimestamp === void 0) {\n track.firstDecodeTimestamp = decodeTimestamp;\n }\n let baseDecodeTimestamp;\n if (__privateGet(this, _options).firstTimestampBehavior === \"offset\") {\n baseDecodeTimestamp = track.firstDecodeTimestamp;\n } else {\n baseDecodeTimestamp = Math.min(\n __privateGet(this, _videoTrack)?.firstDecodeTimestamp ?? Infinity,\n __privateGet(this, _audioTrack)?.firstDecodeTimestamp ?? Infinity\n );\n }\n decodeTimestamp -= baseDecodeTimestamp;\n presentationTimestamp -= baseDecodeTimestamp;\n }\n if (decodeTimestamp < track.lastDecodeTimestamp) {\n throw new Error(\n `Timestamps must be monotonically increasing (DTS went from ${track.lastDecodeTimestamp * 1e6} to ${decodeTimestamp * 1e6}).`\n );\n }\n track.lastDecodeTimestamp = decodeTimestamp;\n return { presentationTimestamp, decodeTimestamp };\n};\n_finalizeCurrentChunk = new WeakSet();\nfinalizeCurrentChunk_fn = function(track) {\n if (__privateGet(this, _options).fastStart === \"fragmented\") {\n throw new Error(\"Can't finalize individual chunks if 'fastStart' is set to 'fragmented'.\");\n }\n if (!track.currentChunk)\n return;\n track.finalizedChunks.push(track.currentChunk);\n __privateGet(this, _finalizedChunks).push(track.currentChunk);\n if (track.compactlyCodedChunkTable.length === 0 || last(track.compactlyCodedChunkTable).samplesPerChunk !== track.currentChunk.samples.length) {\n track.compactlyCodedChunkTable.push({\n firstChunk: track.finalizedChunks.length,\n // 1-indexed\n samplesPerChunk: track.currentChunk.samples.length\n });\n }\n if (__privateGet(this, _options).fastStart === \"in-memory\") {\n track.currentChunk.offset = 0;\n return;\n }\n track.currentChunk.offset = __privateGet(this, _writer).pos;\n for (let sample of track.currentChunk.samples) {\n __privateGet(this, _writer).write(sample.data);\n sample.data = null;\n }\n __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);\n};\n_finalizeFragment = new WeakSet();\nfinalizeFragment_fn = function(flushStreamingWriter = true) {\n if (__privateGet(this, _options).fastStart !== \"fragmented\") {\n throw new Error(\"Can't finalize a fragment unless 'fastStart' is set to 'fragmented'.\");\n }\n let tracks = [__privateGet(this, _videoTrack), __privateGet(this, _audioTrack)].filter((track) => track && track.currentChunk);\n if (tracks.length === 0)\n return;\n let fragmentNumber = __privateWrapper(this, _nextFragmentNumber)._++;\n if (fragmentNumber === 1) {\n let movieBox = moov(tracks, __privateGet(this, _creationTime), true);\n __privateGet(this, _writer).writeBox(movieBox);\n }\n let moofOffset = __privateGet(this, _writer).pos;\n let moofBox = moof(fragmentNumber, tracks);\n __privateGet(this, _writer).writeBox(moofBox);\n {\n let mdatBox = mdat(false);\n let totalTrackSampleSize = 0;\n for (let track of tracks) {\n for (let sample of track.currentChunk.samples) {\n totalTrackSampleSize += sample.size;\n }\n }\n let mdatSize = __privateGet(this, _writer).measureBox(mdatBox) + totalTrackSampleSize;\n if (mdatSize >= 2 ** 32) {\n mdatBox.largeSize = true;\n mdatSize = __privateGet(this, _writer).measureBox(mdatBox) + totalTrackSampleSize;\n }\n mdatBox.size = mdatSize;\n __privateGet(this, _writer).writeBox(mdatBox);\n }\n for (let track of tracks) {\n track.currentChunk.offset = __privateGet(this, _writer).pos;\n track.currentChunk.moofOffset = moofOffset;\n for (let sample of track.currentChunk.samples) {\n __privateGet(this, _writer).write(sample.data);\n sample.data = null;\n }\n }\n let endPos = __privateGet(this, _writer).pos;\n __privateGet(this, _writer).seek(__privateGet(this, _writer).offsets.get(moofBox));\n let newMoofBox = moof(fragmentNumber, tracks);\n __privateGet(this, _writer).writeBox(newMoofBox);\n __privateGet(this, _writer).seek(endPos);\n for (let track of tracks) {\n track.finalizedChunks.push(track.currentChunk);\n __privateGet(this, _finalizedChunks).push(track.currentChunk);\n track.currentChunk = null;\n }\n if (flushStreamingWriter) {\n __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);\n }\n};\n_maybeFlushStreamingTargetWriter = new WeakSet();\nmaybeFlushStreamingTargetWriter_fn = function() {\n if (__privateGet(this, _writer) instanceof StreamTargetWriter) {\n __privateGet(this, _writer).flush();\n }\n};\n_ensureNotFinalized = new WeakSet();\nensureNotFinalized_fn = function() {\n if (__privateGet(this, _finalized)) {\n throw new Error(\"Cannot add new video or audio chunks after the file has been finalized.\");\n }\n};\nexport {\n ArrayBufferTarget,\n FileSystemWritableFileStreamTarget,\n Muxer,\n StreamTarget\n};\n";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { RenderOptions, VisualDocument } from "../types";
|
|
2
|
+
export interface EmbedHtmlOptions extends RenderOptions {
|
|
3
|
+
title?: string;
|
|
4
|
+
fps?: number;
|
|
5
|
+
maxFrames?: number;
|
|
6
|
+
autoplay?: boolean;
|
|
7
|
+
loop?: boolean;
|
|
8
|
+
includeExportControls?: boolean;
|
|
9
|
+
chromeBackground?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function renderToEmbedHtml(document: VisualDocument, options?: EmbedHtmlOptions): string;
|
|
@@ -0,0 +1,698 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.renderToEmbedHtml = renderToEmbedHtml;
|
|
4
|
+
const mp4_muxer_source_1 = require("../mp4-muxer-source");
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
const svg_1 = require("./svg");
|
|
7
|
+
function renderToEmbedHtml(document, options = {}) {
|
|
8
|
+
const title = String(options.title ?? "Sketchmark Embed");
|
|
9
|
+
const duration = Math.max(0, Number(document.canvas.duration ?? 0) || 0);
|
|
10
|
+
const initialTime = (0, utils_1.clamp)(Number(options.time ?? 0) || 0, 0, duration);
|
|
11
|
+
const fps = normalizePositiveInteger(options.fps, normalizePositiveInteger(document.canvas.fps, 24));
|
|
12
|
+
const maxFrames = normalizePositiveInteger(options.maxFrames, 180);
|
|
13
|
+
const frameCount = sampledFrameCount(duration, fps, maxFrames);
|
|
14
|
+
const frameTimes = Array.from({ length: frameCount }, (_, index) => frameTimeAt(index, frameCount, duration));
|
|
15
|
+
const frames = frameTimes.map((time) => (0, svg_1.renderToSvg)(document, {
|
|
16
|
+
time,
|
|
17
|
+
transparent: options.transparent
|
|
18
|
+
}));
|
|
19
|
+
const initialFrameIndex = frameIndexForTime(initialTime, frameTimes);
|
|
20
|
+
const initialFrame = frames[initialFrameIndex] ?? frames[0] ?? (0, svg_1.renderToSvg)(document, options);
|
|
21
|
+
const chromeBackground = escapeHtml(String(options.chromeBackground ?? "transparent"));
|
|
22
|
+
const mp4MuxerRuntimeSource = inlineMp4MuxerRuntime(mp4_muxer_source_1.MP4_MUXER_SOURCE);
|
|
23
|
+
const statusLabel = escapeHtml(title || "Sketchmark embed");
|
|
24
|
+
const payload = {
|
|
25
|
+
title,
|
|
26
|
+
fileBase: safeFileName(title),
|
|
27
|
+
canvas: {
|
|
28
|
+
width: Math.max(1, Math.round(Number(document.canvas.width) || 1)),
|
|
29
|
+
height: Math.max(1, Math.round(Number(document.canvas.height) || 1))
|
|
30
|
+
},
|
|
31
|
+
document,
|
|
32
|
+
duration,
|
|
33
|
+
initialTime,
|
|
34
|
+
frameTimes,
|
|
35
|
+
autoplay: options.autoplay ?? duration > 0,
|
|
36
|
+
loop: options.loop ?? true,
|
|
37
|
+
includeExportControls: options.includeExportControls !== false,
|
|
38
|
+
frames
|
|
39
|
+
};
|
|
40
|
+
return `<!doctype html>
|
|
41
|
+
<html>
|
|
42
|
+
<head>
|
|
43
|
+
<meta charset="utf-8" />
|
|
44
|
+
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
|
45
|
+
<title>${escapeHtml(title)} - Sketchmark Embed</title>
|
|
46
|
+
<style>
|
|
47
|
+
html, body {
|
|
48
|
+
margin: 0;
|
|
49
|
+
width: 100%;
|
|
50
|
+
height: 100%;
|
|
51
|
+
background: ${chromeBackground};
|
|
52
|
+
color-scheme: light dark;
|
|
53
|
+
color: #0f172a;
|
|
54
|
+
font: 12px/1.4 Roboto, Arial, sans-serif;
|
|
55
|
+
}
|
|
56
|
+
body {
|
|
57
|
+
display: grid;
|
|
58
|
+
grid-template-rows: minmax(0, 1fr) auto;
|
|
59
|
+
overflow: hidden;
|
|
60
|
+
--embed-surface: rgba(255, 255, 255, 0.78);
|
|
61
|
+
--embed-surface-strong: rgba(255, 255, 255, 0.94);
|
|
62
|
+
--embed-border: rgba(15, 23, 42, 0.14);
|
|
63
|
+
--embed-border-strong: rgba(15, 23, 42, 0.18);
|
|
64
|
+
--embed-text: #0f172a;
|
|
65
|
+
--embed-muted: #475569;
|
|
66
|
+
--embed-button: rgba(255, 255, 255, 0.72);
|
|
67
|
+
--embed-button-hover: rgba(255, 255, 255, 0.92);
|
|
68
|
+
--embed-shadow: 0 18px 50px rgba(15, 23, 42, 0.14);
|
|
69
|
+
--embed-accent: #2563eb;
|
|
70
|
+
}
|
|
71
|
+
@media (prefers-color-scheme: dark) {
|
|
72
|
+
body {
|
|
73
|
+
--embed-surface: rgba(15, 23, 42, 0.74);
|
|
74
|
+
--embed-surface-strong: rgba(15, 23, 42, 0.92);
|
|
75
|
+
--embed-border: rgba(255, 255, 255, 0.12);
|
|
76
|
+
--embed-border-strong: rgba(255, 255, 255, 0.16);
|
|
77
|
+
--embed-text: #e5edf7;
|
|
78
|
+
--embed-muted: #b6c2d1;
|
|
79
|
+
--embed-button: rgba(255, 255, 255, 0.08);
|
|
80
|
+
--embed-button-hover: rgba(255, 255, 255, 0.14);
|
|
81
|
+
--embed-shadow: 0 18px 50px rgba(2, 6, 23, 0.32);
|
|
82
|
+
--embed-accent: #7dd3fc;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
#stage {
|
|
86
|
+
min-height: 0;
|
|
87
|
+
display: grid;
|
|
88
|
+
place-items: center;
|
|
89
|
+
padding: 14px;
|
|
90
|
+
box-sizing: border-box;
|
|
91
|
+
}
|
|
92
|
+
#stage svg {
|
|
93
|
+
display: block;
|
|
94
|
+
max-width: 100%;
|
|
95
|
+
max-height: 100%;
|
|
96
|
+
width: auto;
|
|
97
|
+
height: auto;
|
|
98
|
+
box-shadow: 0 18px 60px rgba(15, 23, 42, 0.28);
|
|
99
|
+
}
|
|
100
|
+
#controls {
|
|
101
|
+
display: flex;
|
|
102
|
+
flex-wrap: wrap;
|
|
103
|
+
align-items: center;
|
|
104
|
+
gap: 10px;
|
|
105
|
+
margin: 0 14px 14px;
|
|
106
|
+
padding: 10px 12px;
|
|
107
|
+
border: 1px solid var(--embed-border);
|
|
108
|
+
border-radius: 14px;
|
|
109
|
+
background: var(--embed-surface);
|
|
110
|
+
color: var(--embed-text);
|
|
111
|
+
backdrop-filter: blur(16px) saturate(140%);
|
|
112
|
+
-webkit-backdrop-filter: blur(16px) saturate(140%);
|
|
113
|
+
box-shadow: var(--embed-shadow);
|
|
114
|
+
box-sizing: border-box;
|
|
115
|
+
}
|
|
116
|
+
button,
|
|
117
|
+
summary,
|
|
118
|
+
input {
|
|
119
|
+
font: inherit;
|
|
120
|
+
}
|
|
121
|
+
button,
|
|
122
|
+
summary {
|
|
123
|
+
border: 1px solid var(--embed-border-strong);
|
|
124
|
+
border-radius: 8px;
|
|
125
|
+
background: var(--embed-button);
|
|
126
|
+
color: inherit;
|
|
127
|
+
cursor: pointer;
|
|
128
|
+
transition: background 120ms ease, border-color 120ms ease;
|
|
129
|
+
}
|
|
130
|
+
button {
|
|
131
|
+
padding: 8px 12px;
|
|
132
|
+
}
|
|
133
|
+
button:hover,
|
|
134
|
+
summary:hover {
|
|
135
|
+
background: var(--embed-button-hover);
|
|
136
|
+
}
|
|
137
|
+
button:disabled {
|
|
138
|
+
opacity: 0.45;
|
|
139
|
+
cursor: default;
|
|
140
|
+
}
|
|
141
|
+
#play {
|
|
142
|
+
min-width: 68px;
|
|
143
|
+
}
|
|
144
|
+
#time {
|
|
145
|
+
flex: 1 1 180px;
|
|
146
|
+
min-width: 140px;
|
|
147
|
+
accent-color: var(--embed-accent);
|
|
148
|
+
}
|
|
149
|
+
#clock {
|
|
150
|
+
min-width: 110px;
|
|
151
|
+
font-variant-numeric: tabular-nums;
|
|
152
|
+
color: var(--embed-muted);
|
|
153
|
+
text-align: right;
|
|
154
|
+
}
|
|
155
|
+
#meta {
|
|
156
|
+
min-width: 0;
|
|
157
|
+
flex: 1 1 160px;
|
|
158
|
+
color: var(--embed-muted);
|
|
159
|
+
white-space: nowrap;
|
|
160
|
+
overflow: hidden;
|
|
161
|
+
text-overflow: ellipsis;
|
|
162
|
+
text-align: right;
|
|
163
|
+
}
|
|
164
|
+
details {
|
|
165
|
+
position: relative;
|
|
166
|
+
}
|
|
167
|
+
summary {
|
|
168
|
+
list-style: none;
|
|
169
|
+
padding: 8px 12px;
|
|
170
|
+
user-select: none;
|
|
171
|
+
}
|
|
172
|
+
summary::-webkit-details-marker {
|
|
173
|
+
display: none;
|
|
174
|
+
}
|
|
175
|
+
.exportMenu {
|
|
176
|
+
position: absolute;
|
|
177
|
+
right: 0;
|
|
178
|
+
bottom: calc(100% + 8px);
|
|
179
|
+
display: grid;
|
|
180
|
+
gap: 6px;
|
|
181
|
+
min-width: 110px;
|
|
182
|
+
padding: 8px;
|
|
183
|
+
border: 1px solid var(--embed-border);
|
|
184
|
+
border-radius: 10px;
|
|
185
|
+
background: var(--embed-surface-strong);
|
|
186
|
+
box-shadow: var(--embed-shadow);
|
|
187
|
+
backdrop-filter: blur(18px) saturate(150%);
|
|
188
|
+
-webkit-backdrop-filter: blur(18px) saturate(150%);
|
|
189
|
+
}
|
|
190
|
+
.exportMenu button {
|
|
191
|
+
width: 100%;
|
|
192
|
+
text-align: left;
|
|
193
|
+
padding: 8px 10px;
|
|
194
|
+
}
|
|
195
|
+
@media (max-width: 720px) {
|
|
196
|
+
#controls {
|
|
197
|
+
gap: 8px;
|
|
198
|
+
}
|
|
199
|
+
#meta {
|
|
200
|
+
order: 10;
|
|
201
|
+
width: 100%;
|
|
202
|
+
text-align: left;
|
|
203
|
+
}
|
|
204
|
+
details {
|
|
205
|
+
margin-left: auto;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
</style>
|
|
209
|
+
</head>
|
|
210
|
+
<body>
|
|
211
|
+
<div id="stage">${initialFrame}</div>
|
|
212
|
+
<div id="controls">
|
|
213
|
+
<button id="play" type="button">Play</button>
|
|
214
|
+
<input id="time" type="range" min="0" max="${Math.max(duration, 0.001)}" step="0.01" value="${initialTime}" />
|
|
215
|
+
<div id="clock">0.00s / ${duration.toFixed(2)}s</div>
|
|
216
|
+
${options.includeExportControls !== false ? `<details id="exportRoot">
|
|
217
|
+
<summary>Export</summary>
|
|
218
|
+
<div class="exportMenu">
|
|
219
|
+
<button type="button" data-export-format="svg">SVG</button>
|
|
220
|
+
<button type="button" data-export-format="png">PNG</button>
|
|
221
|
+
<button type="button" data-export-format="jpg">JPG</button>
|
|
222
|
+
<button type="button" data-export-format="html">HTML</button>
|
|
223
|
+
<button type="button" data-export-format="json">JSON</button>
|
|
224
|
+
<button type="button" data-export-format="mp4">MP4</button>
|
|
225
|
+
</div>
|
|
226
|
+
</details>` : ""}
|
|
227
|
+
<div id="meta">${statusLabel}</div>
|
|
228
|
+
</div>
|
|
229
|
+
<script>
|
|
230
|
+
${mp4MuxerRuntimeSource}
|
|
231
|
+
</script>
|
|
232
|
+
<script>
|
|
233
|
+
const payload = ${serializeForScript(payload)};
|
|
234
|
+
const stage = document.getElementById("stage");
|
|
235
|
+
const playButton = document.getElementById("play");
|
|
236
|
+
const slider = document.getElementById("time");
|
|
237
|
+
const clock = document.getElementById("clock");
|
|
238
|
+
const meta = document.getElementById("meta");
|
|
239
|
+
const exportRoot = document.getElementById("exportRoot");
|
|
240
|
+
const defaultMeta = meta ? meta.textContent || "" : "";
|
|
241
|
+
const frameTimes = Array.isArray(payload.frameTimes) ? payload.frameTimes : [];
|
|
242
|
+
const state = {
|
|
243
|
+
time: clampTime(payload.initialTime),
|
|
244
|
+
playing: false,
|
|
245
|
+
raf: 0,
|
|
246
|
+
lastTick: 0
|
|
247
|
+
};
|
|
248
|
+
let metaTimer = 0;
|
|
249
|
+
let exportBusy = false;
|
|
250
|
+
|
|
251
|
+
function clampTime(value) {
|
|
252
|
+
if (!payload.duration) return 0;
|
|
253
|
+
const number = Number(value) || 0;
|
|
254
|
+
return Math.max(0, Math.min(payload.duration, number));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function frameIndexForTimeValue(time) {
|
|
258
|
+
if (!frameTimes.length) return 0;
|
|
259
|
+
const clamped = clampTime(time);
|
|
260
|
+
let bestIndex = 0;
|
|
261
|
+
let bestDelta = Number.POSITIVE_INFINITY;
|
|
262
|
+
for (let index = 0; index < frameTimes.length; index += 1) {
|
|
263
|
+
const delta = Math.abs(Number(frameTimes[index] || 0) - clamped);
|
|
264
|
+
if (delta <= bestDelta) {
|
|
265
|
+
bestIndex = index;
|
|
266
|
+
bestDelta = delta;
|
|
267
|
+
} else if (Number(frameTimes[index] || 0) > clamped) {
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return bestIndex;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function currentFrameSvg() {
|
|
275
|
+
const frames = Array.isArray(payload.frames) ? payload.frames : [];
|
|
276
|
+
return frames[frameIndexForTimeValue(state.time)] || frames[0] || "";
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function timeLabel(value) {
|
|
280
|
+
return clampTime(value).toFixed(2).replace(".", "-");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function flashMeta(message) {
|
|
284
|
+
if (!meta) return;
|
|
285
|
+
meta.textContent = message;
|
|
286
|
+
if (metaTimer) window.clearTimeout(metaTimer);
|
|
287
|
+
metaTimer = window.setTimeout(() => {
|
|
288
|
+
meta.textContent = defaultMeta;
|
|
289
|
+
}, 2200);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function notifyRendered() {
|
|
293
|
+
if (window.parent && window.parent !== window) {
|
|
294
|
+
window.parent.postMessage(
|
|
295
|
+
{
|
|
296
|
+
type: "sketchmark-rendered",
|
|
297
|
+
title: payload.title,
|
|
298
|
+
duration: payload.duration,
|
|
299
|
+
time: state.time
|
|
300
|
+
},
|
|
301
|
+
"*"
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function render() {
|
|
307
|
+
stage.innerHTML = currentFrameSvg();
|
|
308
|
+
slider.max = String(Math.max(payload.duration, 0.001));
|
|
309
|
+
slider.value = String(clampTime(state.time));
|
|
310
|
+
slider.disabled = exportBusy || payload.duration <= 0;
|
|
311
|
+
playButton.disabled = exportBusy || payload.duration <= 0;
|
|
312
|
+
playButton.textContent = state.playing ? "Pause" : "Play";
|
|
313
|
+
clock.textContent = payload.duration > 0
|
|
314
|
+
? state.time.toFixed(2) + "s / " + payload.duration.toFixed(2) + "s"
|
|
315
|
+
: "Static preview";
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function pause() {
|
|
319
|
+
state.playing = false;
|
|
320
|
+
if (state.raf) {
|
|
321
|
+
window.cancelAnimationFrame(state.raf);
|
|
322
|
+
state.raf = 0;
|
|
323
|
+
}
|
|
324
|
+
render();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function play() {
|
|
328
|
+
if (payload.duration <= 0 || state.playing) return;
|
|
329
|
+
state.playing = true;
|
|
330
|
+
state.lastTick = performance.now();
|
|
331
|
+
render();
|
|
332
|
+
state.raf = window.requestAnimationFrame(tick);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function seek(time, notify) {
|
|
336
|
+
state.time = clampTime(time);
|
|
337
|
+
render();
|
|
338
|
+
if (notify) notifyRendered();
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function tick(now) {
|
|
342
|
+
if (!state.playing) return;
|
|
343
|
+
const delta = (now - state.lastTick) / 1000;
|
|
344
|
+
state.lastTick = now;
|
|
345
|
+
let nextTime = state.time + delta;
|
|
346
|
+
|
|
347
|
+
if (payload.duration > 0 && nextTime > payload.duration) {
|
|
348
|
+
if (payload.loop) nextTime = nextTime % payload.duration;
|
|
349
|
+
else {
|
|
350
|
+
nextTime = payload.duration;
|
|
351
|
+
state.playing = false;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
state.time = clampTime(nextTime);
|
|
356
|
+
render();
|
|
357
|
+
if (state.playing) state.raf = window.requestAnimationFrame(tick);
|
|
358
|
+
else state.raf = 0;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function downloadBlob(blob, filename) {
|
|
362
|
+
const url = URL.createObjectURL(blob);
|
|
363
|
+
const anchor = document.createElement("a");
|
|
364
|
+
anchor.href = url;
|
|
365
|
+
anchor.download = filename;
|
|
366
|
+
document.body.appendChild(anchor);
|
|
367
|
+
anchor.click();
|
|
368
|
+
anchor.remove();
|
|
369
|
+
window.setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function downloadText(filename, text, mimeType) {
|
|
373
|
+
downloadBlob(new Blob([text], { type: mimeType }), filename);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function loadSvgImage(svg) {
|
|
377
|
+
return loadImage(svgDataUrl(svg));
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function loadImage(url) {
|
|
381
|
+
return new Promise((resolve, reject) => {
|
|
382
|
+
const image = new Image();
|
|
383
|
+
image.onload = () => resolve(image);
|
|
384
|
+
image.onerror = () => reject(new Error("Could not rasterize the current SVG frame."));
|
|
385
|
+
image.src = url;
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function svgDataUrl(svg) {
|
|
390
|
+
return "data:image/svg+xml;charset=utf-8," + encodeURIComponent(svg);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function canvasToBlob(canvas, type, quality) {
|
|
394
|
+
return new Promise((resolve, reject) => {
|
|
395
|
+
canvas.toBlob((blob) => {
|
|
396
|
+
if (blob) resolve(blob);
|
|
397
|
+
else reject(new Error("Could not export the current frame."));
|
|
398
|
+
}, type, quality);
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function yieldToBrowser() {
|
|
403
|
+
return new Promise((resolve) => window.setTimeout(resolve, 0));
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function evenDimension(value) {
|
|
407
|
+
const rounded = Math.max(2, Math.round(Number(value) || 0));
|
|
408
|
+
return rounded % 2 === 0 ? rounded : rounded + 1;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function sampleFrameDuration(index) {
|
|
412
|
+
if (frameTimes.length >= 2) {
|
|
413
|
+
if (index < frameTimes.length - 1) {
|
|
414
|
+
return Math.max(1 / 240, Number(frameTimes[index + 1]) - Number(frameTimes[index]));
|
|
415
|
+
}
|
|
416
|
+
return Math.max(1 / 240, Number(frameTimes[index]) - Number(frameTimes[index - 1]));
|
|
417
|
+
}
|
|
418
|
+
const fallbackFps = Math.max(1, Number(payload.document && payload.document.canvas && payload.document.canvas.fps || 24));
|
|
419
|
+
return 1 / fallbackFps;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function sampleFps() {
|
|
423
|
+
if (payload.duration > 0 && frameTimes.length >= 2) {
|
|
424
|
+
return Math.max(1, Math.round((frameTimes.length - 1) / payload.duration));
|
|
425
|
+
}
|
|
426
|
+
return Math.max(1, Number(payload.document && payload.document.canvas && payload.document.canvas.fps || 24));
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
async function drawSvgToCanvas(svg, canvas, width, height) {
|
|
430
|
+
const context = canvas.getContext("2d");
|
|
431
|
+
if (!context) throw new Error("Could not create a 2D canvas context.");
|
|
432
|
+
const image = await loadSvgImage(svg);
|
|
433
|
+
context.clearRect(0, 0, width, height);
|
|
434
|
+
context.drawImage(image, 0, 0, width, height);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
async function loadMp4Muxer() {
|
|
438
|
+
const module = globalThis.__SKETCHMARK_MP4_MUXER__;
|
|
439
|
+
if (module && module.Muxer && module.ArrayBufferTarget) return module;
|
|
440
|
+
throw new Error("MP4 runtime could not be initialized in this host.");
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
async function rasterBlob(format) {
|
|
444
|
+
const canvas = document.createElement("canvas");
|
|
445
|
+
canvas.width = payload.canvas.width;
|
|
446
|
+
canvas.height = payload.canvas.height;
|
|
447
|
+
await drawSvgToCanvas(currentFrameSvg(), canvas, canvas.width, canvas.height);
|
|
448
|
+
return canvasToBlob(canvas, format === "jpg" ? "image/jpeg" : "image/png", format === "jpg" ? 0.92 : undefined);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
async function exportMp4() {
|
|
452
|
+
const globalApi = globalThis;
|
|
453
|
+
const VideoEncoderCtor = globalApi.VideoEncoder;
|
|
454
|
+
const VideoFrameCtor = globalApi.VideoFrame;
|
|
455
|
+
if (!VideoEncoderCtor || !VideoFrameCtor) {
|
|
456
|
+
throw new Error("MP4 export requires WebCodecs. Try Chrome or Edge.");
|
|
457
|
+
}
|
|
458
|
+
if (!(payload.duration > 0)) {
|
|
459
|
+
throw new Error("MP4 export requires a positive duration.");
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const frames = Array.isArray(payload.frames) ? payload.frames : [];
|
|
463
|
+
if (!frames.length) {
|
|
464
|
+
throw new Error("No frames are available for MP4 export.");
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const { Muxer, ArrayBufferTarget } = await loadMp4Muxer();
|
|
468
|
+
const encodeWidth = evenDimension(payload.canvas.width);
|
|
469
|
+
const encodeHeight = evenDimension(payload.canvas.height);
|
|
470
|
+
const fps = sampleFps();
|
|
471
|
+
const target = new ArrayBufferTarget();
|
|
472
|
+
const muxer = new Muxer({
|
|
473
|
+
target,
|
|
474
|
+
video: { codec: "avc", width: encodeWidth, height: encodeHeight },
|
|
475
|
+
fastStart: "in-memory"
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
let encoderError = null;
|
|
479
|
+
const encoder = new VideoEncoderCtor({
|
|
480
|
+
output: (chunk, metadata) => muxer.addVideoChunk(chunk, metadata),
|
|
481
|
+
error: (error) => {
|
|
482
|
+
encoderError = error;
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
encoder.configure({
|
|
487
|
+
codec: "avc1.640028",
|
|
488
|
+
width: encodeWidth,
|
|
489
|
+
height: encodeHeight,
|
|
490
|
+
bitrate: 5_000_000,
|
|
491
|
+
framerate: fps
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
const canvas = document.createElement("canvas");
|
|
495
|
+
canvas.width = encodeWidth;
|
|
496
|
+
canvas.height = encodeHeight;
|
|
497
|
+
|
|
498
|
+
try {
|
|
499
|
+
for (let index = 0; index < frames.length; index += 1) {
|
|
500
|
+
await drawSvgToCanvas(frames[index], canvas, encodeWidth, encodeHeight);
|
|
501
|
+
const frameTime = frameTimes.length ? Number(frameTimes[index] || 0) : Math.min(payload.duration, index / fps);
|
|
502
|
+
const frameDuration = sampleFrameDuration(index);
|
|
503
|
+
const frame = new VideoFrameCtor(canvas, {
|
|
504
|
+
timestamp: Math.max(0, Math.round(frameTime * 1_000_000)),
|
|
505
|
+
duration: Math.max(1, Math.round(frameDuration * 1_000_000))
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
encoder.encode(frame, { keyFrame: index % Math.max(1, fps * 2) === 0 });
|
|
509
|
+
frame.close();
|
|
510
|
+
|
|
511
|
+
if (encoderError) throw encoderError;
|
|
512
|
+
if (index % 5 === 0 || index === frames.length - 1) {
|
|
513
|
+
if (meta) meta.textContent = "Encoding MP4 " + Math.round(((index + 1) / frames.length) * 100) + "%";
|
|
514
|
+
await yieldToBrowser();
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
await encoder.flush();
|
|
519
|
+
if (encoderError) throw encoderError;
|
|
520
|
+
encoder.close();
|
|
521
|
+
muxer.finalize();
|
|
522
|
+
downloadBlob(new Blob([target.buffer], { type: "video/mp4" }), payload.fileBase + ".mp4");
|
|
523
|
+
} catch (error) {
|
|
524
|
+
try {
|
|
525
|
+
encoder.close();
|
|
526
|
+
} catch {}
|
|
527
|
+
throw error;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
async function exportCurrent(format) {
|
|
532
|
+
if (exportBusy) return;
|
|
533
|
+
const resumePlayback = state.playing;
|
|
534
|
+
if (resumePlayback) pause();
|
|
535
|
+
exportBusy = true;
|
|
536
|
+
render();
|
|
537
|
+
try {
|
|
538
|
+
if (format === "svg") {
|
|
539
|
+
downloadText(payload.fileBase + "-t" + timeLabel(state.time) + ".svg", currentFrameSvg(), "image/svg+xml;charset=utf-8");
|
|
540
|
+
flashMeta("Saved SVG");
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
if (format === "json") {
|
|
544
|
+
downloadText(payload.fileBase + ".visual.json", JSON.stringify(payload.document, null, 2) + "\\n", "application/json;charset=utf-8");
|
|
545
|
+
flashMeta("Saved JSON");
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
if (format === "html") {
|
|
549
|
+
downloadText(payload.fileBase + ".embed.html", "<!doctype html>\\n" + document.documentElement.outerHTML, "text/html;charset=utf-8");
|
|
550
|
+
flashMeta("Saved HTML");
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
if (format === "png" || format === "jpg") {
|
|
554
|
+
const blob = await rasterBlob(format);
|
|
555
|
+
downloadBlob(blob, payload.fileBase + "-t" + timeLabel(state.time) + "." + format);
|
|
556
|
+
flashMeta("Saved " + format.toUpperCase());
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
if (format === "mp4") {
|
|
560
|
+
await exportMp4();
|
|
561
|
+
flashMeta("Saved MP4");
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
throw new Error("Unsupported export format: " + format);
|
|
565
|
+
} catch (error) {
|
|
566
|
+
flashMeta(error && error.message ? error.message : "Export failed.");
|
|
567
|
+
throw error;
|
|
568
|
+
} finally {
|
|
569
|
+
exportBusy = false;
|
|
570
|
+
render();
|
|
571
|
+
if (resumePlayback && !state.playing) play();
|
|
572
|
+
if (exportRoot) exportRoot.open = false;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
playButton.addEventListener("click", () => {
|
|
577
|
+
if (state.playing) pause();
|
|
578
|
+
else play();
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
slider.addEventListener("input", () => {
|
|
582
|
+
seek(Number(slider.value || 0), false);
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
document.addEventListener("click", (event) => {
|
|
586
|
+
const target = event.target;
|
|
587
|
+
const button = target && typeof target.closest === "function"
|
|
588
|
+
? target.closest("[data-export-format]")
|
|
589
|
+
: null;
|
|
590
|
+
const format = button && button.getAttribute ? button.getAttribute("data-export-format") : "";
|
|
591
|
+
if (format) {
|
|
592
|
+
exportCurrent(format).catch(() => {});
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
window.addEventListener("message", (event) => {
|
|
597
|
+
const message = event.data || {};
|
|
598
|
+
if (message.type === "sketchmark-show" && typeof message.time === "number") {
|
|
599
|
+
seek(message.time, true);
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
if (message.type === "sketchmark-play") {
|
|
603
|
+
play();
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
if (message.type === "sketchmark-pause") {
|
|
607
|
+
pause();
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
if (message.type === "sketchmark-export" && typeof message.format === "string") {
|
|
611
|
+
exportCurrent(message.format).catch(() => {});
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
window.__SKETCHMARK_EMBED__ = {
|
|
616
|
+
play,
|
|
617
|
+
pause,
|
|
618
|
+
seek,
|
|
619
|
+
export: exportCurrent,
|
|
620
|
+
getState: () => ({
|
|
621
|
+
time: state.time,
|
|
622
|
+
duration: payload.duration,
|
|
623
|
+
playing: state.playing,
|
|
624
|
+
frameCount: Array.isArray(payload.frames) ? payload.frames.length : 0
|
|
625
|
+
})
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
render();
|
|
629
|
+
notifyRendered();
|
|
630
|
+
if (payload.autoplay && payload.duration > 0) play();
|
|
631
|
+
</script>
|
|
632
|
+
</body>
|
|
633
|
+
</html>`;
|
|
634
|
+
}
|
|
635
|
+
function normalizePositiveInteger(value, fallback) {
|
|
636
|
+
const parsed = Math.round(Number(value));
|
|
637
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
638
|
+
}
|
|
639
|
+
function sampledFrameCount(duration, fps, maxFrames) {
|
|
640
|
+
if (!(duration > 0))
|
|
641
|
+
return 1;
|
|
642
|
+
return Math.max(2, Math.min(maxFrames, Math.ceil(duration * fps) + 1));
|
|
643
|
+
}
|
|
644
|
+
function frameTimeAt(index, frameCount, duration) {
|
|
645
|
+
if (frameCount <= 1 || duration <= 0)
|
|
646
|
+
return 0;
|
|
647
|
+
return (index / (frameCount - 1)) * duration;
|
|
648
|
+
}
|
|
649
|
+
function frameIndexForTime(time, frameTimes) {
|
|
650
|
+
if (!frameTimes.length)
|
|
651
|
+
return 0;
|
|
652
|
+
const clamped = (0, utils_1.clamp)(time, 0, Math.max(0, Number(frameTimes[frameTimes.length - 1] ?? 0)));
|
|
653
|
+
let bestIndex = 0;
|
|
654
|
+
let bestDelta = Number.POSITIVE_INFINITY;
|
|
655
|
+
for (let index = 0; index < frameTimes.length; index += 1) {
|
|
656
|
+
const delta = Math.abs(Number(frameTimes[index] ?? 0) - clamped);
|
|
657
|
+
if (delta <= bestDelta) {
|
|
658
|
+
bestIndex = index;
|
|
659
|
+
bestDelta = delta;
|
|
660
|
+
}
|
|
661
|
+
else if (Number(frameTimes[index] ?? 0) > clamped) {
|
|
662
|
+
break;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
return bestIndex;
|
|
666
|
+
}
|
|
667
|
+
function safeFileName(value) {
|
|
668
|
+
return value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 80) || "sketchmark";
|
|
669
|
+
}
|
|
670
|
+
function escapeHtml(value) {
|
|
671
|
+
return value
|
|
672
|
+
.replace(/&/g, "&")
|
|
673
|
+
.replace(/</g, "<")
|
|
674
|
+
.replace(/>/g, ">")
|
|
675
|
+
.replace(/"/g, """);
|
|
676
|
+
}
|
|
677
|
+
function serializeForScript(value) {
|
|
678
|
+
return JSON.stringify(value)
|
|
679
|
+
.replace(/</g, "\\u003c")
|
|
680
|
+
.replace(/>/g, "\\u003e")
|
|
681
|
+
.replace(/&/g, "\\u0026")
|
|
682
|
+
.replace(/\u2028/g, "\\u2028")
|
|
683
|
+
.replace(/\u2029/g, "\\u2029");
|
|
684
|
+
}
|
|
685
|
+
function inlineMp4MuxerRuntime(source) {
|
|
686
|
+
const withoutExportBlock = source.replace(/\s*export\s*\{[\s\S]*?\}\s*;?\s*$/, "");
|
|
687
|
+
const runtime = `${withoutExportBlock}
|
|
688
|
+
globalThis.__SKETCHMARK_MP4_MUXER__ = {
|
|
689
|
+
ArrayBufferTarget,
|
|
690
|
+
FileSystemWritableFileStreamTarget,
|
|
691
|
+
Muxer,
|
|
692
|
+
StreamTarget
|
|
693
|
+
};`;
|
|
694
|
+
return escapeInlineScript(runtime);
|
|
695
|
+
}
|
|
696
|
+
function escapeInlineScript(value) {
|
|
697
|
+
return value.replace(/<\/script/gi, "<\\/script").replace(/<!--/g, "<\\!--");
|
|
698
|
+
}
|
package/dist/tests/run.js
CHANGED
|
@@ -110,6 +110,38 @@ test("renders path, text, image, and group to SVG and HTML", () => {
|
|
|
110
110
|
assert(svg.includes('stroke-width="1"'), "path stroke should default to width 1 when stroke is set");
|
|
111
111
|
assert((0, src_1.renderToHtml)(doc).includes("Sketchmark Kernel Visual"), "should render HTML shell");
|
|
112
112
|
});
|
|
113
|
+
test("renders a self-contained interactive embed HTML shell", () => {
|
|
114
|
+
const doc = {
|
|
115
|
+
version: 1,
|
|
116
|
+
canvas: { width: 240, height: 140, background: "#f8fafc", duration: 1, fps: 12 },
|
|
117
|
+
elements: [
|
|
118
|
+
{
|
|
119
|
+
id: "label",
|
|
120
|
+
type: "text",
|
|
121
|
+
text: "Embed preview",
|
|
122
|
+
x: 40,
|
|
123
|
+
y: 70,
|
|
124
|
+
fill: "#0f172a",
|
|
125
|
+
timeline: {
|
|
126
|
+
tracks: {
|
|
127
|
+
position: { keyframes: [[0, [40, 70]], [1, [180, 70]]], ease: "linear" }
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
]
|
|
132
|
+
};
|
|
133
|
+
const html = (0, src_1.renderToEmbedHtml)(doc, { title: "Embed Demo", maxFrames: 16 });
|
|
134
|
+
assert(html.includes("Sketchmark Embed"), "should include embed chrome");
|
|
135
|
+
assert(html.includes("background: transparent;"), "should default embed chrome to a transparent background");
|
|
136
|
+
assert(html.includes("prefers-color-scheme: dark"), "should adapt embed chrome for dark mode");
|
|
137
|
+
assert(html.includes('data-export-format="svg"'), "should include export controls");
|
|
138
|
+
assert(html.includes('data-export-format="mp4"'), "should include mp4 export");
|
|
139
|
+
assert(html.includes("__SKETCHMARK_EMBED__"), "should expose a runtime controller");
|
|
140
|
+
assert(html.includes("__SKETCHMARK_MP4_MUXER__"), "should inline the mp4 muxer runtime");
|
|
141
|
+
assert(!html.includes("import(url)"), "should avoid blob-based dynamic module imports");
|
|
142
|
+
assert(html.includes("data:image/svg+xml;charset=utf-8,"), "should use data URLs for SVG rasterization inside embed hosts");
|
|
143
|
+
assert(html.includes("sketchmark-rendered"), "should notify host frames when ready");
|
|
144
|
+
});
|
|
113
145
|
test("resolves element-local timeline tracks", () => {
|
|
114
146
|
const doc = {
|
|
115
147
|
version: 1,
|