@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 190 lines 6.19 KB
Raw
1 import { createHash } from "node:crypto";
2 import { cp, mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
3 import path from "node:path";
4 import { parseArgs } from "node:util";
5 /**
6 * Renders the deck with Remotion and writes a presentation workspace that
7 * `tcut present` opens directly:
8 *
9 * out/presentation/presentation.json
10 * out/presentation/sources/<sha256>/step-<n>.mp4 + step-<n>.jpg
11 *
12 * A slide is one presenter step. A scene is split into its own steps (one
13 * per code patch, plus the `s.step(...)` marks in the scene file); each
14 * plays on one press of → and then holds.
15 *
16 * pnpm render # everything missing, reuse the rest
17 * pnpm render 01-api title # re-render these deck items
18 * pnpm render --deck 01-api-intro,01-api # a presentation of just these items
19 *
20 * Speaker notes come from `deck.ts` and the scene files; edits made in the
21 * presenter's notes panel are replaced on the next render.
22 */
23 import { bundle } from "@remotion/bundler";
24 import { renderMedia, renderStill, selectComposition } from "@remotion/renderer";
25 import { deck } from "./deck.ts";
26 import { schedule } from "./remotion/scene/schedule.ts";
27 import { introTimeline, type IntroJson } from "./shared/intro.ts";
28 import { VIDEO, type SceneCapture } from "./shared/types.ts";
29
30 const root = import.meta.dirname;
31 const captureDir = path.join(root, "out", "capture");
32 const clipsDir = path.join(root, "out", "clips");
33 const presentationDir = path.join(root, "out", "presentation");
34
35 const { values: args, positionals } = parseArgs({
36 allowPositionals: true,
37 options: { deck: { type: "string" } },
38 });
39 const rerender = new Set(positionals);
40 const subset = args.deck ? new Set(args.deck.split(",")) : undefined;
41 const items = subset ? deck.filter((item) => subset.has(item.id)) : deck;
42 if (items.length === 0) throw new Error(`no deck items match ${args.deck}`);
43
44 const exists = (file: string) =>
45 stat(file).then(
46 () => true,
47 () => false,
48 );
49
50 interface Part {
51 title: string;
52 notes: string;
53 /** Frames `[from, to)` of the composition. */
54 from: number;
55 to: number;
56 clip: string;
57 poster: string;
58 }
59
60 /** The presenter steps of one deck item. */
61 const partsOf = async (item: (typeof deck)[number], durationInFrames: number): Promise<Part[]> => {
62 const file = (k: number, ext: string) => path.join(clipsDir, `${item.id}.${k}.${ext}`);
63 if (item.kind === "intro") {
64 const intro = JSON.parse(
65 await readFile(path.join(captureDir, "intro", "intro.json"), "utf8"),
66 ) as IntroJson;
67 return introTimeline(intro.steps).map((range, k) => ({
68 title: intro.steps[k]!.title,
69 notes: intro.steps[k]!.notes,
70 ...range,
71 clip: file(k, "mp4"),
72 poster: file(k, "jpg"),
73 }));
74 }
75 if (item.kind === "slide") {
76 return [
77 {
78 title: item.title,
79 notes: item.notes,
80 from: 0,
81 to: durationInFrames,
82 clip: file(0, "mp4"),
83 poster: file(0, "jpg"),
84 },
85 ];
86 }
87 const capture = JSON.parse(
88 await readFile(path.join(captureDir, item.id, "scene.json"), "utf8"),
89 ) as SceneCapture;
90 const plan = await schedule(capture, VIDEO.fps);
91 return plan.steps.map((step, k) => ({
92 title: step.title,
93 notes: step.notes,
94 from: step.from,
95 to: step.to,
96 clip: file(k, "mp4"),
97 poster: file(k, "jpg"),
98 }));
99 };
100
101 console.log("● bundling the Remotion project");
102 const serveUrl = await bundle({
103 entryPoint: path.join(root, "remotion", "index.ts"),
104 publicDir: captureDir,
105 });
106
107 await mkdir(clipsDir, { recursive: true });
108 const steps = [];
109 let start = 0;
110 for (const item of items) {
111 const composition = await selectComposition({ serveUrl, id: item.id, inputProps: {} });
112 const parts = await partsOf(item, composition.durationInFrames);
113 const missing = (await Promise.all(parts.map((p) => exists(p.clip)))).includes(false);
114 if (rerender.has(item.id) || missing) {
115 console.log(
116 `● rendering ${item.id}: ${parts.length} step(s), ${(composition.durationInFrames / VIDEO.fps).toFixed(1)}s`,
117 );
118 for (const part of parts) {
119 await renderMedia({
120 composition,
121 serveUrl,
122 codec: "h264",
123 crf: 18,
124 imageFormat: "jpeg",
125 jpegQuality: 95,
126 frameRange: [part.from, part.to - 1],
127 outputLocation: part.clip,
128 inputProps: composition.props,
129 });
130 await renderStill({
131 composition,
132 serveUrl,
133 frame: part.from,
134 imageFormat: "jpeg",
135 jpegQuality: 90,
136 output: part.poster,
137 inputProps: composition.props,
138 });
139 console.log(` ✔ ${part.title}`);
140 }
141 } else {
142 console.log(`● reusing ${item.id}`);
143 }
144 for (const part of parts) {
145 const duration = (part.to - part.from) / VIDEO.fps;
146 steps.push({
147 id: `${item.id}.${steps.length}`,
148 title: part.title,
149 notes: part.notes,
150 start,
151 end: start + duration,
152 source: part,
153 });
154 start += duration;
155 }
156 }
157
158 // The workspace id fingerprints the clips, so the presenter never serves stale media.
159 const hash = createHash("sha256");
160 for (const step of steps) {
161 hash.update(await readFile(step.source.clip));
162 hash.update(await readFile(step.source.poster));
163 }
164 const id = hash.digest("hex");
165 const sources = path.join(presentationDir, "sources");
166 await rm(sources, { recursive: true, force: true });
167 await mkdir(path.join(sources, id), { recursive: true });
168 const manifestSteps = [];
169 for (const [index, step] of steps.entries()) {
170 const clip = `step-${index + 1}.mp4`;
171 const poster = `step-${index + 1}.jpg`;
172 await cp(step.source.clip, path.join(sources, id, clip));
173 await cp(step.source.poster, path.join(sources, id, poster));
174 const { source: _, ...rest } = step;
175 manifestSteps.push({ ...rest, clip, poster });
176 }
177 const manifest = {
178 version: 1,
179 id,
180 title: "Alchemy",
181 fps: VIDEO.fps,
182 width: VIDEO.width,
183 height: VIDEO.height,
184 duration: start,
185 steps: manifestSteps,
186 };
187 const temp = path.join(presentationDir, `presentation.${process.pid}.json`);
188 await writeFile(temp, `${JSON.stringify(manifest, null, 2)}\n`);
189 await rename(temp, path.join(presentationDir, "presentation.json"));
190 console.log(`✔ ${steps.length} steps, ${start.toFixed(1)}s → out/presentation (pnpm present)`);