@torrent-tv/proxy 2.87.8 → 2.87.10

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 (455) hide show
  1. package/.claude/commands/opsx/apply.md +155 -155
  2. package/.claude/commands/opsx/archive.md +160 -160
  3. package/.claude/commands/opsx/explore.md +174 -174
  4. package/.claude/commands/opsx/propose.md +109 -109
  5. package/.claude/commands/opsx/sync.md +143 -143
  6. package/.claude/skills/openspec-apply-change/SKILL.md +159 -159
  7. package/.claude/skills/openspec-archive-change/SKILL.md +117 -117
  8. package/.claude/skills/openspec-explore/SKILL.md +289 -289
  9. package/.claude/skills/openspec-propose/SKILL.md +113 -113
  10. package/.claude/skills/openspec-sync-specs/SKILL.md +147 -147
  11. package/.dockerignore +5 -5
  12. package/AGENTS.md +322 -322
  13. package/CHANGELOG.md +8 -0
  14. package/Dockerfile +30 -30
  15. package/LICENSE +16 -16
  16. package/README.md +369 -369
  17. package/assets/calibration/NOTICE.md +100 -100
  18. package/assets/diagnostics/sctpstate.gdb +31 -31
  19. package/bin/cli.js +684 -684
  20. package/bin/thread-pool.js +30 -30
  21. package/biome.json +411 -411
  22. package/docs/container-architecture.md +218 -218
  23. package/docs/download-architecture.md +261 -261
  24. package/docs/encode-architecture.md +729 -729
  25. package/docs/encode-run-state.md +96 -96
  26. package/docs/logs.md +97 -97
  27. package/docs/socks5-egress-research.md +114 -114
  28. package/docs/storage-architecture.md +267 -267
  29. package/knip.json +14 -14
  30. package/openspec/changes/adaptive-bitrate/design.md +92 -92
  31. package/openspec/changes/adaptive-bitrate/proposal.md +56 -56
  32. package/openspec/changes/adaptive-bitrate/specs/adaptive-bitrate/spec.md +51 -51
  33. package/openspec/changes/adaptive-bitrate/tasks.md +40 -40
  34. package/openspec/changes/binary-distribution/.openspec.yaml +2 -2
  35. package/openspec/changes/binary-distribution/proposal.md +53 -53
  36. package/openspec/changes/chunked-request-bodies/design.md +81 -81
  37. package/openspec/changes/chunked-request-bodies/proposal.md +53 -53
  38. package/openspec/changes/chunked-request-bodies/specs/chunked-request-bodies/spec.md +30 -30
  39. package/openspec/changes/chunked-request-bodies/tasks.md +40 -40
  40. package/openspec/changes/cold-start/design.md +98 -98
  41. package/openspec/changes/cold-start/proposal.md +62 -62
  42. package/openspec/changes/cold-start/specs/cold-start/spec.md +47 -47
  43. package/openspec/changes/cold-start/tasks.md +48 -48
  44. package/openspec/changes/disk-cap/.openspec.yaml +2 -2
  45. package/openspec/changes/disk-cap/proposal.md +37 -37
  46. package/openspec/changes/disk-cap/specs/disk-cap/spec.md +25 -25
  47. package/openspec/changes/disk-cap/tasks.md +19 -19
  48. package/openspec/changes/proxy-observability/.openspec.yaml +2 -2
  49. package/openspec/changes/proxy-observability/design.md +38 -38
  50. package/openspec/changes/proxy-observability/proposal.md +49 -49
  51. package/openspec/changes/proxy-observability/specs/observability/spec.md +33 -33
  52. package/openspec/changes/proxy-observability/tasks.md +21 -21
  53. package/openspec/changes/seek-debounce/design.md +118 -118
  54. package/openspec/changes/seek-debounce/proposal.md +59 -59
  55. package/openspec/changes/seek-debounce/specs/seek-debounce/spec.md +41 -41
  56. package/openspec/changes/seek-debounce/tasks.md +33 -33
  57. package/openspec/changes/subtitle-language/.openspec.yaml +2 -2
  58. package/openspec/changes/subtitle-language/proposal.md +50 -50
  59. package/openspec/changes/subtitle-language/specs/subtitle-language/spec.md +34 -34
  60. package/openspec/changes/subtitle-language/tasks.md +26 -26
  61. package/openspec/changes/track-selection/.openspec.yaml +2 -2
  62. package/openspec/changes/track-selection/design.md +47 -47
  63. package/openspec/changes/track-selection/proposal.md +45 -45
  64. package/openspec/changes/track-selection/specs/track-selection/spec.md +41 -41
  65. package/openspec/changes/track-selection/tasks.md +20 -20
  66. package/openspec/changes/transcode-quality/.openspec.yaml +2 -2
  67. package/openspec/changes/transcode-quality/proposal.md +51 -51
  68. package/openspec/changes/transcode-quality/specs/transcode-quality/spec.md +139 -139
  69. package/openspec/changes/transcode-quality/tasks.md +63 -63
  70. package/openspec/config.yaml +38 -38
  71. package/package.json +1 -1
  72. package/public/.well-known/appspecific/com.chrome.devtools.json +1 -1
  73. package/research/delivery-wedge-stand-ladder-2026-09-13.md +329 -329
  74. package/research/double-spawn-2026-09-10.md +171 -171
  75. package/research/handover-2026-09-13.md +209 -209
  76. package/research/handover-reader-claims-removal-2026-09-12.md +166 -166
  77. package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +175 -175
  78. package/research/preset-benchmark-size-scaling-2026-08-22.md +55 -55
  79. package/research/priority-map-is-the-truth-2026-09-12.md +225 -225
  80. package/routes/api/client-logs/post.js +86 -86
  81. package/routes/api/delivery-sink/get.js +72 -72
  82. package/routes/api/link-probe/get.js +51 -51
  83. package/routes/api/playback-plan/audio-tracks/post.js +29 -29
  84. package/routes/api/playback-plan/post.js +53 -53
  85. package/routes/api/sources/files/get.js +100 -100
  86. package/routes/api/sources/post.js +36 -36
  87. package/routes/api/sources/stats/get.js +132 -132
  88. package/routes/api/subtitles/get.js +230 -230
  89. package/routes/api/transcode-sessions/fragment-far/post.js +60 -60
  90. package/routes/api/transcode-sessions/net-report/post.js +129 -129
  91. package/routes/api/transcode-sessions/post.js +160 -160
  92. package/routes/api/transcode-sessions/progress/get.js +27 -27
  93. package/routes/api/transcode-sessions/release/post.js +41 -41
  94. package/routes/api/transcode-sessions/seek/post.js +63 -63
  95. package/routes/health/get.js +13 -13
  96. package/routes/healthz/get.js +13 -13
  97. package/routes/stream/get.js +361 -361
  98. package/routes/transcode/audio-file/get.js +64 -64
  99. package/routes/transcode/audio-warm/get.js +84 -84
  100. package/routes/transcode/session-file/get.js +52 -52
  101. package/routes/transcode/variant-file/get.js +95 -95
  102. package/routes/transcode/variant-warm/get.js +84 -84
  103. package/scripts/render-run-graph.js +173 -173
  104. package/services/encode/CoverageMap.js +428 -428
  105. package/services/encode/CushionReport.js +345 -345
  106. package/services/encode/EncodeAdmission.js +332 -332
  107. package/services/encode/EncodeOrchestrator.js +1217 -1217
  108. package/services/encode/EncodePlan.js +1366 -1366
  109. package/services/encode/EncodeRun.js +776 -776
  110. package/services/encode/EncodeRuns.js +1229 -1229
  111. package/services/encode/Encoder.js +142 -142
  112. package/services/encode/EncoderSelection.js +38 -38
  113. package/services/encode/LocalObservations.js +359 -359
  114. package/services/encode/NvencEncoder.js +50 -50
  115. package/services/encode/OutputOpening.js +755 -755
  116. package/services/encode/OutputTimes.js +579 -579
  117. package/services/encode/QsvEncoder.js +58 -58
  118. package/services/encode/Renditions.js +2162 -2162
  119. package/services/encode/RunProgress.js +84 -84
  120. package/services/encode/SegmentDemand.js +95 -95
  121. package/services/encode/SoftwareEncoder.js +121 -121
  122. package/services/encode/V4l2m2mEncoder.js +53 -53
  123. package/services/encode/VaapiEncoder.js +66 -66
  124. package/services/encode/args.js +416 -416
  125. package/services/encode/available-share.js +89 -89
  126. package/services/encode/calibration.js +361 -361
  127. package/services/encode/contention.js +128 -128
  128. package/services/encode/decode-cost-fit.js +363 -363
  129. package/services/encode/encode-exit.js +114 -114
  130. package/services/encode/encode-run-state.js +551 -551
  131. package/services/encode/encoder-readings.js +45 -45
  132. package/services/encode/fingerprint.js +322 -322
  133. package/services/encode/host-load.js +174 -174
  134. package/services/encode/hwaccel.js +1894 -1894
  135. package/services/encode/index.js +9 -9
  136. package/services/encode/learned-median.js +90 -90
  137. package/services/encode/output/EncodedOutput.js +57 -57
  138. package/services/encode/output/Output.js +131 -131
  139. package/services/encode/output/OutputCatalog.js +414 -414
  140. package/services/encode/output/OutputSpec.js +390 -390
  141. package/services/encode/output/Timeline.js +308 -308
  142. package/services/encode/output/cut-grid.js +107 -107
  143. package/services/encode/output/index.js +20 -20
  144. package/services/encode/output/ladder.js +32 -32
  145. package/services/encode/output/playlists.js +284 -284
  146. package/services/encode/output/rates.js +125 -125
  147. package/services/encode/output-key-format.js +21 -21
  148. package/services/encode/quality/EncodeCost.js +1083 -1083
  149. package/services/encode/quality/HostLoad.js +525 -525
  150. package/services/encode/quality/HostTimings.js +249 -249
  151. package/services/encode/quality/OutputQualityState.js +28 -28
  152. package/services/encode/quality/QualityController.js +935 -935
  153. package/services/encode/quality/QualityOffer.js +416 -416
  154. package/services/encode/quality/drain-threat.js +47 -47
  155. package/services/encode/quality/link-budget.js +254 -254
  156. package/services/encode/quality/output-format.js +266 -266
  157. package/services/encode/quality/serving-output.js +216 -216
  158. package/services/encode/quality/visible-rung.js +57 -57
  159. package/services/encode/run-budget.js +97 -97
  160. package/services/encode/run-command.js +747 -747
  161. package/services/encode/run-costs.js +155 -155
  162. package/services/encode/run-inputs.js +42 -42
  163. package/services/encode/segment-formats/fmp4.js +437 -437
  164. package/services/encode/segment-formats/index.js +77 -77
  165. package/services/encode/segment-formats/init-compat.js +324 -324
  166. package/services/encode/segment-formats/mp4-boxes.js +311 -311
  167. package/services/encode/segment-formats/mpegts.js +134 -134
  168. package/services/encode/start-stop-cost.js +178 -178
  169. package/services/encode/throughput.js +97 -97
  170. package/services/encode/torrent-cost.js +88 -88
  171. package/services/identity/installation-id.js +38 -38
  172. package/services/media/ContainerOrchestrator.js +149 -149
  173. package/services/media/KeyframeTables.js +316 -316
  174. package/services/media/SourceFile.js +298 -298
  175. package/services/media/SubtitleCues.js +520 -520
  176. package/services/media/SubtitleOrchestrator.js +167 -167
  177. package/services/media/container/AviContainer.js +266 -266
  178. package/services/media/container/Container.js +603 -603
  179. package/services/media/container/ContainerFactory.js +56 -56
  180. package/services/media/container/KeyframeTable.js +272 -272
  181. package/services/media/container/MatroskaContainer.js +1494 -1494
  182. package/services/media/container/Mp4Container.js +1244 -1244
  183. package/services/media/container/SubtitleFileContainer.js +322 -322
  184. package/services/media/container/ebml-reader.js +159 -159
  185. package/services/media/ffmpeg-banner.js +260 -260
  186. package/services/media/keyframe-probe.js +157 -157
  187. package/services/media/media-info-probe.js +93 -93
  188. package/services/media/playback-planner.js +3 -0
  189. package/services/media/tracks/AudioTrack.js +131 -131
  190. package/services/media/tracks/ContainerTrack.js +75 -75
  191. package/services/media/tracks/ImageSubtitleTrack.js +19 -19
  192. package/services/media/tracks/SubtitleTrack.js +38 -38
  193. package/services/media/tracks/TextSubtitleTrack.js +287 -287
  194. package/services/media/tracks/VideoTrack.js +33 -33
  195. package/services/media/tracks/index.js +15 -15
  196. package/services/media/tracks/language-detect.js +174 -174
  197. package/services/server/OutputLifecycle.js +418 -418
  198. package/services/server/SegmentServing.js +1146 -1146
  199. package/services/server/ViewerRequests.js +522 -522
  200. package/services/server/WaitLedger.js +142 -142
  201. package/services/server/controllers/SubtitleController.js +109 -109
  202. package/services/server/http-responses.js +60 -60
  203. package/services/server/transcode-session-files.js +325 -325
  204. package/services/server/wire-outputs.js +983 -983
  205. package/services/storage/Diagnostics.js +202 -202
  206. package/services/storage/MachineBudget.js +235 -235
  207. package/services/storage/allowance.js +107 -107
  208. package/services/storage/core-dumps.js +106 -106
  209. package/services/storage/files/CompletedFiles.js +391 -391
  210. package/services/storage/files/piece-from-whole-file.js +118 -118
  211. package/services/storage/free.js +71 -71
  212. package/services/storage/keep.js +48 -48
  213. package/services/storage/machine-memory.js +56 -56
  214. package/services/storage/memory-report.js +575 -575
  215. package/services/storage/piece-store/piece-disk-store.js +516 -516
  216. package/services/storage/piece-store/piece-lru.js +495 -495
  217. package/services/storage/piece-store/shared-piece-store.js +1880 -1880
  218. package/services/storage/returns.js +103 -103
  219. package/services/storage/segment-store/SegmentStore.js +1196 -1196
  220. package/services/storage/wire.js +175 -175
  221. package/services/torrent/Contents.js +324 -324
  222. package/services/torrent/demand/DemandRegister.js +202 -202
  223. package/services/torrent/demand/Urgency.js +137 -137
  224. package/services/torrent/demand/Window.js +118 -118
  225. package/services/torrent/demand/index.js +8 -8
  226. package/services/torrent/demand/pieces.js +140 -140
  227. package/services/torrent/download/SwarmSelection.js +386 -386
  228. package/services/torrent/download/registry.js +102 -102
  229. package/services/torrent/download/withdraw-claim.js +80 -80
  230. package/services/torrent/files.js +455 -455
  231. package/services/torrent/naming.js +619 -619
  232. package/services/torrent/piece-store-of.js +29 -29
  233. package/services/torrent/supply-margin.js +262 -262
  234. package/services/torrent/worker/background-fill.js +213 -213
  235. package/services/torrent/worker/channel.js +271 -271
  236. package/services/torrent/worker/fastest-wires.js +342 -342
  237. package/services/torrent/worker/handle-state.js +23 -23
  238. package/services/torrent/worker/held-bytes.js +155 -155
  239. package/services/torrent/worker/install-webrtc-shim.js +32 -32
  240. package/services/torrent/worker/piece-reader.js +1078 -1078
  241. package/services/torrent/worker/resume-warm.js +107 -107
  242. package/services/torrent/worker/webrtc-shim.js +219 -219
  243. package/services/transport/delivery-probe.js +842 -842
  244. package/services/transport/health-collector.js +53 -53
  245. package/services/transport/nat-classifier.js +231 -231
  246. package/services/transport/packet-witness.js +808 -808
  247. package/services/transport/port-mapper.js +235 -235
  248. package/services/transport/registry-api.js +81 -81
  249. package/services/transport/tunnel-client.js +447 -447
  250. package/services/transport/usrsctp-state.js +175 -175
  251. package/services/transport/webrtc-manager.js +484 -484
  252. package/services/viewer/Assignments.js +340 -340
  253. package/services/viewer/PriorityMap.js +390 -390
  254. package/services/viewer/PriorityOrchestrator.js +325 -325
  255. package/services/viewer/Viewer.js +657 -657
  256. package/services/viewer/Viewers.js +368 -368
  257. package/services/viewer/active-output.js +37 -37
  258. package/services/viewer/audio-start.js +46 -46
  259. package/services/viewer/buffer-trend.js +81 -81
  260. package/services/viewer/choices.js +647 -647
  261. package/services/viewer/positions.js +126 -126
  262. package/services/viewer/report-intake.js +46 -46
  263. package/store/source-registry.js +71 -71
  264. package/test/asset-paths.test.js +28 -28
  265. package/test/audio-track-bitrate.test.js +40 -40
  266. package/test/auto-quality-step.test.js +707 -707
  267. package/test/available-share.test.js +76 -76
  268. package/test/background-fill.test.js +190 -190
  269. package/test/client-log-file.test.js +110 -110
  270. package/test/completed-files.test.js +115 -115
  271. package/test/concurrent-cost.test.js +148 -148
  272. package/test/container-media-info.test.js +228 -228
  273. package/test/contention.test.js +76 -76
  274. package/test/core-dumps.test.js +57 -57
  275. package/test/coverage-follows-the-disk.test.js +228 -228
  276. package/test/coverage-map.test.js +195 -195
  277. package/test/cue-push-carries-what-the-walk-found.test.js +58 -58
  278. package/test/cut-times-timeline.test.js +65 -65
  279. package/test/cuts-follow-published-grid.test.js +109 -109
  280. package/test/data-channel-frame.test.js +63 -63
  281. package/test/dead-torrent-handle.test.js +52 -52
  282. package/test/declared-rates.test.js +116 -116
  283. package/test/declared-tracks.test.js +43 -35
  284. package/test/decode-cost-fit.test.js +202 -202
  285. package/test/decode-cost.test.js +462 -462
  286. package/test/delivery-probe.test.js +393 -393
  287. package/test/demand-register.test.js +206 -206
  288. package/test/dht-bootstrap.test.js +72 -72
  289. package/test/drain-threat.test.js +65 -65
  290. package/test/duplicate-add.test.js +48 -48
  291. package/test/encode-admission.test.js +146 -146
  292. package/test/encode-cost.test.js +283 -283
  293. package/test/encode-exit.test.js +111 -111
  294. package/test/encode-orchestrator.test.js +419 -419
  295. package/test/encode-plan-viewers.test.js +718 -718
  296. package/test/encode-plan.test.js +545 -545
  297. package/test/encode-run-state.test.js +314 -314
  298. package/test/encode-run.test.js +161 -161
  299. package/test/encode-slope.test.js +46 -46
  300. package/test/encoded-output.test.js +39 -39
  301. package/test/encoder-kinds.test.js +122 -122
  302. package/test/encoder-readings.test.js +46 -46
  303. package/test/every-claimant-is-told-its-share.test.js +152 -152
  304. package/test/failed-encoder-still-serves-what-exists.test.js +354 -354
  305. package/test/failed-start-breaker.test.js +202 -202
  306. package/test/fastest-wires.test.js +143 -143
  307. package/test/ffmpeg-stream-counts.test.js +78 -78
  308. package/test/file-edges.test.js +191 -191
  309. package/test/fmp4-self-contained.test.js +80 -80
  310. package/test/fmp4-timestamps.test.js +140 -140
  311. package/test/fmp4-track-completeness.test.js +41 -41
  312. package/test/format-keyed-outputs.test.js +78 -78
  313. package/test/health-metrics.test.js +38 -38
  314. package/test/held-request-width.test.js +157 -157
  315. package/test/helpers/encode-run.js +136 -136
  316. package/test/helpers/held-file.js +29 -29
  317. package/test/helpers/manager.js +57 -57
  318. package/test/helpers/output-spec.js +63 -63
  319. package/test/host-load.test.js +68 -68
  320. package/test/index-accuracy-is-about-the-file.test.js +128 -128
  321. package/test/init-compat.test.js +219 -219
  322. package/test/input-lost-quiets-the-plan.test.js +261 -261
  323. package/test/input-unavailable.test.js +48 -48
  324. package/test/installation-id.test.js +39 -39
  325. package/test/keeping-period.test.js +83 -83
  326. package/test/keyframe-index-accuracy.test.js +188 -188
  327. package/test/keyframe-table-budget.test.js +102 -102
  328. package/test/keyframes-belong-to-the-file.test.js +249 -249
  329. package/test/learned-median.test.js +37 -37
  330. package/test/letting-go-asks-who-is-watching.test.js +191 -191
  331. package/test/limit-row-by-area.test.js +60 -60
  332. package/test/link-budget.test.js +111 -111
  333. package/test/link-probe.test.js +56 -56
  334. package/test/local-encoding-data.test.js +273 -273
  335. package/test/logger-repeats.test.js +134 -134
  336. package/test/look-ahead-coverage.test.js +56 -56
  337. package/test/manager-surface.test.js +292 -292
  338. package/test/matroska-cues-track.test.js +192 -192
  339. package/test/memory-budget.test.js +390 -390
  340. package/test/move-cost.test.js +216 -216
  341. package/test/mp4-subtitles.test.js +173 -173
  342. package/test/one-authority.test.js +285 -285
  343. package/test/one-budget.test.js +149 -149
  344. package/test/one-output-one-session.test.js +204 -204
  345. package/test/one-resource-divided.test.js +185 -185
  346. package/test/one-serving-output-per-height.test.js +458 -458
  347. package/test/orchestrator-wired.test.js +206 -206
  348. package/test/output-catalog.test.js +39 -39
  349. package/test/output-shape.test.js +21 -21
  350. package/test/output-spec.test.js +251 -251
  351. package/test/output-speed.test.js +88 -88
  352. package/test/outputs-being-prepared.test.js +40 -40
  353. package/test/packet-witness-ring.test.js +236 -236
  354. package/test/packet-witness.test.js +148 -148
  355. package/test/piece-disk-store.test.js +381 -381
  356. package/test/piece-from-whole-file.test.js +129 -129
  357. package/test/piece-lru.test.js +319 -319
  358. package/test/piece-reader.test.js +162 -162
  359. package/test/piece-selection-offset.test.js +85 -85
  360. package/test/piece-store-eviction.test.js +560 -560
  361. package/test/piece-store-never-refuses.test.js +153 -153
  362. package/test/piece-store-reservations.test.js +318 -318
  363. package/test/piece-store-slow-disk.test.js +159 -159
  364. package/test/piece-tail.test.js +97 -97
  365. package/test/plan-host-timings.test.js +68 -68
  366. package/test/plan-is-steady.test.js +225 -225
  367. package/test/priority-map-download.test.js +174 -174
  368. package/test/priority-map-emptied.test.js +204 -204
  369. package/test/priority-map-per-output.test.js +215 -215
  370. package/test/priority-map.test.js +163 -163
  371. package/test/probe-wedge-certainty.test.js +60 -60
  372. package/test/produced-copy-choice.test.js +213 -213
  373. package/test/published-timeline.test.js +83 -83
  374. package/test/quality-offer.test.js +162 -162
  375. package/test/quality-variants.test.js +1229 -1229
  376. package/test/rate-control-in-the-output.test.js +180 -180
  377. package/test/read-survives-withdrawal.test.js +164 -164
  378. package/test/read-window.test.js +365 -365
  379. package/test/resume-warm.test.js +39 -39
  380. package/test/run-graph-drift.test.js +37 -37
  381. package/test/run-inputs.test.js +80 -80
  382. package/test/run-intervals.test.js +100 -100
  383. package/test/run-position-follows-published-grid.test.js +89 -89
  384. package/test/run-progress.test.js +65 -65
  385. package/test/same-height-switch.test.js +664 -664
  386. package/test/seek-frees-the-viewers-requests.test.js +217 -217
  387. package/test/seek-landing.test.js +109 -109
  388. package/test/seek-target-not-superseded.test.js +195 -195
  389. package/test/segment-closed-on-both-branches.test.js +96 -96
  390. package/test/segment-cut-times.test.js +63 -63
  391. package/test/segment-demand.test.js +80 -80
  392. package/test/segment-hold-supersede.test.js +91 -91
  393. package/test/segment-judged-by-its-own-header.test.js +221 -221
  394. package/test/segment-serve-wiring.test.js +475 -475
  395. package/test/segment-store-eviction.test.js +247 -247
  396. package/test/segment-store.test.js +323 -323
  397. package/test/segment-true-start.test.js +86 -86
  398. package/test/segments-are-shared.test.js +176 -176
  399. package/test/send-chunk-bytes.test.js +86 -86
  400. package/test/serving-existing-output.test.js +102 -102
  401. package/test/serving-output.test.js +102 -102
  402. package/test/shared-piece-store.test.js +310 -310
  403. package/test/sidecar-naming.test.js +142 -142
  404. package/test/source-file.test.js +133 -133
  405. package/test/source-files-route.test.js +103 -103
  406. package/test/source-registry.test.js +18 -18
  407. package/test/split-a-stretch.test.js +139 -139
  408. package/test/stale-request-after-seek.test.js +158 -158
  409. package/test/startup-readings.test.js +124 -124
  410. package/test/stream-route.test.js +182 -182
  411. package/test/subtitle-cue-framing.test.js +200 -200
  412. package/test/subtitle-cue-source.test.js +102 -102
  413. package/test/subtitle-cue-walk.test.js +370 -370
  414. package/test/subtitle-cursor.test.js +63 -63
  415. package/test/subtitle-defaults.test.js +118 -118
  416. package/test/subtitle-language.test.js +252 -252
  417. package/test/subtitle-track-numbering.test.js +386 -386
  418. package/test/supply-margin.test.js +177 -177
  419. package/test/swarm-follows-readers.test.js +174 -174
  420. package/test/swarm-reach.test.js +130 -130
  421. package/test/swarm-selection.test.js +220 -220
  422. package/test/tail-duplication.test.js +167 -167
  423. package/test/the-channel-has-both-ends.test.js +91 -91
  424. package/test/the-torrent-thread-serves-bytes.test.js +107 -107
  425. package/test/thread-pool.test.js +53 -53
  426. package/test/timeline.test.js +84 -84
  427. package/test/torrent-contents.test.js +235 -235
  428. package/test/torrent-cost.test.js +71 -71
  429. package/test/torrent-files.test.js +221 -221
  430. package/test/torrent-naming.test.js +255 -255
  431. package/test/tracks-begin-together.test.js +209 -209
  432. package/test/tunnel-renewal.test.js +167 -167
  433. package/test/two-viewers-one-picture.test.js +407 -407
  434. package/test/upload-hurry.test.js +186 -186
  435. package/test/usrsctp-state.test.js +131 -131
  436. package/test/video-facts.test.js +102 -102
  437. package/test/viewer-assignments.test.js +143 -143
  438. package/test/viewer-outputs.test.js +285 -285
  439. package/test/viewer-presence.test.js +116 -116
  440. package/test/viewer-requests-admission.test.js +89 -89
  441. package/test/viewer-states.test.js +189 -189
  442. package/test/viewer-subtitle-subscription.test.js +96 -96
  443. package/test/viewer.test.js +139 -139
  444. package/test/viewing-generations.test.js +390 -390
  445. package/test/visible-rung.test.js +37 -37
  446. package/test/wait-ledger.test.js +100 -100
  447. package/test/webrtc-shim.test.js +149 -149
  448. package/test/wedge-certainty.test.js +131 -131
  449. package/test/withdraw-piece-claim.test.js +212 -212
  450. package/test/worker-channel.test.js +158 -158
  451. package/utils/client-log-file.js +151 -151
  452. package/utils/logger.js +312 -312
  453. package/utils/parse-range.js +25 -25
  454. package/utils/perf.js +59 -59
  455. package/utils/torrent-source-key.js +38 -38
@@ -1,747 +1,747 @@
1
- /**
2
- * @file The full argument list for one encoder run.
3
- *
4
- * What a run is given is a fact about WHAT is being produced and WHERE it
5
- * begins, and about nothing else — not the session it belongs to, not who is
6
- * watching, not how many viewers there are. It was a 377-line method of the
7
- * session manager reading fifteen of its fields, which is why a run could only
8
- * ever be built by that class, for the one session it holds.
9
- *
10
- * Stated here, over the material and the stretch alone, a run can be built by
11
- * whoever needs one. That is what lets an output have more than a single
12
- * encoder.
13
- *
14
- * **Nothing in this file runs anything.** It returns an argument list; spawning,
15
- * killing and resuming belong to whoever owns the process.
16
- */
17
-
18
- import { PLAYLIST_FILE_NAME } from "./output/playlists.js";
19
- import { AUDIO_TRANSCODE_KBPS } from "./args.js";
20
-
21
- /**
22
- * What ffmpeg's own CLI subtracts from an input seek, and therefore what has to
23
- * be added back to land where we asked.
24
- *
25
- * `fftools/ffmpeg_demux.c`, in `ifile_open`: when the container does not
26
- * declare `AVFMT_SEEK_TO_PTS` — Matroska does not — and any stream carries
27
- * B-frames, the seek target is moved back by `3*AV_TIME_BASE / 23` before
28
- * `avformat_seek_file` is called. Its purpose is sound: such containers seek in
29
- * decode order while the caller asks in presentation order, and with B-frames
30
- * the two differ, so it backs off far enough to be sure of reaching the frame
31
- * asked for.
32
- *
33
- * The consequence for a COPY is that asking for a keyframe lands on the one
34
- * BEFORE it — deterministically, every time. Measured 2026-08-21 on a Matroska
35
- * file with keyframes every 2 s: `-ss 10` produced a first segment starting at
36
- * 8.000; `-ss 10.130435` produced one starting at 10.000. On MP4, where the
37
- * heuristic does not fire, all of 10, 10.130435 and 10.2 produced 10.000 — so
38
- * adding this is right in one case and harmless in the other.
39
- *
40
- * That landing is what `-segment_times` is measured from, while this code
41
- * computes those offsets from the time it ASKED for. One keyframe interval
42
- * apart, inherited by every cut of the run: 119 of 125 segments arriving a
43
- * uniform 2.002 s early in the field, four times what a player bridges.
44
- *
45
- * Not applied when the picture is re-encoded: a re-encode decodes from the
46
- * keyframe and discards frames up to the requested time, so its output already
47
- * begins exactly where asked (measured the same day: `-ss 11` copied starts at
48
- * 10.000, re-encoded at 11.000).
49
- */
50
- export const SEEK_LANDING_OFFSET_SEC = 3 / 23;
51
-
52
- /**
53
- * A number of seconds as ffmpeg will accept it.
54
- *
55
- * `String(n)` switches to exponential notation below 1e-6, and ffmpeg's
56
- * duration parser rejects that outright: a field session died on
57
- * `Invalid duration for option ss: 3.3333333249174757e-7`, after which the
58
- * transcode was in state `failed` and every segment request answered 500 for
59
- * as long as the viewer kept trying. Anything under a millisecond is also not a
60
- * real offset — it is the residue of subtracting two nearly equal floats — so
61
- * it is dropped rather than passed on.
62
- *
63
- * @param {number} value
64
- * @returns {string}
65
- */
66
- export function ffmpegSeconds(value) {
67
- if (!Number.isFinite(value) || Math.abs(value) < 0.001) {
68
- return "0";
69
- }
70
- // Microsecond resolution, fixed notation, no trailing zero noise.
71
- return value.toFixed(6).replace(/\.?0+$/, "");
72
- }
73
-
74
- /**
75
- * Which timeline an output's own ffmpeg works on.
76
- *
77
- * True — the COPY branch: the source's timestamps are kept (`-copyts`) and the
78
- * output is re-labelled 0-based. Everything handed to the muxer is therefore
79
- * stated in the source's terms, and everything read back out of a produced
80
- * piece is 0-based.
81
- *
82
- * False — the re-encode branch: the output is labelled from the run's start on
83
- * the 0-based timeline, and the muxer is addressed in those same terms.
84
- *
85
- * One predicate for both callers, because the two used to answer it separately
86
- * and a disagreement between them is exactly what desynced picture from sound.
87
- *
88
- * @param {{ audioOnly?: boolean, timeline?: { cutGrid?: string }, transcodeVideo?: boolean }} material
89
- * @returns {boolean}
90
- */
91
- export function onKeyframeGridFor(material) {
92
- return material?.audioOnly === true
93
- ? material?.timeline?.cutGrid === "keyframe"
94
- : material?.transcodeVideo !== true;
95
- }
96
-
97
- /**
98
- * The boundary table the player is working from: the one its playlist was
99
- * written from, falling back to the live table when no playlist was built from
100
- * a table at all (no duration, so no synthetic playlist — and then nothing the
101
- * player holds contradicts it).
102
- *
103
- * @param {{ published?: number[], boundaries?: number[] }} timeline
104
- * @returns {number[]}
105
- */
106
- export function publishedGridFor(timeline) {
107
- return Array.isArray(timeline?.published) && timeline.published.length > 0
108
- ? timeline.published
109
- : (timeline?.boundaries ?? []);
110
- }
111
-
112
- /**
113
- * Where a run beginning at `index` must be positioned: the time the PLAYER was
114
- * told that segment starts at.
115
- *
116
- * Two tables, deliberately: the live one is corrected as produced segments
117
- * reveal where the file's cuts truly are, and those corrections are what let a
118
- * re-encoded rung be forced onto a copied stream's real grid. But the playlist
119
- * a player is holding was written once and never changes, so a position taken
120
- * from the corrected table describes a timeline nobody sent the player. That is
121
- * not a subtlety: it cost ten minutes of a dead film on 2026-08-17, the browser
122
- * asking for two segments 1908 times each.
123
- *
124
- * @param {{ published?: number[], boundaries?: number[] }} timeline
125
- * @param {number} index
126
- * @param {number} segmentDurationSec - Used only when the file has no table at
127
- * all, where a segment is a plain multiple of the nominal length.
128
- * @returns {number}
129
- */
130
- export function publishedStartTime(timeline, index, segmentDurationSec) {
131
- const published = Array.isArray(timeline?.published) && timeline.published.length > 0 ? timeline.published : null;
132
- const table = published ?? (Array.isArray(timeline?.boundaries) ? timeline.boundaries : []);
133
- if (table.length === 0) {
134
- return index * segmentDurationSec;
135
- }
136
- const clamped = Math.max(0, Math.min(index, table.length - 1));
137
- return table[clamped];
138
- }
139
-
140
- /**
141
- * Where a segment REALLY begins, when a produced piece has said so.
142
- *
143
- * The live table is corrected as produced pieces reveal where a file's cuts
144
- * truly are; the published one is what the player was told and may never move.
145
- * Where the two disagree, the live table is a measurement and the published one
146
- * a prediction — and this answers with the measurement, or with nothing when
147
- * they agree or nothing has been measured.
148
- *
149
- * @param {object} timeline
150
- * @param {number} index
151
- * @returns {number | undefined}
152
- */
153
- export function trueStartOf(timeline, index) {
154
- const live = Array.isArray(timeline?.boundaries) ? timeline.boundaries : null;
155
- const published = Array.isArray(timeline?.published) ? timeline.published : null;
156
- if (!live || !published || index < 0 || index >= live.length || index >= published.length) {
157
- return undefined;
158
- }
159
- return live[index] === published[index] ? undefined : live[index];
160
- }
161
-
162
- /**
163
- * The cut times to hand ffmpeg for a run that starts at `startIndex`.
164
- *
165
- * Two adjustments, both of which cost a broken session to learn:
166
- *
167
- * - **Rebased.** `-segment_times` is measured from the start of the run, not
168
- * of the file. Measured: starting at 12 s and asking for a cut at 18 s put
169
- * it at 29.4 s — 12 + 18. So every boundary has the run's own start
170
- * subtracted.
171
- * - **Interior only.** The first boundary is where the run begins and the last
172
- * is where the file ends; neither is a cut. Sending them would produce an
173
- * empty leading segment and a spurious trailing one.
174
- *
175
- * @param {number[]} boundaries
176
- * @param {number} startIndex
177
- * @returns {number[] | null}
178
- */
179
- export function segmentCutTimesFrom(boundaries, startIndex) {
180
- if (!Array.isArray(boundaries) || boundaries.length < 2) {
181
- return null;
182
- }
183
- const index = Number.isInteger(startIndex) && startIndex > 0 ? startIndex : 0;
184
- if (index >= boundaries.length - 1) {
185
- return null;
186
- }
187
- const base = boundaries[index];
188
- const times = [];
189
- for (let at = index + 1; at < boundaries.length - 1; at += 1) {
190
- times.push(Number((boundaries[at] - base).toFixed(6)));
191
- }
192
- return times;
193
- }
194
-
195
- /**
196
- * The largest keyframe time that does not exceed `target`, from a SORTED
197
- * (ascending) array of keyframe times. Null when `target` is before the first
198
- * keyframe or the array is empty — the caller then falls back to its unsnapped
199
- * target.
200
- *
201
- * @param {number[]} keyframeTimes - Sorted ascending.
202
- * @param {number} target
203
- * @returns {number | null}
204
- */
205
- export function nearestKeyframeAtOrBefore(keyframeTimes, target) {
206
- let result = null;
207
- for (const time of keyframeTimes) {
208
- if (time > target) {
209
- break;
210
- }
211
- result = time;
212
- }
213
- return result;
214
- }
215
-
216
- /**
217
- * How much later than a keyframe to ASK, so that ffmpeg lands on that keyframe.
218
- *
219
- * Bounded by half the distance to the next keyframe, which matters only where
220
- * keyframes stand closer together than twice the offset. There no single value
221
- * can satisfy both worlds — asking too little lands a keyframe early when the
222
- * heuristic fires, asking too much lands a keyframe late when it does not — and
223
- * the bound picks the smaller error, which is then under one keyframe interval
224
- * and therefore under what a player bridges.
225
- *
226
- * @param {{ transcodeVideo?: boolean, audioOnly?: boolean, keyframes?: { times?: number[] | null, tolerance?: number } }} material
227
- * @param {number} keyframe - A real keyframe time the run is to begin at.
228
- * @returns {number} Seconds to add to the request.
229
- */
230
- export function seekLandingOffsetFor(material, keyframe) {
231
- // A re-encode trims to the requested time itself, so it needs no help and
232
- // must not be pushed past what it was asked for.
233
- //
234
- // AN OUTPUT CARRYING ONLY SOUND IS SUCH A RE-ENCODE, and asking about the
235
- // picture missed it: the flag says whether the PICTURE is re-encoded, and an
236
- // output with no picture answers no. So the whole offset was added to the
237
- // soundtrack's own seek, and the sound then played 130 ms ahead of the picture
238
- // from every restart onward — reported by the viewer 2026-09-06 as lips out of
239
- // step with the voice from two minutes in, and measured: both outputs were
240
- // repositioned to the same boundary and both given `-ss 166.963435`, after
241
- // which the copied picture landed on the keyframe at 166.833 while the
242
- // re-encoded sound began where it was asked.
243
- if (material?.transcodeVideo === true || material?.audioOnly === true) {
244
- return 0;
245
- }
246
- // A grid whose times are approximate needs that error added on top, or a name
247
- // sitting just below its real keyframe seeks to before it and lands on the
248
- // one before that. Only AVI declares one.
249
- const tolerance = Number.isFinite(material?.keyframes?.tolerance)
250
- ? Math.max(0, material.keyframes.tolerance)
251
- : 0;
252
- const wanted = SEEK_LANDING_OFFSET_SEC + tolerance;
253
- const times = Array.isArray(material?.keyframes?.times) ? material.keyframes.times : [];
254
- const next = times.find((time) => time > keyframe + 0.001);
255
- if (next === undefined) {
256
- return wanted;
257
- }
258
- return Math.min(wanted, (next - keyframe) / 2);
259
- }
260
-
261
- /**
262
- * Everything ffmpeg is told for one run.
263
- *
264
- * @param {object} params
265
- * @param {{ times?: number[] | null, tolerance?: number }} params.keyframes - Where
266
- * the PICTURE's file has its keyframes: what a seek snaps to. Plain values,
267
- * so this can be asked without a container, a torrent or a session.
268
- * @param {{ startTime: number }} params.inputFile - The file this run reads.
269
- * @param {{ startTime: number }} params.audioFile - The file the chosen
270
- * soundtrack lives in, which for a dub shipped beside the picture is not the
271
- * picture's own.
272
- * @param {string} params.inputUrl
273
- * @param {string} params.audioInputUrl - Empty unless a browser that takes its
274
- * audio muxed is watching a release whose soundtrack is a file of its own.
275
- * @param {{ published?: number[], boundaries?: number[], cutGrid?: string }} params.timeline
276
- * @param {{ encodeWidth: number, encodeHeight: number, outputFps: number, softwarePreset: string | null, applyTonemap: boolean }} params.output
277
- * @param {object} params.segmentFormat
278
- * @param {boolean} params.transcodeVideo
279
- * @param {boolean} params.transcodeAudio
280
- * @param {boolean} params.audioOnly - An audio rendition: one track, no picture.
281
- * @param {boolean} params.audioSeparate - The picture's sound is published as a
282
- * rendition, so this output carries none.
283
- * @param {number} params.audioSourceTrackIndex - `0:a:N` within its own file.
284
- * @param {{ maxrateKbps: number, bufsizeKbps: number, level: string | null } | null} params.rateControl
285
- * The output's own, as its key names it; null where nothing bounds the rate.
286
- * @param {number} params.startIndex - First segment number this run makes.
287
- * @param {number} params.endIndex - Last it makes, inclusive; below the start
288
- * means it has no end.
289
- * @param {number | undefined} params.positionSecondsOverride - Where to begin,
290
- * when the caller knows better than the table.
291
- * @param {object} params.videoEncoder
292
- * @param {number} params.segmentDurationSec
293
- * @returns {{ args: string[], safeIndex: number, startSeconds: number, cutTimes: number[] | null }}
294
- */
295
- export function buildRunCommand({
296
- keyframes,
297
- inputFile,
298
- audioFile,
299
- inputUrl,
300
- audioInputUrl: audioInputUrlGiven,
301
- timeline,
302
- output,
303
- segmentFormat,
304
- transcodeVideo,
305
- transcodeAudio,
306
- audioOnly,
307
- audioSeparate,
308
- audioSourceTrackIndex,
309
- rateControl,
310
- startIndex,
311
- endIndex,
312
- positionSecondsOverride,
313
- videoEncoder,
314
- segmentDurationSec
315
- }) {
316
- const safeIndex = Number.isInteger(startIndex) && startIndex > 0 ? startIndex : 0;
317
- // 0-based output time of this segment, from the table the PLAYER holds —
318
- // the same one the cut list below is taken from.
319
- //
320
- // These two were read from different tables until 2026-08-21, and that is
321
- // one fault, not two: `-segment_times` are measured from wherever the run
322
- // really began, so any distance between the position and the cut list moves
323
- // EVERY cut of that run by it. The live table keeps being corrected as
324
- // produced segments reveal where the file's cuts truly are, and those
325
- // corrections run backwards, so each restart began a little earlier than
326
- // the grid the cuts were stated on — and since the corrections accumulate,
327
- // so did the distance. Measured on `JUFD665.mp4`: after one seek restart a
328
- // produced segment held the boundary two places before its own number
329
- // (16.684 s, exactly 2.0000 segments), after the next it held the one four
330
- // places before (33.5 s). The player's buffer then stops extending at all,
331
- // because the content of every fragment lands before the time its playlist
332
- // entry names: `bufferEnd` stood still at 4571.1 s through four `frag-far`
333
- // warnings until hls.js gave up and jumped the viewer 16.8 s forward.
334
- //
335
- // 2.45.0 moved the CUT LIST onto the published table for this same reason
336
- // and left the position on the live one. Both belong on the published
337
- // table: a run must begin where the player was told the segment begins.
338
- const startSeconds = Number.isFinite(positionSecondsOverride)
339
- ? positionSecondsOverride
340
- : publishedStartTime(timeline, safeIndex, segmentDurationSec);
341
- // Where each of the two timelines begins, asked of the files themselves.
342
- // Fresh by construction: the session may have been created before the
343
- // soundtrack file's header could be read, and the reading lands on the file
344
- // object this session holds — so there is nothing to re-read and nothing
345
- // that can be stale. Same property as the keyframe table, which is one
346
- // object shared by every session of the file.
347
- // Which timeline this run works on. Asked once, because the closure that
348
- // adds the second input reads it too, and two readings of one predicate is
349
- // how the picture and the sound came apart before.
350
- const keyframeGrid = onKeyframeGridFor({ audioOnly, timeline, transcodeVideo });
351
- const servesAudioSeparately = audioOnly !== true && audioSeparate === true;
352
- const audioFileStartTime = audioFile.startTime;
353
- // The start time of the file this run READS, which is the picture's own for
354
- // every session except one whose soundtrack is a separate file.
355
- const sourceStartTime = inputFile.startTime;
356
- // Cut where this session's grid says, whoever is producing the frames. The
357
- // times are measured from the start of THIS run; the same list serves as
358
- // the cut points and, when re-encoding, as the keyframes to force — one
359
- // list, so the two cannot drift apart.
360
- const explicitTimes = segmentFormat.explicitTimesMuxerArgs?.() ?? null;
361
- // A COPY is cut by this list whatever grid it ended up on. Even when no
362
- // keyframe index could be read and the boundaries are a plain grid, saying
363
- // them outright is what keeps the playlist and the muxer agreeing — ffmpeg
364
- // moves each cut forward to the first real keyframe, and serving reads back
365
- // where the piece truly begins. Requiring a keyframe grid here dropped a
366
- // copy with no index onto the `hls` muxer, which takes no cut list and
367
- // writes no self-contained pieces, so nothing could read a true start and
368
- // segments were stamped with times the file does not have — the 4.17 s
369
- // speech-against-subtitles drift, back again.
370
- //
371
- // Cut on the grid the PLAYER WAS GIVEN, not on the corrected one. A player
372
- // places a fragment by the playlist it holds, and that text was written
373
- // once and never changes; the live table keeps moving as produced segments
374
- // reveal where the file's cuts really are. Cutting on the moved table makes
375
- // every run faithful to a timeline nobody sent the player — measured
376
- // 2026-08-20, the picture's segments arriving a uniform 2.002 s before the
377
- // times the playlist named for them, which is four times what hls.js will
378
- // bridge, so the fragment does not land and is asked for again.
379
- //
380
- // The corrections keep their purpose: they describe the file, and a variant
381
- // created later inherits the corrected table and PUBLISHES it, so its own
382
- // playlist and its own cuts agree from the start. What they may not do is
383
- // move the cuts of a session whose playlist is already being read.
384
- const gridCutTimes = explicitTimes && (!transcodeVideo || timeline.cutGrid === "keyframe")
385
- ? segmentCutTimesFrom(publishedGridFor(timeline), safeIndex)
386
- : null;
387
- // Cut times are stated on the grid, for both branches.
388
- //
389
- // 2.28.0 added `sourceStartTime` to them on the copy branch, reasoning that
390
- // the muxer decides its cuts before the output is relabelled. The field
391
- // measured it the next session and the reasoning was wrong: of 75 pieces
392
- // the picture produced, only NINE began at a time the container's own
393
- // keyframe table names (the soundtrack, untouched by the change, scored 70
394
- // of 75). Before it, every piece began exactly on a named keyframe and it
395
- // was the PLAYLIST that disagreed with them. So the shift moved the cuts
396
- // OFF the keyframes rather than onto them, and it is gone.
397
- //
398
- // What remains true, and is what that measurement is really about: the
399
- // picture cuts where the source's keyframes are, and the playlist must be
400
- // built from those same times. That is the correction path's job, not the
401
- // cut list's.
402
- const cutTimes = gridCutTimes;
403
-
404
- // Video: re-encode only when required, using the detected encoder
405
- // (hardware-accelerated or software). The descriptor builds the filter +
406
- // codec args (including keyframe alignment on segment boundaries).
407
- const videoCodecArgs = transcodeVideo
408
- ? videoEncoder.buildVideoArgs({
409
- // Budget-selected encode box (may be below the client target on weak
410
- // software hosts); falls back to the client target for hardware.
411
- targetWidth: output.encodeWidth,
412
- targetHeight: output.encodeHeight,
413
- segmentDurationSec: segmentDurationSec,
414
- // Source-inherited output rate (integer, capped); descriptors that
415
- // use time-based keyframes just apply it as the frame rate.
416
- fps: output.outputFps,
417
- // Software-only; hardware descriptors ignore it.
418
- preset: output.softwarePreset ?? undefined,
419
- // HDR→SDR tone map (software path only; gated on filter availability).
420
- tonemap: output.applyTonemap === true,
421
- // On the source's grid the cuts are not evenly spaced, so no frame
422
- // count can describe them: the encoder is told the times outright,
423
- // the same ones the muxer will cut at.
424
- forcedKeyframeTimes: cutTimes,
425
- // The output's own rate control and level. Read off the output, whose
426
- // key names them: two limits are two outputs, and nothing moves a
427
- // limit under an output that already exists.
428
- rateControl: rateControl ?? null
429
- })
430
- : ["-c:v", "copy"];
431
- const audioCodecArgs = transcodeAudio
432
- ? ["-c:a", "aac", "-ac", "2", "-b:a", `${AUDIO_TRANSCODE_KBPS}k`]
433
- : ["-c:a", "copy"];
434
-
435
- const args = ["-hide_banner", "-nostats", "-loglevel", "error", "-progress", "pipe:1"];
436
- // Hardware decode/encode setup (e.g. VAAPI device) must precede -i, and
437
- // only applies when we actually re-encode the video track.
438
- if (transcodeVideo && Array.isArray(videoEncoder.inputArgs)) {
439
- args.push(...videoEncoder.inputArgs);
440
- }
441
- // Seek position in SOURCE time. On the keyframe grid `startSeconds` is a
442
- // real keyframe's offset from zero, so the container's own start time goes
443
- // back on to reach it; on the uniform grid it is a plain offset. This
444
- // follows the GRID, not whether the video is re-encoded — a variant cut on
445
- // the source's keyframes has to seek to them like the copy it accompanies.
446
- const seekSeconds = timeline.cutGrid === "keyframe"
447
- ? startSeconds + sourceStartTime
448
- : startSeconds;
449
- // Two-step seek when we have a real keyframe map: jump to a KNOWN-valid
450
- // keyframe (coarse, before -i — safe because WE sourced it from ffprobe,
451
- // not the container's own on-the-fly seek/index) and trim the short
452
- // residual (bounded by the keyframe interval) precisely AFTER -i, which is
453
- // always frame-accurate regardless of -accurate_seek.
454
- //
455
- // Root cause this works around: `-accurate_seek -ss X` before -i trusts the
456
- // CONTAINER's own seek to land near X. For some containers (observed: AVI
457
- // with VBR MP3 audio) that on-the-fly seek can point at a position with no
458
- // valid frame boundary at all — ffmpeg fails outright ("Seek failed" /
459
- // "Header missing"), not just imprecisely, and repeatedly so since every
460
- // retry re-tries the SAME bad container-computed position. A keyframe we
461
- // read directly from the packet list is a position ffmpeg has already
462
- // proven it can decode.
463
- //
464
- // The keyframe is CARRIED from the table that named this boundary, not looked
465
- // up by value. A boundary is stored on the player's clock, rounded, and
466
- // `seekSeconds` above puts the container's start time back on to reach the
467
- // file's clock — a lossy round trip. A keyframe at 26.234 s in a container
468
- // starting at 0.083 s comes back as 26.233999999999998, and "the keyframe at
469
- // or before that" is then the PREVIOUS one, 8.717 s earlier: two parts in a
470
- // quadrillion, one whole keyframe interval. That interval became a trim, the
471
- // trim moved every cut of the run backwards by another interval, and the
472
- // run's files were numbered from #36 while carrying film 17.4 s before what
473
- // the playlist says #36 holds (field 2026-09-05: the picture covered the
474
- // playhead, the sound had a 17.4 s hole across it, and the viewer waited two
475
- // minutes for a buffer that could never fill).
476
- //
477
- // The search stays for a grid restored without its source clock, which is the
478
- // only case that has nothing to carry.
479
- const carriedKeyframe = timeline.cutGrid === "keyframe" && typeof timeline.sourceStartOf === "function"
480
- ? timeline.sourceStartOf(safeIndex)
481
- : null;
482
- const snappedKeyframe = Number.isFinite(carriedKeyframe)
483
- ? carriedKeyframe
484
- : (Array.isArray(keyframes?.times) && keyframes.times.length > 0
485
- ? nearestKeyframeAtOrBefore(keyframes.times, seekSeconds)
486
- : null);
487
- // A second input, and it exists for exactly one case: a browser that takes
488
- // its audio muxed into the picture, watching a release whose soundtrack is a
489
- // file of its own. An audio RENDITION reads that file as its only input and
490
- // has none of this — which is why the ordinary path, and every browser that
491
- // understands rendition groups, still runs on a single input.
492
- const audioInputUrl =
493
- typeof audioInputUrlGiven === "string" && audioInputUrlGiven.length > 0
494
- ? audioInputUrlGiven
495
- : "";
496
- // Where the picture's own start sits on the soundtrack file's timeline. Both
497
- // files begin at their own container start time, and those need not be the
498
- // same number; the difference is what keeps the two aligned.
499
- const audioTimelineShift = audioInputUrl
500
- ? audioFileStartTime - sourceStartTime
501
- : 0;
502
- /**
503
- * Add the second input, if there is one, with its own seek.
504
- *
505
- * Called between the first `-i` and any OUTPUT option, because ffmpeg reads
506
- * these positionally: an option written after the last `-i` applies to the
507
- * output, and the residual seek below is exactly such an option. Getting the
508
- * order wrong would silently turn the audio file's seek into a trim of the
509
- * finished stream.
510
- *
511
- * @param {number} inputSeekSeconds - Where to start, on the PICTURE's
512
- * timeline. Translated to the soundtrack file's own here.
513
- */
514
- const pushAudioInput = (inputSeekSeconds) => {
515
- if (!audioInputUrl) {
516
- return;
517
- }
518
- // `-itsoffset` states the soundtrack's timestamps on the picture's
519
- // timeline, so everything after this point — `-copyts`, the output offset,
520
- // the cut list — goes on treating the two as one timeline, unchanged.
521
- //
522
- // ONLY on the branch that keeps the source's own timestamps. Without
523
- // `-copyts` ffmpeg rebases each input from its own seek point, and both
524
- // inputs are seeked to the same instant just below — so the two are
525
- // already aligned and adding the offset would pull them apart by exactly
526
- // the amount it exists to remove.
527
- if (audioTimelineShift !== 0 && keyframeGrid) {
528
- args.push("-itsoffset", ffmpegSeconds(-audioTimelineShift));
529
- }
530
- const audioSeek = Math.max(0, inputSeekSeconds + audioTimelineShift);
531
- if (audioSeek > 0) {
532
- // No keyframe to snap to and none needed: every audio frame is a sync
533
- // point, so the seek can be accurate outright.
534
- args.push("-accurate_seek", "-ss", ffmpegSeconds(audioSeek));
535
- }
536
- args.push("-i", audioInputUrl);
537
- };
538
-
539
- if (snappedKeyframe !== null) {
540
- const residualSeconds = Math.max(0, seekSeconds - snappedKeyframe);
541
- if (snappedKeyframe > 0) {
542
- args.push("-ss", ffmpegSeconds(snappedKeyframe + seekLandingOffsetFor({ audioOnly, transcodeVideo, keyframes }, snappedKeyframe)));
543
- }
544
- args.push("-i", inputUrl);
545
- // The coarse landing, not the exact target: the residual below is discarded
546
- // from the OUTPUT and so takes the same slice off every stream. Seeking the
547
- // soundtrack to the exact target as well would take that slice twice and
548
- // leave the sound running ahead of the picture by it.
549
- pushAudioInput(snappedKeyframe);
550
- // An output-side trim, and ONLY where the output is labelled from zero.
551
- //
552
- // Beside `-copyts` it does the opposite of what it says. Measured
553
- // 2026-09-06 on a file with a 5 s keyframe interval: a run landed at 15 s
554
- // and asked to trim to the cut at 20 s produced its first file starting at
555
- // 10 s, and a run asked to trim to 17 s produced cuts at 13.129, 18.129,
556
- // 23.129 — the whole grid moved back by the trim itself. The muxer's cut
557
- // times are absolute under `-copyts` while the trim is not, so every cut of
558
- // the run inherits the difference and the numbering, fixed at spawn, is
559
- // wrong by however many cuts that is.
560
- //
561
- // On the keyframe grid the run begins AT a cut, so there is nothing to
562
- // trim: the carried keyframe above makes this exactly zero rather than
563
- // nearly zero.
564
- if (residualSeconds > 0 && !keyframeGrid) {
565
- args.push("-ss", ffmpegSeconds(residualSeconds));
566
- }
567
- } else {
568
- if (seekSeconds > 0) {
569
- // No keyframe map (probe failed/timed out) — fall back to the previous
570
- // behaviour: trust the container's own accurate seek.
571
- args.push("-accurate_seek", "-ss", ffmpegSeconds(seekSeconds));
572
- }
573
- args.push("-i", inputUrl);
574
- pushAudioInput(seekSeconds);
575
- }
576
- // Which timeline the output is labelled on. An audio rendition has no
577
- // picture of its own to follow, so it follows the grid it was given — the
578
- // same one the video it plays with is on. Deciding by `transcodeVideo`, as
579
- // everything else here does, would put the audio of a re-encoded stream on
580
- // the copy branch: `-copyts` and a shift by the container's start time,
581
- // against a picture labelled from zero. The two would be offset by
582
- // `sourceStartTime` for the whole file.
583
- if (!keyframeGrid) {
584
- // Branch A (re-encode): fixed GOP makes keyframes land exactly on the
585
- // segment grid; relabel output onto the original timeline so segment N
586
- // carries PTS = N × segmentDuration.
587
- if (startSeconds > 0) {
588
- args.push("-output_ts_offset", ffmpegSeconds(startSeconds));
589
- }
590
- } else {
591
- // Branch B (video copied — only audio is transcoded): we cannot insert
592
- // keyframes, so segments are cut at the source's own keyframes (the
593
- // playlist boundaries were built from those keyframes). Keep the source's
594
- // real timestamps (`-copyts`) so copied frames stay continuous across
595
- // boundaries/seeks, and shift by -startTime so the output timeline is
596
- // 0-based (a non-zero container start otherwise puts a hole at the very
597
- // beginning and desyncs audio/video). Audio is transcoded on this timeline.
598
- args.push("-copyts");
599
- if (sourceStartTime !== 0) {
600
- args.push("-output_ts_offset", ffmpegSeconds(-sourceStartTime));
601
- }
602
- }
603
- // Where this run STOPS. Until now a run had a start and no end — neither
604
- // `-to` nor `-t` appeared anywhere in the arguments this proxy builds — so
605
- // every stop was a kill from outside, and two runs on one output could only
606
- // be kept apart by giving each its own directory. With an end they cannot
607
- // reach each other's numbers at all, and a run that finishes its stretch
608
- // exits by itself instead of having to be noticed and killed.
609
- //
610
- // WHICH argument states it is a property of the branch, and it is measured
611
- // rather than reasoned (2026-09-04, `research/encoder-layer-2026-09-04.md`
612
- // §11): `-t` is a duration on the output's own clock, and `-to` a point on
613
- // the input's. The copy branch runs with `-copyts`, where the input's clock
614
- // IS the source's, so `-to` takes the absolute time; the re-encode branch
615
- // has no `-copyts` and takes the duration. Swapping them is not a near
616
- // miss — on the copy branch `-t` produced one segment where five were
617
- // wanted, because the time it names is already past when the run starts.
618
- const runEnd = Number.isInteger(endIndex) ? endIndex : -1;
619
- const publishedGrid = publishedGridFor(timeline);
620
- if (runEnd >= safeIndex && Array.isArray(publishedGrid) && publishedGrid[runEnd + 1] > 0) {
621
- const endsAt = publishedGrid[runEnd + 1];
622
- if (transcodeVideo) {
623
- args.push("-t", ffmpegSeconds(Math.max(0.1, endsAt - publishedGrid[safeIndex])));
624
- } else {
625
- args.push("-to", ffmpegSeconds(endsAt));
626
- }
627
- }
628
- if (audioOnly === true) {
629
- // An audio RENDITION: one track, no picture. Published as its own
630
- // `#EXT-X-MEDIA` and shared by every video variant, so the track is
631
- // encoded once for the file instead of once per rung, and changing it is
632
- // the player switching rendition rather than this proxy rebuilding the
633
- // session. Cut on the same grid as the video it accompanies, which is
634
- // what lets the two be played together.
635
- // `0:` because a rendition's only input IS the file its track lives in —
636
- // the picture's own file, or the one beside it that carries this dub.
637
- args.push("-vn", "-map", `0:a:${audioSourceTrackIndex}?`, ...audioCodecArgs);
638
- } else if (servesAudioSeparately) {
639
- // The other half of the same arrangement: the picture alone, because its
640
- // audio is published as a rendition and would otherwise play twice.
641
- args.push("-an", "-map", "0:v:0?", ...videoCodecArgs);
642
- } else {
643
- args.push(
644
- "-map",
645
- "0:v:0?",
646
- "-map",
647
- // The audio track the viewer chose: input 1 when their choice is a
648
- // soundtrack shipped as its own file, input 0 when it is one of the
649
- // picture's own. Type-relative within that input, which is what
650
- // `audioSourceTrackIndex` holds — the number the browser sent is flat
651
- // across both files and was resolved when the session was made.
652
- `${audioInputUrl ? 1 : 0}:a:${audioSourceTrackIndex}?`,
653
- ...videoCodecArgs,
654
- ...audioCodecArgs
655
- );
656
- }
657
-
658
- // Where the cuts come from. On the copy path they are the source's own
659
- // keyframes, and until now they were only ever GUESSED: ffmpeg got a target
660
- // duration and chose its own cut points, while the playlist was built from
661
- // the container index — two independent calculations with nothing tying
662
- // them together but the hope that they agree. They do not. The index is a
663
- // navigation table and is not obliged to list every keyframe; for a field
664
- // file it held 1902 while ffmpeg found roughly twice as many and cut twice
665
- // as often. Segment #876 then meant 1:26:50 to the player and about minute
666
- // 58 to ffmpeg, which is why a seek landed nowhere near where it was aimed
667
- // and the reported duration drifted.
668
- //
669
- // So stop guessing and say it: the `segment` muxer takes the list of times
670
- // outright. Passing the very boundaries the playlist was built from makes
671
- // the two agree by construction. Only cut points already known to be real
672
- // keyframes are sent, so ffmpeg never has to move one forward.
673
- //
674
- // The list is built above, before the encoder args, because a re-encoded
675
- // variant of a copied stream needs the same times twice over: once as the
676
- // cuts, once as the keyframes to force at them.
677
- if (cutTimes && cutTimes.length > 0) {
678
- args.push(
679
- "-f",
680
- "segment",
681
- // Times are measured from the START OF THIS RUN, not from the start of
682
- // the file — verified: starting at 12 s and asking for a cut at 18 s
683
- // produced one at 29.4 s. `segmentCutTimesFrom` rebases them.
684
- "-segment_times",
685
- cutTimes.join(","),
686
- // A cut lands on the first keyframe at or after its time, so a boundary
687
- // recorded a hair late would skip to the next one and double the
688
- // segment. The tolerance absorbs that rounding.
689
- "-segment_time_delta",
690
- "0.05",
691
- "-segment_start_number",
692
- String(safeIndex),
693
- // THE ENCODER SAYS WHEN A PIECE IS FINISHED, on a channel of its own.
694
- //
695
- // Measured on the addon host 2026-09-05: a name appears in this list when
696
- // the file is CLOSED, not when it is created — at the third sample
697
- // `seg-000.mp4` was on disk and absent from the list, and it appeared at
698
- // the fourth, in the same moment `seg-001.mp4` came into being. So a name
699
- // here is the writer's own statement that the piece is whole.
700
- //
701
- // Without it, a finished file is indistinguishable from one still being
702
- // written, and the only proof available was the existence of the NEXT
703
- // one — which never comes for the last piece of every run.
704
- "-segment_list",
705
- "pipe:3",
706
- "-segment_list_flags",
707
- "+live",
708
- ...explicitTimes,
709
- // UNDER A WORKING NAME, not the one it is served as. A piece under its
710
- // served name is complete by construction then, whoever else is writing
711
- // into the same directory — and the name arrives on the channel above the
712
- // instant ffmpeg closes it, which is what turns it into the served one.
713
- //
714
- // The other branch needs none of this: the HLS muxer writes through a
715
- // temporary name of its own (`+temp_file`), so its files appear under
716
- // their final name whole.
717
- //
718
- // TAGGED WITH THE STRETCH IT WAS GIVEN, which names the run without any
719
- // counter to keep: intervals never overlap, so two live runs of one output
720
- // begin at different numbers by construction. That is what makes clearing
721
- // up after a dead run a well-formed question — its unfinished pieces are
722
- // the ones carrying its own tag — where before it was answered by taking
723
- // the highest SERVED name inside its stretch and judging the bytes.
724
- segmentFormat.makingFileNameTemplate(String(safeIndex))
725
- );
726
- } else {
727
- args.push(
728
- "-f",
729
- "hls",
730
- "-hls_time",
731
- String(segmentDurationSec),
732
- "-hls_list_size",
733
- "0",
734
- "-hls_flags",
735
- "independent_segments+temp_file",
736
- // Container selection + segment naming, from the active format module.
737
- ...segmentFormat.muxerArgs(),
738
- "-start_number",
739
- String(safeIndex),
740
- // ffmpeg writes its own playlist here; we ignore it and serve the
741
- // synthetic VOD playlist instead (see getFileStream).
742
- PLAYLIST_FILE_NAME
743
- );
744
- }
745
- return { args, safeIndex, startSeconds, cutTimes };
746
-
747
- }
1
+ /**
2
+ * @file The full argument list for one encoder run.
3
+ *
4
+ * What a run is given is a fact about WHAT is being produced and WHERE it
5
+ * begins, and about nothing else — not the session it belongs to, not who is
6
+ * watching, not how many viewers there are. It was a 377-line method of the
7
+ * session manager reading fifteen of its fields, which is why a run could only
8
+ * ever be built by that class, for the one session it holds.
9
+ *
10
+ * Stated here, over the material and the stretch alone, a run can be built by
11
+ * whoever needs one. That is what lets an output have more than a single
12
+ * encoder.
13
+ *
14
+ * **Nothing in this file runs anything.** It returns an argument list; spawning,
15
+ * killing and resuming belong to whoever owns the process.
16
+ */
17
+
18
+ import { PLAYLIST_FILE_NAME } from "./output/playlists.js";
19
+ import { AUDIO_TRANSCODE_KBPS } from "./args.js";
20
+
21
+ /**
22
+ * What ffmpeg's own CLI subtracts from an input seek, and therefore what has to
23
+ * be added back to land where we asked.
24
+ *
25
+ * `fftools/ffmpeg_demux.c`, in `ifile_open`: when the container does not
26
+ * declare `AVFMT_SEEK_TO_PTS` — Matroska does not — and any stream carries
27
+ * B-frames, the seek target is moved back by `3*AV_TIME_BASE / 23` before
28
+ * `avformat_seek_file` is called. Its purpose is sound: such containers seek in
29
+ * decode order while the caller asks in presentation order, and with B-frames
30
+ * the two differ, so it backs off far enough to be sure of reaching the frame
31
+ * asked for.
32
+ *
33
+ * The consequence for a COPY is that asking for a keyframe lands on the one
34
+ * BEFORE it — deterministically, every time. Measured 2026-08-21 on a Matroska
35
+ * file with keyframes every 2 s: `-ss 10` produced a first segment starting at
36
+ * 8.000; `-ss 10.130435` produced one starting at 10.000. On MP4, where the
37
+ * heuristic does not fire, all of 10, 10.130435 and 10.2 produced 10.000 — so
38
+ * adding this is right in one case and harmless in the other.
39
+ *
40
+ * That landing is what `-segment_times` is measured from, while this code
41
+ * computes those offsets from the time it ASKED for. One keyframe interval
42
+ * apart, inherited by every cut of the run: 119 of 125 segments arriving a
43
+ * uniform 2.002 s early in the field, four times what a player bridges.
44
+ *
45
+ * Not applied when the picture is re-encoded: a re-encode decodes from the
46
+ * keyframe and discards frames up to the requested time, so its output already
47
+ * begins exactly where asked (measured the same day: `-ss 11` copied starts at
48
+ * 10.000, re-encoded at 11.000).
49
+ */
50
+ export const SEEK_LANDING_OFFSET_SEC = 3 / 23;
51
+
52
+ /**
53
+ * A number of seconds as ffmpeg will accept it.
54
+ *
55
+ * `String(n)` switches to exponential notation below 1e-6, and ffmpeg's
56
+ * duration parser rejects that outright: a field session died on
57
+ * `Invalid duration for option ss: 3.3333333249174757e-7`, after which the
58
+ * transcode was in state `failed` and every segment request answered 500 for
59
+ * as long as the viewer kept trying. Anything under a millisecond is also not a
60
+ * real offset — it is the residue of subtracting two nearly equal floats — so
61
+ * it is dropped rather than passed on.
62
+ *
63
+ * @param {number} value
64
+ * @returns {string}
65
+ */
66
+ export function ffmpegSeconds(value) {
67
+ if (!Number.isFinite(value) || Math.abs(value) < 0.001) {
68
+ return "0";
69
+ }
70
+ // Microsecond resolution, fixed notation, no trailing zero noise.
71
+ return value.toFixed(6).replace(/\.?0+$/, "");
72
+ }
73
+
74
+ /**
75
+ * Which timeline an output's own ffmpeg works on.
76
+ *
77
+ * True — the COPY branch: the source's timestamps are kept (`-copyts`) and the
78
+ * output is re-labelled 0-based. Everything handed to the muxer is therefore
79
+ * stated in the source's terms, and everything read back out of a produced
80
+ * piece is 0-based.
81
+ *
82
+ * False — the re-encode branch: the output is labelled from the run's start on
83
+ * the 0-based timeline, and the muxer is addressed in those same terms.
84
+ *
85
+ * One predicate for both callers, because the two used to answer it separately
86
+ * and a disagreement between them is exactly what desynced picture from sound.
87
+ *
88
+ * @param {{ audioOnly?: boolean, timeline?: { cutGrid?: string }, transcodeVideo?: boolean }} material
89
+ * @returns {boolean}
90
+ */
91
+ export function onKeyframeGridFor(material) {
92
+ return material?.audioOnly === true
93
+ ? material?.timeline?.cutGrid === "keyframe"
94
+ : material?.transcodeVideo !== true;
95
+ }
96
+
97
+ /**
98
+ * The boundary table the player is working from: the one its playlist was
99
+ * written from, falling back to the live table when no playlist was built from
100
+ * a table at all (no duration, so no synthetic playlist — and then nothing the
101
+ * player holds contradicts it).
102
+ *
103
+ * @param {{ published?: number[], boundaries?: number[] }} timeline
104
+ * @returns {number[]}
105
+ */
106
+ export function publishedGridFor(timeline) {
107
+ return Array.isArray(timeline?.published) && timeline.published.length > 0
108
+ ? timeline.published
109
+ : (timeline?.boundaries ?? []);
110
+ }
111
+
112
+ /**
113
+ * Where a run beginning at `index` must be positioned: the time the PLAYER was
114
+ * told that segment starts at.
115
+ *
116
+ * Two tables, deliberately: the live one is corrected as produced segments
117
+ * reveal where the file's cuts truly are, and those corrections are what let a
118
+ * re-encoded rung be forced onto a copied stream's real grid. But the playlist
119
+ * a player is holding was written once and never changes, so a position taken
120
+ * from the corrected table describes a timeline nobody sent the player. That is
121
+ * not a subtlety: it cost ten minutes of a dead film on 2026-08-17, the browser
122
+ * asking for two segments 1908 times each.
123
+ *
124
+ * @param {{ published?: number[], boundaries?: number[] }} timeline
125
+ * @param {number} index
126
+ * @param {number} segmentDurationSec - Used only when the file has no table at
127
+ * all, where a segment is a plain multiple of the nominal length.
128
+ * @returns {number}
129
+ */
130
+ export function publishedStartTime(timeline, index, segmentDurationSec) {
131
+ const published = Array.isArray(timeline?.published) && timeline.published.length > 0 ? timeline.published : null;
132
+ const table = published ?? (Array.isArray(timeline?.boundaries) ? timeline.boundaries : []);
133
+ if (table.length === 0) {
134
+ return index * segmentDurationSec;
135
+ }
136
+ const clamped = Math.max(0, Math.min(index, table.length - 1));
137
+ return table[clamped];
138
+ }
139
+
140
+ /**
141
+ * Where a segment REALLY begins, when a produced piece has said so.
142
+ *
143
+ * The live table is corrected as produced pieces reveal where a file's cuts
144
+ * truly are; the published one is what the player was told and may never move.
145
+ * Where the two disagree, the live table is a measurement and the published one
146
+ * a prediction — and this answers with the measurement, or with nothing when
147
+ * they agree or nothing has been measured.
148
+ *
149
+ * @param {object} timeline
150
+ * @param {number} index
151
+ * @returns {number | undefined}
152
+ */
153
+ export function trueStartOf(timeline, index) {
154
+ const live = Array.isArray(timeline?.boundaries) ? timeline.boundaries : null;
155
+ const published = Array.isArray(timeline?.published) ? timeline.published : null;
156
+ if (!live || !published || index < 0 || index >= live.length || index >= published.length) {
157
+ return undefined;
158
+ }
159
+ return live[index] === published[index] ? undefined : live[index];
160
+ }
161
+
162
+ /**
163
+ * The cut times to hand ffmpeg for a run that starts at `startIndex`.
164
+ *
165
+ * Two adjustments, both of which cost a broken session to learn:
166
+ *
167
+ * - **Rebased.** `-segment_times` is measured from the start of the run, not
168
+ * of the file. Measured: starting at 12 s and asking for a cut at 18 s put
169
+ * it at 29.4 s — 12 + 18. So every boundary has the run's own start
170
+ * subtracted.
171
+ * - **Interior only.** The first boundary is where the run begins and the last
172
+ * is where the file ends; neither is a cut. Sending them would produce an
173
+ * empty leading segment and a spurious trailing one.
174
+ *
175
+ * @param {number[]} boundaries
176
+ * @param {number} startIndex
177
+ * @returns {number[] | null}
178
+ */
179
+ export function segmentCutTimesFrom(boundaries, startIndex) {
180
+ if (!Array.isArray(boundaries) || boundaries.length < 2) {
181
+ return null;
182
+ }
183
+ const index = Number.isInteger(startIndex) && startIndex > 0 ? startIndex : 0;
184
+ if (index >= boundaries.length - 1) {
185
+ return null;
186
+ }
187
+ const base = boundaries[index];
188
+ const times = [];
189
+ for (let at = index + 1; at < boundaries.length - 1; at += 1) {
190
+ times.push(Number((boundaries[at] - base).toFixed(6)));
191
+ }
192
+ return times;
193
+ }
194
+
195
+ /**
196
+ * The largest keyframe time that does not exceed `target`, from a SORTED
197
+ * (ascending) array of keyframe times. Null when `target` is before the first
198
+ * keyframe or the array is empty — the caller then falls back to its unsnapped
199
+ * target.
200
+ *
201
+ * @param {number[]} keyframeTimes - Sorted ascending.
202
+ * @param {number} target
203
+ * @returns {number | null}
204
+ */
205
+ export function nearestKeyframeAtOrBefore(keyframeTimes, target) {
206
+ let result = null;
207
+ for (const time of keyframeTimes) {
208
+ if (time > target) {
209
+ break;
210
+ }
211
+ result = time;
212
+ }
213
+ return result;
214
+ }
215
+
216
+ /**
217
+ * How much later than a keyframe to ASK, so that ffmpeg lands on that keyframe.
218
+ *
219
+ * Bounded by half the distance to the next keyframe, which matters only where
220
+ * keyframes stand closer together than twice the offset. There no single value
221
+ * can satisfy both worlds — asking too little lands a keyframe early when the
222
+ * heuristic fires, asking too much lands a keyframe late when it does not — and
223
+ * the bound picks the smaller error, which is then under one keyframe interval
224
+ * and therefore under what a player bridges.
225
+ *
226
+ * @param {{ transcodeVideo?: boolean, audioOnly?: boolean, keyframes?: { times?: number[] | null, tolerance?: number } }} material
227
+ * @param {number} keyframe - A real keyframe time the run is to begin at.
228
+ * @returns {number} Seconds to add to the request.
229
+ */
230
+ export function seekLandingOffsetFor(material, keyframe) {
231
+ // A re-encode trims to the requested time itself, so it needs no help and
232
+ // must not be pushed past what it was asked for.
233
+ //
234
+ // AN OUTPUT CARRYING ONLY SOUND IS SUCH A RE-ENCODE, and asking about the
235
+ // picture missed it: the flag says whether the PICTURE is re-encoded, and an
236
+ // output with no picture answers no. So the whole offset was added to the
237
+ // soundtrack's own seek, and the sound then played 130 ms ahead of the picture
238
+ // from every restart onward — reported by the viewer 2026-09-06 as lips out of
239
+ // step with the voice from two minutes in, and measured: both outputs were
240
+ // repositioned to the same boundary and both given `-ss 166.963435`, after
241
+ // which the copied picture landed on the keyframe at 166.833 while the
242
+ // re-encoded sound began where it was asked.
243
+ if (material?.transcodeVideo === true || material?.audioOnly === true) {
244
+ return 0;
245
+ }
246
+ // A grid whose times are approximate needs that error added on top, or a name
247
+ // sitting just below its real keyframe seeks to before it and lands on the
248
+ // one before that. Only AVI declares one.
249
+ const tolerance = Number.isFinite(material?.keyframes?.tolerance)
250
+ ? Math.max(0, material.keyframes.tolerance)
251
+ : 0;
252
+ const wanted = SEEK_LANDING_OFFSET_SEC + tolerance;
253
+ const times = Array.isArray(material?.keyframes?.times) ? material.keyframes.times : [];
254
+ const next = times.find((time) => time > keyframe + 0.001);
255
+ if (next === undefined) {
256
+ return wanted;
257
+ }
258
+ return Math.min(wanted, (next - keyframe) / 2);
259
+ }
260
+
261
+ /**
262
+ * Everything ffmpeg is told for one run.
263
+ *
264
+ * @param {object} params
265
+ * @param {{ times?: number[] | null, tolerance?: number }} params.keyframes - Where
266
+ * the PICTURE's file has its keyframes: what a seek snaps to. Plain values,
267
+ * so this can be asked without a container, a torrent or a session.
268
+ * @param {{ startTime: number }} params.inputFile - The file this run reads.
269
+ * @param {{ startTime: number }} params.audioFile - The file the chosen
270
+ * soundtrack lives in, which for a dub shipped beside the picture is not the
271
+ * picture's own.
272
+ * @param {string} params.inputUrl
273
+ * @param {string} params.audioInputUrl - Empty unless a browser that takes its
274
+ * audio muxed is watching a release whose soundtrack is a file of its own.
275
+ * @param {{ published?: number[], boundaries?: number[], cutGrid?: string }} params.timeline
276
+ * @param {{ encodeWidth: number, encodeHeight: number, outputFps: number, softwarePreset: string | null, applyTonemap: boolean }} params.output
277
+ * @param {object} params.segmentFormat
278
+ * @param {boolean} params.transcodeVideo
279
+ * @param {boolean} params.transcodeAudio
280
+ * @param {boolean} params.audioOnly - An audio rendition: one track, no picture.
281
+ * @param {boolean} params.audioSeparate - The picture's sound is published as a
282
+ * rendition, so this output carries none.
283
+ * @param {number} params.audioSourceTrackIndex - `0:a:N` within its own file.
284
+ * @param {{ maxrateKbps: number, bufsizeKbps: number, level: string | null } | null} params.rateControl
285
+ * The output's own, as its key names it; null where nothing bounds the rate.
286
+ * @param {number} params.startIndex - First segment number this run makes.
287
+ * @param {number} params.endIndex - Last it makes, inclusive; below the start
288
+ * means it has no end.
289
+ * @param {number | undefined} params.positionSecondsOverride - Where to begin,
290
+ * when the caller knows better than the table.
291
+ * @param {object} params.videoEncoder
292
+ * @param {number} params.segmentDurationSec
293
+ * @returns {{ args: string[], safeIndex: number, startSeconds: number, cutTimes: number[] | null }}
294
+ */
295
+ export function buildRunCommand({
296
+ keyframes,
297
+ inputFile,
298
+ audioFile,
299
+ inputUrl,
300
+ audioInputUrl: audioInputUrlGiven,
301
+ timeline,
302
+ output,
303
+ segmentFormat,
304
+ transcodeVideo,
305
+ transcodeAudio,
306
+ audioOnly,
307
+ audioSeparate,
308
+ audioSourceTrackIndex,
309
+ rateControl,
310
+ startIndex,
311
+ endIndex,
312
+ positionSecondsOverride,
313
+ videoEncoder,
314
+ segmentDurationSec
315
+ }) {
316
+ const safeIndex = Number.isInteger(startIndex) && startIndex > 0 ? startIndex : 0;
317
+ // 0-based output time of this segment, from the table the PLAYER holds —
318
+ // the same one the cut list below is taken from.
319
+ //
320
+ // These two were read from different tables until 2026-08-21, and that is
321
+ // one fault, not two: `-segment_times` are measured from wherever the run
322
+ // really began, so any distance between the position and the cut list moves
323
+ // EVERY cut of that run by it. The live table keeps being corrected as
324
+ // produced segments reveal where the file's cuts truly are, and those
325
+ // corrections run backwards, so each restart began a little earlier than
326
+ // the grid the cuts were stated on — and since the corrections accumulate,
327
+ // so did the distance. Measured on `JUFD665.mp4`: after one seek restart a
328
+ // produced segment held the boundary two places before its own number
329
+ // (16.684 s, exactly 2.0000 segments), after the next it held the one four
330
+ // places before (33.5 s). The player's buffer then stops extending at all,
331
+ // because the content of every fragment lands before the time its playlist
332
+ // entry names: `bufferEnd` stood still at 4571.1 s through four `frag-far`
333
+ // warnings until hls.js gave up and jumped the viewer 16.8 s forward.
334
+ //
335
+ // 2.45.0 moved the CUT LIST onto the published table for this same reason
336
+ // and left the position on the live one. Both belong on the published
337
+ // table: a run must begin where the player was told the segment begins.
338
+ const startSeconds = Number.isFinite(positionSecondsOverride)
339
+ ? positionSecondsOverride
340
+ : publishedStartTime(timeline, safeIndex, segmentDurationSec);
341
+ // Where each of the two timelines begins, asked of the files themselves.
342
+ // Fresh by construction: the session may have been created before the
343
+ // soundtrack file's header could be read, and the reading lands on the file
344
+ // object this session holds — so there is nothing to re-read and nothing
345
+ // that can be stale. Same property as the keyframe table, which is one
346
+ // object shared by every session of the file.
347
+ // Which timeline this run works on. Asked once, because the closure that
348
+ // adds the second input reads it too, and two readings of one predicate is
349
+ // how the picture and the sound came apart before.
350
+ const keyframeGrid = onKeyframeGridFor({ audioOnly, timeline, transcodeVideo });
351
+ const servesAudioSeparately = audioOnly !== true && audioSeparate === true;
352
+ const audioFileStartTime = audioFile.startTime;
353
+ // The start time of the file this run READS, which is the picture's own for
354
+ // every session except one whose soundtrack is a separate file.
355
+ const sourceStartTime = inputFile.startTime;
356
+ // Cut where this session's grid says, whoever is producing the frames. The
357
+ // times are measured from the start of THIS run; the same list serves as
358
+ // the cut points and, when re-encoding, as the keyframes to force — one
359
+ // list, so the two cannot drift apart.
360
+ const explicitTimes = segmentFormat.explicitTimesMuxerArgs?.() ?? null;
361
+ // A COPY is cut by this list whatever grid it ended up on. Even when no
362
+ // keyframe index could be read and the boundaries are a plain grid, saying
363
+ // them outright is what keeps the playlist and the muxer agreeing — ffmpeg
364
+ // moves each cut forward to the first real keyframe, and serving reads back
365
+ // where the piece truly begins. Requiring a keyframe grid here dropped a
366
+ // copy with no index onto the `hls` muxer, which takes no cut list and
367
+ // writes no self-contained pieces, so nothing could read a true start and
368
+ // segments were stamped with times the file does not have — the 4.17 s
369
+ // speech-against-subtitles drift, back again.
370
+ //
371
+ // Cut on the grid the PLAYER WAS GIVEN, not on the corrected one. A player
372
+ // places a fragment by the playlist it holds, and that text was written
373
+ // once and never changes; the live table keeps moving as produced segments
374
+ // reveal where the file's cuts really are. Cutting on the moved table makes
375
+ // every run faithful to a timeline nobody sent the player — measured
376
+ // 2026-08-20, the picture's segments arriving a uniform 2.002 s before the
377
+ // times the playlist named for them, which is four times what hls.js will
378
+ // bridge, so the fragment does not land and is asked for again.
379
+ //
380
+ // The corrections keep their purpose: they describe the file, and a variant
381
+ // created later inherits the corrected table and PUBLISHES it, so its own
382
+ // playlist and its own cuts agree from the start. What they may not do is
383
+ // move the cuts of a session whose playlist is already being read.
384
+ const gridCutTimes = explicitTimes && (!transcodeVideo || timeline.cutGrid === "keyframe")
385
+ ? segmentCutTimesFrom(publishedGridFor(timeline), safeIndex)
386
+ : null;
387
+ // Cut times are stated on the grid, for both branches.
388
+ //
389
+ // 2.28.0 added `sourceStartTime` to them on the copy branch, reasoning that
390
+ // the muxer decides its cuts before the output is relabelled. The field
391
+ // measured it the next session and the reasoning was wrong: of 75 pieces
392
+ // the picture produced, only NINE began at a time the container's own
393
+ // keyframe table names (the soundtrack, untouched by the change, scored 70
394
+ // of 75). Before it, every piece began exactly on a named keyframe and it
395
+ // was the PLAYLIST that disagreed with them. So the shift moved the cuts
396
+ // OFF the keyframes rather than onto them, and it is gone.
397
+ //
398
+ // What remains true, and is what that measurement is really about: the
399
+ // picture cuts where the source's keyframes are, and the playlist must be
400
+ // built from those same times. That is the correction path's job, not the
401
+ // cut list's.
402
+ const cutTimes = gridCutTimes;
403
+
404
+ // Video: re-encode only when required, using the detected encoder
405
+ // (hardware-accelerated or software). The descriptor builds the filter +
406
+ // codec args (including keyframe alignment on segment boundaries).
407
+ const videoCodecArgs = transcodeVideo
408
+ ? videoEncoder.buildVideoArgs({
409
+ // Budget-selected encode box (may be below the client target on weak
410
+ // software hosts); falls back to the client target for hardware.
411
+ targetWidth: output.encodeWidth,
412
+ targetHeight: output.encodeHeight,
413
+ segmentDurationSec: segmentDurationSec,
414
+ // Source-inherited output rate (integer, capped); descriptors that
415
+ // use time-based keyframes just apply it as the frame rate.
416
+ fps: output.outputFps,
417
+ // Software-only; hardware descriptors ignore it.
418
+ preset: output.softwarePreset ?? undefined,
419
+ // HDR→SDR tone map (software path only; gated on filter availability).
420
+ tonemap: output.applyTonemap === true,
421
+ // On the source's grid the cuts are not evenly spaced, so no frame
422
+ // count can describe them: the encoder is told the times outright,
423
+ // the same ones the muxer will cut at.
424
+ forcedKeyframeTimes: cutTimes,
425
+ // The output's own rate control and level. Read off the output, whose
426
+ // key names them: two limits are two outputs, and nothing moves a
427
+ // limit under an output that already exists.
428
+ rateControl: rateControl ?? null
429
+ })
430
+ : ["-c:v", "copy"];
431
+ const audioCodecArgs = transcodeAudio
432
+ ? ["-c:a", "aac", "-ac", "2", "-b:a", `${AUDIO_TRANSCODE_KBPS}k`]
433
+ : ["-c:a", "copy"];
434
+
435
+ const args = ["-hide_banner", "-nostats", "-loglevel", "error", "-progress", "pipe:1"];
436
+ // Hardware decode/encode setup (e.g. VAAPI device) must precede -i, and
437
+ // only applies when we actually re-encode the video track.
438
+ if (transcodeVideo && Array.isArray(videoEncoder.inputArgs)) {
439
+ args.push(...videoEncoder.inputArgs);
440
+ }
441
+ // Seek position in SOURCE time. On the keyframe grid `startSeconds` is a
442
+ // real keyframe's offset from zero, so the container's own start time goes
443
+ // back on to reach it; on the uniform grid it is a plain offset. This
444
+ // follows the GRID, not whether the video is re-encoded — a variant cut on
445
+ // the source's keyframes has to seek to them like the copy it accompanies.
446
+ const seekSeconds = timeline.cutGrid === "keyframe"
447
+ ? startSeconds + sourceStartTime
448
+ : startSeconds;
449
+ // Two-step seek when we have a real keyframe map: jump to a KNOWN-valid
450
+ // keyframe (coarse, before -i — safe because WE sourced it from ffprobe,
451
+ // not the container's own on-the-fly seek/index) and trim the short
452
+ // residual (bounded by the keyframe interval) precisely AFTER -i, which is
453
+ // always frame-accurate regardless of -accurate_seek.
454
+ //
455
+ // Root cause this works around: `-accurate_seek -ss X` before -i trusts the
456
+ // CONTAINER's own seek to land near X. For some containers (observed: AVI
457
+ // with VBR MP3 audio) that on-the-fly seek can point at a position with no
458
+ // valid frame boundary at all — ffmpeg fails outright ("Seek failed" /
459
+ // "Header missing"), not just imprecisely, and repeatedly so since every
460
+ // retry re-tries the SAME bad container-computed position. A keyframe we
461
+ // read directly from the packet list is a position ffmpeg has already
462
+ // proven it can decode.
463
+ //
464
+ // The keyframe is CARRIED from the table that named this boundary, not looked
465
+ // up by value. A boundary is stored on the player's clock, rounded, and
466
+ // `seekSeconds` above puts the container's start time back on to reach the
467
+ // file's clock — a lossy round trip. A keyframe at 26.234 s in a container
468
+ // starting at 0.083 s comes back as 26.233999999999998, and "the keyframe at
469
+ // or before that" is then the PREVIOUS one, 8.717 s earlier: two parts in a
470
+ // quadrillion, one whole keyframe interval. That interval became a trim, the
471
+ // trim moved every cut of the run backwards by another interval, and the
472
+ // run's files were numbered from #36 while carrying film 17.4 s before what
473
+ // the playlist says #36 holds (field 2026-09-05: the picture covered the
474
+ // playhead, the sound had a 17.4 s hole across it, and the viewer waited two
475
+ // minutes for a buffer that could never fill).
476
+ //
477
+ // The search stays for a grid restored without its source clock, which is the
478
+ // only case that has nothing to carry.
479
+ const carriedKeyframe = timeline.cutGrid === "keyframe" && typeof timeline.sourceStartOf === "function"
480
+ ? timeline.sourceStartOf(safeIndex)
481
+ : null;
482
+ const snappedKeyframe = Number.isFinite(carriedKeyframe)
483
+ ? carriedKeyframe
484
+ : (Array.isArray(keyframes?.times) && keyframes.times.length > 0
485
+ ? nearestKeyframeAtOrBefore(keyframes.times, seekSeconds)
486
+ : null);
487
+ // A second input, and it exists for exactly one case: a browser that takes
488
+ // its audio muxed into the picture, watching a release whose soundtrack is a
489
+ // file of its own. An audio RENDITION reads that file as its only input and
490
+ // has none of this — which is why the ordinary path, and every browser that
491
+ // understands rendition groups, still runs on a single input.
492
+ const audioInputUrl =
493
+ typeof audioInputUrlGiven === "string" && audioInputUrlGiven.length > 0
494
+ ? audioInputUrlGiven
495
+ : "";
496
+ // Where the picture's own start sits on the soundtrack file's timeline. Both
497
+ // files begin at their own container start time, and those need not be the
498
+ // same number; the difference is what keeps the two aligned.
499
+ const audioTimelineShift = audioInputUrl
500
+ ? audioFileStartTime - sourceStartTime
501
+ : 0;
502
+ /**
503
+ * Add the second input, if there is one, with its own seek.
504
+ *
505
+ * Called between the first `-i` and any OUTPUT option, because ffmpeg reads
506
+ * these positionally: an option written after the last `-i` applies to the
507
+ * output, and the residual seek below is exactly such an option. Getting the
508
+ * order wrong would silently turn the audio file's seek into a trim of the
509
+ * finished stream.
510
+ *
511
+ * @param {number} inputSeekSeconds - Where to start, on the PICTURE's
512
+ * timeline. Translated to the soundtrack file's own here.
513
+ */
514
+ const pushAudioInput = (inputSeekSeconds) => {
515
+ if (!audioInputUrl) {
516
+ return;
517
+ }
518
+ // `-itsoffset` states the soundtrack's timestamps on the picture's
519
+ // timeline, so everything after this point — `-copyts`, the output offset,
520
+ // the cut list — goes on treating the two as one timeline, unchanged.
521
+ //
522
+ // ONLY on the branch that keeps the source's own timestamps. Without
523
+ // `-copyts` ffmpeg rebases each input from its own seek point, and both
524
+ // inputs are seeked to the same instant just below — so the two are
525
+ // already aligned and adding the offset would pull them apart by exactly
526
+ // the amount it exists to remove.
527
+ if (audioTimelineShift !== 0 && keyframeGrid) {
528
+ args.push("-itsoffset", ffmpegSeconds(-audioTimelineShift));
529
+ }
530
+ const audioSeek = Math.max(0, inputSeekSeconds + audioTimelineShift);
531
+ if (audioSeek > 0) {
532
+ // No keyframe to snap to and none needed: every audio frame is a sync
533
+ // point, so the seek can be accurate outright.
534
+ args.push("-accurate_seek", "-ss", ffmpegSeconds(audioSeek));
535
+ }
536
+ args.push("-i", audioInputUrl);
537
+ };
538
+
539
+ if (snappedKeyframe !== null) {
540
+ const residualSeconds = Math.max(0, seekSeconds - snappedKeyframe);
541
+ if (snappedKeyframe > 0) {
542
+ args.push("-ss", ffmpegSeconds(snappedKeyframe + seekLandingOffsetFor({ audioOnly, transcodeVideo, keyframes }, snappedKeyframe)));
543
+ }
544
+ args.push("-i", inputUrl);
545
+ // The coarse landing, not the exact target: the residual below is discarded
546
+ // from the OUTPUT and so takes the same slice off every stream. Seeking the
547
+ // soundtrack to the exact target as well would take that slice twice and
548
+ // leave the sound running ahead of the picture by it.
549
+ pushAudioInput(snappedKeyframe);
550
+ // An output-side trim, and ONLY where the output is labelled from zero.
551
+ //
552
+ // Beside `-copyts` it does the opposite of what it says. Measured
553
+ // 2026-09-06 on a file with a 5 s keyframe interval: a run landed at 15 s
554
+ // and asked to trim to the cut at 20 s produced its first file starting at
555
+ // 10 s, and a run asked to trim to 17 s produced cuts at 13.129, 18.129,
556
+ // 23.129 — the whole grid moved back by the trim itself. The muxer's cut
557
+ // times are absolute under `-copyts` while the trim is not, so every cut of
558
+ // the run inherits the difference and the numbering, fixed at spawn, is
559
+ // wrong by however many cuts that is.
560
+ //
561
+ // On the keyframe grid the run begins AT a cut, so there is nothing to
562
+ // trim: the carried keyframe above makes this exactly zero rather than
563
+ // nearly zero.
564
+ if (residualSeconds > 0 && !keyframeGrid) {
565
+ args.push("-ss", ffmpegSeconds(residualSeconds));
566
+ }
567
+ } else {
568
+ if (seekSeconds > 0) {
569
+ // No keyframe map (probe failed/timed out) — fall back to the previous
570
+ // behaviour: trust the container's own accurate seek.
571
+ args.push("-accurate_seek", "-ss", ffmpegSeconds(seekSeconds));
572
+ }
573
+ args.push("-i", inputUrl);
574
+ pushAudioInput(seekSeconds);
575
+ }
576
+ // Which timeline the output is labelled on. An audio rendition has no
577
+ // picture of its own to follow, so it follows the grid it was given — the
578
+ // same one the video it plays with is on. Deciding by `transcodeVideo`, as
579
+ // everything else here does, would put the audio of a re-encoded stream on
580
+ // the copy branch: `-copyts` and a shift by the container's start time,
581
+ // against a picture labelled from zero. The two would be offset by
582
+ // `sourceStartTime` for the whole file.
583
+ if (!keyframeGrid) {
584
+ // Branch A (re-encode): fixed GOP makes keyframes land exactly on the
585
+ // segment grid; relabel output onto the original timeline so segment N
586
+ // carries PTS = N × segmentDuration.
587
+ if (startSeconds > 0) {
588
+ args.push("-output_ts_offset", ffmpegSeconds(startSeconds));
589
+ }
590
+ } else {
591
+ // Branch B (video copied — only audio is transcoded): we cannot insert
592
+ // keyframes, so segments are cut at the source's own keyframes (the
593
+ // playlist boundaries were built from those keyframes). Keep the source's
594
+ // real timestamps (`-copyts`) so copied frames stay continuous across
595
+ // boundaries/seeks, and shift by -startTime so the output timeline is
596
+ // 0-based (a non-zero container start otherwise puts a hole at the very
597
+ // beginning and desyncs audio/video). Audio is transcoded on this timeline.
598
+ args.push("-copyts");
599
+ if (sourceStartTime !== 0) {
600
+ args.push("-output_ts_offset", ffmpegSeconds(-sourceStartTime));
601
+ }
602
+ }
603
+ // Where this run STOPS. Until now a run had a start and no end — neither
604
+ // `-to` nor `-t` appeared anywhere in the arguments this proxy builds — so
605
+ // every stop was a kill from outside, and two runs on one output could only
606
+ // be kept apart by giving each its own directory. With an end they cannot
607
+ // reach each other's numbers at all, and a run that finishes its stretch
608
+ // exits by itself instead of having to be noticed and killed.
609
+ //
610
+ // WHICH argument states it is a property of the branch, and it is measured
611
+ // rather than reasoned (2026-09-04, `research/encoder-layer-2026-09-04.md`
612
+ // §11): `-t` is a duration on the output's own clock, and `-to` a point on
613
+ // the input's. The copy branch runs with `-copyts`, where the input's clock
614
+ // IS the source's, so `-to` takes the absolute time; the re-encode branch
615
+ // has no `-copyts` and takes the duration. Swapping them is not a near
616
+ // miss — on the copy branch `-t` produced one segment where five were
617
+ // wanted, because the time it names is already past when the run starts.
618
+ const runEnd = Number.isInteger(endIndex) ? endIndex : -1;
619
+ const publishedGrid = publishedGridFor(timeline);
620
+ if (runEnd >= safeIndex && Array.isArray(publishedGrid) && publishedGrid[runEnd + 1] > 0) {
621
+ const endsAt = publishedGrid[runEnd + 1];
622
+ if (transcodeVideo) {
623
+ args.push("-t", ffmpegSeconds(Math.max(0.1, endsAt - publishedGrid[safeIndex])));
624
+ } else {
625
+ args.push("-to", ffmpegSeconds(endsAt));
626
+ }
627
+ }
628
+ if (audioOnly === true) {
629
+ // An audio RENDITION: one track, no picture. Published as its own
630
+ // `#EXT-X-MEDIA` and shared by every video variant, so the track is
631
+ // encoded once for the file instead of once per rung, and changing it is
632
+ // the player switching rendition rather than this proxy rebuilding the
633
+ // session. Cut on the same grid as the video it accompanies, which is
634
+ // what lets the two be played together.
635
+ // `0:` because a rendition's only input IS the file its track lives in —
636
+ // the picture's own file, or the one beside it that carries this dub.
637
+ args.push("-vn", "-map", `0:a:${audioSourceTrackIndex}?`, ...audioCodecArgs);
638
+ } else if (servesAudioSeparately) {
639
+ // The other half of the same arrangement: the picture alone, because its
640
+ // audio is published as a rendition and would otherwise play twice.
641
+ args.push("-an", "-map", "0:v:0?", ...videoCodecArgs);
642
+ } else {
643
+ args.push(
644
+ "-map",
645
+ "0:v:0?",
646
+ "-map",
647
+ // The audio track the viewer chose: input 1 when their choice is a
648
+ // soundtrack shipped as its own file, input 0 when it is one of the
649
+ // picture's own. Type-relative within that input, which is what
650
+ // `audioSourceTrackIndex` holds — the number the browser sent is flat
651
+ // across both files and was resolved when the session was made.
652
+ `${audioInputUrl ? 1 : 0}:a:${audioSourceTrackIndex}?`,
653
+ ...videoCodecArgs,
654
+ ...audioCodecArgs
655
+ );
656
+ }
657
+
658
+ // Where the cuts come from. On the copy path they are the source's own
659
+ // keyframes, and until now they were only ever GUESSED: ffmpeg got a target
660
+ // duration and chose its own cut points, while the playlist was built from
661
+ // the container index — two independent calculations with nothing tying
662
+ // them together but the hope that they agree. They do not. The index is a
663
+ // navigation table and is not obliged to list every keyframe; for a field
664
+ // file it held 1902 while ffmpeg found roughly twice as many and cut twice
665
+ // as often. Segment #876 then meant 1:26:50 to the player and about minute
666
+ // 58 to ffmpeg, which is why a seek landed nowhere near where it was aimed
667
+ // and the reported duration drifted.
668
+ //
669
+ // So stop guessing and say it: the `segment` muxer takes the list of times
670
+ // outright. Passing the very boundaries the playlist was built from makes
671
+ // the two agree by construction. Only cut points already known to be real
672
+ // keyframes are sent, so ffmpeg never has to move one forward.
673
+ //
674
+ // The list is built above, before the encoder args, because a re-encoded
675
+ // variant of a copied stream needs the same times twice over: once as the
676
+ // cuts, once as the keyframes to force at them.
677
+ if (cutTimes && cutTimes.length > 0) {
678
+ args.push(
679
+ "-f",
680
+ "segment",
681
+ // Times are measured from the START OF THIS RUN, not from the start of
682
+ // the file — verified: starting at 12 s and asking for a cut at 18 s
683
+ // produced one at 29.4 s. `segmentCutTimesFrom` rebases them.
684
+ "-segment_times",
685
+ cutTimes.join(","),
686
+ // A cut lands on the first keyframe at or after its time, so a boundary
687
+ // recorded a hair late would skip to the next one and double the
688
+ // segment. The tolerance absorbs that rounding.
689
+ "-segment_time_delta",
690
+ "0.05",
691
+ "-segment_start_number",
692
+ String(safeIndex),
693
+ // THE ENCODER SAYS WHEN A PIECE IS FINISHED, on a channel of its own.
694
+ //
695
+ // Measured on the addon host 2026-09-05: a name appears in this list when
696
+ // the file is CLOSED, not when it is created — at the third sample
697
+ // `seg-000.mp4` was on disk and absent from the list, and it appeared at
698
+ // the fourth, in the same moment `seg-001.mp4` came into being. So a name
699
+ // here is the writer's own statement that the piece is whole.
700
+ //
701
+ // Without it, a finished file is indistinguishable from one still being
702
+ // written, and the only proof available was the existence of the NEXT
703
+ // one — which never comes for the last piece of every run.
704
+ "-segment_list",
705
+ "pipe:3",
706
+ "-segment_list_flags",
707
+ "+live",
708
+ ...explicitTimes,
709
+ // UNDER A WORKING NAME, not the one it is served as. A piece under its
710
+ // served name is complete by construction then, whoever else is writing
711
+ // into the same directory — and the name arrives on the channel above the
712
+ // instant ffmpeg closes it, which is what turns it into the served one.
713
+ //
714
+ // The other branch needs none of this: the HLS muxer writes through a
715
+ // temporary name of its own (`+temp_file`), so its files appear under
716
+ // their final name whole.
717
+ //
718
+ // TAGGED WITH THE STRETCH IT WAS GIVEN, which names the run without any
719
+ // counter to keep: intervals never overlap, so two live runs of one output
720
+ // begin at different numbers by construction. That is what makes clearing
721
+ // up after a dead run a well-formed question — its unfinished pieces are
722
+ // the ones carrying its own tag — where before it was answered by taking
723
+ // the highest SERVED name inside its stretch and judging the bytes.
724
+ segmentFormat.makingFileNameTemplate(String(safeIndex))
725
+ );
726
+ } else {
727
+ args.push(
728
+ "-f",
729
+ "hls",
730
+ "-hls_time",
731
+ String(segmentDurationSec),
732
+ "-hls_list_size",
733
+ "0",
734
+ "-hls_flags",
735
+ "independent_segments+temp_file",
736
+ // Container selection + segment naming, from the active format module.
737
+ ...segmentFormat.muxerArgs(),
738
+ "-start_number",
739
+ String(safeIndex),
740
+ // ffmpeg writes its own playlist here; we ignore it and serve the
741
+ // synthetic VOD playlist instead (see getFileStream).
742
+ PLAYLIST_FILE_NAME
743
+ );
744
+ }
745
+ return { args, safeIndex, startSeconds, cutTimes };
746
+
747
+ }