@open-take/compositor 0.5.2 → 0.6.0

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 (439) hide show
  1. package/dist/cdp-CzT5BKNY.js +3 -0
  2. package/dist/cdp-QXbQfXna.js +373 -0
  3. package/dist/cdp-QXbQfXna.js.map +1 -0
  4. package/dist/index.d.ts +4327 -195
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +26282 -160
  7. package/dist/index.js.map +1 -1
  8. package/dist/server-841PMF2H.js +163 -0
  9. package/dist/server-841PMF2H.js.map +1 -0
  10. package/dist/server-C2aVv4-t.js +3 -0
  11. package/kit/audio.mjs +2366 -0
  12. package/kit/camera.js +587 -0
  13. package/kit/cursor.js +264 -0
  14. package/kit/cut.js +427 -0
  15. package/kit/draw.js +346 -0
  16. package/kit/look.js +589 -0
  17. package/kit/map.js +170 -0
  18. package/kit/media.js +3568 -0
  19. package/kit/motion.js +1115 -0
  20. package/kit/seams.js +1006 -0
  21. package/kit/stage.js +923 -0
  22. package/kit/time.js +812 -0
  23. package/kit/world.js +1072 -0
  24. package/package.json +7 -3
  25. package/src/ffmpeg.ts +63 -0
  26. package/src/film/beats.ts +1285 -0
  27. package/src/film/carry.ts +221 -0
  28. package/src/film/changes.ts +330 -0
  29. package/src/film/check.ts +996 -0
  30. package/src/film/clock.ts +2530 -0
  31. package/src/film/components-probe.ts +809 -0
  32. package/src/film/components.ts +1566 -0
  33. package/src/film/frames.ts +389 -0
  34. package/src/film/gallery.ts +3276 -0
  35. package/src/film/kit.ts +515 -0
  36. package/src/film/layout.ts +701 -0
  37. package/src/film/listen.ts +1979 -0
  38. package/src/film/look.ts +3829 -0
  39. package/src/film/materials.ts +976 -0
  40. package/src/film/moving.ts +248 -0
  41. package/src/film/part-files.ts +729 -0
  42. package/src/film/picture.ts +74 -0
  43. package/src/film/probe-text.ts +843 -0
  44. package/src/film/probe.ts +3456 -0
  45. package/src/film/provenance.ts +596 -0
  46. package/src/film/render.ts +2447 -0
  47. package/src/film/report.ts +1631 -0
  48. package/src/film/sheet.ts +156 -0
  49. package/src/film/study-declared.ts +123 -0
  50. package/src/film/study-picture.ts +791 -0
  51. package/src/film/study-sound.ts +416 -0
  52. package/src/film/study.ts +864 -0
  53. package/src/film/tuning.ts +155 -0
  54. package/src/film/viewer.ts +174 -0
  55. package/src/html/cdp.ts +575 -0
  56. package/src/html/server.ts +208 -0
  57. package/src/index.ts +202 -0
  58. package/src/launch-render.ts +3 -2
  59. package/src/math.ts +3 -3
  60. package/src/mjpeg-matroska.ts +228 -0
  61. package/src/render.ts +6 -4
  62. package/src/state-diff.ts +215 -0
  63. package/src/state-index.ts +99 -0
  64. package/src/types.ts +59 -1
  65. package/starter/PLAN.md +84 -0
  66. package/starter/START.md +460 -0
  67. package/starter/lib/anim.js +760 -0
  68. package/starter/lib/devices.js +1402 -0
  69. package/starter/lib/handoff.js +421 -0
  70. package/starter/lib/hud.js +1879 -0
  71. package/starter/lib/measure.mjs +154 -0
  72. package/starter/lib/moves.js +1095 -0
  73. package/starter/lib/palette.mjs +803 -0
  74. package/starter/lib/physics-seeds.mjs +198 -0
  75. package/starter/lib/physics.js +738 -0
  76. package/starter/lib/sampler.mjs +242 -0
  77. package/starter/lib/solid.js +727 -0
  78. package/starter/lib/space.js +1227 -0
  79. package/starter/lib/swarm.js +1398 -0
  80. package/starter/reference/motion.md +276 -0
  81. package/starter/reference/structure.md +466 -0
  82. package/starter/reference/travel/motion.md +433 -0
  83. package/starter/reference/travel/sound.md +241 -0
  84. package/starter/reference/travel/structure.md +518 -0
  85. package/starter/samples/NOTICE.txt +55 -0
  86. package/starter/samples/celli/celli_24_1.opus +0 -0
  87. package/starter/samples/celli/celli_24_3.opus +0 -0
  88. package/starter/samples/celli/celli_28_1.opus +0 -0
  89. package/starter/samples/celli/celli_28_3.opus +0 -0
  90. package/starter/samples/celli/celli_31_1.opus +0 -0
  91. package/starter/samples/celli/celli_31_3.opus +0 -0
  92. package/starter/samples/celli/celli_35_1.opus +0 -0
  93. package/starter/samples/celli/celli_35_3.opus +0 -0
  94. package/starter/samples/celli/celli_38_1.opus +0 -0
  95. package/starter/samples/celli/celli_38_3.opus +0 -0
  96. package/starter/samples/celli/celli_41_1.opus +0 -0
  97. package/starter/samples/celli/celli_41_3.opus +0 -0
  98. package/starter/samples/celli/celli_45_1.opus +0 -0
  99. package/starter/samples/celli/celli_45_3.opus +0 -0
  100. package/starter/samples/celli/celli_48_1.opus +0 -0
  101. package/starter/samples/celli/celli_48_3.opus +0 -0
  102. package/starter/samples/celli/celli_52_1.opus +0 -0
  103. package/starter/samples/celli/celli_52_3.opus +0 -0
  104. package/starter/samples/celli/celli_55_1.opus +0 -0
  105. package/starter/samples/celli/celli_55_3.opus +0 -0
  106. package/starter/samples/celli/celli_59_1.opus +0 -0
  107. package/starter/samples/celli/celli_59_3.opus +0 -0
  108. package/starter/samples/celli/celli_62_1.opus +0 -0
  109. package/starter/samples/celli/celli_62_3.opus +0 -0
  110. package/starter/samples/celli/celli_65_1.opus +0 -0
  111. package/starter/samples/celli/celli_65_3.opus +0 -0
  112. package/starter/samples/crash/crash_f_1.flac +0 -0
  113. package/starter/samples/crash/crash_fff_1.flac +0 -0
  114. package/starter/samples/ebass/ebass_0_x.flac +0 -0
  115. package/starter/samples/ebass/ebass_10_x.flac +0 -0
  116. package/starter/samples/ebass/ebass_11_x.flac +0 -0
  117. package/starter/samples/ebass/ebass_1_x.flac +0 -0
  118. package/starter/samples/ebass/ebass_2_x.flac +0 -0
  119. package/starter/samples/ebass/ebass_3_x.flac +0 -0
  120. package/starter/samples/ebass/ebass_4_x.flac +0 -0
  121. package/starter/samples/ebass/ebass_5_x.flac +0 -0
  122. package/starter/samples/ebass/ebass_6_x.flac +0 -0
  123. package/starter/samples/ebass/ebass_7_x.flac +0 -0
  124. package/starter/samples/ebass/ebass_8_x.flac +0 -0
  125. package/starter/samples/ebass/ebass_9_x.flac +0 -0
  126. package/starter/samples/ek_clap/ek_clap_1_1.flac +0 -0
  127. package/starter/samples/ek_hat/ek_hat_1_1.flac +0 -0
  128. package/starter/samples/ek_hat/ek_hat_1_2.flac +0 -0
  129. package/starter/samples/ek_kick/ek_kick_1_1.flac +0 -0
  130. package/starter/samples/ek_kick/ek_kick_1_2.flac +0 -0
  131. package/starter/samples/glock/glock_67_x.flac +0 -0
  132. package/starter/samples/glock/glock_72_x.flac +0 -0
  133. package/starter/samples/glock/glock_79_x.flac +0 -0
  134. package/starter/samples/glock/glock_84_x.flac +0 -0
  135. package/starter/samples/glock/glock_91_x.flac +0 -0
  136. package/starter/samples/glock/glock_96_x.flac +0 -0
  137. package/starter/samples/harp/harp_101_f.opus +0 -0
  138. package/starter/samples/harp/harp_28_f.opus +0 -0
  139. package/starter/samples/harp/harp_31_mp.opus +0 -0
  140. package/starter/samples/harp/harp_35_mf.opus +0 -0
  141. package/starter/samples/harp/harp_38_mf.opus +0 -0
  142. package/starter/samples/harp/harp_41_mf.opus +0 -0
  143. package/starter/samples/harp/harp_45_mf.opus +0 -0
  144. package/starter/samples/harp/harp_48_mf.opus +0 -0
  145. package/starter/samples/harp/harp_52_mf.opus +0 -0
  146. package/starter/samples/harp/harp_55_mf.opus +0 -0
  147. package/starter/samples/harp/harp_59_mf.opus +0 -0
  148. package/starter/samples/harp/harp_62_mf.opus +0 -0
  149. package/starter/samples/harp/harp_65_mf.opus +0 -0
  150. package/starter/samples/harp/harp_69_mf.opus +0 -0
  151. package/starter/samples/harp/harp_72_mf.opus +0 -0
  152. package/starter/samples/harp/harp_76_mf.opus +0 -0
  153. package/starter/samples/harp/harp_79_mf.opus +0 -0
  154. package/starter/samples/harp/harp_83_mf.opus +0 -0
  155. package/starter/samples/harp/harp_86_mf.opus +0 -0
  156. package/starter/samples/harp/harp_89_mf.opus +0 -0
  157. package/starter/samples/harp/harp_93_mf.opus +0 -0
  158. package/starter/samples/harp/harp_95_mf.opus +0 -0
  159. package/starter/samples/harp/harp_98_f.opus +0 -0
  160. package/starter/samples/manifest.json +2951 -0
  161. package/starter/samples/marimba/marimba_29_x.flac +0 -0
  162. package/starter/samples/marimba/marimba_36_x.flac +0 -0
  163. package/starter/samples/marimba/marimba_43_x.flac +0 -0
  164. package/starter/samples/marimba/marimba_47_x.flac +0 -0
  165. package/starter/samples/marimba/marimba_53_x.flac +0 -0
  166. package/starter/samples/marimba/marimba_60_x.flac +0 -0
  167. package/starter/samples/marimba/marimba_67_x.flac +0 -0
  168. package/starter/samples/marimba/marimba_71_x.flac +0 -0
  169. package/starter/samples/marimba/marimba_77_x.flac +0 -0
  170. package/starter/samples/marimba/marimba_84_x.flac +0 -0
  171. package/starter/samples/piano/piano_102_12.opus +0 -0
  172. package/starter/samples/piano/piano_102_16.opus +0 -0
  173. package/starter/samples/piano/piano_102_4.opus +0 -0
  174. package/starter/samples/piano/piano_102_8.opus +0 -0
  175. package/starter/samples/piano/piano_105_12.opus +0 -0
  176. package/starter/samples/piano/piano_105_16.opus +0 -0
  177. package/starter/samples/piano/piano_105_4.opus +0 -0
  178. package/starter/samples/piano/piano_105_8.opus +0 -0
  179. package/starter/samples/piano/piano_108_12.opus +0 -0
  180. package/starter/samples/piano/piano_108_16.opus +0 -0
  181. package/starter/samples/piano/piano_108_4.opus +0 -0
  182. package/starter/samples/piano/piano_108_8.opus +0 -0
  183. package/starter/samples/piano/piano_21_12.opus +0 -0
  184. package/starter/samples/piano/piano_21_16.opus +0 -0
  185. package/starter/samples/piano/piano_21_4.opus +0 -0
  186. package/starter/samples/piano/piano_21_8.opus +0 -0
  187. package/starter/samples/piano/piano_24_12.opus +0 -0
  188. package/starter/samples/piano/piano_24_16.opus +0 -0
  189. package/starter/samples/piano/piano_24_4.opus +0 -0
  190. package/starter/samples/piano/piano_24_8.opus +0 -0
  191. package/starter/samples/piano/piano_27_12.opus +0 -0
  192. package/starter/samples/piano/piano_27_16.opus +0 -0
  193. package/starter/samples/piano/piano_27_4.opus +0 -0
  194. package/starter/samples/piano/piano_27_8.opus +0 -0
  195. package/starter/samples/piano/piano_30_12.opus +0 -0
  196. package/starter/samples/piano/piano_30_16.opus +0 -0
  197. package/starter/samples/piano/piano_30_4.opus +0 -0
  198. package/starter/samples/piano/piano_30_8.opus +0 -0
  199. package/starter/samples/piano/piano_33_12.opus +0 -0
  200. package/starter/samples/piano/piano_33_16.opus +0 -0
  201. package/starter/samples/piano/piano_33_4.opus +0 -0
  202. package/starter/samples/piano/piano_33_8.opus +0 -0
  203. package/starter/samples/piano/piano_36_12.opus +0 -0
  204. package/starter/samples/piano/piano_36_16.opus +0 -0
  205. package/starter/samples/piano/piano_36_4.opus +0 -0
  206. package/starter/samples/piano/piano_36_8.opus +0 -0
  207. package/starter/samples/piano/piano_39_12.opus +0 -0
  208. package/starter/samples/piano/piano_39_16.opus +0 -0
  209. package/starter/samples/piano/piano_39_4.opus +0 -0
  210. package/starter/samples/piano/piano_39_8.opus +0 -0
  211. package/starter/samples/piano/piano_42_12.opus +0 -0
  212. package/starter/samples/piano/piano_42_16.opus +0 -0
  213. package/starter/samples/piano/piano_42_4.opus +0 -0
  214. package/starter/samples/piano/piano_42_8.opus +0 -0
  215. package/starter/samples/piano/piano_45_12.opus +0 -0
  216. package/starter/samples/piano/piano_45_16.opus +0 -0
  217. package/starter/samples/piano/piano_45_4.opus +0 -0
  218. package/starter/samples/piano/piano_45_8.opus +0 -0
  219. package/starter/samples/piano/piano_48_12.opus +0 -0
  220. package/starter/samples/piano/piano_48_16.opus +0 -0
  221. package/starter/samples/piano/piano_48_4.opus +0 -0
  222. package/starter/samples/piano/piano_48_8.opus +0 -0
  223. package/starter/samples/piano/piano_51_12.opus +0 -0
  224. package/starter/samples/piano/piano_51_16.opus +0 -0
  225. package/starter/samples/piano/piano_51_4.opus +0 -0
  226. package/starter/samples/piano/piano_51_8.opus +0 -0
  227. package/starter/samples/piano/piano_54_12.opus +0 -0
  228. package/starter/samples/piano/piano_54_16.opus +0 -0
  229. package/starter/samples/piano/piano_54_4.opus +0 -0
  230. package/starter/samples/piano/piano_54_8.opus +0 -0
  231. package/starter/samples/piano/piano_57_12.opus +0 -0
  232. package/starter/samples/piano/piano_57_16.opus +0 -0
  233. package/starter/samples/piano/piano_57_4.opus +0 -0
  234. package/starter/samples/piano/piano_57_8.opus +0 -0
  235. package/starter/samples/piano/piano_60_12.opus +0 -0
  236. package/starter/samples/piano/piano_60_16.opus +0 -0
  237. package/starter/samples/piano/piano_60_4.opus +0 -0
  238. package/starter/samples/piano/piano_60_8.opus +0 -0
  239. package/starter/samples/piano/piano_63_12.opus +0 -0
  240. package/starter/samples/piano/piano_63_16.opus +0 -0
  241. package/starter/samples/piano/piano_63_4.opus +0 -0
  242. package/starter/samples/piano/piano_63_8.opus +0 -0
  243. package/starter/samples/piano/piano_66_12.opus +0 -0
  244. package/starter/samples/piano/piano_66_16.opus +0 -0
  245. package/starter/samples/piano/piano_66_4.opus +0 -0
  246. package/starter/samples/piano/piano_66_8.opus +0 -0
  247. package/starter/samples/piano/piano_69_12.opus +0 -0
  248. package/starter/samples/piano/piano_69_16.opus +0 -0
  249. package/starter/samples/piano/piano_69_4.opus +0 -0
  250. package/starter/samples/piano/piano_69_8.opus +0 -0
  251. package/starter/samples/piano/piano_72_12.opus +0 -0
  252. package/starter/samples/piano/piano_72_16.opus +0 -0
  253. package/starter/samples/piano/piano_72_4.opus +0 -0
  254. package/starter/samples/piano/piano_72_8.opus +0 -0
  255. package/starter/samples/piano/piano_75_12.opus +0 -0
  256. package/starter/samples/piano/piano_75_16.opus +0 -0
  257. package/starter/samples/piano/piano_75_4.opus +0 -0
  258. package/starter/samples/piano/piano_75_8.opus +0 -0
  259. package/starter/samples/piano/piano_78_12.opus +0 -0
  260. package/starter/samples/piano/piano_78_16.opus +0 -0
  261. package/starter/samples/piano/piano_78_4.opus +0 -0
  262. package/starter/samples/piano/piano_78_8.opus +0 -0
  263. package/starter/samples/piano/piano_81_12.opus +0 -0
  264. package/starter/samples/piano/piano_81_16.opus +0 -0
  265. package/starter/samples/piano/piano_81_4.opus +0 -0
  266. package/starter/samples/piano/piano_81_8.opus +0 -0
  267. package/starter/samples/piano/piano_84_12.opus +0 -0
  268. package/starter/samples/piano/piano_84_16.opus +0 -0
  269. package/starter/samples/piano/piano_84_4.opus +0 -0
  270. package/starter/samples/piano/piano_84_8.opus +0 -0
  271. package/starter/samples/piano/piano_87_12.opus +0 -0
  272. package/starter/samples/piano/piano_87_16.opus +0 -0
  273. package/starter/samples/piano/piano_87_4.opus +0 -0
  274. package/starter/samples/piano/piano_87_8.opus +0 -0
  275. package/starter/samples/piano/piano_90_12.opus +0 -0
  276. package/starter/samples/piano/piano_90_16.opus +0 -0
  277. package/starter/samples/piano/piano_90_4.opus +0 -0
  278. package/starter/samples/piano/piano_90_8.opus +0 -0
  279. package/starter/samples/piano/piano_93_12.opus +0 -0
  280. package/starter/samples/piano/piano_93_16.opus +0 -0
  281. package/starter/samples/piano/piano_93_4.opus +0 -0
  282. package/starter/samples/piano/piano_93_8.opus +0 -0
  283. package/starter/samples/piano/piano_96_12.opus +0 -0
  284. package/starter/samples/piano/piano_96_16.opus +0 -0
  285. package/starter/samples/piano/piano_96_4.opus +0 -0
  286. package/starter/samples/piano/piano_96_8.opus +0 -0
  287. package/starter/samples/piano/piano_99_12.opus +0 -0
  288. package/starter/samples/piano/piano_99_16.opus +0 -0
  289. package/starter/samples/piano/piano_99_4.opus +0 -0
  290. package/starter/samples/piano/piano_99_8.opus +0 -0
  291. package/starter/samples/piano/piano_rel_102_x.opus +0 -0
  292. package/starter/samples/piano/piano_rel_105_x.opus +0 -0
  293. package/starter/samples/piano/piano_rel_108_x.opus +0 -0
  294. package/starter/samples/piano/piano_rel_21_x.opus +0 -0
  295. package/starter/samples/piano/piano_rel_24_x.opus +0 -0
  296. package/starter/samples/piano/piano_rel_27_x.opus +0 -0
  297. package/starter/samples/piano/piano_rel_30_x.opus +0 -0
  298. package/starter/samples/piano/piano_rel_33_x.opus +0 -0
  299. package/starter/samples/piano/piano_rel_36_x.opus +0 -0
  300. package/starter/samples/piano/piano_rel_39_x.opus +0 -0
  301. package/starter/samples/piano/piano_rel_42_x.opus +0 -0
  302. package/starter/samples/piano/piano_rel_45_x.opus +0 -0
  303. package/starter/samples/piano/piano_rel_48_x.opus +0 -0
  304. package/starter/samples/piano/piano_rel_51_x.opus +0 -0
  305. package/starter/samples/piano/piano_rel_54_x.opus +0 -0
  306. package/starter/samples/piano/piano_rel_57_x.opus +0 -0
  307. package/starter/samples/piano/piano_rel_60_x.opus +0 -0
  308. package/starter/samples/piano/piano_rel_63_x.opus +0 -0
  309. package/starter/samples/piano/piano_rel_66_x.opus +0 -0
  310. package/starter/samples/piano/piano_rel_69_x.opus +0 -0
  311. package/starter/samples/piano/piano_rel_72_x.opus +0 -0
  312. package/starter/samples/piano/piano_rel_75_x.opus +0 -0
  313. package/starter/samples/piano/piano_rel_78_x.opus +0 -0
  314. package/starter/samples/piano/piano_rel_81_x.opus +0 -0
  315. package/starter/samples/piano/piano_rel_84_x.opus +0 -0
  316. package/starter/samples/piano/piano_rel_87_x.opus +0 -0
  317. package/starter/samples/piano/piano_rel_90_x.opus +0 -0
  318. package/starter/samples/piano/piano_rel_93_x.opus +0 -0
  319. package/starter/samples/piano/piano_rel_96_x.opus +0 -0
  320. package/starter/samples/piano/piano_rel_99_x.opus +0 -0
  321. package/starter/samples/pizz/pizz_24_2.flac +0 -0
  322. package/starter/samples/pizz/pizz_28_2.flac +0 -0
  323. package/starter/samples/pizz/pizz_31_2.flac +0 -0
  324. package/starter/samples/pizz/pizz_35_2.flac +0 -0
  325. package/starter/samples/pizz/pizz_38_2.flac +0 -0
  326. package/starter/samples/pizz/pizz_41_2.flac +0 -0
  327. package/starter/samples/pizz/pizz_45_2.flac +0 -0
  328. package/starter/samples/pizz/pizz_48_2.flac +0 -0
  329. package/starter/samples/pizz/pizz_52_2.flac +0 -0
  330. package/starter/samples/pizz/pizz_57_2.flac +0 -0
  331. package/starter/samples/pizz/pizz_60_2.flac +0 -0
  332. package/starter/samples/pizz/pizz_64_2.flac +0 -0
  333. package/starter/samples/pizz/pizz_67_2.flac +0 -0
  334. package/starter/samples/pizz/pizz_71_2.flac +0 -0
  335. package/starter/samples/pizz/pizz_74_2.flac +0 -0
  336. package/starter/samples/pk_hat/pk_hat_2_1.flac +0 -0
  337. package/starter/samples/pk_hat/pk_hat_2_2.flac +0 -0
  338. package/starter/samples/pk_hat/pk_hat_6_1.flac +0 -0
  339. package/starter/samples/pk_hat/pk_hat_6_2.flac +0 -0
  340. package/starter/samples/pk_kick/pk_kick_3_1.flac +0 -0
  341. package/starter/samples/pk_kick/pk_kick_3_2.flac +0 -0
  342. package/starter/samples/pk_kick/pk_kick_7_1.flac +0 -0
  343. package/starter/samples/pk_kick/pk_kick_7_2.flac +0 -0
  344. package/starter/samples/pk_ohat/pk_ohat_3_1.flac +0 -0
  345. package/starter/samples/pk_ohat/pk_ohat_3_2.flac +0 -0
  346. package/starter/samples/pk_ohat/pk_ohat_6_1.flac +0 -0
  347. package/starter/samples/pk_ohat/pk_ohat_6_2.flac +0 -0
  348. package/starter/samples/pk_rim/pk_rim_2_1.flac +0 -0
  349. package/starter/samples/pk_rim/pk_rim_2_2.flac +0 -0
  350. package/starter/samples/pk_rim/pk_rim_5_1.flac +0 -0
  351. package/starter/samples/pk_rim/pk_rim_5_2.flac +0 -0
  352. package/starter/samples/pk_snare/pk_snare_3_1.flac +0 -0
  353. package/starter/samples/pk_snare/pk_snare_3_2.flac +0 -0
  354. package/starter/samples/pk_snare/pk_snare_7_1.flac +0 -0
  355. package/starter/samples/pk_snare/pk_snare_7_2.flac +0 -0
  356. package/starter/samples/shaker/shaker_1_1.flac +0 -0
  357. package/starter/samples/shaker/shaker_2_1.flac +0 -0
  358. package/starter/samples/snap/snap_r_1.flac +0 -0
  359. package/starter/samples/snap/snap_r_2.flac +0 -0
  360. package/starter/samples/snap/snap_r_3.flac +0 -0
  361. package/starter/samples/snap/snap_r_4.flac +0 -0
  362. package/starter/samples/snap/snap_r_5.flac +0 -0
  363. package/starter/samples/snap/snap_r_6.flac +0 -0
  364. package/starter/samples/tom/tom_3_1.flac +0 -0
  365. package/starter/samples/tom/tom_3_2.flac +0 -0
  366. package/starter/samples/tom/tom_4_1.flac +0 -0
  367. package/starter/samples/tom/tom_4_2.flac +0 -0
  368. package/starter/samples/vibes/vibes_41_1.opus +0 -0
  369. package/starter/samples/vibes/vibes_41_2.opus +0 -0
  370. package/starter/samples/vibes/vibes_45_1.opus +0 -0
  371. package/starter/samples/vibes/vibes_45_2.opus +0 -0
  372. package/starter/samples/vibes/vibes_48_1.opus +0 -0
  373. package/starter/samples/vibes/vibes_48_2.opus +0 -0
  374. package/starter/samples/vibes/vibes_52_1.opus +0 -0
  375. package/starter/samples/vibes/vibes_52_2.opus +0 -0
  376. package/starter/samples/vibes/vibes_55_1.opus +0 -0
  377. package/starter/samples/vibes/vibes_55_2.opus +0 -0
  378. package/starter/samples/vibes/vibes_59_1.opus +0 -0
  379. package/starter/samples/vibes/vibes_59_2.opus +0 -0
  380. package/starter/samples/vibes/vibes_62_1.opus +0 -0
  381. package/starter/samples/vibes/vibes_62_2.opus +0 -0
  382. package/starter/samples/vibes/vibes_65_1.opus +0 -0
  383. package/starter/samples/vibes/vibes_65_2.opus +0 -0
  384. package/starter/samples/vibes/vibes_69_1.opus +0 -0
  385. package/starter/samples/vibes/vibes_69_2.opus +0 -0
  386. package/starter/samples/vibes/vibes_72_1.opus +0 -0
  387. package/starter/samples/vibes/vibes_72_2.opus +0 -0
  388. package/starter/samples/vibes/vibes_76_1.opus +0 -0
  389. package/starter/samples/vibes/vibes_76_2.opus +0 -0
  390. package/starter/samples/violas/violas_36_1.opus +0 -0
  391. package/starter/samples/violas/violas_36_2.opus +0 -0
  392. package/starter/samples/violas/violas_38_1.opus +0 -0
  393. package/starter/samples/violas/violas_38_2.opus +0 -0
  394. package/starter/samples/violas/violas_40_1.opus +0 -0
  395. package/starter/samples/violas/violas_40_2.opus +0 -0
  396. package/starter/samples/violas/violas_43_1.opus +0 -0
  397. package/starter/samples/violas/violas_43_2.opus +0 -0
  398. package/starter/samples/violas/violas_47_1.opus +0 -0
  399. package/starter/samples/violas/violas_47_2.opus +0 -0
  400. package/starter/samples/violas/violas_50_1.opus +0 -0
  401. package/starter/samples/violas/violas_50_2.opus +0 -0
  402. package/starter/samples/violas/violas_53_1.opus +0 -0
  403. package/starter/samples/violas/violas_53_2.opus +0 -0
  404. package/starter/samples/violas/violas_57_1.opus +0 -0
  405. package/starter/samples/violas/violas_57_2.opus +0 -0
  406. package/starter/samples/violas/violas_60_1.opus +0 -0
  407. package/starter/samples/violas/violas_60_2.opus +0 -0
  408. package/starter/samples/violas/violas_64_1.opus +0 -0
  409. package/starter/samples/violas/violas_64_2.opus +0 -0
  410. package/starter/samples/violas/violas_67_1.opus +0 -0
  411. package/starter/samples/violas/violas_67_2.opus +0 -0
  412. package/starter/samples/violas/violas_71_1.opus +0 -0
  413. package/starter/samples/violas/violas_71_2.opus +0 -0
  414. package/starter/samples/violas/violas_74_1.opus +0 -0
  415. package/starter/samples/violas/violas_74_2.opus +0 -0
  416. package/starter/samples/violins/violins_43_1.opus +0 -0
  417. package/starter/samples/violins/violins_43_2.opus +0 -0
  418. package/starter/samples/violins/violins_45_1.opus +0 -0
  419. package/starter/samples/violins/violins_45_2.opus +0 -0
  420. package/starter/samples/violins/violins_47_1.opus +0 -0
  421. package/starter/samples/violins/violins_47_2.opus +0 -0
  422. package/starter/samples/violins/violins_50_1.opus +0 -0
  423. package/starter/samples/violins/violins_50_2.opus +0 -0
  424. package/starter/samples/violins/violins_54_1.opus +0 -0
  425. package/starter/samples/violins/violins_54_2.opus +0 -0
  426. package/starter/samples/violins/violins_57_1.opus +0 -0
  427. package/starter/samples/violins/violins_57_2.opus +0 -0
  428. package/starter/samples/violins/violins_60_1.opus +0 -0
  429. package/starter/samples/violins/violins_60_2.opus +0 -0
  430. package/starter/samples/violins/violins_64_1.opus +0 -0
  431. package/starter/samples/violins/violins_64_2.opus +0 -0
  432. package/starter/samples/violins/violins_67_1.opus +0 -0
  433. package/starter/samples/violins/violins_67_2.opus +0 -0
  434. package/starter/samples/violins/violins_71_1.opus +0 -0
  435. package/starter/samples/violins/violins_71_2.opus +0 -0
  436. package/starter/samples/violins/violins_74_1.opus +0 -0
  437. package/starter/samples/violins/violins_74_2.opus +0 -0
  438. package/starter/samples/woodblock/woodblock_f_1.flac +0 -0
  439. package/starter/samples/woodblock/woodblock_f_2.flac +0 -0
@@ -0,0 +1,843 @@
1
+ // Text at film scale, for `film probe`: how much of the frame is text at each
2
+ // moment, how big that text is drawn on the film and how big it would be on a
3
+ // phone, and whether the film's own text sits over pictures, the product's
4
+ // text or the subject a segment names.
5
+ //
6
+ // Two readers, each labelled for what it is:
7
+ // - the page (measured): the film's page played on the film clock, as
8
+ // `film check` plays it, and every visible piece of text read where its
9
+ // glyphs are drawn and at the size they are drawn — the film's own text
10
+ // (titles, captions) apart from the product's text in a saved screen
11
+ // (an <iframe>);
12
+ // - the picture (estimated): the finished film's frames read for text,
13
+ // which is the only way to see text inside a screenshot or a recording.
14
+ // On macOS the system's own text recognizer (Vision, through a small
15
+ // Swift helper compiled once into the temp dir) reads each line and its
16
+ // height; elsewhere, or when that fails, an edge count says only how much
17
+ // of the frame looks like text, with no size.
18
+ //
19
+ // Information only: no size is called too small and no amount too much.
20
+
21
+ import { spawn } from "node:child_process";
22
+ import { createHash } from "node:crypto";
23
+ import { existsSync } from "node:fs";
24
+ import { mkdir, mkdtemp, rename, rm, writeFile } from "node:fs/promises";
25
+ import { platform, tmpdir } from "node:os";
26
+ import { join } from "node:path";
27
+ import { runTool } from "../ffmpeg";
28
+ import { type HtmlBrowser, launchHtmlBrowser } from "../html/cdp";
29
+ import { serveDirectory } from "../html/server";
30
+ import { type FilmPageInfo, filmClockSource } from "./clock";
31
+ import { resolveFilmPage } from "./render";
32
+
33
+ /** the width (CSS px) a film is taken to be shown at on a phone: a phone held
34
+ * upright, the film across the feed */
35
+ export const PHONE_W = 390;
36
+
37
+ export type Box = [number, number, number, number];
38
+
39
+ /** one moment of the page, as read (frame CSS px) */
40
+ export type PageTextSample = {
41
+ t: number;
42
+ /** the film's own text, one entry per block of text */
43
+ own: { text: string; chars: number; px: number; boxes: Box[] }[];
44
+ /** the product's text inside saved screens (iframes), summed */
45
+ product: { chars: number; pieces: number };
46
+ /** char-weighted sizes of all text drawn: [px, chars] */
47
+ sizes: [number, number][];
48
+ /** share of the frame under the lines of: own text, product text (their
49
+ * line boxes, summed) */
50
+ ownShare: number;
51
+ productShare: number;
52
+ /** the grid of cells the product's text covers, row by row, "1" covered */
53
+ grid: string[];
54
+ /** pictures on the page: img, video, canvas, iframe, a background image;
55
+ * `src` the file it shows (a URL as the page has it; after
56
+ * readPageText, a path from the served root when it is served there);
57
+ * for an <img> or a <video>, `drawn`: where its whole picture is drawn
58
+ * (object-fit and transforms taken in, the clip not), and its own size
59
+ * in px (a video's once its size is known); a <canvas> the same, its
60
+ * own size its width × height. `why` "tilt": a layer under a CSS
61
+ * perspective bigger on the frame than the frame, which Chrome draws at
62
+ * about the frame's area and stretches — `natural` the size it draws it */
63
+ media: {
64
+ label: string;
65
+ box: Box;
66
+ src?: string;
67
+ drawn?: Box;
68
+ natural?: [number, number];
69
+ why?: "tilt";
70
+ }[];
71
+ /** where each named subject (a CSS selector) is drawn; null: an element
72
+ * matches but is not visible; absent: no element matches */
73
+ subjects: Record<string, Box | null>;
74
+ /** what `extra` gave at this time, when it was asked */
75
+ extra?: unknown;
76
+ };
77
+
78
+ export type PageTextReading = {
79
+ samples: PageTextSample[];
80
+ /** the folder the page was served from: the media's `src` paths are in it */
81
+ root: string;
82
+ width: number;
83
+ height: number;
84
+ /** window.__film.segments() as the page has it now (unchecked) */
85
+ segments: unknown;
86
+ /** times the page threw at, twice, and was not read */
87
+ failed: { t: number; error: string }[];
88
+ /** what the page says it is (window.__ot.prepare()) */
89
+ info: FilmPageInfo;
90
+ };
91
+
92
+ // --- the page --------------------------------------------------------------------------
93
+
94
+ export function pageTextSource(subjects: readonly string[]): string {
95
+ return `(() => { const __name = (f) => f; return (${probeText.toString()})(${JSON.stringify(subjects)}); })()`;
96
+ }
97
+
98
+ // serialized into the page: nothing outside its own body
99
+ function probeText(subjects: string[]): Omit<PageTextSample, "t"> {
100
+ const vw = innerWidth;
101
+ const vh = innerHeight;
102
+ const GXp = 96;
103
+ const GYp = 54;
104
+ // faded text is still text on screen; a cross-fade's two scenes both count
105
+ const MIN_O = 0.15;
106
+ type B = { x: number; y: number; w: number; h: number };
107
+ const inter = (a: B, b: B): B | null => {
108
+ const x0 = Math.max(a.x, b.x);
109
+ const y0 = Math.max(a.y, b.y);
110
+ const x1 = Math.min(a.x + a.w, b.x + b.w);
111
+ const y1 = Math.min(a.y + a.h, b.y + b.h);
112
+ return x1 > x0 && y1 > y0 ? { x: x0, y: y0, w: x1 - x0, h: y1 - y0 } : null;
113
+ };
114
+ const alphas = new Map<Element, number>();
115
+ const alphaOf = (el: Element | null, view: Window): number => {
116
+ if (!el) return 1;
117
+ const hit = alphas.get(el);
118
+ if (hit !== undefined) return hit;
119
+ const cs = view.getComputedStyle(el);
120
+ const own = cs.display === "none" ? 0 : Number(cs.opacity) || 0;
121
+ const o = own === 0 ? 0 : own * alphaOf(el.parentElement, view);
122
+ alphas.set(el, o);
123
+ return o;
124
+ };
125
+ const opacityOf = (el: Element, view: Window): number => {
126
+ const v = view.getComputedStyle(el).visibility;
127
+ return v === "hidden" || v === "collapse" ? 0 : alphaOf(el, view);
128
+ };
129
+ /** what of the frame the boxes holding an element let show: every
130
+ * ancestor whose overflow is not visible (the root and body aside) */
131
+ const clips = new Map<Element, B | null>();
132
+ const clipOf = (el: Element | null, view: Window, map: (r: DOMRect) => B, base: B): B | null => {
133
+ if (!el) return base;
134
+ if (clips.has(el)) return clips.get(el)!;
135
+ const doc = el.ownerDocument;
136
+ let c = clipOf(el.parentElement, view, map, base);
137
+ if (c && el !== doc.documentElement && el !== doc.body) {
138
+ const cs = view.getComputedStyle(el);
139
+ if (cs.display !== "contents" && (cs.overflowX !== "visible" || cs.overflowY !== "visible"))
140
+ c = inter(c, map(el.getBoundingClientRect()));
141
+ }
142
+ clips.set(el, c);
143
+ return c;
144
+ };
145
+ const describe = (el: Element) => {
146
+ const id = el.id ? `#${el.id}` : "";
147
+ const text = (el.textContent || "").replace(/\s+/g, " ").trim().slice(0, 40);
148
+ return `${el.localName}${id}${text ? ` "${text}"` : ""}`;
149
+ };
150
+ const scaleOf = (el: Element): number => {
151
+ const drawn = el.getBoundingClientRect().height;
152
+ const laid = (el as HTMLElement).offsetHeight;
153
+ if (typeof laid === "number") return laid > 0 ? drawn / laid : 1;
154
+ try {
155
+ const own = (el as SVGGraphicsElement).getBBox?.().height;
156
+ return own && own > 0 ? drawn / own : 1;
157
+ } catch {
158
+ return 1;
159
+ }
160
+ };
161
+ const grid = new Uint8Array(GXp * GYp);
162
+ let ownArea = 0;
163
+ let prodArea = 0;
164
+ const mark = (b: B, isOwn: boolean) => {
165
+ if (isOwn) {
166
+ ownArea += b.w * b.h;
167
+ return;
168
+ }
169
+ prodArea += b.w * b.h;
170
+ const x0 = Math.max(0, Math.floor((b.x / vw) * GXp));
171
+ const x1 = Math.min(GXp - 1, Math.floor(((b.x + b.w) / vw) * GXp - 1e-9));
172
+ const y0 = Math.max(0, Math.floor((b.y / vh) * GYp));
173
+ const y1 = Math.min(GYp - 1, Math.floor(((b.y + b.h) / vh) * GYp - 1e-9));
174
+ for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) grid[y * GXp + x] = 1;
175
+ };
176
+ const own: Omit<PageTextSample, "t">["own"] = [];
177
+ const product = { chars: 0, pieces: 0 };
178
+ const sizes = new Map<number, number>();
179
+ const frame: B = { x: 0, y: 0, w: vw, h: vh };
180
+
181
+ const collect = (
182
+ doc: Document,
183
+ view: Window,
184
+ map: (r: DOMRect) => B,
185
+ clip: B,
186
+ baseO: number,
187
+ scale: number,
188
+ isOwn: boolean,
189
+ ) => {
190
+ if (!doc.body) return;
191
+ const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_TEXT);
192
+ const range = doc.createRange();
193
+ const seen = new Map<Element, { text: string; chars: number; px: number; boxes: Box[] }>();
194
+ for (let n = walker.nextNode(); n; n = walker.nextNode()) {
195
+ const value = n.nodeValue ?? "";
196
+ const chars = value.replace(/\s+/g, "").length;
197
+ if (!chars) continue;
198
+ const el = n.parentElement;
199
+ if (!el || ["SCRIPT", "STYLE", "NOSCRIPT", "TEMPLATE"].includes(el.tagName)) continue;
200
+ if (baseO * opacityOf(el, view) < MIN_O) continue;
201
+ const visible = clipOf(el, view, map, clip);
202
+ if (!visible) continue;
203
+ range.selectNodeContents(n);
204
+ // characters shown: by the width of each line that shows (a line box's
205
+ // empty leading cut off at an edge costs no characters), a line counting
206
+ // when at least half its height shows
207
+ let full = 0;
208
+ let shown = 0;
209
+ const boxes: B[] = [];
210
+ for (const r of Array.from(range.getClientRects())) {
211
+ if (r.width < 1 || r.height < 1) continue;
212
+ const m = map(r);
213
+ full += m.w;
214
+ const v = inter(m, visible);
215
+ if (!v || v.w < 1 || v.h < 1) continue;
216
+ if (v.h >= 0.5 * m.h) shown += v.w;
217
+ boxes.push(v);
218
+ }
219
+ if (!boxes.length) continue;
220
+ // the block it belongs to: a caption split into spans is one piece
221
+ let owner: Element = el;
222
+ while (
223
+ owner.parentElement &&
224
+ owner.parentElement !== doc.body &&
225
+ /^(inline|inline-block|inline-flex|contents)$/.test(view.getComputedStyle(owner).display)
226
+ )
227
+ owner = owner.parentElement;
228
+ const px = (Number.parseFloat(view.getComputedStyle(el).fontSize) || 0) * scaleOf(el) * scale;
229
+ const seenChars = shown ? Math.max(1, Math.round((chars * shown) / Math.max(1, full))) : 0;
230
+ const key = Math.round(px * 2) / 2;
231
+ sizes.set(key, (sizes.get(key) ?? 0) + seenChars);
232
+ for (const b of boxes) mark(b, isOwn);
233
+ if (isOwn) {
234
+ const e = seen.get(owner);
235
+ const rb = boxes.map(
236
+ (b) => [Math.round(b.x), Math.round(b.y), Math.round(b.w), Math.round(b.h)] as Box,
237
+ );
238
+ if (e) {
239
+ e.chars += seenChars;
240
+ e.px = Math.min(e.px, px);
241
+ e.boxes.push(...rb);
242
+ } else {
243
+ const entry = { text: describe(owner), chars: seenChars, px, boxes: rb };
244
+ seen.set(owner, entry);
245
+ own.push(entry);
246
+ }
247
+ } else {
248
+ product.chars += seenChars;
249
+ if (!seen.has(owner)) {
250
+ seen.set(owner, { text: "", chars: 0, px, boxes: [] });
251
+ product.pieces++;
252
+ }
253
+ }
254
+ }
255
+ };
256
+
257
+ const top = (r: DOMRect): B => ({ x: r.left, y: r.top, w: r.width, h: r.height });
258
+ collect(document, window, top, frame, 1, 1, true);
259
+ // same-origin iframes: the product's saved screens
260
+ const frames: { f: HTMLIFrameElement; doc: Document; map: (r: DOMRect) => B; sy: number }[] = [];
261
+ for (const f of Array.from(document.querySelectorAll("iframe"))) {
262
+ let doc: Document | null = null;
263
+ try {
264
+ doc = f.contentDocument;
265
+ } catch {
266
+ continue;
267
+ }
268
+ const view = f.contentWindow;
269
+ if (!doc?.body || !view) continue;
270
+ const o = opacityOf(f, window);
271
+ if (o < MIN_O) continue;
272
+ const fb = f.getBoundingClientRect();
273
+ const sx = f.offsetWidth ? fb.width / f.offsetWidth : 1;
274
+ const sy = f.offsetHeight ? fb.height / f.offsetHeight : 1;
275
+ const left = fb.left + f.clientLeft * sx;
276
+ const topY = fb.top + f.clientTop * sy;
277
+ const map = (r: DOMRect): B => ({
278
+ x: left + r.left * sx,
279
+ y: topY + r.top * sy,
280
+ w: r.width * sx,
281
+ h: r.height * sy,
282
+ });
283
+ const shown = clipOf(f, window, top, frame);
284
+ const box = inter({ x: left, y: topY, w: f.clientWidth * sx, h: f.clientHeight * sy }, frame);
285
+ const clip = shown && box && inter(shown, box);
286
+ if (!clip) continue;
287
+ frames.push({ f, doc, map, sy });
288
+ collect(doc, view, map, clip, o, sy, false);
289
+ }
290
+
291
+ // pictures on the film's own page, as far as they show
292
+ const media: Omit<PageTextSample, "t">["media"] = [];
293
+ for (const el of Array.from(document.body?.querySelectorAll("*") ?? [])) {
294
+ const tag = el.localName;
295
+ let picture = ["img", "video", "canvas", "iframe", "picture", "image"].includes(tag);
296
+ let src = "";
297
+ if (!picture) {
298
+ const bg = getComputedStyle(el).backgroundImage;
299
+ picture = !!bg && bg !== "none" && /url\(/.test(bg);
300
+ if (picture) src = /url\(["']?([^"')]+)["']?\)/.exec(bg)?.[1] ?? "";
301
+ } else if (tag === "img" || tag === "video") {
302
+ const m = el as HTMLImageElement;
303
+ src = m.currentSrc || m.src || "";
304
+ } else if (tag === "iframe") src = (el as HTMLIFrameElement).src || "";
305
+ else if (tag === "image") {
306
+ const href = (el as SVGImageElement).href?.baseVal ?? "";
307
+ try {
308
+ src = href ? new URL(href, document.baseURI).href : "";
309
+ } catch {}
310
+ }
311
+ if (!picture) continue;
312
+ // a canvas is often a texture laid over everything at a few percent (grain)
313
+ if (opacityOf(el, window) < (tag === "canvas" ? 0.02 : MIN_O)) continue;
314
+ const r = el.getBoundingClientRect();
315
+ if (r.width * r.height < 0.01 * vw * vh) continue;
316
+ const c = clipOf(el.parentElement, window, top, frame);
317
+ const v = c && inter(top(r), c);
318
+ if (!v) continue;
319
+ // an image's or a video's own pixels on the frame: its box as laid
320
+ // out, fitted as object-fit says (centred), then as transformed
321
+ let drawn: Box | undefined;
322
+ let natural: [number, number] | undefined;
323
+ if (tag === "img" || tag === "video" || tag === "canvas") {
324
+ const im = el as HTMLImageElement & HTMLVideoElement & HTMLCanvasElement;
325
+ const nw = tag === "img" ? im.naturalWidth : tag === "video" ? im.videoWidth : im.width;
326
+ const nh = tag === "img" ? im.naturalHeight : tag === "video" ? im.videoHeight : im.height;
327
+ const lw = im.offsetWidth;
328
+ const lh = im.offsetHeight;
329
+ if (nw && nh && lw && lh) {
330
+ const fit = getComputedStyle(im).objectFit;
331
+ const contain = Math.min(lw / nw, lh / nh);
332
+ const [fx, fy] =
333
+ fit === "contain"
334
+ ? [contain, contain]
335
+ : fit === "cover"
336
+ ? [Math.max(lw / nw, lh / nh), Math.max(lw / nw, lh / nh)]
337
+ : fit === "none"
338
+ ? [1, 1]
339
+ : fit === "scale-down"
340
+ ? [Math.min(1, contain), Math.min(1, contain)]
341
+ : [lw / nw, lh / nh];
342
+ const kx = r.width / lw;
343
+ const ky = r.height / lh;
344
+ const dw = nw * fx * kx;
345
+ const dh = nh * fy * ky;
346
+ drawn = [
347
+ Math.round((r.left + (r.width - dw) / 2) * 10) / 10,
348
+ Math.round((r.top + (r.height - dh) / 2) * 10) / 10,
349
+ Math.round(dw * 10) / 10,
350
+ Math.round(dh * 10) / 10,
351
+ ];
352
+ natural = [nw, nh];
353
+ }
354
+ }
355
+ media.push({
356
+ label: describe(el).slice(0, 60),
357
+ box: [Math.round(v.x), Math.round(v.y), Math.round(v.w), Math.round(v.h)],
358
+ ...(src && !src.startsWith("data:") && !src.startsWith("blob:") ? { src } : {}),
359
+ ...(drawn ? { drawn, natural } : {}),
360
+ });
361
+ }
362
+
363
+ // layers under a perspective: Chrome draws one whose area on the frame
364
+ // would pass the frame's at about the frame's area and stretches it
365
+ // (measured: a 950×450 box tilted at 2.3× soft, 800×400 sharp, at
366
+ // 1920×1080; --supersample does not change it), so its text goes soft
367
+ const under = (el: Element) => {
368
+ for (let a = el.parentElement; a; a = a.parentElement)
369
+ if (getComputedStyle(a).perspective !== "none") return true;
370
+ return false;
371
+ };
372
+ const tilted: Element[] = [];
373
+ for (const el of Array.from(document.body?.querySelectorAll("*") ?? [])) {
374
+ const tf = getComputedStyle(el).transform;
375
+ if (!tf.startsWith("matrix3d(")) continue;
376
+ if (tilted.some((t) => t.contains(el))) continue;
377
+ // its own perspective() leaves m34 (the 12th number) non-zero
378
+ const m34 = Number(tf.slice(9, -1).split(",")[11]);
379
+ if (!(m34 !== 0 || under(el))) continue;
380
+ if (opacityOf(el, window) < MIN_O) continue;
381
+ const h = el as HTMLElement;
382
+ const lw = h.offsetWidth;
383
+ const lh = h.offsetHeight;
384
+ if (!lw || !lh) continue;
385
+ const clips = getComputedStyle(el).overflow !== "visible";
386
+ const aw = clips ? lw : Math.max(lw, el.scrollWidth);
387
+ const ah = clips ? lh : Math.max(lh, el.scrollHeight);
388
+ const r = el.getBoundingClientRect();
389
+ const sx = r.width / lw;
390
+ const sy = r.height / lh;
391
+ const k = Math.sqrt((vw * vh) / (aw * ah));
392
+ if (aw * ah * sx * sy <= vw * vh || Math.max(sx, sy) <= k * 1.01) continue;
393
+ const c = clipOf(el.parentElement, window, top, frame);
394
+ const v = c && inter(top(r), c);
395
+ if (!v) continue;
396
+ tilted.push(el);
397
+ media.push({
398
+ label: describe(el).slice(0, 60),
399
+ box: [Math.round(v.x), Math.round(v.y), Math.round(v.w), Math.round(v.h)],
400
+ drawn: [
401
+ Math.round(r.left * 10) / 10,
402
+ Math.round(r.top * 10) / 10,
403
+ Math.round(r.width * 10) / 10,
404
+ Math.round(r.height * 10) / 10,
405
+ ],
406
+ natural: [Math.round(lw * k), Math.round(lh * k)],
407
+ why: "tilt",
408
+ });
409
+ }
410
+
411
+ // the subjects the segments name, where a selector finds them
412
+ const where: Record<string, Box | null> = {};
413
+ for (const sel of subjects) {
414
+ let box: B | null = null;
415
+ const find = (doc: Document, map: (r: DOMRect) => B, view: Window) => {
416
+ let el: Element | null = null;
417
+ try {
418
+ el = doc.querySelector(sel);
419
+ } catch {
420
+ return false;
421
+ }
422
+ if (!el) return false;
423
+ if (opacityOf(el, view) < MIN_O) return true;
424
+ const r = el.getBoundingClientRect();
425
+ if (r.width < 1 || r.height < 1) return true;
426
+ box = map(r);
427
+ return true;
428
+ };
429
+ const found =
430
+ find(document, top, window) ||
431
+ frames.some((fr) => !!fr.f.contentWindow && find(fr.doc, fr.map, fr.f.contentWindow));
432
+ if (!found) continue;
433
+ const b = box as B | null;
434
+ where[sel] = b ? [Math.round(b.x), Math.round(b.y), Math.round(b.w), Math.round(b.h)] : null;
435
+ }
436
+
437
+ const share = (a: number) => Math.round(Math.min(1, a / (vw * vh)) * 1000) / 1000;
438
+ const rows: string[] = [];
439
+ for (let y = 0; y < GYp; y++)
440
+ rows.push(Array.from(grid.subarray(y * GXp, (y + 1) * GXp)).join(""));
441
+ return {
442
+ own: own.map((o) => ({ ...o, px: Math.round(o.px * 10) / 10 })),
443
+ product,
444
+ sizes: [...sizes].sort((a, b) => a[0] - b[0]),
445
+ ownShare: share(ownArea),
446
+ productShare: share(prodArea),
447
+ grid: rows,
448
+ media,
449
+ subjects: where,
450
+ };
451
+ }
452
+
453
+ /** Play the film's page on the film clock, as `film check` does, and read its
454
+ * text at each of `times` (the film's own seconds; the page is at from + t). */
455
+ export async function readPageText(opts: {
456
+ page: string;
457
+ root?: string;
458
+ chromePath: string;
459
+ from: number;
460
+ times: readonly number[];
461
+ width?: number;
462
+ height?: number;
463
+ fallback: { width: number; height: number };
464
+ /** CSS selectors to locate at each time */
465
+ subjects?: readonly string[];
466
+ /** an expression asked of the page at each time too (awaited), kept on the
467
+ * sample as `extra` — another reading on the same seek; a function makes
468
+ * it for each time, given the page's time (from + t) */
469
+ extra?: string | ((pageT: number) => string);
470
+ onConsole?: (level: string, text: string) => void;
471
+ }): Promise<PageTextReading> {
472
+ const { root, urlPath } = resolveFilmPage(opts.page, opts.root);
473
+ const server = await serveDirectory(root);
474
+ const open = (width: number, height: number) =>
475
+ launchHtmlBrowser({
476
+ chromePath: opts.chromePath,
477
+ url: `${server.url}${urlPath}`,
478
+ width,
479
+ height,
480
+ initScript: filmClockSource({ seed: 0x5eed, epochMs: Date.now() }),
481
+ stillAnimations: true,
482
+ onConsole: opts.onConsole,
483
+ timeoutMs: 120_000,
484
+ profilePrefix: "open-take-probe-text-",
485
+ });
486
+ const prepare = async (b: HtmlBrowser) => {
487
+ if (!(await b.evaluate<boolean>(`typeof window.__ot === "object"`)))
488
+ throw new Error("film probe: the film clock did not install in the page");
489
+ return b.evaluate<FilmPageInfo>("window.__ot.prepare()", { timeoutMs: 120_000 });
490
+ };
491
+ try {
492
+ let width = opts.width ?? opts.fallback.width;
493
+ let height = opts.height ?? opts.fallback.height;
494
+ let browser = await open(width, height);
495
+ try {
496
+ let info = await prepare(browser);
497
+ if (
498
+ opts.width === undefined &&
499
+ info.width &&
500
+ info.height &&
501
+ (info.width !== width || info.height !== height)
502
+ ) {
503
+ await browser.close();
504
+ width = info.width;
505
+ height = info.height;
506
+ browser = await open(width, height);
507
+ info = await prepare(browser);
508
+ }
509
+ const segments = await browser
510
+ .evaluate<unknown>(
511
+ `(async () => { const f = window.__film?.segments; return typeof f === "function" ? JSON.parse(JSON.stringify((await f()) ?? [])) : null; })()`,
512
+ )
513
+ .catch(() => null);
514
+ // the subjects: the ones asked for, and those the page's segments name
515
+ const subjects = new Set(opts.subjects ?? []);
516
+ if (Array.isArray(segments))
517
+ for (const s of segments as { subject?: unknown }[])
518
+ if (typeof s?.subject === "string" && s.subject) subjects.add(s.subject);
519
+ const source = pageTextSource([...subjects]);
520
+ const samples: PageTextSample[] = [];
521
+ const failed: PageTextReading["failed"] = [];
522
+ for (const t of opts.times) {
523
+ // a page that throws at one time (a still that failed to decode) is
524
+ // asked once more, then left out there
525
+ let error = "";
526
+ for (let tries = 0; tries < 2; tries++) {
527
+ try {
528
+ await browser.evaluate(`window.__ot.seek(${opts.from + t})`);
529
+ const r = await browser.evaluate<Omit<PageTextSample, "t">>(source);
530
+ for (const m of r.media) if (m.src) m.src = servedPath(m.src, server.url);
531
+ const ask = typeof opts.extra === "function" ? opts.extra(opts.from + t) : opts.extra;
532
+ const extra = ask ? await browser.evaluate<unknown>(ask) : undefined;
533
+ samples.push({ t, ...r, ...(opts.extra ? { extra } : {}) });
534
+ error = "";
535
+ break;
536
+ } catch (e) {
537
+ error = (e as Error).message.split("\n")[0] ?? "";
538
+ }
539
+ }
540
+ if (error) failed.push({ t, error });
541
+ }
542
+ return { samples, root, width, height, segments, failed, info };
543
+ } finally {
544
+ await browser.close();
545
+ }
546
+ } finally {
547
+ await server.close();
548
+ }
549
+ }
550
+
551
+ /** a URL the page showed, as a path from the served root ("/shots/001-a.png")
552
+ * when the server serves it, else as it was */
553
+ export function servedPath(src: string, base: string): string {
554
+ const b = base.replace(/\/+$/, "");
555
+ if (!src.startsWith(`${b}/`)) return src;
556
+ const path = src.slice(b.length).split(/[?#]/)[0]!;
557
+ try {
558
+ return decodeURIComponent(path);
559
+ } catch {
560
+ return path;
561
+ }
562
+ }
563
+
564
+ /** how much of a box (frame px) the page's text grid covers, 0..1 */
565
+ export function gridCover(grid: readonly string[], w: number, h: number, b: Box): number {
566
+ if (!grid.length) return 0;
567
+ const gx = grid[0]!.length;
568
+ const gy = grid.length;
569
+ const x0 = Math.max(0, Math.floor((b[0] / w) * gx));
570
+ const x1 = Math.min(gx - 1, Math.floor(((b[0] + b[2]) / w) * gx - 1e-9));
571
+ const y0 = Math.max(0, Math.floor((b[1] / h) * gy));
572
+ const y1 = Math.min(gy - 1, Math.floor(((b[1] + b[3]) / h) * gy - 1e-9));
573
+ let n = 0;
574
+ let k = 0;
575
+ for (let y = y0; y <= y1; y++)
576
+ for (let x = x0; x <= x1; x++) {
577
+ n++;
578
+ if (grid[y]![x] === "1") k++;
579
+ }
580
+ return n ? k / n : 0;
581
+ }
582
+
583
+ // --- the picture ----------------------------------------------------------------------
584
+
585
+ /** a line of text the recognizer read: its box in frame px, characters, confidence */
586
+ export type OcrLine = { box: Box; chars: number; conf: number; text: string };
587
+
588
+ export type FrameText = {
589
+ t: number;
590
+ /** "ocr": lines read with their size; "edges": only a share, from an edge count */
591
+ how: "ocr" | "edges";
592
+ lines: OcrLine[];
593
+ /** share of the frame under text (ocr: the lines' boxes, summed; edges: text-like cells) */
594
+ share: number;
595
+ };
596
+
597
+ const OCR_SWIFT = `import Foundation
598
+ import Vision
599
+ import ImageIO
600
+ // reads image paths on stdin, one a line; writes one JSON line per image
601
+ while let line = readLine() {
602
+ let path = line.trimmingCharacters(in: .whitespaces)
603
+ if path.isEmpty { continue }
604
+ guard let src = CGImageSourceCreateWithURL(URL(fileURLWithPath: path) as CFURL, nil),
605
+ let img = CGImageSourceCreateImageAtIndex(src, 0, nil) else {
606
+ print("{\\"error\\":\\"unreadable\\"}"); fflush(stdout); continue
607
+ }
608
+ let req = VNRecognizeTextRequest()
609
+ req.recognitionLevel = .accurate
610
+ req.usesLanguageCorrection = false
611
+ req.recognitionLanguages = ["zh-Hant", "zh-Hans", "ja-JP", "ko-KR", "en-US"]
612
+ let h = VNImageRequestHandler(cgImage: img, options: [:])
613
+ try? h.perform([req])
614
+ var out: [[String: Any]] = []
615
+ for o in req.results ?? [] {
616
+ guard let c = o.topCandidates(1).first else { continue }
617
+ let b = o.boundingBox
618
+ out.append(["x": b.minX, "y": 1 - b.maxY, "w": b.width, "h": b.height, "c": c.confidence, "s": c.string])
619
+ }
620
+ let d = try! JSONSerialization.data(withJSONObject: ["w": img.width, "h": img.height, "lines": out])
621
+ print(String(data: d, encoding: .utf8)!)
622
+ fflush(stdout)
623
+ }
624
+ `;
625
+
626
+ let ocrBinary: Promise<string | null> | null = null;
627
+
628
+ /** the text recognizer's helper, compiled once per source into the temp dir;
629
+ * null where it cannot be had (not macOS, no swiftc, a failed build) */
630
+ export function ocrHelper(): Promise<string | null> {
631
+ if (process.env.OPEN_TAKE_NO_OCR) return Promise.resolve(null);
632
+ ocrBinary ??= (async () => {
633
+ if (platform() !== "darwin") return null;
634
+ const hash = createHash("sha1").update(OCR_SWIFT).digest("hex").slice(0, 12);
635
+ const dir = join(tmpdir(), `open-take-ocr-${hash}`);
636
+ const bin = join(dir, "ocr");
637
+ if (existsSync(bin)) return bin;
638
+ try {
639
+ await mkdir(dir, { recursive: true });
640
+ const src = join(dir, "ocr.swift");
641
+ await writeFile(src, OCR_SWIFT);
642
+ const tmpBin = join(dir, `ocr-${process.pid}`);
643
+ await runTool("swiftc", ["-O", src, "-o", tmpBin]);
644
+ await rm(bin, { force: true });
645
+ await rename(tmpBin, bin);
646
+ return bin;
647
+ } catch {
648
+ return null;
649
+ }
650
+ })();
651
+ return ocrBinary;
652
+ }
653
+
654
+ /** run the helper over images: one parsed result per image, in order */
655
+ export function runOcr(
656
+ bin: string,
657
+ files: readonly string[],
658
+ ): Promise<
659
+ ({
660
+ w: number;
661
+ h: number;
662
+ lines: { x: number; y: number; w: number; h: number; c: number; s: string }[];
663
+ } | null)[]
664
+ > {
665
+ return new Promise((ok, fail) => {
666
+ const p = spawn(bin, [], { stdio: ["pipe", "pipe", "pipe"] });
667
+ let out = "";
668
+ p.stdout.on("data", (d: Buffer) => {
669
+ out += d.toString();
670
+ });
671
+ p.stderr.on("data", () => {});
672
+ p.on("error", fail);
673
+ p.on("close", (code) => {
674
+ const lines = out.split("\n").filter((l) => l.trim());
675
+ if (code !== 0 && lines.length < files.length)
676
+ return fail(new Error(`text recognizer exited ${code}`));
677
+ ok(
678
+ files.map((_, i) => {
679
+ try {
680
+ const r = JSON.parse(lines[i] ?? "null");
681
+ return r && !r.error ? r : null;
682
+ } catch {
683
+ return null;
684
+ }
685
+ }),
686
+ );
687
+ });
688
+ p.stdin.end(`${files.join("\n")}\n`);
689
+ });
690
+ }
691
+
692
+ /** the film's frames at `times` (its own seconds), read for text; null when
693
+ * neither reader could run */
694
+ export async function readFrameText(opts: {
695
+ ffmpeg: string;
696
+ input: string;
697
+ times: readonly number[];
698
+ /** the film's frame size, which boxes are given in */
699
+ width: number;
700
+ height: number;
701
+ /** skip the recognizer and use the edge count */
702
+ edgesOnly?: boolean;
703
+ }): Promise<{ how: "ocr" | "edges"; frames: FrameText[] } | null> {
704
+ const bin = opts.edgesOnly ? null : await ocrHelper();
705
+ const tmp = await mkdtemp(join(tmpdir(), "open-take-probe-text-"));
706
+ try {
707
+ if (bin) {
708
+ // the frames as images, at most 1920 wide (the recognizer reads small text there)
709
+ const ow = Math.min(1920, opts.width);
710
+ const files: string[] = [];
711
+ let next = 0;
712
+ await Promise.all(
713
+ Array.from({ length: Math.min(4, opts.times.length) }, async () => {
714
+ while (next < opts.times.length) {
715
+ const i = next++;
716
+ const f = join(tmp, `${String(i).padStart(5, "0")}.png`);
717
+ await runTool(opts.ffmpeg, [
718
+ "-v",
719
+ "error",
720
+ "-ss",
721
+ String(Math.max(0, opts.times[i]!)),
722
+ "-i",
723
+ opts.input,
724
+ "-frames:v",
725
+ "1",
726
+ "-vf",
727
+ `scale=${ow}:-2`,
728
+ "-y",
729
+ f,
730
+ ]).catch(() => {});
731
+ files[i] = f;
732
+ }
733
+ }),
734
+ );
735
+ const present = files.filter((f) => existsSync(f));
736
+ if (present.length) {
737
+ try {
738
+ const read = await runOcr(bin, present);
739
+ const byFile = new Map(present.map((f, i) => [f, read[i]] as const));
740
+ const frames: FrameText[] = opts.times.map((t, i) => {
741
+ const r = byFile.get(files[i]!) ?? null;
742
+ const lines: OcrLine[] = (r?.lines ?? []).map((l) => ({
743
+ box: [
744
+ Math.round(l.x * opts.width),
745
+ Math.round(l.y * opts.height),
746
+ Math.round(l.w * opts.width),
747
+ Math.round(l.h * opts.height),
748
+ ] as Box,
749
+ chars: l.s.replace(/\s+/g, "").length,
750
+ conf: Math.round(l.c * 100) / 100,
751
+ text: l.s.slice(0, 60),
752
+ }));
753
+ const share = Math.min(
754
+ 1,
755
+ lines.reduce((a, l) => a + l.box[2] * l.box[3], 0) / (opts.width * opts.height),
756
+ );
757
+ return { t, how: "ocr", lines, share: Math.round(share * 1000) / 1000 };
758
+ });
759
+ return { how: "ocr", frames };
760
+ } catch {
761
+ // fall through to the edge count
762
+ }
763
+ }
764
+ }
765
+ const frames = await edgeText(opts.ffmpeg, opts.input, opts.times);
766
+ return frames ? { how: "edges", frames } : null;
767
+ } finally {
768
+ await rm(tmp, { recursive: true, force: true });
769
+ }
770
+ }
771
+
772
+ /** the share of the frame that looks like text by an edge count: cells of a
773
+ * 960-wide grey copy dense in short, strong horizontal transitions over
774
+ * enough contrast. Photos and fine textures count too; there is no size. */
775
+ async function edgeText(
776
+ ffmpeg: string,
777
+ input: string,
778
+ times: readonly number[],
779
+ ): Promise<FrameText[] | null> {
780
+ const W = 960;
781
+ const out: FrameText[] = [];
782
+ for (const t of times) {
783
+ const buf = await new Promise<Buffer | null>((ok) => {
784
+ const p = spawn(
785
+ ffmpeg,
786
+ [
787
+ "-v",
788
+ "error",
789
+ "-ss",
790
+ String(Math.max(0, t)),
791
+ "-i",
792
+ input,
793
+ "-frames:v",
794
+ "1",
795
+ "-vf",
796
+ `scale=${W}:-2,format=gray`,
797
+ "-f",
798
+ "rawvideo",
799
+ "-",
800
+ ],
801
+ { stdio: ["ignore", "pipe", "ignore"] },
802
+ );
803
+ const chunks: Buffer[] = [];
804
+ p.stdout.on("data", (d: Buffer) => chunks.push(d));
805
+ p.on("error", () => ok(null));
806
+ p.on("close", (code) => ok(code === 0 ? Buffer.concat(chunks) : null));
807
+ });
808
+ if (!buf || buf.length < W * 8) {
809
+ out.push({ t, how: "edges", lines: [], share: 0 });
810
+ continue;
811
+ }
812
+ out.push({ t, how: "edges", lines: [], share: edgeShare(buf, W, Math.floor(buf.length / W)) });
813
+ }
814
+ return out.length ? out : null;
815
+ }
816
+
817
+ /** text-like cells (8×8) of a grey frame, as a share of all cells */
818
+ export function edgeShare(px: Uint8Array, w: number, h: number): number {
819
+ const C = 8;
820
+ const cx = Math.floor(w / C);
821
+ const cy = Math.floor(h / C);
822
+ let text = 0;
823
+ for (let by = 0; by < cy; by++)
824
+ for (let bx = 0; bx < cx; bx++) {
825
+ let edges = 0;
826
+ let s = 0;
827
+ let sq = 0;
828
+ for (let r = 0; r < C; r++) {
829
+ const row = (by * C + r) * w + bx * C;
830
+ for (let c = 0; c < C; c++) {
831
+ const v = px[row + c]!;
832
+ s += v;
833
+ sq += v * v;
834
+ if (c < C - 1 && Math.abs(v - px[row + c + 1]!) > 40) edges++;
835
+ }
836
+ }
837
+ const m = s / (C * C);
838
+ const sd = Math.sqrt(Math.max(0, sq / (C * C) - m * m));
839
+ const density = edges / (C * (C - 1));
840
+ if (sd > 20 && density >= 0.1 && density <= 0.6) text++;
841
+ }
842
+ return cx * cy ? Math.round((text / (cx * cy)) * 1000) / 1000 : 0;
843
+ }