dsh-drawai 0.4.0 → 0.4.2

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.
Files changed (45) hide show
  1. package/LICENSE +430 -418
  2. package/README.md +96 -249
  3. package/lib/audio-media.js +5 -2
  4. package/lib/client.js +45 -12
  5. package/lib/drawing-runner.js +6 -4
  6. package/lib/ffmpeg-manager.js +1056 -0
  7. package/lib/index.js +7719 -7396
  8. package/lib/motion/apply.js +4 -3
  9. package/lib/motion/asset-pack.js +394 -335
  10. package/lib/motion/assets.js +4 -3
  11. package/lib/motion/beats.js +3 -2
  12. package/lib/motion/collect.js +3 -1
  13. package/lib/motion/delivery.js +29 -1
  14. package/lib/motion/demo-assets-lock.js +10 -0
  15. package/lib/motion/ffmpeg.js +5 -4
  16. package/lib/motion/jobs.js +5 -1
  17. package/lib/motion/mgstyles/vendor/README.md +28 -0
  18. package/lib/motion/mgstyles/vendor/modules/d3/LICENSE +13 -0
  19. package/lib/motion/mgstyles/vendor/modules/d3/dist/d3.min.js +2 -0
  20. package/lib/motion/mgstyles/vendor/modules/opentype.js/LICENSE +20 -0
  21. package/lib/motion/mgstyles/vendor/modules/opentype.js/dist/opentype.mjs +14460 -0
  22. package/lib/motion/mgstyles/vendor/modules/simplex-noise/LICENSE +21 -0
  23. package/lib/motion/mgstyles/vendor/modules/simplex-noise/dist/esm/simplex-noise.js +466 -0
  24. package/lib/motion/mgstyles/vendor/modules/topojson-client/LICENSE +13 -0
  25. package/lib/motion/mgstyles/vendor/modules/topojson-client/dist/topojson-client.min.js +2 -0
  26. package/lib/motion/preview.js +13 -4
  27. package/lib/motion/render.js +179 -159
  28. package/lib/motion/runtime/package-lock.json +3058 -0
  29. package/lib/motion/runtime/package.json +16 -0
  30. package/lib/motion/style-frames.js +306 -268
  31. package/lib/motion/sync.js +214 -214
  32. package/lib/motion/video-runtime.js +192 -0
  33. package/lib/motion-capture.js +2 -1
  34. package/lib/motion-film-review.js +2 -2
  35. package/lib/motion-project.js +105 -79
  36. package/lib/motion-render-worker.js +174 -144
  37. package/lib/motion-vision.js +3 -2
  38. package/lib/routing/router.js +222 -211
  39. package/lib/routing/vendor/README.md +96 -96
  40. package/lib/routing/vendor/libavoid-routing.js +753 -753
  41. package/lib/routing/vendor/libavoid.min.js +59 -59
  42. package/lib/shotcard-reference.js +2 -2
  43. package/lib/style-kernel.js +16 -6
  44. package/lib/video-export.js +32 -20
  45. package/package.json +17 -12
@@ -1,268 +1,306 @@
1
- /*
2
- * 取「整片风格」上游 demo 的参考帧。
3
- *
4
- * brief 能写清色板与判据,但写不清动效:转场怎么擦、冲击有没有环、相机有没有抖、
5
- * 颗粒是逐像素还是几百个点 —— 这些只有看帧才知道。这里用上游自带的服务端 + Chrome
6
- * 打开 demos/<slug>/index.html,按 cues.json 的时间点调 renderAt(t) 截帧,交给模型当
7
- * 对照物。缺字体/纸纹时用占位资源兜底,并记在 notes 里,不假装素材齐全。
8
- */
9
- import {openBrowser} from '@remotion/renderer'
10
- import {existsSync,mkdirSync,readFileSync,statSync,writeFileSync} from 'node:fs'
11
- import {createServer} from 'node:http'
12
- import {extname,join,normalize,resolve,sep} from 'node:path'
13
- import {tmpdir} from 'node:os'
14
- import {fileURLToPath} from 'node:url'
15
- import {fontsDir, mediaLibraryRoot} from './asset-pack.js'
16
- import {fontFaces} from './fonts.js'
17
-
18
- const UPSTREAM = fileURLToPath(new URL('./mgstyles/upstream', import.meta.url))
19
- const DEMO_FONT_ALIASES = {
20
- '/demos/03-isometric/assets/fonts/Unbounded-800-static.ttf': 'Unbounded',
21
- '/demos/07-liquid/assets/Fraunces-Liquid-72-900-Soft.ttf': 'Fraunces',
22
- '/demos/08-morph/assets/fonts/Outfit-SemiBold.ttf': 'Outfit',
23
- '/demos/10-synthwave/assets/fonts/MrDafoe-Regular.ttf': 'Mr Dafoe',
24
- }
25
- // 上游 demo 的 importmap 指向 /node_modules/three/...、/node_modules/opentype.js/...,
26
- // 但这些包只在插件根目录的 node_modules 里。旧版只服务 upstream 目录,于是模块 404、
27
- // window.__ready 永不置真,最后报了句"没有在 120 秒内就绪"——错误信息指向了完全错误的地方。
28
- const MODULES = fileURLToPath(new URL('../../node_modules', import.meta.url))
29
-
30
- const MIME = {
31
- '.html': 'text/html; charset=utf-8', '.js': 'text/javascript', '.mjs': 'text/javascript',
32
- '.css': 'text/css', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
33
- '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.svg': 'image/svg+xml', '.ttf': 'font/ttf',
34
- '.otf': 'font/otf', '.woff': 'font/woff', '.woff2': 'font/woff2', '.wav': 'audio/wav',
35
- '.mp3': 'audio/mpeg', '.mp4': 'video/mp4', '.wasm': 'application/wasm', '.glb': 'model/gltf-binary',
36
- }
37
-
38
- // 1×1 透明 PNG:纸纹/贴图没 vendored 时先让页面能 decode 通过,画面里那块会是平的,notes 会写明。
39
- const PLACEHOLDER_PNG = Buffer.from(
40
- 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
41
- 'base64',
42
- )
43
-
44
- // 字体没 vendored 时用系统字体顶上:document.fonts.load() 才不会 reject(字形本来就从 glyphs.json 来,
45
- // 参考帧里字的形状可能略有出入,但不会整页 init 失败)。
46
- const SYSTEM_FONT = [
47
- ...(fontsDir() ? [join(fontsDir(), 'NotoSansSC-VF.ttf')] : []),
48
- 'C:/Windows/Fonts/segoeui.ttf',
49
- 'C:/Windows/Fonts/arial.ttf',
50
- 'C:/Windows/Fonts/msyh.ttc',
51
- ].find((path) => existsSync(path))
52
-
53
- /** 素材库贴图目录:素材包没装好时返回 null。 */
54
- function texturesDir() {
55
- const root = mediaLibraryRoot()
56
- return root ? join(root, 'assets', 'textures') : null
57
- }
58
-
59
- function flattenNumbers(value, out) {
60
- if (typeof value === 'number' && Number.isFinite(value)) out.push(value)
61
- else if (Array.isArray(value)) for (const item of value) flattenNumbers(item, out)
62
- else if (value && typeof value === 'object') for (const item of Object.values(value)) flattenNumbers(item, out)
63
- }
64
-
65
- function cueTimes(slug, duration) {
66
- const file = join(UPSTREAM, 'demos', slug, 'cues.json')
67
- if (!existsSync(file)) return []
68
- try {
69
- const raw = JSON.parse(readFileSync(file, 'utf8'))
70
- const out = []
71
- // bpm/fps 是参数不是时间点;loop 是几何;它们混进来会截到片尾之外的空帧。
72
- for (const [key, value] of Object.entries(raw)) {
73
- if (/^(bpm|fps|loop)$/i.test(key)) continue
74
- flattenNumbers(value, out)
75
- }
76
- const limit = Number.isFinite(duration) && duration > 0 ? duration - 0.04 : 60
77
- return [...new Set(out.filter((t) => t >= 0.05 && t <= limit).map((t) => Math.round(t * 100) / 100))].sort((a, b) => a - b)
78
- } catch {
79
- return []
80
- }
81
- }
82
-
83
- function pickTimes(slug, count, explicit, duration) {
84
- if (Array.isArray(explicit) && explicit.length) {
85
- const limit = Number.isFinite(duration) && duration > 0 ? duration : Infinity
86
- return explicit.map(Number).filter((t) => Number.isFinite(t) && t >= 0 && t <= limit).slice(0, 24)
87
- }
88
- const cues = cueTimes(slug, duration)
89
- const wanted = Math.max(1, Math.min(24, Math.floor(Number(count) || 8)))
90
- if (cues.length >= wanted) {
91
- // 均匀覆盖全片,同时尽量落在 cue 上(动作发生的那几帧信息量最大)
92
- const min = cues[0], max = cues[cues.length - 1]
93
- return Array.from({ length: wanted }, (_, i) => {
94
- const target = min + ((max - min) * i) / Math.max(1, wanted - 1)
95
- return cues.reduce((best, t) => (Math.abs(t - target) < Math.abs(best - target) ? t : best), cues[0])
96
- })
97
- }
98
- const max = Number.isFinite(duration) && duration > 0 ? duration : 10
99
- return Array.from({ length: wanted }, (_, i) => Math.round(((max * i) / Math.max(1, wanted - 1)) * 1000) / 1000)
100
- }
101
-
102
- // demo 的 importmap 指向 /node_modules/*,只有真装在本机的包才服务得出来。
103
- // 缺包时页面会静默卡在模块加载阶段,等到超时也只会说"没就绪"—— 这里提前查,几十毫秒就报清缺什么。
104
- function missingModules(slug) {
105
- const html = join(UPSTREAM, 'demos', slug, 'index.html')
106
- let text = ''
107
- try { text = readFileSync(html, 'utf8') } catch { return [] }
108
- const refs = [...text.matchAll(/["'](\/node_modules\/[^"']+)["']/g)].map((match) => match[1])
109
- return [...new Set(refs)].filter((ref) => !existsSync(join(MODULES, ref.slice('/node_modules/'.length))))
110
- }
111
-
112
- function startStaticServer(misses) {
113
- const roots = {vendor: resolve(MODULES), upstream: resolve(UPSTREAM)}
114
- return new Promise((done) => {
115
- const server = createServer((req, res) => {
116
- try {
117
- const pathname = decodeURIComponent(new URL(req.url, 'http://x').pathname)
118
- if (pathname === '/assets/fonts/fonts.css') {
119
- misses.push('/assets/fonts/fonts.css(使用 DrawAI 内置字体,部分上游字体或字重可能不同)')
120
- res.writeHead(200, {'Content-Type': 'text/css; charset=utf-8'})
121
- return res.end(fontFaces().map(face => '@font-face{font-family:' + JSON.stringify(face.family) + ';src:url(' + JSON.stringify('/__drawai-fonts/' + face.file) + ');font-weight:' + face.weight + ';font-style:' + face.style + ';font-display:block}').join('\n'))
122
- }
123
- if (pathname.startsWith('/__drawai-fonts/') && fontFaces().some(face => pathname === '/__drawai-fonts/' + face.file)) {
124
- const file = join(fontsDir() || '', pathname.split('/').at(-1))
125
- if (!existsSync(file)) { res.writeHead(404); return res.end('missing bundled font') }
126
- res.writeHead(200, {'Content-Type': 'font/ttf'})
127
- return res.end(readFileSync(file))
128
- }
129
- if (DEMO_FONT_ALIASES[pathname]) {
130
- const face = fontFaces().find(item => item.family === DEMO_FONT_ALIASES[pathname] && item.style === 'normal')
131
- if (face && existsSync(join(fontsDir() || '', face.file))) {
132
- if (face.weight.includes(' ')) misses.push(pathname + '(使用同字体族可变字体,字重或轴值可能不同)')
133
- res.writeHead(200, {'Content-Type': 'font/ttf'})
134
- return res.end(readFileSync(join(fontsDir() || '', face.file)))
135
- }
136
- }
137
- const vendor = pathname.startsWith('/node_modules/')
138
- // 两条挂载点:/node_modules/* → 插件 node_modules;其余 → upstream。各自限制在自己目录内。
139
- const root = vendor ? roots.vendor : roots.upstream
140
- const relative = vendor ? pathname.slice('/node_modules'.length) : pathname
141
- let file = resolve(join(root, normalize(relative)))
142
- if (file !== root && !file.startsWith(root + sep)) { res.writeHead(403); return res.end('403') }
143
- if (existsSync(file) && statSync(file).isDirectory()) file = join(file, 'index.html')
144
- // Shared upstream textures are packaged separately from the text-only demo tree.
145
- if (!vendor && !existsSync(file) && pathname.startsWith('/assets/textures/')) {
146
- const tdir = texturesDir()
147
- const texture = tdir ? resolve(tdir, pathname.slice('/assets/textures/'.length)) : ''
148
- if (tdir && texture.startsWith(resolve(tdir) + sep) && existsSync(texture)) file = texture
149
- }
150
- if (!existsSync(file)) {
151
- misses.push(pathname)
152
- // 模块缺失不能给占位:给个假的 three.module.js 只会让页面以更难懂的方式失败。
153
- if (vendor) { res.writeHead(404); return res.end('404 ' + pathname) }
154
- // 字体/纸纹/贴图没 vendor:CSS 给空表,图片给 1px 占位,其余 404(页面会走 fallback)
155
- if (/\.css$/i.test(pathname)) { res.writeHead(200, {'Content-Type': 'text/css'}); return res.end('/* placeholder */') }
156
- if (/\.(png|jpe?g|webp)$/i.test(pathname)) { res.writeHead(200, {'Content-Type': MIME[extname(file).toLowerCase()] || 'image/png'}); return res.end(PLACEHOLDER_PNG) }
157
- if (/\.(ttf|otf|woff2?)$/i.test(pathname) && SYSTEM_FONT) {
158
- res.writeHead(200, {'Content-Type': 'font/ttf'})
159
- return res.end(readFileSync(SYSTEM_FONT))
160
- }
161
- res.writeHead(404); return res.end('404 ' + pathname)
162
- }
163
- res.writeHead(200, {'Content-Type': MIME[extname(file).toLowerCase()] || 'application/octet-stream', 'Cache-Control': 'no-store'})
164
- res.end(readFileSync(file))
165
- } catch (error) {
166
- res.writeHead(500); res.end(String(error))
167
- }
168
- })
169
- server.listen(0, '127.0.0.1', () => done(server))
170
- })
171
- }
172
-
173
- function findChrome(browserExecutable) {
174
- return [browserExecutable, process.env.DRAWAI_CHROME_PATH,
175
- 'C:/Program Files/Google/Chrome/Application/chrome.exe',
176
- 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe',
177
- 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe'].find((path) => path && existsSync(path))
178
- }
179
-
180
- export async function captureStyleFrames({slug, times: explicitTimes, count = 8, browserExecutable, directory, signal} = {}) {
181
- if (typeof slug !== 'string' || !/^[a-z0-9][a-z0-9-]*$/i.test(slug)) throw new Error('slug 无效:' + slug)
182
- const demo = join(UPSTREAM, 'demos', slug)
183
- if (!existsSync(join(demo, 'index.html'))) throw new Error('这个风格没有可截帧的上游 demo:' + slug + '(只有说明、没有 index.html)')
184
- const missing = missingModules(slug)
185
- if (missing.length) {
186
- throw new Error('这个 demo 的依赖没装在本机(' + missing.join(', ') + '),页面加载不到模块,截不了帧。'
187
- + '三条路:① 改用 action:"source" 读 demos/' + slug + '/ 下的源码与 cues.json 反推动效(多数情况够用);'
188
- + '② 在插件目录跑 `npm i --no-save opentype.js simplex-noise topojson-client d3` 后重试(这几个包只为截参考帧,不进 package.json);'
189
- + '③ 换一个不依赖它们的 demo。')
190
- }
191
-
192
- const misses = []
193
- const server = await startStaticServer(misses)
194
- const port = server.address().port
195
- const executable = findChrome(browserExecutable)
196
- signal?.throwIfAborted()
197
- const browser = await openBrowser('chrome', {browserExecutable: executable, logLevel: 'error'})
198
- try {
199
- const page = await browser.newPage({context: () => null, logLevel: 'error', indent: false, pageIndex: 0, onBrowserLog: null, onLog: () => {}})
200
- await page.setViewport({width: 1920, height: 1080, deviceScaleFactor: 1})
201
- await page.goto({url: 'http://127.0.0.1:' + port + '/demos/' + slug + '/index.html', timeout: 60000})
202
- // 就绪等待 30 秒:实测最慢的能截的 demo 8.4 秒就绪;真出问题的页面不会在第 31 秒突然变好。
203
- // 超时要把"缺了哪些资源"一起报出来,并分清是缺 npm 包还是上游素材没 vendored——两者的处理办法不一样。
204
- try {
205
- await page.evaluate(() => new Promise((resolve, reject) => {
206
- const started = Date.now()
207
- const tick = () => {
208
- if (window.__ready === true) return resolve(true)
209
- if (Date.now() - started > 30000) return reject(new Error('__timeout__'))
210
- setTimeout(tick, 100)
211
- }
212
- tick()
213
- }))
214
- } catch {
215
- const lost = [...new Set(misses)]
216
- const modules = lost.filter((path) => path.startsWith('/node_modules/')).slice(0, 4)
217
- const assets = lost.filter((path) => !path.startsWith('/node_modules/') && !/favicon|fonts\.css/.test(path)).slice(0, 4)
218
- throw new Error('上游 demo 没有就绪(30 秒)'
219
- + (modules.length ? ';缺 npm 包:' + modules.join(', ') : '')
220
- + (assets.length ? ';缺上游素材:' + assets.join(', ') + '(这些没有 vendored,不是配置问题)' : '')
221
- + (modules.length || assets.length ? '' : ';模块可能抛错')
222
- + '。改用 action:"source" 读 demos/' + slug + '/ 下的源码与 cues.json 反推动效,通常比等截帧更省事。')
223
- }
224
- const meta = await page.evaluate(() => ({
225
- width: window.DEMO?.width || 1920,
226
- height: window.DEMO?.height || 1080,
227
- fps: window.DEMO?.fps || 30,
228
- duration: window.DEMO?.duration || 10,
229
- hasRenderAt: typeof window.renderAt === 'function',
230
- }))
231
- if (!meta.hasRenderAt) throw new Error('这个 demo 没有 window.renderAt(t),无法按时间取帧;改用 action:"source" 读源码')
232
- const width = Math.max(320, Math.min(2560, Math.round(meta.width)))
233
- const height = Math.max(240, Math.min(1440, Math.round(meta.height)))
234
- const fps = Math.max(1, Math.round(meta.fps || 30))
235
- await page.setViewport({width, height, deviceScaleFactor: 1})
236
- const times = pickTimes(slug, count, explicitTimes, Number(meta.duration) || 10)
237
- if (!times.length) throw new Error('没有可用的时间点(cues.json 里的时间都超出片长)')
238
-
239
- const dir = directory || join(tmpdir(), 'dsh-drawai', 'style-frames', slug)
240
- mkdirSync(dir, {recursive: true})
241
- const files = []
242
- for (const [index, t] of times.entries()) {
243
- signal?.throwIfAborted()
244
- await page.evaluate(async (time, fpsValue) => {
245
- await window.renderAt?.(time)
246
- // 两帧 rAF:让画面里基于 frame 的绘制与 CSS/WAAPI 都落到同一时刻
247
- await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(resolve, 30))))
248
- void fpsValue
249
- }, t, fps)
250
- const result = await page._client().send('Page.captureScreenshot', {
251
- format: 'png', captureBeyondViewport: false,
252
- clip: {x: 0, y: 0, width, height, scale: 1},
253
- })
254
- const data = Buffer.from((result.value || result).data, 'base64')
255
- if (data.length > 12 * 1024 * 1024) throw new Error('参考帧体积过大:' + t + 's')
256
- const file = join(dir, 'frame-' + String(index).padStart(2, '0') + '-' + t.toFixed(2) + 's.png')
257
- writeFileSync(file, data)
258
- files.push({time: t, frame: Math.round(t * fps), path: file, bytes: data.length})
259
- }
260
- const notes = []
261
- if (misses.length) notes.push('缺上游资源(已用占位兜底,画面里的字体/纸纹可能与原片不同):' + [...new Set(misses)].slice(0, 8).join(', '))
262
- notes.push('这些是参考帧:写场景前逐张看,按 STYLE.md 的效果配方逐项对照(转场/冲击/相机/颗粒/二次运动),别只对"形"。')
263
- return {slug, width, height, fps, duration: meta.duration, times, directory: dir, files, notes}
264
- } finally {
265
- await browser.close({silent: true}).catch(() => {})
266
- await new Promise((done) => server.close(done))
267
- }
268
- }
1
+ /*
2
+ * 取「整片风格」上游 demo 的参考帧。
3
+ *
4
+ * brief 能写清色板与判据,但写不清动效:转场怎么擦、冲击有没有环、相机有没有抖、
5
+ * 颗粒是逐像素还是几百个点 —— 这些只有看帧才知道。这里用上游自带的服务端 + Chrome
6
+ * 打开 demos/<slug>/index.html,按 cues.json 的时间点调 renderAt(t) 截帧,交给模型当
7
+ * 对照物。缺字体/纸纹时用占位资源兜底,并记在 notes 里,不假装素材齐全。
8
+ */
9
+ import { loadVideoModule, ensureVideoRuntime } from './video-runtime.js'
10
+ import {existsSync,mkdirSync,readFileSync,statSync,writeFileSync} from 'node:fs'
11
+ import {createServer} from 'node:http'
12
+ import {extname,join,normalize,resolve,sep} from 'node:path'
13
+ import {tmpdir} from 'node:os'
14
+ import {fileURLToPath} from 'node:url'
15
+ import {demoAssetsFile, ensureDemoAssets, fontsDir, mediaLibraryRoot} from './asset-pack.js'
16
+ import {fontFaces} from './fonts.js'
17
+
18
+ const UPSTREAM = fileURLToPath(new URL('./mgstyles/upstream', import.meta.url))
19
+ // 上游 demo 的 importmap 指向 /node_modules/*:three 由视频运行时提供,d3 / topojson-client /
20
+ // opentype.js / simplex-noise 不在运行时依赖里,随插件一起 vendored(见 mgstyles/vendor/README.md)。
21
+ // 目录叫 modules 而不是 node_modules:npm 打包会无条件跳过任何 node_modules 目录。
22
+ const VENDOR_MODULES = fileURLToPath(new URL('./mgstyles/vendor/modules', import.meta.url))
23
+ const DEMO_FONT_ALIASES = {
24
+ '/demos/03-isometric/assets/fonts/Unbounded-800-static.ttf': 'Unbounded',
25
+ '/demos/07-liquid/assets/Fraunces-Liquid-72-900-Soft.ttf': 'Fraunces',
26
+ '/demos/08-morph/assets/fonts/Outfit-SemiBold.ttf': 'Outfit',
27
+ '/demos/10-synthwave/assets/fonts/MrDafoe-Regular.ttf': 'Mr Dafoe',
28
+ }
29
+ // 上游 demo 自带的数据文件(如 22-hud 的 land-50m.json)不在仓库里:随包的只有代码,
30
+ // 数据单独发布到 Release(约 530KB),第一次请求时下载一次并缓存到 ~/.dsh/drawai-assets/。
31
+ // 只对 .json 兜这一层:贴图/字体缺了照旧走占位,别为了缺一张图去拉包(issue #27 的取舍)。
32
+ const DEMO_DATA_PATTERN = /^\/demos\/[a-z0-9][a-z0-9-]*\/assets\/[^/]+\.json$/i
33
+
34
+ async function demoDataFile(pathname) {
35
+ const rel = pathname.slice(1)
36
+ const have = demoAssetsFile(rel)
37
+ if (have) return have
38
+ try {
39
+ await ensureDemoAssets()
40
+ } catch {
41
+ return null
42
+ }
43
+ return demoAssetsFile(rel)
44
+ }
45
+ // 上游 demo 的 importmap 指向 /node_modules/three/...、/node_modules/opentype.js/...,
46
+ // 这些包不在 upstream 目录里,得单独挂载。旧版只服务 upstream 目录,于是模块 404、
47
+ // window.__ready 永不置真,最后报了句"没有在 120 秒内就绪"——错误信息指向了完全错误的地方。
48
+ // 现在 /node_modules/* 先查视频运行时,再回落到随包 vendored 的那一份(issue #27)。
49
+
50
+
51
+ const MIME = {
52
+ '.html': 'text/html; charset=utf-8', '.js': 'text/javascript', '.mjs': 'text/javascript',
53
+ '.css': 'text/css', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
54
+ '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.svg': 'image/svg+xml', '.ttf': 'font/ttf',
55
+ '.otf': 'font/otf', '.woff': 'font/woff', '.woff2': 'font/woff2', '.wav': 'audio/wav',
56
+ '.mp3': 'audio/mpeg', '.mp4': 'video/mp4', '.wasm': 'application/wasm', '.glb': 'model/gltf-binary',
57
+ }
58
+
59
+ // 1×1 透明 PNG:纸纹/贴图没 vendored 时先让页面能 decode 通过,画面里那块会是平的,notes 会写明。
60
+ const PLACEHOLDER_PNG = Buffer.from(
61
+ 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
62
+ 'base64',
63
+ )
64
+
65
+ // 字体没 vendored 时用系统字体顶上:document.fonts.load() 才不会 reject(字形本来就从 glyphs.json 来,
66
+ // 参考帧里字的形状可能略有出入,但不会整页 init 失败)。
67
+ const SYSTEM_FONT = [
68
+ ...(fontsDir() ? [join(fontsDir(), 'NotoSansSC-VF.ttf')] : []),
69
+ 'C:/Windows/Fonts/segoeui.ttf',
70
+ 'C:/Windows/Fonts/arial.ttf',
71
+ 'C:/Windows/Fonts/msyh.ttc',
72
+ ].find((path) => existsSync(path))
73
+
74
+ /** 素材库贴图目录:素材包没装好时返回 null。 */
75
+ function texturesDir() {
76
+ const root = mediaLibraryRoot()
77
+ return root ? join(root, 'assets', 'textures') : null
78
+ }
79
+
80
+ function flattenNumbers(value, out) {
81
+ if (typeof value === 'number' && Number.isFinite(value)) out.push(value)
82
+ else if (Array.isArray(value)) for (const item of value) flattenNumbers(item, out)
83
+ else if (value && typeof value === 'object') for (const item of Object.values(value)) flattenNumbers(item, out)
84
+ }
85
+
86
+ function cueTimes(slug, duration) {
87
+ const file = join(UPSTREAM, 'demos', slug, 'cues.json')
88
+ if (!existsSync(file)) return []
89
+ try {
90
+ const raw = JSON.parse(readFileSync(file, 'utf8'))
91
+ const out = []
92
+ // bpm/fps 是参数不是时间点;loop 是几何;它们混进来会截到片尾之外的空帧。
93
+ for (const [key, value] of Object.entries(raw)) {
94
+ if (/^(bpm|fps|loop)$/i.test(key)) continue
95
+ flattenNumbers(value, out)
96
+ }
97
+ const limit = Number.isFinite(duration) && duration > 0 ? duration - 0.04 : 60
98
+ return [...new Set(out.filter((t) => t >= 0.05 && t <= limit).map((t) => Math.round(t * 100) / 100))].sort((a, b) => a - b)
99
+ } catch {
100
+ return []
101
+ }
102
+ }
103
+
104
+ function pickTimes(slug, count, explicit, duration) {
105
+ if (Array.isArray(explicit) && explicit.length) {
106
+ const limit = Number.isFinite(duration) && duration > 0 ? duration : Infinity
107
+ return explicit.map(Number).filter((t) => Number.isFinite(t) && t >= 0 && t <= limit).slice(0, 24)
108
+ }
109
+ const cues = cueTimes(slug, duration)
110
+ const wanted = Math.max(1, Math.min(24, Math.floor(Number(count) || 8)))
111
+ if (cues.length >= wanted) {
112
+ // 均匀覆盖全片,同时尽量落在 cue 上(动作发生的那几帧信息量最大)
113
+ const min = cues[0], max = cues[cues.length - 1]
114
+ return Array.from({ length: wanted }, (_, i) => {
115
+ const target = min + ((max - min) * i) / Math.max(1, wanted - 1)
116
+ return cues.reduce((best, t) => (Math.abs(t - target) < Math.abs(best - target) ? t : best), cues[0])
117
+ })
118
+ }
119
+ const max = Number.isFinite(duration) && duration > 0 ? duration : 10
120
+ return Array.from({ length: wanted }, (_, i) => Math.round(((max * i) / Math.max(1, wanted - 1)) * 1000) / 1000)
121
+ }
122
+
123
+ // demo 的 importmap 指向 /node_modules/*:运行时里有的(three)直接用,没有的回落到随包 vendored 的副本。
124
+ // 两边都没有说明打包缺文件,提前查出来,几十毫秒就报清缺什么,不必等页面静默卡到超时。
125
+ function missingModules(slug, modules) {
126
+ const html = join(UPSTREAM, 'demos', slug, 'index.html')
127
+ let text = ''
128
+ try { text = readFileSync(html, 'utf8') } catch { return [] }
129
+ const refs = [...text.matchAll(/["'](\/node_modules\/[^"']+)["']/g)].map((match) => match[1])
130
+ return [...new Set(refs)].filter((ref) => {
131
+ const relative = ref.slice('/node_modules/'.length)
132
+ return !existsSync(join(modules, relative)) && !existsSync(join(VENDOR_MODULES, relative))
133
+ })
134
+ }
135
+
136
+ function startStaticServer(misses, modules) {
137
+ const roots = {vendor: resolve(modules), bundled: resolve(VENDOR_MODULES), upstream: resolve(UPSTREAM)}
138
+ return new Promise((done) => {
139
+ const server = createServer(async (req, res) => {
140
+ try {
141
+ const pathname = decodeURIComponent(new URL(req.url, 'http://x').pathname)
142
+ if (pathname === '/assets/fonts/fonts.css') {
143
+ misses.push('/assets/fonts/fonts.css(使用 DrawAI 内置字体,部分上游字体或字重可能不同)')
144
+ res.writeHead(200, {'Content-Type': 'text/css; charset=utf-8'})
145
+ return res.end(fontFaces().map(face => '@font-face{font-family:' + JSON.stringify(face.family) + ';src:url(' + JSON.stringify('/__drawai-fonts/' + face.file) + ');font-weight:' + face.weight + ';font-style:' + face.style + ';font-display:block}').join('\n'))
146
+ }
147
+ if (pathname.startsWith('/__drawai-fonts/') && fontFaces().some(face => pathname === '/__drawai-fonts/' + face.file)) {
148
+ const file = join(fontsDir() || '', pathname.split('/').at(-1))
149
+ if (!existsSync(file)) { res.writeHead(404); return res.end('missing bundled font') }
150
+ res.writeHead(200, {'Content-Type': 'font/ttf'})
151
+ return res.end(readFileSync(file))
152
+ }
153
+ if (DEMO_FONT_ALIASES[pathname]) {
154
+ const face = fontFaces().find(item => item.family === DEMO_FONT_ALIASES[pathname] && item.style === 'normal')
155
+ if (face && existsSync(join(fontsDir() || '', face.file))) {
156
+ if (face.weight.includes(' ')) misses.push(pathname + '(使用同字体族可变字体,字重或轴值可能不同)')
157
+ res.writeHead(200, {'Content-Type': 'font/ttf'})
158
+ return res.end(readFileSync(join(fontsDir() || '', face.file)))
159
+ }
160
+ }
161
+ const vendor = pathname.startsWith('/node_modules/')
162
+ // 两条挂载点:/node_modules/* → 视频运行时(缺则随包 vendored 的副本);其余 → upstream。
163
+ // 各自限制在自己目录内。
164
+ const root = vendor ? roots.vendor : roots.upstream
165
+ const relative = vendor ? pathname.slice('/node_modules'.length) : pathname
166
+ let file = resolve(join(root, normalize(relative)))
167
+ if (file !== root && !file.startsWith(root + sep)) { res.writeHead(403); return res.end('403') }
168
+ if (vendor && !existsSync(file)) {
169
+ const bundled = resolve(join(roots.bundled, normalize(relative)))
170
+ if (bundled === roots.bundled || bundled.startsWith(roots.bundled + sep)) file = bundled
171
+ }
172
+ if (existsSync(file) && statSync(file).isDirectory()) file = join(file, 'index.html')
173
+ // Shared upstream textures are packaged separately from the text-only demo tree.
174
+ if (!vendor && !existsSync(file) && pathname.startsWith('/assets/textures/')) {
175
+ const tdir = texturesDir()
176
+ const texture = tdir ? resolve(tdir, pathname.slice('/assets/textures/'.length)) : ''
177
+ if (tdir && texture.startsWith(resolve(tdir) + sep) && existsSync(texture)) file = texture
178
+ }
179
+ // Demo 自带的数据文件(land-50m.json 之类)不在仓库里,走单独的小素材包。
180
+ if (!vendor && !existsSync(file) && DEMO_DATA_PATTERN.test(pathname)) {
181
+ const data = await demoDataFile(pathname)
182
+ if (data) file = data
183
+ }
184
+ if (!existsSync(file)) {
185
+ misses.push(pathname)
186
+ // 模块缺失不能给占位:给个假的 three.module.js 只会让页面以更难懂的方式失败。
187
+ if (vendor) { res.writeHead(404); return res.end('404 ' + pathname) }
188
+ // 字体/纸纹/贴图没 vendor:CSS 给空表,图片给 1px 占位,其余 404(页面会走 fallback)
189
+ if (/\.css$/i.test(pathname)) { res.writeHead(200, {'Content-Type': 'text/css'}); return res.end('/* placeholder */') }
190
+ if (/\.(png|jpe?g|webp)$/i.test(pathname)) { res.writeHead(200, {'Content-Type': MIME[extname(file).toLowerCase()] || 'image/png'}); return res.end(PLACEHOLDER_PNG) }
191
+ if (/\.(ttf|otf|woff2?)$/i.test(pathname) && SYSTEM_FONT) {
192
+ res.writeHead(200, {'Content-Type': 'font/ttf'})
193
+ return res.end(readFileSync(SYSTEM_FONT))
194
+ }
195
+ res.writeHead(404); return res.end('404 ' + pathname)
196
+ }
197
+ res.writeHead(200, {'Content-Type': MIME[extname(file).toLowerCase()] || 'application/octet-stream', 'Cache-Control': 'no-store'})
198
+ res.end(readFileSync(file))
199
+ } catch (error) {
200
+ res.writeHead(500); res.end(String(error))
201
+ }
202
+ })
203
+ server.listen(0, '127.0.0.1', () => done(server))
204
+ })
205
+ }
206
+
207
+ function findChrome(browserExecutable) {
208
+ return [browserExecutable, process.env.DRAWAI_CHROME_PATH,
209
+ 'C:/Program Files/Google/Chrome/Application/chrome.exe',
210
+ 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe',
211
+ 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe'].find((path) => path && existsSync(path))
212
+ }
213
+
214
+ export async function captureStyleFrames({slug, times: explicitTimes, count = 8, browserExecutable, directory, signal} = {}) {
215
+ if (typeof slug !== 'string' || !/^[a-z0-9][a-z0-9-]*$/i.test(slug)) throw new Error('slug 无效:' + slug)
216
+ const demo = join(UPSTREAM, 'demos', slug)
217
+ if (!existsSync(join(demo, 'index.html'))) throw new Error('这个风格没有可截帧的上游 demo:' + slug + '(只有说明、没有 index.html)')
218
+ const runtime = await ensureVideoRuntime({signal})
219
+ const modules = join(runtime, 'node_modules')
220
+ const { openBrowser } = await loadVideoModule('@remotion/renderer', {signal})
221
+ const missing = missingModules(slug, modules)
222
+ if (missing.length) {
223
+ throw new Error('这个 demo 的依赖没装在本机(' + missing.join(', ') + '),页面加载不到模块,截不了帧。'
224
+ + '这不是配置问题:three 由视频运行时提供,d3 / topojson-client / opentype.js / simplex-noise 随插件 vendored;'
225
+ + '缺这几个说明打包不完整,请把这条错误原样反馈给插件作者。'
226
+ + '临时退路只有 action:"source" 读 demos/' + slug + '/ 下的源码与 cues.json —— 那是有损降级,'
227
+ + '只有文字与时间点,看不到转场怎么擦、冲击有没有环、相机抖不抖、颗粒多粗。')
228
+ }
229
+
230
+ const misses = []
231
+ const server = await startStaticServer(misses, modules)
232
+ const port = server.address().port
233
+ const executable = findChrome(browserExecutable)
234
+ signal?.throwIfAborted()
235
+ const browser = await openBrowser('chrome', {browserExecutable: executable, logLevel: 'error'})
236
+ try {
237
+ const page = await browser.newPage({context: () => null, logLevel: 'error', indent: false, pageIndex: 0, onBrowserLog: null, onLog: () => {}})
238
+ await page.setViewport({width: 1920, height: 1080, deviceScaleFactor: 1})
239
+ await page.goto({url: 'http://127.0.0.1:' + port + '/demos/' + slug + '/index.html', timeout: 60000})
240
+ // 就绪等待 30 秒:实测最慢的能截的 demo 8.4 秒就绪;真出问题的页面不会在第 31 秒突然变好。
241
+ // 超时要把"缺了哪些资源"一起报出来,并分清是缺 npm 包还是上游素材没 vendored——两者的处理办法不一样。
242
+ try {
243
+ await page.evaluate(() => new Promise((resolve, reject) => {
244
+ const started = Date.now()
245
+ const tick = () => {
246
+ if (window.__ready === true) return resolve(true)
247
+ if (Date.now() - started > 30000) return reject(new Error('__timeout__'))
248
+ setTimeout(tick, 100)
249
+ }
250
+ tick()
251
+ }))
252
+ } catch {
253
+ const lost = [...new Set(misses)]
254
+ const modules = lost.filter((path) => path.startsWith('/node_modules/')).slice(0, 4)
255
+ const assets = lost.filter((path) => !path.startsWith('/node_modules/') && !/favicon|fonts\.css/.test(path)).slice(0, 4)
256
+ throw new Error('上游 demo 没有就绪(30 秒)'
257
+ + (modules.length ? ';缺 npm 包:' + modules.join(', ') : '')
258
+ + (assets.length ? ';缺上游素材:' + assets.join(', ') + '(这些没有 vendored,不是配置问题)' : '')
259
+ + (modules.length || assets.length ? '' : ';模块可能抛错')
260
+ + '。只能退到 action:"source" 读源码与 cues.json,但那是有损降级(看得到参数,看不到真实动态)。')
261
+ }
262
+ const meta = await page.evaluate(() => ({
263
+ width: window.DEMO?.width || 1920,
264
+ height: window.DEMO?.height || 1080,
265
+ fps: window.DEMO?.fps || 30,
266
+ duration: window.DEMO?.duration || 10,
267
+ hasRenderAt: typeof window.renderAt === 'function',
268
+ }))
269
+ if (!meta.hasRenderAt) throw new Error('这个 demo 没有 window.renderAt(t),无法按时间取帧;改用 action:"source" 读源码')
270
+ const width = Math.max(320, Math.min(2560, Math.round(meta.width)))
271
+ const height = Math.max(240, Math.min(1440, Math.round(meta.height)))
272
+ const fps = Math.max(1, Math.round(meta.fps || 30))
273
+ await page.setViewport({width, height, deviceScaleFactor: 1})
274
+ const times = pickTimes(slug, count, explicitTimes, Number(meta.duration) || 10)
275
+ if (!times.length) throw new Error('没有可用的时间点(cues.json 里的时间都超出片长)')
276
+
277
+ const dir = directory || join(tmpdir(), 'dsh-drawai', 'style-frames', slug)
278
+ mkdirSync(dir, {recursive: true})
279
+ const files = []
280
+ for (const [index, t] of times.entries()) {
281
+ signal?.throwIfAborted()
282
+ await page.evaluate(async (time, fpsValue) => {
283
+ await window.renderAt?.(time)
284
+ // 两帧 rAF:让画面里基于 frame 的绘制与 CSS/WAAPI 都落到同一时刻
285
+ await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(resolve, 30))))
286
+ void fpsValue
287
+ }, t, fps)
288
+ const result = await page._client().send('Page.captureScreenshot', {
289
+ format: 'png', captureBeyondViewport: false,
290
+ clip: {x: 0, y: 0, width, height, scale: 1},
291
+ })
292
+ const data = Buffer.from((result.value || result).data, 'base64')
293
+ if (data.length > 12 * 1024 * 1024) throw new Error('参考帧体积过大:' + t + 's')
294
+ const file = join(dir, 'frame-' + String(index).padStart(2, '0') + '-' + t.toFixed(2) + 's.png')
295
+ writeFileSync(file, data)
296
+ files.push({time: t, frame: Math.round(t * fps), path: file, bytes: data.length})
297
+ }
298
+ const notes = []
299
+ if (misses.length) notes.push('缺上游资源(已用占位兜底,画面里的字体/纸纹可能与原片不同):' + [...new Set(misses)].slice(0, 8).join(', '))
300
+ notes.push('这些是参考帧:写场景前逐张看,按 STYLE.md 的效果配方逐项对照(转场/冲击/相机/颗粒/二次运动),别只对"形"。')
301
+ return {slug, width, height, fps, duration: meta.duration, times, directory: dir, files, notes}
302
+ } finally {
303
+ await browser.close({silent: true}).catch(() => {})
304
+ await new Promise((done) => server.close(done))
305
+ }
306
+ }