fablecut 1.15.0 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/media-lib.js CHANGED
@@ -140,7 +140,7 @@
140
140
  * inside every composition. Sorted main timeline first, then by time. */
141
141
  function placements(doc, mediaId) {
142
142
  const out = [];
143
- const scan = (clips, comp) => {
143
+ const scan = (clips, comp, seq) => {
144
144
  const groups = new Map();
145
145
  for (const c of clips || []) {
146
146
  if (c.mediaId !== mediaId) continue;
@@ -150,11 +150,21 @@
150
150
  if (!prev || (prev.kind !== "video" && c.kind === "video")) groups.set(key, c);
151
151
  }
152
152
  for (const c of groups.values())
153
- out.push({ clipId: c.id, comp: comp ? comp.id : null, compName: comp ? comp.name : null, track: c.track, start: +c.start || 0, duration: +c.duration || 0, kind: c.kind });
153
+ out.push({ clipId: c.id, comp: comp ? comp.id : null, compName: comp ? comp.name : null, track: c.track, start: +c.start || 0, duration: +c.duration || 0, kind: c.kind,
154
+ ...(seq ? { sequence: seq.id, sequenceName: seq.name || seq.id } : {}) });
154
155
  };
155
156
  scan(doc && doc.clips, null);
156
157
  for (const comp of (doc && doc.comps) || []) scan(comp.clips, comp);
157
- return out.sort((a, b) => (a.comp ? 1 : 0) - (b.comp ? 1 : 0) || String(a.comp || "").localeCompare(String(b.comp || "")) || a.start - b.start);
158
+ for (const seq of otherSequences(doc)) scan(seq.clips, null, seq);
159
+ const rank = (p) => (p.sequence ? 2 : p.comp ? 1 : 0);
160
+ return out.sort((a, b) => rank(a) - rank(b) || String(a.sequence || "").localeCompare(String(b.sequence || "")) ||
161
+ String(a.comp || "").localeCompare(String(b.comp || "")) || a.start - b.start);
162
+ }
163
+ /** The sequences that aren't open (see sequences.js): their clips use media too. */
164
+ function otherSequences(doc) {
165
+ const list = doc && Array.isArray(doc.sequences) ? doc.sequences : [];
166
+ const act = list.some((x) => x.id === doc.sequence) ? doc.sequence : list[0] && list[0].id;
167
+ return list.filter((x) => x && x.id !== act && Array.isArray(x.clips));
158
168
  }
159
169
  /** mediaId → number of placements (see placements()). */
160
170
  function usageCounts(doc) {
@@ -173,6 +183,7 @@
173
183
  };
174
184
  scan(doc && doc.clips);
175
185
  for (const comp of (doc && doc.comps) || []) scan(comp.clips);
186
+ for (const seq of otherSequences(doc)) scan(seq.clips);
176
187
  return n;
177
188
  }
178
189
 
package/mp4-demux.js ADDED
@@ -0,0 +1,256 @@
1
+ /* FableCut MP4 demuxer — the sample table of an MP4 / MOV video track, so the
2
+ exporter can feed WebCodecs' VideoDecoder frame by frame instead of seeking a
3
+ <video> element and waiting for it to present each frame. Zero dependencies.
4
+ Loaded by the editor as a plain script (global `FableCutMp4`) and required by
5
+ the tests.
6
+
7
+ Only what decoding needs is read: the first video track's codec string and
8
+ decoder config (avcC / hvcC / vpcC / av1C), and per sample its byte range,
9
+ whether it is a sync sample, and its presentation time in seconds after the
10
+ track's edit list, which is the clock a <video> element shows. Fragmented
11
+ MP4 (moof) is not supported: findMoov / parseMovie return null and the
12
+ caller keeps the <video> path. */
13
+ (function (root, factory) {
14
+ const api = factory();
15
+ if (typeof module === "object" && module.exports) module.exports = api;
16
+ else root.FableCutMp4 = api;
17
+ })(typeof self !== "undefined" ? self : this, function () {
18
+ "use strict";
19
+
20
+ const CONTAINERS = new Set(["moov", "trak", "mdia", "minf", "stbl", "edts", "dinf"]);
21
+ const VIDEO_ENTRIES = new Set(["avc1", "avc3", "hvc1", "hev1", "vp09", "av01"]);
22
+
23
+ const u32 = (b, o) => ((b[o] << 24) | (b[o + 1] << 16) | (b[o + 2] << 8) | b[o + 3]) >>> 0;
24
+ const i32 = (b, o) => (b[o] << 24) | (b[o + 1] << 16) | (b[o + 2] << 8) | b[o + 3];
25
+ const u64 = (b, o) => u32(b, o) * 4294967296 + u32(b, o + 4);
26
+ const i64 = (b, o) => i32(b, o) * 4294967296 + u32(b, o + 4);
27
+ const fourcc = (b, o) => String.fromCharCode(b[o], b[o + 1], b[o + 2], b[o + 3]);
28
+ const hex2 = (n) => n.toString(16).padStart(2, "0").toUpperCase();
29
+
30
+ /** The boxes directly inside b[start, end): [{ type, start, body, end }]. */
31
+ function boxes(b, start = 0, end = b.length) {
32
+ const out = [];
33
+ let o = start;
34
+ while (o + 8 <= end) {
35
+ let size = u32(b, o), body = o + 8;
36
+ const type = fourcc(b, o + 4);
37
+ if (size === 1) { if (o + 16 > end) break; size = u64(b, o + 8); body = o + 16; }
38
+ else if (size === 0) size = end - o;
39
+ if (size < body - o || o + size > end) break;
40
+ out.push({ type, start: o, body, end: o + size });
41
+ o += size;
42
+ }
43
+ return out;
44
+ }
45
+ const child = (b, box, type) => boxes(b, box.body, box.end).find((x) => x.type === type) || null;
46
+ function path(b, box, ...types) {
47
+ let cur = box;
48
+ for (const t of types) { cur = cur && child(b, cur, t); if (!cur) return null; }
49
+ return cur;
50
+ }
51
+
52
+ /** Walk the top-level boxes through `read(start, end) → Promise<Uint8Array>`
53
+ * (an HTTP Range fetch) and return the moov box's bytes, or null (no moov,
54
+ * or a fragmented file). Faststart files answer from the first read. */
55
+ async function findMoov(read, fileSize) {
56
+ let o = 0, head = null, headAt = -1;
57
+ while (o + 8 <= fileSize) {
58
+ if (!head || o < headAt || o + 16 > headAt + head.length) {
59
+ headAt = o; head = await read(o, Math.min(fileSize, o + 65536));
60
+ }
61
+ const r = o - headAt;
62
+ let size = u32(head, r), hdr = 8;
63
+ const type = fourcc(head, r + 4);
64
+ if (size === 1) { size = u64(head, r + 8); hdr = 16; }
65
+ else if (size === 0) size = fileSize - o;
66
+ if (size < hdr) return null;
67
+ if (type === "moof") return null;
68
+ if (type === "moov") {
69
+ const have = head.length - r;
70
+ return have >= size ? head.subarray(r, r + size) : await read(o, o + size);
71
+ }
72
+ o += size;
73
+ }
74
+ return null;
75
+ }
76
+
77
+ /** Codec string for VideoDecoder.configure from the sample entry's config box. */
78
+ function codecString(entry, b, cfg) {
79
+ const d = b.subarray(cfg.body, cfg.end);
80
+ if (entry === "avc1" || entry === "avc3")
81
+ return `${entry}.${hex2(d[1])}${hex2(d[2])}${hex2(d[3])}`;
82
+ if (entry === "hvc1" || entry === "hev1") { // ISO/IEC 14496-15 Annex E
83
+ const space = ["", "A", "B", "C"][d[1] >> 6], tier = (d[1] >> 5) & 1 ? "H" : "L", idc = d[1] & 31;
84
+ let compat = u32(d, 2), rev = 0;
85
+ for (let i = 0; i < 32; i++) { rev = (rev << 1) | (compat & 1); compat >>>= 1; }
86
+ const cons = [];
87
+ for (let i = 6; i < 12; i++) cons.push(d[i]);
88
+ while (cons.length && cons[cons.length - 1] === 0) cons.pop();
89
+ return [entry, space + idc, (rev >>> 0).toString(16).toUpperCase(), tier + d[12], ...cons.map((x) => x.toString(16).toUpperCase())].join(".");
90
+ }
91
+ if (entry === "vp09") { // vpcC is a full box: version + flags first
92
+ const p = d.subarray(4);
93
+ const two = (n) => String(n).padStart(2, "0");
94
+ return `vp09.${two(p[0])}.${two(p[1])}.${two(p[2] >> 4)}`;
95
+ }
96
+ if (entry === "av01") {
97
+ const profile = d[1] >> 5, level = d[1] & 31, tier = d[2] >> 7 ? "H" : "M";
98
+ const bits = (d[2] >> 6) & 1 ? ((d[2] >> 5) & 1 ? 12 : 10) : 8;
99
+ return `av01.${profile}.${String(level).padStart(2, "0")}${tier}.${String(bits).padStart(2, "0")}`;
100
+ }
101
+ return null;
102
+ }
103
+
104
+ /** The first video track of a moov box: { codec, description, width, height,
105
+ * displayWidth, displayHeight (pasp applied), timescale, samples: [{ offset, size, key, pts, dur }] in decode order }.
106
+ * pts and dur are seconds on the presentation clock (edit list applied). */
107
+ function parseMovie(moov) {
108
+ const b = moov, root = { body: 0, end: b.length, type: "root" };
109
+ const moovBox = child(b, root, "moov") || (fourcc(b, 4) === "moov" ? boxes(b)[0] : null);
110
+ if (!moovBox) return null;
111
+ if (child(b, moovBox, "mvex")) return null; // fragmented: samples live in moof boxes
112
+ const mvhd = child(b, moovBox, "mvhd");
113
+ const movieScale = mvhd ? (b[mvhd.body] === 1 ? u32(b, mvhd.body + 20) : u32(b, mvhd.body + 12)) : 1000;
114
+ for (const trak of boxes(b, moovBox.body, moovBox.end).filter((x) => x.type === "trak")) {
115
+ const hdlr = path(b, trak, "mdia", "hdlr");
116
+ if (!hdlr || fourcc(b, hdlr.body + 8) !== "vide") continue;
117
+ const mdhd = path(b, trak, "mdia", "mdhd"), stbl = path(b, trak, "mdia", "minf", "stbl");
118
+ if (!mdhd || !stbl) continue;
119
+ // A rotated or mirrored track (phone footage) is turned by the player, not
120
+ // by the decoder: leave those to the <video> element.
121
+ const tkhd = child(b, trak, "tkhd");
122
+ if (tkhd) {
123
+ const m = tkhd.body + (b[tkhd.body] === 1 ? 52 : 40);
124
+ if (u32(b, m) !== 0x10000 || u32(b, m + 4) !== 0 || u32(b, m + 12) !== 0 || u32(b, m + 16) !== 0x10000) return null;
125
+ }
126
+ const v1 = b[mdhd.body] === 1;
127
+ const timescale = v1 ? u32(b, mdhd.body + 20) : u32(b, mdhd.body + 12);
128
+ const stsd = child(b, stbl, "stsd");
129
+ if (!stsd || !timescale) continue;
130
+ const entryBox = boxes(b, stsd.body + 8, stsd.end)[0];
131
+ if (!entryBox || !VIDEO_ENTRIES.has(entryBox.type)) continue;
132
+ const entry = entryBox.type;
133
+ const width = (b[entryBox.body + 24] << 8) | b[entryBox.body + 25];
134
+ const height = (b[entryBox.body + 26] << 8) | b[entryBox.body + 27];
135
+ const cfgType = { avc1: "avcC", avc3: "avcC", hvc1: "hvcC", hev1: "hvcC", vp09: "vpcC", av01: "av1C" }[entry];
136
+ const cfg = boxes(b, entryBox.body + 78, entryBox.end).find((x) => x.type === cfgType);
137
+ if (!cfg) continue;
138
+ const codec = codecString(entry, b, cfg);
139
+ if (!codec) continue;
140
+ // vpcC carries no decoder description; the others hand the box body over as is
141
+ const description = entry === "vp09" ? undefined : b.slice(cfg.body, cfg.end);
142
+ const samples = sampleTable(b, stbl, timescale);
143
+ if (!samples) continue;
144
+ applyEdits(b, trak, samples, timescale, movieScale);
145
+ // pasp: non-square pixels (anamorphic). Display width = coded width × h / v.
146
+ const pasp = boxes(b, entryBox.body + 78, entryBox.end).find((x) => x.type === "pasp");
147
+ const hs = pasp ? u32(b, pasp.body) : 0, vs = pasp ? u32(b, pasp.body + 4) : 0;
148
+ const displayWidth = hs && vs && hs !== vs ? Math.round(width * hs / vs) : width;
149
+ return { codec, description, width, height, displayWidth, displayHeight: height, timescale, samples };
150
+ }
151
+ return null;
152
+ }
153
+
154
+ function sampleTable(b, stbl, timescale) {
155
+ const box = (t) => child(b, stbl, t);
156
+ const stsz = box("stsz"), stz2 = box("stz2"), stsc = box("stsc"), stts = box("stts");
157
+ const stco = box("stco"), co64 = box("co64"), stss = box("stss"), ctts = box("ctts");
158
+ if (!(stsz || stz2) || !stsc || !stts || !(stco || co64)) return null;
159
+ // sizes
160
+ const sizes = [];
161
+ if (stsz) {
162
+ const fixed = u32(b, stsz.body + 4), n = u32(b, stsz.body + 8);
163
+ for (let i = 0; i < n; i++) sizes.push(fixed || u32(b, stsz.body + 12 + i * 4));
164
+ } else {
165
+ const field = b[stz2.body + 7], n = u32(b, stz2.body + 8), o = stz2.body + 12;
166
+ for (let i = 0; i < n; i++) {
167
+ if (field === 4) sizes.push(i & 1 ? b[o + (i >> 1)] & 15 : b[o + (i >> 1)] >> 4);
168
+ else if (field === 8) sizes.push(b[o + i]);
169
+ else sizes.push((b[o + i * 2] << 8) | b[o + i * 2 + 1]);
170
+ }
171
+ }
172
+ const n = sizes.length;
173
+ // chunk offsets → sample offsets via sample-to-chunk
174
+ const chunks = [];
175
+ if (stco) { const c = u32(b, stco.body + 4); for (let i = 0; i < c; i++) chunks.push(u32(b, stco.body + 8 + i * 4)); }
176
+ else { const c = u32(b, co64.body + 4); for (let i = 0; i < c; i++) chunks.push(u64(b, co64.body + 8 + i * 8)); }
177
+ const runs = [];
178
+ for (let i = 0, c = u32(b, stsc.body + 4); i < c; i++)
179
+ runs.push({ first: u32(b, stsc.body + 8 + i * 12) - 1, per: u32(b, stsc.body + 12 + i * 12) });
180
+ const offsets = new Array(n);
181
+ let s = 0;
182
+ for (let r = 0; r < runs.length && s < n; r++) {
183
+ const last = r + 1 < runs.length ? runs[r + 1].first : chunks.length;
184
+ for (let ch = runs[r].first; ch < last && s < n; ch++) {
185
+ let o = chunks[ch];
186
+ for (let k = 0; k < runs[r].per && s < n; k++) { offsets[s] = o; o += sizes[s]; s++; }
187
+ }
188
+ }
189
+ if (s < n) return null;
190
+ // decode times, composition offsets, sync samples
191
+ const dts = new Array(n), durs = new Array(n);
192
+ let t = 0, i = 0;
193
+ for (let e = 0, c = u32(b, stts.body + 4); e < c; e++) {
194
+ const count = u32(b, stts.body + 8 + e * 8), delta = u32(b, stts.body + 12 + e * 8);
195
+ for (let k = 0; k < count && i < n; k++, i++) { dts[i] = t; durs[i] = delta; t += delta; }
196
+ }
197
+ for (; i < n; i++) { dts[i] = t; durs[i] = 0; }
198
+ const cto = new Array(n).fill(0);
199
+ if (ctts) {
200
+ let j = 0;
201
+ for (let e = 0, c = u32(b, ctts.body + 4); e < c; e++) {
202
+ const count = u32(b, ctts.body + 8 + e * 8);
203
+ const off = i32(b, ctts.body + 12 + e * 8);
204
+ for (let k = 0; k < count && j < n; k++, j++) cto[j] = off;
205
+ }
206
+ }
207
+ const key = new Array(n).fill(!stss);
208
+ if (stss) for (let e = 0, c = u32(b, stss.body + 4); e < c; e++) {
209
+ const k = u32(b, stss.body + 8 + e * 4) - 1;
210
+ if (k >= 0 && k < n) key[k] = true;
211
+ }
212
+ const out = new Array(n);
213
+ for (let k = 0; k < n; k++)
214
+ out[k] = { offset: offsets[k], size: sizes[k], key: key[k], cts: dts[k] + cto[k], pts: (dts[k] + cto[k]) / timescale, dur: durs[k] / timescale };
215
+ return out;
216
+ }
217
+
218
+ /** Move pts onto the presentation clock, as a <video> element does: the first
219
+ * non-empty edit's media_time is presentation 0, after any leading empty
220
+ * edits (dwell, in movie-timescale units). */
221
+ function applyEdits(b, trak, samples, timescale, movieScale) {
222
+ const elst = path(b, trak, "edts", "elst");
223
+ if (!elst) return;
224
+ const v1 = b[elst.body] === 1, count = u32(b, elst.body + 4), size = v1 ? 20 : 12;
225
+ let empty = 0, mediaTime = null;
226
+ for (let e = 0; e < count; e++) {
227
+ const o = elst.body + 8 + e * size;
228
+ const dur = v1 ? u64(b, o) : u32(b, o), mt = v1 ? i64(b, o + 8) : i32(b, o + 4);
229
+ if (mt === -1) { empty += dur; continue; }
230
+ mediaTime = mt; break;
231
+ }
232
+ const shift = (empty / movieScale) - (mediaTime || 0) / timescale;
233
+ if (!shift) return;
234
+ for (const s of samples) s.pts = s.cts / timescale + shift;
235
+ }
236
+
237
+ /** Index (decode order) of the frame a player shows at presentation time t:
238
+ * the latest pts ≤ t (+ a microsecond for float noise); the first frame
239
+ * before the track starts. `order` is the decode indices sorted by pts. */
240
+ function frameAt(samples, order, t) {
241
+ let lo = 0, hi = order.length - 1, best = 0;
242
+ while (lo <= hi) {
243
+ const mid = (lo + hi) >> 1;
244
+ if (samples[order[mid]].pts <= t + 1e-6) { best = mid; lo = mid + 1; } else hi = mid - 1;
245
+ }
246
+ return order[best];
247
+ }
248
+ const presentationOrder = (samples) => samples.map((_, i) => i).sort((a, b) => samples[a].pts - samples[b].pts);
249
+ /** The sync sample at or before decode index k (where decoding must start). */
250
+ function syncBefore(samples, k) {
251
+ for (let i = k; i >= 0; i--) if (samples[i].key) return i;
252
+ return 0;
253
+ }
254
+
255
+ return { boxes, findMoov, parseMovie, codecString, frameAt, presentationOrder, syncBefore };
256
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fablecut",
3
- "version": "1.15.0",
3
+ "version": "1.17.0",
4
4
  "description": "Zero-dependency browser video editor that AI agents can drive — JSON timeline, MCP server, REST API, live-reloading UI.",
5
5
  "main": "server.js",
6
6
  "bin": {
package/project-ops.js CHANGED
@@ -29,10 +29,10 @@
29
29
  const api = factory(dep("./audio-fx", "FableCutFx"), dep("./edit-ops", "FableCutEdit"), dep("./color", "FableCutColor"),
30
30
  dep("./mask", "FableCutMask"), dep("./tracker", "FableCutTracker"), dep("./motion", "FableCutMotion"),
31
31
  dep("./comp", "FableCutComp"), dep("./templates", "FableCutTemplates"), dep("./media-lib", "FableCutMedia"),
32
- dep("./delivery", "FableCutDelivery"));
32
+ dep("./delivery", "FableCutDelivery"), dep("./sequences", "FableCutSeq"));
33
33
  if (node) module.exports = api;
34
34
  else root.FableCutProjectOps = api;
35
- })(typeof self !== "undefined" ? self : this, function (FX, EditOps, Color, Mask, Tracker, Motion, CompLib, Templates, MediaLib, Delivery) {
35
+ })(typeof self !== "undefined" ? self : this, function (FX, EditOps, Color, Mask, Tracker, Motion, CompLib, Templates, MediaLib, Delivery, SeqLib) {
36
36
  "use strict";
37
37
 
38
38
  const DEFAULT_TRACKS = EditOps.DEFAULT_TRACKS;
@@ -54,7 +54,7 @@
54
54
  bold: true, weight: 0, italic: false, uppercase: false, align: "center",
55
55
  letterSpacing: 0, lineHeight: 1.2, textShadow: 12, glow: 0, glowColor: "",
56
56
  strokeWidth: 0, strokeColor: "#000", bgColor: "#000", bgOpacity: 0,
57
- textAnim: "none", wordRate: 0.15, direction: "auto", boxW: 0, boxH: 0, boxFit: false, vAlign: "middle",
57
+ textAnim: "none", wordRate: 0.15, highlightColor: "#ffd166", direction: "auto", boxW: 0, boxH: 0, boxFit: false, vAlign: "middle",
58
58
  anchorX: 0, anchorY: 0, trimStart: 0, trimEnd: 100, trimOffset: 0,
59
59
  autoOrient: false, motionBlur: 0, motionBlurSamples: 8, pathOffset: 0, pathAlign: "start",
60
60
  };
@@ -253,6 +253,7 @@
253
253
  // props.shapes (shape layers) and clip.parent: validated like setShape / setParent
254
254
  const NO_MEDIA = ["text", "adjust", "shape", "null", "comp"];
255
255
  let inCompId = null; // set while an inComp op runs its ops inside a comp
256
+ let inSeqId = null; // set while an inSequence op runs its ops on a sequence that isn't open
256
257
  // a comp clip names a comp that exists, and placing it here makes no loop
257
258
  const checkCompClip = (c, label) => {
258
259
  if (c.kind !== "comp") { if (c.compId != null) throw new Error(`${label}: compId is for kind:"comp" clips`); return; }
@@ -478,6 +479,7 @@
478
479
  };
479
480
  fit(proj.clips);
480
481
  for (const comp of proj.comps || []) fit(comp.clips);
482
+ for (const seq of SeqLib.others(proj)) fit(seq.clips);
481
483
  if (trimmed) note += ` (${trimmed} clip edge${trimmed === 1 ? "" : "s"} trimmed to the new file's ${m.duration}s)`;
482
484
  }
483
485
  notes.push(note);
@@ -490,6 +492,10 @@
490
492
  const inner = (comp.clips || []).find((c) => c.mediaId === op.id);
491
493
  if (inner) throw new Error(`removeMedia: media ${op.id} is used by clip ${inner.id} in comp ${comp.id}`);
492
494
  }
495
+ for (const seq of SeqLib.others(proj)) {
496
+ const inner = (seq.clips || []).find((c) => c.mediaId === op.id);
497
+ if (inner) throw new Error(`removeMedia: media ${op.id} is used by clip ${inner.id} in sequence ${seq.id}`);
498
+ }
493
499
  const n = proj.media.length;
494
500
  proj.media = proj.media.filter((x) => x.id !== op.id);
495
501
  if (proj.media.length === n) throw new Error("removeMedia: no media " + op.id);
@@ -791,7 +797,7 @@
791
797
  const comp = CompLib.getComp(proj, op.id);
792
798
  if (!comp) throw new Error("removeComp: no comp " + op.id);
793
799
  const used = CompLib.usages(proj, comp.id);
794
- if (used.length) throw new Error(`removeComp: ${comp.id} is used by ${used.map((u) => u.clip.id + (u.in ? ` (in ${u.in})` : "")).join(", ")} — remove those clips first`);
800
+ if (used.length) throw new Error(`removeComp: ${comp.id} is used by ${used.map((u) => u.clip.id + (u.in ? ` (in ${u.in})` : u.sequence ? ` (in sequence ${u.sequence})` : "")).join(", ")} — remove those clips first`);
795
801
  proj.comps = proj.comps.filter((c) => c.id !== comp.id);
796
802
  notes.push(`-comp ${comp.id}`);
797
803
  break;
@@ -1027,8 +1033,63 @@
1027
1033
  notes.push("-" + op.id);
1028
1034
  break;
1029
1035
  }
1036
+ case "addSequence": case "openSequence": case "updateSequence": case "removeSequence": case "inSequence": {
1037
+ // sequences: several timelines in one project; the open one is the top level (see sequences.js)
1038
+ const name = op.op;
1039
+ if (inSeqId && name !== "updateSequence") throw new Error(`${name} can't run inside inSequence — run it on its own`);
1040
+ if (name === "addSequence") {
1041
+ // {op:"addSequence", name?, id?, from?: "empty" | "open" | <id>, width?, height?, fps?, open?} (or those in sequence:{…})
1042
+ const src = op.sequence && typeof op.sequence === "object" ? op.sequence : {};
1043
+ const { op: _op, sequence: _s, open: _o, ...flat } = op;
1044
+ // a typo (widht, form) must not quietly make the wrong sequence
1045
+ for (const k of Object.keys({ ...flat, ...src })) if (!["id", "name", "from", "width", "height", "fps"].includes(k))
1046
+ throw new Error(`addSequence: '${k}' isn't a sequence setting (id, name, from, width, height, fps, open)`);
1047
+ let s;
1048
+ try { s = SeqLib.add(proj, { ...flat, ...src }); } catch (err) { throw new Error(err.message); }
1049
+ notes.push(`+sequence ${s.id} "${s.name}" ${s.width}x${s.height}@${s.fps}${s.clips.length ? ` (${s.clips.length} clips)` : ""}`);
1050
+ if (op.open) { SeqLib.open(proj, s.id); notes.push(`opened ${s.id}`); }
1051
+ break;
1052
+ }
1053
+ const seq = SeqLib.find(proj, op.id || op.sequence);
1054
+ if (!seq) throw new Error(`${name}: no sequence ${op.id || op.sequence} (${SeqLib.entries(proj).map((x) => x.id).join(", ")})`);
1055
+ if (name === "openSequence") {
1056
+ SeqLib.open(proj, seq.id);
1057
+ notes.push(`opened ${seq.id} "${seq.name}"`);
1058
+ } else if (name === "updateSequence") {
1059
+ const set = op.set || {};
1060
+ for (const k of Object.keys(set)) if (!["name", "index"].includes(k))
1061
+ throw new Error(`updateSequence: ${k} can't be set here (name, index) — set the canvas, frame rate and the rest with setProject, inside {op:"inSequence"} for one that isn't open`);
1062
+ try {
1063
+ if (set.name != null) SeqLib.rename(proj, seq.id, set.name);
1064
+ if (set.index != null) SeqLib.move(proj, seq.id, set.index);
1065
+ } catch (err) { throw new Error(`updateSequence: ${err.message}`); }
1066
+ notes.push(`~sequence ${seq.id}`);
1067
+ } else if (name === "removeSequence") {
1068
+ const wasOpen = SeqLib.activeId(proj) === seq.id;
1069
+ // a sequence holding the user's locked work goes only when they asked (force:true)
1070
+ const body = wasOpen ? proj : seq;
1071
+ const lockedLanes = new Set(Array.isArray(body.lockedTracks) ? body.lockedTracks : []);
1072
+ const hit = (body.clips || []).find((c) => c.locked === true || lockedLanes.has(c.track));
1073
+ refuseLocked("removeSequence", hit ? `sequence ${seq.id} holds locked clip ${hit.id}` : lockedLanes.size ? `sequence ${seq.id} has locked tracks (${[...lockedLanes].join(", ")})` : null, op);
1074
+ try { SeqLib.remove(proj, seq.id); } catch (err) { throw new Error(`removeSequence: ${err.message}`); }
1075
+ notes.push(`-sequence ${seq.id}` + (wasOpen ? ` (opened ${SeqLib.activeId(proj)})` : ""));
1076
+ } else {
1077
+ // {op:"inSequence", sequence, ops:[…]} — run ops on a sequence without opening it
1078
+ if (!Array.isArray(op.ops)) throw new Error("inSequence needs ops:[…]");
1079
+ if (inCompId) throw new Error("inSequence can't run inside inComp");
1080
+ const back = SeqLib.activeId(proj);
1081
+ SeqLib.open(proj, seq.id);
1082
+ inSeqId = seq.id;
1083
+ const n0 = notes.length;
1084
+ try { applyOps(op.ops); }
1085
+ finally { inSeqId = null; SeqLib.open(proj, back); }
1086
+ const inner = notes.splice(n0);
1087
+ notes.push(`${seq.id}{${inner.join(" ")}}`);
1088
+ }
1089
+ break;
1090
+ }
1030
1091
  case "setProject": {
1031
- const allowed = ["name", "width", "height", "fps", "background", "markers", "disabledTracks", "lockedTracks", "untargetedTracks", "exportFrame", "encodeProfile", "delivery", "outputs", "master", "inPoint", "outPoint"];
1092
+ const allowed =["name", "width", "height", "fps", "background", "markers", "disabledTracks", "lockedTracks", "untargetedTracks", "exportFrame", "encodeProfile", "delivery", "outputs", "master", "inPoint", "outPoint"];
1032
1093
  for (const [k, v] of Object.entries(op.set || {})) {
1033
1094
  if (!allowed.includes(k)) throw new Error(`setProject: '${k}' not settable (allowed: ${allowed.join(", ")})`);
1034
1095
  if ((k === "inPoint" || k === "outPoint") && v != null && (typeof v !== "number" || !Number.isFinite(v) || v < 0))
@@ -1083,7 +1144,8 @@
1083
1144
  default:
1084
1145
  throw new Error("Unknown op: " + op.op + " (addClip|updateClip|removeClip|addMedia|updateMedia|removeMedia|relinkMedia|replaceMedia|setCollection|removeCollection|setProject|setTrack|setBus|removeBus|setFx|setFxKeys|setGrade|setGradeLayer|setMask|removeMask|setMaskKeys|setMatte|removeTrack|setShape|removeShape|setParent|setMotionPath|setTextPath|" +
1085
1146
  "split|rippleDelete|closeGap|lift|extract|rippleTrim|roll|slip|slide|insert|overwrite|crossfade|" +
1086
- "addComp|updateComp|removeComp|precompose|inComp|applyPreset|addTemplate|setTemplate|savePreset|saveTemplate)");
1147
+ "addComp|updateComp|removeComp|precompose|inComp|applyPreset|addTemplate|setTemplate|savePreset|saveTemplate|" +
1148
+ "addSequence|openSequence|updateSequence|removeSequence|inSequence)");
1087
1149
  }
1088
1150
  } };
1089
1151
  applyOps(ops);
@@ -1107,14 +1169,15 @@
1107
1169
  kept[k] = v;
1108
1170
  continue;
1109
1171
  }
1110
- if (k === "masks" || k === "tracks" || k === "shapes" || k === "textPath") continue; // summarized below — point lists and samples are long
1172
+ if (k === "masks" || k === "tracks" || k === "shapes" || k === "textPath" || k === "wordTimes") continue; // summarized below — point lists and samples are long
1111
1173
  if (hex(PROP_DEFAULTS[k]) !== hex(v)) kept[k] = v;
1112
1174
  }
1113
1175
  const masks = Mask.describe(Mask.normalizeMasks(o.masks)), tracks = Tracker.describe(Tracker.normalizeTracks(o.tracks));
1114
1176
  const shapes = Motion.describe(Motion.normalizeShapes(o.shapes));
1115
1177
  const tp = o.textPath && Motion.normalizeTextPath(o.textPath);
1116
1178
  if (tp) kept.textPath = tp.type + (tp.points ? `(${tp.points.length})` : tp.w ? ` ${tp.w}×${tp.h}` : "");
1117
- return (Object.keys(kept).length ? " " + JSON.stringify(kept) : "") + (shapes ? ` [${shapes}]` : "") + (masks ? ` [${masks}]` : "") + (tracks ? ` [tracks: ${tracks}]` : "");
1179
+ const timed = Array.isArray(o.wordTimes) && o.wordTimes.length ? ` [${o.wordTimes.length} words timed to speech]` : "";
1180
+ return (Object.keys(kept).length ? " " + JSON.stringify(kept) : "") + (shapes ? ` [${shapes}]` : "") + (masks ? ` [${masks}]` : "") + (tracks ? ` [tracks: ${tracks}]` : "") + timed;
1118
1181
  };
1119
1182
  // Mixer: only off-default faders / pans, so a fresh project adds no line.
1120
1183
  const signed = (v) => (v > 0 ? "+" : "") + v;
@@ -1140,6 +1203,8 @@
1140
1203
  (doc.untargetedTracks?.length ? ` untargetedTracks:[${doc.untargetedTracks.join(",")}]` : "") +
1141
1204
  (doc.disabledTracks?.length ? ` disabledTracks:[${doc.disabledTracks.join(",")}]` : ""),
1142
1205
  ...mixLine(doc),
1206
+ ...(SeqLib.entries(doc).length > 1 ? [`SEQUENCES (${SeqLib.entries(doc).length}) — this view is the open one; edit another with {op:"inSequence", sequence, ops:[…]} or open it with openSequence:`,
1207
+ ...SeqLib.list(doc).map((r) => " " + SeqLib.describe(r))] : []),
1143
1208
  `MEDIA (${doc.media.length}):`,
1144
1209
  ...(() => {
1145
1210
  const uses = MediaLib.usageCounts(doc);
@@ -1209,9 +1274,12 @@
1209
1274
  seen.add(comp.id);
1210
1275
  }
1211
1276
  }
1212
- // the same checks for clips on the timeline and clips inside comps
1213
- const checkClip = (c, where) => {
1214
- const at = where ? `comp ${where}: ` : "";
1277
+ if (doc.sequences != null || doc.sequence != null) {
1278
+ try { SeqLib.normalize(doc); } catch (err) { throw new Error(err.message); }
1279
+ }
1280
+ // the same checks for clips on the timeline, inside comps and in the sequences that aren't open
1281
+ const checkClip = (c, where, seq) => {
1282
+ const at = where ? `comp ${where}: ` : seq ? `sequence ${seq}: ` : "";
1215
1283
  if (!c.id || !c.track || typeof c.start !== "number" || typeof c.duration !== "number")
1216
1284
  throw new Error(`${at}clip ${c.id || "?"} needs id, track, numeric start and duration`);
1217
1285
  if (!["text", "adjust", "shape", "null", "comp"].includes(c.kind) && !doc.media.some((m) => m.id === c.mediaId))
@@ -1227,6 +1295,7 @@
1227
1295
  };
1228
1296
  for (const c of doc.clips) checkClip(c, null);
1229
1297
  for (const comp of doc.comps || []) for (const c of comp.clips) checkClip(c, comp.id);
1298
+ for (const seq of SeqLib.others(doc)) for (const c of seq.clips || []) checkClip(c, null, seq.id);
1230
1299
  }
1231
1300
 
1232
1301
  const TEXT_ONLY_NOTE = [