kasaframe 0.0.0-stage → 0.1.1

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 (338) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +146 -0
  3. package/README.md +77 -2
  4. package/app/package.json +12 -0
  5. package/app/server/engine.ts +415 -0
  6. package/app/server/main.ts +205 -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 +1013 -0
  11. package/app/web/style.css +430 -0
  12. package/bin/hooks.mjs +51 -0
  13. package/bin/kf.mjs +14 -0
  14. package/docs/AGENTS.md +238 -0
  15. package/music/README.md +13 -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 +273 -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 +42 -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 +135 -0
  43. package/packages/check/src/rules/layout.ts +257 -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 +29 -0
  52. package/packages/cli/src/blur.ts +68 -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/blender.ts +270 -0
  58. package/packages/cli/src/commands/check.ts +25 -0
  59. package/packages/cli/src/commands/doctor.ts +22 -0
  60. package/packages/cli/src/commands/gen.ts +173 -0
  61. package/packages/cli/src/commands/look.ts +39 -0
  62. package/packages/cli/src/commands/lyrics.ts +111 -0
  63. package/packages/cli/src/commands/make.ts +542 -0
  64. package/packages/cli/src/commands/new.ts +176 -0
  65. package/packages/cli/src/commands/preview.ts +43 -0
  66. package/packages/cli/src/commands/render.ts +216 -0
  67. package/packages/cli/src/commands/setup.ts +63 -0
  68. package/packages/cli/src/commands/sheet.ts +114 -0
  69. package/packages/cli/src/commands/skill.ts +43 -0
  70. package/packages/cli/src/commands/still.ts +40 -0
  71. package/packages/cli/src/commands/theme.ts +85 -0
  72. package/packages/cli/src/commands/transitions.ts +40 -0
  73. package/packages/cli/src/deps.ts +122 -0
  74. package/packages/cli/src/ffmpeg.ts +81 -0
  75. package/packages/cli/src/index.ts +8 -0
  76. package/packages/cli/src/kf.ts +53 -0
  77. package/packages/cli/src/lock.ts +72 -0
  78. package/packages/cli/src/out.ts +13 -0
  79. package/packages/cli/src/serve.ts +124 -0
  80. package/packages/clock/package.json +14 -0
  81. package/packages/clock/src/index.ts +25 -0
  82. package/packages/clock/src/install.ts +351 -0
  83. package/packages/clock/src/types.ts +45 -0
  84. package/packages/core/package.json +9 -0
  85. package/packages/core/src/beats.ts +283 -0
  86. package/packages/core/src/ease.ts +87 -0
  87. package/packages/core/src/frame.ts +99 -0
  88. package/packages/core/src/index.ts +27 -0
  89. package/packages/core/src/project.ts +146 -0
  90. package/packages/core/src/rand.ts +56 -0
  91. package/packages/core/src/spring.ts +52 -0
  92. package/packages/core/src/style.ts +100 -0
  93. package/packages/core/src/time.ts +88 -0
  94. package/packages/gen/PROVIDERS.md +55 -0
  95. package/packages/gen/package.json +9 -0
  96. package/packages/gen/src/generate.ts +204 -0
  97. package/packages/gen/src/image/og.ts +133 -0
  98. package/packages/gen/src/index.ts +18 -0
  99. package/packages/gen/src/keys.ts +60 -0
  100. package/packages/gen/src/media.ts +150 -0
  101. package/packages/gen/src/plan.ts +106 -0
  102. package/packages/gen/src/registry.ts +51 -0
  103. package/packages/gen/src/seam.ts +103 -0
  104. package/packages/gen/src/types.ts +86 -0
  105. package/packages/gen/src/video/fal.ts +140 -0
  106. package/packages/gen/src/video/h3.ts +107 -0
  107. package/packages/gen/src/video/http.ts +44 -0
  108. package/packages/gen/src/video/ltx.ts +60 -0
  109. package/packages/gen/src/video/minimax.ts +55 -0
  110. package/packages/gen/src/video/veo.ts +71 -0
  111. package/packages/jizura/package.json +16 -0
  112. package/packages/jizura/src/deny.ts +73 -0
  113. package/packages/jizura/src/index.ts +7 -0
  114. package/packages/jizura/src/install.ts +226 -0
  115. package/packages/jizura/src/layer.ts +282 -0
  116. package/packages/jizura/src/lyrics.ts +81 -0
  117. package/packages/jizura/src/pin.ts +5 -0
  118. package/packages/jizura/src/vite.ts +36 -0
  119. package/packages/motion/README.md +142 -0
  120. package/packages/motion/package.json +29 -0
  121. package/packages/motion/src/camera.ts +97 -0
  122. package/packages/motion/src/captions.ts +479 -0
  123. package/packages/motion/src/enter.ts +128 -0
  124. package/packages/motion/src/hangul.ts +129 -0
  125. package/packages/motion/src/impact.ts +100 -0
  126. package/packages/motion/src/index.ts +17 -0
  127. package/packages/motion/src/moves/brushWipe.ts +90 -0
  128. package/packages/motion/src/moves/cloneArray.ts +111 -0
  129. package/packages/motion/src/moves/curtains.ts +81 -0
  130. package/packages/motion/src/moves/echoStack.ts +70 -0
  131. package/packages/motion/src/moves/fit.ts +34 -0
  132. package/packages/motion/src/moves/flood.ts +61 -0
  133. package/packages/motion/src/moves/ghostStack.ts +79 -0
  134. package/packages/motion/src/moves/gridFloor.ts +100 -0
  135. package/packages/motion/src/moves/imageSwarm.ts +109 -0
  136. package/packages/motion/src/moves/index.ts +19 -0
  137. package/packages/motion/src/moves/irisBlades.ts +113 -0
  138. package/packages/motion/src/moves/karaokeLine.ts +94 -0
  139. package/packages/motion/src/moves/ledBoard.ts +94 -0
  140. package/packages/motion/src/moves/liquidTab.ts +72 -0
  141. package/packages/motion/src/moves/markerWrite.ts +63 -0
  142. package/packages/motion/src/moves/morphBox.ts +96 -0
  143. package/packages/motion/src/moves/neonSign.ts +77 -0
  144. package/packages/motion/src/moves/panelPop.ts +78 -0
  145. package/packages/motion/src/moves/paperTear.ts +75 -0
  146. package/packages/motion/src/moves/path.ts +64 -0
  147. package/packages/motion/src/moves/pathText.ts +107 -0
  148. package/packages/motion/src/moves/pointSphere.ts +109 -0
  149. package/packages/motion/src/moves/ransomLetters.ts +73 -0
  150. package/packages/motion/src/moves/shapeMorph.ts +143 -0
  151. package/packages/motion/src/moves/slamWords.ts +89 -0
  152. package/packages/motion/src/moves/sparkTrail.ts +114 -0
  153. package/packages/motion/src/moves/speedLines.ts +91 -0
  154. package/packages/motion/src/moves/splitFlap.ts +147 -0
  155. package/packages/motion/src/moves/stepWords.ts +117 -0
  156. package/packages/motion/src/moves/strikeOut.ts +98 -0
  157. package/packages/motion/src/moves/sunburst.ts +58 -0
  158. package/packages/motion/src/moves/tickerRows.ts +87 -0
  159. package/packages/motion/src/moves/tokenPicker.ts +114 -0
  160. package/packages/motion/src/moves/truchet.ts +85 -0
  161. package/packages/motion/src/moves/wordWall.ts +76 -0
  162. package/packages/motion/src/moves/worldRoll.ts +31 -0
  163. package/packages/motion/src/particles.ts +102 -0
  164. package/packages/motion/src/react/index.tsx +21 -0
  165. package/packages/motion/src/react/parts.tsx +431 -0
  166. package/packages/motion/src/react/time.tsx +97 -0
  167. package/packages/motion/src/text.ts +103 -0
  168. package/packages/motion/src/transition.ts +82 -0
  169. package/packages/motion/src/transition3d/cards.ts +65 -0
  170. package/packages/motion/src/transition3d/chain.ts +84 -0
  171. package/packages/motion/src/transition3d/dive.ts +181 -0
  172. package/packages/motion/src/transition3d/fold.ts +110 -0
  173. package/packages/motion/src/transition3d/gl.ts +219 -0
  174. package/packages/motion/src/transition3d/index.ts +228 -0
  175. package/packages/motion/src/transition3d/live.ts +70 -0
  176. package/packages/motion/src/transition3d/portal.ts +115 -0
  177. package/packages/motion/src/transition3d/react.tsx +29 -0
  178. package/packages/motion/src/transition3d/rig.ts +502 -0
  179. package/packages/motion/src/transition3d/shatter.ts +101 -0
  180. package/packages/motion/src/transition3d/whip.ts +65 -0
  181. package/packages/motion/src/transition3d/world.ts +683 -0
  182. package/packages/motion/src/transition3d/zoomOut.ts +114 -0
  183. package/packages/motion/src/transitions2d/catalog.ts +127 -0
  184. package/packages/motion/src/transitions2d/css.d.ts +2 -0
  185. package/packages/motion/src/transitions2d/cult-ui/LICENSE.md +9 -0
  186. package/packages/motion/src/transitions2d/cult-ui/RollingNumber.tsx +43 -0
  187. package/packages/motion/src/transitions2d/gl-transitions/LICENSE +25 -0
  188. package/packages/motion/src/transitions2d/gl-transitions/shaders.ts +1931 -0
  189. package/packages/motion/src/transitions2d/index.ts +2 -0
  190. package/packages/motion/src/transitions2d/kokonutui/LICENSE +21 -0
  191. package/packages/motion/src/transitions2d/kokonutui/MatrixText.css +16 -0
  192. package/packages/motion/src/transitions2d/kokonutui/MatrixText.tsx +94 -0
  193. package/packages/motion/src/transitions2d/magicui/HyperText.css +15 -0
  194. package/packages/motion/src/transitions2d/magicui/HyperText.tsx +115 -0
  195. package/packages/motion/src/transitions2d/magicui/LICENSE.md +21 -0
  196. package/packages/motion/src/transitions2d/magicui/MorphingText.css +25 -0
  197. package/packages/motion/src/transitions2d/magicui/MorphingText.tsx +130 -0
  198. package/packages/motion/src/transitions2d/magicui/WordRotate.tsx +42 -0
  199. package/packages/motion/src/transitions2d/react.ts +13 -0
  200. package/packages/motion/src/transitions2d/skiper-ui/ClipDiv.css +39 -0
  201. package/packages/motion/src/transitions2d/skiper-ui/ClipDiv.tsx +32 -0
  202. package/packages/motion/src/transitions2d/smoothui/LICENSE +21 -0
  203. package/packages/motion/src/transitions2d/smoothui/SharedAxisY.tsx +50 -0
  204. package/packages/motion/src/transitions2d/smoothui/TextMorph.tsx +90 -0
  205. package/packages/motion/src/transitions2d/smoothui/shaders.ts +223 -0
  206. package/packages/motion/src/transitions2d/unlumen/LICENSE +21 -0
  207. package/packages/motion/src/transitions2d/unlumen/ScrambleText.tsx +114 -0
  208. package/packages/motion/src/transitions2d/vengeance-ui/LICENSE +21 -0
  209. package/packages/motion/src/transitions2d/vengeance-ui/MorphText.css +69 -0
  210. package/packages/motion/src/transitions2d/vengeance-ui/MorphText.tsx +45 -0
  211. package/packages/motion/src/type.ts +131 -0
  212. package/packages/motion/src/ui.ts +172 -0
  213. package/packages/plan/package.json +15 -0
  214. package/packages/plan/src/brief.ts +254 -0
  215. package/packages/plan/src/captions.ts +75 -0
  216. package/packages/plan/src/detect.ts +93 -0
  217. package/packages/plan/src/index.ts +23 -0
  218. package/packages/plan/src/layout.ts +176 -0
  219. package/packages/plan/src/llm.ts +110 -0
  220. package/packages/plan/src/look.ts +220 -0
  221. package/packages/plan/src/plan.ts +125 -0
  222. package/packages/plan/src/request.ts +55 -0
  223. package/packages/plan/src/studio.ts +53 -0
  224. package/packages/plan/src/templates.ts +42 -0
  225. package/packages/plan/src/themes.ts +48 -0
  226. package/packages/plan/src/types.ts +106 -0
  227. package/packages/studio/package.json +12 -0
  228. package/packages/studio/src/index.ts +1 -0
  229. package/packages/studio/src/plugin.ts +236 -0
  230. package/packages/studio/src/ui/main.ts +503 -0
  231. package/packages/studio/src/ui/studio.css +165 -0
  232. package/packages/studio/src/ui/timeline.ts +317 -0
  233. package/packages/themes/kit/THEME-MAKE.md +68 -0
  234. package/packages/themes/kit/THEME.md +30 -0
  235. package/packages/themes/kit/briefs/og-intro-15s/brief.json +16 -0
  236. package/packages/themes/kit/briefs/og-intro-15s/facts.ts +29 -0
  237. package/packages/themes/kit/briefs/og-intro-15s/logo.svg +12 -0
  238. package/packages/themes/kit/effects.ts +5 -0
  239. package/packages/themes/kit/sample/index.html +11 -0
  240. package/packages/themes/kit/sample/main.ts +9 -0
  241. package/packages/themes/kit/sample/scenes/scene.ts +15 -0
  242. package/packages/themes/package.json +16 -0
  243. package/packages/themes/src/factory.ts +296 -0
  244. package/packages/themes/src/fx.ts +14 -0
  245. package/packages/themes/src/index.ts +105 -0
  246. package/packages/themes/src/stage3d.ts +177 -0
  247. package/packages/themes/src/types.ts +58 -0
  248. package/presets/kawaii-pastel.json +94 -0
  249. package/presets/neutral-dark.json +82 -0
  250. package/presets/og.json +98 -0
  251. package/skills/kasaframe/SKILL.md +32 -0
  252. package/templates/_shared/project/index.html +11 -0
  253. package/templates/_shared/project/lib/react.ts +22 -0
  254. package/templates/_shared/project/lib/stage.ts +272 -0
  255. package/templates/_shared/project/main.ts +9 -0
  256. package/templates/_shared/scenes/default.ts +18 -0
  257. package/templates/_shared/scenes/drop.ts +30 -0
  258. package/templates/_shared/scenes/end.ts +23 -0
  259. package/templates/_shared/scenes/hook.ts +16 -0
  260. package/templates/_shared/scenes/seam.ts +41 -0
  261. package/templates/_shared/scenes/step.ts +40 -0
  262. package/templates/ad/preview.png +0 -0
  263. package/templates/ad/template.json +132 -0
  264. package/templates/demo/preview.png +0 -0
  265. package/templates/demo/template.json +130 -0
  266. package/templates/explainer/preview.png +0 -0
  267. package/templates/explainer/template.json +142 -0
  268. package/templates/launch/preview.png +0 -0
  269. package/templates/launch/template.json +130 -0
  270. package/templates/mv/preview.png +0 -0
  271. package/templates/mv/scenes/default.ts +95 -0
  272. package/templates/mv/template.json +176 -0
  273. package/templates/promo/preview.png +0 -0
  274. package/templates/promo/template.json +144 -0
  275. package/templates/shorts/preview.png +0 -0
  276. package/templates/shorts/scenes/hook.ts +34 -0
  277. package/templates/shorts/template.json +127 -0
  278. package/templates/square/preview.png +0 -0
  279. package/templates/square/template.json +113 -0
  280. package/themes/debug-hud/THEME.md +39 -0
  281. package/themes/debug-hud/effects.ts +389 -0
  282. package/themes/debug-hud/sample/assets/og/logo.svg +12 -0
  283. package/themes/debug-hud/sample/beats.json +1 -0
  284. package/themes/debug-hud/sample/index.html +11 -0
  285. package/themes/debug-hud/sample/kf.json +59 -0
  286. package/themes/debug-hud/sample/lib/hudkit.ts +49 -0
  287. package/themes/debug-hud/sample/lib/og.ts +29 -0
  288. package/themes/debug-hud/sample/main.ts +9 -0
  289. package/themes/debug-hud/sample/scenes/end.ts +62 -0
  290. package/themes/debug-hud/sample/scenes/gather.ts +67 -0
  291. package/themes/debug-hud/sample/scenes/models.ts +70 -0
  292. package/themes/debug-hud/sample/scenes/price.ts +79 -0
  293. package/themes/debug-hud/sample/style.json +91 -0
  294. package/themes/debug-hud/still.png +0 -0
  295. package/themes/debug-hud/style.json +91 -0
  296. package/themes/debug-hud/theme.json +68 -0
  297. package/themes/og-measure/THEME.md +47 -0
  298. package/themes/og-measure/effects.ts +432 -0
  299. package/themes/og-measure/sample/assets/brand/og-logo-full.svg +12 -0
  300. package/themes/og-measure/sample/assets/brand/og-logo.svg +12 -0
  301. package/themes/og-measure/sample/assets/data/measure.json +353 -0
  302. package/themes/og-measure/sample/beats.json +1 -0
  303. package/themes/og-measure/sample/index.html +11 -0
  304. package/themes/og-measure/sample/kf.json +66 -0
  305. package/themes/og-measure/sample/lib/data.ts +62 -0
  306. package/themes/og-measure/sample/main.ts +9 -0
  307. package/themes/og-measure/sample/measure.ts +78 -0
  308. package/themes/og-measure/sample/scenes/end.ts +47 -0
  309. package/themes/og-measure/sample/scenes/gather.ts +51 -0
  310. package/themes/og-measure/sample/scenes/models.ts +34 -0
  311. package/themes/og-measure/sample/scenes/one.ts +39 -0
  312. package/themes/og-measure/sample/scenes/price.ts +56 -0
  313. package/themes/og-measure/sample/style.json +97 -0
  314. package/themes/og-measure/still.png +0 -0
  315. package/themes/og-measure/style.json +97 -0
  316. package/themes/og-measure/theme.json +79 -0
  317. package/themes/riso-zine/THEME.md +43 -0
  318. package/themes/riso-zine/effects.ts +321 -0
  319. package/themes/riso-zine/sample/assets/og/logo.svg +12 -0
  320. package/themes/riso-zine/sample/beats.json +1 -0
  321. package/themes/riso-zine/sample/index.html +11 -0
  322. package/themes/riso-zine/sample/kf.json +59 -0
  323. package/themes/riso-zine/sample/lib/og.ts +29 -0
  324. package/themes/riso-zine/sample/lib/shot.ts +86 -0
  325. package/themes/riso-zine/sample/lib/zine.ts +42 -0
  326. package/themes/riso-zine/sample/main.ts +9 -0
  327. package/themes/riso-zine/sample/scenes/end.ts +61 -0
  328. package/themes/riso-zine/sample/scenes/gather.ts +62 -0
  329. package/themes/riso-zine/sample/scenes/models.ts +52 -0
  330. package/themes/riso-zine/sample/scenes/price.ts +82 -0
  331. package/themes/riso-zine/sample/style.json +99 -0
  332. package/themes/riso-zine/still.png +0 -0
  333. package/themes/riso-zine/style.json +99 -0
  334. package/themes/riso-zine/theme.json +75 -0
  335. package/tools/beats/beats.py +372 -0
  336. package/tools/beats/requirements.txt +3 -0
  337. package/tools/lyrics/align.py +164 -0
  338. package/tools/lyrics/requirements.txt +9 -0
@@ -0,0 +1,133 @@
1
+ import { readKey } from "../keys.ts";
2
+ import { KEY_BG, refJpeg } from "../media.ts";
3
+ import type { ImageProvider, ImageRequest, RawResult } from "../types.ts";
4
+
5
+ const HOST = process.env.KF_OG_HOST ?? "https://apis.opengateway.ai";
6
+
7
+ // 1M 토큰당 달러. OpenAI 단가표(2026-09) — gpt-image-2 와 2.5 둘 다 같은 값이다.
8
+ const PRICE = { textIn: 5, imageIn: 8, imageOut: 30 };
9
+
10
+ // 2.5 는 background:transparent 를 직접 받는다(2026-10-08 실측, 알파 61%). 나머지는 단색 바탕 + 키잉.
11
+ const TRANSPARENT_OK = /gpt-image-(2\.5|1\.5)/;
12
+
13
+ export function costFromUsage(usage: any): number | null {
14
+ if (!usage) return null;
15
+ const inD = usage.input_tokens_details ?? {};
16
+ const outD = usage.output_tokens_details ?? {};
17
+ const imageIn = inD.image_tokens ?? 0;
18
+ const textIn = inD.text_tokens ?? Math.max(0, (usage.input_tokens ?? 0) - imageIn);
19
+ const imageOut = outD.image_tokens ?? usage.output_tokens ?? 0;
20
+ return (textIn * PRICE.textIn + imageIn * PRICE.imageIn + imageOut * PRICE.imageOut) / 1e6;
21
+ }
22
+
23
+ // gpt-image-2 계열이 받는 크기: 변이 16의 배수, 긴 변 3840 이하, 비율 3:1 이하, 픽셀 655,360..8,294,400.
24
+ export function apiSize(size: string): { w: number; h: number } {
25
+ const m = /^(\d+)x(\d+)$/.exec(size);
26
+ if (!m) throw new Error(`크기는 1536x1024 꼴이어야 한다: ${size}`);
27
+ let w = Number(m[1]);
28
+ let h = Number(m[2]);
29
+ const ratio = Math.max(w, h) / Math.min(w, h);
30
+ if (ratio > 3) throw new Error(`비율 3:1 을 넘는 크기는 못 만든다: ${size}`);
31
+ const px = w * h;
32
+ const k = px < 655_360 ? Math.sqrt(655_360 / px) : px > 8_294_400 ? Math.sqrt(8_294_400 / px) : 1;
33
+ w *= k;
34
+ h *= k;
35
+ const longEdge = Math.max(w, h);
36
+ if (longEdge > 3840) {
37
+ w *= 3840 / longEdge;
38
+ h *= 3840 / longEdge;
39
+ }
40
+ const r16 = (v: number, up: boolean) => (up ? Math.ceil(v / 16) : Math.floor(v / 16)) * 16;
41
+ // 키운 쪽은 올림, 줄인 쪽은 내림 — 반올림하면 픽셀 한도를 살짝 넘어 400 이 난다.
42
+ return { w: r16(w, k >= 1), h: r16(h, k >= 1) };
43
+ }
44
+
45
+ async function call(path: string, init: RequestInit): Promise<any> {
46
+ const key = readKey("opengateway");
47
+ const r = await fetch(HOST + path, {
48
+ ...init,
49
+ headers: { Authorization: `Bearer ${key}`, ...(init.headers ?? {}) },
50
+ });
51
+ const text = await r.text();
52
+ if (!r.ok) {
53
+ // 413 은 본문이 비어 온다 — 상태 코드가 곧 원인이다.
54
+ const hint = r.status === 413 ? " (업로드가 크다)" : r.status === 402 ? " (OG 잔액 부족 — 충전하면 키가 자동 복구된다)" : "";
55
+ throw new Error(`OG ${path} ${r.status}${hint}: ${text.slice(0, 300)}`);
56
+ }
57
+ return JSON.parse(text);
58
+ }
59
+
60
+ function toRaw(j: any, model: string, params: Record<string, unknown>): RawResult {
61
+ const b64 = j.data?.[0]?.b64_json;
62
+ if (!b64) throw new Error(`OG 응답에 그림이 없다: ${JSON.stringify(j).slice(0, 200)}`);
63
+ const cost = costFromUsage(j.usage);
64
+ return {
65
+ bytes: Buffer.from(b64, "base64"),
66
+ ext: "png",
67
+ model,
68
+ seed: null,
69
+ costUsd: cost ?? 0,
70
+ costEstimated: cost === null,
71
+ params: { ...params, usage: j.usage },
72
+ };
73
+ }
74
+
75
+ export function ogImage(model = "openai/gpt-image-2.5-sunburst"): ImageProvider {
76
+ const nativeAlpha = TRANSPARENT_OK.test(model);
77
+
78
+ function body(o: ImageRequest) {
79
+ const { w, h } = apiSize(o.size);
80
+ const keyed = o.transparent && !nativeAlpha;
81
+ return {
82
+ keyed,
83
+ fields: {
84
+ model,
85
+ prompt: keyed ? `${o.prompt}\n\n${KEY_BG}` : o.prompt,
86
+ size: `${w}x${h}`,
87
+ quality: o.quality ?? "high",
88
+ output_format: "png",
89
+ n: 1,
90
+ ...(o.transparent && nativeAlpha ? { background: "transparent" } : {}),
91
+ },
92
+ };
93
+ }
94
+
95
+ return {
96
+ id: `og-${model.replace(/^openai\//, "")}`,
97
+ kind: "img",
98
+ model,
99
+ license: "OpenAI 이용약관 — 출력물 권리는 사용자에게, 상업 사용 가능",
100
+ region: "OG live(apis.opengateway.ai) 전 세계, 한국 사용 가능",
101
+ keyName: "opengateway",
102
+
103
+ async generate(o) {
104
+ const { keyed, fields } = body(o);
105
+ const j = await call("/v1/images/generations", {
106
+ method: "POST",
107
+ headers: { "Content-Type": "application/json" },
108
+ body: JSON.stringify(fields),
109
+ });
110
+ const raw = toRaw(j, model, { ...fields, prompt: undefined });
111
+ if (keyed) raw.notes = ["chroma-key"];
112
+ return raw;
113
+ },
114
+
115
+ // 마스크 없는 참조 편집. 참조가 먹었는지는 usage.input_tokens_details.image_tokens 로만 알 수 있다.
116
+ async edit(o) {
117
+ if (!o.refs?.length) throw new Error("edit 에는 refs 가 필요하다");
118
+ const { keyed, fields } = body(o);
119
+ const form = new FormData();
120
+ for (const [k, v] of Object.entries(fields)) form.append(k, String(v));
121
+ const field = o.refs.length > 1 ? "image[]" : "image";
122
+ for (const [i, ref] of o.refs.entries()) {
123
+ const jpg = await refJpeg(ref);
124
+ form.append(field, new Blob([new Uint8Array(jpg)], { type: "image/jpeg" }), `ref${i}.jpg`);
125
+ }
126
+ const j = await call("/v1/images/edits", { method: "POST", body: form });
127
+ const raw = toRaw(j, model, { ...fields, prompt: undefined, refs: o.refs.length });
128
+ const used = j.usage?.input_tokens_details?.image_tokens ?? 0;
129
+ raw.notes = [...(keyed ? ["chroma-key"] : []), ...(used > 0 ? [] : ["refs-ignored"])];
130
+ return raw;
131
+ },
132
+ };
133
+ }
@@ -0,0 +1,18 @@
1
+ export type { Asset, AssetKind, AssetMeta, ImageProvider, ImageRequest, ProviderInfo, RawResult, VideoProvider, VideoRequest } from "./types.ts";
2
+ export { findKey, hasKey, MissingKeyError, readKey, whereToPut } from "./keys.ts";
3
+ export { DEFAULT_IMAGE, imageProviders, pickImage, pickVideo, videoProviders } from "./registry.ts";
4
+ export {
5
+ cacheDir,
6
+ cacheKey,
7
+ DEFAULT_IMAGE_MAX_USD,
8
+ DEFAULT_MAX_USD,
9
+ estimateImageUsd,
10
+ genImage,
11
+ genVideo,
12
+ NeedsApproval,
13
+ planVideo,
14
+ type GenContext,
15
+ } from "./generate.ts";
16
+ export { aspectOf, seam, seamFrames, still, type SeamOptions } from "./seam.ts";
17
+ export { genSpecs, referencedIds, resolvePlan, type GenReport, type GenSpec } from "./plan.ts";
18
+ export { retime } from "./media.ts";
@@ -0,0 +1,60 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+
5
+ // 값은 이 모듈 밖으로 헤더에 실릴 때만 나간다. 오류 글에는 어디를 찾았는지만 적는다.
6
+ const SOURCES: Record<string, { env: string[]; files: string[] }> = {
7
+ opengateway: { env: ["OPENGATEWAY_API_KEY", "OG_API_KEY"], files: ["~/.config/opengateway.key"] },
8
+ fal: { env: ["FAL_KEY"], files: ["~/.config/fal.key", "~/.secrets/gcp-sm/fal-ai-api-key"] },
9
+ minimax: { env: ["MINIMAX_API_KEY"], files: ["~/.config/minimax.key"] },
10
+ gemini: { env: ["GEMINI_API_KEY", "GOOGLE_API_KEY"], files: ["~/.config/gemini.key"] },
11
+ ltx: { env: ["LTX_API_KEY"], files: ["~/.config/ltx.key"] },
12
+ h3: { env: ["KF_H3_URL"], files: [] },
13
+ };
14
+
15
+ export class MissingKeyError extends Error {
16
+ keyName: string;
17
+ constructor(keyName: string) {
18
+ super(`키 없음: ${keyName} — ${whereToPut(keyName)}`);
19
+ this.keyName = keyName;
20
+ }
21
+ }
22
+
23
+ function expand(p: string): string {
24
+ return p.startsWith("~/") ? join(homedir(), p.slice(2)) : p;
25
+ }
26
+
27
+ function sources(name: string): { env: string[]; files: string[] } {
28
+ const s = SOURCES[name] ?? { env: [], files: [] };
29
+ return { env: s.env, files: [...s.files, `~/.config/kasaframe/keys/${name}`] };
30
+ }
31
+
32
+ export function whereToPut(name: string): string {
33
+ const s = sources(name);
34
+ return `환경 변수 ${s.env.join("·") || "(없음)"} 또는 파일 ${s.files.join("·")}`;
35
+ }
36
+
37
+ export function findKey(name: string): string | null {
38
+ const s = sources(name);
39
+ for (const e of s.env) {
40
+ const v = process.env[e]?.trim();
41
+ if (v) return v;
42
+ }
43
+ for (const f of s.files) {
44
+ const p = expand(f);
45
+ if (!existsSync(p)) continue;
46
+ const v = readFileSync(p, "utf8").trim();
47
+ if (v) return v;
48
+ }
49
+ return null;
50
+ }
51
+
52
+ export function hasKey(name: string): boolean {
53
+ return findKey(name) !== null;
54
+ }
55
+
56
+ export function readKey(name: string): string {
57
+ const v = findKey(name);
58
+ if (!v) throw new MissingKeyError(name);
59
+ return v;
60
+ }
@@ -0,0 +1,150 @@
1
+ import { spawn } from "node:child_process";
2
+ import { renameSync } from "node:fs";
3
+
4
+ export function run(cmd: string, args: string[], input?: Uint8Array): Promise<Buffer> {
5
+ return new Promise((resolve, reject) => {
6
+ const p = spawn(cmd, args, { stdio: ["pipe", "pipe", "pipe"] });
7
+ const out: Buffer[] = [];
8
+ const err: Buffer[] = [];
9
+ p.stdout.on("data", (b: Buffer) => out.push(b));
10
+ p.stderr.on("data", (b: Buffer) => err.push(b));
11
+ p.on("error", reject);
12
+ p.on("close", (code) => {
13
+ if (code === 0) return resolve(Buffer.concat(out));
14
+ const tail = Buffer.concat(err).toString().trim().split("\n").slice(-4).join("\n");
15
+ reject(new Error(`${cmd} 실패(${code}): ${tail}`));
16
+ });
17
+ if (input) p.stdin.end(input);
18
+ else p.stdin.end();
19
+ });
20
+ }
21
+
22
+ const ff = (args: string[], input?: Uint8Array) => run("ffmpeg", ["-v", "error", "-y", ...args], input);
23
+
24
+ export interface Probe {
25
+ width: number;
26
+ height: number;
27
+ duration?: number;
28
+ fps?: number;
29
+ }
30
+
31
+ export async function probe(path: string): Promise<Probe> {
32
+ const out = await run("ffprobe", [
33
+ "-v", "error", "-select_streams", "v:0",
34
+ "-show_entries", "stream=width,height,r_frame_rate:format=duration",
35
+ "-of", "json", path,
36
+ ]);
37
+ const j = JSON.parse(out.toString());
38
+ const s = j.streams?.[0] ?? {};
39
+ const [n, d] = String(s.r_frame_rate ?? "0/1").split("/").map(Number);
40
+ const duration = Number(j.format?.duration);
41
+ return {
42
+ width: s.width,
43
+ height: s.height,
44
+ duration: Number.isFinite(duration) ? duration : undefined,
45
+ fps: d ? n / d : undefined,
46
+ };
47
+ }
48
+
49
+ // 64x64 로 줄인 알파에서 거의 투명한 칸의 비율. 0 이면 투명 배경이 안 먹은 것이다.
50
+ export async function transparentRatio(path: string): Promise<number> {
51
+ const raw = await ff(["-i", path, "-vf", "format=rgba,alphaextract,scale=64:64", "-f", "rawvideo", "-pix_fmt", "gray", "-"]);
52
+ let n = 0;
53
+ for (const v of raw) if (v < 250) n++;
54
+ return raw.length ? n / raw.length : 0;
55
+ }
56
+
57
+ // chromaKey 와 짝 — 투명 배경을 못 받는 모델에 이 문장을 붙여 그리게 한 뒤 키잉한다.
58
+ export const KEY_BG =
59
+ "Place the subject on a perfectly flat, uniform pure green (#00FF00) background with no shadow, gradient or texture.";
60
+
61
+ // ffmpeg 은 읽는 파일에 바로 쓰지 못한다 — 같은 경로면 옆 파일에 쓰고 바꿔 끼운다.
62
+ async function toFile(src: string, dst: string, args: (out: string) => string[]): Promise<void> {
63
+ const out = src === dst ? dst.replace(/(\.[a-z0-9]+)$/i, ".work$1") : dst;
64
+ await ff(args(out));
65
+ if (out !== dst) renameSync(out, dst);
66
+ }
67
+
68
+ export async function chromaKey(src: string, dst: string, color = "0x00FF00"): Promise<void> {
69
+ await toFile(src, dst, (out) => ["-i", src, "-vf", `colorkey=${color}:0.28:0.08,despill=type=green,format=rgba`, "-frames:v", "1", out]);
70
+ }
71
+
72
+ // 참조 그림은 실루엣만 전하면 되고 OG 는 큰 업로드를 413 으로 자른다 — 흰 바탕에 얹은 512px JPEG 로 보낸다.
73
+ export async function refJpeg(src: string, maxEdge = 512): Promise<Buffer> {
74
+ return ff([
75
+ "-i", src,
76
+ "-filter_complex",
77
+ `[0]format=rgba,scale='min(${maxEdge},iw)':'min(${maxEdge},ih)':force_original_aspect_ratio=decrease[fg];` +
78
+ `color=white:s=16x16[bg0];[bg0][fg]scale2ref[bg][fg2];[bg][fg2]overlay=format=auto,format=yuvj420p`,
79
+ "-frames:v", "1", "-q:v", "4", "-f", "image2", "-c:v", "mjpeg", "-",
80
+ ]);
81
+ }
82
+
83
+ export async function fitImage(src: string, dst: string, w: number, h: number): Promise<void> {
84
+ await toFile(src, dst, (out) => [
85
+ "-i", src,
86
+ "-vf", `scale=${w}:${h}:force_original_aspect_ratio=increase:flags=lanczos,crop=${w}:${h}`,
87
+ "-frames:v", "1", out,
88
+ ]);
89
+ }
90
+
91
+ // 생성 클립(16·24fps, 5초 남짓)을 장면 틈 길이·프로젝트 fps 에 정확히 맞춘다. 미디어 어댑터는 seek 만 하면 된다.
92
+ // size 를 주면 비율을 무시하고 늘린다 — H3 는 16:9 입력을 1344x768(1.75:1)로 잘라 내지 않고 늘려 받으니,
93
+ // 되늘려야 첫·끝 프레임이 앞뒤 장면과 픽셀 자리까지 겹친다(2026-10-08 실측).
94
+ export async function retime(src: string, dst: string, dur: number, fps: number, o: { smooth?: boolean; size?: { w: number; h: number } } = {}): Promise<void> {
95
+ const p = await probe(src);
96
+ if (!p.duration) throw new Error(`길이를 못 읽음: ${src}`);
97
+ const factor = dur / p.duration;
98
+ const rate = o.smooth ? `minterpolate=fps=${fps}:mi_mode=mci` : `fps=${fps}`;
99
+ const scale = o.size && (o.size.w !== p.width || o.size.h !== p.height) ? `,scale=${o.size.w}:${o.size.h}:flags=lanczos,setsar=1` : "";
100
+ await toFile(src, dst, (out) => [
101
+ "-i", src, "-an",
102
+ "-vf", `setpts=${factor.toFixed(6)}*PTS,${rate}${scale}`,
103
+ "-t", dur.toFixed(4),
104
+ "-c:v", "libx264", "-pix_fmt", "yuv420p", "-crf", "16", "-preset", "medium", "-movflags", "+faststart",
105
+ out,
106
+ ]);
107
+ }
108
+
109
+ // 모델은 첫 키프레임은 꽉 붙잡지만 끝 키프레임은 몇 픽셀 밀리고 빛이 달라진다(H3 실측 PSNR 37.8 대 24.6 dB).
110
+ // 양 끝 몇 프레임을 진짜 앞뒤 장면 프레임으로 녹여 넘겨 경계 프레임을 픽셀까지 같게 만든다.
111
+ export async function snapEnds(
112
+ src: string,
113
+ dst: string,
114
+ o: { first?: string; last?: string; frames: number; fps: number; dur: number; w: number; h: number },
115
+ ): Promise<void> {
116
+ if (!o.first && !o.last) return;
117
+ const ramp = Math.max(1, o.frames - 1) / o.fps;
118
+ const inputs = ["-i", src];
119
+ const chains: string[] = [];
120
+ let cur = "[0]";
121
+ let n = 1;
122
+ if (o.first) {
123
+ inputs.push("-loop", "1", "-framerate", String(o.fps), "-t", o.dur.toFixed(4), "-i", o.first);
124
+ chains.push(`[${n}]scale=${o.w}:${o.h},format=rgba,fade=t=out:st=0:d=${ramp.toFixed(4)}:alpha=1[f]`, `${cur}[f]overlay=eof_action=pass[v${n}]`);
125
+ cur = `[v${n}]`;
126
+ n++;
127
+ }
128
+ if (o.last) {
129
+ inputs.push("-loop", "1", "-framerate", String(o.fps), "-t", o.dur.toFixed(4), "-i", o.last);
130
+ const st = o.dur - 1 / o.fps - ramp;
131
+ chains.push(`[${n}]scale=${o.w}:${o.h},format=rgba,fade=t=in:st=${st.toFixed(4)}:d=${ramp.toFixed(4)}:alpha=1[l]`, `${cur}[l]overlay=eof_action=pass[v${n}]`);
132
+ cur = `[v${n}]`;
133
+ }
134
+ await toFile(src, dst, (out) => [
135
+ ...inputs,
136
+ "-filter_complex", chains.join(";"),
137
+ "-map", cur, "-r", String(o.fps), "-frames:v", String(Math.round(o.dur * o.fps)),
138
+ "-c:v", "libx264", "-pix_fmt", "yuv420p", "-crf", "16", "-preset", "medium", "-movflags", "+faststart",
139
+ out,
140
+ ]);
141
+ }
142
+
143
+ export async function dataUri(src: string, maxEdge = 1280): Promise<string> {
144
+ const jpg = await ff([
145
+ "-i", src,
146
+ "-vf", `scale='min(${maxEdge},iw)':'min(${maxEdge},ih)':force_original_aspect_ratio=decrease,format=yuvj420p`,
147
+ "-frames:v", "1", "-q:v", "3", "-f", "image2", "-c:v", "mjpeg", "-",
148
+ ]);
149
+ return `data:image/jpeg;base64,${jpg.toString("base64")}`;
150
+ }
@@ -0,0 +1,106 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { isAbsolute, join, resolve } from "node:path";
3
+ import { genImage, genVideo, NeedsApproval, type GenContext } from "./generate.ts";
4
+ import { seam } from "./seam.ts";
5
+
6
+ // plan.json 의 gen — 장면 assets 의 "gen:img:<id>"·"gen:vid:<id>" 가 가리키는 만들 거리.
7
+ export interface GenSpec {
8
+ id?: string;
9
+ kind: "img" | "vid";
10
+ scene?: string;
11
+ prompt: string;
12
+ size?: string; // img
13
+ transparent?: boolean;
14
+ quality?: string;
15
+ refs?: string[]; // 프로젝트 기준 경로
16
+ seam?: boolean; // vid: 이 id 가 kf.json 의 이음매 장면이다
17
+ dur?: number; // vid(이음매가 아닐 때)
18
+ first?: string;
19
+ last?: string;
20
+ provider?: string;
21
+ }
22
+
23
+ export interface GenReport {
24
+ id: string;
25
+ kind: "img" | "vid";
26
+ status: "made" | "cached" | "needs-approval" | "error";
27
+ path?: string;
28
+ costUsd?: number;
29
+ estimateUsd?: number;
30
+ provider?: string;
31
+ error?: string;
32
+ }
33
+
34
+ export function referencedIds(plan: any): { kind: "img" | "vid"; id: string }[] {
35
+ const out: { kind: "img" | "vid"; id: string }[] = [];
36
+ for (const s of plan.scenes ?? []) {
37
+ for (const a of s.assets ?? []) {
38
+ const m = /^gen:(img|vid):(.+)$/.exec(a);
39
+ if (m && !out.some((x) => x.id === m[2])) out.push({ kind: m[1] as "img" | "vid", id: m[2] });
40
+ }
41
+ }
42
+ return out;
43
+ }
44
+
45
+ // 정본은 id 를 품은 배열. id 를 키로 쓴 객체도 받는다.
46
+ export function genSpecs(plan: any): Record<string, GenSpec> {
47
+ const g = plan.gen ?? {};
48
+ if (!Array.isArray(g)) return g;
49
+ const out: Record<string, GenSpec> = {};
50
+ for (const [i, spec] of g.entries()) {
51
+ if (typeof spec?.id !== "string") throw new Error(`plan.json gen[${i}] 에 id 가 없다`);
52
+ out[spec.id] = spec;
53
+ }
54
+ return out;
55
+ }
56
+
57
+ function aspectSize(aspect: string | undefined): string {
58
+ return aspect === "9:16" ? "1080x1920" : aspect === "1:1" ? "1080x1080" : "1920x1080";
59
+ }
60
+
61
+ export async function resolvePlan(project: string, ctx: Omit<GenContext, "outDir">): Promise<GenReport[]> {
62
+ const dir = resolve(project);
63
+ const plan = JSON.parse(readFileSync(join(dir, "plan.json"), "utf8"));
64
+ const specs = genSpecs(plan);
65
+ const at = (p?: string) => (p ? (isAbsolute(p) ? p : join(dir, p)) : undefined);
66
+ const outDir = join(dir, "assets", "gen");
67
+ const full = { ...ctx, outDir };
68
+ const reports: GenReport[] = [];
69
+ const ids = referencedIds(plan);
70
+ for (const id of Object.keys(specs)) if (!ids.some((x) => x.id === id)) ids.push({ kind: specs[id].kind, id });
71
+ // 이음매는 앞뒤 장면을 찍어 만든다 — 그 장면들이 쓰는 그림·영상이 먼저 있어야 한다.
72
+ const rank = (id: string) => (specs[id]?.kind === "img" ? 0 : specs[id]?.seam ? 2 : 1);
73
+ ids.sort((a, b) => rank(a.id) - rank(b.id));
74
+
75
+ for (const { kind, id } of ids) {
76
+ const spec = specs[id];
77
+ if (!spec) {
78
+ reports.push({ id, kind, status: "error", error: `plan.json gen.${id} 이 없다 — prompt 를 적어야 만든다` });
79
+ continue;
80
+ }
81
+ try {
82
+ if (spec.kind === "img") {
83
+ const a = await genImage(
84
+ id,
85
+ { prompt: spec.prompt, size: spec.size ?? aspectSize(plan.aspect), transparent: spec.transparent, quality: spec.quality, refs: spec.refs?.map((r) => at(r)!) },
86
+ full,
87
+ spec.provider,
88
+ );
89
+ reports.push({ id, kind, status: a.cached ? "cached" : "made", path: a.path, costUsd: a.cached ? 0 : a.meta.costUsd, provider: a.meta.provider });
90
+ } else if (spec.seam) {
91
+ const a = await seam({ project: dir, scene: id, prompt: spec.prompt, provider: spec.provider, first: at(spec.first), last: at(spec.last) }, full);
92
+ reports.push({ id, kind, status: a.cached ? "cached" : "made", path: a.path, costUsd: a.cached ? 0 : a.meta.costUsd, provider: a.meta.provider });
93
+ } else {
94
+ const first = at(spec.first);
95
+ if (first && !existsSync(first)) throw new Error(`첫 프레임 없음: ${spec.first} — 그림을 먼저 만든다(gen 순서)`);
96
+ const req = { prompt: spec.prompt, duration: spec.dur ?? 5, aspect: plan.aspect ?? "16:9", firstFrame: first, lastFrame: at(spec.last) };
97
+ const a = await genVideo(id, req, full, spec.provider);
98
+ reports.push({ id, kind, status: a.cached ? "cached" : "made", path: a.path, costUsd: a.cached ? 0 : a.meta.costUsd, provider: a.meta.provider });
99
+ }
100
+ } catch (e) {
101
+ if (e instanceof NeedsApproval) reports.push({ id, kind, status: "needs-approval", estimateUsd: e.estimateUsd, provider: e.provider, error: e.message });
102
+ else reports.push({ id, kind, status: "error", error: e instanceof Error ? e.message : String(e) });
103
+ }
104
+ }
105
+ return reports;
106
+ }
@@ -0,0 +1,51 @@
1
+ import { ogImage } from "./image/og.ts";
2
+ import { hasKey } from "./keys.ts";
3
+ import type { ImageProvider, VideoProvider, VideoRequest } from "./types.ts";
4
+ import { FAL_MODELS, falVideo } from "./video/fal.ts";
5
+ import { h3Video } from "./video/h3.ts";
6
+ import { ltxVideo } from "./video/ltx.ts";
7
+ import { minimaxVideo } from "./video/minimax.ts";
8
+ import { veoVideo } from "./video/veo.ts";
9
+
10
+ export const DEFAULT_IMAGE = "og-gpt-image-2.5-sunburst";
11
+
12
+ export function imageProviders(): ImageProvider[] {
13
+ return [ogImage("openai/gpt-image-2.5-sunburst"), ogImage("openai/gpt-image-2.5-flare"), ogImage("openai/gpt-image-2")];
14
+ }
15
+
16
+ export function videoProviders(): VideoProvider[] {
17
+ return [
18
+ h3Video(),
19
+ ...FAL_MODELS.map((m) => falVideo(m.id)),
20
+ minimaxVideo(),
21
+ veoVideo("veo-3.1-lite-generate-preview"),
22
+ veoVideo("veo-3.1-fast-generate-preview"),
23
+ ltxVideo(),
24
+ ];
25
+ }
26
+
27
+ // id 는 앞뒤 접두 없이도 찾는다 — "wan-2.2-turbo" 와 "fal-wan-2.2-turbo" 가 같은 것.
28
+ function find<T extends { id: string }>(list: T[], id: string): T | undefined {
29
+ return list.find((p) => p.id === id) ?? list.find((p) => p.id.endsWith(`-${id}`) || p.id.replace(/^[a-z]+-/, "") === id);
30
+ }
31
+
32
+ export function pickImage(id = DEFAULT_IMAGE): ImageProvider {
33
+ const p = find(imageProviders(), id);
34
+ if (!p) throw new Error(`그림 공급자 없음: ${id} (있는 것: ${imageProviders().map((x) => x.id).join(" ")})`);
35
+ return p;
36
+ }
37
+
38
+ // 이름을 안 주면 키가 있는 것 중 가장 싼 것. 끝 프레임이 필요하면 그걸 받는 것만.
39
+ export function pickVideo(id: string | undefined, req: VideoRequest): VideoProvider {
40
+ const all = videoProviders();
41
+ if (id) {
42
+ const p = find(all, id);
43
+ if (!p) throw new Error(`영상 공급자 없음: ${id} (있는 것: ${all.map((x) => x.id).join(" ")})`);
44
+ return p;
45
+ }
46
+ const usable = all
47
+ .filter((p) => hasKey(p.keyName) && (!req.lastFrame || p.firstLast))
48
+ .sort((a, b) => a.estimateUsd(req) - b.estimateUsd(req));
49
+ if (!usable.length) throw new Error("키가 있는 영상 공급자가 없다 — kf gen models 로 무엇이 필요한지 본다");
50
+ return usable[0];
51
+ }
@@ -0,0 +1,103 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { join, resolve } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { genVideo, type GenContext } from "./generate.ts";
5
+ import { retime, run, snapEnds } from "./media.ts";
6
+ import type { Asset } from "./types.ts";
7
+
8
+ interface KfScene {
9
+ id: string;
10
+ from: number;
11
+ dur: number;
12
+ role?: string;
13
+ }
14
+
15
+ interface KfJson {
16
+ fps: number;
17
+ width: number;
18
+ height: number;
19
+ scenes: KfScene[];
20
+ }
21
+
22
+ export function aspectOf(w: number, h: number): string {
23
+ const r = w / h;
24
+ if (Math.abs(r - 16 / 9) < 0.05) return "16:9";
25
+ if (Math.abs(r - 9 / 16) < 0.05) return "9:16";
26
+ if (Math.abs(r - 1) < 0.05) return "1:1";
27
+ return r > 1 ? "16:9" : "9:16";
28
+ }
29
+
30
+ // 이음매 장면(role "seam")은 앞 장면의 마지막 프레임에서 시작해 뒤 장면의 첫 프레임에서 끝난다.
31
+ // 프레임 번호로 다룬다 — 초를 소수로 넘기면 반올림에 따라 앞뒤 장면이 한 프레임씩 바뀐다.
32
+ export function seamFrames(kf: KfJson, sceneId: string): { scene: KfScene; firstFrame?: number; lastFrame?: number } {
33
+ const i = kf.scenes.findIndex((s) => s.id === sceneId);
34
+ if (i < 0) throw new Error(`kf.json 에 장면 없음: ${sceneId}`);
35
+ const scene = kf.scenes[i];
36
+ const start = Math.round(scene.from * kf.fps);
37
+ return {
38
+ scene,
39
+ firstFrame: i > 0 ? Math.max(0, start - 1) : undefined,
40
+ lastFrame: i < kf.scenes.length - 1 ? Math.round((scene.from + scene.dur) * kf.fps) : undefined,
41
+ };
42
+ }
43
+
44
+ const KF = fileURLToPath(new URL("../../cli/src/kf.ts", import.meta.url));
45
+
46
+ export async function still(project: string, frame: number, out: string): Promise<void> {
47
+ await run(process.execPath, [KF, "still", project, "--frame", String(frame), "--out", out]);
48
+ if (!existsSync(out)) throw new Error(`kf still 이 ${out} 을 안 만들었다`);
49
+ }
50
+
51
+ export interface SeamOptions {
52
+ project: string;
53
+ scene: string;
54
+ prompt: string;
55
+ provider?: string;
56
+ first?: string; // 주면 kf still 대신 이 그림
57
+ last?: string;
58
+ smooth?: boolean;
59
+ resolution?: string;
60
+ snapFrames?: number; // 양 끝을 진짜 앞뒤 프레임으로 녹이는 프레임 수. 기본 0.15초어치, 0 이면 끈다
61
+ }
62
+
63
+ // 받은 클립은 장면 길이·fps 에 맞춰 assets/gen/<장면>.mp4 로 둔다. 장면 파일은 media 어댑터로 seek 만 한다.
64
+ export async function seam(o: SeamOptions, ctx: Omit<GenContext, "outDir"> & { outDir?: string }): Promise<Asset> {
65
+ const project = resolve(o.project);
66
+ const kf = JSON.parse(readFileSync(join(project, "kf.json"), "utf8")) as KfJson;
67
+ const { scene, firstFrame, lastFrame } = seamFrames(kf, o.scene);
68
+ const framesDir = join(project, "out", "seam");
69
+ mkdirSync(framesDir, { recursive: true });
70
+ let first = o.first;
71
+ let last = o.last;
72
+ if (!first && firstFrame !== undefined) {
73
+ first = join(framesDir, `${scene.id}-first.png`);
74
+ await still(project, firstFrame, first);
75
+ }
76
+ if (!last && lastFrame !== undefined) {
77
+ last = join(framesDir, `${scene.id}-last.png`);
78
+ await still(project, lastFrame, last);
79
+ }
80
+ const outDir = ctx.outDir ?? join(project, "assets", "gen");
81
+ const raw = await genVideo(
82
+ `${scene.id}.raw`,
83
+ { prompt: o.prompt, firstFrame: first, lastFrame: last, duration: scene.dur, aspect: aspectOf(kf.width, kf.height), resolution: o.resolution },
84
+ { ...ctx, outDir },
85
+ o.provider,
86
+ );
87
+ const fitted = join(outDir, `${scene.id}.mp4`);
88
+ await retime(raw.path, fitted, scene.dur, kf.fps, { smooth: o.smooth, size: { w: kf.width, h: kf.height } });
89
+ const snap = o.snapFrames ?? Math.round(kf.fps * 0.15);
90
+ if (snap > 0) await snapEnds(fitted, fitted, { first, last, frames: snap, fps: kf.fps, dur: scene.dur, w: kf.width, h: kf.height });
91
+ const meta = {
92
+ ...raw.meta,
93
+ id: scene.id,
94
+ file: `${scene.id}.mp4`,
95
+ width: kf.width,
96
+ height: kf.height,
97
+ duration: scene.dur,
98
+ fps: kf.fps,
99
+ notes: [...(raw.meta.notes ?? []), `retimed from ${raw.meta.file}`, ...(snap > 0 ? [`snap ${snap}f`] : []), ...(first ? [] : ["no-first-frame"]), ...(last ? [] : ["no-last-frame"])],
100
+ };
101
+ writeFileSync(join(outDir, `${scene.id}.json`), JSON.stringify(meta, null, 2) + "\n");
102
+ return { path: fitted, meta, cached: raw.cached };
103
+ }