@uploadfilm/sdk 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +509 -2
  3. package/dist/ai/embed.d.ts +58 -0
  4. package/dist/ai/fake.d.ts +41 -0
  5. package/dist/ai/http.d.ts +17 -0
  6. package/dist/ai/index.d.ts +27 -0
  7. package/dist/ai/languages.d.ts +2 -0
  8. package/dist/ai/ollama.d.ts +23 -0
  9. package/dist/ai/openaiCompatible.d.ts +54 -0
  10. package/dist/ai/passages.d.ts +28 -0
  11. package/dist/ai/transcribe.d.ts +84 -0
  12. package/dist/ai/types.d.ts +141 -0
  13. package/dist/ai/usage.d.ts +66 -0
  14. package/dist/ai/whisperJson.d.ts +15 -0
  15. package/dist/ai/workersAi.d.ts +56 -0
  16. package/dist/ai.js +1 -0
  17. package/dist/ai.mjs +1 -0
  18. package/dist/cards/files.d.ts +91 -0
  19. package/dist/cards/index.d.ts +15 -0
  20. package/dist/cards/layouts.d.ts +121 -0
  21. package/dist/cards/probe.d.ts +69 -0
  22. package/dist/cards/proxies.d.ts +70 -0
  23. package/dist/cards/select.d.ts +68 -0
  24. package/dist/cards/volumes.d.ts +170 -0
  25. package/dist/cards/xml.d.ts +80 -0
  26. package/dist/cards.js +1 -0
  27. package/dist/cards.mjs +1 -0
  28. package/dist/copy/checksums.d.ts +66 -0
  29. package/dist/copy/engine.d.ts +24 -0
  30. package/dist/copy/errors.d.ts +109 -0
  31. package/dist/copy/index.d.ts +15 -0
  32. package/dist/copy/steps.d.ts +28 -0
  33. package/dist/copy/sweep.d.ts +24 -0
  34. package/dist/copy/types.d.ts +336 -0
  35. package/dist/embed/player.js +2 -0
  36. package/dist/host/adapters.d.ts +107 -0
  37. package/dist/host/detect.d.ts +68 -0
  38. package/dist/host/encoders.d.ts +34 -0
  39. package/dist/host/environment.d.ts +53 -0
  40. package/dist/host/index.d.ts +20 -0
  41. package/dist/host/linux.d.ts +94 -0
  42. package/dist/host/mac.d.ts +47 -0
  43. package/dist/host/network.d.ts +54 -0
  44. package/dist/host/node.d.ts +13 -0
  45. package/dist/host/policy.d.ts +101 -0
  46. package/dist/host/tuner.d.ts +48 -0
  47. package/dist/host/types.d.ts +118 -0
  48. package/dist/host-node.js +1 -0
  49. package/dist/host-node.mjs +1 -0
  50. package/dist/host.js +1 -0
  51. package/dist/host.mjs +1 -0
  52. package/dist/images/index.d.ts +11 -0
  53. package/dist/images/settings.d.ts +43 -0
  54. package/dist/images/types.d.ts +83 -0
  55. package/dist/images/url.d.ts +65 -0
  56. package/dist/index.d.ts +16 -0
  57. package/dist/index.js +1 -0
  58. package/dist/index.mjs +1 -0
  59. package/dist/player/adapters/hlsjs.d.ts +28 -0
  60. package/dist/player/bandwidthMemory.d.ts +28 -0
  61. package/dist/player/captions/index.d.ts +55 -0
  62. package/dist/player/embed/code.d.ts +195 -0
  63. package/dist/player/embed/constants.d.ts +4 -0
  64. package/dist/player/embed/controller.d.ts +62 -0
  65. package/dist/player/embed/element.d.ts +32 -0
  66. package/dist/player/embed/frame.d.ts +65 -0
  67. package/dist/player/embed/global.d.ts +14 -0
  68. package/dist/player/embed/index.d.ts +15 -0
  69. package/dist/player/embed/loader.d.ts +1 -0
  70. package/dist/player/embed/oembed.d.ts +74 -0
  71. package/dist/player/embed/protocol.d.ts +64 -0
  72. package/dist/player/engine.d.ts +215 -0
  73. package/dist/player/index.d.ts +18 -0
  74. package/dist/player/start.d.ts +62 -0
  75. package/dist/player/store.d.ts +10 -0
  76. package/dist/player/storyboard/index.d.ts +161 -0
  77. package/dist/player/stream.d.ts +206 -0
  78. package/dist/player/tracks.d.ts +41 -0
  79. package/dist/player.js +1 -0
  80. package/dist/player.mjs +1 -0
  81. package/dist/proxies/args.d.ts +19 -0
  82. package/dist/proxies/budget.d.ts +57 -0
  83. package/dist/proxies/camera.d.ts +104 -0
  84. package/dist/proxies/formats.d.ts +36 -0
  85. package/dist/proxies/index.d.ts +18 -0
  86. package/dist/proxies/jobs.d.ts +98 -0
  87. package/dist/proxies/node.d.ts +32 -0
  88. package/dist/proxies/plan.d.ts +89 -0
  89. package/dist/proxies/probe.d.ts +39 -0
  90. package/dist/proxies/render.d.ts +104 -0
  91. package/dist/proxies/safety.d.ts +45 -0
  92. package/dist/proxies/types.d.ts +83 -0
  93. package/dist/proxies/upload.d.ts +14 -0
  94. package/dist/react/BigPlay.d.ts +26 -0
  95. package/dist/react/Player.d.ts +58 -0
  96. package/dist/react/embed/index.d.ts +45 -0
  97. package/dist/react/examples/CustomControls.d.ts +8 -0
  98. package/dist/react/images/Image.d.ts +16 -0
  99. package/dist/react/images/index.d.ts +4 -0
  100. package/dist/react/images/useImage.d.ts +47 -0
  101. package/dist/react/index.d.ts +17 -0
  102. package/dist/react/storyboard/index.d.ts +58 -0
  103. package/dist/react/theme.d.ts +37 -0
  104. package/dist/react/useAdapterErrors.d.ts +8 -0
  105. package/dist/react/usePlaybackStart.d.ts +18 -0
  106. package/dist/react/usePlayer.d.ts +28 -0
  107. package/dist/react/useReducedMotion.d.ts +2 -0
  108. package/dist/react/useTimedTrack.d.ts +22 -0
  109. package/dist/react.js +1 -0
  110. package/dist/react.mjs +1 -0
  111. package/dist/server/auth/apiKeys.d.ts +30 -0
  112. package/dist/server/auth/cache.d.ts +21 -0
  113. package/dist/server/auth/deviceTokens.d.ts +38 -0
  114. package/dist/server/auth/envelope.d.ts +15 -0
  115. package/dist/server/auth/grants.d.ts +76 -0
  116. package/dist/server/auth/index.d.ts +19 -0
  117. package/dist/server/auth/jwt.d.ts +65 -0
  118. package/dist/server/auth/keyring.d.ts +31 -0
  119. package/dist/server/auth/kv.d.ts +12 -0
  120. package/dist/server/auth/repository.d.ts +45 -0
  121. package/dist/server/auth/revocations.d.ts +21 -0
  122. package/dist/server/auth/scope.d.ts +4 -0
  123. package/dist/server/auth/serviceAuth.d.ts +24 -0
  124. package/dist/server/auth/signedUrls.d.ts +28 -0
  125. package/dist/server/embed/index.d.ts +36 -0
  126. package/dist/server/embed/lifecycle.d.ts +53 -0
  127. package/dist/server/embed/playerScript.d.ts +4 -0
  128. package/dist/server/images/index.d.ts +15 -0
  129. package/dist/server/images/job.d.ts +80 -0
  130. package/dist/server/images/layout.d.ts +24 -0
  131. package/dist/server/images/params.d.ts +90 -0
  132. package/dist/server/images/route.d.ts +101 -0
  133. package/dist/server/images/sign.d.ts +55 -0
  134. package/dist/server/index.d.ts +42 -0
  135. package/dist/server/media/client.d.ts +66 -0
  136. package/dist/server/media/frameCheck.d.ts +62 -0
  137. package/dist/server/media/index.d.ts +10 -0
  138. package/dist/server/media/protocol.d.ts +318 -0
  139. package/dist/server/media/storyboard.d.ts +144 -0
  140. package/dist/server/media/transcript.d.ts +143 -0
  141. package/dist/server/pipeline/index.d.ts +12 -0
  142. package/dist/server/pipeline/match.d.ts +10 -0
  143. package/dist/server/pipeline/pipeline.d.ts +182 -0
  144. package/dist/server/pipeline/queues.d.ts +108 -0
  145. package/dist/server/pipeline/sources.d.ts +54 -0
  146. package/dist/server/r2/client.d.ts +150 -0
  147. package/dist/server/r2/crc32.d.ts +24 -0
  148. package/dist/server/r2/encoding.d.ts +35 -0
  149. package/dist/server/r2/errors.d.ts +9 -0
  150. package/dist/server/r2/integrity.d.ts +25 -0
  151. package/dist/server/r2/parts.d.ts +46 -0
  152. package/dist/server/r2/sign.d.ts +47 -0
  153. package/dist/server/redis/index.d.ts +39 -0
  154. package/dist/server/state/memory.d.ts +18 -0
  155. package/dist/server/state/r2.d.ts +73 -0
  156. package/dist/server/state/types.d.ts +57 -0
  157. package/dist/server/storage/chunked.d.ts +21 -0
  158. package/dist/server/storage/index.d.ts +12 -0
  159. package/dist/server/storage/r2.d.ts +96 -0
  160. package/dist/server/storage/s3.d.ts +93 -0
  161. package/dist/server/testing/fake-r2.d.ts +390 -0
  162. package/dist/server/testing/index.d.ts +10 -0
  163. package/dist/server/testing/kv-contract.d.ts +14 -0
  164. package/dist/server/transfer/handler.d.ts +167 -0
  165. package/dist/server/transfer/keys.d.ts +45 -0
  166. package/dist/server/transfer/protocol.d.ts +141 -0
  167. package/dist/server/zip/client.d.ts +32 -0
  168. package/dist/server/zip/generations.d.ts +83 -0
  169. package/dist/server/zip/index.d.ts +12 -0
  170. package/dist/server/zip/planner.d.ts +94 -0
  171. package/dist/server/zip/sign.d.ts +22 -0
  172. package/dist/server-media.js +1 -0
  173. package/dist/server-media.mjs +1 -0
  174. package/dist/server-redis.js +1 -0
  175. package/dist/server-redis.mjs +1 -0
  176. package/dist/server-testing.js +1 -0
  177. package/dist/server-testing.mjs +1 -0
  178. package/dist/server.js +1 -0
  179. package/dist/server.mjs +1 -0
  180. package/dist/storage/browserFile.d.ts +19 -0
  181. package/dist/storage/index.d.ts +12 -0
  182. package/dist/storage/local.d.ts +9 -0
  183. package/dist/storage/presigned.d.ts +22 -0
  184. package/dist/sync/ffmpeg.d.ts +44 -0
  185. package/dist/sync/index.d.ts +17 -0
  186. package/dist/sync/match.d.ts +28 -0
  187. package/dist/sync/probe.d.ts +30 -0
  188. package/dist/sync/timecode.d.ts +50 -0
  189. package/dist/sync/transcript.d.ts +35 -0
  190. package/dist/sync/types.d.ts +172 -0
  191. package/dist/sync/waveform.d.ts +89 -0
  192. package/dist/sync.js +1 -0
  193. package/dist/sync.mjs +1 -0
  194. package/dist/transfer/claims.d.ts +41 -0
  195. package/dist/transfer/client.d.ts +256 -0
  196. package/dist/transfer/download.d.ts +192 -0
  197. package/dist/transfer/engine.d.ts +382 -0
  198. package/dist/transfer/errors.d.ts +50 -0
  199. package/dist/transfer/index.d.ts +39 -0
  200. package/dist/transfer/integrity.d.ts +93 -0
  201. package/dist/transfer/limits.d.ts +34 -0
  202. package/dist/transfer/node.d.ts +63 -0
  203. package/dist/transfer/pool.d.ts +83 -0
  204. package/dist/transfer/protocol.d.ts +167 -0
  205. package/dist/transfer/signer.d.ts +29 -0
  206. package/dist/transfer/storage.d.ts +110 -0
  207. package/dist/transfer/timing.d.ts +132 -0
  208. package/dist/transfer/transports/electronNet.d.ts +62 -0
  209. package/dist/transfer/transports/fetch.d.ts +15 -0
  210. package/dist/transfer/transports/retryAfter.d.ts +12 -0
  211. package/dist/transfer/transports/undici.d.ts +72 -0
  212. package/dist/transfer/tuner.d.ts +56 -0
  213. package/dist/zip/index.d.ts +13 -0
  214. package/dist/zip/layout.d.ts +129 -0
  215. package/dist/zip/names.d.ts +34 -0
  216. package/dist/zip/plan.d.ts +115 -0
  217. package/dist/zip/sw.d.ts +7 -0
  218. package/package.json +265 -4
@@ -0,0 +1,30 @@
1
+ import type { SyncAudio, SyncClip } from "./types";
2
+ export interface ProbeOptions {
3
+ ffprobe?: string;
4
+ timeoutMs?: number;
5
+ signal?: AbortSignal;
6
+ }
7
+ /** A camera clip's sync facts. `id` defaults to the path. */
8
+ export declare function probeClip(path: string, o?: ProbeOptions & {
9
+ id?: string;
10
+ }): Promise<SyncClip>;
11
+ /** A recorder file's sync facts: duration, bext time reference and dates, iXML. `id` defaults to the path. */
12
+ export declare function probeAudio(path: string, o?: ProbeOptions & {
13
+ id?: string;
14
+ }): Promise<SyncAudio>;
15
+ export interface Ixml {
16
+ samplesSinceMidnight?: number;
17
+ timestampRate?: number;
18
+ timecodeRate?: string;
19
+ timecodeFlag?: "DF" | "NDF";
20
+ slate: {
21
+ project?: string;
22
+ scene?: string;
23
+ take?: string;
24
+ tape?: string;
25
+ };
26
+ }
27
+ /** The iXML chunk of a RIFF/RF64/BW64 WAV, or undefined (not a WAV, no chunk, unreadable). */
28
+ export declare function readIxml(path: string): Promise<Ixml | undefined>;
29
+ /** The few iXML fields sync uses (see the file header). */
30
+ export declare function parseIxml(xml: string): Ixml;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * SMPTE timecode labels → seconds since midnight, and the day/overlap checks the timecode method needs.
3
+ *
4
+ * Hand-rolled on purpose (a few lines of arithmetic; the SDK takes no dependency for it): a label counts frames at the
5
+ * NOMINAL rate (24, 25, 30), and those frames run at the ACTUAL rate (24000/1001 for 23.976). Drop-frame (29.97, 59.94)
6
+ * skips labels 00 and 01 (02 and 03 at 59.94) in every minute but each tenth. A BWF TimeReference is samples since
7
+ * midnight at the recorder's sample clock, so seconds = samples / rate on both sides compare directly.
8
+ */
9
+ /**
10
+ * "25", "29.97", "30000/1001", 23.976 → frames per second. Decimal NTSC rates (23.976, 29.97, 59.94, …) snap to the
11
+ * exact n × 1000/1001 they stand for: 23.976 taken literally is 2 frames off in an hour.
12
+ */
13
+ export declare function parseFps(fps: number | string | undefined): number | undefined;
14
+ /** Nominal (label) rate of an actual rate: 23.976 → 24, 29.97 → 30, 25 → 25. */
15
+ export declare const nominalFps: (fps: number) => number;
16
+ /**
17
+ * Frame count of a label. `dropFrame` defaults to the label's separator (";" or "." before the frames) and is only
18
+ * honoured at 29.97 and 59.94. Invalid labels (fields out of range, dropped labels) → undefined.
19
+ */
20
+ export declare function timecodeToFrames(tc: string, fps: number, dropFrame?: boolean): number | undefined;
21
+ /** Seconds since midnight (real time) of a label at `fps`. */
22
+ export declare function timecodeToSeconds(tc: string, fps: number | string, dropFrame?: boolean): number | undefined;
23
+ /**
24
+ * Real seconds in one timecode day at `fps`: the frames of 24 h of labels at the actual rate. NTSC non-drop:
25
+ * 86,486.4 s at 23.976; drop-frame 29.97: 2,589,408 frames = 86,399.9136 s.
26
+ */
27
+ export declare function timecodeDayS(fps: number | undefined, dropFrame?: boolean): number;
28
+ /** a − b on a clock of `day` seconds, in (−day/2, day/2]: a take that crosses midnight still lines up. */
29
+ export declare function clockDiffS(a: number, b: number, day?: number): number;
30
+ /**
31
+ * The time-of-day offset `d` placed on the right day: of d + k·day, the one closest to `wallS` (the offset the two
32
+ * start dates say). Without dates, d itself.
33
+ */
34
+ export declare function placeOnDay(d: number, day: number, wallS: number | undefined): number;
35
+ /** Seconds of overlap between the clip [0, clipDur] and the audio placed at offset (audio t = offset ↔ clip t = 0). */
36
+ export declare function overlapS(offsetS: number, clipDurS: number, audioDurS: number): number;
37
+ /** ms since epoch of a start time, or undefined. Strings without a zone are read as UTC (both sides alike). */
38
+ export declare function startMs(t: string | Date | undefined): number | undefined;
39
+ /**
40
+ * Are clip and audio from the same session? true / false when both start times are known; undefined otherwise.
41
+ * "Same" = their recording spans come within `maxSkewS` of each other (camera and recorder clocks are rarely set
42
+ * together; a different day is a different session).
43
+ */
44
+ export declare function sameSession(clip: {
45
+ startedAt?: string | Date;
46
+ durationS: number;
47
+ }, audio: {
48
+ startedAt?: string | Date;
49
+ durationS: number;
50
+ }, maxSkewS: number): boolean | undefined;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Transcript method, a tiebreaker only: when the waveform (or timecode) leaves two or more offsets standing, the one
3
+ * where the clip's words are heard in the recorder at the same moments wins. It never matches on its own: repeated
4
+ * takes say the same lines, so words alone can't tell take 3 from take 4.
5
+ *
6
+ * Transcribers come from @uploadfilm/sdk/ai (any adapter: Workers AI, OpenAI-compatible, the local ai-plugins ones);
7
+ * transcriberFor() cuts the range to a 16 kHz WAV with FFmpeg and shifts the word times back to file time.
8
+ */
9
+ import type { Transcriber } from "../ai/types";
10
+ import type { SyncTranscribe, SyncWord } from "./types";
11
+ /** Lower case, letters and digits only (Unicode), so "Okay," and "okay" agree. */
12
+ export declare const normWord: (w: string) => string;
13
+ /**
14
+ * Fraction of the clip's meaningful words (within the overlap) heard in the audio at `offsetS` ± `toleranceS`, each
15
+ * audio word used once. Clip words are in clip time, audio words in audio time.
16
+ */
17
+ export declare function transcriptAgreement(clipWords: SyncWord[], audioWords: SyncWord[], offsetS: number, o?: {
18
+ toleranceS?: number;
19
+ audioDurS?: number;
20
+ }): {
21
+ agreement: number;
22
+ matched: number;
23
+ considered: number;
24
+ };
25
+ /** An offset the transcripts alone suggest (mode of word-time differences), for diagnostics. */
26
+ export declare function transcriptOffset(clipWords: SyncWord[], audioWords: SyncWord[], binS?: number): number | undefined;
27
+ /**
28
+ * A SyncTranscribe from any @uploadfilm/sdk/ai Transcriber: FFmpeg cuts [startS, startS + durationS] to a 16 kHz mono
29
+ * WAV (keep ranges under the transcriber's limit: a few minutes), and word times come back in file time.
30
+ */
31
+ export declare function transcriberFor(t: Transcriber, o?: {
32
+ ffmpeg?: string;
33
+ language?: string;
34
+ tmpDir?: string;
35
+ }): SyncTranscribe;
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Dual-system audio sync: which external recorder file belongs to which camera clip, and where.
3
+ *
4
+ * Convention used everywhere in this folder: an audio match's `offsetS` is the position in the AUDIO FILE (seconds
5
+ * from its first sample) that plays at the clip's t = 0 (its first video frame). With drift,
6
+ *
7
+ * audioTime = offsetS + clipTime × (1 + driftPpm / 1e6)
8
+ *
9
+ * A negative offset means the recorder started after the camera (the clip's first seconds have no recorder audio).
10
+ */
11
+ /** A camera clip, as the card reader (or probeSyncMedia) describes it. */
12
+ export interface SyncClip {
13
+ id: string;
14
+ /** A media file with the camera's scratch audio (the original, or a proxy with the same audio). Absent: timecode only. */
15
+ path?: string;
16
+ /** Length in seconds. */
17
+ durationS: number;
18
+ /** Start timecode "HH:MM:SS:FF" (";" or "." before the frames for drop-frame), and its frame rate. */
19
+ timecode?: string;
20
+ /** Frames per second: 25, 29.97, "30000/1001", … Needed with `timecode`. */
21
+ fps?: number | string;
22
+ /** When recording started (camera clock), ISO 8601 or Date. Used only to keep other days' files apart. */
23
+ startedAt?: string | Date;
24
+ /**
25
+ * Seconds from the clip's first video frame to its first audio sample (ffprobe audio start_time − video start_time).
26
+ * probeSyncMedia fills it; default 0.
27
+ */
28
+ audioLeadS?: number;
29
+ /** False when the clip has no audio stream (no waveform or transcript method). probeSyncMedia fills it. */
30
+ hasAudio?: boolean;
31
+ }
32
+ /** An external recorder file (WAV/BWF/RF64/MP3/…). */
33
+ export interface SyncAudio {
34
+ id: string;
35
+ path: string;
36
+ durationS: number;
37
+ /** Seconds since midnight of its first sample, from BWF bext TimeReference or iXML (samples / sample rate). */
38
+ timeOfDayS?: number;
39
+ /** Timecode rate the recorder ran at (iXML TIMECODE_RATE), for display only: sync uses seconds. */
40
+ timecodeRate?: string;
41
+ /** When recording started (recorder clock): bext OriginationDate + OriginationTime, or the caller's. */
42
+ startedAt?: string | Date;
43
+ /** iXML project/scene/take/tape, when the recorder wrote them (shown to people, never used to match). */
44
+ slate?: {
45
+ project?: string;
46
+ scene?: string;
47
+ take?: string;
48
+ tape?: string;
49
+ };
50
+ }
51
+ /** One word with its time (seconds from the start of the file it came from). */
52
+ export interface SyncWord {
53
+ word: string;
54
+ start: number;
55
+ end: number;
56
+ }
57
+ /**
58
+ * Transcribe part of a file. Word times: for an audio file, seconds from its first sample; for a clip, CLIP time
59
+ * (seconds from its first video frame, i.e. including `audioLeadS`), the same timeline as offsets. `range` is in the
60
+ * same units. `transcriberFor()` adapts any @uploadfilm/sdk/ai Transcriber; products may cache results.
61
+ */
62
+ export type SyncTranscribe = (file: {
63
+ kind: "clip";
64
+ clip: SyncClip;
65
+ } | {
66
+ kind: "audio";
67
+ audio: SyncAudio;
68
+ }, range: {
69
+ startS: number;
70
+ durationS: number;
71
+ }, signal?: AbortSignal) => Promise<SyncWord[]>;
72
+ export type SyncMethod = "timecode" | "waveform" | "timecode+waveform" | "waveform+transcript";
73
+ /** One recorder file in a take. */
74
+ export interface TakeAudio {
75
+ audioId: string;
76
+ /** See the file header: position in the audio file at the clip's t = 0. */
77
+ offsetS: number;
78
+ /** Recorder clock minus camera clock, parts per million (waveform with 2+ agreeing windows), else 0. */
79
+ driftPpm: number;
80
+ /** 0..1. Only matches at or above `minConfidence` are returned. */
81
+ confidence: number;
82
+ method: SyncMethod;
83
+ /**
84
+ * True when no sound confirmed this match: method "timecode" on a clip with no usable scratch audio (no audio
85
+ * stream, or silent). Timecode and day agree, but another unit's recorder jammed to the same clock would too: a
86
+ * product UI should ask a person to confirm these. False for every waveform-based method.
87
+ */
88
+ unverifiedBySound: boolean;
89
+ /** Seconds of the clip covered by this audio (the rest has no recorder audio). */
90
+ overlapS: number;
91
+ evidence: MatchEvidence;
92
+ }
93
+ export interface MatchEvidence {
94
+ /** Offset timecode implies, when both sides had it. */
95
+ timecodeOffsetS?: number;
96
+ /** Both start dates known and within maxClockSkewS: true; known and apart: never matched; unknown: undefined. */
97
+ sameDay?: boolean;
98
+ /** Windows of the clip's scratch audio that agree on this offset, out of the usable (non-silent, non-tonal) ones. */
99
+ windowsAgreeing?: number;
100
+ windowsUsable?: number;
101
+ /** Median normalized correlation of the agreeing windows (0..1). */
102
+ correlation?: number;
103
+ /** Median peak height in standard deviations of the correlation over all lags. */
104
+ peakZ?: number;
105
+ /** Score of the next-best offset in the same file over the best's (0..1; near 1 = ambiguous). */
106
+ rivalRatio?: number;
107
+ /** Fraction of the clip's transcript words heard in the audio at this offset. */
108
+ transcriptAgreement?: number;
109
+ /** Waveform beat timecode: how far timecode was off (seconds). */
110
+ timecodeDisagreesS?: number;
111
+ /** For a timecode-only match: why sound couldn't confirm it. */
112
+ scratchAudio?: "none" | "silent";
113
+ /** What decided between competing offsets. */
114
+ tiebreak?: "timecode" | "transcript";
115
+ /** Where the waveform looked: only around the timecode offset (it agreed there, so timecode decides any look-alike
116
+ * elsewhere in the file), or the whole file. */
117
+ searched?: "around-timecode" | "whole-file";
118
+ }
119
+ /** A camera clip with the recorder files that belong to it. */
120
+ export interface Take {
121
+ clipId: string;
122
+ audio: TakeAudio[];
123
+ }
124
+ export type UnmatchedReason = "no-audio-candidates" | "different-day" | "no-overlap" | "no-scratch-audio" | "silent-scratch-audio" | "no-waveform-match" | "ambiguous" | "low-confidence" | "timecode-day-unverified" | "error";
125
+ /** A clip/audio pairing that was considered and refused, with why (for people and for tests). */
126
+ export interface RejectedPair {
127
+ clipId: string;
128
+ audioId: string;
129
+ reason: UnmatchedReason;
130
+ detail?: string;
131
+ /** Best confidence reached, when there was a candidate. */
132
+ confidence?: number;
133
+ }
134
+ export interface SyncResult {
135
+ takes: Take[];
136
+ /** Clips with no recorder audio at all, with the most telling reason. */
137
+ unmatchedClips: {
138
+ clipId: string;
139
+ reason: UnmatchedReason;
140
+ detail?: string;
141
+ }[];
142
+ /** Recorder files no clip matched. */
143
+ unmatchedAudio: {
144
+ audioId: string;
145
+ reason: UnmatchedReason;
146
+ }[];
147
+ rejected: RejectedPair[];
148
+ }
149
+ export interface SyncOptions {
150
+ ffmpeg?: string;
151
+ /** Working sample rate for correlation (default 4000 Hz: 0.25 ms per sample, voice band kept). */
152
+ sampleRate?: number;
153
+ /** Length of each scratch-audio window correlated against the recorder (default 8 s). */
154
+ windowS?: number;
155
+ /** Most windows per clip (default 6). */
156
+ maxWindows?: number;
157
+ /** Below this confidence a pairing stays unmatched (default 0.7). */
158
+ minConfidence?: number;
159
+ /** Two start times further apart than this are different days: never matched (default 12 h). */
160
+ maxClockSkewS?: number;
161
+ /** Most recorder drift allowed between windows of one clip (default 200 ppm). */
162
+ maxDriftPpm?: number;
163
+ /** Least overlap between clip and audio, seconds (default 1). */
164
+ minOverlapS?: number;
165
+ /** Optional transcript tiebreaker (see transcriberFor). */
166
+ transcribe?: SyncTranscribe;
167
+ /** Parallel ffmpeg processes (default 2, max 4: this runs on machines that also upload and render). */
168
+ concurrency?: number;
169
+ signal?: AbortSignal;
170
+ /** Directory for temporary files (default os.tmpdir()); everything written is deleted before returning. */
171
+ tmpDir?: string;
172
+ }
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Waveform method: pick windows of the camera's scratch audio, find each window in the recorder track (ffmpeg.ts
3
+ * correlate), and keep an offset only when several windows agree on it (or one window is unmistakable).
4
+ *
5
+ * Why windows rather than the whole clip: the recorder may cover only part of the clip (started late, stopped early),
6
+ * and windows that agree are what rules chance out: an unrelated track puts each window's best peak at a random lag, so
7
+ * two windows landing within a frame of the same offset by chance is ~1e-4 per pair on a 10-minute file.
8
+ * Their spread also measures drift (the slope of offset against clip time).
9
+ */
10
+ import type { WindowPeaks } from "./ffmpeg";
11
+ export interface ClipWindow {
12
+ /** Start, seconds of clip time (from its first video frame). */
13
+ clipS: number;
14
+ samples: Float32Array;
15
+ }
16
+ export interface WindowPlan {
17
+ windows: ClipWindow[];
18
+ /** Windows considered before dropping silent ones. */
19
+ considered: number;
20
+ }
21
+ /** RMS below this (after the voice-band filter) is silence: −50 dBFS. */
22
+ export declare const SILENCE_RMS: number;
23
+ /**
24
+ * Up to `max` windows of `windowS` spread over the clip; silent ones are dropped. `leadS` = clip time of sample 0.
25
+ */
26
+ export declare function planWindows(samples: Float32Array, sr: number, windowS: number, max: number, leadS?: number): WindowPlan;
27
+ export interface WaveformTuning {
28
+ /** A peak this many standard deviations above the lag noise counts as a hit (default 10)… */
29
+ zHit: number;
30
+ /** …with at least this normalized correlation (default 0.12; unrelated speech peaks at ≤ 0.1, a 2 s reverberant
31
+ * room at 0 dB SNR still gives 0.14–0.25). */
32
+ nccHit: number;
33
+ /** Correlation that counts as full evidence for one window (default 0.3). */
34
+ nccFull: number;
35
+ /** A window alone counts only if its next peak is at most this fraction of its best (default 0.6). */
36
+ loneRatio: number;
37
+ /** Offsets within this many seconds agree (default 0.02 s), plus maxDriftPpm × their distance. */
38
+ agreeS: number;
39
+ maxDriftPpm: number;
40
+ /** Drift is measured only over agreeing windows at least this far apart (default 60 s); over a shorter span the
41
+ * offset is their median and the zero-drift error is at most maxDriftPpm × span (12 ms). */
42
+ minDriftSpanS: number;
43
+ /** A rival offset scoring at least this fraction of the best makes the pairing ambiguous (default 0.5). */
44
+ ambiguousRatio: number;
45
+ }
46
+ export declare const DEFAULT_TUNING: WaveformTuning;
47
+ export interface OffsetCandidate {
48
+ offsetS: number;
49
+ driftPpm: number;
50
+ confidence: number;
51
+ agreeing: number;
52
+ /** Usable windows the audio covers at this offset. */
53
+ usable: number;
54
+ correlation: number;
55
+ peakZ: number;
56
+ score: number;
57
+ /** Polarity: most agreeing windows peak negative (the recorder inverted relative to the camera, or a room that
58
+ * flips the dominant path: informational, never used to match). */
59
+ inverted: boolean;
60
+ /** The agreeing windows: index into the plan's windows, and the audio time (s) its peak puts the window's start at. */
61
+ members: {
62
+ window: number;
63
+ audioS: number;
64
+ }[];
65
+ }
66
+ export interface WaveformOutcome {
67
+ /** Strongest first. Empty: no window found anything. */
68
+ candidates: OffsetCandidate[];
69
+ /** Second-best candidate's score over the best's. */
70
+ rivalRatio: number;
71
+ }
72
+ /**
73
+ * Turn per-window peaks into offset candidates. `regionS` = audio time of recorder sample 0 of the correlated range.
74
+ */
75
+ export declare function offsetsFromPeaks(windows: ClipWindow[], peaks: WindowPeaks[], sr: number, ctx: {
76
+ clipDurS: number;
77
+ audioDurS: number;
78
+ regionS?: number;
79
+ }, t?: WaveformTuning): WaveformOutcome;
80
+ /**
81
+ * How periodic the sound is around an alignment: the strongest normalized correlation at lags 40–100 ms either side
82
+ * of `audioS`, over the correlation at `audioS` (±2 samples). Near 1: the clip fits a repetition one or two frames
83
+ * away as well as here (a motif repeating faster than the 0.1 s peak spacing), so the offset can't be trusted.
84
+ * Direct dot products (≈ 2 × 240 lags × the window at 4 kHz), only for an alignment about to be accepted.
85
+ */
86
+ export declare function nearRivalRatio(rec: Float32Array, w: Float32Array, audioS: number, sr: number, o?: {
87
+ guardS?: number;
88
+ spanS?: number;
89
+ }): number;
package/dist/sync.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var t=require("node:fs/promises"),e=require("node:os"),n=require("node:path"),o=require("node:child_process");const r=t=>`highpass=f=100,lowpass=f=${Math.floor(.45*t)}`;function a(t,e,n){return new Promise((r,a)=>{if(e.signal?.aborted)return a(new Error("aborted"));const i=o.spawn(e.ffmpeg??"ffmpeg",["-hide_banner","-nostdin","-v","error",...t],{stdio:["ignore","pipe","pipe"]});let s="";const c=()=>i.kill("SIGKILL");e.signal?.addEventListener("abort",c,{once:!0}),i.stdout.on("data",t=>{try{n(t)}catch(t){i.kill("SIGKILL"),a(t)}}),i.stderr.on("data",t=>{s.length<4e3&&(s+=t.toString())}),i.on("error",t=>{e.signal?.removeEventListener("abort",c),a(t)}),i.on("close",t=>{e.signal?.removeEventListener("abort",c),e.signal?.aborted?a(new Error("aborted")):0===t?r():a(new Error(`ffmpeg exited ${t}: ${s.trim().slice(0,1e3)}`))})})}class i{parts=[];carry=Buffer.alloc(0);length=0;push(t){const e=this.carry.length?Buffer.concat([this.carry,t]):t,n=Math.floor(e.length/4),o=new Float32Array(n);for(let t=0;t<n;t++)o[t]=e.readFloatLE(4*t);this.carry=Buffer.from(e.subarray(4*n)),this.parts.push(o),this.length+=n}done(){const t=new Float32Array(this.length);let e=0;for(const n of this.parts)t.set(n,e),e+=n.length;return this.parts=[],t}}async function s(t,e,n={}){const o=new i,s=!1===n.filter?`aresample=${e}`:`aresample=${e},${n.filter??r(e)}`;return await a([...n.startS?["-ss",String(n.startS)]:[],"-i",t,...void 0!==n.durationS?["-t",String(n.durationS)]:[],"-map","0:a:0","-ac","1","-af",s,"-f","f32le","-"],n,t=>o.push(t)),o.done()}async function c(t,e,n,o={}){const r=[];return await a([...e>0?["-ss",String(e)]:[],"-i",t,"-t",String(n),"-map","0:a:0","-ac","1","-ar","16000","-c:a","pcm_s16le","-f","wav","-"],o,t=>r.push(t)),new Uint8Array(Buffer.concat(r))}async function d(e,n){await t.writeFile(e,Buffer.from(n.buffer,n.byteOffset,n.byteLength))}async function f(o,r,i,s={}){const c=r.length;if(!c)return[];const d=r[0].length;if(d<2||r.some(t=>t.length!==d))throw new Error("correlate: windows must have one length");if(o.length<d)return r.map(()=>({peaks:[],mean:0,std:0}));const f=s.topK??6,l=Math.max(1,s.separation??Math.round(.1*i)),u=r.map(t=>{let e=0,n=0;for(let o=0;o<d;o++)e+=Math.abs(t[o]),n+=t[o]*t[o];return{l1:e||1,l2:Math.sqrt(n)}}),m=new Float32Array(d*c);for(let t=0;t<c;t++){const e=r[t],n=u[t].l1;for(let o=0;o<d;o++)m[o*c+t]=e[d-1-o]/n}const p=await t.mkdtemp(n.join(s.tmpDir??e.tmpdir(),"uf-sync-"));try{const e=s.recPath??n.join(p,"rec.f32"),h=n.join(p,"ir.f32");s.recPath||await t.writeFile(e,Buffer.from(o.buffer,o.byteOffset,o.byteLength)),await t.writeFile(h,Buffer.from(m.buffer,m.byteOffset,m.byteLength));const g=Math.min(60,Math.ceil(d/i*1e3+1)/1e3),w=1===c?"mono":`${c}c`,S=1===c?"anull":`pan=${w}|${Array.from({length:c},(t,e)=>`c${e}=c0`).join("|")}`,M=Math.max(1,Math.floor(l/4)),b=Math.ceil(o.length/M)+1,v=Array.from({length:c},()=>new Float32Array(b)),y=Array.from({length:c},()=>new Float64Array(b).fill(-1)),x=Array.from({length:c},()=>new Float32Array(b)),k=Array.from({length:c},()=>new Float32Array(b)),A=Array.from({length:c},()=>new Float32Array(b)),E=new Float64Array(c),_=new Float64Array(c),$=new Float64Array(c);let F=0,I=0,D=0,R=0,N=Buffer.alloc(0);const T=(t,e,n)=>{if(n<d-1)return;const o=n-d+1,r=u[e].l2*Math.sqrt(I),a=r>1e-12?t*u[e].l1/r:0,i=Math.abs(a);_[e]+=i,$[e]+=i*i;const s=Math.floor(o/M),c=v[e],f=Math.floor((o-1)/M);o>0&&y[e][f]===o-1&&(A[e][f]=i),(i>c[s]||y[e][s]<0)&&(c[s]=i,y[e][s]=o,x[e][s]=a,k[e][s]=E[e],A[e][s]=0),E[e]=i};if(await a(["-f","f32le","-ar",String(i),"-ac","1","-i",e,"-f","f32le","-ar",String(i),"-ac",String(c),...c>1?["-ch_layout",w]:[],"-i",h,"-filter_complex",`[0]${S}[x];[x][1]afir=gtype=none:irfmt=input:irlink=0:precision=double:maxir=${g}[o]`,"-map","[o]","-f","f32le","-"],s,t=>{const e=N.length?Buffer.concat([N,t]):t,n=Math.floor(e.length/4);for(let t=0;t<n&&!(D>=o.length);t++){if(0===R){if(I+=o[D]*o[D],D>=d&&(I-=o[D-d]*o[D-d]),D%65536==0&&D>=d-1){I=0;for(let t=D-d+1;t<=D;t++)I+=o[t]*o[t]}I<0&&(I=0)}T(e.readFloatLE(4*t),R,D),++R===c&&(R=0,D>=d-1&&F++,D++)}N=Buffer.from(e.subarray(4*n))}),D<o.length)throw new Error(`ffmpeg afir returned ${D} of ${o.length} samples`);return r.map((t,e)=>{const n=F?_[e]/F:0,o=F?Math.sqrt(Math.max(0,$[e]/F-n*n)):0,r=[];for(let t=0;t<b;t++)y[e][t]>=0&&r.push(t);r.sort((t,n)=>v[e][n]-v[e][t]);const a=[];for(const t of r){if(a.length>=f)break;const r=y[e][t];if(a.some(t=>Math.abs(t.lag-r)<l))continue;const i=v[e][t],s=k[e][t],c=A[e][t];if(s>i||c>i)continue;const d=s-2*i+c,u=d<0?Math.max(-.5,Math.min(.5,.5*(s-c)/d)):0;a.push({lag:r+u,ncc:x[e][t],z:o>0?(i-n)/o:0})}return{peaks:a,mean:n,std:o}})}finally{await t.rm(p,{recursive:!0,force:!0})}}function l(t){if(void 0===t)return;let e;if("number"==typeof t)e=t;else{const n=t.trim(),o=/^(\d+(?:\.\d+)?)\s*\/\s*(\d+(?:\.\d+)?)$/.exec(n);e=o?Number(o[1])/Number(o[2]):n?Number(n):NaN}if(!Number.isFinite(e)||e<=0||e>=1e3)return;const n=1e3*Math.round(1.001*e)/1001;return Math.abs(e-Math.round(e))>.001&&Math.abs(e-n)<.005?n:e}const u=t=>Math.round(t),m=t=>Math.abs(t-Math.round(t))>.001;function p(t,e,n){const o=/^(\d{1,2})[:;.](\d{2})[:;.](\d{2})([:;.])(\d{2,3})$/.exec(t.trim());if(!o)return;const[r,a,i,s]=[Number(o[1]),Number(o[2]),Number(o[3]),Number(o[5])],c=u(e);if(r>23||a>59||i>59||s>=c)return;const d=60*r+a;let f=(60*d+i)*c+s;if((n??(";"===o[4]||"."===o[4]))&&m(e)&&(30===c||60===c)){const t=c/15;if(a%10!=0&&0===i&&s<t)return;f-=t*(d-Math.floor(d/10))}return f}function h(t,e,n){const o=l(e);if(void 0===o)return;const r=p(t,o,n);return void 0===r?void 0:r/o}const g=86400;function w(t,e=!1){if(void 0===t||!m(t))return g;const n=u(t);return(g*n-(e&&(30===n||60===n)?n/15*1296:0))/t}function S(t,e,n=g){let o=(t-e)%n;return o>n/2&&(o-=n),o<=-n/2&&(o+=n),o}function M(t,e,n){return void 0===n?t:t+Math.round((n-t)/e)*e}function b(t,e,n){const o=Math.max(0,-t),r=Math.min(e,n-t);return Math.max(0,r-o)}function v(t){if(void 0===t)return;if(t instanceof Date)return Number.isFinite(t.getTime())?t.getTime():void 0;const e=t.trim();if(!e)return;const n=e.replace(" ","T").replace(/([+-]\d{2})(\d{2})$/,"$1:$2"),o=/([zZ]|[+-]\d{2}:\d{2})$/.test(n)?n:`${n}Z`,r=Date.parse(o);return Number.isFinite(r)?r:void 0}function y(t,e,n){const o=v(t.startedAt),r=v(e.startedAt);if(void 0===o||void 0===r)return;const a=o+1e3*t.durationS,i=r+1e3*e.durationS;return Math.max(0,r-a,o-i)/1e3<=n}const x=t=>t.toLowerCase().normalize("NFKC").replace(/[^\p{L}\p{N}]+/gu,""),k=t=>t.map(t=>({...t,n:x(t.word)})).filter(t=>t.n.length>=3&&Number.isFinite(t.start));function A(t,e,n,o={}){const r=o.toleranceS??.35,a=k(t).filter(t=>{const e=t.start+n;return e>=0&&(void 0===o.audioDurS||e<=o.audioDurS)}),i=k(e).sort((t,e)=>t.start-e.start||(t.n<e.n?-1:t.n>e.n?1:0));a.sort((t,e)=>t.start-e.start||(t.n<e.n?-1:t.n>e.n?1:0));const s=new Set;let c=0;for(const t of a){const e=t.start+n;for(let n=0;n<i.length;n++){const o=i[n];if(!(o.start<e-r)){if(o.start>e+r)break;if(o.n===t.n&&!s.has(n)){s.add(n),c++;break}}}}return{agreement:a.length?c/a.length:0,matched:c,considered:a.length}}const E=10**-2.5;function _(t,e,n,o,r=0){const a=t.length/e,i=a<2*n?Math.max(3,Math.min(n,a/2)):n,s=Math.min(Math.round(i*e),t.length);if(s<2*e)return{windows:[],considered:0};const c=Math.max(1,Math.min(o,Math.floor(t.length/s))),d=t.length-s,f=[];for(let n=0;n<c;n++){const o=1===c?Math.floor(d/2):Math.round(d*n/(c-1)),a=t.slice(o,o+s);let i=0;for(let t=0;t<s;t++)i+=a[t]*a[t];Math.sqrt(i/s)<E||f.push({clipS:o/e+r,samples:a})}return{windows:f,considered:c}}const $={zHit:10,nccHit:.12,nccFull:.3,loneRatio:.6,agreeS:.02,maxDriftPpm:200,minDriftSpanS:60,ambiguousRatio:.5},F=t=>{const e=[...t].sort((t,e)=>t-e),n=e.length>>1;return e.length%2?e[n]:(e[n-1]+e[n])/2};function I(t,e,n,o,r=$){const a=[];e.forEach((e,i)=>{for(const s of e.peaks){if(s.z<r.zHit||Math.abs(s.ncc)<r.nccHit)continue;const e=s.lag/n+(o.regionS??0);a.push({m:i,clipS:t[i].clipS,offsetS:e-t[i].clipS,p:s})}}),a.sort((t,e)=>e.p.z-t.p.z);const i=[];for(const t of a){const e=i.find(e=>!e.some(e=>e.m===t.m)&&Math.abs(t.offsetS-e[0].offsetS)<=r.agreeS+r.maxDriftPpm/1e6*Math.abs(t.clipS-e[0].clipS));e?e.push(t):i.push([t])}const s=[];for(const a of i){let i=F(a.map(t=>t.offsetS)),c=0;const d=a.length>=2?Math.max(...a.map(t=>t.clipS))-Math.min(...a.map(t=>t.clipS)):0,f=a.every(t=>e[t.m].peaks.every(e=>e===t.p||Math.abs(e.lag-t.p.lag)<=.04*n||Math.abs(e.ncc)<.8*Math.abs(t.p.ncc)));if(a.length>=2&&d>=r.minDriftSpanS&&f){const t=a.reduce((t,e)=>t+e.clipS,0)/a.length,e=a.reduce((t,e)=>t+e.offsetS,0)/a.length;let n=0,o=0;for(const r of a)n+=(r.clipS-t)**2,o+=(r.clipS-t)*(r.offsetS-e);c=n>0?o/n:0,i=e-c*t}const l=t.filter(t=>{const e=i+t.clipS;return e>=0&&e+t.samples.length/n<=o.audioDurS}).length,u=a.map(t=>t.p.z),m=F(u),p=F(a.map(t=>Math.abs(t.p.ncc))),h=1===a.length;let g=1;for(const t of a){g*=1-.75*Math.max(0,Math.min(1,(Math.abs(t.p.ncc)-r.nccHit)/(r.nccFull-r.nccHit)))}let w=(1-g)*(.6+.4*(l>0?Math.min(1,a.length/l):0));if(h){const t=e[a[0].m].peaks.find(t=>t!==a[0].p);(t?Math.abs(t.ncc)/Math.abs(a[0].p.ncc):0)>r.loneRatio&&(w=Math.min(w,.4))}s.push({offsetS:i,driftPpm:1e6*c,confidence:w,agreeing:a.length,usable:l,correlation:p,peakZ:m,score:u.reduce((t,e)=>t+e,0),inverted:2*a.filter(t=>t.p.ncc<0).length>a.length,members:a.map(t=>({window:t.m,audioS:t.p.lag/n+(o.regionS??0)}))})}s.sort((t,e)=>e.score-t.score);return{candidates:s,rivalRatio:s.length>=2?s[1].score/s[0].score:0}}function D(t,e,n,o,r={}){const a=e.length;let i=0;for(let t=0;t<a;t++)i+=e[t]*e[t];const s=n=>{if(n<0||n+a>t.length)return 0;let o=0,r=0;for(let i=0;i<a;i++){const a=t[n+i];o+=e[i]*a,r+=a*a}const s=Math.sqrt(i*r);return s>1e-12?Math.abs(o/s):0},c=Math.round(n*o);let d=0;for(let t=-2;t<=2;t++)d=Math.max(d,s(c+t));if(d<=0)return 0;const f=Math.max(1,Math.round((r.guardS??.04)*o)),l=Math.round((r.spanS??.1)*o);let u=0;for(let t=f;t<=l;t++)u=Math.max(u,s(c-t),s(c+t));return u/d}const R={sampleRate:4e3,windowS:8,maxWindows:6,minConfidence:.7,maxClockSkewS:43200,maxDriftPpm:200,minOverlapS:1,concurrency:2};async function N(t,e,n){let o=0;await Promise.all(Array.from({length:Math.max(1,Math.min(e,t.length))},async()=>{for(;o<t.length;)await n(t[o++])}))}const T=(t,e)=>1-(1-t)*(1-e);function L(t,e,n,o,r,a){return O((t??e).candidates,n,o,r,a)}function O(t,e,n,o,r){if(void 0===e)return;const a=t.filter(t=>Math.abs(t.offsetS-e)<=n).sort((t,e)=>e.score-t.score)[0];if(!a)return;return t.some(t=>Math.abs(t.offsetS-a.offsetS)>.002&&Math.abs(t.offsetS-e)<=o&&t.score>=r*a.score)?void 0:a}function P(t,e){return new Promise((n,r)=>{o.execFile(e.ffprobe??"ffprobe",["-v","error","-show_format","-show_streams","-of","json",t],{timeout:e.timeoutMs??3e4,maxBuffer:16777216,signal:e.signal},(e,o)=>{if(e)return r(new Error(`ffprobe failed on ${t}: ${e.message}`));try{n(JSON.parse(o))}catch(e){r(new Error(`ffprobe gave no JSON for ${t}`,{cause:e}))}})})}const C=t=>{const e=void 0===t?NaN:Number(t);return Number.isFinite(e)?e:void 0},B=t=>Object.fromEntries(Object.entries(t??{}).map(([t,e])=>[t.toLowerCase(),e]));const W=1048576;async function j(e){const n=await t.open(e,"r");try{const t=Buffer.alloc(12);if((await n.read(t,0,12,0)).bytesRead<12)return;const e=t.toString("latin1",0,4);if(!["RIFF","RF64","BW64"].includes(e)||"WAVE"!==t.toString("latin1",8,12))return;const o=(await n.stat()).size;let r,a=12;const i=Buffer.alloc(8);for(let t=0;t<1e4&&a+8<=o;t++){if((await n.read(i,0,8,a)).bytesRead<8)return;const t=i.toString("latin1",0,4);let e=i.readUInt32LE(4);if("ds64"===t&&e>=16){const t=Buffer.alloc(16);await n.read(t,0,16,a+8),r=Number(t.readBigUInt64LE(8))}if("data"===t&&4294967295===e&&void 0!==r&&(e=r),"iXML"===t){if(e>W||a+8+e>o)return;const t=Buffer.alloc(e);return await n.read(t,0,e,a+8),z(t.toString("utf8"))}a+=8+e+e%2}return}finally{await n.close()}}const q=(t,e)=>{const n=new RegExp(`<${e}>([^<]{0,200})</${e}>`).exec(t),o=n?.[1]?.trim().replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"').replace(/&apos;/g,"'").replace(/&amp;/g,"&");return o||void 0},H=t=>void 0!==t&&/^\d{1,10}$/.test(t)?Number(t):void 0;function z(t){const e=new RegExp("<TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_HI[\\s/>]").test(t),n=H(q(t,"TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_HI")),o=H(q(t,"TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_LO")),r=H(q(t,"TIMESTAMP_SAMPLE_RATE")),a=q(t,"TIMECODE_FLAG"),i={};for(const[e,n]of[["project","PROJECT"],["scene","SCENE"],["take","TAKE"],["tape","TAPE"]]){const o=q(t,n);o&&(i[e]=o)}const s={slate:i};void 0!==o&&o<=4294967295&&(!e||void 0!==n)&&(n??0)<=2097151&&(s.samplesSinceMidnight=(n??0)*2**32+o),r&&r>0&&(s.timestampRate=r);const c=q(t,"TIMECODE_RATE");return c&&/^\d+(\.\d+)?(\/\d+)?$/.test(c)&&(s.timecodeRate=c),"DF"!==a&&"NDF"!==a||(s.timecodeFlag=a),s}exports.DEFAULT_TUNING=$,exports.SDK_ENTRY="sync",exports.SILENCE_RMS=E,exports.clockDiffS=S,exports.correlate=f,exports.decodeMono=s,exports.extractWav=c,exports.nearRivalRatio=D,exports.normWord=x,exports.offsetsFromPeaks=I,exports.overlapS=b,exports.parseFps=l,exports.parseIxml=z,exports.pickWeak=L,exports.pickWeakByTimecode=O,exports.placeOnDay=M,exports.planWindows=_,exports.preFilter=r,exports.probeAudio=async function(t,e={}){const n=await P(t,e),o=(n.streams??[]).find(t=>"audio"===t.codec_type),r=B(n.format?.tags),a=C(o?.sample_rate),i={id:e.id??t,path:t,durationS:C(n.format?.duration)??C(o?.duration)??0},s=await j(t).catch(()=>{}),c=C(r.time_reference);void 0!==c&&a?i.timeOfDayS=c/a:void 0!==s?.samplesSinceMidnight&&(s.timestampRate??a)&&(i.timeOfDayS=s.samplesSinceMidnight/(s.timestampRate??a));const d=/^(\d{4})[-:/](\d{2})[-:/](\d{2})$/.exec(r.date??""),f=/^(\d{2})[-:.](\d{2})[-:.](\d{2})$/.exec(r.creation_time??"");return d?i.startedAt=`${d[1]}-${d[2]}-${d[3]}T${f?`${f[1]}:${f[2]}:${f[3]}`:"00:00:00"}`:r.creation_time&&!f&&(i.startedAt=r.creation_time),s?.timecodeRate&&(i.timecodeRate=s.timecodeRate+("DF"===s.timecodeFlag?" DF":"")),s?.slate&&Object.keys(s.slate).length&&(i.slate=s.slate),i},exports.probeClip=async function(t,e={}){const n=await P(t,e),o=n.streams??[],r=o.find(t=>"video"===t.codec_type),a=o.find(t=>"audio"===t.codec_type),i=B(n.format?.tags),s=o.map(t=>B(t.tags).timecode).find(Boolean)??i.timecode,c=r?.r_frame_rate&&"0/0"!==r.r_frame_rate?r.r_frame_rate:r?.avg_frame_rate,d=C(n.format?.duration)??C(r?.duration)??C(a?.duration)??0,f=C(r?.start_time)??0,l=C(a?.start_time)??0,u=i["com.apple.quicktime.creationdate"]??i.creation_time??B(r?.tags).creation_time;return{id:e.id??t,path:t,durationS:d,hasAudio:!!a,audioLeadS:a&&r?l-f:0,...s?{timecode:s}:{},...c&&"0/0"!==c?{fps:c}:{},...u?{startedAt:u}:{}}},exports.readIxml=j,exports.sameSession=y,exports.startMs=v,exports.syncAudio=async function(o,r,a={}){const i={...R,...a},c=i.sampleRate,u=a.tuning??{},m={...$,maxDriftPpm:i.maxDriftPpm,...u},p=u.timecodeToleranceS??.5,g=u.timecodeSearchS??5,x=u.timecodeOnlyConfidence??.85,k=Math.max(1,Math.min(4,i.concurrency)),E={...i.ffmpeg?{ffmpeg:i.ffmpeg}:{},...i.signal?{signal:i.signal}:{},...i.tmpDir?{tmpDir:i.tmpDir}:{}},F=new Set;for(const t of[...o.map(t=>`clip:${t.id}`),...r.map(t=>`audio:${t.id}`)]){if(F.has(t))throw new Error(`syncAudio: duplicate id ${t}`);F.add(t)}const O=new Map;await N(o,k,async t=>{if(!t.path||!1===t.hasAudio)return void O.set(t.id,{clip:t,windows:[],noWindows:"no-scratch-audio"});let e;try{e=await s(t.path,c,E)}catch(e){if(i.signal?.aborted)throw e;const n=e instanceof Error?e.message:String(e),o=/matches no streams/i.test(n);return void O.set(t.id,{clip:t,windows:[],noWindows:o?"no-scratch-audio":"error",...o?{}:{error:n.slice(0,300)}})}const n=_(e,c,i.windowS,i.maxWindows,t.audioLeadS??0);O.set(t.id,{clip:t,windows:n.windows,...n.windows.length?{}:{noWindows:n.considered?"silent-scratch-audio":"no-scratch-audio"}})});const P=[],C=[];for(const a of r){if(i.signal?.aborted)throw new Error("aborted");let r,f;if(o.some(t=>O.get(t.id).windows.length>0))try{r=await s(a.path,c,E)}catch(t){if(i.signal?.aborted)throw t;f=t instanceof Error?t.message:String(t)}const l=a.durationS||(r?r.length/c:0),u=r?await t.mkdtemp(n.join(i.tmpDir??e.tmpdir(),"uf-sync-rec-")):void 0,m=u?n.join(u,"rec.f32"):void 0;try{r&&m&&await d(m,r),await N(o,k,async t=>{const e=O.get(t.id);let n;try{n=await B(e,a,l,r,f,m)}catch(t){if(i.signal?.aborted)throw t;n={ok:!1,reason:"error",detail:t instanceof Error?t.message:String(t)}}n.ok?P.push({clipId:t.id,audio:n.audio}):C.push({clipId:t.id,audioId:a.id,reason:n.reason,...n.detail?{detail:n.detail}:{},...void 0!==n.confidence?{confidence:n.confidence}:{}})})}finally{u&&await t.rm(u,{recursive:!0,force:!0})}}async function B(t,e,n,o,r,a){const s=t.clip,d=y(s,{...e,durationS:n},i.maxClockSkewS);if(!1===d)return{ok:!1,reason:"different-day"};const k=void 0===d?{}:{sameDay:d};if("error"===t.noWindows)return{ok:!1,reason:"error",detail:`clip audio: ${t.error??"unreadable"}`};const _=v(s.startedAt),$=v(e.startedAt),F=void 0!==_&&void 0!==$?(_-$)/1e3:void 0;let R;const N=l(s.fps),O=s.timecode&&N?h(s.timecode,N):void 0;if(void 0!==O&&void 0!==e.timeOfDayS){const t=/[;.]\d{2,3}$/.test(s.timecode.trim()),o=M(S(O,e.timeOfDayS,w(N,t)),w(N,t),F);k.timecodeOffsetS=Math.round(1e6*o)/1e6;(void 0===F||Math.abs(o-F)<=i.maxClockSkewS)&&b(o,s.durationS,n)>=i.minOverlapS&&(R=o)}const P=(t,o,r,a)=>{if(void 0!==F&&Math.abs(t-F)>i.maxClockSkewS)return{ok:!1,reason:"different-day",detail:"the offset doesn't fit the recording dates",confidence:r};const c=b(t,s.durationS,n);return c<i.minOverlapS?{ok:!1,reason:"no-overlap",confidence:r}:r<i.minConfidence?{ok:!1,reason:"low-confidence",confidence:r}:{ok:!0,audio:{audioId:e.id,offsetS:Math.round(1e6*t)/1e6,driftPpm:Math.round(10*o)/10,confidence:Math.round(1e3*r)/1e3,method:a,unverifiedBySound:"timecode"===a,overlapS:Math.round(1e3*c)/1e3,evidence:k}}};if(!t.windows.length)return void 0===R?{ok:!1,reason:void 0!==k.timecodeOffsetS?"no-overlap":t.noWindows??"no-scratch-audio"}:!0!==d?{ok:!1,reason:"timecode-day-unverified",detail:"no scratch audio to confirm the timecode"}:(k.scratchAudio="silent-scratch-audio"===t.noWindows?"silent":"none",P(R,0,x,"timecode"));if(!o)return{ok:!1,reason:"error",detail:r??"recorder audio could not be decoded"};const C=t=>void 0!==R&&Math.abs(t.offsetS-R)<=p,B=o,W=(e,n,o)=>{const r=(t=>{const e=[...t].sort((t,e)=>t-e),n=e.length>>1;return 0===e.length?0:e.length%2?e[n]:(e[n-1]+e[n])/2})(e.members.map(e=>D(B,t.windows[e.window].samples,e.audioS,c)));return r>=(u.nearRivalMax??.85)?{ok:!1,reason:"ambiguous",detail:`the sound repeats within 0.1 s of this offset (periodic, ${r.toFixed(2)})`,confidence:n}:P(e.offsetS,e.driftPpm,n,o)},j=async(e,r)=>{const i=Math.max(0,Math.floor(e*c)),d=Math.min(o.length,Math.ceil(r*c));if(d-i<t.windows[0].samples.length)return{candidates:[],rivalRatio:0};const l=0===i&&d===o.length,u=await f(o.subarray(i,d),t.windows.map(t=>t.samples),c,{...E,...l&&a?{recPath:a}:{}});return I(t.windows,u,c,{clipDurS:s.durationS,audioDurS:n,regionS:i/c},m)};let q,H;if(void 0!==R){H=await j(R-g,R+s.durationS+g);const t=H.candidates[0];t&&t.confidence>=i.minConfidence&&H.rivalRatio<m.ambiguousRatio&&C(t)&&(q=H,k.searched="around-timecode")}q||(q=await j(0,n),k.searched="whole-file");const z=q.candidates[0],G=t=>{k.windowsAgreeing=t.agreeing,k.windowsUsable=t.usable,k.correlation=Math.round(1e3*t.correlation)/1e3,k.peakZ=Math.round(10*t.peakZ)/10,k.rivalRatio=Math.round(1e3*q.rivalRatio)/1e3};if(!z&&!H?.candidates.length)return{ok:!1,reason:"no-waveform-match",...void 0!==R?{detail:"timecode overlaps, but the clip's sound is not in this file"}:{}};if(z&&z.confidence>=i.minConfidence){if(q.rivalRatio<m.ambiguousRatio)return G(z),C(z)?W(z,T(z.confidence,.5),"timecode+waveform"):(void 0!==R&&(k.timecodeDisagreesS=Math.round(1e3*(z.offsetS-R))/1e3),W(z,z.confidence,"waveform"));const o=q.candidates.filter(t=>t.score>=m.ambiguousRatio*z.score),r=o.filter(C);if(1===r.length&&r[0].confidence>=i.minConfidence){const t=r[0];return G(t),k.tiebreak="timecode",W(t,T(t.confidence,.5)*(1-.2*q.rivalRatio),"timecode+waveform")}if(i.transcribe){const r=await(t=>t.words??=i.transcribe?i.transcribe({kind:"clip",clip:t.clip},{startS:0,durationS:Math.min(t.clip.durationS,u.transcriptMaxS??300)},i.signal):Promise.resolve([]))(t),a=[];for(const t of o){const o=Math.max(0,t.offsetS-1),c=Math.min(s.durationS,u.transcriptMaxS??300)+2,d=await i.transcribe({kind:"audio",audio:e},{startS:o,durationS:c},i.signal);a.push({c:t,agreement:A(r,d,t.offsetS,{audioDurS:n}).agreement})}a.sort((t,e)=>e.agreement-t.agreement);const[c,...d]=a;if(c&&c.c.confidence>=i.minConfidence&&c.agreement>=(u.transcriptMin??.5)&&d.every(t=>c.agreement-t.agreement>=(u.transcriptMargin??.25)))return G(c.c),k.tiebreak="transcript",k.transcriptAgreement=Math.round(1e3*c.agreement)/1e3,W(c.c,c.c.confidence*(1-.2*q.rivalRatio),"waveform+transcript")}return G(z),{ok:!1,reason:"ambiguous",detail:`${o.length} offsets fit: ${o.map(t=>t.offsetS.toFixed(3)).join(", ")} s`,confidence:z.confidence}}const K=L(H,q,R,p,g,m.ambiguousRatio);return K?(G(K),W(K,T(K.confidence,d?.75:.6),"timecode+waveform")):z?(G(z),{ok:!1,reason:"low-confidence",confidence:z.confidence}):{ok:!1,reason:"no-waveform-match"}}const W=new Map;for(const t of P)(W.get(t.clipId)??W.set(t.clipId,[]).get(t.clipId)).push(t.audio);const j=new Map(r.map((t,e)=>[t.id,e])),q=o.filter(t=>W.has(t.id)).map(t=>({clipId:t.id,audio:W.get(t.id).sort((t,e)=>e.confidence-t.confidence||j.get(t.audioId)-j.get(e.audioId))})),H=["error","ambiguous","low-confidence","timecode-day-unverified","silent-scratch-audio","no-scratch-audio","no-overlap","no-waveform-match","different-day","no-audio-candidates"],z=t=>t.length?t.reduce((t,e)=>H.indexOf(e.reason)<H.indexOf(t.reason)?e:t):void 0,G=o.filter(t=>!W.has(t.id)).map(t=>{const e=z(C.filter(e=>e.clipId===t.id));return e?{clipId:t.id,reason:e.reason,...e.detail?{detail:e.detail}:{}}:{clipId:t.id,reason:"no-audio-candidates"}}),K=new Set(P.map(t=>t.audio.audioId));return{takes:q,unmatchedClips:G,unmatchedAudio:r.filter(t=>!K.has(t.id)).map(t=>({audioId:t.id,reason:z(C.filter(e=>e.audioId===t.id))?.reason??"no-audio-candidates"})),rejected:C}},exports.timecodeDayS=w,exports.timecodeToFrames=p,exports.timecodeToSeconds=h,exports.transcriberFor=function(t,e={}){return async(n,o,r)=>{const a="clip"===n.kind?n.clip.path:n.audio.path;if(!a)return[];const i="clip"===n.kind?n.clip.audioLeadS??0:0,s=Math.max(0,i<0?o.startS-i:o.startS),d=i<0?s+i:s+Math.max(0,i-s),f=await c(a,s,o.durationS,{...e.ffmpeg?{ffmpeg:e.ffmpeg}:{},...r?{signal:r}:{}}),l=await t.transcribe({bytes:f,mimeType:"audio/wav",name:"sync.wav",seconds:o.durationS},{...e.language?{language:e.language}:{},...r?{signal:r}:{}}),u=[];for(const t of l.segments)if(t.words?.length)for(const e of t.words)u.push({word:e.word,start:e.start+d,end:e.end+d});else{const e=t.text.split(/\s+/).filter(Boolean),n=e.length?(t.end-t.start)/e.length:0;e.forEach((e,o)=>u.push({word:e,start:t.start+o*n+d,end:t.start+(o+1)*n+d}))}return u}},exports.transcriptAgreement=A,exports.transcriptOffset=function(t,e,n=.1){const o=k(t),r=k(e),a=new Map;for(const t of o)for(const e of r){if(e.n!==t.n)continue;const o=e.start-t.start,r=Math.round(o/n);(a.get(r)??a.set(r,[]).get(r)).push(o)}let i,s=0;for(const[t,e]of a){const n=e.length+(a.get(t-1)?.length??0)+(a.get(t+1)?.length??0);n>s&&(s=n,i=[...e,...a.get(t-1)??[],...a.get(t+1)??[]])}if(i&&!(s<3))return i.sort((t,e)=>t-e),i[i.length>>1]},exports.writeF32=d;
package/dist/sync.mjs ADDED
@@ -0,0 +1 @@
1
+ import{mkdtemp as t,writeFile as e,rm as n,open as o}from"node:fs/promises";import{tmpdir as a}from"node:os";import{join as r}from"node:path";import{spawn as i,execFile as s}from"node:child_process";const c=t=>`highpass=f=100,lowpass=f=${Math.floor(.45*t)}`;function d(t,e,n){return new Promise((o,a)=>{if(e.signal?.aborted)return a(new Error("aborted"));const r=i(e.ffmpeg??"ffmpeg",["-hide_banner","-nostdin","-v","error",...t],{stdio:["ignore","pipe","pipe"]});let s="";const c=()=>r.kill("SIGKILL");e.signal?.addEventListener("abort",c,{once:!0}),r.stdout.on("data",t=>{try{n(t)}catch(t){r.kill("SIGKILL"),a(t)}}),r.stderr.on("data",t=>{s.length<4e3&&(s+=t.toString())}),r.on("error",t=>{e.signal?.removeEventListener("abort",c),a(t)}),r.on("close",t=>{e.signal?.removeEventListener("abort",c),e.signal?.aborted?a(new Error("aborted")):0===t?o():a(new Error(`ffmpeg exited ${t}: ${s.trim().slice(0,1e3)}`))})})}class f{parts=[];carry=Buffer.alloc(0);length=0;push(t){const e=this.carry.length?Buffer.concat([this.carry,t]):t,n=Math.floor(e.length/4),o=new Float32Array(n);for(let t=0;t<n;t++)o[t]=e.readFloatLE(4*t);this.carry=Buffer.from(e.subarray(4*n)),this.parts.push(o),this.length+=n}done(){const t=new Float32Array(this.length);let e=0;for(const n of this.parts)t.set(n,e),e+=n.length;return this.parts=[],t}}async function l(t,e,n={}){const o=new f,a=!1===n.filter?`aresample=${e}`:`aresample=${e},${n.filter??c(e)}`;return await d([...n.startS?["-ss",String(n.startS)]:[],"-i",t,...void 0!==n.durationS?["-t",String(n.durationS)]:[],"-map","0:a:0","-ac","1","-af",a,"-f","f32le","-"],n,t=>o.push(t)),o.done()}async function u(t,e,n,o={}){const a=[];return await d([...e>0?["-ss",String(e)]:[],"-i",t,"-t",String(n),"-map","0:a:0","-ac","1","-ar","16000","-c:a","pcm_s16le","-f","wav","-"],o,t=>a.push(t)),new Uint8Array(Buffer.concat(a))}async function m(t,n){await e(t,Buffer.from(n.buffer,n.byteOffset,n.byteLength))}async function h(o,i,s,c={}){const f=i.length;if(!f)return[];const l=i[0].length;if(l<2||i.some(t=>t.length!==l))throw new Error("correlate: windows must have one length");if(o.length<l)return i.map(()=>({peaks:[],mean:0,std:0}));const u=c.topK??6,m=Math.max(1,c.separation??Math.round(.1*s)),h=i.map(t=>{let e=0,n=0;for(let o=0;o<l;o++)e+=Math.abs(t[o]),n+=t[o]*t[o];return{l1:e||1,l2:Math.sqrt(n)}}),p=new Float32Array(l*f);for(let t=0;t<f;t++){const e=i[t],n=h[t].l1;for(let o=0;o<l;o++)p[o*f+t]=e[l-1-o]/n}const g=await t(r(c.tmpDir??a(),"uf-sync-"));try{const t=c.recPath??r(g,"rec.f32"),n=r(g,"ir.f32");c.recPath||await e(t,Buffer.from(o.buffer,o.byteOffset,o.byteLength)),await e(n,Buffer.from(p.buffer,p.byteOffset,p.byteLength));const a=Math.min(60,Math.ceil(l/s*1e3+1)/1e3),w=1===f?"mono":`${f}c`,S=1===f?"anull":`pan=${w}|${Array.from({length:f},(t,e)=>`c${e}=c0`).join("|")}`,M=Math.max(1,Math.floor(m/4)),b=Math.ceil(o.length/M)+1,v=Array.from({length:f},()=>new Float32Array(b)),y=Array.from({length:f},()=>new Float64Array(b).fill(-1)),x=Array.from({length:f},()=>new Float32Array(b)),k=Array.from({length:f},()=>new Float32Array(b)),A=Array.from({length:f},()=>new Float32Array(b)),E=new Float64Array(f),$=new Float64Array(f),_=new Float64Array(f);let I=0,D=0,R=0,F=0,N=Buffer.alloc(0);const L=(t,e,n)=>{if(n<l-1)return;const o=n-l+1,a=h[e].l2*Math.sqrt(D),r=a>1e-12?t*h[e].l1/a:0,i=Math.abs(r);$[e]+=i,_[e]+=i*i;const s=Math.floor(o/M),c=v[e],d=Math.floor((o-1)/M);o>0&&y[e][d]===o-1&&(A[e][d]=i),(i>c[s]||y[e][s]<0)&&(c[s]=i,y[e][s]=o,x[e][s]=r,k[e][s]=E[e],A[e][s]=0),E[e]=i};if(await d(["-f","f32le","-ar",String(s),"-ac","1","-i",t,"-f","f32le","-ar",String(s),"-ac",String(f),...f>1?["-ch_layout",w]:[],"-i",n,"-filter_complex",`[0]${S}[x];[x][1]afir=gtype=none:irfmt=input:irlink=0:precision=double:maxir=${a}[o]`,"-map","[o]","-f","f32le","-"],c,t=>{const e=N.length?Buffer.concat([N,t]):t,n=Math.floor(e.length/4);for(let t=0;t<n&&!(R>=o.length);t++){if(0===F){if(D+=o[R]*o[R],R>=l&&(D-=o[R-l]*o[R-l]),R%65536==0&&R>=l-1){D=0;for(let t=R-l+1;t<=R;t++)D+=o[t]*o[t]}D<0&&(D=0)}L(e.readFloatLE(4*t),F,R),++F===f&&(F=0,R>=l-1&&I++,R++)}N=Buffer.from(e.subarray(4*n))}),R<o.length)throw new Error(`ffmpeg afir returned ${R} of ${o.length} samples`);return i.map((t,e)=>{const n=I?$[e]/I:0,o=I?Math.sqrt(Math.max(0,_[e]/I-n*n)):0,a=[];for(let t=0;t<b;t++)y[e][t]>=0&&a.push(t);a.sort((t,n)=>v[e][n]-v[e][t]);const r=[];for(const t of a){if(r.length>=u)break;const a=y[e][t];if(r.some(t=>Math.abs(t.lag-a)<m))continue;const i=v[e][t],s=k[e][t],c=A[e][t];if(s>i||c>i)continue;const d=s-2*i+c,f=d<0?Math.max(-.5,Math.min(.5,.5*(s-c)/d)):0;r.push({lag:a+f,ncc:x[e][t],z:o>0?(i-n)/o:0})}return{peaks:r,mean:n,std:o}})}finally{await n(g,{recursive:!0,force:!0})}}function p(t){if(void 0===t)return;let e;if("number"==typeof t)e=t;else{const n=t.trim(),o=/^(\d+(?:\.\d+)?)\s*\/\s*(\d+(?:\.\d+)?)$/.exec(n);e=o?Number(o[1])/Number(o[2]):n?Number(n):NaN}if(!Number.isFinite(e)||e<=0||e>=1e3)return;const n=1e3*Math.round(1.001*e)/1001;return Math.abs(e-Math.round(e))>.001&&Math.abs(e-n)<.005?n:e}const g=t=>Math.round(t),w=t=>Math.abs(t-Math.round(t))>.001;function S(t,e,n){const o=/^(\d{1,2})[:;.](\d{2})[:;.](\d{2})([:;.])(\d{2,3})$/.exec(t.trim());if(!o)return;const[a,r,i,s]=[Number(o[1]),Number(o[2]),Number(o[3]),Number(o[5])],c=g(e);if(a>23||r>59||i>59||s>=c)return;const d=60*a+r;let f=(60*d+i)*c+s;if((n??(";"===o[4]||"."===o[4]))&&w(e)&&(30===c||60===c)){const t=c/15;if(r%10!=0&&0===i&&s<t)return;f-=t*(d-Math.floor(d/10))}return f}function M(t,e,n){const o=p(e);if(void 0===o)return;const a=S(t,o,n);return void 0===a?void 0:a/o}const b=86400;function v(t,e=!1){if(void 0===t||!w(t))return b;const n=g(t);return(b*n-(e&&(30===n||60===n)?n/15*1296:0))/t}function y(t,e,n=b){let o=(t-e)%n;return o>n/2&&(o-=n),o<=-n/2&&(o+=n),o}function x(t,e,n){return void 0===n?t:t+Math.round((n-t)/e)*e}function k(t,e,n){const o=Math.max(0,-t),a=Math.min(e,n-t);return Math.max(0,a-o)}function A(t){if(void 0===t)return;if(t instanceof Date)return Number.isFinite(t.getTime())?t.getTime():void 0;const e=t.trim();if(!e)return;const n=e.replace(" ","T").replace(/([+-]\d{2})(\d{2})$/,"$1:$2"),o=/([zZ]|[+-]\d{2}:\d{2})$/.test(n)?n:`${n}Z`,a=Date.parse(o);return Number.isFinite(a)?a:void 0}function E(t,e,n){const o=A(t.startedAt),a=A(e.startedAt);if(void 0===o||void 0===a)return;const r=o+1e3*t.durationS,i=a+1e3*e.durationS;return Math.max(0,a-r,o-i)/1e3<=n}const $=t=>t.toLowerCase().normalize("NFKC").replace(/[^\p{L}\p{N}]+/gu,""),_=t=>t.map(t=>({...t,n:$(t.word)})).filter(t=>t.n.length>=3&&Number.isFinite(t.start));function I(t,e,n,o={}){const a=o.toleranceS??.35,r=_(t).filter(t=>{const e=t.start+n;return e>=0&&(void 0===o.audioDurS||e<=o.audioDurS)}),i=_(e).sort((t,e)=>t.start-e.start||(t.n<e.n?-1:t.n>e.n?1:0));r.sort((t,e)=>t.start-e.start||(t.n<e.n?-1:t.n>e.n?1:0));const s=new Set;let c=0;for(const t of r){const e=t.start+n;for(let n=0;n<i.length;n++){const o=i[n];if(!(o.start<e-a)){if(o.start>e+a)break;if(o.n===t.n&&!s.has(n)){s.add(n),c++;break}}}}return{agreement:r.length?c/r.length:0,matched:c,considered:r.length}}function D(t,e,n=.1){const o=_(t),a=_(e),r=new Map;for(const t of o)for(const e of a){if(e.n!==t.n)continue;const o=e.start-t.start,a=Math.round(o/n);(r.get(a)??r.set(a,[]).get(a)).push(o)}let i,s=0;for(const[t,e]of r){const n=e.length+(r.get(t-1)?.length??0)+(r.get(t+1)?.length??0);n>s&&(s=n,i=[...e,...r.get(t-1)??[],...r.get(t+1)??[]])}if(i&&!(s<3))return i.sort((t,e)=>t-e),i[i.length>>1]}function R(t,e={}){return async(n,o,a)=>{const r="clip"===n.kind?n.clip.path:n.audio.path;if(!r)return[];const i="clip"===n.kind?n.clip.audioLeadS??0:0,s=Math.max(0,i<0?o.startS-i:o.startS),c=i<0?s+i:s+Math.max(0,i-s),d=await u(r,s,o.durationS,{...e.ffmpeg?{ffmpeg:e.ffmpeg}:{},...a?{signal:a}:{}}),f=await t.transcribe({bytes:d,mimeType:"audio/wav",name:"sync.wav",seconds:o.durationS},{...e.language?{language:e.language}:{},...a?{signal:a}:{}}),l=[];for(const t of f.segments)if(t.words?.length)for(const e of t.words)l.push({word:e.word,start:e.start+c,end:e.end+c});else{const e=t.text.split(/\s+/).filter(Boolean),n=e.length?(t.end-t.start)/e.length:0;e.forEach((e,o)=>l.push({word:e,start:t.start+o*n+c,end:t.start+(o+1)*n+c}))}return l}}const F=10**-2.5;function N(t,e,n,o,a=0){const r=t.length/e,i=r<2*n?Math.max(3,Math.min(n,r/2)):n,s=Math.min(Math.round(i*e),t.length);if(s<2*e)return{windows:[],considered:0};const c=Math.max(1,Math.min(o,Math.floor(t.length/s))),d=t.length-s,f=[];for(let n=0;n<c;n++){const o=1===c?Math.floor(d/2):Math.round(d*n/(c-1)),r=t.slice(o,o+s);let i=0;for(let t=0;t<s;t++)i+=r[t]*r[t];Math.sqrt(i/s)<F||f.push({clipS:o/e+a,samples:r})}return{windows:f,considered:c}}const L={zHit:10,nccHit:.12,nccFull:.3,loneRatio:.6,agreeS:.02,maxDriftPpm:200,minDriftSpanS:60,ambiguousRatio:.5},P=t=>{const e=[...t].sort((t,e)=>t-e),n=e.length>>1;return e.length%2?e[n]:(e[n-1]+e[n])/2};function O(t,e,n,o,a=L){const r=[];e.forEach((e,i)=>{for(const s of e.peaks){if(s.z<a.zHit||Math.abs(s.ncc)<a.nccHit)continue;const e=s.lag/n+(o.regionS??0);r.push({m:i,clipS:t[i].clipS,offsetS:e-t[i].clipS,p:s})}}),r.sort((t,e)=>e.p.z-t.p.z);const i=[];for(const t of r){const e=i.find(e=>!e.some(e=>e.m===t.m)&&Math.abs(t.offsetS-e[0].offsetS)<=a.agreeS+a.maxDriftPpm/1e6*Math.abs(t.clipS-e[0].clipS));e?e.push(t):i.push([t])}const s=[];for(const r of i){let i=P(r.map(t=>t.offsetS)),c=0;const d=r.length>=2?Math.max(...r.map(t=>t.clipS))-Math.min(...r.map(t=>t.clipS)):0,f=r.every(t=>e[t.m].peaks.every(e=>e===t.p||Math.abs(e.lag-t.p.lag)<=.04*n||Math.abs(e.ncc)<.8*Math.abs(t.p.ncc)));if(r.length>=2&&d>=a.minDriftSpanS&&f){const t=r.reduce((t,e)=>t+e.clipS,0)/r.length,e=r.reduce((t,e)=>t+e.offsetS,0)/r.length;let n=0,o=0;for(const a of r)n+=(a.clipS-t)**2,o+=(a.clipS-t)*(a.offsetS-e);c=n>0?o/n:0,i=e-c*t}const l=t.filter(t=>{const e=i+t.clipS;return e>=0&&e+t.samples.length/n<=o.audioDurS}).length,u=r.map(t=>t.p.z),m=P(u),h=P(r.map(t=>Math.abs(t.p.ncc))),p=1===r.length;let g=1;for(const t of r){g*=1-.75*Math.max(0,Math.min(1,(Math.abs(t.p.ncc)-a.nccHit)/(a.nccFull-a.nccHit)))}let w=(1-g)*(.6+.4*(l>0?Math.min(1,r.length/l):0));if(p){const t=e[r[0].m].peaks.find(t=>t!==r[0].p);(t?Math.abs(t.ncc)/Math.abs(r[0].p.ncc):0)>a.loneRatio&&(w=Math.min(w,.4))}s.push({offsetS:i,driftPpm:1e6*c,confidence:w,agreeing:r.length,usable:l,correlation:h,peakZ:m,score:u.reduce((t,e)=>t+e,0),inverted:2*r.filter(t=>t.p.ncc<0).length>r.length,members:r.map(t=>({window:t.m,audioS:t.p.lag/n+(o.regionS??0)}))})}s.sort((t,e)=>e.score-t.score);return{candidates:s,rivalRatio:s.length>=2?s[1].score/s[0].score:0}}function T(t,e,n,o,a={}){const r=e.length;let i=0;for(let t=0;t<r;t++)i+=e[t]*e[t];const s=n=>{if(n<0||n+r>t.length)return 0;let o=0,a=0;for(let i=0;i<r;i++){const r=t[n+i];o+=e[i]*r,a+=r*r}const s=Math.sqrt(i*a);return s>1e-12?Math.abs(o/s):0},c=Math.round(n*o);let d=0;for(let t=-2;t<=2;t++)d=Math.max(d,s(c+t));if(d<=0)return 0;const f=Math.max(1,Math.round((a.guardS??.04)*o)),l=Math.round((a.spanS??.1)*o);let u=0;for(let t=f;t<=l;t++)u=Math.max(u,s(c-t),s(c+t));return u/d}const C={sampleRate:4e3,windowS:8,maxWindows:6,minConfidence:.7,maxClockSkewS:43200,maxDriftPpm:200,minOverlapS:1,concurrency:2};async function B(t,e,n){let o=0;await Promise.all(Array.from({length:Math.max(1,Math.min(e,t.length))},async()=>{for(;o<t.length;)await n(t[o++])}))}const H=(t,e)=>1-(1-t)*(1-e);function z(t,e,n,o,a,r){return W((t??e).candidates,n,o,a,r)}function W(t,e,n,o,a){if(void 0===e)return;const r=t.filter(t=>Math.abs(t.offsetS-e)<=n).sort((t,e)=>e.score-t.score)[0];if(!r)return;return t.some(t=>Math.abs(t.offsetS-r.offsetS)>.002&&Math.abs(t.offsetS-e)<=o&&t.score>=a*r.score)?void 0:r}async function j(e,o,i={}){const s={...C,...i},c=s.sampleRate,d=i.tuning??{},f={...L,maxDriftPpm:s.maxDriftPpm,...d},u=d.timecodeToleranceS??.5,g=d.timecodeSearchS??5,w=d.timecodeOnlyConfidence??.85,S=Math.max(1,Math.min(4,s.concurrency)),b={...s.ffmpeg?{ffmpeg:s.ffmpeg}:{},...s.signal?{signal:s.signal}:{},...s.tmpDir?{tmpDir:s.tmpDir}:{}},$=new Set;for(const t of[...e.map(t=>`clip:${t.id}`),...o.map(t=>`audio:${t.id}`)]){if($.has(t))throw new Error(`syncAudio: duplicate id ${t}`);$.add(t)}const _=new Map;await B(e,S,async t=>{if(!t.path||!1===t.hasAudio)return void _.set(t.id,{clip:t,windows:[],noWindows:"no-scratch-audio"});let e;try{e=await l(t.path,c,b)}catch(e){if(s.signal?.aborted)throw e;const n=e instanceof Error?e.message:String(e),o=/matches no streams/i.test(n);return void _.set(t.id,{clip:t,windows:[],noWindows:o?"no-scratch-audio":"error",...o?{}:{error:n.slice(0,300)}})}const n=N(e,c,s.windowS,s.maxWindows,t.audioLeadS??0);_.set(t.id,{clip:t,windows:n.windows,...n.windows.length?{}:{noWindows:n.considered?"silent-scratch-audio":"no-scratch-audio"}})});const D=[],R=[];for(const i of o){if(s.signal?.aborted)throw new Error("aborted");let o,d;if(e.some(t=>_.get(t.id).windows.length>0))try{o=await l(i.path,c,b)}catch(t){if(s.signal?.aborted)throw t;d=t instanceof Error?t.message:String(t)}const f=i.durationS||(o?o.length/c:0),u=o?await t(r(s.tmpDir??a(),"uf-sync-rec-")):void 0,h=u?r(u,"rec.f32"):void 0;try{o&&h&&await m(h,o),await B(e,S,async t=>{const e=_.get(t.id);let n;try{n=await F(e,i,f,o,d,h)}catch(t){if(s.signal?.aborted)throw t;n={ok:!1,reason:"error",detail:t instanceof Error?t.message:String(t)}}n.ok?D.push({clipId:t.id,audio:n.audio}):R.push({clipId:t.id,audioId:i.id,reason:n.reason,...n.detail?{detail:n.detail}:{},...void 0!==n.confidence?{confidence:n.confidence}:{}})})}finally{u&&await n(u,{recursive:!0,force:!0})}}async function F(t,e,n,o,a,r){const i=t.clip,l=E(i,{...e,durationS:n},s.maxClockSkewS);if(!1===l)return{ok:!1,reason:"different-day"};const m=void 0===l?{}:{sameDay:l};if("error"===t.noWindows)return{ok:!1,reason:"error",detail:`clip audio: ${t.error??"unreadable"}`};const S=A(i.startedAt),$=A(e.startedAt),_=void 0!==S&&void 0!==$?(S-$)/1e3:void 0;let D;const R=p(i.fps),F=i.timecode&&R?M(i.timecode,R):void 0;if(void 0!==F&&void 0!==e.timeOfDayS){const t=/[;.]\d{2,3}$/.test(i.timecode.trim()),o=x(y(F,e.timeOfDayS,v(R,t)),v(R,t),_);m.timecodeOffsetS=Math.round(1e6*o)/1e6;(void 0===_||Math.abs(o-_)<=s.maxClockSkewS)&&k(o,i.durationS,n)>=s.minOverlapS&&(D=o)}const N=(t,o,a,r)=>{if(void 0!==_&&Math.abs(t-_)>s.maxClockSkewS)return{ok:!1,reason:"different-day",detail:"the offset doesn't fit the recording dates",confidence:a};const c=k(t,i.durationS,n);return c<s.minOverlapS?{ok:!1,reason:"no-overlap",confidence:a}:a<s.minConfidence?{ok:!1,reason:"low-confidence",confidence:a}:{ok:!0,audio:{audioId:e.id,offsetS:Math.round(1e6*t)/1e6,driftPpm:Math.round(10*o)/10,confidence:Math.round(1e3*a)/1e3,method:r,unverifiedBySound:"timecode"===r,overlapS:Math.round(1e3*c)/1e3,evidence:m}}};if(!t.windows.length)return void 0===D?{ok:!1,reason:void 0!==m.timecodeOffsetS?"no-overlap":t.noWindows??"no-scratch-audio"}:!0!==l?{ok:!1,reason:"timecode-day-unverified",detail:"no scratch audio to confirm the timecode"}:(m.scratchAudio="silent-scratch-audio"===t.noWindows?"silent":"none",N(D,0,w,"timecode"));if(!o)return{ok:!1,reason:"error",detail:a??"recorder audio could not be decoded"};const L=t=>void 0!==D&&Math.abs(t.offsetS-D)<=u,P=o,C=(e,n,o)=>{const a=(t=>{const e=[...t].sort((t,e)=>t-e),n=e.length>>1;return 0===e.length?0:e.length%2?e[n]:(e[n-1]+e[n])/2})(e.members.map(e=>T(P,t.windows[e.window].samples,e.audioS,c)));return a>=(d.nearRivalMax??.85)?{ok:!1,reason:"ambiguous",detail:`the sound repeats within 0.1 s of this offset (periodic, ${a.toFixed(2)})`,confidence:n}:N(e.offsetS,e.driftPpm,n,o)},B=async(e,a)=>{const s=Math.max(0,Math.floor(e*c)),d=Math.min(o.length,Math.ceil(a*c));if(d-s<t.windows[0].samples.length)return{candidates:[],rivalRatio:0};const l=0===s&&d===o.length,u=await h(o.subarray(s,d),t.windows.map(t=>t.samples),c,{...b,...l&&r?{recPath:r}:{}});return O(t.windows,u,c,{clipDurS:i.durationS,audioDurS:n,regionS:s/c},f)};let W,j;if(void 0!==D){j=await B(D-g,D+i.durationS+g);const t=j.candidates[0];t&&t.confidence>=s.minConfidence&&j.rivalRatio<f.ambiguousRatio&&L(t)&&(W=j,m.searched="around-timecode")}W||(W=await B(0,n),m.searched="whole-file");const q=W.candidates[0],G=t=>{m.windowsAgreeing=t.agreeing,m.windowsUsable=t.usable,m.correlation=Math.round(1e3*t.correlation)/1e3,m.peakZ=Math.round(10*t.peakZ)/10,m.rivalRatio=Math.round(1e3*W.rivalRatio)/1e3};if(!q&&!j?.candidates.length)return{ok:!1,reason:"no-waveform-match",...void 0!==D?{detail:"timecode overlaps, but the clip's sound is not in this file"}:{}};if(q&&q.confidence>=s.minConfidence){if(W.rivalRatio<f.ambiguousRatio)return G(q),L(q)?C(q,H(q.confidence,.5),"timecode+waveform"):(void 0!==D&&(m.timecodeDisagreesS=Math.round(1e3*(q.offsetS-D))/1e3),C(q,q.confidence,"waveform"));const o=W.candidates.filter(t=>t.score>=f.ambiguousRatio*q.score),a=o.filter(L);if(1===a.length&&a[0].confidence>=s.minConfidence){const t=a[0];return G(t),m.tiebreak="timecode",C(t,H(t.confidence,.5)*(1-.2*W.rivalRatio),"timecode+waveform")}if(s.transcribe){const a=await(t=>t.words??=s.transcribe?s.transcribe({kind:"clip",clip:t.clip},{startS:0,durationS:Math.min(t.clip.durationS,d.transcriptMaxS??300)},s.signal):Promise.resolve([]))(t),r=[];for(const t of o){const o=Math.max(0,t.offsetS-1),c=Math.min(i.durationS,d.transcriptMaxS??300)+2,f=await s.transcribe({kind:"audio",audio:e},{startS:o,durationS:c},s.signal);r.push({c:t,agreement:I(a,f,t.offsetS,{audioDurS:n}).agreement})}r.sort((t,e)=>e.agreement-t.agreement);const[c,...f]=r;if(c&&c.c.confidence>=s.minConfidence&&c.agreement>=(d.transcriptMin??.5)&&f.every(t=>c.agreement-t.agreement>=(d.transcriptMargin??.25)))return G(c.c),m.tiebreak="transcript",m.transcriptAgreement=Math.round(1e3*c.agreement)/1e3,C(c.c,c.c.confidence*(1-.2*W.rivalRatio),"waveform+transcript")}return G(q),{ok:!1,reason:"ambiguous",detail:`${o.length} offsets fit: ${o.map(t=>t.offsetS.toFixed(3)).join(", ")} s`,confidence:q.confidence}}const K=z(j,W,D,u,g,f.ambiguousRatio);return K?(G(K),C(K,H(K.confidence,l?.75:.6),"timecode+waveform")):q?(G(q),{ok:!1,reason:"low-confidence",confidence:q.confidence}):{ok:!1,reason:"no-waveform-match"}}const P=new Map;for(const t of D)(P.get(t.clipId)??P.set(t.clipId,[]).get(t.clipId)).push(t.audio);const W=new Map(o.map((t,e)=>[t.id,e])),j=e.filter(t=>P.has(t.id)).map(t=>({clipId:t.id,audio:P.get(t.id).sort((t,e)=>e.confidence-t.confidence||W.get(t.audioId)-W.get(e.audioId))})),q=["error","ambiguous","low-confidence","timecode-day-unverified","silent-scratch-audio","no-scratch-audio","no-overlap","no-waveform-match","different-day","no-audio-candidates"],G=t=>t.length?t.reduce((t,e)=>q.indexOf(e.reason)<q.indexOf(t.reason)?e:t):void 0,K=e.filter(t=>!P.has(t.id)).map(t=>{const e=G(R.filter(e=>e.clipId===t.id));return e?{clipId:t.id,reason:e.reason,...e.detail?{detail:e.detail}:{}}:{clipId:t.id,reason:"no-audio-candidates"}}),Z=new Set(D.map(t=>t.audio.audioId));return{takes:j,unmatchedClips:K,unmatchedAudio:o.filter(t=>!Z.has(t.id)).map(t=>({audioId:t.id,reason:G(R.filter(e=>e.audioId===t.id))?.reason??"no-audio-candidates"})),rejected:R}}function q(t,e){return new Promise((n,o)=>{s(e.ffprobe??"ffprobe",["-v","error","-show_format","-show_streams","-of","json",t],{timeout:e.timeoutMs??3e4,maxBuffer:16777216,signal:e.signal},(e,a)=>{if(e)return o(new Error(`ffprobe failed on ${t}: ${e.message}`));try{n(JSON.parse(a))}catch(e){o(new Error(`ffprobe gave no JSON for ${t}`,{cause:e}))}})})}const G=t=>{const e=void 0===t?NaN:Number(t);return Number.isFinite(e)?e:void 0},K=t=>Object.fromEntries(Object.entries(t??{}).map(([t,e])=>[t.toLowerCase(),e]));async function Z(t,e={}){const n=await q(t,e),o=n.streams??[],a=o.find(t=>"video"===t.codec_type),r=o.find(t=>"audio"===t.codec_type),i=K(n.format?.tags),s=o.map(t=>K(t.tags).timecode).find(Boolean)??i.timecode,c=a?.r_frame_rate&&"0/0"!==a.r_frame_rate?a.r_frame_rate:a?.avg_frame_rate,d=G(n.format?.duration)??G(a?.duration)??G(r?.duration)??0,f=G(a?.start_time)??0,l=G(r?.start_time)??0,u=i["com.apple.quicktime.creationdate"]??i.creation_time??K(a?.tags).creation_time;return{id:e.id??t,path:t,durationS:d,hasAudio:!!r,audioLeadS:r&&a?l-f:0,...s?{timecode:s}:{},...c&&"0/0"!==c?{fps:c}:{},...u?{startedAt:u}:{}}}async function U(t,e={}){const n=await q(t,e),o=(n.streams??[]).find(t=>"audio"===t.codec_type),a=K(n.format?.tags),r=G(o?.sample_rate),i={id:e.id??t,path:t,durationS:G(n.format?.duration)??G(o?.duration)??0},s=await V(t).catch(()=>{}),c=G(a.time_reference);void 0!==c&&r?i.timeOfDayS=c/r:void 0!==s?.samplesSinceMidnight&&(s.timestampRate??r)&&(i.timeOfDayS=s.samplesSinceMidnight/(s.timestampRate??r));const d=/^(\d{4})[-:/](\d{2})[-:/](\d{2})$/.exec(a.date??""),f=/^(\d{2})[-:.](\d{2})[-:.](\d{2})$/.exec(a.creation_time??"");return d?i.startedAt=`${d[1]}-${d[2]}-${d[3]}T${f?`${f[1]}:${f[2]}:${f[3]}`:"00:00:00"}`:a.creation_time&&!f&&(i.startedAt=a.creation_time),s?.timecodeRate&&(i.timecodeRate=s.timecodeRate+("DF"===s.timecodeFlag?" DF":"")),s?.slate&&Object.keys(s.slate).length&&(i.slate=s.slate),i}const J=1048576;async function V(t){const e=await o(t,"r");try{const t=Buffer.alloc(12);if((await e.read(t,0,12,0)).bytesRead<12)return;const n=t.toString("latin1",0,4);if(!["RIFF","RF64","BW64"].includes(n)||"WAVE"!==t.toString("latin1",8,12))return;const o=(await e.stat()).size;let a,r=12;const i=Buffer.alloc(8);for(let t=0;t<1e4&&r+8<=o;t++){if((await e.read(i,0,8,r)).bytesRead<8)return;const t=i.toString("latin1",0,4);let n=i.readUInt32LE(4);if("ds64"===t&&n>=16){const t=Buffer.alloc(16);await e.read(t,0,16,r+8),a=Number(t.readBigUInt64LE(8))}if("data"===t&&4294967295===n&&void 0!==a&&(n=a),"iXML"===t){if(n>J||r+8+n>o)return;const t=Buffer.alloc(n);return await e.read(t,0,n,r+8),Y(t.toString("utf8"))}r+=8+n+n%2}return}finally{await e.close()}}const X=(t,e)=>{const n=new RegExp(`<${e}>([^<]{0,200})</${e}>`).exec(t),o=n?.[1]?.trim().replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"').replace(/&apos;/g,"'").replace(/&amp;/g,"&");return o||void 0},Q=t=>void 0!==t&&/^\d{1,10}$/.test(t)?Number(t):void 0;function Y(t){const e=new RegExp("<TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_HI[\\s/>]").test(t),n=Q(X(t,"TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_HI")),o=Q(X(t,"TIMESTAMP_SAMPLES_SINCE_MIDNIGHT_LO")),a=Q(X(t,"TIMESTAMP_SAMPLE_RATE")),r=X(t,"TIMECODE_FLAG"),i={};for(const[e,n]of[["project","PROJECT"],["scene","SCENE"],["take","TAKE"],["tape","TAPE"]]){const o=X(t,n);o&&(i[e]=o)}const s={slate:i};void 0!==o&&o<=4294967295&&(!e||void 0!==n)&&(n??0)<=2097151&&(s.samplesSinceMidnight=(n??0)*2**32+o),a&&a>0&&(s.timestampRate=a);const c=X(t,"TIMECODE_RATE");return c&&/^\d+(\.\d+)?(\/\d+)?$/.test(c)&&(s.timecodeRate=c),"DF"!==r&&"NDF"!==r||(s.timecodeFlag=r),s}const tt="sync";export{L as DEFAULT_TUNING,tt as SDK_ENTRY,F as SILENCE_RMS,y as clockDiffS,h as correlate,l as decodeMono,u as extractWav,T as nearRivalRatio,$ as normWord,O as offsetsFromPeaks,k as overlapS,p as parseFps,Y as parseIxml,z as pickWeak,W as pickWeakByTimecode,x as placeOnDay,N as planWindows,c as preFilter,U as probeAudio,Z as probeClip,V as readIxml,E as sameSession,A as startMs,j as syncAudio,v as timecodeDayS,S as timecodeToFrames,M as timecodeToSeconds,R as transcriberFor,I as transcriptAgreement,D as transcriptOffset,m as writeF32};
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Claim stores (gap 4): who uploads a key right now, as a lease with an expiry, so engines in several processes (or
3
+ * machines) that may upload the same content don't send it twice, and a holder that died is taken over once its lease
4
+ * runs out, keeping the parts it already proved (its upload id and part size are recorded in the claim).
5
+ *
6
+ * - memoryClaims(): one process (several engines, or tests).
7
+ * - kvClaims(kv): any KeyValueStore with compareAndSet (the SDK's memory store, `server/redis` over Redis/Upstash, the
8
+ * product's own): every write is a compare-and-set on the value read, so two takers can't both win.
9
+ *
10
+ * Leases use the wall clock (`now`, default Date.now): they are compared across processes. The record outlives the
11
+ * lease by `keepMs` (default 7 days), so a takeover long after a crash still finds the dead holder's upload id.
12
+ */
13
+ import type { ClaimRecord, ClaimStore } from "./protocol";
14
+ type Held = {
15
+ token: string;
16
+ expiresAt: number;
17
+ released?: boolean;
18
+ } & ClaimRecord;
19
+ /** Claims in this process's memory. */
20
+ export declare function memoryClaims(opts?: {
21
+ now?: () => number;
22
+ }): ClaimStore & {
23
+ peek(key: string): Held | undefined;
24
+ };
25
+ /** The KeyValueStore subset kvClaims needs (`@uploadfilm/sdk/server` KeyValueStore fits). */
26
+ export interface ClaimKv {
27
+ get(key: string): Promise<string | null>;
28
+ compareAndSet(key: string, expected: string | null, value: string, ttl?: number | {
29
+ ttlMs?: number;
30
+ }): Promise<boolean>;
31
+ }
32
+ /** Claims in a shared KeyValueStore. `prefix` namespaces the keys (default "claim:"). */
33
+ export declare function kvClaims(kv: ClaimKv, opts?: {
34
+ prefix?: string;
35
+ now?: () => number;
36
+ keepMs?: number;
37
+ ttlArg?: (ms: number) => number | {
38
+ ttlMs?: number;
39
+ };
40
+ }): ClaimStore;
41
+ export {};