kasaframe 0.0.0-stage → 0.1.3

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 (442) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +148 -0
  3. package/README.md +92 -2
  4. package/app/package.json +12 -0
  5. package/app/server/engine.ts +533 -0
  6. package/app/server/main.ts +209 -0
  7. package/app/web/env.d.ts +2 -0
  8. package/app/web/icons.ts +31 -0
  9. package/app/web/index.html +21 -0
  10. package/app/web/main.ts +1195 -0
  11. package/app/web/style.css +463 -0
  12. package/bin/hooks.mjs +64 -0
  13. package/bin/kf.mjs +32 -0
  14. package/docs/AGENTS.md +286 -0
  15. package/music/README.md +37 -0
  16. package/package.json +44 -4
  17. package/packages/adapters/package.json +36 -0
  18. package/packages/adapters/src/css.ts +11 -0
  19. package/packages/adapters/src/gsap.ts +26 -0
  20. package/packages/adapters/src/index.ts +9 -0
  21. package/packages/adapters/src/lottie.ts +22 -0
  22. package/packages/adapters/src/media.ts +59 -0
  23. package/packages/adapters/src/page.ts +229 -0
  24. package/packages/adapters/src/project.ts +214 -0
  25. package/packages/adapters/src/react.ts +36 -0
  26. package/packages/adapters/src/svelte.ts +22 -0
  27. package/packages/adapters/src/three.ts +18 -0
  28. package/packages/adapters/src/vue.ts +27 -0
  29. package/packages/check/package.json +19 -0
  30. package/packages/check/src/check.ts +280 -0
  31. package/packages/check/src/cli.ts +70 -0
  32. package/packages/check/src/image.ts +146 -0
  33. package/packages/check/src/index.ts +9 -0
  34. package/packages/check/src/ocr.swift +37 -0
  35. package/packages/check/src/probe.ts +328 -0
  36. package/packages/check/src/report.ts +44 -0
  37. package/packages/check/src/rules/assets.ts +45 -0
  38. package/packages/check/src/rules/beats.ts +212 -0
  39. package/packages/check/src/rules/camera.ts +217 -0
  40. package/packages/check/src/rules/captions.ts +49 -0
  41. package/packages/check/src/rules/flash.ts +235 -0
  42. package/packages/check/src/rules/frames.ts +136 -0
  43. package/packages/check/src/rules/layout.ts +281 -0
  44. package/packages/check/src/rules/page.ts +87 -0
  45. package/packages/check/src/rules/runs.ts +20 -0
  46. package/packages/check/src/rules/text.ts +326 -0
  47. package/packages/check/src/scan.ts +240 -0
  48. package/packages/check/src/serve.ts +67 -0
  49. package/packages/check/src/types.ts +80 -0
  50. package/packages/check/src/video.ts +137 -0
  51. package/packages/cli/package.json +30 -0
  52. package/packages/cli/src/blur.ts +80 -0
  53. package/packages/cli/src/browser.ts +203 -0
  54. package/packages/cli/src/command.ts +44 -0
  55. package/packages/cli/src/commands/app.ts +25 -0
  56. package/packages/cli/src/commands/beats.ts +36 -0
  57. package/packages/cli/src/commands/bench.ts +123 -0
  58. package/packages/cli/src/commands/blender.ts +293 -0
  59. package/packages/cli/src/commands/check.ts +25 -0
  60. package/packages/cli/src/commands/direct.ts +210 -0
  61. package/packages/cli/src/commands/doctor.ts +30 -0
  62. package/packages/cli/src/commands/gen.ts +173 -0
  63. package/packages/cli/src/commands/look.ts +46 -0
  64. package/packages/cli/src/commands/lyrics.ts +157 -0
  65. package/packages/cli/src/commands/make.ts +1121 -0
  66. package/packages/cli/src/commands/music.ts +78 -0
  67. package/packages/cli/src/commands/narrate.ts +238 -0
  68. package/packages/cli/src/commands/new.ts +185 -0
  69. package/packages/cli/src/commands/preview.ts +43 -0
  70. package/packages/cli/src/commands/render.ts +250 -0
  71. package/packages/cli/src/commands/series.ts +110 -0
  72. package/packages/cli/src/commands/setup.ts +102 -0
  73. package/packages/cli/src/commands/sheet.ts +114 -0
  74. package/packages/cli/src/commands/skill.ts +43 -0
  75. package/packages/cli/src/commands/still.ts +40 -0
  76. package/packages/cli/src/commands/theme.ts +172 -0
  77. package/packages/cli/src/commands/transitions.ts +40 -0
  78. package/packages/cli/src/deps.ts +122 -0
  79. package/packages/cli/src/ffmpeg.ts +81 -0
  80. package/packages/cli/src/index.ts +8 -0
  81. package/packages/cli/src/kf.ts +64 -0
  82. package/packages/cli/src/lock.ts +72 -0
  83. package/packages/cli/src/mix.ts +131 -0
  84. package/packages/cli/src/out.ts +13 -0
  85. package/packages/cli/src/pool.ts +34 -0
  86. package/packages/cli/src/segments.ts +402 -0
  87. package/packages/cli/src/serve.ts +124 -0
  88. package/packages/clock/package.json +14 -0
  89. package/packages/clock/src/index.ts +25 -0
  90. package/packages/clock/src/install.ts +351 -0
  91. package/packages/clock/src/types.ts +45 -0
  92. package/packages/core/package.json +9 -0
  93. package/packages/core/src/beats.ts +283 -0
  94. package/packages/core/src/ease.ts +87 -0
  95. package/packages/core/src/frame.ts +99 -0
  96. package/packages/core/src/index.ts +27 -0
  97. package/packages/core/src/project.ts +157 -0
  98. package/packages/core/src/rand.ts +56 -0
  99. package/packages/core/src/spring.ts +52 -0
  100. package/packages/core/src/style.ts +100 -0
  101. package/packages/core/src/time.ts +88 -0
  102. package/packages/director/package.json +23 -0
  103. package/packages/director/src/assemble.ts +361 -0
  104. package/packages/director/src/brief.ts +122 -0
  105. package/packages/director/src/catalog.ts +49 -0
  106. package/packages/director/src/content.ts +343 -0
  107. package/packages/director/src/fallback.ts +58 -0
  108. package/packages/director/src/index.ts +13 -0
  109. package/packages/director/src/pattern.ts +90 -0
  110. package/packages/director/src/patterns/fill/cells.ts +115 -0
  111. package/packages/director/src/patterns/formula/stack.ts +93 -0
  112. package/packages/director/src/patterns/util.ts +58 -0
  113. package/packages/director/src/scene.ts +62 -0
  114. package/packages/director/src/select.ts +163 -0
  115. package/packages/director/src/transitions.ts +115 -0
  116. package/packages/gen/PROVIDERS.md +90 -0
  117. package/packages/gen/package.json +9 -0
  118. package/packages/gen/src/ask.ts +132 -0
  119. package/packages/gen/src/audio.ts +91 -0
  120. package/packages/gen/src/blender.ts +54 -0
  121. package/packages/gen/src/capabilities.ts +151 -0
  122. package/packages/gen/src/generate.ts +206 -0
  123. package/packages/gen/src/image/codex.ts +117 -0
  124. package/packages/gen/src/image/og.ts +133 -0
  125. package/packages/gen/src/index.ts +26 -0
  126. package/packages/gen/src/keys.ts +86 -0
  127. package/packages/gen/src/media.ts +150 -0
  128. package/packages/gen/src/music/elevenlabs.ts +41 -0
  129. package/packages/gen/src/music/index.ts +120 -0
  130. package/packages/gen/src/music/library.ts +73 -0
  131. package/packages/gen/src/music/lyria.ts +52 -0
  132. package/packages/gen/src/music/types.ts +28 -0
  133. package/packages/gen/src/plan.ts +106 -0
  134. package/packages/gen/src/registry.ts +54 -0
  135. package/packages/gen/src/seam.ts +103 -0
  136. package/packages/gen/src/tts/dashscope.ts +64 -0
  137. package/packages/gen/src/tts/elevenlabs.ts +84 -0
  138. package/packages/gen/src/tts/gemini.ts +73 -0
  139. package/packages/gen/src/tts/index.ts +241 -0
  140. package/packages/gen/src/tts/local.ts +42 -0
  141. package/packages/gen/src/tts/types.ts +44 -0
  142. package/packages/gen/src/types.ts +90 -0
  143. package/packages/gen/src/video/fal.ts +140 -0
  144. package/packages/gen/src/video/h3.ts +112 -0
  145. package/packages/gen/src/video/http.ts +44 -0
  146. package/packages/gen/src/video/ltx.ts +60 -0
  147. package/packages/gen/src/video/minimax.ts +55 -0
  148. package/packages/gen/src/video/veo.ts +71 -0
  149. package/packages/jizura/package.json +16 -0
  150. package/packages/jizura/src/deny.ts +73 -0
  151. package/packages/jizura/src/index.ts +7 -0
  152. package/packages/jizura/src/install.ts +269 -0
  153. package/packages/jizura/src/layer.ts +325 -0
  154. package/packages/jizura/src/lyrics.ts +81 -0
  155. package/packages/jizura/src/pin.ts +8 -0
  156. package/packages/jizura/src/vendor.ts +140 -0
  157. package/packages/jizura/src/vite.ts +63 -0
  158. package/packages/jizura/vendor/JIZURA_LICENSE.txt +21 -0
  159. package/packages/jizura/vendor/NOTICE +43 -0
  160. package/packages/jizura/vendor/engine.json +47 -0
  161. package/packages/jizura/vendor/fonts/BlackHanSans-Regular.woff2 +0 -0
  162. package/packages/jizura/vendor/fonts/DotGothic16-Regular.woff2 +0 -0
  163. package/packages/jizura/vendor/fonts/GowunBatang-Bold.woff2 +0 -0
  164. package/packages/jizura/vendor/fonts/IBMPlexMono-Medium.ttf +0 -0
  165. package/packages/jizura/vendor/fonts/IBMPlexMono-SemiBold.ttf +0 -0
  166. package/packages/jizura/vendor/fonts/NotoSansKR-VF.woff2 +0 -0
  167. package/packages/jizura/vendor/fonts/NotoSerifKR-VF.woff2 +0 -0
  168. package/packages/jizura/vendor/fonts/OFL-blackhansans.txt +93 -0
  169. package/packages/jizura/vendor/fonts/OFL-dotgothic16.txt +93 -0
  170. package/packages/jizura/vendor/fonts/OFL-gowunbatang.txt +94 -0
  171. package/packages/jizura/vendor/fonts/OFL-ibmplexmono.txt +93 -0
  172. package/packages/jizura/vendor/fonts/OFL-notosanskr.txt +93 -0
  173. package/packages/jizura/vendor/fonts/OFL-notoserifkr.txt +94 -0
  174. package/packages/jizura/vendor/fonts/fonts.json +52 -0
  175. package/packages/jizura/vendor/jizura_engine.js +35024 -0
  176. package/packages/motion/README.md +153 -0
  177. package/packages/motion/package.json +30 -0
  178. package/packages/motion/src/camera.ts +97 -0
  179. package/packages/motion/src/captions.ts +487 -0
  180. package/packages/motion/src/enter.ts +128 -0
  181. package/packages/motion/src/hangul.ts +129 -0
  182. package/packages/motion/src/impact.ts +100 -0
  183. package/packages/motion/src/index.ts +17 -0
  184. package/packages/motion/src/moves/brushWipe.ts +103 -0
  185. package/packages/motion/src/moves/cloneArray.ts +111 -0
  186. package/packages/motion/src/moves/curtains.ts +81 -0
  187. package/packages/motion/src/moves/dress.ts +191 -0
  188. package/packages/motion/src/moves/echoStack.ts +70 -0
  189. package/packages/motion/src/moves/fit.ts +70 -0
  190. package/packages/motion/src/moves/flood.ts +61 -0
  191. package/packages/motion/src/moves/ghostStack.ts +88 -0
  192. package/packages/motion/src/moves/gridFloor.ts +100 -0
  193. package/packages/motion/src/moves/imageSwarm.ts +113 -0
  194. package/packages/motion/src/moves/index.ts +38 -0
  195. package/packages/motion/src/moves/irisBlades.ts +113 -0
  196. package/packages/motion/src/moves/karaokeLine.ts +108 -0
  197. package/packages/motion/src/moves/ledBoard.ts +94 -0
  198. package/packages/motion/src/moves/liquidTab.ts +72 -0
  199. package/packages/motion/src/moves/markerWrite.ts +63 -0
  200. package/packages/motion/src/moves/menu.ts +138 -0
  201. package/packages/motion/src/moves/morphBox.ts +96 -0
  202. package/packages/motion/src/moves/neonSign.ts +78 -0
  203. package/packages/motion/src/moves/panelPop.ts +78 -0
  204. package/packages/motion/src/moves/paperTear.ts +75 -0
  205. package/packages/motion/src/moves/path.ts +64 -0
  206. package/packages/motion/src/moves/pathText.ts +107 -0
  207. package/packages/motion/src/moves/pointSphere.ts +109 -0
  208. package/packages/motion/src/moves/ransomLetters.ts +73 -0
  209. package/packages/motion/src/moves/shapeMorph.ts +143 -0
  210. package/packages/motion/src/moves/slamWords.ts +96 -0
  211. package/packages/motion/src/moves/sparkTrail.ts +130 -0
  212. package/packages/motion/src/moves/speedLines.ts +91 -0
  213. package/packages/motion/src/moves/splitFlap.ts +149 -0
  214. package/packages/motion/src/moves/stepWords.ts +128 -0
  215. package/packages/motion/src/moves/strikeOut.ts +98 -0
  216. package/packages/motion/src/moves/sunburst.ts +58 -0
  217. package/packages/motion/src/moves/tickerRows.ts +91 -0
  218. package/packages/motion/src/moves/tokenPicker.ts +115 -0
  219. package/packages/motion/src/moves/truchet.ts +85 -0
  220. package/packages/motion/src/moves/wordWall.ts +78 -0
  221. package/packages/motion/src/moves/worldRoll.ts +32 -0
  222. package/packages/motion/src/particles.ts +102 -0
  223. package/packages/motion/src/react/index.tsx +21 -0
  224. package/packages/motion/src/react/parts.tsx +431 -0
  225. package/packages/motion/src/react/time.tsx +97 -0
  226. package/packages/motion/src/text.ts +103 -0
  227. package/packages/motion/src/transition.ts +82 -0
  228. package/packages/motion/src/transition3d/cards.ts +65 -0
  229. package/packages/motion/src/transition3d/chain.ts +84 -0
  230. package/packages/motion/src/transition3d/dive.ts +181 -0
  231. package/packages/motion/src/transition3d/fold.ts +110 -0
  232. package/packages/motion/src/transition3d/gl.ts +219 -0
  233. package/packages/motion/src/transition3d/index.ts +235 -0
  234. package/packages/motion/src/transition3d/live.ts +70 -0
  235. package/packages/motion/src/transition3d/portal.ts +115 -0
  236. package/packages/motion/src/transition3d/react.tsx +29 -0
  237. package/packages/motion/src/transition3d/rig.ts +600 -0
  238. package/packages/motion/src/transition3d/shatter.ts +104 -0
  239. package/packages/motion/src/transition3d/space.ts +173 -0
  240. package/packages/motion/src/transition3d/whip.ts +78 -0
  241. package/packages/motion/src/transition3d/world.ts +737 -0
  242. package/packages/motion/src/transition3d/zoomOut.ts +114 -0
  243. package/packages/motion/src/transitions2d/catalog.ts +127 -0
  244. package/packages/motion/src/transitions2d/css.d.ts +2 -0
  245. package/packages/motion/src/transitions2d/cult-ui/LICENSE.md +9 -0
  246. package/packages/motion/src/transitions2d/cult-ui/RollingNumber.tsx +43 -0
  247. package/packages/motion/src/transitions2d/gl-transitions/LICENSE +25 -0
  248. package/packages/motion/src/transitions2d/gl-transitions/shaders.ts +1931 -0
  249. package/packages/motion/src/transitions2d/index.ts +2 -0
  250. package/packages/motion/src/transitions2d/kokonutui/LICENSE +21 -0
  251. package/packages/motion/src/transitions2d/kokonutui/MatrixText.css +16 -0
  252. package/packages/motion/src/transitions2d/kokonutui/MatrixText.tsx +94 -0
  253. package/packages/motion/src/transitions2d/magicui/HyperText.css +15 -0
  254. package/packages/motion/src/transitions2d/magicui/HyperText.tsx +115 -0
  255. package/packages/motion/src/transitions2d/magicui/LICENSE.md +21 -0
  256. package/packages/motion/src/transitions2d/magicui/MorphingText.css +25 -0
  257. package/packages/motion/src/transitions2d/magicui/MorphingText.tsx +130 -0
  258. package/packages/motion/src/transitions2d/magicui/WordRotate.tsx +42 -0
  259. package/packages/motion/src/transitions2d/react.ts +13 -0
  260. package/packages/motion/src/transitions2d/skiper-ui/ClipDiv.css +39 -0
  261. package/packages/motion/src/transitions2d/skiper-ui/ClipDiv.tsx +32 -0
  262. package/packages/motion/src/transitions2d/smoothui/LICENSE +21 -0
  263. package/packages/motion/src/transitions2d/smoothui/SharedAxisY.tsx +50 -0
  264. package/packages/motion/src/transitions2d/smoothui/TextMorph.tsx +90 -0
  265. package/packages/motion/src/transitions2d/smoothui/shaders.ts +223 -0
  266. package/packages/motion/src/transitions2d/unlumen/LICENSE +21 -0
  267. package/packages/motion/src/transitions2d/unlumen/ScrambleText.tsx +114 -0
  268. package/packages/motion/src/transitions2d/vengeance-ui/LICENSE +21 -0
  269. package/packages/motion/src/transitions2d/vengeance-ui/MorphText.css +69 -0
  270. package/packages/motion/src/transitions2d/vengeance-ui/MorphText.tsx +45 -0
  271. package/packages/motion/src/type.ts +131 -0
  272. package/packages/motion/src/ui.ts +172 -0
  273. package/packages/patterns/package.json +15 -0
  274. package/packages/patterns/src/bars/rank.ts +119 -0
  275. package/packages/patterns/src/before-after/shrink.ts +127 -0
  276. package/packages/patterns/src/before-after/strike.ts +125 -0
  277. package/packages/patterns/src/code/terminal.ts +133 -0
  278. package/packages/patterns/src/compare/bars.ts +142 -0
  279. package/packages/patterns/src/compare/duel.ts +103 -0
  280. package/packages/patterns/src/end/lockup.ts +85 -0
  281. package/packages/patterns/src/end/slam.ts +91 -0
  282. package/packages/patterns/src/flow/graph.ts +250 -0
  283. package/packages/patterns/src/hook-number/counter.ts +101 -0
  284. package/packages/patterns/src/hook-number/roll.ts +179 -0
  285. package/packages/patterns/src/hook-number/slam.ts +91 -0
  286. package/packages/patterns/src/index.ts +38 -0
  287. package/packages/patterns/src/kit.ts +449 -0
  288. package/packages/patterns/src/limit/gauge.ts +117 -0
  289. package/packages/patterns/src/list/cards.ts +131 -0
  290. package/packages/patterns/src/list/cycle.ts +22 -0
  291. package/packages/patterns/src/media/panels.ts +100 -0
  292. package/packages/patterns/src/objects.ts +234 -0
  293. package/packages/patterns/src/quote/karaoke.ts +126 -0
  294. package/packages/patterns/src/quote/slam.ts +22 -0
  295. package/packages/patterns/src/screen/focus.ts +85 -0
  296. package/packages/patterns/src/steps/path.ts +137 -0
  297. package/packages/patterns/src/steps/stack.ts +111 -0
  298. package/packages/patterns/src/term-reveal/chips.ts +92 -0
  299. package/packages/patterns/src/term-reveal/expand.ts +127 -0
  300. package/packages/patterns/src/timeline/axis.ts +144 -0
  301. package/packages/patterns/src/title/mask.ts +85 -0
  302. package/packages/patterns/src/title/slam.ts +23 -0
  303. package/packages/patterns/src/toggle-sim/switch.ts +92 -0
  304. package/packages/patterns/src/trend/columns.ts +108 -0
  305. package/packages/patterns/src/trend/draw.ts +140 -0
  306. package/packages/patterns/src/types.ts +67 -0
  307. package/packages/patterns/src/words/moves.ts +42 -0
  308. package/packages/patterns/src/words/slam.ts +4 -0
  309. package/packages/plan/package.json +15 -0
  310. package/packages/plan/src/asks.ts +256 -0
  311. package/packages/plan/src/brief.ts +383 -0
  312. package/packages/plan/src/captions.ts +78 -0
  313. package/packages/plan/src/detect.ts +93 -0
  314. package/packages/plan/src/index.ts +27 -0
  315. package/packages/plan/src/layout.ts +176 -0
  316. package/packages/plan/src/llm.ts +172 -0
  317. package/packages/plan/src/look.ts +222 -0
  318. package/packages/plan/src/plan.ts +179 -0
  319. package/packages/plan/src/request.ts +55 -0
  320. package/packages/plan/src/series.ts +76 -0
  321. package/packages/plan/src/studio.ts +53 -0
  322. package/packages/plan/src/templates.ts +42 -0
  323. package/packages/plan/src/themes.ts +48 -0
  324. package/packages/plan/src/types.ts +157 -0
  325. package/packages/studio/package.json +12 -0
  326. package/packages/studio/src/index.ts +1 -0
  327. package/packages/studio/src/plugin.ts +236 -0
  328. package/packages/studio/src/ui/main.ts +503 -0
  329. package/packages/studio/src/ui/studio.css +165 -0
  330. package/packages/studio/src/ui/timeline.ts +317 -0
  331. package/packages/themes/kit/THEME-MAKE.md +68 -0
  332. package/packages/themes/kit/THEME.md +30 -0
  333. package/packages/themes/kit/briefs/og-intro-15s/brief.json +16 -0
  334. package/packages/themes/kit/briefs/og-intro-15s/facts.ts +29 -0
  335. package/packages/themes/kit/briefs/og-intro-15s/logo.svg +12 -0
  336. package/packages/themes/kit/effects.ts +5 -0
  337. package/packages/themes/kit/sample/index.html +11 -0
  338. package/packages/themes/kit/sample/main.ts +9 -0
  339. package/packages/themes/kit/sample/scenes/scene.ts +15 -0
  340. package/packages/themes/package.json +19 -0
  341. package/packages/themes/src/factory.ts +296 -0
  342. package/packages/themes/src/fx.ts +14 -0
  343. package/packages/themes/src/index.ts +114 -0
  344. package/packages/themes/src/slotkit.ts +79 -0
  345. package/packages/themes/src/slots.ts +46 -0
  346. package/packages/themes/src/stage3d.ts +197 -0
  347. package/packages/themes/src/types.ts +58 -0
  348. package/presets/kawaii-pastel.json +94 -0
  349. package/presets/neutral-dark.json +82 -0
  350. package/presets/og.json +98 -0
  351. package/series/og-shorts.json +14 -0
  352. package/skills/kasaframe/SKILL.md +33 -0
  353. package/templates/_shared/project/index.html +11 -0
  354. package/templates/_shared/project/lib/react.ts +22 -0
  355. package/templates/_shared/project/lib/stage.ts +292 -0
  356. package/templates/_shared/project/main.ts +9 -0
  357. package/templates/_shared/scenes/default.ts +21 -0
  358. package/templates/_shared/scenes/drop.ts +33 -0
  359. package/templates/_shared/scenes/end.ts +26 -0
  360. package/templates/_shared/scenes/hook.ts +18 -0
  361. package/templates/_shared/scenes/seam.ts +41 -0
  362. package/templates/_shared/scenes/step.ts +49 -0
  363. package/templates/ad/preview.png +0 -0
  364. package/templates/ad/template.json +151 -0
  365. package/templates/demo/preview.png +0 -0
  366. package/templates/demo/template.json +141 -0
  367. package/templates/explainer/preview.png +0 -0
  368. package/templates/explainer/template.json +193 -0
  369. package/templates/launch/preview.png +0 -0
  370. package/templates/launch/template.json +163 -0
  371. package/templates/mv/preview.png +0 -0
  372. package/templates/mv/scenes/default.ts +98 -0
  373. package/templates/mv/template.json +178 -0
  374. package/templates/promo/preview.png +0 -0
  375. package/templates/promo/template.json +185 -0
  376. package/templates/shorts/preview.png +0 -0
  377. package/templates/shorts/scenes/hook.ts +37 -0
  378. package/templates/shorts/template.json +196 -0
  379. package/templates/square/preview.png +0 -0
  380. package/templates/square/template.json +133 -0
  381. package/themes/debug-hud/THEME.md +39 -0
  382. package/themes/debug-hud/effects.ts +389 -0
  383. package/themes/debug-hud/sample/assets/og/logo.svg +12 -0
  384. package/themes/debug-hud/sample/beats.json +1 -0
  385. package/themes/debug-hud/sample/index.html +11 -0
  386. package/themes/debug-hud/sample/kf.json +59 -0
  387. package/themes/debug-hud/sample/lib/hudkit.ts +49 -0
  388. package/themes/debug-hud/sample/lib/og.ts +29 -0
  389. package/themes/debug-hud/sample/main.ts +9 -0
  390. package/themes/debug-hud/sample/scenes/end.ts +62 -0
  391. package/themes/debug-hud/sample/scenes/gather.ts +67 -0
  392. package/themes/debug-hud/sample/scenes/models.ts +70 -0
  393. package/themes/debug-hud/sample/scenes/price.ts +79 -0
  394. package/themes/debug-hud/sample/style.json +91 -0
  395. package/themes/debug-hud/slots.ts +99 -0
  396. package/themes/debug-hud/still.png +0 -0
  397. package/themes/debug-hud/style.json +91 -0
  398. package/themes/debug-hud/theme.json +68 -0
  399. package/themes/og-measure/THEME.md +47 -0
  400. package/themes/og-measure/effects.ts +432 -0
  401. package/themes/og-measure/sample/assets/brand/og-logo-full.svg +12 -0
  402. package/themes/og-measure/sample/assets/brand/og-logo.svg +12 -0
  403. package/themes/og-measure/sample/assets/data/measure.json +353 -0
  404. package/themes/og-measure/sample/beats.json +1 -0
  405. package/themes/og-measure/sample/index.html +11 -0
  406. package/themes/og-measure/sample/kf.json +66 -0
  407. package/themes/og-measure/sample/lib/data.ts +62 -0
  408. package/themes/og-measure/sample/main.ts +9 -0
  409. package/themes/og-measure/sample/measure.ts +78 -0
  410. package/themes/og-measure/sample/scenes/end.ts +47 -0
  411. package/themes/og-measure/sample/scenes/gather.ts +51 -0
  412. package/themes/og-measure/sample/scenes/models.ts +34 -0
  413. package/themes/og-measure/sample/scenes/one.ts +39 -0
  414. package/themes/og-measure/sample/scenes/price.ts +56 -0
  415. package/themes/og-measure/sample/style.json +97 -0
  416. package/themes/og-measure/slots.ts +88 -0
  417. package/themes/og-measure/still.png +0 -0
  418. package/themes/og-measure/style.json +97 -0
  419. package/themes/og-measure/theme.json +79 -0
  420. package/themes/riso-zine/THEME.md +43 -0
  421. package/themes/riso-zine/effects.ts +321 -0
  422. package/themes/riso-zine/sample/assets/og/logo.svg +12 -0
  423. package/themes/riso-zine/sample/beats.json +1 -0
  424. package/themes/riso-zine/sample/index.html +11 -0
  425. package/themes/riso-zine/sample/kf.json +59 -0
  426. package/themes/riso-zine/sample/lib/og.ts +29 -0
  427. package/themes/riso-zine/sample/lib/shot.ts +86 -0
  428. package/themes/riso-zine/sample/lib/zine.ts +42 -0
  429. package/themes/riso-zine/sample/main.ts +9 -0
  430. package/themes/riso-zine/sample/scenes/end.ts +61 -0
  431. package/themes/riso-zine/sample/scenes/gather.ts +62 -0
  432. package/themes/riso-zine/sample/scenes/models.ts +52 -0
  433. package/themes/riso-zine/sample/scenes/price.ts +82 -0
  434. package/themes/riso-zine/sample/style.json +99 -0
  435. package/themes/riso-zine/slots.ts +81 -0
  436. package/themes/riso-zine/still.png +0 -0
  437. package/themes/riso-zine/style.json +99 -0
  438. package/themes/riso-zine/theme.json +75 -0
  439. package/tools/beats/beats.py +372 -0
  440. package/tools/beats/requirements.txt +3 -0
  441. package/tools/lyrics/align.py +164 -0
  442. package/tools/lyrics/requirements.txt +9 -0
@@ -0,0 +1,131 @@
1
+ import { spawn } from "node:child_process";
2
+ import { existsSync, readFileSync } from "node:fs";
3
+ import { join, resolve } from "node:path";
4
+ import { audioDuration, findSilences } from "@kasaframe/gen";
5
+
6
+ // 내레이션 + 곡 섞기 — kf.json 에 audio(섞기 값)가 있는 프로젝트만. 없으면 렌더는 예전처럼 곡만 얹는다
7
+ export interface AudioMix {
8
+ musicDb: number; // 말하는 동안 곡이 목소리보다 이만큼 아래(덕킹 포함)
9
+ duck: boolean;
10
+ lufs: number;
11
+ truePeak: number;
12
+ fadeOut: number;
13
+ }
14
+
15
+ // 목소리를 먼저 이 크기로 맞춘다 — 덕킹 문턱이 목소리 크기에 묶여 있어서 공급자마다 다른 크기를 한 자리로
16
+ const VOICE_LUFS = -18;
17
+ // 덕킹 두 입력의 프레임 크기를 맞춘다 — 목소리 wav(1024)·곡 mp3(1152) 프레임이 도착 차례에 따라 sidechaincompress 결과가
18
+ // 실행마다 달라졌다(같은 요청이면 바이트까지 같은 영상이어야 한다). 두 갈래를 같은 크기로 잘라 넣으면 늘 같은 값이 나온다
19
+ const FRAMES = "asetnsamples=n=1024:p=0";
20
+ const SIDECHAIN = "sidechaincompress=threshold=0.025:ratio=3:attack=20:release=400:knee=4";
21
+
22
+ function ffmpeg(args: string[]): Promise<string> {
23
+ return new Promise((done, fail) => {
24
+ const p = spawn("ffmpeg", ["-hide_banner", "-nostats", "-y", ...args]);
25
+ let err = "";
26
+ p.stderr.on("data", (b: Buffer) => (err += b));
27
+ p.on("error", fail);
28
+ p.on("close", (code) => (code === 0 ? done(err) : fail(new Error(`ffmpeg 섞기 실패(${code}): ${err.trim().split("\n").slice(-3).join(" / ")}`))));
29
+ });
30
+ }
31
+
32
+ // 통합 크기(LUFS) — 무음은 게이트가 빼서 말·곡이 실제로 울리는 곳만 잰다
33
+ async function loudness(input: string[], filter = ""): Promise<number> {
34
+ const err = await ffmpeg([...input, "-af", `${filter ? `${filter},` : ""}ebur128=framelog=quiet`, "-f", "null", "-"]);
35
+ const m = err.match(/I:\s+(-?[\d.]+|-inf) LUFS/g)?.at(-1)?.match(/(-?[\d.]+|-inf)/);
36
+ const v = m ? Number(m[1]) : NaN;
37
+ return Number.isFinite(v) ? v : -70;
38
+ }
39
+
40
+ // 말하는 구간만 고르는 aselect 식 — 목소리 한 줄에서 0.3초 넘는 숨을 뺀 자리
41
+ async function speechSelect(voice: string): Promise<string | null> {
42
+ const total = await audioDuration(voice);
43
+ const spans: [number, number][] = [];
44
+ let t = 0;
45
+ for (const s of await findSilences(voice, { db: -40, min: 0.3 })) {
46
+ if (s.from > t + 0.2) spans.push([t, s.from]);
47
+ t = Math.min(s.to, total);
48
+ }
49
+ if (total > t + 0.2) spans.push([t, total]);
50
+ return spans.length ? spans.map(([a, b]) => `between(t,${a.toFixed(3)},${b.toFixed(3)})`).join("+") : null;
51
+ }
52
+
53
+ interface Sources {
54
+ voice?: string;
55
+ music?: string;
56
+ musicFrom: number; // 곡 시각 = musicFrom + 영상 시각
57
+ }
58
+
59
+ export function mixSettings(dir: string): { audio: AudioMix; voice?: string } | null {
60
+ const kf = JSON.parse(readFileSync(join(dir, "kf.json"), "utf8"));
61
+ if (!kf.audio) return null;
62
+ const voice = kf.narration ? resolve(dir, kf.narration) : undefined;
63
+ return { audio: kf.audio, voice: voice && existsSync(voice) ? voice : undefined };
64
+ }
65
+
66
+ // 영상 [from, from+dur) 의 소리를 out(wav)으로. total 은 영상 전체 길이 — 끝 페이드는 영상 끝에 걸린다
67
+ export async function mixAudio(src: Sources, o: { audio: AudioMix; from: number; dur: number; total: number; out: string }): Promise<string> {
68
+ const { audio } = o;
69
+ const seg = (path: string, at: number) => ["-ss", at.toFixed(6), "-t", o.dur.toFixed(6), "-i", path];
70
+ const inputs: string[] = [];
71
+ const chains: string[] = [];
72
+ let k = 0;
73
+ let voiceLabel = "";
74
+ let musicLabel = "";
75
+ // 끝 페이드는 영상 끝 몇 초에만 — 구간만 굽을 때도 같은 자리
76
+ const fadeAt = o.total - audio.fadeOut - o.from;
77
+ const fade = audio.fadeOut > 0 && fadeAt < o.dur ? `,afade=t=out:st=${Math.max(0, fadeAt).toFixed(4)}:d=${audio.fadeOut.toFixed(4)}` : "";
78
+ let voiceGain = 0;
79
+ if (src.voice) {
80
+ // 크기는 영상 전체로 잰다 — 구간만 재면 구간마다 목소리 크기가 달라진다
81
+ voiceGain = VOICE_LUFS - (await loudness(["-i", src.voice]));
82
+ inputs.push(...seg(src.voice, o.from));
83
+ chains.push(`[${k}]aresample=48000,volume=${voiceGain.toFixed(2)}dB,pan=stereo|c0=c0|c1=c0,${FRAMES},asplit=2[v][vsc]`);
84
+ voiceLabel = "[v]";
85
+ k++;
86
+ }
87
+ if (src.music) {
88
+ // 말이 있으면 말하는 동안 곡이 목소리보다 |musicDb| 아래, 없으면 곡이 주인공이라 그대로 두고 정규화만
89
+ const gain = src.voice ? await musicGain(src as Required<Sources>, voiceGain, audio, o.total) : 0;
90
+ inputs.push("-stream_loop", "-1", ...seg(src.music, src.musicFrom + o.from));
91
+ chains.push(`[${k}]aresample=48000,aformat=channel_layouts=stereo,volume=${gain.toFixed(2)}dB${fade},${FRAMES}[m0]`);
92
+ musicLabel = "[m0]";
93
+ if (src.voice && audio.duck) {
94
+ chains.push(`[m0][vsc]${SIDECHAIN}[m1]`);
95
+ musicLabel = "[m1]";
96
+ }
97
+ k++;
98
+ }
99
+ if (!voiceLabel && !musicLabel) throw new Error("섞을 소리가 없다");
100
+ if (voiceLabel && !musicLabel) chains.push("[vsc]anullsink");
101
+ const parts = [voiceLabel, musicLabel].filter(Boolean);
102
+ chains.push(parts.length === 2 ? `${parts.join("")}amix=inputs=2:normalize=0:duration=longest,apad,atrim=0:${o.dur.toFixed(6)}[pre]` : `${parts[0]}apad,atrim=0:${o.dur.toFixed(6)}[pre]`);
103
+ const pre = o.out.replace(/\.wav$/, ".pre.wav");
104
+ await ffmpeg([...inputs, "-filter_complex", chains.join(";"), "-map", "[pre]", "-c:a", "pcm_s24le", "-ar", "48000", pre]);
105
+ // 두 번 잰다 — 한 번에 loudnorm 을 걸면 동적 모드로 들어가 덕킹 모양이 뭉개진다. 잰 값을 주고 linear 로 한 번에 올린다
106
+ const err = await ffmpeg(["-i", pre, "-af", `loudnorm=I=${audio.lufs}:TP=${audio.truePeak}:LRA=11:print_format=json`, "-f", "null", "-"]);
107
+ const j = JSON.parse(err.slice(err.lastIndexOf("{"), err.lastIndexOf("}") + 1));
108
+ const ln = `loudnorm=I=${audio.lufs}:TP=${audio.truePeak}:LRA=11:measured_I=${j.input_i}:measured_TP=${j.input_tp}:measured_LRA=${j.input_lra}:measured_thresh=${j.input_thresh}:offset=${j.target_offset}:linear=true`;
109
+ await ffmpeg(["-i", pre, "-af", `${ln},aresample=48000`, "-c:a", "pcm_s16le", o.out]);
110
+ return o.out;
111
+ }
112
+
113
+ // 곡 셈여림(조용한 도입·부푸는 가운데)과 덕킹 깊이(5–10dB)가 곡마다 달라 상수로는 안 맞는다 — 영상 전체에서 말하는 구간만 골라
114
+ // 목소리와 덕킹된 곡을 한 번 재고 그 차만큼 곡을 옮긴다. 덕킹은 목소리로만 걸려 곡 크기와 무관하니 한 번이면 된다
115
+ export async function musicGain(src: Required<Sources>, voiceGain: number, audio: AudioMix, total: number): Promise<number> {
116
+ const music = ["-stream_loop", "-1", "-ss", src.musicFrom.toFixed(6), "-t", total.toFixed(6), "-i", src.music];
117
+ const g0 = VOICE_LUFS + audio.musicDb - (await loudness(music));
118
+ const sel = await speechSelect(src.voice);
119
+ if (!sel) return g0;
120
+ const pick = `aselect='${sel}',asetpts=N/SR/TB`;
121
+ const voiceSpeech = await loudness(["-i", src.voice], `volume=${voiceGain.toFixed(2)}dB,${pick}`);
122
+ const duck = audio.duck ? `;[m0][vsc]${SIDECHAIN}[m1]` : "";
123
+ const err = await ffmpeg([
124
+ "-i", src.voice, ...music,
125
+ "-filter_complex", `[0]aresample=48000,volume=${voiceGain.toFixed(2)}dB,pan=stereo|c0=c0|c1=c0,${FRAMES}[vsc];[1]aresample=48000,aformat=channel_layouts=stereo,volume=${g0.toFixed(2)}dB,${FRAMES}[m0]${duck};[${audio.duck ? "m1" : "m0"}]${pick},ebur128=framelog=quiet[out]${audio.duck ? "" : ";[vsc]anullsink"}`,
126
+ "-map", "[out]", "-f", "null", "-",
127
+ ]);
128
+ const m = err.match(/I:\s+(-?[\d.]+|-inf) LUFS/g)?.at(-1)?.match(/(-?[\d.]+|-inf)/);
129
+ const musicSpeech = m ? Number(m[1]) : NaN;
130
+ return Number.isFinite(musicSpeech) ? g0 + (voiceSpeech + audio.musicDb - musicSpeech) : g0;
131
+ }
@@ -0,0 +1,13 @@
1
+ import { mkdirSync } from "node:fs";
2
+ import { dirname, join, resolve } from "node:path";
3
+
4
+ // 결과물은 기본으로 프로젝트의 out/ 에 둔다.
5
+ export function outPath(dir: string, given: string | boolean | undefined, fallback: string): string {
6
+ const p = typeof given === "string" ? resolve(given) : join(resolve(dir), "out", fallback);
7
+ mkdirSync(dirname(p), { recursive: true });
8
+ return p;
9
+ }
10
+
11
+ export function fmtTime(t: number): string {
12
+ return t.toFixed(3).replace(/\.?0+$/, "") || "0";
13
+ }
@@ -0,0 +1,34 @@
1
+ // 장면별 일(장면 쓰기·그림·목소리)을 동시에 — 모델·공급자 호출이 대부분 기다림이라 겹치면 장면 수만큼 빨라진다.
2
+ // 동시 수는 KF_JOBS 로 누른다(요금 한도·속도 제한이 빡빡한 키). 돈 드는 호출의 묻기·예산은 부르는 쪽 그대로다
3
+
4
+ export const jobLimit = (fallback: number): number => Math.max(1, Math.floor(Number(process.env.KF_JOBS) || fallback));
5
+
6
+ /** n 개까지 동시에. 결과는 넣은 순서대로 */
7
+ export async function pool<T, R>(items: readonly T[], n: number, fn: (item: T, i: number) => Promise<R>): Promise<R[]> {
8
+ const out: R[] = new Array(items.length);
9
+ let next = 0;
10
+ await Promise.all(
11
+ Array.from({ length: Math.min(n, items.length) }, async () => {
12
+ for (let i = next++; i < items.length; i = next++) out[i] = await fn(items[i], i);
13
+ }),
14
+ );
15
+ return out;
16
+ }
17
+
18
+ /** 한 번에 n 개만 지나가는 문 — 크롬을 띄우는 일처럼 기계가 감당할 만큼만 */
19
+ export function gate(n: number): <R>(fn: () => Promise<R>) => Promise<R> {
20
+ let busy = 0;
21
+ const wait: (() => void)[] = [];
22
+ return async (fn) => {
23
+ // 자리를 기다리던 쪽에 바로 넘긴다 — 풀었다 다시 잡는 사이에 새로 온 쪽이 끼어들면 n 을 넘는다
24
+ if (busy < n) busy++;
25
+ else await new Promise<void>((r) => wait.push(r));
26
+ try {
27
+ return await fn();
28
+ } finally {
29
+ const w = wait.shift();
30
+ if (w) w();
31
+ else busy--;
32
+ }
33
+ };
34
+ }
@@ -0,0 +1,402 @@
1
+ import { createHash } from "node:crypto";
2
+ import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
3
+ import { join, relative, sep } from "node:path";
4
+ import type { ViteDevServer } from "vite";
5
+ import type { Session, Tab } from "./browser.ts";
6
+ import { type BlurOpts, captureBlurred } from "./blur.ts";
7
+ import { concat, encoder, type VideoFormat } from "./ffmpeg.ts";
8
+ import { repoRoot } from "./serve.ts";
9
+
10
+ // 장면 조각 렌더 — 장면마다 out/segments/<장면>-<해시>.mp4 를 두고, 해시가 같으면 다시 찍지 않는다.
11
+ // 해시는 그 장면이 실제로 불러온 모듈(Vite 모듈 그래프 — 장면 파일·lib·@kasaframe 소스·테마·glob 로 읽은 json)·
12
+ // kf.json 의 그 장면 칸·자산·자막 줄·굽기 설정이다. seek 모드는 프레임이 시각의 순수 함수라 조각을 이어도 통짜로 구운 것과 프레임이 같다.
13
+
14
+ // 조각 꼴(인코딩·해시 재료)이 바뀌면 올린다 — 옛 조각을 다시 쓰지 않게
15
+ const VERSION = "seg-1";
16
+ // 화면을 안 바꾸는 kf.json 칸 — 소리·굽기 기본값·검사 예외. 굽기 값(모션 블러)은 KeyOpts 로 따로 들어간다
17
+ const NOT_FRAMES = new Set(["scenes", "music", "audio", "voice", "narration", "render", "check"]);
18
+ const AUDIO = /\.(mp3|wav|m4a|aac|flac|ogg)$/i;
19
+
20
+ export interface SceneEntry {
21
+ id: string;
22
+ from: number;
23
+ dur: number;
24
+ role?: string;
25
+ file?: string;
26
+ [k: string]: unknown;
27
+ }
28
+
29
+ export interface SceneSpan {
30
+ id: string;
31
+ role?: string;
32
+ /** 프레임 [start, end) */
33
+ start: number;
34
+ end: number;
35
+ }
36
+
37
+ /** 프레임 f 의 장면 — 페이지(mountProject)와 같은 판정 from ≤ f/fps. 조각들이 0..total 을 빈틈·겹침 없이 한 번씩 덮는다 */
38
+ export function sceneSpans(scenes: Pick<SceneEntry, "id" | "from" | "role">[], fps: number, total: number): SceneSpan[] {
39
+ const firstFrame = (from: number) => {
40
+ let f = Math.max(0, Math.round(from * fps) - 2);
41
+ while (f / fps < from) f++;
42
+ return Math.min(total, f);
43
+ };
44
+ return scenes
45
+ .map((s, i) => ({ id: s.id, role: s.role, start: i === 0 ? 0 : firstFrame(s.from), end: i === scenes.length - 1 ? total : firstFrame(scenes[i + 1].from) }))
46
+ .filter((s) => s.end > s.start);
47
+ }
48
+
49
+ export interface KeyOpts {
50
+ width: number;
51
+ height: number;
52
+ fps: number;
53
+ format: VideoFormat;
54
+ crf?: number;
55
+ blur: BlurOpts | null;
56
+ captions?: string;
57
+ /** 크롬 판 — 래스터가 판마다 다를 수 있다 */
58
+ browser: string;
59
+ }
60
+
61
+ /** 굽기 설정 묶음 하나 — 자막 켠 판·끈 판처럼 같은 장면을 다른 설정으로 구운 조각을 따로 센다 */
62
+ export const variantOf = (o: KeyOpts): string => sha(JSON.stringify([VERSION, o])).slice(0, 10);
63
+
64
+ const sha = (...parts: (string | Buffer)[]) => {
65
+ const h = createHash("sha256");
66
+ for (const p of parts) h.update(p).update("\0");
67
+ return h.digest("hex");
68
+ };
69
+
70
+ const omit = (o: Record<string, unknown>, keys: Set<string>) => Object.fromEntries(Object.entries(o).filter(([k]) => !keys.has(k)));
71
+
72
+ function listFiles(dir: string): string[] {
73
+ if (!existsSync(dir)) return [];
74
+ return readdirSync(dir, { withFileTypes: true }).flatMap((e) => (e.isDirectory() ? listFiles(join(dir, e.name)) : [join(dir, e.name)]));
75
+ }
76
+
77
+ interface Line {
78
+ scene?: string;
79
+ from?: number;
80
+ to?: number;
81
+ }
82
+
83
+ export interface SceneKey {
84
+ key: string;
85
+ /** 해시 재료 이름 → 그 해시. 조각 기록에 남겨 다음에 「무엇이 바뀌어 다시 찍나」를 짚는다 */
86
+ inputs: Record<string, string>;
87
+ }
88
+
89
+ /**
90
+ * 장면마다 조각 해시. 장면이 다른 장면 모듈을 불러오면(이음매 — 앞 장면 끝·뒤 장면 처음을 살아 있는 화면으로 잇는다) 그 장면들의 해시도 묻는다.
91
+ * 이음매는 앞뒤 둘만 쓰므로(transition3d live) 이웃 둘로 좁힌다 — 멀리 있는 장면을 고쳐도 이음매는 그대로.
92
+ */
93
+ export function sceneKeys(server: ViteDevServer, root: string, o: KeyOpts): Map<string, SceneKey> {
94
+ const kf = JSON.parse(readFileSync(join(root, "kf.json"), "utf8")) as { scenes: SceneEntry[]; [k: string]: unknown };
95
+ const graph = server.environments.client.moduleGraph;
96
+ const kfFile = join(root, "kf.json");
97
+ const capFile = join(root, "captions.json");
98
+ const sceneFile = new Map(kf.scenes.map((s) => [join(root, s.file ?? `scenes/${s.id}.ts`), s.id]));
99
+ const memo = new Map<string, string>();
100
+ const fileHash = (f: string) => {
101
+ let h = memo.get(f);
102
+ if (h === undefined) {
103
+ h = existsSync(f) && statSync(f).isFile() ? sha(readFileSync(f)) : "-";
104
+ memo.set(f, h);
105
+ }
106
+ return h;
107
+ };
108
+ // 레포 소스와 프로젝트 파일만 — node_modules(외부 꾸러미·미리 묶은 의존)는 판이 바뀔 일이 드물고 수가 많다
109
+ const label = (f: string): string | null => {
110
+ for (const [base, tag] of [[root, ""], [repoRoot, "kf:"]] as const) {
111
+ const rel = relative(base, f);
112
+ if (rel.startsWith("..") || rel.split(sep).includes("node_modules")) continue;
113
+ if (tag === "" && rel.split(sep)[0] === "out") return null;
114
+ return `${tag}${rel}`;
115
+ }
116
+ return null;
117
+ };
118
+
119
+ function walk(start: string): { files: Map<string, string>; scenes: Set<string> } {
120
+ const files = new Map<string, string>();
121
+ const scenes = new Set<string>();
122
+ const stack = [...(graph.getModulesByFile(start) ?? [])];
123
+ const seen = new Set<unknown>();
124
+ while (stack.length) {
125
+ const m = stack.pop()!;
126
+ if (seen.has(m)) continue;
127
+ seen.add(m);
128
+ const f = m.file;
129
+ if (f && f !== start && sceneFile.has(f)) {
130
+ scenes.add(sceneFile.get(f)!);
131
+ continue;
132
+ }
133
+ const name = f ? label(f) : null;
134
+ if (f && !name) continue;
135
+ if (f && name && f !== kfFile && f !== capFile) files.set(name, fileHash(f));
136
+ stack.push(...m.importedModules);
137
+ }
138
+ if (!files.size && existsSync(start)) files.set(label(start) ?? start, fileHash(start));
139
+ return { files, scenes };
140
+ }
141
+ const listed = (m: Map<string, string>) => JSON.stringify([...m].sort((a, b) => (a[0] < b[0] ? -1 : 1)));
142
+
143
+ const base = walk(join(root, "main.ts")).files;
144
+ base.set("index.html", fileHash(join(root, "index.html")));
145
+ base.set("kf.json 화면 칸", sha(JSON.stringify(omit(kf, NOT_FRAMES))));
146
+ base.set("굽기 설정", sha(JSON.stringify([VERSION, o])));
147
+ const common = sha(listed(base));
148
+ const lines: Line[] = existsSync(capFile) ? JSON.parse(readFileSync(capFile, "utf8")) : [];
149
+ const assets = listFiles(join(root, "assets")).filter((f) => !AUDIO.test(f)).map((f) => ({ f, rel: relative(join(root, "assets"), f).split(sep).join("/") }));
150
+
151
+ const own = new Map<string, string>();
152
+ const inputs = new Map<string, Record<string, string>>();
153
+ const deps = new Map<string, Set<string>>();
154
+ for (const [i, s] of kf.scenes.entries()) {
155
+ const file = join(root, s.file ?? `scenes/${s.id}.ts`);
156
+ const w = walk(file);
157
+ // 자산은 장면이 글자로 적은 경로(프로젝트 안 소스) + 장면 id 로 시작하는 파일(gen/<id>.png·blender/<id>.mp4·seam/<id>-from.png)
158
+ const text = [...w.files.keys()].filter((n) => !n.startsWith("kf:")).map((n) => (existsSync(join(root, n)) ? readFileSync(join(root, n), "utf8") : "")).join("\n");
159
+ const used = assets
160
+ .filter(({ f, rel }) => text.includes(rel) || new RegExp(`^${s.id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}([.\\-_]|$)`).test(f.split(sep).pop()!))
161
+ .map(({ f, rel }) => [rel, fileHash(f)]);
162
+ const cap = lines.filter((l) => l.scene === s.id || (l.scene === undefined && l.from !== undefined && l.from < s.from + s.dur && (l.to ?? l.from) >= s.from));
163
+ own.set(s.id, sha(common, listed(w.files), JSON.stringify(omit(s, new Set(["idea"]))), JSON.stringify(cap), JSON.stringify(used)));
164
+ inputs.set(s.id, {
165
+ ...Object.fromEntries(base),
166
+ ...Object.fromEntries(w.files),
167
+ "kf.json 장면 칸": sha(JSON.stringify(omit(s, new Set(["idea"])))),
168
+ "자막 줄": sha(JSON.stringify(cap)),
169
+ ...Object.fromEntries(used.map(([rel, h]) => [`assets/${rel}`, h])),
170
+ });
171
+ const d = new Set(s.role === "seam" ? [kf.scenes[i - 1]?.id, kf.scenes[i + 1]?.id].filter((x): x is string => !!x) : w.scenes);
172
+ deps.set(s.id, d);
173
+ }
174
+ // 경계 프레임에서 앞 장면이 아직 켜져 있으면(from+dur 이 다음 from 보다 끝자리만큼 큰 경우) 그 프레임은 두 장면이 겹쳐 그린다
175
+ const spans = sceneSpans(kf.scenes, o.fps, Math.round(Number(kf.duration) * o.fps));
176
+ for (const [i, sp] of spans.entries()) {
177
+ const prev = kf.scenes.find((s) => s.id === spans[i - 1]?.id);
178
+ if (prev && sp.start / o.fps < prev.from + prev.dur) deps.get(sp.id)!.add(prev.id);
179
+ }
180
+ return new Map(
181
+ kf.scenes.map((s) => {
182
+ const ds = [...deps.get(s.id)!].sort();
183
+ const key = sha(own.get(s.id)!, ...ds.map((d) => own.get(d) ?? d)).slice(0, 12);
184
+ return [s.id, { key, inputs: { ...inputs.get(s.id)!, ...Object.fromEntries(ds.map((d) => [`장면 ${d}`, own.get(d) ?? d])) } }];
185
+ }),
186
+ );
187
+ }
188
+
189
+ // 그 장면의 가장 최근 조각 기록(해시가 달라도) — 바뀐 재료와 장당 걸린 시간을 여기서 읽는다
190
+ function lastRecord(root: string, id: string): Partial<SegmentReport> & { tabSeconds?: number } | null {
191
+ const dir = segmentDir(root);
192
+ if (!existsSync(dir)) return null;
193
+ const prev = readdirSync(dir)
194
+ .filter((f) => f.startsWith(`${id}-`) && f.endsWith(".json") && /^[0-9a-f]{12}$/.test(f.slice(id.length + 1, -5)))
195
+ .map((f) => join(dir, f))
196
+ .sort((a, b) => statSync(b).mtimeMs - statSync(a).mtimeMs)[0];
197
+ if (!prev) return null;
198
+ try {
199
+ return JSON.parse(readFileSync(prev, "utf8"));
200
+ } catch {
201
+ return null;
202
+ }
203
+ }
204
+
205
+ /** 같은 장면의 지난 조각 기록과 견줘 바뀐 재료 이름 — 「왜 다시 찍나」 */
206
+ export function changedInputs(root: string, id: string, now: Record<string, string>): string[] | null {
207
+ const old = lastRecord(root, id)?.inputs;
208
+ if (!old) return null;
209
+ return [...new Set([...Object.keys(old), ...Object.keys(now)])].filter((k) => old[k] !== now[k]).sort();
210
+ }
211
+
212
+ export interface SegmentReport {
213
+ id: string;
214
+ key: string;
215
+ file: string;
216
+ frames: number;
217
+ seconds: number;
218
+ events: MotionEvent[];
219
+ inputs?: Record<string, string>;
220
+ /** 탭들이 이 장면을 찍는 데 쓴 초의 합 — 다음 굽기가 장면 무게를 어림한다 */
221
+ tabSeconds?: number;
222
+ }
223
+
224
+ export interface MotionEvent {
225
+ t: number;
226
+ kind: string;
227
+ source: string;
228
+ scene?: string;
229
+ }
230
+
231
+ export const segmentDir = (root: string) => join(root, "out", "segments");
232
+ export const segmentFile = (root: string, id: string, key: string, ext: string) => join(segmentDir(root), `${id}-${key}${ext}`);
233
+
234
+ /** 이미 구운 조각 — 영상과 기록(.json) 둘 다 있어야 다시 쓴다 */
235
+ export function cachedSegment(root: string, id: string, key: string, ext: string): SegmentReport | null {
236
+ const file = segmentFile(root, id, key, ext);
237
+ const meta = segmentFile(root, id, key, ".json");
238
+ if (!existsSync(file) || !existsSync(meta)) return null;
239
+ try {
240
+ return { ...JSON.parse(readFileSync(meta, "utf8")), file };
241
+ } catch {
242
+ return null;
243
+ }
244
+ }
245
+
246
+ function split(start: number, end: number, n: number): [number, number][] {
247
+ const out: [number, number][] = [];
248
+ for (let i = 0; i < n; i++) {
249
+ const a = start + Math.floor(((end - start) * i) / n);
250
+ const b = start + Math.floor(((end - start) * (i + 1)) / n);
251
+ if (b > a) out.push([a, b]);
252
+ }
253
+ return out;
254
+ }
255
+
256
+ export interface SegmentWork {
257
+ span: SceneSpan;
258
+ key: string;
259
+ inputs?: Record<string, string>;
260
+ }
261
+
262
+ /**
263
+ * 고른 장면 조각을 탭 여러 개로 굽는다. 장면 하나가 길면 탭 몫으로 쪼개 찍고 그 장면 조각 하나로 잇는다(탭이 놀지 않게).
264
+ * 장면마다 「장면 <id>: 렌더」「장면 <id>: 구움」 줄을 찍는다 — 작업실이 타임라인 상태로 읽는다
265
+ */
266
+ export async function renderSegments(o: {
267
+ session: Session;
268
+ root: string;
269
+ work: SegmentWork[];
270
+ tabs: number;
271
+ format: VideoFormat;
272
+ ext: string;
273
+ crf?: number;
274
+ blur: BlurOpts | null;
275
+ tmp: string;
276
+ onFrame?: () => void;
277
+ }): Promise<SegmentReport[]> {
278
+ const { session: s, root } = o;
279
+ const { fps, width, height } = s.info.meta;
280
+ mkdirSync(segmentDir(root), { recursive: true });
281
+ mkdirSync(o.tmp, { recursive: true });
282
+ // 장면마다 장당 무게가 다르다(3D 이음매는 글 장면의 두 배 넘게) — 지난 기록으로 어림해 탭마다 두 몫쯤 되게 쪼개고
283
+ // 무거운 몫부터 나눠 준다. 기록이 없으면 장 수로 어림한다
284
+ const perFrame = new Map(o.work.map((w) => {
285
+ const r = lastRecord(root, w.span.id);
286
+ return [w.span.id, r?.frames ? (r.tabSeconds ?? r.seconds ?? 0) / r.frames : 0];
287
+ }));
288
+ const known = [...perFrame.values()].filter((x) => x > 0);
289
+ const guess = known.length ? known.reduce((a, b) => a + b, 0) / known.length : 1;
290
+ const cost = (w: SegmentWork) => (w.span.end - w.span.start) * (perFrame.get(w.span.id) || guess);
291
+ const unit = o.work.reduce((n, w) => n + cost(w), 0) / Math.max(1, o.tabs * 2);
292
+ const minFrames = Math.ceil(fps / 2);
293
+ const parts = o.work
294
+ .flatMap((w) => {
295
+ const frames = w.span.end - w.span.start;
296
+ const n = Math.max(1, Math.min(Math.round(cost(w) / unit), Math.floor(frames / minFrames)));
297
+ return split(w.span.start, w.span.end, n).map(([a, b], k) => ({ w, k, a, b, cost: (cost(w) * (b - a)) / frames }));
298
+ })
299
+ .sort((x, y) => y.cost - x.cost);
300
+ const busy = new Map<string, number>();
301
+ const left = new Map(o.work.map((w) => [w.span.id, parts.filter((p) => p.w === w).length]));
302
+ const started = new Map<string, number>();
303
+ const finished: Promise<void>[] = [];
304
+ const queue = [...parts];
305
+ const tabs: Tab[] = [];
306
+ const workers = Math.min(o.tabs, parts.length);
307
+
308
+ async function join1(w: SegmentWork): Promise<void> {
309
+ const n = parts.filter((p) => p.w === w).length;
310
+ const list = join(o.tmp, `${w.span.id}.txt`);
311
+ writeFileSync(list, Array.from({ length: n }, (_, k) => `file '${join(o.tmp, `${w.span.id}-${k}${o.ext}`).replace(/'/g, "'\\''")}'`).join("\n") + "\n");
312
+ const tmpOut = join(o.tmp, `${w.span.id}${o.ext}`);
313
+ await concat({ list, out: tmpOut, format: o.format, from: 0, dur: (w.span.end - w.span.start) / fps });
314
+ renameSync(tmpOut, segmentFile(root, w.span.id, w.key, o.ext));
315
+ console.error(`장면 ${w.span.id}: 구움 — ${w.span.end - w.span.start}장 ${((Date.now() - started.get(w.span.id)!) / 1000).toFixed(1)}초`);
316
+ }
317
+
318
+ await Promise.all(
319
+ Array.from({ length: workers }, async (_, i) => {
320
+ const tab = i === 0 ? s.first : await s.tab();
321
+ tabs.push(tab);
322
+ for (let p = queue.shift(); p; p = queue.shift()) {
323
+ if (!started.has(p.w.span.id)) {
324
+ started.set(p.w.span.id, Date.now());
325
+ const why = p.w.inputs && changedInputs(root, p.w.span.id, p.w.inputs);
326
+ console.error(`장면 ${p.w.span.id}: 렌더${why?.length ? ` — 바뀜: ${why.slice(0, 3).join(", ")}${why.length > 3 ? ` 외 ${why.length - 3}` : ""}` : ""}`);
327
+ }
328
+ const raw = o.blur ? { width, height } : undefined;
329
+ const t0 = Date.now();
330
+ const enc = encoder(join(o.tmp, `${p.w.span.id}-${p.k}${o.ext}`), fps, o.format, o.crf, raw);
331
+ for (let f = p.a; f < p.b; f++) {
332
+ if (o.blur) await enc.write((await captureBlurred(tab, s.info, f / fps, o.blur)).data);
333
+ else {
334
+ await tab.seek(f / fps);
335
+ await enc.write(await tab.capture());
336
+ }
337
+ o.onFrame?.();
338
+ }
339
+ await enc.end();
340
+ busy.set(p.w.span.id, (busy.get(p.w.span.id) ?? 0) + (Date.now() - t0) / 1000);
341
+ const rest = left.get(p.w.span.id)! - 1;
342
+ left.set(p.w.span.id, rest);
343
+ if (rest === 0) finished.push(join1(p.w));
344
+ }
345
+ }),
346
+ );
347
+ await Promise.all(finished);
348
+
349
+ // 움직임 기록은 탭에 쌓인다 — 장면 시각으로 갈라 조각마다 남긴다(검사가 영상 옆 .json 으로 센다)
350
+ const events = await collectEvents(tabs);
351
+ return o.work.map((w) => {
352
+ const from = w.span.start / fps;
353
+ const to = w.span.end / fps;
354
+ const rep: SegmentReport = {
355
+ id: w.span.id,
356
+ key: w.key,
357
+ file: segmentFile(root, w.span.id, w.key, o.ext),
358
+ frames: w.span.end - w.span.start,
359
+ seconds: Math.round((Date.now() - started.get(w.span.id)!) / 100) / 10,
360
+ events: events.filter((e) => e.t >= from - 1e-9 && e.t < to - 1e-9),
361
+ tabSeconds: Math.round((busy.get(w.span.id) ?? 0) * 10) / 10,
362
+ ...(w.inputs ? { inputs: w.inputs } : {}),
363
+ };
364
+ const { file: _f, ...meta } = rep;
365
+ writeFileSync(segmentFile(root, w.span.id, w.key, ".json"), JSON.stringify(meta));
366
+ return rep;
367
+ });
368
+ }
369
+
370
+ export async function collectEvents(tabs: Tab[]): Promise<MotionEvent[]> {
371
+ const seen = new Set<string>();
372
+ const out: MotionEvent[] = [];
373
+ for (const tab of tabs) {
374
+ const rows = await tab.page.evaluate(() => (window.__kf?.events ?? []) as unknown[]).catch(() => []);
375
+ for (const r of rows as MotionEvent[]) {
376
+ const key = `${r.t}|${r.kind}|${r.source}|${r.scene ?? ""}`;
377
+ if (seen.has(key)) continue;
378
+ seen.add(key);
379
+ out.push(r);
380
+ }
381
+ }
382
+ return out.sort((a, b) => a.t - b.t);
383
+ }
384
+
385
+ /** 굽기 설정마다 지금 쓰는 조각을 적고, 어느 설정에서도 안 쓰는 옛 조각은 지운다(최근 설정 넷까지 둔다) */
386
+ export function pruneSegments(root: string, variant: string, current: Record<string, string>): void {
387
+ const dir = segmentDir(root);
388
+ const indexFile = join(dir, "index.json");
389
+ let index: Record<string, { at: number; scenes: Record<string, string> }> = {};
390
+ try {
391
+ index = JSON.parse(readFileSync(indexFile, "utf8"));
392
+ } catch {}
393
+ index[variant] = { at: Date.now(), scenes: current };
394
+ const keep = Object.entries(index).sort((a, b) => b[1].at - a[1].at).slice(0, 4);
395
+ index = Object.fromEntries(keep);
396
+ writeFileSync(indexFile, JSON.stringify(index, null, 1));
397
+ const used = new Set(keep.flatMap(([, v]) => Object.entries(v.scenes).map(([id, key]) => `${id}-${key}`)));
398
+ for (const f of readdirSync(dir)) {
399
+ if (f === "index.json") continue;
400
+ if (!used.has(f.replace(/\.[^.]+$/, ""))) rmSync(join(dir, f), { force: true });
401
+ }
402
+ }