Add StreamSource

This commit is contained in:
Vanilagy
2025-02-09 17:34:59 +01:00
parent 6cdaa06c07
commit 9f6d561772
4 changed files with 46 additions and 9 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ const input = new Metamuxer.Input({
source
});
console.log(await input.getMimeType())
console.log(await input.getMimeType());
let videoTrack = await input.getPrimaryVideoTrack();
let audioTrack = await input.getPrimaryAudioTrack();
+1 -1
View File
@@ -68,7 +68,7 @@ export {
} from './codec';
export { Target, BufferTarget, StreamTarget, StreamTargetChunk, StreamTargetOptions } from './target';
export { Rotation, TransformationMatrix, AnyIterable, setVideoFrameTiming } from './misc';
export { Source, BufferSource, BlobSource, UrlSource } from './source';
export { Source, BufferSource, StreamSource, StreamSourceOptions, BlobSource, UrlSource } from './source';
export {
InputFormat,
IsobmffInputFormat,
+44 -6
View File
@@ -32,16 +32,54 @@ export class BufferSource extends Source {
}
/** @internal */
override async _read(start: number, end: number) {
async _read(start: number, end: number) {
return this._bytes.subarray(start, end);
}
/** @internal */
override async _retrieveSize() {
async _retrieveSize() {
return this._bytes.byteLength;
}
}
/** @public */
export type StreamSourceOptions = {
read: (start: number, end: number) => Uint8Array | Promise<Uint8Array>;
getSize: () => number | Promise<number>;
};
/** @public */
export class StreamSource extends Source {
/** @internal */
_options: StreamSourceOptions;
constructor(options: StreamSourceOptions) {
if (!options || typeof options !== 'object') {
throw new TypeError('options must be an object.');
}
if (typeof options.read !== 'function') {
throw new TypeError('options.read must be a function.');
}
if (typeof options.getSize !== 'function') {
throw new TypeError('options.getSize must be a function.');
}
super();
this._options = options;
}
/** @internal */
async _read(start: number, end: number) {
return this._options.read(start, end);
}
/** @internal */
async _retrieveSize() {
return this._options.getSize();
}
}
/** @public */
export class BlobSource extends Source {
/** @internal */
@@ -58,14 +96,14 @@ export class BlobSource extends Source {
}
/** @internal */
override async _read(start: number, end: number) {
async _read(start: number, end: number) {
const slice = this._blob.slice(start, end);
const buffer = await slice.arrayBuffer();
return new Uint8Array(buffer);
}
/** @internal */
override async _retrieveSize() {
async _retrieveSize() {
return this._blob.size;
}
}
@@ -140,7 +178,7 @@ export class UrlSource extends Source {
}
/** @internal */
override async _read(start: number, end: number): Promise<Uint8Array> {
async _read(start: number, end: number): Promise<Uint8Array> {
if (this._fullData) {
return new Uint8Array(this._fullData, start, end - start);
}
@@ -158,7 +196,7 @@ export class UrlSource extends Source {
}
/** @internal */
override async _retrieveSize(): Promise<number> {
async _retrieveSize(): Promise<number> {
if (this._fullData) {
return this._fullData.byteLength;
}
-1
View File
@@ -1,3 +1,2 @@
- A stream source?? Or like a callback-driven source
- onHeader, etc callbacks for Matroska
- https://github.com/Vanilagy/mp4-muxer/issues/83 tell him it's possible now