@trillboards/edge-sdk 0.2.273 → 0.2.276

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.
@@ -19,6 +19,11 @@ export interface RingSegment {
19
19
  height: number | null;
20
20
  /** Which recording closed it: the main-stream recorder names its files `<start>-main-…` (MainStreamRing). */
21
21
  stream: 'sub' | 'main';
22
+ /**
23
+ * The camera's audio codec as the recorder saw it (sidecar `stream.audio.codec`): `aac` is copied into the
24
+ * file, anything else transcoded to AAC once. Null: no audio, or not stated.
25
+ */
26
+ audioCodec?: string | null;
22
27
  }
23
28
  /** Which recording a ring file came from, by its name. */
24
29
  export declare function ringSegmentStream(name: string): 'sub' | 'main';
@@ -51,6 +56,92 @@ export declare function buildCutList(run: RingSegment[], videoStartSec: VideoSta
51
56
  text: string;
52
57
  startWallMs: number;
53
58
  };
59
+ /** One track of a ring file on the file's own timeline (edge-platform-rtsp `SegmentTrackTimes`). */
60
+ export interface RingFileTrack {
61
+ startSec: number;
62
+ durationSec: number;
63
+ /** Usual sample duration (the one most samples have): the frame interval, or an AAC frame. Null: unread. */
64
+ sampleSec: number | null;
65
+ }
66
+ /** A ring file's video and audio tracks; null for one it does not hold or that cannot be read. */
67
+ export interface RingFileTracks {
68
+ video: RingFileTrack | null;
69
+ audio: RingFileTrack | null;
70
+ }
71
+ export type TrackReader = (file: string) => RingFileTracks;
72
+ /** One ring file's lead: where its sound starts minus where its picture starts, seconds, and its tracks' sample durations. */
73
+ export interface SoundLead {
74
+ leadSec: number;
75
+ audioFrameSec: number;
76
+ videoFrameSec: number;
77
+ }
78
+ /**
79
+ * L, how many seconds after its picture the camera stamped its sound, from one
80
+ * recorder session's file leads; null when no file shows a lead past the
81
+ * allowance (the lag is then under it, and cannot be read from the files) or
82
+ * the leads disagree in sign.
83
+ *
84
+ * Video first (L > 0): the muxer holds each audio packet until the newest is
85
+ * more than the allowance later, so a file starts its sound
86
+ * `L - grid(audio frame) - delay + phase` after its picture, the phase in
87
+ * [0, one AAC frame) depending on where the split falls on the audio's frame
88
+ * grid. Audio first (L < 0): it holds the video instead, and a file starts its
89
+ * sound `L + grid(frame interval) - delay + phase` after its picture (before
90
+ * it). Each file bounds L to one AAC frame; across the session the phase walks
91
+ * the frame (the files are not a whole number of frames long), so the middle
92
+ * of the files' estimates is L. An estimate more than a frame from the
93
+ * session's median is an audio dropout, not the lag. The median of an even
94
+ * count is the lower middle estimate, a file's own, so it always keeps itself
95
+ * even when the estimates form two clusters; a dropout only ever starts a
96
+ * file's sound later, so the lower cluster is the lag.
97
+ */
98
+ export declare function soundLagFromLeads(leads: SoundLead[], copied: boolean): number | null;
99
+ /** Where the clip's sound comes from. */
100
+ export type SoundPlan =
101
+ /** As recorded, from the picture's own files: the lag is under the allowance (unreadable) or the files do not say. */
102
+ {
103
+ kind: 'as_recorded';
104
+ reason: string;
105
+ }
106
+ /** The files holding the window's sound have not closed yet: cut once the ring's capture end reaches `coverMs`. */
107
+ | {
108
+ kind: 'wait';
109
+ lagSec: number;
110
+ coverMs: number;
111
+ }
112
+ /** From a second concat input over the files that hold it, `atSec` into its timeline at the clip's start. */
113
+ | {
114
+ kind: 'moved';
115
+ lagSec: number;
116
+ text: string;
117
+ atSec: number;
118
+ files: number;
119
+ short: boolean;
120
+ }
121
+ /** The lag is known and no file in the ring holds the window's sound: the clip has none. */
122
+ | {
123
+ kind: 'missing';
124
+ lagSec: number;
125
+ };
126
+ /**
127
+ * Where the window's sound is. `segments` is the listing around the window
128
+ * (SOUND_SEARCH_MS either side), `run` the files the picture is cut from.
129
+ * A ring file's sound starts at `captureStartMs + (audio start - video start)`
130
+ * on the picture's wall clock (a session's first file is listed from its
131
+ * first packet, so its video start counts as 0), and the sound of the moment
132
+ * captured at t sits at t + L on that clock. The second input lists those
133
+ * files back to back, each from its audio start for as long as the next file's
134
+ * sound starts after it, so its timeline is that wall clock from the first
135
+ * file's audio start.
136
+ */
137
+ export declare function planSound(o: {
138
+ segments: RingSegment[];
139
+ run: RingSegment[];
140
+ clipStartMs: number;
141
+ clipEndMs: number;
142
+ tracks: TrackReader | null;
143
+ waitForSound: boolean;
144
+ }): SoundPlan;
54
145
  /** The cut, exactly. Pure, so the argv can be asserted. */
55
146
  export declare function buildCutArgs(o: {
56
147
  listFile: string;
@@ -58,6 +149,14 @@ export declare function buildCutArgs(o: {
58
149
  durationSec: number;
59
150
  out: string;
60
151
  hevc: boolean;
152
+ /**
153
+ * The sound: from a second list (`planSound`), `atSec` into its timeline at
154
+ * the clip's start; `none`, no sound; absent, the picture's own files' sound.
155
+ */
156
+ sound?: {
157
+ listFile: string;
158
+ atSec: number;
159
+ } | 'none' | null;
61
160
  }): string[];
62
161
  export interface CutClip {
63
162
  file: string;
@@ -66,12 +165,26 @@ export interface CutClip {
66
165
  captureEndMs: number;
67
166
  /** Ring segments the clip was cut from. */
68
167
  sourceSegments: number;
168
+ /**
169
+ * How far after its picture the camera stamped the sound the clip was cut to
170
+ * meet, seconds (`planSound`). Null: the sound is as recorded (the lag is
171
+ * under the interleave allowance, or the files did not say).
172
+ */
173
+ soundLagSec?: number | null;
174
+ /** Why the sound is as recorded, why the clip has none, or that it ends short; null when moved in full. */
175
+ soundNote?: string | null;
69
176
  }
70
177
  export type CutResult = {
71
178
  clip: CutClip;
72
179
  } | {
73
180
  unavailable: 'no_buffered_video' | 'too_short' | 'cut_failed';
74
181
  detail?: string;
182
+ }
183
+ /** `waitForSound`: the files holding the window's sound have not all closed; ask again once the ring's capture end reaches `coverMs`. */
184
+ | {
185
+ wait: 'sound';
186
+ coverMs: number;
187
+ soundLagSec: number;
75
188
  };
76
189
  export type ExecFileFn = (file: string, args: string[], options: {
77
190
  timeout: number;
@@ -86,6 +199,13 @@ export declare function cutAlertWindow(o: {
86
199
  execFileFn?: ExecFileFn;
87
200
  /** Test seam; default edge-platform-rtsp's `readVideoStartSec`, uncapped. */
88
201
  videoStartSec?: VideoStartReader | null;
202
+ /** Test seam; default edge-platform-rtsp's `readSegmentTracks`. Null: the sound is cut as recorded. */
203
+ segmentTracks?: TrackReader | null;
204
+ /**
205
+ * Return `{ wait: 'sound' }` instead of cutting while the files that hold the
206
+ * window's sound have not all closed (they close about 10 s after its picture's).
207
+ */
208
+ waitForSound?: boolean;
89
209
  }): Promise<CutResult>;
90
210
  /** An event clip left on disk by an earlier process: cut, not yet delivered. */
91
211
  export interface PendingEventClip {
@@ -1 +1 @@
1
- {"version":3,"file":"AlertClips.d.ts","sourceRoot":"","sources":["../src/AlertClips.ts"],"names":[],"mappings":"AAmDA,OAAO,EAAqB,KAAK,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAEvF,+EAA+E;AAC/E,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,wGAAwG;IACxG,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,6GAA6G;IAC7G,MAAM,EAAE,KAAK,GAAG,MAAM,CAAC;CACxB;AAKD,0DAA0D;AAC1D,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,CAE9D;AAYD;;;;;GAKG;AACH,wBAAsB,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAmD9G;AAaD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,OAAO,EA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AAC,UAAU,CAAyC;IACpE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAa;IACpC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAe;IACnC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAoD;IACxE,OAAO,CAAC,MAAM,CAA2E;IACzF;;;;;;;;;OASG;IACH,OAAO,CAAC,eAAe,CAAuB;IAC9C,OAAO,CAAC,KAAK,CAA+B;IAC5C,oEAAoE;IACpE,OAAO,CAAC,YAAY,CAAuB;IAC3C,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAS;IACxB,QAAQ,CAAC,KAAK;;;;;;MAAmE;gBAErE,OAAO,EAAE,6BAA6B;IAalD,yEAAyE;IACzE,gBAAgB,IAAI,OAAO;IAS3B,qEAAqE;IACrE,KAAK,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,IAAI;IAe9D,IAAI,IAAI,IAAI;IASZ,OAAO,CAAC,QAAQ;YAWF,IAAI;CAwEnB"}
@@ -38,6 +38,8 @@ exports.ringSegmentStream = ringSegmentStream;
38
38
  exports.listRingSegments = listRingSegments;
39
39
  exports.selectWindowRun = selectWindowRun;
40
40
  exports.buildCutList = buildCutList;
41
+ exports.soundLagFromLeads = soundLagFromLeads;
42
+ exports.planSound = planSound;
41
43
  exports.buildCutArgs = buildCutArgs;
42
44
  exports.cutAlertWindow = cutAlertWindow;
43
45
  exports.recoverEventClips = recoverEventClips;
@@ -88,6 +90,21 @@ exports.isReceiptCameraId = isReceiptCameraId;
88
90
  * lead-long hole at every file boundary: 60 of 88 Texas alert clips held under
89
91
  * 95% of their window, Nov 10-17 2026. The durations close the hole either way;
90
92
  * a video-first file's inpoint is 0.
93
+ *
94
+ * THE SOUND. The camera stamped its sound L seconds after its picture (Texas: a
95
+ * DVR flushes up to ~35 s of buffered audio when a session opens, then its
96
+ * audio clock runs 378-475 ppm fast), and every ring file keeps that: placed by
97
+ * its timestamps, a moment's sound plays L after its frame, on any player. The
98
+ * box is the only place L can be read back. Past ffmpeg's interleave allowance
99
+ * the muxer holds each audio packet until the newest video packet is that
100
+ * allowance later, so a file's sound starts `lead` after its video and L is the
101
+ * lead plus the allowance, less the tracks' path delays (`soundLagFromLeads`,
102
+ * calibrated on the box's ffmpeg); under it the file shows no lead and L cannot
103
+ * be told, and the sound is left where it is. When the
104
+ * lead is visible the cut takes the clip's sound from a second concat input over
105
+ * the files that hold it (about 10 s after the picture's files: the muxer wrote
106
+ * each file's audio that much later than its video), seeked so that each
107
+ * moment's sound meets its frame (`planSound`). The video cut is unchanged.
91
108
  */
92
109
  const fs = __importStar(require("fs"));
93
110
  const path = __importStar(require("path"));
@@ -171,6 +188,7 @@ async function listRingSegments(closedDir, fromMs, toMs) {
171
188
  width: size(stream?.width),
172
189
  height: size(stream?.height),
173
190
  stream: ringSegmentStream(name),
191
+ audioCodec: stream?.audio && typeof stream.audio.codec === 'string' ? stream.audio.codec : null,
174
192
  });
175
193
  }
176
194
  return out.sort((a, b) => a.captureStartMs - b.captureStartMs);
@@ -248,8 +266,220 @@ function buildCutList(run, videoStartSec) {
248
266
  });
249
267
  return { text: [...lines, ''].join('\n'), startWallMs: starts[0].wallMs };
250
268
  }
269
+ /** edge-platform-rtsp's `readSegmentTracks`, loaded at runtime like `readVideoStartSec`. Null in a package without it. */
270
+ function loadTrackReader() {
271
+ try {
272
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
273
+ const read = require('@trillboards/edge-platform-rtsp').readSegmentTracks;
274
+ return typeof read === 'function' ? read : null;
275
+ }
276
+ catch {
277
+ return null;
278
+ }
279
+ }
280
+ /** ffmpeg's interleave allowance (`max_interleave_delta`, default 10 s): the recorder sets none. */
281
+ const INTERLEAVE_ALLOWANCE_SEC = 10;
282
+ /**
283
+ * A file whose sound starts at least this far from its picture was written past
284
+ * the allowance. Under it the two start together, within one AAC frame (0.128 s
285
+ * at 8 kHz).
286
+ */
287
+ const VISIBLE_LEAD_SEC = 0.3;
288
+ /**
289
+ * How much later than its frame a moment's sound reaches the recorder's muxer
290
+ * from a camera whose picture leaves it at once, seconds, by its timestamps: the
291
+ * box's AAC encode when the camera's audio is transcoded (G.711, 8 kHz), the
292
+ * camera's own when it is copied. Measured on the box's ffmpeg (6.0) against a
293
+ * rig camera with no encode latency (H.264 320x240, 20-ms G.711 packets), files
294
+ * written video first and audio first alike: 0.189 and 0.192 s transcoded,
295
+ * 0.078 and 0.106 s copied (16-kHz AAC), each net of AAC_DECODE_DELAY_FRAMES.
296
+ */
297
+ const AUDIO_PATH_DELAY_TRANSCODED_SEC = 0.19;
298
+ const AUDIO_PATH_DELAY_COPIED_SEC = 0.09;
299
+ /**
300
+ * A decoder entering an AAC stream mid-way, as every ring file after a
301
+ * session's first and every clip does (no priming to trim), plays it one frame
302
+ * late: a ring file's beep decodes 0.128 s after its timestamp at 8 kHz. The
303
+ * cut takes the sound that much early, so it is heard with its picture.
304
+ */
305
+ const AAC_DECODE_DELAY_FRAMES = 1;
306
+ /**
307
+ * How much later than its sound a camera's picture leaves it: its video encode.
308
+ * The box cannot tell it from the lag (both reach the muxer as one offset
309
+ * between the tracks' arrivals), so this is a typical camera's: the rig's HEVC
310
+ * 800x448 encoder at 20 fps shaped like Texas's cam 3, 0.25 s (its files' sound
311
+ * starts 0.25 s later than the H.264 rig's at the same lag). A camera that
312
+ * encodes faster keeps its clips' sound that much late, which is the direction
313
+ * a viewer forgives; slower, early.
314
+ */
315
+ const CAMERA_PICTURE_LATENCY_SEC = 0.25;
316
+ /** The camera's own sample rate is unknown when a file's audio track cannot be read: G.711's. */
317
+ const DEFAULT_AUDIO_FRAME_SEC = 1024 / 8000;
318
+ const DEFAULT_VIDEO_FRAME_SEC = 0.05;
319
+ /** A file's estimate further than one AAC frame plus this from the session's median is not the lag (an audio dropout). */
320
+ const SOUND_LAG_SPREAD_SEC = 0.1;
321
+ /**
322
+ * How far either side of the window the cut looks for the files that hold its
323
+ * sound: past the allowance it sits about 10.4 s after (video first) or 9.8 s
324
+ * before (audio first) the picture's files, plus one 10-14-s file.
325
+ */
326
+ const SOUND_SEARCH_MS = 25000;
327
+ /** The clip's sound may end this much short of its picture before the cut waits for the next file. */
328
+ const SOUND_SHORT_TOLERANCE_MS = 250;
329
+ /**
330
+ * The smallest multiple of `stepSec` past the allowance: when one track runs
331
+ * ahead, ffmpeg holds the other's packets until the newest is more than the
332
+ * allowance after the oldest, and packets arrive a frame apart.
333
+ */
334
+ function interleaveGrid(stepSec) {
335
+ return (Math.floor(INTERLEAVE_ALLOWANCE_SEC / stepSec + 1e-9) + 1) * stepSec;
336
+ }
337
+ /**
338
+ * L, how many seconds after its picture the camera stamped its sound, from one
339
+ * recorder session's file leads; null when no file shows a lead past the
340
+ * allowance (the lag is then under it, and cannot be read from the files) or
341
+ * the leads disagree in sign.
342
+ *
343
+ * Video first (L > 0): the muxer holds each audio packet until the newest is
344
+ * more than the allowance later, so a file starts its sound
345
+ * `L - grid(audio frame) - delay + phase` after its picture, the phase in
346
+ * [0, one AAC frame) depending on where the split falls on the audio's frame
347
+ * grid. Audio first (L < 0): it holds the video instead, and a file starts its
348
+ * sound `L + grid(frame interval) - delay + phase` after its picture (before
349
+ * it). Each file bounds L to one AAC frame; across the session the phase walks
350
+ * the frame (the files are not a whole number of frames long), so the middle
351
+ * of the files' estimates is L. An estimate more than a frame from the
352
+ * session's median is an audio dropout, not the lag. The median of an even
353
+ * count is the lower middle estimate, a file's own, so it always keeps itself
354
+ * even when the estimates form two clusters; a dropout only ever starts a
355
+ * file's sound later, so the lower cluster is the lag.
356
+ */
357
+ function soundLagFromLeads(leads, copied) {
358
+ const visible = leads.filter((l) => Number.isFinite(l.leadSec) && Math.abs(l.leadSec) >= VISIBLE_LEAD_SEC);
359
+ if (!visible.length)
360
+ return null;
361
+ const sign = Math.sign(visible[0].leadSec);
362
+ if (visible.some((l) => Math.sign(l.leadSec) !== sign))
363
+ return null;
364
+ const delay = (copied ? AUDIO_PATH_DELAY_COPIED_SEC : AUDIO_PATH_DELAY_TRANSCODED_SEC) - CAMERA_PICTURE_LATENCY_SEC;
365
+ const estimates = visible.map((l) => l.leadSec + (sign > 0 ? interleaveGrid(l.audioFrameSec) : -interleaveGrid(l.videoFrameSec)) + delay - l.audioFrameSec / 2);
366
+ const sorted = [...estimates].sort((a, b) => a - b);
367
+ const median = sorted[(sorted.length - 1) >> 1];
368
+ const frame = Math.max(...visible.map((l) => l.audioFrameSec));
369
+ const kept = sorted.filter((e) => Math.abs(e - median) <= frame + SOUND_LAG_SPREAD_SEC);
370
+ if (!kept.length)
371
+ return null;
372
+ const lag = (kept[0] + kept[kept.length - 1]) / 2;
373
+ return Number.isFinite(lag) ? lag : null;
374
+ }
375
+ /**
376
+ * One recorder session's files: a session's first file (`startSec` 0), a gap or
377
+ * a change of stream starts the next. Each session has its own lag (the DVR's
378
+ * audio flush when the session opens).
379
+ */
380
+ function recorderSessions(segments) {
381
+ const sessions = [];
382
+ // The substream's and the main stream's recorders are separate sessions, side by side for a few seconds.
383
+ const open = new Map();
384
+ for (const segment of segments) {
385
+ const session = open.get(segment.stream);
386
+ const last = session ? session[session.length - 1] : null;
387
+ if (session && last && segment.startSec !== 0 && segment.captureStartMs - last.captureEndMs <= RUN_GAP_MS && sameStream(last, segment)) {
388
+ session.push(segment);
389
+ }
390
+ else {
391
+ const next = [segment];
392
+ sessions.push(next);
393
+ open.set(segment.stream, next);
394
+ }
395
+ }
396
+ return sessions;
397
+ }
398
+ /**
399
+ * Where the window's sound is. `segments` is the listing around the window
400
+ * (SOUND_SEARCH_MS either side), `run` the files the picture is cut from.
401
+ * A ring file's sound starts at `captureStartMs + (audio start - video start)`
402
+ * on the picture's wall clock (a session's first file is listed from its
403
+ * first packet, so its video start counts as 0), and the sound of the moment
404
+ * captured at t sits at t + L on that clock. The second input lists those
405
+ * files back to back, each from its audio start for as long as the next file's
406
+ * sound starts after it, so its timeline is that wall clock from the first
407
+ * file's audio start.
408
+ */
409
+ function planSound(o) {
410
+ if (!o.tracks)
411
+ return { kind: 'as_recorded', reason: 'no track reader' };
412
+ const session = recorderSessions(o.segments).find((s) => s.some((f) => f.file === o.run[0].file));
413
+ if (!session || o.run.some((f) => !session.includes(f)))
414
+ return { kind: 'as_recorded', reason: 'the window spans a recorder restart' };
415
+ const read = session.map((f) => {
416
+ try {
417
+ return o.tracks(f.file);
418
+ }
419
+ catch {
420
+ return { video: null, audio: null };
421
+ }
422
+ });
423
+ const leads = [];
424
+ session.forEach((f, i) => {
425
+ const { video, audio } = read[i];
426
+ if (f.startSec === 0 || !video || !audio)
427
+ return;
428
+ leads.push({
429
+ leadSec: audio.startSec - video.startSec,
430
+ audioFrameSec: audio.sampleSec ?? DEFAULT_AUDIO_FRAME_SEC,
431
+ videoFrameSec: video.sampleSec ?? DEFAULT_VIDEO_FRAME_SEC,
432
+ });
433
+ });
434
+ const lagSec = soundLagFromLeads(leads, o.run[0].audioCodec === 'aac');
435
+ if (lagSec === null)
436
+ return { kind: 'as_recorded', reason: leads.length ? 'no file shows a lead past the allowance' : 'no file with both tracks' };
437
+ // The sound of the moment captured at t sits at t + L on the picture's clock; it is heard a decoder's frame after it is taken.
438
+ const frameSec = Math.max(...leads.map((l) => l.audioFrameSec));
439
+ const takeSec = lagSec + AAC_DECODE_DELAY_FRAMES * frameSec;
440
+ const needFromMs = o.clipStartMs + takeSec * 1000;
441
+ const needToMs = o.clipEndMs + takeSec * 1000;
442
+ const spans = session.map((f, i) => {
443
+ const { video, audio } = read[i];
444
+ if (!audio || !(audio.durationSec > 0) || (f.startSec !== 0 && !video))
445
+ return null;
446
+ const startMs = f.captureStartMs + (audio.startSec - (f.startSec === 0 ? 0 : video.startSec)) * 1000;
447
+ return { f, inSec: audio.startSec, startMs, endMs: startMs + audio.durationSec * 1000 };
448
+ });
449
+ const first = spans.findIndex((s) => s !== null && s.endMs > needFromMs);
450
+ let last = first;
451
+ while (first >= 0 && last + 1 < spans.length && spans[last + 1] !== null && spans[last + 1].startMs < needToMs)
452
+ last += 1;
453
+ const minLead = Math.min(...leads.filter((l) => Math.abs(l.leadSec) >= VISIBLE_LEAD_SEC).map((l) => l.leadSec));
454
+ // The ring's capture end at which a file's sound reaches the window's end:
455
+ // a file's sound runs `lead` past its picture's end.
456
+ const coverMs = Math.ceil(needToMs - minLead * 1000) + 1;
457
+ // More of this session is still to come only while no later file of its stream started another.
458
+ const lastFile = session[session.length - 1];
459
+ const sessionOpen = !o.segments.some((s) => s.stream === lastFile.stream && s.captureStartMs > lastFile.captureStartMs);
460
+ const tailMissing = first < 0 || spans[last].endMs < needToMs - SOUND_SHORT_TOLERANCE_MS;
461
+ if (tailMissing && sessionOpen && o.waitForSound && (first < 0 || last + 1 === spans.length))
462
+ return { kind: 'wait', lagSec, coverMs };
463
+ if (first < 0 || spans[first].startMs >= needToMs)
464
+ return { kind: 'missing', lagSec };
465
+ const picked = spans.slice(first, last + 1);
466
+ const lines = ['ffconcat version 1.0'];
467
+ picked.forEach((s, i) => {
468
+ const untilMs = i + 1 < picked.length ? picked[i + 1].startMs : s.endMs;
469
+ lines.push(`file ${ffconcatPath(s.f.file)}`, `inpoint ${s.inSec.toFixed(6)}`, `duration ${Math.max(0.001, (untilMs - s.startMs) / 1000).toFixed(6)}`);
470
+ });
471
+ return {
472
+ kind: 'moved',
473
+ lagSec,
474
+ text: [...lines, ''].join('\n'),
475
+ atSec: (needFromMs - picked[0].startMs) / 1000,
476
+ files: picked.length,
477
+ short: tailMissing,
478
+ };
479
+ }
251
480
  /** The cut, exactly. Pure, so the argv can be asserted. */
252
481
  function buildCutArgs(o) {
482
+ const sound = o.sound && o.sound !== 'none' ? o.sound : null;
253
483
  return [
254
484
  '-hide_banner', '-loglevel', 'error', '-nostdin', '-y',
255
485
  // On the INPUT: the copy starts at the keyframe at or before the offset.
@@ -260,8 +490,20 @@ function buildCutArgs(o) {
260
490
  '-seek_timestamp', '1',
261
491
  '-ss', o.offsetSec.toFixed(3),
262
492
  '-f', 'concat', '-safe', '0', '-i', o.listFile,
493
+ // The sound's list maps its `atSec` to the clip's 0 the same way; a sound
494
+ // that starts after the clip does is delayed instead of seeked.
495
+ ...(sound ? [
496
+ '-seek_timestamp', '1',
497
+ ...(sound.atSec >= 0 ? ['-ss', sound.atSec.toFixed(3)] : ['-itsoffset', (-sound.atSec).toFixed(3)]),
498
+ '-f', 'concat', '-safe', '0', '-i', sound.listFile,
499
+ ] : []),
263
500
  '-t', o.durationSec.toFixed(3),
264
- '-map', '0:v:0', '-map', '0:a:0?',
501
+ '-map', '0:v:0', ...(o.sound === 'none' ? [] : ['-map', sound ? '1:a:0?' : '0:a:0?']),
502
+ // The seek lands the sound on the picture's keyframe, which in a file
503
+ // written video first sits seconds before its sound: drop what precedes the
504
+ // clip's start, so a player that ignores edit lists is off by the picture's
505
+ // pre-roll (at most a GOP) and not by that.
506
+ ...(sound ? ['-copypriorss:a', '0'] : []),
265
507
  '-c', 'copy',
266
508
  ...(o.hevc ? ['-tag:v', 'hvc1'] : []),
267
509
  '-movflags', '+faststart',
@@ -277,7 +519,11 @@ async function cutAlertWindow(o) {
277
519
  // A request names its recording (the substream or the main stream) and is cut
278
520
  // only from it; an alert's window is cut from whatever the ring holds.
279
521
  const wanted = request.kind && request.kind !== 'alert' && request.stream ? request.stream : null;
280
- const segments = (await listRingSegments(o.closedDir, fromMs, toMs)).filter((s) => wanted === null || s.stream === wanted);
522
+ // Listed wider than the window: the files around it say how late the sound
523
+ // was stamped, and past the interleave allowance hold the window's sound.
524
+ const nearby = (await listRingSegments(o.closedDir, fromMs - SOUND_SEARCH_MS, toMs + SOUND_SEARCH_MS))
525
+ .filter((s) => wanted === null || s.stream === wanted);
526
+ const segments = nearby.filter((s) => s.captureEndMs > fromMs && s.captureStartMs < toMs);
281
527
  const run = selectWindowRun(segments, request.capturedAtMs, fromMs, toMs);
282
528
  if (!run.length)
283
529
  return { unavailable: 'no_buffered_video' };
@@ -286,29 +532,60 @@ async function cutAlertWindow(o) {
286
532
  const clipEnd = Math.min(toMs, run[run.length - 1].captureEndMs);
287
533
  if (clipEnd - clipStart < MIN_CLIP_MS)
288
534
  return { unavailable: 'too_short' };
535
+ const sound = planSound({
536
+ segments: nearby,
537
+ run,
538
+ clipStartMs: clipStart,
539
+ clipEndMs: clipEnd,
540
+ tracks: o.segmentTracks !== undefined ? o.segmentTracks : loadTrackReader(),
541
+ waitForSound: o.waitForSound === true,
542
+ });
543
+ if (sound.kind === 'wait')
544
+ return { wait: 'sound', coverMs: sound.coverMs, soundLagSec: sound.lagSec };
289
545
  fs.mkdirSync(o.outDir, { recursive: true });
290
546
  const listFile = path.join(o.outDir, `${request.triggerId}.ffconcat`);
547
+ const soundListFile = path.join(o.outDir, `${request.triggerId}.sound.ffconcat`);
291
548
  const part = path.join(o.outDir, `${request.triggerId}.part.mp4`);
292
549
  const file = path.join(o.outDir, `${request.triggerId}.mp4`);
293
550
  const metaFile = path.join(o.outDir, `${request.triggerId}.json`);
294
551
  fs.writeFileSync(listFile, list.text);
295
- const args = buildCutArgs({
552
+ if (sound.kind === 'moved')
553
+ fs.writeFileSync(soundListFile, sound.text);
554
+ // Once the lag is known, the picture's own files' sound is another moment's:
555
+ // no sound beats it, when the ring holds none of the window's or the moved
556
+ // sound fails to cut.
557
+ const cutArgs = (soundless) => buildCutArgs({
296
558
  listFile,
297
559
  offsetSec: (clipStart - list.startWallMs) / 1000,
298
560
  durationSec: (clipEnd - clipStart) / 1000,
299
561
  out: part,
300
562
  hevc: run[0].codec === 'hevc',
563
+ sound: soundless || sound.kind === 'missing' ? 'none' : sound.kind === 'moved' ? { listFile: soundListFile, atSec: sound.atSec } : null,
301
564
  });
302
565
  const exec = o.execFileFn ?? child_process_1.execFile;
303
- try {
304
- await new Promise((resolve, reject) => {
305
- exec(o.ffmpegPath, args, { timeout: CUT_TIMEOUT_MS, windowsHide: true }, (error, _stdout, stderr) => {
306
- if (error)
307
- reject(new Error(`${error.message} ${String(stderr || '').slice(0, 300)}`.trim()));
308
- else
309
- resolve();
310
- });
566
+ const runCut = (args) => new Promise((resolve, reject) => {
567
+ exec(o.ffmpegPath, args, { timeout: CUT_TIMEOUT_MS, windowsHide: true }, (error, _stdout, stderr) => {
568
+ if (error)
569
+ reject(new Error(`${error.message} ${String(stderr || '').slice(0, 300)}`.trim()));
570
+ else
571
+ resolve();
311
572
  });
573
+ });
574
+ const soundLagSec = sound.kind === 'as_recorded' ? null : sound.lagSec;
575
+ let soundNote = sound.kind === 'as_recorded' ? sound.reason
576
+ : sound.kind === 'missing' ? 'the ring holds none of the window\'s sound'
577
+ : sound.short ? 're-timed to meet it, but it ends before the picture: its files had not closed' : null;
578
+ try {
579
+ try {
580
+ await runCut(cutArgs(false));
581
+ }
582
+ catch (err) {
583
+ if (sound.kind !== 'moved')
584
+ throw err;
585
+ // The picture is the clip: a sound input that fails costs the sound, never the clip.
586
+ await runCut(cutArgs(true));
587
+ soundNote = `the moved sound failed to cut (${(err instanceof Error ? err.message : String(err)).slice(0, 120)}); no sound`;
588
+ }
312
589
  const size = fs.statSync(part).size;
313
590
  if (size <= 0)
314
591
  throw new Error('the cut wrote an empty file');
@@ -322,7 +599,7 @@ async function cutAlertWindow(o) {
322
599
  sourceSegments: run.length,
323
600
  }));
324
601
  fs.renameSync(part, file);
325
- return { clip: { file, metaFile, captureStartMs: clipStart, captureEndMs: clipEnd, sourceSegments: run.length } };
602
+ return { clip: { file, metaFile, captureStartMs: clipStart, captureEndMs: clipEnd, sourceSegments: run.length, soundLagSec, soundNote } };
326
603
  }
327
604
  catch (err) {
328
605
  for (const leftover of [part, metaFile]) {
@@ -336,11 +613,13 @@ async function cutAlertWindow(o) {
336
613
  return { unavailable: 'cut_failed', detail: err instanceof Error ? err.message : String(err) };
337
614
  }
338
615
  finally {
339
- try {
340
- fs.unlinkSync(listFile);
341
- }
342
- catch {
343
- /* already gone */
616
+ for (const leftover of [listFile, soundListFile]) {
617
+ try {
618
+ fs.unlinkSync(leftover);
619
+ }
620
+ catch {
621
+ /* already gone */
622
+ }
344
623
  }
345
624
  }
346
625
  }