@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,12 +1,20 @@
1
- import type {BufferIterator} from '../../buffer-iterator';
1
+ import {registerTrack} from '../../add-new-matroska-tracks';
2
+ import {type BufferIterator} from '../../buffer-iterator';
3
+ import type {ParserContext} from '../../parser-context';
4
+ import type {VideoSample} from '../../webcodec-sample-types';
5
+ import {getSampleFromBlock} from './get-sample-from-block';
6
+ import {getTrack} from './get-track';
2
7
  import type {PossibleEbml} from './segments/all-segments';
3
- import {ebmlMap, type Ebml, type EbmlParsed} from './segments/all-segments';
8
+ import {ebmlMap} from './segments/all-segments';
4
9
 
5
- type Prettify<T> = {
10
+ export type Prettify<T> = {
6
11
  [K in keyof T]: T[K];
7
12
  } & {};
8
13
 
9
- export const parseEbml = (iterator: BufferIterator): Prettify<PossibleEbml> => {
14
+ export const parseEbml = async (
15
+ iterator: BufferIterator,
16
+ parserContext: ParserContext,
17
+ ): Promise<Prettify<PossibleEbml>> => {
10
18
  const hex = iterator.getMatroskaSegmentId();
11
19
  if (hex === null) {
12
20
  throw new Error(
@@ -21,7 +29,10 @@ export const parseEbml = (iterator: BufferIterator): Prettify<PossibleEbml> => {
21
29
  );
22
30
  }
23
31
 
32
+ const off = iterator.counter.getOffset();
24
33
  const size = iterator.getVint();
34
+ const minVintWidth = iterator.counter.getOffset() - off;
35
+
25
36
  if (size === null) {
26
37
  throw new Error(
27
38
  'Not enough bytes left to parse EBML - this should not happen',
@@ -29,9 +40,17 @@ export const parseEbml = (iterator: BufferIterator): Prettify<PossibleEbml> => {
29
40
  }
30
41
 
31
42
  if (hasInMap.type === 'uint') {
43
+ const beforeUintOffset = iterator.counter.getOffset();
32
44
  const value = iterator.getUint(size);
33
45
 
34
- return {type: hasInMap.name, value, hex};
46
+ return {
47
+ type: hasInMap.name,
48
+ value: {
49
+ value,
50
+ byteLength: iterator.counter.getOffset() - beforeUintOffset,
51
+ },
52
+ minVintWidth,
53
+ };
35
54
  }
36
55
 
37
56
  if (hasInMap.type === 'string') {
@@ -40,7 +59,7 @@ export const parseEbml = (iterator: BufferIterator): Prettify<PossibleEbml> => {
40
59
  return {
41
60
  type: hasInMap.name,
42
61
  value,
43
- hex,
62
+ minVintWidth,
44
63
  };
45
64
  }
46
65
 
@@ -49,29 +68,49 @@ export const parseEbml = (iterator: BufferIterator): Prettify<PossibleEbml> => {
49
68
 
50
69
  return {
51
70
  type: hasInMap.name,
52
- value,
53
- hex,
71
+ value: {
72
+ value,
73
+ size: size === 4 ? '32' : '64',
74
+ },
75
+ minVintWidth,
54
76
  };
55
77
  }
56
78
 
57
- if (hasInMap.type === 'void') {
58
- iterator.discard(size);
79
+ if (hasInMap.type === 'hex-string') {
80
+ return {
81
+ type: hasInMap.name,
82
+ value:
83
+ '0x' +
84
+ [...iterator.getSlice(size)]
85
+ .map((b) => b.toString(16).padStart(2, '0'))
86
+ .join(''),
87
+ minVintWidth,
88
+ };
89
+ }
59
90
 
91
+ if (hasInMap.type === 'uint8array') {
60
92
  return {
61
93
  type: hasInMap.name,
62
- value: undefined,
63
- hex,
94
+ value: iterator.getSlice(size),
95
+ minVintWidth,
64
96
  };
65
97
  }
66
98
 
67
99
  if (hasInMap.type === 'children') {
68
- const children: EbmlParsed<Ebml>[] = [];
100
+ const children: PossibleEbml[] = [];
69
101
  const startOffset = iterator.counter.getOffset();
70
102
 
71
103
  // eslint-disable-next-line no-constant-condition
72
104
  while (true) {
73
- const value = parseEbml(iterator);
74
- children.push(value);
105
+ const offset = iterator.counter.getOffset();
106
+ const value = await parseEbml(iterator, parserContext);
107
+ const remapped = await postprocessEbml({
108
+ offset,
109
+ ebml: value,
110
+ parserContext,
111
+ });
112
+ children.push(remapped);
113
+
75
114
  const offsetNow = iterator.counter.getOffset();
76
115
 
77
116
  if (offsetNow - startOffset > size) {
@@ -85,9 +124,124 @@ export const parseEbml = (iterator: BufferIterator): Prettify<PossibleEbml> => {
85
124
  }
86
125
  }
87
126
 
88
- return {type: hasInMap.name, value: children as EbmlParsed<Ebml>[], hex};
127
+ return {type: hasInMap.name, value: children, minVintWidth};
89
128
  }
90
129
 
91
130
  // @ts-expect-error
92
131
  throw new Error(`Unknown segment type ${hasInMap.type}`);
93
132
  };
133
+
134
+ export const postprocessEbml = async ({
135
+ offset,
136
+ ebml,
137
+ parserContext,
138
+ }: {
139
+ offset: number;
140
+ ebml: Prettify<PossibleEbml>;
141
+ parserContext: ParserContext;
142
+ }): Promise<Prettify<PossibleEbml>> => {
143
+ if (ebml.type === 'TimestampScale') {
144
+ parserContext.parserState.setTimescale(ebml.value.value);
145
+ }
146
+
147
+ if (ebml.type === 'TrackEntry') {
148
+ parserContext.parserState.onTrackEntrySegment(ebml);
149
+
150
+ const track = getTrack({
151
+ track: ebml,
152
+ timescale: parserContext.parserState.getTimescale(),
153
+ });
154
+
155
+ if (track) {
156
+ await registerTrack({
157
+ state: parserContext.parserState,
158
+ options: parserContext,
159
+ track,
160
+ });
161
+ }
162
+ }
163
+
164
+ if (ebml.type === 'Timestamp') {
165
+ parserContext.parserState.setTimestampOffset(offset, ebml.value.value);
166
+ }
167
+
168
+ if (ebml.type === 'Block' || ebml.type === 'SimpleBlock') {
169
+ const sample = getSampleFromBlock(ebml, parserContext, offset);
170
+
171
+ if (sample.type === 'video-sample' && parserContext.nullifySamples) {
172
+ await parserContext.parserState.onVideoSample(
173
+ sample.videoSample.trackId,
174
+ sample.videoSample,
175
+ );
176
+ return {
177
+ type: 'Block',
178
+ value: new Uint8Array([]),
179
+ minVintWidth: ebml.minVintWidth,
180
+ };
181
+ }
182
+
183
+ if (sample.type === 'audio-sample' && parserContext.nullifySamples) {
184
+ await parserContext.parserState.onAudioSample(
185
+ sample.audioSample.trackId,
186
+ sample.audioSample,
187
+ );
188
+ return {
189
+ type: 'Block',
190
+ value: new Uint8Array([]),
191
+ minVintWidth: ebml.minVintWidth,
192
+ };
193
+ }
194
+
195
+ if (sample.type === 'no-sample' && parserContext.nullifySamples) {
196
+ return {
197
+ type: 'Block',
198
+ value: new Uint8Array([]),
199
+ minVintWidth: ebml.minVintWidth,
200
+ };
201
+ }
202
+ }
203
+
204
+ if (ebml.type === 'BlockGroup') {
205
+ // Blocks don't have information about keyframes.
206
+ // https://ffmpeg.org/pipermail/ffmpeg-devel/2015-June/173825.html
207
+ // "For Blocks, keyframes is
208
+ // inferred by the absence of ReferenceBlock element (as done by matroskadec).""
209
+
210
+ const block = ebml.value.find(
211
+ (c) => c.type === 'SimpleBlock' || c.type === 'Block',
212
+ );
213
+ if (!block || (block.type !== 'SimpleBlock' && block.type !== 'Block')) {
214
+ throw new Error('Expected block segment');
215
+ }
216
+
217
+ const hasReferenceBlock = ebml.value.find(
218
+ (c) => c.type === 'ReferenceBlock',
219
+ );
220
+
221
+ const sample =
222
+ block.value.length === 0
223
+ ? null
224
+ : getSampleFromBlock(block, parserContext, offset);
225
+
226
+ if (sample && sample.type === 'partial-video-sample') {
227
+ const completeFrame: VideoSample = {
228
+ ...sample.partialVideoSample,
229
+ type: hasReferenceBlock ? 'delta' : 'key',
230
+ };
231
+ await parserContext.parserState.onVideoSample(
232
+ sample.partialVideoSample.trackId,
233
+ completeFrame,
234
+ );
235
+ }
236
+
237
+ if (parserContext.nullifySamples) {
238
+ return {
239
+ type: 'BlockGroup',
240
+ value: [],
241
+ minVintWidth: ebml.minVintWidth,
242
+ };
243
+ }
244
+ }
245
+
246
+ return ebml;
247
+ };