@slidevideo/cli 0.10.0 → 0.11.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/dist/timing.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { isMediaScene } from './standalone-media.js';
1
2
  import { requireCondition } from './errors.js';
2
3
  import { probe } from './media.js';
3
4
  import { checkSpeech, retainedSpeechTiming } from './speech.js';
@@ -79,13 +80,38 @@ async function resolveSchedule(story, speech) {
79
80
  const segments = new Map();
80
81
  let cursor = 0;
81
82
  const sceneRanges = new Map();
82
- function duration(action) {
83
- if (action.type === 'speak') {
84
- const asset = assets.get(action.segment);
85
- requireCondition(asset, 'MISSING_SPEECH', `Generate speech before resolving ${action.segment}`);
86
- requireCondition(asset.transcript.original === story.narration.find(n => n.id === action.segment)?.text, 'SPEECH_MISMATCH', `Speech text changed: ${action.segment}`);
87
- return asset.duration_ms;
83
+ const playTimelines = new Map();
84
+ const playByMedia = new Map();
85
+ function resolvePhraseOffset(ownerLabel, segmentId, spec, context) {
86
+ const asset = assets.get(segmentId);
87
+ const alignment = asset.alignment;
88
+ requireCondition(alignment && alignment.units.length > 0, 'CUE_ALIGNMENT_REQUIRED', `${ownerLabel} requires alignment for ${segmentId}`, 2, context);
89
+ const matches = [];
90
+ let pos = alignment.text.indexOf(spec.phrase);
91
+ while (pos >= 0) {
92
+ matches.push(pos);
93
+ pos = alignment.text.indexOf(spec.phrase, pos + 1);
88
94
  }
95
+ requireCondition(matches.length > 0, 'CUE_PHRASE_MISSING', `${ownerLabel}: phrase not found in ${alignment.form} transcript`, 2, context);
96
+ requireCondition(spec.occurrence !== undefined || matches.length === 1, 'CUE_AMBIGUOUS', `${ownerLabel}: specify occurrence for repeated phrase`, 2, context);
97
+ const start = matches[(spec.occurrence ?? 1) - 1];
98
+ requireCondition(start !== undefined, 'CUE_OCCURRENCE', `${ownerLabel}: occurrence does not exist`, 2, context);
99
+ const end = start + spec.phrase.length;
100
+ const first = alignment.units.find(unit => unit.start === start);
101
+ const last = alignment.units.find(unit => unit.end === end);
102
+ requireCondition(first && last, 'CUE_BOUNDARY', `${ownerLabel}: exact phrase boundaries unavailable`, 2, context);
103
+ const offset = (spec.boundary === 'start' ? first.start_ms : last.end_ms) + (spec.offset_ms ?? 0);
104
+ return { offset, cue_span: { segment: segmentId, start, end, form: alignment.form } };
105
+ }
106
+ function resolveSpeechDuration(segmentId) {
107
+ const asset = assets.get(segmentId);
108
+ requireCondition(asset, 'MISSING_SPEECH', `Generate speech before resolving ${segmentId}`);
109
+ requireCondition(asset.transcript.original === story.narration.find(n => n.id === segmentId)?.text, 'SPEECH_MISMATCH', `Speech text changed: ${segmentId}`);
110
+ return asset.duration_ms;
111
+ }
112
+ function duration(action) {
113
+ if (action.type === 'speak')
114
+ return resolveSpeechDuration(action.segment);
89
115
  if (action.type === 'play') {
90
116
  const clip = media.find(item => item.id === action.media);
91
117
  const start = action.trim_start_ms ?? 0;
@@ -96,10 +122,236 @@ async function resolveSchedule(story, speech) {
96
122
  }
97
123
  return action.duration_ms;
98
124
  }
125
+ const scenePlayIds = new Map();
126
+ for (const scene of story.scenes) {
127
+ for (const action of scene.actions) {
128
+ if (action.type === 'play') {
129
+ const list = scenePlayIds.get(action.media) ?? [];
130
+ list.push(action.id);
131
+ scenePlayIds.set(action.media, list);
132
+ }
133
+ }
134
+ }
135
+ function mediaCuePlayId(at) {
136
+ return at.play ?? scenePlayIds.get(at.media)[0];
137
+ }
138
+ const speakCuesByPlay = new Map();
139
+ for (const [order, cue] of story.cues.entries()) {
140
+ if ('media' in cue.at && cue.action.type === 'speak') {
141
+ const speakCue = { id: cue.id, at: cue.at, action: cue.action, ...(cue.align ? { align: cue.align } : {}) };
142
+ const playId = mediaCuePlayId(speakCue.at);
143
+ const list = speakCuesByPlay.get(playId) ?? [];
144
+ list.push({ cue: speakCue, order });
145
+ speakCuesByPlay.set(playId, list);
146
+ }
147
+ }
148
+ function resolveScenePlay(sceneId, action, playStart) {
149
+ const clip = media.find(item => item.id === action.media);
150
+ const trimStart = action.trim_start_ms ?? 0;
151
+ const trimEnd = action.trim_end_ms ?? clip.duration_ms;
152
+ requireCondition(trimStart < trimEnd && trimEnd <= clip.duration_ms, 'MEDIA_TRIM', `Invalid trim for ${action.id}`);
153
+ requireCondition(action.audio !== 'source' || clip.has_audio, 'MEDIA_AUDIO', `Source audio missing for ${action.id}`);
154
+ const baseRate = action.rate ?? 1;
155
+ const overflow = action.overflow ?? 'continuous';
156
+ const minRate = action.min_rate ?? 0.75;
157
+ const maxRate = action.max_rate ?? 1.3;
158
+ const boundSpeaks = speakCuesByPlay.get(action.id) ?? [];
159
+ if (!boundSpeaks.length) {
160
+ const endMs = playStart + (trimEnd - trimStart) / baseRate;
161
+ const slice = { mediaStart: trimStart, mediaEnd: trimEnd, timeStart: playStart, timeEnd: endMs, rate: baseRate };
162
+ actions.push({ id: action.id, scene: sceneId, start_ms: playStart, end_ms: endMs, action });
163
+ const timeline = { id: action.id, scene: sceneId, media: action.media, trimStart, trimEnd, slices: [slice], endMs };
164
+ playTimelines.set(action.id, timeline);
165
+ playByMedia.set(action.media, [...(playByMedia.get(action.media) ?? []), timeline]);
166
+ return endMs;
167
+ }
168
+ const beats = boundSpeaks.map(({ cue, order }) => {
169
+ const speechDurationMs = resolveSpeechDuration(cue.action.segment);
170
+ const targetMediaMs = cue.at.time_ms + (cue.at.offset_ms ?? 0);
171
+ const context = { cue: cue.id, media: cue.at.media, play: action.id, segment: cue.action.segment };
172
+ requireCondition(targetMediaMs >= trimStart && targetMediaMs <= trimEnd, 'MEDIA_CUE_RANGE', `Cue ${cue.id}: target ${Math.round(targetMediaMs)} ms lies outside ${action.media} [${Math.round(trimStart)}, ${Math.round(trimEnd)}] ms`, 2, context);
173
+ let leadMs = 0;
174
+ let cue_span;
175
+ if (cue.align) {
176
+ const aligned = resolvePhraseOffset(`Cue ${cue.id}`, cue.action.segment, cue.align, context);
177
+ leadMs = aligned.offset;
178
+ cue_span = aligned.cue_span;
179
+ }
180
+ const desiredMediaStartMs = targetMediaMs - leadMs * baseRate;
181
+ requireCondition(desiredMediaStartMs >= trimStart - 1e-6, 'MEDIA_CUE_RANGE', `Cue ${cue.id}: aligned phrase lead (${Math.round(leadMs)} ms) starts before ${action.media} clip start`, 2, context);
182
+ return {
183
+ cue,
184
+ order,
185
+ targetMediaMs,
186
+ desiredMediaStartMs: Math.max(trimStart, desiredMediaStartMs),
187
+ leadMs,
188
+ speechDurationMs,
189
+ cue_span,
190
+ };
191
+ });
192
+ beats.sort((a, b) => a.desiredMediaStartMs - b.desiredMediaStartMs || a.order - b.order);
193
+ const rawSlices = [];
194
+ let curMedia = trimStart;
195
+ let curTime = playStart;
196
+ let speechFreeAt = playStart;
197
+ let prevAlignAnchorMedia;
198
+ function fitInterval(intervalStartMedia, intervalEndMedia, intervalStartTime, intervalEndTime, label, context) {
199
+ const totalMedia = intervalEndMedia - intervalStartMedia;
200
+ const totalTime = intervalEndTime - intervalStartTime;
201
+ requireCondition(totalMedia > 1e-6 && totalTime > 1e-6, 'MEDIA_BEAT_RATE', `${label}: cannot fit zero-length video interval to speech overrun`, 2, context);
202
+ if (prevAlignAnchorMedia !== undefined && prevAlignAnchorMedia > intervalStartMedia + 1e-6) {
203
+ const remMedia = intervalEndMedia - prevAlignAnchorMedia;
204
+ const anchorTime = intervalStartTime + (prevAlignAnchorMedia - intervalStartMedia) / baseRate;
205
+ const remTime = intervalEndTime - anchorTime;
206
+ requireCondition(remMedia > 1e-6 && remTime > 1e-6, 'MEDIA_BEAT_RATE', `${label}: cannot fit speech overrun without shifting aligned phrase at ${Math.round(prevAlignAnchorMedia)} ms`, 2, context);
207
+ const fitted = remMedia / remTime;
208
+ requireCondition(fitted >= minRate - 1e-6 && fitted <= maxRate + 1e-6, 'MEDIA_BEAT_RATE', `${label}: required post-anchor playback rate ${fitted.toFixed(3)} is outside [${minRate}, ${maxRate}]`, 2, context);
209
+ rawSlices.push({ mediaStart: intervalStartMedia, mediaEnd: prevAlignAnchorMedia, timeStart: intervalStartTime, timeEnd: anchorTime, rate: baseRate });
210
+ rawSlices.push({ mediaStart: prevAlignAnchorMedia, mediaEnd: intervalEndMedia, timeStart: anchorTime, timeEnd: intervalEndTime, rate: Math.min(maxRate, Math.max(minRate, fitted)) });
211
+ return;
212
+ }
213
+ const fittedRate = totalMedia / totalTime;
214
+ requireCondition(fittedRate >= minRate - 1e-6 && fittedRate <= maxRate + 1e-6, 'MEDIA_BEAT_RATE', `${label}: required playback rate ${fittedRate.toFixed(3)} is outside [${minRate}, ${maxRate}]`, 2, context);
215
+ rawSlices.push({ mediaStart: intervalStartMedia, mediaEnd: intervalEndMedia, timeStart: intervalStartTime, timeEnd: intervalEndTime, rate: Math.min(maxRate, Math.max(minRate, fittedRate)) });
216
+ }
217
+ for (const beat of beats) {
218
+ const mediaDelta = beat.desiredMediaStartMs - curMedia;
219
+ const naturalArrival = curTime + mediaDelta / baseRate;
220
+ const overrun = speechFreeAt - naturalArrival;
221
+ const context = { cue: beat.cue.id, media: action.media, play: action.id, segment: beat.cue.action.segment, overrun_ms: Math.round(Math.max(0, overrun)) };
222
+ if (overrun <= 1e-6) {
223
+ if (mediaDelta > 1e-6) {
224
+ rawSlices.push({ mediaStart: curMedia, mediaEnd: beat.desiredMediaStartMs, timeStart: curTime, timeEnd: naturalArrival, rate: baseRate });
225
+ }
226
+ curMedia = beat.desiredMediaStartMs;
227
+ curTime = naturalArrival;
228
+ }
229
+ else if (overflow === 'continuous') {
230
+ requireCondition(false, 'MEDIA_BEAT_OVERRUN', `Cue ${beat.cue.id}: previous narration overruns ${action.media} at ${Math.round(beat.targetMediaMs)} ms by ${Math.round(overrun)} ms`, 2, context);
231
+ }
232
+ else if (overflow === 'freeze') {
233
+ if (mediaDelta > 1e-6) {
234
+ rawSlices.push({ mediaStart: curMedia, mediaEnd: beat.desiredMediaStartMs, timeStart: curTime, timeEnd: naturalArrival, rate: baseRate });
235
+ }
236
+ curMedia = beat.desiredMediaStartMs;
237
+ curTime = speechFreeAt;
238
+ }
239
+ else {
240
+ fitInterval(curMedia, beat.desiredMediaStartMs, curTime, speechFreeAt, `Cue ${beat.cue.id}`, context);
241
+ curMedia = beat.desiredMediaStartMs;
242
+ curTime = speechFreeAt;
243
+ }
244
+ const speakItem = {
245
+ id: beat.cue.id,
246
+ scene: sceneId,
247
+ start_ms: curTime,
248
+ end_ms: curTime + beat.speechDurationMs,
249
+ action: { ...beat.cue.action, id: beat.cue.id },
250
+ ...(beat.cue_span ? { cue_span: beat.cue_span } : {}),
251
+ };
252
+ actions.push(speakItem);
253
+ segments.set(beat.cue.action.segment, speakItem);
254
+ speechFreeAt = speakItem.end_ms;
255
+ prevAlignAnchorMedia = beat.leadMs > 0 ? beat.targetMediaMs : undefined;
256
+ }
257
+ const remMedia = trimEnd - curMedia;
258
+ const naturalEnd = curTime + remMedia / baseRate;
259
+ const tailOverrun = speechFreeAt - naturalEnd;
260
+ let endMs = naturalEnd;
261
+ if (tailOverrun <= 1e-6) {
262
+ if (remMedia > 1e-6) {
263
+ rawSlices.push({ mediaStart: curMedia, mediaEnd: trimEnd, timeStart: curTime, timeEnd: naturalEnd, rate: baseRate });
264
+ }
265
+ endMs = naturalEnd;
266
+ }
267
+ else if (overflow === 'continuous') {
268
+ requireCondition(false, 'MEDIA_BEAT_OVERRUN', `Play ${action.id}: narration overruns ${action.media} clip end (${Math.round(trimEnd)} ms) by ${Math.round(tailOverrun)} ms`, 2, { play: action.id, media: action.media, overrun_ms: Math.round(tailOverrun) });
269
+ }
270
+ else if (overflow === 'freeze') {
271
+ if (remMedia > 1e-6) {
272
+ rawSlices.push({ mediaStart: curMedia, mediaEnd: trimEnd, timeStart: curTime, timeEnd: naturalEnd, rate: baseRate });
273
+ }
274
+ endMs = speechFreeAt;
275
+ }
276
+ else {
277
+ fitInterval(curMedia, trimEnd, curTime, speechFreeAt, `Play ${action.id}`, { play: action.id, media: action.media, overrun_ms: Math.round(tailOverrun) });
278
+ endMs = speechFreeAt;
279
+ }
280
+ const merged = [];
281
+ for (const slice of rawSlices) {
282
+ const prev = merged.at(-1);
283
+ if (prev
284
+ && Math.abs(prev.timeEnd - slice.timeStart) < 1e-6
285
+ && Math.abs(prev.mediaEnd - slice.mediaStart) < 1e-6
286
+ && Math.abs(prev.rate - slice.rate) < 1e-6) {
287
+ prev.mediaEnd = slice.mediaEnd;
288
+ prev.timeEnd = slice.timeEnd;
289
+ }
290
+ else {
291
+ merged.push({ ...slice });
292
+ }
293
+ }
294
+ merged.forEach((slice, index) => {
295
+ const sliceId = merged.length === 1 ? action.id : `${action.id}:slice-${index + 1}`;
296
+ actions.push({
297
+ id: sliceId,
298
+ scene: sceneId,
299
+ start_ms: slice.timeStart,
300
+ end_ms: slice.timeEnd,
301
+ action: {
302
+ id: sliceId,
303
+ type: 'play',
304
+ media: action.media,
305
+ audio: action.audio,
306
+ volume: action.volume,
307
+ trim_start_ms: slice.mediaStart,
308
+ trim_end_ms: slice.mediaEnd,
309
+ rate: slice.rate,
310
+ },
311
+ });
312
+ });
313
+ const timeline = { id: action.id, scene: sceneId, media: action.media, trimStart, trimEnd, slices: merged, endMs };
314
+ playTimelines.set(action.id, timeline);
315
+ playByMedia.set(action.media, [...(playByMedia.get(action.media) ?? []), timeline]);
316
+ return endMs;
317
+ }
318
+ function resolveMediaCueTime(ownerLabel, at, context) {
319
+ const playId = mediaCuePlayId(at);
320
+ const timeline = playTimelines.get(playId);
321
+ const targetMediaMs = at.time_ms + (at.offset_ms ?? 0);
322
+ requireCondition(targetMediaMs >= timeline.trimStart - 1e-6 && targetMediaMs <= timeline.trimEnd + 1e-6, 'MEDIA_CUE_RANGE', `${ownerLabel}: target ${Math.round(targetMediaMs)} ms lies outside ${at.media} [${Math.round(timeline.trimStart)}, ${Math.round(timeline.trimEnd)}] ms`, 2, context);
323
+ const clamped = Math.min(timeline.trimEnd, Math.max(timeline.trimStart, targetMediaMs));
324
+ for (let i = 0; i < timeline.slices.length; i++) {
325
+ const slice = timeline.slices[i];
326
+ if (clamped >= slice.mediaStart - 1e-6 && clamped <= slice.mediaEnd + 1e-6) {
327
+ if (Math.abs(clamped - slice.mediaEnd) < 1e-6) {
328
+ const next = timeline.slices[i + 1];
329
+ if (next && Math.abs(next.mediaStart - clamped) < 1e-6) {
330
+ return { time: next.timeStart, scene: timeline.scene };
331
+ }
332
+ }
333
+ return { time: slice.timeStart + (clamped - slice.mediaStart) / slice.rate, scene: timeline.scene };
334
+ }
335
+ }
336
+ return { time: timeline.endMs, scene: timeline.scene };
337
+ }
338
+ const resolvedScenes = [];
339
+ const mediaSceneMap = new Map();
99
340
  for (const scene of story.scenes) {
100
341
  const start = cursor;
101
- actions.push({ id: `${scene.id}:slide`, scene: scene.id, start_ms: cursor, end_ms: cursor, action: { id: `${scene.id}:slide`, type: 'slide', target: scene.slide, duration_ms: 0 } });
342
+ if (isMediaScene(scene)) {
343
+ const clip = media.find(item => item.id === scene.media);
344
+ requireCondition(clip.width > 0 && clip.height > 0, 'MEDIA_VIDEO_REQUIRED', `Standalone media scene ${scene.id} requires a video stream in ${scene.media}`);
345
+ mediaSceneMap.set(scene.id, scene.media);
346
+ }
347
+ else {
348
+ actions.push({ id: `${scene.id}:slide`, scene: scene.id, start_ms: cursor, end_ms: cursor, action: { id: `${scene.id}:slide`, type: 'slide', target: scene.slide, duration_ms: 0 } });
349
+ }
102
350
  for (const action of scene.actions) {
351
+ if (action.type === 'play') {
352
+ cursor = resolveScenePlay(scene.id, action, cursor);
353
+ continue;
354
+ }
103
355
  const item = { id: action.id, scene: scene.id, start_ms: cursor, end_ms: cursor + duration(action), action };
104
356
  actions.push(item);
105
357
  if (action.type === 'speak')
@@ -111,32 +363,30 @@ async function resolveSchedule(story, speech) {
111
363
  cursor = start + scene.duration_ms;
112
364
  }
113
365
  sceneRanges.set(scene.id, { start, end: cursor });
366
+ resolvedScenes.push(isMediaScene(scene)
367
+ ? { id: scene.id, kind: 'media', media: scene.media, start_ms: start, end_ms: cursor }
368
+ : { id: scene.id, kind: 'slide', slide: scene.slide, start_ms: start, end_ms: cursor });
114
369
  }
115
370
  function phrase(cue, annotation) {
116
371
  const segment = segments.get(cue.at.segment);
117
- const asset = assets.get(cue.at.segment);
118
- const alignment = asset.alignment;
119
372
  const context = { cue: cue.id, segment: cue.at.segment, ...(annotation ? { annotation: cue.id, target: annotation } : {}) };
120
- requireCondition(alignment && alignment.units.length > 0, 'CUE_ALIGNMENT_REQUIRED', `Cue ${cue.id} requires alignment for ${cue.at.segment}`, 2, context);
121
- const matches = [];
122
- let pos = alignment.text.indexOf(cue.at.phrase);
123
- while (pos >= 0) {
124
- matches.push(pos);
125
- pos = alignment.text.indexOf(cue.at.phrase, pos + 1);
126
- }
127
- requireCondition(matches.length > 0, 'CUE_PHRASE_MISSING', `Cue ${cue.id}: phrase not found in ${alignment.form} transcript`, 2, context);
128
- requireCondition(cue.at.occurrence !== undefined || matches.length === 1, 'CUE_AMBIGUOUS', `Cue ${cue.id}: specify occurrence for repeated phrase`, 2, context);
129
- const start = matches[(cue.at.occurrence ?? 1) - 1];
130
- requireCondition(start !== undefined, 'CUE_OCCURRENCE', `Cue ${cue.id}: occurrence does not exist`, 2, context);
131
- const end = start + cue.at.phrase.length;
132
- const first = alignment.units.find(unit => unit.start === start);
133
- const last = alignment.units.find(unit => unit.end === end);
134
- requireCondition(first && last, 'CUE_BOUNDARY', `Cue ${cue.id}: exact phrase boundaries unavailable`, 2, context);
135
- const time = segment.start_ms + (cue.at.boundary === 'start' ? first.start_ms : last.end_ms) + cue.at.offset_ms;
136
- return { time, segment, context, cue_span: { segment: cue.at.segment, start, end, form: alignment.form } };
373
+ const { offset, cue_span } = resolvePhraseOffset(`Cue ${cue.id}`, cue.at.segment, cue.at, context);
374
+ const time = segment.start_ms + offset;
375
+ return { time, segment, context, cue_span };
137
376
  }
138
377
  for (const cue of story.cues) {
139
- const { time, segment, context, cue_span } = phrase(cue);
378
+ if ('media' in cue.at) {
379
+ if (cue.action.type === 'speak')
380
+ continue;
381
+ const context = { cue: cue.id, media: cue.at.media, ...(cue.at.play ? { play: cue.at.play } : {}) };
382
+ const { time, scene } = resolveMediaCueTime(`Cue ${cue.id}`, cue.at, context);
383
+ const action = { ...cue.action, id: cue.id };
384
+ const endTime = time + duration(action);
385
+ requireCondition(time >= 0 && endTime <= cursor, 'CUE_RANGE', `Cue ${cue.id} lies outside production`, 2, context);
386
+ actions.push({ id: cue.id, scene, start_ms: time, end_ms: endTime, action });
387
+ continue;
388
+ }
389
+ const { time, segment, context, cue_span } = phrase({ id: cue.id, at: cue.at });
140
390
  const action = { ...cue.action, id: cue.id };
141
391
  const endTime = time + duration(action);
142
392
  requireCondition(time >= 0 && endTime <= cursor, 'CUE_RANGE', `Cue ${cue.id} lies outside production`, 2, context);
@@ -147,6 +397,10 @@ async function resolveSchedule(story, speech) {
147
397
  let lastClick = 0;
148
398
  for (let index = 0; index < actions.length; index++) {
149
399
  const item = actions[index];
400
+ if (item.action.type === 'play' && mediaSceneMap.has(item.scene)) {
401
+ const range = sceneRanges.get(item.scene);
402
+ requireCondition(item.start_ms >= range.start - 1e-6 && item.end_ms <= range.end + 1e-6, 'MEDIA_SCENE_RANGE', `Play ${item.id} exceeds standalone media scene ${item.scene} bounds [${Math.round(range.start)}, ${Math.round(range.end)}] ms`);
403
+ }
150
404
  if (item.action.type === 'slide') {
151
405
  activeSlide = item.action.target;
152
406
  lastClick = 0;
@@ -199,17 +453,28 @@ async function resolveSchedule(story, speech) {
199
453
  requireCondition(end > time, 'ANNOTATION_DURATION', `Annotation ${resolvedId}: interval must have positive duration`, 2, context);
200
454
  const range = sceneRanges.get(annotation.scene);
201
455
  requireCondition(segmentScene === annotation.scene && time >= range.start && end <= range.end, 'ANNOTATION_RANGE', `Annotation ${resolvedId}: interval must fit its narration scene ${annotation.scene}`, 2, context);
202
- const preceding = visuals.filter(item => item.start_ms <= time);
203
- const slide = preceding.findLast(item => item.action.type === 'slide');
204
- const state = preceding.at(-1);
205
- requireCondition(time >= state.end_ms && !visuals.some(item => item.start_ms > time && item.start_ms < end), 'ANNOTATION_STATE', `Annotation ${resolvedId}: interval must fit one settled slide visit and reveal state`, 2, context);
206
- const click = state.action.type === 'reveal' ? state.action.click : 0;
207
- requireCondition(slide.action.type === 'slide', 'ANNOTATION_STATE', `Annotation ${resolvedId}: no active slide`, 2, context);
456
+ let slideTarget;
457
+ let visitId;
458
+ let click = 0;
459
+ if (mediaSceneMap.has(annotation.scene)) {
460
+ slideTarget = mediaSceneMap.get(annotation.scene);
461
+ visitId = `${annotation.scene}:media`;
462
+ }
463
+ else {
464
+ const preceding = visuals.filter(item => item.start_ms <= time);
465
+ const slide = preceding.findLast(item => item.action.type === 'slide');
466
+ const state = preceding.at(-1);
467
+ requireCondition(time >= state.end_ms && !visuals.some(item => item.start_ms > time && item.start_ms < end), 'ANNOTATION_STATE', `Annotation ${resolvedId}: interval must fit one settled slide visit and reveal state`, 2, context);
468
+ click = state.action.type === 'reveal' ? state.action.click : 0;
469
+ requireCondition(slide.action.type === 'slide', 'ANNOTATION_STATE', `Annotation ${resolvedId}: no active slide`, 2, context);
470
+ slideTarget = slide.action.target;
471
+ visitId = slide.id;
472
+ }
208
473
  const windowFields = {
209
474
  start_ms: time,
210
475
  end_ms: end,
211
- slide: slide.action.target,
212
- visit: slide.id,
476
+ slide: slideTarget,
477
+ visit: visitId,
213
478
  click,
214
479
  ...(cue_span ? { cue_span } : {}),
215
480
  ...(sourceId ? { source_id: sourceId } : {}),
@@ -354,6 +619,11 @@ async function resolveSchedule(story, speech) {
354
619
  const context = { annotation: annotation.id, scene: annotation.scene, target: normalizedTarget };
355
620
  return [buildResolvedAnnotation(annotation, annotation.id, undefined, normalizedTarget, annotation.scene, time, time + annotation.duration_ms, undefined, context)];
356
621
  }
622
+ if ('media' in annotation.at) {
623
+ const context = { annotation: annotation.id, scene: annotation.scene, media: annotation.at.media, ...(annotation.at.play ? { play: annotation.at.play } : {}), target: normalizedTarget };
624
+ const { time, scene } = resolveMediaCueTime(`Annotation ${annotation.id}`, annotation.at, context);
625
+ return [buildResolvedAnnotation(annotation, annotation.id, undefined, normalizedTarget, scene, time, time + annotation.duration_ms, undefined, context)];
626
+ }
357
627
  const { time, segment, context, cue_span } = phrase({ id: annotation.id, at: annotation.at }, normalizedTarget);
358
628
  return [buildResolvedAnnotation(annotation, annotation.id, undefined, normalizedTarget, segment.scene, time, time + annotation.duration_ms, cue_span, context)];
359
629
  }
@@ -490,6 +760,7 @@ async function resolveSchedule(story, speech) {
490
760
  });
491
761
  return {
492
762
  version: 1, annotations,
763
+ ...(mediaSceneMap.size ? { scenes: resolvedScenes } : {}),
493
764
  duration_ms: cursor, actions, media: media.map(({ id, src, duration_ms, has_audio }) => ({ id, src, duration_ms, has_audio })),
494
765
  ...(overlays.length ? { overlays } : {}),
495
766
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slidevideo/cli",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Turn a slide deck into a narrated video.",
5
5
  "license": "SEE LICENSE IN LICENSE-TERMS.md",
6
6
  "type": "module",
@@ -117,6 +117,8 @@
117
117
  "dist/speech.js",
118
118
  "dist/stamp-fonts.d.ts",
119
119
  "dist/stamp-fonts.js",
120
+ "dist/standalone-media.d.ts",
121
+ "dist/standalone-media.js",
120
122
  "dist/storyboard.d.ts",
121
123
  "dist/storyboard.js",
122
124
  "dist/timing.d.ts",
@@ -10,6 +10,73 @@
10
10
  "type": "number",
11
11
  "minimum": 0
12
12
  },
13
+ "scenes": {
14
+ "type": "array",
15
+ "items": {
16
+ "oneOf": [
17
+ {
18
+ "type": "object",
19
+ "properties": {
20
+ "id": {
21
+ "type": "string"
22
+ },
23
+ "kind": {
24
+ "const": "slide"
25
+ },
26
+ "slide": {
27
+ "type": "string"
28
+ },
29
+ "start_ms": {
30
+ "type": "number",
31
+ "minimum": 0
32
+ },
33
+ "end_ms": {
34
+ "type": "number",
35
+ "minimum": 0
36
+ }
37
+ },
38
+ "required": [
39
+ "id",
40
+ "kind",
41
+ "slide",
42
+ "start_ms",
43
+ "end_ms"
44
+ ],
45
+ "additionalProperties": false
46
+ },
47
+ {
48
+ "type": "object",
49
+ "properties": {
50
+ "id": {
51
+ "type": "string"
52
+ },
53
+ "kind": {
54
+ "const": "media"
55
+ },
56
+ "media": {
57
+ "type": "string"
58
+ },
59
+ "start_ms": {
60
+ "type": "number",
61
+ "minimum": 0
62
+ },
63
+ "end_ms": {
64
+ "type": "number",
65
+ "minimum": 0
66
+ }
67
+ },
68
+ "required": [
69
+ "id",
70
+ "kind",
71
+ "media",
72
+ "start_ms",
73
+ "end_ms"
74
+ ],
75
+ "additionalProperties": false
76
+ }
77
+ ]
78
+ }
79
+ },
13
80
  "actions": {
14
81
  "type": "array",
15
82
  "items": {