@revideo/2d 0.4.7-two.1024 → 0.4.8-alpha.1032
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/editor/index.js +151 -380
- package/editor/index.js.map +1 -1
- package/lib/code/CodeCursor.d.ts +74 -74
- package/lib/code/CodeDiffer.d.ts +16 -21
- package/lib/code/CodeDiffer.js +41 -39
- package/lib/code/CodeFragment.d.ts +8 -16
- package/lib/code/CodeFragment.js +36 -36
- package/lib/code/CodeHighlighter.d.ts +59 -59
- package/lib/code/CodeHighlighter.js +1 -1
- package/lib/code/CodeMetrics.d.ts +8 -12
- package/lib/code/CodeMetrics.js +24 -26
- package/lib/code/CodeRange.d.ts +5 -21
- package/lib/code/CodeRange.js +124 -131
- package/lib/code/CodeScope.d.ts +8 -18
- package/lib/code/CodeSelection.d.ts +4 -9
- package/lib/code/CodeSelection.js +8 -8
- package/lib/code/CodeSignal.d.ts +47 -74
- package/lib/code/CodeTokenizer.d.ts +1 -1
- package/lib/code/CodeTokenizer.js +41 -41
- package/lib/code/DefaultHighlightStyle.d.ts +2 -2
- package/lib/code/DefaultHighlightStyle.js +96 -96
- package/lib/code/LezerHighlighter.d.ts +19 -21
- package/lib/code/diff.d.ts +9 -12
- package/lib/code/diff.js +205 -218
- package/lib/code/extractRange.d.ts +4 -7
- package/lib/code/extractRange.js +71 -79
- package/lib/code/index.d.ts +1 -1
- package/lib/code/index.js +1 -1
- package/lib/components/Audio.d.ts +11 -11
- package/lib/components/Bezier.d.ts +18 -18
- package/lib/components/Circle.d.ts +90 -95
- package/lib/components/Code.d.ts +211 -244
- package/lib/components/CodeBlock.d.ts +1 -1
- package/lib/components/CubicBezier.d.ts +36 -36
- package/lib/components/Curve.d.ts +198 -208
- package/lib/components/Grid.d.ts +42 -47
- package/lib/components/Icon.d.ts +50 -55
- package/lib/components/Img.d.ts +79 -86
- package/lib/components/Knot.d.ts +78 -97
- package/lib/components/Latex.d.ts +12 -12
- package/lib/components/Layout.d.ts +413 -455
- package/lib/components/Line.d.ts +49 -61
- package/lib/components/Media.d.ts +37 -37
- package/lib/components/Node.d.ts +1 -1
- package/lib/components/Path.d.ts +1 -1
- package/lib/components/Polygon.d.ts +55 -60
- package/lib/components/QuadBezier.d.ts +29 -29
- package/lib/components/Ray.d.ts +28 -28
- package/lib/components/Rect.d.ts +108 -115
- package/lib/components/SVG.d.ts +1 -1
- package/lib/components/Shape.d.ts +1 -1
- package/lib/components/Spline.d.ts +40 -46
- package/lib/components/Txt.d.ts +44 -55
- package/lib/components/TxtLeaf.d.ts +16 -20
- package/lib/components/Video.d.ts +1 -0
- package/lib/components/Video.d.ts.map +1 -1
- package/lib/components/Video.js +9 -3
- package/lib/components/View2D.d.ts +22 -22
- package/lib/components/index.d.ts +2 -1
- package/lib/components/index.d.ts.map +1 -1
- package/lib/components/index.js +2 -1
- package/lib/components/types.d.ts +9 -22
- package/lib/curves/ArcSegment.d.ts +24 -36
- package/lib/curves/CircleSegment.d.ts +16 -26
- package/lib/curves/CubicBezierSegment.d.ts +13 -18
- package/lib/curves/CurveDrawingInfo.d.ts +9 -9
- package/lib/curves/CurvePoint.d.ts +13 -13
- package/lib/curves/CurveProfile.d.ts +5 -5
- package/lib/curves/CurveProfile.js +1 -1
- package/lib/curves/KnotInfo.d.ts +8 -8
- package/lib/curves/LineSegment.d.ts +14 -19
- package/lib/curves/Polynomial.d.ts +110 -110
- package/lib/curves/Polynomial2D.d.ts +20 -20
- package/lib/curves/PolynomialSegment.d.ts +37 -42
- package/lib/curves/QuadBezierSegment.d.ts +12 -12
- package/lib/curves/Segment.d.ts +7 -12
- package/lib/curves/UniformPolynomialCurveSampler.d.ts +34 -34
- package/lib/curves/createCurveProfileLerp.d.ts +6 -17
- package/lib/curves/getBezierSplineProfile.d.ts +4 -8
- package/lib/curves/getCircleProfile.d.ts +4 -10
- package/lib/curves/getPathProfile.d.ts +2 -2
- package/lib/curves/getPointAtDistance.d.ts +4 -7
- package/lib/curves/getPolylineProfile.d.ts +4 -8
- package/lib/curves/getRectProfile.d.ts +4 -9
- package/lib/curves/index.d.ts +1 -1
- package/lib/curves/index.js +1 -1
- package/lib/decorators/canvasStyleSignal.d.ts +3 -3
- package/lib/decorators/colorSignal.d.ts +1 -1
- package/lib/decorators/compound.d.ts +2 -4
- package/lib/decorators/computed.d.ts +1 -1
- package/lib/decorators/defaultStyle.d.ts +2 -5
- package/lib/decorators/filtersSignal.d.ts +8 -28
- package/lib/decorators/index.d.ts +1 -1
- package/lib/decorators/index.js +1 -1
- package/lib/decorators/initializers.d.ts +2 -5
- package/lib/decorators/initializers.js +19 -18
- package/lib/decorators/nodeName.d.ts +1 -1
- package/lib/decorators/nodeName.js +4 -4
- package/lib/decorators/signal.d.ts +20 -40
- package/lib/decorators/spacingSignal.d.ts +1 -1
- package/lib/decorators/vector2Signal.d.ts +7 -13
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/jsx-dev-runtime.d.ts +3 -3
- package/lib/jsx-dev-runtime.js +3 -3
- package/lib/jsx-runtime.d.ts +9 -19
- package/lib/jsx-runtime.js +18 -18
- package/lib/partials/Filter.d.ts +16 -24
- package/lib/partials/Gradient.d.ts +24 -30
- package/lib/partials/Pattern.d.ts +9 -14
- package/lib/partials/ShaderConfig.d.ts +74 -81
- package/lib/partials/index.d.ts +1 -1
- package/lib/partials/index.js +1 -1
- package/lib/partials/types.d.ts +6 -19
- package/lib/scenes/Scene2D.d.ts +24 -42
- package/lib/scenes/Scene2D.d.ts.map +1 -1
- package/lib/scenes/Scene2D.js +10 -2
- package/lib/scenes/index.d.ts +1 -1
- package/lib/scenes/index.js +1 -1
- package/lib/scenes/makeScene2D.d.ts +5 -7
- package/lib/scenes/useScene2D.d.ts +2 -2
- package/lib/tsconfig.build.tsbuildinfo +1 -1
- package/lib/utils/diff.d.ts +18 -25
- package/lib/utils/diff.js +87 -84
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +1 -1
- package/lib/utils/is.d.ts +2 -4
- package/lib/utils/is.js +2 -2
- package/lib/utils/makeSignalExtensions.d.ts +4 -11
- package/lib/utils/video/mp4-parser-manager.d.ts +1 -0
- package/lib/utils/video/mp4-parser-manager.d.ts.map +1 -1
- package/lib/utils/video/mp4-parser-manager.js +21 -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 +168 -0
- package/lib/utils/video/parser/sampler.d.ts +16 -0
- package/lib/utils/video/parser/sampler.d.ts.map +1 -0
- package/lib/utils/video/parser/sampler.js +56 -0
- package/lib/utils/video/parser/segment.d.ts +44 -0
- package/lib/utils/video/parser/segment.d.ts.map +1 -0
- package/lib/utils/video/parser/segment.js +195 -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 +4 -4
- package/src/lib/components/Video.ts +9 -2
- package/src/lib/components/index.ts +1 -0
- package/src/lib/scenes/Scene2D.ts +11 -1
- package/src/lib/utils/video/mp4-parser-manager.ts +24 -5
- package/src/lib/utils/video/parser/index.ts +1 -0
- package/src/lib/utils/video/parser/parser.ts +256 -0
- package/src/lib/utils/video/parser/sampler.ts +72 -0
- package/src/lib/utils/video/parser/segment.ts +252 -0
- package/src/lib/utils/video/parser/sink.ts +29 -0
- package/src/lib/utils/video/parser/utils.ts +31 -0
- package/lib/components/HlsVideo.d.ts +0 -62
- package/lib/components/HlsVideo.d.ts.map +0 -1
- package/lib/components/HlsVideo.js +0 -169
- package/lib/tsconfig.tsbuildinfo +0 -1
- 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
|
@@ -0,0 +1,256 @@
|
|
|
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
|
+
const controller = new AbortController();
|
|
23
|
+
|
|
24
|
+
file.onReady = (info: any) => {
|
|
25
|
+
found = true;
|
|
26
|
+
controller.abort();
|
|
27
|
+
|
|
28
|
+
const track = info.videoTracks[0];
|
|
29
|
+
|
|
30
|
+
const config = {
|
|
31
|
+
// Browser doesn't support parsing full vp8 codec (eg: `vp08.00.41.08`),
|
|
32
|
+
// they only support `vp8`.
|
|
33
|
+
codec: track.codec.startsWith('vp08') ? 'vp8' : track.codec,
|
|
34
|
+
codedHeight: track.video.height,
|
|
35
|
+
codedWidth: track.video.width,
|
|
36
|
+
description: description(file, track),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const defaultEdit: Omit<Edit, 'fps'> = {
|
|
40
|
+
mediaTime: 0,
|
|
41
|
+
segmentDuration: track.duration,
|
|
42
|
+
mediaRateInteger: 1,
|
|
43
|
+
mediaRateFraction: 0,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const editsFromVideo: Omit<Edit, 'fps'>[] = track.edits?.map(
|
|
47
|
+
(edit: any) => mp4BoxEditToEdit(edit),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
// If there are no entries, use the default edit
|
|
51
|
+
const editsWithoutFps = editsFromVideo?.length
|
|
52
|
+
? editsFromVideo
|
|
53
|
+
: [defaultEdit];
|
|
54
|
+
|
|
55
|
+
// Calculate FPS for each segment
|
|
56
|
+
const edits = editsWithoutFps.map(edit => {
|
|
57
|
+
const trackDurationInSec = track.duration / track.timescale;
|
|
58
|
+
const segmentDurationInSec =
|
|
59
|
+
edit.segmentDuration / track.movie_timescale;
|
|
60
|
+
const segmentFrames =
|
|
61
|
+
track.nb_samples * (segmentDurationInSec / trackDurationInSec);
|
|
62
|
+
const mediaRate =
|
|
63
|
+
edit.mediaRateInteger + edit.mediaRateFraction / 0xffff;
|
|
64
|
+
const fps = (segmentFrames / segmentDurationInSec) * mediaRate;
|
|
65
|
+
return {...edit, fps};
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
file.setExtractionOptions(track.id);
|
|
69
|
+
file.start();
|
|
70
|
+
|
|
71
|
+
res({file, edits, config});
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
return fetch(uri, {signal: controller.signal}).then(async response => {
|
|
75
|
+
if (!response.body) {
|
|
76
|
+
throw new Error('Response body is null');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const reader = response.body.getReader();
|
|
80
|
+
const sink = new MP4FileSink(file, () => {});
|
|
81
|
+
|
|
82
|
+
while (!found) {
|
|
83
|
+
await reader.read().then(({done, value}) => {
|
|
84
|
+
if (done) {
|
|
85
|
+
file.flush();
|
|
86
|
+
controller.abort();
|
|
87
|
+
rej('Could not find moov');
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
sink.write(value);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export class Mp4Parser {
|
|
99
|
+
private uri: string;
|
|
100
|
+
private file: any;
|
|
101
|
+
private edits: Edit[] = [];
|
|
102
|
+
private decoderConfig?: VideoDecoderConfig;
|
|
103
|
+
|
|
104
|
+
private startTime: number;
|
|
105
|
+
private targetFps: number;
|
|
106
|
+
|
|
107
|
+
private nextSegment = 0;
|
|
108
|
+
private sampler?: FrameSampler;
|
|
109
|
+
|
|
110
|
+
public constructor(uri: string, targetFps: number, startTime: number) {
|
|
111
|
+
this.uri = uri;
|
|
112
|
+
this.targetFps = targetFps;
|
|
113
|
+
this.startTime = startTime;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public async start() {
|
|
117
|
+
const {file, edits, config} = await getFileInfo(this.uri);
|
|
118
|
+
this.file = file;
|
|
119
|
+
this.edits = edits;
|
|
120
|
+
this.decoderConfig = config;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public async getNextFrame() {
|
|
124
|
+
// Start the first segment
|
|
125
|
+
if (!this.sampler) {
|
|
126
|
+
// Skip segments until the start time
|
|
127
|
+
let startTimeWithinSegment = this.startTime;
|
|
128
|
+
while (this.nextSegment < this.edits.length) {
|
|
129
|
+
const segmentDurationInSeconds = this.getSecondDurationOfSegment(
|
|
130
|
+
this.edits[this.nextSegment],
|
|
131
|
+
);
|
|
132
|
+
if (startTimeWithinSegment < segmentDurationInSeconds) {
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
startTimeWithinSegment -= segmentDurationInSeconds;
|
|
137
|
+
this.nextSegment++;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// The timestamp is outside of the video
|
|
141
|
+
if (this.nextSegment >= this.edits.length) {
|
|
142
|
+
throw new Error(
|
|
143
|
+
`Timestamp ${this.startTime} is outside of the video, max timestamp is ${this.getDuration()}`,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const segment = new Segment(
|
|
148
|
+
this.uri,
|
|
149
|
+
this.file,
|
|
150
|
+
this.edits,
|
|
151
|
+
this.nextSegment,
|
|
152
|
+
startTimeWithinSegment,
|
|
153
|
+
);
|
|
154
|
+
await segment.start(this.decoderConfig!);
|
|
155
|
+
|
|
156
|
+
this.sampler = new FrameSampler(
|
|
157
|
+
segment,
|
|
158
|
+
this.edits[this.nextSegment].fps,
|
|
159
|
+
this.targetFps,
|
|
160
|
+
0,
|
|
161
|
+
);
|
|
162
|
+
this.nextSegment++;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Try to get the next frame
|
|
166
|
+
let frame = await this.sampler.getNextFrame();
|
|
167
|
+
|
|
168
|
+
// If there are no more frames in the current segment, start the next segment
|
|
169
|
+
while (!frame && this.nextSegment < this.edits.length) {
|
|
170
|
+
this.sampler.getSegment().close();
|
|
171
|
+
this.sampler.getLastFrame()?.close();
|
|
172
|
+
const segment = new Segment(
|
|
173
|
+
this.uri,
|
|
174
|
+
this.file,
|
|
175
|
+
this.edits,
|
|
176
|
+
this.nextSegment,
|
|
177
|
+
0,
|
|
178
|
+
);
|
|
179
|
+
await segment.start(this.decoderConfig!);
|
|
180
|
+
|
|
181
|
+
this.sampler = new FrameSampler(
|
|
182
|
+
segment,
|
|
183
|
+
this.edits[this.nextSegment].fps,
|
|
184
|
+
this.targetFps,
|
|
185
|
+
this.sampler.getSum(), // Carry over the sum from the previous segment
|
|
186
|
+
);
|
|
187
|
+
this.nextSegment++;
|
|
188
|
+
|
|
189
|
+
frame = await this.sampler.getNextFrame();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// If there are no more frames, return the last frame
|
|
193
|
+
if (!frame) {
|
|
194
|
+
frame = this.sampler.getLastFrame();
|
|
195
|
+
|
|
196
|
+
// We can close the segment
|
|
197
|
+
await this.sampler.getSegment().close();
|
|
198
|
+
|
|
199
|
+
// If we still don't have a frame, throw an error
|
|
200
|
+
if (!frame) {
|
|
201
|
+
throw new Error('There are no frames in the video.');
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return frame;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private getSecondDurationOfSegment(edit: Edit) {
|
|
209
|
+
const mediaRate = edit.mediaRateInteger + edit.mediaRateFraction / 0xffff;
|
|
210
|
+
const duration =
|
|
211
|
+
edit.segmentDuration / this.file.getInfo().videoTracks[0].movie_timescale;
|
|
212
|
+
return duration / mediaRate;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
public getDuration() {
|
|
216
|
+
return this.edits.reduce(
|
|
217
|
+
(acc, edit) => acc + this.getSecondDurationOfSegment(edit),
|
|
218
|
+
0,
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private getTimeSubtractingFrames(frames: number) {
|
|
223
|
+
const durationOfPastSegmentsInSeconds = this.edits
|
|
224
|
+
.slice(0, this.nextSegment - 1)
|
|
225
|
+
.reduce((acc, edit) => acc + this.getSecondDurationOfSegment(edit), 0);
|
|
226
|
+
|
|
227
|
+
if (!this.sampler) {
|
|
228
|
+
throw new Error('No current segment');
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const currentSegmentStartTime = this.sampler.getSegment().getStartTime();
|
|
232
|
+
const samplerTime = this.sampler.getTime(frames);
|
|
233
|
+
|
|
234
|
+
return (
|
|
235
|
+
durationOfPastSegmentsInSeconds + currentSegmentStartTime + samplerTime
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
public getTime() {
|
|
240
|
+
return this.getTimeSubtractingFrames(0);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
public getLastTime() {
|
|
244
|
+
return this.getTimeSubtractingFrames(1);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
public getLastFrame() {
|
|
248
|
+
return this.sampler?.getLastFrame();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
public close() {
|
|
252
|
+
this.sampler?.getSegment().close();
|
|
253
|
+
this.sampler?.getLastFrame()?.close();
|
|
254
|
+
this.file.flush();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {Segment} from './segment';
|
|
2
|
+
|
|
3
|
+
export class FrameSampler {
|
|
4
|
+
private framesRequested = 0;
|
|
5
|
+
private lastFrame?: VideoFrame;
|
|
6
|
+
|
|
7
|
+
public constructor(
|
|
8
|
+
private segment: Segment,
|
|
9
|
+
private sourceFps: number,
|
|
10
|
+
private targetFps: number,
|
|
11
|
+
private sum: number = 0,
|
|
12
|
+
) {}
|
|
13
|
+
|
|
14
|
+
public async getNextFrame() {
|
|
15
|
+
const samplingRate = this.sourceFps / this.targetFps;
|
|
16
|
+
this.sum += samplingRate;
|
|
17
|
+
this.framesRequested += 1;
|
|
18
|
+
|
|
19
|
+
// Return last frame if we haven't progressed enough to get a new frame.
|
|
20
|
+
if (this.sum < 1 && this.lastFrame) {
|
|
21
|
+
return this.lastFrame;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Burn frames if we have too many.
|
|
25
|
+
while (this.sum >= 2) {
|
|
26
|
+
// Burn frame
|
|
27
|
+
const frame = await this.segment.getNextFrame();
|
|
28
|
+
|
|
29
|
+
// If there are no more frames, return
|
|
30
|
+
if (!frame) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
frame.close();
|
|
35
|
+
this.sum -= 1;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Get a new frame.
|
|
39
|
+
if (this.sum >= 1 || !this.lastFrame) {
|
|
40
|
+
this.sum -= 1;
|
|
41
|
+
const frame = await this.segment.getNextFrame();
|
|
42
|
+
|
|
43
|
+
// If there are no more frames, return
|
|
44
|
+
if (!frame) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this.lastFrame?.close();
|
|
49
|
+
this.lastFrame = frame;
|
|
50
|
+
return frame;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// One of the three if statements above is always true.
|
|
54
|
+
throw new Error('Unreachable code');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public getLastFrame() {
|
|
58
|
+
return this.lastFrame;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public getSum() {
|
|
62
|
+
return this.sum;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public getSegment() {
|
|
66
|
+
return this.segment;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public getTime(framesToSubtract: number) {
|
|
70
|
+
return (this.framesRequested - framesToSubtract) / this.targetFps;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import {MP4FileSink} from './sink';
|
|
2
|
+
import {Edit} from './utils';
|
|
3
|
+
|
|
4
|
+
export class Segment {
|
|
5
|
+
private done: boolean = false;
|
|
6
|
+
private abortController = new AbortController();
|
|
7
|
+
private uri: string;
|
|
8
|
+
|
|
9
|
+
private file: any;
|
|
10
|
+
private edit: Edit;
|
|
11
|
+
|
|
12
|
+
private responseFinished: boolean = false;
|
|
13
|
+
private decoder: VideoDecoder;
|
|
14
|
+
|
|
15
|
+
private framesProcessed = 0;
|
|
16
|
+
private startTime: number;
|
|
17
|
+
private framesDue = 0;
|
|
18
|
+
private frameBuffer: VideoFrame[] = [];
|
|
19
|
+
|
|
20
|
+
private readMore: () => Promise<void> = async () => {};
|
|
21
|
+
|
|
22
|
+
public constructor(
|
|
23
|
+
uri: string,
|
|
24
|
+
file: any,
|
|
25
|
+
edits: Edit[],
|
|
26
|
+
currentSegment: number,
|
|
27
|
+
startTime: number = 0,
|
|
28
|
+
) {
|
|
29
|
+
this.uri = uri;
|
|
30
|
+
|
|
31
|
+
this.file = file;
|
|
32
|
+
this.file.onSamples = this.onSamples.bind(this);
|
|
33
|
+
this.edit = edits[currentSegment];
|
|
34
|
+
|
|
35
|
+
this.startTime = startTime;
|
|
36
|
+
|
|
37
|
+
// Check how many frames we skipped because of the startTime and add them to the frames processed.
|
|
38
|
+
const framesSkipped = Math.floor(this.startTime * this.edit.fps);
|
|
39
|
+
this.framesProcessed += framesSkipped;
|
|
40
|
+
|
|
41
|
+
this.decoder = new VideoDecoder({
|
|
42
|
+
output: this.onFrame.bind(this),
|
|
43
|
+
error(e) {
|
|
44
|
+
console.error(e);
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public async start(decoderConfig: VideoDecoderConfig) {
|
|
50
|
+
// If this is an empty edit, we just need to fill the buffer with empty frames.
|
|
51
|
+
if (this.edit.mediaTime === -1) {
|
|
52
|
+
this.done = true;
|
|
53
|
+
this.responseFinished = true;
|
|
54
|
+
this.decoder.close();
|
|
55
|
+
|
|
56
|
+
const segmentDurationInSeconds =
|
|
57
|
+
this.edit.segmentDuration /
|
|
58
|
+
this.file.getInfo().videoTracks[0].movie_timescale;
|
|
59
|
+
const framesToFill = segmentDurationInSeconds * this.edit.fps;
|
|
60
|
+
|
|
61
|
+
const height = this.file.getInfo().videoTracks[0].track_height;
|
|
62
|
+
const width = this.file.getInfo().videoTracks[0].track_width;
|
|
63
|
+
|
|
64
|
+
const bufferSize = height * width * 4;
|
|
65
|
+
const buffer = new ArrayBuffer(bufferSize);
|
|
66
|
+
const uint8Array = new Uint8Array(buffer);
|
|
67
|
+
|
|
68
|
+
// Make the frame black
|
|
69
|
+
uint8Array.fill(0);
|
|
70
|
+
|
|
71
|
+
this.frameBuffer = Array.from({length: framesToFill}, () => {
|
|
72
|
+
return new VideoFrame(uint8Array, {
|
|
73
|
+
timestamp: 0,
|
|
74
|
+
duration: 1 / this.edit.fps,
|
|
75
|
+
codedHeight: height,
|
|
76
|
+
codedWidth: width,
|
|
77
|
+
format: 'BGRA',
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
this.decoder.configure(decoderConfig);
|
|
85
|
+
const videoTrack = this.file.getInfo().videoTracks[0];
|
|
86
|
+
const trak = this.file.getTrackById(videoTrack.id);
|
|
87
|
+
|
|
88
|
+
const mediaTimeInTimescale = this.edit.mediaTime;
|
|
89
|
+
const startTimeInTimescale = this.startTime * videoTrack.timescale;
|
|
90
|
+
const seekTimeInTimescale = mediaTimeInTimescale + startTimeInTimescale;
|
|
91
|
+
|
|
92
|
+
const seekTimeInSec = seekTimeInTimescale / videoTrack.timescale;
|
|
93
|
+
const seekInfo = this.file.seekTrack(seekTimeInSec, true, trak);
|
|
94
|
+
this.readMore = await this.startStreamingAtOffset(
|
|
95
|
+
this.file,
|
|
96
|
+
this.uri,
|
|
97
|
+
seekInfo.offset,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Starts streaming the video at the given URI from the given offset.
|
|
103
|
+
* @param file - MP4Box file. Needs to be created and configured before calling this function.
|
|
104
|
+
* @param uri - URI of the video file.
|
|
105
|
+
* @param offset - Offset to start streaming from.
|
|
106
|
+
* @returns - A function to read more data from the response.
|
|
107
|
+
*/
|
|
108
|
+
private async startStreamingAtOffset(file: any, uri: string, offset: number) {
|
|
109
|
+
return fetch(uri, {
|
|
110
|
+
headers: {
|
|
111
|
+
/* eslint-disable-next-line @typescript-eslint/naming-convention */
|
|
112
|
+
Range: `bytes=${offset}-`,
|
|
113
|
+
},
|
|
114
|
+
signal: this.abortController.signal,
|
|
115
|
+
}).then(async response => {
|
|
116
|
+
if (!response.body) {
|
|
117
|
+
throw new Error('Response body is null');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const reader = response.body.getReader();
|
|
121
|
+
const sink = new MP4FileSink(file, () => {}, offset);
|
|
122
|
+
|
|
123
|
+
return async () => {
|
|
124
|
+
return reader.read().then(({done, value}) => {
|
|
125
|
+
// Request is done.
|
|
126
|
+
if (done) {
|
|
127
|
+
this.responseFinished = true;
|
|
128
|
+
this.abortController.abort();
|
|
129
|
+
this.decoder.flush();
|
|
130
|
+
sink.close();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
sink.write(value);
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Called when samples are available on the MP4 file.
|
|
142
|
+
* Sends chunks to the decoder.
|
|
143
|
+
*/
|
|
144
|
+
private onSamples(_unused1: any, _unused2: any, samples: any) {
|
|
145
|
+
for (const sample of samples) {
|
|
146
|
+
const chunk = new EncodedVideoChunk({
|
|
147
|
+
type: sample.is_sync ? 'key' : 'delta',
|
|
148
|
+
timestamp: (1e6 * sample.cts) / sample.timescale,
|
|
149
|
+
duration: (1e6 * sample.duration) / sample.timescale,
|
|
150
|
+
data: sample.data,
|
|
151
|
+
});
|
|
152
|
+
this.framesDue++;
|
|
153
|
+
this.decoder.decode(chunk);
|
|
154
|
+
|
|
155
|
+
const videoTrack = this.file.getInfo().videoTracks[0];
|
|
156
|
+
const trak = this.file.getTrackById(videoTrack.id);
|
|
157
|
+
this.file.releaseSample(trak, sample.number);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Called when the decoder has a frame ready.
|
|
163
|
+
* Pushes the frame to the buffer so it can be consumed.
|
|
164
|
+
*/
|
|
165
|
+
private onFrame(frame: VideoFrame) {
|
|
166
|
+
this.framesDue--;
|
|
167
|
+
|
|
168
|
+
// If the frame comes before the seek time, close it.
|
|
169
|
+
const mediaTimeInSec =
|
|
170
|
+
this.edit.mediaTime / this.file.getInfo().videoTracks[0].timescale;
|
|
171
|
+
const seekTimeInSec = this.startTime + mediaTimeInSec;
|
|
172
|
+
const frameTimeInSec = frame.timestamp / 1e6;
|
|
173
|
+
if (frameTimeInSec < seekTimeInSec) {
|
|
174
|
+
frame.close();
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Check if we are past the segment duration.
|
|
179
|
+
const segmentDurationInSec =
|
|
180
|
+
this.edit.segmentDuration /
|
|
181
|
+
this.file.getInfo().videoTracks[0].movie_timescale;
|
|
182
|
+
const segmentEndTime = mediaTimeInSec + segmentDurationInSec;
|
|
183
|
+
if (frameTimeInSec > segmentEndTime) {
|
|
184
|
+
frame.close();
|
|
185
|
+
this.done = true;
|
|
186
|
+
this.decoder.flush();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
this.frameBuffer.push(frame);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private async populateBuffer() {
|
|
194
|
+
// Fetch more frames if we don't have any.
|
|
195
|
+
while (this.frameBuffer.length === 0 && !this.responseFinished) {
|
|
196
|
+
await this.readMore();
|
|
197
|
+
await new Promise(res => setTimeout(res, 0));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Wait for decoder if there are frames due.
|
|
201
|
+
if (this.frameBuffer.length === 0 && this.framesDue > 0) {
|
|
202
|
+
let maxIterations = 1000;
|
|
203
|
+
while (this.frameBuffer.length === 0) {
|
|
204
|
+
await new Promise(res => setTimeout(res, 10));
|
|
205
|
+
maxIterations--;
|
|
206
|
+
|
|
207
|
+
if (this.done) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (maxIterations === 0) {
|
|
212
|
+
throw new Error(
|
|
213
|
+
`Timed out while waiting for VideoDecoder to produce a frame. Frames due: ${this.framesDue}`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
public async getNextFrame() {
|
|
221
|
+
await this.populateBuffer();
|
|
222
|
+
const frame = this.frameBuffer.shift();
|
|
223
|
+
if (frame) {
|
|
224
|
+
this.framesProcessed++;
|
|
225
|
+
}
|
|
226
|
+
return frame;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Called when we are done with the extractor.
|
|
231
|
+
*/
|
|
232
|
+
public async close() {
|
|
233
|
+
this.abortController.abort();
|
|
234
|
+
this.frameBuffer.forEach(frame => frame.close());
|
|
235
|
+
try {
|
|
236
|
+
if (this.decoder.state === 'configured') {
|
|
237
|
+
await this.decoder.flush();
|
|
238
|
+
this.decoder.close();
|
|
239
|
+
}
|
|
240
|
+
} catch (e) {
|
|
241
|
+
// Ignore
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
public getFramesProcessed() {
|
|
246
|
+
return this.framesProcessed;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
public getStartTime() {
|
|
250
|
+
return this.startTime;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import {SerializedVector2, SignalValue, SimpleSignal} from '@revideo/core';
|
|
2
|
-
import {DesiredLength} from '../partials';
|
|
3
|
-
import {Media, MediaProps} from './Media';
|
|
4
|
-
export interface HlsVideoProps extends MediaProps {
|
|
5
|
-
/**
|
|
6
|
-
* {@inheritDoc Video.alpha}
|
|
7
|
-
*/
|
|
8
|
-
alpha?: SignalValue<number>;
|
|
9
|
-
/**
|
|
10
|
-
* {@inheritDoc Video.smoothing}
|
|
11
|
-
*/
|
|
12
|
-
smoothing?: SignalValue<boolean>;
|
|
13
|
-
/**
|
|
14
|
-
* {@inheritDoc Video.png}
|
|
15
|
-
*/
|
|
16
|
-
png?: SignalValue<boolean>;
|
|
17
|
-
}
|
|
18
|
-
export declare class HlsVideo extends Media {
|
|
19
|
-
/**
|
|
20
|
-
* The alpha value of this video.
|
|
21
|
-
*
|
|
22
|
-
* @remarks
|
|
23
|
-
* Unlike opacity, the alpha value affects only the video itself, leaving the
|
|
24
|
-
* fill, stroke, and children intact.
|
|
25
|
-
*/
|
|
26
|
-
readonly alpha: SimpleSignal<number, this>;
|
|
27
|
-
/**
|
|
28
|
-
* Whether the video should be smoothed.
|
|
29
|
-
*
|
|
30
|
-
* @remarks
|
|
31
|
-
* When disabled, the video will be scaled using the nearest neighbor
|
|
32
|
-
* interpolation with no smoothing. The resulting video will appear pixelated.
|
|
33
|
-
*
|
|
34
|
-
* @defaultValue true
|
|
35
|
-
*/
|
|
36
|
-
readonly smoothing: SimpleSignal<boolean, this>;
|
|
37
|
-
/**
|
|
38
|
-
* Whether the video frames should be extracted as PNGs. Uses JPEGs when
|
|
39
|
-
* set to false.
|
|
40
|
-
*
|
|
41
|
-
* @remarks
|
|
42
|
-
* PNGs have better image quality and support transparency, but they make
|
|
43
|
-
* rendering slower.
|
|
44
|
-
*
|
|
45
|
-
* @defaultValue false
|
|
46
|
-
*/
|
|
47
|
-
readonly png: SimpleSignal<boolean, this>;
|
|
48
|
-
private static readonly pool;
|
|
49
|
-
constructor(props: HlsVideoProps);
|
|
50
|
-
protected desiredSize(): SerializedVector2<DesiredLength>;
|
|
51
|
-
protected mediaElement(): HTMLVideoElement;
|
|
52
|
-
protected seekedMedia(): HTMLVideoElement;
|
|
53
|
-
protected fastSeekedMedia(): HTMLVideoElement;
|
|
54
|
-
private video;
|
|
55
|
-
protected seekedVideo(): HTMLVideoElement;
|
|
56
|
-
protected fastSeekedVideo(): HTMLVideoElement;
|
|
57
|
-
protected lastFrame: HTMLImageElement | null;
|
|
58
|
-
protected seekFunction(): Promise<HTMLVideoElement>;
|
|
59
|
-
protected draw(context: CanvasRenderingContext2D): Promise<void>;
|
|
60
|
-
protected applyFlex(): void;
|
|
61
|
-
}
|
|
62
|
-
//# sourceMappingURL=HlsVideo.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"HlsVideo.d.ts","sourceRoot":"","sources":["../../src/lib/components/HlsVideo.ts"],"names":[],"mappings":"AAAA,OAAO,EAIH,iBAAiB,EACjB,WAAW,EACX,YAAY,EAEb,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAE1C,OAAO,EAAC,KAAK,EAAE,UAAU,EAAC,MAAM,SAAS,CAAC;AAG1C,MAAM,WAAW,aAAc,SAAQ,UAAU;IAC/C;;OAEG;IACH,KAAK,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC5B;;OAEG;IACH,SAAS,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;IACjC;;OAEG;IACH,GAAG,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;CAC5B;AAED,qBACa,QAAS,SAAQ,KAAK;IACjC;;;;;;OAMG;IACH,SAEwB,KAAK,EAAE,YAAY,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAE1D;;;;;;;;OAQG;IACH,SAEwB,SAAS,EAAE,YAAY,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAE/D;;;;;;;;;OASG;IACH,SAEwB,GAAG,EAAE,YAAY,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAEzD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAwC;gBAEjD,KAAK,EAAE,aAAa;cAIpB,WAAW,IAAI,iBAAiB,CAAC,aAAa,CAAC;IAalE,SAAS,CAAC,YAAY,IAAI,gBAAgB;IAI1C,SAAS,CAAC,WAAW,IAAI,gBAAgB;IAIzC,SAAS,CAAC,eAAe,IAAI,gBAAgB;IAK7C,OAAO,CAAC,KAAK;IAsCb,SAAS,CAAC,WAAW,IAAI,gBAAgB;IAoBzC,SAAS,CAAC,eAAe,IAAI,gBAAgB;IA+B7C,SAAS,CAAC,SAAS,EAAE,gBAAgB,GAAG,IAAI,CAAQ;cAGpC,YAAY;cAgBH,IAAI,CAAC,OAAO,EAAE,wBAAwB;cAwB5C,SAAS;CAO7B"}
|