Files

8 lines
31 KiB
JavaScript

/**
* Bundled by jsDelivr using Rollup v2.79.2 and Terser v5.39.0.
* Original file: /npm/mp4-muxer@5.2.2/build/mp4-muxer.mjs
*
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
*/
var e,t,i,s,a,r,n,o,h=(e,t,i)=>{if(!t.has(e))throw TypeError("Cannot "+i)},l=(e,t,i)=>(h(e,t,"read from private field"),i?i.call(e):t.get(e)),d=(e,t,i)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,i)},f=(e,t,i,s)=>(h(e,t,"write to private field"),s?s.call(e,i):t.set(e,i),i),u=(e,t,i)=>(h(e,t,"access private method"),i),m=new Uint8Array(8),p=new DataView(m.buffer),c=e=>[(e%256+256)%256],w=e=>(p.setUint16(0,e,!1),[m[0],m[1]]),g=e=>(p.setUint32(0,e,!1),[m[1],m[2],m[3]]),b=e=>(p.setUint32(0,e,!1),[m[0],m[1],m[2],m[3]]),y=e=>(p.setUint32(0,Math.floor(e/2**32),!1),p.setUint32(4,e,!1),[m[0],m[1],m[2],m[3],m[4],m[5],m[6],m[7]]),k=e=>(p.setInt16(0,256*e,!1),[m[0],m[1]]),C=e=>(p.setInt32(0,65536*e,!1),[m[0],m[1],m[2],m[3]]),T=e=>(p.setInt32(0,2**30*e,!1),[m[0],m[1],m[2],m[3]]),v=(e,t=!1)=>{let i=Array(e.length).fill(null).map(((t,i)=>e.charCodeAt(i)));return t&&i.push(0),i},S=e=>e&&e[e.length-1],x=e=>{let t;for(let i of e)(!t||i.presentationTimestamp>t.presentationTimestamp)&&(t=i);return t},z=(e,t,i=!0)=>{let s=e*t;return i?Math.round(s):s},E=e=>{let t=e*(Math.PI/180),i=Math.cos(t),s=Math.sin(t);return[i,s,0,-s,i,0,0,0,1]},A=E(0),M=e=>[C(e[0]),C(e[1]),T(e[2]),C(e[3]),C(e[4]),T(e[5]),C(e[6]),C(e[7]),T(e[8])],W=e=>e?"object"!=typeof e?e:Array.isArray(e)?e.map(W):Object.fromEntries(Object.entries(e).map((([e,t])=>[e,W(t)]))):e,U=e=>e>=0&&e<2**32,B=(e,t,i)=>({type:e,contents:t&&new Uint8Array(t.flat(10)),children:i}),D=(e,t,i,s,a)=>B(e,[c(t),g(i),s??[]],a),O=e=>({type:"mdat",largeSize:e}),R=(e,t,i=!1)=>B("moov",null,[I(t,e),...e.map((e=>N(e,t))),i?he(e):null]),I=(e,t)=>{let i=z(Math.max(0,...t.filter((e=>e.samples.length>0)).map((e=>{const t=x(e.samples);return t.presentationTimestamp+t.duration}))),zt),s=Math.max(...t.map((e=>e.id)))+1,a=!U(e)||!U(i),r=a?y:b;return D("mvhd",+a,0,[r(e),r(e),b(zt),r(i),C(1),k(1),Array(10).fill(0),M(A),Array(24).fill(0),b(s)])},N=(e,t)=>B("trak",null,[V(e,t),j(e,t)]),V=(e,t)=>{let i,s=x(e.samples),a=z(s?s.presentationTimestamp+s.duration:0,zt),r=!U(t)||!U(a),n=r?y:b;return i="video"===e.info.type?"number"==typeof e.info.rotation?E(e.info.rotation):e.info.rotation:A,D("tkhd",+r,3,[n(t),n(t),b(e.id),b(0),n(a),Array(8).fill(0),w(0),w(0),k("audio"===e.info.type?1:0),w(0),M(i),C("video"===e.info.type?e.info.width:0),C("video"===e.info.type?e.info.height:0)])},j=(e,t)=>B("mdia",null,[L(e,t),F("video"===e.info.type?"vide":"soun"),$(e)]),L=(e,t)=>{let i=x(e.samples),s=z(i?i.presentationTimestamp+i.duration:0,e.timescale),a=!U(t)||!U(s),r=a?y:b;return D("mdhd",+a,0,[r(t),r(t),b(e.timescale),r(s),w(21956),w(0)])},F=e=>D("hdlr",0,0,[v("mhlr"),v(e),b(0),b(0),b(0),v("mp4-muxer-hdlr",!0)]),$=e=>B("minf",null,["video"===e.info.type?H():P(),q(),J(e)]),H=()=>D("vmhd",0,1,[w(0),w(0),w(0),w(0)]),P=()=>D("smhd",0,0,[w(0),w(0)]),q=()=>B("dinf",null,[_()]),_=()=>D("dref",0,0,[b(1)],[G()]),G=()=>D("url ",0,1),J=e=>{const t=e.compositionTimeOffsetTable.length>1||e.compositionTimeOffsetTable.some((e=>0!==e.sampleCompositionTimeOffset));return B("stbl",null,[K(e),ie(e),se(e),ae(e),re(e),ne(e),t?oe(e):null])},K=e=>D("stsd",0,0,[b(1)],["video"===e.info.type?Q(ye[e.info.codec],e):te(Ce[e.info.codec],e)]),Q=(e,t)=>{return B(e,[Array(6).fill(0),w(1),w(0),w(0),Array(12).fill(0),w(t.info.width),w(t.info.height),b(4718592),b(4718592),b(0),w(1),Array(32).fill(0),w(24),(i=65535,p.setInt16(0,i,!1),[m[0],m[1]])],[ke[t.info.codec](t),t.info.decoderConfig.colorSpace?ee(t):null]);var i},X={bt709:1,bt470bg:5,smpte170m:6},Y={bt709:1,smpte170m:6,"iec61966-2-1":13},Z={rgb:0,bt709:1,bt470bg:5,smpte170m:6},ee=e=>B("colr",[v("nclx"),w(X[e.info.decoderConfig.colorSpace.primaries]),w(Y[e.info.decoderConfig.colorSpace.transfer]),w(Z[e.info.decoderConfig.colorSpace.matrix]),c((e.info.decoderConfig.colorSpace.fullRange?1:0)<<7)]),te=(e,t)=>B(e,[Array(6).fill(0),w(1),w(0),w(0),b(0),w(t.info.numberOfChannels),w(16),w(0),w(0),C(t.info.sampleRate)],[Te[t.info.codec](t)]),ie=e=>D("stts",0,0,[b(e.timeToSampleTable.length),e.timeToSampleTable.map((e=>[b(e.sampleCount),b(e.sampleDelta)]))]),se=e=>{if(e.samples.every((e=>"key"===e.type)))return null;let t=[...e.samples.entries()].filter((([,e])=>"key"===e.type));return D("stss",0,0,[b(t.length),t.map((([e])=>b(e+1)))])},ae=e=>D("stsc",0,0,[b(e.compactlyCodedChunkTable.length),e.compactlyCodedChunkTable.map((e=>[b(e.firstChunk),b(e.samplesPerChunk),b(1)]))]),re=e=>D("stsz",0,0,[b(0),b(e.samples.length),e.samples.map((e=>b(e.size)))]),ne=e=>e.finalizedChunks.length>0&&S(e.finalizedChunks).offset>=2**32?D("co64",0,0,[b(e.finalizedChunks.length),e.finalizedChunks.map((e=>y(e.offset)))]):D("stco",0,0,[b(e.finalizedChunks.length),e.finalizedChunks.map((e=>b(e.offset)))]),oe=e=>D("ctts",0,0,[b(e.compositionTimeOffsetTable.length),e.compositionTimeOffsetTable.map((e=>[b(e.sampleCount),b(e.sampleCompositionTimeOffset)]))]),he=e=>B("mvex",null,e.map(le)),le=e=>D("trex",0,0,[b(e.id),b(1),b(0),b(0),b(0)]),de=(e,t)=>B("moof",null,[fe(e),...t.map(me)]),fe=e=>D("mfhd",0,0,[b(e)]),ue=e=>{let t=0,i=0,s="delta"===e.type;return i|=+s,t|=s?1:2,t<<24|i<<16},me=e=>B("traf",null,[pe(e),ce(e),we(e)]),pe=e=>{let t=0;t|=8,t|=16,t|=32,t|=131072;let i=e.currentChunk.samples[1]??e.currentChunk.samples[0],s={duration:i.timescaleUnitsToNextSample,size:i.size,flags:ue(i)};return D("tfhd",0,131128,[b(e.id),b(s.duration),b(s.size),b(s.flags)])},ce=e=>D("tfdt",1,0,[y(z(e.currentChunk.startTimestamp,e.timescale))]),we=e=>{let t=e.currentChunk.samples.map((e=>e.timescaleUnitsToNextSample)),i=e.currentChunk.samples.map((e=>e.size)),s=e.currentChunk.samples.map(ue),a=e.currentChunk.samples.map((t=>z(t.presentationTimestamp-t.decodeTimestamp,e.timescale))),r=new Set(t),n=new Set(i),o=new Set(s),h=new Set(a),l=2===o.size&&s[0]!==s[1],d=r.size>1,f=n.size>1,u=!l&&o.size>1,c=h.size>1||[...h].some((e=>0!==e)),w=0;return w|=1,w|=4*+l,w|=256*+d,w|=512*+f,w|=1024*+u,w|=2048*+c,D("trun",1,w,[b(e.currentChunk.samples.length),b(e.currentChunk.offset-e.currentChunk.moofOffset||0),l?b(s[0]):[],e.currentChunk.samples.map(((e,r)=>{return[d?b(t[r]):[],f?b(i[r]):[],u?b(s[r]):[],c?(n=a[r],p.setInt32(0,n,!1),[m[0],m[1],m[2],m[3]]):[]];var n}))])},ge=(e,t)=>D("tfra",1,0,[b(e.id),b(63),b(e.finalizedChunks.length),e.finalizedChunks.map((i=>[y(z(i.startTimestamp,e.timescale)),y(i.moofOffset),b(t+1),b(1),b(1)]))]),be=()=>D("mfro",0,0,[b(0)]),ye={avc:"avc1",hevc:"hvc1",vp9:"vp09",av1:"av01"},ke={avc:e=>e.info.decoderConfig&&B("avcC",[...new Uint8Array(e.info.decoderConfig.description)]),hevc:e=>e.info.decoderConfig&&B("hvcC",[...new Uint8Array(e.info.decoderConfig.description)]),vp9:e=>{if(!e.info.decoderConfig)return null;let t=e.info.decoderConfig;if(!t.colorSpace)throw new Error("'colorSpace' is required in the decoder config for VP9.");let i=t.codec.split("."),s=Number(i[1]),a=Number(i[2]),r=0+(Number(i[3])<<4)+Number(t.colorSpace.fullRange);return D("vpcC",1,0,[c(s),c(a),c(r),c(2),c(2),c(2),w(0)])},av1:()=>B("av1C",[129,0,0,0])},Ce={aac:"mp4a",opus:"Opus"},Te={aac:e=>{let t=new Uint8Array(e.info.decoderConfig.description);return D("esds",0,0,[b(58753152),c(32+t.byteLength),w(1),c(0),b(75530368),c(18+t.byteLength),c(64),c(21),g(0),b(130071),b(130071),b(92307584),c(t.byteLength),...t,b(109084800),c(1),c(2)])},opus:e=>{let t=3840,i=0;const s=e.info.decoderConfig?.description;if(s){if(s.byteLength<18)throw new TypeError("Invalid decoder description provided for Opus; must be at least 18 bytes long.");const e=ArrayBuffer.isView(s)?new DataView(s.buffer,s.byteOffset,s.byteLength):new DataView(s);t=e.getUint16(10,!0),i=e.getInt16(14,!0)}return B("dOps",[c(0),c(e.info.numberOfChannels),w(t),b(e.info.sampleRate),k(i),c(0)])}},ve=class{},Se=class extends ve{constructor(){super(...arguments),this.buffer=null}},xe=class extends ve{constructor(e){if(super(),this.options=e,"object"!=typeof e)throw new TypeError("StreamTarget requires an options object to be passed to its constructor.");if(e.onData){if("function"!=typeof e.onData)throw new TypeError("options.onData, when provided, must be a function.");if(e.onData.length<2)throw new TypeError("options.onData, when provided, must be a function that takes in at least two arguments (data and position). Ignoring the position argument, which specifies the byte offset at which the data is to be written, can lead to broken outputs.")}if(void 0!==e.chunked&&"boolean"!=typeof e.chunked)throw new TypeError("options.chunked, when provided, must be a boolean.");if(void 0!==e.chunkSize&&(!Number.isInteger(e.chunkSize)||e.chunkSize<1024))throw new TypeError("options.chunkSize, when provided, must be an integer and not smaller than 1024.")}},ze=class extends ve{constructor(e,t){if(super(),this.stream=e,this.options=t,!(e instanceof FileSystemWritableFileStream))throw new TypeError("FileSystemWritableFileStreamTarget requires a FileSystemWritableFileStream instance.");if(void 0!==t&&"object"!=typeof t)throw new TypeError("FileSystemWritableFileStreamTarget's options, when provided, must be an object.");if(t&&void 0!==t.chunkSize&&(!Number.isInteger(t.chunkSize)||t.chunkSize<=0))throw new TypeError("options.chunkSize, when provided, must be a positive integer")}},Ee=class{constructor(){this.pos=0,d(this,e,new Uint8Array(8)),d(this,t,new DataView(l(this,e).buffer)),this.offsets=new WeakMap}seek(e){this.pos=e}writeU32(i){l(this,t).setUint32(0,i,!1),this.write(l(this,e).subarray(0,4))}writeU64(i){l(this,t).setUint32(0,Math.floor(i/2**32),!1),l(this,t).setUint32(4,i,!1),this.write(l(this,e).subarray(0,8))}writeAscii(i){for(let s=0;s<i.length;s++)l(this,t).setUint8(s%8,i.charCodeAt(s)),s%8==7&&this.write(l(this,e));i.length%8!=0&&this.write(l(this,e).subarray(0,i.length%8))}writeBox(e){if(this.offsets.set(e,this.pos),e.contents&&!e.children)this.writeBoxHeader(e,e.size??e.contents.byteLength+8),this.write(e.contents);else{let t=this.pos;if(this.writeBoxHeader(e,0),e.contents&&this.write(e.contents),e.children)for(let t of e.children)t&&this.writeBox(t);let i=this.pos,s=e.size??i-t;this.seek(t),this.writeBoxHeader(e,s),this.seek(i)}}writeBoxHeader(e,t){this.writeU32(e.largeSize?1:t),this.writeAscii(e.type),e.largeSize&&this.writeU64(t)}measureBoxHeader(e){return 8+(e.largeSize?8:0)}patchBox(e){let t=this.pos;this.seek(this.offsets.get(e)),this.writeBox(e),this.seek(t)}measureBox(e){if(e.contents&&!e.children){return this.measureBoxHeader(e)+e.contents.byteLength}{let t=this.measureBoxHeader(e);if(e.contents&&(t+=e.contents.byteLength),e.children)for(let i of e.children)i&&(t+=this.measureBox(i));return t}}};e=new WeakMap,t=new WeakMap;var Ae=class extends Ee{constructor(e){super(),d(this,n),d(this,i,void 0),d(this,s,new ArrayBuffer(65536)),d(this,a,new Uint8Array(l(this,s))),d(this,r,0),f(this,i,e)}write(e){u(this,n,o).call(this,this.pos+e.byteLength),l(this,a).set(e,this.pos),this.pos+=e.byteLength,f(this,r,Math.max(l(this,r),this.pos))}finalize(){u(this,n,o).call(this,this.pos),l(this,i).buffer=l(this,s).slice(0,Math.max(l(this,r),this.pos))}};i=new WeakMap,s=new WeakMap,a=new WeakMap,r=new WeakMap,n=new WeakSet,o=function(e){let t=l(this,s).byteLength;for(;t<e;)t*=2;if(t===l(this,s).byteLength)return;let i=new ArrayBuffer(t),r=new Uint8Array(i);r.set(l(this,a),0),f(this,s,i),f(this,a,r)};var Me,We,Ue,Be,De,Oe,Re,Ie,Ne,Ve,je,Le,Fe,$e=class extends Ee{constructor(e){super(),d(this,Oe),d(this,Ie),d(this,Ve),d(this,Le),d(this,Me,void 0),d(this,We,[]),d(this,Ue,void 0),d(this,Be,void 0),d(this,De,[]),f(this,Me,e),f(this,Ue,e.options?.chunked??!1),f(this,Be,e.options?.chunkSize??16777216)}write(e){l(this,We).push({data:e.slice(),start:this.pos}),this.pos+=e.byteLength}flush(){if(0===l(this,We).length)return;let e=[],t=[...l(this,We)].sort(((e,t)=>e.start-t.start));e.push({start:t[0].start,size:t[0].data.byteLength});for(let i=1;i<t.length;i++){let s=e[e.length-1],a=t[i];a.start<=s.start+s.size?s.size=Math.max(s.size,a.start+a.data.byteLength-s.start):e.push({start:a.start,size:a.data.byteLength})}for(let t of e){t.data=new Uint8Array(t.size);for(let e of l(this,We))t.start<=e.start&&e.start<t.start+t.size&&t.data.set(e.data,e.start-t.start);l(this,Ue)?(u(this,Oe,Re).call(this,t.data,t.start),u(this,Le,Fe).call(this)):l(this,Me).options.onData?.(t.data,t.start)}l(this,We).length=0}finalize(){l(this,Ue)&&u(this,Le,Fe).call(this,!0)}};Me=new WeakMap,We=new WeakMap,Ue=new WeakMap,Be=new WeakMap,De=new WeakMap,Oe=new WeakSet,Re=function(e,t){let i=l(this,De).findIndex((e=>e.start<=t&&t<e.start+l(this,Be)));-1===i&&(i=u(this,Ve,je).call(this,t));let s=l(this,De)[i],a=t-s.start,r=e.subarray(0,Math.min(l(this,Be)-a,e.byteLength));s.data.set(r,a);let n={start:a,end:a+r.byteLength};if(u(this,Ie,Ne).call(this,s,n),0===s.written[0].start&&s.written[0].end===l(this,Be)&&(s.shouldFlush=!0),l(this,De).length>2){for(let e=0;e<l(this,De).length-1;e++)l(this,De)[e].shouldFlush=!0;u(this,Le,Fe).call(this)}r.byteLength<e.byteLength&&u(this,Oe,Re).call(this,e.subarray(r.byteLength),t+r.byteLength)},Ie=new WeakSet,Ne=function(e,t){let i=0,s=e.written.length-1,a=-1;for(;i<=s;){let r=Math.floor(i+(s-i+1)/2);e.written[r].start<=t.start?(i=r+1,a=r):s=r-1}for(e.written.splice(a+1,0,t),(-1===a||e.written[a].end<t.start)&&a++;a<e.written.length-1&&e.written[a].end>=e.written[a+1].start;)e.written[a].end=Math.max(e.written[a].end,e.written[a+1].end),e.written.splice(a+1,1)},Ve=new WeakSet,je=function(e){let t={start:Math.floor(e/l(this,Be))*l(this,Be),data:new Uint8Array(l(this,Be)),written:[],shouldFlush:!1};return l(this,De).push(t),l(this,De).sort(((e,t)=>e.start-t.start)),l(this,De).indexOf(t)},Le=new WeakSet,Fe=function(e=!1){for(let t=0;t<l(this,De).length;t++){let i=l(this,De)[t];if(i.shouldFlush||e){for(let e of i.written)l(this,Me).options.onData?.(i.data.subarray(e.start,e.end),i.start+e.start);l(this,De).splice(t--,1)}}};var He,Pe,qe,_e,Ge,Je,Ke,Qe,Xe,Ye,Ze,et,tt,it,st,at,rt,nt,ot,ht,lt,dt,ft,ut,mt,pt,ct,wt,gt,bt,yt,kt,Ct,Tt,vt,St,xt=class extends $e{constructor(e){super(new xe({onData:(t,i)=>e.stream.write({type:"write",data:t,position:i}),chunked:!0,chunkSize:e.options?.chunkSize}))}},zt=1e3,Et=["avc","hevc","vp9","av1"],At=["aac","opus"],Mt=["strict","offset","cross-track-offset"],Wt=class{constructor(e){if(d(this,tt),d(this,st),d(this,rt),d(this,ot),d(this,lt),d(this,ft),d(this,mt),d(this,ct),d(this,gt),d(this,yt),d(this,Ct),d(this,vt),d(this,He,void 0),d(this,Pe,void 0),d(this,qe,void 0),d(this,_e,void 0),d(this,Ge,null),d(this,Je,null),d(this,Ke,Math.floor(Date.now()/1e3)+2082844800),d(this,Qe,[]),d(this,Xe,1),d(this,Ye,[]),d(this,Ze,[]),d(this,et,!1),u(this,tt,it).call(this,e),e.video=W(e.video),e.audio=W(e.audio),e.fastStart=W(e.fastStart),this.target=e.target,f(this,He,{firstTimestampBehavior:"strict",...e}),e.target instanceof Se)f(this,Pe,new Ae(e.target));else if(e.target instanceof xe)f(this,Pe,new $e(e.target));else{if(!(e.target instanceof ze))throw new Error(`Invalid target: ${e.target}`);f(this,Pe,new xt(e.target))}u(this,ot,ht).call(this),u(this,st,at).call(this)}addVideoChunk(e,t,i,s){if(!(e instanceof EncodedVideoChunk))throw new TypeError("addVideoChunk's first argument (sample) must be of type EncodedVideoChunk.");if(t&&"object"!=typeof t)throw new TypeError("addVideoChunk's second argument (meta), when provided, must be an object.");if(void 0!==i&&(!Number.isFinite(i)||i<0))throw new TypeError("addVideoChunk's third argument (timestamp), when provided, must be a non-negative real number.");if(void 0!==s&&!Number.isFinite(s))throw new TypeError("addVideoChunk's fourth argument (compositionTimeOffset), when provided, must be a real number.");let a=new Uint8Array(e.byteLength);e.copyTo(a),this.addVideoChunkRaw(a,e.type,i??e.timestamp,e.duration,t,s)}addVideoChunkRaw(e,t,i,s,a,r){if(!(e instanceof Uint8Array))throw new TypeError("addVideoChunkRaw's first argument (data) must be an instance of Uint8Array.");if("key"!==t&&"delta"!==t)throw new TypeError("addVideoChunkRaw's second argument (type) must be either 'key' or 'delta'.");if(!Number.isFinite(i)||i<0)throw new TypeError("addVideoChunkRaw's third argument (timestamp) must be a non-negative real number.");if(!Number.isFinite(s)||s<0)throw new TypeError("addVideoChunkRaw's fourth argument (duration) must be a non-negative real number.");if(a&&"object"!=typeof a)throw new TypeError("addVideoChunkRaw's fifth argument (meta), when provided, must be an object.");if(void 0!==r&&!Number.isFinite(r))throw new TypeError("addVideoChunkRaw's sixth argument (compositionTimeOffset), when provided, must be a real number.");if(u(this,vt,St).call(this),!l(this,He).video)throw new Error("No video track declared.");if("object"==typeof l(this,He).fastStart&&l(this,Ge).samples.length===l(this,He).fastStart.expectedVideoChunks)throw new Error(`Cannot add more video chunks than specified in 'fastStart' (${l(this,He).fastStart.expectedVideoChunks}).`);let n=u(this,ft,ut).call(this,l(this,Ge),e,t,i,s,a,r);if("fragmented"===l(this,He).fastStart&&l(this,Je)){for(;l(this,Ze).length>0&&l(this,Ze)[0].decodeTimestamp<=n.decodeTimestamp;){let e=l(this,Ze).shift();u(this,mt,pt).call(this,l(this,Je),e)}n.decodeTimestamp<=l(this,Je).lastDecodeTimestamp?u(this,mt,pt).call(this,l(this,Ge),n):l(this,Ye).push(n)}else u(this,mt,pt).call(this,l(this,Ge),n)}addAudioChunk(e,t,i){if(!(e instanceof EncodedAudioChunk))throw new TypeError("addAudioChunk's first argument (sample) must be of type EncodedAudioChunk.");if(t&&"object"!=typeof t)throw new TypeError("addAudioChunk's second argument (meta), when provided, must be an object.");if(void 0!==i&&(!Number.isFinite(i)||i<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,i??e.timestamp,e.duration,t)}addAudioChunkRaw(e,t,i,s,a){if(!(e instanceof Uint8Array))throw new TypeError("addAudioChunkRaw's first argument (data) must be an instance of Uint8Array.");if("key"!==t&&"delta"!==t)throw new TypeError("addAudioChunkRaw's second argument (type) must be either 'key' or 'delta'.");if(!Number.isFinite(i)||i<0)throw new TypeError("addAudioChunkRaw's third argument (timestamp) must be a non-negative real number.");if(!Number.isFinite(s)||s<0)throw new TypeError("addAudioChunkRaw's fourth argument (duration) must be a non-negative real number.");if(a&&"object"!=typeof a)throw new TypeError("addAudioChunkRaw's fifth argument (meta), when provided, must be an object.");if(u(this,vt,St).call(this),!l(this,He).audio)throw new Error("No audio track declared.");if("object"==typeof l(this,He).fastStart&&l(this,Je).samples.length===l(this,He).fastStart.expectedAudioChunks)throw new Error(`Cannot add more audio chunks than specified in 'fastStart' (${l(this,He).fastStart.expectedAudioChunks}).`);let r=u(this,ft,ut).call(this,l(this,Je),e,t,i,s,a);if("fragmented"===l(this,He).fastStart&&l(this,Ge)){for(;l(this,Ye).length>0&&l(this,Ye)[0].decodeTimestamp<=r.decodeTimestamp;){let e=l(this,Ye).shift();u(this,mt,pt).call(this,l(this,Ge),e)}r.decodeTimestamp<=l(this,Ge).lastDecodeTimestamp?u(this,mt,pt).call(this,l(this,Je),r):l(this,Ze).push(r)}else u(this,mt,pt).call(this,l(this,Je),r)}finalize(){if(l(this,et))throw new Error("Cannot finalize a muxer more than once.");if("fragmented"===l(this,He).fastStart){for(let e of l(this,Ye))u(this,mt,pt).call(this,l(this,Ge),e);for(let e of l(this,Ze))u(this,mt,pt).call(this,l(this,Je),e);u(this,yt,kt).call(this,!1)}else l(this,Ge)&&u(this,gt,bt).call(this,l(this,Ge)),l(this,Je)&&u(this,gt,bt).call(this,l(this,Je));let e=[l(this,Ge),l(this,Je)].filter(Boolean);if("in-memory"===l(this,He).fastStart){let t;for(let i=0;i<2;i++){let i=R(e,l(this,Ke)),s=l(this,Pe).measureBox(i);t=l(this,Pe).measureBox(l(this,_e));let a=l(this,Pe).pos+s+t;for(let e of l(this,Qe)){e.offset=a;for(let{data:i}of e.samples)a+=i.byteLength,t+=i.byteLength}if(a<2**32)break;t>=2**32&&(l(this,_e).largeSize=!0)}let i=R(e,l(this,Ke));l(this,Pe).writeBox(i),l(this,_e).size=t,l(this,Pe).writeBox(l(this,_e));for(let e of l(this,Qe))for(let t of e.samples)l(this,Pe).write(t.data),t.data=null}else if("fragmented"===l(this,He).fastStart){let t=l(this,Pe).pos,i=(e=>B("mfra",null,[...e.map(ge),be()]))(e);l(this,Pe).writeBox(i);let s=l(this,Pe).pos-t;l(this,Pe).seek(l(this,Pe).pos-4),l(this,Pe).writeU32(s)}else{let t=l(this,Pe).offsets.get(l(this,_e)),i=l(this,Pe).pos-t;l(this,_e).size=i,l(this,_e).largeSize=i>=2**32,l(this,Pe).patchBox(l(this,_e));let s=R(e,l(this,Ke));if("object"==typeof l(this,He).fastStart){l(this,Pe).seek(l(this,qe)),l(this,Pe).writeBox(s);let e=t-l(this,Pe).pos;l(this,Pe).writeBox({type:"free",size:e})}else l(this,Pe).writeBox(s)}u(this,Ct,Tt).call(this),l(this,Pe).finalize(),f(this,et,!0)}};He=new WeakMap,Pe=new WeakMap,qe=new WeakMap,_e=new WeakMap,Ge=new WeakMap,Je=new WeakMap,Ke=new WeakMap,Qe=new WeakMap,Xe=new WeakMap,Ye=new WeakMap,Ze=new WeakMap,et=new WeakMap,tt=new WeakSet,it=function(e){if("object"!=typeof e)throw new TypeError("The muxer requires an options object to be passed to its constructor.");if(!(e.target instanceof ve))throw new TypeError("The target must be provided and an instance of Target.");if(e.video){if(!Et.includes(e.video.codec))throw new TypeError(`Unsupported video codec: ${e.video.codec}`);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.`);const t=e.video.rotation;if("number"==typeof t&&![0,90,180,270].includes(t))throw new TypeError(`Invalid video rotation: ${t}. Has to be 0, 90, 180 or 270.`);if(Array.isArray(t)&&(9!==t.length||t.some((e=>"number"!=typeof e))))throw new TypeError(`Invalid video transformation matrix: ${t.join()}`);if(void 0!==e.video.frameRate&&(!Number.isInteger(e.video.frameRate)||e.video.frameRate<=0))throw new TypeError(`Invalid video frame rate: ${e.video.frameRate}. Must be a positive integer.`)}if(e.audio){if(!At.includes(e.audio.codec))throw new TypeError(`Unsupported audio codec: ${e.audio.codec}`);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.firstTimestampBehavior&&!Mt.includes(e.firstTimestampBehavior))throw new TypeError(`Invalid first timestamp behavior: ${e.firstTimestampBehavior}`);if("object"==typeof e.fastStart){if(e.video){if(void 0===e.fastStart.expectedVideoChunks)throw new TypeError("'fastStart' is an object but is missing property 'expectedVideoChunks'.");if(!Number.isInteger(e.fastStart.expectedVideoChunks)||e.fastStart.expectedVideoChunks<0)throw new TypeError("'expectedVideoChunks' must be a non-negative integer.")}if(e.audio){if(void 0===e.fastStart.expectedAudioChunks)throw new TypeError("'fastStart' is an object but is missing property 'expectedAudioChunks'.");if(!Number.isInteger(e.fastStart.expectedAudioChunks)||e.fastStart.expectedAudioChunks<0)throw new TypeError("'expectedAudioChunks' must be a non-negative integer.")}}else if(![!1,"in-memory","fragmented"].includes(e.fastStart))throw new TypeError("'fastStart' option must be false, 'in-memory', 'fragmented' or an object.");if(void 0!==e.minFragmentDuration&&(!Number.isFinite(e.minFragmentDuration)||e.minFragmentDuration<0))throw new TypeError("'minFragmentDuration' must be a non-negative number.")},st=new WeakSet,at=function(){var e;if(l(this,Pe).writeBox((e={holdsAvc:"avc"===l(this,He).video?.codec,fragmented:"fragmented"===l(this,He).fastStart}).fragmented?B("ftyp",[v("iso5"),b(512),v("iso5"),v("iso6"),v("mp41")]):B("ftyp",[v("isom"),b(512),v("isom"),e.holdsAvc?v("avc1"):[],v("mp41")])),f(this,qe,l(this,Pe).pos),"in-memory"===l(this,He).fastStart)f(this,_e,O(!1));else if("fragmented"===l(this,He).fastStart);else{if("object"==typeof l(this,He).fastStart){let e=u(this,rt,nt).call(this);l(this,Pe).seek(l(this,Pe).pos+e)}f(this,_e,O(!0)),l(this,Pe).writeBox(l(this,_e))}u(this,Ct,Tt).call(this)},rt=new WeakSet,nt=function(){if("object"!=typeof l(this,He).fastStart)return;let e=0,t=[l(this,He).fastStart.expectedVideoChunks,l(this,He).fastStart.expectedAudioChunks];for(let i of t)i&&(e+=8*Math.ceil(2/3*i),e+=4*i,e+=12*Math.ceil(2/3*i),e+=4*i,e+=8*i);return e+=4096,e},ot=new WeakSet,ht=function(){if(l(this,He).video&&f(this,Ge,{id:1,info:{type:"video",codec:l(this,He).video.codec,width:l(this,He).video.width,height:l(this,He).video.height,rotation:l(this,He).video.rotation??0,decoderConfig:null},timescale:l(this,He).video.frameRate??57600,samples:[],finalizedChunks:[],currentChunk:null,firstDecodeTimestamp:void 0,lastDecodeTimestamp:-1,timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,compactlyCodedChunkTable:[]}),l(this,He).audio&&(f(this,Je,{id:l(this,He).video?2:1,info:{type:"audio",codec:l(this,He).audio.codec,numberOfChannels:l(this,He).audio.numberOfChannels,sampleRate:l(this,He).audio.sampleRate,decoderConfig:null},timescale:l(this,He).audio.sampleRate,samples:[],finalizedChunks:[],currentChunk:null,firstDecodeTimestamp:void 0,lastDecodeTimestamp:-1,timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,compactlyCodedChunkTable:[]}),"aac"===l(this,He).audio.codec)){let e=u(this,lt,dt).call(this,2,l(this,He).audio.sampleRate,l(this,He).audio.numberOfChannels);l(this,Je).info.decoderConfig={codec:l(this,He).audio.codec,description:e,numberOfChannels:l(this,He).audio.numberOfChannels,sampleRate:l(this,He).audio.sampleRate}}},lt=new WeakSet,dt=function(e,t,i){let s=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350].indexOf(t),a=i,r="";r+=e.toString(2).padStart(5,"0"),r+=s.toString(2).padStart(4,"0"),15===s&&(r+=t.toString(2).padStart(24,"0")),r+=a.toString(2).padStart(4,"0");let n=8*Math.ceil(r.length/8);r=r.padEnd(n,"0");let o=new Uint8Array(r.length/8);for(let e=0;e<r.length;e+=8)o[e/8]=parseInt(r.slice(e,e+8),2);return o},ft=new WeakSet,ut=function(e,t,i,s,a,r,n){let o=s/1e6,h=(s-(n??0))/1e6,l=a/1e6,d=u(this,ct,wt).call(this,o,h,e);return o=d.presentationTimestamp,h=d.decodeTimestamp,r?.decoderConfig&&(null===e.info.decoderConfig?e.info.decoderConfig=r.decoderConfig:Object.assign(e.info.decoderConfig,r.decoderConfig)),{presentationTimestamp:o,decodeTimestamp:h,duration:l,data:t,size:t.byteLength,type:i,timescaleUnitsToNextSample:z(l,e.timescale)}},mt=new WeakSet,pt=function(e,t){"fragmented"!==l(this,He).fastStart&&e.samples.push(t);const i=z(t.presentationTimestamp-t.decodeTimestamp,e.timescale);if(null!==e.lastTimescaleUnits){let s=z(t.decodeTimestamp,e.timescale,!1),a=Math.round(s-e.lastTimescaleUnits);if(e.lastTimescaleUnits+=a,e.lastSample.timescaleUnitsToNextSample=a,"fragmented"!==l(this,He).fastStart){let t=S(e.timeToSampleTable);1===t.sampleCount?(t.sampleDelta=a,t.sampleCount++):t.sampleDelta===a?t.sampleCount++:(t.sampleCount--,e.timeToSampleTable.push({sampleCount:2,sampleDelta:a}));const s=S(e.compositionTimeOffsetTable);s.sampleCompositionTimeOffset===i?s.sampleCount++:e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:i})}}else e.lastTimescaleUnits=0,"fragmented"!==l(this,He).fastStart&&(e.timeToSampleTable.push({sampleCount:1,sampleDelta:z(t.duration,e.timescale)}),e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:i}));e.lastSample=t;let s=!1;if(e.currentChunk){let i=t.presentationTimestamp-e.currentChunk.startTimestamp;if("fragmented"===l(this,He).fastStart){let a=l(this,Ge)??l(this,Je);const r=l(this,He).minFragmentDuration??1;e===a&&"key"===t.type&&i>=r&&(s=!0,u(this,yt,kt).call(this))}else s=i>=.5}else s=!0;s&&(e.currentChunk&&u(this,gt,bt).call(this,e),e.currentChunk={startTimestamp:t.presentationTimestamp,samples:[]}),e.currentChunk.samples.push(t)},ct=new WeakSet,wt=function(e,t,i){const s="strict"===l(this,He).firstTimestampBehavior,a=-1===i.lastDecodeTimestamp;if(s&&a&&0!==t)throw new Error(`The first chunk for your media track must have a timestamp of 0 (received DTS=${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 thedocument, which is probably what you want.\n\nIf you want to offset all timestamps of a track such that the first one is zero, set firstTimestampBehavior: 'offset' in the options.\n`);if("offset"===l(this,He).firstTimestampBehavior||"cross-track-offset"===l(this,He).firstTimestampBehavior){let s;void 0===i.firstDecodeTimestamp&&(i.firstDecodeTimestamp=t),s="offset"===l(this,He).firstTimestampBehavior?i.firstDecodeTimestamp:Math.min(l(this,Ge)?.firstDecodeTimestamp??1/0,l(this,Je)?.firstDecodeTimestamp??1/0),t-=s,e-=s}if(t<i.lastDecodeTimestamp)throw new Error(`Timestamps must be monotonically increasing (DTS went from ${1e6*i.lastDecodeTimestamp} to ${1e6*t}).`);return i.lastDecodeTimestamp=t,{presentationTimestamp:e,decodeTimestamp:t}},gt=new WeakSet,bt=function(e){if("fragmented"===l(this,He).fastStart)throw new Error("Can't finalize individual chunks if 'fastStart' is set to 'fragmented'.");if(e.currentChunk)if(e.finalizedChunks.push(e.currentChunk),l(this,Qe).push(e.currentChunk),0!==e.compactlyCodedChunkTable.length&&S(e.compactlyCodedChunkTable).samplesPerChunk===e.currentChunk.samples.length||e.compactlyCodedChunkTable.push({firstChunk:e.finalizedChunks.length,samplesPerChunk:e.currentChunk.samples.length}),"in-memory"!==l(this,He).fastStart){e.currentChunk.offset=l(this,Pe).pos;for(let t of e.currentChunk.samples)l(this,Pe).write(t.data),t.data=null;u(this,Ct,Tt).call(this)}else e.currentChunk.offset=0},yt=new WeakSet,kt=function(e=!0){if("fragmented"!==l(this,He).fastStart)throw new Error("Can't finalize a fragment unless 'fastStart' is set to 'fragmented'.");let t=[l(this,Ge),l(this,Je)].filter((e=>e&&e.currentChunk));if(0===t.length)return;let i=(s=this,a=Xe,{set _(e){f(s,a,e,r)},get _(){return l(s,a,n)}})._++;var s,a,r,n;if(1===i){let e=R(t,l(this,Ke),!0);l(this,Pe).writeBox(e)}let o=l(this,Pe).pos,h=de(i,t);l(this,Pe).writeBox(h);{let e=O(!1),i=0;for(let e of t)for(let t of e.currentChunk.samples)i+=t.size;let s=l(this,Pe).measureBox(e)+i;s>=2**32&&(e.largeSize=!0,s=l(this,Pe).measureBox(e)+i),e.size=s,l(this,Pe).writeBox(e)}for(let e of t){e.currentChunk.offset=l(this,Pe).pos,e.currentChunk.moofOffset=o;for(let t of e.currentChunk.samples)l(this,Pe).write(t.data),t.data=null}let d=l(this,Pe).pos;l(this,Pe).seek(l(this,Pe).offsets.get(h));let m=de(i,t);l(this,Pe).writeBox(m),l(this,Pe).seek(d);for(let e of t)e.finalizedChunks.push(e.currentChunk),l(this,Qe).push(e.currentChunk),e.currentChunk=null;e&&u(this,Ct,Tt).call(this)},Ct=new WeakSet,Tt=function(){l(this,Pe)instanceof $e&&l(this,Pe).flush()},vt=new WeakSet,St=function(){if(l(this,et))throw new Error("Cannot add new video or audio chunks after the file has been finalized.")};export{Se as ArrayBufferTarget,ze as FileSystemWritableFileStreamTarget,Wt as Muxer,xe as StreamTarget};export default null;
//# sourceMappingURL=/sm/bb026e6ea3b43a57fb59f4d34e430809fe77713999a974cc76380c4558d8101f.map