@remotion/media-parser 4.0.204 → 4.0.205

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 (50) hide show
  1. package/dist/boxes/iso-base-media/esds/decoder-specific-config.js +6 -1
  2. package/dist/boxes/iso-base-media/make-track.js +3 -30
  3. package/dist/boxes/iso-base-media/mdat/mdat.js +25 -22
  4. package/dist/boxes/iso-base-media/mdhd.d.ts +2 -0
  5. package/dist/boxes/iso-base-media/mdhd.js +6 -7
  6. package/dist/boxes/iso-base-media/mvhd.js +11 -13
  7. package/dist/boxes/iso-base-media/process-box.js +36 -0
  8. package/dist/boxes/iso-base-media/tkhd.js +9 -12
  9. package/dist/boxes/webm/ebml.d.ts +1 -1
  10. package/dist/boxes/webm/get-track.js +2 -2
  11. package/dist/buffer-iterator.d.ts +1 -0
  12. package/dist/buffer-iterator.js +3 -2
  13. package/dist/create/cluster-segment.d.ts +10 -0
  14. package/dist/create/cluster-segment.js +41 -0
  15. package/dist/create/create-media.d.ts +7 -0
  16. package/dist/create/create-media.js +90 -0
  17. package/dist/create/matroska-header.d.ts +1 -0
  18. package/dist/create/matroska-header.js +66 -0
  19. package/dist/create/matroska-info.d.ts +4 -0
  20. package/dist/create/matroska-info.js +39 -0
  21. package/dist/create/matroska-segment.d.ts +2 -0
  22. package/dist/create/matroska-segment.js +12 -0
  23. package/dist/create/matroska-trackentry.d.ts +22 -0
  24. package/dist/create/matroska-trackentry.js +191 -0
  25. package/dist/get-duration.d.ts +3 -2
  26. package/dist/get-duration.js +24 -4
  27. package/dist/get-tracks.d.ts +4 -4
  28. package/dist/has-all-info.js +1 -1
  29. package/dist/parse-media.js +1 -1
  30. package/dist/parse-result.d.ts +4 -1
  31. package/dist/readers/from-fetch.d.ts +2 -0
  32. package/dist/readers/from-fetch.js +64 -0
  33. package/dist/readers/from-node.d.ts +2 -0
  34. package/dist/readers/from-node.js +40 -0
  35. package/dist/readers/from-web-file.d.ts +2 -0
  36. package/dist/readers/from-web-file.js +39 -0
  37. package/dist/readers/reader.d.ts +11 -0
  38. package/dist/readers/reader.js +2 -0
  39. package/dist/samples-from-moof.js +2 -1
  40. package/dist/traversal.d.ts +8 -1
  41. package/dist/traversal.js +39 -1
  42. package/dist/writers/web-fs.d.ts +2 -0
  43. package/dist/writers/web-fs.js +44 -0
  44. package/dist/writers/writer.d.ts +11 -0
  45. package/dist/writers/writer.js +2 -0
  46. package/package.json +2 -2
  47. package/src/boxes/iso-base-media/esds/decoder-specific-config.ts +8 -1
  48. package/src/samples-from-moof.ts +2 -1
  49. package/src/test/samples-from-moof.test.ts +3 -3
  50. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeMatroskaHeader = void 0;
4
+ const make_header_1 = require("../boxes/webm/make-header");
5
+ const makeMatroskaHeader = () => {
6
+ return (0, make_header_1.makeMatroskaBytes)({
7
+ type: 'Header',
8
+ value: [
9
+ {
10
+ minVintWidth: null,
11
+ type: 'EBMLVersion',
12
+ value: {
13
+ value: 1,
14
+ byteLength: null,
15
+ },
16
+ },
17
+ {
18
+ minVintWidth: null,
19
+ type: 'EBMLReadVersion',
20
+ value: {
21
+ value: 1,
22
+ byteLength: null,
23
+ },
24
+ },
25
+ {
26
+ type: 'EBMLMaxIDLength',
27
+ value: {
28
+ byteLength: null,
29
+ value: 4,
30
+ },
31
+ minVintWidth: null,
32
+ },
33
+ {
34
+ type: 'EBMLMaxSizeLength',
35
+ value: {
36
+ byteLength: null,
37
+ value: 8,
38
+ },
39
+ minVintWidth: null,
40
+ },
41
+ {
42
+ type: 'DocType',
43
+ value: 'webm',
44
+ minVintWidth: null,
45
+ },
46
+ {
47
+ type: 'DocTypeVersion',
48
+ value: {
49
+ byteLength: null,
50
+ value: 4,
51
+ },
52
+ minVintWidth: null,
53
+ },
54
+ {
55
+ type: 'DocTypeReadVersion',
56
+ value: {
57
+ byteLength: null,
58
+ value: 2,
59
+ },
60
+ minVintWidth: null,
61
+ },
62
+ ],
63
+ minVintWidth: null,
64
+ });
65
+ };
66
+ exports.makeMatroskaHeader = makeMatroskaHeader;
@@ -0,0 +1,4 @@
1
+ export declare const makeMatroskaInfo: ({ timescale, duration, }: {
2
+ timescale: number;
3
+ duration: number;
4
+ }) => import("../boxes/webm/segments/all-segments").BytesAndOffset;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeMatroskaInfo = void 0;
4
+ const make_header_1 = require("../boxes/webm/make-header");
5
+ const makeMatroskaInfo = ({ timescale, duration, }) => {
6
+ return (0, make_header_1.makeMatroskaBytes)({
7
+ type: 'Info',
8
+ value: [
9
+ {
10
+ type: 'TimestampScale',
11
+ value: {
12
+ value: timescale,
13
+ byteLength: null,
14
+ },
15
+ minVintWidth: null,
16
+ },
17
+ {
18
+ type: 'MuxingApp',
19
+ value: '@remotion/media-parser',
20
+ minVintWidth: null,
21
+ },
22
+ {
23
+ type: 'WritingApp',
24
+ value: '@remotion/media-parser',
25
+ minVintWidth: null,
26
+ },
27
+ ...(0, make_header_1.padMatroskaBytes)({
28
+ type: 'Duration',
29
+ value: {
30
+ value: duration,
31
+ size: '64',
32
+ },
33
+ minVintWidth: null,
34
+ }, 1000),
35
+ ],
36
+ minVintWidth: null,
37
+ });
38
+ };
39
+ exports.makeMatroskaInfo = makeMatroskaInfo;
@@ -0,0 +1,2 @@
1
+ import type { BytesAndOffset } from '../boxes/webm/segments/all-segments';
2
+ export declare const createMatroskaSegment: (children: BytesAndOffset[]) => BytesAndOffset;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createMatroskaSegment = void 0;
4
+ const make_header_1 = require("../boxes/webm/make-header");
5
+ const createMatroskaSegment = (children) => {
6
+ return (0, make_header_1.makeMatroskaBytes)({
7
+ type: 'Segment',
8
+ value: children,
9
+ minVintWidth: 8,
10
+ });
11
+ };
12
+ exports.createMatroskaSegment = createMatroskaSegment;
@@ -0,0 +1,22 @@
1
+ import type { BytesAndOffset } from '../boxes/webm/segments/all-segments';
2
+ export type MatroskaColorParams = {
3
+ transferChracteristics: 'bt709' | 'smpte170m' | 'iec61966-2-1' | null;
4
+ matrixCoefficients: 'bt709' | 'bt470bg' | 'rgb' | 'smpte170m' | null;
5
+ primaries: 'bt709' | 'smpte170m' | 'bt470bg' | null;
6
+ fullRange: boolean | null;
7
+ };
8
+ export declare const makeMatroskaColorBytes: ({ transferChracteristics, matrixCoefficients, primaries, fullRange, }: MatroskaColorParams) => BytesAndOffset;
9
+ export declare const makeMatroskaVideoBytes: ({ color, width, height, }: {
10
+ color: MatroskaColorParams;
11
+ width: number;
12
+ height: number;
13
+ }) => BytesAndOffset;
14
+ export declare const makeMatroskaVideoTrackEntryBytes: ({ color, width, height, defaultDuration, trackNumber, codecId, }: {
15
+ color: MatroskaColorParams;
16
+ width: number;
17
+ height: number;
18
+ defaultDuration: number;
19
+ trackNumber: number;
20
+ codecId: string;
21
+ }) => BytesAndOffset;
22
+ export declare const makeMatroskaTracks: (tracks: BytesAndOffset[]) => BytesAndOffset;
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeMatroskaTracks = exports.makeMatroskaVideoTrackEntryBytes = 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 makeMatroskaVideoTrackEntryBytes = ({ color, width, height, defaultDuration, trackNumber, codecId, }) => {
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: '0xab2171012bb9020a',
131
+ minVintWidth: null,
132
+ },
133
+ {
134
+ type: 'FlagLacing',
135
+ value: {
136
+ value: 0,
137
+ byteLength: null,
138
+ },
139
+ minVintWidth: null,
140
+ },
141
+ {
142
+ type: 'Language',
143
+ value: 'und',
144
+ minVintWidth: null,
145
+ },
146
+ {
147
+ type: 'FlagDefault',
148
+ value: {
149
+ value: 0,
150
+ byteLength: null,
151
+ },
152
+ minVintWidth: null,
153
+ },
154
+ {
155
+ type: 'CodecID',
156
+ value: codecId,
157
+ minVintWidth: null,
158
+ },
159
+ {
160
+ type: 'TrackType',
161
+ value: {
162
+ value: 1, // 'video'
163
+ byteLength: null,
164
+ },
165
+ minVintWidth: null,
166
+ },
167
+ {
168
+ type: 'DefaultDuration',
169
+ value: {
170
+ value: defaultDuration,
171
+ byteLength: null,
172
+ },
173
+ minVintWidth: null,
174
+ },
175
+ (0, exports.makeMatroskaVideoBytes)({
176
+ color,
177
+ width,
178
+ height,
179
+ }),
180
+ ],
181
+ });
182
+ };
183
+ exports.makeMatroskaVideoTrackEntryBytes = makeMatroskaVideoTrackEntryBytes;
184
+ const makeMatroskaTracks = (tracks) => {
185
+ return (0, make_header_1.makeMatroskaBytes)({
186
+ type: 'Tracks',
187
+ value: tracks,
188
+ minVintWidth: null,
189
+ });
190
+ };
191
+ exports.makeMatroskaTracks = makeMatroskaTracks;
@@ -1,3 +1,4 @@
1
1
  import type { AnySegment } from './parse-result';
2
- export declare const getDuration: (boxes: AnySegment[]) => number | null;
3
- export declare const hasDuration: (boxes: AnySegment[]) => boolean;
2
+ import type { ParserState } from './parser-state';
3
+ export declare const getDuration: (boxes: AnySegment[], parserState: ParserState) => number | null;
4
+ export declare const hasDuration: (boxes: AnySegment[], parserState: ParserState) => boolean;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.hasDuration = exports.getDuration = void 0;
4
+ const get_sample_positions_from_track_1 = require("./boxes/iso-base-media/get-sample-positions-from-track");
5
+ const get_tracks_1 = require("./get-tracks");
4
6
  const traversal_1 = require("./traversal");
5
7
  const getDurationFromMatroska = (segments) => {
6
8
  const mainSegment = segments.find((s) => s.type === 'Segment');
@@ -26,7 +28,7 @@ const getDurationFromMatroska = (segments) => {
26
28
  }
27
29
  return (duration.value.value / timestampScale.value.value) * 1000;
28
30
  };
29
- const getDuration = (boxes) => {
31
+ const getDuration = (boxes, parserState) => {
30
32
  const matroskaBox = boxes.find((b) => b.type === 'Segment');
31
33
  if (matroskaBox) {
32
34
  return getDurationFromMatroska(boxes);
@@ -35,6 +37,7 @@ const getDuration = (boxes) => {
35
37
  if (!moovBox) {
36
38
  return null;
37
39
  }
40
+ const moofBox = (0, traversal_1.getMoofBox)(boxes);
38
41
  const mvhdBox = (0, traversal_1.getMvhdBox)(moovBox);
39
42
  if (!mvhdBox) {
40
43
  return null;
@@ -42,12 +45,29 @@ const getDuration = (boxes) => {
42
45
  if (mvhdBox.type !== 'mvhd-box') {
43
46
  throw new Error('Expected mvhd-box');
44
47
  }
45
- return mvhdBox.durationInSeconds;
48
+ if (mvhdBox.durationInSeconds > 0) {
49
+ return mvhdBox.durationInSeconds;
50
+ }
51
+ const tracks = (0, get_tracks_1.getTracks)(boxes, parserState);
52
+ const allTracks = [
53
+ ...tracks.videoTracks,
54
+ ...tracks.audioTracks,
55
+ ...tracks.otherTracks,
56
+ ];
57
+ const allSamples = allTracks.map((t) => {
58
+ const { timescale: ts } = t;
59
+ const samplePositions = (0, get_sample_positions_from_track_1.getSamplePositionsFromTrack)(t.trakBox, moofBox);
60
+ const highest = samplePositions === null || samplePositions === void 0 ? void 0 : samplePositions.map((sp) => (sp.cts + sp.duration) / ts).reduce((a, b) => Math.max(a, b), 0);
61
+ return highest !== null && highest !== void 0 ? highest : 0;
62
+ });
63
+ const highestTimestamp = Math.max(...allSamples);
64
+ return highestTimestamp;
46
65
  };
47
66
  exports.getDuration = getDuration;
48
- const hasDuration = (boxes) => {
67
+ const hasDuration = (boxes, parserState) => {
49
68
  try {
50
- return (0, exports.getDuration)(boxes) !== null;
69
+ const duration = (0, exports.getDuration)(boxes, parserState);
70
+ return (0, exports.getDuration)(boxes, parserState) !== null && duration !== 0;
51
71
  }
52
72
  catch (err) {
53
73
  return false;
@@ -1,5 +1,5 @@
1
1
  import type { MoovBox } from './boxes/iso-base-media/moov/moov';
2
- import type { SamplePosition } from './get-sample-positions';
2
+ import type { TrakBox } from './boxes/iso-base-media/trak/trak';
3
3
  import type { AnySegment } from './parse-result';
4
4
  import type { ParserState } from './parser-state';
5
5
  type SampleAspectRatio = {
@@ -8,7 +8,6 @@ type SampleAspectRatio = {
8
8
  };
9
9
  export type VideoTrack = {
10
10
  type: 'video';
11
- samplePositions: SamplePosition[] | null;
12
11
  trackId: number;
13
12
  description: Uint8Array | undefined;
14
13
  timescale: number;
@@ -21,22 +20,23 @@ export type VideoTrack = {
21
20
  codedWidth: number;
22
21
  codedHeight: number;
23
22
  rotation: number;
23
+ trakBox: TrakBox | null;
24
24
  };
25
25
  export type AudioTrack = {
26
26
  type: 'audio';
27
- samplePositions: SamplePosition[] | null;
28
27
  trackId: number;
29
28
  timescale: number;
30
29
  codec: string;
31
30
  numberOfChannels: number;
32
31
  sampleRate: number;
33
32
  description: Uint8Array | undefined;
33
+ trakBox: TrakBox | null;
34
34
  };
35
35
  export type OtherTrack = {
36
36
  type: 'other';
37
- samplePositions: SamplePosition[] | null;
38
37
  trackId: number;
39
38
  timescale: number;
39
+ trakBox: TrakBox | null;
40
40
  };
41
41
  export type Track = VideoTrack | AudioTrack | OtherTrack;
42
42
  export declare const getNumberOfTracks: (moovBox: MoovBox) => number;
@@ -16,7 +16,7 @@ const hasAllInfo = (options, parseResult, state) => {
16
16
  return parseResult.status === 'done';
17
17
  }
18
18
  if (key === 'durationInSeconds') {
19
- return (0, get_duration_1.hasDuration)(parseResult.segments);
19
+ return (0, get_duration_1.hasDuration)(parseResult.segments, state);
20
20
  }
21
21
  if (key === 'dimensions' ||
22
22
  key === 'rotation' ||
@@ -100,7 +100,7 @@ const parseMedia = async ({ src, fields, reader: readerInterface = from_fetch_1.
100
100
  returnValue.rotation = dimensions.rotation;
101
101
  }
102
102
  if (fields === null || fields === void 0 ? void 0 : fields.durationInSeconds) {
103
- returnValue.durationInSeconds = (0, get_duration_1.getDuration)(parseResult.segments);
103
+ returnValue.durationInSeconds = (0, get_duration_1.getDuration)(parseResult.segments, state);
104
104
  }
105
105
  if (fields === null || fields === void 0 ? void 0 : fields.fps) {
106
106
  returnValue.fps = (0, get_fps_1.getFps)(parseResult.segments);
@@ -19,8 +19,11 @@ import type { StsdBox } from './boxes/iso-base-media/stsd/stsd';
19
19
  import type { StssBox } from './boxes/iso-base-media/stsd/stss';
20
20
  import type { StszBox } from './boxes/iso-base-media/stsd/stsz';
21
21
  import type { SttsBox } from './boxes/iso-base-media/stsd/stts';
22
+ import type { TfdtBox } from './boxes/iso-base-media/tfdt';
23
+ import type { TfhdBox } from './boxes/iso-base-media/tfhd';
22
24
  import type { TkhdBox } from './boxes/iso-base-media/tkhd';
23
25
  import type { TrakBox } from './boxes/iso-base-media/trak/trak';
26
+ import type { TrunBox } from './boxes/iso-base-media/trun';
24
27
  import type { VoidBox } from './boxes/iso-base-media/void-box';
25
28
  import type { MatroskaSegment } from './boxes/webm/segments';
26
29
  export interface RegularBox extends BaseBox {
@@ -30,7 +33,7 @@ export interface RegularBox extends BaseBox {
30
33
  offset: number;
31
34
  type: 'regular-box';
32
35
  }
33
- export type IsoBaseMediaBox = RegularBox | FtypBox | MvhdBox | TkhdBox | StsdBox | MebxBox | KeysBox | MoovBox | TrakBox | SttsBox | MdhdBox | EsdsBox | MdatBox | StszBox | StcoBox | StscBox | AvccBox | HvccBox | VoidBox | StssBox | PaspBox | CttsBox | Av1CBox | ColorParameterBox;
36
+ export type IsoBaseMediaBox = RegularBox | FtypBox | MvhdBox | TkhdBox | StsdBox | MebxBox | KeysBox | MoovBox | TrakBox | SttsBox | MdhdBox | EsdsBox | MdatBox | StszBox | StcoBox | StscBox | AvccBox | HvccBox | VoidBox | StssBox | PaspBox | CttsBox | Av1CBox | TrunBox | ColorParameterBox | TfdtBox | TfhdBox;
34
37
  export type AnySegment = MatroskaSegment | IsoBaseMediaBox;
35
38
  export type ParseResult = {
36
39
  status: 'done';
@@ -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
+ };
@@ -0,0 +1,2 @@
1
+ import type { ReaderInterface } from './reader';
2
+ export declare const nodeReader: ReaderInterface;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.nodeReader = void 0;
4
+ const fs_1 = require("fs");
5
+ const promises_1 = require("node:fs/promises");
6
+ const stream_1 = require("stream");
7
+ exports.nodeReader = {
8
+ read: async (src, range, signal) => {
9
+ if (typeof src !== 'string') {
10
+ throw new Error('src must be a string when using `nodeReader`');
11
+ }
12
+ const stream = (0, fs_1.createReadStream)(src, {
13
+ start: range === null ? 0 : typeof range === 'number' ? range : range[0],
14
+ end: range === null
15
+ ? Infinity
16
+ : typeof range === 'number'
17
+ ? Infinity
18
+ : range[1],
19
+ signal,
20
+ });
21
+ const stats = await (0, promises_1.stat)(src);
22
+ const reader = stream_1.Readable.toWeb(stream).getReader();
23
+ if (signal) {
24
+ signal.addEventListener('abort', () => {
25
+ reader.cancel();
26
+ }, { once: true });
27
+ }
28
+ return {
29
+ reader,
30
+ contentLength: stats.size,
31
+ };
32
+ },
33
+ getLength: async (src) => {
34
+ if (typeof src !== 'string') {
35
+ throw new Error('src must be a string when using `nodeReader`');
36
+ }
37
+ const stats = await (0, promises_1.stat)(src);
38
+ return stats.size;
39
+ },
40
+ };
@@ -0,0 +1,2 @@
1
+ import type { ReaderInterface } from './reader';
2
+ export declare const webFileReader: ReaderInterface;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.webFileReader = void 0;
4
+ exports.webFileReader = {
5
+ read: (file, range, signal) => {
6
+ if (typeof file === 'string') {
7
+ throw new Error('`inputTypeFileReader` only supports `File` objects');
8
+ }
9
+ const part = range === null
10
+ ? file
11
+ : typeof range === 'number'
12
+ ? file.slice(range)
13
+ : file.slice(range[0], range[1]);
14
+ const reader = new FileReader();
15
+ reader.readAsArrayBuffer(file);
16
+ if (signal) {
17
+ signal.addEventListener('abort', () => {
18
+ reader.abort();
19
+ }, { once: true });
20
+ }
21
+ return new Promise((resolve, reject) => {
22
+ reader.onload = () => {
23
+ resolve({
24
+ reader: part.stream().getReader(),
25
+ contentLength: file.size,
26
+ });
27
+ };
28
+ reader.onerror = (error) => {
29
+ reject(error);
30
+ };
31
+ });
32
+ },
33
+ getLength: (src) => {
34
+ if (typeof src === 'string') {
35
+ throw new Error('`inputTypeFileReader` only supports `File` objects');
36
+ }
37
+ return Promise.resolve(src.size);
38
+ },
39
+ };
@@ -0,0 +1,11 @@
1
+ type ReadResult = {
2
+ reader: ReadableStreamDefaultReader<Uint8Array>;
3
+ contentLength: number | null;
4
+ };
5
+ type ReadContent = (src: string | File, range: [number, number] | number | null, signal: AbortSignal | undefined) => Promise<ReadResult>;
6
+ type GetLength = (src: string | File) => Promise<number>;
7
+ export type ReaderInterface = {
8
+ read: ReadContent;
9
+ getLength: GetLength;
10
+ };
11
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });