@remotion/media-parser 4.0.206 → 4.0.208

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 (143) hide show
  1. package/dist/av1-codec-string.d.ts +3 -0
  2. package/dist/av1-codec-string.js +91 -0
  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/stsd/avcc-hvcc.d.ts +20 -0
  6. package/dist/boxes/iso-base-media/stsd/avcc-hvcc.js +73 -0
  7. package/dist/boxes/iso-base-media/stsd/ctts.js +8 -1
  8. package/dist/boxes/iso-base-media/stsd/samples.d.ts +6 -3
  9. package/dist/boxes/iso-base-media/stsd/samples.js +6 -6
  10. package/dist/boxes/iso-base-media/stts/stts.d.ts +15 -0
  11. package/dist/boxes/iso-base-media/stts/stts.js +35 -0
  12. package/dist/boxes/webm/bitstream/av1/bitstream-frame-header.d.ts +14 -0
  13. package/dist/boxes/webm/bitstream/av1/bitstream-frame-header.js +67 -0
  14. package/dist/boxes/webm/bitstream/av1/bitstream-frame.d.ts +11 -0
  15. package/dist/boxes/webm/bitstream/av1/bitstream-frame.js +14 -0
  16. package/dist/boxes/webm/bitstream/av1/chroma-sample-position.d.ts +6 -0
  17. package/dist/boxes/webm/bitstream/av1/chroma-sample-position.js +9 -0
  18. package/dist/boxes/webm/bitstream/av1/color-config.d.ts +16 -0
  19. package/dist/boxes/webm/bitstream/av1/color-config.js +103 -0
  20. package/dist/boxes/webm/bitstream/av1/color-primaries.d.ts +14 -0
  21. package/dist/boxes/webm/bitstream/av1/color-primaries.js +17 -0
  22. package/dist/boxes/webm/bitstream/av1/decoder-model-info.d.ts +9 -0
  23. package/dist/boxes/webm/bitstream/av1/decoder-model-info.js +17 -0
  24. package/dist/boxes/webm/bitstream/av1/frame.d.ts +0 -0
  25. package/dist/boxes/webm/bitstream/av1/frame.js +1 -0
  26. package/dist/boxes/webm/bitstream/av1/header-segment.d.ts +51 -0
  27. package/dist/boxes/webm/bitstream/av1/header-segment.js +183 -0
  28. package/dist/boxes/webm/bitstream/av1/matrix-coefficients.d.ts +17 -0
  29. package/dist/boxes/webm/bitstream/av1/matrix-coefficients.js +20 -0
  30. package/dist/boxes/webm/bitstream/av1/operating-parameters-info.d.ts +10 -0
  31. package/dist/boxes/webm/bitstream/av1/operating-parameters-info.js +15 -0
  32. package/dist/boxes/webm/bitstream/av1/temporal-point-info.d.ts +5 -0
  33. package/dist/boxes/webm/bitstream/av1/temporal-point-info.js +8 -0
  34. package/dist/boxes/webm/bitstream/av1/timing-info.d.ts +8 -0
  35. package/dist/boxes/webm/bitstream/av1/timing-info.js +20 -0
  36. package/dist/boxes/webm/bitstream/av1/transfer-characteristics.d.ts +21 -0
  37. package/dist/boxes/webm/bitstream/av1/transfer-characteristics.js +24 -0
  38. package/dist/boxes/webm/bitstream/av1/uvlc.d.ts +2 -0
  39. package/dist/boxes/webm/bitstream/av1/uvlc.js +20 -0
  40. package/dist/boxes/webm/bitstream/av1.d.ts +20 -0
  41. package/dist/boxes/webm/bitstream/av1.js +118 -0
  42. package/dist/boxes/webm/bitstream/h264/get-h264-descriptor.d.ts +0 -0
  43. package/dist/boxes/webm/bitstream/h264/get-h264-descriptor.js +1 -0
  44. package/dist/boxes/webm/make-header.d.ts +6 -2
  45. package/dist/boxes/webm/make-header.js +102 -18
  46. package/dist/boxes/webm/parse-ebml.js +3 -1
  47. package/dist/boxes/webm/segments/all-segments.d.ts +70 -12
  48. package/dist/boxes/webm/segments/all-segments.js +43 -5
  49. package/dist/boxes/webm/segments/duration.d.ts +6 -0
  50. package/dist/boxes/webm/segments/duration.js +19 -0
  51. package/dist/boxes/webm/segments/info.d.ts +9 -0
  52. package/dist/boxes/webm/segments/info.js +22 -0
  53. package/dist/boxes/webm/segments/main.d.ts +5 -0
  54. package/dist/boxes/webm/segments/main.js +2 -0
  55. package/dist/boxes/webm/segments/muxing.d.ts +6 -0
  56. package/dist/boxes/webm/segments/muxing.js +11 -0
  57. package/dist/boxes/webm/segments/seek-head.d.ts +9 -0
  58. package/dist/boxes/webm/segments/seek-head.js +22 -0
  59. package/dist/boxes/webm/segments/seek-position.d.ts +6 -0
  60. package/dist/boxes/webm/segments/seek-position.js +11 -0
  61. package/dist/boxes/webm/segments/seek.d.ts +13 -0
  62. package/dist/boxes/webm/segments/seek.js +35 -0
  63. package/dist/boxes/webm/segments/timestamp-scale.d.ts +6 -0
  64. package/dist/boxes/webm/segments/timestamp-scale.js +11 -0
  65. package/dist/boxes/webm/segments/tracks.d.ts +8 -0
  66. package/dist/boxes/webm/segments/tracks.js +21 -0
  67. package/dist/boxes/webm/segments/unknown.d.ts +6 -0
  68. package/dist/boxes/webm/segments/unknown.js +11 -0
  69. package/dist/boxes/webm/segments/void.d.ts +6 -0
  70. package/dist/boxes/webm/segments/void.js +11 -0
  71. package/dist/boxes/webm/segments/writing.d.ts +6 -0
  72. package/dist/boxes/webm/segments/writing.js +11 -0
  73. package/dist/boxes/webm/tracks.d.ts +8 -0
  74. package/dist/boxes/webm/tracks.js +21 -0
  75. package/dist/combine-uint8array.d.ts +1 -0
  76. package/dist/combine-uint8array.js +13 -0
  77. package/dist/create/cluster-segment.d.ts +10 -0
  78. package/dist/create/cluster-segment.js +41 -0
  79. package/dist/create/create-media.d.ts +13 -0
  80. package/dist/create/create-media.js +108 -0
  81. package/dist/create/matroska-header.d.ts +1 -0
  82. package/dist/create/matroska-header.js +66 -0
  83. package/dist/create/matroska-info.d.ts +4 -0
  84. package/dist/create/matroska-info.js +41 -0
  85. package/dist/create/matroska-segment.d.ts +2 -0
  86. package/dist/create/matroska-segment.js +12 -0
  87. package/dist/create/matroska-trackentry.d.ts +32 -0
  88. package/dist/create/matroska-trackentry.js +266 -0
  89. package/dist/from-web.d.ts +2 -0
  90. package/dist/from-web.js +45 -0
  91. package/dist/get-video-metadata.d.ts +2 -0
  92. package/dist/get-video-metadata.js +44 -0
  93. package/dist/index.d.ts +5 -0
  94. package/dist/index.js +5 -1
  95. package/dist/options.d.ts +1 -1
  96. package/dist/parse-media.js +1 -1
  97. package/dist/read-and-increment-offset.d.ts +28 -0
  98. package/dist/read-and-increment-offset.js +177 -0
  99. package/dist/readers/from-fetch.d.ts +2 -0
  100. package/dist/readers/from-fetch.js +64 -0
  101. package/dist/readers/from-node.d.ts +2 -0
  102. package/dist/readers/from-node.js +40 -0
  103. package/dist/readers/from-web-file.d.ts +2 -0
  104. package/dist/readers/from-web-file.js +39 -0
  105. package/dist/readers/reader.d.ts +11 -0
  106. package/dist/readers/reader.js +2 -0
  107. package/dist/traversal.d.ts +1 -1
  108. package/dist/understand-vorbis.d.ts +1 -0
  109. package/dist/understand-vorbis.js +12 -0
  110. package/dist/writers/web-fs.d.ts +2 -0
  111. package/dist/writers/web-fs.js +44 -0
  112. package/dist/writers/writer.d.ts +11 -0
  113. package/dist/writers/writer.js +2 -0
  114. package/package.json +13 -8
  115. package/src/boxes/iso-base-media/stsd/ctts.ts +10 -1
  116. package/src/boxes/iso-base-media/stsd/samples.ts +12 -9
  117. package/src/boxes/webm/make-header.ts +132 -24
  118. package/src/boxes/webm/parse-ebml.ts +4 -1
  119. package/src/boxes/webm/segments/all-segments.ts +67 -7
  120. package/src/create/cluster-segment.ts +62 -0
  121. package/src/create/create-media.ts +172 -0
  122. package/src/create/matroska-header.ts +63 -0
  123. package/src/create/matroska-info.ts +46 -0
  124. package/src/create/matroska-segment.ts +10 -0
  125. package/src/create/matroska-trackentry.ts +325 -0
  126. package/src/index.ts +9 -0
  127. package/src/options.ts +1 -1
  128. package/src/parse-media.ts +1 -1
  129. package/src/test/av1.test.ts +1 -1
  130. package/src/test/create-matroska.test.ts +31 -6
  131. package/src/test/duration.test.ts +1 -1
  132. package/src/test/matroska.test.ts +35 -5
  133. package/src/test/parse-video.test.ts +1 -1
  134. package/src/test/parse-webm.test.ts +1 -1
  135. package/src/test/stream-local.test.ts +1 -1
  136. package/src/test/stream-samples.test.ts +1 -1
  137. package/src/writers/web-fs.ts +50 -0
  138. package/src/writers/writer.ts +12 -0
  139. package/tsconfig.tsbuildinfo +1 -1
  140. /package/src/{from-fetch.ts → readers/from-fetch.ts} +0 -0
  141. /package/src/{from-node.ts → readers/from-node.ts} +0 -0
  142. /package/src/{from-web-file.ts → readers/from-web-file.ts} +0 -0
  143. /package/src/{reader.ts → readers/reader.ts} +0 -0
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeMatroskaTracks = exports.makeMatroskaVideoTrackEntryBytes = exports.makeMatroskaAudioTrackEntryBytes = exports.makeMatroskaVideoBytes = exports.makeMatroskaColorBytes = void 0;
4
+ const make_header_1 = require("../boxes/webm/make-header");
5
+ const makeMatroskaColorBytes = ({ transferChracteristics, matrixCoefficients, primaries, fullRange, }) => {
6
+ const rangeValue = transferChracteristics && matrixCoefficients
7
+ ? 3
8
+ : fullRange === true
9
+ ? 2
10
+ : fullRange === false
11
+ ? 1
12
+ : 0;
13
+ // https://datatracker.ietf.org/doc/draft-ietf-cellar-matroska/
14
+ // 5.1.4.1.28.27
15
+ const primariesValue = primaries === 'bt709'
16
+ ? 1
17
+ : primaries === 'smpte170m'
18
+ ? 6
19
+ : primaries === 'bt470bg'
20
+ ? 5
21
+ : 2;
22
+ const transferChracteristicsValue = transferChracteristics === 'bt709'
23
+ ? 1
24
+ : transferChracteristics === 'smpte170m'
25
+ ? 6
26
+ : transferChracteristics === 'iec61966-2-1'
27
+ ? 13
28
+ : 2;
29
+ if (matrixCoefficients === 'rgb') {
30
+ throw new Error('Cannot encode Matroska in RGB');
31
+ }
32
+ const matrixCoefficientsValue = matrixCoefficients === 'bt709'
33
+ ? 1
34
+ : matrixCoefficients === 'bt470bg'
35
+ ? 5
36
+ : matrixCoefficients === 'smpte170m'
37
+ ? 6
38
+ : 2;
39
+ return (0, make_header_1.makeMatroskaBytes)({
40
+ type: 'Colour',
41
+ minVintWidth: null,
42
+ value: [
43
+ {
44
+ type: 'TransferCharacteristics',
45
+ value: {
46
+ value: transferChracteristicsValue,
47
+ byteLength: null,
48
+ },
49
+ minVintWidth: null,
50
+ },
51
+ {
52
+ type: 'MatrixCoefficients',
53
+ value: {
54
+ value: matrixCoefficientsValue,
55
+ byteLength: null,
56
+ },
57
+ minVintWidth: null,
58
+ },
59
+ {
60
+ type: 'Primaries',
61
+ value: {
62
+ value: primariesValue,
63
+ byteLength: null,
64
+ },
65
+ minVintWidth: null,
66
+ },
67
+ {
68
+ type: 'Range',
69
+ value: {
70
+ value: rangeValue,
71
+ byteLength: null,
72
+ },
73
+ minVintWidth: null,
74
+ },
75
+ ],
76
+ });
77
+ };
78
+ exports.makeMatroskaColorBytes = makeMatroskaColorBytes;
79
+ const makeMatroskaVideoBytes = ({ color, width, height, }) => {
80
+ return (0, make_header_1.makeMatroskaBytes)({
81
+ type: 'Video',
82
+ value: [
83
+ {
84
+ type: 'PixelWidth',
85
+ value: {
86
+ value: width,
87
+ byteLength: null,
88
+ },
89
+ minVintWidth: null,
90
+ },
91
+ {
92
+ type: 'PixelHeight',
93
+ value: {
94
+ value: height,
95
+ byteLength: null,
96
+ },
97
+ minVintWidth: null,
98
+ },
99
+ {
100
+ type: 'FlagInterlaced',
101
+ value: {
102
+ // https://datatracker.ietf.org/doc/draft-ietf-cellar-matroska/
103
+ // 5.1.4.1.28.1.
104
+ value: 2, // 2 - progressive, no interlaced
105
+ byteLength: null,
106
+ },
107
+ minVintWidth: null,
108
+ },
109
+ (0, exports.makeMatroskaColorBytes)(color),
110
+ ],
111
+ minVintWidth: null,
112
+ });
113
+ };
114
+ exports.makeMatroskaVideoBytes = makeMatroskaVideoBytes;
115
+ const makeMatroskaAudioTrackEntryBytes = ({ trackNumber, codecId, numberOfChannels: audioChannels, sampleRate, }) => {
116
+ return (0, make_header_1.makeMatroskaBytes)({
117
+ type: 'TrackEntry',
118
+ minVintWidth: null,
119
+ value: [
120
+ {
121
+ type: 'TrackNumber',
122
+ value: {
123
+ value: trackNumber,
124
+ byteLength: null,
125
+ },
126
+ minVintWidth: null,
127
+ },
128
+ {
129
+ type: 'TrackUID',
130
+ value: '0x188FEB95C8EFABA',
131
+ minVintWidth: null,
132
+ },
133
+ {
134
+ type: 'TrackType',
135
+ value: {
136
+ value: 2,
137
+ byteLength: null,
138
+ },
139
+ minVintWidth: null,
140
+ },
141
+ {
142
+ type: 'TrackTimestampScale',
143
+ value: {
144
+ value: 1,
145
+ size: '64',
146
+ },
147
+ minVintWidth: null,
148
+ },
149
+ {
150
+ type: 'CodecID',
151
+ value: codecId,
152
+ minVintWidth: null,
153
+ },
154
+ {
155
+ type: 'Audio',
156
+ value: [
157
+ {
158
+ type: 'Channels',
159
+ minVintWidth: null,
160
+ value: {
161
+ value: audioChannels,
162
+ byteLength: null,
163
+ },
164
+ },
165
+ {
166
+ type: 'SamplingFrequency',
167
+ minVintWidth: null,
168
+ value: {
169
+ value: sampleRate,
170
+ size: '64',
171
+ },
172
+ },
173
+ {
174
+ type: 'BitDepth',
175
+ minVintWidth: null,
176
+ value: {
177
+ value: 32,
178
+ byteLength: null,
179
+ },
180
+ },
181
+ ],
182
+ minVintWidth: null,
183
+ },
184
+ ],
185
+ });
186
+ };
187
+ exports.makeMatroskaAudioTrackEntryBytes = makeMatroskaAudioTrackEntryBytes;
188
+ const makeMatroskaVideoTrackEntryBytes = ({ color, width, height, defaultDuration, trackNumber, codecId, }) => {
189
+ return (0, make_header_1.makeMatroskaBytes)({
190
+ type: 'TrackEntry',
191
+ minVintWidth: null,
192
+ value: [
193
+ {
194
+ type: 'TrackNumber',
195
+ value: {
196
+ value: trackNumber,
197
+ byteLength: null,
198
+ },
199
+ minVintWidth: null,
200
+ },
201
+ {
202
+ type: 'TrackUID',
203
+ value: '0xab2171012bb9020a',
204
+ minVintWidth: null,
205
+ },
206
+ {
207
+ type: 'FlagLacing',
208
+ value: {
209
+ value: 0,
210
+ byteLength: null,
211
+ },
212
+ minVintWidth: null,
213
+ },
214
+ {
215
+ type: 'Language',
216
+ value: 'und',
217
+ minVintWidth: null,
218
+ },
219
+ {
220
+ type: 'FlagDefault',
221
+ value: {
222
+ value: 0,
223
+ byteLength: null,
224
+ },
225
+ minVintWidth: null,
226
+ },
227
+ {
228
+ type: 'CodecID',
229
+ value: codecId,
230
+ minVintWidth: null,
231
+ },
232
+ {
233
+ type: 'TrackType',
234
+ value: {
235
+ value: 1, // 'video'
236
+ byteLength: null,
237
+ },
238
+ minVintWidth: null,
239
+ },
240
+ {
241
+ type: 'DefaultDuration',
242
+ value: {
243
+ value: defaultDuration,
244
+ byteLength: null,
245
+ },
246
+ minVintWidth: null,
247
+ },
248
+ (0, exports.makeMatroskaVideoBytes)({
249
+ color,
250
+ width,
251
+ height,
252
+ }),
253
+ ],
254
+ });
255
+ };
256
+ exports.makeMatroskaVideoTrackEntryBytes = makeMatroskaVideoTrackEntryBytes;
257
+ const makeMatroskaTracks = (tracks) => {
258
+ return (0, make_header_1.padMatroskaBytes)((0, make_header_1.makeMatroskaBytes)({
259
+ type: 'Tracks',
260
+ value: tracks,
261
+ minVintWidth: null,
262
+ }),
263
+ // TODO: That's too much padding
264
+ 1000);
265
+ };
266
+ exports.makeMatroskaTracks = makeMatroskaTracks;
@@ -0,0 +1,2 @@
1
+ import type { ReaderInterface } from './reader';
2
+ export declare const webReader: ReaderInterface;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.webReader = void 0;
4
+ exports.webReader = {
5
+ read: async (src, range) => {
6
+ const resolvedUrl = typeof window !== 'undefined' && typeof window.location !== 'undefined'
7
+ ? new URL(src, window.location.origin).toString()
8
+ : src;
9
+ if (!resolvedUrl.startsWith('https://') &&
10
+ !resolvedUrl.startsWith('http://')) {
11
+ return Promise.reject(new Error(resolvedUrl +
12
+ ' is not a URL - needs to start with http:// or https://. If you want to read a local file, pass `nodeReader` to parseMedia().'));
13
+ }
14
+ const res = await fetch(resolvedUrl, {
15
+ headers: range === null
16
+ ? {}
17
+ : {
18
+ Range: `bytes=${`${range[0]}-${range[1]}`}`,
19
+ },
20
+ });
21
+ if (!res.body) {
22
+ throw new Error('No body');
23
+ }
24
+ const length = res.headers.get('content-length');
25
+ if (!length) {
26
+ throw new Error('No content-length');
27
+ }
28
+ const contentLength = length === null ? null : parseInt(length, 10);
29
+ const reader = res.body.getReader();
30
+ return { reader, contentLength };
31
+ },
32
+ getLength: async (src) => {
33
+ const res = await fetch(src, {
34
+ method: 'HEAD',
35
+ });
36
+ if (!res.body) {
37
+ throw new Error('No body');
38
+ }
39
+ const length = res.headers.get('content-length');
40
+ if (!length) {
41
+ throw new Error('No content-length');
42
+ }
43
+ return parseInt(length, 10);
44
+ },
45
+ };
@@ -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;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,7 @@
1
+ export { AudioTrack, OtherTrack, Track, VideoTrack } from './get-tracks';
1
2
  export { parseMedia } from './parse-media';
2
3
  export { AudioSample, OnAudioSample, OnAudioTrack, OnVideoSample, OnVideoTrack, VideoSample, } from './webcodec-sample-types';
4
+ export type { MediaFn } from './create/create-media';
5
+ export declare const MediaParserInternals: {
6
+ createMedia: (writer: import("./writers/writer").WriterInterface) => Promise<import("./create/create-media").MediaFn>;
7
+ };
package/dist/index.js CHANGED
@@ -1,5 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseMedia = void 0;
3
+ exports.MediaParserInternals = exports.parseMedia = void 0;
4
+ const create_media_1 = require("./create/create-media");
4
5
  var parse_media_1 = require("./parse-media");
5
6
  Object.defineProperty(exports, "parseMedia", { enumerable: true, get: function () { return parse_media_1.parseMedia; } });
7
+ exports.MediaParserInternals = {
8
+ createMedia: create_media_1.createMedia,
9
+ };
package/dist/options.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { Dimensions } from './get-dimensions';
2
2
  import type { AudioTrack, VideoTrack } from './get-tracks';
3
3
  import type { AnySegment } from './parse-result';
4
4
  import type { InternalStats } from './parser-state';
5
- import type { ReaderInterface } from './reader';
5
+ import type { ReaderInterface } from './readers/reader';
6
6
  import type { OnAudioTrack, OnVideoTrack } from './webcodec-sample-types';
7
7
  export type KnownVideoCodecs = 'h264' | 'h265' | 'vp8' | 'vp9' | 'av1' | 'prores';
8
8
  export type KnownAudioCodecs = 'aac' | 'mp3' | 'aiff' | 'opus' | 'pcm' | 'vorbis' | 'unknown';
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseMedia = void 0;
4
4
  const buffer_iterator_1 = require("./buffer-iterator");
5
- const from_fetch_1 = require("./from-fetch");
6
5
  const get_audio_codec_1 = require("./get-audio-codec");
7
6
  const get_dimensions_1 = require("./get-dimensions");
8
7
  const get_duration_1 = require("./get-duration");
@@ -12,6 +11,7 @@ const get_video_codec_1 = require("./get-video-codec");
12
11
  const has_all_info_1 = require("./has-all-info");
13
12
  const parse_video_1 = require("./parse-video");
14
13
  const parser_state_1 = require("./parser-state");
14
+ const from_fetch_1 = require("./readers/from-fetch");
15
15
  const parseMedia = async ({ src, fields, reader: readerInterface = from_fetch_1.fetchReader, onAudioTrack, onVideoTrack, signal, }) => {
16
16
  const state = (0, parser_state_1.makeParserState)({
17
17
  hasAudioCallbacks: onAudioTrack !== null,
@@ -0,0 +1,28 @@
1
+ export declare class OffsetCounter {
2
+ #private;
3
+ constructor(initial: number);
4
+ increment(amount: number): void;
5
+ getOffset(): number;
6
+ }
7
+ export declare const getArrayBufferIterator: (data: ArrayBuffer, initialOffset: number) => {
8
+ view: DataView;
9
+ counter: OffsetCounter;
10
+ data: ArrayBuffer;
11
+ discard: (length: number) => void;
12
+ getSlice: (amount: number) => ArrayBuffer;
13
+ getAtom: () => string;
14
+ getMatroskaSegmentId: () => string;
15
+ getVint: (bytes: number) => number;
16
+ getUint8: () => number;
17
+ getEBML: () => number;
18
+ getInt8: () => number;
19
+ getUint16: () => number;
20
+ getInt16: () => number;
21
+ getUint32: () => number;
22
+ getFixedPoint1616Number: () => number;
23
+ getPascalString: () => number[];
24
+ getDecimalBytes(length: number): number;
25
+ getByteString(length: number): string;
26
+ getFloat64: () => number;
27
+ };
28
+ export type BufferIterator = ReturnType<typeof getArrayBufferIterator>;
@@ -0,0 +1,177 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _OffsetCounter_offset;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getArrayBufferIterator = exports.OffsetCounter = void 0;
16
+ class OffsetCounter {
17
+ constructor(initial) {
18
+ _OffsetCounter_offset.set(this, void 0);
19
+ __classPrivateFieldSet(this, _OffsetCounter_offset, initial, "f");
20
+ }
21
+ increment(amount) {
22
+ if (amount < 0) {
23
+ throw new Error('Cannot increment by a negative amount');
24
+ }
25
+ __classPrivateFieldSet(this, _OffsetCounter_offset, __classPrivateFieldGet(this, _OffsetCounter_offset, "f") + amount, "f");
26
+ }
27
+ getOffset() {
28
+ return __classPrivateFieldGet(this, _OffsetCounter_offset, "f");
29
+ }
30
+ }
31
+ exports.OffsetCounter = OffsetCounter;
32
+ _OffsetCounter_offset = new WeakMap();
33
+ const makeOffsetCounter = (initial) => {
34
+ return new OffsetCounter(initial);
35
+ };
36
+ const getArrayBufferIterator = (data, initialOffset) => {
37
+ const view = new DataView(data);
38
+ const counter = makeOffsetCounter(initialOffset);
39
+ const getSlice = (amount) => {
40
+ const value = data.slice(counter.getOffset(), counter.getOffset() + amount);
41
+ counter.increment(amount);
42
+ return value;
43
+ };
44
+ const getUint8 = () => {
45
+ const val = view.getUint8(counter.getOffset());
46
+ counter.increment(1);
47
+ return val;
48
+ };
49
+ const getUint32 = () => {
50
+ const val = view.getUint32(counter.getOffset());
51
+ counter.increment(4);
52
+ return val;
53
+ };
54
+ return {
55
+ view,
56
+ counter,
57
+ data,
58
+ discard: (length) => {
59
+ counter.increment(length);
60
+ },
61
+ getSlice,
62
+ getAtom: () => {
63
+ const atom = getSlice(4);
64
+ return new TextDecoder().decode(atom);
65
+ },
66
+ getMatroskaSegmentId: () => {
67
+ const first = getSlice(1);
68
+ const firstOneString = `0x${Array.from(new Uint8Array(first))
69
+ .map((b) => {
70
+ return b.toString(16).padStart(2, '0');
71
+ })
72
+ .join('')}`;
73
+ // Catch void block
74
+ const knownIdsWithOneLength = ['0xec', '0xae'];
75
+ if (knownIdsWithOneLength.includes(firstOneString)) {
76
+ return firstOneString;
77
+ }
78
+ const firstTwo = getSlice(1);
79
+ const knownIdsWithTwoLength = ['0x4dbb', '0x53ac', '0xec01'];
80
+ const firstTwoString = `${firstOneString}${Array.from(new Uint8Array(firstTwo))
81
+ .map((b) => {
82
+ return b.toString(16).padStart(2, '0');
83
+ })
84
+ .join('')}`;
85
+ if (knownIdsWithTwoLength.includes(firstTwoString)) {
86
+ return firstTwoString;
87
+ }
88
+ const knownIdsWithThreeLength = ['0x4d808c', '0x57418c', '0x448988'];
89
+ const firstThree = getSlice(1);
90
+ const firstThreeString = `${firstTwoString}${Array.from(new Uint8Array(firstThree))
91
+ .map((b) => {
92
+ return b.toString(16).padStart(2, '0');
93
+ })
94
+ .join('')}`;
95
+ if (knownIdsWithThreeLength.includes(firstThreeString)) {
96
+ return firstThreeString;
97
+ }
98
+ const segmentId = getSlice(1);
99
+ return `${firstThreeString}${Array.from(new Uint8Array(segmentId))
100
+ .map((b) => {
101
+ return b.toString(16).padStart(2, '0');
102
+ })
103
+ .join('')}`;
104
+ },
105
+ getVint: (bytes) => {
106
+ const slice = getSlice(bytes);
107
+ const d = [...Array.from(new Uint8Array(slice))];
108
+ const totalLength = d[0];
109
+ if (totalLength === 0) {
110
+ return 0;
111
+ }
112
+ // Calculate the actual length of the data based on the first set bit
113
+ let actualLength = 0;
114
+ while (((totalLength >> (7 - actualLength)) & 0x01) === 0) {
115
+ actualLength++;
116
+ }
117
+ actualLength += 1; // Include the first byte set as 1
118
+ // Combine the numbers to form the integer value
119
+ let value = 0;
120
+ // Mask the first byte properly then start combining
121
+ value = totalLength & (0xff >> actualLength);
122
+ for (let i = 1; i < actualLength; i++) {
123
+ value = (value << 8) | d[i];
124
+ }
125
+ return value;
126
+ },
127
+ getUint8,
128
+ getEBML: () => {
129
+ const val = getUint8();
130
+ // https://darkcoding.net/software/reading-mediarecorders-webm-opus-output/#:~:text=The%20first%20four%20bytes%20(%201A,%E2%80%93%20read%20on%20for%20why).
131
+ // You drop the initial 0 bits and the first 1 bit to get the value. 0x81 is 0b10000001, so there are zero inital 0 bits, meaning length one byte, and the value is 1. The 0x9F value for length of the EBML header we saw earlier is 0b10011111, still one byte, value is 0b0011111, which is 31 (the python repl is very helpful for these conversions).
132
+ const actualValue = val & 0x7f; // 0x7F is binary 01111111, which masks out the first bit
133
+ return actualValue;
134
+ },
135
+ getInt8: () => {
136
+ const val = view.getInt8(counter.getOffset());
137
+ counter.increment(1);
138
+ return val;
139
+ },
140
+ getUint16: () => {
141
+ const val = view.getUint16(counter.getOffset());
142
+ counter.increment(2);
143
+ return val;
144
+ },
145
+ getInt16: () => {
146
+ const val = view.getInt16(counter.getOffset());
147
+ counter.increment(2);
148
+ return val;
149
+ },
150
+ getUint32,
151
+ // https://developer.apple.com/documentation/quicktime-file-format/sound_sample_description_version_1
152
+ // A 32-bit unsigned fixed-point number (16.16) that indicates the rate at which the sound samples were obtained.
153
+ getFixedPoint1616Number: () => {
154
+ const val = getUint32();
155
+ return val / 2 ** 16;
156
+ },
157
+ getPascalString: () => {
158
+ const val = getSlice(32);
159
+ return [...Array.from(new Uint8Array(val))];
160
+ },
161
+ getDecimalBytes(length) {
162
+ const bytes = getSlice(length);
163
+ const numbers = [...Array.from(new Uint8Array(bytes))];
164
+ return numbers.reduce((acc, byte, index) => acc + (byte << (8 * (numbers.length - index - 1))), 0);
165
+ },
166
+ getByteString(length) {
167
+ const bytes = getSlice(length);
168
+ return new TextDecoder().decode(bytes);
169
+ },
170
+ getFloat64: () => {
171
+ const val = view.getFloat64(counter.getOffset());
172
+ counter.increment(8);
173
+ return val;
174
+ },
175
+ };
176
+ };
177
+ exports.getArrayBufferIterator = getArrayBufferIterator;
@@ -0,0 +1,2 @@
1
+ import type { ReaderInterface } from './reader';
2
+ export declare const fetchReader: ReaderInterface;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchReader = void 0;
4
+ exports.fetchReader = {
5
+ read: async (src, range, signal) => {
6
+ if (typeof src !== 'string') {
7
+ throw new Error('src must be a string when using `fetchReader`');
8
+ }
9
+ const resolvedUrl = typeof window !== 'undefined' && typeof window.location !== 'undefined'
10
+ ? new URL(src, window.location.origin).toString()
11
+ : src;
12
+ if (!resolvedUrl.startsWith('https://') &&
13
+ !resolvedUrl.startsWith('http://')) {
14
+ return Promise.reject(new Error(resolvedUrl +
15
+ ' is not a URL - needs to start with http:// or https://. If you want to read a local file, pass `nodeReader` to parseMedia().'));
16
+ }
17
+ const res = await fetch(resolvedUrl, {
18
+ headers: range === null
19
+ ? {}
20
+ : typeof range === 'number'
21
+ ? {
22
+ Range: `bytes=${range}`,
23
+ }
24
+ : {
25
+ Range: `bytes=${`${range[0]}-${range[1]}`}`,
26
+ },
27
+ signal,
28
+ // Disable Next.js caching
29
+ cache: 'no-store',
30
+ });
31
+ if (res.status.toString().startsWith('4') ||
32
+ res.status.toString().startsWith('5')) {
33
+ throw new Error(`Server returned status code ${res.status} for ${src}`);
34
+ }
35
+ if (!res.body) {
36
+ throw new Error('No body');
37
+ }
38
+ const length = res.headers.get('content-length');
39
+ const contentLength = length === null ? null : parseInt(length, 10);
40
+ const reader = res.body.getReader();
41
+ if (signal) {
42
+ signal.addEventListener('abort', () => {
43
+ reader.cancel();
44
+ }, { once: true });
45
+ }
46
+ return { reader, contentLength };
47
+ },
48
+ getLength: async (src) => {
49
+ if (typeof src !== 'string') {
50
+ throw new Error('src must be a string when using `fetchReader`');
51
+ }
52
+ const res = await fetch(src, {
53
+ method: 'HEAD',
54
+ });
55
+ if (!res.body) {
56
+ throw new Error('No body');
57
+ }
58
+ const length = res.headers.get('content-length');
59
+ if (!length) {
60
+ throw new Error('No content-length');
61
+ }
62
+ return parseInt(length, 10);
63
+ },
64
+ };