DocDoeAI / src /video /tuition /renderLessonVideoPlan.ts
asnannp's picture
deploy: sync backend to Space root (learn-lesson HF cache fix)
65d68f5
Raw History Blame Contribute Delete
5.51 kB
/**
* 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,
};
}