diff --git a/build/webm-muxer.js b/build/webm-muxer.js index dc17df5..de42301 100644 --- a/build/webm-muxer.js +++ b/build/webm-muxer.js @@ -146,8 +146,9 @@ var WebMMuxer = (() => { } }; var FileSystemWritableFileStreamTarget = class { - constructor(stream) { + constructor(stream, options) { this.stream = stream; + this.options = options; } }; @@ -403,22 +404,28 @@ var WebMMuxer = (() => { _sections = new WeakMap(); _lastFlushEnd = new WeakMap(); _ensureMonotonicity = new WeakMap(); - var CHUNK_SIZE = __pow(2, 24); + var DEFAULT_CHUNK_SIZE = __pow(2, 24); var MAX_CHUNKS_AT_ONCE = 2; - var _target3, _chunks, _lastFlushEnd2, _ensureMonotonicity2, _writeDataIntoChunks, writeDataIntoChunks_fn, _insertSectionIntoChunk, insertSectionIntoChunk_fn, _createChunk, createChunk_fn, _flushChunks, flushChunks_fn; + var _target3, _chunkSize, _chunks, _lastFlushEnd2, _ensureMonotonicity2, _writeDataIntoChunks, writeDataIntoChunks_fn, _insertSectionIntoChunk, insertSectionIntoChunk_fn, _createChunk, createChunk_fn, _flushChunks, flushChunks_fn; var ChunkedStreamTargetWriter = class extends Writer { constructor(target, ensureMonotonicity) { + var _a, _b; super(); __privateAdd(this, _writeDataIntoChunks); __privateAdd(this, _insertSectionIntoChunk); __privateAdd(this, _createChunk); __privateAdd(this, _flushChunks); __privateAdd(this, _target3, void 0); + __privateAdd(this, _chunkSize, void 0); __privateAdd(this, _chunks, []); __privateAdd(this, _lastFlushEnd2, 0); __privateAdd(this, _ensureMonotonicity2, void 0); __privateSet(this, _target3, target); + __privateSet(this, _chunkSize, (_b = (_a = target.options) == null ? void 0 : _a.chunkSize) != null ? _b : DEFAULT_CHUNK_SIZE); __privateSet(this, _ensureMonotonicity2, ensureMonotonicity); + if (!Number.isInteger(__privateGet(this, _chunkSize)) || __privateGet(this, _chunkSize) < __pow(2, 10)) { + throw new Error("Invalid StreamTarget options: chunkSize must be an integer not smaller than 1024."); + } } write(data) { __privateMethod(this, _writeDataIntoChunks, writeDataIntoChunks_fn).call(this, data, this.pos); @@ -432,24 +439,25 @@ var WebMMuxer = (() => { } }; _target3 = new WeakMap(); + _chunkSize = new WeakMap(); _chunks = new WeakMap(); _lastFlushEnd2 = new WeakMap(); _ensureMonotonicity2 = new WeakMap(); _writeDataIntoChunks = new WeakSet(); writeDataIntoChunks_fn = function(data, position) { - let chunkIndex = __privateGet(this, _chunks).findIndex((x) => x.start <= position && position < x.start + CHUNK_SIZE); + let chunkIndex = __privateGet(this, _chunks).findIndex((x) => x.start <= position && position < x.start + __privateGet(this, _chunkSize)); if (chunkIndex === -1) chunkIndex = __privateMethod(this, _createChunk, createChunk_fn).call(this, position); let chunk = __privateGet(this, _chunks)[chunkIndex]; let relativePosition = position - chunk.start; - let toWrite = data.subarray(0, Math.min(CHUNK_SIZE - relativePosition, data.byteLength)); + let toWrite = data.subarray(0, Math.min(__privateGet(this, _chunkSize) - relativePosition, data.byteLength)); chunk.data.set(toWrite, relativePosition); let section = { start: relativePosition, end: relativePosition + toWrite.byteLength }; __privateMethod(this, _insertSectionIntoChunk, insertSectionIntoChunk_fn).call(this, chunk, section); - if (chunk.written[0].start === 0 && chunk.written[0].end === CHUNK_SIZE) { + if (chunk.written[0].start === 0 && chunk.written[0].end === __privateGet(this, _chunkSize)) { chunk.shouldFlush = true; } if (__privateGet(this, _chunks).length > MAX_CHUNKS_AT_ONCE) { @@ -486,10 +494,10 @@ var WebMMuxer = (() => { }; _createChunk = new WeakSet(); createChunk_fn = function(includesPosition) { - let start = Math.floor(includesPosition / CHUNK_SIZE) * CHUNK_SIZE; + let start = Math.floor(includesPosition / __privateGet(this, _chunkSize)) * __privateGet(this, _chunkSize); let chunk = { start, - data: new Uint8Array(CHUNK_SIZE), + data: new Uint8Array(__privateGet(this, _chunkSize)), written: [], shouldFlush: false }; @@ -518,12 +526,15 @@ var WebMMuxer = (() => { }; var FileSystemWritableFileStreamTargetWriter = class extends ChunkedStreamTargetWriter { constructor(target, ensureMonotonicity) { + var _a; super(new StreamTarget( (data, position) => target.stream.write({ type: "write", data, position - }) + }), + void 0, + { chunkSize: (_a = target.options) == null ? void 0 : _a.chunkSize } ), ensureMonotonicity); } }; diff --git a/build/webm-muxer.min.js b/build/webm-muxer.min.js index f45897d..8e086f4 100644 --- a/build/webm-muxer.min.js +++ b/build/webm-muxer.min.js @@ -1,8 +1,8 @@ -"use strict";var WebMMuxer=(()=>{var At=Object.defineProperty;var _e=Object.getOwnPropertyDescriptor;var ve=Object.getOwnPropertyNames,oe=Object.getOwnPropertySymbols;var de=Object.prototype.hasOwnProperty,Ne=Object.prototype.propertyIsEnumerable;var b=Math.pow,le=(h,t,i)=>t in h?At(h,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):h[t]=i,fe=(h,t)=>{for(var i in t||={})de.call(t,i)&&le(h,i,t[i]);if(oe)for(var i of oe(t))Ne.call(t,i)&&le(h,i,t[i]);return h};var Oe=(h,t)=>{for(var i in t)At(h,i,{get:t[i],enumerable:!0})},He=(h,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of ve(t))!de.call(h,r)&&r!==i&&At(h,r,{get:()=>t[r],enumerable:!(s=_e(t,r))||s.enumerable});return h};var We=h=>He(At({},"__esModule",{value:!0}),h);var jt=(h,t,i)=>{if(!t.has(h))throw TypeError("Cannot "+i)};var e=(h,t,i)=>(jt(h,t,"read from private field"),i?i.call(h):t.get(h)),a=(h,t,i)=>{if(t.has(h))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(h):t.set(h,i)},u=(h,t,i,s)=>(jt(h,t,"write to private field"),s?s.call(h,i):t.set(h,i),i);var o=(h,t,i)=>(jt(h,t,"access private method"),i);var Ze={};Oe(Ze,{ArrayBufferTarget:()=>st,FileSystemWritableFileStreamTarget:()=>rt,Muxer:()=>Nt,StreamTarget:()=>P});var W=class{constructor(t){this.value=t}},V=class{constructor(t){this.value=t}};var Jt=h=>h<1<<8?1:h<1<<16?2:h<1<<24?3:h{if(h<(1<<7)-1)return 1;if(h<(1<<14)-1)return 2;if(h<(1<<21)-1)return 3;if(h<(1<<28)-1)return 4;if(h{let s=0;for(let r=t;r>c;s<<=1,s|=it}return s},me=(h,t,i,s)=>{for(let r=t;r>i-r-1<>8),e(this,m).setUint8(s++,t);break;case 3:e(this,m).setUint8(s++,1<<5|t>>16),e(this,m).setUint8(s++,t>>8),e(this,m).setUint8(s++,t);break;case 4:e(this,m).setUint8(s++,1<<4|t>>24),e(this,m).setUint8(s++,t>>16),e(this,m).setUint8(s++,t>>8),e(this,m).setUint8(s++,t);break;case 5:e(this,m).setUint8(s++,1<<3|t/b(2,32)&7),e(this,m).setUint8(s++,t>>24),e(this,m).setUint8(s++,t>>16),e(this,m).setUint8(s++,t>>8),e(this,m).setUint8(s++,t);break;case 6:e(this,m).setUint8(s++,1<<2|t/b(2,40)&3),e(this,m).setUint8(s++,t/b(2,32)|0),e(this,m).setUint8(s++,t>>24),e(this,m).setUint8(s++,t>>16),e(this,m).setUint8(s++,t>>8),e(this,m).setUint8(s++,t);break;default:throw new Error("Bad EBML VINT size "+i)}this.write(e(this,y).subarray(0,s))}writeEBML(t){var i,s;if(t!==null)if(t instanceof Uint8Array)this.write(t);else if(Array.isArray(t))for(let r of t)this.writeEBML(r);else if(this.offsets.set(t,this.pos),o(this,ot,Lt).call(this,t.id),Array.isArray(t.data)){let r=this.pos,n=t.size===-1?1:(i=t.size)!=null?i:4;t.size===-1?o(this,Vt,ce).call(this,255):this.seek(this.pos+n);let d=this.pos;if(this.dataOffsets.set(t,d),this.writeEBML(t.data),t.size!==-1){let c=this.pos-d,it=this.pos;this.seek(r),this.writeEBMLVarInt(c,n),this.seek(it)}}else if(typeof t.data=="number"){let r=(s=t.size)!=null?s:Jt(t.data);this.writeEBMLVarInt(r),o(this,ot,Lt).call(this,t.data,r)}else typeof t.data=="string"?(this.writeEBMLVarInt(t.data.length),o(this,Et,ge).call(this,t.data)):t.data instanceof Uint8Array?(this.writeEBMLVarInt(t.data.byteLength,t.size),this.write(t.data)):t.data instanceof W?(this.writeEBMLVarInt(4),o(this,Dt,be).call(this,t.data.value)):t.data instanceof V&&(this.writeEBMLVarInt(8),o(this,Pt,pe).call(this,t.data.value))}};y=new WeakMap,m=new WeakMap,Vt=new WeakSet,ce=function(t){e(this,m).setUint8(0,t),this.write(e(this,y).subarray(0,1))},Dt=new WeakSet,be=function(t){e(this,m).setFloat32(0,t,!1),this.write(e(this,y).subarray(0,4))},Pt=new WeakSet,pe=function(t){e(this,m).setFloat64(0,t,!1),this.write(e(this,y))},ot=new WeakSet,Lt=function(t,i=Jt(t)){let s=0;switch(i){case 6:e(this,m).setUint8(s++,t/b(2,40)|0);case 5:e(this,m).setUint8(s++,t/b(2,32)|0);case 4:e(this,m).setUint8(s++,t>>24);case 3:e(this,m).setUint8(s++,t>>16);case 2:e(this,m).setUint8(s++,t>>8);case 1:e(this,m).setUint8(s++,t);break;default:throw new Error("Bad UINT size "+i)}this.write(e(this,y).subarray(0,s))},Et=new WeakSet,ge=function(t){this.write(new Uint8Array(t.split("").map(i=>i.charCodeAt(0))))};var lt,A,K,dt,It,Ut=class extends at{constructor(i){super();a(this,dt);a(this,lt,void 0);a(this,A,new ArrayBuffer(b(2,16)));a(this,K,new Uint8Array(e(this,A)));u(this,lt,i)}write(i){o(this,dt,It).call(this,this.pos+i.byteLength),e(this,K).set(i,this.pos),this.pos+=i.byteLength}finalize(){o(this,dt,It).call(this,this.pos),e(this,lt).buffer=e(this,A).slice(0,this.pos)}};lt=new WeakMap,A=new WeakMap,K=new WeakMap,dt=new WeakSet,It=function(i){let s=e(this,A).byteLength;for(;sr.start-n.start);i.push({start:s[0].start,size:s[0].data.byteLength});for(let r=1;rH.start<=s&&sBe){for(let H=0;H=i.written[d+1].start;)i.written[d].end=Math.max(i.written[d].end,i.written[d+1].end),i.written.splice(d+1,1)},Rt=new WeakSet,ye=function(i){let r={start:Math.floor(i/B)*B,data:new Uint8Array(B),written:[],shouldFlush:!1};return e(this,p).push(r),e(this,p).sort((n,d)=>n.start-d.start),e(this,p).indexOf(r)},Z=new WeakSet,St=function(i=!1){for(let s=0;st.stream.write({type:"write",data:s,position:r})),i)}};var E=1,Mt=2,Ke=1,$e=2,ee=b(2,15),_t=b(2,12),xe="https://github.com/Vanilagy/webm-muxer",Ce=6,Te=5,Ye=["strict","offset","permissive"],f,l,q,G,g,R,M,U,_,x,v,w,N,Q,C,T,j,J,L,I,tt,gt,Ot,ke,Ht,Ae,Wt,Se,Bt,Ue,Kt,ze,$t,Ve,Yt,De,wt,ie,yt,se,Zt,Pe,O,pt,z,X,Xt,Ee,qt,Fe,xt,re,Gt,Re,k,F,et,vt,Ct,ae,Qt,Me,Tt,ne,kt,he,Nt=class{constructor(t){a(this,Ot);a(this,Ht);a(this,Wt);a(this,Bt);a(this,Kt);a(this,$t);a(this,Yt);a(this,wt);a(this,yt);a(this,Zt);a(this,O);a(this,z);a(this,Xt);a(this,qt);a(this,xt);a(this,Gt);a(this,k);a(this,et);a(this,Ct);a(this,Qt);a(this,Tt);a(this,kt);a(this,f,void 0);a(this,l,void 0);a(this,q,void 0);a(this,G,void 0);a(this,g,void 0);a(this,R,void 0);a(this,M,void 0);a(this,U,void 0);a(this,_,void 0);a(this,x,void 0);a(this,v,void 0);a(this,w,void 0);a(this,N,void 0);a(this,Q,0);a(this,C,[]);a(this,T,[]);a(this,j,void 0);a(this,J,void 0);a(this,L,-1);a(this,I,-1);a(this,tt,void 0);a(this,gt,!1);var s;o(this,Ot,ke).call(this,t),u(this,f,fe({type:"webm",firstTimestampBehavior:"strict"},t)),this.target=t.target;let i=!!e(this,f).streaming;if(t.target instanceof st)u(this,l,new Ut(t.target));else if(t.target instanceof P)u(this,l,(s=t.target.options)!=null&&s.chunked?new ht(t.target,i):new nt(t.target,i));else if(t.target instanceof rt)u(this,l,new zt(t.target,i));else throw new Error(`Invalid target: ${t.target}`);o(this,Ht,Ae).call(this)}addVideoChunk(t,i,s){let r=new Uint8Array(t.byteLength);t.copyTo(r),this.addVideoChunkRaw(r,t.type,s!=null?s:t.timestamp,i)}addVideoChunkRaw(t,i,s,r){if(o(this,kt,he).call(this),!e(this,f).video)throw new Error("No video track declared.");e(this,j)===void 0&&u(this,j,s),r&&o(this,Xt,Ee).call(this,r);let n=o(this,xt,re).call(this,t,i,s,E);for(e(this,f).video.codec==="V_VP9"&&o(this,qt,Fe).call(this,n),u(this,L,n.timestamp);e(this,T).length>0&&e(this,T)[0].timestamp<=n.timestamp;){let d=e(this,T).shift();o(this,k,F).call(this,d)}!e(this,f).audio||n.timestamp<=e(this,I)?o(this,k,F).call(this,n):e(this,C).push(n),o(this,O,pt).call(this)}addAudioChunk(t,i,s){let r=new Uint8Array(t.byteLength);t.copyTo(r),this.addAudioChunkRaw(r,t.type,s!=null?s:t.timestamp,i)}addAudioChunkRaw(t,i,s,r){if(o(this,kt,he).call(this),!e(this,f).audio)throw new Error("No audio track declared.");e(this,J)===void 0&&u(this,J,s),r!=null&&r.decoderConfig&&(e(this,f).streaming?u(this,x,o(this,et,vt).call(this,r.decoderConfig.description)):o(this,Ct,ae).call(this,e(this,x),r.decoderConfig.description));let n=o(this,xt,re).call(this,t,i,s,Mt);for(u(this,I,n.timestamp);e(this,C).length>0&&e(this,C)[0].timestamp<=n.timestamp;){let d=e(this,C).shift();o(this,k,F).call(this,d)}!e(this,f).video||n.timestamp<=e(this,L)?o(this,k,F).call(this,n):e(this,T).push(n),o(this,O,pt).call(this)}finalize(){for(;e(this,C).length>0;)o(this,k,F).call(this,e(this,C).shift());for(;e(this,T).length>0;)o(this,k,F).call(this,e(this,T).shift());if(e(this,f).streaming||o(this,Tt,ne).call(this),e(this,l).writeEBML(e(this,v)),!e(this,f).streaming){let t=e(this,l).pos,i=e(this,l).pos-e(this,z,X);e(this,l).seek(e(this,l).offsets.get(e(this,q))+4),e(this,l).writeEBMLVarInt(i,Ce),e(this,M).data=new V(e(this,Q)),e(this,l).seek(e(this,l).offsets.get(e(this,M))),e(this,l).writeEBML(e(this,M)),e(this,g).data[0].data[1].data=e(this,l).offsets.get(e(this,v))-e(this,z,X),e(this,g).data[1].data[1].data=e(this,l).offsets.get(e(this,G))-e(this,z,X),e(this,g).data[2].data[1].data=e(this,l).offsets.get(e(this,R))-e(this,z,X),e(this,l).seek(e(this,l).offsets.get(e(this,g))),e(this,l).writeEBML(e(this,g)),e(this,l).seek(t)}o(this,O,pt).call(this),e(this,l).finalize(),u(this,gt,!0)}};f=new WeakMap,l=new WeakMap,q=new WeakMap,G=new WeakMap,g=new WeakMap,R=new WeakMap,M=new WeakMap,U=new WeakMap,_=new WeakMap,x=new WeakMap,v=new WeakMap,w=new WeakMap,N=new WeakMap,Q=new WeakMap,C=new WeakMap,T=new WeakMap,j=new WeakMap,J=new WeakMap,L=new WeakMap,I=new WeakMap,tt=new WeakMap,gt=new WeakMap,Ot=new WeakSet,ke=function(t){if(t.type&&t.type!=="webm"&&t.type!=="matroska")throw new Error(`Invalid type: ${t.type}`);if(t.firstTimestampBehavior&&!Ye.includes(t.firstTimestampBehavior))throw new Error(`Invalid first timestamp behavior: ${t.firstTimestampBehavior}`)},Ht=new WeakSet,Ae=function(){o(this,Wt,Se).call(this),e(this,f).streaming||o(this,$t,Ve).call(this),o(this,Yt,De).call(this),o(this,Bt,Ue).call(this),o(this,Kt,ze).call(this),e(this,f).streaming||(o(this,wt,ie).call(this),o(this,yt,se).call(this)),o(this,Zt,Pe).call(this),o(this,O,pt).call(this)},Wt=new WeakSet,Se=function(){var i;let t={id:440786851,data:[{id:17030,data:1},{id:17143,data:1},{id:17138,data:4},{id:17139,data:8},{id:17026,data:(i=e(this,f).type)!=null?i:"webm"},{id:17031,data:2},{id:17029,data:2}]};e(this,l).writeEBML(t)},Bt=new WeakSet,Ue=function(){u(this,_,{id:236,size:4,data:new Uint8Array(_t)}),u(this,x,{id:236,size:4,data:new Uint8Array(_t)})},Kt=new WeakSet,ze=function(){u(this,U,{id:21936,data:[{id:21937,data:2},{id:21946,data:2},{id:21947,data:2},{id:21945,data:0}]})},$t=new WeakSet,Ve=function(){let t=new Uint8Array([28,83,187,107]),i=new Uint8Array([21,73,169,102]),s=new Uint8Array([22,84,174,107]),r={id:290298740,data:[{id:19899,data:[{id:21419,data:t},{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:s},{id:21420,size:5,data:0}]}]};u(this,g,r)},Yt=new WeakSet,De=function(){let t={id:17545,data:new V(0)};u(this,M,t);let i={id:357149030,data:[{id:2807729,data:1e6},{id:19840,data:xe},{id:22337,data:xe},e(this,f).streaming?null:t]};u(this,G,i)},wt=new WeakSet,ie=function(){let t={id:374648427,data:[]};u(this,R,t),e(this,f).video&&t.data.push({id:174,data:[{id:215,data:E},{id:29637,data:E},{id:131,data:Ke},{id:134,data:e(this,f).video.codec},e(this,_),e(this,f).video.frameRate?{id:2352003,data:1e9/e(this,f).video.frameRate}:null,{id:224,data:[{id:176,data:e(this,f).video.width},{id:186,data:e(this,f).video.height},e(this,f).video.alpha?{id:21440,data:1}:null,e(this,U)]}]}),e(this,f).audio&&(u(this,x,e(this,f).streaming?e(this,x)||null:{id:236,size:4,data:new Uint8Array(_t)}),t.data.push({id:174,data:[{id:215,data:Mt},{id:29637,data:Mt},{id:131,data:$e},{id:134,data:e(this,f).audio.codec},e(this,x),{id:225,data:[{id:181,data:new W(e(this,f).audio.sampleRate)},{id:159,data:e(this,f).audio.numberOfChannels},e(this,f).audio.bitDepth?{id:25188,data:e(this,f).audio.bitDepth}:null]}]}))},yt=new WeakSet,se=function(){let t={id:408125543,size:e(this,f).streaming?-1:Ce,data:[e(this,f).streaming?null:e(this,g),e(this,G),e(this,R)]};u(this,q,t),e(this,l).writeEBML(t)},Zt=new WeakSet,Pe=function(){u(this,v,{id:475249515,data:[]})},O=new WeakSet,pt=function(){e(this,l)instanceof nt&&e(this,l).flush()},z=new WeakSet,X=function(){return e(this,l).dataOffsets.get(e(this,q))},Xt=new WeakSet,Ee=function(t){if(!!t.decoderConfig){if(t.decoderConfig.colorSpace){let i=t.decoderConfig.colorSpace;if(u(this,tt,i),e(this,U).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)]}],!e(this,f).streaming){let s=e(this,l).pos;e(this,l).seek(e(this,l).offsets.get(e(this,U))),e(this,l).writeEBML(e(this,U)),e(this,l).seek(s)}}t.decoderConfig.description&&(e(this,f).streaming?u(this,_,o(this,et,vt).call(this,t.decoderConfig.description)):o(this,Ct,ae).call(this,e(this,_),t.decoderConfig.description))}},qt=new WeakSet,Fe=function(t){if(t.type!=="key"||!e(this,tt))return;let i=0;if(D(t.data,0,2)!==2)return;i+=2;let s=(D(t.data,i+1,i+2)<<1)+D(t.data,i+0,i+1);i+=2,s===3&&i++;let r=D(t.data,i+0,i+1);if(i++,r)return;let n=D(t.data,i+0,i+1);if(i++,n!==0)return;i+=2;let d=D(t.data,i+0,i+24);if(i+=24,d!==4817730)return;s>=2&&i++;let c={rgb:7,bt709:2,bt470bg:1,smpte170m:3}[e(this,tt).matrix];me(t.data,i+0,i+3,c)},xt=new WeakSet,re=function(t,i,s,r){let n=o(this,Gt,Re).call(this,s,r);return{data:t,type:i,timestamp:n,trackNumber:r}},Gt=new WeakSet,Re=function(t,i){let s=i===E?e(this,j):e(this,J),r=i===E?e(this,L):e(this,I);if(e(this,f).firstTimestampBehavior==="strict"&&r===-1&&t!==0)throw new Error(`The first chunk for your media track must have a timestamp of 0 (received ${t}). 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 St=Object.defineProperty;var Me=Object.getOwnPropertyDescriptor;var ve=Object.getOwnPropertyNames,oe=Object.getOwnPropertySymbols;var de=Object.prototype.hasOwnProperty,Ne=Object.prototype.propertyIsEnumerable;var b=Math.pow,le=(h,e,i)=>e in h?St(h,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):h[e]=i,ue=(h,e)=>{for(var i in e||={})de.call(e,i)&&le(h,i,e[i]);if(oe)for(var i of oe(e))Ne.call(e,i)&&le(h,i,e[i]);return h};var Oe=(h,e)=>{for(var i in e)St(h,i,{get:e[i],enumerable:!0})},He=(h,e,i,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of ve(e))!de.call(h,r)&&r!==i&&St(h,r,{get:()=>e[r],enumerable:!(s=Me(e,r))||s.enumerable});return h};var We=h=>He(St({},"__esModule",{value:!0}),h);var jt=(h,e,i)=>{if(!e.has(h))throw TypeError("Cannot "+i)};var t=(h,e,i)=>(jt(h,e,"read from private field"),i?i.call(h):e.get(h)),a=(h,e,i)=>{if(e.has(h))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(h):e.set(h,i)},f=(h,e,i,s)=>(jt(h,e,"write to private field"),s?s.call(h,i):e.set(h,i),i);var o=(h,e,i)=>(jt(h,e,"access private method"),i);var Xe={};Oe(Xe,{ArrayBufferTarget:()=>st,FileSystemWritableFileStreamTarget:()=>rt,Muxer:()=>Nt,StreamTarget:()=>E});var B=class{constructor(e){this.value=e}},D=class{constructor(e){this.value=e}};var Lt=h=>h<1<<8?1:h<1<<16?2:h<1<<24?3:h{if(h<(1<<7)-1)return 1;if(h<(1<<14)-1)return 2;if(h<(1<<21)-1)return 3;if(h<(1<<28)-1)return 4;if(h{let s=0;for(let r=e;r>c;s<<=1,s|=it}return s},me=(h,e,i,s)=>{for(let r=e;r>i-r-1<>8),t(this,m).setUint8(s++,e);break;case 3:t(this,m).setUint8(s++,1<<5|e>>16),t(this,m).setUint8(s++,e>>8),t(this,m).setUint8(s++,e);break;case 4:t(this,m).setUint8(s++,1<<4|e>>24),t(this,m).setUint8(s++,e>>16),t(this,m).setUint8(s++,e>>8),t(this,m).setUint8(s++,e);break;case 5:t(this,m).setUint8(s++,1<<3|e/b(2,32)&7),t(this,m).setUint8(s++,e>>24),t(this,m).setUint8(s++,e>>16),t(this,m).setUint8(s++,e>>8),t(this,m).setUint8(s++,e);break;case 6:t(this,m).setUint8(s++,1<<2|e/b(2,40)&3),t(this,m).setUint8(s++,e/b(2,32)|0),t(this,m).setUint8(s++,e>>24),t(this,m).setUint8(s++,e>>16),t(this,m).setUint8(s++,e>>8),t(this,m).setUint8(s++,e);break;default:throw new Error("Bad EBML VINT size "+i)}this.write(t(this,x).subarray(0,s))}writeEBML(e){var i,s;if(e!==null)if(e instanceof Uint8Array)this.write(e);else if(Array.isArray(e))for(let r of e)this.writeEBML(r);else if(this.offsets.set(e,this.pos),o(this,ot,Jt).call(this,e.id),Array.isArray(e.data)){let r=this.pos,n=e.size===-1?1:(i=e.size)!=null?i:4;e.size===-1?o(this,Vt,ce).call(this,255):this.seek(this.pos+n);let d=this.pos;if(this.dataOffsets.set(e,d),this.writeEBML(e.data),e.size!==-1){let c=this.pos-d,it=this.pos;this.seek(r),this.writeEBMLVarInt(c,n),this.seek(it)}}else if(typeof e.data=="number"){let r=(s=e.size)!=null?s:Lt(e.data);this.writeEBMLVarInt(r),o(this,ot,Jt).call(this,e.data,r)}else typeof e.data=="string"?(this.writeEBMLVarInt(e.data.length),o(this,Et,ge).call(this,e.data)):e.data instanceof Uint8Array?(this.writeEBMLVarInt(e.data.byteLength,e.size),this.write(e.data)):e.data instanceof B?(this.writeEBMLVarInt(4),o(this,Dt,be).call(this,e.data.value)):e.data instanceof D&&(this.writeEBMLVarInt(8),o(this,Pt,pe).call(this,e.data.value))}};x=new WeakMap,m=new WeakMap,Vt=new WeakSet,ce=function(e){t(this,m).setUint8(0,e),this.write(t(this,x).subarray(0,1))},Dt=new WeakSet,be=function(e){t(this,m).setFloat32(0,e,!1),this.write(t(this,x).subarray(0,4))},Pt=new WeakSet,pe=function(e){t(this,m).setFloat64(0,e,!1),this.write(t(this,x))},ot=new WeakSet,Jt=function(e,i=Lt(e)){let s=0;switch(i){case 6:t(this,m).setUint8(s++,e/b(2,40)|0);case 5:t(this,m).setUint8(s++,e/b(2,32)|0);case 4:t(this,m).setUint8(s++,e>>24);case 3:t(this,m).setUint8(s++,e>>16);case 2:t(this,m).setUint8(s++,e>>8);case 1:t(this,m).setUint8(s++,e);break;default:throw new Error("Bad UINT size "+i)}this.write(t(this,x).subarray(0,s))},Et=new WeakSet,ge=function(e){this.write(new Uint8Array(e.split("").map(i=>i.charCodeAt(0))))};var lt,A,K,dt,It,Ut=class extends at{constructor(i){super();a(this,dt);a(this,lt,void 0);a(this,A,new ArrayBuffer(b(2,16)));a(this,K,new Uint8Array(t(this,A)));f(this,lt,i)}write(i){o(this,dt,It).call(this,this.pos+i.byteLength),t(this,K).set(i,this.pos),this.pos+=i.byteLength}finalize(){o(this,dt,It).call(this,this.pos),t(this,lt).buffer=t(this,A).slice(0,this.pos)}};lt=new WeakMap,A=new WeakMap,K=new WeakMap,dt=new WeakSet,It=function(i){let s=t(this,A).byteLength;for(;sr.start-n.start);i.push({start:s[0].start,size:s[0].data.byteLength});for(let r=1;rW.start<=s&&sKe){for(let W=0;W=i.written[d+1].start;)i.written[d].end=Math.max(i.written[d].end,i.written[d+1].end),i.written.splice(d+1,1)},Rt=new WeakSet,ye=function(i){let r={start:Math.floor(i/t(this,g))*t(this,g),data:new Uint8Array(t(this,g)),written:[],shouldFlush:!1};return t(this,p).push(r),t(this,p).sort((n,d)=>n.start-d.start),t(this,p).indexOf(r)},Z=new WeakSet,At=function(i=!1){for(let s=0;se.stream.write({type:"write",data:r,position:n}),void 0,{chunkSize:(s=e.options)==null?void 0:s.chunkSize}),i)}};var F=1,_t=2,$e=1,Ye=2,ee=b(2,15),Mt=b(2,12),xe="https://github.com/Vanilagy/webm-muxer",ke=6,Ce=5,Ze=["strict","offset","permissive"],u,l,q,G,w,_,M,z,v,k,N,y,O,Q,C,T,j,L,J,I,tt,gt,Ot,Te,Ht,Se,Wt,Ae,Bt,Ue,Kt,ze,$t,Ve,Yt,De,wt,ie,yt,se,Zt,Pe,H,pt,V,X,Xt,Ee,qt,Fe,xt,re,Gt,Re,S,R,et,vt,kt,ae,Qt,_e,Ct,ne,Tt,he,Nt=class{constructor(e){a(this,Ot);a(this,Ht);a(this,Wt);a(this,Bt);a(this,Kt);a(this,$t);a(this,Yt);a(this,wt);a(this,yt);a(this,Zt);a(this,H);a(this,V);a(this,Xt);a(this,qt);a(this,xt);a(this,Gt);a(this,S);a(this,et);a(this,kt);a(this,Qt);a(this,Ct);a(this,Tt);a(this,u,void 0);a(this,l,void 0);a(this,q,void 0);a(this,G,void 0);a(this,w,void 0);a(this,_,void 0);a(this,M,void 0);a(this,z,void 0);a(this,v,void 0);a(this,k,void 0);a(this,N,void 0);a(this,y,void 0);a(this,O,void 0);a(this,Q,0);a(this,C,[]);a(this,T,[]);a(this,j,void 0);a(this,L,void 0);a(this,J,-1);a(this,I,-1);a(this,tt,void 0);a(this,gt,!1);var s;o(this,Ot,Te).call(this,e),f(this,u,ue({type:"webm",firstTimestampBehavior:"strict"},e)),this.target=e.target;let i=!!t(this,u).streaming;if(e.target instanceof st)f(this,l,new Ut(e.target));else if(e.target instanceof E)f(this,l,(s=e.target.options)!=null&&s.chunked?new ht(e.target,i):new nt(e.target,i));else if(e.target instanceof rt)f(this,l,new zt(e.target,i));else throw new Error(`Invalid target: ${e.target}`);o(this,Ht,Se).call(this)}addVideoChunk(e,i,s){let r=new Uint8Array(e.byteLength);e.copyTo(r),this.addVideoChunkRaw(r,e.type,s!=null?s:e.timestamp,i)}addVideoChunkRaw(e,i,s,r){if(o(this,Tt,he).call(this),!t(this,u).video)throw new Error("No video track declared.");t(this,j)===void 0&&f(this,j,s),r&&o(this,Xt,Ee).call(this,r);let n=o(this,xt,re).call(this,e,i,s,F);for(t(this,u).video.codec==="V_VP9"&&o(this,qt,Fe).call(this,n),f(this,J,n.timestamp);t(this,T).length>0&&t(this,T)[0].timestamp<=n.timestamp;){let d=t(this,T).shift();o(this,S,R).call(this,d)}!t(this,u).audio||n.timestamp<=t(this,I)?o(this,S,R).call(this,n):t(this,C).push(n),o(this,H,pt).call(this)}addAudioChunk(e,i,s){let r=new Uint8Array(e.byteLength);e.copyTo(r),this.addAudioChunkRaw(r,e.type,s!=null?s:e.timestamp,i)}addAudioChunkRaw(e,i,s,r){if(o(this,Tt,he).call(this),!t(this,u).audio)throw new Error("No audio track declared.");t(this,L)===void 0&&f(this,L,s),r!=null&&r.decoderConfig&&(t(this,u).streaming?f(this,k,o(this,et,vt).call(this,r.decoderConfig.description)):o(this,kt,ae).call(this,t(this,k),r.decoderConfig.description));let n=o(this,xt,re).call(this,e,i,s,_t);for(f(this,I,n.timestamp);t(this,C).length>0&&t(this,C)[0].timestamp<=n.timestamp;){let d=t(this,C).shift();o(this,S,R).call(this,d)}!t(this,u).video||n.timestamp<=t(this,J)?o(this,S,R).call(this,n):t(this,T).push(n),o(this,H,pt).call(this)}finalize(){for(;t(this,C).length>0;)o(this,S,R).call(this,t(this,C).shift());for(;t(this,T).length>0;)o(this,S,R).call(this,t(this,T).shift());if(t(this,u).streaming||o(this,Ct,ne).call(this),t(this,l).writeEBML(t(this,N)),!t(this,u).streaming){let e=t(this,l).pos,i=t(this,l).pos-t(this,V,X);t(this,l).seek(t(this,l).offsets.get(t(this,q))+4),t(this,l).writeEBMLVarInt(i,ke),t(this,M).data=new D(t(this,Q)),t(this,l).seek(t(this,l).offsets.get(t(this,M))),t(this,l).writeEBML(t(this,M)),t(this,w).data[0].data[1].data=t(this,l).offsets.get(t(this,N))-t(this,V,X),t(this,w).data[1].data[1].data=t(this,l).offsets.get(t(this,G))-t(this,V,X),t(this,w).data[2].data[1].data=t(this,l).offsets.get(t(this,_))-t(this,V,X),t(this,l).seek(t(this,l).offsets.get(t(this,w))),t(this,l).writeEBML(t(this,w)),t(this,l).seek(e)}o(this,H,pt).call(this),t(this,l).finalize(),f(this,gt,!0)}};u=new WeakMap,l=new WeakMap,q=new WeakMap,G=new WeakMap,w=new WeakMap,_=new WeakMap,M=new WeakMap,z=new WeakMap,v=new WeakMap,k=new WeakMap,N=new WeakMap,y=new WeakMap,O=new WeakMap,Q=new WeakMap,C=new WeakMap,T=new WeakMap,j=new WeakMap,L=new WeakMap,J=new WeakMap,I=new WeakMap,tt=new WeakMap,gt=new WeakMap,Ot=new WeakSet,Te=function(e){if(e.type&&e.type!=="webm"&&e.type!=="matroska")throw new Error(`Invalid type: ${e.type}`);if(e.firstTimestampBehavior&&!Ze.includes(e.firstTimestampBehavior))throw new Error(`Invalid first timestamp behavior: ${e.firstTimestampBehavior}`)},Ht=new WeakSet,Se=function(){o(this,Wt,Ae).call(this),t(this,u).streaming||o(this,$t,Ve).call(this),o(this,Yt,De).call(this),o(this,Bt,Ue).call(this),o(this,Kt,ze).call(this),t(this,u).streaming||(o(this,wt,ie).call(this),o(this,yt,se).call(this)),o(this,Zt,Pe).call(this),o(this,H,pt).call(this)},Wt=new WeakSet,Ae=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,u).type)!=null?i:"webm"},{id:17031,data:2},{id:17029,data:2}]};t(this,l).writeEBML(e)},Bt=new WeakSet,Ue=function(){f(this,v,{id:236,size:4,data:new Uint8Array(Mt)}),f(this,k,{id:236,size:4,data:new Uint8Array(Mt)})},Kt=new WeakSet,ze=function(){f(this,z,{id:21936,data:[{id:21937,data:2},{id:21946,data:2},{id:21947,data:2},{id:21945,data:0}]})},$t=new WeakSet,Ve=function(){let e=new Uint8Array([28,83,187,107]),i=new Uint8Array([21,73,169,102]),s=new Uint8Array([22,84,174,107]),r={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:s},{id:21420,size:5,data:0}]}]};f(this,w,r)},Yt=new WeakSet,De=function(){let e={id:17545,data:new D(0)};f(this,M,e);let i={id:357149030,data:[{id:2807729,data:1e6},{id:19840,data:xe},{id:22337,data:xe},t(this,u).streaming?null:e]};f(this,G,i)},wt=new WeakSet,ie=function(){let e={id:374648427,data:[]};f(this,_,e),t(this,u).video&&e.data.push({id:174,data:[{id:215,data:F},{id:29637,data:F},{id:131,data:$e},{id:134,data:t(this,u).video.codec},t(this,v),t(this,u).video.frameRate?{id:2352003,data:1e9/t(this,u).video.frameRate}:null,{id:224,data:[{id:176,data:t(this,u).video.width},{id:186,data:t(this,u).video.height},t(this,u).video.alpha?{id:21440,data:1}:null,t(this,z)]}]}),t(this,u).audio&&(f(this,k,t(this,u).streaming?t(this,k)||null:{id:236,size:4,data:new Uint8Array(Mt)}),e.data.push({id:174,data:[{id:215,data:_t},{id:29637,data:_t},{id:131,data:Ye},{id:134,data:t(this,u).audio.codec},t(this,k),{id:225,data:[{id:181,data:new B(t(this,u).audio.sampleRate)},{id:159,data:t(this,u).audio.numberOfChannels},t(this,u).audio.bitDepth?{id:25188,data:t(this,u).audio.bitDepth}:null]}]}))},yt=new WeakSet,se=function(){let e={id:408125543,size:t(this,u).streaming?-1:ke,data:[t(this,u).streaming?null:t(this,w),t(this,G),t(this,_)]};f(this,q,e),t(this,l).writeEBML(e)},Zt=new WeakSet,Pe=function(){f(this,N,{id:475249515,data:[]})},H=new WeakSet,pt=function(){t(this,l)instanceof nt&&t(this,l).flush()},V=new WeakSet,X=function(){return t(this,l).dataOffsets.get(t(this,q))},Xt=new WeakSet,Ee=function(e){if(!!e.decoderConfig){if(e.decoderConfig.colorSpace){let i=e.decoderConfig.colorSpace;if(f(this,tt,i),t(this,z).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,u).streaming){let s=t(this,l).pos;t(this,l).seek(t(this,l).offsets.get(t(this,z))),t(this,l).writeEBML(t(this,z)),t(this,l).seek(s)}}e.decoderConfig.description&&(t(this,u).streaming?f(this,v,o(this,et,vt).call(this,e.decoderConfig.description)):o(this,kt,ae).call(this,t(this,v),e.decoderConfig.description))}},qt=new WeakSet,Fe=function(e){if(e.type!=="key"||!t(this,tt))return;let i=0;if(P(e.data,0,2)!==2)return;i+=2;let s=(P(e.data,i+1,i+2)<<1)+P(e.data,i+0,i+1);i+=2,s===3&&i++;let r=P(e.data,i+0,i+1);if(i++,r)return;let n=P(e.data,i+0,i+1);if(i++,n!==0)return;i+=2;let d=P(e.data,i+0,i+24);if(i+=24,d!==4817730)return;s>=2&&i++;let c={rgb:7,bt709:2,bt470bg:1,smpte170m:3}[t(this,tt).matrix];me(e.data,i+0,i+3,c)},xt=new WeakSet,re=function(e,i,s,r){let n=o(this,Gt,Re).call(this,s,r);return{data:e,type:i,timestamp:n,trackNumber:r}},Gt=new WeakSet,Re=function(e,i){let s=i===F?t(this,j):t(this,L),r=i===F?t(this,J):t(this,I);if(t(this,u).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'. -`);if(e(this,f).firstTimestampBehavior==="offset"&&(t-=s),t=ee)throw new Error(`Current Matroska cluster exceeded its maximum allowed length of ${ee} milliseconds. In order to produce a correct WebM file, you must pass in a video key frame at least every ${ee} milliseconds.`);let r=(t.trackNumber===E||!e(this,f).video)&&t.type==="key"&&i-e(this,N)>=1e3;(!e(this,w)||r)&&o(this,Qt,Me).call(this,i);let n=new Uint8Array(4),d=new DataView(n.buffer);d.setUint8(0,128|t.trackNumber),d.setUint16(1,i-e(this,N),!1),d.setUint8(3,Number(t.type==="key")<<7);let c={id:163,data:[n,t.data]};e(this,l).writeEBML(c),u(this,Q,Math.max(e(this,Q),i))},et=new WeakSet,vt=function(t){return{id:25506,size:4,data:new Uint8Array(t)}},Ct=new WeakSet,ae=function(t,i){let s=e(this,l).pos;e(this,l).seek(e(this,l).offsets.get(t)),t=[o(this,et,vt).call(this,i),{id:236,size:4,data:new Uint8Array(_t-2-4-i.byteLength)}],e(this,l).writeEBML(t),e(this,l).seek(s)},Qt=new WeakSet,Me=function(t){e(this,w)&&!e(this,f).streaming&&o(this,Tt,ne).call(this),u(this,w,{id:524531317,size:e(this,f).streaming?-1:Te,data:[{id:231,data:t}]}),e(this,l).writeEBML(e(this,w)),u(this,N,t);let i=e(this,l).offsets.get(e(this,w))-e(this,z,X);e(this,v).data.push({id:187,data:[{id:179,data:t},e(this,f).video?{id:183,data:[{id:247,data:E},{id:241,data:i}]}:null,e(this,f).audio?{id:183,data:[{id:247,data:Mt},{id:241,data:i}]}:null]})},Tt=new WeakSet,ne=function(){let t=e(this,l).pos-e(this,l).dataOffsets.get(e(this,w)),i=e(this,l).pos;e(this,l).seek(e(this,l).offsets.get(e(this,w))+4),e(this,l).writeEBMLVarInt(t,Te),e(this,l).seek(i)},kt=new WeakSet,he=function(){if(e(this,gt))throw new Error("Cannot add new video or audio chunks after the file has been finalized.")};return We(Ze);})(); +`);if(t(this,u).firstTimestampBehavior==="offset"&&(e-=s),e=ee)throw new Error(`Current Matroska cluster exceeded its maximum allowed length of ${ee} milliseconds. In order to produce a correct WebM file, you must pass in a video key frame at least every ${ee} milliseconds.`);let r=(e.trackNumber===F||!t(this,u).video)&&e.type==="key"&&i-t(this,O)>=1e3;(!t(this,y)||r)&&o(this,Qt,_e).call(this,i);let n=new Uint8Array(4),d=new DataView(n.buffer);d.setUint8(0,128|e.trackNumber),d.setUint16(1,i-t(this,O),!1),d.setUint8(3,Number(e.type==="key")<<7);let c={id:163,data:[n,e.data]};t(this,l).writeEBML(c),f(this,Q,Math.max(t(this,Q),i))},et=new WeakSet,vt=function(e){return{id:25506,size:4,data:new Uint8Array(e)}},kt=new WeakSet,ae=function(e,i){let s=t(this,l).pos;t(this,l).seek(t(this,l).offsets.get(e)),e=[o(this,et,vt).call(this,i),{id:236,size:4,data:new Uint8Array(Mt-2-4-i.byteLength)}],t(this,l).writeEBML(e),t(this,l).seek(s)},Qt=new WeakSet,_e=function(e){t(this,y)&&!t(this,u).streaming&&o(this,Ct,ne).call(this),f(this,y,{id:524531317,size:t(this,u).streaming?-1:Ce,data:[{id:231,data:e}]}),t(this,l).writeEBML(t(this,y)),f(this,O,e);let i=t(this,l).offsets.get(t(this,y))-t(this,V,X);t(this,N).data.push({id:187,data:[{id:179,data:e},t(this,u).video?{id:183,data:[{id:247,data:F},{id:241,data:i}]}:null,t(this,u).audio?{id:183,data:[{id:247,data:_t},{id:241,data:i}]}:null]})},Ct=new WeakSet,ne=function(){let e=t(this,l).pos-t(this,l).dataOffsets.get(t(this,y)),i=t(this,l).pos;t(this,l).seek(t(this,l).offsets.get(t(this,y))+4),t(this,l).writeEBMLVarInt(e,Ce),t(this,l).seek(i)},Tt=new WeakSet,he=function(){if(t(this,gt))throw new Error("Cannot add new video or audio chunks after the file has been finalized.")};return We(Xe);})(); if (typeof module === "object" && typeof module.exports === "object") { module.exports.Muxer = WebMMuxer.Muxer; module.exports.ArrayBufferTarget = WebMMuxer.ArrayBufferTarget;