xgplayer-mp4-loader 0.0.1
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.
- package/README.md +72 -0
- package/dist/index.min.js +2 -0
- package/dist/index.min.js.map +1 -0
- package/es/cache.d.ts +6 -0
- package/es/cache.js +32 -0
- package/es/config.d.ts +1 -0
- package/es/config.js +25 -0
- package/es/error.d.ts +6 -0
- package/es/error.js +11 -0
- package/es/index.d.ts +2 -0
- package/es/index.js +5 -0
- package/es/loader.d.ts +42 -0
- package/es/loader.js +715 -0
- package/es/utils.d.ts +15 -0
- package/es/utils.js +280 -0
- package/package.json +27 -0
package/es/utils.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function moovToSegments(moov: any, duration: any): {
|
|
2
|
+
videoSegments: any[];
|
|
3
|
+
audioSegments: any[];
|
|
4
|
+
};
|
|
5
|
+
export function moovToMeta(moov: any): {
|
|
6
|
+
videoCodec: string;
|
|
7
|
+
audioCodec: string;
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
audioChannelCount: number;
|
|
11
|
+
audioSampleRate: number;
|
|
12
|
+
duration: number;
|
|
13
|
+
moov: any;
|
|
14
|
+
};
|
|
15
|
+
export function isNumber(n: any): boolean;
|
package/es/utils.js
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
|
|
2
|
+
import 'core-js/modules/es.array.find.js';
|
|
3
|
+
import 'core-js/modules/es.array.map.js';
|
|
4
|
+
import 'core-js/modules/es.array.iterator.js';
|
|
5
|
+
import 'core-js/modules/es.object.to-string.js';
|
|
6
|
+
import 'core-js/modules/web.dom-collections.iterator.js';
|
|
7
|
+
import 'core-js/modules/web.dom-collections.for-each.js';
|
|
8
|
+
|
|
9
|
+
function moovToSegments(moov, duration) {
|
|
10
|
+
var tracks = moov.trak;
|
|
11
|
+
if (!tracks || !tracks.length) return;
|
|
12
|
+
var videoTrack = tracks.find(function (t) {
|
|
13
|
+
var _t$mdia, _t$mdia$hdlr;
|
|
14
|
+
|
|
15
|
+
return ((_t$mdia = t.mdia) === null || _t$mdia === void 0 ? void 0 : (_t$mdia$hdlr = _t$mdia.hdlr) === null || _t$mdia$hdlr === void 0 ? void 0 : _t$mdia$hdlr.handlerType) === 'vide';
|
|
16
|
+
});
|
|
17
|
+
var audioTrack = tracks.find(function (t) {
|
|
18
|
+
var _t$mdia2, _t$mdia2$hdlr;
|
|
19
|
+
|
|
20
|
+
return ((_t$mdia2 = t.mdia) === null || _t$mdia2 === void 0 ? void 0 : (_t$mdia2$hdlr = _t$mdia2.hdlr) === null || _t$mdia2$hdlr === void 0 ? void 0 : _t$mdia2$hdlr.handlerType) === 'soun';
|
|
21
|
+
});
|
|
22
|
+
if (!videoTrack && !audioTrack) return;
|
|
23
|
+
var videoSegments = [];
|
|
24
|
+
var audioSegments = [];
|
|
25
|
+
var segmentDurations;
|
|
26
|
+
|
|
27
|
+
if (videoTrack) {
|
|
28
|
+
var _videoTrack$mdia, _videoTrack$mdia$minf, _videoTrack$mdia$mdhd;
|
|
29
|
+
|
|
30
|
+
var videoStbl = (_videoTrack$mdia = videoTrack.mdia) === null || _videoTrack$mdia === void 0 ? void 0 : (_videoTrack$mdia$minf = _videoTrack$mdia.minf) === null || _videoTrack$mdia$minf === void 0 ? void 0 : _videoTrack$mdia$minf.stbl;
|
|
31
|
+
if (!videoStbl) return;
|
|
32
|
+
var timescale = (_videoTrack$mdia$mdhd = videoTrack.mdia.mdhd) === null || _videoTrack$mdia$mdhd === void 0 ? void 0 : _videoTrack$mdia$mdhd.timescale;
|
|
33
|
+
var stts = videoStbl.stts,
|
|
34
|
+
stsc = videoStbl.stsc,
|
|
35
|
+
stsz = videoStbl.stsz,
|
|
36
|
+
stco = videoStbl.stco,
|
|
37
|
+
stss = videoStbl.stss;
|
|
38
|
+
if (!timescale || !stts || !stsc || !stsz || !stco || !stss) return;
|
|
39
|
+
videoSegments = getSegments(duration, timescale, stts, stsc, stsz, stco, stss);
|
|
40
|
+
segmentDurations = videoSegments.map(function (x) {
|
|
41
|
+
return x.endTime - x.startTime;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (audioTrack) {
|
|
46
|
+
var _audioTrack$mdia, _audioTrack$mdia$minf, _audioTrack$mdia$mdhd;
|
|
47
|
+
|
|
48
|
+
var audioStbl = (_audioTrack$mdia = audioTrack.mdia) === null || _audioTrack$mdia === void 0 ? void 0 : (_audioTrack$mdia$minf = _audioTrack$mdia.minf) === null || _audioTrack$mdia$minf === void 0 ? void 0 : _audioTrack$mdia$minf.stbl;
|
|
49
|
+
if (!audioStbl) return;
|
|
50
|
+
|
|
51
|
+
var _timescale = (_audioTrack$mdia$mdhd = audioTrack.mdia.mdhd) === null || _audioTrack$mdia$mdhd === void 0 ? void 0 : _audioTrack$mdia$mdhd.timescale;
|
|
52
|
+
|
|
53
|
+
var _stts = audioStbl.stts,
|
|
54
|
+
_stsc = audioStbl.stsc,
|
|
55
|
+
_stsz = audioStbl.stsz,
|
|
56
|
+
_stco = audioStbl.stco;
|
|
57
|
+
if (!_timescale || !_stts || !_stsc || !_stsz || !_stco) return;
|
|
58
|
+
audioSegments = getSegments(duration, _timescale, _stts, _stsc, _stsz, _stco, null, segmentDurations);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
videoSegments: videoSegments,
|
|
63
|
+
audioSegments: audioSegments
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function getSegments(segDuration, timescale, stts, stsc, stsz, stco, stss, segmentDurations) {
|
|
68
|
+
var frames = [];
|
|
69
|
+
var gop = [];
|
|
70
|
+
var gopDuration = [];
|
|
71
|
+
var stscEntries = stsc.entries;
|
|
72
|
+
var stcoEntries = stco.entries;
|
|
73
|
+
var stszEntrySizes = stsz.entrySizes;
|
|
74
|
+
var stssEntries = stss === null || stss === void 0 ? void 0 : stss.entries;
|
|
75
|
+
var keyframeMap;
|
|
76
|
+
|
|
77
|
+
if (stssEntries) {
|
|
78
|
+
keyframeMap = {};
|
|
79
|
+
stssEntries.forEach(function (x) {
|
|
80
|
+
keyframeMap[x - 1] = true;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
var frame;
|
|
85
|
+
var duration;
|
|
86
|
+
var startTime = 0;
|
|
87
|
+
var pos = 0;
|
|
88
|
+
var chunkIndex = 0;
|
|
89
|
+
var chunkRunIndex = 0;
|
|
90
|
+
var offsetInChunk = 0;
|
|
91
|
+
var lastSampleInChunk = stscEntries[0].samplesPerChunk;
|
|
92
|
+
var lastChunkInRun = stscEntries[1] ? stscEntries[1].firstChunk - 1 : Infinity;
|
|
93
|
+
stts.entries.forEach(function (_ref) {
|
|
94
|
+
var count = _ref.count,
|
|
95
|
+
delta = _ref.delta;
|
|
96
|
+
duration = delta / timescale;
|
|
97
|
+
|
|
98
|
+
for (var i = 0; i < count; i++) {
|
|
99
|
+
frame = {
|
|
100
|
+
startTime: startTime,
|
|
101
|
+
duration: duration,
|
|
102
|
+
size: stszEntrySizes[pos] || stsz.sampleSize,
|
|
103
|
+
offset: stcoEntries[chunkIndex] + offsetInChunk,
|
|
104
|
+
index: pos
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
if (stssEntries) {
|
|
108
|
+
frame.keyframe = keyframeMap[pos];
|
|
109
|
+
|
|
110
|
+
if (frame.keyframe) {
|
|
111
|
+
gop.push([frame]);
|
|
112
|
+
gopDuration.push(frame.duration);
|
|
113
|
+
} else {
|
|
114
|
+
gop[gop.length - 1].push(frame);
|
|
115
|
+
gopDuration[gop.length - 1] += frame.duration;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
frames.push(frame);
|
|
120
|
+
startTime += duration;
|
|
121
|
+
pos++;
|
|
122
|
+
|
|
123
|
+
if (pos < lastSampleInChunk) {
|
|
124
|
+
offsetInChunk += frame.size;
|
|
125
|
+
} else {
|
|
126
|
+
chunkIndex++;
|
|
127
|
+
offsetInChunk = 0;
|
|
128
|
+
|
|
129
|
+
if (chunkIndex >= lastChunkInRun) {
|
|
130
|
+
chunkRunIndex++;
|
|
131
|
+
lastChunkInRun = stscEntries[chunkRunIndex + 1] ? stscEntries[chunkRunIndex + 1].firstChunk - 1 : Infinity;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
lastSampleInChunk += stscEntries[chunkRunIndex].samplesPerChunk;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
var l = frames.length;
|
|
139
|
+
if (!l || stss && !frames[0].keyframe) return;
|
|
140
|
+
var segments = [];
|
|
141
|
+
var segFrames = [];
|
|
142
|
+
var time = 0;
|
|
143
|
+
var lastFrame;
|
|
144
|
+
|
|
145
|
+
var pushSegment = function pushSegment() {
|
|
146
|
+
var _segments;
|
|
147
|
+
|
|
148
|
+
lastFrame = segFrames[segFrames.length - 1];
|
|
149
|
+
segments.push({
|
|
150
|
+
index: segments.length,
|
|
151
|
+
startTime: ((_segments = segments[segments.length - 1]) === null || _segments === void 0 ? void 0 : _segments.endTime) || segFrames[0].startTime,
|
|
152
|
+
endTime: lastFrame.startTime + lastFrame.duration,
|
|
153
|
+
range: [segFrames[0].offset, lastFrame.offset + lastFrame.size],
|
|
154
|
+
frames: segFrames
|
|
155
|
+
});
|
|
156
|
+
time = 0;
|
|
157
|
+
segFrames = [];
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
if (stss) {
|
|
161
|
+
var nextTime = 0;
|
|
162
|
+
|
|
163
|
+
for (var i = 0, _l = gop.length; i < _l; i++) {
|
|
164
|
+
var _segFrames;
|
|
165
|
+
|
|
166
|
+
time += gopDuration[i];
|
|
167
|
+
|
|
168
|
+
(_segFrames = segFrames).push.apply(_segFrames, _toConsumableArray(gop[i]));
|
|
169
|
+
|
|
170
|
+
if (i + 1 < _l) {
|
|
171
|
+
nextTime = time + gopDuration[i + 1];
|
|
172
|
+
|
|
173
|
+
if (nextTime >= segDuration) {
|
|
174
|
+
if (nextTime - segDuration < Math.abs(segDuration - time)) {
|
|
175
|
+
var _segFrames2;
|
|
176
|
+
|
|
177
|
+
(_segFrames2 = segFrames).push.apply(_segFrames2, _toConsumableArray(gop[i + 1]));
|
|
178
|
+
|
|
179
|
+
pushSegment();
|
|
180
|
+
i++;
|
|
181
|
+
} else {
|
|
182
|
+
pushSegment();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
pushSegment();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
} else {
|
|
190
|
+
segmentDurations = segmentDurations || [];
|
|
191
|
+
|
|
192
|
+
var _duration = segmentDurations[0] || segDuration;
|
|
193
|
+
|
|
194
|
+
for (var _i = 0; _i < l; _i++) {
|
|
195
|
+
segFrames.push(frames[_i]);
|
|
196
|
+
time += frames[_i].duration;
|
|
197
|
+
|
|
198
|
+
if (_i + 1 >= l || time >= _duration) {
|
|
199
|
+
pushSegment();
|
|
200
|
+
_duration = segmentDurations[segments.length] || segDuration;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return segments;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function moovToMeta(moov) {
|
|
209
|
+
var videoCodec = '';
|
|
210
|
+
var audioCodec = '';
|
|
211
|
+
var width = 0;
|
|
212
|
+
var height = 0;
|
|
213
|
+
var audioChannelCount = 0;
|
|
214
|
+
var audioSampleRate = 0;
|
|
215
|
+
var duration = 0;
|
|
216
|
+
|
|
217
|
+
if (moov.mvhd) {
|
|
218
|
+
duration = moov.mvhd.duration / moov.mvhd.timescale;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
var tracks = moov.trak;
|
|
222
|
+
|
|
223
|
+
if (tracks) {
|
|
224
|
+
var videoTrack = tracks.find(function (t) {
|
|
225
|
+
var _t$mdia3, _t$mdia3$hdlr;
|
|
226
|
+
|
|
227
|
+
return ((_t$mdia3 = t.mdia) === null || _t$mdia3 === void 0 ? void 0 : (_t$mdia3$hdlr = _t$mdia3.hdlr) === null || _t$mdia3$hdlr === void 0 ? void 0 : _t$mdia3$hdlr.handlerType) === 'vide';
|
|
228
|
+
});
|
|
229
|
+
var audioTrack = tracks.find(function (t) {
|
|
230
|
+
var _t$mdia4, _t$mdia4$hdlr;
|
|
231
|
+
|
|
232
|
+
return ((_t$mdia4 = t.mdia) === null || _t$mdia4 === void 0 ? void 0 : (_t$mdia4$hdlr = _t$mdia4.hdlr) === null || _t$mdia4$hdlr === void 0 ? void 0 : _t$mdia4$hdlr.handlerType) === 'soun';
|
|
233
|
+
});
|
|
234
|
+
var e1;
|
|
235
|
+
|
|
236
|
+
if (videoTrack) {
|
|
237
|
+
var _videoTrack$mdia2, _videoTrack$mdia2$min, _videoTrack$mdia2$min2;
|
|
238
|
+
|
|
239
|
+
e1 = (_videoTrack$mdia2 = videoTrack.mdia) === null || _videoTrack$mdia2 === void 0 ? void 0 : (_videoTrack$mdia2$min = _videoTrack$mdia2.minf) === null || _videoTrack$mdia2$min === void 0 ? void 0 : (_videoTrack$mdia2$min2 = _videoTrack$mdia2$min.stbl) === null || _videoTrack$mdia2$min2 === void 0 ? void 0 : _videoTrack$mdia2$min2.stsd.entries[0];
|
|
240
|
+
|
|
241
|
+
if (e1) {
|
|
242
|
+
var _ref2;
|
|
243
|
+
|
|
244
|
+
width = e1.width;
|
|
245
|
+
height = e1.height;
|
|
246
|
+
videoCodec = (_ref2 = e1.avcC || e1.hvcC) === null || _ref2 === void 0 ? void 0 : _ref2.codec;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (audioTrack) {
|
|
251
|
+
var _audioTrack$mdia2, _audioTrack$mdia2$min, _audioTrack$mdia2$min2;
|
|
252
|
+
|
|
253
|
+
e1 = (_audioTrack$mdia2 = audioTrack.mdia) === null || _audioTrack$mdia2 === void 0 ? void 0 : (_audioTrack$mdia2$min = _audioTrack$mdia2.minf) === null || _audioTrack$mdia2$min === void 0 ? void 0 : (_audioTrack$mdia2$min2 = _audioTrack$mdia2$min.stbl) === null || _audioTrack$mdia2$min2 === void 0 ? void 0 : _audioTrack$mdia2$min2.stsd.entries[0];
|
|
254
|
+
|
|
255
|
+
if (e1) {
|
|
256
|
+
var _e1$esds;
|
|
257
|
+
|
|
258
|
+
audioChannelCount = e1.channelCount;
|
|
259
|
+
audioSampleRate = e1.sampleRate;
|
|
260
|
+
audioCodec = (_e1$esds = e1.esds) === null || _e1$esds === void 0 ? void 0 : _e1$esds.codec;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return {
|
|
266
|
+
videoCodec: videoCodec,
|
|
267
|
+
audioCodec: audioCodec,
|
|
268
|
+
width: width,
|
|
269
|
+
height: height,
|
|
270
|
+
audioChannelCount: audioChannelCount,
|
|
271
|
+
audioSampleRate: audioSampleRate,
|
|
272
|
+
duration: duration,
|
|
273
|
+
moov: moov
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
function isNumber(n) {
|
|
277
|
+
return typeof n === 'number' && !isNaN(n);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export { isNumber, moovToMeta, moovToSegments };
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "xgplayer-mp4-loader",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "dist/index.min.js",
|
|
5
|
+
"module": "es/index.js",
|
|
6
|
+
"typings": "es/index.d.ts",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"umdName": "MP4Loader",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"es"
|
|
12
|
+
],
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"registry": "https://registry.npmjs.org/",
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"unpkgFiles": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"core-js": "3",
|
|
23
|
+
"@babel/runtime": "^7.15.3",
|
|
24
|
+
"xgplayer-transmuxer": "3.0.0-next.2-1",
|
|
25
|
+
"xgplayer-streaming-shared": "3.0.0-next.2-1"
|
|
26
|
+
}
|
|
27
|
+
}
|