-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrecord_animation.js
More file actions
345 lines (299 loc) · 11.8 KB
/
Copy pathrecord_animation.js
File metadata and controls
345 lines (299 loc) · 11.8 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env node
/**
* Records the chained Sankey animation to an MP4 video.
*
* Usage:
* node record_animation.js [options]
*
* Options:
* --fps N Frame rate (default: 30)
* --width N Viewport width (default: 1920)
* --height N Viewport height (default: 1080)
* --speed N Animation speed multiplier (default: 1)
* --output FILE Output file (default: animation.mp4)
* --duration N Max duration in seconds (default: 60)
* --quality N CRF quality 0-51, lower=better (default: 18)
*/
const puppeteer = require('puppeteer');
const path = require('path');
const fs = require('fs');
const { execSync, spawn } = require('child_process');
// Parse CLI args
const args = process.argv.slice(2);
function getArg(name, defaultVal) {
const idx = args.indexOf('--' + name);
return idx >= 0 && args[idx + 1] ? args[idx + 1] : defaultVal;
}
const FPS = parseInt(getArg('fps', '30'));
const WIDTH = parseInt(getArg('width', '1920'));
const HEIGHT = parseInt(getArg('height', '1080'));
const SPEED = parseFloat(getArg('speed', '1'));
const OUTPUT = getArg('output', 'animation.mp4');
const MAX_DURATION = parseInt(getArg('duration', '60'));
const QUALITY = parseInt(getArg('quality', '18'));
const FRAME_MS = 1000 / FPS;
const MAX_FRAMES = MAX_DURATION * FPS;
const FRAMES_DIR = path.join(__dirname, '.animation-frames');
async function main() {
// Clean up / create frames directory
if (fs.existsSync(FRAMES_DIR)) {
fs.rmSync(FRAMES_DIR, { recursive: true });
}
fs.mkdirSync(FRAMES_DIR);
console.log(`Recording: ${WIDTH}x${HEIGHT} @ ${FPS}fps, speed ${SPEED}x`);
console.log(`Max duration: ${MAX_DURATION}s (${MAX_FRAMES} frames)`);
console.log(`Output: ${OUTPUT}`);
// Start a local HTTP server for the site
const http = require('http');
const handler = require('fs');
const serverPath = path.join(__dirname, 'site');
const server = http.createServer((req, res) => {
let filePath = path.join(serverPath, req.url === '/' ? 'index.html' : req.url);
// Remove query string
filePath = filePath.split('?')[0];
const ext = path.extname(filePath).toLowerCase();
const mimeTypes = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.svg': 'image/svg+xml',
};
const contentType = mimeTypes[ext] || 'application/octet-stream';
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(404);
res.end('Not found: ' + filePath);
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);
}
});
});
await new Promise(resolve => server.listen(0, resolve));
const PORT = server.address().port;
console.log(`Local server on port ${PORT}`);
// Launch browser
const browser = await puppeteer.launch({
headless: 'new',
args: [
`--window-size=${WIDTH},${HEIGHT}`,
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--font-render-hinting=none',
],
defaultViewport: { width: WIDTH, height: HEIGHT, deviceScaleFactor: 1 },
});
const page = await browser.newPage();
// Inject time control BEFORE any scripts load
await page.evaluateOnNewDocument((frameMs, speed) => {
// Controlled time
window.__fakeTime = 0;
window.__frameMs = frameMs;
window.__animDone = false;
// Override performance.now
const origPerfNow = performance.now.bind(performance);
performance.now = () => window.__fakeTime;
// Override Date.now
Date.now = () => window.__fakeTime;
// Fake setTimeout/clearTimeout
let __nextTimerId = 1;
const __pendingTimers = new Map(); // id -> {fn, fireAt}
window.__origSetTimeout = window.setTimeout;
window.__origClearTimeout = window.clearTimeout;
window.setTimeout = (fn, delay = 0, ...args) => {
const id = __nextTimerId++;
__pendingTimers.set(id, {
fn: () => fn(...args),
fireAt: window.__fakeTime + delay
});
return id;
};
window.clearTimeout = (id) => {
__pendingTimers.delete(id);
};
// Fake setInterval/clearInterval
window.setInterval = (fn, interval, ...args) => {
const id = __nextTimerId++;
const fire = () => {
fn(...args);
if (__pendingTimers.has(id)) {
__pendingTimers.set(id, {
fn: fire,
fireAt: window.__fakeTime + interval
});
}
};
__pendingTimers.set(id, {
fn: fire,
fireAt: window.__fakeTime + interval
});
return id;
};
window.clearInterval = window.clearTimeout;
// Fake requestAnimationFrame
let __rafCallbacks = [];
let __nextRafId = 1;
window.requestAnimationFrame = (cb) => {
const id = __nextRafId++;
__rafCallbacks.push({ id, cb });
return id;
};
window.cancelAnimationFrame = (id) => {
__rafCallbacks = __rafCallbacks.filter(r => r.id !== id);
};
// Advance time by one frame — called from Puppeteer
window.__advanceFrame = () => {
window.__fakeTime += frameMs;
// Fire expired timers
const toFire = [];
for (const [id, timer] of __pendingTimers) {
if (timer.fireAt <= window.__fakeTime) {
toFire.push({ id, fn: timer.fn });
}
}
for (const { id, fn } of toFire) {
__pendingTimers.delete(id);
try { fn(); } catch(e) { console.error('Timer error:', e); }
}
// Fire requestAnimationFrame callbacks
const rafs = __rafCallbacks.slice();
__rafCallbacks = [];
for (const { cb } of rafs) {
try { cb(window.__fakeTime); } catch(e) { console.error('RAF error:', e); }
}
};
// Also override d3's internal timer if loaded later
window.__patchD3Timer = () => {
if (window.d3 && window.d3.now) {
window.d3.now = () => window.__fakeTime;
}
};
}, FRAME_MS, SPEED);
// Navigate to animation page
console.log('Loading animation page...');
await page.goto(`http://localhost:${PORT}/animation.html`, {
waitUntil: 'networkidle0',
timeout: 30000,
});
// Patch d3.now after d3 is loaded
await page.evaluate(() => {
window.__patchD3Timer();
// Also patch d3.timer's internal clock
if (window.d3 && d3.timerFlush) {
// d3-timer uses performance.now which we already overrode
// but we need to flush manually
}
});
// Wait for data to load and animation to initialize
// The animation starts automatically via `new ChainedSankeyAnimation()`
// We need to let the constructor run and data fetch complete
console.log('Waiting for animation to initialize...');
// Advance time in small steps to let fetch promises resolve
// and the animation class to construct
for (let i = 0; i < 30; i++) {
await page.evaluate(() => window.__advanceFrame());
await new Promise(r => setTimeout(r, 50)); // real-time wait for network
}
// Check if animation is ready
const ready = await page.evaluate(() => {
return document.getElementById('loading')?.style.display === 'none';
});
if (!ready) {
console.log('Waiting more for data load...');
for (let i = 0; i < 60; i++) {
await page.evaluate(() => window.__advanceFrame());
await new Promise(r => setTimeout(r, 100));
const ok = await page.evaluate(() =>
document.getElementById('loading')?.style.display === 'none'
);
if (ok) break;
}
}
// Hide UI controls for recording
await page.evaluate(() => {
const controls = document.getElementById('controls');
if (controls) controls.style.display = 'none';
const backLink = document.getElementById('back-link');
if (backLink) backLink.style.display = 'none';
});
console.log('Starting frame capture...');
let frameNum = 0;
let consecutiveIdleFrames = 0;
const startTime = Date.now();
for (frameNum = 0; frameNum < MAX_FRAMES; frameNum++) {
// Advance fake time by one frame
await page.evaluate(() => {
window.__advanceFrame();
// Also flush d3 timers
if (window.d3 && d3.timerFlush) {
try { d3.timerFlush(); } catch(e) {}
}
});
// Take screenshot
const framePath = path.join(FRAMES_DIR, `frame_${String(frameNum).padStart(5, '0')}.png`);
await page.screenshot({ path: framePath, type: 'png' });
// Progress indicator
if (frameNum % FPS === 0) {
const sec = frameNum / FPS;
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
process.stdout.write(`\rFrame ${frameNum} (${sec.toFixed(0)}s animation / ${elapsed}s real)`);
}
// Check if animation is done (outro visible + some hold time)
const animState = await page.evaluate(() => {
const outro = document.getElementById('outro-overlay');
return {
outroVisible: outro?.classList.contains('visible'),
fakeTime: window.__fakeTime
};
});
if (animState.outroVisible) {
consecutiveIdleFrames++;
// Hold outro for 4 seconds then stop
if (consecutiveIdleFrames > FPS * 4) {
console.log(`\nOutro held for 4s, stopping at frame ${frameNum}`);
break;
}
} else {
consecutiveIdleFrames = 0;
}
}
const totalFrames = frameNum + 1;
console.log(`\nCaptured ${totalFrames} frames (${(totalFrames / FPS).toFixed(1)}s)`);
// Close browser & server
await browser.close();
server.close();
// Compile with ffmpeg
console.log('Compiling video with ffmpeg...');
const ffmpegArgs = [
'-y',
'-framerate', String(FPS),
'-i', path.join(FRAMES_DIR, 'frame_%05d.png'),
'-c:v', 'libx264',
'-preset', 'slow',
'-crf', String(QUALITY),
'-pix_fmt', 'yuv420p',
'-movflags', '+faststart',
OUTPUT
];
console.log(`ffmpeg ${ffmpegArgs.join(' ')}`);
const ffmpeg = spawn('ffmpeg', ffmpegArgs, { stdio: 'inherit' });
await new Promise((resolve, reject) => {
ffmpeg.on('close', code => {
if (code === 0) resolve();
else reject(new Error(`ffmpeg exited with code ${code}`));
});
});
// Clean up frames
console.log('Cleaning up frames...');
fs.rmSync(FRAMES_DIR, { recursive: true });
const stats = fs.statSync(OUTPUT);
console.log(`\nDone! ${OUTPUT} (${(stats.size / 1024 / 1024).toFixed(1)} MB)`);
}
main().catch(err => {
console.error('Error:', err);
process.exit(1);
});