@remotion/media-parser 4.0.201 → 4.0.204

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 (144) hide show
  1. package/dist/av1-codec-string.d.ts +0 -5
  2. package/dist/av1-codec-string.js +1 -18
  3. package/dist/boxes/iso-base-media/ftype.d.ts +9 -0
  4. package/dist/boxes/iso-base-media/ftype.js +31 -0
  5. package/dist/boxes/iso-base-media/get-sample-positions-from-track.d.ts +4 -0
  6. package/dist/boxes/iso-base-media/get-sample-positions-from-track.js +48 -0
  7. package/dist/boxes/iso-base-media/mvhd.js +2 -2
  8. package/dist/boxes/iso-base-media/stsd/keys.js +1 -1
  9. package/dist/boxes/iso-base-media/tfdt.d.ts +12 -0
  10. package/dist/boxes/iso-base-media/tfdt.js +20 -0
  11. package/dist/boxes/iso-base-media/tfhd.d.ts +16 -0
  12. package/dist/boxes/iso-base-media/tfhd.js +41 -0
  13. package/dist/boxes/iso-base-media/trun.d.ts +21 -0
  14. package/dist/boxes/iso-base-media/trun.js +44 -0
  15. package/dist/boxes/webm/av1-codec-private.js +1 -1
  16. package/dist/boxes/webm/bitstream/av1.js +1 -10
  17. package/dist/boxes/webm/description.d.ts +2 -2
  18. package/dist/boxes/webm/description.js +2 -2
  19. package/dist/boxes/webm/ebml.d.ts +2 -2
  20. package/dist/boxes/webm/ebml.js +23 -1
  21. package/dist/boxes/webm/get-ready-tracks.d.ts +1 -1
  22. package/dist/boxes/webm/get-ready-tracks.js +3 -3
  23. package/dist/boxes/webm/get-sample-from-block.d.ts +17 -0
  24. package/dist/boxes/webm/get-sample-from-block.js +78 -0
  25. package/dist/boxes/webm/get-track.d.ts +2 -2
  26. package/dist/boxes/webm/get-track.js +26 -25
  27. package/dist/boxes/webm/make-header.d.ts +3 -8
  28. package/dist/boxes/webm/make-header.js +43 -20
  29. package/dist/boxes/webm/parse-ebml.d.ts +9 -4
  30. package/dist/boxes/webm/parse-ebml.js +122 -13
  31. package/dist/boxes/webm/segments/all-segments.d.ts +421 -107
  32. package/dist/boxes/webm/segments/all-segments.js +260 -33
  33. package/dist/boxes/webm/segments/track-entry.d.ts +3 -191
  34. package/dist/boxes/webm/segments/track-entry.js +2 -456
  35. package/dist/boxes/webm/segments.d.ts +3 -16
  36. package/dist/boxes/webm/segments.js +12 -196
  37. package/dist/boxes/webm/tracks.d.ts +8 -0
  38. package/dist/boxes/webm/tracks.js +21 -0
  39. package/dist/boxes/webm/traversal.d.ts +5 -6
  40. package/dist/boxes/webm/traversal.js +6 -6
  41. package/dist/buffer-iterator.d.ts +1 -1
  42. package/dist/buffer-iterator.js +3 -3
  43. package/dist/from-web.js +6 -15
  44. package/dist/get-audio-codec.d.ts +1 -1
  45. package/dist/get-audio-codec.js +13 -13
  46. package/dist/get-duration.js +12 -14
  47. package/dist/get-tracks.js +2 -2
  48. package/dist/get-video-codec.js +13 -13
  49. package/dist/get-video-metadata.d.ts +2 -0
  50. package/dist/get-video-metadata.js +44 -0
  51. package/dist/parse-media.js +4 -1
  52. package/dist/parser-context.d.ts +1 -0
  53. package/dist/parser-state.js +3 -2
  54. package/dist/read-and-increment-offset.d.ts +28 -0
  55. package/dist/read-and-increment-offset.js +177 -0
  56. package/dist/samples-from-moof.d.ts +6 -0
  57. package/dist/samples-from-moof.js +74 -0
  58. package/dist/traversal.d.ts +19 -17
  59. package/dist/traversal.js +38 -39
  60. package/dist/understand-vorbis.d.ts +1 -0
  61. package/dist/understand-vorbis.js +12 -0
  62. package/input.webm +0 -0
  63. package/package.json +2 -2
  64. package/src/boxes/iso-base-media/get-sample-positions-from-track.ts +69 -0
  65. package/src/boxes/iso-base-media/make-track.ts +4 -45
  66. package/src/boxes/iso-base-media/mdat/mdat.ts +33 -24
  67. package/src/boxes/iso-base-media/mdhd.ts +10 -7
  68. package/src/boxes/iso-base-media/mvhd.ts +17 -16
  69. package/src/boxes/iso-base-media/process-box.ts +42 -0
  70. package/src/boxes/iso-base-media/stsd/keys.ts +1 -1
  71. package/src/boxes/iso-base-media/tfdt.ts +37 -0
  72. package/src/boxes/iso-base-media/tfhd.ts +66 -0
  73. package/src/boxes/iso-base-media/tkhd.ts +11 -13
  74. package/src/boxes/iso-base-media/trun.ts +74 -0
  75. package/src/boxes/webm/av1-codec-private.ts +1 -1
  76. package/src/boxes/webm/description.ts +7 -4
  77. package/src/boxes/webm/ebml.ts +24 -4
  78. package/src/boxes/webm/get-ready-tracks.ts +4 -4
  79. package/src/boxes/webm/get-sample-from-block.ts +125 -0
  80. package/src/boxes/webm/get-track.ts +40 -33
  81. package/src/boxes/webm/make-header.ts +58 -51
  82. package/src/boxes/webm/parse-ebml.ts +170 -16
  83. package/src/boxes/webm/segments/all-segments.ts +379 -62
  84. package/src/boxes/webm/segments/track-entry.ts +3 -846
  85. package/src/boxes/webm/segments.ts +18 -410
  86. package/src/boxes/webm/traversal.ts +17 -17
  87. package/src/buffer-iterator.ts +8 -6
  88. package/src/get-audio-codec.ts +14 -16
  89. package/src/get-duration.ts +55 -21
  90. package/src/get-tracks.ts +6 -6
  91. package/src/get-video-codec.ts +13 -15
  92. package/src/has-all-info.ts +1 -1
  93. package/src/parse-media.ts +7 -2
  94. package/src/parse-result.ts +7 -1
  95. package/src/parser-context.ts +1 -0
  96. package/src/parser-state.ts +2 -2
  97. package/src/samples-from-moof.ts +101 -0
  98. package/src/test/create-matroska.test.ts +237 -23
  99. package/src/test/matroska.test.ts +283 -348
  100. package/src/test/mvhd.test.ts +1 -1
  101. package/src/test/parse-esds.test.ts +2 -2
  102. package/src/test/parse-stco.test.ts +2 -2
  103. package/src/test/parse-stsc.test.ts +2 -2
  104. package/src/test/parse-stsz.test.ts +2 -2
  105. package/src/test/parse-stts.test.ts +1 -1
  106. package/src/test/samples-from-moof.test.ts +2496 -0
  107. package/src/test/stream-local.test.ts +28 -30
  108. package/src/test/stream-samples.test.ts +153 -231
  109. package/src/test/stsd.test.ts +4 -2
  110. package/src/test/tkhd.test.ts +1 -1
  111. package/src/traversal.ts +118 -86
  112. package/tsconfig.tsbuildinfo +1 -1
  113. package/dist/bitstream/av1.d.ts +0 -2
  114. package/dist/bitstream/av1.js +0 -12
  115. package/dist/boxes/iso-base-media/avcc-hvcc.d.ts +0 -20
  116. package/dist/boxes/iso-base-media/avcc-hvcc.js +0 -73
  117. package/dist/boxes/iso-base-media/avcc.d.ts +0 -18
  118. package/dist/boxes/iso-base-media/avcc.js +0 -27
  119. package/dist/boxes/iso-base-media/esds-descriptors.d.ts +0 -21
  120. package/dist/boxes/iso-base-media/esds-descriptors.js +0 -62
  121. package/dist/boxes/iso-base-media/esds.d.ts +0 -15
  122. package/dist/boxes/iso-base-media/esds.js +0 -27
  123. package/dist/from-input-type-file.d.ts +0 -2
  124. package/dist/from-input-type-file.js +0 -37
  125. package/dist/get-codec.d.ts +0 -4
  126. package/dist/get-codec.js +0 -22
  127. package/dist/web-file.d.ts +0 -2
  128. package/dist/web-file.js +0 -37
  129. package/src/boxes/webm/segments/duration.ts +0 -29
  130. package/src/boxes/webm/segments/info.ts +0 -34
  131. package/src/boxes/webm/segments/main.ts +0 -6
  132. package/src/boxes/webm/segments/muxing.ts +0 -18
  133. package/src/boxes/webm/segments/seek-head.ts +0 -34
  134. package/src/boxes/webm/segments/seek-position.ts +0 -18
  135. package/src/boxes/webm/segments/seek.ts +0 -55
  136. package/src/boxes/webm/segments/timestamp-scale.ts +0 -17
  137. package/src/boxes/webm/segments/tracks.ts +0 -32
  138. package/src/boxes/webm/segments/void.ts +0 -18
  139. package/src/boxes/webm/segments/writing.ts +0 -18
  140. package/src/combine-uint8array.ts +0 -13
  141. /package/dist/{get-samples.d.ts → boxes/webm/bitstream/av1/frame.d.ts} +0 -0
  142. /package/dist/{get-samples.js → boxes/webm/bitstream/av1/frame.js} +0 -0
  143. /package/dist/{sample-aspect-ratio.d.ts → boxes/webm/bitstream/h264/get-h264-descriptor.d.ts} +0 -0
  144. /package/dist/{sample-aspect-ratio.js → boxes/webm/bitstream/h264/get-h264-descriptor.js} +0 -0
@@ -1,205 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.expectSegment = void 0;
4
- const add_new_matroska_tracks_1 = require("../../add-new-matroska-tracks");
5
- const get_track_1 = require("./get-track");
6
4
  const parse_ebml_1 = require("./parse-ebml");
7
- const all_segments_1 = require("./segments/all-segments");
8
- const duration_1 = require("./segments/duration");
9
- const info_1 = require("./segments/info");
10
- const muxing_1 = require("./segments/muxing");
11
5
  const parse_children_1 = require("./segments/parse-children");
12
- const seek_1 = require("./segments/seek");
13
- const seek_head_1 = require("./segments/seek-head");
14
- const seek_position_1 = require("./segments/seek-position");
15
- const timestamp_scale_1 = require("./segments/timestamp-scale");
16
- const track_entry_1 = require("./segments/track-entry");
17
- const tracks_1 = require("./segments/tracks");
18
- const void_1 = require("./segments/void");
19
- const writing_1 = require("./segments/writing");
20
6
  const parseSegment = async ({ segmentId, iterator, length, parserContext, headerReadSoFar, }) => {
21
7
  if (length === 0) {
22
8
  throw new Error(`Expected length of ${segmentId} to be greater than 0`);
23
9
  }
24
- if (segmentId === '0x114d9b74') {
25
- return (0, seek_head_1.parseSeekHeadSegment)(iterator, length, parserContext);
26
- }
27
- if (segmentId === '0x53ab') {
28
- return (0, seek_1.parseSeekIdSegment)(iterator, length);
29
- }
30
- if (segmentId === '0x4dbb') {
31
- return (0, seek_1.parseSeekSegment)(iterator, length, parserContext);
32
- }
33
- if (segmentId === '0x53ac') {
34
- return (0, seek_position_1.parseSeekPositionSegment)(iterator, length);
35
- }
36
- if (segmentId === '0xec') {
37
- return (0, void_1.parseVoidSegment)(iterator, length);
38
- }
39
- if (segmentId === '0x1549a966') {
40
- return (0, info_1.parseInfoSegment)(iterator, length, parserContext);
41
- }
42
- if (segmentId === all_segments_1.matroskaElements.TimestampScale) {
43
- const timestampScale = (0, timestamp_scale_1.parseTimestampScaleSegment)(iterator);
44
- parserContext.parserState.setTimescale(timestampScale.timestampScale);
45
- return timestampScale;
46
- }
47
- if (segmentId === '0x4d80') {
48
- return (0, muxing_1.parseMuxingSegment)(iterator, length);
49
- }
50
- if (segmentId === '0x5741') {
51
- return (0, writing_1.parseWritingSegment)(iterator, length);
52
- }
53
- if (segmentId === '0x4489') {
54
- return (0, duration_1.parseDurationSegment)(iterator, length);
55
- }
56
- if (segmentId === '0x1654ae6b') {
57
- return (0, tracks_1.parseTracksSegment)(iterator, length, parserContext);
58
- }
59
- if (segmentId === all_segments_1.matroskaElements.TrackEntry) {
60
- const trackEntry = await (0, track_entry_1.parseTrackEntry)(iterator, length, parserContext);
61
- parserContext.parserState.onTrackEntrySegment(trackEntry);
62
- const track = (0, get_track_1.getTrack)({
63
- track: trackEntry,
64
- timescale: parserContext.parserState.getTimescale(),
65
- });
66
- if (track) {
67
- await (0, add_new_matroska_tracks_1.registerTrack)({
68
- state: parserContext.parserState,
69
- options: parserContext,
70
- track,
71
- });
72
- }
73
- return trackEntry;
74
- }
75
- if (segmentId === '0xd7') {
76
- return (0, track_entry_1.parseTrackNumber)(iterator, length);
77
- }
78
- if (segmentId === '0x73c5') {
79
- return (0, track_entry_1.parseTrackUID)(iterator, length);
80
- }
81
- if (segmentId === '0x9c') {
82
- return (0, track_entry_1.parseFlagLacing)(iterator, length);
83
- }
84
- if (segmentId === '0x22b59c') {
85
- return (0, track_entry_1.parseLanguageSegment)(iterator, length);
86
- }
87
- if (segmentId === '0x86') {
88
- return (0, track_entry_1.parseCodecSegment)(iterator, length);
89
- }
90
- if (segmentId === '0x83') {
91
- return (0, track_entry_1.parseTrackTypeSegment)(iterator, length);
92
- }
93
- if (segmentId === '0x55ee') {
94
- return (0, track_entry_1.parseMaxBlockAdditionId)(iterator, length);
95
- }
96
- if (segmentId === '0x55b0') {
97
- return (0, track_entry_1.parseColorSegment)(iterator, length);
98
- }
99
- if (segmentId === '0x23e383') {
100
- return (0, track_entry_1.parseDefaultDurationSegment)(iterator, length);
101
- }
102
- if (segmentId === '0xe0') {
103
- return (0, track_entry_1.parseVideoSegment)(iterator, length, parserContext);
104
- }
105
- if (segmentId === '0xe1') {
106
- return (0, track_entry_1.parseAudioSegment)(iterator, length, parserContext);
107
- }
108
- if (segmentId === '0xb0') {
109
- return (0, track_entry_1.parseWidthSegment)(iterator, length);
110
- }
111
- if (segmentId === '0xba') {
112
- return (0, track_entry_1.parseHeightSegment)(iterator, length);
113
- }
114
- if (segmentId === all_segments_1.matroskaElements.DisplayWidth) {
115
- return (0, track_entry_1.parseDisplayWidthSegment)(iterator, length);
116
- }
117
- if (segmentId === all_segments_1.matroskaElements.DisplayHeight) {
118
- return (0, track_entry_1.parseDisplayHeightSegment)(iterator, length);
119
- }
120
- if (segmentId === '0x9a') {
121
- return (0, track_entry_1.parseInterlacedSegment)(iterator, length);
122
- }
123
- if (segmentId === '0x53c0') {
124
- return (0, track_entry_1.parseAlphaModeSegment)(iterator, length);
125
- }
126
- if (segmentId === '0x63a2') {
127
- return (0, track_entry_1.parseCodecPrivateSegment)(iterator, length);
128
- }
129
- if (segmentId === '0x7ba9') {
130
- return (0, track_entry_1.parseTitleSegment)(iterator, length);
131
- }
132
- if (segmentId === '0xbf') {
133
- return (0, track_entry_1.parseCrc32Segment)(iterator, length);
134
- }
135
- if (segmentId === '0x73a4') {
136
- return (0, track_entry_1.parseSegmentUUIDSegment)(iterator, length);
137
- }
138
- if (segmentId === '0x88') {
139
- return (0, track_entry_1.parseDefaultFlagSegment)(iterator, length);
140
- }
141
- if (segmentId === '0x1254c367') {
142
- return (0, track_entry_1.parseTagsSegment)(iterator, length, parserContext);
143
- }
144
- if (segmentId === '0x7373') {
145
- return (0, track_entry_1.parseTagSegment)(iterator, length);
146
- }
147
- if (segmentId === all_segments_1.matroskaElements.SamplingFrequency) {
148
- return (0, track_entry_1.parseSamplingFrequencySegment)(iterator, length);
149
- }
150
- if (segmentId === all_segments_1.matroskaElements.Channels) {
151
- return (0, track_entry_1.parseChannelsSegment)(iterator, length);
152
- }
153
- if (segmentId === all_segments_1.matroskaElements.BitDepth) {
154
- return (0, track_entry_1.parseBitDepthSegment)(iterator, length);
155
- }
156
- if (segmentId === all_segments_1.matroskaElements.Timestamp) {
157
- const offset = iterator.counter.getOffset();
158
- const timestampSegment = (0, track_entry_1.parseTimestampSegment)(iterator, length);
159
- parserContext.parserState.setTimestampOffset(offset, timestampSegment.timestamp);
160
- return timestampSegment;
161
- }
162
- if (segmentId === all_segments_1.matroskaElements.SimpleBlock ||
163
- segmentId === all_segments_1.matroskaElements.Block) {
164
- return (0, track_entry_1.parseSimpleBlockOrBlockSegment)({
165
- iterator,
166
- length,
167
- parserContext,
168
- type: segmentId,
169
- });
170
- }
171
- if (segmentId === all_segments_1.matroskaElements.ReferenceBlock) {
172
- return (0, track_entry_1.parseReferenceBlockSegment)(iterator, length);
173
- }
174
- if (segmentId === all_segments_1.matroskaElements.BlockAdditions) {
175
- return (0, track_entry_1.parseBlockAdditionsSegment)(iterator, length);
176
- }
177
- if (segmentId === '0xa0') {
178
- const blockGroup = await (0, track_entry_1.parseBlockGroupSegment)(iterator, length, parserContext);
179
- // Blocks don't have information about keyframes.
180
- // https://ffmpeg.org/pipermail/ffmpeg-devel/2015-June/173825.html
181
- // "For Blocks, keyframes is
182
- // inferred by the absence of ReferenceBlock element (as done by matroskadec).""
183
- const block = blockGroup.children.find((c) => c.type === 'simple-block-or-block-segment');
184
- if (!block || block.type !== 'simple-block-or-block-segment') {
185
- throw new Error('Expected block segment');
186
- }
187
- const hasReferenceBlock = blockGroup.children.find((c) => c.type === 'reference-block-segment');
188
- const partialVideoSample = block.videoSample;
189
- if (partialVideoSample) {
190
- const completeFrame = {
191
- ...partialVideoSample,
192
- type: hasReferenceBlock ? 'delta' : 'key',
193
- };
194
- await parserContext.parserState.onVideoSample(partialVideoSample.trackId, completeFrame);
195
- }
196
- return blockGroup;
197
- }
198
- if (segmentId === '0xa1') {
199
- return (0, track_entry_1.parseBlockElementSegment)(iterator, length);
200
- }
201
10
  iterator.counter.decrement(headerReadSoFar);
202
- return (0, parse_ebml_1.parseEbml)(iterator);
11
+ const offset = iterator.counter.getOffset();
12
+ const ebml = await (0, parse_ebml_1.parseEbml)(iterator, parserContext);
13
+ const remapped = await (0, parse_ebml_1.postprocessEbml)({ offset, ebml, parserContext });
14
+ return remapped;
203
15
  };
204
16
  const expectSegment = async (iterator, parserContext) => {
205
17
  const offset = iterator.counter.getOffset();
@@ -225,7 +37,9 @@ const expectSegment = async (iterator, parserContext) => {
225
37
  skipTo: null,
226
38
  };
227
39
  }
40
+ const offsetBeforeVInt = iterator.counter.getOffset();
228
41
  const length = iterator.getVint();
42
+ const offsetAfterVInt = iterator.counter.getOffset();
229
43
  if (length === null) {
230
44
  iterator.counter.decrement(iterator.counter.getOffset() - offset);
231
45
  return {
@@ -245,12 +59,14 @@ const expectSegment = async (iterator, parserContext) => {
245
59
  initialChildren: [],
246
60
  wrap: segmentId === '0x18538067'
247
61
  ? (s) => ({
248
- type: 'main-segment',
249
- children: s,
62
+ type: 'Segment',
63
+ value: s,
64
+ minVintWidth: offsetAfterVInt - offsetBeforeVInt,
250
65
  })
251
66
  : (s) => ({
252
- type: 'cluster-segment',
253
- children: s,
67
+ type: 'Cluster',
68
+ value: s,
69
+ minVintWidth: offsetAfterVInt - offsetBeforeVInt,
254
70
  }),
255
71
  parserContext,
256
72
  });
@@ -0,0 +1,8 @@
1
+ import type { BufferIterator } from '../../buffer-iterator';
2
+ import type { ParserContext } from '../../parser-context';
3
+ import type { MatroskaSegment } from './segments';
4
+ export type TracksSegment = {
5
+ type: 'tracks-segment';
6
+ children: MatroskaSegment[];
7
+ };
8
+ export declare const parseTracksSegment: (iterator: BufferIterator, length: number, parserContext: ParserContext) => Promise<TracksSegment>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseTracksSegment = void 0;
4
+ const parse_children_1 = require("./segments/parse-children");
5
+ const parseTracksSegment = async (iterator, length, parserContext) => {
6
+ const children = await (0, parse_children_1.expectChildren)({
7
+ iterator,
8
+ length,
9
+ initialChildren: [],
10
+ wrap: null,
11
+ parserContext,
12
+ });
13
+ if (children.status === 'incomplete') {
14
+ throw new Error('Incomplete children');
15
+ }
16
+ return {
17
+ type: 'tracks-segment',
18
+ children: children.segments,
19
+ };
20
+ };
21
+ exports.parseTracksSegment = parseTracksSegment;
@@ -1,8 +1,7 @@
1
1
  import type { AnySegment } from '../../parse-result';
2
- import type { MainSegment } from './segments/main';
3
- import type { CodecSegment, TrackEntrySegment } from './segments/track-entry';
2
+ import type { CodecIdSegment, MainSegment, TrackEntry } from './segments/all-segments';
4
3
  export declare const getMainSegment: (segments: AnySegment[]) => MainSegment | null;
5
- export declare const getTrackNumber: (track: TrackEntrySegment) => number | null;
6
- export declare const getTrackCodec: (track: TrackEntrySegment) => CodecSegment | null;
7
- export declare const getTrackTimestampScale: (track: TrackEntrySegment) => number | null;
8
- export declare const getTrackByNumber: (tracks: TrackEntrySegment[], id: number) => TrackEntrySegment | undefined;
4
+ export declare const getTrackNumber: (track: TrackEntry) => import("./segments/all-segments").UintWithSize | null;
5
+ export declare const getTrackCodec: (track: TrackEntry) => CodecIdSegment | null;
6
+ export declare const getTrackTimestampScale: (track: TrackEntry) => import("./segments/all-segments").FloatWithSize | null;
7
+ export declare const getTrackByNumber: (tracks: TrackEntry[], id: number) => TrackEntry | undefined;
@@ -2,22 +2,22 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getTrackByNumber = exports.getTrackTimestampScale = exports.getTrackCodec = exports.getTrackNumber = exports.getMainSegment = void 0;
4
4
  const getMainSegment = (segments) => {
5
- return segments.find((s) => s.type === 'main-segment');
5
+ return segments.find((s) => s.type === 'Segment');
6
6
  };
7
7
  exports.getMainSegment = getMainSegment;
8
8
  const getTrackNumber = (track) => {
9
9
  var _a;
10
- const child = track.children.find((b) => b.type === 'track-number-segment');
11
- return (_a = child === null || child === void 0 ? void 0 : child.trackNumber) !== null && _a !== void 0 ? _a : null;
10
+ const child = track.value.find((b) => b.type === 'TrackNumber');
11
+ return (_a = child === null || child === void 0 ? void 0 : child.value) !== null && _a !== void 0 ? _a : null;
12
12
  };
13
13
  exports.getTrackNumber = getTrackNumber;
14
14
  const getTrackCodec = (track) => {
15
- const child = track.children.find((b) => b.type === 'codec-segment');
15
+ const child = track.value.find((b) => b.type === 'CodecID');
16
16
  return child !== null && child !== void 0 ? child : null;
17
17
  };
18
18
  exports.getTrackCodec = getTrackCodec;
19
19
  const getTrackTimestampScale = (track) => {
20
- const child = track.children.find((b) => b.type === 'TrackTimestampScale');
20
+ const child = track.value.find((b) => b.type === 'TrackTimestampScale');
21
21
  if (!child) {
22
22
  return null;
23
23
  }
@@ -30,7 +30,7 @@ exports.getTrackTimestampScale = getTrackTimestampScale;
30
30
  const getTrackByNumber = (tracks, id) => {
31
31
  return tracks.find((track) => {
32
32
  const trackNumber = (0, exports.getTrackNumber)(track);
33
- return trackNumber === id;
33
+ return (trackNumber === null || trackNumber === void 0 ? void 0 : trackNumber.value) === id;
34
34
  });
35
35
  };
36
36
  exports.getTrackByNumber = getTrackByNumber;
@@ -8,7 +8,7 @@ export declare class OffsetCounter {
8
8
  discardBytes(amount: number): void;
9
9
  decrement(amount: number): void;
10
10
  }
11
- export declare const getArrayBufferIterator: (initialData: Uint8Array, maxBytes?: number) => {
11
+ export declare const getArrayBufferIterator: (initialData: Uint8Array, maxBytes: number | null) => {
12
12
  startReadingBits: () => void;
13
13
  stopReadingBits: () => void;
14
14
  skipTo: (offset: number) => void;
@@ -62,9 +62,9 @@ const makeOffsetCounter = () => {
62
62
  };
63
63
  const getArrayBufferIterator = (initialData, maxBytes) => {
64
64
  const buf = new ArrayBuffer(initialData.byteLength, {
65
- maxByteLength: maxBytes
66
- ? Math.min(maxBytes, 2 ** 32)
67
- : 1000000000,
65
+ maxByteLength: maxBytes === null
66
+ ? initialData.byteLength
67
+ : Math.min(maxBytes, 2 ** 32),
68
68
  });
69
69
  if (!buf.resize) {
70
70
  throw new Error('`ArrayBuffer.resize` is not supported in this Runtime. On the server: Use at least Node.js 20 or Bun. In the browser: Chrome 111, Edge 111, Safari 16.4, Firefox 128, Opera 111');
package/dist/from-web.js CHANGED
@@ -3,9 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.webReader = void 0;
4
4
  exports.webReader = {
5
5
  read: async (src, range) => {
6
- if (typeof src !== 'string') {
7
- throw new Error('src must be a string when using `webReader`');
8
- }
9
6
  const resolvedUrl = typeof window !== 'undefined' && typeof window.location !== 'undefined'
10
7
  ? new URL(src, window.location.origin).toString()
11
8
  : src;
@@ -17,28 +14,22 @@ exports.webReader = {
17
14
  const res = await fetch(resolvedUrl, {
18
15
  headers: range === null
19
16
  ? {}
20
- : typeof range === 'number'
21
- ? {
22
- Range: `bytes=${range}`,
23
- }
24
- : {
25
- Range: `bytes=${`${range[0]}-${range[1]}`}`,
26
- },
27
- // Disable Next.js caching
28
- cache: 'no-store',
17
+ : {
18
+ Range: `bytes=${`${range[0]}-${range[1]}`}`,
19
+ },
29
20
  });
30
21
  if (!res.body) {
31
22
  throw new Error('No body');
32
23
  }
33
24
  const length = res.headers.get('content-length');
25
+ if (!length) {
26
+ throw new Error('No content-length');
27
+ }
34
28
  const contentLength = length === null ? null : parseInt(length, 10);
35
29
  const reader = res.body.getReader();
36
30
  return { reader, contentLength };
37
31
  },
38
32
  getLength: async (src) => {
39
- if (typeof src !== 'string') {
40
- throw new Error('src must be a string when using `webReader`');
41
- }
42
33
  const res = await fetch(src, {
43
34
  method: 'HEAD',
44
35
  });
@@ -1,6 +1,6 @@
1
1
  import type { MoovBox } from './boxes/iso-base-media/moov/moov';
2
2
  import type { TrakBox } from './boxes/iso-base-media/trak/trak';
3
- import type { MainSegment } from './boxes/webm/segments/main';
3
+ import type { MainSegment } from './boxes/webm/segments/all-segments';
4
4
  import type { KnownAudioCodecs } from './options';
5
5
  import type { AnySegment } from './parse-result';
6
6
  export declare const hasAudioCodec: (boxes: AnySegment[]) => boolean;
@@ -115,27 +115,27 @@ const getAudioCodecFromIso = (moov) => {
115
115
  };
116
116
  exports.getAudioCodecFromIso = getAudioCodecFromIso;
117
117
  const getAudioCodecFromMatroska = (mainSegment) => {
118
- const tracksSegment = mainSegment.children.find((b) => b.type === 'tracks-segment');
119
- if (!tracksSegment || tracksSegment.type !== 'tracks-segment') {
118
+ const tracksSegment = mainSegment.value.find((b) => b.type === 'Tracks');
119
+ if (!tracksSegment || tracksSegment.type !== 'Tracks') {
120
120
  return null;
121
121
  }
122
- for (const track of tracksSegment.children) {
123
- if (track.type === 'track-entry-segment') {
124
- const trackType = track.children.find((b) => b.type === 'codec-segment');
125
- if (trackType && trackType.type === 'codec-segment') {
126
- if (trackType.codec === 'A_OPUS') {
122
+ for (const track of tracksSegment.value) {
123
+ if (track.type === 'TrackEntry') {
124
+ const trackType = track.value.find((b) => b.type === 'CodecID');
125
+ if (trackType && trackType.type === 'CodecID') {
126
+ if (trackType.value === 'A_OPUS') {
127
127
  return 'opus';
128
128
  }
129
- if (trackType.codec === 'A_VORBIS') {
129
+ if (trackType.value === 'A_VORBIS') {
130
130
  return 'vorbis';
131
131
  }
132
- if (trackType.codec === 'A_PCM/INT/LIT') {
132
+ if (trackType.value === 'A_PCM/INT/LIT') {
133
133
  return 'pcm';
134
134
  }
135
- if (trackType.codec === 'A_AAC') {
135
+ if (trackType.value === 'A_AAC') {
136
136
  return 'aac';
137
137
  }
138
- if (trackType.codec === 'A_MPEG/L3') {
138
+ if (trackType.value === 'A_MPEG/L3') {
139
139
  return 'mp3';
140
140
  }
141
141
  }
@@ -190,8 +190,8 @@ const getAudioCodec = (boxes) => {
190
190
  }
191
191
  throw new Error('Unknown audio format: ' + codec.format);
192
192
  }
193
- const mainSegment = boxes.find((b) => b.type === 'main-segment');
194
- if (!mainSegment || mainSegment.type !== 'main-segment') {
193
+ const mainSegment = boxes.find((b) => b.type === 'Segment');
194
+ if (!mainSegment || mainSegment.type !== 'Segment') {
195
195
  return null;
196
196
  }
197
197
  return (0, exports.getAudioCodecFromMatroska)(mainSegment);
@@ -3,33 +3,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.hasDuration = exports.getDuration = void 0;
4
4
  const traversal_1 = require("./traversal");
5
5
  const getDurationFromMatroska = (segments) => {
6
- const mainSegment = segments.find((s) => s.type === 'main-segment');
7
- if (!mainSegment || mainSegment.type !== 'main-segment') {
6
+ const mainSegment = segments.find((s) => s.type === 'Segment');
7
+ if (!mainSegment || mainSegment.type !== 'Segment') {
8
8
  return null;
9
9
  }
10
- const { children } = mainSegment;
10
+ const { value: children } = mainSegment;
11
11
  if (!children) {
12
12
  return null;
13
13
  }
14
- const infoSegment = children.find((s) => s.type === 'info-segment');
14
+ const infoSegment = children.find((s) => s.type === 'Info');
15
15
  const relevantBoxes = [
16
- ...mainSegment.children,
17
- ...(infoSegment && infoSegment.type === 'info-segment'
18
- ? infoSegment.children
19
- : []),
16
+ ...mainSegment.value,
17
+ ...(infoSegment && infoSegment.type === 'Info' ? infoSegment.value : []),
20
18
  ];
21
- const timestampScale = relevantBoxes.find((s) => s.type === 'timestamp-scale-segment');
22
- if (!timestampScale || timestampScale.type !== 'timestamp-scale-segment') {
19
+ const timestampScale = relevantBoxes.find((s) => s.type === 'TimestampScale');
20
+ if (!timestampScale || timestampScale.type !== 'TimestampScale') {
23
21
  return null;
24
22
  }
25
- const duration = relevantBoxes.find((s) => s.type === 'duration-segment');
26
- if (!duration || duration.type !== 'duration-segment') {
23
+ const duration = relevantBoxes.find((s) => s.type === 'Duration');
24
+ if (!duration || duration.type !== 'Duration') {
27
25
  return null;
28
26
  }
29
- return (duration.duration / timestampScale.timestampScale) * 1000;
27
+ return (duration.value.value / timestampScale.value.value) * 1000;
30
28
  };
31
29
  const getDuration = (boxes) => {
32
- const matroskaBox = boxes.find((b) => b.type === 'main-segment');
30
+ const matroskaBox = boxes.find((b) => b.type === 'Segment');
33
31
  if (matroskaBox) {
34
32
  return getDurationFromMatroska(boxes);
35
33
  }
@@ -31,8 +31,8 @@ const getTracks = (segments, state) => {
31
31
  const videoTracks = [];
32
32
  const audioTracks = [];
33
33
  const otherTracks = [];
34
- const mainSegment = segments.find((s) => s.type === 'main-segment');
35
- if (mainSegment && mainSegment.type === 'main-segment') {
34
+ const mainSegment = segments.find((s) => s.type === 'Segment');
35
+ if (mainSegment && mainSegment.type === 'Segment') {
36
36
  const matroskaTracks = (0, get_ready_tracks_1.getTracksFromMatroska)(mainSegment, state.getTimescale());
37
37
  for (const track of matroskaTracks) {
38
38
  if (track.type === 'video') {
@@ -60,31 +60,31 @@ const getVideoCodec = (boxes) => {
60
60
  }
61
61
  }
62
62
  }
63
- const mainSegment = boxes.find((b) => b.type === 'main-segment');
64
- if (!mainSegment || mainSegment.type !== 'main-segment') {
63
+ const mainSegment = boxes.find((b) => b.type === 'Segment');
64
+ if (!mainSegment || mainSegment.type !== 'Segment') {
65
65
  return null;
66
66
  }
67
- const tracksSegment = mainSegment.children.find((b) => b.type === 'tracks-segment');
68
- if (!tracksSegment || tracksSegment.type !== 'tracks-segment') {
67
+ const tracksSegment = mainSegment.value.find((b) => b.type === 'Tracks');
68
+ if (!tracksSegment || tracksSegment.type !== 'Tracks') {
69
69
  return null;
70
70
  }
71
- for (const track of tracksSegment.children) {
72
- if (track.type === 'track-entry-segment') {
73
- const trackType = track.children.find((b) => b.type === 'codec-segment');
74
- if (trackType && trackType.type === 'codec-segment') {
75
- if (trackType.codec === 'V_VP8') {
71
+ for (const track of tracksSegment.value) {
72
+ if (track.type === 'TrackEntry') {
73
+ const trackType = track.value.find((b) => b.type === 'CodecID');
74
+ if (trackType && trackType.type === 'CodecID') {
75
+ if (trackType.value === 'V_VP8') {
76
76
  return 'vp8';
77
77
  }
78
- if (trackType.codec === 'V_VP9') {
78
+ if (trackType.value === 'V_VP9') {
79
79
  return 'vp9';
80
80
  }
81
- if (trackType.codec === 'V_AV1') {
81
+ if (trackType.value === 'V_AV1') {
82
82
  return 'av1';
83
83
  }
84
- if (trackType.codec === 'V_MPEG4/ISO/AVC') {
84
+ if (trackType.value === 'V_MPEG4/ISO/AVC') {
85
85
  return 'h264';
86
86
  }
87
- if (trackType.codec === 'V_MPEGH/ISO/HEVC') {
87
+ if (trackType.value === 'V_MPEGH/ISO/HEVC') {
88
88
  return 'h265';
89
89
  }
90
90
  }
@@ -0,0 +1,2 @@
1
+ import type { ParseMedia } from './options';
2
+ export declare const parseMedia: ParseMedia;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseMedia = void 0;
4
+ const buffer_iterator_1 = require("./buffer-iterator");
5
+ const from_web_1 = require("./from-web");
6
+ const get_dimensions_1 = require("./get-dimensions");
7
+ const get_duration_1 = require("./get-duration");
8
+ const get_fps_1 = require("./get-fps");
9
+ const has_all_info_1 = require("./has-all-info");
10
+ const parse_video_1 = require("./parse-video");
11
+ const parseMedia = async (src, options, readerInterface = from_web_1.webReader) => {
12
+ const reader = await readerInterface.read(src, null);
13
+ const returnValue = {};
14
+ const iterator = (0, buffer_iterator_1.getArrayBufferIterator)(new Uint8Array([]));
15
+ let parseResult = (0, parse_video_1.parseVideo)(iterator);
16
+ while (parseResult.status === 'incomplete') {
17
+ const result = await reader.read();
18
+ if (result.done) {
19
+ break;
20
+ }
21
+ iterator.addData(result.value);
22
+ parseResult = parseResult.continueParsing();
23
+ if ((0, has_all_info_1.hasAllInfo)(options, parseResult)) {
24
+ if (!reader.closed) {
25
+ reader.cancel(new Error('has all information'));
26
+ }
27
+ break;
28
+ }
29
+ }
30
+ if (options.dimensions) {
31
+ returnValue.dimensions = (0, get_dimensions_1.getDimensions)(parseResult.segments);
32
+ }
33
+ if (options.durationInSeconds) {
34
+ returnValue.durationInSeconds = (0, get_duration_1.getDuration)(parseResult.segments);
35
+ }
36
+ if (options.fps) {
37
+ returnValue.fps = (0, get_fps_1.getFps)(parseResult.segments);
38
+ }
39
+ if (options.boxes) {
40
+ returnValue.boxes = parseResult.segments;
41
+ }
42
+ return returnValue;
43
+ };
44
+ exports.parseMedia = parseMedia;
@@ -28,6 +28,9 @@ const parseMedia = async ({ src, fields, reader: readerInterface = from_fetch_1.
28
28
  onAudioTrack: onAudioTrack !== null && onAudioTrack !== void 0 ? onAudioTrack : null,
29
29
  onVideoTrack: onVideoTrack !== null && onVideoTrack !== void 0 ? onVideoTrack : null,
30
30
  parserState: state,
31
+ nullifySamples: !(typeof process !== 'undefined' &&
32
+ typeof process.env !== 'undefined' &&
33
+ process.env.KEEP_SAMPLES === 'true'),
31
34
  };
32
35
  while (parseResult === null || parseResult.status === 'incomplete') {
33
36
  if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
@@ -43,7 +46,7 @@ const parseMedia = async ({ src, fields, reader: readerInterface = from_fetch_1.
43
46
  if (result.done) {
44
47
  throw new Error('Unexpectedly reached EOF');
45
48
  }
46
- iterator = (0, buffer_iterator_1.getArrayBufferIterator)(result.value, contentLength !== null && contentLength !== void 0 ? contentLength : undefined);
49
+ iterator = (0, buffer_iterator_1.getArrayBufferIterator)(result.value, contentLength !== null && contentLength !== void 0 ? contentLength : 1000000000);
47
50
  }
48
51
  if (parseResult && parseResult.status === 'incomplete') {
49
52
  parseResult = await parseResult.continueParsing();
@@ -5,4 +5,5 @@ export type ParserContext = {
5
5
  onVideoTrack: OnVideoTrack | null;
6
6
  canSkipVideoData: boolean;
7
7
  parserState: ParserState;
8
+ nullifySamples: boolean;
8
9
  };
@@ -6,6 +6,7 @@ const traversal_2 = require("./traversal");
6
6
  const makeParserState = ({ hasAudioCallbacks, hasVideoCallbacks, signal, }) => {
7
7
  const trackEntries = {};
8
8
  const onTrackEntrySegment = (trackEntry) => {
9
+ var _a;
9
10
  const trackId = (0, traversal_2.getTrackId)(trackEntry);
10
11
  if (!trackId) {
11
12
  throw new Error('Expected track id');
@@ -19,8 +20,8 @@ const makeParserState = ({ hasAudioCallbacks, hasVideoCallbacks, signal, }) => {
19
20
  }
20
21
  const trackTimescale = (0, traversal_1.getTrackTimestampScale)(trackEntry);
21
22
  trackEntries[trackId] = {
22
- codec: codec.codec,
23
- trackTimescale,
23
+ codec: codec.value,
24
+ trackTimescale: (_a = trackTimescale === null || trackTimescale === void 0 ? void 0 : trackTimescale.value) !== null && _a !== void 0 ? _a : null,
24
25
  };
25
26
  };
26
27
  const videoSampleCallbacks = {};