@remotion/media-parser 4.0.209 → 4.0.211

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 (290) hide show
  1. package/buffer.js +2 -0
  2. package/dist/boxes/iso-base-media/get-sample-positions-from-track.js +1 -1
  3. package/dist/boxes/iso-base-media/make-track.js +14 -2
  4. package/dist/boxes/iso-base-media/mdat/mdat.d.ts +13 -4
  5. package/dist/boxes/iso-base-media/mdat/mdat.js +20 -5
  6. package/dist/boxes/iso-base-media/moov/moov.d.ts +2 -1
  7. package/dist/boxes/iso-base-media/moov/moov.js +2 -1
  8. package/dist/boxes/iso-base-media/process-box.d.ts +6 -3
  9. package/dist/boxes/iso-base-media/process-box.js +66 -12
  10. package/dist/boxes/iso-base-media/stsd/avcc.d.ts +1 -1
  11. package/dist/boxes/iso-base-media/stsd/avcc.js +2 -2
  12. package/dist/boxes/iso-base-media/stsd/ctts.js +2 -5
  13. package/dist/boxes/iso-base-media/stsd/hvcc.d.ts +1 -1
  14. package/dist/boxes/iso-base-media/stsd/hvcc.js +2 -2
  15. package/dist/boxes/iso-base-media/stsd/mebx.d.ts +2 -1
  16. package/dist/boxes/iso-base-media/stsd/mebx.js +2 -1
  17. package/dist/boxes/iso-base-media/stsd/samples.d.ts +4 -2
  18. package/dist/boxes/iso-base-media/stsd/samples.js +69 -11
  19. package/dist/boxes/iso-base-media/stsd/stsd.d.ts +2 -1
  20. package/dist/boxes/iso-base-media/stsd/stsd.js +2 -1
  21. package/dist/boxes/iso-base-media/trak/trak.d.ts +2 -1
  22. package/dist/boxes/iso-base-media/trak/trak.js +2 -1
  23. package/dist/boxes/iso-base-media/traversal.d.ts +39 -0
  24. package/dist/boxes/iso-base-media/traversal.js +201 -0
  25. package/dist/boxes/webm/color.d.ts +4 -0
  26. package/dist/boxes/webm/color.js +127 -0
  27. package/dist/boxes/webm/description.js +1 -1
  28. package/dist/boxes/webm/ebml.d.ts +1 -1
  29. package/dist/boxes/webm/get-ready-tracks.js +3 -3
  30. package/dist/boxes/webm/get-sample-from-block.js +1 -1
  31. package/dist/boxes/webm/make-track.d.ts +9 -0
  32. package/dist/boxes/webm/make-track.js +260 -0
  33. package/dist/boxes/webm/parse-ebml.js +2 -2
  34. package/dist/boxes/webm/segments/all-segments.d.ts +36 -3
  35. package/dist/boxes/webm/segments/all-segments.js +34 -6
  36. package/dist/boxes/webm/segments/parse-children.js +6 -0
  37. package/dist/boxes/webm/traversal.d.ts +26 -1
  38. package/dist/boxes/webm/traversal.js +214 -1
  39. package/dist/buffer-iterator.d.ts +4 -1
  40. package/dist/buffer-iterator.js +31 -5
  41. package/dist/create/cluster-segment.js +0 -2
  42. package/dist/create/cluster.d.ts +16 -2
  43. package/dist/create/cluster.js +17 -10
  44. package/dist/create/create-media.d.ts +4 -2
  45. package/dist/create/create-media.js +101 -22
  46. package/dist/create/make-duration-with-padding.d.ts +1 -1
  47. package/dist/create/make-duration-with-padding.js +3 -3
  48. package/dist/create/matroska-cues.d.ts +6 -0
  49. package/dist/create/matroska-cues.js +49 -0
  50. package/dist/create/matroska-info.js +1 -1
  51. package/dist/create/matroska-seek.d.ts +6 -0
  52. package/dist/create/matroska-seek.js +32 -0
  53. package/dist/create/matroska-segment.d.ts +1 -0
  54. package/dist/create/matroska-segment.js +3 -2
  55. package/dist/create/matroska-trackentry.d.ts +9 -14
  56. package/dist/create/matroska-trackentry.js +79 -102
  57. package/dist/emit-available-info.d.ts +12 -0
  58. package/dist/emit-available-info.js +133 -0
  59. package/dist/esm/buffer.mjs +57 -0
  60. package/dist/esm/from-fetch.mjs +107 -0
  61. package/dist/esm/from-node.mjs +49 -0
  62. package/dist/esm/from-web-file.mjs +49 -0
  63. package/dist/esm/index.mjs +6822 -0
  64. package/dist/esm/web-fs.mjs +67 -0
  65. package/dist/get-audio-codec.d.ts +5 -5
  66. package/dist/get-audio-codec.js +44 -57
  67. package/dist/get-container.d.ts +4 -0
  68. package/dist/get-container.js +26 -0
  69. package/dist/get-duration.js +1 -1
  70. package/dist/get-fps.js +1 -1
  71. package/dist/get-sample-aspect-ratio.d.ts +1 -1
  72. package/dist/get-sample-aspect-ratio.js +19 -19
  73. package/dist/get-tracks.d.ts +13 -0
  74. package/dist/get-tracks.js +8 -8
  75. package/dist/get-video-codec.d.ts +5 -2
  76. package/dist/get-video-codec.js +111 -21
  77. package/dist/has-all-info.d.ts +2 -2
  78. package/dist/has-all-info.js +23 -9
  79. package/dist/index.d.ts +4 -1
  80. package/dist/options.d.ts +97 -30
  81. package/dist/parse-media.js +59 -69
  82. package/dist/parse-video.d.ts +2 -1
  83. package/dist/parse-video.js +3 -1
  84. package/dist/parser-context.d.ts +1 -0
  85. package/dist/parser-state.js +1 -2
  86. package/dist/readers/from-fetch.d.ts +12 -0
  87. package/dist/readers/from-fetch.js +83 -17
  88. package/dist/readers/from-node.js +14 -3
  89. package/dist/readers/from-web-file.js +15 -2
  90. package/dist/readers/reader.d.ts +7 -1
  91. package/dist/samples-from-moof.js +1 -1
  92. package/dist/truthy.d.ts +3 -0
  93. package/dist/truthy.js +7 -0
  94. package/dist/writers/buffer.d.ts +2 -0
  95. package/dist/writers/buffer.js +58 -0
  96. package/dist/writers/web-fs.d.ts +1 -0
  97. package/dist/writers/web-fs.js +42 -18
  98. package/dist/writers/writer.d.ts +4 -2
  99. package/fetch.js +2 -0
  100. package/node.js +2 -0
  101. package/package.json +43 -8
  102. package/web-file.js +2 -0
  103. package/web-fs.js +2 -0
  104. package/.eslintrc +0 -8
  105. package/dist/av1-codec-string.d.ts +0 -3
  106. package/dist/av1-codec-string.js +0 -91
  107. package/dist/boxes/iso-base-media/ftype.d.ts +0 -9
  108. package/dist/boxes/iso-base-media/ftype.js +0 -31
  109. package/dist/boxes/iso-base-media/stsd/avcc-hvcc.d.ts +0 -20
  110. package/dist/boxes/iso-base-media/stsd/avcc-hvcc.js +0 -73
  111. package/dist/boxes/iso-base-media/stts/stts.d.ts +0 -15
  112. package/dist/boxes/iso-base-media/stts/stts.js +0 -35
  113. package/dist/boxes/webm/bitstream/av1/bitstream-frame-header.d.ts +0 -14
  114. package/dist/boxes/webm/bitstream/av1/bitstream-frame-header.js +0 -67
  115. package/dist/boxes/webm/bitstream/av1/bitstream-frame.d.ts +0 -11
  116. package/dist/boxes/webm/bitstream/av1/bitstream-frame.js +0 -14
  117. package/dist/boxes/webm/bitstream/av1/chroma-sample-position.d.ts +0 -6
  118. package/dist/boxes/webm/bitstream/av1/chroma-sample-position.js +0 -9
  119. package/dist/boxes/webm/bitstream/av1/color-config.d.ts +0 -16
  120. package/dist/boxes/webm/bitstream/av1/color-config.js +0 -103
  121. package/dist/boxes/webm/bitstream/av1/color-primaries.d.ts +0 -14
  122. package/dist/boxes/webm/bitstream/av1/color-primaries.js +0 -17
  123. package/dist/boxes/webm/bitstream/av1/decoder-model-info.d.ts +0 -9
  124. package/dist/boxes/webm/bitstream/av1/decoder-model-info.js +0 -17
  125. package/dist/boxes/webm/bitstream/av1/frame.d.ts +0 -0
  126. package/dist/boxes/webm/bitstream/av1/frame.js +0 -1
  127. package/dist/boxes/webm/bitstream/av1/header-segment.d.ts +0 -51
  128. package/dist/boxes/webm/bitstream/av1/header-segment.js +0 -183
  129. package/dist/boxes/webm/bitstream/av1/matrix-coefficients.d.ts +0 -17
  130. package/dist/boxes/webm/bitstream/av1/matrix-coefficients.js +0 -20
  131. package/dist/boxes/webm/bitstream/av1/operating-parameters-info.d.ts +0 -10
  132. package/dist/boxes/webm/bitstream/av1/operating-parameters-info.js +0 -15
  133. package/dist/boxes/webm/bitstream/av1/temporal-point-info.d.ts +0 -5
  134. package/dist/boxes/webm/bitstream/av1/temporal-point-info.js +0 -8
  135. package/dist/boxes/webm/bitstream/av1/timing-info.d.ts +0 -8
  136. package/dist/boxes/webm/bitstream/av1/timing-info.js +0 -20
  137. package/dist/boxes/webm/bitstream/av1/transfer-characteristics.d.ts +0 -21
  138. package/dist/boxes/webm/bitstream/av1/transfer-characteristics.js +0 -24
  139. package/dist/boxes/webm/bitstream/av1/uvlc.d.ts +0 -2
  140. package/dist/boxes/webm/bitstream/av1/uvlc.js +0 -20
  141. package/dist/boxes/webm/bitstream/av1.d.ts +0 -20
  142. package/dist/boxes/webm/bitstream/av1.js +0 -118
  143. package/dist/boxes/webm/bitstream/h264/get-h264-descriptor.d.ts +0 -0
  144. package/dist/boxes/webm/bitstream/h264/get-h264-descriptor.js +0 -1
  145. package/dist/boxes/webm/segments/duration.d.ts +0 -6
  146. package/dist/boxes/webm/segments/duration.js +0 -19
  147. package/dist/boxes/webm/segments/info.d.ts +0 -9
  148. package/dist/boxes/webm/segments/info.js +0 -22
  149. package/dist/boxes/webm/segments/main.d.ts +0 -5
  150. package/dist/boxes/webm/segments/main.js +0 -2
  151. package/dist/boxes/webm/segments/muxing.d.ts +0 -6
  152. package/dist/boxes/webm/segments/muxing.js +0 -11
  153. package/dist/boxes/webm/segments/seek-head.d.ts +0 -9
  154. package/dist/boxes/webm/segments/seek-head.js +0 -22
  155. package/dist/boxes/webm/segments/seek-position.d.ts +0 -6
  156. package/dist/boxes/webm/segments/seek-position.js +0 -11
  157. package/dist/boxes/webm/segments/seek.d.ts +0 -13
  158. package/dist/boxes/webm/segments/seek.js +0 -35
  159. package/dist/boxes/webm/segments/timestamp-scale.d.ts +0 -6
  160. package/dist/boxes/webm/segments/timestamp-scale.js +0 -11
  161. package/dist/boxes/webm/segments/tracks.d.ts +0 -8
  162. package/dist/boxes/webm/segments/tracks.js +0 -21
  163. package/dist/boxes/webm/segments/unknown.d.ts +0 -6
  164. package/dist/boxes/webm/segments/unknown.js +0 -11
  165. package/dist/boxes/webm/segments/void.d.ts +0 -6
  166. package/dist/boxes/webm/segments/void.js +0 -11
  167. package/dist/boxes/webm/segments/writing.d.ts +0 -6
  168. package/dist/boxes/webm/segments/writing.js +0 -11
  169. package/dist/boxes/webm/tracks.d.ts +0 -8
  170. package/dist/boxes/webm/tracks.js +0 -21
  171. package/dist/combine-uint8array.d.ts +0 -1
  172. package/dist/combine-uint8array.js +0 -13
  173. package/dist/from-web.d.ts +0 -2
  174. package/dist/from-web.js +0 -45
  175. package/dist/get-video-metadata.d.ts +0 -2
  176. package/dist/get-video-metadata.js +0 -44
  177. package/dist/read-and-increment-offset.d.ts +0 -28
  178. package/dist/read-and-increment-offset.js +0 -177
  179. package/dist/understand-vorbis.d.ts +0 -1
  180. package/dist/understand-vorbis.js +0 -12
  181. package/input.webm +0 -0
  182. package/src/add-new-matroska-tracks.ts +0 -23
  183. package/src/boxes/iso-base-media/base-type.ts +0 -4
  184. package/src/boxes/iso-base-media/esds/decoder-specific-config.ts +0 -68
  185. package/src/boxes/iso-base-media/esds/esds-descriptors.ts +0 -135
  186. package/src/boxes/iso-base-media/esds/esds.ts +0 -49
  187. package/src/boxes/iso-base-media/ftyp.ts +0 -39
  188. package/src/boxes/iso-base-media/get-sample-positions-from-track.ts +0 -69
  189. package/src/boxes/iso-base-media/make-track.ts +0 -116
  190. package/src/boxes/iso-base-media/mdat/mdat.ts +0 -140
  191. package/src/boxes/iso-base-media/mdhd.ts +0 -59
  192. package/src/boxes/iso-base-media/moov/moov.ts +0 -43
  193. package/src/boxes/iso-base-media/mvhd.ts +0 -114
  194. package/src/boxes/iso-base-media/process-box.ts +0 -748
  195. package/src/boxes/iso-base-media/stsd/av1c.ts +0 -19
  196. package/src/boxes/iso-base-media/stsd/avcc.ts +0 -36
  197. package/src/boxes/iso-base-media/stsd/colr.ts +0 -49
  198. package/src/boxes/iso-base-media/stsd/ctts.ts +0 -64
  199. package/src/boxes/iso-base-media/stsd/hvcc.ts +0 -32
  200. package/src/boxes/iso-base-media/stsd/keys.ts +0 -27
  201. package/src/boxes/iso-base-media/stsd/mebx.ts +0 -54
  202. package/src/boxes/iso-base-media/stsd/pasp.ts +0 -32
  203. package/src/boxes/iso-base-media/stsd/samples.ts +0 -359
  204. package/src/boxes/iso-base-media/stsd/stco.ts +0 -52
  205. package/src/boxes/iso-base-media/stsd/stsc.ts +0 -61
  206. package/src/boxes/iso-base-media/stsd/stsd.ts +0 -55
  207. package/src/boxes/iso-base-media/stsd/stss.ts +0 -47
  208. package/src/boxes/iso-base-media/stsd/stsz.ts +0 -75
  209. package/src/boxes/iso-base-media/stsd/stts.ts +0 -62
  210. package/src/boxes/iso-base-media/tfdt.ts +0 -37
  211. package/src/boxes/iso-base-media/tfhd.ts +0 -66
  212. package/src/boxes/iso-base-media/tkhd.ts +0 -150
  213. package/src/boxes/iso-base-media/to-date.ts +0 -9
  214. package/src/boxes/iso-base-media/trak/trak.ts +0 -43
  215. package/src/boxes/iso-base-media/trun.ts +0 -74
  216. package/src/boxes/iso-base-media/void-box.ts +0 -4
  217. package/src/boxes/webm/allowed-partial-segments.ts +0 -1
  218. package/src/boxes/webm/av1-codec-private.ts +0 -113
  219. package/src/boxes/webm/description.ts +0 -104
  220. package/src/boxes/webm/ebml.ts +0 -98
  221. package/src/boxes/webm/get-ready-tracks.ts +0 -36
  222. package/src/boxes/webm/get-sample-from-block.ts +0 -125
  223. package/src/boxes/webm/get-track.ts +0 -257
  224. package/src/boxes/webm/make-header.ts +0 -253
  225. package/src/boxes/webm/parse-ebml.ts +0 -259
  226. package/src/boxes/webm/parse-webm-header.ts +0 -18
  227. package/src/boxes/webm/segments/all-segments.ts +0 -888
  228. package/src/boxes/webm/segments/block-simple-block-flags.ts +0 -52
  229. package/src/boxes/webm/segments/parse-children.ts +0 -144
  230. package/src/boxes/webm/segments/track-entry.ts +0 -38
  231. package/src/boxes/webm/segments.ts +0 -147
  232. package/src/boxes/webm/traversal.ts +0 -45
  233. package/src/buffer-iterator.ts +0 -555
  234. package/src/create/cluster-segment.ts +0 -62
  235. package/src/create/cluster.ts +0 -64
  236. package/src/create/create-media.ts +0 -136
  237. package/src/create/make-duration-with-padding.ts +0 -15
  238. package/src/create/matroska-header.ts +0 -63
  239. package/src/create/matroska-info.ts +0 -30
  240. package/src/create/matroska-segment.ts +0 -10
  241. package/src/create/matroska-trackentry.ts +0 -325
  242. package/src/create/timescale.ts +0 -1
  243. package/src/get-audio-codec.ts +0 -270
  244. package/src/get-dimensions.ts +0 -47
  245. package/src/get-duration.ts +0 -103
  246. package/src/get-fps.ts +0 -113
  247. package/src/get-sample-aspect-ratio.ts +0 -204
  248. package/src/get-sample-positions.ts +0 -93
  249. package/src/get-tracks.ts +0 -147
  250. package/src/get-video-codec.ts +0 -117
  251. package/src/has-all-info.ts +0 -81
  252. package/src/index.ts +0 -18
  253. package/src/make-hvc1-codec-strings.ts +0 -55
  254. package/src/options.ts +0 -118
  255. package/src/parse-media.ts +0 -183
  256. package/src/parse-result.ts +0 -79
  257. package/src/parse-video.ts +0 -83
  258. package/src/parser-context.ts +0 -10
  259. package/src/parser-state.ts +0 -178
  260. package/src/readers/from-fetch.ts +0 -90
  261. package/src/readers/from-node.ts +0 -51
  262. package/src/readers/from-web-file.ts +0 -49
  263. package/src/readers/reader.ts +0 -15
  264. package/src/samples-from-moof.ts +0 -102
  265. package/src/test/aspect-ratio.test.ts +0 -42
  266. package/src/test/av1.test.ts +0 -108
  267. package/src/test/create-matroska.test.ts +0 -287
  268. package/src/test/duration.test.ts +0 -18
  269. package/src/test/keys.test.ts +0 -47
  270. package/src/test/matroska.test.ts +0 -463
  271. package/src/test/mvhd.test.ts +0 -94
  272. package/src/test/parse-esds.test.ts +0 -98
  273. package/src/test/parse-stco.test.ts +0 -61
  274. package/src/test/parse-stsc.test.ts +0 -104
  275. package/src/test/parse-stsz.test.ts +0 -57
  276. package/src/test/parse-stts.test.ts +0 -38
  277. package/src/test/parse-video.test.ts +0 -101
  278. package/src/test/parse-webm.test.ts +0 -17
  279. package/src/test/samples-from-moof.test.ts +0 -2496
  280. package/src/test/stream-local.test.ts +0 -743
  281. package/src/test/stream-remote.test.ts +0 -59
  282. package/src/test/stream-samples.test.ts +0 -181
  283. package/src/test/stsd.test.ts +0 -265
  284. package/src/test/tkhd.test.ts +0 -87
  285. package/src/traversal.ts +0 -528
  286. package/src/webcodec-sample-types.ts +0 -29
  287. package/src/writers/web-fs.ts +0 -50
  288. package/src/writers/writer.ts +0 -12
  289. package/tsconfig.json +0 -14
  290. package/tsconfig.tsbuildinfo +0 -1
@@ -1,748 +0,0 @@
1
- /* eslint-disable max-depth */
2
- import type {BufferIterator} from '../../buffer-iterator';
3
- import {hasTracks} from '../../get-tracks';
4
- import type {
5
- AnySegment,
6
- IsoBaseMediaBox,
7
- ParseResult,
8
- } from '../../parse-result';
9
- import type {BoxAndNext, PartialMdatBox} from '../../parse-video';
10
- import type {ParserContext} from '../../parser-context';
11
- import {hasSkippedMdatProcessing} from '../../traversal';
12
- import {parseEsds} from './esds/esds';
13
- import {parseFtyp} from './ftyp';
14
- import {makeBaseMediaTrack} from './make-track';
15
- import {parseMdat} from './mdat/mdat';
16
- import {parseMdhd} from './mdhd';
17
- import {parseMoov} from './moov/moov';
18
- import {parseMvhd} from './mvhd';
19
- import {parseAv1C} from './stsd/av1c';
20
- import {parseAvcc} from './stsd/avcc';
21
- import {parseColorParameterBox} from './stsd/colr';
22
- import {parseCtts} from './stsd/ctts';
23
- import {parseHvcc} from './stsd/hvcc';
24
- import {parseMebx} from './stsd/mebx';
25
- import {parsePasp} from './stsd/pasp';
26
- import {parseStco} from './stsd/stco';
27
- import {parseStsc} from './stsd/stsc';
28
- import {parseStsd} from './stsd/stsd';
29
- import {parseStss} from './stsd/stss';
30
- import {parseStsz} from './stsd/stsz';
31
- import {parseStts} from './stsd/stts';
32
- import {parseTfdt} from './tfdt';
33
- import {getTfhd} from './tfhd';
34
- import {parseTkhd} from './tkhd';
35
- import {parseTrak} from './trak/trak';
36
- import {parseTrun} from './trun';
37
-
38
- const getChildren = async ({
39
- boxType,
40
- iterator,
41
- bytesRemainingInBox,
42
- options,
43
- littleEndian,
44
- }: {
45
- boxType: string;
46
- iterator: BufferIterator;
47
- bytesRemainingInBox: number;
48
- options: ParserContext;
49
- littleEndian: boolean;
50
- }) => {
51
- const parseChildren =
52
- boxType === 'mdia' ||
53
- boxType === 'minf' ||
54
- boxType === 'stbl' ||
55
- boxType === 'moof' ||
56
- boxType === 'dims' ||
57
- boxType === 'wave' ||
58
- boxType === 'traf' ||
59
- boxType === 'stsb';
60
-
61
- if (parseChildren) {
62
- const parsed = await parseBoxes({
63
- iterator,
64
- maxBytes: bytesRemainingInBox,
65
- allowIncompleteBoxes: false,
66
- initialBoxes: [],
67
- options,
68
- continueMdat: false,
69
- littleEndian,
70
- });
71
-
72
- if (parsed.status === 'incomplete') {
73
- throw new Error('Incomplete boxes are not allowed');
74
- }
75
-
76
- return parsed.segments;
77
- }
78
-
79
- if (bytesRemainingInBox < 0) {
80
- throw new Error('Box size is too big ' + JSON.stringify({boxType}));
81
- }
82
-
83
- iterator.discard(bytesRemainingInBox);
84
- return [];
85
- };
86
-
87
- export const parseMdatPartially = async ({
88
- iterator,
89
- boxSize,
90
- fileOffset,
91
- parsedBoxes,
92
- options,
93
- }: {
94
- iterator: BufferIterator;
95
- boxSize: number;
96
- fileOffset: number;
97
- parsedBoxes: AnySegment[];
98
- options: ParserContext;
99
- }): Promise<BoxAndNext> => {
100
- const box = await parseMdat({
101
- data: iterator,
102
- size: boxSize,
103
- fileOffset,
104
- existingBoxes: parsedBoxes,
105
- options,
106
- });
107
-
108
- if (
109
- box.samplesProcessed &&
110
- box.fileOffset + boxSize === iterator.counter.getOffset()
111
- ) {
112
- return {
113
- type: 'complete',
114
- box,
115
- size: boxSize,
116
- skipTo: null,
117
- };
118
- }
119
-
120
- return {
121
- type: 'partial-mdat-box',
122
- boxSize,
123
- fileOffset,
124
- };
125
- };
126
-
127
- export const processBox = async ({
128
- iterator,
129
- allowIncompleteBoxes,
130
- parsedBoxes,
131
- options,
132
- littleEndian,
133
- }: {
134
- iterator: BufferIterator;
135
- allowIncompleteBoxes: boolean;
136
- parsedBoxes: AnySegment[];
137
- options: ParserContext;
138
- littleEndian: boolean;
139
- }): Promise<BoxAndNext> => {
140
- const fileOffset = iterator.counter.getOffset();
141
- const bytesRemaining = iterator.bytesRemaining();
142
-
143
- const boxSizeRaw = iterator.getFourByteNumber(littleEndian);
144
-
145
- // If `boxSize === 1`, the 8 bytes after the box type are the size of the box.
146
- if (
147
- (boxSizeRaw === 1 && iterator.bytesRemaining() < 12) ||
148
- iterator.bytesRemaining() < 4
149
- ) {
150
- iterator.counter.decrement(iterator.counter.getOffset() - fileOffset);
151
- if (allowIncompleteBoxes) {
152
- return {
153
- type: 'incomplete',
154
- };
155
- }
156
-
157
- throw new Error(
158
- `Expected box size of ${bytesRemaining}, got ${boxSizeRaw}. Incomplete boxes are not allowed.`,
159
- );
160
- }
161
-
162
- if (boxSizeRaw === 0) {
163
- return {
164
- type: 'complete',
165
- box: {
166
- type: 'void-box',
167
- boxSize: 0,
168
- },
169
- size: 4,
170
- skipTo: null,
171
- };
172
- }
173
-
174
- const boxType = iterator.getByteString(4);
175
-
176
- const boxSize =
177
- boxSizeRaw === 1 ? iterator.getEightByteNumber(littleEndian) : boxSizeRaw;
178
-
179
- if (bytesRemaining < boxSize) {
180
- if (boxType === 'mdat') {
181
- const shouldSkip = options.canSkipVideoData || !hasTracks(parsedBoxes);
182
-
183
- if (shouldSkip) {
184
- const skipTo = fileOffset + boxSize;
185
- const bytesToSkip = skipTo - iterator.counter.getOffset();
186
-
187
- // If there is a huge mdat chunk, we can skip it because we don't need it for the metadata
188
- if (bytesToSkip > 1_000_000) {
189
- return {
190
- type: 'complete',
191
- box: {
192
- type: 'mdat-box',
193
- boxSize,
194
- fileOffset,
195
- samplesProcessed: false,
196
- },
197
- size: boxSize,
198
- skipTo: fileOffset + boxSize,
199
- };
200
- }
201
- } else {
202
- return parseMdatPartially({
203
- iterator,
204
- boxSize,
205
- fileOffset,
206
- parsedBoxes,
207
- options,
208
- });
209
- }
210
- }
211
-
212
- iterator.counter.decrement(iterator.counter.getOffset() - fileOffset);
213
- if (allowIncompleteBoxes) {
214
- return {
215
- type: 'incomplete',
216
- };
217
- }
218
-
219
- throw new Error(
220
- `Expected box size of ${bytesRemaining}, got ${boxSize}. Incomplete boxes are not allowed.`,
221
- );
222
- }
223
-
224
- if (boxType === 'ftyp') {
225
- const box = parseFtyp({iterator, size: boxSize, offset: fileOffset});
226
- return {
227
- type: 'complete',
228
- box,
229
- size: boxSize,
230
- skipTo: null,
231
- };
232
- }
233
-
234
- if (boxType === 'colr') {
235
- const box = parseColorParameterBox({
236
- iterator,
237
- });
238
- return {
239
- type: 'complete',
240
- box,
241
- size: boxSize,
242
- skipTo: null,
243
- };
244
- }
245
-
246
- if (boxType === 'mvhd') {
247
- const box = parseMvhd({iterator, offset: fileOffset, size: boxSize});
248
-
249
- return {
250
- type: 'complete',
251
- box,
252
- size: boxSize,
253
- skipTo: null,
254
- };
255
- }
256
-
257
- if (boxType === 'tkhd') {
258
- const box = parseTkhd({iterator, offset: fileOffset, size: boxSize});
259
-
260
- return {
261
- type: 'complete',
262
- box,
263
- size: boxSize,
264
- skipTo: null,
265
- };
266
- }
267
-
268
- if (boxType === 'trun') {
269
- const box = parseTrun({iterator, offset: fileOffset, size: boxSize});
270
-
271
- return {
272
- type: 'complete',
273
- box,
274
- size: boxSize,
275
- skipTo: null,
276
- };
277
- }
278
-
279
- if (boxType === 'tfdt') {
280
- const box = parseTfdt({iterator, size: boxSize, offset: fileOffset});
281
-
282
- return {
283
- type: 'complete',
284
- box,
285
- size: boxSize,
286
- skipTo: null,
287
- };
288
- }
289
-
290
- if (boxType === 'stsd') {
291
- const box = await parseStsd({
292
- iterator,
293
- offset: fileOffset,
294
- size: boxSize,
295
- options,
296
- });
297
-
298
- return {
299
- type: 'complete',
300
- box,
301
- size: boxSize,
302
- skipTo: null,
303
- };
304
- }
305
-
306
- if (boxType === 'stsz') {
307
- const box = parseStsz({
308
- iterator,
309
- offset: fileOffset,
310
- size: boxSize,
311
- });
312
-
313
- return {
314
- type: 'complete',
315
- box,
316
- size: boxSize,
317
- skipTo: null,
318
- };
319
- }
320
-
321
- if (boxType === 'stco' || boxType === 'co64') {
322
- const box = parseStco({
323
- iterator,
324
- offset: fileOffset,
325
- size: boxSize,
326
- mode64Bit: boxType === 'co64',
327
- });
328
-
329
- return {
330
- type: 'complete',
331
- box,
332
- size: boxSize,
333
- skipTo: null,
334
- };
335
- }
336
-
337
- if (boxType === 'pasp') {
338
- const box = parsePasp({
339
- iterator,
340
- offset: fileOffset,
341
- size: boxSize,
342
- });
343
-
344
- return {
345
- type: 'complete',
346
- box,
347
- size: boxSize,
348
- skipTo: null,
349
- };
350
- }
351
-
352
- if (boxType === 'stss') {
353
- const box = parseStss({
354
- iterator,
355
- offset: fileOffset,
356
- boxSize,
357
- });
358
-
359
- return {
360
- type: 'complete',
361
- box,
362
- size: boxSize,
363
- skipTo: null,
364
- };
365
- }
366
-
367
- if (boxType === 'ctts') {
368
- const box = parseCtts({
369
- iterator,
370
- offset: fileOffset,
371
- size: boxSize,
372
- });
373
-
374
- return {
375
- type: 'complete',
376
- box,
377
- size: boxSize,
378
- skipTo: null,
379
- };
380
- }
381
-
382
- if (boxType === 'stsc') {
383
- const box = parseStsc({
384
- iterator,
385
- offset: fileOffset,
386
- size: boxSize,
387
- });
388
-
389
- return {
390
- type: 'complete',
391
- box,
392
- size: boxSize,
393
- skipTo: null,
394
- };
395
- }
396
-
397
- if (boxType === 'mebx') {
398
- const box = await parseMebx({
399
- iterator,
400
- offset: fileOffset,
401
- size: boxSize,
402
- options,
403
- littleEndian,
404
- });
405
-
406
- return {
407
- type: 'complete',
408
- box,
409
- size: boxSize,
410
- skipTo: null,
411
- };
412
- }
413
-
414
- if (boxType === 'moov') {
415
- const box = await parseMoov({
416
- iterator,
417
- offset: fileOffset,
418
- size: boxSize,
419
- options,
420
- });
421
-
422
- return {
423
- type: 'complete',
424
- box,
425
- size: boxSize,
426
- skipTo: null,
427
- };
428
- }
429
-
430
- if (boxType === 'trak') {
431
- const box = await parseTrak({
432
- data: iterator,
433
- size: boxSize,
434
- offsetAtStart: fileOffset,
435
- options,
436
- });
437
- const transformedTrack = makeBaseMediaTrack(box);
438
- if (transformedTrack) {
439
- if (transformedTrack.type === 'audio') {
440
- const callback = await options.onAudioTrack?.(transformedTrack);
441
- await options.parserState.registerAudioSampleCallback(
442
- transformedTrack.trackId,
443
- callback ?? null,
444
- );
445
- }
446
-
447
- if (transformedTrack.type === 'video') {
448
- const callback = await options.onVideoTrack?.(transformedTrack);
449
- await options.parserState.registerVideoSampleCallback(
450
- transformedTrack.trackId,
451
- callback ?? null,
452
- );
453
- }
454
- }
455
-
456
- return {
457
- type: 'complete',
458
- box,
459
- size: boxSize,
460
- skipTo: null,
461
- };
462
- }
463
-
464
- if (boxType === 'stts') {
465
- const box = parseStts({
466
- data: iterator,
467
- size: boxSize,
468
- fileOffset,
469
- });
470
-
471
- return {
472
- type: 'complete',
473
- box,
474
- size: boxSize,
475
- skipTo: null,
476
- };
477
- }
478
-
479
- if (boxType === 'avcC') {
480
- const box = parseAvcc({
481
- data: iterator,
482
- size: boxSize,
483
- });
484
-
485
- return {
486
- type: 'complete',
487
- box,
488
- size: boxSize,
489
- skipTo: null,
490
- };
491
- }
492
-
493
- if (boxType === 'av1C') {
494
- const box = parseAv1C({
495
- data: iterator,
496
- size: boxSize,
497
- });
498
-
499
- return {
500
- type: 'complete',
501
- box,
502
- size: boxSize,
503
- skipTo: null,
504
- };
505
- }
506
-
507
- if (boxType === 'hvcC') {
508
- const box = parseHvcc({
509
- data: iterator,
510
- size: boxSize,
511
- offset: fileOffset,
512
- });
513
-
514
- return {
515
- type: 'complete',
516
- box,
517
- size: boxSize,
518
- skipTo: null,
519
- };
520
- }
521
-
522
- if (boxType === 'tfhd') {
523
- const box = getTfhd({
524
- iterator,
525
- offset: fileOffset,
526
- size: boxSize,
527
- });
528
-
529
- return {
530
- type: 'complete',
531
- box,
532
- size: boxSize,
533
- skipTo: null,
534
- };
535
- }
536
-
537
- if (boxType === 'mdhd') {
538
- const box = parseMdhd({
539
- data: iterator,
540
- size: boxSize,
541
- fileOffset,
542
- });
543
-
544
- return {
545
- type: 'complete',
546
- box,
547
- size: boxSize,
548
- skipTo: null,
549
- };
550
- }
551
-
552
- if (boxType === 'esds') {
553
- const box = parseEsds({
554
- data: iterator,
555
- size: boxSize,
556
- fileOffset,
557
- });
558
-
559
- return {
560
- type: 'complete',
561
- box,
562
- size: boxSize,
563
- skipTo: null,
564
- };
565
- }
566
-
567
- if (boxType === 'mdat') {
568
- const box = await parseMdat({
569
- data: iterator,
570
- size: boxSize,
571
- fileOffset,
572
- existingBoxes: parsedBoxes,
573
- options,
574
- });
575
-
576
- return {
577
- type: 'complete',
578
- box,
579
- size: boxSize,
580
- skipTo: null,
581
- };
582
- }
583
-
584
- const bytesRemainingInBox =
585
- boxSize - (iterator.counter.getOffset() - fileOffset);
586
-
587
- const children = await getChildren({
588
- boxType,
589
- iterator,
590
- bytesRemainingInBox,
591
- options,
592
- littleEndian,
593
- });
594
-
595
- return {
596
- type: 'complete',
597
- box: {
598
- type: 'regular-box',
599
- boxType,
600
- boxSize,
601
- children,
602
- offset: fileOffset,
603
- },
604
- size: boxSize,
605
- skipTo: null,
606
- };
607
- };
608
-
609
- export const parseBoxes = async ({
610
- iterator,
611
- maxBytes,
612
- allowIncompleteBoxes,
613
- initialBoxes,
614
- options,
615
- continueMdat,
616
- littleEndian,
617
- }: {
618
- iterator: BufferIterator;
619
- maxBytes: number;
620
- allowIncompleteBoxes: boolean;
621
- initialBoxes: IsoBaseMediaBox[];
622
- options: ParserContext;
623
- continueMdat: false | PartialMdatBox;
624
- littleEndian: boolean;
625
- }): Promise<ParseResult> => {
626
- let boxes: IsoBaseMediaBox[] = initialBoxes;
627
- const initialOffset = iterator.counter.getOffset();
628
- const alreadyHasMdat = boxes.find((b) => b.type === 'mdat-box');
629
-
630
- while (
631
- iterator.bytesRemaining() > 0 &&
632
- iterator.counter.getOffset() - initialOffset < maxBytes
633
- ) {
634
- const result = continueMdat
635
- ? await parseMdatPartially({
636
- iterator,
637
- boxSize: continueMdat.boxSize,
638
- fileOffset: continueMdat.fileOffset,
639
- parsedBoxes: initialBoxes,
640
- options,
641
- })
642
- : await processBox({
643
- iterator,
644
- allowIncompleteBoxes,
645
- parsedBoxes: initialBoxes,
646
- options,
647
- littleEndian,
648
- });
649
-
650
- if (result.type === 'incomplete') {
651
- if (Number.isFinite(maxBytes)) {
652
- throw new Error('maxBytes must be Infinity for top-level boxes');
653
- }
654
-
655
- return {
656
- status: 'incomplete',
657
- segments: boxes,
658
- continueParsing: () => {
659
- return parseBoxes({
660
- iterator,
661
- maxBytes,
662
- allowIncompleteBoxes,
663
- initialBoxes: boxes,
664
- options,
665
- continueMdat: false,
666
- littleEndian,
667
- });
668
- },
669
- skipTo: null,
670
- };
671
- }
672
-
673
- if (result.type === 'partial-mdat-box') {
674
- return {
675
- status: 'incomplete',
676
- segments: boxes,
677
- continueParsing: () => {
678
- return Promise.resolve(
679
- parseBoxes({
680
- iterator,
681
- maxBytes,
682
- allowIncompleteBoxes,
683
- initialBoxes: boxes,
684
- options,
685
- continueMdat: result,
686
- littleEndian,
687
- }),
688
- );
689
- },
690
- skipTo: null,
691
- };
692
- }
693
-
694
- if (result.box.type === 'mdat-box' && alreadyHasMdat) {
695
- boxes = boxes.filter((b) => b.type !== 'mdat-box');
696
- boxes.push(result.box);
697
- break;
698
- } else {
699
- boxes.push(result.box);
700
- }
701
-
702
- if (result.skipTo !== null) {
703
- return {
704
- status: 'incomplete',
705
- segments: boxes,
706
- continueParsing: () => {
707
- return parseBoxes({
708
- iterator,
709
- maxBytes,
710
- allowIncompleteBoxes,
711
- initialBoxes: boxes,
712
- options,
713
- continueMdat: false,
714
- littleEndian,
715
- });
716
- },
717
- skipTo: result.skipTo,
718
- };
719
- }
720
-
721
- iterator.removeBytesRead();
722
- }
723
-
724
- const mdatState = hasSkippedMdatProcessing(boxes);
725
- if (mdatState.skipped && !options.canSkipVideoData) {
726
- return {
727
- status: 'incomplete',
728
- segments: boxes,
729
- continueParsing: () => {
730
- return parseBoxes({
731
- iterator,
732
- maxBytes,
733
- allowIncompleteBoxes,
734
- initialBoxes: boxes,
735
- options,
736
- continueMdat: false,
737
- littleEndian,
738
- });
739
- },
740
- skipTo: mdatState.fileOffset,
741
- };
742
- }
743
-
744
- return {
745
- status: 'done',
746
- segments: boxes,
747
- };
748
- };