diff --git a/build/webm-muxer.d.ts b/build/webm-muxer.d.ts index bd126b9..5c53818 100644 --- a/build/webm-muxer.d.ts +++ b/build/webm-muxer.d.ts @@ -89,10 +89,13 @@ declare interface MuxerOptions { firstTimestampBehavior?: 'strict' | 'offset' | 'permissive' } -declare type Target = ArrayBufferTarget | StreamTarget | FileSystemWritableFileStreamTarget; +declare const isTarget: unique symbol; +declare abstract class Target { + [isTarget]: true; +} /** The file data will be written into a single large buffer, which is then stored in `buffer` upon finalization. */ -declare class ArrayBufferTarget { +declare class ArrayBufferTarget extends Target { buffer: ArrayBuffer; } @@ -104,7 +107,7 @@ declare class ArrayBufferTarget { * once it has reached sufficient size, using a default chunk size of 16 MiB. This is useful for reducing the total * amount of writes, at the cost of latency. */ -declare class StreamTarget { +declare class StreamTarget extends Target { constructor(options: { onData?: (data: Uint8Array, position: number) => void, onHeader?: (data: Uint8Array, position: number) => void, @@ -119,7 +122,7 @@ declare class StreamTarget { * library with the File System Access API. Writing the file directly to disk as it's being created comes with many * benefits, such as creating files way larger than the available RAM. */ -declare class FileSystemWritableFileStreamTarget { +declare class FileSystemWritableFileStreamTarget extends Target { constructor( stream: FileSystemWritableFileStream, options?: { chunkSize?: number } @@ -258,14 +261,14 @@ declare global { export { Muxer, + MuxerOptions, ArrayBufferTarget, StreamTarget, FileSystemWritableFileStreamTarget, SubtitleEncoder, - type MuxerOptions, - type EncodedSubtitleChunk, - type EncodedSubtitleChunkMetadata, - type SubtitleEncoderOptions, - type SubtitleEncoderConfig + EncodedSubtitleChunk, + EncodedSubtitleChunkMetadata, + SubtitleEncoderOptions, + SubtitleEncoderConfig }; export as namespace WebMMuxer; \ No newline at end of file diff --git a/build/webm-muxer.js b/build/webm-muxer.js index a250e9b..f6a55c1 100644 --- a/build/webm-muxer.js +++ b/build/webm-muxer.js @@ -3,22 +3,7 @@ var WebMMuxer = (() => { var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; - var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; - var __propIsEnum = Object.prototype.propertyIsEnumerable; - var __pow = Math.pow; - var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; - var __spreadValues = (a, b) => { - for (var prop in b ||= {}) - if (__hasOwnProp.call(b, prop)) - __defNormalProp(a, prop, b[prop]); - if (__getOwnPropSymbols) - for (var prop of __getOwnPropSymbols(b)) { - if (__propIsEnum.call(b, prop)) - __defNormalProp(a, prop, b[prop]); - } - return a; - }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); @@ -83,9 +68,9 @@ var WebMMuxer = (() => { return 2; } else if (value < 1 << 24) { return 3; - } else if (value < __pow(2, 32)) { + } else if (value < 2 ** 32) { return 4; - } else if (value < __pow(2, 40)) { + } else if (value < 2 ** 40) { return 5; } else { return 6; @@ -100,9 +85,9 @@ var WebMMuxer = (() => { return 3; } else if (value < (1 << 28) - 1) { return 4; - } else if (value < __pow(2, 35) - 1) { + } else if (value < 2 ** 35 - 1) { return 5; - } else if (value < __pow(2, 42) - 1) { + } else if (value < 2 ** 42 - 1) { return 6; } else { throw new Error("EBML VINT size not supported " + value); @@ -134,13 +119,19 @@ var WebMMuxer = (() => { }; // src/target.ts - var ArrayBufferTarget = class { + var isTarget = Symbol("isTarget"); + var Target = class { + }; + isTarget; + var ArrayBufferTarget = class extends Target { constructor() { + super(...arguments); this.buffer = null; } }; - var StreamTarget = class { + var StreamTarget = class extends Target { constructor(options) { + super(); this.options = options; if (typeof options !== "object") { throw new TypeError("StreamTarget requires an options object to be passed to its constructor."); @@ -169,8 +160,9 @@ var WebMMuxer = (() => { } } }; - var FileSystemWritableFileStreamTarget = class { + var FileSystemWritableFileStreamTarget = class extends Target { constructor(stream, options) { + super(); this.stream = stream; this.options = options; if (!(stream instanceof FileSystemWritableFileStream)) { @@ -227,15 +219,15 @@ var WebMMuxer = (() => { __privateGet(this, _helperView).setUint8(pos++, value); break; case 5: - __privateGet(this, _helperView).setUint8(pos++, 1 << 3 | value / __pow(2, 32) & 7); + __privateGet(this, _helperView).setUint8(pos++, 1 << 3 | value / 2 ** 32 & 7); __privateGet(this, _helperView).setUint8(pos++, value >> 24); __privateGet(this, _helperView).setUint8(pos++, value >> 16); __privateGet(this, _helperView).setUint8(pos++, value >> 8); __privateGet(this, _helperView).setUint8(pos++, value); break; case 6: - __privateGet(this, _helperView).setUint8(pos++, 1 << 2 | value / __pow(2, 40) & 3); - __privateGet(this, _helperView).setUint8(pos++, value / __pow(2, 32) | 0); + __privateGet(this, _helperView).setUint8(pos++, 1 << 2 | value / 2 ** 40 & 3); + __privateGet(this, _helperView).setUint8(pos++, value / 2 ** 32 | 0); __privateGet(this, _helperView).setUint8(pos++, value >> 24); __privateGet(this, _helperView).setUint8(pos++, value >> 16); __privateGet(this, _helperView).setUint8(pos++, value >> 8); @@ -247,7 +239,6 @@ var WebMMuxer = (() => { this.write(__privateGet(this, _helper).subarray(0, pos)); } writeEBML(data) { - var _a, _b; if (data === null) return; if (data instanceof Uint8Array) { @@ -261,7 +252,7 @@ var WebMMuxer = (() => { __privateMethod(this, _writeUnsignedInt, writeUnsignedInt_fn).call(this, data.id); if (Array.isArray(data.data)) { let sizePos = this.pos; - let sizeSize = data.size === -1 ? 1 : (_a = data.size) != null ? _a : 4; + let sizeSize = data.size === -1 ? 1 : data.size ?? 4; if (data.size === -1) { __privateMethod(this, _writeByte, writeByte_fn).call(this, 255); } else { @@ -278,7 +269,7 @@ var WebMMuxer = (() => { this.seek(endPos); } } else if (typeof data.data === "number") { - let size = (_b = data.size) != null ? _b : measureUnsignedInt(data.data); + let size = data.size ?? measureUnsignedInt(data.data); this.writeEBMLVarInt(size); __privateMethod(this, _writeUnsignedInt, writeUnsignedInt_fn).call(this, data.data, size); } else if (typeof data.data === "string") { @@ -319,9 +310,9 @@ var WebMMuxer = (() => { let pos = 0; switch (width) { case 6: - __privateGet(this, _helperView).setUint8(pos++, value / __pow(2, 40) | 0); + __privateGet(this, _helperView).setUint8(pos++, value / 2 ** 40 | 0); case 5: - __privateGet(this, _helperView).setUint8(pos++, value / __pow(2, 32) | 0); + __privateGet(this, _helperView).setUint8(pos++, value / 2 ** 32 | 0); case 4: __privateGet(this, _helperView).setUint8(pos++, value >> 24); case 3: @@ -346,7 +337,7 @@ var WebMMuxer = (() => { super(); __privateAdd(this, _ensureSize); __privateAdd(this, _target, void 0); - __privateAdd(this, _buffer, new ArrayBuffer(__pow(2, 16))); + __privateAdd(this, _buffer, new ArrayBuffer(2 ** 16)); __privateAdd(this, _bytes, new Uint8Array(__privateGet(this, _buffer))); __privateSet(this, _target, target); } @@ -410,7 +401,7 @@ var WebMMuxer = (() => { } startTrackingWrites() { __privateSet(this, _trackingWrites, true); - __privateSet(this, _trackedWrites, new Uint8Array(__pow(2, 10))); + __privateSet(this, _trackedWrites, new Uint8Array(2 ** 10)); __privateSet(this, _trackedStart, this.pos); __privateSet(this, _trackedEnd, this.pos); } @@ -451,7 +442,6 @@ var WebMMuxer = (() => { this.pos += data.byteLength; } flush() { - var _a, _b; if (__privateGet(this, _sections).length === 0) return; let chunks = []; @@ -482,7 +472,7 @@ var WebMMuxer = (() => { if (__privateGet(this, _ensureMonotonicity) && chunk.start < __privateGet(this, _lastFlushEnd)) { throw new Error("Internal error: Monotonicity violation."); } - (_b = (_a = this.target.options).onData) == null ? void 0 : _b.call(_a, chunk.data, chunk.start); + this.target.options.onData?.(chunk.data, chunk.start); __privateSet(this, _lastFlushEnd, chunk.start + chunk.data.byteLength); } __privateGet(this, _sections).length = 0; @@ -493,12 +483,11 @@ var WebMMuxer = (() => { _sections = new WeakMap(); _lastFlushEnd = new WeakMap(); _ensureMonotonicity = new WeakMap(); - var DEFAULT_CHUNK_SIZE = __pow(2, 24); + var DEFAULT_CHUNK_SIZE = 2 ** 24; var MAX_CHUNKS_AT_ONCE = 2; var _chunkSize, _chunks, _lastFlushEnd2, _ensureMonotonicity2, _writeDataIntoChunks, writeDataIntoChunks_fn, _insertSectionIntoChunk, insertSectionIntoChunk_fn, _createChunk, createChunk_fn, _flushChunks, flushChunks_fn; var ChunkedStreamTargetWriter = class extends BaseStreamTargetWriter { constructor(target, ensureMonotonicity) { - var _a, _b; super(target); __privateAdd(this, _writeDataIntoChunks); __privateAdd(this, _insertSectionIntoChunk); @@ -508,9 +497,9 @@ var WebMMuxer = (() => { __privateAdd(this, _chunks, []); __privateAdd(this, _lastFlushEnd2, 0); __privateAdd(this, _ensureMonotonicity2, void 0); - __privateSet(this, _chunkSize, (_b = (_a = target.options) == null ? void 0 : _a.chunkSize) != null ? _b : DEFAULT_CHUNK_SIZE); + __privateSet(this, _chunkSize, target.options?.chunkSize ?? DEFAULT_CHUNK_SIZE); __privateSet(this, _ensureMonotonicity2, ensureMonotonicity); - if (!Number.isInteger(__privateGet(this, _chunkSize)) || __privateGet(this, _chunkSize) < __pow(2, 10)) { + if (!Number.isInteger(__privateGet(this, _chunkSize)) || __privateGet(this, _chunkSize) < 2 ** 10) { throw new Error("Invalid StreamTarget options: chunkSize must be an integer not smaller than 1024."); } } @@ -592,7 +581,6 @@ var WebMMuxer = (() => { }; _flushChunks = new WeakSet(); flushChunks_fn = function(force = false) { - var _a, _b; for (let i = 0; i < __privateGet(this, _chunks).length; i++) { let chunk = __privateGet(this, _chunks)[i]; if (!chunk.shouldFlush && !force) @@ -601,8 +589,7 @@ var WebMMuxer = (() => { if (__privateGet(this, _ensureMonotonicity2) && chunk.start + section.start < __privateGet(this, _lastFlushEnd2)) { throw new Error("Internal error: Monotonicity violation."); } - (_b = (_a = this.target.options).onData) == null ? void 0 : _b.call( - _a, + this.target.options.onData?.( chunk.data.subarray(section.start, section.end), chunk.start + section.start ); @@ -613,7 +600,6 @@ var WebMMuxer = (() => { }; var FileSystemWritableFileStreamTargetWriter = class extends ChunkedStreamTargetWriter { constructor(target, ensureMonotonicity) { - var _a; super(new StreamTarget({ onData: (data, position) => target.stream.write({ type: "write", @@ -621,7 +607,7 @@ var WebMMuxer = (() => { position }), chunked: true, - chunkSize: (_a = target.options) == null ? void 0 : _a.chunkSize + chunkSize: target.options?.chunkSize }), ensureMonotonicity); } }; @@ -633,8 +619,8 @@ var WebMMuxer = (() => { var VIDEO_TRACK_TYPE = 1; var AUDIO_TRACK_TYPE = 2; var SUBTITLE_TRACK_TYPE = 17; - var MAX_CHUNK_LENGTH_MS = __pow(2, 15); - var CODEC_PRIVATE_MAX_SIZE = __pow(2, 12); + var MAX_CHUNK_LENGTH_MS = 2 ** 15; + var CODEC_PRIVATE_MAX_SIZE = 2 ** 12; var APP_NAME = "https://github.com/Vanilagy/webm-muxer"; var SEGMENT_SIZE_BYTES = 6; var CLUSTER_SIZE_BYTES = 5; @@ -690,18 +676,18 @@ var WebMMuxer = (() => { __privateAdd(this, _lastSubtitleTimestamp, -1); __privateAdd(this, _colorSpace, void 0); __privateAdd(this, _finalized, false); - var _a; __privateMethod(this, _validateOptions, validateOptions_fn).call(this, options); - __privateSet(this, _options, __spreadValues({ + __privateSet(this, _options, { type: "webm", - firstTimestampBehavior: "strict" - }, options)); + firstTimestampBehavior: "strict", + ...options + }); this.target = options.target; let ensureMonotonicity = !!__privateGet(this, _options).streaming; if (options.target instanceof ArrayBufferTarget) { __privateSet(this, _writer, new ArrayBufferTargetWriter(options.target)); } else if (options.target instanceof StreamTarget) { - __privateSet(this, _writer, ((_a = options.target.options) == null ? void 0 : _a.chunked) ? new ChunkedStreamTargetWriter(options.target, ensureMonotonicity) : new StreamTargetWriter(options.target, ensureMonotonicity)); + __privateSet(this, _writer, options.target.options?.chunked ? new ChunkedStreamTargetWriter(options.target, ensureMonotonicity) : new StreamTargetWriter(options.target, ensureMonotonicity)); } else if (options.target instanceof FileSystemWritableFileStreamTarget) { __privateSet(this, _writer, new FileSystemWritableFileStreamTargetWriter(options.target, ensureMonotonicity)); } else { @@ -723,7 +709,7 @@ var WebMMuxer = (() => { } let data = new Uint8Array(chunk.byteLength); chunk.copyTo(data); - this.addVideoChunkRaw(data, chunk.type, timestamp != null ? timestamp : chunk.timestamp, meta); + this.addVideoChunkRaw(data, chunk.type, timestamp ?? chunk.timestamp, meta); } addVideoChunkRaw(data, type, timestamp, meta) { if (!(data instanceof Uint8Array)) { @@ -775,7 +761,7 @@ var WebMMuxer = (() => { } let data = new Uint8Array(chunk.byteLength); chunk.copyTo(data); - this.addAudioChunkRaw(data, chunk.type, timestamp != null ? timestamp : chunk.timestamp, meta); + this.addAudioChunkRaw(data, chunk.type, timestamp ?? chunk.timestamp, meta); } addAudioChunkRaw(data, type, timestamp, meta) { if (!(data instanceof Uint8Array)) { @@ -795,7 +781,7 @@ var WebMMuxer = (() => { throw new Error("No audio track declared."); if (__privateGet(this, _firstAudioTimestamp) === void 0) __privateSet(this, _firstAudioTimestamp, timestamp); - if (meta == null ? void 0 : meta.decoderConfig) { + if (meta?.decoderConfig) { if (__privateGet(this, _options).streaming) { __privateSet(this, _audioCodecPrivate, __privateMethod(this, _createCodecPrivateElement, createCodecPrivateElement_fn).call(this, meta.decoderConfig.description)); } else { @@ -839,14 +825,14 @@ var WebMMuxer = (() => { __privateMethod(this, _ensureNotFinalized, ensureNotFinalized_fn).call(this); if (!__privateGet(this, _options).subtitles) throw new Error("No subtitle track declared."); - if (meta == null ? void 0 : meta.decoderConfig) { + if (meta?.decoderConfig) { if (__privateGet(this, _options).streaming) { __privateSet(this, _subtitleCodecPrivate, __privateMethod(this, _createCodecPrivateElement, createCodecPrivateElement_fn).call(this, meta.decoderConfig.description)); } else { __privateMethod(this, _writeCodecPrivate, writeCodecPrivate_fn).call(this, __privateGet(this, _subtitleCodecPrivate), meta.decoderConfig.description); } } - let subtitleChunk = __privateMethod(this, _createInternalChunk, createInternalChunk_fn).call(this, chunk.body, "key", timestamp != null ? timestamp : chunk.timestamp, SUBTITLE_TRACK_NUMBER, chunk.duration, chunk.additions); + let subtitleChunk = __privateMethod(this, _createInternalChunk, createInternalChunk_fn).call(this, chunk.body, "key", timestamp ?? chunk.timestamp, SUBTITLE_TRACK_NUMBER, chunk.duration, chunk.additions); __privateSet(this, _lastSubtitleTimestamp, subtitleChunk.timestamp); __privateGet(this, _subtitleChunkQueue).push(subtitleChunk); __privateMethod(this, _writeSubtitleChunks, writeSubtitleChunks_fn).call(this); @@ -917,6 +903,9 @@ var WebMMuxer = (() => { if (typeof options !== "object") { throw new TypeError("The muxer requires an options object to be passed to its constructor."); } + if (!(options.target instanceof Target)) { + throw new TypeError("The target must be provided and an instance of Target."); + } if (options.video) { if (typeof options.video.codec !== "string") { throw new TypeError(`Invalid video codec: ${options.video.codec}. Must be a string.`); @@ -997,13 +986,12 @@ var WebMMuxer = (() => { }; _writeEBMLHeader = new WeakSet(); writeEBMLHeader_fn = function() { - var _a; let ebmlHeader = { id: 440786851 /* EBML */, data: [ { id: 17030 /* EBMLVersion */, data: 1 }, { id: 17143 /* EBMLReadVersion */, data: 1 }, { id: 17138 /* EBMLMaxIDLength */, data: 4 }, { id: 17139 /* EBMLMaxSizeLength */, data: 8 }, - { id: 17026 /* DocType */, data: (_a = __privateGet(this, _options).type) != null ? _a : "webm" }, + { id: 17026 /* DocType */, data: __privateGet(this, _options).type ?? "webm" }, { id: 17031 /* DocTypeVersion */, data: 2 }, { id: 17029 /* DocTypeReadVersion */, data: 2 } ] }; @@ -1409,7 +1397,6 @@ If you want to allow non-zero first timestamps, set firstTimestampBehavior: 'per __privateSet(this, _config, config); } encode(text) { - var _a; if (!__privateGet(this, _config)) { throw new Error("Encoder not configured."); } @@ -1423,7 +1410,7 @@ If you want to allow non-zero first timestamps, set firstTimestampBehavior: 'per throw error; } match = cueBlockHeaderRegex.exec(text); - let preamble = text.slice(0, (_a = match == null ? void 0 : match.index) != null ? _a : text.length).trimEnd(); + let preamble = text.slice(0, match?.index ?? text.length).trimEnd(); if (!preamble) { let error = new Error("No WebVTT preamble provided."); __privateGet(this, _options2).error(error); diff --git a/build/webm-muxer.min.js b/build/webm-muxer.min.js index 7aad7b4..96d823f 100644 --- a/build/webm-muxer.min.js +++ b/build/webm-muxer.min.js @@ -1,14 +1,14 @@ -"use strict";var WebMMuxer=(()=>{var $e=Object.defineProperty;var li=Object.getOwnPropertyDescriptor;var fi=Object.getOwnPropertyNames,Rt=Object.getOwnPropertySymbols;var Mt=Object.prototype.hasOwnProperty,ci=Object.prototype.propertyIsEnumerable;var w=Math.pow,Ft=(d,e,i)=>e in d?$e(d,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):d[e]=i,Pt=(d,e)=>{for(var i in e||={})Mt.call(e,i)&&Ft(d,i,e[i]);if(Rt)for(var i of Rt(e))ci.call(e,i)&&Ft(d,i,e[i]);return d};var mi=(d,e)=>{for(var i in e)$e(d,i,{get:e[i],enumerable:!0})},bi=(d,e,i,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of fi(e))!Mt.call(d,s)&&s!==i&&$e(d,s,{get:()=>e[s],enumerable:!(r=li(e,s))||r.enumerable});return d};var wi=d=>bi($e({},"__esModule",{value:!0}),d);var Tt=(d,e,i)=>{if(!e.has(d))throw TypeError("Cannot "+i)};var t=(d,e,i)=>(Tt(d,e,"read from private field"),i?i.call(d):e.get(d)),a=(d,e,i)=>{if(e.has(d))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(d):e.set(d,i)},l=(d,e,i,r)=>(Tt(d,e,"write to private field"),r?r.call(d,i):e.set(d,i),i);var h=(d,e,i)=>(Tt(d,e,"access private method"),i);var Ai={};mi(Ai,{ArrayBufferTarget:()=>pe,FileSystemWritableFileStreamTarget:()=>ge,Muxer:()=>rt,StreamTarget:()=>H,SubtitleEncoder:()=>pt});var J=class{constructor(e){this.value=e}},O=class{constructor(e){this.value=e}};var kt=d=>d<1<<8?1:d<1<<16?2:d<1<<24?3:d{if(d<(1<<7)-1)return 1;if(d<(1<<14)-1)return 2;if(d<(1<<21)-1)return 3;if(d<(1<<28)-1)return 4;if(d{let r=0;for(let s=e;s>c;r<<=1,r|=b}return r},_t=(d,e,i,r)=>{for(let s=e;s>i-s-1<>8),t(this,m).setUint8(r++,e);break;case 3:t(this,m).setUint8(r++,1<<5|e>>16),t(this,m).setUint8(r++,e>>8),t(this,m).setUint8(r++,e);break;case 4:t(this,m).setUint8(r++,1<<4|e>>24),t(this,m).setUint8(r++,e>>16),t(this,m).setUint8(r++,e>>8),t(this,m).setUint8(r++,e);break;case 5:t(this,m).setUint8(r++,1<<3|e/w(2,32)&7),t(this,m).setUint8(r++,e>>24),t(this,m).setUint8(r++,e>>16),t(this,m).setUint8(r++,e>>8),t(this,m).setUint8(r++,e);break;case 6:t(this,m).setUint8(r++,1<<2|e/w(2,40)&3),t(this,m).setUint8(r++,e/w(2,32)|0),t(this,m).setUint8(r++,e>>24),t(this,m).setUint8(r++,e>>16),t(this,m).setUint8(r++,e>>8),t(this,m).setUint8(r++,e);break;default:throw new Error("Bad EBML VINT size "+i)}this.write(t(this,A).subarray(0,r))}writeEBML(e){var i,r;if(e!==null)if(e instanceof Uint8Array)this.write(e);else if(Array.isArray(e))for(let s of e)this.writeEBML(s);else if(this.offsets.set(e,this.pos),h(this,ke,Ct).call(this,e.id),Array.isArray(e.data)){let s=this.pos,n=e.size===-1?1:(i=e.size)!=null?i:4;e.size===-1?h(this,Ie,Ot).call(this,255):this.seek(this.pos+n);let o=this.pos;if(this.dataOffsets.set(e,o),this.writeEBML(e.data),e.size!==-1){let c=this.pos-o,b=this.pos;this.seek(s),this.writeEBMLVarInt(c,n),this.seek(b)}}else if(typeof e.data=="number"){let s=(r=e.size)!=null?r:kt(e.data);this.writeEBMLVarInt(s),h(this,ke,Ct).call(this,e.data,s)}else typeof e.data=="string"?(this.writeEBMLVarInt(e.data.length),h(this,Ye,$t).call(this,e.data)):e.data instanceof Uint8Array?(this.writeEBMLVarInt(e.data.byteLength,e.size),this.write(e.data)):e.data instanceof J?(this.writeEBMLVarInt(4),h(this,Ge,Wt).call(this,e.data.value)):e.data instanceof O&&(this.writeEBMLVarInt(8),h(this,Le,Ht).call(this,e.data.value))}};A=new WeakMap,m=new WeakMap,Ie=new WeakSet,Ot=function(e){t(this,m).setUint8(0,e),this.write(t(this,A).subarray(0,1))},Ge=new WeakSet,Wt=function(e){t(this,m).setFloat32(0,e,!1),this.write(t(this,A).subarray(0,4))},Le=new WeakSet,Ht=function(e){t(this,m).setFloat64(0,e,!1),this.write(t(this,A))},ke=new WeakSet,Ct=function(e,i=kt(e)){let r=0;switch(i){case 6:t(this,m).setUint8(r++,e/w(2,40)|0);case 5:t(this,m).setUint8(r++,e/w(2,32)|0);case 4:t(this,m).setUint8(r++,e>>24);case 3:t(this,m).setUint8(r++,e>>16);case 2:t(this,m).setUint8(r++,e>>8);case 1:t(this,m).setUint8(r++,e);break;default:throw new Error("Bad UINT size "+i)}this.write(t(this,A).subarray(0,r))},Ye=new WeakSet,$t=function(e){this.write(new Uint8Array(e.split("").map(i=>i.charCodeAt(0))))};var Ce,D,ee,xe,xt,Ke=class extends je{constructor(i){super();a(this,xe);a(this,Ce,void 0);a(this,D,new ArrayBuffer(w(2,16)));a(this,ee,new Uint8Array(t(this,D)));l(this,Ce,i)}write(i){h(this,xe,xt).call(this,this.pos+i.byteLength),t(this,ee).set(i,this.pos),this.pos+=i.byteLength}finalize(){h(this,xe,xt).call(this,this.pos),t(this,Ce).buffer=t(this,D).slice(0,this.pos)}};Ce=new WeakMap,D=new WeakMap,ee=new WeakMap,xe=new WeakSet,xt=function(i){let r=t(this,D).byteLength;for(;ro.start-c.start);i.push({start:r[0].start,size:r[0].data.byteLength});for(let o=1;op.start<=r&&rgi){for(let p=0;p=i.written[o+1].start;)i.written[o].end=Math.max(i.written[o].end,i.written[o+1].end),i.written.splice(o+1,1)},Qe=new WeakSet,jt=function(i){let s={start:Math.floor(i/t(this,y))*t(this,y),data:new Uint8Array(t(this,y)),written:[],shouldFlush:!1};return t(this,g).push(s),t(this,g).sort((n,o)=>n.start-o.start),t(this,g).indexOf(s)},te=new WeakSet,Be=function(i=!1){var r,s;for(let n=0;ne.stream.write({type:"write",data:s,position:n}),chunked:!0,chunkSize:(r=e.options)==null?void 0:r.chunkSize}),i)}};var ie=1,ze=2,Xe=3,yi=1,Ti=2,ki=17,At=w(2,15),Ve=w(2,12),Kt="https://github.com/Vanilagy/webm-muxer",qt=6,It=5,Ci=["strict","offset","permissive"],f,u,ae,ne,x,B,j,M,K,v,q,I,S,G,L,z,V,P,oe,he,Y,Z,Re,de,ue,st,Gt,at,Lt,nt,Yt,ot,Zt,ht,Qt,dt,Xt,ut,Jt,Fe,Et,Me,Ut,lt,ei,N,re,_,se,ft,ti,ct,ii,le,Je,fe,et,mt,ri,C,U,Q,De,ce,tt,bt,si,Pe,vt,me,it,rt=class{constructor(e){a(this,st);a(this,at);a(this,nt);a(this,ot);a(this,ht);a(this,dt);a(this,ut);a(this,Fe);a(this,Me);a(this,lt);a(this,N);a(this,_);a(this,ft);a(this,ct);a(this,le);a(this,fe);a(this,mt);a(this,C);a(this,Q);a(this,ce);a(this,bt);a(this,Pe);a(this,me);a(this,f,void 0);a(this,u,void 0);a(this,ae,void 0);a(this,ne,void 0);a(this,x,void 0);a(this,B,void 0);a(this,j,void 0);a(this,M,void 0);a(this,K,void 0);a(this,v,void 0);a(this,q,void 0);a(this,I,void 0);a(this,S,void 0);a(this,G,void 0);a(this,L,0);a(this,z,[]);a(this,V,[]);a(this,P,[]);a(this,oe,void 0);a(this,he,void 0);a(this,Y,-1);a(this,Z,-1);a(this,Re,-1);a(this,de,void 0);a(this,ue,!1);var r;h(this,st,Gt).call(this,e),l(this,f,Pt({type:"webm",firstTimestampBehavior:"strict"},e)),this.target=e.target;let i=!!t(this,f).streaming;if(e.target instanceof pe)l(this,u,new Ke(e.target));else if(e.target instanceof H)l(this,u,(r=e.target.options)!=null&&r.chunked?new Te(e.target,i):new ye(e.target,i));else if(e.target instanceof ge)l(this,u,new qe(e.target,i));else throw new Error(`Invalid target: ${e.target}`);h(this,at,Lt).call(this)}addVideoChunk(e,i,r){if(!(e instanceof EncodedVideoChunk))throw new TypeError("addVideoChunk's first argument (chunk) must be of type EncodedVideoChunk.");if(i&&typeof i!="object")throw new TypeError("addVideoChunk's second argument (meta), when provided, must be an object.");if(r!==void 0&&(!Number.isFinite(r)||r<0))throw new TypeError("addVideoChunk's third argument (timestamp), when provided, must be a non-negative real number.");let s=new Uint8Array(e.byteLength);e.copyTo(s),this.addVideoChunkRaw(s,e.type,r!=null?r:e.timestamp,i)}addVideoChunkRaw(e,i,r,s){if(!(e instanceof Uint8Array))throw new TypeError("addVideoChunkRaw's first argument (data) must be an instance of Uint8Array.");if(i!=="key"&&i!=="delta")throw new TypeError("addVideoChunkRaw's second argument (type) must be either 'key' or 'delta'.");if(!Number.isFinite(r)||r<0)throw new TypeError("addVideoChunkRaw's third argument (timestamp) must be a non-negative real number.");if(s&&typeof s!="object")throw new TypeError("addVideoChunkRaw's fourth argument (meta), when provided, must be an object.");if(h(this,me,it).call(this),!t(this,f).video)throw new Error("No video track declared.");t(this,oe)===void 0&&l(this,oe,r),s&&h(this,ft,ti).call(this,s);let n=h(this,fe,et).call(this,e,i,r,ie);for(t(this,f).video.codec==="V_VP9"&&h(this,ct,ii).call(this,n),l(this,Y,n.timestamp);t(this,V).length>0&&t(this,V)[0].timestamp<=n.timestamp;){let o=t(this,V).shift();h(this,C,U).call(this,o,!1)}!t(this,f).audio||n.timestamp<=t(this,Z)?h(this,C,U).call(this,n,!0):t(this,z).push(n),h(this,le,Je).call(this),h(this,N,re).call(this)}addAudioChunk(e,i,r){if(!(e instanceof EncodedAudioChunk))throw new TypeError("addAudioChunk's first argument (chunk) must be of type EncodedAudioChunk.");if(i&&typeof i!="object")throw new TypeError("addAudioChunk's second argument (meta), when provided, must be an object.");if(r!==void 0&&(!Number.isFinite(r)||r<0))throw new TypeError("addAudioChunk's third argument (timestamp), when provided, must be a non-negative real number.");let s=new Uint8Array(e.byteLength);e.copyTo(s),this.addAudioChunkRaw(s,e.type,r!=null?r:e.timestamp,i)}addAudioChunkRaw(e,i,r,s){if(!(e instanceof Uint8Array))throw new TypeError("addAudioChunkRaw's first argument (data) must be an instance of Uint8Array.");if(i!=="key"&&i!=="delta")throw new TypeError("addAudioChunkRaw's second argument (type) must be either 'key' or 'delta'.");if(!Number.isFinite(r)||r<0)throw new TypeError("addAudioChunkRaw's third argument (timestamp) must be a non-negative real number.");if(s&&typeof s!="object")throw new TypeError("addAudioChunkRaw's fourth argument (meta), when provided, must be an object.");if(h(this,me,it).call(this),!t(this,f).audio)throw new Error("No audio track declared.");t(this,he)===void 0&&l(this,he,r),s!=null&&s.decoderConfig&&(t(this,f).streaming?l(this,v,h(this,Q,De).call(this,s.decoderConfig.description)):h(this,ce,tt).call(this,t(this,v),s.decoderConfig.description));let n=h(this,fe,et).call(this,e,i,r,ze);for(l(this,Z,n.timestamp);t(this,z).length>0&&t(this,z)[0].timestamp<=n.timestamp;){let o=t(this,z).shift();h(this,C,U).call(this,o,!0)}!t(this,f).video||n.timestamp<=t(this,Y)?h(this,C,U).call(this,n,!t(this,f).video):t(this,V).push(n),h(this,le,Je).call(this),h(this,N,re).call(this)}addSubtitleChunk(e,i,r){if(typeof e!="object"||!e)throw new TypeError("addSubtitleChunk's first argument (chunk) must be an object.");if(!(e.body instanceof Uint8Array))throw new TypeError("body must be an instance of Uint8Array.");if(!Number.isFinite(e.timestamp)||e.timestamp<0)throw new TypeError("timestamp must be a non-negative real number.");if(!Number.isFinite(e.duration)||e.duration<0)throw new TypeError("duration must be a non-negative real number.");if(e.additions&&!(e.additions instanceof Uint8Array))throw new TypeError("additions, when present, must be an instance of Uint8Array.");if(typeof i!="object")throw new TypeError("addSubtitleChunk's second argument (meta) must be an object.");if(h(this,me,it).call(this),!t(this,f).subtitles)throw new Error("No subtitle track declared.");i!=null&&i.decoderConfig&&(t(this,f).streaming?l(this,q,h(this,Q,De).call(this,i.decoderConfig.description)):h(this,ce,tt).call(this,t(this,q),i.decoderConfig.description));let s=h(this,fe,et).call(this,e.body,"key",r!=null?r:e.timestamp,Xe,e.duration,e.additions);l(this,Re,s.timestamp),t(this,P).push(s),h(this,le,Je).call(this),h(this,N,re).call(this)}finalize(){if(t(this,ue))throw new Error("Cannot finalize a muxer more than once.");for(;t(this,z).length>0;)h(this,C,U).call(this,t(this,z).shift(),!0);for(;t(this,V).length>0;)h(this,C,U).call(this,t(this,V).shift(),!0);for(;t(this,P).length>0&&t(this,P)[0].timestamp<=t(this,L);)h(this,C,U).call(this,t(this,P).shift(),!1);if(t(this,f).streaming||h(this,Pe,vt).call(this),t(this,u).writeEBML(t(this,I)),!t(this,f).streaming){let e=t(this,u).pos,i=t(this,u).pos-t(this,_,se);t(this,u).seek(t(this,u).offsets.get(t(this,ae))+4),t(this,u).writeEBMLVarInt(i,qt),t(this,j).data=new O(t(this,L)),t(this,u).seek(t(this,u).offsets.get(t(this,j))),t(this,u).writeEBML(t(this,j)),t(this,x).data[0].data[1].data=t(this,u).offsets.get(t(this,I))-t(this,_,se),t(this,x).data[1].data[1].data=t(this,u).offsets.get(t(this,ne))-t(this,_,se),t(this,x).data[2].data[1].data=t(this,u).offsets.get(t(this,B))-t(this,_,se),t(this,u).seek(t(this,u).offsets.get(t(this,x))),t(this,u).writeEBML(t(this,x)),t(this,u).seek(e)}h(this,N,re).call(this),t(this,u).finalize(),l(this,ue,!0)}};f=new WeakMap,u=new WeakMap,ae=new WeakMap,ne=new WeakMap,x=new WeakMap,B=new WeakMap,j=new WeakMap,M=new WeakMap,K=new WeakMap,v=new WeakMap,q=new WeakMap,I=new WeakMap,S=new WeakMap,G=new WeakMap,L=new WeakMap,z=new WeakMap,V=new WeakMap,P=new WeakMap,oe=new WeakMap,he=new WeakMap,Y=new WeakMap,Z=new WeakMap,Re=new WeakMap,de=new WeakMap,ue=new WeakMap,st=new WeakSet,Gt=function(e){if(typeof e!="object")throw new TypeError("The muxer requires an options object to be passed to its constructor.");if(e.video){if(typeof e.video.codec!="string")throw new TypeError(`Invalid video codec: ${e.video.codec}. Must be a string.`);if(!Number.isInteger(e.video.width)||e.video.width<=0)throw new TypeError(`Invalid video width: ${e.video.width}. Must be a positive integer.`);if(!Number.isInteger(e.video.height)||e.video.height<=0)throw new TypeError(`Invalid video height: ${e.video.height}. Must be a positive integer.`);if(e.video.frameRate!==void 0&&(!Number.isFinite(e.video.frameRate)||e.video.frameRate<=0))throw new TypeError(`Invalid video frame rate: ${e.video.frameRate}. Must be a positive number.`);if(e.video.alpha!==void 0&&typeof e.video.alpha!="boolean")throw new TypeError(`Invalid video alpha: ${e.video.alpha}. Must be a boolean.`)}if(e.audio){if(typeof e.audio.codec!="string")throw new TypeError(`Invalid audio codec: ${e.audio.codec}. Must be a string.`);if(!Number.isInteger(e.audio.numberOfChannels)||e.audio.numberOfChannels<=0)throw new TypeError(`Invalid number of audio channels: ${e.audio.numberOfChannels}. Must be a positive integer.`);if(!Number.isInteger(e.audio.sampleRate)||e.audio.sampleRate<=0)throw new TypeError(`Invalid audio sample rate: ${e.audio.sampleRate}. Must be a positive integer.`);if(e.audio.bitDepth!==void 0&&(!Number.isInteger(e.audio.bitDepth)||e.audio.bitDepth<=0))throw new TypeError(`Invalid audio bit depth: ${e.audio.bitDepth}. Must be a positive integer.`)}if(e.subtitles&&typeof e.subtitles.codec!="string")throw new TypeError(`Invalid subtitles codec: ${e.subtitles.codec}. Must be a string.`);if(e.type!==void 0&&!["webm","matroska"].includes(e.type))throw new TypeError(`Invalid type: ${e.type}. Must be 'webm' or 'matroska'.`);if(e.firstTimestampBehavior&&!Ci.includes(e.firstTimestampBehavior))throw new TypeError(`Invalid first timestamp behavior: ${e.firstTimestampBehavior}`);if(e.streaming!==void 0&&typeof e.streaming!="boolean")throw new TypeError(`Invalid streaming option: ${e.streaming}. Must be a boolean.`)},at=new WeakSet,Lt=function(){t(this,u)instanceof E&&t(this,u).target.options.onHeader&&t(this,u).startTrackingWrites(),h(this,nt,Yt).call(this),t(this,f).streaming||h(this,dt,Xt).call(this),h(this,ut,Jt).call(this),h(this,ot,Zt).call(this),h(this,ht,Qt).call(this),t(this,f).streaming||(h(this,Fe,Et).call(this),h(this,Me,Ut).call(this)),h(this,lt,ei).call(this),h(this,N,re).call(this)},nt=new WeakSet,Yt=function(){var i;let e={id:440786851,data:[{id:17030,data:1},{id:17143,data:1},{id:17138,data:4},{id:17139,data:8},{id:17026,data:(i=t(this,f).type)!=null?i:"webm"},{id:17031,data:2},{id:17029,data:2}]};t(this,u).writeEBML(e)},ot=new WeakSet,Zt=function(){l(this,K,{id:236,size:4,data:new Uint8Array(Ve)}),l(this,v,{id:236,size:4,data:new Uint8Array(Ve)}),l(this,q,{id:236,size:4,data:new Uint8Array(Ve)})},ht=new WeakSet,Qt=function(){l(this,M,{id:21936,data:[{id:21937,data:2},{id:21946,data:2},{id:21947,data:2},{id:21945,data:0}]})},dt=new WeakSet,Xt=function(){let e=new Uint8Array([28,83,187,107]),i=new Uint8Array([21,73,169,102]),r=new Uint8Array([22,84,174,107]),s={id:290298740,data:[{id:19899,data:[{id:21419,data:e},{id:21420,size:5,data:0}]},{id:19899,data:[{id:21419,data:i},{id:21420,size:5,data:0}]},{id:19899,data:[{id:21419,data:r},{id:21420,size:5,data:0}]}]};l(this,x,s)},ut=new WeakSet,Jt=function(){let e={id:17545,data:new O(0)};l(this,j,e);let i={id:357149030,data:[{id:2807729,data:1e6},{id:19840,data:Kt},{id:22337,data:Kt},t(this,f).streaming?null:e]};l(this,ne,i)},Fe=new WeakSet,Et=function(){let e={id:374648427,data:[]};l(this,B,e),t(this,f).video&&e.data.push({id:174,data:[{id:215,data:ie},{id:29637,data:ie},{id:131,data:yi},{id:134,data:t(this,f).video.codec},t(this,K),t(this,f).video.frameRate?{id:2352003,data:1e9/t(this,f).video.frameRate}:null,{id:224,data:[{id:176,data:t(this,f).video.width},{id:186,data:t(this,f).video.height},t(this,f).video.alpha?{id:21440,data:1}:null,t(this,M)]}]}),t(this,f).audio&&(l(this,v,t(this,f).streaming?t(this,v)||null:{id:236,size:4,data:new Uint8Array(Ve)}),e.data.push({id:174,data:[{id:215,data:ze},{id:29637,data:ze},{id:131,data:Ti},{id:134,data:t(this,f).audio.codec},t(this,v),{id:225,data:[{id:181,data:new J(t(this,f).audio.sampleRate)},{id:159,data:t(this,f).audio.numberOfChannels},t(this,f).audio.bitDepth?{id:25188,data:t(this,f).audio.bitDepth}:null]}]})),t(this,f).subtitles&&e.data.push({id:174,data:[{id:215,data:Xe},{id:29637,data:Xe},{id:131,data:ki},{id:134,data:t(this,f).subtitles.codec},t(this,q)]})},Me=new WeakSet,Ut=function(){let e={id:408125543,size:t(this,f).streaming?-1:qt,data:[t(this,f).streaming?null:t(this,x),t(this,ne),t(this,B)]};if(l(this,ae,e),t(this,u).writeEBML(e),t(this,u)instanceof E&&t(this,u).target.options.onHeader){let{data:i,start:r}=t(this,u).getTrackedWrites();t(this,u).target.options.onHeader(i,r)}},lt=new WeakSet,ei=function(){l(this,I,{id:475249515,data:[]})},N=new WeakSet,re=function(){t(this,u)instanceof ye&&t(this,u).flush()},_=new WeakSet,se=function(){return t(this,u).dataOffsets.get(t(this,ae))},ft=new WeakSet,ti=function(e){if(!!e.decoderConfig){if(e.decoderConfig.colorSpace){let i=e.decoderConfig.colorSpace;if(l(this,de,i),t(this,M).data=[{id:21937,data:{rgb:1,bt709:1,bt470bg:5,smpte170m:6}[i.matrix]},{id:21946,data:{bt709:1,smpte170m:6,"iec61966-2-1":13}[i.transfer]},{id:21947,data:{bt709:1,bt470bg:5,smpte170m:6}[i.primaries]},{id:21945,data:[1,2][Number(i.fullRange)]}],!t(this,f).streaming){let r=t(this,u).pos;t(this,u).seek(t(this,u).offsets.get(t(this,M))),t(this,u).writeEBML(t(this,M)),t(this,u).seek(r)}}e.decoderConfig.description&&(t(this,f).streaming?l(this,K,h(this,Q,De).call(this,e.decoderConfig.description)):h(this,ce,tt).call(this,t(this,K),e.decoderConfig.description))}},ct=new WeakSet,ii=function(e){if(e.type!=="key"||!t(this,de))return;let i=0;if(W(e.data,0,2)!==2)return;i+=2;let r=(W(e.data,i+1,i+2)<<1)+W(e.data,i+0,i+1);i+=2,r===3&&i++;let s=W(e.data,i+0,i+1);if(i++,s)return;let n=W(e.data,i+0,i+1);if(i++,n!==0)return;i+=2;let o=W(e.data,i+0,i+24);if(i+=24,o!==4817730)return;r>=2&&i++;let c={rgb:7,bt709:2,bt470bg:1,smpte170m:3}[t(this,de).matrix];_t(e.data,i+0,i+3,c)},le=new WeakSet,Je=function(){let e=Math.min(t(this,f).video?t(this,Y):1/0,t(this,f).audio?t(this,Z):1/0),i=t(this,P);for(;i.length>0&&i[0].timestamp<=e;)h(this,C,U).call(this,i.shift(),!t(this,f).video&&!t(this,f).audio)},fe=new WeakSet,et=function(e,i,r,s,n,o){let c=h(this,mt,ri).call(this,r,s);return{data:e,additions:o,type:i,timestamp:c,duration:n,trackNumber:s}},mt=new WeakSet,ri=function(e,i){let r=i===ie?t(this,Y):i===ze?t(this,Z):t(this,Re);if(i!==Xe){let s=i===ie?t(this,oe):t(this,he);if(t(this,f).firstTimestampBehavior==="strict"&&r===-1&&e!==0)throw new Error(`The first chunk for your media track must have a timestamp of 0 (received ${e}). Non-zero first timestamps are often caused by directly piping frames or audio data from a MediaStreamTrack into the encoder. Their timestamps are typically relative to the age of the document, which is probably what you want. +"use strict";var WebMMuxer=(()=>{var gt=Object.defineProperty;var oi=Object.getOwnPropertyDescriptor;var hi=Object.getOwnPropertyNames;var di=Object.prototype.hasOwnProperty;var ui=(d,e)=>{for(var i in e)gt(d,i,{get:e[i],enumerable:!0})},li=(d,e,i,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of hi(e))!di.call(d,s)&&s!==i&>(d,s,{get:()=>e[s],enumerable:!(r=oi(e,s))||r.enumerable});return d};var fi=d=>li(gt({},"__esModule",{value:!0}),d);var yt=(d,e,i)=>{if(!e.has(d))throw TypeError("Cannot "+i)};var t=(d,e,i)=>(yt(d,e,"read from private field"),i?i.call(d):e.get(d)),a=(d,e,i)=>{if(e.has(d))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(d):e.set(d,i)},u=(d,e,i,r)=>(yt(d,e,"write to private field"),r?r.call(d,i):e.set(d,i),i);var o=(d,e,i)=>(yt(d,e,"access private method"),i);var Ci={};ui(Ci,{ArrayBufferTarget:()=>ee,FileSystemWritableFileStreamTarget:()=>te,Muxer:()=>tt,StreamTarget:()=>V,SubtitleEncoder:()=>bt});var J=class{constructor(e){this.value=e}},O=class{constructor(e){this.value=e}};var Tt=d=>d<1<<8?1:d<1<<16?2:d<1<<24?3:d<2**32?4:d<2**40?5:6,Rt=d=>{if(d<(1<<7)-1)return 1;if(d<(1<<14)-1)return 2;if(d<(1<<21)-1)return 3;if(d<(1<<28)-1)return 4;if(d<2**35-1)return 5;if(d<2**42-1)return 6;throw new Error("EBML VINT size not supported "+d)};var W=(d,e,i)=>{let r=0;for(let s=e;s>m;r<<=1,r|=p}return r},Ft=(d,e,i,r)=>{for(let s=e;s>i-s-1<>8),t(this,c).setUint8(r++,e);break;case 3:t(this,c).setUint8(r++,1<<5|e>>16),t(this,c).setUint8(r++,e>>8),t(this,c).setUint8(r++,e);break;case 4:t(this,c).setUint8(r++,1<<4|e>>24),t(this,c).setUint8(r++,e>>16),t(this,c).setUint8(r++,e>>8),t(this,c).setUint8(r++,e);break;case 5:t(this,c).setUint8(r++,1<<3|e/2**32&7),t(this,c).setUint8(r++,e>>24),t(this,c).setUint8(r++,e>>16),t(this,c).setUint8(r++,e>>8),t(this,c).setUint8(r++,e);break;case 6:t(this,c).setUint8(r++,1<<2|e/2**40&3),t(this,c).setUint8(r++,e/2**32|0),t(this,c).setUint8(r++,e>>24),t(this,c).setUint8(r++,e>>16),t(this,c).setUint8(r++,e>>8),t(this,c).setUint8(r++,e);break;default:throw new Error("Bad EBML VINT size "+i)}this.write(t(this,S).subarray(0,r))}writeEBML(e){if(e!==null)if(e instanceof Uint8Array)this.write(e);else if(Array.isArray(e))for(let i of e)this.writeEBML(i);else if(this.offsets.set(e,this.pos),o(this,Te,kt).call(this,e.id),Array.isArray(e.data)){let i=this.pos,r=e.size===-1?1:e.size??4;e.size===-1?o(this,Ke,Mt).call(this,255):this.seek(this.pos+r);let s=this.pos;if(this.dataOffsets.set(e,s),this.writeEBML(e.data),e.size!==-1){let n=this.pos-s,l=this.pos;this.seek(i),this.writeEBMLVarInt(n,r),this.seek(l)}}else if(typeof e.data=="number"){let i=e.size??Tt(e.data);this.writeEBMLVarInt(i),o(this,Te,kt).call(this,e.data,i)}else typeof e.data=="string"?(this.writeEBMLVarInt(e.data.length),o(this,Ge,_t).call(this,e.data)):e.data instanceof Uint8Array?(this.writeEBMLVarInt(e.data.byteLength,e.size),this.write(e.data)):e.data instanceof J?(this.writeEBMLVarInt(4),o(this,qe,Pt).call(this,e.data.value)):e.data instanceof O&&(this.writeEBMLVarInt(8),o(this,Ie,Nt).call(this,e.data.value))}};S=new WeakMap,c=new WeakMap,Ke=new WeakSet,Mt=function(e){t(this,c).setUint8(0,e),this.write(t(this,S).subarray(0,1))},qe=new WeakSet,Pt=function(e){t(this,c).setFloat32(0,e,!1),this.write(t(this,S).subarray(0,4))},Ie=new WeakSet,Nt=function(e){t(this,c).setFloat64(0,e,!1),this.write(t(this,S))},Te=new WeakSet,kt=function(e,i=Tt(e)){let r=0;switch(i){case 6:t(this,c).setUint8(r++,e/2**40|0);case 5:t(this,c).setUint8(r++,e/2**32|0);case 4:t(this,c).setUint8(r++,e>>24);case 3:t(this,c).setUint8(r++,e>>16);case 2:t(this,c).setUint8(r++,e>>8);case 1:t(this,c).setUint8(r++,e);break;default:throw new Error("Bad UINT size "+i)}this.write(t(this,S).subarray(0,r))},Ge=new WeakSet,_t=function(e){this.write(new Uint8Array(e.split("").map(i=>i.charCodeAt(0))))};var ke,D,ie,Ce,Ct,Be=class extends $e{constructor(i){super();a(this,Ce);a(this,ke,void 0);a(this,D,new ArrayBuffer(2**16));a(this,ie,new Uint8Array(t(this,D)));u(this,ke,i)}write(i){o(this,Ce,Ct).call(this,this.pos+i.byteLength),t(this,ie).set(i,this.pos),this.pos+=i.byteLength}finalize(){o(this,Ce,Ct).call(this,this.pos),t(this,ke).buffer=t(this,D).slice(0,this.pos)}};ke=new WeakMap,D=new WeakMap,ie=new WeakMap,Ce=new WeakSet,Ct=function(i){let r=t(this,D).byteLength;for(;rs.start-n.start);i.push({start:r[0].start,size:r[0].data.byteLength});for(let s=1;sb.start<=r&&rbi){for(let b=0;b=i.written[l+1].start;)i.written[l].end=Math.max(i.written[l].end,i.written[l+1].end),i.written.splice(l+1,1)},Ye=new WeakSet,Wt=function(i){let s={start:Math.floor(i/t(this,g))*t(this,g),data:new Uint8Array(t(this,g)),written:[],shouldFlush:!1};return t(this,w).push(s),t(this,w).sort((n,l)=>n.start-l.start),t(this,w).indexOf(s)},re=new WeakSet,He=function(i=!1){for(let r=0;re.stream.write({type:"write",data:r,position:s}),chunked:!0,chunkSize:e.options?.chunkSize}),i)}};var se=1,ve=2,Ze=3,wi=1,pi=2,gi=17,St=2**15,ze=2**12,Ht="https://github.com/Vanilagy/webm-muxer",$t=6,Bt=5,yi=["strict","offset","permissive"],f,h,oe,he,C,B,j,M,K,U,q,I,x,G,L,v,z,P,de,ue,Y,Z,De,le,fe,it,jt,rt,Kt,st,qt,at,It,nt,Gt,ot,Lt,ht,Yt,Re,Et,Fe,At,dt,Zt,N,ae,_,ne,ut,Qt,lt,Xt,ce,Qe,me,Xe,ft,Jt,k,A,Q,Ve,be,Je,ct,ei,Me,Ut,we,et,tt=class{constructor(e){a(this,it);a(this,rt);a(this,st);a(this,at);a(this,nt);a(this,ot);a(this,ht);a(this,Re);a(this,Fe);a(this,dt);a(this,N);a(this,_);a(this,ut);a(this,lt);a(this,ce);a(this,me);a(this,ft);a(this,k);a(this,Q);a(this,be);a(this,ct);a(this,Me);a(this,we);a(this,f,void 0);a(this,h,void 0);a(this,oe,void 0);a(this,he,void 0);a(this,C,void 0);a(this,B,void 0);a(this,j,void 0);a(this,M,void 0);a(this,K,void 0);a(this,U,void 0);a(this,q,void 0);a(this,I,void 0);a(this,x,void 0);a(this,G,void 0);a(this,L,0);a(this,v,[]);a(this,z,[]);a(this,P,[]);a(this,de,void 0);a(this,ue,void 0);a(this,Y,-1);a(this,Z,-1);a(this,De,-1);a(this,le,void 0);a(this,fe,!1);o(this,it,jt).call(this,e),u(this,f,{type:"webm",firstTimestampBehavior:"strict",...e}),this.target=e.target;let i=!!t(this,f).streaming;if(e.target instanceof ee)u(this,h,new Be(e.target));else if(e.target instanceof V)u(this,h,e.target.options?.chunked?new ye(e.target,i):new ge(e.target,i));else if(e.target instanceof te)u(this,h,new je(e.target,i));else throw new Error(`Invalid target: ${e.target}`);o(this,rt,Kt).call(this)}addVideoChunk(e,i,r){if(!(e instanceof EncodedVideoChunk))throw new TypeError("addVideoChunk's first argument (chunk) must be of type EncodedVideoChunk.");if(i&&typeof i!="object")throw new TypeError("addVideoChunk's second argument (meta), when provided, must be an object.");if(r!==void 0&&(!Number.isFinite(r)||r<0))throw new TypeError("addVideoChunk's third argument (timestamp), when provided, must be a non-negative real number.");let s=new Uint8Array(e.byteLength);e.copyTo(s),this.addVideoChunkRaw(s,e.type,r??e.timestamp,i)}addVideoChunkRaw(e,i,r,s){if(!(e instanceof Uint8Array))throw new TypeError("addVideoChunkRaw's first argument (data) must be an instance of Uint8Array.");if(i!=="key"&&i!=="delta")throw new TypeError("addVideoChunkRaw's second argument (type) must be either 'key' or 'delta'.");if(!Number.isFinite(r)||r<0)throw new TypeError("addVideoChunkRaw's third argument (timestamp) must be a non-negative real number.");if(s&&typeof s!="object")throw new TypeError("addVideoChunkRaw's fourth argument (meta), when provided, must be an object.");if(o(this,we,et).call(this),!t(this,f).video)throw new Error("No video track declared.");t(this,de)===void 0&&u(this,de,r),s&&o(this,ut,Qt).call(this,s);let n=o(this,me,Xe).call(this,e,i,r,se);for(t(this,f).video.codec==="V_VP9"&&o(this,lt,Xt).call(this,n),u(this,Y,n.timestamp);t(this,z).length>0&&t(this,z)[0].timestamp<=n.timestamp;){let l=t(this,z).shift();o(this,k,A).call(this,l,!1)}!t(this,f).audio||n.timestamp<=t(this,Z)?o(this,k,A).call(this,n,!0):t(this,v).push(n),o(this,ce,Qe).call(this),o(this,N,ae).call(this)}addAudioChunk(e,i,r){if(!(e instanceof EncodedAudioChunk))throw new TypeError("addAudioChunk's first argument (chunk) must be of type EncodedAudioChunk.");if(i&&typeof i!="object")throw new TypeError("addAudioChunk's second argument (meta), when provided, must be an object.");if(r!==void 0&&(!Number.isFinite(r)||r<0))throw new TypeError("addAudioChunk's third argument (timestamp), when provided, must be a non-negative real number.");let s=new Uint8Array(e.byteLength);e.copyTo(s),this.addAudioChunkRaw(s,e.type,r??e.timestamp,i)}addAudioChunkRaw(e,i,r,s){if(!(e instanceof Uint8Array))throw new TypeError("addAudioChunkRaw's first argument (data) must be an instance of Uint8Array.");if(i!=="key"&&i!=="delta")throw new TypeError("addAudioChunkRaw's second argument (type) must be either 'key' or 'delta'.");if(!Number.isFinite(r)||r<0)throw new TypeError("addAudioChunkRaw's third argument (timestamp) must be a non-negative real number.");if(s&&typeof s!="object")throw new TypeError("addAudioChunkRaw's fourth argument (meta), when provided, must be an object.");if(o(this,we,et).call(this),!t(this,f).audio)throw new Error("No audio track declared.");t(this,ue)===void 0&&u(this,ue,r),s?.decoderConfig&&(t(this,f).streaming?u(this,U,o(this,Q,Ve).call(this,s.decoderConfig.description)):o(this,be,Je).call(this,t(this,U),s.decoderConfig.description));let n=o(this,me,Xe).call(this,e,i,r,ve);for(u(this,Z,n.timestamp);t(this,v).length>0&&t(this,v)[0].timestamp<=n.timestamp;){let l=t(this,v).shift();o(this,k,A).call(this,l,!0)}!t(this,f).video||n.timestamp<=t(this,Y)?o(this,k,A).call(this,n,!t(this,f).video):t(this,z).push(n),o(this,ce,Qe).call(this),o(this,N,ae).call(this)}addSubtitleChunk(e,i,r){if(typeof e!="object"||!e)throw new TypeError("addSubtitleChunk's first argument (chunk) must be an object.");if(!(e.body instanceof Uint8Array))throw new TypeError("body must be an instance of Uint8Array.");if(!Number.isFinite(e.timestamp)||e.timestamp<0)throw new TypeError("timestamp must be a non-negative real number.");if(!Number.isFinite(e.duration)||e.duration<0)throw new TypeError("duration must be a non-negative real number.");if(e.additions&&!(e.additions instanceof Uint8Array))throw new TypeError("additions, when present, must be an instance of Uint8Array.");if(typeof i!="object")throw new TypeError("addSubtitleChunk's second argument (meta) must be an object.");if(o(this,we,et).call(this),!t(this,f).subtitles)throw new Error("No subtitle track declared.");i?.decoderConfig&&(t(this,f).streaming?u(this,q,o(this,Q,Ve).call(this,i.decoderConfig.description)):o(this,be,Je).call(this,t(this,q),i.decoderConfig.description));let s=o(this,me,Xe).call(this,e.body,"key",r??e.timestamp,Ze,e.duration,e.additions);u(this,De,s.timestamp),t(this,P).push(s),o(this,ce,Qe).call(this),o(this,N,ae).call(this)}finalize(){if(t(this,fe))throw new Error("Cannot finalize a muxer more than once.");for(;t(this,v).length>0;)o(this,k,A).call(this,t(this,v).shift(),!0);for(;t(this,z).length>0;)o(this,k,A).call(this,t(this,z).shift(),!0);for(;t(this,P).length>0&&t(this,P)[0].timestamp<=t(this,L);)o(this,k,A).call(this,t(this,P).shift(),!1);if(t(this,f).streaming||o(this,Me,Ut).call(this),t(this,h).writeEBML(t(this,I)),!t(this,f).streaming){let e=t(this,h).pos,i=t(this,h).pos-t(this,_,ne);t(this,h).seek(t(this,h).offsets.get(t(this,oe))+4),t(this,h).writeEBMLVarInt(i,$t),t(this,j).data=new O(t(this,L)),t(this,h).seek(t(this,h).offsets.get(t(this,j))),t(this,h).writeEBML(t(this,j)),t(this,C).data[0].data[1].data=t(this,h).offsets.get(t(this,I))-t(this,_,ne),t(this,C).data[1].data[1].data=t(this,h).offsets.get(t(this,he))-t(this,_,ne),t(this,C).data[2].data[1].data=t(this,h).offsets.get(t(this,B))-t(this,_,ne),t(this,h).seek(t(this,h).offsets.get(t(this,C))),t(this,h).writeEBML(t(this,C)),t(this,h).seek(e)}o(this,N,ae).call(this),t(this,h).finalize(),u(this,fe,!0)}};f=new WeakMap,h=new WeakMap,oe=new WeakMap,he=new WeakMap,C=new WeakMap,B=new WeakMap,j=new WeakMap,M=new WeakMap,K=new WeakMap,U=new WeakMap,q=new WeakMap,I=new WeakMap,x=new WeakMap,G=new WeakMap,L=new WeakMap,v=new WeakMap,z=new WeakMap,P=new WeakMap,de=new WeakMap,ue=new WeakMap,Y=new WeakMap,Z=new WeakMap,De=new WeakMap,le=new WeakMap,fe=new WeakMap,it=new WeakSet,jt=function(e){if(typeof e!="object")throw new TypeError("The muxer requires an options object to be passed to its constructor.");if(!(e.target instanceof H))throw new TypeError("The target must be provided and an instance of Target.");if(e.video){if(typeof e.video.codec!="string")throw new TypeError(`Invalid video codec: ${e.video.codec}. Must be a string.`);if(!Number.isInteger(e.video.width)||e.video.width<=0)throw new TypeError(`Invalid video width: ${e.video.width}. Must be a positive integer.`);if(!Number.isInteger(e.video.height)||e.video.height<=0)throw new TypeError(`Invalid video height: ${e.video.height}. Must be a positive integer.`);if(e.video.frameRate!==void 0&&(!Number.isFinite(e.video.frameRate)||e.video.frameRate<=0))throw new TypeError(`Invalid video frame rate: ${e.video.frameRate}. Must be a positive number.`);if(e.video.alpha!==void 0&&typeof e.video.alpha!="boolean")throw new TypeError(`Invalid video alpha: ${e.video.alpha}. Must be a boolean.`)}if(e.audio){if(typeof e.audio.codec!="string")throw new TypeError(`Invalid audio codec: ${e.audio.codec}. Must be a string.`);if(!Number.isInteger(e.audio.numberOfChannels)||e.audio.numberOfChannels<=0)throw new TypeError(`Invalid number of audio channels: ${e.audio.numberOfChannels}. Must be a positive integer.`);if(!Number.isInteger(e.audio.sampleRate)||e.audio.sampleRate<=0)throw new TypeError(`Invalid audio sample rate: ${e.audio.sampleRate}. Must be a positive integer.`);if(e.audio.bitDepth!==void 0&&(!Number.isInteger(e.audio.bitDepth)||e.audio.bitDepth<=0))throw new TypeError(`Invalid audio bit depth: ${e.audio.bitDepth}. Must be a positive integer.`)}if(e.subtitles&&typeof e.subtitles.codec!="string")throw new TypeError(`Invalid subtitles codec: ${e.subtitles.codec}. Must be a string.`);if(e.type!==void 0&&!["webm","matroska"].includes(e.type))throw new TypeError(`Invalid type: ${e.type}. Must be 'webm' or 'matroska'.`);if(e.firstTimestampBehavior&&!yi.includes(e.firstTimestampBehavior))throw new TypeError(`Invalid first timestamp behavior: ${e.firstTimestampBehavior}`);if(e.streaming!==void 0&&typeof e.streaming!="boolean")throw new TypeError(`Invalid streaming option: ${e.streaming}. Must be a boolean.`)},rt=new WeakSet,Kt=function(){t(this,h)instanceof E&&t(this,h).target.options.onHeader&&t(this,h).startTrackingWrites(),o(this,st,qt).call(this),t(this,f).streaming||o(this,ot,Lt).call(this),o(this,ht,Yt).call(this),o(this,at,It).call(this),o(this,nt,Gt).call(this),t(this,f).streaming||(o(this,Re,Et).call(this),o(this,Fe,At).call(this)),o(this,dt,Zt).call(this),o(this,N,ae).call(this)},st=new WeakSet,qt=function(){let e={id:440786851,data:[{id:17030,data:1},{id:17143,data:1},{id:17138,data:4},{id:17139,data:8},{id:17026,data:t(this,f).type??"webm"},{id:17031,data:2},{id:17029,data:2}]};t(this,h).writeEBML(e)},at=new WeakSet,It=function(){u(this,K,{id:236,size:4,data:new Uint8Array(ze)}),u(this,U,{id:236,size:4,data:new Uint8Array(ze)}),u(this,q,{id:236,size:4,data:new Uint8Array(ze)})},nt=new WeakSet,Gt=function(){u(this,M,{id:21936,data:[{id:21937,data:2},{id:21946,data:2},{id:21947,data:2},{id:21945,data:0}]})},ot=new WeakSet,Lt=function(){let e=new Uint8Array([28,83,187,107]),i=new Uint8Array([21,73,169,102]),r=new Uint8Array([22,84,174,107]),s={id:290298740,data:[{id:19899,data:[{id:21419,data:e},{id:21420,size:5,data:0}]},{id:19899,data:[{id:21419,data:i},{id:21420,size:5,data:0}]},{id:19899,data:[{id:21419,data:r},{id:21420,size:5,data:0}]}]};u(this,C,s)},ht=new WeakSet,Yt=function(){let e={id:17545,data:new O(0)};u(this,j,e);let i={id:357149030,data:[{id:2807729,data:1e6},{id:19840,data:Ht},{id:22337,data:Ht},t(this,f).streaming?null:e]};u(this,he,i)},Re=new WeakSet,Et=function(){let e={id:374648427,data:[]};u(this,B,e),t(this,f).video&&e.data.push({id:174,data:[{id:215,data:se},{id:29637,data:se},{id:131,data:wi},{id:134,data:t(this,f).video.codec},t(this,K),t(this,f).video.frameRate?{id:2352003,data:1e9/t(this,f).video.frameRate}:null,{id:224,data:[{id:176,data:t(this,f).video.width},{id:186,data:t(this,f).video.height},t(this,f).video.alpha?{id:21440,data:1}:null,t(this,M)]}]}),t(this,f).audio&&(u(this,U,t(this,f).streaming?t(this,U)||null:{id:236,size:4,data:new Uint8Array(ze)}),e.data.push({id:174,data:[{id:215,data:ve},{id:29637,data:ve},{id:131,data:pi},{id:134,data:t(this,f).audio.codec},t(this,U),{id:225,data:[{id:181,data:new J(t(this,f).audio.sampleRate)},{id:159,data:t(this,f).audio.numberOfChannels},t(this,f).audio.bitDepth?{id:25188,data:t(this,f).audio.bitDepth}:null]}]})),t(this,f).subtitles&&e.data.push({id:174,data:[{id:215,data:Ze},{id:29637,data:Ze},{id:131,data:gi},{id:134,data:t(this,f).subtitles.codec},t(this,q)]})},Fe=new WeakSet,At=function(){let e={id:408125543,size:t(this,f).streaming?-1:$t,data:[t(this,f).streaming?null:t(this,C),t(this,he),t(this,B)]};if(u(this,oe,e),t(this,h).writeEBML(e),t(this,h)instanceof E&&t(this,h).target.options.onHeader){let{data:i,start:r}=t(this,h).getTrackedWrites();t(this,h).target.options.onHeader(i,r)}},dt=new WeakSet,Zt=function(){u(this,I,{id:475249515,data:[]})},N=new WeakSet,ae=function(){t(this,h)instanceof ge&&t(this,h).flush()},_=new WeakSet,ne=function(){return t(this,h).dataOffsets.get(t(this,oe))},ut=new WeakSet,Qt=function(e){if(!!e.decoderConfig){if(e.decoderConfig.colorSpace){let i=e.decoderConfig.colorSpace;if(u(this,le,i),t(this,M).data=[{id:21937,data:{rgb:1,bt709:1,bt470bg:5,smpte170m:6}[i.matrix]},{id:21946,data:{bt709:1,smpte170m:6,"iec61966-2-1":13}[i.transfer]},{id:21947,data:{bt709:1,bt470bg:5,smpte170m:6}[i.primaries]},{id:21945,data:[1,2][Number(i.fullRange)]}],!t(this,f).streaming){let r=t(this,h).pos;t(this,h).seek(t(this,h).offsets.get(t(this,M))),t(this,h).writeEBML(t(this,M)),t(this,h).seek(r)}}e.decoderConfig.description&&(t(this,f).streaming?u(this,K,o(this,Q,Ve).call(this,e.decoderConfig.description)):o(this,be,Je).call(this,t(this,K),e.decoderConfig.description))}},lt=new WeakSet,Xt=function(e){if(e.type!=="key"||!t(this,le))return;let i=0;if(W(e.data,0,2)!==2)return;i+=2;let r=(W(e.data,i+1,i+2)<<1)+W(e.data,i+0,i+1);i+=2,r===3&&i++;let s=W(e.data,i+0,i+1);if(i++,s)return;let n=W(e.data,i+0,i+1);if(i++,n!==0)return;i+=2;let l=W(e.data,i+0,i+24);if(i+=24,l!==4817730)return;r>=2&&i++;let m={rgb:7,bt709:2,bt470bg:1,smpte170m:3}[t(this,le).matrix];Ft(e.data,i+0,i+3,m)},ce=new WeakSet,Qe=function(){let e=Math.min(t(this,f).video?t(this,Y):1/0,t(this,f).audio?t(this,Z):1/0),i=t(this,P);for(;i.length>0&&i[0].timestamp<=e;)o(this,k,A).call(this,i.shift(),!t(this,f).video&&!t(this,f).audio)},me=new WeakSet,Xe=function(e,i,r,s,n,l){let m=o(this,ft,Jt).call(this,r,s);return{data:e,additions:l,type:i,timestamp:m,duration:n,trackNumber:s}},ft=new WeakSet,Jt=function(e,i){let r=i===se?t(this,Y):i===ve?t(this,Z):t(this,De);if(i!==Ze){let s=i===se?t(this,de):t(this,ue);if(t(this,f).firstTimestampBehavior==="strict"&&r===-1&&e!==0)throw new Error(`The first chunk for your media track must have a timestamp of 0 (received ${e}). Non-zero first timestamps are often caused by directly piping frames or audio data from a MediaStreamTrack into the encoder. Their timestamps are typically relative to the age of the document, which is probably what you want. If you want to offset all timestamps of a track such that the first one is zero, set firstTimestampBehavior: 'offset' in the options. If you want to allow non-zero first timestamps, set firstTimestampBehavior: 'permissive'. -`);t(this,f).firstTimestampBehavior==="offset"&&(e-=s)}if(e=1e3;(!t(this,S)||s)&&h(this,bt,si).call(this,r);let n=r-t(this,G);if(n<0)return;if(n>=At)throw new Error(`Current Matroska cluster exceeded its maximum allowed length of ${At} milliseconds. In order to produce a correct WebM file, you must pass in a key frame at least every ${At} milliseconds.`);let c=new Uint8Array(4),b=new DataView(c.buffer);if(b.setUint8(0,128|e.trackNumber),b.setInt16(1,n,!1),e.duration===void 0&&!e.additions){b.setUint8(3,Number(e.type==="key")<<7);let p={id:163,data:[c,e.data]};t(this,u).writeEBML(p)}else{let p=Math.floor(e.duration/1e3),we={id:160,data:[{id:161,data:[c,e.data]},e.duration!==void 0?{id:155,data:p}:null,e.additions?{id:30113,data:e.additions}:null]};t(this,u).writeEBML(we)}l(this,L,Math.max(t(this,L),r))},Q=new WeakSet,De=function(e){return{id:25506,size:4,data:new Uint8Array(e)}},ce=new WeakSet,tt=function(e,i){let r=t(this,u).pos;t(this,u).seek(t(this,u).offsets.get(e));let s=2+4+i.byteLength,n=Ve-s;if(n<0){let o=i.byteLength+n;i instanceof ArrayBuffer?i=i.slice(0,o):i=i.buffer.slice(0,o),n=0}e=[h(this,Q,De).call(this,i),{id:236,size:4,data:new Uint8Array(n)}],t(this,u).writeEBML(e),t(this,u).seek(r)},bt=new WeakSet,si=function(e){t(this,S)&&!t(this,f).streaming&&h(this,Pe,vt).call(this),t(this,u)instanceof E&&t(this,u).target.options.onCluster&&t(this,u).startTrackingWrites(),l(this,S,{id:524531317,size:t(this,f).streaming?-1:It,data:[{id:231,data:e}]}),t(this,u).writeEBML(t(this,S)),l(this,G,e);let i=t(this,u).offsets.get(t(this,S))-t(this,_,se);t(this,I).data.push({id:187,data:[{id:179,data:e},t(this,f).video?{id:183,data:[{id:247,data:ie},{id:241,data:i}]}:null,t(this,f).audio?{id:183,data:[{id:247,data:ze},{id:241,data:i}]}:null]})},Pe=new WeakSet,vt=function(){let e=t(this,u).pos-t(this,u).dataOffsets.get(t(this,S)),i=t(this,u).pos;if(t(this,u).seek(t(this,u).offsets.get(t(this,S))+4),t(this,u).writeEBMLVarInt(e,It),t(this,u).seek(i),t(this,u)instanceof E&&t(this,u).target.options.onCluster){let{data:r,start:s}=t(this,u).getTrackedWrites();t(this,u).target.options.onCluster(r,s,t(this,G))}},me=new WeakSet,it=function(){if(t(this,ue))throw new Error("Cannot add new video or audio chunks after the file has been finalized.")};var Ne=/(?:(.+?)\n)?((?:\d{2}:)?\d{2}:\d{2}.\d{3})\s+-->\s+((?:\d{2}:)?\d{2}:\d{2}.\d{3})/g,xi=/^WEBVTT.*?\n{2}/,Si=/(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})/,ai=/<(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})>/g,zt=new TextEncoder,X,_e,Oe,We,He,be,wt,gt,ni,pt=class{constructor(e){a(this,be);a(this,gt);a(this,X,void 0);a(this,_e,void 0);a(this,Oe,!1);a(this,We,void 0);a(this,He,!1);l(this,X,e)}configure(e){if(e.codec!=="webvtt")throw new Error("Codec must be 'webvtt'.");l(this,_e,e)}encode(e){var r;if(!t(this,_e))throw new Error("Encoder not configured.");e=e.replace(`\r +`);t(this,f).firstTimestampBehavior==="offset"&&(e-=s)}if(e=1e3;(!t(this,x)||s)&&o(this,ct,ei).call(this,r);let n=r-t(this,G);if(n<0)return;if(n>=St)throw new Error(`Current Matroska cluster exceeded its maximum allowed length of ${St} milliseconds. In order to produce a correct WebM file, you must pass in a key frame at least every ${St} milliseconds.`);let m=new Uint8Array(4),p=new DataView(m.buffer);if(p.setUint8(0,128|e.trackNumber),p.setInt16(1,n,!1),e.duration===void 0&&!e.additions){p.setUint8(3,Number(e.type==="key")<<7);let b={id:163,data:[m,e.data]};t(this,h).writeEBML(b)}else{let b=Math.floor(e.duration/1e3),zt={id:160,data:[{id:161,data:[m,e.data]},e.duration!==void 0?{id:155,data:b}:null,e.additions?{id:30113,data:e.additions}:null]};t(this,h).writeEBML(zt)}u(this,L,Math.max(t(this,L),r))},Q=new WeakSet,Ve=function(e){return{id:25506,size:4,data:new Uint8Array(e)}},be=new WeakSet,Je=function(e,i){let r=t(this,h).pos;t(this,h).seek(t(this,h).offsets.get(e));let s=2+4+i.byteLength,n=ze-s;if(n<0){let l=i.byteLength+n;i instanceof ArrayBuffer?i=i.slice(0,l):i=i.buffer.slice(0,l),n=0}e=[o(this,Q,Ve).call(this,i),{id:236,size:4,data:new Uint8Array(n)}],t(this,h).writeEBML(e),t(this,h).seek(r)},ct=new WeakSet,ei=function(e){t(this,x)&&!t(this,f).streaming&&o(this,Me,Ut).call(this),t(this,h)instanceof E&&t(this,h).target.options.onCluster&&t(this,h).startTrackingWrites(),u(this,x,{id:524531317,size:t(this,f).streaming?-1:Bt,data:[{id:231,data:e}]}),t(this,h).writeEBML(t(this,x)),u(this,G,e);let i=t(this,h).offsets.get(t(this,x))-t(this,_,ne);t(this,I).data.push({id:187,data:[{id:179,data:e},t(this,f).video?{id:183,data:[{id:247,data:se},{id:241,data:i}]}:null,t(this,f).audio?{id:183,data:[{id:247,data:ve},{id:241,data:i}]}:null]})},Me=new WeakSet,Ut=function(){let e=t(this,h).pos-t(this,h).dataOffsets.get(t(this,x)),i=t(this,h).pos;if(t(this,h).seek(t(this,h).offsets.get(t(this,x))+4),t(this,h).writeEBMLVarInt(e,Bt),t(this,h).seek(i),t(this,h)instanceof E&&t(this,h).target.options.onCluster){let{data:r,start:s}=t(this,h).getTrackedWrites();t(this,h).target.options.onCluster(r,s,t(this,G))}},we=new WeakSet,et=function(){if(t(this,fe))throw new Error("Cannot add new video or audio chunks after the file has been finalized.")};var Pe=/(?:(.+?)\n)?((?:\d{2}:)?\d{2}:\d{2}.\d{3})\s+-->\s+((?:\d{2}:)?\d{2}:\d{2}.\d{3})/g,Ti=/^WEBVTT.*?\n{2}/,ki=/(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})/,ti=/<(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})>/g,vt=new TextEncoder,X,Ne,_e,Oe,We,pe,mt,wt,ii,bt=class{constructor(e){a(this,pe);a(this,wt);a(this,X,void 0);a(this,Ne,void 0);a(this,_e,!1);a(this,Oe,void 0);a(this,We,!1);u(this,X,e)}configure(e){if(e.codec!=="webvtt")throw new Error("Codec must be 'webvtt'.");u(this,Ne,e)}encode(e){if(!t(this,Ne))throw new Error("Encoder not configured.");e=e.replace(`\r `,` `).replace("\r",` -`),Ne.lastIndex=0;let i;if(!t(this,Oe)){if(!xi.test(e)){let n=new Error("WebVTT preamble incorrect.");throw t(this,X).error(n),n}i=Ne.exec(e);let s=e.slice(0,(r=i==null?void 0:i.index)!=null?r:e.length).trimEnd();if(!s){let n=new Error("No WebVTT preamble provided.");throw t(this,X).error(n),n}l(this,We,zt.encode(s)),l(this,Oe,!0),i&&(e=e.slice(i.index),Ne.lastIndex=0)}for(;i=Ne.exec(e);){let s=e.slice(0,i.index),n=i[1]||"",o=i.index+i[0].length,c=e.indexOf(` -`,o)+1,b=e.slice(o,c).trim(),p=e.indexOf(` +`),Pe.lastIndex=0;let i;if(!t(this,_e)){if(!Ti.test(e)){let s=new Error("WebVTT preamble incorrect.");throw t(this,X).error(s),s}i=Pe.exec(e);let r=e.slice(0,i?.index??e.length).trimEnd();if(!r){let s=new Error("No WebVTT preamble provided.");throw t(this,X).error(s),s}u(this,Oe,vt.encode(r)),u(this,_e,!0),i&&(e=e.slice(i.index),Pe.lastIndex=0)}for(;i=Pe.exec(e);){let r=e.slice(0,i.index),s=i[1]||"",n=i.index+i[0].length,l=e.indexOf(` +`,n)+1,m=e.slice(n,l).trim(),p=e.indexOf(` -`,o);p===-1&&(p=e.length);let we=h(this,be,wt).call(this,i[2]),oi=h(this,be,wt).call(this,i[3])-we,yt=e.slice(c,p),Vt=`${b} -${n} -${s}`;ai.lastIndex=0,yt=yt.replace(ai,di=>{let ui=h(this,be,wt).call(this,di.slice(1,-1))-we;return`<${h(this,gt,ni).call(this,ui)}>`}),e=e.slice(p).trimStart(),Ne.lastIndex=0;let hi={body:zt.encode(yt),additions:Vt.trim()===""?void 0:zt.encode(Vt),timestamp:we*1e3,duration:oi*1e3},Dt={};t(this,He)||(Dt.decoderConfig={description:t(this,We)},l(this,He,!0)),t(this,X).output(hi,Dt)}}};X=new WeakMap,_e=new WeakMap,Oe=new WeakMap,We=new WeakMap,He=new WeakMap,be=new WeakSet,wt=function(e){let i=Si.exec(e);if(!i)throw new Error("Expected match.");return 60*60*1e3*Number(i[1]||"0")+60*1e3*Number(i[2])+1e3*Number(i[3])+Number(i[4])},gt=new WeakSet,ni=function(e){let i=Math.floor(e/36e5),r=Math.floor(e%(60*60*1e3)/(60*1e3)),s=Math.floor(e%(60*1e3)/1e3),n=e%1e3;return i.toString().padStart(2,"0")+":"+r.toString().padStart(2,"0")+":"+s.toString().padStart(2,"0")+"."+n.toString().padStart(3,"0")};return wi(Ai);})(); +`,n);p===-1&&(p=e.length);let b=o(this,pe,mt).call(this,i[2]),ri=o(this,pe,mt).call(this,i[3])-b,pt=e.slice(l,p),Vt=`${m} +${s} +${r}`;ti.lastIndex=0,pt=pt.replace(ti,ai=>{let ni=o(this,pe,mt).call(this,ai.slice(1,-1))-b;return`<${o(this,wt,ii).call(this,ni)}>`}),e=e.slice(p).trimStart(),Pe.lastIndex=0;let si={body:vt.encode(pt),additions:Vt.trim()===""?void 0:vt.encode(Vt),timestamp:b*1e3,duration:ri*1e3},Dt={};t(this,We)||(Dt.decoderConfig={description:t(this,Oe)},u(this,We,!0)),t(this,X).output(si,Dt)}}};X=new WeakMap,Ne=new WeakMap,_e=new WeakMap,Oe=new WeakMap,We=new WeakMap,pe=new WeakSet,mt=function(e){let i=ki.exec(e);if(!i)throw new Error("Expected match.");return 60*60*1e3*Number(i[1]||"0")+60*1e3*Number(i[2])+1e3*Number(i[3])+Number(i[4])},wt=new WeakSet,ii=function(e){let i=Math.floor(e/36e5),r=Math.floor(e%(60*60*1e3)/(60*1e3)),s=Math.floor(e%(60*1e3)/1e3),n=e%1e3;return i.toString().padStart(2,"0")+":"+r.toString().padStart(2,"0")+":"+s.toString().padStart(2,"0")+"."+n.toString().padStart(3,"0")};return fi(Ci);})(); if (typeof module === "object" && typeof module.exports === "object") Object.assign(module.exports, WebMMuxer) diff --git a/build/webm-muxer.min.mjs b/build/webm-muxer.min.mjs index 6340314..7aa7ee1 100644 --- a/build/webm-muxer.min.mjs +++ b/build/webm-muxer.min.mjs @@ -1,13 +1,13 @@ -var di=Object.defineProperty;var Dt=Object.getOwnPropertySymbols;var ui=Object.prototype.hasOwnProperty,li=Object.prototype.propertyIsEnumerable;var w=Math.pow,Rt=(u,e,i)=>e in u?di(u,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):u[e]=i,Ft=(u,e)=>{for(var i in e||={})ui.call(e,i)&&Rt(u,i,e[i]);if(Dt)for(var i of Dt(e))li.call(e,i)&&Rt(u,i,e[i]);return u};var pt=(u,e,i)=>{if(!e.has(u))throw TypeError("Cannot "+i)};var t=(u,e,i)=>(pt(u,e,"read from private field"),i?i.call(u):e.get(u)),a=(u,e,i)=>{if(e.has(u))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(u):e.set(u,i)},l=(u,e,i,r)=>(pt(u,e,"write to private field"),r?r.call(u,i):e.set(u,i),i);var h=(u,e,i)=>(pt(u,e,"access private method"),i);var X=class{constructor(e){this.value=e}},O=class{constructor(e){this.value=e}};var gt=u=>u<1<<8?1:u<1<<16?2:u<1<<24?3:u{if(u<(1<<7)-1)return 1;if(u<(1<<14)-1)return 2;if(u<(1<<21)-1)return 3;if(u<(1<<28)-1)return 4;if(u{let r=0;for(let s=e;s>c;r<<=1,r|=b}return r},Pt=(u,e,i,r)=>{for(let s=e;s>i-s-1<>8),t(this,m).setUint8(r++,e);break;case 3:t(this,m).setUint8(r++,1<<5|e>>16),t(this,m).setUint8(r++,e>>8),t(this,m).setUint8(r++,e);break;case 4:t(this,m).setUint8(r++,1<<4|e>>24),t(this,m).setUint8(r++,e>>16),t(this,m).setUint8(r++,e>>8),t(this,m).setUint8(r++,e);break;case 5:t(this,m).setUint8(r++,1<<3|e/w(2,32)&7),t(this,m).setUint8(r++,e>>24),t(this,m).setUint8(r++,e>>16),t(this,m).setUint8(r++,e>>8),t(this,m).setUint8(r++,e);break;case 6:t(this,m).setUint8(r++,1<<2|e/w(2,40)&3),t(this,m).setUint8(r++,e/w(2,32)|0),t(this,m).setUint8(r++,e>>24),t(this,m).setUint8(r++,e>>16),t(this,m).setUint8(r++,e>>8),t(this,m).setUint8(r++,e);break;default:throw new Error("Bad EBML VINT size "+i)}this.write(t(this,A).subarray(0,r))}writeEBML(e){var i,r;if(e!==null)if(e instanceof Uint8Array)this.write(e);else if(Array.isArray(e))for(let s of e)this.writeEBML(s);else if(this.offsets.set(e,this.pos),h(this,ye,yt).call(this,e.id),Array.isArray(e.data)){let s=this.pos,n=e.size===-1?1:(i=e.size)!=null?i:4;e.size===-1?h(this,qe,Nt).call(this,255):this.seek(this.pos+n);let o=this.pos;if(this.dataOffsets.set(e,o),this.writeEBML(e.data),e.size!==-1){let c=this.pos-o,b=this.pos;this.seek(s),this.writeEBMLVarInt(c,n),this.seek(b)}}else if(typeof e.data=="number"){let s=(r=e.size)!=null?r:gt(e.data);this.writeEBMLVarInt(s),h(this,ye,yt).call(this,e.data,s)}else typeof e.data=="string"?(this.writeEBMLVarInt(e.data.length),h(this,Le,Wt).call(this,e.data)):e.data instanceof Uint8Array?(this.writeEBMLVarInt(e.data.byteLength,e.size),this.write(e.data)):e.data instanceof X?(this.writeEBMLVarInt(4),h(this,Ie,_t).call(this,e.data.value)):e.data instanceof O&&(this.writeEBMLVarInt(8),h(this,Ge,Ot).call(this,e.data.value))}};A=new WeakMap,m=new WeakMap,qe=new WeakSet,Nt=function(e){t(this,m).setUint8(0,e),this.write(t(this,A).subarray(0,1))},Ie=new WeakSet,_t=function(e){t(this,m).setFloat32(0,e,!1),this.write(t(this,A).subarray(0,4))},Ge=new WeakSet,Ot=function(e){t(this,m).setFloat64(0,e,!1),this.write(t(this,A))},ye=new WeakSet,yt=function(e,i=gt(e)){let r=0;switch(i){case 6:t(this,m).setUint8(r++,e/w(2,40)|0);case 5:t(this,m).setUint8(r++,e/w(2,32)|0);case 4:t(this,m).setUint8(r++,e>>24);case 3:t(this,m).setUint8(r++,e>>16);case 2:t(this,m).setUint8(r++,e>>8);case 1:t(this,m).setUint8(r++,e);break;default:throw new Error("Bad UINT size "+i)}this.write(t(this,A).subarray(0,r))},Le=new WeakSet,Wt=function(e){this.write(new Uint8Array(e.split("").map(i=>i.charCodeAt(0))))};var Te,D,ee,ke,Tt,je=class extends Be{constructor(i){super();a(this,ke);a(this,Te,void 0);a(this,D,new ArrayBuffer(w(2,16)));a(this,ee,new Uint8Array(t(this,D)));l(this,Te,i)}write(i){h(this,ke,Tt).call(this,this.pos+i.byteLength),t(this,ee).set(i,this.pos),this.pos+=i.byteLength}finalize(){h(this,ke,Tt).call(this,this.pos),t(this,Te).buffer=t(this,D).slice(0,this.pos)}};Te=new WeakMap,D=new WeakMap,ee=new WeakMap,ke=new WeakSet,Tt=function(i){let r=t(this,D).byteLength;for(;ro.start-c.start);i.push({start:r[0].start,size:r[0].data.byteLength});for(let o=1;op.start<=r&&rci){for(let p=0;p=i.written[o+1].start;)i.written[o].end=Math.max(i.written[o].end,i.written[o+1].end),i.written.splice(o+1,1)},Ze=new WeakSet,$t=function(i){let s={start:Math.floor(i/t(this,y))*t(this,y),data:new Uint8Array(t(this,y)),written:[],shouldFlush:!1};return t(this,g).push(s),t(this,g).sort((n,o)=>n.start-o.start),t(this,g).indexOf(s)},te=new WeakSet,$e=function(i=!1){var r,s;for(let n=0;ne.stream.write({type:"write",data:s,position:n}),chunked:!0,chunkSize:(r=e.options)==null?void 0:r.chunkSize}),i)}};var ie=1,Ue=2,Qe=3,mi=1,bi=2,wi=17,Ct=w(2,15),ve=w(2,12),Bt="https://github.com/Vanilagy/webm-muxer",jt=6,Kt=5,pi=["strict","offset","permissive"],f,d,ae,ne,x,$,B,M,j,v,K,q,S,I,G,z,V,P,oe,he,L,Y,Ve,de,ue,it,qt,rt,It,st,Gt,at,Lt,nt,Yt,ot,Zt,ht,Qt,De,St,Re,At,dt,Xt,N,re,_,se,ut,Jt,lt,ei,le,Xe,fe,Je,ft,ti,C,U,Z,ze,ce,et,ct,ii,Fe,Et,me,tt,xt=class{constructor(e){a(this,it);a(this,rt);a(this,st);a(this,at);a(this,nt);a(this,ot);a(this,ht);a(this,De);a(this,Re);a(this,dt);a(this,N);a(this,_);a(this,ut);a(this,lt);a(this,le);a(this,fe);a(this,ft);a(this,C);a(this,Z);a(this,ce);a(this,ct);a(this,Fe);a(this,me);a(this,f,void 0);a(this,d,void 0);a(this,ae,void 0);a(this,ne,void 0);a(this,x,void 0);a(this,$,void 0);a(this,B,void 0);a(this,M,void 0);a(this,j,void 0);a(this,v,void 0);a(this,K,void 0);a(this,q,void 0);a(this,S,void 0);a(this,I,void 0);a(this,G,0);a(this,z,[]);a(this,V,[]);a(this,P,[]);a(this,oe,void 0);a(this,he,void 0);a(this,L,-1);a(this,Y,-1);a(this,Ve,-1);a(this,de,void 0);a(this,ue,!1);var r;h(this,it,qt).call(this,e),l(this,f,Ft({type:"webm",firstTimestampBehavior:"strict"},e)),this.target=e.target;let i=!!t(this,f).streaming;if(e.target instanceof We)l(this,d,new je(e.target));else if(e.target instanceof J)l(this,d,(r=e.target.options)!=null&&r.chunked?new ge(e.target,i):new pe(e.target,i));else if(e.target instanceof He)l(this,d,new Ke(e.target,i));else throw new Error(`Invalid target: ${e.target}`);h(this,rt,It).call(this)}addVideoChunk(e,i,r){if(!(e instanceof EncodedVideoChunk))throw new TypeError("addVideoChunk's first argument (chunk) must be of type EncodedVideoChunk.");if(i&&typeof i!="object")throw new TypeError("addVideoChunk's second argument (meta), when provided, must be an object.");if(r!==void 0&&(!Number.isFinite(r)||r<0))throw new TypeError("addVideoChunk's third argument (timestamp), when provided, must be a non-negative real number.");let s=new Uint8Array(e.byteLength);e.copyTo(s),this.addVideoChunkRaw(s,e.type,r!=null?r:e.timestamp,i)}addVideoChunkRaw(e,i,r,s){if(!(e instanceof Uint8Array))throw new TypeError("addVideoChunkRaw's first argument (data) must be an instance of Uint8Array.");if(i!=="key"&&i!=="delta")throw new TypeError("addVideoChunkRaw's second argument (type) must be either 'key' or 'delta'.");if(!Number.isFinite(r)||r<0)throw new TypeError("addVideoChunkRaw's third argument (timestamp) must be a non-negative real number.");if(s&&typeof s!="object")throw new TypeError("addVideoChunkRaw's fourth argument (meta), when provided, must be an object.");if(h(this,me,tt).call(this),!t(this,f).video)throw new Error("No video track declared.");t(this,oe)===void 0&&l(this,oe,r),s&&h(this,ut,Jt).call(this,s);let n=h(this,fe,Je).call(this,e,i,r,ie);for(t(this,f).video.codec==="V_VP9"&&h(this,lt,ei).call(this,n),l(this,L,n.timestamp);t(this,V).length>0&&t(this,V)[0].timestamp<=n.timestamp;){let o=t(this,V).shift();h(this,C,U).call(this,o,!1)}!t(this,f).audio||n.timestamp<=t(this,Y)?h(this,C,U).call(this,n,!0):t(this,z).push(n),h(this,le,Xe).call(this),h(this,N,re).call(this)}addAudioChunk(e,i,r){if(!(e instanceof EncodedAudioChunk))throw new TypeError("addAudioChunk's first argument (chunk) must be of type EncodedAudioChunk.");if(i&&typeof i!="object")throw new TypeError("addAudioChunk's second argument (meta), when provided, must be an object.");if(r!==void 0&&(!Number.isFinite(r)||r<0))throw new TypeError("addAudioChunk's third argument (timestamp), when provided, must be a non-negative real number.");let s=new Uint8Array(e.byteLength);e.copyTo(s),this.addAudioChunkRaw(s,e.type,r!=null?r:e.timestamp,i)}addAudioChunkRaw(e,i,r,s){if(!(e instanceof Uint8Array))throw new TypeError("addAudioChunkRaw's first argument (data) must be an instance of Uint8Array.");if(i!=="key"&&i!=="delta")throw new TypeError("addAudioChunkRaw's second argument (type) must be either 'key' or 'delta'.");if(!Number.isFinite(r)||r<0)throw new TypeError("addAudioChunkRaw's third argument (timestamp) must be a non-negative real number.");if(s&&typeof s!="object")throw new TypeError("addAudioChunkRaw's fourth argument (meta), when provided, must be an object.");if(h(this,me,tt).call(this),!t(this,f).audio)throw new Error("No audio track declared.");t(this,he)===void 0&&l(this,he,r),s!=null&&s.decoderConfig&&(t(this,f).streaming?l(this,v,h(this,Z,ze).call(this,s.decoderConfig.description)):h(this,ce,et).call(this,t(this,v),s.decoderConfig.description));let n=h(this,fe,Je).call(this,e,i,r,Ue);for(l(this,Y,n.timestamp);t(this,z).length>0&&t(this,z)[0].timestamp<=n.timestamp;){let o=t(this,z).shift();h(this,C,U).call(this,o,!0)}!t(this,f).video||n.timestamp<=t(this,L)?h(this,C,U).call(this,n,!t(this,f).video):t(this,V).push(n),h(this,le,Xe).call(this),h(this,N,re).call(this)}addSubtitleChunk(e,i,r){if(typeof e!="object"||!e)throw new TypeError("addSubtitleChunk's first argument (chunk) must be an object.");if(!(e.body instanceof Uint8Array))throw new TypeError("body must be an instance of Uint8Array.");if(!Number.isFinite(e.timestamp)||e.timestamp<0)throw new TypeError("timestamp must be a non-negative real number.");if(!Number.isFinite(e.duration)||e.duration<0)throw new TypeError("duration must be a non-negative real number.");if(e.additions&&!(e.additions instanceof Uint8Array))throw new TypeError("additions, when present, must be an instance of Uint8Array.");if(typeof i!="object")throw new TypeError("addSubtitleChunk's second argument (meta) must be an object.");if(h(this,me,tt).call(this),!t(this,f).subtitles)throw new Error("No subtitle track declared.");i!=null&&i.decoderConfig&&(t(this,f).streaming?l(this,K,h(this,Z,ze).call(this,i.decoderConfig.description)):h(this,ce,et).call(this,t(this,K),i.decoderConfig.description));let s=h(this,fe,Je).call(this,e.body,"key",r!=null?r:e.timestamp,Qe,e.duration,e.additions);l(this,Ve,s.timestamp),t(this,P).push(s),h(this,le,Xe).call(this),h(this,N,re).call(this)}finalize(){if(t(this,ue))throw new Error("Cannot finalize a muxer more than once.");for(;t(this,z).length>0;)h(this,C,U).call(this,t(this,z).shift(),!0);for(;t(this,V).length>0;)h(this,C,U).call(this,t(this,V).shift(),!0);for(;t(this,P).length>0&&t(this,P)[0].timestamp<=t(this,G);)h(this,C,U).call(this,t(this,P).shift(),!1);if(t(this,f).streaming||h(this,Fe,Et).call(this),t(this,d).writeEBML(t(this,q)),!t(this,f).streaming){let e=t(this,d).pos,i=t(this,d).pos-t(this,_,se);t(this,d).seek(t(this,d).offsets.get(t(this,ae))+4),t(this,d).writeEBMLVarInt(i,jt),t(this,B).data=new O(t(this,G)),t(this,d).seek(t(this,d).offsets.get(t(this,B))),t(this,d).writeEBML(t(this,B)),t(this,x).data[0].data[1].data=t(this,d).offsets.get(t(this,q))-t(this,_,se),t(this,x).data[1].data[1].data=t(this,d).offsets.get(t(this,ne))-t(this,_,se),t(this,x).data[2].data[1].data=t(this,d).offsets.get(t(this,$))-t(this,_,se),t(this,d).seek(t(this,d).offsets.get(t(this,x))),t(this,d).writeEBML(t(this,x)),t(this,d).seek(e)}h(this,N,re).call(this),t(this,d).finalize(),l(this,ue,!0)}};f=new WeakMap,d=new WeakMap,ae=new WeakMap,ne=new WeakMap,x=new WeakMap,$=new WeakMap,B=new WeakMap,M=new WeakMap,j=new WeakMap,v=new WeakMap,K=new WeakMap,q=new WeakMap,S=new WeakMap,I=new WeakMap,G=new WeakMap,z=new WeakMap,V=new WeakMap,P=new WeakMap,oe=new WeakMap,he=new WeakMap,L=new WeakMap,Y=new WeakMap,Ve=new WeakMap,de=new WeakMap,ue=new WeakMap,it=new WeakSet,qt=function(e){if(typeof e!="object")throw new TypeError("The muxer requires an options object to be passed to its constructor.");if(e.video){if(typeof e.video.codec!="string")throw new TypeError(`Invalid video codec: ${e.video.codec}. Must be a string.`);if(!Number.isInteger(e.video.width)||e.video.width<=0)throw new TypeError(`Invalid video width: ${e.video.width}. Must be a positive integer.`);if(!Number.isInteger(e.video.height)||e.video.height<=0)throw new TypeError(`Invalid video height: ${e.video.height}. Must be a positive integer.`);if(e.video.frameRate!==void 0&&(!Number.isFinite(e.video.frameRate)||e.video.frameRate<=0))throw new TypeError(`Invalid video frame rate: ${e.video.frameRate}. Must be a positive number.`);if(e.video.alpha!==void 0&&typeof e.video.alpha!="boolean")throw new TypeError(`Invalid video alpha: ${e.video.alpha}. Must be a boolean.`)}if(e.audio){if(typeof e.audio.codec!="string")throw new TypeError(`Invalid audio codec: ${e.audio.codec}. Must be a string.`);if(!Number.isInteger(e.audio.numberOfChannels)||e.audio.numberOfChannels<=0)throw new TypeError(`Invalid number of audio channels: ${e.audio.numberOfChannels}. Must be a positive integer.`);if(!Number.isInteger(e.audio.sampleRate)||e.audio.sampleRate<=0)throw new TypeError(`Invalid audio sample rate: ${e.audio.sampleRate}. Must be a positive integer.`);if(e.audio.bitDepth!==void 0&&(!Number.isInteger(e.audio.bitDepth)||e.audio.bitDepth<=0))throw new TypeError(`Invalid audio bit depth: ${e.audio.bitDepth}. Must be a positive integer.`)}if(e.subtitles&&typeof e.subtitles.codec!="string")throw new TypeError(`Invalid subtitles codec: ${e.subtitles.codec}. Must be a string.`);if(e.type!==void 0&&!["webm","matroska"].includes(e.type))throw new TypeError(`Invalid type: ${e.type}. Must be 'webm' or 'matroska'.`);if(e.firstTimestampBehavior&&!pi.includes(e.firstTimestampBehavior))throw new TypeError(`Invalid first timestamp behavior: ${e.firstTimestampBehavior}`);if(e.streaming!==void 0&&typeof e.streaming!="boolean")throw new TypeError(`Invalid streaming option: ${e.streaming}. Must be a boolean.`)},rt=new WeakSet,It=function(){t(this,d)instanceof E&&t(this,d).target.options.onHeader&&t(this,d).startTrackingWrites(),h(this,st,Gt).call(this),t(this,f).streaming||h(this,ot,Zt).call(this),h(this,ht,Qt).call(this),h(this,at,Lt).call(this),h(this,nt,Yt).call(this),t(this,f).streaming||(h(this,De,St).call(this),h(this,Re,At).call(this)),h(this,dt,Xt).call(this),h(this,N,re).call(this)},st=new WeakSet,Gt=function(){var i;let e={id:440786851,data:[{id:17030,data:1},{id:17143,data:1},{id:17138,data:4},{id:17139,data:8},{id:17026,data:(i=t(this,f).type)!=null?i:"webm"},{id:17031,data:2},{id:17029,data:2}]};t(this,d).writeEBML(e)},at=new WeakSet,Lt=function(){l(this,j,{id:236,size:4,data:new Uint8Array(ve)}),l(this,v,{id:236,size:4,data:new Uint8Array(ve)}),l(this,K,{id:236,size:4,data:new Uint8Array(ve)})},nt=new WeakSet,Yt=function(){l(this,M,{id:21936,data:[{id:21937,data:2},{id:21946,data:2},{id:21947,data:2},{id:21945,data:0}]})},ot=new WeakSet,Zt=function(){let e=new Uint8Array([28,83,187,107]),i=new Uint8Array([21,73,169,102]),r=new Uint8Array([22,84,174,107]),s={id:290298740,data:[{id:19899,data:[{id:21419,data:e},{id:21420,size:5,data:0}]},{id:19899,data:[{id:21419,data:i},{id:21420,size:5,data:0}]},{id:19899,data:[{id:21419,data:r},{id:21420,size:5,data:0}]}]};l(this,x,s)},ht=new WeakSet,Qt=function(){let e={id:17545,data:new O(0)};l(this,B,e);let i={id:357149030,data:[{id:2807729,data:1e6},{id:19840,data:Bt},{id:22337,data:Bt},t(this,f).streaming?null:e]};l(this,ne,i)},De=new WeakSet,St=function(){let e={id:374648427,data:[]};l(this,$,e),t(this,f).video&&e.data.push({id:174,data:[{id:215,data:ie},{id:29637,data:ie},{id:131,data:mi},{id:134,data:t(this,f).video.codec},t(this,j),t(this,f).video.frameRate?{id:2352003,data:1e9/t(this,f).video.frameRate}:null,{id:224,data:[{id:176,data:t(this,f).video.width},{id:186,data:t(this,f).video.height},t(this,f).video.alpha?{id:21440,data:1}:null,t(this,M)]}]}),t(this,f).audio&&(l(this,v,t(this,f).streaming?t(this,v)||null:{id:236,size:4,data:new Uint8Array(ve)}),e.data.push({id:174,data:[{id:215,data:Ue},{id:29637,data:Ue},{id:131,data:bi},{id:134,data:t(this,f).audio.codec},t(this,v),{id:225,data:[{id:181,data:new X(t(this,f).audio.sampleRate)},{id:159,data:t(this,f).audio.numberOfChannels},t(this,f).audio.bitDepth?{id:25188,data:t(this,f).audio.bitDepth}:null]}]})),t(this,f).subtitles&&e.data.push({id:174,data:[{id:215,data:Qe},{id:29637,data:Qe},{id:131,data:wi},{id:134,data:t(this,f).subtitles.codec},t(this,K)]})},Re=new WeakSet,At=function(){let e={id:408125543,size:t(this,f).streaming?-1:jt,data:[t(this,f).streaming?null:t(this,x),t(this,ne),t(this,$)]};if(l(this,ae,e),t(this,d).writeEBML(e),t(this,d)instanceof E&&t(this,d).target.options.onHeader){let{data:i,start:r}=t(this,d).getTrackedWrites();t(this,d).target.options.onHeader(i,r)}},dt=new WeakSet,Xt=function(){l(this,q,{id:475249515,data:[]})},N=new WeakSet,re=function(){t(this,d)instanceof pe&&t(this,d).flush()},_=new WeakSet,se=function(){return t(this,d).dataOffsets.get(t(this,ae))},ut=new WeakSet,Jt=function(e){if(!!e.decoderConfig){if(e.decoderConfig.colorSpace){let i=e.decoderConfig.colorSpace;if(l(this,de,i),t(this,M).data=[{id:21937,data:{rgb:1,bt709:1,bt470bg:5,smpte170m:6}[i.matrix]},{id:21946,data:{bt709:1,smpte170m:6,"iec61966-2-1":13}[i.transfer]},{id:21947,data:{bt709:1,bt470bg:5,smpte170m:6}[i.primaries]},{id:21945,data:[1,2][Number(i.fullRange)]}],!t(this,f).streaming){let r=t(this,d).pos;t(this,d).seek(t(this,d).offsets.get(t(this,M))),t(this,d).writeEBML(t(this,M)),t(this,d).seek(r)}}e.decoderConfig.description&&(t(this,f).streaming?l(this,j,h(this,Z,ze).call(this,e.decoderConfig.description)):h(this,ce,et).call(this,t(this,j),e.decoderConfig.description))}},lt=new WeakSet,ei=function(e){if(e.type!=="key"||!t(this,de))return;let i=0;if(W(e.data,0,2)!==2)return;i+=2;let r=(W(e.data,i+1,i+2)<<1)+W(e.data,i+0,i+1);i+=2,r===3&&i++;let s=W(e.data,i+0,i+1);if(i++,s)return;let n=W(e.data,i+0,i+1);if(i++,n!==0)return;i+=2;let o=W(e.data,i+0,i+24);if(i+=24,o!==4817730)return;r>=2&&i++;let c={rgb:7,bt709:2,bt470bg:1,smpte170m:3}[t(this,de).matrix];Pt(e.data,i+0,i+3,c)},le=new WeakSet,Xe=function(){let e=Math.min(t(this,f).video?t(this,L):1/0,t(this,f).audio?t(this,Y):1/0),i=t(this,P);for(;i.length>0&&i[0].timestamp<=e;)h(this,C,U).call(this,i.shift(),!t(this,f).video&&!t(this,f).audio)},fe=new WeakSet,Je=function(e,i,r,s,n,o){let c=h(this,ft,ti).call(this,r,s);return{data:e,additions:o,type:i,timestamp:c,duration:n,trackNumber:s}},ft=new WeakSet,ti=function(e,i){let r=i===ie?t(this,L):i===Ue?t(this,Y):t(this,Ve);if(i!==Qe){let s=i===ie?t(this,oe):t(this,he);if(t(this,f).firstTimestampBehavior==="strict"&&r===-1&&e!==0)throw new Error(`The first chunk for your media track must have a timestamp of 0 (received ${e}). Non-zero first timestamps are often caused by directly piping frames or audio data from a MediaStreamTrack into the encoder. Their timestamps are typically relative to the age of the document, which is probably what you want. +var wt=(f,e,i)=>{if(!e.has(f))throw TypeError("Cannot "+i)};var t=(f,e,i)=>(wt(f,e,"read from private field"),i?i.call(f):e.get(f)),a=(f,e,i)=>{if(e.has(f))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(f):e.set(f,i)},d=(f,e,i,r)=>(wt(f,e,"write to private field"),r?r.call(f,i):e.set(f,i),i);var o=(f,e,i)=>(wt(f,e,"access private method"),i);var J=class{constructor(e){this.value=e}},_=class{constructor(e){this.value=e}};var pt=f=>f<1<<8?1:f<1<<16?2:f<1<<24?3:f<2**32?4:f<2**40?5:6,Dt=f=>{if(f<(1<<7)-1)return 1;if(f<(1<<14)-1)return 2;if(f<(1<<21)-1)return 3;if(f<(1<<28)-1)return 4;if(f<2**35-1)return 5;if(f<2**42-1)return 6;throw new Error("EBML VINT size not supported "+f)};var O=(f,e,i)=>{let r=0;for(let s=e;s>m;r<<=1,r|=p}return r},Rt=(f,e,i,r)=>{for(let s=e;s>i-s-1<>8),t(this,c).setUint8(r++,e);break;case 3:t(this,c).setUint8(r++,1<<5|e>>16),t(this,c).setUint8(r++,e>>8),t(this,c).setUint8(r++,e);break;case 4:t(this,c).setUint8(r++,1<<4|e>>24),t(this,c).setUint8(r++,e>>16),t(this,c).setUint8(r++,e>>8),t(this,c).setUint8(r++,e);break;case 5:t(this,c).setUint8(r++,1<<3|e/2**32&7),t(this,c).setUint8(r++,e>>24),t(this,c).setUint8(r++,e>>16),t(this,c).setUint8(r++,e>>8),t(this,c).setUint8(r++,e);break;case 6:t(this,c).setUint8(r++,1<<2|e/2**40&3),t(this,c).setUint8(r++,e/2**32|0),t(this,c).setUint8(r++,e>>24),t(this,c).setUint8(r++,e>>16),t(this,c).setUint8(r++,e>>8),t(this,c).setUint8(r++,e);break;default:throw new Error("Bad EBML VINT size "+i)}this.write(t(this,S).subarray(0,r))}writeEBML(e){if(e!==null)if(e instanceof Uint8Array)this.write(e);else if(Array.isArray(e))for(let i of e)this.writeEBML(i);else if(this.offsets.set(e,this.pos),o(this,Te,gt).call(this,e.id),Array.isArray(e.data)){let i=this.pos,r=e.size===-1?1:e.size??4;e.size===-1?o(this,Ke,Ft).call(this,255):this.seek(this.pos+r);let s=this.pos;if(this.dataOffsets.set(e,s),this.writeEBML(e.data),e.size!==-1){let n=this.pos-s,u=this.pos;this.seek(i),this.writeEBMLVarInt(n,r),this.seek(u)}}else if(typeof e.data=="number"){let i=e.size??pt(e.data);this.writeEBMLVarInt(i),o(this,Te,gt).call(this,e.data,i)}else typeof e.data=="string"?(this.writeEBMLVarInt(e.data.length),o(this,Ge,Nt).call(this,e.data)):e.data instanceof Uint8Array?(this.writeEBMLVarInt(e.data.byteLength,e.size),this.write(e.data)):e.data instanceof J?(this.writeEBMLVarInt(4),o(this,qe,Mt).call(this,e.data.value)):e.data instanceof _&&(this.writeEBMLVarInt(8),o(this,Ie,Pt).call(this,e.data.value))}};S=new WeakMap,c=new WeakMap,Ke=new WeakSet,Ft=function(e){t(this,c).setUint8(0,e),this.write(t(this,S).subarray(0,1))},qe=new WeakSet,Mt=function(e){t(this,c).setFloat32(0,e,!1),this.write(t(this,S).subarray(0,4))},Ie=new WeakSet,Pt=function(e){t(this,c).setFloat64(0,e,!1),this.write(t(this,S))},Te=new WeakSet,gt=function(e,i=pt(e)){let r=0;switch(i){case 6:t(this,c).setUint8(r++,e/2**40|0);case 5:t(this,c).setUint8(r++,e/2**32|0);case 4:t(this,c).setUint8(r++,e>>24);case 3:t(this,c).setUint8(r++,e>>16);case 2:t(this,c).setUint8(r++,e>>8);case 1:t(this,c).setUint8(r++,e);break;default:throw new Error("Bad UINT size "+i)}this.write(t(this,S).subarray(0,r))},Ge=new WeakSet,Nt=function(e){this.write(new Uint8Array(e.split("").map(i=>i.charCodeAt(0))))};var ke,V,ee,Ce,yt,Be=class extends $e{constructor(i){super();a(this,Ce);a(this,ke,void 0);a(this,V,new ArrayBuffer(2**16));a(this,ee,new Uint8Array(t(this,V)));d(this,ke,i)}write(i){o(this,Ce,yt).call(this,this.pos+i.byteLength),t(this,ee).set(i,this.pos),this.pos+=i.byteLength}finalize(){o(this,Ce,yt).call(this,this.pos),t(this,ke).buffer=t(this,V).slice(0,this.pos)}};ke=new WeakMap,V=new WeakMap,ee=new WeakMap,Ce=new WeakSet,yt=function(i){let r=t(this,V).byteLength;for(;rs.start-n.start);i.push({start:r[0].start,size:r[0].data.byteLength});for(let s=1;sb.start<=r&&rhi){for(let b=0;b=i.written[u+1].start;)i.written[u].end=Math.max(i.written[u].end,i.written[u+1].end),i.written.splice(u+1,1)},Ye=new WeakSet,Ot=function(i){let s={start:Math.floor(i/t(this,g))*t(this,g),data:new Uint8Array(t(this,g)),written:[],shouldFlush:!1};return t(this,w).push(s),t(this,w).sort((n,u)=>n.start-u.start),t(this,w).indexOf(s)},te=new WeakSet,He=function(i=!1){for(let r=0;re.stream.write({type:"write",data:r,position:s}),chunked:!0,chunkSize:e.options?.chunkSize}),i)}};var ie=1,ve=2,Ze=3,di=1,ui=2,li=17,kt=2**15,ze=2**12,Wt="https://github.com/Vanilagy/webm-muxer",Ht=6,$t=5,fi=["strict","offset","permissive"],l,h,ae,ne,C,B,j,F,K,U,q,I,x,G,L,v,z,M,oe,he,Y,Z,De,de,ue,tt,Bt,it,jt,rt,Kt,st,qt,at,It,nt,Gt,ot,Lt,Re,xt,Fe,St,ht,Yt,P,re,N,se,dt,Zt,ut,Qt,le,Qe,fe,Xe,lt,Xt,k,A,Q,Ve,ce,Je,ft,Jt,Me,Et,me,et,Ct=class{constructor(e){a(this,tt);a(this,it);a(this,rt);a(this,st);a(this,at);a(this,nt);a(this,ot);a(this,Re);a(this,Fe);a(this,ht);a(this,P);a(this,N);a(this,dt);a(this,ut);a(this,le);a(this,fe);a(this,lt);a(this,k);a(this,Q);a(this,ce);a(this,ft);a(this,Me);a(this,me);a(this,l,void 0);a(this,h,void 0);a(this,ae,void 0);a(this,ne,void 0);a(this,C,void 0);a(this,B,void 0);a(this,j,void 0);a(this,F,void 0);a(this,K,void 0);a(this,U,void 0);a(this,q,void 0);a(this,I,void 0);a(this,x,void 0);a(this,G,void 0);a(this,L,0);a(this,v,[]);a(this,z,[]);a(this,M,[]);a(this,oe,void 0);a(this,he,void 0);a(this,Y,-1);a(this,Z,-1);a(this,De,-1);a(this,de,void 0);a(this,ue,!1);o(this,tt,Bt).call(this,e),d(this,l,{type:"webm",firstTimestampBehavior:"strict",...e}),this.target=e.target;let i=!!t(this,l).streaming;if(e.target instanceof we)d(this,h,new Be(e.target));else if(e.target instanceof H)d(this,h,e.target.options?.chunked?new ye(e.target,i):new ge(e.target,i));else if(e.target instanceof pe)d(this,h,new je(e.target,i));else throw new Error(`Invalid target: ${e.target}`);o(this,it,jt).call(this)}addVideoChunk(e,i,r){if(!(e instanceof EncodedVideoChunk))throw new TypeError("addVideoChunk's first argument (chunk) must be of type EncodedVideoChunk.");if(i&&typeof i!="object")throw new TypeError("addVideoChunk's second argument (meta), when provided, must be an object.");if(r!==void 0&&(!Number.isFinite(r)||r<0))throw new TypeError("addVideoChunk's third argument (timestamp), when provided, must be a non-negative real number.");let s=new Uint8Array(e.byteLength);e.copyTo(s),this.addVideoChunkRaw(s,e.type,r??e.timestamp,i)}addVideoChunkRaw(e,i,r,s){if(!(e instanceof Uint8Array))throw new TypeError("addVideoChunkRaw's first argument (data) must be an instance of Uint8Array.");if(i!=="key"&&i!=="delta")throw new TypeError("addVideoChunkRaw's second argument (type) must be either 'key' or 'delta'.");if(!Number.isFinite(r)||r<0)throw new TypeError("addVideoChunkRaw's third argument (timestamp) must be a non-negative real number.");if(s&&typeof s!="object")throw new TypeError("addVideoChunkRaw's fourth argument (meta), when provided, must be an object.");if(o(this,me,et).call(this),!t(this,l).video)throw new Error("No video track declared.");t(this,oe)===void 0&&d(this,oe,r),s&&o(this,dt,Zt).call(this,s);let n=o(this,fe,Xe).call(this,e,i,r,ie);for(t(this,l).video.codec==="V_VP9"&&o(this,ut,Qt).call(this,n),d(this,Y,n.timestamp);t(this,z).length>0&&t(this,z)[0].timestamp<=n.timestamp;){let u=t(this,z).shift();o(this,k,A).call(this,u,!1)}!t(this,l).audio||n.timestamp<=t(this,Z)?o(this,k,A).call(this,n,!0):t(this,v).push(n),o(this,le,Qe).call(this),o(this,P,re).call(this)}addAudioChunk(e,i,r){if(!(e instanceof EncodedAudioChunk))throw new TypeError("addAudioChunk's first argument (chunk) must be of type EncodedAudioChunk.");if(i&&typeof i!="object")throw new TypeError("addAudioChunk's second argument (meta), when provided, must be an object.");if(r!==void 0&&(!Number.isFinite(r)||r<0))throw new TypeError("addAudioChunk's third argument (timestamp), when provided, must be a non-negative real number.");let s=new Uint8Array(e.byteLength);e.copyTo(s),this.addAudioChunkRaw(s,e.type,r??e.timestamp,i)}addAudioChunkRaw(e,i,r,s){if(!(e instanceof Uint8Array))throw new TypeError("addAudioChunkRaw's first argument (data) must be an instance of Uint8Array.");if(i!=="key"&&i!=="delta")throw new TypeError("addAudioChunkRaw's second argument (type) must be either 'key' or 'delta'.");if(!Number.isFinite(r)||r<0)throw new TypeError("addAudioChunkRaw's third argument (timestamp) must be a non-negative real number.");if(s&&typeof s!="object")throw new TypeError("addAudioChunkRaw's fourth argument (meta), when provided, must be an object.");if(o(this,me,et).call(this),!t(this,l).audio)throw new Error("No audio track declared.");t(this,he)===void 0&&d(this,he,r),s?.decoderConfig&&(t(this,l).streaming?d(this,U,o(this,Q,Ve).call(this,s.decoderConfig.description)):o(this,ce,Je).call(this,t(this,U),s.decoderConfig.description));let n=o(this,fe,Xe).call(this,e,i,r,ve);for(d(this,Z,n.timestamp);t(this,v).length>0&&t(this,v)[0].timestamp<=n.timestamp;){let u=t(this,v).shift();o(this,k,A).call(this,u,!0)}!t(this,l).video||n.timestamp<=t(this,Y)?o(this,k,A).call(this,n,!t(this,l).video):t(this,z).push(n),o(this,le,Qe).call(this),o(this,P,re).call(this)}addSubtitleChunk(e,i,r){if(typeof e!="object"||!e)throw new TypeError("addSubtitleChunk's first argument (chunk) must be an object.");if(!(e.body instanceof Uint8Array))throw new TypeError("body must be an instance of Uint8Array.");if(!Number.isFinite(e.timestamp)||e.timestamp<0)throw new TypeError("timestamp must be a non-negative real number.");if(!Number.isFinite(e.duration)||e.duration<0)throw new TypeError("duration must be a non-negative real number.");if(e.additions&&!(e.additions instanceof Uint8Array))throw new TypeError("additions, when present, must be an instance of Uint8Array.");if(typeof i!="object")throw new TypeError("addSubtitleChunk's second argument (meta) must be an object.");if(o(this,me,et).call(this),!t(this,l).subtitles)throw new Error("No subtitle track declared.");i?.decoderConfig&&(t(this,l).streaming?d(this,q,o(this,Q,Ve).call(this,i.decoderConfig.description)):o(this,ce,Je).call(this,t(this,q),i.decoderConfig.description));let s=o(this,fe,Xe).call(this,e.body,"key",r??e.timestamp,Ze,e.duration,e.additions);d(this,De,s.timestamp),t(this,M).push(s),o(this,le,Qe).call(this),o(this,P,re).call(this)}finalize(){if(t(this,ue))throw new Error("Cannot finalize a muxer more than once.");for(;t(this,v).length>0;)o(this,k,A).call(this,t(this,v).shift(),!0);for(;t(this,z).length>0;)o(this,k,A).call(this,t(this,z).shift(),!0);for(;t(this,M).length>0&&t(this,M)[0].timestamp<=t(this,L);)o(this,k,A).call(this,t(this,M).shift(),!1);if(t(this,l).streaming||o(this,Me,Et).call(this),t(this,h).writeEBML(t(this,I)),!t(this,l).streaming){let e=t(this,h).pos,i=t(this,h).pos-t(this,N,se);t(this,h).seek(t(this,h).offsets.get(t(this,ae))+4),t(this,h).writeEBMLVarInt(i,Ht),t(this,j).data=new _(t(this,L)),t(this,h).seek(t(this,h).offsets.get(t(this,j))),t(this,h).writeEBML(t(this,j)),t(this,C).data[0].data[1].data=t(this,h).offsets.get(t(this,I))-t(this,N,se),t(this,C).data[1].data[1].data=t(this,h).offsets.get(t(this,ne))-t(this,N,se),t(this,C).data[2].data[1].data=t(this,h).offsets.get(t(this,B))-t(this,N,se),t(this,h).seek(t(this,h).offsets.get(t(this,C))),t(this,h).writeEBML(t(this,C)),t(this,h).seek(e)}o(this,P,re).call(this),t(this,h).finalize(),d(this,ue,!0)}};l=new WeakMap,h=new WeakMap,ae=new WeakMap,ne=new WeakMap,C=new WeakMap,B=new WeakMap,j=new WeakMap,F=new WeakMap,K=new WeakMap,U=new WeakMap,q=new WeakMap,I=new WeakMap,x=new WeakMap,G=new WeakMap,L=new WeakMap,v=new WeakMap,z=new WeakMap,M=new WeakMap,oe=new WeakMap,he=new WeakMap,Y=new WeakMap,Z=new WeakMap,De=new WeakMap,de=new WeakMap,ue=new WeakMap,tt=new WeakSet,Bt=function(e){if(typeof e!="object")throw new TypeError("The muxer requires an options object to be passed to its constructor.");if(!(e.target instanceof W))throw new TypeError("The target must be provided and an instance of Target.");if(e.video){if(typeof e.video.codec!="string")throw new TypeError(`Invalid video codec: ${e.video.codec}. Must be a string.`);if(!Number.isInteger(e.video.width)||e.video.width<=0)throw new TypeError(`Invalid video width: ${e.video.width}. Must be a positive integer.`);if(!Number.isInteger(e.video.height)||e.video.height<=0)throw new TypeError(`Invalid video height: ${e.video.height}. Must be a positive integer.`);if(e.video.frameRate!==void 0&&(!Number.isFinite(e.video.frameRate)||e.video.frameRate<=0))throw new TypeError(`Invalid video frame rate: ${e.video.frameRate}. Must be a positive number.`);if(e.video.alpha!==void 0&&typeof e.video.alpha!="boolean")throw new TypeError(`Invalid video alpha: ${e.video.alpha}. Must be a boolean.`)}if(e.audio){if(typeof e.audio.codec!="string")throw new TypeError(`Invalid audio codec: ${e.audio.codec}. Must be a string.`);if(!Number.isInteger(e.audio.numberOfChannels)||e.audio.numberOfChannels<=0)throw new TypeError(`Invalid number of audio channels: ${e.audio.numberOfChannels}. Must be a positive integer.`);if(!Number.isInteger(e.audio.sampleRate)||e.audio.sampleRate<=0)throw new TypeError(`Invalid audio sample rate: ${e.audio.sampleRate}. Must be a positive integer.`);if(e.audio.bitDepth!==void 0&&(!Number.isInteger(e.audio.bitDepth)||e.audio.bitDepth<=0))throw new TypeError(`Invalid audio bit depth: ${e.audio.bitDepth}. Must be a positive integer.`)}if(e.subtitles&&typeof e.subtitles.codec!="string")throw new TypeError(`Invalid subtitles codec: ${e.subtitles.codec}. Must be a string.`);if(e.type!==void 0&&!["webm","matroska"].includes(e.type))throw new TypeError(`Invalid type: ${e.type}. Must be 'webm' or 'matroska'.`);if(e.firstTimestampBehavior&&!fi.includes(e.firstTimestampBehavior))throw new TypeError(`Invalid first timestamp behavior: ${e.firstTimestampBehavior}`);if(e.streaming!==void 0&&typeof e.streaming!="boolean")throw new TypeError(`Invalid streaming option: ${e.streaming}. Must be a boolean.`)},it=new WeakSet,jt=function(){t(this,h)instanceof E&&t(this,h).target.options.onHeader&&t(this,h).startTrackingWrites(),o(this,rt,Kt).call(this),t(this,l).streaming||o(this,nt,Gt).call(this),o(this,ot,Lt).call(this),o(this,st,qt).call(this),o(this,at,It).call(this),t(this,l).streaming||(o(this,Re,xt).call(this),o(this,Fe,St).call(this)),o(this,ht,Yt).call(this),o(this,P,re).call(this)},rt=new WeakSet,Kt=function(){let e={id:440786851,data:[{id:17030,data:1},{id:17143,data:1},{id:17138,data:4},{id:17139,data:8},{id:17026,data:t(this,l).type??"webm"},{id:17031,data:2},{id:17029,data:2}]};t(this,h).writeEBML(e)},st=new WeakSet,qt=function(){d(this,K,{id:236,size:4,data:new Uint8Array(ze)}),d(this,U,{id:236,size:4,data:new Uint8Array(ze)}),d(this,q,{id:236,size:4,data:new Uint8Array(ze)})},at=new WeakSet,It=function(){d(this,F,{id:21936,data:[{id:21937,data:2},{id:21946,data:2},{id:21947,data:2},{id:21945,data:0}]})},nt=new WeakSet,Gt=function(){let e=new Uint8Array([28,83,187,107]),i=new Uint8Array([21,73,169,102]),r=new Uint8Array([22,84,174,107]),s={id:290298740,data:[{id:19899,data:[{id:21419,data:e},{id:21420,size:5,data:0}]},{id:19899,data:[{id:21419,data:i},{id:21420,size:5,data:0}]},{id:19899,data:[{id:21419,data:r},{id:21420,size:5,data:0}]}]};d(this,C,s)},ot=new WeakSet,Lt=function(){let e={id:17545,data:new _(0)};d(this,j,e);let i={id:357149030,data:[{id:2807729,data:1e6},{id:19840,data:Wt},{id:22337,data:Wt},t(this,l).streaming?null:e]};d(this,ne,i)},Re=new WeakSet,xt=function(){let e={id:374648427,data:[]};d(this,B,e),t(this,l).video&&e.data.push({id:174,data:[{id:215,data:ie},{id:29637,data:ie},{id:131,data:di},{id:134,data:t(this,l).video.codec},t(this,K),t(this,l).video.frameRate?{id:2352003,data:1e9/t(this,l).video.frameRate}:null,{id:224,data:[{id:176,data:t(this,l).video.width},{id:186,data:t(this,l).video.height},t(this,l).video.alpha?{id:21440,data:1}:null,t(this,F)]}]}),t(this,l).audio&&(d(this,U,t(this,l).streaming?t(this,U)||null:{id:236,size:4,data:new Uint8Array(ze)}),e.data.push({id:174,data:[{id:215,data:ve},{id:29637,data:ve},{id:131,data:ui},{id:134,data:t(this,l).audio.codec},t(this,U),{id:225,data:[{id:181,data:new J(t(this,l).audio.sampleRate)},{id:159,data:t(this,l).audio.numberOfChannels},t(this,l).audio.bitDepth?{id:25188,data:t(this,l).audio.bitDepth}:null]}]})),t(this,l).subtitles&&e.data.push({id:174,data:[{id:215,data:Ze},{id:29637,data:Ze},{id:131,data:li},{id:134,data:t(this,l).subtitles.codec},t(this,q)]})},Fe=new WeakSet,St=function(){let e={id:408125543,size:t(this,l).streaming?-1:Ht,data:[t(this,l).streaming?null:t(this,C),t(this,ne),t(this,B)]};if(d(this,ae,e),t(this,h).writeEBML(e),t(this,h)instanceof E&&t(this,h).target.options.onHeader){let{data:i,start:r}=t(this,h).getTrackedWrites();t(this,h).target.options.onHeader(i,r)}},ht=new WeakSet,Yt=function(){d(this,I,{id:475249515,data:[]})},P=new WeakSet,re=function(){t(this,h)instanceof ge&&t(this,h).flush()},N=new WeakSet,se=function(){return t(this,h).dataOffsets.get(t(this,ae))},dt=new WeakSet,Zt=function(e){if(!!e.decoderConfig){if(e.decoderConfig.colorSpace){let i=e.decoderConfig.colorSpace;if(d(this,de,i),t(this,F).data=[{id:21937,data:{rgb:1,bt709:1,bt470bg:5,smpte170m:6}[i.matrix]},{id:21946,data:{bt709:1,smpte170m:6,"iec61966-2-1":13}[i.transfer]},{id:21947,data:{bt709:1,bt470bg:5,smpte170m:6}[i.primaries]},{id:21945,data:[1,2][Number(i.fullRange)]}],!t(this,l).streaming){let r=t(this,h).pos;t(this,h).seek(t(this,h).offsets.get(t(this,F))),t(this,h).writeEBML(t(this,F)),t(this,h).seek(r)}}e.decoderConfig.description&&(t(this,l).streaming?d(this,K,o(this,Q,Ve).call(this,e.decoderConfig.description)):o(this,ce,Je).call(this,t(this,K),e.decoderConfig.description))}},ut=new WeakSet,Qt=function(e){if(e.type!=="key"||!t(this,de))return;let i=0;if(O(e.data,0,2)!==2)return;i+=2;let r=(O(e.data,i+1,i+2)<<1)+O(e.data,i+0,i+1);i+=2,r===3&&i++;let s=O(e.data,i+0,i+1);if(i++,s)return;let n=O(e.data,i+0,i+1);if(i++,n!==0)return;i+=2;let u=O(e.data,i+0,i+24);if(i+=24,u!==4817730)return;r>=2&&i++;let m={rgb:7,bt709:2,bt470bg:1,smpte170m:3}[t(this,de).matrix];Rt(e.data,i+0,i+3,m)},le=new WeakSet,Qe=function(){let e=Math.min(t(this,l).video?t(this,Y):1/0,t(this,l).audio?t(this,Z):1/0),i=t(this,M);for(;i.length>0&&i[0].timestamp<=e;)o(this,k,A).call(this,i.shift(),!t(this,l).video&&!t(this,l).audio)},fe=new WeakSet,Xe=function(e,i,r,s,n,u){let m=o(this,lt,Xt).call(this,r,s);return{data:e,additions:u,type:i,timestamp:m,duration:n,trackNumber:s}},lt=new WeakSet,Xt=function(e,i){let r=i===ie?t(this,Y):i===ve?t(this,Z):t(this,De);if(i!==Ze){let s=i===ie?t(this,oe):t(this,he);if(t(this,l).firstTimestampBehavior==="strict"&&r===-1&&e!==0)throw new Error(`The first chunk for your media track must have a timestamp of 0 (received ${e}). Non-zero first timestamps are often caused by directly piping frames or audio data from a MediaStreamTrack into the encoder. Their timestamps are typically relative to the age of the document, which is probably what you want. If you want to offset all timestamps of a track such that the first one is zero, set firstTimestampBehavior: 'offset' in the options. If you want to allow non-zero first timestamps, set firstTimestampBehavior: 'permissive'. -`);t(this,f).firstTimestampBehavior==="offset"&&(e-=s)}if(e=1e3;(!t(this,S)||s)&&h(this,ct,ii).call(this,r);let n=r-t(this,I);if(n<0)return;if(n>=Ct)throw new Error(`Current Matroska cluster exceeded its maximum allowed length of ${Ct} milliseconds. In order to produce a correct WebM file, you must pass in a key frame at least every ${Ct} milliseconds.`);let c=new Uint8Array(4),b=new DataView(c.buffer);if(b.setUint8(0,128|e.trackNumber),b.setInt16(1,n,!1),e.duration===void 0&&!e.additions){b.setUint8(3,Number(e.type==="key")<<7);let p={id:163,data:[c,e.data]};t(this,d).writeEBML(p)}else{let p=Math.floor(e.duration/1e3),we={id:160,data:[{id:161,data:[c,e.data]},e.duration!==void 0?{id:155,data:p}:null,e.additions?{id:30113,data:e.additions}:null]};t(this,d).writeEBML(we)}l(this,G,Math.max(t(this,G),r))},Z=new WeakSet,ze=function(e){return{id:25506,size:4,data:new Uint8Array(e)}},ce=new WeakSet,et=function(e,i){let r=t(this,d).pos;t(this,d).seek(t(this,d).offsets.get(e));let s=2+4+i.byteLength,n=ve-s;if(n<0){let o=i.byteLength+n;i instanceof ArrayBuffer?i=i.slice(0,o):i=i.buffer.slice(0,o),n=0}e=[h(this,Z,ze).call(this,i),{id:236,size:4,data:new Uint8Array(n)}],t(this,d).writeEBML(e),t(this,d).seek(r)},ct=new WeakSet,ii=function(e){t(this,S)&&!t(this,f).streaming&&h(this,Fe,Et).call(this),t(this,d)instanceof E&&t(this,d).target.options.onCluster&&t(this,d).startTrackingWrites(),l(this,S,{id:524531317,size:t(this,f).streaming?-1:Kt,data:[{id:231,data:e}]}),t(this,d).writeEBML(t(this,S)),l(this,I,e);let i=t(this,d).offsets.get(t(this,S))-t(this,_,se);t(this,q).data.push({id:187,data:[{id:179,data:e},t(this,f).video?{id:183,data:[{id:247,data:ie},{id:241,data:i}]}:null,t(this,f).audio?{id:183,data:[{id:247,data:Ue},{id:241,data:i}]}:null]})},Fe=new WeakSet,Et=function(){let e=t(this,d).pos-t(this,d).dataOffsets.get(t(this,S)),i=t(this,d).pos;if(t(this,d).seek(t(this,d).offsets.get(t(this,S))+4),t(this,d).writeEBMLVarInt(e,Kt),t(this,d).seek(i),t(this,d)instanceof E&&t(this,d).target.options.onCluster){let{data:r,start:s}=t(this,d).getTrackedWrites();t(this,d).target.options.onCluster(r,s,t(this,I))}},me=new WeakSet,tt=function(){if(t(this,ue))throw new Error("Cannot add new video or audio chunks after the file has been finalized.")};var Me=/(?:(.+?)\n)?((?:\d{2}:)?\d{2}:\d{2}.\d{3})\s+-->\s+((?:\d{2}:)?\d{2}:\d{2}.\d{3})/g,gi=/^WEBVTT.*?\n{2}/,yi=/(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})/,ri=/<(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})>/g,Ut=new TextEncoder,Q,Pe,Ne,_e,Oe,be,mt,bt,si,vt=class{constructor(e){a(this,be);a(this,bt);a(this,Q,void 0);a(this,Pe,void 0);a(this,Ne,!1);a(this,_e,void 0);a(this,Oe,!1);l(this,Q,e)}configure(e){if(e.codec!=="webvtt")throw new Error("Codec must be 'webvtt'.");l(this,Pe,e)}encode(e){var r;if(!t(this,Pe))throw new Error("Encoder not configured.");e=e.replace(`\r +`);t(this,l).firstTimestampBehavior==="offset"&&(e-=s)}if(e=1e3;(!t(this,x)||s)&&o(this,ft,Jt).call(this,r);let n=r-t(this,G);if(n<0)return;if(n>=kt)throw new Error(`Current Matroska cluster exceeded its maximum allowed length of ${kt} milliseconds. In order to produce a correct WebM file, you must pass in a key frame at least every ${kt} milliseconds.`);let m=new Uint8Array(4),p=new DataView(m.buffer);if(p.setUint8(0,128|e.trackNumber),p.setInt16(1,n,!1),e.duration===void 0&&!e.additions){p.setUint8(3,Number(e.type==="key")<<7);let b={id:163,data:[m,e.data]};t(this,h).writeEBML(b)}else{let b=Math.floor(e.duration/1e3),vt={id:160,data:[{id:161,data:[m,e.data]},e.duration!==void 0?{id:155,data:b}:null,e.additions?{id:30113,data:e.additions}:null]};t(this,h).writeEBML(vt)}d(this,L,Math.max(t(this,L),r))},Q=new WeakSet,Ve=function(e){return{id:25506,size:4,data:new Uint8Array(e)}},ce=new WeakSet,Je=function(e,i){let r=t(this,h).pos;t(this,h).seek(t(this,h).offsets.get(e));let s=2+4+i.byteLength,n=ze-s;if(n<0){let u=i.byteLength+n;i instanceof ArrayBuffer?i=i.slice(0,u):i=i.buffer.slice(0,u),n=0}e=[o(this,Q,Ve).call(this,i),{id:236,size:4,data:new Uint8Array(n)}],t(this,h).writeEBML(e),t(this,h).seek(r)},ft=new WeakSet,Jt=function(e){t(this,x)&&!t(this,l).streaming&&o(this,Me,Et).call(this),t(this,h)instanceof E&&t(this,h).target.options.onCluster&&t(this,h).startTrackingWrites(),d(this,x,{id:524531317,size:t(this,l).streaming?-1:$t,data:[{id:231,data:e}]}),t(this,h).writeEBML(t(this,x)),d(this,G,e);let i=t(this,h).offsets.get(t(this,x))-t(this,N,se);t(this,I).data.push({id:187,data:[{id:179,data:e},t(this,l).video?{id:183,data:[{id:247,data:ie},{id:241,data:i}]}:null,t(this,l).audio?{id:183,data:[{id:247,data:ve},{id:241,data:i}]}:null]})},Me=new WeakSet,Et=function(){let e=t(this,h).pos-t(this,h).dataOffsets.get(t(this,x)),i=t(this,h).pos;if(t(this,h).seek(t(this,h).offsets.get(t(this,x))+4),t(this,h).writeEBMLVarInt(e,$t),t(this,h).seek(i),t(this,h)instanceof E&&t(this,h).target.options.onCluster){let{data:r,start:s}=t(this,h).getTrackedWrites();t(this,h).target.options.onCluster(r,s,t(this,G))}},me=new WeakSet,et=function(){if(t(this,ue))throw new Error("Cannot add new video or audio chunks after the file has been finalized.")};var Pe=/(?:(.+?)\n)?((?:\d{2}:)?\d{2}:\d{2}.\d{3})\s+-->\s+((?:\d{2}:)?\d{2}:\d{2}.\d{3})/g,ci=/^WEBVTT.*?\n{2}/,mi=/(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})/,ei=/<(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})>/g,At=new TextEncoder,X,Ne,_e,Oe,We,be,ct,mt,ti,Ut=class{constructor(e){a(this,be);a(this,mt);a(this,X,void 0);a(this,Ne,void 0);a(this,_e,!1);a(this,Oe,void 0);a(this,We,!1);d(this,X,e)}configure(e){if(e.codec!=="webvtt")throw new Error("Codec must be 'webvtt'.");d(this,Ne,e)}encode(e){if(!t(this,Ne))throw new Error("Encoder not configured.");e=e.replace(`\r `,` `).replace("\r",` -`),Me.lastIndex=0;let i;if(!t(this,Ne)){if(!gi.test(e)){let n=new Error("WebVTT preamble incorrect.");throw t(this,Q).error(n),n}i=Me.exec(e);let s=e.slice(0,(r=i==null?void 0:i.index)!=null?r:e.length).trimEnd();if(!s){let n=new Error("No WebVTT preamble provided.");throw t(this,Q).error(n),n}l(this,_e,Ut.encode(s)),l(this,Ne,!0),i&&(e=e.slice(i.index),Me.lastIndex=0)}for(;i=Me.exec(e);){let s=e.slice(0,i.index),n=i[1]||"",o=i.index+i[0].length,c=e.indexOf(` -`,o)+1,b=e.slice(o,c).trim(),p=e.indexOf(` +`),Pe.lastIndex=0;let i;if(!t(this,_e)){if(!ci.test(e)){let s=new Error("WebVTT preamble incorrect.");throw t(this,X).error(s),s}i=Pe.exec(e);let r=e.slice(0,i?.index??e.length).trimEnd();if(!r){let s=new Error("No WebVTT preamble provided.");throw t(this,X).error(s),s}d(this,Oe,At.encode(r)),d(this,_e,!0),i&&(e=e.slice(i.index),Pe.lastIndex=0)}for(;i=Pe.exec(e);){let r=e.slice(0,i.index),s=i[1]||"",n=i.index+i[0].length,u=e.indexOf(` +`,n)+1,m=e.slice(n,u).trim(),p=e.indexOf(` -`,o);p===-1&&(p=e.length);let we=h(this,be,mt).call(this,i[2]),ai=h(this,be,mt).call(this,i[3])-we,wt=e.slice(c,p),zt=`${b} -${n} -${s}`;ri.lastIndex=0,wt=wt.replace(ri,oi=>{let hi=h(this,be,mt).call(this,oi.slice(1,-1))-we;return`<${h(this,bt,si).call(this,hi)}>`}),e=e.slice(p).trimStart(),Me.lastIndex=0;let ni={body:Ut.encode(wt),additions:zt.trim()===""?void 0:Ut.encode(zt),timestamp:we*1e3,duration:ai*1e3},Vt={};t(this,Oe)||(Vt.decoderConfig={description:t(this,_e)},l(this,Oe,!0)),t(this,Q).output(ni,Vt)}}};Q=new WeakMap,Pe=new WeakMap,Ne=new WeakMap,_e=new WeakMap,Oe=new WeakMap,be=new WeakSet,mt=function(e){let i=yi.exec(e);if(!i)throw new Error("Expected match.");return 60*60*1e3*Number(i[1]||"0")+60*1e3*Number(i[2])+1e3*Number(i[3])+Number(i[4])},bt=new WeakSet,si=function(e){let i=Math.floor(e/36e5),r=Math.floor(e%(60*60*1e3)/(60*1e3)),s=Math.floor(e%(60*1e3)/1e3),n=e%1e3;return i.toString().padStart(2,"0")+":"+r.toString().padStart(2,"0")+":"+s.toString().padStart(2,"0")+"."+n.toString().padStart(3,"0")};export{We as ArrayBufferTarget,He as FileSystemWritableFileStreamTarget,xt as Muxer,J as StreamTarget,vt as SubtitleEncoder}; +`,n);p===-1&&(p=e.length);let b=o(this,be,ct).call(this,i[2]),ii=o(this,be,ct).call(this,i[3])-b,bt=e.slice(u,p),zt=`${m} +${s} +${r}`;ei.lastIndex=0,bt=bt.replace(ei,si=>{let ai=o(this,be,ct).call(this,si.slice(1,-1))-b;return`<${o(this,mt,ti).call(this,ai)}>`}),e=e.slice(p).trimStart(),Pe.lastIndex=0;let ri={body:At.encode(bt),additions:zt.trim()===""?void 0:At.encode(zt),timestamp:b*1e3,duration:ii*1e3},Vt={};t(this,We)||(Vt.decoderConfig={description:t(this,Oe)},d(this,We,!0)),t(this,X).output(ri,Vt)}}};X=new WeakMap,Ne=new WeakMap,_e=new WeakMap,Oe=new WeakMap,We=new WeakMap,be=new WeakSet,ct=function(e){let i=mi.exec(e);if(!i)throw new Error("Expected match.");return 60*60*1e3*Number(i[1]||"0")+60*1e3*Number(i[2])+1e3*Number(i[3])+Number(i[4])},mt=new WeakSet,ti=function(e){let i=Math.floor(e/36e5),r=Math.floor(e%(60*60*1e3)/(60*1e3)),s=Math.floor(e%(60*1e3)/1e3),n=e%1e3;return i.toString().padStart(2,"0")+":"+r.toString().padStart(2,"0")+":"+s.toString().padStart(2,"0")+"."+n.toString().padStart(3,"0")};export{we as ArrayBufferTarget,pe as FileSystemWritableFileStreamTarget,Ct as Muxer,H as StreamTarget,Ut as SubtitleEncoder}; diff --git a/build/webm-muxer.mjs b/build/webm-muxer.mjs index 5685dc2..0153e12 100644 --- a/build/webm-muxer.mjs +++ b/build/webm-muxer.mjs @@ -1,20 +1,3 @@ -var __defProp = Object.defineProperty; -var __getOwnPropSymbols = Object.getOwnPropertySymbols; -var __hasOwnProp = Object.prototype.hasOwnProperty; -var __propIsEnum = Object.prototype.propertyIsEnumerable; -var __pow = Math.pow; -var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; -var __spreadValues = (a, b) => { - for (var prop in b ||= {}) - if (__hasOwnProp.call(b, prop)) - __defNormalProp(a, prop, b[prop]); - if (__getOwnPropSymbols) - for (var prop of __getOwnPropSymbols(b)) { - if (__propIsEnum.call(b, prop)) - __defNormalProp(a, prop, b[prop]); - } - return a; -}; var __accessCheck = (obj, member, msg) => { if (!member.has(obj)) throw TypeError("Cannot " + msg); @@ -56,9 +39,9 @@ var measureUnsignedInt = (value) => { return 2; } else if (value < 1 << 24) { return 3; - } else if (value < __pow(2, 32)) { + } else if (value < 2 ** 32) { return 4; - } else if (value < __pow(2, 40)) { + } else if (value < 2 ** 40) { return 5; } else { return 6; @@ -73,9 +56,9 @@ var measureEBMLVarInt = (value) => { return 3; } else if (value < (1 << 28) - 1) { return 4; - } else if (value < __pow(2, 35) - 1) { + } else if (value < 2 ** 35 - 1) { return 5; - } else if (value < __pow(2, 42) - 1) { + } else if (value < 2 ** 42 - 1) { return 6; } else { throw new Error("EBML VINT size not supported " + value); @@ -107,13 +90,19 @@ var writeBits = (bytes, start, end, value) => { }; // src/target.ts -var ArrayBufferTarget = class { +var isTarget = Symbol("isTarget"); +var Target = class { +}; +isTarget; +var ArrayBufferTarget = class extends Target { constructor() { + super(...arguments); this.buffer = null; } }; -var StreamTarget = class { +var StreamTarget = class extends Target { constructor(options) { + super(); this.options = options; if (typeof options !== "object") { throw new TypeError("StreamTarget requires an options object to be passed to its constructor."); @@ -142,8 +131,9 @@ var StreamTarget = class { } } }; -var FileSystemWritableFileStreamTarget = class { +var FileSystemWritableFileStreamTarget = class extends Target { constructor(stream, options) { + super(); this.stream = stream; this.options = options; if (!(stream instanceof FileSystemWritableFileStream)) { @@ -200,15 +190,15 @@ var Writer = class { __privateGet(this, _helperView).setUint8(pos++, value); break; case 5: - __privateGet(this, _helperView).setUint8(pos++, 1 << 3 | value / __pow(2, 32) & 7); + __privateGet(this, _helperView).setUint8(pos++, 1 << 3 | value / 2 ** 32 & 7); __privateGet(this, _helperView).setUint8(pos++, value >> 24); __privateGet(this, _helperView).setUint8(pos++, value >> 16); __privateGet(this, _helperView).setUint8(pos++, value >> 8); __privateGet(this, _helperView).setUint8(pos++, value); break; case 6: - __privateGet(this, _helperView).setUint8(pos++, 1 << 2 | value / __pow(2, 40) & 3); - __privateGet(this, _helperView).setUint8(pos++, value / __pow(2, 32) | 0); + __privateGet(this, _helperView).setUint8(pos++, 1 << 2 | value / 2 ** 40 & 3); + __privateGet(this, _helperView).setUint8(pos++, value / 2 ** 32 | 0); __privateGet(this, _helperView).setUint8(pos++, value >> 24); __privateGet(this, _helperView).setUint8(pos++, value >> 16); __privateGet(this, _helperView).setUint8(pos++, value >> 8); @@ -220,7 +210,6 @@ var Writer = class { this.write(__privateGet(this, _helper).subarray(0, pos)); } writeEBML(data) { - var _a, _b; if (data === null) return; if (data instanceof Uint8Array) { @@ -234,7 +223,7 @@ var Writer = class { __privateMethod(this, _writeUnsignedInt, writeUnsignedInt_fn).call(this, data.id); if (Array.isArray(data.data)) { let sizePos = this.pos; - let sizeSize = data.size === -1 ? 1 : (_a = data.size) != null ? _a : 4; + let sizeSize = data.size === -1 ? 1 : data.size ?? 4; if (data.size === -1) { __privateMethod(this, _writeByte, writeByte_fn).call(this, 255); } else { @@ -251,7 +240,7 @@ var Writer = class { this.seek(endPos); } } else if (typeof data.data === "number") { - let size = (_b = data.size) != null ? _b : measureUnsignedInt(data.data); + let size = data.size ?? measureUnsignedInt(data.data); this.writeEBMLVarInt(size); __privateMethod(this, _writeUnsignedInt, writeUnsignedInt_fn).call(this, data.data, size); } else if (typeof data.data === "string") { @@ -292,9 +281,9 @@ writeUnsignedInt_fn = function(value, width = measureUnsignedInt(value)) { let pos = 0; switch (width) { case 6: - __privateGet(this, _helperView).setUint8(pos++, value / __pow(2, 40) | 0); + __privateGet(this, _helperView).setUint8(pos++, value / 2 ** 40 | 0); case 5: - __privateGet(this, _helperView).setUint8(pos++, value / __pow(2, 32) | 0); + __privateGet(this, _helperView).setUint8(pos++, value / 2 ** 32 | 0); case 4: __privateGet(this, _helperView).setUint8(pos++, value >> 24); case 3: @@ -319,7 +308,7 @@ var ArrayBufferTargetWriter = class extends Writer { super(); __privateAdd(this, _ensureSize); __privateAdd(this, _target, void 0); - __privateAdd(this, _buffer, new ArrayBuffer(__pow(2, 16))); + __privateAdd(this, _buffer, new ArrayBuffer(2 ** 16)); __privateAdd(this, _bytes, new Uint8Array(__privateGet(this, _buffer))); __privateSet(this, _target, target); } @@ -383,7 +372,7 @@ var BaseStreamTargetWriter = class extends Writer { } startTrackingWrites() { __privateSet(this, _trackingWrites, true); - __privateSet(this, _trackedWrites, new Uint8Array(__pow(2, 10))); + __privateSet(this, _trackedWrites, new Uint8Array(2 ** 10)); __privateSet(this, _trackedStart, this.pos); __privateSet(this, _trackedEnd, this.pos); } @@ -424,7 +413,6 @@ var StreamTargetWriter = class extends BaseStreamTargetWriter { this.pos += data.byteLength; } flush() { - var _a, _b; if (__privateGet(this, _sections).length === 0) return; let chunks = []; @@ -455,7 +443,7 @@ var StreamTargetWriter = class extends BaseStreamTargetWriter { if (__privateGet(this, _ensureMonotonicity) && chunk.start < __privateGet(this, _lastFlushEnd)) { throw new Error("Internal error: Monotonicity violation."); } - (_b = (_a = this.target.options).onData) == null ? void 0 : _b.call(_a, chunk.data, chunk.start); + this.target.options.onData?.(chunk.data, chunk.start); __privateSet(this, _lastFlushEnd, chunk.start + chunk.data.byteLength); } __privateGet(this, _sections).length = 0; @@ -466,12 +454,11 @@ var StreamTargetWriter = class extends BaseStreamTargetWriter { _sections = new WeakMap(); _lastFlushEnd = new WeakMap(); _ensureMonotonicity = new WeakMap(); -var DEFAULT_CHUNK_SIZE = __pow(2, 24); +var DEFAULT_CHUNK_SIZE = 2 ** 24; var MAX_CHUNKS_AT_ONCE = 2; var _chunkSize, _chunks, _lastFlushEnd2, _ensureMonotonicity2, _writeDataIntoChunks, writeDataIntoChunks_fn, _insertSectionIntoChunk, insertSectionIntoChunk_fn, _createChunk, createChunk_fn, _flushChunks, flushChunks_fn; var ChunkedStreamTargetWriter = class extends BaseStreamTargetWriter { constructor(target, ensureMonotonicity) { - var _a, _b; super(target); __privateAdd(this, _writeDataIntoChunks); __privateAdd(this, _insertSectionIntoChunk); @@ -481,9 +468,9 @@ var ChunkedStreamTargetWriter = class extends BaseStreamTargetWriter { __privateAdd(this, _chunks, []); __privateAdd(this, _lastFlushEnd2, 0); __privateAdd(this, _ensureMonotonicity2, void 0); - __privateSet(this, _chunkSize, (_b = (_a = target.options) == null ? void 0 : _a.chunkSize) != null ? _b : DEFAULT_CHUNK_SIZE); + __privateSet(this, _chunkSize, target.options?.chunkSize ?? DEFAULT_CHUNK_SIZE); __privateSet(this, _ensureMonotonicity2, ensureMonotonicity); - if (!Number.isInteger(__privateGet(this, _chunkSize)) || __privateGet(this, _chunkSize) < __pow(2, 10)) { + if (!Number.isInteger(__privateGet(this, _chunkSize)) || __privateGet(this, _chunkSize) < 2 ** 10) { throw new Error("Invalid StreamTarget options: chunkSize must be an integer not smaller than 1024."); } } @@ -565,7 +552,6 @@ createChunk_fn = function(includesPosition) { }; _flushChunks = new WeakSet(); flushChunks_fn = function(force = false) { - var _a, _b; for (let i = 0; i < __privateGet(this, _chunks).length; i++) { let chunk = __privateGet(this, _chunks)[i]; if (!chunk.shouldFlush && !force) @@ -574,8 +560,7 @@ flushChunks_fn = function(force = false) { if (__privateGet(this, _ensureMonotonicity2) && chunk.start + section.start < __privateGet(this, _lastFlushEnd2)) { throw new Error("Internal error: Monotonicity violation."); } - (_b = (_a = this.target.options).onData) == null ? void 0 : _b.call( - _a, + this.target.options.onData?.( chunk.data.subarray(section.start, section.end), chunk.start + section.start ); @@ -586,7 +571,6 @@ flushChunks_fn = function(force = false) { }; var FileSystemWritableFileStreamTargetWriter = class extends ChunkedStreamTargetWriter { constructor(target, ensureMonotonicity) { - var _a; super(new StreamTarget({ onData: (data, position) => target.stream.write({ type: "write", @@ -594,7 +578,7 @@ var FileSystemWritableFileStreamTargetWriter = class extends ChunkedStreamTarget position }), chunked: true, - chunkSize: (_a = target.options) == null ? void 0 : _a.chunkSize + chunkSize: target.options?.chunkSize }), ensureMonotonicity); } }; @@ -606,8 +590,8 @@ var SUBTITLE_TRACK_NUMBER = 3; var VIDEO_TRACK_TYPE = 1; var AUDIO_TRACK_TYPE = 2; var SUBTITLE_TRACK_TYPE = 17; -var MAX_CHUNK_LENGTH_MS = __pow(2, 15); -var CODEC_PRIVATE_MAX_SIZE = __pow(2, 12); +var MAX_CHUNK_LENGTH_MS = 2 ** 15; +var CODEC_PRIVATE_MAX_SIZE = 2 ** 12; var APP_NAME = "https://github.com/Vanilagy/webm-muxer"; var SEGMENT_SIZE_BYTES = 6; var CLUSTER_SIZE_BYTES = 5; @@ -663,18 +647,18 @@ var Muxer = class { __privateAdd(this, _lastSubtitleTimestamp, -1); __privateAdd(this, _colorSpace, void 0); __privateAdd(this, _finalized, false); - var _a; __privateMethod(this, _validateOptions, validateOptions_fn).call(this, options); - __privateSet(this, _options, __spreadValues({ + __privateSet(this, _options, { type: "webm", - firstTimestampBehavior: "strict" - }, options)); + firstTimestampBehavior: "strict", + ...options + }); this.target = options.target; let ensureMonotonicity = !!__privateGet(this, _options).streaming; if (options.target instanceof ArrayBufferTarget) { __privateSet(this, _writer, new ArrayBufferTargetWriter(options.target)); } else if (options.target instanceof StreamTarget) { - __privateSet(this, _writer, ((_a = options.target.options) == null ? void 0 : _a.chunked) ? new ChunkedStreamTargetWriter(options.target, ensureMonotonicity) : new StreamTargetWriter(options.target, ensureMonotonicity)); + __privateSet(this, _writer, options.target.options?.chunked ? new ChunkedStreamTargetWriter(options.target, ensureMonotonicity) : new StreamTargetWriter(options.target, ensureMonotonicity)); } else if (options.target instanceof FileSystemWritableFileStreamTarget) { __privateSet(this, _writer, new FileSystemWritableFileStreamTargetWriter(options.target, ensureMonotonicity)); } else { @@ -696,7 +680,7 @@ var Muxer = class { } let data = new Uint8Array(chunk.byteLength); chunk.copyTo(data); - this.addVideoChunkRaw(data, chunk.type, timestamp != null ? timestamp : chunk.timestamp, meta); + this.addVideoChunkRaw(data, chunk.type, timestamp ?? chunk.timestamp, meta); } addVideoChunkRaw(data, type, timestamp, meta) { if (!(data instanceof Uint8Array)) { @@ -748,7 +732,7 @@ var Muxer = class { } let data = new Uint8Array(chunk.byteLength); chunk.copyTo(data); - this.addAudioChunkRaw(data, chunk.type, timestamp != null ? timestamp : chunk.timestamp, meta); + this.addAudioChunkRaw(data, chunk.type, timestamp ?? chunk.timestamp, meta); } addAudioChunkRaw(data, type, timestamp, meta) { if (!(data instanceof Uint8Array)) { @@ -768,7 +752,7 @@ var Muxer = class { throw new Error("No audio track declared."); if (__privateGet(this, _firstAudioTimestamp) === void 0) __privateSet(this, _firstAudioTimestamp, timestamp); - if (meta == null ? void 0 : meta.decoderConfig) { + if (meta?.decoderConfig) { if (__privateGet(this, _options).streaming) { __privateSet(this, _audioCodecPrivate, __privateMethod(this, _createCodecPrivateElement, createCodecPrivateElement_fn).call(this, meta.decoderConfig.description)); } else { @@ -812,14 +796,14 @@ var Muxer = class { __privateMethod(this, _ensureNotFinalized, ensureNotFinalized_fn).call(this); if (!__privateGet(this, _options).subtitles) throw new Error("No subtitle track declared."); - if (meta == null ? void 0 : meta.decoderConfig) { + if (meta?.decoderConfig) { if (__privateGet(this, _options).streaming) { __privateSet(this, _subtitleCodecPrivate, __privateMethod(this, _createCodecPrivateElement, createCodecPrivateElement_fn).call(this, meta.decoderConfig.description)); } else { __privateMethod(this, _writeCodecPrivate, writeCodecPrivate_fn).call(this, __privateGet(this, _subtitleCodecPrivate), meta.decoderConfig.description); } } - let subtitleChunk = __privateMethod(this, _createInternalChunk, createInternalChunk_fn).call(this, chunk.body, "key", timestamp != null ? timestamp : chunk.timestamp, SUBTITLE_TRACK_NUMBER, chunk.duration, chunk.additions); + let subtitleChunk = __privateMethod(this, _createInternalChunk, createInternalChunk_fn).call(this, chunk.body, "key", timestamp ?? chunk.timestamp, SUBTITLE_TRACK_NUMBER, chunk.duration, chunk.additions); __privateSet(this, _lastSubtitleTimestamp, subtitleChunk.timestamp); __privateGet(this, _subtitleChunkQueue).push(subtitleChunk); __privateMethod(this, _writeSubtitleChunks, writeSubtitleChunks_fn).call(this); @@ -890,6 +874,9 @@ validateOptions_fn = function(options) { if (typeof options !== "object") { throw new TypeError("The muxer requires an options object to be passed to its constructor."); } + if (!(options.target instanceof Target)) { + throw new TypeError("The target must be provided and an instance of Target."); + } if (options.video) { if (typeof options.video.codec !== "string") { throw new TypeError(`Invalid video codec: ${options.video.codec}. Must be a string.`); @@ -970,13 +957,12 @@ createFileHeader_fn = function() { }; _writeEBMLHeader = new WeakSet(); writeEBMLHeader_fn = function() { - var _a; let ebmlHeader = { id: 440786851 /* EBML */, data: [ { id: 17030 /* EBMLVersion */, data: 1 }, { id: 17143 /* EBMLReadVersion */, data: 1 }, { id: 17138 /* EBMLMaxIDLength */, data: 4 }, { id: 17139 /* EBMLMaxSizeLength */, data: 8 }, - { id: 17026 /* DocType */, data: (_a = __privateGet(this, _options).type) != null ? _a : "webm" }, + { id: 17026 /* DocType */, data: __privateGet(this, _options).type ?? "webm" }, { id: 17031 /* DocTypeVersion */, data: 2 }, { id: 17029 /* DocTypeReadVersion */, data: 2 } ] }; @@ -1382,7 +1368,6 @@ var SubtitleEncoder = class { __privateSet(this, _config, config); } encode(text) { - var _a; if (!__privateGet(this, _config)) { throw new Error("Encoder not configured."); } @@ -1396,7 +1381,7 @@ var SubtitleEncoder = class { throw error; } match = cueBlockHeaderRegex.exec(text); - let preamble = text.slice(0, (_a = match == null ? void 0 : match.index) != null ? _a : text.length).trimEnd(); + let preamble = text.slice(0, match?.index ?? text.length).trimEnd(); if (!preamble) { let error = new Error("No WebVTT preamble provided."); __privateGet(this, _options2).error(error); diff --git a/package-lock.json b/package-lock.json index 3e2ee2b..98991d3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "webm-muxer", - "version": "5.0.0", + "version": "5.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "webm-muxer", - "version": "5.0.0", + "version": "5.0.1", "license": "MIT", "dependencies": { "@types/dom-webcodecs": "^0.1.4", diff --git a/package.json b/package.json index 5f2b4b0..6445990 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webm-muxer", - "version": "5.0.1", + "version": "5.0.2", "description": "WebM multiplexer in pure TypeScript with support for WebCodecs API, video & audio.", "main": "./build/webm-muxer.js", "module": "./build/webm-muxer.mjs", diff --git a/src/index.ts b/src/index.ts index 2caf1cd..5689693 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ export { Muxer } from './muxer'; export { SubtitleEncoder } from './subtitles'; -export * from './target'; \ No newline at end of file +export { ArrayBufferTarget, StreamTarget, FileSystemWritableFileStreamTarget } from './target'; \ No newline at end of file diff --git a/src/muxer.ts b/src/muxer.ts index b89c1f4..295f9f6 100644 --- a/src/muxer.ts +++ b/src/muxer.ts @@ -22,7 +22,7 @@ const CODEC_PRIVATE_MAX_SIZE = 2**12; const APP_NAME = 'https://github.com/Vanilagy/webm-muxer'; const SEGMENT_SIZE_BYTES = 6; const CLUSTER_SIZE_BYTES = 5; -const FIRST_TIMESTAMP_BEHAVIORS = ['strict', 'offset', 'permissive'] as const; +const FIRST_TIMESTAMP_BEHAVIORS = ['strict', 'offset', 'permissive'] as const; interface MuxerOptions { target: T, @@ -136,6 +136,10 @@ export class Muxer { throw new TypeError('The muxer requires an options object to be passed to its constructor.'); } + if (!(options.target instanceof Target)) { + throw new TypeError('The target must be provided and an instance of Target.'); + } + if (options.video) { if (typeof options.video.codec !== 'string') { throw new TypeError(`Invalid video codec: ${options.video.codec}. Must be a string.`); diff --git a/src/target.ts b/src/target.ts index 3afbd85..8ac4f30 100644 --- a/src/target.ts +++ b/src/target.ts @@ -1,10 +1,14 @@ -export type Target = ArrayBufferTarget | StreamTarget | FileSystemWritableFileStreamTarget; +const isTarget = Symbol('isTarget'); +export abstract class Target { + // If we didn't add this symbol, then {} would be assignable to Target + [isTarget]: true; +} -export class ArrayBufferTarget { +export class ArrayBufferTarget extends Target { buffer: ArrayBuffer = null; } -export class StreamTarget { +export class StreamTarget extends Target { constructor(public options: { onData?: (data: Uint8Array, position: number) => void, onHeader?: (data: Uint8Array, position: number) => void, @@ -12,6 +16,8 @@ export class StreamTarget { chunked?: boolean, chunkSize?: number }) { + super(); + if (typeof options !== 'object') { throw new TypeError('StreamTarget requires an options object to be passed to its constructor.'); } @@ -44,11 +50,13 @@ export class StreamTarget { } } -export class FileSystemWritableFileStreamTarget { +export class FileSystemWritableFileStreamTarget extends Target { constructor( public stream: FileSystemWritableFileStream, public options?: { chunkSize?: number } ) { + super(); + if (!(stream instanceof FileSystemWritableFileStream)) { throw new TypeError('FileSystemWritableFileStreamTarget requires a FileSystemWritableFileStream instance.'); } diff --git a/tsconfig.json b/tsconfig.json index 361e041..e1e6c02 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,10 @@ { "compilerOptions": { - "target": "ES2015", + "target": "ES2021", "strict": true, "strictNullChecks": false, "noImplicitAny": true, - "noImplicitOverride": true, - "types": ["Node", "@types/wicg-file-system-access", "@types/dom-webcodecs"] + "noImplicitOverride": true }, "include": [ "src/**/*",