@ahmedrowaihi/mediabunny 1.65.2-beta.719 → 1.65.2-beta.720

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.
@@ -52472,6 +52472,208 @@ ${cue.notes ?? ""}`;
52472
52472
  }
52473
52473
  };
52474
52474
 
52475
+ // src/mpeg-ts/scte35.ts
52476
+ var SYNC_BYTE = 71;
52477
+ var SPLICE_INFO_TABLE_ID = 252;
52478
+ var CUEI_FORMAT_IDENTIFIER2 = 1129661769;
52479
+ var SCTE35_STREAM_TYPE2 = 134;
52480
+ var SPLICE_INSERT = 5;
52481
+ var PROGRAM_SPLICE_FLAG = 64;
52482
+ var SPLICE_IMMEDIATE_FLAG = 16;
52483
+ var TIME_SIGNAL = 6;
52484
+ var readU163 = (bytes2, offset) => bytes2[offset] << 8 | bytes2[offset + 1];
52485
+ var readU323 = (bytes2, offset) => (bytes2[offset] << 24 | bytes2[offset + 1] << 16 | bytes2[offset + 2] << 8 | bytes2[offset + 3]) >>> 0;
52486
+ var read33Bits = (bytes2, offset, lowBitsOfFirstByte) => {
52487
+ const high = bytes2[offset] & (1 << lowBitsOfFirstByte) - 1;
52488
+ return high * 2 ** 32 + readU323(bytes2, offset + 1);
52489
+ };
52490
+ var readScte35Sections = (bytes2) => {
52491
+ const sections = [];
52492
+ let pmtPid = null;
52493
+ let scte35Pid = null;
52494
+ const assembly = /* @__PURE__ */ new Map();
52495
+ let start = 0;
52496
+ while (start < bytes2.length && bytes2[start] !== SYNC_BYTE) {
52497
+ start++;
52498
+ }
52499
+ for (let offset = start; offset + TS_PACKET_SIZE <= bytes2.length; offset += TS_PACKET_SIZE) {
52500
+ if (bytes2[offset] !== SYNC_BYTE) {
52501
+ break;
52502
+ }
52503
+ const pid = readU163(bytes2, offset + 1) & 8191;
52504
+ const payloadUnitStart = (bytes2[offset + 1] & 64) !== 0;
52505
+ const hasAdaptation = (bytes2[offset + 3] & 32) !== 0;
52506
+ const hasPayload = (bytes2[offset + 3] & 16) !== 0;
52507
+ if (!hasPayload) {
52508
+ continue;
52509
+ }
52510
+ let payloadStart = offset + 4;
52511
+ if (hasAdaptation) {
52512
+ payloadStart += 1 + bytes2[payloadStart];
52513
+ }
52514
+ if (payloadStart >= offset + TS_PACKET_SIZE) {
52515
+ continue;
52516
+ }
52517
+ if (pid === PAT_PID) {
52518
+ pmtPid ??= readPmtPidFromPat(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart);
52519
+ continue;
52520
+ }
52521
+ if (pmtPid !== null && pid === pmtPid) {
52522
+ scte35Pid ??= readScte35PidFromPmt(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart);
52523
+ continue;
52524
+ }
52525
+ if (scte35Pid === null || pid !== scte35Pid) {
52526
+ continue;
52527
+ }
52528
+ collectSection(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart, pid, assembly, sections);
52529
+ }
52530
+ return sections;
52531
+ };
52532
+ var sectionStart = (bytes2, payloadStart, payloadEnd) => {
52533
+ const pointerField = bytes2[payloadStart];
52534
+ const start = payloadStart + 1 + pointerField;
52535
+ return start < payloadEnd ? start : null;
52536
+ };
52537
+ var readPmtPidFromPat = (bytes2, payloadStart, payloadEnd, payloadUnitStart) => {
52538
+ if (!payloadUnitStart) {
52539
+ return null;
52540
+ }
52541
+ const start = sectionStart(bytes2, payloadStart, payloadEnd);
52542
+ if (start === null || start + 12 > payloadEnd) {
52543
+ return null;
52544
+ }
52545
+ for (let pos = start + 8; pos + 4 <= payloadEnd; pos += 4) {
52546
+ const programNumber = readU163(bytes2, pos);
52547
+ const pid = readU163(bytes2, pos + 2) & 8191;
52548
+ if (programNumber !== 0) {
52549
+ return pid;
52550
+ }
52551
+ }
52552
+ return null;
52553
+ };
52554
+ var readScte35PidFromPmt = (bytes2, payloadStart, payloadEnd, payloadUnitStart) => {
52555
+ if (!payloadUnitStart) {
52556
+ return null;
52557
+ }
52558
+ const start = sectionStart(bytes2, payloadStart, payloadEnd);
52559
+ if (start === null || start + 12 > payloadEnd) {
52560
+ return null;
52561
+ }
52562
+ const sectionLength = readU163(bytes2, start + 1) & 4095;
52563
+ const sectionEnd = Math.min(start + 3 + sectionLength, payloadEnd);
52564
+ const programInfoLength = readU163(bytes2, start + 10) & 4095;
52565
+ let pos = start + 12 + programInfoLength;
52566
+ while (pos + 5 <= sectionEnd - 4) {
52567
+ const streamType = bytes2[pos];
52568
+ const pid = readU163(bytes2, pos + 1) & 8191;
52569
+ const esInfoLength = readU163(bytes2, pos + 3) & 4095;
52570
+ const descriptorsEnd = Math.min(pos + 5 + esInfoLength, sectionEnd);
52571
+ if (streamType === SCTE35_STREAM_TYPE2) {
52572
+ for (let d = pos + 5; d + 2 <= descriptorsEnd; ) {
52573
+ const tag = bytes2[d];
52574
+ const length = bytes2[d + 1];
52575
+ if (tag === REGISTRATION_DESCRIPTOR_TAG && length >= 4 && d + 6 <= descriptorsEnd && readU323(bytes2, d + 2) === CUEI_FORMAT_IDENTIFIER2) {
52576
+ return pid;
52577
+ }
52578
+ d += 2 + length;
52579
+ }
52580
+ }
52581
+ pos = descriptorsEnd;
52582
+ }
52583
+ return null;
52584
+ };
52585
+ var collectSection = (bytes2, payloadStart, payloadEnd, payloadUnitStart, pid, assembly, out) => {
52586
+ let pos = payloadStart;
52587
+ if (payloadUnitStart) {
52588
+ const start = sectionStart(bytes2, payloadStart, payloadEnd);
52589
+ if (start === null) {
52590
+ return;
52591
+ }
52592
+ if (bytes2[start] !== SPLICE_INFO_TABLE_ID) {
52593
+ assembly.delete(pid);
52594
+ return;
52595
+ }
52596
+ if (start + 3 > payloadEnd) {
52597
+ return;
52598
+ }
52599
+ const sectionLength = readU163(bytes2, start + 1) & 4095;
52600
+ assembly.set(pid, { bytes: [], remaining: 3 + sectionLength });
52601
+ pos = start;
52602
+ }
52603
+ const pending = assembly.get(pid);
52604
+ if (!pending) {
52605
+ return;
52606
+ }
52607
+ const take = Math.min(pending.remaining, payloadEnd - pos);
52608
+ for (let i = 0; i < take; i++) {
52609
+ pending.bytes.push(bytes2[pos + i]);
52610
+ }
52611
+ pending.remaining -= take;
52612
+ if (pending.remaining === 0) {
52613
+ assembly.delete(pid);
52614
+ out.push(parseSpliceInfoSection(Uint8Array.from(pending.bytes)));
52615
+ }
52616
+ };
52617
+ var parseSpliceInfoSection = (section) => {
52618
+ if (section.length < 14) {
52619
+ throw new MalformedInputError("SCTE-35 section is too short to hold its header.");
52620
+ }
52621
+ if (section[0] !== SPLICE_INFO_TABLE_ID) {
52622
+ throw new MalformedInputError(
52623
+ `SCTE-35 section states table_id 0x${section[0].toString(16)}, expected 0xfc.`
52624
+ );
52625
+ }
52626
+ const ptsAdjustment = read33Bits(section, 4, 1);
52627
+ const encrypted = (section[4] & 128) !== 0;
52628
+ const commandLength = readU163(section, 11) & 4095;
52629
+ const commandType = section[13];
52630
+ let commandPos = 14;
52631
+ const wrap = (ticks) => ticks % TIMESTAMP_MODULUS / MPEG_TS_TIMESCALE;
52632
+ const noTime = { section, time: null, commandType, ptsAdjustment };
52633
+ if (encrypted || commandLength === 0) {
52634
+ return noTime;
52635
+ }
52636
+ if (commandType === TIME_SIGNAL) {
52637
+ if (commandPos >= section.length) {
52638
+ return noTime;
52639
+ }
52640
+ const timeSpecified2 = (section[commandPos] & 128) !== 0;
52641
+ if (!timeSpecified2 || commandPos + 5 > section.length) {
52642
+ return noTime;
52643
+ }
52644
+ return { section, time: wrap(ptsAdjustment + read33Bits(section, commandPos, 1)), commandType, ptsAdjustment };
52645
+ }
52646
+ if (commandType !== SPLICE_INSERT) {
52647
+ return noTime;
52648
+ }
52649
+ commandPos += 4;
52650
+ if (commandPos >= section.length) {
52651
+ return noTime;
52652
+ }
52653
+ if ((section[commandPos] & 128) !== 0) {
52654
+ return noTime;
52655
+ }
52656
+ commandPos += 1;
52657
+ if (commandPos >= section.length) {
52658
+ return noTime;
52659
+ }
52660
+ const flags = section[commandPos];
52661
+ const programSplice = (flags & PROGRAM_SPLICE_FLAG) !== 0;
52662
+ const spliceImmediate = (flags & SPLICE_IMMEDIATE_FLAG) !== 0;
52663
+ commandPos += 1;
52664
+ if (spliceImmediate || !programSplice) {
52665
+ return noTime;
52666
+ }
52667
+ if (commandPos >= section.length) {
52668
+ return noTime;
52669
+ }
52670
+ const timeSpecified = (section[commandPos] & 128) !== 0;
52671
+ if (!timeSpecified || commandPos + 5 > section.length) {
52672
+ return noTime;
52673
+ }
52674
+ return { section, time: wrap(ptsAdjustment + read33Bits(section, commandPos, 1)), commandType, ptsAdjustment };
52675
+ };
52676
+
52475
52677
  // src/segment-pipeline-muxer.ts
52476
52678
  var SUBTITLE_GROUP_ID = "subtitles";
52477
52679
  var segmentVideoMetadata = (videoTrack) => ({
@@ -52555,6 +52757,7 @@ ${cue.notes ?? ""}`;
52555
52757
  this.playlistDeclarations = [];
52556
52758
  this.subtitlePlaylists = [];
52557
52759
  this.subtitleBoundarySource = null;
52760
+ this.pendingScte35 = [];
52558
52761
  this.options = options;
52559
52762
  this.targetSegmentDuration = options.targetDuration ?? 2;
52560
52763
  this.singleFilePerPlaylist = options.singleFilePerPlaylist ?? false;
@@ -53428,6 +53631,14 @@ ${cue.notes ?? ""}`;
53428
53631
  partStarts,
53429
53632
  partEnds
53430
53633
  );
53634
+ const firstPacket = videoTrack?.packets[0] ?? audioTrack?.packets[0];
53635
+ if (firstPacket && Number.isFinite(maxEndTimestamp)) {
53636
+ this.flushScte35Into(
53637
+ muxer,
53638
+ this.toMediaTimestamp(firstPacket.timestamp),
53639
+ this.toMediaTimestamp(maxEndTimestamp)
53640
+ );
53641
+ }
53431
53642
  if (playlist.singleFile?.fragmentedIsobmffOutput) {
53432
53643
  segmentSize = await finalizeFragmentedIsobmffSegment(playlist);
53433
53644
  } else {
@@ -53857,6 +54068,47 @@ ${cue.notes ?? ""}`;
53857
54068
  }
53858
54069
  return packet.clone({ timestamp: this.toMediaTimestamp(packet.timestamp) });
53859
54070
  }
54071
+ /**
54072
+ * Hands every queued SCTE-35 section whose splice point this segment covers to the segment's own
54073
+ * muxer, so the section travels in the segment a receiver reaches it by. A section stating no time -
54074
+ * a `splice_null` heartbeat, a cancelled insert, an immediate splice - goes into the first segment
54075
+ * written after it was handed over, which is what "now" means to a packager.
54076
+ */
54077
+ flushScte35Into(muxer, start, end) {
54078
+ if (this.pendingScte35.length === 0) {
54079
+ return;
54080
+ }
54081
+ if (!(muxer instanceof MpegTsMuxer)) {
54082
+ this.pendingScte35 = [];
54083
+ return;
54084
+ }
54085
+ const kept = [];
54086
+ for (const pending of this.pendingScte35) {
54087
+ if (pending.mediaTime !== null && pending.mediaTime >= end) {
54088
+ kept.push(pending);
54089
+ continue;
54090
+ }
54091
+ if (pending.mediaTime !== null && pending.mediaTime < start - (end - start)) {
54092
+ Logging._warn(
54093
+ `An SCTE-35 section states a splice at ${pending.mediaTime}s, which is more than one segment behind the segment being written (${start}s to ${end}s). It is written here anyway. Splice times are on the output's own media timeline, which mediaTimestampBase shifts - a time on the input's timeline lands early by exactly that.`
54094
+ );
54095
+ }
54096
+ muxer.writeScte35Section(pending.section);
54097
+ }
54098
+ this.pendingScte35 = kept;
54099
+ }
54100
+ /**
54101
+ * Queues a `splice_info_section` for the segment covering its splice point.
54102
+ *
54103
+ * A segmented output has no one transport stream to write into - it opens a muxer per segment - so
54104
+ * the section waits here until the segment its time falls in is written.
54105
+ *
54106
+ * @internal
54107
+ */
54108
+ writeScte35Section(section) {
54109
+ const parsed = parseSpliceInfoSection(section);
54110
+ this.pendingScte35.push({ section, mediaTime: parsed.time });
54111
+ }
53860
54112
  toMediaTimestamp(timestamp) {
53861
54113
  assert(this.firstPacketTimestamp !== null);
53862
54114
  return timestamp - this.firstPacketTimestamp + this.mediaTimestampBase;
@@ -53886,6 +54138,12 @@ ${cue.notes ?? ""}`;
53886
54138
  }
53887
54139
  await Promise.all(this.playlists.map((playlist) => playlist.done || playlist.subtitleBoundaries ? Promise.resolve() : this.advancePlaylist(playlist)));
53888
54140
  await Promise.all(this.subtitlePlaylists.map((playlist) => playlist.done ? Promise.resolve() : this.advanceSubtitlePlaylist(playlist)));
54141
+ if (this.pendingScte35.length > 0) {
54142
+ Logging._warn(
54143
+ `${this.pendingScte35.length} SCTE-35 section(s) were never written: their splice times (${this.pendingScte35.map((p) => p.mediaTime).join(", ")}) fall past the end of the media this output wrote. Splice times are on the output's own media timeline, which mediaTimestampBase shifts.`
54144
+ );
54145
+ this.pendingScte35 = [];
54146
+ }
53889
54147
  await this.broadcast("onFinalize");
53890
54148
  }
53891
54149
  };
@@ -60138,7 +60396,7 @@ ${baseUrl}${p.getFileName()}`;
60138
60396
  mp4a: 28,
60139
60397
  enca: 28
60140
60398
  };
60141
- var readU323 = (bytes2, offset) => (bytes2[offset] << 24 | bytes2[offset + 1] << 16 | bytes2[offset + 2] << 8 | bytes2[offset + 3]) >>> 0;
60399
+ var readU324 = (bytes2, offset) => (bytes2[offset] << 24 | bytes2[offset + 1] << 16 | bytes2[offset + 2] << 8 | bytes2[offset + 3]) >>> 0;
60142
60400
  var writeU32 = (bytes2, offset, value) => {
60143
60401
  bytes2[offset] = value >>> 24 & 255;
60144
60402
  bytes2[offset + 1] = value >>> 16 & 255;
@@ -60171,7 +60429,7 @@ ${baseUrl}${p.getFileName()}`;
60171
60429
  var trafsOf = (moof2) => (moof2.children ?? []).filter((box2) => box2.type === "traf");
60172
60430
  var trunsOf = (traf2) => (traf2.children ?? []).filter((box2) => box2.type === "trun");
60173
60431
  var tfhdOf = (traf2) => (traf2.children ?? []).find((box2) => box2.type === "tfhd");
60174
- var trunDataOffset = (data) => readU323(data, TRUN_DATA_OFFSET_POSITION) | 0;
60432
+ var trunDataOffset = (data) => readU324(data, TRUN_DATA_OFFSET_POSITION) | 0;
60175
60433
  var defaultSampleSizeOf = (tfhd2) => {
60176
60434
  const flags = boxFlags(tfhd2);
60177
60435
  if (!(flags & TFHD_DEFAULT_SAMPLE_SIZE_PRESENT)) {
@@ -60187,7 +60445,7 @@ ${baseUrl}${p.getFileName()}`;
60187
60445
  if (flags & TFHD_DEFAULT_SAMPLE_DURATION_PRESENT) {
60188
60446
  offset += 4;
60189
60447
  }
60190
- return readU323(tfhd2, offset);
60448
+ return readU324(tfhd2, offset);
60191
60449
  };
60192
60450
  var trafDataSpan = (traf2) => {
60193
60451
  const tfhd2 = tfhdOf(traf2);
@@ -60204,14 +60462,14 @@ ${baseUrl}${p.getFileName()}`;
60204
60462
  cursor = trunDataOffset(data);
60205
60463
  }
60206
60464
  start = Math.min(start, cursor);
60207
- const count = readU323(data, 4);
60465
+ const count = readU324(data, 4);
60208
60466
  let offset = 8 + (flags & TRUN_DATA_OFFSET_PRESENT ? 4 : 0) + (flags & TRUN_FIRST_SAMPLE_FLAGS_PRESENT ? 4 : 0);
60209
60467
  for (let i = 0; i < count; i++) {
60210
60468
  if (flags & TRUN_SAMPLE_DURATION_PRESENT) {
60211
60469
  offset += 4;
60212
60470
  }
60213
60471
  if (flags & TRUN_SAMPLE_SIZE_PRESENT) {
60214
- cursor += readU323(data, offset);
60472
+ cursor += readU324(data, offset);
60215
60473
  offset += 4;
60216
60474
  } else {
60217
60475
  cursor += defaultSampleSize;
@@ -60245,7 +60503,7 @@ ${baseUrl}${p.getFileName()}`;
60245
60503
  base = previousEnd !== moofOffset && !fits(previousEnd) && fits(moofOffset) ? moofOffset : previousEnd;
60246
60504
  }
60247
60505
  resolved.push({
60248
- trackId: data === void 0 ? 0 : readU323(data, 4),
60506
+ trackId: data === void 0 ? 0 : readU324(data, 4),
60249
60507
  base,
60250
60508
  // Only a base that is the moof moves what its offsets point at when that moof grows
60251
60509
  followsMoof: base === moofOffset
@@ -60265,7 +60523,7 @@ ${baseUrl}${p.getFileName()}`;
60265
60523
  writeU32(
60266
60524
  trun2.data,
60267
60525
  TRUN_DATA_OFFSET_POSITION,
60268
- readU323(trun2.data, TRUN_DATA_OFFSET_POSITION) + delta
60526
+ readU324(trun2.data, TRUN_DATA_OFFSET_POSITION) + delta
60269
60527
  );
60270
60528
  }
60271
60529
  }
@@ -64568,7 +64826,7 @@ ${baseUrl}${p.getFileName()}`;
64568
64826
  var SENC_ENTRIES_OFFSET = 8 + 4 + 4;
64569
64827
  var parseTrun = (data, defaultSampleSize, defaultSampleDuration) => {
64570
64828
  const flags = boxFlags(data);
64571
- const sampleCount = readU323(data, 4);
64829
+ const sampleCount = readU324(data, 4);
64572
64830
  let offset = 8;
64573
64831
  let dataOffsetPos = -1;
64574
64832
  if (flags & 1) {
@@ -64583,13 +64841,13 @@ ${baseUrl}${p.getFileName()}`;
64583
64841
  for (let i = 0; i < sampleCount; i++) {
64584
64842
  let duration = defaultSampleDuration;
64585
64843
  if (flags & 256) {
64586
- duration = readU323(data, offset);
64844
+ duration = readU324(data, offset);
64587
64845
  offset += 4;
64588
64846
  }
64589
64847
  durations.push(duration);
64590
64848
  let size = defaultSampleSize;
64591
64849
  if (flags & 512) {
64592
- size = readU323(data, offset);
64850
+ size = readU324(data, offset);
64593
64851
  offset += 4;
64594
64852
  }
64595
64853
  sizes.push(size);
@@ -64641,8 +64899,8 @@ ${baseUrl}${p.getFileName()}`;
64641
64899
  var trexSampleDuration = (moov2, trackId) => {
64642
64900
  const mvex2 = (moov2.children ?? []).find((box2) => box2.type === "mvex");
64643
64901
  for (const trex2 of (mvex2?.children ?? []).filter((box2) => box2.type === "trex")) {
64644
- if (trex2.data !== void 0 && readU323(trex2.data, 4) === trackId) {
64645
- return readU323(trex2.data, 12);
64902
+ if (trex2.data !== void 0 && readU324(trex2.data, 4) === trackId) {
64903
+ return readU324(trex2.data, 12);
64646
64904
  }
64647
64905
  }
64648
64906
  return 0;
@@ -64660,7 +64918,7 @@ ${baseUrl}${p.getFileName()}`;
64660
64918
  if (tkhd2?.data === void 0 || sampleEntry === void 0) {
64661
64919
  continue;
64662
64920
  }
64663
- const trackId = readU323(tkhd2.data, tkhd2.data[0] === 1 ? 20 : 12);
64921
+ const trackId = readU324(tkhd2.data, tkhd2.data[0] === 1 ? 20 : 12);
64664
64922
  const defaultSampleDuration = trexSampleDuration(moov2, trackId);
64665
64923
  if (handlerType === "vide") {
64666
64924
  tracks.push({
@@ -64987,7 +65245,7 @@ ${baseUrl}${p.getFileName()}`;
64987
65245
  const referenceCount = view2.getUint16(offset + 2);
64988
65246
  offset += 4;
64989
65247
  for (let i = 0; i < referenceCount; i++) {
64990
- const raw = readU323(data, offset);
65248
+ const raw = readU324(data, offset);
64991
65249
  const next = cursor + (raw & 2147483647);
64992
65250
  if (next > endOfInput) {
64993
65251
  Logging._warn(
@@ -65081,7 +65339,7 @@ ${baseUrl}${p.getFileName()}`;
65081
65339
  };
65082
65340
  var findTrafByTrackId = (moof2, trackId) => (moof2.children ?? []).filter((b) => b.type === "traf").find((traf2) => {
65083
65341
  const tfhd2 = findBox([traf2], "tfhd");
65084
- return tfhd2?.data !== void 0 && readU323(tfhd2.data, 4) === trackId;
65342
+ return tfhd2?.data !== void 0 && readU324(tfhd2.data, 4) === trackId;
65085
65343
  });
65086
65344
  var defaultSampleSizeOf2 = (tfhd2) => {
65087
65345
  const flags = boxFlags(tfhd2.data);
@@ -65095,9 +65353,9 @@ ${baseUrl}${p.getFileName()}`;
65095
65353
  if (flags & 8) {
65096
65354
  offset += 4;
65097
65355
  }
65098
- return flags & 16 ? readU323(tfhd2.data, offset) : 0;
65356
+ return flags & 16 ? readU324(tfhd2.data, offset) : 0;
65099
65357
  };
65100
- var trunDataOffset2 = (trun2, position) => readU323(trun2.data, position) | 0;
65358
+ var trunDataOffset2 = (trun2, position) => readU324(trun2.data, position) | 0;
65101
65359
  var cmafEncryptedLayout = (init, segment, options) => {
65102
65360
  const scheme = options.scheme ?? "cbcs";
65103
65361
  const tracks = requireTracks(requireMoov(parseBoxes(init, 0, init.length)));
@@ -65213,7 +65471,7 @@ ${baseUrl}${p.getFileName()}`;
65213
65471
  for (const traf2 of (moof2.children ?? []).filter((b) => b.type === "traf")) {
65214
65472
  for (const trun2 of trunsOf(traf2)) {
65215
65473
  if (boxFlags(trun2.data) & TRUN_DATA_OFFSET_PRESENT) {
65216
- writeU32(trun2.data, TRUN_DATA_OFFSET_POSITION, readU323(trun2.data, TRUN_DATA_OFFSET_POSITION) + delta);
65474
+ writeU32(trun2.data, TRUN_DATA_OFFSET_POSITION, readU324(trun2.data, TRUN_DATA_OFFSET_POSITION) + delta);
65217
65475
  }
65218
65476
  }
65219
65477
  }
@@ -65235,9 +65493,9 @@ ${baseUrl}${p.getFileName()}`;
65235
65493
  var sampleSizes = (stbl2) => {
65236
65494
  const stsz2 = findBox([stbl2], "stsz");
65237
65495
  if (stsz2?.data !== void 0) {
65238
- const uniform = readU323(stsz2.data, 4);
65239
- const count2 = readU323(stsz2.data, 8);
65240
- return Array.from({ length: count2 }, (_, i) => uniform !== 0 ? uniform : readU323(stsz2.data, 12 + i * 4));
65496
+ const uniform = readU324(stsz2.data, 4);
65497
+ const count2 = readU324(stsz2.data, 8);
65498
+ return Array.from({ length: count2 }, (_, i) => uniform !== 0 ? uniform : readU324(stsz2.data, 12 + i * 4));
65241
65499
  }
65242
65500
  const stz2 = findBox([stbl2], "stz2");
65243
65501
  if (stz2?.data === void 0) {
@@ -65247,7 +65505,7 @@ ${baseUrl}${p.getFileName()}`;
65247
65505
  }
65248
65506
  const data = stz2.data;
65249
65507
  const fieldSize = data[7];
65250
- const count = readU323(data, 8);
65508
+ const count = readU324(data, 8);
65251
65509
  const view2 = new DataView(data.buffer, data.byteOffset, data.byteLength);
65252
65510
  if (fieldSize === 16) {
65253
65511
  return Array.from({ length: count }, (_, i) => view2.getUint16(12 + i * 2));
@@ -65272,12 +65530,12 @@ ${baseUrl}${p.getFileName()}`;
65272
65530
  "The sample table has no stsc, so its samples cannot be located in their chunks."
65273
65531
  );
65274
65532
  }
65275
- const entryCount = readU323(stsc2, 4);
65533
+ const entryCount = readU324(stsc2, 4);
65276
65534
  const counts = [];
65277
65535
  for (let i = 0; i < entryCount; i++) {
65278
- const firstChunk = readU323(stsc2, 8 + i * 12);
65279
- const perChunk = readU323(stsc2, 12 + i * 12);
65280
- const nextFirstChunk = i + 1 < entryCount ? readU323(stsc2, 8 + (i + 1) * 12) : chunkCount + 1;
65536
+ const firstChunk = readU324(stsc2, 8 + i * 12);
65537
+ const perChunk = readU324(stsc2, 12 + i * 12);
65538
+ const nextFirstChunk = i + 1 < entryCount ? readU324(stsc2, 8 + (i + 1) * 12) : chunkCount + 1;
65281
65539
  for (let chunk = firstChunk; chunk < nextFirstChunk; chunk++) {
65282
65540
  counts.push(perChunk);
65283
65541
  }
@@ -65295,13 +65553,13 @@ ${baseUrl}${p.getFileName()}`;
65295
65553
  }
65296
65554
  const wide = stco2 === null;
65297
65555
  const view2 = new DataView(box2.data.buffer, box2.data.byteOffset, box2.data.byteLength);
65298
- const count = readU323(box2.data, 4);
65556
+ const count = readU324(box2.data, 4);
65299
65557
  return {
65300
65558
  box: box2,
65301
65559
  wide,
65302
65560
  offsets: Array.from(
65303
65561
  { length: count },
65304
- (_, i) => wide ? Number(view2.getBigUint64(8 + i * 8)) : readU323(box2.data, 8 + i * 4)
65562
+ (_, i) => wide ? Number(view2.getBigUint64(8 + i * 8)) : readU324(box2.data, 8 + i * 4)
65305
65563
  )
65306
65564
  };
65307
65565
  };
@@ -67286,12 +67544,12 @@ ${baseUrl}${p.getFileName()}`;
67286
67544
  }
67287
67545
  const version = data[0];
67288
67546
  let position = 4 + (boxFlags(data) & 1 ? 8 : 0);
67289
- const entryCount = readU323(data, position);
67547
+ const entryCount = readU324(data, position);
67290
67548
  position += 4;
67291
67549
  const view2 = new DataView(data.buffer, data.byteOffset, data.byteLength);
67292
67550
  for (let i = 0; i < entryCount; i++) {
67293
67551
  if (version === 0) {
67294
- writeU32(data, position, readU323(data, position) + added);
67552
+ writeU32(data, position, readU324(data, position) + added);
67295
67553
  position += 4;
67296
67554
  } else {
67297
67555
  view2.setBigUint64(position, view2.getBigUint64(position) + BigInt(added));
@@ -67309,7 +67567,7 @@ ${baseUrl}${p.getFileName()}`;
67309
67567
  return;
67310
67568
  }
67311
67569
  const at = position + 2 + fragmentIndex * 12;
67312
- const word = readU323(bytes2, at);
67570
+ const word = readU324(bytes2, at);
67313
67571
  const grown = (word & 2147483647) + added;
67314
67572
  if (grown > 2147483647) {
67315
67573
  throw new InvalidUsageError("The grown fragment no longer fits a sidx referenced_size.");
@@ -67342,7 +67600,7 @@ ${baseUrl}${p.getFileName()}`;
67342
67600
  const children2 = parseBoxes(segment, box2.start + box2.headerSize, box2.start + box2.size);
67343
67601
  const found = children2.filter((child) => child.type === "traf").findIndex((traf3) => {
67344
67602
  const tfhd2 = tfhdOf(traf3);
67345
- return tfhd2?.data !== void 0 && readU323(tfhd2.data, 4) === trackId;
67603
+ return tfhd2?.data !== void 0 && readU324(tfhd2.data, 4) === trackId;
67346
67604
  });
67347
67605
  if (found !== -1) {
67348
67606
  moofStart = box2.start;
@@ -67360,7 +67618,7 @@ ${baseUrl}${p.getFileName()}`;
67360
67618
  const trafs = trafsOf(moof2);
67361
67619
  const traf2 = trafs.find((candidate) => {
67362
67620
  const tfhd2 = tfhdOf(candidate);
67363
- return tfhd2?.data !== void 0 && readU323(tfhd2.data, 4) === trackId;
67621
+ return tfhd2?.data !== void 0 && readU324(tfhd2.data, 4) === trackId;
67364
67622
  });
67365
67623
  const children = traf2.children ?? [];
67366
67624
  const lastTrun = children.map((box2) => box2.type).lastIndexOf("trun");
@@ -67371,7 +67629,7 @@ ${baseUrl}${p.getFileName()}`;
67371
67629
  const geometry = {
67372
67630
  moofOffset: moofStart,
67373
67631
  mdatDataStart: moofEnd + 8,
67374
- mdatLength: readU323(segment, moofEnd) - 8
67632
+ mdatLength: readU324(segment, moofEnd) - 8
67375
67633
  };
67376
67634
  const resolved = shiftTrunDataOffsets(moof2, added, geometry);
67377
67635
  for (let index = trafs.indexOf(traf2); index < trafs.length; index++) {
@@ -67393,208 +67651,6 @@ ${baseUrl}${p.getFileName()}`;
67393
67651
  return out;
67394
67652
  };
67395
67653
 
67396
- // src/mpeg-ts/scte35.ts
67397
- var SYNC_BYTE = 71;
67398
- var SPLICE_INFO_TABLE_ID = 252;
67399
- var CUEI_FORMAT_IDENTIFIER2 = 1129661769;
67400
- var SCTE35_STREAM_TYPE2 = 134;
67401
- var SPLICE_INSERT = 5;
67402
- var PROGRAM_SPLICE_FLAG = 64;
67403
- var SPLICE_IMMEDIATE_FLAG = 16;
67404
- var TIME_SIGNAL = 6;
67405
- var readU163 = (bytes2, offset) => bytes2[offset] << 8 | bytes2[offset + 1];
67406
- var readU324 = (bytes2, offset) => (bytes2[offset] << 24 | bytes2[offset + 1] << 16 | bytes2[offset + 2] << 8 | bytes2[offset + 3]) >>> 0;
67407
- var read33Bits = (bytes2, offset, lowBitsOfFirstByte) => {
67408
- const high = bytes2[offset] & (1 << lowBitsOfFirstByte) - 1;
67409
- return high * 2 ** 32 + readU324(bytes2, offset + 1);
67410
- };
67411
- var readScte35Sections = (bytes2) => {
67412
- const sections = [];
67413
- let pmtPid = null;
67414
- let scte35Pid = null;
67415
- const assembly = /* @__PURE__ */ new Map();
67416
- let start = 0;
67417
- while (start < bytes2.length && bytes2[start] !== SYNC_BYTE) {
67418
- start++;
67419
- }
67420
- for (let offset = start; offset + TS_PACKET_SIZE <= bytes2.length; offset += TS_PACKET_SIZE) {
67421
- if (bytes2[offset] !== SYNC_BYTE) {
67422
- break;
67423
- }
67424
- const pid = readU163(bytes2, offset + 1) & 8191;
67425
- const payloadUnitStart = (bytes2[offset + 1] & 64) !== 0;
67426
- const hasAdaptation = (bytes2[offset + 3] & 32) !== 0;
67427
- const hasPayload = (bytes2[offset + 3] & 16) !== 0;
67428
- if (!hasPayload) {
67429
- continue;
67430
- }
67431
- let payloadStart = offset + 4;
67432
- if (hasAdaptation) {
67433
- payloadStart += 1 + bytes2[payloadStart];
67434
- }
67435
- if (payloadStart >= offset + TS_PACKET_SIZE) {
67436
- continue;
67437
- }
67438
- if (pid === PAT_PID) {
67439
- pmtPid ??= readPmtPidFromPat(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart);
67440
- continue;
67441
- }
67442
- if (pmtPid !== null && pid === pmtPid) {
67443
- scte35Pid ??= readScte35PidFromPmt(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart);
67444
- continue;
67445
- }
67446
- if (scte35Pid === null || pid !== scte35Pid) {
67447
- continue;
67448
- }
67449
- collectSection(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart, pid, assembly, sections);
67450
- }
67451
- return sections;
67452
- };
67453
- var sectionStart = (bytes2, payloadStart, payloadEnd) => {
67454
- const pointerField = bytes2[payloadStart];
67455
- const start = payloadStart + 1 + pointerField;
67456
- return start < payloadEnd ? start : null;
67457
- };
67458
- var readPmtPidFromPat = (bytes2, payloadStart, payloadEnd, payloadUnitStart) => {
67459
- if (!payloadUnitStart) {
67460
- return null;
67461
- }
67462
- const start = sectionStart(bytes2, payloadStart, payloadEnd);
67463
- if (start === null || start + 12 > payloadEnd) {
67464
- return null;
67465
- }
67466
- for (let pos = start + 8; pos + 4 <= payloadEnd; pos += 4) {
67467
- const programNumber = readU163(bytes2, pos);
67468
- const pid = readU163(bytes2, pos + 2) & 8191;
67469
- if (programNumber !== 0) {
67470
- return pid;
67471
- }
67472
- }
67473
- return null;
67474
- };
67475
- var readScte35PidFromPmt = (bytes2, payloadStart, payloadEnd, payloadUnitStart) => {
67476
- if (!payloadUnitStart) {
67477
- return null;
67478
- }
67479
- const start = sectionStart(bytes2, payloadStart, payloadEnd);
67480
- if (start === null || start + 12 > payloadEnd) {
67481
- return null;
67482
- }
67483
- const sectionLength = readU163(bytes2, start + 1) & 4095;
67484
- const sectionEnd = Math.min(start + 3 + sectionLength, payloadEnd);
67485
- const programInfoLength = readU163(bytes2, start + 10) & 4095;
67486
- let pos = start + 12 + programInfoLength;
67487
- while (pos + 5 <= sectionEnd - 4) {
67488
- const streamType = bytes2[pos];
67489
- const pid = readU163(bytes2, pos + 1) & 8191;
67490
- const esInfoLength = readU163(bytes2, pos + 3) & 4095;
67491
- const descriptorsEnd = Math.min(pos + 5 + esInfoLength, sectionEnd);
67492
- if (streamType === SCTE35_STREAM_TYPE2) {
67493
- for (let d = pos + 5; d + 2 <= descriptorsEnd; ) {
67494
- const tag = bytes2[d];
67495
- const length = bytes2[d + 1];
67496
- if (tag === REGISTRATION_DESCRIPTOR_TAG && length >= 4 && d + 6 <= descriptorsEnd && readU324(bytes2, d + 2) === CUEI_FORMAT_IDENTIFIER2) {
67497
- return pid;
67498
- }
67499
- d += 2 + length;
67500
- }
67501
- }
67502
- pos = descriptorsEnd;
67503
- }
67504
- return null;
67505
- };
67506
- var collectSection = (bytes2, payloadStart, payloadEnd, payloadUnitStart, pid, assembly, out) => {
67507
- let pos = payloadStart;
67508
- if (payloadUnitStart) {
67509
- const start = sectionStart(bytes2, payloadStart, payloadEnd);
67510
- if (start === null) {
67511
- return;
67512
- }
67513
- if (bytes2[start] !== SPLICE_INFO_TABLE_ID) {
67514
- assembly.delete(pid);
67515
- return;
67516
- }
67517
- if (start + 3 > payloadEnd) {
67518
- return;
67519
- }
67520
- const sectionLength = readU163(bytes2, start + 1) & 4095;
67521
- assembly.set(pid, { bytes: [], remaining: 3 + sectionLength });
67522
- pos = start;
67523
- }
67524
- const pending = assembly.get(pid);
67525
- if (!pending) {
67526
- return;
67527
- }
67528
- const take = Math.min(pending.remaining, payloadEnd - pos);
67529
- for (let i = 0; i < take; i++) {
67530
- pending.bytes.push(bytes2[pos + i]);
67531
- }
67532
- pending.remaining -= take;
67533
- if (pending.remaining === 0) {
67534
- assembly.delete(pid);
67535
- out.push(parseSpliceInfoSection(Uint8Array.from(pending.bytes)));
67536
- }
67537
- };
67538
- var parseSpliceInfoSection = (section) => {
67539
- if (section.length < 14) {
67540
- throw new MalformedInputError("SCTE-35 section is too short to hold its header.");
67541
- }
67542
- if (section[0] !== SPLICE_INFO_TABLE_ID) {
67543
- throw new MalformedInputError(
67544
- `SCTE-35 section states table_id 0x${section[0].toString(16)}, expected 0xfc.`
67545
- );
67546
- }
67547
- const ptsAdjustment = read33Bits(section, 4, 1);
67548
- const encrypted = (section[4] & 128) !== 0;
67549
- const commandLength = readU163(section, 11) & 4095;
67550
- const commandType = section[13];
67551
- let commandPos = 14;
67552
- const wrap = (ticks) => ticks % TIMESTAMP_MODULUS / MPEG_TS_TIMESCALE;
67553
- const noTime = { section, time: null, commandType, ptsAdjustment };
67554
- if (encrypted || commandLength === 0) {
67555
- return noTime;
67556
- }
67557
- if (commandType === TIME_SIGNAL) {
67558
- if (commandPos >= section.length) {
67559
- return noTime;
67560
- }
67561
- const timeSpecified2 = (section[commandPos] & 128) !== 0;
67562
- if (!timeSpecified2 || commandPos + 5 > section.length) {
67563
- return noTime;
67564
- }
67565
- return { section, time: wrap(ptsAdjustment + read33Bits(section, commandPos, 1)), commandType, ptsAdjustment };
67566
- }
67567
- if (commandType !== SPLICE_INSERT) {
67568
- return noTime;
67569
- }
67570
- commandPos += 4;
67571
- if (commandPos >= section.length) {
67572
- return noTime;
67573
- }
67574
- if ((section[commandPos] & 128) !== 0) {
67575
- return noTime;
67576
- }
67577
- commandPos += 1;
67578
- if (commandPos >= section.length) {
67579
- return noTime;
67580
- }
67581
- const flags = section[commandPos];
67582
- const programSplice = (flags & PROGRAM_SPLICE_FLAG) !== 0;
67583
- const spliceImmediate = (flags & SPLICE_IMMEDIATE_FLAG) !== 0;
67584
- commandPos += 1;
67585
- if (spliceImmediate || !programSplice) {
67586
- return noTime;
67587
- }
67588
- if (commandPos >= section.length) {
67589
- return noTime;
67590
- }
67591
- const timeSpecified = (section[commandPos] & 128) !== 0;
67592
- if (!timeSpecified || commandPos + 5 > section.length) {
67593
- return noTime;
67594
- }
67595
- return { section, time: wrap(ptsAdjustment + read33Bits(section, commandPos, 1)), commandType, ptsAdjustment };
67596
- };
67597
-
67598
67654
  // src/mpeg-ts/private-streams.ts
67599
67655
  var SYNC_BYTE2 = 71;
67600
67656
  var TELETEXT_DESCRIPTOR_TAG = 86;
@@ -67794,8 +67850,14 @@ ${baseUrl}${p.getFileName()}`;
67794
67850
  throw new InvalidUsageError("A splice_info_section is at least three bytes.");
67795
67851
  }
67796
67852
  const muxer = output._muxer;
67853
+ if (muxer instanceof SegmentPipelineMuxer) {
67854
+ muxer.writeScte35Section(section);
67855
+ return;
67856
+ }
67797
67857
  if (!(muxer instanceof MpegTsMuxer)) {
67798
- throw new InvalidUsageError("SCTE-35 sections can only be written to an MpegTsOutputFormat output.");
67858
+ throw new InvalidUsageError(
67859
+ "SCTE-35 sections can only be written to an MpegTsOutputFormat output, or to a segmented one whose segmentFormat is MpegTsOutputFormat."
67860
+ );
67799
67861
  }
67800
67862
  muxer.writeScte35Section(section);
67801
67863
  };