@revideo/2d 0.4.3 → 0.4.5-alpha.1013
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/editor/editor/tsconfig.build.tsbuildinfo +1 -1
- package/lib/components/Video.d.ts.map +1 -1
- package/lib/components/Video.js +4 -2
- package/lib/tsconfig.build.tsbuildinfo +1 -1
- package/lib/utils/video/ffmpeg-client.d.ts.map +1 -1
- package/lib/utils/video/ffmpeg-client.js +3 -4
- package/lib/utils/video/mp4-parser-manager.d.ts.map +1 -1
- package/lib/utils/video/mp4-parser-manager.js +13 -4
- package/lib/utils/video/parser/index.d.ts +2 -0
- package/lib/utils/video/parser/index.d.ts.map +1 -0
- package/lib/utils/video/parser/index.js +2 -0
- package/lib/utils/video/parser/parser.d.ts +21 -0
- package/lib/utils/video/parser/parser.d.ts.map +1 -0
- package/lib/utils/video/parser/parser.js +163 -0
- package/lib/utils/video/parser/sampler.d.ts +15 -0
- package/lib/utils/video/parser/sampler.d.ts.map +1 -0
- package/lib/utils/video/parser/sampler.js +55 -0
- package/lib/utils/video/parser/segment.d.ts +42 -0
- package/lib/utils/video/parser/segment.d.ts.map +1 -0
- package/lib/utils/video/parser/segment.js +149 -0
- package/lib/utils/video/parser/sink.d.ts +9 -0
- package/lib/utils/video/parser/sink.d.ts.map +1 -0
- package/lib/utils/video/parser/sink.js +22 -0
- package/lib/utils/video/parser/utils.d.ts +10 -0
- package/lib/utils/video/parser/utils.d.ts.map +1 -0
- package/lib/utils/video/parser/utils.js +22 -0
- package/package.json +5 -5
- package/src/lib/components/Video.ts +3 -1
- package/src/lib/utils/video/ffmpeg-client.ts +2 -3
- package/src/lib/utils/video/mp4-parser-manager.ts +15 -5
- package/src/lib/utils/video/parser/index.ts +1 -0
- package/src/lib/utils/video/parser/parser.ts +247 -0
- package/src/lib/utils/video/parser/sampler.ts +71 -0
- package/src/lib/utils/video/parser/segment.ts +197 -0
- package/src/lib/utils/video/parser/sink.ts +29 -0
- package/src/lib/utils/video/parser/utils.ts +31 -0
- package/lib/utils/video/mp4-parser.d.ts +0 -63
- package/lib/utils/video/mp4-parser.d.ts.map +0 -1
- package/lib/utils/video/mp4-parser.js +0 -264
- package/src/lib/utils/video/mp4-parser.ts +0 -340
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revideo/2d",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.5-alpha.1013+946fa9ea",
|
|
4
4
|
"description": "A 2D renderer for revideo",
|
|
5
5
|
"author": "revideo",
|
|
6
6
|
"homepage": "https://re.video/",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
],
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@preact/signals": "^1.2.1",
|
|
32
|
-
"@revideo/internal": "0.0.0",
|
|
33
|
-
"@revideo/ui": "0.4.
|
|
32
|
+
"@revideo/internal": "^0.0.0",
|
|
33
|
+
"@revideo/ui": "^0.4.5-alpha.1013+946fa9ea",
|
|
34
34
|
"clsx": "^2.0.0",
|
|
35
35
|
"jsdom": "^22.1.0",
|
|
36
36
|
"preact": "^10.19.2",
|
|
@@ -40,12 +40,12 @@
|
|
|
40
40
|
"@codemirror/language": "^6.10.1",
|
|
41
41
|
"@lezer/common": "^1.2.1",
|
|
42
42
|
"@lezer/highlight": "^1.2.0",
|
|
43
|
-
"@revideo/core": "0.4.
|
|
43
|
+
"@revideo/core": "^0.4.5-alpha.1013+946fa9ea",
|
|
44
44
|
"code-fns": "^0.8.2",
|
|
45
45
|
"hls.js": "^1.5.11",
|
|
46
46
|
"mathjax-full": "^3.2.2",
|
|
47
47
|
"mp4box": "^0.5.2",
|
|
48
48
|
"parse-svg-path": "^0.1.2"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "946fa9ead6ec3af889f9c013f72520a208cf3da6"
|
|
51
51
|
}
|
|
@@ -81,7 +81,9 @@ export class Video extends Media {
|
|
|
81
81
|
super(props);
|
|
82
82
|
|
|
83
83
|
// If the file is not mp4, warn.
|
|
84
|
-
|
|
84
|
+
// strip the query string from the source
|
|
85
|
+
const src = this.fullSource().split('?')[0];
|
|
86
|
+
if (!src.endsWith('.mp4')) {
|
|
85
87
|
console.warn(
|
|
86
88
|
`WARNING: Video source is not an MP4 file. This may significantly slow down rendering: ${this.fullSource()}`,
|
|
87
89
|
);
|
|
@@ -5,7 +5,7 @@ export class ImageCommunication {
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
import.meta.hot.on(
|
|
8
|
-
'revideo:ffmpeg-video-frame-res',
|
|
8
|
+
'revideo:ffmpeg-decoder:video-frame-res',
|
|
9
9
|
this.handler.bind(this),
|
|
10
10
|
);
|
|
11
11
|
}
|
|
@@ -50,14 +50,13 @@ export class ImageCommunication {
|
|
|
50
50
|
|
|
51
51
|
this.nextFrameHandlers.push(handler);
|
|
52
52
|
|
|
53
|
-
import.meta.hot.send('revideo:ffmpeg-video-frame', {
|
|
53
|
+
import.meta.hot.send('revideo:ffmpeg-decoder:video-frame', {
|
|
54
54
|
data: {
|
|
55
55
|
id: id,
|
|
56
56
|
filePath: src,
|
|
57
57
|
startTime: time,
|
|
58
58
|
duration,
|
|
59
59
|
fps,
|
|
60
|
-
png: true,
|
|
61
60
|
},
|
|
62
61
|
});
|
|
63
62
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {Mp4Parser} from './parser';
|
|
2
2
|
|
|
3
3
|
// List of VideoFrameExtractors
|
|
4
|
-
const videoFrameExtractors = new Map<string,
|
|
4
|
+
const videoFrameExtractors = new Map<string, Mp4Parser>();
|
|
5
5
|
|
|
6
6
|
export async function getFrame(
|
|
7
7
|
id: string,
|
|
@@ -15,12 +15,22 @@ export async function getFrame(
|
|
|
15
15
|
|
|
16
16
|
const frameDuration = 1 / fps;
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Sometimes, HTMLVideoElement.duration is not accurate, which can lead to the
|
|
20
|
+
* requested time being greater than the duration.
|
|
21
|
+
* To prevent this, we clamp the time to the duration reported by the extractor.
|
|
22
|
+
*/
|
|
23
|
+
const duration = extractor?.getDuration();
|
|
24
|
+
if (duration && time > duration) {
|
|
25
|
+
time = duration;
|
|
26
|
+
}
|
|
27
|
+
|
|
18
28
|
const isOldFrame =
|
|
19
29
|
extractor && Math.abs(time - extractor.getLastTime()) < frameDuration / 2;
|
|
20
30
|
|
|
21
31
|
// If time has not changed, return the last frame
|
|
22
|
-
if (isOldFrame) {
|
|
23
|
-
const lastFrame = extractor
|
|
32
|
+
if (extractor && isOldFrame) {
|
|
33
|
+
const lastFrame = extractor.getLastFrame();
|
|
24
34
|
if (!lastFrame) {
|
|
25
35
|
throw new Error('No last frame');
|
|
26
36
|
}
|
|
@@ -43,7 +53,7 @@ export async function getFrame(
|
|
|
43
53
|
}
|
|
44
54
|
|
|
45
55
|
if (!extractor) {
|
|
46
|
-
extractor = new
|
|
56
|
+
extractor = new Mp4Parser(filePath, fps, time);
|
|
47
57
|
await extractor.start();
|
|
48
58
|
videoFrameExtractors.set(extractorId, extractor);
|
|
49
59
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './parser';
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import {createFile} from 'mp4box';
|
|
2
|
+
import {FrameSampler} from './sampler';
|
|
3
|
+
import {Segment} from './segment';
|
|
4
|
+
import {MP4FileSink} from './sink';
|
|
5
|
+
import {Edit, description, mp4BoxEditToEdit} from './utils';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Loads the file at the given URI until it finds the moov box.
|
|
9
|
+
* Once found, it calls `setConfig` with the video configuration.
|
|
10
|
+
* @param uri - The URI of the video file.
|
|
11
|
+
* @param setConfig - Callback to set the video configuration.
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
async function getFileInfo(uri: string) {
|
|
15
|
+
return new Promise<{
|
|
16
|
+
file: any;
|
|
17
|
+
edits: Edit[];
|
|
18
|
+
config: VideoDecoderConfig;
|
|
19
|
+
}>((res, rej) => {
|
|
20
|
+
const file = createFile();
|
|
21
|
+
let found = false;
|
|
22
|
+
|
|
23
|
+
file.onReady = (info: any) => {
|
|
24
|
+
found = true;
|
|
25
|
+
|
|
26
|
+
const track = info.videoTracks[0];
|
|
27
|
+
|
|
28
|
+
const config = {
|
|
29
|
+
// Browser doesn't support parsing full vp8 codec (eg: `vp08.00.41.08`),
|
|
30
|
+
// they only support `vp8`.
|
|
31
|
+
codec: track.codec.startsWith('vp08') ? 'vp8' : track.codec,
|
|
32
|
+
codedHeight: track.video.height,
|
|
33
|
+
codedWidth: track.video.width,
|
|
34
|
+
description: description(file, track),
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const defaultEdit: Omit<Edit, 'fps'> = {
|
|
38
|
+
mediaTime: 0,
|
|
39
|
+
segmentDuration: track.duration,
|
|
40
|
+
mediaRateInteger: 1,
|
|
41
|
+
mediaRateFraction: 0,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const editsFromVideo: Omit<Edit, 'fps'>[] = track.edits?.map(
|
|
45
|
+
(edit: any) => mp4BoxEditToEdit(edit),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// If there are no entries, use the default edit
|
|
49
|
+
const editsWithoutFps = editsFromVideo.length
|
|
50
|
+
? editsFromVideo
|
|
51
|
+
: [defaultEdit];
|
|
52
|
+
|
|
53
|
+
// Calculate FPS for each segment
|
|
54
|
+
const edits = editsWithoutFps.map(edit => {
|
|
55
|
+
const trackDurationInSec = track.duration / track.timescale;
|
|
56
|
+
const segmentDurationInSec = edit.segmentDuration / track.timescale;
|
|
57
|
+
const segmentFrames =
|
|
58
|
+
track.nb_samples * (segmentDurationInSec / trackDurationInSec);
|
|
59
|
+
const mediaRate =
|
|
60
|
+
edit.mediaRateInteger + edit.mediaRateFraction / 0xffff;
|
|
61
|
+
const fps = (segmentFrames / segmentDurationInSec) * mediaRate;
|
|
62
|
+
return {...edit, fps};
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
file.setExtractionOptions(track.id);
|
|
66
|
+
file.start();
|
|
67
|
+
|
|
68
|
+
res({file, edits, config});
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return fetch(uri).then(async response => {
|
|
72
|
+
if (!response.body) {
|
|
73
|
+
throw new Error('Response body is null');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const reader = response.body.getReader();
|
|
77
|
+
const sink = new MP4FileSink(file, () => {});
|
|
78
|
+
|
|
79
|
+
while (!found) {
|
|
80
|
+
await reader.read().then(({done, value}) => {
|
|
81
|
+
if (done) {
|
|
82
|
+
file.flush();
|
|
83
|
+
rej('Could not find moov');
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
sink.write(value);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class Mp4Parser {
|
|
95
|
+
private uri: string;
|
|
96
|
+
private file: any;
|
|
97
|
+
private edits: Edit[] = [];
|
|
98
|
+
private decoderConfig?: VideoDecoderConfig;
|
|
99
|
+
|
|
100
|
+
private startTime: number;
|
|
101
|
+
private targetFps: number;
|
|
102
|
+
|
|
103
|
+
private nextSegment = 0;
|
|
104
|
+
private sampler?: FrameSampler;
|
|
105
|
+
|
|
106
|
+
public constructor(uri: string, targetFps: number, startTime: number) {
|
|
107
|
+
this.uri = uri;
|
|
108
|
+
this.targetFps = targetFps;
|
|
109
|
+
this.startTime = startTime;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public async start() {
|
|
113
|
+
const {file, edits, config} = await getFileInfo(this.uri);
|
|
114
|
+
this.file = file;
|
|
115
|
+
this.edits = edits;
|
|
116
|
+
this.decoderConfig = config;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public async getNextFrame() {
|
|
120
|
+
// Start the first segment
|
|
121
|
+
if (!this.sampler) {
|
|
122
|
+
// Skip segments until the start time
|
|
123
|
+
let startTimeWithinSegment = this.startTime;
|
|
124
|
+
while (this.nextSegment < this.edits.length) {
|
|
125
|
+
const segmentDurationInSeconds = this.getSecondDurationOfSegment(
|
|
126
|
+
this.edits[this.nextSegment],
|
|
127
|
+
);
|
|
128
|
+
if (startTimeWithinSegment < segmentDurationInSeconds) {
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
startTimeWithinSegment -= segmentDurationInSeconds;
|
|
133
|
+
this.nextSegment++;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// The timestamp is outside of the video
|
|
137
|
+
if (this.nextSegment >= this.edits.length) {
|
|
138
|
+
throw new Error(
|
|
139
|
+
`Timestamp ${this.startTime} is outside of the video, max timestamp is ${this.getDuration()}`,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const segment = new Segment(
|
|
144
|
+
this.uri,
|
|
145
|
+
this.file,
|
|
146
|
+
this.edits,
|
|
147
|
+
this.nextSegment,
|
|
148
|
+
startTimeWithinSegment,
|
|
149
|
+
);
|
|
150
|
+
await segment.start(this.decoderConfig!);
|
|
151
|
+
|
|
152
|
+
this.sampler = new FrameSampler(
|
|
153
|
+
segment,
|
|
154
|
+
this.edits[this.nextSegment].fps,
|
|
155
|
+
this.targetFps,
|
|
156
|
+
0,
|
|
157
|
+
);
|
|
158
|
+
this.nextSegment++;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Try to get the next frame
|
|
162
|
+
let frame = await this.sampler.getNextFrame();
|
|
163
|
+
|
|
164
|
+
// If there are no more frames in the current segment, start the next segment
|
|
165
|
+
while (!frame && this.nextSegment < this.edits.length) {
|
|
166
|
+
this.sampler.close();
|
|
167
|
+
const segment = new Segment(
|
|
168
|
+
this.uri,
|
|
169
|
+
this.file,
|
|
170
|
+
this.edits,
|
|
171
|
+
this.nextSegment,
|
|
172
|
+
0,
|
|
173
|
+
);
|
|
174
|
+
await segment.start(this.decoderConfig!);
|
|
175
|
+
|
|
176
|
+
this.sampler = new FrameSampler(
|
|
177
|
+
segment,
|
|
178
|
+
this.edits[this.nextSegment].fps,
|
|
179
|
+
this.targetFps,
|
|
180
|
+
this.sampler.getSum(), // Carry over the sum from the previous segment
|
|
181
|
+
);
|
|
182
|
+
this.nextSegment++;
|
|
183
|
+
|
|
184
|
+
frame = await this.sampler.getNextFrame();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// If there are no more frames, return the last frame
|
|
188
|
+
if (!frame) {
|
|
189
|
+
frame = this.sampler.getLastFrame();
|
|
190
|
+
|
|
191
|
+
// If we still don't have a frame, throw an error
|
|
192
|
+
if (!frame) {
|
|
193
|
+
throw new Error('There are no frames in the video.');
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return frame;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private getSecondDurationOfSegment(edit: Edit) {
|
|
201
|
+
const mediaRate = edit.mediaRateInteger + edit.mediaRateFraction / 0xffff;
|
|
202
|
+
const duration =
|
|
203
|
+
edit.segmentDuration / this.file.getInfo().videoTracks[0].timescale;
|
|
204
|
+
return duration / mediaRate;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
public getDuration() {
|
|
208
|
+
return this.edits.reduce(
|
|
209
|
+
(acc, edit) => acc + this.getSecondDurationOfSegment(edit),
|
|
210
|
+
0,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private getTimeSubtractingFrames(frames: number) {
|
|
215
|
+
const durationOfPastSegmentsInSeconds = this.edits
|
|
216
|
+
.slice(0, this.nextSegment - 1)
|
|
217
|
+
.reduce((acc, edit) => acc + this.getSecondDurationOfSegment(edit), 0);
|
|
218
|
+
|
|
219
|
+
const currentSegment = this.sampler?.getSegment();
|
|
220
|
+
if (!currentSegment) {
|
|
221
|
+
throw new Error('No current segment');
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const currentSegmentFrames = currentSegment.getFramesProcessed() - frames;
|
|
225
|
+
const currentSegmentFps = this.edits[this.nextSegment - 1].fps;
|
|
226
|
+
const currentSegmentDuration = currentSegmentFrames / currentSegmentFps;
|
|
227
|
+
|
|
228
|
+
return durationOfPastSegmentsInSeconds + currentSegmentDuration;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
public getTime() {
|
|
232
|
+
return this.getTimeSubtractingFrames(0);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
public getLastTime() {
|
|
236
|
+
return this.getTimeSubtractingFrames(1);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
public getLastFrame() {
|
|
240
|
+
return this.sampler?.getLastFrame();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
public close() {
|
|
244
|
+
this.sampler?.close();
|
|
245
|
+
this.file.close();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {Segment} from './segment';
|
|
2
|
+
|
|
3
|
+
export class FrameSampler {
|
|
4
|
+
private lastFrame?: VideoFrame;
|
|
5
|
+
|
|
6
|
+
public constructor(
|
|
7
|
+
private segment: Segment,
|
|
8
|
+
private sourceFps: number,
|
|
9
|
+
private targetFps: number,
|
|
10
|
+
private sum: number = 0,
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
public async getNextFrame() {
|
|
14
|
+
const samplingRate = this.sourceFps / this.targetFps;
|
|
15
|
+
this.sum += samplingRate;
|
|
16
|
+
|
|
17
|
+
// Return last frame if we haven't progressed enough to get a new frame.
|
|
18
|
+
if (this.sum < 1 && this.lastFrame) {
|
|
19
|
+
return this.lastFrame;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Burn frames if we have too many.
|
|
23
|
+
while (this.sum >= 2) {
|
|
24
|
+
// Burn frame
|
|
25
|
+
const frame = await this.segment.getNextFrame();
|
|
26
|
+
|
|
27
|
+
// If there are no more frames, return
|
|
28
|
+
if (!frame) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
frame.close();
|
|
33
|
+
this.sum -= 1;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Get a new frame.
|
|
37
|
+
if (this.sum >= 1 || !this.lastFrame) {
|
|
38
|
+
this.sum -= 1;
|
|
39
|
+
const frame = await this.segment.getNextFrame();
|
|
40
|
+
|
|
41
|
+
// If there are no more frames, return
|
|
42
|
+
if (!frame) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.lastFrame?.close();
|
|
47
|
+
this.lastFrame = frame;
|
|
48
|
+
return frame;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// One of the three if statements above is always true.
|
|
52
|
+
throw new Error('Unreachable code');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public getLastFrame() {
|
|
56
|
+
return this.lastFrame;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public async close() {
|
|
60
|
+
this.lastFrame?.close();
|
|
61
|
+
this.segment.close();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public getSum() {
|
|
65
|
+
return this.sum;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public getSegment() {
|
|
69
|
+
return this.segment;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import {MP4FileSink} from './sink';
|
|
2
|
+
import {Edit} from './utils';
|
|
3
|
+
|
|
4
|
+
export class Segment {
|
|
5
|
+
private done: boolean = false;
|
|
6
|
+
private uri: string;
|
|
7
|
+
|
|
8
|
+
private file: any;
|
|
9
|
+
private edit: Edit;
|
|
10
|
+
|
|
11
|
+
private responseFinished: boolean = false;
|
|
12
|
+
private decoder: VideoDecoder;
|
|
13
|
+
|
|
14
|
+
private framesProcessed = 0;
|
|
15
|
+
private startTime: number;
|
|
16
|
+
private framesDue = 0;
|
|
17
|
+
private frameBuffer: VideoFrame[] = [];
|
|
18
|
+
|
|
19
|
+
private readMore: () => Promise<void> = async () => {};
|
|
20
|
+
|
|
21
|
+
public constructor(
|
|
22
|
+
uri: string,
|
|
23
|
+
file: any,
|
|
24
|
+
edits: Edit[],
|
|
25
|
+
currentSegment: number,
|
|
26
|
+
startTime: number = 0,
|
|
27
|
+
) {
|
|
28
|
+
this.uri = uri;
|
|
29
|
+
|
|
30
|
+
this.file = file;
|
|
31
|
+
this.file.onSamples = this.onSamples.bind(this);
|
|
32
|
+
this.edit = edits[currentSegment];
|
|
33
|
+
|
|
34
|
+
this.startTime = startTime;
|
|
35
|
+
|
|
36
|
+
// Check how many frames we skipped because of the startTime and add them to the frames processed.
|
|
37
|
+
const framesSkipped = Math.floor(this.startTime * this.edit.fps);
|
|
38
|
+
this.framesProcessed += framesSkipped;
|
|
39
|
+
|
|
40
|
+
this.decoder = new VideoDecoder({
|
|
41
|
+
output: this.onFrame.bind(this),
|
|
42
|
+
error(e) {
|
|
43
|
+
console.error(e);
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public async start(decoderConfig: VideoDecoderConfig) {
|
|
49
|
+
this.decoder.configure(decoderConfig);
|
|
50
|
+
const videoTrack = this.file.getInfo().videoTracks[0];
|
|
51
|
+
const trak = this.file.getTrackById(videoTrack.id);
|
|
52
|
+
|
|
53
|
+
const mediaTimeInTimescale = this.edit.mediaTime;
|
|
54
|
+
const startTimeInTimescale = this.startTime * videoTrack.timescale;
|
|
55
|
+
const seekTimeInTimescale = mediaTimeInTimescale + startTimeInTimescale;
|
|
56
|
+
|
|
57
|
+
const seekTimeInSec = seekTimeInTimescale / videoTrack.timescale;
|
|
58
|
+
const seekInfo = this.file.seekTrack(seekTimeInSec, true, trak);
|
|
59
|
+
this.readMore = await this.startStreamingAtOffset(
|
|
60
|
+
this.file,
|
|
61
|
+
this.uri,
|
|
62
|
+
seekInfo.offset,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Starts streaming the video at the given URI from the given offset.
|
|
68
|
+
* @param file - MP4Box file. Needs to be created and configured before calling this function.
|
|
69
|
+
* @param uri - URI of the video file.
|
|
70
|
+
* @param offset - Offset to start streaming from.
|
|
71
|
+
* @returns - A function to read more data from the response.
|
|
72
|
+
*/
|
|
73
|
+
private async startStreamingAtOffset(file: any, uri: string, offset: number) {
|
|
74
|
+
return fetch(uri, {
|
|
75
|
+
headers: {
|
|
76
|
+
/* eslint-disable-next-line @typescript-eslint/naming-convention */
|
|
77
|
+
Range: `bytes=${offset}-`,
|
|
78
|
+
},
|
|
79
|
+
}).then(async response => {
|
|
80
|
+
if (!response.body) {
|
|
81
|
+
throw new Error('Response body is null');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const reader = response.body.getReader();
|
|
85
|
+
const sink = new MP4FileSink(file, () => {}, offset);
|
|
86
|
+
|
|
87
|
+
return async () => {
|
|
88
|
+
return reader.read().then(({done, value}) => {
|
|
89
|
+
// Request is done.
|
|
90
|
+
if (done) {
|
|
91
|
+
this.responseFinished = true;
|
|
92
|
+
this.decoder.flush();
|
|
93
|
+
sink.close();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
sink.write(value);
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Called when samples are available on the MP4 file.
|
|
105
|
+
* Sends chunks to the decoder.
|
|
106
|
+
*/
|
|
107
|
+
private onSamples(_unused1: any, _unused2: any, samples: any) {
|
|
108
|
+
for (const sample of samples) {
|
|
109
|
+
const chunk = new EncodedVideoChunk({
|
|
110
|
+
type: sample.is_sync ? 'key' : 'delta',
|
|
111
|
+
timestamp: (1e6 * sample.cts) / sample.timescale,
|
|
112
|
+
duration: (1e6 * sample.duration) / sample.timescale,
|
|
113
|
+
data: sample.data,
|
|
114
|
+
});
|
|
115
|
+
this.framesDue++;
|
|
116
|
+
this.decoder.decode(chunk);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Called when the decoder has a frame ready.
|
|
122
|
+
* Pushes the frame to the buffer so it can be consumed.
|
|
123
|
+
*/
|
|
124
|
+
private onFrame(frame: VideoFrame) {
|
|
125
|
+
this.framesDue--;
|
|
126
|
+
|
|
127
|
+
// If the frame comes before the seek time, close it.
|
|
128
|
+
const mediaTimeInSec =
|
|
129
|
+
this.edit.mediaTime / this.file.getInfo().videoTracks[0].timescale;
|
|
130
|
+
const seekTimeInSec = this.startTime + mediaTimeInSec;
|
|
131
|
+
const frameTimeInSec = frame.timestamp / 1e6;
|
|
132
|
+
if (frameTimeInSec < seekTimeInSec) {
|
|
133
|
+
frame.close();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Check if we are past the segment duration.
|
|
138
|
+
const segmentDurationInSec =
|
|
139
|
+
this.edit.segmentDuration / this.file.getInfo().videoTracks[0].timescale;
|
|
140
|
+
const segmentEndTime = mediaTimeInSec + segmentDurationInSec;
|
|
141
|
+
if (frameTimeInSec > segmentEndTime) {
|
|
142
|
+
frame.close();
|
|
143
|
+
this.done = true;
|
|
144
|
+
this.decoder.flush();
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
this.frameBuffer.push(frame);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private async populateBuffer() {
|
|
152
|
+
// Fetch more frames if we don't have any.
|
|
153
|
+
while (this.frameBuffer.length === 0 && !this.responseFinished) {
|
|
154
|
+
await this.readMore();
|
|
155
|
+
await new Promise(res => setTimeout(res, 0));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Wait for decoder if there are frames due.
|
|
159
|
+
if (this.frameBuffer.length === 0 && this.framesDue > 0) {
|
|
160
|
+
let maxIterations = 1000;
|
|
161
|
+
while (this.frameBuffer.length === 0) {
|
|
162
|
+
await new Promise(res => setTimeout(res, 10));
|
|
163
|
+
maxIterations--;
|
|
164
|
+
|
|
165
|
+
if (this.done) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (maxIterations === 0) {
|
|
170
|
+
throw new Error(
|
|
171
|
+
`Timed out while waiting for VideoDecoder to produce a frame. Frames due: ${this.framesDue}`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public async getNextFrame() {
|
|
179
|
+
await this.populateBuffer();
|
|
180
|
+
const frame = this.frameBuffer.shift();
|
|
181
|
+
if (frame) {
|
|
182
|
+
this.framesProcessed++;
|
|
183
|
+
}
|
|
184
|
+
return frame;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Called when we are done with the extractor.
|
|
189
|
+
*/
|
|
190
|
+
public close() {
|
|
191
|
+
this.frameBuffer.forEach(frame => frame.close());
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
public getFramesProcessed() {
|
|
195
|
+
return this.framesProcessed;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Wraps an MP4Box File as a WritableStream underlying sink.
|
|
2
|
+
export class MP4FileSink {
|
|
3
|
+
private setStatus;
|
|
4
|
+
private file;
|
|
5
|
+
private offset;
|
|
6
|
+
|
|
7
|
+
public constructor(file: any, setStatus: any, offset: number = 0) {
|
|
8
|
+
this.file = file;
|
|
9
|
+
this.setStatus = setStatus;
|
|
10
|
+
this.offset = offset;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public write(chunk: any) {
|
|
14
|
+
const buffer = new ArrayBuffer(chunk.byteLength);
|
|
15
|
+
new Uint8Array(buffer).set(chunk);
|
|
16
|
+
|
|
17
|
+
// Inform MP4Box where in the file this chunk is from.
|
|
18
|
+
(buffer as any).fileStart = this.offset;
|
|
19
|
+
this.offset += buffer.byteLength;
|
|
20
|
+
|
|
21
|
+
this.setStatus('fetch', (this.offset / 1024 ** 2).toFixed(1) + ' MiB');
|
|
22
|
+
this.file.appendBuffer(buffer);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public close() {
|
|
26
|
+
this.setStatus('fetch', 'Done');
|
|
27
|
+
this.file.flush();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {DataStream} from 'mp4box';
|
|
2
|
+
|
|
3
|
+
export function description(file: any, track: any) {
|
|
4
|
+
const trak = file.getTrackById(track.id);
|
|
5
|
+
for (const entry of trak.mdia.minf.stbl.stsd.entries) {
|
|
6
|
+
const box = entry.avcC || entry.hvcC || entry.vpcC || entry.av1C;
|
|
7
|
+
if (box) {
|
|
8
|
+
const stream = new DataStream(undefined, 0, DataStream.BIG_ENDIAN);
|
|
9
|
+
box.write(stream);
|
|
10
|
+
return new Uint8Array(stream.buffer, 8); // Remove the box header.
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
throw new Error('avcC, hvcC, vpcC, or av1C box not found');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface Edit {
|
|
17
|
+
mediaTime: number;
|
|
18
|
+
segmentDuration: number;
|
|
19
|
+
mediaRateInteger: number;
|
|
20
|
+
mediaRateFraction: number;
|
|
21
|
+
fps: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function mp4BoxEditToEdit(edit: any): Omit<Edit, 'fps'> {
|
|
25
|
+
return {
|
|
26
|
+
mediaTime: edit.media_time,
|
|
27
|
+
segmentDuration: edit.segment_duration,
|
|
28
|
+
mediaRateInteger: edit.media_rate_integer,
|
|
29
|
+
mediaRateFraction: edit.media_rate_fraction,
|
|
30
|
+
};
|
|
31
|
+
}
|