@ahmedrowaihi/mediabunny 1.65.2-beta.548 → 1.65.2-beta.554

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 (34) hide show
  1. package/dist/bundles/mediabunny.cjs +289 -46
  2. package/dist/bundles/mediabunny.min.cjs +39 -39
  3. package/dist/bundles/mediabunny.min.mjs +39 -39
  4. package/dist/bundles/mediabunny.mjs +289 -46
  5. package/dist/bundles/mediabunny.node.cjs +289 -46
  6. package/dist/mediabunny.d.ts +140 -0
  7. package/dist/modules/src/codec-data.d.ts +2 -0
  8. package/dist/modules/src/codec-data.d.ts.map +1 -1
  9. package/dist/modules/src/codec-data.js +2 -0
  10. package/dist/modules/src/crypto/h264-slice-header-parser.js +2 -2
  11. package/dist/modules/src/index.d.ts +2 -0
  12. package/dist/modules/src/index.d.ts.map +1 -1
  13. package/dist/modules/src/index.js +2 -0
  14. package/dist/modules/src/mpeg-ts/mpeg-ts-muxer.d.ts +9 -0
  15. package/dist/modules/src/mpeg-ts/mpeg-ts-muxer.d.ts.map +1 -1
  16. package/dist/modules/src/mpeg-ts/mpeg-ts-muxer.js +55 -4
  17. package/dist/modules/src/mpeg-ts/private-streams.d.ts +103 -0
  18. package/dist/modules/src/mpeg-ts/private-streams.d.ts.map +1 -0
  19. package/dist/modules/src/mpeg-ts/private-streams.js +221 -0
  20. package/dist/modules/src/mpeg-ts/scte35-output.d.ts +39 -0
  21. package/dist/modules/src/mpeg-ts/scte35-output.d.ts.map +1 -0
  22. package/dist/modules/src/mpeg-ts/scte35-output.js +52 -0
  23. package/dist/modules/src/output-format.d.ts +8 -0
  24. package/dist/modules/src/output-format.d.ts.map +1 -1
  25. package/dist/modules/src/output-format.js +5 -0
  26. package/dist/modules/src/tsconfig.tsbuildinfo +1 -1
  27. package/package.json +1 -1
  28. package/src/codec-data.ts +3 -0
  29. package/src/crypto/h264-slice-header-parser.ts +2 -2
  30. package/src/index.ts +9 -0
  31. package/src/mpeg-ts/mpeg-ts-muxer.ts +66 -3
  32. package/src/mpeg-ts/private-streams.ts +339 -0
  33. package/src/mpeg-ts/scte35-output.ts +57 -0
  34. package/src/output-format.ts +16 -0
@@ -300,6 +300,7 @@ var Mediabunny = (() => {
300
300
  WebMOutputFormat: () => WebMOutputFormat,
301
301
  XmlNode: () => XmlNode,
302
302
  addContentProtectionElements: () => addContentProtectionElements,
303
+ addScte35Section: () => addScte35Section,
303
304
  adjustHlsVideoCodec: () => adjustHlsVideoCodec,
304
305
  asc: () => asc,
305
306
  buildCbcsContentProtections: () => buildCbcsContentProtections,
@@ -460,6 +461,8 @@ var Mediabunny = (() => {
460
461
  readAllWmPaceInfo: () => readAllWmPaceInfo,
461
462
  readEmsgs: () => readEmsgs,
462
463
  readMpdThumbnailTile: () => readMpdThumbnailTile,
464
+ readMpegTsPrivateStreams: () => readMpegTsPrivateStreams,
465
+ readPrivatePesPayloads: () => readPrivatePesPayloads,
463
466
  readScte35Sections: () => readScte35Sections,
464
467
  readSegmentHead: () => readSegmentHead,
465
468
  readSegmentWmPaceInfo: () => readSegmentWmPaceInfo,
@@ -1975,11 +1978,11 @@ var Mediabunny = (() => {
1975
1978
  }
1976
1979
  return { mainLanguage: tag.slice(0, dash), subtag: tag.slice(dash) };
1977
1980
  };
1978
- var languageToShortestForm = (language) => {
1979
- if (language.length === 0) {
1980
- return language;
1981
+ var languageToShortestForm = (language2) => {
1982
+ if (language2.length === 0) {
1983
+ return language2;
1981
1984
  }
1982
- const { mainLanguage, subtag } = splitLanguageTag(language);
1985
+ const { mainLanguage, subtag } = splitLanguageTag(language2);
1983
1986
  if (mainLanguage.length === 2) {
1984
1987
  return mainLanguage + subtag;
1985
1988
  }
@@ -1990,8 +1993,8 @@ var Mediabunny = (() => {
1990
1993
  }
1991
1994
  return mainLanguage + subtag;
1992
1995
  };
1993
- var languageToISO6392 = (language) => {
1994
- const { mainLanguage, subtag } = splitLanguageTag(language);
1996
+ var languageToISO6392 = (language2) => {
1997
+ const { mainLanguage, subtag } = splitLanguageTag(language2);
1995
1998
  if (mainLanguage.length === 3) {
1996
1999
  return mainLanguage + subtag;
1997
2000
  }
@@ -5263,6 +5266,7 @@ var Mediabunny = (() => {
5263
5266
  var AC3_SAMPLES_PER_FRAME = 1536;
5264
5267
  var AC3_REGISTRATION_DESCRIPTOR = new Uint8Array([5, 4, 65, 67, 45, 51]);
5265
5268
  var EAC3_REGISTRATION_DESCRIPTOR = new Uint8Array([5, 4, 69, 65, 67, 51]);
5269
+ var CUEI_REGISTRATION_DESCRIPTOR = new Uint8Array([5, 4, 67, 85, 69, 73]);
5266
5270
  var EAC3_NUMBLKS_TABLE = [1, 2, 3, 6];
5267
5271
  var parseEac3SyncFrame = (data) => {
5268
5272
  if (data.length < 6) {
@@ -8318,12 +8322,12 @@ var Mediabunny = (() => {
8318
8322
  if (track.timescale === 0) {
8319
8323
  throw new Error("Invalid mdhd box: timescale must not be 0.");
8320
8324
  }
8321
- let language = readU16Be(slice);
8322
- if (language > 0) {
8325
+ let language2 = readU16Be(slice);
8326
+ if (language2 > 0) {
8323
8327
  track.languageCode = "";
8324
8328
  for (let i = 0; i < 3; i++) {
8325
- track.languageCode = String.fromCharCode(96 + (language & 31)) + track.languageCode;
8326
- language >>= 5;
8329
+ track.languageCode = String.fromCharCode(96 + (language2 & 31)) + track.languageCode;
8330
+ language2 >>= 5;
8327
8331
  }
8328
8332
  if (!isIso639Dash2LanguageCode(track.languageCode)) {
8329
8333
  track.languageCode = UNDETERMINED_LANGUAGE;
@@ -41369,12 +41373,12 @@ var Mediabunny = (() => {
41369
41373
  var getLanguageCodeInt = (code) => {
41370
41374
  assert(code.length === 3);
41371
41375
  ;
41372
- let language = 0;
41376
+ let language2 = 0;
41373
41377
  for (let i = 0; i < 3; i++) {
41374
- language <<= 5;
41375
- language += code.charCodeAt(i) - 96;
41378
+ language2 <<= 5;
41379
+ language2 += code.charCodeAt(i) - 96;
41376
41380
  }
41377
- return language;
41381
+ return language2;
41378
41382
  };
41379
41383
 
41380
41384
  // src/writer.ts
@@ -45270,6 +45274,7 @@ ${cue.notes ?? ""}`;
45270
45274
  // src/mpeg-ts/mpeg-ts-muxer.ts
45271
45275
  var PAT_PID2 = 0;
45272
45276
  var PMT_PID = 4096;
45277
+ var SCTE35_STREAM_TYPE = 134;
45273
45278
  var FIRST_TRACK_PID = 256;
45274
45279
  var VIDEO_STREAM_ID_BASE = 224;
45275
45280
  var AUDIO_STREAM_ID_BASE = 192;
@@ -45282,6 +45287,11 @@ ${cue.notes ?? ""}`;
45282
45287
  // offset leaking into the next shifts a programme that needed no shift at all.
45283
45288
  this.defaultTimestampOffset = null;
45284
45289
  this.trackDatas = [];
45290
+ /**
45291
+ * Sections handed over before the tables went out. A track's data exists only once its first packet
45292
+ * is added, so writing the tables early would publish a PMT that names the cue stream and no media.
45293
+ */
45294
+ this.pendingScte35Sections = [];
45285
45295
  this.tablesWritten = false;
45286
45296
  this.continuityCounters = /* @__PURE__ */ new Map();
45287
45297
  this.packetBuffer = new Uint8Array(TS_PACKET_SIZE);
@@ -45295,6 +45305,7 @@ ${cue.notes ?? ""}`;
45295
45305
  this.format = format;
45296
45306
  this.timestampOffset = format._options.timestampOffset ?? 0;
45297
45307
  this.offsetIsStated = format._options.timestampOffset !== void 0;
45308
+ this.scte35Pid = format._options.scte35Pid ?? null;
45298
45309
  }
45299
45310
  get maxInterleaveDelta() {
45300
45311
  return this.format._options.maxInterleaveDelta ?? DEFAULT_MAX_INTERLEAVE_DELTA;
@@ -45695,8 +45706,36 @@ ${cue.notes ?? ""}`;
45695
45706
  assert(!this.tablesWritten);
45696
45707
  this.writePsiSection(PAT_PID2, PAT_SECTION);
45697
45708
  this.pcrPid = pcrTrackOf(this.trackDatas)?.pid ?? null;
45698
- this.writePsiSection(PMT_PID, buildPmt(this.trackDatas));
45709
+ this.writePsiSection(PMT_PID, buildPmt(this.trackDatas, this.scte35Pid));
45699
45710
  this.tablesWritten = true;
45711
+ this.flushScte35Sections();
45712
+ }
45713
+ /**
45714
+ * Writes a `splice_info_section` into the declared SCTE-35 PID, at the current point in the stream.
45715
+ *
45716
+ * The tables go first when they have not been written yet: a section on a PID the PMT has not named
45717
+ * is one a receiver has no reason to look at.
45718
+ *
45719
+ * @internal
45720
+ */
45721
+ writeScte35Section(section) {
45722
+ if (this.scte35Pid === null) {
45723
+ throw new InvalidUsageError(
45724
+ "MpegTsOutputFormat was not given an scte35Pid, so there is no cue stream to write into."
45725
+ );
45726
+ }
45727
+ this.pendingScte35Sections.push(section);
45728
+ this.flushScte35Sections();
45729
+ }
45730
+ /** Writes whatever is queued, once the tables exist for it to belong to. */
45731
+ flushScte35Sections() {
45732
+ if (!this.tablesWritten || this.scte35Pid === null) {
45733
+ return;
45734
+ }
45735
+ for (const section of this.pendingScte35Sections) {
45736
+ this.writePsiSection(this.scte35Pid, section);
45737
+ }
45738
+ this.pendingScte35Sections.length = 0;
45700
45739
  }
45701
45740
  writePsiSection(pid, section) {
45702
45741
  let offset = 0;
@@ -45913,7 +45952,7 @@ ${cue.notes ?? ""}`;
45913
45952
  buf[offset + 4] = base90kHz % 2 << 7 | 126;
45914
45953
  buf[offset + 5] = 0;
45915
45954
  };
45916
- var buildPmt = (trackDatas) => {
45955
+ var buildPmt = (trackDatas, scte35Pid) => {
45917
45956
  const sampleAesDescriptors = /* @__PURE__ */ new Map();
45918
45957
  let totalEsBytes = 0;
45919
45958
  for (const trackData of trackDatas) {
@@ -45927,6 +45966,9 @@ ${cue.notes ?? ""}`;
45927
45966
  totalEsBytes += EAC3_REGISTRATION_DESCRIPTOR.length;
45928
45967
  }
45929
45968
  }
45969
+ if (scte35Pid !== null) {
45970
+ totalEsBytes += 5 + CUEI_REGISTRATION_DESCRIPTOR.length;
45971
+ }
45930
45972
  const sectionLength = 9 + totalEsBytes + 4;
45931
45973
  const section = new Uint8Array(3 + sectionLength - 4);
45932
45974
  const view2 = toDataView(section);
@@ -45968,6 +46010,15 @@ ${cue.notes ?? ""}`;
45968
46010
  offset += 2;
45969
46011
  }
45970
46012
  }
46013
+ if (scte35Pid !== null) {
46014
+ section[offset++] = SCTE35_STREAM_TYPE;
46015
+ view2.setUint16(offset, 57344 | scte35Pid & 8191, false);
46016
+ offset += 2;
46017
+ view2.setUint16(offset, 61440 | CUEI_REGISTRATION_DESCRIPTOR.length, false);
46018
+ offset += 2;
46019
+ section.set(CUEI_REGISTRATION_DESCRIPTOR, offset);
46020
+ offset += CUEI_REGISTRATION_DESCRIPTOR.length;
46021
+ }
45971
46022
  const crc = computeMpegTsCrc32(section);
45972
46023
  const result = new Uint8Array(section.length + 4);
45973
46024
  result.set(section, 0);
@@ -48851,7 +48902,7 @@ ${cue.notes ?? ""}`;
48851
48902
  return uuids;
48852
48903
  };
48853
48904
  var AdaptationSet = class {
48854
- constructor(language, mpdOptions, representationCounter) {
48905
+ constructor(language2, mpdOptions, representationCounter) {
48855
48906
  /** @internal */
48856
48907
  this.contentProtectionElements = [];
48857
48908
  /** @internal */
@@ -48896,7 +48947,7 @@ ${cue.notes ?? ""}`;
48896
48947
  this.label = "";
48897
48948
  /** @internal */
48898
48949
  this.protectedContentValue = null;
48899
- this.languageValue = language;
48950
+ this.languageValue = language2;
48900
48951
  this.mpdOptions = mpdOptions;
48901
48952
  this.representationCounter = representationCounter;
48902
48953
  }
@@ -49511,10 +49562,10 @@ ${cue.notes ?? ""}`;
49511
49562
  return set;
49512
49563
  }
49513
49564
  }
49514
- const language = getLanguage(mediaInfo);
49515
- const newSet = this.newAdaptationSet(language);
49565
+ const language2 = getLanguage(mediaInfo);
49566
+ const newSet = this.newAdaptationSet(language2);
49516
49567
  if (!this.setNewAdaptationSetAttributes(
49517
- language,
49568
+ language2,
49518
49569
  mediaInfo,
49519
49570
  adaptationSetsInBucket,
49520
49571
  contentProtectionInAdaptationSet,
@@ -49613,11 +49664,11 @@ ${cue.notes ?? ""}`;
49613
49664
  return this.trickplayCacheMap;
49614
49665
  }
49615
49666
  /** @internal */
49616
- newAdaptationSet(language) {
49617
- return new AdaptationSet(language, this.mpdOptions, this.representationCounter);
49667
+ newAdaptationSet(language2) {
49668
+ return new AdaptationSet(language2, this.mpdOptions, this.representationCounter);
49618
49669
  }
49619
49670
  /** @internal */
49620
- setNewAdaptationSetAttributes(language, mediaInfo, adaptationSets, contentProtectionInAdaptationSet, newAdaptationSet) {
49671
+ setNewAdaptationSetAttributes(language2, mediaInfo, adaptationSets, contentProtectionInAdaptationSet, newAdaptationSet) {
49621
49672
  if (mediaInfo.dashRoles && mediaInfo.dashRoles.length > 0) {
49622
49673
  for (const roleStr of mediaInfo.dashRoles) {
49623
49674
  const role = roleFromString(roleStr);
@@ -49626,8 +49677,8 @@ ${cue.notes ?? ""}`;
49626
49677
  }
49627
49678
  newAdaptationSet.addRole(role);
49628
49679
  }
49629
- } else if (language.length > 0) {
49630
- const isMainRole = language === (mediaInfo.audioInfo ? getDefaultAudioLanguage(this.mpdOptions) : getDefaultTextLanguage(this.mpdOptions));
49680
+ } else if (language2.length > 0) {
49681
+ const isMainRole = language2 === (mediaInfo.audioInfo ? getDefaultAudioLanguage(this.mpdOptions) : getDefaultTextLanguage(this.mpdOptions));
49631
49682
  if (isMainRole) {
49632
49683
  newAdaptationSet.addRole("main");
49633
49684
  }
@@ -50396,9 +50447,9 @@ ${cue.notes ?? ""}`;
50396
50447
  if (track.metadata.disposition?.forced) {
50397
50448
  textInfo.forced = true;
50398
50449
  }
50399
- const language = track.metadata.languageCode;
50400
- if (language !== void 0) {
50401
- textInfo.language = language;
50450
+ const language2 = track.metadata.languageCode;
50451
+ if (language2 !== void 0) {
50452
+ textInfo.language = language2;
50402
50453
  }
50403
50454
  return textInfo;
50404
50455
  };
@@ -50465,9 +50516,9 @@ ${cue.notes ?? ""}`;
50465
50516
  numChannels: config.numberOfChannels,
50466
50517
  timeScale: config.sampleRate
50467
50518
  };
50468
- const language = track.metadata.languageCode;
50469
- if (language !== void 0) {
50470
- audioInfo.language = language;
50519
+ const language2 = track.metadata.languageCode;
50520
+ if (language2 !== void 0) {
50521
+ audioInfo.language = language2;
50471
50522
  }
50472
50523
  return audioInfo;
50473
50524
  };
@@ -52977,6 +53028,9 @@ ${cue.text}
52977
53028
  if (options.timestampOffset !== void 0 && (!Number.isFinite(options.timestampOffset) || options.timestampOffset < 0)) {
52978
53029
  throw new TypeError("options.timestampOffset, when provided, must be a non-negative number.");
52979
53030
  }
53031
+ if (options.scte35Pid !== void 0 && (!Number.isInteger(options.scte35Pid) || options.scte35Pid < 16 || options.scte35Pid > 8190)) {
53032
+ throw new TypeError("options.scte35Pid, when provided, must be an integer between 0x0010 and 0x1ffe.");
53033
+ }
52980
53034
  if (options.maxInterleaveDelta !== void 0 && !(options.maxInterleaveDelta > 0)) {
52981
53035
  throw new TypeError("options.maxInterleaveDelta, when provided, must be a positive number.");
52982
53036
  }
@@ -57764,9 +57818,9 @@ ${this.fileName}`;
57764
57818
  }
57765
57819
  tag.addQuotedString("URI", baseUrl + p.getFileName());
57766
57820
  tag.addQuotedString("GROUP-ID", groupIdOverride);
57767
- const language = p.getLanguage();
57768
- if (language) {
57769
- tag.addQuotedString("LANGUAGE", language);
57821
+ const language2 = p.getLanguage();
57822
+ if (language2) {
57823
+ tag.addQuotedString("LANGUAGE", language2);
57770
57824
  }
57771
57825
  tag.addQuotedString("NAME", p.getName());
57772
57826
  tag.addString("DEFAULT", isDefault ? "YES" : "NO");
@@ -57988,30 +58042,30 @@ ${baseUrl}${p.getFileName()}`;
57988
58042
  t.isAutoselect = true;
57989
58043
  continue;
57990
58044
  }
57991
- const language = t.playlist.getLanguage();
58045
+ const language2 = t.playlist.getLanguage();
57992
58046
  let languages = audioGroupLanguages.get(t.groupId);
57993
58047
  if (!languages) {
57994
58048
  languages = /* @__PURE__ */ new Set();
57995
58049
  audioGroupLanguages.set(t.groupId, languages);
57996
58050
  }
57997
- if (!languages.has(language)) {
57998
- t.isDefault = !!language && language === this.options.defaultAudioLanguage;
58051
+ if (!languages.has(language2)) {
58052
+ t.isDefault = !!language2 && language2 === this.options.defaultAudioLanguage;
57999
58053
  t.isAutoselect = true;
58000
- languages.add(language);
58054
+ languages.add(language2);
58001
58055
  }
58002
58056
  }
58003
58057
  const subtitleGroupLanguages = /* @__PURE__ */ new Map();
58004
58058
  for (const t of subtitleTags) {
58005
- const language = t.playlist.getLanguage();
58059
+ const language2 = t.playlist.getLanguage();
58006
58060
  let languages = subtitleGroupLanguages.get(t.groupId);
58007
58061
  if (!languages) {
58008
58062
  languages = /* @__PURE__ */ new Set();
58009
58063
  subtitleGroupLanguages.set(t.groupId, languages);
58010
58064
  }
58011
- if (!languages.has(language)) {
58012
- t.isDefault = !!language && language === this.options.defaultSubtitleLanguage;
58065
+ if (!languages.has(language2)) {
58066
+ t.isDefault = !!language2 && language2 === this.options.defaultSubtitleLanguage;
58013
58067
  t.isAutoselect = true;
58014
- languages.add(language);
58068
+ languages.add(language2);
58015
58069
  }
58016
58070
  if (t.playlist.isForcedSubtitle()) {
58017
58071
  t.isAutoselect = true;
@@ -61879,7 +61933,7 @@ ${baseUrl}${p.getFileName()}`;
61879
61933
  if (!sps.frameMbsOnlyFlag) {
61880
61934
  fieldPicFlag = bs.readBits(1) === 1;
61881
61935
  if (fieldPicFlag) {
61882
- throw new UnsupportedMediaError("Field pictures are not supported");
61936
+ bs.skipBits(1);
61883
61937
  }
61884
61938
  }
61885
61939
  if (idrPicFlag) {
@@ -65557,7 +65611,7 @@ ${baseUrl}${p.getFileName()}`;
65557
65611
  var SYNC_BYTE = 71;
65558
65612
  var SPLICE_INFO_TABLE_ID = 252;
65559
65613
  var CUEI_FORMAT_IDENTIFIER = 1129661769;
65560
- var SCTE35_STREAM_TYPE = 134;
65614
+ var SCTE35_STREAM_TYPE2 = 134;
65561
65615
  var SPLICE_INSERT = 5;
65562
65616
  var PROGRAM_SPLICE_FLAG = 64;
65563
65617
  var SPLICE_IMMEDIATE_FLAG = 16;
@@ -65649,7 +65703,7 @@ ${baseUrl}${p.getFileName()}`;
65649
65703
  const pid = readU163(bytes2, pos + 1) & 8191;
65650
65704
  const esInfoLength = readU163(bytes2, pos + 3) & 4095;
65651
65705
  const descriptorsEnd = Math.min(pos + 5 + esInfoLength, sectionEnd);
65652
- if (streamType === SCTE35_STREAM_TYPE) {
65706
+ if (streamType === SCTE35_STREAM_TYPE2) {
65653
65707
  for (let d = pos + 5; d + 2 <= descriptorsEnd; ) {
65654
65708
  const tag = bytes2[d];
65655
65709
  const length = bytes2[d + 1];
@@ -65755,6 +65809,195 @@ ${baseUrl}${p.getFileName()}`;
65755
65809
  return { section, time: wrap(ptsAdjustment + read33Bits(section, commandPos, 1)), commandType, ptsAdjustment };
65756
65810
  };
65757
65811
 
65812
+ // src/mpeg-ts/private-streams.ts
65813
+ var SYNC_BYTE2 = 71;
65814
+ var TELETEXT_DESCRIPTOR_TAG = 86;
65815
+ var SUBTITLING_DESCRIPTOR_TAG = 89;
65816
+ var PRIVATE_DATA_STREAM_TYPE = 6;
65817
+ var TELETEXT_ENTRY_SIZE = 5;
65818
+ var SUBTITLING_ENTRY_SIZE = 8;
65819
+ var readU164 = (bytes2, offset) => bytes2[offset] << 8 | bytes2[offset + 1];
65820
+ var language = (bytes2, offset) => String.fromCharCode(bytes2[offset], bytes2[offset + 1], bytes2[offset + 2]);
65821
+ var iteratePackets = function* (bytes2) {
65822
+ for (let offset = 0; offset + TS_PACKET_SIZE <= bytes2.length; offset += TS_PACKET_SIZE) {
65823
+ if (bytes2[offset] !== SYNC_BYTE2) {
65824
+ continue;
65825
+ }
65826
+ const pid = readU164(bytes2, offset + 1) & 8191;
65827
+ const payloadUnitStart = (bytes2[offset + 1] & 64) !== 0;
65828
+ const adaptationFieldControl = bytes2[offset + 3] >> 4 & 3;
65829
+ if ((adaptationFieldControl & 1) === 0) {
65830
+ continue;
65831
+ }
65832
+ let payloadStart = offset + 4;
65833
+ if (adaptationFieldControl === 3) {
65834
+ payloadStart += 1 + bytes2[offset + 4];
65835
+ }
65836
+ if (payloadStart >= offset + TS_PACKET_SIZE) {
65837
+ continue;
65838
+ }
65839
+ yield { pid, payloadUnitStart, payloadStart, payloadEnd: offset + TS_PACKET_SIZE };
65840
+ }
65841
+ };
65842
+ var sectionStart2 = (bytes2, payloadStart, payloadEnd) => {
65843
+ const pointer = bytes2[payloadStart];
65844
+ const start = payloadStart + 1 + pointer;
65845
+ return start < payloadEnd ? start : null;
65846
+ };
65847
+ var readTeletextEntries = (bytes2, start, end) => {
65848
+ const pages = [];
65849
+ for (let at = start; at + TELETEXT_ENTRY_SIZE <= end; at += TELETEXT_ENTRY_SIZE) {
65850
+ const packed = bytes2[at + 3];
65851
+ const magazine = packed & 7;
65852
+ const page = bytes2[at + 4];
65853
+ pages.push({
65854
+ language: language(bytes2, at),
65855
+ type: packed >> 3 & 31,
65856
+ magazine,
65857
+ // Two hex digits for the page, and magazine 0 means 8, so 888 reads back as 888
65858
+ page: (magazine === 0 ? 8 : magazine) * 100 + (page >> 4) * 10 + (page & 15)
65859
+ });
65860
+ }
65861
+ return pages;
65862
+ };
65863
+ var readSubtitlingEntries = (bytes2, start, end) => {
65864
+ const streams = [];
65865
+ for (let at = start; at + SUBTITLING_ENTRY_SIZE <= end; at += SUBTITLING_ENTRY_SIZE) {
65866
+ streams.push({
65867
+ language: language(bytes2, at),
65868
+ type: bytes2[at + 3],
65869
+ compositionPageId: readU164(bytes2, at + 4),
65870
+ ancillaryPageId: readU164(bytes2, at + 6)
65871
+ });
65872
+ }
65873
+ return streams;
65874
+ };
65875
+ var pmtPidFromPat = (bytes2, packet) => {
65876
+ const start = sectionStart2(bytes2, packet.payloadStart, packet.payloadEnd);
65877
+ if (start === null || start + 12 > packet.payloadEnd) {
65878
+ return null;
65879
+ }
65880
+ for (let pos = start + 8; pos + 4 <= packet.payloadEnd; pos += 4) {
65881
+ if (readU164(bytes2, pos) !== 0) {
65882
+ return readU164(bytes2, pos + 2) & 8191;
65883
+ }
65884
+ }
65885
+ return null;
65886
+ };
65887
+ var privateStreamsFromPmt = (bytes2, packet) => {
65888
+ const start = sectionStart2(bytes2, packet.payloadStart, packet.payloadEnd);
65889
+ if (start === null || start + 12 > packet.payloadEnd) {
65890
+ return [];
65891
+ }
65892
+ const sectionLength = readU164(bytes2, start + 1) & 4095;
65893
+ const sectionEnd = Math.min(start + 3 + sectionLength, packet.payloadEnd);
65894
+ const programInfoLength = readU164(bytes2, start + 10) & 4095;
65895
+ const out = [];
65896
+ let pos = start + 12 + programInfoLength;
65897
+ while (pos + 5 <= sectionEnd - 4) {
65898
+ const streamType = bytes2[pos];
65899
+ const pid = readU164(bytes2, pos + 1) & 8191;
65900
+ const esInfoLength = readU164(bytes2, pos + 3) & 4095;
65901
+ const descriptorsEnd = Math.min(pos + 5 + esInfoLength, sectionEnd);
65902
+ if (streamType === PRIVATE_DATA_STREAM_TYPE) {
65903
+ const stream = { pid, streamType };
65904
+ for (let d = pos + 5; d + 2 <= descriptorsEnd; ) {
65905
+ const tag = bytes2[d];
65906
+ const length = bytes2[d + 1];
65907
+ const end = Math.min(d + 2 + length, descriptorsEnd);
65908
+ if (tag === TELETEXT_DESCRIPTOR_TAG) {
65909
+ stream.teletext = readTeletextEntries(bytes2, d + 2, end);
65910
+ } else if (tag === SUBTITLING_DESCRIPTOR_TAG) {
65911
+ stream.subtitles = readSubtitlingEntries(bytes2, d + 2, end);
65912
+ }
65913
+ d += 2 + length;
65914
+ }
65915
+ if (stream.teletext || stream.subtitles) {
65916
+ out.push(stream);
65917
+ }
65918
+ }
65919
+ pos = descriptorsEnd;
65920
+ }
65921
+ return out;
65922
+ };
65923
+ var readMpegTsPrivateStreams = (bytes2) => {
65924
+ let pmtPid = null;
65925
+ const found = /* @__PURE__ */ new Map();
65926
+ for (const packet of iteratePackets(bytes2)) {
65927
+ if (!packet.payloadUnitStart) {
65928
+ continue;
65929
+ }
65930
+ if (packet.pid === PAT_PID) {
65931
+ pmtPid ??= pmtPidFromPat(bytes2, packet);
65932
+ } else if (pmtPid !== null && packet.pid === pmtPid) {
65933
+ for (const stream of privateStreamsFromPmt(bytes2, packet)) {
65934
+ found.set(stream.pid, stream);
65935
+ }
65936
+ }
65937
+ }
65938
+ return [...found.values()];
65939
+ };
65940
+ var readPts = (bytes2, at) => {
65941
+ const high = bytes2[at] >> 1 & 7;
65942
+ const mid = (bytes2[at + 1] << 8 | bytes2[at + 2]) >>> 1;
65943
+ const low = (bytes2[at + 3] << 8 | bytes2[at + 4]) >>> 1;
65944
+ return high * 2 ** 30 + mid * 2 ** 15 + low;
65945
+ };
65946
+ var readPrivatePesPayloads = (bytes2, pid) => {
65947
+ const out = [];
65948
+ let current = null;
65949
+ const finish = () => {
65950
+ if (current !== null && current.chunks.length > 0) {
65951
+ out.push({ pid, time: current.time, data: new Uint8Array(current.chunks) });
65952
+ }
65953
+ current = null;
65954
+ };
65955
+ for (const packet of iteratePackets(bytes2)) {
65956
+ if (packet.pid !== pid) {
65957
+ continue;
65958
+ }
65959
+ if (packet.payloadUnitStart) {
65960
+ finish();
65961
+ const at = packet.payloadStart;
65962
+ if (at + 9 > packet.payloadEnd || bytes2[at] !== 0 || bytes2[at + 1] !== 0 || bytes2[at + 2] !== 1) {
65963
+ continue;
65964
+ }
65965
+ const headerLength = bytes2[at + 8];
65966
+ const payloadStart = at + 9 + headerLength;
65967
+ if (payloadStart > packet.payloadEnd) {
65968
+ continue;
65969
+ }
65970
+ const ptsPresent = (bytes2[at + 7] & 128) !== 0;
65971
+ const time = ptsPresent && at + 14 <= packet.payloadEnd ? readPts(bytes2, at + 9) % TIMESTAMP_MODULUS / MPEG_TS_TIMESCALE : null;
65972
+ current = { time, chunks: [] };
65973
+ for (let i = payloadStart; i < packet.payloadEnd; i++) {
65974
+ current.chunks.push(bytes2[i]);
65975
+ }
65976
+ } else if (current !== null) {
65977
+ for (let i = packet.payloadStart; i < packet.payloadEnd; i++) {
65978
+ current.chunks.push(bytes2[i]);
65979
+ }
65980
+ }
65981
+ }
65982
+ finish();
65983
+ return out;
65984
+ };
65985
+
65986
+ // src/mpeg-ts/scte35-output.ts
65987
+ var addScte35Section = (output, section) => {
65988
+ if (!(section instanceof Uint8Array)) {
65989
+ throw new TypeError("section must be a Uint8Array.");
65990
+ }
65991
+ if (section.length < 3) {
65992
+ throw new InvalidUsageError("A splice_info_section is at least three bytes.");
65993
+ }
65994
+ const muxer = output._muxer;
65995
+ if (!(muxer instanceof MpegTsMuxer)) {
65996
+ throw new InvalidUsageError("SCTE-35 sections can only be written to an MpegTsOutputFormat output.");
65997
+ }
65998
+ muxer.writeScte35Section(section);
65999
+ };
66000
+
65758
66001
  // src/segment-index.ts
65759
66002
  var segmentIndexOf = async (input, options) => {
65760
66003
  if (!Number.isFinite(options.targetDuration) || options.targetDuration <= 0) {