@remotion/media-parser 4.0.200 → 4.0.202

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 (180) hide show
  1. package/dist/av1-codec-string.d.ts +5 -0
  2. package/dist/av1-codec-string.js +18 -1
  3. package/dist/bitstream/av1.d.ts +2 -0
  4. package/dist/bitstream/av1.js +12 -0
  5. package/dist/boxes/iso-base-media/avcc-hvcc.d.ts +20 -0
  6. package/dist/boxes/iso-base-media/avcc-hvcc.js +73 -0
  7. package/dist/boxes/iso-base-media/avcc.d.ts +18 -0
  8. package/dist/boxes/iso-base-media/avcc.js +27 -0
  9. package/dist/boxes/iso-base-media/esds-descriptors.d.ts +21 -0
  10. package/dist/boxes/iso-base-media/esds-descriptors.js +62 -0
  11. package/dist/boxes/iso-base-media/esds.d.ts +15 -0
  12. package/dist/boxes/iso-base-media/esds.js +27 -0
  13. package/dist/boxes/iso-base-media/mdat/mdat.js +2 -1
  14. package/dist/boxes/iso-base-media/moov/moov.js +1 -0
  15. package/dist/boxes/iso-base-media/mvhd.js +2 -2
  16. package/dist/boxes/iso-base-media/process-box.d.ts +4 -2
  17. package/dist/boxes/iso-base-media/process-box.js +56 -40
  18. package/dist/boxes/iso-base-media/stsd/keys.js +1 -1
  19. package/dist/boxes/iso-base-media/stsd/mebx.d.ts +2 -1
  20. package/dist/boxes/iso-base-media/stsd/mebx.js +2 -1
  21. package/dist/boxes/iso-base-media/stsd/samples.js +3 -0
  22. package/dist/boxes/iso-base-media/stsd/stco.d.ts +3 -2
  23. package/dist/boxes/iso-base-media/stsd/stco.js +2 -2
  24. package/dist/boxes/iso-base-media/trak/trak.js +1 -0
  25. package/dist/boxes/webm/av1-codec-private.js +1 -1
  26. package/dist/boxes/webm/bitstream/av1.js +10 -1
  27. package/dist/boxes/webm/description.d.ts +2 -2
  28. package/dist/boxes/webm/description.js +2 -2
  29. package/dist/boxes/webm/ebml.d.ts +2 -2
  30. package/dist/boxes/webm/ebml.js +23 -1
  31. package/dist/boxes/webm/get-ready-tracks.d.ts +1 -1
  32. package/dist/boxes/webm/get-ready-tracks.js +3 -3
  33. package/dist/boxes/webm/get-sample-from-block.d.ts +17 -0
  34. package/dist/boxes/webm/get-sample-from-block.js +78 -0
  35. package/dist/boxes/webm/get-track.d.ts +2 -2
  36. package/dist/boxes/webm/get-track.js +26 -25
  37. package/dist/boxes/webm/make-header.d.ts +3 -1
  38. package/dist/boxes/webm/make-header.js +90 -32
  39. package/dist/boxes/webm/parse-ebml.d.ts +12 -0
  40. package/dist/boxes/webm/parse-ebml.js +175 -0
  41. package/dist/boxes/webm/parse-webm-header.js +8 -9
  42. package/dist/boxes/webm/segments/all-segments.d.ts +572 -1
  43. package/dist/boxes/webm/segments/all-segments.js +353 -2
  44. package/dist/boxes/webm/segments/track-entry.d.ts +5 -189
  45. package/dist/boxes/webm/segments/track-entry.js +2 -457
  46. package/dist/boxes/webm/segments.d.ts +3 -16
  47. package/dist/boxes/webm/segments.js +40 -219
  48. package/dist/boxes/webm/traversal.d.ts +5 -5
  49. package/dist/boxes/webm/traversal.js +17 -6
  50. package/dist/buffer-iterator.d.ts +10 -7
  51. package/dist/buffer-iterator.js +83 -7
  52. package/dist/create/create-media.d.ts +2 -0
  53. package/dist/create/create-media.js +36 -0
  54. package/dist/create/matroska-header.d.ts +1 -0
  55. package/dist/create/matroska-header.js +66 -0
  56. package/dist/create/matroska-info.d.ts +4 -0
  57. package/dist/create/matroska-info.js +39 -0
  58. package/dist/create/matroska-segment.d.ts +1 -0
  59. package/dist/create/matroska-segment.js +12 -0
  60. package/dist/create/matroska-trackentry.d.ts +21 -0
  61. package/dist/create/matroska-trackentry.js +191 -0
  62. package/dist/create-media.d.ts +1 -0
  63. package/dist/create-media.js +78 -0
  64. package/dist/from-fetch.js +13 -3
  65. package/dist/from-input-type-file.d.ts +2 -0
  66. package/dist/from-input-type-file.js +37 -0
  67. package/dist/from-node.js +9 -2
  68. package/dist/from-web-file.js +6 -1
  69. package/dist/from-web.js +15 -6
  70. package/dist/get-audio-codec.d.ts +2 -2
  71. package/dist/get-audio-codec.js +13 -13
  72. package/dist/get-codec.d.ts +4 -0
  73. package/dist/get-codec.js +22 -0
  74. package/dist/get-duration.js +12 -14
  75. package/dist/get-sample-positions.js +1 -1
  76. package/dist/get-tracks.js +2 -2
  77. package/dist/get-video-codec.js +13 -13
  78. package/dist/has-all-info.js +1 -1
  79. package/dist/options.d.ts +3 -2
  80. package/dist/parse-media.js +17 -10
  81. package/dist/parse-video.js +16 -0
  82. package/dist/parser-context.d.ts +1 -0
  83. package/dist/parser-state.d.ts +4 -3
  84. package/dist/parser-state.js +16 -3
  85. package/dist/reader.d.ts +1 -1
  86. package/dist/readers/from-fetch.d.ts +2 -0
  87. package/dist/readers/from-fetch.js +64 -0
  88. package/dist/readers/from-node.d.ts +2 -0
  89. package/dist/readers/from-node.js +40 -0
  90. package/dist/readers/from-web-file.d.ts +2 -0
  91. package/dist/readers/from-web-file.js +39 -0
  92. package/dist/readers/reader.d.ts +11 -0
  93. package/dist/readers/reader.js +2 -0
  94. package/dist/traversal.d.ts +19 -17
  95. package/dist/traversal.js +38 -39
  96. package/dist/web-file.d.ts +2 -0
  97. package/dist/web-file.js +37 -0
  98. package/dist/writers/web-fs.d.ts +2 -0
  99. package/dist/writers/web-fs.js +28 -0
  100. package/dist/writers/writer.d.ts +9 -0
  101. package/dist/writers/writer.js +2 -0
  102. package/input.webm +0 -0
  103. package/package.json +2 -2
  104. package/src/boxes/iso-base-media/mdat/mdat.ts +2 -1
  105. package/src/boxes/iso-base-media/moov/moov.ts +1 -0
  106. package/src/boxes/iso-base-media/mvhd.ts +2 -2
  107. package/src/boxes/iso-base-media/process-box.ts +70 -40
  108. package/src/boxes/iso-base-media/stsd/keys.ts +1 -1
  109. package/src/boxes/iso-base-media/stsd/mebx.ts +3 -0
  110. package/src/boxes/iso-base-media/stsd/samples.ts +3 -0
  111. package/src/boxes/iso-base-media/stsd/stco.ts +5 -3
  112. package/src/boxes/iso-base-media/trak/trak.ts +1 -0
  113. package/src/boxes/webm/av1-codec-private.ts +1 -1
  114. package/src/boxes/webm/description.ts +7 -4
  115. package/src/boxes/webm/ebml.ts +24 -4
  116. package/src/boxes/webm/get-ready-tracks.ts +4 -4
  117. package/src/boxes/webm/get-sample-from-block.ts +125 -0
  118. package/src/boxes/webm/get-track.ts +38 -31
  119. package/src/boxes/webm/make-header.ts +129 -32
  120. package/src/boxes/webm/parse-ebml.ts +247 -0
  121. package/src/boxes/webm/parse-webm-header.ts +8 -12
  122. package/src/boxes/webm/segments/all-segments.ts +539 -1
  123. package/src/boxes/webm/segments/track-entry.ts +5 -843
  124. package/src/boxes/webm/segments.ts +48 -435
  125. package/src/boxes/webm/traversal.ts +28 -15
  126. package/src/buffer-iterator.ts +104 -10
  127. package/src/from-fetch.ts +22 -3
  128. package/src/from-node.ts +18 -4
  129. package/src/from-web-file.ts +11 -1
  130. package/src/get-audio-codec.ts +14 -16
  131. package/src/get-duration.ts +15 -16
  132. package/src/get-sample-positions.ts +1 -1
  133. package/src/get-tracks.ts +2 -2
  134. package/src/get-video-codec.ts +13 -15
  135. package/src/has-all-info.ts +1 -1
  136. package/src/options.ts +3 -2
  137. package/src/parse-media.ts +20 -9
  138. package/src/parse-video.ts +17 -0
  139. package/src/parser-context.ts +1 -0
  140. package/src/parser-state.ts +22 -5
  141. package/src/reader.ts +1 -0
  142. package/src/test/create-matroska.test.ts +255 -7
  143. package/src/test/matroska.test.ts +311 -334
  144. package/src/test/mvhd.test.ts +1 -1
  145. package/src/test/parse-esds.test.ts +2 -2
  146. package/src/test/parse-stco.test.ts +4 -2
  147. package/src/test/parse-stsc.test.ts +2 -2
  148. package/src/test/parse-stsz.test.ts +2 -2
  149. package/src/test/parse-stts.test.ts +1 -1
  150. package/src/test/stream-local.test.ts +23 -9
  151. package/src/test/stream-remote.test.ts +23 -19
  152. package/src/test/stsd.test.ts +6 -2
  153. package/src/test/tkhd.test.ts +1 -1
  154. package/src/traversal.ts +62 -85
  155. package/tsconfig.tsbuildinfo +1 -1
  156. package/dist/boxes/iso-base-media/ftype.d.ts +0 -9
  157. package/dist/boxes/iso-base-media/ftype.js +0 -31
  158. package/dist/get-video-metadata.d.ts +0 -2
  159. package/dist/get-video-metadata.js +0 -44
  160. package/dist/read-and-increment-offset.d.ts +0 -28
  161. package/dist/read-and-increment-offset.js +0 -177
  162. package/dist/understand-vorbis.d.ts +0 -1
  163. package/dist/understand-vorbis.js +0 -12
  164. package/src/boxes/webm/segments/duration.ts +0 -29
  165. package/src/boxes/webm/segments/info.ts +0 -34
  166. package/src/boxes/webm/segments/main.ts +0 -6
  167. package/src/boxes/webm/segments/muxing.ts +0 -18
  168. package/src/boxes/webm/segments/seek-head.ts +0 -34
  169. package/src/boxes/webm/segments/seek-position.ts +0 -18
  170. package/src/boxes/webm/segments/seek.ts +0 -45
  171. package/src/boxes/webm/segments/timestamp-scale.ts +0 -17
  172. package/src/boxes/webm/segments/tracks.ts +0 -32
  173. package/src/boxes/webm/segments/unknown.ts +0 -19
  174. package/src/boxes/webm/segments/void.ts +0 -18
  175. package/src/boxes/webm/segments/writing.ts +0 -18
  176. package/src/combine-uint8array.ts +0 -13
  177. /package/dist/{boxes/webm/bitstream/av1/frame.d.ts → get-samples.d.ts} +0 -0
  178. /package/dist/{boxes/webm/bitstream/av1/frame.js → get-samples.js} +0 -0
  179. /package/dist/{boxes/webm/bitstream/h264/get-h264-descriptor.d.ts → sample-aspect-ratio.d.ts} +0 -0
  180. /package/dist/{boxes/webm/bitstream/h264/get-h264-descriptor.js → sample-aspect-ratio.js} +0 -0
@@ -0,0 +1,247 @@
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';
7
+ import type {PossibleEbml} from './segments/all-segments';
8
+ import {ebmlMap} from './segments/all-segments';
9
+
10
+ export type Prettify<T> = {
11
+ [K in keyof T]: T[K];
12
+ } & {};
13
+
14
+ export const parseEbml = async (
15
+ iterator: BufferIterator,
16
+ parserContext: ParserContext,
17
+ ): Promise<Prettify<PossibleEbml>> => {
18
+ const hex = iterator.getMatroskaSegmentId();
19
+ if (hex === null) {
20
+ throw new Error(
21
+ 'Not enough bytes left to parse EBML - this should not happen',
22
+ );
23
+ }
24
+
25
+ const hasInMap = ebmlMap[hex as keyof typeof ebmlMap];
26
+ if (!hasInMap) {
27
+ throw new Error(
28
+ `Don't know how to parse EBML hex ID ${JSON.stringify(hex)}`,
29
+ );
30
+ }
31
+
32
+ const off = iterator.counter.getOffset();
33
+ const size = iterator.getVint();
34
+ const minVintWidth = iterator.counter.getOffset() - off;
35
+
36
+ if (size === null) {
37
+ throw new Error(
38
+ 'Not enough bytes left to parse EBML - this should not happen',
39
+ );
40
+ }
41
+
42
+ if (hasInMap.type === 'uint') {
43
+ const beforeUintOffset = iterator.counter.getOffset();
44
+ const value = iterator.getUint(size);
45
+
46
+ return {
47
+ type: hasInMap.name,
48
+ value: {
49
+ value,
50
+ byteLength: iterator.counter.getOffset() - beforeUintOffset,
51
+ },
52
+ minVintWidth,
53
+ };
54
+ }
55
+
56
+ if (hasInMap.type === 'string') {
57
+ const value = iterator.getByteString(size);
58
+
59
+ return {
60
+ type: hasInMap.name,
61
+ value,
62
+ minVintWidth,
63
+ };
64
+ }
65
+
66
+ if (hasInMap.type === 'float') {
67
+ const value = size === 4 ? iterator.getFloat32() : iterator.getFloat64();
68
+
69
+ return {
70
+ type: hasInMap.name,
71
+ value: {
72
+ value,
73
+ size: size === 4 ? '32' : '64',
74
+ },
75
+ minVintWidth,
76
+ };
77
+ }
78
+
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
+ }
90
+
91
+ if (hasInMap.type === 'uint8array') {
92
+ return {
93
+ type: hasInMap.name,
94
+ value: iterator.getSlice(size),
95
+ minVintWidth,
96
+ };
97
+ }
98
+
99
+ if (hasInMap.type === 'children') {
100
+ const children: PossibleEbml[] = [];
101
+ const startOffset = iterator.counter.getOffset();
102
+
103
+ // eslint-disable-next-line no-constant-condition
104
+ while (true) {
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
+
114
+ const offsetNow = iterator.counter.getOffset();
115
+
116
+ if (offsetNow - startOffset > size) {
117
+ throw new Error(
118
+ `Offset ${offsetNow - startOffset} is larger than the length of the hex ${size}`,
119
+ );
120
+ }
121
+
122
+ if (offsetNow - startOffset === size) {
123
+ break;
124
+ }
125
+ }
126
+
127
+ return {type: hasInMap.name, value: children, minVintWidth};
128
+ }
129
+
130
+ // @ts-expect-error
131
+ throw new Error(`Unknown segment type ${hasInMap.type}`);
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
+ };
@@ -1,22 +1,18 @@
1
1
  import type {BufferIterator} from '../../buffer-iterator';
2
2
  import type {ParseResult} from '../../parse-result';
3
3
  import type {ParserContext} from '../../parser-context';
4
- import {expectSegment} from './segments';
4
+ import {expectChildren} from './segments/parse-children';
5
5
 
6
6
  // Parsing according to https://darkcoding.net/software/reading-mediarecorders-webm-opus-output/
7
7
  export const parseWebm = (
8
8
  counter: BufferIterator,
9
9
  parserContext: ParserContext,
10
10
  ): Promise<ParseResult> => {
11
- counter.discard(4);
12
- const length = counter.getVint();
13
-
14
- if (length !== 31 && length !== 35) {
15
- throw new Error(`Expected header length 31 or 25, got ${length}`);
16
- }
17
-
18
- // Discard header for now
19
- counter.discard(length);
20
-
21
- return expectSegment(counter, parserContext);
11
+ return expectChildren({
12
+ iterator: counter,
13
+ length: Infinity,
14
+ initialChildren: [],
15
+ wrap: null,
16
+ parserContext,
17
+ });
22
18
  };