@editframe/assets 0.7.0-beta.8 → 0.8.0-beta.2
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/dist/EncodedAsset.d.ts +102 -0
- package/dist/EncodedAsset.js +564 -0
- package/dist/MP4File.d.ts +31 -0
- package/dist/{packages/assets/src/Probe.d.ts → Probe.d.ts} +45 -41
- package/dist/{packages/assets/src/Probe.js → Probe.js} +54 -5
- package/dist/index.d.ts +9 -0
- package/dist/index.js +24 -0
- package/dist/{packages/assets/src/md5.d.ts → md5.d.ts} +1 -0
- package/dist/{packages/assets/src/md5.js → md5.js} +6 -0
- package/dist/memoize.d.ts +2 -0
- package/dist/memoize.js +14 -0
- package/dist/{packages/assets/src/mp4FileWritable.d.ts → mp4FileWritable.d.ts} +1 -1
- package/dist/tasks/cacheImage.d.ts +1 -0
- package/dist/tasks/findOrCreateCaptions.d.ts +2 -0
- package/dist/tasks/findOrCreateCaptions.js +30 -0
- package/dist/tasks/generateTrack.d.ts +5 -0
- package/dist/{packages/assets/src/tasks → tasks}/generateTrack.js +26 -23
- package/dist/tasks/generateTrackFragmentIndex.d.ts +4 -0
- package/dist/{packages/assets/src/tasks → tasks}/generateTrackFragmentIndex.js +12 -6
- package/package.json +14 -10
- package/src/tasks/cacheImage.ts +1 -1
- package/src/tasks/findOrCreateCaptions.ts +18 -11
- package/src/tasks/generateTrack.ts +36 -31
- package/src/tasks/generateTrackFragmentIndex.ts +16 -9
- package/dist/lib/av/MP4File.cjs +0 -187
- package/dist/lib/util/execPromise.cjs +0 -6
- package/dist/lib/util/execPromise.js +0 -6
- package/dist/packages/assets/src/Probe.cjs +0 -224
- package/dist/packages/assets/src/VideoRenderOptions.cjs +0 -36
- package/dist/packages/assets/src/idempotentTask.cjs +0 -57
- package/dist/packages/assets/src/index.cjs +0 -20
- package/dist/packages/assets/src/index.d.ts +0 -9
- package/dist/packages/assets/src/index.js +0 -20
- package/dist/packages/assets/src/md5.cjs +0 -60
- package/dist/packages/assets/src/mp4FileWritable.cjs +0 -21
- package/dist/packages/assets/src/tasks/cacheImage.cjs +0 -22
- package/dist/packages/assets/src/tasks/cacheImage.d.ts +0 -1
- package/dist/packages/assets/src/tasks/findOrCreateCaptions.cjs +0 -26
- package/dist/packages/assets/src/tasks/findOrCreateCaptions.d.ts +0 -1
- package/dist/packages/assets/src/tasks/findOrCreateCaptions.js +0 -26
- package/dist/packages/assets/src/tasks/generateTrack.cjs +0 -52
- package/dist/packages/assets/src/tasks/generateTrack.d.ts +0 -1
- package/dist/packages/assets/src/tasks/generateTrackFragmentIndex.cjs +0 -105
- package/dist/packages/assets/src/tasks/generateTrackFragmentIndex.d.ts +0 -1
- /package/dist/{lib/av/MP4File.js → MP4File.js} +0 -0
- /package/dist/{packages/assets/src/VideoRenderOptions.d.ts → VideoRenderOptions.d.ts} +0 -0
- /package/dist/{packages/assets/src/VideoRenderOptions.js → VideoRenderOptions.js} +0 -0
- /package/dist/{packages/assets/src/idempotentTask.d.ts → idempotentTask.d.ts} +0 -0
- /package/dist/{packages/assets/src/idempotentTask.js → idempotentTask.js} +0 -0
- /package/dist/{packages/assets/src/mp4FileWritable.js → mp4FileWritable.js} +0 -0
- /package/dist/{packages/assets/src/tasks → tasks}/cacheImage.js +0 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ReadableStream as ReadableStreamNode } from 'node:stream/web';
|
|
2
|
+
|
|
3
|
+
import * as MP4Box from "mp4box";
|
|
4
|
+
type AnyReadableStream = ReadableStream<Uint8Array> | ReadableStreamNode<Uint8Array>;
|
|
5
|
+
type FrameCallback = (frame: VideoFrame) => void;
|
|
6
|
+
export interface FetchContext {
|
|
7
|
+
fetchConfig: RequestInit;
|
|
8
|
+
origin: string;
|
|
9
|
+
}
|
|
10
|
+
export declare class AssetNotAvailableLocally extends Error {
|
|
11
|
+
}
|
|
12
|
+
export declare class FileAsset {
|
|
13
|
+
localName: string;
|
|
14
|
+
protected readonly file: File;
|
|
15
|
+
constructor(localName: string, file: File);
|
|
16
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
17
|
+
get byteSize(): number;
|
|
18
|
+
get fileExtension(): string | undefined;
|
|
19
|
+
slice(start: number, end: number): Blob;
|
|
20
|
+
}
|
|
21
|
+
export declare class ISOFileAsset extends FileAsset {
|
|
22
|
+
localName: string;
|
|
23
|
+
protected readonly file: File;
|
|
24
|
+
readonly mp4boxFile: MP4Box.ISOFile;
|
|
25
|
+
constructor(localName: string, file: File, mp4boxFile: MP4Box.ISOFile);
|
|
26
|
+
get fileInfo(): MP4Box.Info;
|
|
27
|
+
get containerFormat(): "mp4";
|
|
28
|
+
}
|
|
29
|
+
export declare class VideoAsset extends ISOFileAsset {
|
|
30
|
+
static createFromReadableStream(id: string, stream: AnyReadableStream, file: File): Promise<VideoAsset>;
|
|
31
|
+
videoDecoder: VideoDecoder;
|
|
32
|
+
lastDecodedSample?: MP4Box.Sample;
|
|
33
|
+
lastSoughtFrame?: VideoFrame;
|
|
34
|
+
decodedFrames: VideoFrame[];
|
|
35
|
+
requestedSampleNumber: number;
|
|
36
|
+
outCursor: number;
|
|
37
|
+
sampleCursor: number;
|
|
38
|
+
/**
|
|
39
|
+
* **Only use this function in tests to reset a VideoAsset to its initial state.**
|
|
40
|
+
*
|
|
41
|
+
* @deprecated
|
|
42
|
+
*/
|
|
43
|
+
TEST_ONLY_RESET(): Promise<void>;
|
|
44
|
+
private readonly eventListeners;
|
|
45
|
+
addEventListener(type: "frame", callback: FrameCallback): void;
|
|
46
|
+
removeEventListener(type: "frame", callback: FrameCallback): void;
|
|
47
|
+
emit(type: "frame", frame: VideoFrame): void;
|
|
48
|
+
constructor(localName: string, mp4boxFile: MP4Box.ISOFile, file: File);
|
|
49
|
+
get videoCodec(): string;
|
|
50
|
+
get fragmentInfo(): {
|
|
51
|
+
offset: number;
|
|
52
|
+
size: number;
|
|
53
|
+
start_ms: number;
|
|
54
|
+
duration_ms: number;
|
|
55
|
+
}[];
|
|
56
|
+
pruneBuffer(): void;
|
|
57
|
+
get editsOffset(): number;
|
|
58
|
+
waitUntilVideoQueueDrained(): Promise<void>;
|
|
59
|
+
get canDecodeNextSample(): boolean;
|
|
60
|
+
decodeNextSample(): Promise<void>;
|
|
61
|
+
decodeSlice(start: number, end: number): Promise<void>;
|
|
62
|
+
get decoderConfiguration(): {
|
|
63
|
+
codec: string;
|
|
64
|
+
codedWidth: number;
|
|
65
|
+
codedHeight: number;
|
|
66
|
+
optimizeForLatency: true;
|
|
67
|
+
description: Uint8Array;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Configures the video decoder with the appropriate codec, dimensions, and hardware acceleration settings.
|
|
71
|
+
* If the decoder is already configured, it will be reset before being reconfigured.
|
|
72
|
+
*/
|
|
73
|
+
configureDecoder(): void;
|
|
74
|
+
getSample(index?: number): MP4Box.Sample;
|
|
75
|
+
get timescale(): number;
|
|
76
|
+
get samples(): MP4Box.Sample[];
|
|
77
|
+
get displayOrderedSamples(): MP4Box.Sample[];
|
|
78
|
+
getSampleClosetToTime(seconds: number): MP4Box.Sample;
|
|
79
|
+
seekingWillEmitNewFrame(seconds: number): boolean;
|
|
80
|
+
seekingWillSkipPictureGroup(seconds: number): boolean;
|
|
81
|
+
seekingWillGoBackwards(seconds: number): boolean;
|
|
82
|
+
latestSeekCts: number;
|
|
83
|
+
seekToTime(seconds: number): Promise<VideoFrame | undefined>;
|
|
84
|
+
get defaultVideoTrack(): MP4Box.VideoTrackInfo | undefined;
|
|
85
|
+
get defaultVideoTrak(): MP4Box.TrakBox;
|
|
86
|
+
get duration(): number;
|
|
87
|
+
}
|
|
88
|
+
export declare class AudioAsset extends ISOFileAsset {
|
|
89
|
+
static createFromReadableStream(id: string, stream: AnyReadableStream, file: File): Promise<AudioAsset>;
|
|
90
|
+
get defaultAudioTrack(): MP4Box.AudioTrackInfo | undefined;
|
|
91
|
+
get defaultAudioTrak(): MP4Box.TrakBox;
|
|
92
|
+
get audioCodec(): string;
|
|
93
|
+
get samplerate(): number;
|
|
94
|
+
get channelCount(): number;
|
|
95
|
+
}
|
|
96
|
+
export declare class ImageAsset extends FileAsset {
|
|
97
|
+
static createFromReadableStream(id: string, file: File): Promise<ImageAsset>;
|
|
98
|
+
get objectUrl(): string;
|
|
99
|
+
get format(): string | undefined;
|
|
100
|
+
get type(): string;
|
|
101
|
+
}
|
|
102
|
+
export {};
|
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
import * as MP4Box from "mp4box";
|
|
2
|
+
import debug from "debug";
|
|
3
|
+
import { memoize } from "./memoize.js";
|
|
4
|
+
import { MP4File } from "./MP4File.js";
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
8
|
+
var result = __getOwnPropDesc(target, key);
|
|
9
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
10
|
+
if (decorator = decorators[i])
|
|
11
|
+
result = decorator(target, key, result) || result;
|
|
12
|
+
if (result) __defProp(target, key, result);
|
|
13
|
+
return result;
|
|
14
|
+
};
|
|
15
|
+
const log = debug("ef:av");
|
|
16
|
+
const BUFFER_SIZE = 10;
|
|
17
|
+
class AssetNotAvailableLocally extends Error {
|
|
18
|
+
}
|
|
19
|
+
class FileAsset {
|
|
20
|
+
constructor(localName, file) {
|
|
21
|
+
this.localName = localName;
|
|
22
|
+
this.file = file;
|
|
23
|
+
}
|
|
24
|
+
async arrayBuffer() {
|
|
25
|
+
return this.file.arrayBuffer();
|
|
26
|
+
}
|
|
27
|
+
get byteSize() {
|
|
28
|
+
return this.file.size;
|
|
29
|
+
}
|
|
30
|
+
get fileExtension() {
|
|
31
|
+
return this.file.name.split(".").pop();
|
|
32
|
+
}
|
|
33
|
+
slice(start, end) {
|
|
34
|
+
return this.file.slice(start, end);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
class ISOFileAsset extends FileAsset {
|
|
38
|
+
constructor(localName, file, mp4boxFile) {
|
|
39
|
+
super(localName, file);
|
|
40
|
+
this.localName = localName;
|
|
41
|
+
this.file = file;
|
|
42
|
+
this.mp4boxFile = mp4boxFile;
|
|
43
|
+
}
|
|
44
|
+
get fileInfo() {
|
|
45
|
+
return this.mp4boxFile.getInfo();
|
|
46
|
+
}
|
|
47
|
+
get containerFormat() {
|
|
48
|
+
return "mp4";
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
__decorateClass([
|
|
52
|
+
memoize
|
|
53
|
+
], ISOFileAsset.prototype, "fileInfo");
|
|
54
|
+
const _VideoAsset = class _VideoAsset2 extends ISOFileAsset {
|
|
55
|
+
constructor(localName, mp4boxFile, file) {
|
|
56
|
+
super(localName, file, mp4boxFile);
|
|
57
|
+
this.decodedFrames = [];
|
|
58
|
+
this.requestedSampleNumber = 0;
|
|
59
|
+
this.outCursor = 0;
|
|
60
|
+
this.sampleCursor = 0;
|
|
61
|
+
this.eventListeners = {};
|
|
62
|
+
this.latestSeekCts = 0;
|
|
63
|
+
this.videoDecoder = new VideoDecoder({
|
|
64
|
+
error: (e) => {
|
|
65
|
+
console.error("Video Decoder Error", e);
|
|
66
|
+
},
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
68
|
+
output: async (decodedFrame) => {
|
|
69
|
+
const clone = decodedFrame.clone();
|
|
70
|
+
log("🖼️ frame cts=", decodedFrame.timestamp);
|
|
71
|
+
this.decodedFrames.push(clone);
|
|
72
|
+
this.pruneBuffer();
|
|
73
|
+
decodedFrame.close();
|
|
74
|
+
this.outCursor = this.samples.findIndex(
|
|
75
|
+
(sample) => sample.cts === decodedFrame.timestamp
|
|
76
|
+
);
|
|
77
|
+
log(`cursor=${this.sampleCursor} outCursor=${this.outCursor}`);
|
|
78
|
+
this.emit("frame", clone);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
this.configureDecoder();
|
|
82
|
+
}
|
|
83
|
+
static async createFromReadableStream(id, stream, file) {
|
|
84
|
+
let fileStart = 0;
|
|
85
|
+
const inputFile = new MP4File();
|
|
86
|
+
const reader = stream.getReader();
|
|
87
|
+
const processChunk = ({
|
|
88
|
+
done,
|
|
89
|
+
value
|
|
90
|
+
}) => {
|
|
91
|
+
if (done) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (!value) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const mp4buffer = value.buffer;
|
|
98
|
+
mp4buffer.fileStart = fileStart;
|
|
99
|
+
const isLast = file.size === fileStart + value.byteLength;
|
|
100
|
+
inputFile.appendBuffer(mp4buffer, isLast);
|
|
101
|
+
fileStart += value.byteLength;
|
|
102
|
+
return reader.read().then(processChunk);
|
|
103
|
+
};
|
|
104
|
+
await reader.read().then(processChunk);
|
|
105
|
+
return new _VideoAsset2(id, inputFile, file);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* **Only use this function in tests to reset a VideoAsset to its initial state.**
|
|
109
|
+
*
|
|
110
|
+
* @deprecated
|
|
111
|
+
*/
|
|
112
|
+
async TEST_ONLY_RESET() {
|
|
113
|
+
await this.videoDecoder.flush();
|
|
114
|
+
this.configureDecoder();
|
|
115
|
+
this.requestedSampleNumber = 0;
|
|
116
|
+
this.outCursor = 0;
|
|
117
|
+
this.sampleCursor = 0;
|
|
118
|
+
for (const frame of this.decodedFrames) {
|
|
119
|
+
frame.close();
|
|
120
|
+
}
|
|
121
|
+
this.decodedFrames = [];
|
|
122
|
+
this.lastDecodedSample = void 0;
|
|
123
|
+
this.lastSoughtFrame?.close();
|
|
124
|
+
this.lastSoughtFrame = void 0;
|
|
125
|
+
}
|
|
126
|
+
addEventListener(type, callback) {
|
|
127
|
+
this.eventListeners[type] ||= /* @__PURE__ */ new Set();
|
|
128
|
+
this.eventListeners[type]?.add(callback);
|
|
129
|
+
}
|
|
130
|
+
removeEventListener(type, callback) {
|
|
131
|
+
this.eventListeners[type]?.delete(callback);
|
|
132
|
+
}
|
|
133
|
+
emit(type, ...args) {
|
|
134
|
+
for (const listener of this.eventListeners[type] ?? []) {
|
|
135
|
+
listener(...args);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
get videoCodec() {
|
|
139
|
+
if (!this.defaultVideoTrack) {
|
|
140
|
+
throw new Error("No default video track found");
|
|
141
|
+
}
|
|
142
|
+
return this.defaultVideoTrack?.codec;
|
|
143
|
+
}
|
|
144
|
+
get fragmentInfo() {
|
|
145
|
+
const fragments = [];
|
|
146
|
+
const [first, ...samples] = this.samples;
|
|
147
|
+
if (!first) {
|
|
148
|
+
return fragments;
|
|
149
|
+
}
|
|
150
|
+
let currentFragment = {
|
|
151
|
+
offset: first.offset,
|
|
152
|
+
size: first.size,
|
|
153
|
+
start_ms: first.cts,
|
|
154
|
+
duration_ms: 0
|
|
155
|
+
};
|
|
156
|
+
for (const sample of samples) {
|
|
157
|
+
if (sample.is_sync) {
|
|
158
|
+
if (currentFragment) {
|
|
159
|
+
currentFragment.duration_ms = sample.cts - currentFragment.start_ms;
|
|
160
|
+
fragments.push(currentFragment);
|
|
161
|
+
}
|
|
162
|
+
currentFragment = {
|
|
163
|
+
offset: sample.offset,
|
|
164
|
+
size: sample.size,
|
|
165
|
+
start_ms: sample.cts,
|
|
166
|
+
duration_ms: 0
|
|
167
|
+
};
|
|
168
|
+
} else {
|
|
169
|
+
currentFragment.size += sample.size;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return fragments;
|
|
173
|
+
}
|
|
174
|
+
pruneBuffer() {
|
|
175
|
+
if (this.decodedFrames.length > BUFFER_SIZE) {
|
|
176
|
+
this.decodedFrames.shift()?.close();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
get editsOffset() {
|
|
180
|
+
if (!this.defaultVideoTrack?.edits) {
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
return this.defaultVideoTrack.edits.reduce((acc, edit) => {
|
|
184
|
+
return acc + edit.media_time;
|
|
185
|
+
}, 0);
|
|
186
|
+
}
|
|
187
|
+
async waitUntilVideoQueueDrained() {
|
|
188
|
+
if (this.videoDecoder.decodeQueueSize === 0) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
await new Promise((resolve) => {
|
|
192
|
+
this.videoDecoder.addEventListener(
|
|
193
|
+
"dequeue",
|
|
194
|
+
() => {
|
|
195
|
+
resolve();
|
|
196
|
+
},
|
|
197
|
+
{ once: true }
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
await this.waitUntilVideoQueueDrained();
|
|
201
|
+
}
|
|
202
|
+
get canDecodeNextSample() {
|
|
203
|
+
return this.sampleCursor < this.samples.length;
|
|
204
|
+
}
|
|
205
|
+
async decodeNextSample() {
|
|
206
|
+
if (!this.canDecodeNextSample) {
|
|
207
|
+
throw new Error("No more samples to decode");
|
|
208
|
+
}
|
|
209
|
+
await this.decodeSlice(this.sampleCursor, this.sampleCursor);
|
|
210
|
+
this.sampleCursor++;
|
|
211
|
+
}
|
|
212
|
+
async decodeSlice(start, end) {
|
|
213
|
+
const samples = this.samples.slice(start, end + 1);
|
|
214
|
+
const firstSample = samples[0];
|
|
215
|
+
const lastSample = samples[samples.length - 1];
|
|
216
|
+
if (!firstSample || !lastSample) {
|
|
217
|
+
throw new Error("Samples not found");
|
|
218
|
+
}
|
|
219
|
+
const sliceStart = firstSample.offset;
|
|
220
|
+
const sliceEnd = lastSample.offset + lastSample.size;
|
|
221
|
+
const buffer = await this.file.slice(sliceStart, sliceEnd).arrayBuffer();
|
|
222
|
+
const firstSampleOffset = firstSample.offset;
|
|
223
|
+
for (let i = start; i <= end; i++) {
|
|
224
|
+
await this.waitUntilVideoQueueDrained();
|
|
225
|
+
const sample = this.getSample(i);
|
|
226
|
+
log("Decoding sample #", i, `cts=${sample.cts}`);
|
|
227
|
+
const sampleStart = sample.offset - firstSampleOffset;
|
|
228
|
+
const sampleEnd = sample.offset + sample.size - firstSampleOffset;
|
|
229
|
+
const chunk = new EncodedVideoChunk({
|
|
230
|
+
data: buffer.slice(sampleStart, sampleEnd),
|
|
231
|
+
timestamp: sample.cts,
|
|
232
|
+
duration: sample.duration,
|
|
233
|
+
type: sample.is_sync ? "key" : "delta"
|
|
234
|
+
});
|
|
235
|
+
this.videoDecoder.decode(chunk);
|
|
236
|
+
const nextSample = this.defaultVideoTrak?.samples?.[i + 1];
|
|
237
|
+
if (nextSample === void 0) {
|
|
238
|
+
log("ENDFLUSH");
|
|
239
|
+
await this.videoDecoder.flush();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
get decoderConfiguration() {
|
|
244
|
+
if (!this.defaultVideoTrack) {
|
|
245
|
+
throw new Error("No default video track found");
|
|
246
|
+
}
|
|
247
|
+
let description = new Uint8Array();
|
|
248
|
+
const trak = this.mp4boxFile.getTrackById(this.defaultVideoTrack.id);
|
|
249
|
+
for (const entry of trak.mdia.minf.stbl.stsd.entries) {
|
|
250
|
+
if (entry.avcC ?? entry.hvcC) {
|
|
251
|
+
const stream = new MP4Box.DataStream(
|
|
252
|
+
void 0,
|
|
253
|
+
0,
|
|
254
|
+
MP4Box.DataStream.BIG_ENDIAN
|
|
255
|
+
);
|
|
256
|
+
if (entry.avcC) {
|
|
257
|
+
entry.avcC.write(stream);
|
|
258
|
+
} else {
|
|
259
|
+
entry.hvcC.write(stream);
|
|
260
|
+
}
|
|
261
|
+
description = new Uint8Array(stream.buffer, 8);
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
codec: this.defaultVideoTrack.codec,
|
|
267
|
+
codedWidth: this.defaultVideoTrack.track_width,
|
|
268
|
+
codedHeight: this.defaultVideoTrack.track_height,
|
|
269
|
+
optimizeForLatency: true,
|
|
270
|
+
description
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Configures the video decoder with the appropriate codec, dimensions, and hardware acceleration settings.
|
|
275
|
+
* If the decoder is already configured, it will be reset before being reconfigured.
|
|
276
|
+
*/
|
|
277
|
+
configureDecoder() {
|
|
278
|
+
if (this.videoDecoder.state === "configured") {
|
|
279
|
+
this.videoDecoder.reset();
|
|
280
|
+
}
|
|
281
|
+
log("Attempting to configure decoder", this.decoderConfiguration);
|
|
282
|
+
this.videoDecoder.configure(this.decoderConfiguration);
|
|
283
|
+
}
|
|
284
|
+
// Default to -1 to throw error if called without an index
|
|
285
|
+
getSample(index = -1) {
|
|
286
|
+
const sample = this.samples?.[index];
|
|
287
|
+
if (!sample) {
|
|
288
|
+
throw new Error(`Sample not found at index ${index}`);
|
|
289
|
+
}
|
|
290
|
+
return sample;
|
|
291
|
+
}
|
|
292
|
+
get timescale() {
|
|
293
|
+
if (!this.defaultVideoTrack) {
|
|
294
|
+
throw new Error("No default video track found");
|
|
295
|
+
}
|
|
296
|
+
return this.defaultVideoTrack.timescale;
|
|
297
|
+
}
|
|
298
|
+
get samples() {
|
|
299
|
+
if (!this.defaultVideoTrak.samples) {
|
|
300
|
+
throw new Error("No video samples found");
|
|
301
|
+
}
|
|
302
|
+
return this.defaultVideoTrak.samples;
|
|
303
|
+
}
|
|
304
|
+
get displayOrderedSamples() {
|
|
305
|
+
return Array.from(this.samples).sort((a, b) => {
|
|
306
|
+
return a.cts - b.cts;
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
getSampleClosetToTime(seconds) {
|
|
310
|
+
const targetTime = Math.round(seconds * this.timescale + this.editsOffset);
|
|
311
|
+
const sampleIndex = this.displayOrderedSamples.findIndex(
|
|
312
|
+
(sample) => sample.cts >= targetTime
|
|
313
|
+
);
|
|
314
|
+
if (sampleIndex === -1) {
|
|
315
|
+
return this.displayOrderedSamples[this.displayOrderedSamples.length - 1];
|
|
316
|
+
}
|
|
317
|
+
return this.displayOrderedSamples[sampleIndex];
|
|
318
|
+
}
|
|
319
|
+
seekingWillEmitNewFrame(seconds) {
|
|
320
|
+
if (!this.lastSoughtFrame) {
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
if (this.seekingWillGoBackwards(seconds)) {
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
const nextCts = this.getSampleClosetToTime(seconds).cts;
|
|
327
|
+
return nextCts > this.lastSoughtFrame.timestamp;
|
|
328
|
+
}
|
|
329
|
+
seekingWillSkipPictureGroup(seconds) {
|
|
330
|
+
let start = this.sampleCursor;
|
|
331
|
+
const end = this.getSampleClosetToTime(seconds).number;
|
|
332
|
+
let syncFrameCrossings = 0;
|
|
333
|
+
while (start <= end) {
|
|
334
|
+
const sample = this.getSample(start);
|
|
335
|
+
if (sample.is_sync) {
|
|
336
|
+
if (syncFrameCrossings > 1) {
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
syncFrameCrossings++;
|
|
340
|
+
}
|
|
341
|
+
start++;
|
|
342
|
+
}
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
seekingWillGoBackwards(seconds) {
|
|
346
|
+
const targetSample = this.getSampleClosetToTime(seconds);
|
|
347
|
+
const targetIndex = this.displayOrderedSamples.indexOf(targetSample);
|
|
348
|
+
const targetInCache = this.decodedFrames.find(
|
|
349
|
+
(frame) => frame.timestamp === targetSample.cts
|
|
350
|
+
);
|
|
351
|
+
const atEnd = this.sampleCursor === this.samples.length - 1;
|
|
352
|
+
log(
|
|
353
|
+
"this.outCursor <= targetSample.number",
|
|
354
|
+
this.outCursor <= targetSample.number
|
|
355
|
+
);
|
|
356
|
+
log("this.outCursor <= targetIndex", this.outCursor <= targetIndex);
|
|
357
|
+
if (atEnd) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
if (targetInCache) {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
log("========");
|
|
364
|
+
log("sampleCursor", this.sampleCursor);
|
|
365
|
+
log(" outCursor", this.outCursor);
|
|
366
|
+
log(" target", targetSample.number);
|
|
367
|
+
log(" targetIndex", targetIndex);
|
|
368
|
+
log(" inCache", !!targetInCache);
|
|
369
|
+
log(" atEnd", atEnd);
|
|
370
|
+
return this.outCursor > targetIndex;
|
|
371
|
+
}
|
|
372
|
+
async seekToTime(seconds) {
|
|
373
|
+
const sample = this.getSampleClosetToTime(seconds);
|
|
374
|
+
const cts = sample.cts;
|
|
375
|
+
this.latestSeekCts = cts;
|
|
376
|
+
const alreadyDecodedFrame = this.decodedFrames.find(
|
|
377
|
+
(f) => f.timestamp === cts
|
|
378
|
+
);
|
|
379
|
+
if (alreadyDecodedFrame) {
|
|
380
|
+
return alreadyDecodedFrame;
|
|
381
|
+
}
|
|
382
|
+
if (this.seekingWillSkipPictureGroup(seconds)) {
|
|
383
|
+
await this.videoDecoder.flush();
|
|
384
|
+
let syncSampleNumber = sample.number;
|
|
385
|
+
while (!this.getSample(syncSampleNumber).is_sync) {
|
|
386
|
+
syncSampleNumber--;
|
|
387
|
+
if (syncSampleNumber < 0) {
|
|
388
|
+
throw new Error("No sync sample found when traversing backwards");
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
this.sampleCursor = syncSampleNumber;
|
|
392
|
+
}
|
|
393
|
+
if (this.seekingWillGoBackwards(seconds)) {
|
|
394
|
+
log("BACKWARDS FLUSH");
|
|
395
|
+
await this.videoDecoder.flush();
|
|
396
|
+
for (const frame2 of this.decodedFrames) {
|
|
397
|
+
frame2.close();
|
|
398
|
+
}
|
|
399
|
+
this.decodedFrames = [];
|
|
400
|
+
let syncSampleNumber = sample.number;
|
|
401
|
+
while (!this.getSample(syncSampleNumber).is_sync) {
|
|
402
|
+
syncSampleNumber--;
|
|
403
|
+
if (syncSampleNumber < 0) {
|
|
404
|
+
throw new Error("No sync sample found when traversing backwards");
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
this.sampleCursor = syncSampleNumber;
|
|
408
|
+
}
|
|
409
|
+
let frame;
|
|
410
|
+
const maybeFrame = (_frame) => {
|
|
411
|
+
if (frame) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
log("Maybe frame", _frame.timestamp, cts);
|
|
415
|
+
if (_frame.timestamp === cts) {
|
|
416
|
+
this.removeEventListener("frame", maybeFrame);
|
|
417
|
+
frame = _frame;
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
this.addEventListener("frame", maybeFrame);
|
|
421
|
+
while (frame === void 0 && this.canDecodeNextSample) {
|
|
422
|
+
await this.decodeNextSample();
|
|
423
|
+
}
|
|
424
|
+
this.removeEventListener("frame", maybeFrame);
|
|
425
|
+
if (frame) {
|
|
426
|
+
if (this.lastSoughtFrame && !this.decodedFrames.includes(this.lastSoughtFrame)) {
|
|
427
|
+
try {
|
|
428
|
+
this.lastSoughtFrame.close();
|
|
429
|
+
} catch (error) {
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
this.lastSoughtFrame = frame;
|
|
433
|
+
}
|
|
434
|
+
return frame;
|
|
435
|
+
}
|
|
436
|
+
get defaultVideoTrack() {
|
|
437
|
+
return this.fileInfo.videoTracks[0];
|
|
438
|
+
}
|
|
439
|
+
get defaultVideoTrak() {
|
|
440
|
+
return this.mp4boxFile.getTrackById(this.defaultVideoTrack?.id ?? -1);
|
|
441
|
+
}
|
|
442
|
+
get duration() {
|
|
443
|
+
return this.fileInfo.duration / this.fileInfo.timescale;
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
__decorateClass([
|
|
447
|
+
memoize
|
|
448
|
+
], _VideoAsset.prototype, "editsOffset");
|
|
449
|
+
__decorateClass([
|
|
450
|
+
memoize
|
|
451
|
+
], _VideoAsset.prototype, "timescale");
|
|
452
|
+
__decorateClass([
|
|
453
|
+
memoize
|
|
454
|
+
], _VideoAsset.prototype, "samples");
|
|
455
|
+
__decorateClass([
|
|
456
|
+
memoize
|
|
457
|
+
], _VideoAsset.prototype, "displayOrderedSamples");
|
|
458
|
+
__decorateClass([
|
|
459
|
+
memoize
|
|
460
|
+
], _VideoAsset.prototype, "defaultVideoTrack");
|
|
461
|
+
__decorateClass([
|
|
462
|
+
memoize
|
|
463
|
+
], _VideoAsset.prototype, "defaultVideoTrak");
|
|
464
|
+
__decorateClass([
|
|
465
|
+
memoize
|
|
466
|
+
], _VideoAsset.prototype, "duration");
|
|
467
|
+
let VideoAsset = _VideoAsset;
|
|
468
|
+
const _AudioAsset = class _AudioAsset2 extends ISOFileAsset {
|
|
469
|
+
static async createFromReadableStream(id, stream, file) {
|
|
470
|
+
let fileStart = 0;
|
|
471
|
+
const inputFile = new MP4File();
|
|
472
|
+
const reader = stream.getReader();
|
|
473
|
+
const processChunk = ({
|
|
474
|
+
done,
|
|
475
|
+
value
|
|
476
|
+
}) => {
|
|
477
|
+
if (done) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (!value) {
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
const mp4buffer = value.buffer;
|
|
484
|
+
mp4buffer.fileStart = fileStart;
|
|
485
|
+
fileStart += value.byteLength;
|
|
486
|
+
inputFile.appendBuffer(mp4buffer);
|
|
487
|
+
return reader.read().then(processChunk);
|
|
488
|
+
};
|
|
489
|
+
await reader.read().then(processChunk);
|
|
490
|
+
return new _AudioAsset2(id, file, inputFile);
|
|
491
|
+
}
|
|
492
|
+
get defaultAudioTrack() {
|
|
493
|
+
return this.fileInfo.audioTracks[0];
|
|
494
|
+
}
|
|
495
|
+
get defaultAudioTrak() {
|
|
496
|
+
return this.mp4boxFile.getTrackById(this.defaultAudioTrack?.id ?? -1);
|
|
497
|
+
}
|
|
498
|
+
get audioCodec() {
|
|
499
|
+
if (!this.defaultAudioTrack) {
|
|
500
|
+
throw new Error("No default audio track found");
|
|
501
|
+
}
|
|
502
|
+
return this.defaultAudioTrack.codec;
|
|
503
|
+
}
|
|
504
|
+
get samplerate() {
|
|
505
|
+
if (!this.defaultAudioTrack) {
|
|
506
|
+
throw new Error("No default audio track found");
|
|
507
|
+
}
|
|
508
|
+
return this.defaultAudioTrack.audio.sample_rate;
|
|
509
|
+
}
|
|
510
|
+
get channelCount() {
|
|
511
|
+
if (!this.defaultAudioTrack) {
|
|
512
|
+
throw new Error("No default audio track found");
|
|
513
|
+
}
|
|
514
|
+
return this.defaultAudioTrack.audio.channel_count;
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
__decorateClass([
|
|
518
|
+
memoize
|
|
519
|
+
], _AudioAsset.prototype, "defaultAudioTrack");
|
|
520
|
+
__decorateClass([
|
|
521
|
+
memoize
|
|
522
|
+
], _AudioAsset.prototype, "defaultAudioTrak");
|
|
523
|
+
__decorateClass([
|
|
524
|
+
memoize
|
|
525
|
+
], _AudioAsset.prototype, "audioCodec");
|
|
526
|
+
__decorateClass([
|
|
527
|
+
memoize
|
|
528
|
+
], _AudioAsset.prototype, "samplerate");
|
|
529
|
+
__decorateClass([
|
|
530
|
+
memoize
|
|
531
|
+
], _AudioAsset.prototype, "channelCount");
|
|
532
|
+
let AudioAsset = _AudioAsset;
|
|
533
|
+
const _ImageAsset = class _ImageAsset2 extends FileAsset {
|
|
534
|
+
static async createFromReadableStream(id, file) {
|
|
535
|
+
if (file.size === 0) {
|
|
536
|
+
throw new AssetNotAvailableLocally();
|
|
537
|
+
}
|
|
538
|
+
return new _ImageAsset2(id, file);
|
|
539
|
+
}
|
|
540
|
+
get objectUrl() {
|
|
541
|
+
return URL.createObjectURL(this.file);
|
|
542
|
+
}
|
|
543
|
+
get format() {
|
|
544
|
+
return this.fileExtension;
|
|
545
|
+
}
|
|
546
|
+
get type() {
|
|
547
|
+
return `image/${this.format}`;
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
__decorateClass([
|
|
551
|
+
memoize
|
|
552
|
+
], _ImageAsset.prototype, "objectUrl");
|
|
553
|
+
__decorateClass([
|
|
554
|
+
memoize
|
|
555
|
+
], _ImageAsset.prototype, "format");
|
|
556
|
+
let ImageAsset = _ImageAsset;
|
|
557
|
+
export {
|
|
558
|
+
AssetNotAvailableLocally,
|
|
559
|
+
AudioAsset,
|
|
560
|
+
FileAsset,
|
|
561
|
+
ISOFileAsset,
|
|
562
|
+
ImageAsset,
|
|
563
|
+
VideoAsset
|
|
564
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as MP4Box from "mp4box";
|
|
2
|
+
export declare class MP4File extends MP4Box.ISOFile {
|
|
3
|
+
readyPromise: Promise<void>;
|
|
4
|
+
setSegmentOptions(id: number, user: any, options: MP4Box.SegmentOptions): void;
|
|
5
|
+
/**
|
|
6
|
+
* Fragments all tracks in a file into separate array buffers.
|
|
7
|
+
*/
|
|
8
|
+
fragmentAllTracks(): Promise<Record<number, ArrayBuffer[]>>;
|
|
9
|
+
fragmentIterator(): AsyncGenerator<{
|
|
10
|
+
track: number;
|
|
11
|
+
segment: "init";
|
|
12
|
+
data: ArrayBuffer;
|
|
13
|
+
complete: false;
|
|
14
|
+
cts?: undefined;
|
|
15
|
+
dts?: undefined;
|
|
16
|
+
duration?: undefined;
|
|
17
|
+
} | {
|
|
18
|
+
track: number;
|
|
19
|
+
segment: number;
|
|
20
|
+
data: ArrayBuffer;
|
|
21
|
+
complete: boolean;
|
|
22
|
+
cts: number;
|
|
23
|
+
dts: number;
|
|
24
|
+
duration: number;
|
|
25
|
+
}, void, unknown>;
|
|
26
|
+
waitingForSamples: Array<(last: boolean) => void>;
|
|
27
|
+
_hasSeenLastSamples: boolean;
|
|
28
|
+
waitForMoreSamples(): Promise<boolean>;
|
|
29
|
+
processSamples(last: boolean): void;
|
|
30
|
+
_arrayBufferFileStart: number;
|
|
31
|
+
}
|