File size: 5,508 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
 * Day 13 — flatten a TuitionVideoPlan into a frame-timed timeline.
 *
 * The Remotion composition renders from this timeline. Every scene gets an
 * absolute [fromFrame, durationInFrames) range at the plan's fps. A helper
 * produces a short "sample" slice (first N seconds) so preview/CI never has to
 * render the whole 40-60 minute video.
 */

import type { TuitionSegmentKind, TuitionVideoPlan, VideoScene } from "./types";

export type TimelineScene = {
  scene: VideoScene;
  segmentId: string;
  segmentKind: VideoScene["segmentKind"];
  fromFrame: number;
  durationInFrames: number;
  fromSeconds: number;
  durationSeconds: number;
};

export type TuitionVideoTimeline = {
  fps: number;
  totalFrames: number;
  totalSeconds: number;
  scenes: TimelineScene[];
};

export function buildTimeline(plan: TuitionVideoPlan): TuitionVideoTimeline {
  const fps = plan.fps;
  let frameCursor = 0;
  let secondsCursor = 0;
  const scenes: TimelineScene[] = [];

  for (const segment of plan.segments) {
    for (const scene of segment.scenes) {
      const durationInFrames = Math.max(1, Math.round(scene.durationSeconds * fps));
      scenes.push({
        scene,
        segmentId: segment.id,
        segmentKind: segment.kind,
        fromFrame: frameCursor,
        durationInFrames,
        fromSeconds: secondsCursor,
        durationSeconds: scene.durationSeconds,
      });
      frameCursor += durationInFrames;
      secondsCursor += scene.durationSeconds;
    }
  }

  return { fps, totalFrames: frameCursor, totalSeconds: secondsCursor, scenes };
}

/**
 * A short preview slice: every scene that starts within the first `seconds`.
 * Used to render only the opening 3-5 minutes for a fast preview.
 */
export function sampleTimeline(plan: TuitionVideoPlan, seconds = 240): TuitionVideoTimeline {
  const full = buildTimeline(plan);
  const cutoffFrames = Math.round(seconds * full.fps);
  const scenes = full.scenes.filter((s) => s.fromFrame < cutoffFrames);
  const lastEnd = scenes.length > 0 ? scenes[scenes.length - 1].fromFrame + scenes[scenes.length - 1].durationInFrames : 0;
  const lastSeconds =
    scenes.length > 0 ? scenes[scenes.length - 1].fromSeconds + scenes[scenes.length - 1].durationSeconds : 0;
  return { fps: full.fps, totalFrames: lastEnd, totalSeconds: lastSeconds, scenes };
}

/** One item in a highlight-reel pick list: which segment, and an optional time cap. */
export type HighlightPick = {
  segmentKind: TuitionSegmentKind;
  /** Cap this scene's rendered length (e.g. show only the "intro" of a long scene). */
  maxSeconds?: number;
};

/**
 * Day 15 — a short "highlight reel" timeline: one representative scene per
 * named segment, concatenated back to back and re-based to start at 0.
 * Segment scene durations are content-driven (real narration + board-action
 * time) and cannot be shrunk by asking for a smaller `targetDurationMinutes`,
 * so this is how a genuine 60-90 second sample covering several segments is
 * built: pick one real scene from each segment (the first one with a diagram
 * for `visual_board`, so the sample actually shows a diagram reveal), and
 * optionally cap a long scene's length to just its opening beat.
 */
export function highlightTimeline(plan: TuitionVideoPlan, picks: HighlightPick[]): TuitionVideoTimeline {
  const full = buildTimeline(plan);
  let frameCursor = 0;
  let secondsCursor = 0;
  const scenes: TimelineScene[] = [];

  for (const pick of picks) {
    const segmentScenes = full.scenes.filter((s) => s.segmentKind === pick.segmentKind);
    const chosen = pick.segmentKind === "visual_board" ? (segmentScenes.find((s) => s.scene.diagram) ?? segmentScenes[0]) : segmentScenes[0];
    if (!chosen) continue;

    const durationSeconds = pick.maxSeconds ? Math.min(chosen.durationSeconds, pick.maxSeconds) : chosen.durationSeconds;
    const durationInFrames = Math.max(1, Math.round(durationSeconds * full.fps));
    scenes.push({
      ...chosen,
      fromFrame: frameCursor,
      fromSeconds: secondsCursor,
      durationSeconds,
      durationInFrames,
    });
    frameCursor += durationInFrames;
    secondsCursor += durationSeconds;
  }

  return { fps: full.fps, totalFrames: frameCursor, totalSeconds: secondsCursor, scenes };
}

/**
 * Day 15 — one segment's scenes, re-based so the segment starts at frame 0
 * and second 0. This is what makes "render one segment at a time" possible:
 * a segment renders as its own independent clip instead of needing the whole
 * 40-60 minute timeline in memory. Returns an empty timeline if the plan has
 * no segment of that kind (should not happen for a `strategyReady` plan,
 * since every plan carries all ten canonical segments).
 */
export function segmentTimeline(plan: TuitionVideoPlan, segmentKind: TuitionSegmentKind): TuitionVideoTimeline {
  const full = buildTimeline(plan);
  const segmentScenes = full.scenes.filter((s) => s.segmentKind === segmentKind);
  if (segmentScenes.length === 0) return { fps: full.fps, totalFrames: 0, totalSeconds: 0, scenes: [] };

  const offsetFrames = segmentScenes[0].fromFrame;
  const offsetSeconds = segmentScenes[0].fromSeconds;
  const scenes = segmentScenes.map((s) => ({
    ...s,
    fromFrame: s.fromFrame - offsetFrames,
    fromSeconds: s.fromSeconds - offsetSeconds,
  }));
  const last = scenes[scenes.length - 1];
  return {
    fps: full.fps,
    totalFrames: last.fromFrame + last.durationInFrames,
    totalSeconds: last.fromSeconds + last.durationSeconds,
    scenes,
  };
}