mediabunny 1.45.2 → 1.45.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@
5
5
  * License, v. 2.0. If a copy of the MPL was not distributed with this
6
6
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
7
  */
8
- var Xd=Object.create;var ou=Object.defineProperty;var $d=Object.getOwnPropertyDescriptor;var Yd=Object.getOwnPropertyNames;var Zd=Object.getPrototypeOf,Jd=Object.prototype.hasOwnProperty;var au=(n,t)=>(t=Symbol[n])?t:Symbol.for("Symbol."+n),su=n=>{throw TypeError(n)};var em=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var tm=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Yd(t))!Jd.call(n,i)&&i!==e&&ou(n,i,{get:()=>t[i],enumerable:!(r=$d(t,i))||r.enumerable});return n};var cu=(n,t,e)=>(e=n!=null?Xd(Zd(n)):{},tm(t||!n||!n.__esModule?ou(e,"default",{value:n,enumerable:!0}):e,n));var qi=(n,t,e)=>{if(t!=null){typeof t!="object"&&typeof t!="function"&&su("Object expected");var r,i;e&&(r=t[au("asyncDispose")]),r===void 0&&(r=t[au("dispose")],e&&(i=r)),typeof r!="function"&&su("Object not disposable"),i&&(r=function(){try{i.call(this)}catch(a){return Promise.reject(a)}}),n.push([e,r,t])}else e&&n.push([e]);return t},Gi=(n,t,e)=>{var r=typeof SuppressedError=="function"?SuppressedError:function(s,o,c,u){return u=Error(c),u.name="SuppressedError",u.error=s,u.suppressed=o,u},i=s=>t=e?new r(s,t,"An error was suppressed during disposal"):(e=!0,s),a=s=>{for(;s=n.pop();)try{var o=s[1]&&s[1].call(s[2]);if(s[0])return Promise.resolve(o).then(a,c=>(i(c),a()))}catch(c){i(c)}if(e)throw t};return a()};var Xo=em(()=>{});function h(n){if(!n)throw new Error("Assertion failed.")}var Ze=n=>{let t=(n%360+360)%360;if(t===0||t===90||t===180||t===270)return t;throw new Error(`Invalid rotation ${n}.`)},X=n=>n&&n[n.length-1],_t=n=>n>=0&&n<2**32,M=n=>{let t=0;for(;n.readBits(1)===0&&t<32;)t++;if(t>=32)throw new Error("Invalid exponential-Golomb code.");return(1<<t)-1+n.readBits(t)},gt=n=>{let t=M(n);return(t&1)===0?-(t>>1):t+1>>1},du=(n,t,e,r)=>{for(let i=t;i<e;i++){let a=Math.floor(i/8),s=n[a],o=7-(i&7);s&=~(1<<o),s|=(r&1<<e-i-1)>>e-i-1<<o,n[a]=s}},j=n=>n.constructor===Uint8Array?n:ArrayBuffer.isView(n)?new Uint8Array(n.buffer,n.byteOffset,n.byteLength):new Uint8Array(n),V=n=>n.constructor===DataView?n:ArrayBuffer.isView(n)?new DataView(n.buffer,n.byteOffset,n.byteLength):new DataView(n),me=new TextDecoder,$=new TextEncoder,Je=n=>{for(let t=0;t<n.length;t++)if(n.charCodeAt(t)>255)return!1;return!0},oo=n=>Object.fromEntries(Object.entries(n).map(([t,e])=>[e,t])),et={bt709:1,bt470bg:5,smpte170m:6,bt2020:9,smpte432:12},Ar=oo(et),tt={bt709:1,smpte170m:6,linear:8,"iec61966-2-1":13,pq:16,hlg:18},xr=oo(tt),rt={rgb:0,bt709:1,bt470bg:5,smpte170m:6,"bt2020-ncl":9},Pr=oo(rt),zn=n=>!!n&&!!n.primaries&&!!n.transfer&&!!n.matrix&&n.fullRange!==void 0,Rt=n=>n instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&n instanceof SharedArrayBuffer||ArrayBuffer.isView(n),Ie=class{constructor(){this.currentPromise=Promise.resolve();this.pending=0}async acquire(){let t,e=new Promise(i=>{let a=!1;t=()=>{a||(i(),this.pending--,a=!0)}}),r=this.currentPromise;return this.currentPromise=e,this.pending++,await r,t}},mu=/^[0-9a-fA-F]+$/,tr=n=>[...n].map(t=>t.toString(16).padStart(2,"0")).join(""),fu=n=>{h(n.length%2===0);let t=new Uint8Array(n.length/2);for(let e=0;e<n.length;e+=2)t[e/2]=parseInt(n.slice(e,e+2),16);return t},co=n=>(n=n>>1&1431655765|(n&1431655765)<<1,n=n>>2&858993459|(n&858993459)<<2,n=n>>4&252645135|(n&252645135)<<4,n=n>>8&16711935|(n&16711935)<<8,n=n>>16&65535|(n&65535)<<16,n>>>0),Ft=(n,t,e)=>{let r=0,i=n.length-1,a=-1;for(;r<=i;){let s=r+i>>1,o=e(n[s]);o===t?(a=s,i=s-1):o<t?r=s+1:i=s-1}return a},H=(n,t,e)=>{let r=0,i=n.length-1,a=-1;for(;r<=i;){let s=r+(i-r+1)/2|0;e(n[s])<=t?(a=s,r=s+1):i=s-1}return a},uo=(n,t,e)=>{let r=H(n,e(t),e);n.splice(r+1,0,t)},Z=()=>{let n,t;return{promise:new Promise((r,i)=>{n=r,t=i}),resolve:n,reject:t}},lo=(n,t)=>{let e=n.indexOf(t);e!==-1&&n.splice(e,1)},mo=(n,t)=>{for(let e=n.length-1;e>=0;e--)if(t(n[e]))return n[e]},rr=(n,t)=>{for(let e=n.length-1;e>=0;e--)if(t(n[e]))return e;return-1},pu=async function*(n){Symbol.iterator in n?yield*n[Symbol.iterator]():yield*n[Symbol.asyncIterator]()},hu=n=>{if(!(Symbol.iterator in n)&&!(Symbol.asyncIterator in n))throw new TypeError("Argument must be an iterable or async iterable.")},ne=n=>{throw new Error(`Unexpected value: ${n}`)},Cr=(n,t,e)=>{let r=n.getUint8(t),i=n.getUint8(t+1),a=n.getUint8(t+2);return e?r|i<<8|a<<16:r<<16|i<<8|a},gu=(n,t,e)=>Cr(n,t,e)<<8>>8,Ir=(n,t,e,r)=>{e=e>>>0,e=e&16777215,r?(n.setUint8(t,e&255),n.setUint8(t+1,e>>>8&255),n.setUint8(t+2,e>>>16&255)):(n.setUint8(t,e>>>16&255),n.setUint8(t+1,e>>>8&255),n.setUint8(t+2,e&255))},bu=(n,t,e,r)=>{e=ue(e,-8388608,8388607),e<0&&(e=e+16777216&16777215),Ir(n,t,e,r)},yu=(n,t,e,r)=>{r?(n.setUint32(t+0,e,!0),n.setInt32(t+4,Math.floor(e/2**32),!0)):(n.setInt32(t+0,Math.floor(e/2**32),!0),n.setUint32(t+4,e,!0))},Qi=(n,t)=>({async next(){let e=await n.next();return e.done?{value:void 0,done:!0}:{value:t(e.value),done:!1}},return(){return n.return()},throw(e){return n.throw(e)},[Symbol.asyncIterator](){return this}}),ue=(n,t,e)=>Math.max(t,Math.min(e,n)),J="und",Mt=n=>{let t=Math.round(n);return Math.abs(n/t-1)<10*Number.EPSILON?t:n},ji=(n,t)=>Math.round(n/t)*t,bt=(n,t)=>Math.round(n*t)/t,Nn=(n,t)=>Math.floor(n/t)*t,fo=(n,t)=>Math.floor(n*t)/t,ku=n=>{let t=0;for(;n;)t++,n>>=1;return t},rm=/^[a-z]{3}$/,Bt=n=>rm.test(n),yt=1e6*(1+Number.EPSILON),Tu=(n,t)=>{let e={...n,...t};if(n.headers||t.headers){let r=n.headers?uu(n.headers):{},i=t.headers?uu(t.headers):{},a={...r};Object.entries(i).forEach(([s,o])=>{let c=Object.keys(a).find(u=>u.toLowerCase()===s.toLowerCase());c&&delete a[c],a[s]=o}),e.headers=a}return e},uu=n=>{if(n instanceof Headers){let t={};return n.forEach((e,r)=>{t[r]=e}),t}if(Array.isArray(n)){let t={};return n.forEach(([e,r])=>{t[e]=r}),t}return n},wu=async(n,t,e,r,i)=>{let a=0;for(;;)try{return await n(t,e)}catch(s){if(i())throw s;a++;let o=r(a,s,t);if(o===null)throw s;if(console.error("Retrying failed fetch. Error:",s),!Number.isFinite(o)||o<0)throw new TypeError("Retry delay must be a non-negative finite number.");if(o>0&&await $i(1e3*o),i())throw s}},Su=(n,t)=>{let e=n<0?-1:1;n=Math.abs(n);let r=0,i=1,a=1,s=0,o=n;for(;;){let c=Math.floor(o),u=c*a+r,l=c*s+i;if(l>t)return{num:e*a,den:s};if(r=a,i=s,a=u,s=l,o=1/(o-c),!isFinite(o))break}return{num:e*a,den:s}},vt=class{constructor(){this.currentPromise=Promise.resolve()}call(t){return this.currentPromise=this.currentPromise.then(t)}},ro=null,ir=()=>ro!==null?ro:ro=!!(typeof navigator<"u"&&(navigator.vendor?.match(/apple/i)||/AppleWebKit/.test(navigator.userAgent)&&!/Chrome/.test(navigator.userAgent)||/\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent))),io=null,Er=()=>io!==null?io:io=typeof navigator<"u"&&navigator.userAgent?.includes("Firefox"),no=null,Xi=()=>no!==null?no:no=!!(typeof navigator<"u"&&(navigator.vendor?.includes("Google Inc")||/Chrome/.test(navigator.userAgent))),ao=null,Au=()=>{if(ao!==null)return ao;if(typeof navigator>"u")return null;let n=/\bChrome\/(\d+)/.exec(navigator.userAgent);return n?ao=Number(n[1]):null},vr=(n,t)=>n!==-1?n:t,Ln=(n,t,e,r)=>n<=r&&e<=t,it=function*(n){for(let t in n){let e=n[t];e!==void 0&&(yield{key:t,value:e})}},xu=n=>{switch(n.toLowerCase()){case"image/jpeg":case"image/jpg":return".jpg";case"image/png":return".png";case"image/gif":return".gif";case"image/webp":return".webp";case"image/bmp":return".bmp";case"image/svg+xml":return".svg";case"image/tiff":return".tiff";case"image/avif":return".avif";case"image/x-icon":case"image/vnd.microsoft.icon":return".ico";default:return null}},_r=n=>{let t=atob(n),e=new Uint8Array(t.length);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r);return e},Pu=n=>{let t="";for(let e=0;e<n.length;e++)t+=String.fromCharCode(n[e]);return btoa(t)},Wn=(n,t)=>{if(n.length!==t.length)return!1;for(let e=0;e<n.length;e++)if(n[e]!==t[e])return!1;return!0},Jr=()=>{Symbol.dispose??=Symbol("Symbol.dispose")},nr=n=>typeof n=="number"&&!Number.isNaN(n),fe=(n,t)=>{if(t.includes("://"))return t;if(n.includes("://")){let o=n.indexOf("?");o!==-1&&(n=n.slice(0,o))}let e;if(t.startsWith("/")){let o=n.indexOf("://");if(o===-1)e=t;else{let c=n.indexOf("/",o+3);c===-1?e=n+t:e=n.slice(0,c)+t}}else{let o=n.lastIndexOf("/");o===-1?e=t:e=n.slice(0,o+1)+t}let r="",i=e.indexOf("://");if(i!==-1){let o=e.indexOf("/",i+3);o!==-1&&(r=e.slice(0,o),e=e.slice(o))}let a=e.split("/"),s=[];for(let o of a)o===".."?s.pop():o!=="."&&s.push(o);return r+s.join("/")},Rr=(n,t)=>{let e=0;for(let r=0;r<n.length;r++)t(n[r])&&e++;return e},ei=(n,t)=>{let e=-1,r=1/0;for(let i=0;i<n.length;i++){let a=t(n[i]);a<r&&(r=a,e=i)}return e},Cu=(n,t)=>{let e=-1,r=-1/0;for(let i=0;i<n.length;i++){let a=t(n[i]);a>r&&(r=a,e=i)}return e},Ot=n=>{h(Number.isInteger(n.num)),h(Number.isInteger(n.den)),h(n.den!==0);let t=Math.abs(n.num),e=Math.abs(n.den);for(;e!==0;){let i=t%e;t=e,e=i}let r=t||1;return{num:n.num/r,den:n.den/r}},po=(n,t)=>{if(typeof n!="object"||!n)throw new TypeError(`${t} must be an object.`);if(!Number.isInteger(n.left)||n.left<0)throw new TypeError(`${t}.left must be a non-negative integer.`);if(!Number.isInteger(n.top)||n.top<0)throw new TypeError(`${t}.top must be a non-negative integer.`);if(!Number.isInteger(n.width)||n.width<0)throw new TypeError(`${t}.width must be a non-negative integer.`);if(!Number.isInteger(n.height)||n.height<0)throw new TypeError(`${t}.height must be a non-negative integer.`)},Ki,im=1,lu=new Map,ho=new Map,Iu=()=>typeof window>"u",nm=()=>{let n=new Map,t=new Map;self.onmessage=e=>{let r=e.data;switch(r.type){case"set-timeout":{let i=setTimeout(()=>{n.delete(r.timerId),self.postMessage({type:"fire",timerId:r.timerId})},r.delay);n.set(r.timerId,i)}break;case"set-interval":{let i=setInterval(()=>{self.postMessage({type:"fire",timerId:r.timerId})},r.delay);t.set(r.timerId,i)}break;case"clear-timeout":{let i=n.get(r.timerId);i!==void 0&&(clearTimeout(i),n.delete(r.timerId))}break;case"clear-interval":{let i=t.get(r.timerId);i!==void 0&&(clearInterval(i),t.delete(r.timerId))}break}}},Eu=()=>{if(Ki)return Ki;let n=`(${nm.toString()})();`,t=URL.createObjectURL(new Blob([n],{type:"text/javascript"}));return Ki=new Worker(t),URL.revokeObjectURL(t),Ki.onmessage=e=>{let r=e.data,i=lu.get(r.timerId);if(i){lu.delete(r.timerId),i();return}let a=ho.get(r.timerId);a&&a()},Ki};var vu=(n,t)=>{if(Iu())return{id:setInterval(n,t)};let e=im++;return ho.set(e,()=>{n()}),Eu().postMessage({type:"set-interval",timerId:e,delay:t}),{id:e}},_u=n=>{if(Iu()){clearInterval(n.id);return}h(typeof n.id=="number"),ho.delete(n.id),Eu().postMessage({type:"clear-interval",timerId:n.id})},$i=n=>new Promise(t=>setTimeout(t,n));var Dt=n=>Array.isArray(n)?n:[n],Ye=class{constructor(){this._listeners=new Map}on(t,e,r){this._listeners.has(t)||this._listeners.set(t,new Set);let i={fn:e,once:r?.once??!1};return this._listeners.get(t).add(i),()=>{this._listeners.get(t)?.delete(i)}}_emit(...t){let[e,r]=t,i=this._listeners.get(e);if(i)for(let a of i){try{a.fn(r)}catch(s){console.error(s)}a.once&&i.delete(a)}}},Fr=n=>Math.ceil(n/2)*2,so=class{constructor(t){this._queue=[];this._errored=!1;this.parallelism=t}get errored(){return this._errored}get inFlightCount(){return this._queue.length}async run(t){for(this._errored&&await Promise.race(this._queue);this._queue.length>=this.parallelism;)await Promise.race(this._queue);let e=t();this._queue.push(e),e.then(()=>lo(this._queue,e)).catch(()=>this._errored=!0)}async flush(){await Promise.all(this._queue)}},Hn=n=>n!==null&&typeof n=="object"&&Object.getPrototypeOf(n)===Object.prototype&&Object.values(n).every(t=>typeof t=="string");var He=class{constructor(t,e){this.data=t;this.mimeType=e;if(!(t instanceof Uint8Array))throw new TypeError("data must be a Uint8Array.");if(typeof e!="string")throw new TypeError("mimeType must be a string.")}},ar=class{constructor(t,e,r,i){this.data=t;this.mimeType=e;this.name=r;this.description=i;if(!(t instanceof Uint8Array))throw new TypeError("data must be a Uint8Array.");if(e!==void 0&&typeof e!="string")throw new TypeError("mimeType, when provided, must be a string.");if(r!==void 0&&typeof r!="string")throw new TypeError("name, when provided, must be a string.");if(i!==void 0&&typeof i!="string")throw new TypeError("description, when provided, must be a string.")}},Yi=n=>{if(!n||typeof n!="object")throw new TypeError("tags must be an object.");if(n.title!==void 0&&typeof n.title!="string")throw new TypeError("tags.title, when provided, must be a string.");if(n.description!==void 0&&typeof n.description!="string")throw new TypeError("tags.description, when provided, must be a string.");if(n.artist!==void 0&&typeof n.artist!="string")throw new TypeError("tags.artist, when provided, must be a string.");if(n.album!==void 0&&typeof n.album!="string")throw new TypeError("tags.album, when provided, must be a string.");if(n.albumArtist!==void 0&&typeof n.albumArtist!="string")throw new TypeError("tags.albumArtist, when provided, must be a string.");if(n.trackNumber!==void 0&&(!Number.isInteger(n.trackNumber)||n.trackNumber<=0))throw new TypeError("tags.trackNumber, when provided, must be a positive integer.");if(n.tracksTotal!==void 0&&(!Number.isInteger(n.tracksTotal)||n.tracksTotal<=0))throw new TypeError("tags.tracksTotal, when provided, must be a positive integer.");if(n.discNumber!==void 0&&(!Number.isInteger(n.discNumber)||n.discNumber<=0))throw new TypeError("tags.discNumber, when provided, must be a positive integer.");if(n.discsTotal!==void 0&&(!Number.isInteger(n.discsTotal)||n.discsTotal<=0))throw new TypeError("tags.discsTotal, when provided, must be a positive integer.");if(n.genre!==void 0&&typeof n.genre!="string")throw new TypeError("tags.genre, when provided, must be a string.");if(n.date!==void 0&&(!(n.date instanceof Date)||Number.isNaN(n.date.getTime())))throw new TypeError("tags.date, when provided, must be a valid Date.");if(n.lyrics!==void 0&&typeof n.lyrics!="string")throw new TypeError("tags.lyrics, when provided, must be a string.");if(n.images!==void 0){if(!Array.isArray(n.images))throw new TypeError("tags.images, when provided, must be an array.");for(let t of n.images){if(!t||typeof t!="object")throw new TypeError("Each image in tags.images must be an object.");if(!(t.data instanceof Uint8Array))throw new TypeError("Each image.data must be a Uint8Array.");if(typeof t.mimeType!="string")throw new TypeError("Each image.mimeType must be a string.");if(!["coverFront","coverBack","unknown"].includes(t.kind))throw new TypeError("Each image.kind must be 'coverFront', 'coverBack', or 'unknown'.")}}if(n.comment!==void 0&&typeof n.comment!="string")throw new TypeError("tags.comment, when provided, must be a string.");if(n.raw!==void 0){if(!n.raw||typeof n.raw!="object")throw new TypeError("tags.raw, when provided, must be an object.");for(let t of Object.values(n.raw))if(t!==null&&typeof t!="string"&&!(t instanceof Uint8Array)&&!(t instanceof He)&&!(t instanceof ar)&&!Hn(t))throw new TypeError("Each value in tags.raw must be a string, Uint8Array, RichImageData, AttachedFile, Record<string, string>, or null.")}},Vt=n=>n.title===void 0&&n.description===void 0&&n.artist===void 0&&n.album===void 0&&n.albumArtist===void 0&&n.trackNumber===void 0&&n.tracksTotal===void 0&&n.discNumber===void 0&&n.discsTotal===void 0&&n.genre===void 0&&n.date===void 0&&n.lyrics===void 0&&(!n.images||n.images.length===0)&&n.comment===void 0&&(n.raw===void 0||Object.keys(n.raw).length===0),we={default:!0,primary:!0,forced:!1,original:!1,commentary:!1,hearingImpaired:!1,visuallyImpaired:!1},Ru=n=>{if(!n||typeof n!="object")throw new TypeError("disposition must be an object.");if(n.default!==void 0&&typeof n.default!="boolean")throw new TypeError("disposition.default must be a boolean.");if(n.primary!==void 0&&typeof n.primary!="boolean")throw new TypeError("disposition.primary must be a boolean.");if(n.forced!==void 0&&typeof n.forced!="boolean")throw new TypeError("disposition.forced must be a boolean.");if(n.original!==void 0&&typeof n.original!="boolean")throw new TypeError("disposition.original must be a boolean.");if(n.commentary!==void 0&&typeof n.commentary!="boolean")throw new TypeError("disposition.commentary must be a boolean.");if(n.hearingImpaired!==void 0&&typeof n.hearingImpaired!="boolean")throw new TypeError("disposition.hearingImpaired must be a boolean.");if(n.visuallyImpaired!==void 0&&typeof n.visuallyImpaired!="boolean")throw new TypeError("disposition.visuallyImpaired must be a boolean.")};var W=class n{constructor(t){this.bytes=t;this.pos=0}seekToByte(t){this.pos=8*t}readBit(){let t=Math.floor(this.pos/8),e=this.bytes[t]??0,r=7-(this.pos&7),i=(e&1<<r)>>r;return this.pos++,i}readBits(t){if(t===1)return this.readBit();let e=0;for(let r=0;r<t;r++)e<<=1,e|=this.readBit();return e}writeBits(t,e){let r=this.pos+t;for(let i=this.pos;i<r;i++){let a=Math.floor(i/8),s=this.bytes[a],o=7-(i&7);s&=~(1<<o),s|=(e&1<<r-i-1)>>r-i-1<<o,this.bytes[a]=s}this.pos=r}readAlignedByte(){if(this.pos%8!==0)throw new Error("Bitstream is not byte-aligned.");let t=this.pos/8,e=this.bytes[t]??0;return this.pos+=8,e}skipBits(t){this.pos+=t}getBitsLeft(){return this.bytes.length*8-this.pos}clone(){let t=new n(this.bytes);return t.pos=this.pos,t}};var qe=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],kt=[-1,1,2,3,4,5,6,8],Tt=n=>{if(!n||n.byteLength<2)throw new TypeError("AAC description must be at least 2 bytes long.");let t=new W(n),e=t.readBits(5);e===31&&(e=32+t.readBits(6));let r=t.readBits(4),i=null;r===15?i=t.readBits(24):r<qe.length&&(i=qe[r]);let a=t.readBits(4),s=null;return a>=1&&a<=7&&(s=kt[a]),{objectType:e,frequencyIndex:r,sampleRate:i,channelConfiguration:a,numberOfChannels:s}},ti=n=>{let t=qe.indexOf(n.sampleRate),e=null;t===-1&&(t=15,e=n.sampleRate);let r=kt.indexOf(n.numberOfChannels);if(r===-1)throw new TypeError(`Unsupported number of channels: ${n.numberOfChannels}`);let i=13;n.objectType>=32&&(i+=6),t===15&&(i+=24);let a=Math.ceil(i/8),s=new Uint8Array(a),o=new W(s);return n.objectType<32?o.writeBits(5,n.objectType):(o.writeBits(5,31),o.writeBits(6,n.objectType-32)),o.writeBits(4,t),t===15&&o.writeBits(24,e),o.writeBits(4,r),s},qn=n=>{let t=new Uint8Array(7),e=new W(t),{objectType:r,frequencyIndex:i,channelConfiguration:a}=n,s=r-1;return e.writeBits(12,4095),e.writeBits(1,0),e.writeBits(2,0),e.writeBits(1,1),e.writeBits(2,s),e.writeBits(4,i),e.writeBits(1,0),e.writeBits(3,a),e.writeBits(1,0),e.writeBits(1,0),e.writeBits(1,0),e.writeBits(1,0),e.skipBits(13),e.writeBits(11,2047),e.writeBits(2,0),{header:t,bitstream:e}},Gn=(n,t)=>{n.pos=30,n.writeBits(13,t)};var re=["avc","hevc","vp9","av1","vp8"],ee=["pcm-s16","pcm-s16be","pcm-s24","pcm-s24be","pcm-s32","pcm-s32be","pcm-f32","pcm-f32be","pcm-f64","pcm-f64be","pcm-u8","pcm-s8","ulaw","alaw"],wt=["aac","opus","mp3","vorbis","flac","ac3","eac3"],ae=[...wt,...ee],Me=["webvtt"],Zi=[{maxMacroblocks:99,maxBitrate:64e3,maxDpbMbs:396,level:10},{maxMacroblocks:396,maxBitrate:192e3,maxDpbMbs:900,level:11},{maxMacroblocks:396,maxBitrate:384e3,maxDpbMbs:2376,level:12},{maxMacroblocks:396,maxBitrate:768e3,maxDpbMbs:2376,level:13},{maxMacroblocks:396,maxBitrate:2e6,maxDpbMbs:2376,level:20},{maxMacroblocks:792,maxBitrate:4e6,maxDpbMbs:4752,level:21},{maxMacroblocks:1620,maxBitrate:4e6,maxDpbMbs:8100,level:22},{maxMacroblocks:1620,maxBitrate:1e7,maxDpbMbs:8100,level:30},{maxMacroblocks:3600,maxBitrate:14e6,maxDpbMbs:18e3,level:31},{maxMacroblocks:5120,maxBitrate:2e7,maxDpbMbs:20480,level:32},{maxMacroblocks:8192,maxBitrate:2e7,maxDpbMbs:32768,level:40},{maxMacroblocks:8192,maxBitrate:5e7,maxDpbMbs:32768,level:41},{maxMacroblocks:8704,maxBitrate:5e7,maxDpbMbs:34816,level:42},{maxMacroblocks:22080,maxBitrate:135e6,maxDpbMbs:110400,level:50},{maxMacroblocks:36864,maxBitrate:24e7,maxDpbMbs:184320,level:51},{maxMacroblocks:36864,maxBitrate:24e7,maxDpbMbs:184320,level:52},{maxMacroblocks:139264,maxBitrate:24e7,maxDpbMbs:696320,level:60},{maxMacroblocks:139264,maxBitrate:48e7,maxDpbMbs:696320,level:61},{maxMacroblocks:139264,maxBitrate:8e8,maxDpbMbs:696320,level:62}],Fu=[{maxPictureSize:36864,maxBitrate:128e3,tier:"L",level:30},{maxPictureSize:122880,maxBitrate:15e5,tier:"L",level:60},{maxPictureSize:245760,maxBitrate:3e6,tier:"L",level:63},{maxPictureSize:552960,maxBitrate:6e6,tier:"L",level:90},{maxPictureSize:983040,maxBitrate:1e7,tier:"L",level:93},{maxPictureSize:2228224,maxBitrate:12e6,tier:"L",level:120},{maxPictureSize:2228224,maxBitrate:3e7,tier:"H",level:120},{maxPictureSize:2228224,maxBitrate:2e7,tier:"L",level:123},{maxPictureSize:2228224,maxBitrate:5e7,tier:"H",level:123},{maxPictureSize:8912896,maxBitrate:25e6,tier:"L",level:150},{maxPictureSize:8912896,maxBitrate:1e8,tier:"H",level:150},{maxPictureSize:8912896,maxBitrate:4e7,tier:"L",level:153},{maxPictureSize:8912896,maxBitrate:16e7,tier:"H",level:153},{maxPictureSize:8912896,maxBitrate:6e7,tier:"L",level:156},{maxPictureSize:8912896,maxBitrate:24e7,tier:"H",level:156},{maxPictureSize:35651584,maxBitrate:6e7,tier:"L",level:180},{maxPictureSize:35651584,maxBitrate:24e7,tier:"H",level:180},{maxPictureSize:35651584,maxBitrate:12e7,tier:"L",level:183},{maxPictureSize:35651584,maxBitrate:48e7,tier:"H",level:183},{maxPictureSize:35651584,maxBitrate:24e7,tier:"L",level:186},{maxPictureSize:35651584,maxBitrate:8e8,tier:"H",level:186}],Ut=[{maxPictureSize:36864,maxBitrate:2e5,level:10},{maxPictureSize:73728,maxBitrate:8e5,level:11},{maxPictureSize:122880,maxBitrate:18e5,level:20},{maxPictureSize:245760,maxBitrate:36e5,level:21},{maxPictureSize:552960,maxBitrate:72e5,level:30},{maxPictureSize:983040,maxBitrate:12e6,level:31},{maxPictureSize:2228224,maxBitrate:18e6,level:40},{maxPictureSize:2228224,maxBitrate:3e7,level:41},{maxPictureSize:8912896,maxBitrate:6e7,level:50},{maxPictureSize:8912896,maxBitrate:12e7,level:51},{maxPictureSize:8912896,maxBitrate:18e7,level:52},{maxPictureSize:35651584,maxBitrate:18e7,level:60},{maxPictureSize:35651584,maxBitrate:24e7,level:61},{maxPictureSize:35651584,maxBitrate:48e7,level:62}],Mu=[{maxPictureSize:147456,maxBitrate:15e5,tier:"M",level:0},{maxPictureSize:278784,maxBitrate:3e6,tier:"M",level:1},{maxPictureSize:665856,maxBitrate:6e6,tier:"M",level:4},{maxPictureSize:1065024,maxBitrate:1e7,tier:"M",level:5},{maxPictureSize:2359296,maxBitrate:12e6,tier:"M",level:8},{maxPictureSize:2359296,maxBitrate:3e7,tier:"H",level:8},{maxPictureSize:2359296,maxBitrate:2e7,tier:"M",level:9},{maxPictureSize:2359296,maxBitrate:5e7,tier:"H",level:9},{maxPictureSize:8912896,maxBitrate:3e7,tier:"M",level:12},{maxPictureSize:8912896,maxBitrate:1e8,tier:"H",level:12},{maxPictureSize:8912896,maxBitrate:4e7,tier:"M",level:13},{maxPictureSize:8912896,maxBitrate:16e7,tier:"H",level:13},{maxPictureSize:8912896,maxBitrate:6e7,tier:"M",level:14},{maxPictureSize:8912896,maxBitrate:24e7,tier:"H",level:14},{maxPictureSize:35651584,maxBitrate:6e7,tier:"M",level:15},{maxPictureSize:35651584,maxBitrate:24e7,tier:"H",level:15},{maxPictureSize:35651584,maxBitrate:6e7,tier:"M",level:16},{maxPictureSize:35651584,maxBitrate:24e7,tier:"H",level:16},{maxPictureSize:35651584,maxBitrate:1e8,tier:"M",level:17},{maxPictureSize:35651584,maxBitrate:48e7,tier:"H",level:17},{maxPictureSize:35651584,maxBitrate:16e7,tier:"M",level:18},{maxPictureSize:35651584,maxBitrate:8e8,tier:"H",level:18},{maxPictureSize:35651584,maxBitrate:16e7,tier:"M",level:19},{maxPictureSize:35651584,maxBitrate:8e8,tier:"H",level:19}],Bu=".01.01.01.01.00",Ou=".0.110.01.01.01.0",Kn=(n,t,e,r)=>{if(n==="avc"){let a=Math.ceil(t/16)*Math.ceil(e/16),s=Zi.find(m=>a<=m.maxMacroblocks&&r<=m.maxBitrate)??X(Zi),o=s?s.level:0,c="64".padStart(2,"0"),u="00",l=o.toString(16).padStart(2,"0");return`avc1.${c}${u}${l}`}else if(n==="hevc"){let i="",s="6",o=t*e,c=Fu.find(l=>o<=l.maxPictureSize&&r<=l.maxBitrate)??X(Fu);return`hev1.${i}1.${s}.${c.tier}${c.level}.B0`}else{if(n==="vp8")return"vp8";if(n==="vp9"){let i="00",a=t*e,s=Ut.find(c=>a<=c.maxPictureSize&&r<=c.maxBitrate)??X(Ut);return`vp09.${i}.${s.level.toString().padStart(2,"0")}.08`}else if(n==="av1"){let a=t*e,s=Mu.find(u=>a<=u.maxPictureSize&&r<=u.maxBitrate)??X(Mu);return`av01.0.${s.level.toString().padStart(2,"0")}${s.tier}.08`}}throw new TypeError(`Unhandled codec '${n}'.`)},Du=n=>{let t=n.split("."),e=Number(t[1]),r=Number(t[2]),i=Number(t[3]),a=t[4]?Number(t[4]):1;return[1,1,e,2,1,r,3,1,i,4,1,a]},Qn=n=>{let t=n.split("."),i=(1<<7)+1,a=Number(t[1]),s=t[2],o=Number(s.slice(0,-1)),c=(a<<5)+o,u=s.slice(-1)==="H"?1:0,m=Number(t[3])===8?0:1,d=0,f=t[4]?Number(t[4]):0,p=t[5]?Number(t[5][0]):1,b=t[5]?Number(t[5][1]):1,g=t[5]?Number(t[5][2]):0,y=(u<<7)+(m<<6)+(d<<5)+(f<<4)+(p<<3)+(b<<2)+g;return[i,c,y,0]},ri=n=>{let{codec:t,codecDescription:e,colorSpace:r,avcCodecInfo:i,hevcCodecInfo:a,vp9CodecInfo:s,av1CodecInfo:o}=n;if(t==="avc"){if(h(n.avcType!==null),i){let c=new Uint8Array([i.avcProfileIndication,i.profileCompatibility,i.avcLevelIndication]);return`avc${n.avcType}.${tr(c)}`}if(!e||e.byteLength<4)throw new TypeError("AVC decoder description is not provided or is not at least 4 bytes long.");return`avc${n.avcType}.${tr(e.subarray(1,4))}`}else if(t==="hevc"){let c,u,l,m,d,f;if(a)c=a.generalProfileSpace,u=a.generalProfileIdc,l=co(a.generalProfileCompatibilityFlags),m=a.generalTierFlag,d=a.generalLevelIdc,f=[...a.generalConstraintIndicatorFlags];else{if(!e||e.byteLength<23)throw new TypeError("HEVC decoder description is not provided or is not at least 23 bytes long.");let b=V(e),g=b.getUint8(1);c=g>>6&3,u=g&31,l=co(b.getUint32(2)),m=g>>5&1,d=b.getUint8(12),f=[];for(let y=0;y<6;y++)f.push(b.getUint8(6+y))}let p="hev1.";for(p+=["","A","B","C"][c]+u,p+=".",p+=l.toString(16).toUpperCase(),p+=".",p+=m===0?"L":"H",p+=d;f.length>0&&f[f.length-1]===0;)f.pop();return f.length>0&&(p+=".",p+=f.map(b=>b.toString(16).toUpperCase()).join(".")),p}else{if(t==="vp8")return"vp8";if(t==="vp9"){if(!s){let y=n.width*n.height,k=X(Ut).level;for(let T of Ut)if(y<=T.maxPictureSize){k=T.level;break}return`vp09.00.${k.toString().padStart(2,"0")}.08`}let c=s.profile.toString().padStart(2,"0"),u=s.level.toString().padStart(2,"0"),l=s.bitDepth.toString().padStart(2,"0"),m=s.chromaSubsampling.toString().padStart(2,"0"),d=s.colourPrimaries.toString().padStart(2,"0"),f=s.transferCharacteristics.toString().padStart(2,"0"),p=s.matrixCoefficients.toString().padStart(2,"0"),b=s.videoFullRangeFlag.toString().padStart(2,"0"),g=`vp09.${c}.${u}.${l}.${m}`;return g+=`.${d}.${f}.${p}.${b}`,g.endsWith(Bu)&&(g=g.slice(0,-Bu.length)),g}else if(t==="av1"){if(!o){let T=n.width*n.height,w=X(Ut).level;for(let S of Ut)if(T<=S.maxPictureSize){w=S.level;break}return`av01.0.${w.toString().padStart(2,"0")}M.08`}let c=o.profile,u=o.level.toString().padStart(2,"0"),l=o.tier?"H":"M",m=o.bitDepth.toString().padStart(2,"0"),d=o.monochrome?"1":"0",f=100*o.chromaSubsamplingX+10*o.chromaSubsamplingY+1*(o.chromaSubsamplingX&&o.chromaSubsamplingY?o.chromaSamplePosition:0),p=r?.primaries?et[r.primaries]:1,b=r?.transfer?tt[r.transfer]:1,g=r?.matrix?rt[r.matrix]:1,y=r?.fullRange?1:0,k=`av01.${c}.${u}${l}.${m}`;return k+=`.${d}.${f.toString().padStart(3,"0")}`,k+=`.${p.toString().padStart(2,"0")}`,k+=`.${b.toString().padStart(2,"0")}`,k+=`.${g.toString().padStart(2,"0")}`,k+=`.${y}`,k.endsWith(Ou)&&(k=k.slice(0,-Ou.length)),k}}throw new TypeError(`Unhandled codec '${t}'.`)},jn=(n,t,e)=>{if(n==="aac")return t>=2&&e<=24e3?"mp4a.40.29":e<=24e3?"mp4a.40.5":"mp4a.40.2";if(n==="mp3")return"mp3";if(n==="opus")return"opus";if(n==="vorbis")return"vorbis";if(n==="flac")return"flac";if(n==="ac3")return"ac-3";if(n==="eac3")return"ec-3";if(ee.includes(n))return n;throw new TypeError(`Unhandled codec '${n}'.`)},ii=n=>{let{codec:t,codecDescription:e,aacCodecInfo:r}=n;if(t==="aac"){if(!r)throw new TypeError("AAC codec info must be provided.");if(r.isMpeg2)return"mp4a.67";{let i;return r.objectType!==null?i=r.objectType:i=Tt(e).objectType,`mp4a.40.${i}`}}else{if(t==="mp3")return"mp3";if(t==="opus")return"opus";if(t==="vorbis")return"vorbis";if(t==="flac")return"flac";if(t==="ac3")return"ac-3";if(t==="eac3")return"ec-3";if(t&&ee.includes(t))return t}throw new TypeError(`Unhandled codec '${t}'.`)},Vu=n=>{},Uu=n=>{switch(n.codec){case"flac":{let t=_r("ZkxhQ4AAACIQABAAAAYtACWtCsRC8AANRBhVFucAcYu5ASE2m1Dxv8tw");return n.sampleRate>=1<<20||n.numberOfChannels>8?!1:(t[18]=n.sampleRate>>>12,t[19]=n.sampleRate>>>4,t[20]=(n.sampleRate&15)<<4|n.numberOfChannels-1<<1,t)}case"vorbis":{let t=_r("Ah7/AgF2b3JiaXMAAAAAAoC7AAAAAAAAgLUBAAAAAAC4AQN2b3JiaXMNAAAATGF2ZjU4Ljc2LjEwMAgAAAAMAAAAbGFuZ3VhZ2U9dW5kGQAAAGhhbmRsZXJfbmFtZT1Tb3VuZEhhbmRsZXIWAAAAdmVuZG9yX2lkPVswXVswXVswXVswXSAAAABlbmNvZGVyPUxhdmM1OC4xMzQuMTAwIGxpYnZvcmJpcxAAAABtYWpvcl9icmFuZD1pc29tEQAAAG1pbm9yX3ZlcnNpb249NTEyIgAAAGNvbXBhdGlibGVfYnJhbmRzPWlzb21pc28yYXZjMW1wNDEmAAAAREVTQ1JJUFRJT049TWFkZSB3aXRoIFJlbW90aW9uIDQuMC4yNzgBBXZvcmJpcyVCQ1YBAEAAACRzGCpGpXMWhBAaQlAZ4xxCzmvsGUJMEYIcMkxbyyVzkCGkoEKIWyiB0JBVAABAAACHQXgUhIpBCCGEJT1YkoMnPQghhIg5eBSEaUEIIYQQQgghhBBCCCGERTlokoMnQQgdhOMwOAyD5Tj4HIRFOVgQgydB6CCED0K4moOsOQghhCQ1SFCDBjnoHITCLCiKgsQwuBaEBDUojILkMMjUgwtCiJqDSTX4GoRnQXgWhGlBCCGEJEFIkIMGQcgYhEZBWJKDBjm4FITLQagahCo5CB+EIDRkFQCQAACgoiiKoigKEBqyCgDIAAAQQFEUx3EcyZEcybEcCwgNWQUAAAEACAAAoEiKpEiO5EiSJFmSJVmSJVmS5omqLMuyLMuyLMsyEBqyCgBIAABQUQxFcRQHCA1ZBQBkAAAIoDiKpViKpWiK54iOCISGrAIAgAAABAAAEDRDUzxHlETPVFXXtm3btm3btm3btm3btm1blmUZCA1ZBQBAAAAQ0mlmqQaIMAMZBkJDVgEACAAAgBGKMMSA0JBVAABAAACAGEoOogmtOd+c46BZDppKsTkdnEi1eZKbirk555xzzsnmnDHOOeecopxZDJoJrTnnnMSgWQqaCa0555wnsXnQmiqtOeeccc7pYJwRxjnnnCateZCajbU555wFrWmOmkuxOeecSLl5UptLtTnnnHPOOeecc84555zqxekcnBPOOeecqL25lpvQxTnnnE/G6d6cEM4555xzzjnnnHPOOeecIDRkFQAABABAEIaNYdwpCNLnaCBGEWIaMulB9+gwCRqDnELq0ehopJQ6CCWVcVJKJwgNWQUAAAIAQAghhRRSSCGFFFJIIYUUYoghhhhyyimnoIJKKqmooowyyyyzzDLLLLPMOuyssw47DDHEEEMrrcRSU2011lhr7jnnmoO0VlprrbVSSimllFIKQkNWAQAgAAAEQgYZZJBRSCGFFGKIKaeccgoqqIDQkFUAACAAgAAAAABP8hzRER3RER3RER3RER3R8RzPESVREiVREi3TMjXTU0VVdWXXlnVZt31b2IVd933d933d+HVhWJZlWZZlWZZlWZZlWZZlWZYgNGQVAAACAAAghBBCSCGFFFJIKcYYc8w56CSUEAgNWQUAAAIACAAAAHAUR3EcyZEcSbIkS9IkzdIsT/M0TxM9URRF0zRV0RVdUTdtUTZl0zVdUzZdVVZtV5ZtW7Z125dl2/d93/d93/d93/d93/d9XQdCQ1YBABIAADqSIymSIimS4ziOJElAaMgqAEAGAEAAAIriKI7jOJIkSZIlaZJneZaomZrpmZ4qqkBoyCoAABAAQAAAAAAAAIqmeIqpeIqoeI7oiJJomZaoqZoryqbsuq7ruq7ruq7ruq7ruq7ruq7ruq7ruq7ruq7ruq7ruq7ruq4LhIasAgAkAAB0JEdyJEdSJEVSJEdygNCQVQCADACAAAAcwzEkRXIsy9I0T/M0TxM90RM901NFV3SB0JBVAAAgAIAAAAAAAAAMybAUy9EcTRIl1VItVVMt1VJF1VNVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVN0zRNEwgNWQkAkAEAkBBTLS3GmgmLJGLSaqugYwxS7KWxSCpntbfKMYUYtV4ah5RREHupJGOKQcwtpNApJq3WVEKFFKSYYyoVUg5SIDRkhQAQmgHgcBxAsixAsiwAAAAAAAAAkDQN0DwPsDQPAAAAAAAAACRNAyxPAzTPAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABA0jRA8zxA8zwAAAAAAAAA0DwP8DwR8EQRAAAAAAAAACzPAzTRAzxRBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABA0jRA8zxA8zwAAAAAAAAAsDwP8EQR0DwRAAAAAAAAACzPAzxRBDzRAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAEOAAABBgIRQasiIAiBMAcEgSJAmSBM0DSJYFTYOmwTQBkmVB06BpME0AAAAAAAAAAAAAJE2DpkHTIIoASdOgadA0iCIAAAAAAAAAAAAAkqZB06BpEEWApGnQNGgaRBEAAAAAAAAAAAAAzzQhihBFmCbAM02IIkQRpgkAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAGHAAAAgwoQwUGrIiAIgTAHA4imUBAIDjOJYFAACO41gWAABYliWKAABgWZooAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAYcAAACDChDBQashIAiAIAcCiKZQHHsSzgOJYFJMmyAJYF0DyApgFEEQAIAAAocAAACLBBU2JxgEJDVgIAUQAABsWxLE0TRZKkaZoniiRJ0zxPFGma53meacLzPM80IYqiaJoQRVE0TZimaaoqME1VFQAAUOAAABBgg6bE4gCFhqwEAEICAByKYlma5nmeJ4qmqZokSdM8TxRF0TRNU1VJkqZ5niiKommapqqyLE3zPFEURdNUVVWFpnmeKIqiaaqq6sLzPE8URdE0VdV14XmeJ4qiaJqq6roQRVE0TdNUTVV1XSCKpmmaqqqqrgtETxRNU1Vd13WB54miaaqqq7ouEE3TVFVVdV1ZBpimaaqq68oyQFVV1XVdV5YBqqqqruu6sgxQVdd1XVmWZQCu67qyLMsCAAAOHAAAAoygk4wqi7DRhAsPQKEhKwKAKAAAwBimFFPKMCYhpBAaxiSEFEImJaXSUqogpFJSKRWEVEoqJaOUUmopVRBSKamUCkIqJZVSAADYgQMA2IGFUGjISgAgDwCAMEYpxhhzTiKkFGPOOScRUoox55yTSjHmnHPOSSkZc8w556SUzjnnnHNSSuacc845KaVzzjnnnJRSSuecc05KKSWEzkEnpZTSOeecEwAAVOAAABBgo8jmBCNBhYasBABSAQAMjmNZmuZ5omialiRpmud5niiapiZJmuZ5nieKqsnzPE8URdE0VZXneZ4oiqJpqirXFUXTNE1VVV2yLIqmaZqq6rowTdNUVdd1XZimaaqq67oubFtVVdV1ZRm2raqq6rqyDFzXdWXZloEsu67s2rIAAPAEBwCgAhtWRzgpGgssNGQlAJABAEAYg5BCCCFlEEIKIYSUUggJAAAYcAAACDChDBQashIASAUAAIyx1lprrbXWQGettdZaa62AzFprrbXWWmuttdZaa6211lJrrbXWWmuttdZaa6211lprrbXWWmuttdZaa6211lprrbXWWmuttdZaa6211lprrbXWWmstpZRSSimllFJKKaWUUkoppZRSSgUA+lU4APg/2LA6wknRWGChISsBgHAAAMAYpRhzDEIppVQIMeacdFRai7FCiDHnJKTUWmzFc85BKCGV1mIsnnMOQikpxVZjUSmEUlJKLbZYi0qho5JSSq3VWIwxqaTWWoutxmKMSSm01FqLMRYjbE2ptdhqq7EYY2sqLbQYY4zFCF9kbC2m2moNxggjWywt1VprMMYY3VuLpbaaizE++NpSLDHWXAAAd4MDAESCjTOsJJ0VjgYXGrISAAgJACAQUooxxhhzzjnnpFKMOeaccw5CCKFUijHGnHMOQgghlIwx5pxzEEIIIYRSSsaccxBCCCGEkFLqnHMQQgghhBBKKZ1zDkIIIYQQQimlgxBCCCGEEEoopaQUQgghhBBCCKmklEIIIYRSQighlZRSCCGEEEIpJaSUUgohhFJCCKGElFJKKYUQQgillJJSSimlEkoJJYQSUikppRRKCCGUUkpKKaVUSgmhhBJKKSWllFJKIYQQSikFAAAcOAAABBhBJxlVFmGjCRcegEJDVgIAZAAAkKKUUiktRYIipRikGEtGFXNQWoqocgxSzalSziDmJJaIMYSUk1Qy5hRCDELqHHVMKQYtlRhCxhik2HJLoXMOAAAAQQCAgJAAAAMEBTMAwOAA4XMQdAIERxsAgCBEZohEw0JweFAJEBFTAUBigkIuAFRYXKRdXECXAS7o4q4DIQQhCEEsDqCABByccMMTb3jCDU7QKSp1IAAAAAAADADwAACQXAAREdHMYWRobHB0eHyAhIiMkAgAAAAAABcAfAAAJCVAREQ0cxgZGhscHR4fICEiIyQBAIAAAgAAAAAggAAEBAQAAAAAAAIAAAAEBA=="),e=V(t);return e.setUint8(15,n.numberOfChannels),e.setUint32(16,n.sampleRate,!0),t}default:return}},nt=48e3,zu=/^pcm-([usf])(\d+)(be)?$/,Se=n=>{if(h(ee.includes(n)),n==="ulaw")return{dataType:"ulaw",sampleSize:1,littleEndian:!0,silentValue:255};if(n==="alaw")return{dataType:"alaw",sampleSize:1,littleEndian:!0,silentValue:213};let t=zu.exec(n);h(t);let e;t[1]==="u"?e="unsigned":t[1]==="s"?e="signed":e="float";let r=Number(t[2])/8,i=t[3]!=="be",a=n==="pcm-u8"?2**7:0;return{dataType:e,sampleSize:r,littleEndian:i,silentValue:a}},Ge=n=>n.startsWith("avc1")||n.startsWith("avc3")?"avc":n.startsWith("hev1")||n.startsWith("hvc1")?"hevc":n==="vp8"?"vp8":n.startsWith("vp09")?"vp9":n.startsWith("av01")?"av1":n==="mp3"||n==="mp4a.69"||n==="mp4a.6B"||n==="mp4a.6b"||n==="mp4a.40.34"?"mp3":n.startsWith("mp4a.40.")||n==="mp4a.67"?"aac":n==="opus"?"opus":n==="vorbis"?"vorbis":n==="flac"?"flac":n==="ac-3"||n==="ac3"?"ac3":n==="ec-3"||n==="eac3"?"eac3":n==="ulaw"?"ulaw":n==="alaw"?"alaw":zu.test(n)?n:n==="webvtt"?"webvtt":null,Nu=n=>n==="avc"?{avc:{format:"avc"}}:n==="hevc"?{hevc:{format:"hevc"}}:{},Lu=n=>n==="aac"?{aac:{format:"aac"}}:n==="opus"?{opus:{format:"opus"}}:{},am=["avc1","avc3","hev1","hvc1","vp8","vp09","av01"],sm=/^(avc1|avc3)\.[0-9a-fA-F]{6}$/,om=/^(hev1|hvc1)\.(?:[ABC]?\d+)\.[0-9a-fA-F]{1,8}\.[LH]\d+(?:\.[0-9a-fA-F]{1,2}){0,6}$/,cm=/^vp09(?:\.\d{2}){3}(?:(?:\.\d{2}){5})?$/,um=/^av01\.\d\.\d{2}[MH]\.\d{2}(?:\.\d\.\d{3}\.\d{2}\.\d{2}\.\d{2}\.\d)?$/,sr=n=>{if(!n)throw new TypeError("Video chunk metadata must be provided.");if(typeof n!="object")throw new TypeError("Video chunk metadata must be an object.");if(!n.decoderConfig)throw new TypeError("Video chunk metadata must include a decoder configuration.");if(typeof n.decoderConfig!="object")throw new TypeError("Video chunk metadata decoder configuration must be an object.");if(typeof n.decoderConfig.codec!="string")throw new TypeError("Video chunk metadata decoder configuration must specify a codec string.");if(!am.some(t=>n.decoderConfig.codec.startsWith(t)))throw new TypeError("Video chunk metadata decoder configuration codec string must be a valid video codec string as specified in the Mediabunny Codec Registry.");if(!Number.isInteger(n.decoderConfig.codedWidth)||n.decoderConfig.codedWidth<=0)throw new TypeError("Video chunk metadata decoder configuration must specify a valid codedWidth (positive integer).");if(!Number.isInteger(n.decoderConfig.codedHeight)||n.decoderConfig.codedHeight<=0)throw new TypeError("Video chunk metadata decoder configuration must specify a valid codedHeight (positive integer).");if(n.decoderConfig.displayAspectWidth!==void 0&&(!Number.isInteger(n.decoderConfig.displayAspectWidth)||n.decoderConfig.displayAspectWidth<=0))throw new TypeError("Video chunk metadata decoder configuration displayAspectWidth, when defined, must be a positive integer.");if(n.decoderConfig.displayAspectHeight!==void 0&&(!Number.isInteger(n.decoderConfig.displayAspectHeight)||n.decoderConfig.displayAspectHeight<=0))throw new TypeError("Video chunk metadata decoder configuration displayAspectHeight, when defined, must be a positive integer.");if(n.decoderConfig.displayAspectWidth!==void 0!=(n.decoderConfig.displayAspectHeight!==void 0))throw new TypeError("Video chunk metadata decoder configuration must specify both displayAspectWidth and displayAspectHeight, or neither.");if(n.decoderConfig.description!==void 0&&!Rt(n.decoderConfig.description))throw new TypeError("Video chunk metadata decoder configuration description, when defined, must be an ArrayBuffer or an ArrayBuffer view.");if(n.decoderConfig.colorSpace!==void 0){let{colorSpace:t}=n.decoderConfig;if(typeof t!="object")throw new TypeError("Video chunk metadata decoder configuration colorSpace, when provided, must be an object.");let e=Object.keys(et);if(t.primaries!=null&&!e.includes(t.primaries))throw new TypeError(`Video chunk metadata decoder configuration colorSpace primaries, when defined, must be one of ${e.join(", ")}.`);let r=Object.keys(tt);if(t.transfer!=null&&!r.includes(t.transfer))throw new TypeError(`Video chunk metadata decoder configuration colorSpace transfer, when defined, must be one of ${r.join(", ")}.`);let i=Object.keys(rt);if(t.matrix!=null&&!i.includes(t.matrix))throw new TypeError(`Video chunk metadata decoder configuration colorSpace matrix, when defined, must be one of ${i.join(", ")}.`);if(t.fullRange!=null&&typeof t.fullRange!="boolean")throw new TypeError("Video chunk metadata decoder configuration colorSpace fullRange, when defined, must be a boolean.")}if(n.decoderConfig.codec.startsWith("avc1")||n.decoderConfig.codec.startsWith("avc3")){if(!sm.test(n.decoderConfig.codec))throw new TypeError("Video chunk metadata decoder configuration codec string for AVC must be a valid AVC codec string as specified in Section 3.4 of RFC 6381.")}else if(n.decoderConfig.codec.startsWith("hev1")||n.decoderConfig.codec.startsWith("hvc1")){if(!om.test(n.decoderConfig.codec))throw new TypeError("Video chunk metadata decoder configuration codec string for HEVC must be a valid HEVC codec string as specified in Section E.3 of ISO 14496-15.")}else if(n.decoderConfig.codec.startsWith("vp8")){if(n.decoderConfig.codec!=="vp8")throw new TypeError('Video chunk metadata decoder configuration codec string for VP8 must be "vp8".')}else if(n.decoderConfig.codec.startsWith("vp09")){if(!cm.test(n.decoderConfig.codec))throw new TypeError('Video chunk metadata decoder configuration codec string for VP9 must be a valid VP9 codec string as specified in Section "Codecs Parameter String" of https://www.webmproject.org/vp9/mp4/.')}else if(n.decoderConfig.codec.startsWith("av01")&&!um.test(n.decoderConfig.codec))throw new TypeError('Video chunk metadata decoder configuration codec string for AV1 must be a valid AV1 codec string as specified in Section "Codecs Parameter String" of https://aomediacodec.github.io/av1-isobmff/.')},lm=["mp4a","mp3","opus","vorbis","flac","ulaw","alaw","pcm","ac-3","ec-3"],Ee=n=>{if(!n)throw new TypeError("Audio chunk metadata must be provided.");if(typeof n!="object")throw new TypeError("Audio chunk metadata must be an object.");if(!n.decoderConfig)throw new TypeError("Audio chunk metadata must include a decoder configuration.");if(typeof n.decoderConfig!="object")throw new TypeError("Audio chunk metadata decoder configuration must be an object.");if(typeof n.decoderConfig.codec!="string")throw new TypeError("Audio chunk metadata decoder configuration must specify a codec string.");if(!lm.some(t=>n.decoderConfig.codec.startsWith(t)))throw new TypeError("Audio chunk metadata decoder configuration codec string must be a valid audio codec string as specified in the Mediabunny Codec Registry.");if(!Number.isInteger(n.decoderConfig.sampleRate)||n.decoderConfig.sampleRate<=0)throw new TypeError("Audio chunk metadata decoder configuration must specify a valid sampleRate (positive integer).");if(!Number.isInteger(n.decoderConfig.numberOfChannels)||n.decoderConfig.numberOfChannels<=0)throw new TypeError("Audio chunk metadata decoder configuration must specify a valid numberOfChannels (positive integer).");if(n.decoderConfig.description!==void 0&&!Rt(n.decoderConfig.description))throw new TypeError("Audio chunk metadata decoder configuration description, when defined, must be an ArrayBuffer or an ArrayBuffer view.");if(n.decoderConfig.codec.startsWith("mp4a")&&n.decoderConfig.codec!=="mp4a.69"&&n.decoderConfig.codec!=="mp4a.6B"&&n.decoderConfig.codec!=="mp4a.6b"){if(!["mp4a.40.2","mp4a.40.02","mp4a.40.5","mp4a.40.05","mp4a.40.29","mp4a.67"].includes(n.decoderConfig.codec))throw new TypeError("Audio chunk metadata decoder configuration codec string for AAC must be a valid AAC codec string as specified in https://www.w3.org/TR/webcodecs-aac-codec-registration/.")}else if(n.decoderConfig.codec.startsWith("mp3")||n.decoderConfig.codec.startsWith("mp4a")){if(n.decoderConfig.codec!=="mp3"&&n.decoderConfig.codec!=="mp4a.69"&&n.decoderConfig.codec!=="mp4a.6B"&&n.decoderConfig.codec!=="mp4a.6b")throw new TypeError('Audio chunk metadata decoder configuration codec string for MP3 must be "mp3", "mp4a.69" or "mp4a.6B".')}else if(n.decoderConfig.codec.startsWith("opus")){if(n.decoderConfig.codec!=="opus")throw new TypeError('Audio chunk metadata decoder configuration codec string for Opus must be "opus".');if(n.decoderConfig.description&&n.decoderConfig.description.byteLength<18)throw new TypeError("Audio chunk metadata decoder configuration description, when specified, is expected to be an Identification Header as specified in Section 5.1 of RFC 7845.")}else if(n.decoderConfig.codec.startsWith("vorbis")){if(n.decoderConfig.codec!=="vorbis")throw new TypeError('Audio chunk metadata decoder configuration codec string for Vorbis must be "vorbis".');if(!n.decoderConfig.description)throw new TypeError("Audio chunk metadata decoder configuration for Vorbis must include a description, which is expected to adhere to the format described in https://www.w3.org/TR/webcodecs-vorbis-codec-registration/.")}else if(n.decoderConfig.codec.startsWith("flac")){if(n.decoderConfig.codec!=="flac")throw new TypeError('Audio chunk metadata decoder configuration codec string for FLAC must be "flac".');if(!n.decoderConfig.description||n.decoderConfig.description.byteLength<42)throw new TypeError("Audio chunk metadata decoder configuration for FLAC must include a description, which is expected to adhere to the format described in https://www.w3.org/TR/webcodecs-flac-codec-registration/.")}else if(n.decoderConfig.codec.startsWith("ac-3")||n.decoderConfig.codec.startsWith("ac3")){if(n.decoderConfig.codec!=="ac-3")throw new TypeError('Audio chunk metadata decoder configuration codec string for AC-3 must be "ac-3".')}else if(n.decoderConfig.codec.startsWith("ec-3")||n.decoderConfig.codec.startsWith("eac3")){if(n.decoderConfig.codec!=="ec-3")throw new TypeError('Audio chunk metadata decoder configuration codec string for EC-3 must be "ec-3".')}else if((n.decoderConfig.codec.startsWith("pcm")||n.decoderConfig.codec.startsWith("ulaw")||n.decoderConfig.codec.startsWith("alaw"))&&!ee.includes(n.decoderConfig.codec))throw new TypeError(`Audio chunk metadata decoder configuration codec string for PCM must be one of the supported PCM codecs (${ee.join(", ")}).`)},Xn=n=>{if(!n)throw new TypeError("Subtitle metadata must be provided.");if(typeof n!="object")throw new TypeError("Subtitle metadata must be an object.");if(!n.config)throw new TypeError("Subtitle metadata must include a config object.");if(typeof n.config!="object")throw new TypeError("Subtitle metadata config must be an object.");if(typeof n.config.description!="string")throw new TypeError("Subtitle metadata config description must be a string.")};var dm=[44100,48e3,32e3],$n=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,32,40,48,56,64,80,96,112,128,160,192,224,256,320,-1,-1,32,48,56,64,80,96,112,128,160,192,224,256,320,384,-1,-1,32,64,96,128,160,192,224,256,288,320,352,384,416,448,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,8,16,24,32,40,48,56,64,80,96,112,128,144,160,-1,-1,8,16,24,32,40,48,56,64,80,96,112,128,144,160,-1,-1,32,48,56,64,80,96,112,128,144,160,176,192,224,256,-1],or=1483304551,ni=1231971951,Yn=(n,t,e,r,i)=>t===0?0:t===1?Math.floor(144*e/(r<<n))+i:t===2?Math.floor(144*e/r)+i:(Math.floor(12*e/r)+i)*4,Wu=(n,t,e,r)=>t===0?0:t===1?144*e/(r<<n):t===2?144*e/r:12*e/r*4,cr=(n,t)=>n===3?t===3?21:36:t===3?13:21,Mr=(n,t)=>{let e=n>>>24,r=n>>>16&255,i=n>>>8&255,a=n&255;if(e!==255&&r!==255&&i!==255&&a!==255)return{header:null,bytesAdvanced:4};if(e!==255)return{header:null,bytesAdvanced:1};if((r&224)!==224)return{header:null,bytesAdvanced:1};let s=0,o=0;r&16?s=r&8?0:1:(s=1,o=1);let c=r>>3&3,u=r>>1&3,l=i>>4&15,m=(i>>2&3)%3,d=i>>1&1,f=a>>6&3,p=a>>4&3,b=a>>3&1,g=a>>2&1,y=a&3,k=$n[s*16*4+u*16+l];if(k===-1)return{header:null,bytesAdvanced:1};let T=k*1e3,w=dm[m]>>s+o,S=Yn(s,u,T,w,d);if(t!==null&&t<S)return{header:null,bytesAdvanced:1};let x;return c===3?x=u===3?384:1152:u===3?x=384:u===2?x=1152:x=576,{header:{totalSize:S,mpegVersionId:c,lowSamplingFrequency:s,layer:u,bitrate:T,frequencyIndex:m,sampleRate:w,channel:f,modeExtension:p,copyright:b,original:g,emphasis:y,audioSamplesInFrame:x},bytesAdvanced:1}},Hu=n=>{let t=127,e=0,r=n;for(;(t^2147483647)!==0;)e=r&~t,e<<=1,e|=r&t,t=(t+1<<8)-1,r=e;return e},Zn=n=>{let t=2130706432,e=0;for(;t!==0;)e>>=1,e|=n&t,t>>=8;return e};var Br=[48e3,44100,32e3],go=[24e3,22050,16e3];var ur=function*(n){let t=0,e=-1;for(;t<n.length-2;){let r=n.indexOf(0,t);if(r===-1||r>=n.length-2)break;t=r;let i=0;if(t+3<n.length&&n[t+1]===0&&n[t+2]===0&&n[t+3]===1?i=4:n[t+1]===0&&n[t+2]===1&&(i=3),i===0){t++;continue}e!==-1&&t>e&&(yield{offset:e,length:t-e}),e=t+i,t=e}e!==-1&&e<n.length&&(yield{offset:e,length:n.length-e})},Jn=function*(n,t){let e=0,r=new DataView(n.buffer,n.byteOffset,n.byteLength);for(;e+t<=n.length;){let i;t===1?i=r.getUint8(e):t===2?i=r.getUint16(e,!1):t===3?i=Cr(r,e,!1):(h(t===4),i=r.getUint32(e,!1)),e+=t,yield{offset:e,length:i},e+=i}},yo=(n,t)=>{if(t.description){let i=(j(t.description)[4]&3)+1;return Jn(n,i)}else return ur(n)},zt=n=>n&31,ea=n=>{let t=[],e=n.length;for(let r=0;r<e;r++)r+2<e&&n[r]===0&&n[r+1]===0&&n[r+2]===3?(t.push(0,0),r+=2):t.push(n[r]);return new Uint8Array(t)},bo=new Uint8Array([0,0,0,1]),en=n=>{let t=n.reduce((i,a)=>i+bo.byteLength+a.byteLength,0),e=new Uint8Array(t),r=0;for(let i of n)e.set(bo,r),r+=bo.byteLength,e.set(i,r),r+=i.byteLength;return e},ta=(n,t)=>{let e=n.reduce((a,s)=>a+t+s.byteLength,0),r=new Uint8Array(e),i=0;for(let a of n){let s=new DataView(r.buffer,r.byteOffset,r.byteLength);switch(t){case 1:s.setUint8(i,a.byteLength);break;case 2:s.setUint16(i,a.byteLength,!1);break;case 3:Ir(s,i,a.byteLength,!1);break;case 4:s.setUint32(i,a.byteLength,!1);break}i+=t,r.set(a,i),i+=a.byteLength}return r},Ku=(n,t)=>{if(t.description){let i=(j(t.description)[4]&3)+1;return ta(n,i)}else return en(n)},ai=n=>{try{let t=[],e=[],r=[];for(let o of ur(n)){let c=n.subarray(o.offset,o.offset+o.length),u=zt(c[0]);u===7?t.push(c):u===8?e.push(c):u===13&&r.push(c)}if(t.length===0||e.length===0)return null;let i=t[0],a=tn(i);h(a!==null);let s=a.profileIdc===100||a.profileIdc===110||a.profileIdc===122||a.profileIdc===144;return{configurationVersion:1,avcProfileIndication:a.profileIdc,profileCompatibility:a.constraintFlags,avcLevelIndication:a.levelIdc,lengthSizeMinusOne:3,sequenceParameterSets:t,pictureParameterSets:e,chromaFormat:s?a.chromaFormatIdc:null,bitDepthLumaMinus8:s?a.bitDepthLumaMinus8:null,bitDepthChromaMinus8:s?a.bitDepthChromaMinus8:null,sequenceParameterSetExt:s?r:null}}catch(t){return console.error("Error building AVC Decoder Configuration Record:",t),null}},Qu=n=>{let t=[];t.push(n.configurationVersion),t.push(n.avcProfileIndication),t.push(n.profileCompatibility),t.push(n.avcLevelIndication),t.push(252|n.lengthSizeMinusOne&3),t.push(224|n.sequenceParameterSets.length&31);for(let e of n.sequenceParameterSets){let r=e.byteLength;t.push(r>>8),t.push(r&255);for(let i=0;i<r;i++)t.push(e[i])}t.push(n.pictureParameterSets.length);for(let e of n.pictureParameterSets){let r=e.byteLength;t.push(r>>8),t.push(r&255);for(let i=0;i<r;i++)t.push(e[i])}if(n.avcProfileIndication===100||n.avcProfileIndication===110||n.avcProfileIndication===122||n.avcProfileIndication===144){h(n.chromaFormat!==null),h(n.bitDepthLumaMinus8!==null),h(n.bitDepthChromaMinus8!==null),h(n.sequenceParameterSetExt!==null),t.push(252|n.chromaFormat&3),t.push(248|n.bitDepthLumaMinus8&7),t.push(248|n.bitDepthChromaMinus8&7),t.push(n.sequenceParameterSetExt.length);for(let e of n.sequenceParameterSetExt){let r=e.byteLength;t.push(r>>8),t.push(r&255);for(let i=0;i<r;i++)t.push(e[i])}}return new Uint8Array(t)},ra=n=>{try{let t=V(n),e=0,r=t.getUint8(e++),i=t.getUint8(e++),a=t.getUint8(e++),s=t.getUint8(e++),o=t.getUint8(e++)&3,c=t.getUint8(e++)&31,u=[];for(let f=0;f<c;f++){let p=t.getUint16(e,!1);e+=2,u.push(n.subarray(e,e+p)),e+=p}let l=t.getUint8(e++),m=[];for(let f=0;f<l;f++){let p=t.getUint16(e,!1);e+=2,m.push(n.subarray(e,e+p)),e+=p}let d={configurationVersion:r,avcProfileIndication:i,profileCompatibility:a,avcLevelIndication:s,lengthSizeMinusOne:o,sequenceParameterSets:u,pictureParameterSets:m,chromaFormat:null,bitDepthLumaMinus8:null,bitDepthChromaMinus8:null,sequenceParameterSetExt:null};if((i===100||i===110||i===122||i===144)&&e+4<=n.length){let f=t.getUint8(e++)&3,p=t.getUint8(e++)&7,b=t.getUint8(e++)&7,g=t.getUint8(e++);d.chromaFormat=f,d.bitDepthLumaMinus8=p,d.bitDepthChromaMinus8=b;let y=[];for(let k=0;k<g;k++){let T=t.getUint16(e,!1);e+=2,y.push(n.subarray(e,e+T)),e+=T}d.sequenceParameterSetExt=y}return d}catch(t){return console.error("Error deserializing AVC Decoder Configuration Record:",t),null}},ju={1:{num:1,den:1},2:{num:12,den:11},3:{num:10,den:11},4:{num:16,den:11},5:{num:40,den:33},6:{num:24,den:11},7:{num:20,den:11},8:{num:32,den:11},9:{num:80,den:33},10:{num:18,den:11},11:{num:15,den:11},12:{num:64,den:33},13:{num:160,den:99},14:{num:4,den:3},15:{num:3,den:2},16:{num:2,den:1}},tn=n=>{try{let t=new W(ea(n));if(t.skipBits(1),t.skipBits(2),t.readBits(5)!==7)return null;let r=t.readAlignedByte(),i=t.readAlignedByte(),a=t.readAlignedByte();M(t);let s=1,o=0,c=0,u=0;if((r===100||r===110||r===122||r===244||r===44||r===83||r===86||r===118||r===128)&&(s=M(t),s===3&&(u=t.readBits(1)),o=M(t),c=M(t),t.skipBits(1),t.readBits(1))){for(let v=0;v<(s!==3?8:12);v++)if(t.readBits(1)){let D=v<6?16:64,U=8,z=8;for(let q=0;q<D;q++){if(z!==0){let ke=gt(t);z=(U+ke+256)%256}U=z===0?U:z}}}M(t);let l=M(t);if(l===0)M(t);else if(l===1){t.skipBits(1),gt(t),gt(t);let R=M(t);for(let v=0;v<R;v++)gt(t)}M(t),t.skipBits(1);let m=M(t),d=M(t),f=16*(m+1),p=16*(d+1),b=f,g=p,y=t.readBits(1);if(y||t.skipBits(1),t.skipBits(1),t.readBits(1)){let R=M(t),v=M(t),E=M(t),D=M(t),U,z;if((u===0?s:0)===0)U=1,z=2-y;else{let ke=s===3?1:2,Te=s===1?2:1;U=ke,z=Te*(2-y)}b-=U*(R+v),g-=z*(E+D)}let T=2,w=2,S=2,x=0,P={num:1,den:1},A=null,C=null;if(t.readBits(1)){if(t.readBits(1)){let Te=t.readBits(8);if(Te===255)P={num:t.readBits(16),den:t.readBits(16)};else{let Un=ju[Te];Un&&(P=Un)}}t.readBits(1)&&t.skipBits(1),t.readBits(1)&&(t.skipBits(3),x=t.readBits(1),t.readBits(1)&&(T=t.readBits(8),w=t.readBits(8),S=t.readBits(8))),t.readBits(1)&&(M(t),M(t)),t.readBits(1)&&(t.skipBits(32),t.skipBits(32),t.skipBits(1));let z=t.readBits(1);z&&qu(t);let q=t.readBits(1);q&&qu(t),(z||q)&&t.skipBits(1),t.skipBits(1),t.readBits(1)&&(t.skipBits(1),M(t),M(t),M(t),M(t),A=M(t),C=M(t))}if(A===null){h(C===null);let R=i&16;if((r===44||r===86||r===100||r===110||r===122||r===244)&&R)A=0,C=0;else{let v=m+1,E=d+1,D=(2-y)*E,U=Zi.find(q=>q.level>=a)??X(Zi),z=Math.min(Math.floor(U.maxDpbMbs/(v*D)),16);A=z,C=z}}return h(C!==null),{profileIdc:r,constraintFlags:i,levelIdc:a,frameMbsOnlyFlag:y,chromaFormatIdc:s,bitDepthLumaMinus8:o,bitDepthChromaMinus8:c,codedWidth:f,codedHeight:p,displayWidth:b,displayHeight:g,pixelAspectRatio:P,colourPrimaries:T,matrixCoefficients:S,transferCharacteristics:w,fullRangeFlag:x,numReorderFrames:A,maxDecFrameBuffering:C}}catch(t){return console.error("Error parsing AVC SPS:",t),null}},qu=n=>{let t=M(n);n.skipBits(4),n.skipBits(4);for(let e=0;e<=t;e++)M(n),M(n),n.skipBits(1);n.skipBits(5),n.skipBits(5),n.skipBits(5),n.skipBits(5)},mm=(n,t)=>{if(t.description){let i=(j(t.description)[21]&3)+1;return ta(n,i)}else return en(n)},Ji=(n,t)=>{if(t.description){let i=(j(t.description)[21]&3)+1;return Jn(n,i)}else return ur(n)},at=n=>n>>1&63,ko=n=>{try{let t=new W(ea(n));t.skipBits(16),t.readBits(4);let e=t.readBits(3),r=t.readBits(1),{general_profile_space:i,general_tier_flag:a,general_profile_idc:s,general_profile_compatibility_flags:o,general_constraint_indicator_flags:c,general_level_idc:u}=fm(t,e);M(t);let l=M(t),m=0;l===3&&(m=t.readBits(1));let d=M(t),f=M(t),p=d,b=f;if(t.readBits(1)){let v=M(t),E=M(t),D=M(t),U=M(t),z=1,q=1,ke=m===0?l:0;ke===1?(z=2,q=2):ke===2&&(z=2,q=1),p-=(v+E)*z,b-=(D+U)*q}let g=M(t),y=M(t);M(t);let T=t.readBits(1)?0:e,w=0;for(let v=T;v<=e;v++)M(t),w=M(t),M(t);M(t),M(t),M(t),M(t),M(t),M(t),t.readBits(1)&&t.readBits(1)&&pm(t),t.skipBits(1),t.skipBits(1),t.readBits(1)&&(t.skipBits(4),t.skipBits(4),M(t),M(t),t.skipBits(1));let S=M(t);if(hm(t,S),t.readBits(1)){let v=M(t);for(let E=0;E<v;E++)M(t),t.skipBits(1)}t.skipBits(1),t.skipBits(1);let x=2,P=2,A=2,C=0,I=0,R={num:1,den:1};if(t.readBits(1)){let v=bm(t,e);R=v.pixelAspectRatio,x=v.colourPrimaries,P=v.transferCharacteristics,A=v.matrixCoefficients,C=v.fullRangeFlag,I=v.minSpatialSegmentationIdc}return{displayWidth:p,displayHeight:b,pixelAspectRatio:R,colourPrimaries:x,transferCharacteristics:P,matrixCoefficients:A,fullRangeFlag:C,maxDecFrameBuffering:w+1,spsMaxSubLayersMinus1:e,spsTemporalIdNestingFlag:r,generalProfileSpace:i,generalTierFlag:a,generalProfileIdc:s,generalProfileCompatibilityFlags:o,generalConstraintIndicatorFlags:c,generalLevelIdc:u,chromaFormatIdc:l,bitDepthLumaMinus8:g,bitDepthChromaMinus8:y,minSpatialSegmentationIdc:I}}catch(t){return console.error("Error parsing HEVC SPS:",t),null}},si=n=>{try{let t=[],e=[],r=[],i=[];for(let u of ur(n)){let l=n.subarray(u.offset,u.offset+u.length),m=at(l[0]);m===32?t.push(l):m===33?e.push(l):m===34?r.push(l):(m===39||m===40)&&i.push(l)}if(e.length===0||r.length===0)return null;let a=ko(e[0]);if(!a)return null;let s=0;if(r.length>0){let u=r[0],l=new W(ea(u));l.skipBits(16),M(l),M(l),l.skipBits(1),l.skipBits(1),l.skipBits(3),l.skipBits(1),l.skipBits(1),M(l),M(l),gt(l),l.skipBits(1),l.skipBits(1),l.readBits(1)&&M(l),gt(l),gt(l),l.skipBits(1),l.skipBits(1),l.skipBits(1),l.skipBits(1);let m=l.readBits(1),d=l.readBits(1);!m&&!d?s=0:m&&!d?s=2:!m&&d?s=3:s=0}let o=[...t.length?[{arrayCompleteness:1,nalUnitType:32,nalUnits:t}]:[],...e.length?[{arrayCompleteness:1,nalUnitType:33,nalUnits:e}]:[],...r.length?[{arrayCompleteness:1,nalUnitType:34,nalUnits:r}]:[],...i.length?[{arrayCompleteness:1,nalUnitType:at(i[0][0]),nalUnits:i}]:[]];return{configurationVersion:1,generalProfileSpace:a.generalProfileSpace,generalTierFlag:a.generalTierFlag,generalProfileIdc:a.generalProfileIdc,generalProfileCompatibilityFlags:a.generalProfileCompatibilityFlags,generalConstraintIndicatorFlags:a.generalConstraintIndicatorFlags,generalLevelIdc:a.generalLevelIdc,minSpatialSegmentationIdc:a.minSpatialSegmentationIdc,parallelismType:s,chromaFormatIdc:a.chromaFormatIdc,bitDepthLumaMinus8:a.bitDepthLumaMinus8,bitDepthChromaMinus8:a.bitDepthChromaMinus8,avgFrameRate:0,constantFrameRate:0,numTemporalLayers:a.spsMaxSubLayersMinus1+1,temporalIdNested:a.spsTemporalIdNestingFlag,lengthSizeMinusOne:3,arrays:o}}catch(t){return console.error("Error building HEVC Decoder Configuration Record:",t),null}},fm=(n,t)=>{let e=n.readBits(2),r=n.readBits(1),i=n.readBits(5),a=0;for(let l=0;l<32;l++)a=a<<1|n.readBits(1);let s=new Uint8Array(6);for(let l=0;l<6;l++)s[l]=n.readBits(8);let o=n.readBits(8),c=[],u=[];for(let l=0;l<t;l++)c.push(n.readBits(1)),u.push(n.readBits(1));if(t>0)for(let l=t;l<8;l++)n.skipBits(2);for(let l=0;l<t;l++)c[l]&&n.skipBits(88),u[l]&&n.skipBits(8);return{general_profile_space:e,general_tier_flag:r,general_profile_idc:i,general_profile_compatibility_flags:a,general_constraint_indicator_flags:s,general_level_idc:o}},pm=n=>{for(let t=0;t<4;t++)for(let e=0;e<(t===3?2:6);e++)if(!n.readBits(1))M(n);else{let i=Math.min(64,1<<4+(t<<1));t>1&&gt(n);for(let a=0;a<i;a++)gt(n)}},hm=(n,t)=>{let e=[];for(let r=0;r<t;r++)e[r]=gm(n,r,t,e)},gm=(n,t,e,r)=>{let i=0,a=0,s=0;if(t!==0&&(a=n.readBits(1)),a){if(t===e){let c=M(n);s=t-(c+1)}else s=t-1;n.readBits(1),M(n);let o=r[s]??0;for(let c=0;c<=o;c++)n.readBits(1)||n.readBits(1);i=r[s]}else{let o=M(n),c=M(n);for(let u=0;u<o;u++)M(n),n.readBits(1);for(let u=0;u<c;u++)M(n),n.readBits(1);i=o+c}return i},bm=(n,t)=>{let e=2,r=2,i=2,a=0,s=0,o={num:1,den:1};if(n.readBits(1)){let c=n.readBits(8);if(c===255)o={num:n.readBits(16),den:n.readBits(16)};else{let u=ju[c];u&&(o=u)}}return n.readBits(1)&&n.readBits(1),n.readBits(1)&&(n.readBits(3),a=n.readBits(1),n.readBits(1)&&(e=n.readBits(8),r=n.readBits(8),i=n.readBits(8))),n.readBits(1)&&(M(n),M(n)),n.readBits(1),n.readBits(1),n.readBits(1),n.readBits(1)&&(M(n),M(n),M(n),M(n)),n.readBits(1)&&(n.readBits(32),n.readBits(32),n.readBits(1)&&M(n),n.readBits(1)&&ym(n,!0,t)),n.readBits(1)&&(n.readBits(1),n.readBits(1),n.readBits(1),s=M(n),M(n),M(n),M(n),M(n)),{pixelAspectRatio:o,colourPrimaries:e,transferCharacteristics:r,matrixCoefficients:i,fullRangeFlag:a,minSpatialSegmentationIdc:s}},ym=(n,t,e)=>{let r=!1,i=!1,a=!1;t&&(r=n.readBits(1)===1,i=n.readBits(1)===1,(r||i)&&(a=n.readBits(1)===1,a&&(n.readBits(8),n.readBits(5),n.readBits(1),n.readBits(5)),n.readBits(4),n.readBits(4),a&&n.readBits(4),n.readBits(5),n.readBits(5),n.readBits(5)));for(let s=0;s<=e;s++){let o=n.readBits(1)===1,c=!0;o||(c=n.readBits(1)===1);let u=!1;c?M(n):u=n.readBits(1)===1;let l=1;u||(l=M(n)+1),r&&Gu(n,l,a),i&&Gu(n,l,a)}},Gu=(n,t,e)=>{for(let r=0;r<t;r++)M(n),M(n),e&&(M(n),M(n)),n.readBits(1)},Xu=n=>{let t=[];t.push(n.configurationVersion),t.push((n.generalProfileSpace&3)<<6|(n.generalTierFlag&1)<<5|n.generalProfileIdc&31),t.push(n.generalProfileCompatibilityFlags>>>24&255),t.push(n.generalProfileCompatibilityFlags>>>16&255),t.push(n.generalProfileCompatibilityFlags>>>8&255),t.push(n.generalProfileCompatibilityFlags&255),t.push(...n.generalConstraintIndicatorFlags),t.push(n.generalLevelIdc&255),t.push(240|n.minSpatialSegmentationIdc>>8&15),t.push(n.minSpatialSegmentationIdc&255),t.push(252|n.parallelismType&3),t.push(252|n.chromaFormatIdc&3),t.push(248|n.bitDepthLumaMinus8&7),t.push(248|n.bitDepthChromaMinus8&7),t.push(n.avgFrameRate>>8&255),t.push(n.avgFrameRate&255),t.push((n.constantFrameRate&3)<<6|(n.numTemporalLayers&7)<<3|(n.temporalIdNested&1)<<2|n.lengthSizeMinusOne&3),t.push(n.arrays.length&255);for(let e of n.arrays){t.push((e.arrayCompleteness&1)<<7|0|e.nalUnitType&63),t.push(e.nalUnits.length>>8&255),t.push(e.nalUnits.length&255);for(let r of e.nalUnits){t.push(r.length>>8&255),t.push(r.length&255);for(let i=0;i<r.length;i++)t.push(r[i])}}return new Uint8Array(t)},$u=n=>{try{let t=V(n),e=0,r=t.getUint8(e++),i=t.getUint8(e++),a=i>>6&3,s=i>>5&1,o=i&31,c=t.getUint32(e,!1);e+=4;let u=n.subarray(e,e+6);e+=6;let l=t.getUint8(e++),m=(t.getUint8(e++)&15)<<8|t.getUint8(e++),d=t.getUint8(e++)&3,f=t.getUint8(e++)&3,p=t.getUint8(e++)&7,b=t.getUint8(e++)&7,g=t.getUint16(e,!1);e+=2;let y=t.getUint8(e++),k=y>>6&3,T=y>>3&7,w=y>>2&1,S=y&3,x=t.getUint8(e++),P=[];for(let A=0;A<x;A++){let C=t.getUint8(e++),I=C>>7&1,R=C&63,v=t.getUint16(e,!1);e+=2;let E=[];for(let D=0;D<v;D++){let U=t.getUint16(e,!1);e+=2,E.push(n.subarray(e,e+U)),e+=U}P.push({arrayCompleteness:I,nalUnitType:R,nalUnits:E})}return{configurationVersion:r,generalProfileSpace:a,generalTierFlag:s,generalProfileIdc:o,generalProfileCompatibilityFlags:c,generalConstraintIndicatorFlags:u,generalLevelIdc:l,minSpatialSegmentationIdc:m,parallelismType:d,chromaFormatIdc:f,bitDepthLumaMinus8:p,bitDepthChromaMinus8:b,avgFrameRate:g,constantFrameRate:k,numTemporalLayers:T,temporalIdNested:w,lengthSizeMinusOne:S,arrays:P}}catch(t){return console.error("Error deserializing HEVC Decoder Configuration Record:",t),null}};var Yu=(n,t)=>{let e=new Set,r=0;for(let a of Ji(n,t)){if(r===4){e.add(a.offset);continue}let s=at(n[a.offset]);if(r===3&&s!==37){e.add(a.offset);continue}let o=!1;s===35?r>0?o=!0:r=1:s<=31?r>2?o=!0:r=2:s===36?r!==2?o=!0:r=3:s===37?r<2?o=!0:r=4:s===32||s===33||s===34||s===39||s>=41&&s<=44||s>=48&&s<=55?r>1?o=!0:r=1:(s===38||s===40||s>=45&&s<=47||s>=56&&s<=63)&&r<2&&(o=!0),o&&e.add(a.offset)}if(e.size===0)return null;let i=[];for(let a of Ji(n,t))e.has(a.offset)||i.push(n.subarray(a.offset,a.offset+a.length));return mm(i,t)},ia=n=>{let t=new W(n);if(t.readBits(2)!==2)return null;let r=t.readBits(1),a=(t.readBits(1)<<1)+r;if(a===3&&t.skipBits(1),t.readBits(1)===1||t.readBits(1)!==0||(t.skipBits(2),t.readBits(24)!==4817730))return null;let u=8;a>=2&&(u=t.readBits(1)?12:10);let l=t.readBits(3),m=0,d=0;if(l!==7)if(d=t.readBits(1),a===1||a===3){let P=t.readBits(1),A=t.readBits(1);m=!P&&!A?3:P&&!A?2:1,t.skipBits(1)}else m=1;else m=3,d=1;let f=t.readBits(16),p=t.readBits(16),b=f+1,g=p+1,y=b*g,k=X(Ut).level;for(let x of Ut)if(y<=x.maxPictureSize){k=x.level;break}return{profile:a,level:k,bitDepth:u,chromaSubsampling:m,videoFullRangeFlag:d,colourPrimaries:l===2?1:l===1?6:2,transferCharacteristics:l===2?1:l===1?6:2,matrixCoefficients:l===7?0:l===2?1:l===1?6:2}},Zu=function*(n){let t=new W(n),e=()=>{let r=0;for(let i=0;i<8;i++){let a=t.readAlignedByte();if(r|=(a&127)<<i*7,!(a&128))break;if(i===7&&a&128)return null}return r>=2**32-1?null:r};for(;t.getBitsLeft()>=8;){t.skipBits(1);let r=t.readBits(4),i=t.readBits(1),a=t.readBits(1);t.skipBits(1),i&&t.skipBits(8);let s;if(a){let o=e();if(o===null)return;s=o}else s=Math.floor(t.getBitsLeft()/8);h(t.pos%8===0),yield{type:r,data:n.subarray(t.pos/8,t.pos/8+s)},t.skipBits(s*8)}},na=n=>{for(let{type:t,data:e}of Zu(n)){if(t!==1)continue;let r=new W(e),i=r.readBits(3),a=r.readBits(1),s=r.readBits(1),o=0,c=0,u=0;if(s)o=r.readBits(5);else{if(r.readBits(1)&&(r.skipBits(32),r.skipBits(32),r.readBits(1)))return null;let x=r.readBits(1);x&&(u=r.readBits(5),r.skipBits(32),r.skipBits(5),r.skipBits(5));let P=r.readBits(5);for(let A=0;A<=P;A++){r.skipBits(12);let C=r.readBits(5);if(A===0&&(o=C),C>7){let R=r.readBits(1);A===0&&(c=R)}if(x&&r.readBits(1)){let v=u+1;r.skipBits(v),r.skipBits(v),r.skipBits(1)}r.readBits(1)&&r.skipBits(4)}}let l=r.readBits(4),m=r.readBits(4),d=l+1;r.skipBits(d);let f=m+1;r.skipBits(f);let p=0;if(s?p=0:p=r.readBits(1),p&&(r.skipBits(4),r.skipBits(3)),r.skipBits(1),r.skipBits(1),r.skipBits(1),!s){r.skipBits(1),r.skipBits(1),r.skipBits(1),r.skipBits(1);let S=r.readBits(1);S&&(r.skipBits(1),r.skipBits(1));let x=r.readBits(1),P=0;x?P=2:P=r.readBits(1),P>0&&(r.readBits(1)||r.skipBits(1)),S&&r.skipBits(3)}r.skipBits(1),r.skipBits(1),r.skipBits(1);let b=r.readBits(1),g=8;i===2&&b?g=r.readBits(1)?12:10:i<=2&&(g=b?10:8);let y=0;i!==1&&(y=r.readBits(1));let k=1,T=1,w=0;return y||(i===0?(k=1,T=1):i===1?(k=0,T=0):g===12&&(k=r.readBits(1),k&&(T=r.readBits(1))),k&&T&&(w=r.readBits(2))),{profile:i,level:o,tier:c,bitDepth:g,monochrome:y,chromaSubsamplingX:k,chromaSubsamplingY:T,chromaSamplePosition:w}}return null},lr=n=>{let t=V(n),e=t.getUint8(9),r=t.getUint16(10,!0),i=t.getUint32(12,!0),a=t.getInt16(16,!0),s=t.getUint8(18),o=null;return s&&(o=n.subarray(19,21+e)),{outputChannelCount:e,preSkip:r,inputSampleRate:i,outputGain:a,channelMappingFamily:s,channelMappingTable:o}},km=[480,960,1920,2880,480,960,1920,2880,480,960,1920,2880,480,960,480,960,120,240,480,960,120,240,480,960,120,240,480,960,120,240,480,960],Ju=n=>{let t=n[0]>>3;return{durationInSamples:km[t]}},aa=n=>{if(n.length<7)throw new Error("Setup header is too short.");if(n[0]!==5)throw new Error("Wrong packet type in Setup header.");if(String.fromCharCode(...n.slice(1,7))!=="vorbis")throw new Error("Invalid packet signature in Setup header.");let e=n.length,r=new Uint8Array(e);for(let m=0;m<e;m++)r[m]=n[e-1-m];let i=new W(r),a=0;for(;i.getBitsLeft()>97;)if(i.readBits(1)===1){a=i.pos;break}if(a===0)throw new Error("Invalid Setup header: framing bit not found.");let s=0,o=!1,c=0;for(;i.getBitsLeft()>=97;){let m=i.pos,d=i.readBits(8),f=i.readBits(16),p=i.readBits(16);if(d>63||f!==0||p!==0){i.pos=m;break}if(i.skipBits(1),s++,s>64)break;i.clone().readBits(6)+1===s&&(o=!0,c=s)}if(!o)throw new Error("Invalid Setup header: mode header not found.");if(c>63)throw new Error(`Unsupported mode count: ${c}.`);let u=c;i.pos=0,i.skipBits(a);let l=Array(u).fill(0);for(let m=u-1;m>=0;m--)i.skipBits(40),l[m]=i.readBits(1);return{modeBlockflags:l}},oi=(n,t,e)=>{switch(n){case"avc":{for(let r of yo(e,t)){let i=e[r.offset],a=zt(i);if(a>=1&&a<=4)return"delta";if(a===5)return"key";if(a===6&&(!Xi()||Au()>=144)){let s=e.subarray(r.offset,r.offset+r.length),o=ea(s),c=1;do{let u=0;for(;;){let d=o[c++];if(d===void 0||(u+=d,d<255))break}let l=0;for(;;){let d=o[c++];if(d===void 0||(l+=d,d<255))break}if(u===6){let d=new W(o);d.pos=8*c;let f=M(d),p=d.readBits(1);if(f===0&&p===1)return"key"}c+=l}while(c<o.length-1)}}return"delta"}case"hevc":{for(let r of Ji(e,t)){let i=at(e[r.offset]);if(i<16)return"delta";if(i<=23)return"key"}return"delta"}case"vp8":return(e[0]&1)===0?"key":"delta";case"vp9":{let r=new W(e);if(r.readBits(2)!==2)return null;let i=r.readBits(1);return(r.readBits(1)<<1)+i===3&&r.skipBits(1),r.readBits(1)?null:r.readBits(1)===0?"key":"delta"}case"av1":{let r=!1;for(let{type:i,data:a}of Zu(e))if(i===1){let s=new W(a);s.skipBits(4),r=!!s.readBits(1)}else if(i===3||i===6||i===7){if(r)return"key";let s=new W(a);return s.readBits(1)?null:s.readBits(2)===0?"key":"delta"}return null}default:ne(n),h(!1)}};var rn=(n,t)=>{let e=V(n),r=0,i=e.getUint32(r,!0);r+=4;let a=me.decode(n.subarray(r,r+i));r+=i,i>0&&(t.raw??={},t.raw.vendor??=a);let s=e.getUint32(r,!0);r+=4;for(let o=0;o<s;o++){let c=e.getUint32(r,!0);r+=4;let u=me.decode(n.subarray(r,r+c));r+=c;let l=u.indexOf("=");if(l===-1)continue;let m=u.slice(0,l).toUpperCase(),d=u.slice(l+1);switch(t.raw??={},t.raw[m]??=d,m){case"TITLE":t.title??=d;break;case"DESCRIPTION":t.description??=d;break;case"ARTIST":t.artist??=d;break;case"ALBUM":t.album??=d;break;case"ALBUMARTIST":t.albumArtist??=d;break;case"COMMENT":t.comment??=d;break;case"LYRICS":t.lyrics??=d;break;case"TRACKNUMBER":{let f=d.split("/"),p=Number.parseInt(f[0],10),b=f[1]&&Number.parseInt(f[1],10);Number.isInteger(p)&&p>0&&(t.trackNumber??=p),b&&Number.isInteger(b)&&b>0&&(t.tracksTotal??=b)}break;case"TRACKTOTAL":{let f=Number.parseInt(d,10);Number.isInteger(f)&&f>0&&(t.tracksTotal??=f)}break;case"DISCNUMBER":{let f=d.split("/"),p=Number.parseInt(f[0],10),b=f[1]&&Number.parseInt(f[1],10);Number.isInteger(p)&&p>0&&(t.discNumber??=p),b&&Number.isInteger(b)&&b>0&&(t.discsTotal??=b)}break;case"DISCTOTAL":{let f=Number.parseInt(d,10);Number.isInteger(f)&&f>0&&(t.discsTotal??=f)}break;case"DATE":{let f=new Date(d);Number.isNaN(f.getTime())||(t.date??=f)}break;case"GENRE":t.genre??=d;break;case"METADATA_BLOCK_PICTURE":{let f=_r(d),p=V(f),b=p.getUint32(0,!1),g=p.getUint32(4,!1),y=String.fromCharCode(...f.subarray(8,8+g)),k=p.getUint32(8+g,!1),T=me.decode(f.subarray(12+g,12+g+k)),w=p.getUint32(g+k+28),S=f.subarray(g+k+32,g+k+32+w);t.images??=[],t.images.push({data:S,mimeType:y,kind:b===3?"coverFront":b===4?"coverBack":"unknown",name:void 0,description:T||void 0})}break}}},nn=(n,t,e)=>{let r=[n],a=$.encode("Mediabunny"),s=new Uint8Array(4+a.length),o=new DataView(s.buffer);o.setUint32(0,a.length,!0),s.set(a,4),r.push(s);let c=new Set,u=(p,b)=>{let g=`${p}=${b}`,y=$.encode(g);s=new Uint8Array(4+y.length),o=new DataView(s.buffer),o.setUint32(0,y.length,!0),s.set(y,4),r.push(s),c.add(p)};for(let{key:p,value:b}of it(t))switch(p){case"title":u("TITLE",b);break;case"description":u("DESCRIPTION",b);break;case"artist":u("ARTIST",b);break;case"album":u("ALBUM",b);break;case"albumArtist":u("ALBUMARTIST",b);break;case"genre":u("GENRE",b);break;case"date":{let g=t.raw?.DATE??t.raw?.date;g&&typeof g=="string"?u("DATE",g):u("DATE",b.toISOString().slice(0,10))}break;case"comment":u("COMMENT",b);break;case"lyrics":u("LYRICS",b);break;case"trackNumber":u("TRACKNUMBER",b.toString());break;case"tracksTotal":u("TRACKTOTAL",b.toString());break;case"discNumber":u("DISCNUMBER",b.toString());break;case"discsTotal":u("DISCTOTAL",b.toString());break;case"images":{if(!e)break;for(let g of b){let y=g.kind==="coverFront"?3:g.kind==="coverBack"?4:0,k=new Uint8Array(g.mimeType.length);for(let P=0;P<g.mimeType.length;P++)k[P]=g.mimeType.charCodeAt(P);let T=$.encode(g.description??""),w=new Uint8Array(8+k.length+4+T.length+16+4+g.data.length),S=V(w);S.setUint32(0,y,!1),S.setUint32(4,k.length,!1),w.set(k,8),S.setUint32(8+k.length,T.length,!1),w.set(T,12+k.length),S.setUint32(28+k.length+T.length,g.data.length,!1),w.set(g.data,32+k.length+T.length);let x=Pu(w);u("METADATA_BLOCK_PICTURE",x)}}break;case"raw":break;default:ne(p)}if(t.raw)for(let p in t.raw){let b=t.raw[p]??t.raw[p.toLowerCase()];p==="vendor"||b==null||c.has(p)||typeof b=="string"&&u(p,b)}let l=new Uint8Array(4);V(l).setUint32(0,c.size,!0),r.splice(2,0,l);let m=r.reduce((p,b)=>p+b.length,0),d=new Uint8Array(m),f=0;for(let p of r)d.set(p,f),f+=p.length;return d},an=[2,1,2,3,3,4,4,5],sa=n=>{if(n.length<7||n[0]!==11||n[1]!==119)return null;let t=new W(n);t.skipBits(16),t.skipBits(16);let e=t.readBits(2);if(e===3)return null;let r=t.readBits(6),i=t.readBits(5);if(i>8)return null;let a=t.readBits(3),s=t.readBits(3);(s&1)!==0&&s!==1&&t.skipBits(2),(s&4)!==0&&t.skipBits(2),s===2&&t.skipBits(2);let o=t.readBits(1),c=Math.floor(r/2);return{fscod:e,bsid:i,bsmod:a,acmod:s,lfeon:o,bitRateCode:c}},el=[64*2,69*2,96*2,64*2,70*2,96*2,80*2,87*2,120*2,80*2,88*2,120*2,96*2,104*2,144*2,96*2,105*2,144*2,112*2,121*2,168*2,112*2,122*2,168*2,128*2,139*2,192*2,128*2,140*2,192*2,160*2,174*2,240*2,160*2,175*2,240*2,192*2,208*2,288*2,192*2,209*2,288*2,224*2,243*2,336*2,224*2,244*2,336*2,256*2,278*2,384*2,256*2,279*2,384*2,320*2,348*2,480*2,320*2,349*2,480*2,384*2,417*2,576*2,384*2,418*2,576*2,448*2,487*2,672*2,448*2,488*2,672*2,512*2,557*2,768*2,512*2,558*2,768*2,640*2,696*2,960*2,640*2,697*2,960*2,768*2,835*2,1152*2,768*2,836*2,1152*2,896*2,975*2,1344*2,896*2,976*2,1344*2,1024*2,1114*2,1536*2,1024*2,1115*2,1536*2,1152*2,1253*2,1728*2,1152*2,1254*2,1728*2,1280*2,1393*2,1920*2,1280*2,1394*2,1920*2],tl=1536,sn=new Uint8Array([5,4,65,67,45,51]),on=new Uint8Array([5,4,69,65,67,51]),To=[1,2,3,6],oa=n=>{if(n.length<6||n[0]!==11||n[1]!==119)return null;let t=new W(n);t.skipBits(16);let e=t.readBits(2);if(t.skipBits(3),e!==0&&e!==2)return null;let r=t.readBits(11),i=t.readBits(2),a=0,s;i===3?(a=t.readBits(2),s=3):s=t.readBits(2);let o=t.readBits(3),c=t.readBits(1),u=t.readBits(5);if(u<11||u>16)return null;let l=To[s],m;return i<3?m=Br[i]/1e3:m=go[a]/1e3,{dataRate:Math.round((r+1)*m/(l*16)),substreams:[{fscod:i,fscod2:a,bsid:u,bsmod:0,acmod:o,lfeon:c,numDepSub:0,chanLoc:0}]}},rl=n=>{if(n.length<2)return null;let t=new W(n),e=t.readBits(13),r=t.readBits(3),i=[];for(let a=0;a<=r&&!(Math.ceil(t.pos/8)+3>n.length);a++){let s=t.readBits(2),o=t.readBits(5);t.skipBits(1),t.skipBits(1);let c=t.readBits(3),u=t.readBits(3),l=t.readBits(1);t.skipBits(3);let m=t.readBits(4),d=0;m>0?d=t.readBits(9):t.skipBits(1),i.push({fscod:s,fscod2:null,bsid:o,bsmod:c,acmod:u,lfeon:l,numDepSub:m,chanLoc:d})}return i.length===0?null:{dataRate:e,substreams:i}},ca=n=>{let t=n.substreams[0];return h(t),t.fscod<3?Br[t.fscod]:t.fscod2!==null&&t.fscod2<3?go[t.fscod2]:null},ua=n=>{let t=n.substreams[0];h(t);let e=an[t.acmod]+t.lfeon;if(t.numDepSub>0){let r=[2,2,1,1,2,2,2,1,1];for(let i=0;i<9;i++)t.chanLoc&1<<8-i&&(e+=r[i])}return e};var pe=class{constructor(t){this.input=t}dispose(){}};var de=new Uint8Array(0),Q=class n{constructor(t,e,r,i,a=-1,s,o){this.data=t;this.type=e;this.timestamp=r;this.duration=i;this.sequenceNumber=a;if(t===de&&s===void 0)throw new Error("Internal error: byteLength must be explicitly provided when constructing metadata-only packets.");if(s===void 0&&(s=t.byteLength),!(t instanceof Uint8Array))throw new TypeError("data must be a Uint8Array.");if(e!=="key"&&e!=="delta")throw new TypeError('type must be either "key" or "delta".');if(!Number.isFinite(r))throw new TypeError("timestamp must be a number.");if(!Number.isFinite(i)||i<0)throw new TypeError("duration must be a non-negative number.");if(!Number.isFinite(a))throw new TypeError("sequenceNumber must be a number.");if(!Number.isInteger(s)||s<0)throw new TypeError("byteLength must be a non-negative integer.");if(o!==void 0&&(typeof o!="object"||!o))throw new TypeError("sideData, when provided, must be an object.");if(o?.alpha!==void 0&&!(o.alpha instanceof Uint8Array))throw new TypeError("sideData.alpha, when provided, must be a Uint8Array.");if(o?.alphaByteLength!==void 0&&(!Number.isInteger(o.alphaByteLength)||o.alphaByteLength<0))throw new TypeError("sideData.alphaByteLength, when provided, must be a non-negative integer.");this.byteLength=s,this.sideData=o??{},this.sideData.alpha&&this.sideData.alphaByteLength===void 0&&(this.sideData.alphaByteLength=this.sideData.alpha.byteLength)}get isMetadataOnly(){return this.data===de}get microsecondTimestamp(){return Math.trunc(yt*this.timestamp)}get microsecondDuration(){return Math.trunc(yt*this.duration)}toEncodedVideoChunk(){if(this.isMetadataOnly)throw new TypeError("Metadata-only packets cannot be converted to a video chunk.");if(typeof EncodedVideoChunk>"u")throw new Error("Your browser does not support EncodedVideoChunk.");return new EncodedVideoChunk({data:this.data,type:this.type,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration})}alphaToEncodedVideoChunk(t=this.type){if(!this.sideData.alpha)throw new TypeError("This packet does not contain alpha side data.");if(this.isMetadataOnly)throw new TypeError("Metadata-only packets cannot be converted to a video chunk.");if(typeof EncodedVideoChunk>"u")throw new Error("Your browser does not support EncodedVideoChunk.");return new EncodedVideoChunk({data:this.sideData.alpha,type:t,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration})}toEncodedAudioChunk(){if(this.isMetadataOnly)throw new TypeError("Metadata-only packets cannot be converted to an audio chunk.");if(typeof EncodedAudioChunk>"u")throw new Error("Your browser does not support EncodedAudioChunk.");return new EncodedAudioChunk({data:this.data,type:this.type,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration})}static fromEncodedChunk(t,e){if(!(t instanceof EncodedVideoChunk||t instanceof EncodedAudioChunk))throw new TypeError("chunk must be an EncodedVideoChunk or EncodedAudioChunk.");let r=new Uint8Array(t.byteLength);return t.copyTo(r),new n(r,t.type,t.timestamp/1e6,(t.duration??0)/1e6,void 0,void 0,e)}clone(t){if(t!==void 0&&(typeof t!="object"||t===null))throw new TypeError("options, when provided, must be an object.");if(t?.data!==void 0&&!(t.data instanceof Uint8Array))throw new TypeError("options.data, when provided, must be a Uint8Array.");if(t?.type!==void 0&&t.type!=="key"&&t.type!=="delta")throw new TypeError('options.type, when provided, must be either "key" or "delta".');if(t?.timestamp!==void 0&&!Number.isFinite(t.timestamp))throw new TypeError("options.timestamp, when provided, must be a number.");if(t?.duration!==void 0&&!Number.isFinite(t.duration))throw new TypeError("options.duration, when provided, must be a number.");if(t?.sequenceNumber!==void 0&&!Number.isFinite(t.sequenceNumber))throw new TypeError("options.sequenceNumber, when provided, must be a number.");if(t?.sideData!==void 0&&(typeof t.sideData!="object"||t.sideData===null))throw new TypeError("options.sideData, when provided, must be an object.");return new n(t?.data??this.data,t?.type??this.type,t?.timestamp??this.timestamp,t?.duration??this.duration,t?.sequenceNumber??this.sequenceNumber,this.byteLength,t?.sideData??this.sideData)}};var la=n=>{let e=(n.hasVideo?"video/":n.hasAudio?"audio/":"application/")+(n.isQuickTime?"quicktime":"mp4");if(n.codecStrings.length>0){let r=[...new Set(n.codecStrings)];e+=`; codecs="${r.join(", ")}"`}return e},da=n=>{let t=V(n),e=0,r=t.getUint8(e);e+=1,e+=3;let i=tr(n.subarray(e,e+16));e+=16;let a=null;if(r>0){let o=t.getUint32(e);if(e+=4,o>0){a=[];for(let c=0;c<o;c++)a.push(tr(n.subarray(e,e+16))),e+=16}}let s=t.getUint32(e);return e+=4,{systemId:i,keyIds:a,data:n.slice(e,e+s)}},ma=(n,t)=>n.systemId===t.systemId&&Wn(n.data,t.data);var st=8,Nt=16,Lt=n=>{let t=F(n),e=te(n,4),r=8;t===1&&(t=Be(n),r=16);let a=t-r;return a<0?null:{name:e,totalSize:t,headerSize:r,contentSize:a}},dr=n=>Wt(n)/65536,fa=n=>Wt(n)/1073741824,pa=n=>{let t=0;for(let e=0;e<4;e++){t<<=7;let r=O(n);if(t|=r&127,(r&128)===0)break}return t},ot=n=>{let t=se(n);return n.skip(2),t=Math.min(t,n.remainingLength),me.decode(B(n,t))},il=n=>{let t=Lt(n);if(!t||t.name!=="data"||n.remainingLength<8)return null;let e=F(n);n.skip(4);let r=B(n,t.contentSize-8);switch(e){case 1:return me.decode(r);case 2:return new TextDecoder("utf-16be").decode(r);case 13:return new He(r,"image/jpeg");case 14:return new He(r,"image/png");case 27:return new He(r,"image/bmp");default:return r}};var ct=16,Ht=new Uint32Array(256),ci=new Uint32Array(256),ui=new Uint32Array(256),li=new Uint32Array(256),di=new Uint32Array(256),xe=new Uint32Array(256),nl=new Uint32Array(10),al=!1,Tm=()=>{let n=new Uint8Array(256),t=new Uint8Array(256),e=new Uint8Array(256);for(let a=0,s=1;a<256;a++)e[a]=s,t[s]=a,s=s^s<<1^(s&128?283:0);let r=(a,s)=>a&&s?e[(t[a]+t[s])%255]:0;n[0]=99;for(let a=1;a<256;a++){let s=e[255-t[a]],o=s^s<<1^s<<2^s<<3^s<<4;o=o>>>8^o&255^99,n[a]=o}for(let a=0;a<256;a++){let s=n[a],o=n.indexOf(a);Ht[a]=s<<24|s<<16|s<<8|s,xe[a]=o<<24|o<<16|o<<8|o;let c=r(o,14),u=r(o,9),l=r(o,13),m=r(o,11),d=c<<24|u<<16|l<<8|m;ci[a]=d,ui[a]=d>>>8|d<<24,li[a]=d>>>16|d<<16,di[a]=d>>>24|d<<8}let i=1;for(let a=0;a<10;a++)nl[a]=i<<24,i=i<<1^(i&128?283:0);al=!0},cn=class{constructor(){this.roundkey=new Uint32Array(44);this.iv=new Uint32Array(ct/Uint32Array.BYTES_PER_ELEMENT);this.in=new Uint8Array(ct);this.out=new Uint8Array(ct);this.inView=new DataView(this.in.buffer);this.outView=new DataView(this.out.buffer)}init({key:t,iv:e}){h(t.byteLength===16),h(e.byteLength===16),al||Tm();let r=new DataView(t.buffer,t.byteOffset,t.byteLength),i=new DataView(e.buffer,e.byteOffset,e.byteLength);this.roundkey[0]=r.getUint32(0,!1),this.roundkey[1]=r.getUint32(4,!1),this.roundkey[2]=r.getUint32(8,!1),this.roundkey[3]=r.getUint32(12,!1),this.iv[0]=i.getUint32(0,!1),this.iv[1]=i.getUint32(4,!1),this.iv[2]=i.getUint32(8,!1),this.iv[3]=i.getUint32(12,!1);for(let a=4;a<44;a+=4){let s=this.roundkey[a-1];this.roundkey[a]=this.roundkey[a-4]^Ht[s>>>16&255]&4278190080^Ht[s>>>8&255]&16711680^Ht[s>>>0&255]&65280^Ht[s>>>24&255]&255^nl[a/4-1],this.roundkey[a+1]=this.roundkey[a-3]^this.roundkey[a],this.roundkey[a+2]=this.roundkey[a-2]^this.roundkey[a+1],this.roundkey[a+3]=this.roundkey[a-1]^this.roundkey[a+2]}for(let a=0,s=40;a<s;a+=4,s-=4)for(let o=0;o<4;o++){let c=this.roundkey[a+o];this.roundkey[a+o]=this.roundkey[s+o],this.roundkey[s+o]=c}for(let a=4;a<40;a+=4)for(let s=0;s<4;s++){let o=this.roundkey[a+s];this.roundkey[a+s]=ci[Ht[o>>>24&255]&255]^ui[Ht[o>>>16&255]&255]^li[Ht[o>>>8&255]&255]^di[Ht[o>>>0&255]&255]}}decrypt(){let t=this.inView.getUint32(0,!1)^this.roundkey[0],e=this.inView.getUint32(4,!1)^this.roundkey[1],r=this.inView.getUint32(8,!1)^this.roundkey[2],i=this.inView.getUint32(12,!1)^this.roundkey[3],a=this.inView.getUint32(0,!1),s=this.inView.getUint32(4,!1),o=this.inView.getUint32(8,!1),c=this.inView.getUint32(12,!1),u,l,m,d;for(let y=1;y<10;y++){let k=y*4;u=ci[t>>>24]^ui[i>>>16&255]^li[r>>>8&255]^di[e&255]^this.roundkey[k],l=ci[e>>>24]^ui[t>>>16&255]^li[i>>>8&255]^di[r&255]^this.roundkey[k+1],m=ci[r>>>24]^ui[e>>>16&255]^li[t>>>8&255]^di[i&255]^this.roundkey[k+2],d=ci[i>>>24]^ui[r>>>16&255]^li[e>>>8&255]^di[t&255]^this.roundkey[k+3],t=u,e=l,r=m,i=d}let f=xe[t>>>24&255]&4278190080^xe[i>>>16&255]&16711680^xe[r>>>8&255]&65280^xe[e>>>0&255]&255^this.roundkey[40],p=xe[e>>>24&255]&4278190080^xe[t>>>16&255]&16711680^xe[i>>>8&255]&65280^xe[r>>>0&255]&255^this.roundkey[41],b=xe[r>>>24&255]&4278190080^xe[e>>>16&255]&16711680^xe[t>>>8&255]&65280^xe[i>>>0&255]&255^this.roundkey[42],g=xe[i>>>24&255]&4278190080^xe[r>>>16&255]&16711680^xe[e>>>8&255]&65280^xe[t>>>0&255]&255^this.roundkey[43];this.outView.setUint32(0,f^this.iv[0],!1),this.outView.setUint32(4,p^this.iv[1],!1),this.outView.setUint32(8,b^this.iv[2],!1),this.outView.setUint32(12,g^this.iv[3],!1),this.iv[0]=a,this.iv[1]=s,this.iv[2]=o,this.iv[3]=c}},sl=(n,t,e)=>{let r=!1,i=0,a=2**16,s=16,o=new cn;return new ReadableStream({pull:async c=>{r||(o.init(await t()),r=!0);let u=a+s,l=n.requestSliceRange(i,0,u);if(l instanceof Promise&&(l=await l),!l||l.length===0)throw new Error("Invalid ciphertext.");let m=l.length;if(m%16!==0)throw new Error("Invalid ciphertext.");let d=m===u?m-s:m,f=B(l,d),p=new Uint8Array(d);for(let b=0;b<d;b+=16)o.in.set(f.subarray(b,b+16)),o.decrypt(),p.set(o.out,b);if(d<m)c.enqueue(p),i+=d;else{let b=p[d-1];if(b===0||b>16)throw new Error("Invalid PKCS#7 padding. Incorrect key or corrupted data.");let g=p.subarray(0,d-b);c.enqueue(g),c.close(),e()}},cancel:()=>{e()}})};var ha=class n extends pe{constructor(e){super(e);this.moovSlice=null;this.currentTrack=null;this.tracks=[];this.metadataPromise=null;this.movieTimescale=-1;this.movieDurationInTimescale=-1;this.isQuickTime=!1;this.metadataTags={};this.currentMetadataKeys=null;this.isFragmented=!1;this.fragmentTrackDefaults=[];this.psshBoxes=[];this.currentFragment=null;this.lastReadFragment=null;this.decryptionKeyCache=new Map;this.reader=e._reader}async getTrackBackings(){return await this.readMetadata(),this.tracks.map(e=>e.trackBacking)}async getMimeType(){await this.readMetadata();let e=await this.getTrackBackings(),r=await Promise.all(e.map(i=>i.getDecoderConfig().then(a=>a?.codec??null)));return la({isQuickTime:this.isQuickTime,hasVideo:this.tracks.some(i=>i.info?.type==="video"),hasAudio:this.tracks.some(i=>i.info?.type==="audio"),codecStrings:r.filter(Boolean)})}async getMetadataTags(){return await this.readMetadata(),this.metadataTags}readMetadata(){return this.metadataPromise??=(async()=>{let e=0,r=!1;for(;;){let i=this.reader.requestSliceRange(e,st,Nt);if(i instanceof Promise&&(i=await i),!i)break;let a=e,s=Lt(i);if(!s)break;if(s.name==="ftyp"||s.name==="styp"){let o=te(i,4);this.isQuickTime=o==="qt "}else if(s.name==="moov"){let o=this.reader.requestSlice(i.filePos,s.contentSize);if(o instanceof Promise&&(o=await o),!o)break;this.moovSlice=o,this.readContiguousBoxes(this.moovSlice);for(let c of this.tracks){let u=c.editListPreviousSegmentDurations/this.movieTimescale;c.editListOffset-=Math.round(u*c.timescale)}r=this.isFragmented&&this.reader.fileSize!==null&&this.reader.fileSize>a+s.totalSize;break}else if(s.name==="moof"){if(!this.input._initInput)throw new Error('"moof" box encountered with no "moov" box present; this file is likely a Segment as described in ISO/IEC 14496-12 Section 8.16. A separate init file that contains a "moov" box is required to read this file, please provide it using InputOptions.initInput.');let o=await this.input._initInput._getDemuxer();if(o.constructor!==n)throw new Error("Init input must match the input's format.");await o.readMetadata(),this.movieTimescale=o.movieTimescale,this.movieDurationInTimescale=o.movieDurationInTimescale,this.metadataTags=o.metadataTags,this.isFragmented=!0,this.fragmentTrackDefaults=o.fragmentTrackDefaults,this.psshBoxes=o.psshBoxes;for(let c of o.tracks){let u={id:c.id,demuxer:this,trackBacking:null,disposition:c.disposition,timescale:c.timescale,durationInMediaTimescale:c.durationInMediaTimescale,durationInMovieTimescale:c.durationInMovieTimescale,rotation:c.rotation,internalCodecId:c.internalCodecId,name:c.name,languageCode:c.languageCode,sampleTableByteOffset:null,sampleTable:null,fragmentLookupTable:[],currentFragmentState:null,fragmentPositionCache:[],editListPreviousSegmentDurations:c.editListPreviousSegmentDurations,editListOffset:c.editListOffset,encryptionInfo:c.encryptionInfo,encryptionAuxInfo:null,frmaCodecString:null,info:c.info};if(c.trackBacking){if(h(u.info),u.info.type==="video"&&u.info.width!==-1){let l=u;u.trackBacking=new ba(l),this.tracks.push(u)}else if(u.info.type==="audio"&&u.info.numberOfChannels!==-1){let l=u;u.trackBacking=new ya(l),this.tracks.push(u)}}}r=!1;break}e=a+s.totalSize}if(r){h(this.reader.fileSize!==null);let i=this.reader.requestSlice(this.reader.fileSize-4,4);i instanceof Promise&&(i=await i),h(i);let a=F(i),s=this.reader.fileSize-a;if(s>=0&&s<=this.reader.fileSize-Nt){let o=this.reader.requestSliceRange(s,st,Nt);if(o instanceof Promise&&(o=await o),o){let c=Lt(o);if(c&&c.name==="mfra"){let u=this.reader.requestSlice(o.filePos,c.contentSize);u instanceof Promise&&(u=await u),u&&this.readContiguousBoxes(u)}}}}})()}getSampleTableForTrack(e){if(e.sampleTable)return e.sampleTable;let r={sampleTimingEntries:[],sampleCompositionTimeOffsets:[],sampleSizes:[],keySampleIndices:null,chunkOffsets:[],sampleToChunk:[],presentationTimestamps:null,presentationTimestampIndexMap:null};if(e.sampleTable=r,e.sampleTableByteOffset===null)return r;h(this.moovSlice);let i=this.moovSlice.slice(e.sampleTableByteOffset);if(this.currentTrack=e,this.traverseBox(i),this.currentTrack=null,e.info?.type==="audio"&&e.info.codec&&ee.includes(e.info.codec)&&r.sampleCompositionTimeOffsets.length===0){h(e.info?.type==="audio");let s=Se(e.info.codec),o=[],c=[];for(let u=0;u<r.sampleToChunk.length;u++){let l=r.sampleToChunk[u],m=r.sampleToChunk[u+1],d=(m?m.startChunkIndex:r.chunkOffsets.length)-l.startChunkIndex;for(let f=0;f<d;f++){let p=l.startSampleIndex+f*l.samplesPerChunk,b=p+l.samplesPerChunk,g=H(r.sampleTimingEntries,p,C=>C.startIndex),y=r.sampleTimingEntries[g],k=H(r.sampleTimingEntries,b,C=>C.startIndex),T=r.sampleTimingEntries[k],w=y.startDecodeTimestamp+(p-y.startIndex)*y.delta,x=T.startDecodeTimestamp+(b-T.startIndex)*T.delta-w,P=X(o);P&&P.delta===x?P.count++:o.push({startIndex:l.startChunkIndex+f,startDecodeTimestamp:w,count:1,delta:x});let A=l.samplesPerChunk*s.sampleSize*e.info.numberOfChannels;c.push(A)}l.startSampleIndex=l.startChunkIndex,l.samplesPerChunk=1}r.sampleTimingEntries=o,r.sampleSizes=c}if(r.sampleCompositionTimeOffsets.length>0){r.presentationTimestamps=[];for(let s of r.sampleTimingEntries)for(let o=0;o<s.count;o++)r.presentationTimestamps.push({presentationTimestamp:s.startDecodeTimestamp+o*s.delta,sampleIndex:s.startIndex+o});for(let s of r.sampleCompositionTimeOffsets)for(let o=0;o<s.count;o++){let c=s.startIndex+o,u=r.presentationTimestamps[c];u&&(u.presentationTimestamp+=s.offset)}r.presentationTimestamps.sort((s,o)=>s.presentationTimestamp-o.presentationTimestamp),r.presentationTimestampIndexMap=Array(r.presentationTimestamps.length).fill(-1);for(let s=0;s<r.presentationTimestamps.length;s++)r.presentationTimestampIndexMap[r.presentationTimestamps[s].sampleIndex]=s}return r}async readFragment(e){if(this.lastReadFragment?.moofOffset===e)return this.lastReadFragment;let r=this.reader.requestSliceRange(e,st,Nt);r instanceof Promise&&(r=await r),h(r);let i=Lt(r);h(i?.name==="moof");let a=this.reader.requestSlice(e,i.totalSize);a instanceof Promise&&(a=await a),h(a),this.traverseBox(a);let s=this.lastReadFragment;h(s&&s.moofOffset===e);for(let[,o]of s.trackData){let c=o.track,{fragmentPositionCache:u}=c;if(!o.startTimestampIsFinal){let m=c.fragmentLookupTable.find(d=>d.moofOffset===s.moofOffset);if(m)wo(o,m.timestamp);else{let d=H(u,s.moofOffset-1,f=>f.moofOffset);if(d!==-1){let f=u[d];wo(o,f.endTimestamp)}}o.startTimestampIsFinal=!0}let l=H(u,o.startTimestamp,m=>m.startTimestamp);if((l===-1||u[l].moofOffset!==s.moofOffset)&&u.splice(l+1,0,{moofOffset:s.moofOffset,startTimestamp:o.startTimestamp,endTimestamp:o.endTimestamp}),o.encryptionAuxInfo&&c.encryptionInfo){let m=await dl(this.reader,c.encryptionInfo,o.encryptionAuxInfo);for(let d=0;d<Math.min(o.samples.length,m.length);d++){let f=m[d];o.samples[d].encryption=f}}}return s}readContiguousBoxes(e){let r=e.filePos;for(;e.filePos-r<=e.length-st&&this.traverseBox(e););}*iterateContiguousBoxes(e){let r=e.filePos;for(;e.filePos-r<=e.length-st;){let i=e.filePos,a=Lt(e);if(!a)break;yield{boxInfo:a,slice:e},e.filePos=i+a.totalSize}}traverseBox(e){let r=e.filePos,i=Lt(e);if(!i)return!1;let a=e.filePos,s=r+i.totalSize;switch(i.name){case"mdia":case"minf":case"dinf":case"mfra":case"edts":case"sinf":case"schi":this.readContiguousBoxes(e.slice(a,i.contentSize));break;case"mvhd":{let o=O(e);e.skip(3),o===1?(e.skip(16),this.movieTimescale=F(e),this.movieDurationInTimescale=Be(e)):(e.skip(8),this.movieTimescale=F(e),this.movieDurationInTimescale=F(e))}break;case"trak":{let o={id:-1,demuxer:this,trackBacking:null,disposition:{...we,primary:!1},info:null,timescale:-1,durationInMovieTimescale:-1,durationInMediaTimescale:-1,rotation:0,internalCodecId:null,name:null,languageCode:J,sampleTableByteOffset:-1,sampleTable:null,fragmentLookupTable:[],currentFragmentState:null,fragmentPositionCache:[],editListPreviousSegmentDurations:0,editListOffset:0,encryptionInfo:null,encryptionAuxInfo:null,frmaCodecString:null};if(this.currentTrack=o,this.readContiguousBoxes(e.slice(a,i.contentSize)),o.id!==-1&&o.timescale!==-1&&o.info!==null){if(o.info.type==="video"&&o.info.width!==-1){let c=o;o.trackBacking=new ba(c),this.tracks.push(o)}else if(o.info.type==="audio"&&o.info.numberOfChannels!==-1){let c=o;o.trackBacking=new ya(c),this.tracks.push(o)}}this.currentTrack=null}break;case"tkhd":{let o=this.currentTrack;if(!o)break;let c=O(e),l=!!(ut(e)&1);if(o.disposition.default=l,c===0)e.skip(8),o.id=F(e),e.skip(4),o.durationInMovieTimescale=F(e);else if(c===1)e.skip(16),o.id=F(e),e.skip(4),o.durationInMovieTimescale=Be(e);else throw new Error(`Incorrect track header version ${c}.`);e.skip(2*4+2+2+2+2);let m=[dr(e),dr(e),fa(e),dr(e),dr(e),fa(e),dr(e),dr(e),fa(e)],d=Ze(ji(xm(m),90));h(d===0||d===90||d===180||d===270),o.rotation=d}break;case"elst":{let o=this.currentTrack;if(!o)break;let c=O(e);e.skip(3);let u=!1,l=0,m=F(e);for(let d=0;d<m;d++){let f=c===1?Be(e):F(e),p=c===1?fl(e):Wt(e),b=dr(e);if(f!==0){if(u){console.warn("Unsupported edit list: multiple edits are not currently supported. Only using first edit.");break}if(p===-1){l+=f;continue}if(b!==1){console.warn("Unsupported edit list entry: media rate must be 1.");break}o.editListPreviousSegmentDurations=l,o.editListOffset=p,u=!0}}}break;case"mdhd":{let o=this.currentTrack;if(!o)break;let c=O(e);e.skip(3),c===0?(e.skip(8),o.timescale=F(e),o.durationInMediaTimescale=F(e)):c===1&&(e.skip(16),o.timescale=F(e),o.durationInMediaTimescale=Be(e));let u=se(e);if(u>0){o.languageCode="";for(let l=0;l<3;l++)o.languageCode=String.fromCharCode(96+(u&31))+o.languageCode,u>>=5;Bt(o.languageCode)||(o.languageCode=J)}}break;case"hdlr":{let o=this.currentTrack;if(!o)break;e.skip(8);let c=te(e,4);c==="vide"?o.info={type:"video",width:-1,height:-1,squarePixelWidth:-1,squarePixelHeight:-1,codec:null,codecDescription:null,colorSpace:null,avcType:null,avcCodecInfo:null,hevcCodecInfo:null,vp9CodecInfo:null,av1CodecInfo:null}:c==="soun"&&(o.info={type:"audio",numberOfChannels:-1,sampleRate:-1,codec:null,codecDescription:null,aacCodecInfo:null,pcmLittleEndian:!1,pcmSampleSize:null})}break;case"stbl":{let o=this.currentTrack;if(!o)break;o.sampleTableByteOffset=r,this.readContiguousBoxes(e.slice(a,i.contentSize))}break;case"stsd":{let o=this.currentTrack;if(!o||o.info===null||o.sampleTable)break;let c=O(e);e.skip(3);let u=F(e);for(let l=0;l<u;l++){let m=e.filePos,d=Lt(e);if(!d)break;o.internalCodecId=d.name;let f=d.name.toLowerCase();if(o.info.type==="video"){e.skip(6*1+2+2+2+3*4),o.info.width=se(e),o.info.height=se(e),o.info.squarePixelWidth=o.info.width,o.info.squarePixelHeight=o.info.height,e.skip(50),o.frmaCodecString=null,this.readContiguousBoxes(e.slice(e.filePos,m+d.totalSize-e.filePos));let p=f==="encv"?o.frmaCodecString:f;o.frmaCodecString=null,p==="avc1"||p==="avc3"?(o.info.codec="avc",o.info.avcType=p==="avc1"?1:3):p==="hvc1"||p==="hev1"?o.info.codec="hevc":p==="vp08"?o.info.codec="vp8":p==="vp09"?o.info.codec="vp9":p==="av01"?o.info.codec="av1":console.warn(p===null?"Unknown encrypted video codec due to missing frma box.":`Unsupported video codec (sample entry type '${d.name}').`)}else{e.skip(6*1+2);let p=se(e);e.skip(3*2);let b=se(e),g=se(e);e.skip(2*2);let y=F(e)/65536,k=null;c===0&&p>0&&(p===1?(e.skip(4),g=8*F(e),e.skip(2*4)):p===2&&(e.skip(4),y=ka(e),b=F(e),e.skip(4),g=F(e),k=F(e),e.skip(2*4))),o.info.numberOfChannels=b,o.info.sampleRate=y,o.frmaCodecString=null,this.readContiguousBoxes(e.slice(e.filePos,m+d.totalSize-e.filePos));let T=f==="enca"?o.frmaCodecString:f;if(o.frmaCodecString=null,T!=="mp4a")if(T==="opus")o.info.codec="opus",o.info.sampleRate=nt;else if(T==="flac")o.info.codec="flac";else if(T==="ulaw")o.info.codec="ulaw";else if(T==="alaw")o.info.codec="alaw";else if(T==="ac-3")o.info.codec="ac3";else if(T==="ec-3")o.info.codec="eac3";else if(T==="twos")g===8?o.info.codec="pcm-s8":g===16?o.info.codec=o.info.pcmLittleEndian?"pcm-s16":"pcm-s16be":(console.warn(`Unsupported sample size ${g} for codec 'twos'.`),o.info.codec=null);else if(T==="sowt")g===8?o.info.codec="pcm-s8":g===16?o.info.codec="pcm-s16":(console.warn(`Unsupported sample size ${g} for codec 'sowt'.`),o.info.codec=null);else if(T==="raw ")o.info.codec="pcm-u8";else if(T==="in24")o.info.codec=o.info.pcmLittleEndian?"pcm-s24":"pcm-s24be";else if(T==="in32")o.info.codec=o.info.pcmLittleEndian?"pcm-s32":"pcm-s32be";else if(T==="fl32")o.info.codec=o.info.pcmLittleEndian?"pcm-f32":"pcm-f32be";else if(T==="fl64")o.info.codec=o.info.pcmLittleEndian?"pcm-f64":"pcm-f64be";else if(T==="ipcm"){let w=o.info.pcmSampleSize;o.info.pcmLittleEndian?w===16?o.info.codec="pcm-s16":w===24?o.info.codec="pcm-s24":w===32?o.info.codec="pcm-s32":(console.warn(`Invalid ipcm sample size ${w}.`),o.info.codec=null):w===16?o.info.codec="pcm-s16be":w===24?o.info.codec="pcm-s24be":w===32?o.info.codec="pcm-s32be":(console.warn(`Invalid ipcm sample size ${w}.`),o.info.codec=null)}else if(T==="fpcm"){let w=o.info.pcmSampleSize;o.info.pcmLittleEndian?w===32?o.info.codec="pcm-f32":w===64?o.info.codec="pcm-f64":(console.warn(`Invalid fpcm sample size ${w}.`),o.info.codec=null):w===32?o.info.codec="pcm-f32be":w===64?o.info.codec="pcm-f64be":(console.warn(`Invalid fpcm sample size ${w}.`),o.info.codec=null)}else if(T==="lpcm"&&k!==null){let w=g+7>>3,S=!!(k&1),x=!!(k&2),P=k&4?-1:0;g>0&&g<=64&&(S?g===32&&(o.info.codec=x?"pcm-f32be":"pcm-f32"):P&1<<w-1?w===1?o.info.codec="pcm-s8":w===2?o.info.codec=x?"pcm-s16be":"pcm-s16":w===3?o.info.codec=x?"pcm-s24be":"pcm-s24":w===4&&(o.info.codec=x?"pcm-s32be":"pcm-s32"):w===1&&(o.info.codec="pcm-u8")),o.info.codec===null&&console.warn("Unsupported PCM format.")}else console.warn(T===null?"Unknown encrypted audio codec due to missing frma box.":`Unsupported audio codec (sample entry type '${d.name}').`)}e.filePos=m+d.totalSize}}break;case"frma":{let o=this.currentTrack;if(!o)break;let u=te(e,4).toLowerCase();o.frmaCodecString=u}break;case"schm":{let o=this.currentTrack;if(!o)break;e.skip(4);let c=te(e,4);c==="cenc"||c==="cens"||c==="cbcs"?o.encryptionInfo={scheme:c,defaultKid:null,defaultIsProtected:null,defaultPerSampleIvSize:null,defaultConstantIv:null,defaultCryptByteBlock:null,defaultSkipByteBlock:null}:console.warn(`Unsupported encryption scheme '${c}'.`)}break;case"tenc":{let o=this.currentTrack;if(!o||!o.encryptionInfo)break;let c=O(e);e.skip(3),e.skip(1);let u=O(e);if(c>0?(o.encryptionInfo.defaultCryptByteBlock=u>>4,o.encryptionInfo.defaultSkipByteBlock=u&15):(o.encryptionInfo.defaultCryptByteBlock=0,o.encryptionInfo.defaultSkipByteBlock=0),o.encryptionInfo.defaultIsProtected=O(e)!==0,o.encryptionInfo.defaultPerSampleIvSize=O(e),o.encryptionInfo.defaultKid=tr(B(e,16)),o.encryptionInfo.defaultIsProtected&&o.encryptionInfo.defaultPerSampleIvSize===0){let l=O(e),m=new Uint8Array(16);m.set(B(e,l),0),o.encryptionInfo.defaultConstantIv=m}}break;case"avcC":{let o=this.currentTrack;if(!o)break;h(o.info),o.info.codecDescription=B(e,i.contentSize)}break;case"hvcC":{let o=this.currentTrack;if(!o)break;h(o.info),o.info.codecDescription=B(e,i.contentSize)}break;case"vpcC":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="video"),e.skip(4);let c=O(e),u=O(e),l=O(e),m=l>>4,d=l>>1&7,f=l&1,p=O(e),b=O(e),g=O(e);o.info.vp9CodecInfo={profile:c,level:u,bitDepth:m,chromaSubsampling:d,videoFullRangeFlag:f,colourPrimaries:p,transferCharacteristics:b,matrixCoefficients:g}}break;case"av1C":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="video"),e.skip(1);let c=O(e),u=c>>5,l=c&31,m=O(e),d=m>>7,f=m>>6&1,p=m>>5&1,b=m>>4&1,g=m>>3&1,y=m>>2&1,k=m&3,T=u===2&&f?p?12:10:f?10:8;o.info.av1CodecInfo={profile:u,level:l,tier:d,bitDepth:T,monochrome:b,chromaSubsamplingX:g,chromaSubsamplingY:y,chromaSamplePosition:k}}break;case"colr":{let o=this.currentTrack;if(!o||(h(o.info?.type==="video"),te(e,4)!=="nclx"))break;let u=se(e),l=se(e),m=se(e),d=!!(O(e)&128);o.info.colorSpace={primaries:Ar[u],transfer:xr[l],matrix:Pr[m],fullRange:d}}break;case"pasp":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="video");let c=F(e),u=F(e);c>0&&u>0&&(c>u?o.info.squarePixelWidth=Math.round(o.info.width*c/u):o.info.squarePixelHeight=Math.round(o.info.height*u/c))}break;case"wave":this.readContiguousBoxes(e.slice(a,i.contentSize));break;case"esds":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio"),e.skip(4);let c=O(e);h(c===3),pa(e),e.skip(2);let u=O(e),l=(u&128)!==0,m=(u&64)!==0,d=(u&32)!==0;if(l&&e.skip(2),m){let y=O(e);e.skip(y)}d&&e.skip(2);let f=O(e);h(f===4);let p=pa(e),b=e.filePos,g=O(e);if(g===64||g===103?(o.info.codec="aac",o.info.aacCodecInfo={isMpeg2:g===103,objectType:null}):g===105||g===107?o.info.codec="mp3":g===221?o.info.codec="vorbis":console.warn(`Unsupported audio codec (objectTypeIndication ${g}) - discarding track.`),e.skip(12),p>e.filePos-b){let y=O(e);h(y===5);let k=pa(e);if(o.info.codecDescription=B(e,k),o.info.codec==="aac"){let T=Tt(o.info.codecDescription);T.numberOfChannels!==null&&(o.info.numberOfChannels=T.numberOfChannels),T.sampleRate!==null&&(o.info.sampleRate=T.sampleRate)}}}break;case"enda":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio"),o.info.pcmLittleEndian=!!(se(e)&255)}break;case"pcmC":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio"),e.skip(4);let c=O(e);o.info.pcmLittleEndian=!!(c&1),o.info.pcmSampleSize=O(e)}break;case"dOps":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio"),e.skip(1);let c=O(e),u=se(e),l=F(e),m=un(e),d=O(e),f;d!==0?f=B(e,2+c):f=new Uint8Array(0);let p=new Uint8Array(19+f.byteLength),b=new DataView(p.buffer);b.setUint32(0,1332770163,!1),b.setUint32(4,1214603620,!1),b.setUint8(8,1),b.setUint8(9,c),b.setUint16(10,u,!0),b.setUint32(12,l,!0),b.setInt16(16,m,!0),b.setUint8(18,d),p.set(f,19),o.info.codecDescription=p,o.info.numberOfChannels=c}break;case"dfLa":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio"),e.skip(4);let c=127,u=128,l=e.filePos;for(;e.filePos<s;){let b=O(e),g=ut(e);if((b&c)===0){e.skip(10);let k=F(e),T=k>>>12,w=(k>>9&7)+1;o.info.sampleRate=T,o.info.numberOfChannels=w,e.skip(20)}else e.skip(g);if(b&u)break}let m=e.filePos;e.filePos=l;let d=B(e,m-l),f=new Uint8Array(4+d.byteLength);new DataView(f.buffer).setUint32(0,1716281667,!1),f.set(d,4),o.info.codecDescription=f}break;case"dac3":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio");let c=B(e,3),u=new W(c),l=u.readBits(2);u.skipBits(8);let m=u.readBits(3),d=u.readBits(1);l<3&&(o.info.sampleRate=Br[l]),o.info.numberOfChannels=an[m]+d}break;case"dec3":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio");let c=B(e,i.contentSize),u=rl(c);if(!u){console.warn("Invalid dec3 box contents, ignoring.");break}let l=ca(u);l!==null&&(o.info.sampleRate=l),o.info.numberOfChannels=ua(u)}break;case"stts":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e),u=0,l=0;for(let m=0;m<c;m++){let d=F(e),f=F(e);o.sampleTable.sampleTimingEntries.push({startIndex:u,startDecodeTimestamp:l,count:d,delta:f}),u+=d,l+=d*f}}break;case"ctts":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e),u=0;for(let l=0;l<c;l++){let m=F(e),d=Wt(e);o.sampleTable.sampleCompositionTimeOffsets.push({startIndex:u,count:m,offset:d}),u+=m}}break;case"stsz":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e),u=F(e);if(c===0)for(let l=0;l<u;l++){let m=F(e);o.sampleTable.sampleSizes.push(m)}else o.sampleTable.sampleSizes.push(c)}break;case"stz2":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4),e.skip(3);let c=O(e),u=F(e),l=B(e,Math.ceil(u*c/8)),m=new W(l);for(let d=0;d<u;d++){let f=m.readBits(c);o.sampleTable.sampleSizes.push(f)}}break;case"stss":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4),o.sampleTable.keySampleIndices=[];let c=F(e);for(let u=0;u<c;u++){let l=F(e)-1;o.sampleTable.keySampleIndices.push(l)}o.sampleTable.keySampleIndices[0]!==0&&o.sampleTable.keySampleIndices.unshift(0)}break;case"stsc":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e);for(let l=0;l<c;l++){let m=F(e)-1,d=F(e),f=F(e);o.sampleTable.sampleToChunk.push({startSampleIndex:-1,startChunkIndex:m,samplesPerChunk:d,sampleDescriptionIndex:f})}let u=0;for(let l=0;l<o.sampleTable.sampleToChunk.length;l++)if(o.sampleTable.sampleToChunk[l].startSampleIndex=u,l<o.sampleTable.sampleToChunk.length-1){let d=o.sampleTable.sampleToChunk[l+1].startChunkIndex-o.sampleTable.sampleToChunk[l].startChunkIndex;u+=d*o.sampleTable.sampleToChunk[l].samplesPerChunk}}break;case"stco":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e);for(let u=0;u<c;u++){let l=F(e);o.sampleTable.chunkOffsets.push(l)}}break;case"co64":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e);for(let u=0;u<c;u++){let l=Be(e);o.sampleTable.chunkOffsets.push(l)}}break;case"mvex":this.isFragmented=!0,this.readContiguousBoxes(e.slice(a,i.contentSize));break;case"mehd":{let o=O(e);e.skip(3);let c=o===1?Be(e):F(e);this.movieDurationInTimescale=c}break;case"trex":{e.skip(4);let o=F(e),c=F(e),u=F(e),l=F(e),m=F(e);this.fragmentTrackDefaults.push({trackId:o,defaultSampleDescriptionIndex:c,defaultSampleDuration:u,defaultSampleSize:l,defaultSampleFlags:m})}break;case"tfra":{let o=O(e);e.skip(3);let c=F(e),u=this.tracks.find(T=>T.id===c);if(!u)break;let l=F(e),m=(l&48)>>4,d=(l&12)>>2,f=l&3,p=[O,se,ut,F],b=p[m],g=p[d],y=p[f],k=F(e);for(let T=0;T<k;T++){let w=o===1?Be(e):F(e),S=o===1?Be(e):F(e);b(e),g(e),y(e),u.fragmentLookupTable.push({timestamp:w,moofOffset:S})}u.fragmentLookupTable.sort((T,w)=>T.timestamp-w.timestamp);for(let T=0;T<u.fragmentLookupTable.length-1;T++){let w=u.fragmentLookupTable[T],S=u.fragmentLookupTable[T+1];w.timestamp===S.timestamp&&(u.fragmentLookupTable.splice(T+1,1),T--)}}break;case"moof":this.currentFragment={moofOffset:r,moofSize:i.totalSize,implicitBaseDataOffset:r,trackData:new Map,psshBoxes:[]},this.readContiguousBoxes(e.slice(a,i.contentSize)),this.lastReadFragment=this.currentFragment,this.currentFragment=null;break;case"traf":if(h(this.currentFragment),this.readContiguousBoxes(e.slice(a,i.contentSize)),this.currentTrack){let o=this.currentFragment.trackData.get(this.currentTrack.id);if(o){this.currentFragment.implicitBaseDataOffset=o.currentOffset,o.presentationTimestamps=o.samples.map((m,d)=>({presentationTimestamp:m.presentationTimestamp,sampleIndex:d})).sort((m,d)=>m.presentationTimestamp-d.presentationTimestamp);for(let m=0;m<o.presentationTimestamps.length;m++){let d=o.presentationTimestamps[m],f=o.samples[d.sampleIndex];if(o.firstKeyFrameTimestamp===null&&f.isKeyFrame&&(o.firstKeyFrameTimestamp=f.presentationTimestamp),m<o.presentationTimestamps.length-1){let b=o.presentationTimestamps[m+1].presentationTimestamp-d.presentationTimestamp;f.duration=b}}let c=o.samples[o.presentationTimestamps[0].sampleIndex],u=o.samples[X(o.presentationTimestamps).sampleIndex];o.startTimestamp=c.presentationTimestamp,o.endTimestamp=u.presentationTimestamp+u.duration;let{currentFragmentState:l}=this.currentTrack;h(l),l.startTimestamp!==null&&(wo(o,l.startTimestamp),o.startTimestampIsFinal=!0),l.encryptionAuxInfo&&!o.samples[0].encryption&&(o.encryptionAuxInfo=l.encryptionAuxInfo)}this.currentTrack.currentFragmentState=null,this.currentTrack=null}break;case"pssh":{if(this.input._formatOptions.isobmff?._suppressPsshParsing)break;let o=da(B(e,i.contentSize));this.currentFragment?this.currentFragment.psshBoxes.push(o):this.currentTrack||this.psshBoxes.push(o)}break;case"tfhd":{h(this.currentFragment),e.skip(1);let o=ut(e),c=!!(o&1),u=!!(o&2),l=!!(o&8),m=!!(o&16),d=!!(o&32),f=!!(o&65536),p=!!(o&131072),b=F(e),g=this.tracks.find(k=>k.id===b);if(!g)break;let y=this.fragmentTrackDefaults.find(k=>k.trackId===b);this.currentTrack=g,g.currentFragmentState={baseDataOffset:this.currentFragment.implicitBaseDataOffset,sampleDescriptionIndex:y?.defaultSampleDescriptionIndex??null,defaultSampleDuration:y?.defaultSampleDuration??null,defaultSampleSize:y?.defaultSampleSize??null,defaultSampleFlags:y?.defaultSampleFlags??null,startTimestamp:null,encryptionAuxInfo:null},c?g.currentFragmentState.baseDataOffset=Be(e):p&&(g.currentFragmentState.baseDataOffset=this.currentFragment.moofOffset),u&&(g.currentFragmentState.sampleDescriptionIndex=F(e)),l&&(g.currentFragmentState.defaultSampleDuration=F(e)),m&&(g.currentFragmentState.defaultSampleSize=F(e)),d&&(g.currentFragmentState.defaultSampleFlags=F(e)),f&&(g.currentFragmentState.defaultSampleDuration=0)}break;case"tfdt":{let o=this.currentTrack;if(!o)break;h(o.currentFragmentState);let c=O(e);e.skip(3);let u=c===0?F(e):Be(e);o.currentFragmentState.startTimestamp=u}break;case"trun":{let o=this.currentTrack;if(!o)break;h(this.currentFragment),h(o.currentFragmentState);let c=O(e),u=ut(e),l=!!(u&1),m=!!(u&4),d=!!(u&256),f=!!(u&512),p=!!(u&1024),b=!!(u&2048),g=F(e),y=null;l&&(y=Wt(e));let k=null;m&&(k=F(e));let T;if(this.currentFragment.trackData.has(o.id)?(T=this.currentFragment.trackData.get(o.id),y!==null&&(T.currentOffset=o.currentFragmentState.baseDataOffset+y)):(T={track:o,currentTimestamp:0,currentOffset:o.currentFragmentState.baseDataOffset+(y??0),startTimestamp:0,endTimestamp:0,firstKeyFrameTimestamp:null,samples:[],presentationTimestamps:[],startTimestampIsFinal:!1,encryptionAuxInfo:null},this.currentFragment.trackData.set(o.id,T)),g===0){this.currentFragment.implicitBaseDataOffset=T.currentOffset;break}for(let w=0;w<g;w++){let S;d?S=F(e):(h(o.currentFragmentState.defaultSampleDuration!==null),S=o.currentFragmentState.defaultSampleDuration);let x;f?x=F(e):(h(o.currentFragmentState.defaultSampleSize!==null),x=o.currentFragmentState.defaultSampleSize);let P;p?P=F(e):(h(o.currentFragmentState.defaultSampleFlags!==null),P=o.currentFragmentState.defaultSampleFlags),w===0&&k!==null&&(P=k);let A=0;b&&(c===0?A=F(e):A=Wt(e));let C=!(P&65536);T.samples.push({presentationTimestamp:T.currentTimestamp+A,duration:S,byteOffset:T.currentOffset,byteSize:x,isKeyFrame:C,encryption:null}),T.currentOffset+=x,T.currentTimestamp+=S}}break;case"saiz":{let o=this.currentTrack;if(!o||!o.encryptionInfo)break;if(e.skip(1),ut(e)&1){let f=te(e,4),p=F(e);if(f!==o.encryptionInfo.scheme||p!==0)break}let u=O(e),l=F(e),m=null;u===0&&l>0&&(m=B(e,l));let d=cl(o);d.defaultSampleInfoSize=u,d.sampleSizes=m,d.sampleCount=l}break;case"saio":{let o=this.currentTrack;if(!o||!o.encryptionInfo)break;let c=O(e);if(ut(e)&1){let f=te(e,4),p=F(e);if(f!==o.encryptionInfo.scheme||p!==0)break}let l=F(e);if(l===0)break;l>1&&console.warn("Multiple saio entries are not supported; using the first offset only.");let m=c===0?F(e):Number(Be(e));this.currentFragment&&(m+=this.currentFragment.moofOffset);let d=cl(o);d.offset=m}break;case"senc":{let o=this.currentTrack;if(!o||!o.encryptionInfo)break;h(this.currentFragment);let c=this.currentFragment.trackData.get(o.id);if(!c)break;e.skip(1);let l=!!(ut(e)&2),m=F(e),d=o.encryptionInfo.defaultPerSampleIvSize;h(d!==null);for(let f=0;f<Math.min(m,c.samples.length);f++){let p=new Uint8Array(16);d>0?p.set(B(e,d),0):p.set(o.encryptionInfo.defaultConstantIv,0);let b=null;if(l){let y=se(e);b=[];for(let k=0;k<y;k++){let T=se(e),w=F(e);b.push({clearLen:T,protectedLen:w})}}let g=c.samples[f];g.encryption={iv:p,subsamples:b}}}break;case"udta":{let o=this.iterateContiguousBoxes(e.slice(a,i.contentSize));for(let{boxInfo:c,slice:u}of o){if(c.name!=="meta"&&!this.currentTrack){let l=u.filePos;this.metadataTags.raw??={},c.name[0]==="\xA9"?this.metadataTags.raw[c.name]??=ot(u):this.metadataTags.raw[c.name]??=B(u,c.contentSize),u.filePos=l}switch(c.name){case"meta":u.skip(-c.headerSize),this.traverseBox(u);break;case"\xA9nam":case"name":this.currentTrack?this.currentTrack.name=me.decode(B(u,c.contentSize)):this.metadataTags.title??=ot(u);break;case"\xA9des":this.currentTrack||(this.metadataTags.description??=ot(u));break;case"\xA9ART":this.currentTrack||(this.metadataTags.artist??=ot(u));break;case"\xA9alb":this.currentTrack||(this.metadataTags.album??=ot(u));break;case"albr":this.currentTrack||(this.metadataTags.albumArtist??=ot(u));break;case"\xA9gen":this.currentTrack||(this.metadataTags.genre??=ot(u));break;case"\xA9day":if(!this.currentTrack){let l=new Date(ot(u));Number.isNaN(l.getTime())||(this.metadataTags.date??=l)}break;case"\xA9cmt":this.currentTrack||(this.metadataTags.comment??=ot(u));break;case"\xA9lyr":this.currentTrack||(this.metadataTags.lyrics??=ot(u));break}}}break;case"meta":{if(this.currentTrack)break;let c=F(e)!==0;this.currentMetadataKeys=new Map,c?this.readContiguousBoxes(e.slice(a,i.contentSize)):this.readContiguousBoxes(e.slice(a+4,i.contentSize-4)),this.currentMetadataKeys=null}break;case"keys":{if(!this.currentMetadataKeys)break;e.skip(4);let o=F(e);for(let c=0;c<o;c++){let u=F(e);e.skip(4);let l=me.decode(B(e,u-8));this.currentMetadataKeys.set(c+1,l)}}break;case"ilst":{if(!this.currentMetadataKeys)break;let o=this.iterateContiguousBoxes(e.slice(a,i.contentSize));for(let{boxInfo:c,slice:u}of o){let l=c.name,m=(l.charCodeAt(0)<<24)+(l.charCodeAt(1)<<16)+(l.charCodeAt(2)<<8)+l.charCodeAt(3);this.currentMetadataKeys.has(m)&&(l=this.currentMetadataKeys.get(m));let d=il(u);switch(this.metadataTags.raw??={},this.metadataTags.raw[l]??=d,l){case"\xA9nam":case"titl":case"com.apple.quicktime.title":case"title":typeof d=="string"&&(this.metadataTags.title??=d);break;case"\xA9des":case"desc":case"dscp":case"com.apple.quicktime.description":case"description":typeof d=="string"&&(this.metadataTags.description??=d);break;case"\xA9ART":case"com.apple.quicktime.artist":case"artist":typeof d=="string"&&(this.metadataTags.artist??=d);break;case"\xA9alb":case"albm":case"com.apple.quicktime.album":case"album":typeof d=="string"&&(this.metadataTags.album??=d);break;case"aART":case"album_artist":typeof d=="string"&&(this.metadataTags.albumArtist??=d);break;case"\xA9cmt":case"com.apple.quicktime.comment":case"comment":typeof d=="string"&&(this.metadataTags.comment??=d);break;case"\xA9gen":case"gnre":case"com.apple.quicktime.genre":case"genre":typeof d=="string"&&(this.metadataTags.genre??=d);break;case"\xA9lyr":case"lyrics":typeof d=="string"&&(this.metadataTags.lyrics??=d);break;case"\xA9day":case"rldt":case"com.apple.quicktime.creationdate":case"date":if(typeof d=="string"){let f=new Date(d);Number.isNaN(f.getTime())||(this.metadataTags.date??=f)}break;case"covr":case"com.apple.quicktime.artwork":d instanceof He?(this.metadataTags.images??=[],this.metadataTags.images.push({data:d.data,kind:"coverFront",mimeType:d.mimeType})):d instanceof Uint8Array&&(this.metadataTags.images??=[],this.metadataTags.images.push({data:d,kind:"coverFront",mimeType:"image/*"}));break;case"track":if(typeof d=="string"){let f=d.split("/"),p=Number.parseInt(f[0],10),b=f[1]&&Number.parseInt(f[1],10);Number.isInteger(p)&&p>0&&(this.metadataTags.trackNumber??=p),b&&Number.isInteger(b)&&b>0&&(this.metadataTags.tracksTotal??=b)}break;case"trkn":if(d instanceof Uint8Array&&d.length>=6){let f=V(d),p=f.getUint16(2,!1),b=f.getUint16(4,!1);p>0&&(this.metadataTags.trackNumber??=p),b>0&&(this.metadataTags.tracksTotal??=b)}break;case"disc":case"disk":if(d instanceof Uint8Array&&d.length>=6){let f=V(d),p=f.getUint16(2,!1),b=f.getUint16(4,!1);p>0&&(this.metadataTags.discNumber??=p),b>0&&(this.metadataTags.discsTotal??=b)}break}}}break}return e.filePos=s,!0}},ga=class{constructor(t){this.internalTrack=t;this.packetToSampleIndex=new WeakMap;this.packetToFragmentLocation=new WeakMap}getId(){return this.internalTrack.id}getNumber(){let t=this.internalTrack.demuxer,e=this.internalTrack.trackBacking.getType(),r=0;for(let i of t.tracks)if(i.trackBacking.getType()===e&&r++,i===this.internalTrack)break;return r}getCodec(){throw new Error("Not implemented on base class.")}getInternalCodecId(){return this.internalTrack.internalCodecId}getName(){return this.internalTrack.name}getLanguageCode(){return this.internalTrack.languageCode}getTimeResolution(){return this.internalTrack.timescale}isRelativeToUnixEpoch(){return!1}getDisposition(){return this.internalTrack.disposition}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){let t=this.internalTrack;return t.durationInMediaTimescale<=0?null:(h(t.trackBacking),((await t.trackBacking.getFirstPacket({metadataOnly:!0}))?.timestamp??0)+t.durationInMediaTimescale/t.timescale)}async getLiveRefreshInterval(){return null}async getFirstPacket(t){let e=await this.fetchPacketForSampleIndex(0,t);return e||!this.internalTrack.demuxer.isFragmented?e:this.performFragmentedLookup(null,r=>r.trackData.get(this.internalTrack.id)?{sampleIndex:0,correctSampleFound:!0}:{sampleIndex:-1,correctSampleFound:!1},-1/0,1/0,t)}mapTimestampIntoTimescale(t){return Mt(t*this.internalTrack.timescale)+this.internalTrack.editListOffset}async getPacket(t,e){let r=this.mapTimestampIntoTimescale(t),i=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),a=So(i,r),s=await this.fetchPacketForSampleIndex(a,e);return!ol(i)||!this.internalTrack.demuxer.isFragmented?s:this.performFragmentedLookup(null,o=>{let c=o.trackData.get(this.internalTrack.id);if(!c)return{sampleIndex:-1,correctSampleFound:!1};let u=H(c.presentationTimestamps,r,d=>d.presentationTimestamp),l=u!==-1?c.presentationTimestamps[u].sampleIndex:-1,m=u!==-1&&r<c.endTimestamp;return{sampleIndex:l,correctSampleFound:m}},r,r,e)}async getNextPacket(t,e){let r=this.packetToSampleIndex.get(t);if(r!==void 0)return this.fetchPacketForSampleIndex(r+1,e);let i=this.packetToFragmentLocation.get(t);if(i===void 0)throw new Error("Packet was not created from this track.");return this.performFragmentedLookup(i.fragment,a=>{if(a===i.fragment){let s=a.trackData.get(this.internalTrack.id);if(i.sampleIndex+1<s.samples.length)return{sampleIndex:i.sampleIndex+1,correctSampleFound:!0}}else if(a.trackData.get(this.internalTrack.id))return{sampleIndex:0,correctSampleFound:!0};return{sampleIndex:-1,correctSampleFound:!1}},-1/0,1/0,e)}async getKeyPacket(t,e){let r=this.mapTimestampIntoTimescale(t),i=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),a=wm(i,r),s=await this.fetchPacketForSampleIndex(a,e);return!ol(i)||!this.internalTrack.demuxer.isFragmented?s:this.performFragmentedLookup(null,o=>{let c=o.trackData.get(this.internalTrack.id);if(!c)return{sampleIndex:-1,correctSampleFound:!1};let u=rr(c.presentationTimestamps,d=>c.samples[d.sampleIndex].isKeyFrame&&d.presentationTimestamp<=r),l=u!==-1?c.presentationTimestamps[u].sampleIndex:-1,m=u!==-1&&r<c.endTimestamp;return{sampleIndex:l,correctSampleFound:m}},r,r,e)}async getNextKeyPacket(t,e){let r=this.packetToSampleIndex.get(t);if(r!==void 0){let a=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),s=Am(a,r);return this.fetchPacketForSampleIndex(s,e)}let i=this.packetToFragmentLocation.get(t);if(i===void 0)throw new Error("Packet was not created from this track.");return this.performFragmentedLookup(i.fragment,a=>{if(a===i.fragment){let o=a.trackData.get(this.internalTrack.id).samples.findIndex((c,u)=>c.isKeyFrame&&u>i.sampleIndex);if(o!==-1)return{sampleIndex:o,correctSampleFound:!0}}else{let s=a.trackData.get(this.internalTrack.id);if(s&&s.firstKeyFrameTimestamp!==null){let o=s.samples.findIndex(c=>c.isKeyFrame);return h(o!==-1),{sampleIndex:o,correctSampleFound:!0}}}return{sampleIndex:-1,correctSampleFound:!1}},-1/0,1/0,e)}async fetchPacketForSampleIndex(t,e){if(t===-1)return null;let r=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),i=Sm(r,t);if(!i)return null;let a;if(e.metadataOnly)a=de;else{let u=this.internalTrack.demuxer.reader.requestSlice(i.sampleOffset,i.sampleSize);if(u instanceof Promise&&(u=await u),!u)return null;if(a=B(u,i.sampleSize),this.internalTrack.encryptionAuxInfo){h(this.internalTrack.encryptionInfo);let l=await dl(this.internalTrack.demuxer.reader,this.internalTrack.encryptionInfo,this.internalTrack.encryptionAuxInfo);t<l.length&&(a=await ul(this.internalTrack,l[t],a,null))}}let s=(i.presentationTimestamp-this.internalTrack.editListOffset)/this.internalTrack.timescale,o=i.duration/this.internalTrack.timescale,c=new Q(a,i.isKeyFrame?"key":"delta",s,o,t,i.sampleSize);return this.packetToSampleIndex.set(c,t),c}async fetchPacketInFragment(t,e,r){if(e===-1)return null;let a=t.trackData.get(this.internalTrack.id).samples[e];h(a);let s;if(r.metadataOnly)s=de;else{let l=this.internalTrack.demuxer.reader.requestSlice(a.byteOffset,a.byteSize);if(l instanceof Promise&&(l=await l),!l)return null;s=B(l,a.byteSize),a.encryption&&(s=await ul(this.internalTrack,a.encryption,s,t))}let o=(a.presentationTimestamp-this.internalTrack.editListOffset)/this.internalTrack.timescale,c=a.duration/this.internalTrack.timescale,u=new Q(s,a.isKeyFrame?"key":"delta",o,c,t.moofOffset+e,a.byteSize);return this.packetToFragmentLocation.set(u,{fragment:t,sampleIndex:e}),u}async performFragmentedLookup(t,e,r,i,a){let s=this.internalTrack.demuxer,o=null,c=null,u=-1;if(t){let{sampleIndex:g,correctSampleFound:y}=e(t);if(y)return this.fetchPacketInFragment(t,g,a);g!==-1&&(c=t,u=g)}let l=H(this.internalTrack.fragmentLookupTable,r,g=>g.timestamp),m=l!==-1?this.internalTrack.fragmentLookupTable[l]:null,d=H(this.internalTrack.fragmentPositionCache,r,g=>g.startTimestamp),f=d!==-1?this.internalTrack.fragmentPositionCache[d]:null,p=Math.max(m?.moofOffset??0,f?.moofOffset??0)||null,b;for(t?p===null||t.moofOffset>=p?(b=t.moofOffset+t.moofSize,o=t):b=p:b=p??0;;){if(o){let T=o.trackData.get(this.internalTrack.id);if(T&&T.startTimestamp>i)break}let g=s.reader.requestSliceRange(b,st,Nt);if(g instanceof Promise&&(g=await g),!g)break;let y=b,k=Lt(g);if(!k)break;if(k.name==="moof"){o=await s.readFragment(y);let{sampleIndex:T,correctSampleFound:w}=e(o);if(w)return this.fetchPacketInFragment(o,T,a);T!==-1&&(c=o,u=T)}b=y+k.totalSize}if(m&&(!c||c.moofOffset<m.moofOffset)){let g=this.internalTrack.fragmentLookupTable[l-1];h(!g||g.timestamp<m.timestamp);let y=g?.timestamp??-1/0;return this.performFragmentedLookup(null,e,y,i,a)}return c?this.fetchPacketInFragment(c,u,a):null}},ba=class extends ga{constructor(e){super(e);this.decoderConfigPromise=null;this.internalTrack=e}getType(){return"video"}getCodec(){return this.internalTrack.info.codec}getCodedWidth(){return this.internalTrack.info.width}getCodedHeight(){return this.internalTrack.info.height}getSquarePixelWidth(){return this.internalTrack.info.squarePixelWidth}getSquarePixelHeight(){return this.internalTrack.info.squarePixelHeight}getRotation(){return this.internalTrack.rotation}async getColorSpace(){return{primaries:this.internalTrack.info.colorSpace?.primaries,transfer:this.internalTrack.info.colorSpace?.transfer,matrix:this.internalTrack.info.colorSpace?.matrix,fullRange:this.internalTrack.info.colorSpace?.fullRange}}async canBeTransparent(){return!1}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfigPromise??=(async()=>{if(this.internalTrack.info.codec==="vp9"&&!this.internalTrack.info.vp9CodecInfo){let r=await this.getFirstPacket({});this.internalTrack.info.vp9CodecInfo=r&&ia(r.data)}else if(this.internalTrack.info.codec==="av1"&&!this.internalTrack.info.av1CodecInfo){let r=await this.getFirstPacket({});this.internalTrack.info.av1CodecInfo=r&&na(r.data)}let e={codec:ri(this.internalTrack.info),codedWidth:this.internalTrack.info.width,codedHeight:this.internalTrack.info.height,description:this.internalTrack.info.codecDescription??void 0,colorSpace:this.internalTrack.info.colorSpace??void 0};return(this.internalTrack.info.width!==this.internalTrack.info.squarePixelWidth||this.internalTrack.info.height!==this.internalTrack.info.squarePixelHeight)&&(e.displayAspectWidth=this.internalTrack.info.squarePixelWidth,e.displayAspectHeight=this.internalTrack.info.squarePixelHeight),e})():null}},ya=class extends ga{constructor(e){super(e);this.decoderConfig=null;this.internalTrack=e}getType(){return"audio"}getCodec(){return this.internalTrack.info.codec}getNumberOfChannels(){return this.internalTrack.info.numberOfChannels}getSampleRate(){return this.internalTrack.info.sampleRate}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfig??={codec:ii(this.internalTrack.info),numberOfChannels:this.internalTrack.info.numberOfChannels,sampleRate:this.internalTrack.info.sampleRate,description:this.internalTrack.info.codecDescription??void 0}:null}},So=(n,t)=>{if(n.presentationTimestamps){let e=H(n.presentationTimestamps,t,r=>r.presentationTimestamp);return e===-1?-1:n.presentationTimestamps[e].sampleIndex}else{let e=H(n.sampleTimingEntries,t,i=>i.startDecodeTimestamp);if(e===-1)return-1;let r=n.sampleTimingEntries[e];return r.startIndex+Math.min(Math.floor((t-r.startDecodeTimestamp)/r.delta),r.count-1)}},wm=(n,t)=>{if(!n.keySampleIndices)return So(n,t);if(n.presentationTimestamps){let e=H(n.presentationTimestamps,t,r=>r.presentationTimestamp);if(e===-1)return-1;for(let r=e;r>=0;r--){let i=n.presentationTimestamps[r].sampleIndex;if(Ft(n.keySampleIndices,i,s=>s)!==-1)return i}return-1}else{let e=So(n,t),r=H(n.keySampleIndices,e,i=>i);return n.keySampleIndices[r]??-1}},Sm=(n,t)=>{let e=H(n.sampleTimingEntries,t,y=>y.startIndex),r=n.sampleTimingEntries[e];if(!r||r.startIndex+r.count<=t)return null;let a=r.startDecodeTimestamp+(t-r.startIndex)*r.delta,s=H(n.sampleCompositionTimeOffsets,t,y=>y.startIndex),o=n.sampleCompositionTimeOffsets[s];o&&t-o.startIndex<o.count&&(a+=o.offset);let c=n.sampleSizes[Math.min(t,n.sampleSizes.length-1)],u=H(n.sampleToChunk,t,y=>y.startSampleIndex),l=n.sampleToChunk[u];h(l);let m=l.startChunkIndex+Math.floor((t-l.startSampleIndex)/l.samplesPerChunk),d=n.chunkOffsets[m],f=l.startSampleIndex+(m-l.startChunkIndex)*l.samplesPerChunk,p=0,b=d;if(n.sampleSizes.length===1)b+=c*(t-f),p+=c*l.samplesPerChunk;else for(let y=f;y<f+l.samplesPerChunk;y++){let k=n.sampleSizes[y];y<t&&(b+=k),p+=k}let g=r.delta;if(n.presentationTimestamps){let y=n.presentationTimestampIndexMap[t];h(y!==void 0),y<n.presentationTimestamps.length-1&&(g=n.presentationTimestamps[y+1].presentationTimestamp-a)}return{presentationTimestamp:a,duration:g,sampleOffset:b,sampleSize:c,chunkOffset:d,chunkSize:p,isKeyFrame:n.keySampleIndices?Ft(n.keySampleIndices,t,y=>y)!==-1:!0}},Am=(n,t)=>{if(!n.keySampleIndices)return t+1;let e=H(n.keySampleIndices,t,r=>r);return n.keySampleIndices[e+1]??-1},wo=(n,t)=>{n.startTimestamp+=t,n.endTimestamp+=t;for(let e of n.samples)e.presentationTimestamp+=t;for(let e of n.presentationTimestamps)e.presentationTimestamp+=t},xm=n=>{let[t,e]=n,r=Math.atan2(e,t);return Number.isFinite(r)?r*(180/Math.PI):0},ol=n=>n.sampleSizes.length===0,cl=n=>n.currentFragmentState?n.currentFragmentState.encryptionAuxInfo??={defaultSampleInfoSize:0,sampleSizes:null,sampleCount:0,offset:null,resolved:null}:n.encryptionAuxInfo??={defaultSampleInfoSize:0,sampleSizes:null,sampleCount:0,offset:null,resolved:null},dl=async(n,t,e)=>{if(e.resolved)return e.resolved;if(e.offset===null||e.sampleCount===0)throw new Error("Incomplete saiz/saio info; cannot resolve encryption data.");let r=0;if(e.defaultSampleInfoSize>0)r=e.defaultSampleInfoSize*e.sampleCount;else{h(e.sampleSizes);for(let o=0;o<e.sampleCount;o++)r+=e.sampleSizes[o]}let i=n.requestSlice(e.offset,r);if(i instanceof Promise&&(i=await i),!i)throw new Error("Failed to read auxiliary encryption info.");let a=t.defaultPerSampleIvSize;h(a!==null);let s=[];for(let o=0;o<e.sampleCount;o++){let c=e.defaultSampleInfoSize>0?e.defaultSampleInfoSize:e.sampleSizes[o],u=new Uint8Array(16);a>0?u.set(B(i,a),0):u.set(t.defaultConstantIv,0);let l=null;if(c>a){let m=se(i);l=[];for(let d=0;d<m;d++){let f=se(i),p=F(i);l.push({clearLen:f,protectedLen:p})}}s.push({iv:u,subsamples:l})}return e.resolved=s,s},ul=async(n,t,e,r)=>{h(n.encryptionInfo);let i=n.encryptionInfo;h(i.defaultKid!==null);let a=i.defaultKid,s,o=n.demuxer.decryptionKeyCache.get(a);if(o)s=await o;else{if(!n.demuxer.input._formatOptions.isobmff?.resolveKeyId)throw new Error("Encrypted media samples encountered. To decrypt them, please provide a callback for InputOptions.formatOptions.isobmff.resolveKeyId.");let c=(async()=>{let u=n.demuxer.psshBoxes;if(r){u=[...u,...r.psshBoxes].filter(m=>m.keyIds===null||m.keyIds.includes(a));for(let m=0;m<u.length-1;m++)for(let d=m+1;d<u.length;d++)ma(u[m],u[d])&&(u.splice(d,1),d--)}let l=await n.demuxer.input._formatOptions.isobmff.resolveKeyId({keyId:a,psshBoxes:u});if(!(typeof l=="string"&&l.length===32&&mu.test(l)||l instanceof Uint8Array&&l.byteLength===16))throw new TypeError("resolveKeyId must return a 32-character hex string or a 16-byte Uint8Array containing the decryption key.");return l instanceof Uint8Array?l:fu(l)})();n.demuxer.decryptionKeyCache.set(a,c),s=await c}return i.scheme==="cenc"||i.scheme==="cens"?Pm(s,i,t,e):Cm(s,i,t,e)},Pm=async(n,t,e,r)=>{let i=new Uint8Array(16);i.set(e.iv,0);let a=await crypto.subtle.importKey("raw",n,{name:"AES-CTR"},!1,["decrypt"]),s=async p=>{let b=await crypto.subtle.decrypt({name:"AES-CTR",counter:i,length:64},a,p);return new Uint8Array(b)};if(!e.subsamples)return s(r);h(t.defaultCryptByteBlock!==null&&t.defaultSkipByteBlock!==null);let o=ml(e.subsamples,t.defaultCryptByteBlock,t.defaultSkipByteBlock),c=0;for(let p of o)for(let b of p.perSubsample)c+=b.length;let u=new Uint8Array(c),l=0;for(let p of o)for(let b of p.perSubsample)u.set(r.subarray(b.offset,b.offset+b.length),l),l+=b.length;let m=await s(u),d=new Uint8Array(r),f=0;for(let p of o)for(let b of p.perSubsample)d.set(m.subarray(f,f+b.length),b.offset),f+=b.length;return d},Cm=(n,t,e,r)=>{let i=new cn;i.init({key:n,iv:e.iv});let a=t.defaultCryptByteBlock,s=t.defaultSkipByteBlock;if(h(a!==null&&s!==null),!e.subsamples){let l=new Uint8Array(r),m=Math.floor(r.length/16);for(let d=0;d<m;d++){let f=d*16;i.in.set(r.subarray(f,f+16)),i.decrypt(),l.set(i.out,f)}return l}if(a===0&&s===0)throw new Error("cbcs with subsamples requires pattern encryption.");let o=new Uint8Array(r),c=ml(e.subsamples,a,s),u=new DataView(e.iv.buffer,e.iv.byteOffset,16);for(let l of c){i.iv[0]=u.getUint32(0,!1),i.iv[1]=u.getUint32(4,!1),i.iv[2]=u.getUint32(8,!1),i.iv[3]=u.getUint32(12,!1);for(let m of l.perSubsample){let d=m.length/16;for(let f=0;f<d;f++){let p=m.offset+f*16;i.in.set(r.subarray(p,p+16)),i.decrypt(),o.set(i.out,p)}}}return o},ml=(n,t,e)=>{let r=[],i=t!==0||e!==0,a=0;for(let s of n){a+=s.clearLen;let o=[];if(!i)s.protectedLen>0&&o.push({offset:a,length:s.protectedLen}),a+=s.protectedLen;else{let c=s.protectedLen,u=a;for(;c>0&&!(c<16*t);){let l=16*t;o.push({offset:u,length:l}),u+=l,c-=l;let m=Math.min(16*e,c);u+=m,c-=m}a+=s.protectedLen}r.push({perSubsample:o})}return r};var mi=class{constructor(t){this.value=t}},fi=class{constructor(t){this.value=t}},ln=class{constructor(t){this.value=t}},lt=class{constructor(t){this.value=t}};var Im=[440786851,408125543],pi=[290298740,357149030,524531317,374648427,475249515,423732329,272869232,307544935],dn=[...Im,...pi],pl=n=>n<256?1:n<65536?2:n<1<<24?3:n<2**32?4:n<2**40?5:6,hl=n=>n<1n<<8n?1:n<1n<<16n?2:n<1n<<24n?3:n<1n<<32n?4:n<1n<<40n?5:n<1n<<48n?6:n<1n<<56n?7:8,gl=n=>n>=-64&&n<64?1:n>=-8192&&n<8192?2:n>=-(1<<20)&&n<1<<20?3:n>=-(1<<27)&&n<1<<27?4:n>=-(2**34)&&n<2**34?5:6,Em=n=>{if(n<127)return 1;if(n<16383)return 2;if(n<(1<<21)-1)return 3;if(n<(1<<28)-1)return 4;if(n<2**35-1)return 5;if(n<2**42-1)return 6;throw new Error("EBML varint size not supported "+n)},Ta=class{constructor(t){this.writer=t;this.helper=new Uint8Array(8);this.helperView=new DataView(this.helper.buffer);this.offsets=new WeakMap;this.dataOffsets=new WeakMap}writeByte(t){this.helperView.setUint8(0,t),this.writer.write(this.helper.subarray(0,1))}writeFloat32(t){this.helperView.setFloat32(0,t,!1),this.writer.write(this.helper.subarray(0,4))}writeFloat64(t){this.helperView.setFloat64(0,t,!1),this.writer.write(this.helper)}writeUnsignedInt(t,e=pl(t)){let r=0;switch(e){case 6:this.helperView.setUint8(r++,t/2**40|0);case 5:this.helperView.setUint8(r++,t/2**32|0);case 4:this.helperView.setUint8(r++,t>>24);case 3:this.helperView.setUint8(r++,t>>16);case 2:this.helperView.setUint8(r++,t>>8);case 1:this.helperView.setUint8(r++,t);break;default:throw new Error("Bad unsigned int size "+e)}this.writer.write(this.helper.subarray(0,r))}writeUnsignedBigInt(t,e=hl(t)){let r=0;for(let i=e-1;i>=0;i--)this.helperView.setUint8(r++,Number(t>>BigInt(i*8)&0xffn));this.writer.write(this.helper.subarray(0,r))}writeSignedInt(t,e=gl(t)){t<0&&(t+=2**(e*8)),this.writeUnsignedInt(t,e)}writeVarInt(t,e=Em(t)){let r=0;switch(e){case 1:this.helperView.setUint8(r++,128|t);break;case 2:this.helperView.setUint8(r++,64|t>>8),this.helperView.setUint8(r++,t);break;case 3:this.helperView.setUint8(r++,32|t>>16),this.helperView.setUint8(r++,t>>8),this.helperView.setUint8(r++,t);break;case 4:this.helperView.setUint8(r++,16|t>>24),this.helperView.setUint8(r++,t>>16),this.helperView.setUint8(r++,t>>8),this.helperView.setUint8(r++,t);break;case 5:this.helperView.setUint8(r++,8|t/2**32&7),this.helperView.setUint8(r++,t>>24),this.helperView.setUint8(r++,t>>16),this.helperView.setUint8(r++,t>>8),this.helperView.setUint8(r++,t);break;case 6:this.helperView.setUint8(r++,4|t/2**40&3),this.helperView.setUint8(r++,t/2**32|0),this.helperView.setUint8(r++,t>>24),this.helperView.setUint8(r++,t>>16),this.helperView.setUint8(r++,t>>8),this.helperView.setUint8(r++,t);break;default:throw new Error("Bad EBML varint size "+e)}this.writer.write(this.helper.subarray(0,r))}writeAsciiString(t){this.writer.write(new Uint8Array(t.split("").map(e=>e.charCodeAt(0))))}writeEBML(t){if(t!==null)if(t instanceof Uint8Array)this.writer.write(t);else if(Array.isArray(t))for(let e of t)this.writeEBML(e);else if(this.offsets.set(t,this.writer.getPos()),this.writeUnsignedInt(t.id),Array.isArray(t.data)){let e=this.writer.getPos(),r=t.size===-1?1:t.size??4;t.size===-1?this.writeByte(255):this.writer.seek(this.writer.getPos()+r);let i=this.writer.getPos();if(this.dataOffsets.set(t,i),this.writeEBML(t.data),t.size!==-1){let a=this.writer.getPos()-i,s=this.writer.getPos();this.writer.seek(e),this.writeVarInt(a,r),this.writer.seek(s)}}else if(typeof t.data=="number"){let e=t.size??pl(t.data);this.writeVarInt(e),this.writeUnsignedInt(t.data,e)}else if(typeof t.data=="bigint"){let e=t.size??hl(t.data);this.writeVarInt(e),this.writeUnsignedBigInt(t.data,e)}else if(typeof t.data=="string")this.writeVarInt(t.data.length),this.writeAsciiString(t.data);else if(t.data instanceof Uint8Array)this.writeVarInt(t.data.byteLength,t.size),this.writer.write(t.data);else if(t.data instanceof mi)this.writeVarInt(4),this.writeFloat32(t.data.value);else if(t.data instanceof fi)this.writeVarInt(8),this.writeFloat64(t.data.value);else if(t.data instanceof ln){let e=t.size??gl(t.data.value);this.writeVarInt(e),this.writeSignedInt(t.data.value,e)}else if(t.data instanceof lt){let e=$.encode(t.data.value);this.writeVarInt(e.length),this.writer.write(e)}else ne(t.data)}},Ao=8,Oe=2,dt=2*Ao,xo=n=>{if(n.remainingLength<1)return null;let t=O(n);if(n.skip(-1),t===0)return null;let e=1,r=128;for(;(t&r)===0;)e++,r>>=1;return n.remainingLength<e?null:e},hi=n=>{if(n.remainingLength<1)return null;let t=O(n);if(t===0)return null;let e=1,r=128;for(;(t&r)===0;)e++,r>>=1;if(n.remainingLength<e-1)return null;let i=t&r-1;for(let a=1;a<e;a++)i*=256,i+=O(n);return i},G=(n,t)=>{if(t<1||t>8)throw new Error("Bad unsigned int size "+t);let e=0;for(let r=0;r<t;r++)e*=256,e+=O(n);return e},bl=(n,t)=>{if(t<1)throw new Error("Bad unsigned int size "+t);let e=0n;for(let r=0;r<t;r++)e<<=8n,e+=BigInt(O(n));return e};var wa=n=>{let t=xo(n);return t===null||n.remainingLength<t?null:G(n,t)},Po=n=>{if(n.remainingLength<1)return null;if(O(n)===255)return;n.skip(-1);let e=hi(n);if(e===null)return null;if(e!==72057594037927940)return e},mt=n=>{h(n.remainingLength>=Oe);let t=wa(n);if(t===null)return null;let e=Po(n);return e===null?null:{id:t,size:e}},mr=(n,t)=>{let e=B(n,t),r=0;for(;r<t&&e[r]!==0;)r+=1;return String.fromCharCode(...e.subarray(0,r))},gi=(n,t)=>{let e=B(n,t),r=0;for(;r<t&&e[r]!==0;)r+=1;return me.decode(e.subarray(0,r))},Sa=(n,t)=>{if(t===0)return 0;if(t!==4&&t!==8)throw new Error("Bad float size "+t);return t===4?yl(n):ka(n)},Aa=async(n,t,e,r)=>{let i=new Set(e),a=t;for(;r===null||a<r;){let s=n.requestSliceRange(a,Oe,dt);if(s instanceof Promise&&(s=await s),!s)break;let o=mt(s);if(!o)break;if(i.has(o.id))return{pos:a,found:!0};qt(o.size),a=s.filePos+o.size}return{pos:r!==null&&r>a?r:a,found:!1}},Co=async(n,t,e,r)=>{let a=new Set(e),s=t;for(;s<r;){let o=n.requestSliceRange(s,0,Math.min(65536,r-s));if(o instanceof Promise&&(o=await o),!o||o.length<Ao)break;for(let c=0;c<o.length-Ao;c++){o.filePos=s;let u=wa(o);if(u!==null&&a.has(u))return s;s++}}return null},Fe={avc:"V_MPEG4/ISO/AVC",hevc:"V_MPEGH/ISO/HEVC",vp8:"V_VP8",vp9:"V_VP9",av1:"V_AV1",aac:"A_AAC",mp3:"A_MPEG/L3",opus:"A_OPUS",vorbis:"A_VORBIS",flac:"A_FLAC",ac3:"A_AC3",eac3:"A_EAC3","pcm-u8":"A_PCM/INT/LIT","pcm-s16":"A_PCM/INT/LIT","pcm-s16be":"A_PCM/INT/BIG","pcm-s24":"A_PCM/INT/LIT","pcm-s24be":"A_PCM/INT/BIG","pcm-s32":"A_PCM/INT/LIT","pcm-s32be":"A_PCM/INT/BIG","pcm-f32":"A_PCM/FLOAT/IEEE","pcm-f64":"A_PCM/FLOAT/IEEE",webvtt:"S_TEXT/WEBVTT"};function qt(n){if(n===void 0)throw new Error("Undefined element size is used in a place where it is not supported.")}var xa=n=>{let e=(n.hasVideo?"video/":n.hasAudio?"audio/":"application/")+(n.isWebM?"webm":"x-matroska");if(n.codecStrings.length>0){let r=[...new Set(n.codecStrings.filter(Boolean))];e+=`; codecs="${r.join(", ")}"`}return e};var Io=[{id:290298740,flag:"seekHeadSeen"},{id:357149030,flag:"infoSeen"},{id:374648427,flag:"tracksSeen"},{id:475249515,flag:"cuesSeen"}],Tl=10*2**20,Pa=class extends pe{constructor(e){super(e);this.readMetadataPromise=null;this.segments=[];this.currentSegment=null;this.currentTrack=null;this.currentCluster=null;this.currentBlock=null;this.currentBlockAdditional=null;this.currentCueTime=null;this.currentDecodingInstruction=null;this.currentTagTargetIsMovie=!0;this.currentSimpleTagName=null;this.currentAttachedFile=null;this.isWebM=!1;this.reader=e._reader}async getTrackBackings(){return await this.readMetadata(),this.segments.flatMap(e=>e.tracks.map(r=>r.trackBacking))}async getMimeType(){await this.readMetadata();let e=await this.getTrackBackings(),r=await Promise.all(e.map(i=>i.getDecoderConfig().then(a=>a?.codec??null)));return xa({isWebM:this.isWebM,hasVideo:this.segments.some(i=>i.tracks.some(a=>a.info?.type==="video")),hasAudio:this.segments.some(i=>i.tracks.some(a=>a.info?.type==="audio")),codecStrings:r.filter(Boolean)})}async getMetadataTags(){await this.readMetadata();for(let r of this.segments)r.metadataTagsCollected||(this.reader.fileSize!==null&&await this.loadSegmentMetadata(r),r.metadataTagsCollected=!0);let e={};for(let r of this.segments)e={...e,...r.metadataTags};return e}readMetadata(){return this.readMetadataPromise??=(async()=>{let e=0;for(;;){let r=this.reader.requestSliceRange(e,Oe,dt);if(r instanceof Promise&&(r=await r),!r)break;let i=mt(r);if(!i)break;let a=i.id,s=i.size,o=r.filePos;if(a===440786851){qt(s);let c=this.reader.requestSlice(o,s);if(c instanceof Promise&&(c=await c),!c)break;this.readContiguousElements(c)}else if(a===408125543){if(await this.readSegment(o,s),s===void 0||this.reader.fileSize===null)break}else if(a===524531317){if(this.reader.fileSize===null)break;s===void 0&&(s=(await Aa(this.reader,o,dn,this.reader.fileSize)).pos-o);let c=X(this.segments);c&&(c.elementEndPos=o+s)}qt(s),e=o+s}})()}async readSegment(e,r){this.currentSegment={seekHeadSeen:!1,infoSeen:!1,tracksSeen:!1,cuesSeen:!1,tagsSeen:!1,attachmentsSeen:!1,timestampScale:-1,timestampFactor:-1,duration:-1,seekEntries:[],tracks:[],cuePoints:[],dataStartPos:e,elementEndPos:r===void 0?null:e+r,clusterSeekStartPos:e,lastReadCluster:null,metadataTags:{},metadataTagsCollected:!1},this.segments.push(this.currentSegment);let i=e;for(;this.currentSegment.elementEndPos===null||i<this.currentSegment.elementEndPos;){let c=this.reader.requestSliceRange(i,Oe,dt);if(c instanceof Promise&&(c=await c),!c)break;let u=i,l=mt(c);if(!l||!pi.includes(l.id)&&l.id!==236){let b=await Co(this.reader,u,pi,Math.min(this.currentSegment.elementEndPos??1/0,u+Tl));if(b){i=b;continue}else break}let{id:m,size:d}=l,f=c.filePos,p=Io.findIndex(b=>b.id===m);if(p!==-1){let b=Io[p].flag;this.currentSegment[b]=!0,qt(d);let g=this.reader.requestSlice(f,d);g instanceof Promise&&(g=await g),g&&this.readContiguousElements(g)}else if(m===307544935||m===423732329){m===307544935?this.currentSegment.tagsSeen=!0:this.currentSegment.attachmentsSeen=!0,qt(d);let b=this.reader.requestSlice(f,d);b instanceof Promise&&(b=await b),b&&this.readContiguousElements(b)}else if(m===524531317){this.currentSegment.clusterSeekStartPos=u;break}if(d===void 0)break;i=f+d}if(this.currentSegment.seekEntries.sort((c,u)=>c.segmentPosition-u.segmentPosition),this.reader.fileSize!==null)for(let c of this.currentSegment.seekEntries){let u=Io.find(b=>b.id===c.id);if(!u||this.currentSegment[u.flag])continue;let l=this.reader.requestSliceRange(e+c.segmentPosition,Oe,dt);if(l instanceof Promise&&(l=await l),!l)continue;let m=mt(l);if(!m)continue;let{id:d,size:f}=m;if(d!==u.id)continue;qt(f),this.currentSegment[u.flag]=!0;let p=this.reader.requestSlice(l.filePos,f);p instanceof Promise&&(p=await p),p&&this.readContiguousElements(p)}this.currentSegment.timestampScale===-1&&(this.currentSegment.timestampScale=1e6,this.currentSegment.timestampFactor=1e9/1e6);for(let c of this.currentSegment.tracks)c.defaultDurationNs!==null&&(c.defaultDuration=this.currentSegment.timestampFactor*c.defaultDurationNs/1e9);let a=new Map(this.currentSegment.tracks.map(c=>[c.id,c]));for(let c of this.currentSegment.cuePoints){let u=a.get(c.trackId);u&&u.cuePoints.push(c)}for(let c of this.currentSegment.tracks){c.cuePoints.sort((u,l)=>u.time-l.time);for(let u=0;u<c.cuePoints.length-1;u++){let l=c.cuePoints[u],m=c.cuePoints[u+1];l.time===m.time&&(c.cuePoints.splice(u+1,1),u--)}}let s=null,o=-1/0;for(let c of this.currentSegment.tracks)c.cuePoints.length>o&&(o=c.cuePoints.length,s=c);for(let c of this.currentSegment.tracks)c.cuePoints.length===0&&(c.cuePoints=s.cuePoints);this.currentSegment=null}async readCluster(e,r){if(r.lastReadCluster?.elementStartPos===e)return r.lastReadCluster;let i=this.reader.requestSliceRange(e,Oe,dt);i instanceof Promise&&(i=await i),h(i);let a=e,s=mt(i);h(s);let o=s.id;h(o===524531317);let c=s.size,u=i.filePos;c===void 0&&(c=(await Aa(this.reader,u,dn,r.elementEndPos)).pos-u);let l=this.reader.requestSlice(u,c);l instanceof Promise&&(l=await l);let m={segment:r,elementStartPos:a,elementEndPos:u+c,dataStartPos:u,timestamp:-1,trackData:new Map};if(this.currentCluster=m,l){let d=this.readContiguousElements(l,dn);m.elementEndPos=d}for(let[,d]of m.trackData){let f=d.track;h(d.blocks.length>0);let p=!1;for(let k=0;k<d.blocks.length;k++){let T=d.blocks[k];T.timestamp+=m.timestamp,p||=T.lacing!==0}d.presentationTimestamps=d.blocks.map((k,T)=>({timestamp:k.timestamp,blockIndex:T})).sort((k,T)=>k.timestamp-T.timestamp);for(let k=0;k<d.presentationTimestamps.length;k++){let T=d.presentationTimestamps[k],w=d.blocks[T.blockIndex];if(d.firstKeyFrameTimestamp===null&&w.isKeyFrame&&(d.firstKeyFrameTimestamp=w.timestamp),k<d.presentationTimestamps.length-1){let S=d.presentationTimestamps[k+1];w.duration=S.timestamp-w.timestamp}else w.duration===0&&f.defaultDuration!=null&&w.lacing===0&&(w.duration=f.defaultDuration)}p&&(this.expandLacedBlocks(d.blocks,f),d.presentationTimestamps=d.blocks.map((k,T)=>({timestamp:k.timestamp,blockIndex:T})).sort((k,T)=>k.timestamp-T.timestamp));let b=d.blocks[d.presentationTimestamps[0].blockIndex],g=d.blocks[X(d.presentationTimestamps).blockIndex];d.startTimestamp=b.timestamp,d.endTimestamp=g.timestamp+g.duration;let y=H(f.clusterPositionCache,d.startTimestamp,k=>k.startTimestamp);(y===-1||f.clusterPositionCache[y].elementStartPos!==a)&&f.clusterPositionCache.splice(y+1,0,{elementStartPos:m.elementStartPos,startTimestamp:d.startTimestamp})}return r.lastReadCluster=m,m}getTrackDataInCluster(e,r){let i=e.trackData.get(r);if(!i){let a=e.segment.tracks.find(s=>s.id===r);if(!a)return null;i={track:a,startTimestamp:0,endTimestamp:0,firstKeyFrameTimestamp:null,blocks:[],presentationTimestamps:[]},e.trackData.set(r,i)}return i}expandLacedBlocks(e,r){for(let i=0;i<e.length;i++){let a=e[i];if(a.lacing===0)continue;a.decoded||(a.data=this.decodeBlockData(r,a.data),a.decoded=!0);let s=he.tempFromBytes(a.data),o=[],c=O(s)+1;switch(a.lacing){case 1:{let l=0;for(let m=0;m<c-1;m++){let d=0;for(;s.bufferPos<s.length;){let f=O(s);if(d+=f,f<255){o.push(d),l+=d;break}}}o.push(s.length-(s.bufferPos+l))}break;case 2:{let l=s.length-1,m=Math.floor(l/c);for(let d=0;d<c;d++)o.push(m)}break;case 3:{let l=hi(s);h(l!==null);let m=l;o.push(m);let d=m;for(let f=1;f<c-1;f++){let p=s.bufferPos,b=hi(s);h(b!==null);let g=b,k=(1<<(s.bufferPos-p)*7-1)-1,T=g-k;m+=T,o.push(m),d+=m}o.push(s.length-(s.bufferPos+d))}break;default:h(!1)}h(o.length===c),e.splice(i,1);let u=a.duration||c*(r.defaultDuration??0);for(let l=0;l<c;l++){let m=o[l],d=B(s,m),f=a.timestamp+u*l/c,p=u/c;e.splice(i+l,0,{timestamp:f,duration:p,isKeyFrame:a.isKeyFrame,data:d,lacing:0,decoded:!0,mainAdditional:a.mainAdditional})}i+=c,i--}}async loadSegmentMetadata(e){for(let r of e.seekEntries){if(!(r.id===307544935&&!e.tagsSeen)){if(!(r.id===423732329&&!e.attachmentsSeen))continue}let i=this.reader.requestSliceRange(e.dataStartPos+r.segmentPosition,Oe,dt);if(i instanceof Promise&&(i=await i),!i)continue;let a=mt(i);if(!a||a.id!==r.id)continue;let{size:s}=a;qt(s),h(!this.currentSegment),this.currentSegment=e;let o=this.reader.requestSlice(i.filePos,s);o instanceof Promise&&(o=await o),o&&this.readContiguousElements(o),this.currentSegment=null,r.id===307544935?e.tagsSeen=!0:r.id===423732329&&(e.attachmentsSeen=!0)}}readContiguousElements(e,r){for(;e.remainingLength>=Oe;){let i=e.filePos;if(!this.traverseElement(e,r))return i}return e.filePos}traverseElement(e,r){let i=mt(e);if(!i||r&&r.includes(i.id))return!1;let{id:a,size:s}=i,o=e.filePos;switch(qt(s),a){case 17026:this.isWebM=mr(e,s)==="webm";break;case 19899:{if(!this.currentSegment)break;let c={id:-1,segmentPosition:-1};this.currentSegment.seekEntries.push(c),this.readContiguousElements(e.slice(o,s)),(c.id===-1||c.segmentPosition===-1)&&this.currentSegment.seekEntries.pop()}break;case 21419:{let c=this.currentSegment?.seekEntries[this.currentSegment.seekEntries.length-1];if(!c)break;c.id=G(e,s)}break;case 21420:{let c=this.currentSegment?.seekEntries[this.currentSegment.seekEntries.length-1];if(!c)break;c.segmentPosition=G(e,s)}break;case 2807729:{if(!this.currentSegment)break;this.currentSegment.timestampScale=G(e,s),this.currentSegment.timestampFactor=1e9/this.currentSegment.timestampScale}break;case 17545:{if(!this.currentSegment)break;this.currentSegment.duration=Sa(e,s)}break;case 174:{if(!this.currentSegment||(this.currentTrack={id:-1,segment:this.currentSegment,demuxer:this,clusterPositionCache:[],cuePoints:[],disposition:{...we,primary:!1},trackBacking:null,codecId:null,codecPrivate:null,defaultDuration:null,defaultDurationNs:null,name:null,languageCode:"eng",hasLanguageBcp47:!1,decodingInstructions:[],info:null},this.readContiguousElements(e.slice(o,s)),!this.currentTrack))break;if(this.currentTrack.decodingInstructions.some(c=>c.data?.type!=="decompress"||c.scope!==1||c.data.algorithm!==3)&&(console.warn(`Track #${this.currentTrack.id} has an unsupported content encoding; dropping.`),this.currentTrack=null),this.currentTrack&&this.currentTrack.id!==-1&&this.currentTrack.codecId&&this.currentTrack.info){let c=this.currentTrack.codecId.indexOf("/"),u=c===-1?this.currentTrack.codecId:this.currentTrack.codecId.slice(0,c);if(this.currentTrack.info.type==="video"&&this.currentTrack.info.width!==-1&&this.currentTrack.info.height!==-1){if(this.currentTrack.info.squarePixelWidth=this.currentTrack.info.width,this.currentTrack.info.squarePixelHeight=this.currentTrack.info.height,this.currentTrack.info.displayWidth!==null&&this.currentTrack.info.displayHeight!==null){let m=this.currentTrack.info.displayWidth*this.currentTrack.info.height,d=this.currentTrack.info.displayHeight*this.currentTrack.info.width;m>0&&d>0&&(m>d?this.currentTrack.info.squarePixelWidth=Math.round(this.currentTrack.info.width*m/d):this.currentTrack.info.squarePixelHeight=Math.round(this.currentTrack.info.height*d/m))}this.currentTrack.codecId===Fe.avc?(this.currentTrack.info.codec="avc",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):this.currentTrack.codecId===Fe.hevc?(this.currentTrack.info.codec="hevc",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):u===Fe.vp8?this.currentTrack.info.codec="vp8":u===Fe.vp9?this.currentTrack.info.codec="vp9":u===Fe.av1&&(this.currentTrack.info.codec="av1");let l=this.currentTrack;this.currentTrack.trackBacking=new Eo(l),this.currentSegment.tracks.push(this.currentTrack)}else if(this.currentTrack.info.type==="audio"){u===Fe.aac?(this.currentTrack.info.codec="aac",this.currentTrack.info.aacCodecInfo={isMpeg2:this.currentTrack.codecId.includes("MPEG2"),objectType:null},this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):this.currentTrack.codecId===Fe.mp3?this.currentTrack.info.codec="mp3":u===Fe.opus?(this.currentTrack.info.codec="opus",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate,this.currentTrack.info.sampleRate=nt):u===Fe.vorbis?(this.currentTrack.info.codec="vorbis",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):u===Fe.flac?(this.currentTrack.info.codec="flac",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):u===Fe.ac3?(this.currentTrack.info.codec="ac3",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):u===Fe.eac3?(this.currentTrack.info.codec="eac3",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):this.currentTrack.codecId==="A_PCM/INT/LIT"?this.currentTrack.info.bitDepth===8?this.currentTrack.info.codec="pcm-u8":this.currentTrack.info.bitDepth===16?this.currentTrack.info.codec="pcm-s16":this.currentTrack.info.bitDepth===24?this.currentTrack.info.codec="pcm-s24":this.currentTrack.info.bitDepth===32&&(this.currentTrack.info.codec="pcm-s32"):this.currentTrack.codecId==="A_PCM/INT/BIG"?this.currentTrack.info.bitDepth===8?this.currentTrack.info.codec="pcm-u8":this.currentTrack.info.bitDepth===16?this.currentTrack.info.codec="pcm-s16be":this.currentTrack.info.bitDepth===24?this.currentTrack.info.codec="pcm-s24be":this.currentTrack.info.bitDepth===32&&(this.currentTrack.info.codec="pcm-s32be"):this.currentTrack.codecId==="A_PCM/FLOAT/IEEE"&&(this.currentTrack.info.bitDepth===32?this.currentTrack.info.codec="pcm-f32":this.currentTrack.info.bitDepth===64&&(this.currentTrack.info.codec="pcm-f64"));let l=this.currentTrack;this.currentTrack.trackBacking=new vo(l),this.currentSegment.tracks.push(this.currentTrack)}}this.currentTrack=null}break;case 215:{if(!this.currentTrack)break;this.currentTrack.id=G(e,s)}break;case 131:{if(!this.currentTrack)break;let c=G(e,s);c===1?this.currentTrack.info={type:"video",width:-1,height:-1,displayWidth:null,displayHeight:null,displayUnit:null,squarePixelWidth:-1,squarePixelHeight:-1,rotation:0,codec:null,codecDescription:null,colorSpace:null,alphaMode:!1}:c===2&&(this.currentTrack.info={type:"audio",numberOfChannels:1,sampleRate:8e3,bitDepth:-1,codec:null,codecDescription:null,aacCodecInfo:null})}break;case 185:{if(!this.currentTrack)break;G(e,s)||(this.currentTrack=null)}break;case 136:{if(!this.currentTrack)break;this.currentTrack.disposition.default=!!G(e,s)}break;case 21930:{if(!this.currentTrack)break;this.currentTrack.disposition.forced=!!G(e,s)}break;case 21934:{if(!this.currentTrack)break;this.currentTrack.disposition.original=!!G(e,s)}break;case 21931:{if(!this.currentTrack)break;this.currentTrack.disposition.hearingImpaired=!!G(e,s)}break;case 21932:{if(!this.currentTrack)break;this.currentTrack.disposition.visuallyImpaired=!!G(e,s)}break;case 21935:{if(!this.currentTrack)break;this.currentTrack.disposition.commentary=!!G(e,s)}break;case 134:{if(!this.currentTrack)break;this.currentTrack.codecId=mr(e,s)}break;case 25506:{if(!this.currentTrack)break;this.currentTrack.codecPrivate=B(e,s)}break;case 2352003:{if(!this.currentTrack)break;this.currentTrack.defaultDurationNs=G(e,s)}break;case 21358:{if(!this.currentTrack)break;this.currentTrack.name=gi(e,s)}break;case 2274716:{if(!this.currentTrack||this.currentTrack.hasLanguageBcp47)break;this.currentTrack.languageCode=mr(e,s),Bt(this.currentTrack.languageCode)||(this.currentTrack.languageCode=J)}break;case 2274717:{if(!this.currentTrack)break;let u=mr(e,s).split("-")[0];u?this.currentTrack.languageCode=u:this.currentTrack.languageCode=J,this.currentTrack.hasLanguageBcp47=!0}break;case 224:{if(this.currentTrack?.info?.type!=="video")break;this.readContiguousElements(e.slice(o,s))}break;case 176:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.width=G(e,s)}break;case 186:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.height=G(e,s)}break;case 21680:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.displayWidth=G(e,s)}break;case 21690:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.displayHeight=G(e,s)}break;case 21682:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.displayUnit=G(e,s)}break;case 21440:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.alphaMode=G(e,s)===1}break;case 21936:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.colorSpace={},this.readContiguousElements(e.slice(o,s))}break;case 21937:{if(this.currentTrack?.info?.type!=="video"||!this.currentTrack.info.colorSpace)break;let c=G(e,s),u=Pr[c]??null;this.currentTrack.info.colorSpace.matrix=u}break;case 21945:{if(this.currentTrack?.info?.type!=="video"||!this.currentTrack.info.colorSpace)break;this.currentTrack.info.colorSpace.fullRange=G(e,s)===2}break;case 21946:{if(this.currentTrack?.info?.type!=="video"||!this.currentTrack.info.colorSpace)break;let c=G(e,s),u=xr[c]??null;this.currentTrack.info.colorSpace.transfer=u}break;case 21947:{if(this.currentTrack?.info?.type!=="video"||!this.currentTrack.info.colorSpace)break;let c=G(e,s),u=Ar[c]??null;this.currentTrack.info.colorSpace.primaries=u}break;case 30320:{if(this.currentTrack?.info?.type!=="video")break;this.readContiguousElements(e.slice(o,s))}break;case 30325:{if(this.currentTrack?.info?.type!=="video")break;let u=-Sa(e,s);try{this.currentTrack.info.rotation=Ze(u)}catch{}}break;case 225:{if(this.currentTrack?.info?.type!=="audio")break;this.readContiguousElements(e.slice(o,s))}break;case 181:{if(this.currentTrack?.info?.type!=="audio")break;this.currentTrack.info.sampleRate=Sa(e,s)}break;case 159:{if(this.currentTrack?.info?.type!=="audio")break;this.currentTrack.info.numberOfChannels=G(e,s)}break;case 25188:{if(this.currentTrack?.info?.type!=="audio")break;this.currentTrack.info.bitDepth=G(e,s)}break;case 187:{if(!this.currentSegment)break;this.readContiguousElements(e.slice(o,s)),this.currentCueTime=null}break;case 179:this.currentCueTime=G(e,s);break;case 183:{if(this.currentCueTime===null)break;h(this.currentSegment);let c={time:this.currentCueTime,trackId:-1,clusterPosition:-1};this.currentSegment.cuePoints.push(c),this.readContiguousElements(e.slice(o,s)),(c.trackId===-1||c.clusterPosition===-1)&&this.currentSegment.cuePoints.pop()}break;case 247:{let c=this.currentSegment?.cuePoints[this.currentSegment.cuePoints.length-1];if(!c)break;c.trackId=G(e,s)}break;case 241:{let c=this.currentSegment?.cuePoints[this.currentSegment.cuePoints.length-1];if(!c)break;h(this.currentSegment),c.clusterPosition=this.currentSegment.dataStartPos+G(e,s)}break;case 231:{if(!this.currentCluster)break;this.currentCluster.timestamp=G(e,s)}break;case 163:{if(!this.currentCluster)break;let c=hi(e);if(c===null)break;let u=this.getTrackDataInCluster(this.currentCluster,c);if(!u)break;let l=un(e),m=O(e),d=m>>1&3,f=!!(m&128);u.track.info?.type==="audio"&&u.track.info.codec&&(f=!0);let p=B(e,s-(e.filePos-o)),b=u.track.decodingInstructions.length>0;u.blocks.push({timestamp:l,duration:0,isKeyFrame:f,data:p,lacing:d,decoded:!b,mainAdditional:null})}break;case 160:{if(!this.currentCluster)break;this.readContiguousElements(e.slice(o,s)),this.currentBlock=null}break;case 161:{if(!this.currentCluster)break;let c=hi(e);if(c===null)break;let u=this.getTrackDataInCluster(this.currentCluster,c);if(!u)break;let l=un(e),d=O(e)>>1&3,f=B(e,s-(e.filePos-o)),p=u.track.decodingInstructions.length>0;this.currentBlock={timestamp:l,duration:0,isKeyFrame:!0,data:f,lacing:d,decoded:!p,mainAdditional:null},u.blocks.push(this.currentBlock)}break;case 30113:this.readContiguousElements(e.slice(o,s));break;case 166:{if(!this.currentBlock)break;this.currentBlockAdditional={addId:1,data:null},this.readContiguousElements(e.slice(o,s)),this.currentBlockAdditional.data&&this.currentBlockAdditional.addId===1&&(this.currentBlock.mainAdditional=this.currentBlockAdditional.data),this.currentBlockAdditional=null}break;case 165:{if(!this.currentBlockAdditional)break;this.currentBlockAdditional.data=B(e,s)}break;case 238:{if(!this.currentBlockAdditional)break;this.currentBlockAdditional.addId=G(e,s)}break;case 155:{if(!this.currentBlock)break;this.currentBlock.duration=G(e,s)}break;case 251:{if(!this.currentBlock)break;this.currentBlock.isKeyFrame=!1}break;case 29555:this.currentTagTargetIsMovie=!0,this.readContiguousElements(e.slice(o,s));break;case 25536:this.readContiguousElements(e.slice(o,s));break;case 26826:G(e,s)!==50&&(this.currentTagTargetIsMovie=!1);break;case 25541:case 25545:case 25540:case 25542:this.currentTagTargetIsMovie=!1;break;case 26568:{if(!this.currentTagTargetIsMovie)break;this.currentSimpleTagName=null,this.readContiguousElements(e.slice(o,s))}break;case 17827:this.currentSimpleTagName=gi(e,s);break;case 17543:{if(!this.currentSimpleTagName)break;let c=gi(e,s);this.processTagValue(this.currentSimpleTagName,c)}break;case 17541:{if(!this.currentSimpleTagName)break;let c=B(e,s);this.processTagValue(this.currentSimpleTagName,c)}break;case 24999:{if(!this.currentSegment)break;this.currentAttachedFile={fileUid:null,fileName:null,fileMediaType:null,fileData:null,fileDescription:null},this.readContiguousElements(e.slice(o,s));let c=this.currentSegment.metadataTags;if(this.currentAttachedFile.fileUid&&this.currentAttachedFile.fileData&&(c.raw??={},c.raw[this.currentAttachedFile.fileUid.toString()]=new ar(this.currentAttachedFile.fileData,this.currentAttachedFile.fileMediaType??void 0,this.currentAttachedFile.fileName??void 0,this.currentAttachedFile.fileDescription??void 0)),this.currentAttachedFile.fileMediaType?.startsWith("image/")&&this.currentAttachedFile.fileData){let u=this.currentAttachedFile.fileName,l="unknown";if(u){let m=u.toLowerCase();m.startsWith("cover.")?l="coverFront":m.startsWith("back.")&&(l="coverBack")}c.images??=[],c.images.push({data:this.currentAttachedFile.fileData,mimeType:this.currentAttachedFile.fileMediaType,kind:l,name:this.currentAttachedFile.fileName??void 0,description:this.currentAttachedFile.fileDescription??void 0})}this.currentAttachedFile=null}break;case 18094:{if(!this.currentAttachedFile)break;this.currentAttachedFile.fileUid=bl(e,s)}break;case 18030:{if(!this.currentAttachedFile)break;this.currentAttachedFile.fileName=gi(e,s)}break;case 18016:{if(!this.currentAttachedFile)break;this.currentAttachedFile.fileMediaType=mr(e,s)}break;case 18012:{if(!this.currentAttachedFile)break;this.currentAttachedFile.fileData=B(e,s)}break;case 18046:{if(!this.currentAttachedFile)break;this.currentAttachedFile.fileDescription=gi(e,s)}break;case 28032:{if(!this.currentTrack)break;this.readContiguousElements(e.slice(o,s)),this.currentTrack.decodingInstructions.sort((c,u)=>u.order-c.order)}break;case 25152:this.currentDecodingInstruction={order:0,scope:1,data:null},this.readContiguousElements(e.slice(o,s)),this.currentDecodingInstruction.data&&this.currentTrack.decodingInstructions.push(this.currentDecodingInstruction),this.currentDecodingInstruction=null;break;case 20529:{if(!this.currentDecodingInstruction)break;this.currentDecodingInstruction.order=G(e,s)}break;case 20530:{if(!this.currentDecodingInstruction)break;this.currentDecodingInstruction.scope=G(e,s)}break;case 20532:{if(!this.currentDecodingInstruction)break;this.currentDecodingInstruction.data={type:"decompress",algorithm:0,settings:null},this.readContiguousElements(e.slice(o,s))}break;case 16980:{if(this.currentDecodingInstruction?.data?.type!=="decompress")break;this.currentDecodingInstruction.data.algorithm=G(e,s)}break;case 16981:{if(this.currentDecodingInstruction?.data?.type!=="decompress")break;this.currentDecodingInstruction.data.settings=B(e,s)}break;case 20533:{if(!this.currentDecodingInstruction)break;this.currentDecodingInstruction.data={type:"decrypt"}}break}return e.filePos=o+s,!0}decodeBlockData(e,r){h(e.decodingInstructions.length>0);let i=r;for(let a of e.decodingInstructions)switch(h(a.data),a.data.type){case"decompress":switch(a.data.algorithm){case 3:if(a.data.settings&&a.data.settings.length>0){let s=a.data.settings,o=new Uint8Array(s.length+i.length);o.set(s,0),o.set(i,s.length),i=o}break;default:}break;default:}return i}processTagValue(e,r){if(!this.currentSegment?.metadataTags)return;let i=this.currentSegment.metadataTags;if(i.raw??={},i.raw[e]??=r,typeof r=="string")switch(e.toLowerCase()){case"title":i.title??=r;break;case"description":i.description??=r;break;case"artist":i.artist??=r;break;case"album":i.album??=r;break;case"album_artist":i.albumArtist??=r;break;case"genre":i.genre??=r;break;case"comment":i.comment??=r;break;case"lyrics":i.lyrics??=r;break;case"date":{let a=new Date(r);Number.isNaN(a.getTime())||(i.date??=a)}break;case"track_number":case"part_number":{let a=r.split("/"),s=Number.parseInt(a[0],10),o=a[1]&&Number.parseInt(a[1],10);Number.isInteger(s)&&s>0&&(i.trackNumber??=s),o&&Number.isInteger(o)&&o>0&&(i.tracksTotal??=o)}break;case"disc_number":case"disc":{let a=r.split("/"),s=Number.parseInt(a[0],10),o=a[1]&&Number.parseInt(a[1],10);Number.isInteger(s)&&s>0&&(i.discNumber??=s),o&&Number.isInteger(o)&&o>0&&(i.discsTotal??=o)}break}}},Ca=class{constructor(t){this.internalTrack=t;this.packetToClusterLocation=new WeakMap}getId(){return this.internalTrack.id}getNumber(){let t=this.internalTrack.demuxer,e=this.internalTrack.trackBacking.getType(),r=0;for(let i of t.segments)for(let a of i.tracks)if(a.trackBacking.getType()===e&&r++,a===this.internalTrack)break;return r}getCodec(){throw new Error("Not implemented on base class.")}getInternalCodecId(){return this.internalTrack.codecId}getName(){return this.internalTrack.name}getLanguageCode(){return this.internalTrack.languageCode}getTimeResolution(){return this.internalTrack.segment.timestampFactor}isRelativeToUnixEpoch(){return!1}getDisposition(){return this.internalTrack.disposition}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){let t=this.internalTrack.segment;if(t.duration<=0)return null;let e=t.duration/t.timestampFactor,r=await this.getFirstPacket({metadataOnly:!0});return e+=r?.timestamp??0,e}async getLiveRefreshInterval(){return null}async getFirstPacket(t){return this.performClusterLookup(null,e=>e.trackData.get(this.internalTrack.id)?{blockIndex:0,correctBlockFound:!0}:{blockIndex:-1,correctBlockFound:!1},-1/0,1/0,t)}intoTimescale(t){return Mt(t*this.internalTrack.segment.timestampFactor)}async getPacket(t,e){let r=this.intoTimescale(t);return this.performClusterLookup(null,i=>{let a=i.trackData.get(this.internalTrack.id);if(!a)return{blockIndex:-1,correctBlockFound:!1};let s=H(a.presentationTimestamps,r,u=>u.timestamp),o=s!==-1?a.presentationTimestamps[s].blockIndex:-1,c=s!==-1&&r<a.endTimestamp;return{blockIndex:o,correctBlockFound:c}},r,r,e)}async getNextPacket(t,e){let r=this.packetToClusterLocation.get(t);if(r===void 0)throw new Error("Packet was not created from this track.");return this.performClusterLookup(r.cluster,i=>{if(i===r.cluster){let a=i.trackData.get(this.internalTrack.id);if(r.blockIndex+1<a.blocks.length)return{blockIndex:r.blockIndex+1,correctBlockFound:!0}}else if(i.trackData.get(this.internalTrack.id))return{blockIndex:0,correctBlockFound:!0};return{blockIndex:-1,correctBlockFound:!1}},-1/0,1/0,e)}async getKeyPacket(t,e){let r=this.intoTimescale(t);return this.performClusterLookup(null,i=>{let a=i.trackData.get(this.internalTrack.id);if(!a)return{blockIndex:-1,correctBlockFound:!1};let s=rr(a.presentationTimestamps,u=>a.blocks[u.blockIndex].isKeyFrame&&u.timestamp<=r),o=s!==-1?a.presentationTimestamps[s].blockIndex:-1,c=s!==-1&&r<a.endTimestamp;return{blockIndex:o,correctBlockFound:c}},r,r,e)}async getNextKeyPacket(t,e){let r=this.packetToClusterLocation.get(t);if(r===void 0)throw new Error("Packet was not created from this track.");return this.performClusterLookup(r.cluster,i=>{if(i===r.cluster){let s=i.trackData.get(this.internalTrack.id).blocks.findIndex((o,c)=>o.isKeyFrame&&c>r.blockIndex);if(s!==-1)return{blockIndex:s,correctBlockFound:!0}}else{let a=i.trackData.get(this.internalTrack.id);if(a&&a.firstKeyFrameTimestamp!==null){let s=a.blocks.findIndex(o=>o.isKeyFrame);return h(s!==-1),{blockIndex:s,correctBlockFound:!0}}}return{blockIndex:-1,correctBlockFound:!1}},-1/0,1/0,e)}async fetchPacketInCluster(t,e,r){if(e===-1)return null;let a=t.trackData.get(this.internalTrack.id).blocks[e];h(a),a.decoded||(a.data=this.internalTrack.demuxer.decodeBlockData(this.internalTrack,a.data),a.decoded=!0);let s=r.metadataOnly?de:a.data,o=a.timestamp/this.internalTrack.segment.timestampFactor,c=a.duration/this.internalTrack.segment.timestampFactor,u={};a.mainAdditional&&this.internalTrack.info?.type==="video"&&this.internalTrack.info.alphaMode&&(u.alpha=r.metadataOnly?de:a.mainAdditional,u.alphaByteLength=a.mainAdditional.byteLength);let l=new Q(s,a.isKeyFrame?"key":"delta",o,c,t.dataStartPos+e,a.data.byteLength,u);return this.packetToClusterLocation.set(l,{cluster:t,blockIndex:e}),l}async performClusterLookup(t,e,r,i,a){let{demuxer:s,segment:o}=this.internalTrack,c=null,u=null,l=-1;if(t){let{blockIndex:y,correctBlockFound:k}=e(t);if(k)return this.fetchPacketInCluster(t,y,a);y!==-1&&(u=t,l=y)}let m=H(this.internalTrack.cuePoints,r,y=>y.time),d=m!==-1?this.internalTrack.cuePoints[m]:null,f=H(this.internalTrack.clusterPositionCache,r,y=>y.startTimestamp),p=f!==-1?this.internalTrack.clusterPositionCache[f]:null,b=Math.max(d?.clusterPosition??0,p?.elementStartPos??0)||null,g;for(t?b===null||t.elementStartPos>=b?(g=t.elementEndPos,c=t):g=b:g=b??o.clusterSeekStartPos;o.elementEndPos===null||g<=o.elementEndPos-Oe;){if(c){let A=c.trackData.get(this.internalTrack.id);if(A&&A.startTimestamp>i)break}let y=s.reader.requestSliceRange(g,Oe,dt);if(y instanceof Promise&&(y=await y),!y)break;let k=g,T=mt(y);if(!T||!pi.includes(T.id)&&T.id!==236){let A=await Co(s.reader,k,pi,Math.min(o.elementEndPos??1/0,k+Tl));if(A){g=A;continue}else break}let w=T.id,S=T.size,x=y.filePos;if(w===524531317){c=await s.readCluster(k,o),S=c.elementEndPos-x;let{blockIndex:A,correctBlockFound:C}=e(c);if(C)return this.fetchPacketInCluster(c,A,a);A!==-1&&(u=c,l=A)}S===void 0&&(h(w!==524531317),S=(await Aa(s.reader,x,dn,o.elementEndPos)).pos-x);let P=x+S;if(o.elementEndPos===null){let A=s.reader.requestSliceRange(P,Oe,dt);if(A instanceof Promise&&(A=await A),!A)break;if(wa(A)===408125543){o.elementEndPos=P;break}}g=P}if(d&&(!u||u.elementStartPos<d.clusterPosition)){let y=this.internalTrack.cuePoints[m-1];h(!y||y.time<d.time);let k=y?.time??-1/0;return this.performClusterLookup(null,e,k,i,a)}return u?this.fetchPacketInCluster(u,l,a):null}},Eo=class extends Ca{constructor(e){super(e);this.decoderConfigPromise=null;this.internalTrack=e}getType(){return"video"}getCodec(){return this.internalTrack.info.codec}getCodedWidth(){return this.internalTrack.info.width}getCodedHeight(){return this.internalTrack.info.height}getSquarePixelWidth(){return this.internalTrack.info.squarePixelWidth}getSquarePixelHeight(){return this.internalTrack.info.squarePixelHeight}getRotation(){return this.internalTrack.info.rotation}async getColorSpace(){return{primaries:this.internalTrack.info.colorSpace?.primaries,transfer:this.internalTrack.info.colorSpace?.transfer,matrix:this.internalTrack.info.colorSpace?.matrix,fullRange:this.internalTrack.info.colorSpace?.fullRange}}async canBeTransparent(){return this.internalTrack.info.alphaMode}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfigPromise??=(async()=>{let e=null;(this.internalTrack.info.codec==="vp9"||this.internalTrack.info.codec==="av1"||this.internalTrack.info.codec==="avc"&&!this.internalTrack.info.codecDescription||this.internalTrack.info.codec==="hevc"&&!this.internalTrack.info.codecDescription)&&(e=await this.getFirstPacket({}));let i={codec:ri({width:this.internalTrack.info.width,height:this.internalTrack.info.height,codec:this.internalTrack.info.codec,codecDescription:this.internalTrack.info.codecDescription,colorSpace:this.internalTrack.info.colorSpace,avcType:1,avcCodecInfo:this.internalTrack.info.codec==="avc"&&e?ai(e.data):null,hevcCodecInfo:this.internalTrack.info.codec==="hevc"&&e?si(e.data):null,vp9CodecInfo:this.internalTrack.info.codec==="vp9"&&e?ia(e.data):null,av1CodecInfo:this.internalTrack.info.codec==="av1"&&e?na(e.data):null}),codedWidth:this.internalTrack.info.width,codedHeight:this.internalTrack.info.height,description:this.internalTrack.info.codecDescription??void 0,colorSpace:this.internalTrack.info.colorSpace??void 0};return(this.internalTrack.info.width!==this.internalTrack.info.squarePixelWidth||this.internalTrack.info.height!==this.internalTrack.info.squarePixelHeight)&&(i.displayAspectWidth=this.internalTrack.info.squarePixelWidth,i.displayAspectHeight=this.internalTrack.info.squarePixelHeight),i})():null}},vo=class extends Ca{constructor(e){super(e);this.decoderConfig=null;this.internalTrack=e}getType(){return"audio"}getCodec(){return this.internalTrack.info.codec}getNumberOfChannels(){return this.internalTrack.info.numberOfChannels}getSampleRate(){return this.internalTrack.info.sampleRate}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfig??={codec:ii({codec:this.internalTrack.info.codec,codecDescription:this.internalTrack.info.codecDescription,aacCodecInfo:this.internalTrack.info.aacCodecInfo}),numberOfChannels:this.internalTrack.info.numberOfChannels,sampleRate:this.internalTrack.info.sampleRate,description:this.internalTrack.info.codecDescription??void 0}:null}};var mn=async(n,t,e)=>{let i=t;for(;e===null||i<e;){let a=e!==null?Math.min(65536,e-i):65536,s=n.requestSliceRange(i,4,a);if(s instanceof Promise&&(s=await s),!s||s.length<4)break;for(;s.remainingLength>=4;){let o=s.filePos,c=F(s),u=n.fileSize!==null?n.fileSize-i:null,l=Mr(c,u);if(l.header)return{header:l.header,startPos:i};s.filePos=o+l.bytesAdvanced,i=s.filePos}}return null};var Ia=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.firstFrameHeader=null;this.firstFrameHeaderPos=null;this.loadedSamples=[];this.metadataTags=null;this.xingData=null;this.trackBackings=[];this.readingMutex=new Ie;this.lastSampleLoaded=!1;this.lastLoadedPos=0;this.nextTimestampInSamples=0;this.reader=e._reader}async readMetadata(){return this.metadataPromise??=(async()=>{for(;!this.firstFrameHeader&&!this.lastSampleLoaded;)await this.advanceReader();if(!this.firstFrameHeader)throw new Error("No valid MP3 frame found.");this.trackBackings=[new _o(this)]})()}async advanceReader(){if(this.lastLoadedPos===0)for(;;){let c=this.reader.requestSlice(this.lastLoadedPos,ze);if(c instanceof Promise&&(c=await c),!c){this.lastSampleLoaded=!0;return}let u=ft(c);if(!u)break;this.lastLoadedPos=c.filePos+u.size}let e=await mn(this.reader,this.lastLoadedPos,this.reader.fileSize);if(!e){this.lastSampleLoaded=!0;return}let r=e.header;this.lastLoadedPos=e.startPos+r.totalSize-1;let i=cr(r.mpegVersionId,r.channel),a=this.reader.requestSlice(e.startPos+i,4);if(a instanceof Promise&&(a=await a),a){let c=F(a);if(c===or||c===ni){if(!this.xingData){let l=this.reader.requestSlice(e.startPos+i+4,12);if(l instanceof Promise&&(l=await l),l){let m=B(l,12),d=V(m),f=d.getUint32(0,!1);this.xingData={frameCount:f&1?d.getUint32(4,!1):null,fileSize:f&2?d.getUint32(8,!1):null}}}return}}this.firstFrameHeader||(this.firstFrameHeader=r,this.firstFrameHeaderPos=e.startPos),r.sampleRate!==this.firstFrameHeader.sampleRate&&console.warn(`MP3 changed sample rate mid-file: ${this.firstFrameHeader.sampleRate} Hz to ${r.sampleRate} Hz. Might be a bug, so please report this file.`);let s=r.audioSamplesInFrame/this.firstFrameHeader.sampleRate,o={timestamp:this.nextTimestampInSamples/this.firstFrameHeader.sampleRate,duration:s,dataStart:e.startPos,dataSize:r.totalSize};this.loadedSamples.push(o),this.nextTimestampInSamples+=r.audioSamplesInFrame}async getMimeType(){return"audio/mpeg"}async getTrackBackings(){return await this.readMetadata(),this.trackBackings}async getMetadataTags(){let e=await this.readingMutex.acquire();try{if(await this.readMetadata(),this.metadataTags)return this.metadataTags;this.metadataTags={};let r=0,i=!1;for(;;){let a=this.reader.requestSlice(r,ze);if(a instanceof Promise&&(a=await a),!a)break;let s=ft(a);if(!s)break;i=!0;let o=this.reader.requestSlice(a.filePos,s.size);if(o instanceof Promise&&(o=await o),!o)break;bi(o,s,this.metadataTags),r=a.filePos+s.size}if(!i&&this.reader.fileSize!==null&&this.reader.fileSize>=fn){let a=this.reader.requestSlice(this.reader.fileSize-fn,fn);a instanceof Promise&&(a=await a),h(a),te(a,3)==="TAG"&&wl(a,this.metadataTags)}return this.metadataTags}finally{e()}}},_o=class{constructor(t){this.demuxer=t}getType(){return"audio"}getId(){return 1}getNumber(){return 1}getTimeResolution(){return h(this.demuxer.firstFrameHeader),this.demuxer.firstFrameHeader.sampleRate/this.demuxer.firstFrameHeader.audioSamplesInFrame}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){let t=this.demuxer;if(h(t.firstFrameHeader!==null),h(t.firstFrameHeaderPos!==null),t.xingData){if(t.xingData.frameCount!==null)return t.xingData.frameCount*t.firstFrameHeader.audioSamplesInFrame/t.firstFrameHeader.sampleRate}else if(t.reader.fileSize!==null){let e=Wu(t.firstFrameHeader.lowSamplingFrequency,t.firstFrameHeader.layer,t.firstFrameHeader.bitrate,t.firstFrameHeader.sampleRate),r=(t.reader.fileSize-t.firstFrameHeaderPos)/e;return Math.round(r)*t.firstFrameHeader.audioSamplesInFrame/t.firstFrameHeader.sampleRate}return null}async getLiveRefreshInterval(){return null}getName(){return null}getLanguageCode(){return J}getCodec(){return"mp3"}getInternalCodecId(){return null}getNumberOfChannels(){return h(this.demuxer.firstFrameHeader),this.demuxer.firstFrameHeader.channel===3?1:2}getSampleRate(){return h(this.demuxer.firstFrameHeader),this.demuxer.firstFrameHeader.sampleRate}getDisposition(){return{...we}}async getDecoderConfig(){return h(this.demuxer.firstFrameHeader),{codec:"mp3",numberOfChannels:this.demuxer.firstFrameHeader.channel===3?1:2,sampleRate:this.demuxer.firstFrameHeader.sampleRate}}async getPacketAtIndex(t,e){if(t===-1)return null;let r=this.demuxer.loadedSamples[t];if(!r)return null;let i;if(e.metadataOnly)i=de;else{let a=this.demuxer.reader.requestSlice(r.dataStart,r.dataSize);if(a instanceof Promise&&(a=await a),!a)return null;i=B(a,r.dataSize)}return new Q(i,"key",r.timestamp,r.duration,t,r.dataSize)}getFirstPacket(t){return this.getPacketAtIndex(0,t)}async getNextPacket(t,e){let r=await this.demuxer.readingMutex.acquire();try{let i=Ft(this.demuxer.loadedSamples,t.timestamp,s=>s.timestamp);if(i===-1)throw new Error("Packet was not created from this track.");let a=i+1;for(;a>=this.demuxer.loadedSamples.length&&!this.demuxer.lastSampleLoaded;)await this.demuxer.advanceReader();return this.getPacketAtIndex(a,e)}finally{r()}}async getPacket(t,e){let r=await this.demuxer.readingMutex.acquire();try{for(;;){let i=H(this.demuxer.loadedSamples,t,a=>a.timestamp);if(i===-1&&this.demuxer.loadedSamples.length>0)return null;if(this.demuxer.lastSampleLoaded)return this.getPacketAtIndex(i,e);if(i>=0&&i+1<this.demuxer.loadedSamples.length)return this.getPacketAtIndex(i,e);await this.demuxer.advanceReader()}}finally{r()}}getKeyPacket(t,e){return this.getPacket(t,e)}getNextKeyPacket(t,e){return this.getNextPacket(t,e)}};var pn=1399285583,_m=79764919,Sl=new Uint32Array(256);for(let n=0;n<256;n++){let t=n<<24;for(let e=0;e<8;e++)t=t&2147483648?t<<1^_m:t<<1;Sl[n]=t>>>0&4294967295}var Ea=n=>{let t=V(n),e=t.getUint32(22,!0);t.setUint32(22,0,!0);let r=0;for(let i=0;i<n.length;i++){let a=n[i];r=(r<<8^Sl[r>>>24^a])>>>0}return t.setUint32(22,e,!0),r},va=(n,t,e)=>{let r=0,i=null;if(n.length>0)if(t.codec==="vorbis"){h(t.vorbisInfo);let a=t.vorbisInfo.modeBlockflags.length,o=(1<<ku(a-1))-1<<1,c=(n[0]&o)>>1;if(c>=t.vorbisInfo.modeBlockflags.length)throw new Error("Invalid mode number.");let u=e,l=t.vorbisInfo.modeBlockflags[c];if(i=t.vorbisInfo.blocksizes[l],l===1){let m=(o|1)+1,d=n[0]&m?1:0;u=t.vorbisInfo.blocksizes[d]}r=u!==null?u+i>>2:0}else t.codec==="opus"&&(r=Ju(n).durationInSamples);return{durationInSamples:r,vorbisBlockSize:i}},_a=n=>{let t="audio/ogg";if(n.codecStrings){let e=[...new Set(n.codecStrings)];t+=`; codecs="${e.join(", ")}"`}return t};var fr=27,Dr=282,Ra=Dr+255*255,yi=n=>{let t=n.filePos;if(Or(n)!==pn)return null;n.skip(1);let r=O(n),i=xl(n),a=Or(n),s=Or(n),o=Or(n),c=O(n),u=new Uint8Array(c);for(let f=0;f<c;f++)u[f]=O(n);let l=27+c,m=u.reduce((f,p)=>f+p,0),d=l+m;return{headerStartPos:t,totalSize:d,dataStartPos:t+l,dataSize:m,headerType:r,granulePosition:i,serialNumber:a,sequenceNumber:s,checksum:o,lacingValues:u}},Al=(n,t)=>{for(;n.filePos<t-3;){let e=Or(n),r=e&255,i=e>>>8&255,a=e>>>16&255,s=e>>>24&255,o=79;if(!(r!==o&&i!==o&&a!==o&&s!==o)){if(n.skip(-4),e===pn)return!0;n.skip(1)}}return!1};var Fa=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.bitstreams=[];this.trackBackings=[];this.metadataTags={};this.reader=e._reader}async readMetadata(){return this.metadataPromise??=(async()=>{let e=0;for(;;){let r=this.reader.requestSliceRange(e,fr,Dr);if(r instanceof Promise&&(r=await r),!r)break;let i=yi(r);if(!i||!!!(i.headerType&2))break;this.bitstreams.push({serialNumber:i.serialNumber,bosPage:i,description:null,numberOfChannels:-1,sampleRate:-1,codecInfo:{codec:null,vorbisInfo:null,opusInfo:null},lastMetadataPacket:null}),e=i.headerStartPos+i.totalSize}for(let r of this.bitstreams){let i=await this.readPacket(r.bosPage,0);i&&(i.data.byteLength>=7&&i.data[0]===1&&i.data[1]===118&&i.data[2]===111&&i.data[3]===114&&i.data[4]===98&&i.data[5]===105&&i.data[6]===115?await this.readVorbisMetadata(i,r):i.data.byteLength>=8&&i.data[0]===79&&i.data[1]===112&&i.data[2]===117&&i.data[3]===115&&i.data[4]===72&&i.data[5]===101&&i.data[6]===97&&i.data[7]===100&&await this.readOpusMetadata(i,r),r.codecInfo.codec!==null&&this.trackBackings.push(new Ro(r,this)))}})()}async readVorbisMetadata(e,r){let i=await this.findNextPacketStart(e);if(!i)return;let a=await this.readPacket(i.startPage,i.startSegmentIndex);if(!a||(i=await this.findNextPacketStart(a),!i))return;let s=await this.readPacket(i.startPage,i.startSegmentIndex);if(!s||a.data[0]!==3||s.data[0]!==5)return;let o=[],c=d=>{for(;o.push(Math.min(255,d)),!(d<255);)d-=255};c(e.data.length),c(a.data.length);let u=new Uint8Array(1+o.length+e.data.length+a.data.length+s.data.length);u[0]=2,u.set(o,1),u.set(e.data,1+o.length),u.set(a.data,1+o.length+e.data.length),u.set(s.data,1+o.length+e.data.length+a.data.length),r.codecInfo.codec="vorbis",r.description=u,r.lastMetadataPacket=s;let l=V(e.data);r.numberOfChannels=l.getUint8(11),r.sampleRate=l.getUint32(12,!0);let m=l.getUint8(28);r.codecInfo.vorbisInfo={blocksizes:[1<<(m&15),1<<(m>>4)],modeBlockflags:aa(s.data).modeBlockflags},rn(a.data.subarray(7),this.metadataTags)}async readOpusMetadata(e,r){let i=await this.findNextPacketStart(e);if(!i)return;let a=await this.readPacket(i.startPage,i.startSegmentIndex);if(!a)return;r.codecInfo.codec="opus",r.description=e.data,r.lastMetadataPacket=a;let s=lr(e.data);r.numberOfChannels=s.outputChannelCount,r.sampleRate=nt,r.codecInfo.opusInfo={preSkip:s.preSkip},rn(a.data.subarray(8),this.metadataTags)}async readPacket(e,r){h(r<e.lacingValues.length);let i=0;for(let d=0;d<r;d++)i+=e.lacingValues[d];let a=e,s=i,o=r,c=[];e:for(;;){let d=this.reader.requestSlice(a.dataStartPos,a.dataSize);d instanceof Promise&&(d=await d),h(d);let f=B(d,a.dataSize);for(;;){if(o===a.lacingValues.length){c.push(f.subarray(i,s));break}let b=a.lacingValues[o];if(s+=b,b<255){c.push(f.subarray(i,s));break e}o++}let p=a.headerStartPos+a.totalSize;for(;;){let b=this.reader.requestSliceRange(p,fr,Dr);if(b instanceof Promise&&(b=await b),!b)return null;let g=yi(b);if(!g)return null;if(a=g,a.serialNumber===e.serialNumber)break;p=a.headerStartPos+a.totalSize}i=0,s=0,o=0}let u=c.reduce((d,f)=>d+f.length,0);if(u===0)return null;let l=new Uint8Array(u),m=0;for(let d=0;d<c.length;d++){let f=c[d];l.set(f,m),m+=f.length}return{data:l,endPage:a,endSegmentIndex:o}}async findNextPacketStart(e){if(e.endSegmentIndex<e.endPage.lacingValues.length-1)return{startPage:e.endPage,startSegmentIndex:e.endSegmentIndex+1};if(!!(e.endPage.headerType&4))return null;let i=e.endPage.headerStartPos+e.endPage.totalSize;for(;;){let a=this.reader.requestSliceRange(i,fr,Dr);if(a instanceof Promise&&(a=await a),!a)return null;let s=yi(a);if(!s)return null;if(s.serialNumber===e.endPage.serialNumber)return{startPage:s,startSegmentIndex:0};i=s.headerStartPos+s.totalSize}}async getMimeType(){await this.readMetadata();let e=await Promise.all(this.trackBackings.map(r=>r.getDecoderConfig().then(i=>i?.codec??null)));return _a({codecStrings:e.filter(Boolean)})}async getTrackBackings(){return await this.readMetadata(),this.trackBackings}async getMetadataTags(){return await this.readMetadata(),this.metadataTags}},Ro=class{constructor(t,e){this.bitstream=t;this.demuxer=e;this.encodedPacketToMetadata=new WeakMap;this.sequentialScanCache=[];this.sequentialScanMutex=new Ie;this.internalSampleRate=t.codecInfo.codec==="opus"?nt:t.sampleRate}getType(){return"audio"}getId(){return this.bitstream.serialNumber}getNumber(){let t=this.demuxer.trackBackings.findIndex(e=>e.bitstream===this.bitstream);return h(t!==-1),t+1}getNumberOfChannels(){return this.bitstream.numberOfChannels}getSampleRate(){return this.bitstream.sampleRate}getTimeResolution(){return this.bitstream.sampleRate}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){return null}async getLiveRefreshInterval(){return null}getCodec(){return this.bitstream.codecInfo.codec}getInternalCodecId(){return null}async getDecoderConfig(){return h(this.bitstream.codecInfo.codec),{codec:this.bitstream.codecInfo.codec,numberOfChannels:this.bitstream.numberOfChannels,sampleRate:this.bitstream.sampleRate,description:this.bitstream.description??void 0}}getName(){return null}getLanguageCode(){return J}getDisposition(){return{...we,primary:!1}}granulePositionToTimestampInSamples(t){return this.bitstream.codecInfo.codec==="opus"?(h(this.bitstream.codecInfo.opusInfo),t-this.bitstream.codecInfo.opusInfo.preSkip):t}createEncodedPacketFromOggPacket(t,e,r){if(!t)return null;let{durationInSamples:i,vorbisBlockSize:a}=va(t.data,this.bitstream.codecInfo,e.vorbisLastBlocksize),s=new Q(r.metadataOnly?de:t.data,"key",Math.max(0,e.timestampInSamples)/this.internalSampleRate,i/this.internalSampleRate,t.endPage.headerStartPos+t.endSegmentIndex,t.data.byteLength);return this.encodedPacketToMetadata.set(s,{packet:t,timestampInSamples:e.timestampInSamples,durationInSamples:i,vorbisLastBlockSize:e.vorbisLastBlocksize,vorbisBlockSize:a}),s}async getFirstPacket(t){h(this.bitstream.lastMetadataPacket);let e=await this.demuxer.findNextPacketStart(this.bitstream.lastMetadataPacket);if(!e)return null;let r=0;this.bitstream.codecInfo.codec==="opus"&&(h(this.bitstream.codecInfo.opusInfo),r-=this.bitstream.codecInfo.opusInfo.preSkip);let i=await this.demuxer.readPacket(e.startPage,e.startSegmentIndex);return this.createEncodedPacketFromOggPacket(i,{timestampInSamples:r,vorbisLastBlocksize:null},t)}async getNextPacket(t,e){let r=this.encodedPacketToMetadata.get(t);if(!r)throw new Error("Packet was not created from this track.");let i=await this.demuxer.findNextPacketStart(r.packet);if(!i)return null;let a=r.timestampInSamples+r.durationInSamples,s=await this.demuxer.readPacket(i.startPage,i.startSegmentIndex);return this.createEncodedPacketFromOggPacket(s,{timestampInSamples:a,vorbisLastBlocksize:r.vorbisBlockSize},e)}async getPacket(t,e){if(this.demuxer.reader.fileSize===null)return this.getPacketSequential(t,e);let r=Mt(t*this.internalSampleRate);if(r===0)return this.getFirstPacket(e);if(r<0)return null;h(this.bitstream.lastMetadataPacket);let i=await this.demuxer.findNextPacketStart(this.bitstream.lastMetadataPacket);if(!i)return null;let a=i.startPage,s=this.demuxer.reader.fileSize,o=[a];e:for(;a.headerStartPos+a.totalSize<s;){let k=a.headerStartPos,T=Math.floor((k+s)/2),w=T;for(;;){let S=Math.min(w+Ra,s-fr),x=this.demuxer.reader.requestSlice(w,S-w);if(x instanceof Promise&&(x=await x),h(x),!Al(x,S)){s=T+fr;continue e}let A=this.demuxer.reader.requestSliceRange(x.filePos,fr,Dr);A instanceof Promise&&(A=await A),h(A);let C=yi(A);h(C);let I=!1;if(C.serialNumber===this.bitstream.serialNumber)I=!0;else{let v=this.demuxer.reader.requestSlice(C.headerStartPos,C.totalSize);v instanceof Promise&&(v=await v),h(v);let E=B(v,C.totalSize);I=Ea(E)===C.checksum}if(!I){w=C.headerStartPos+4;continue}if(I&&C.serialNumber!==this.bitstream.serialNumber){w=C.headerStartPos+C.totalSize;continue}if(C.granulePosition===-1){w=C.headerStartPos+C.totalSize;continue}this.granulePositionToTimestampInSamples(C.granulePosition)>r?s=C.headerStartPos:(a=C,o.push(C));continue e}}let c=i.startPage;for(let k of o){if(k.granulePosition===a.granulePosition)break;(!c||k.headerStartPos>c.headerStartPos)&&(c=k)}let u=c,l=[u];for(;!(u.serialNumber===this.bitstream.serialNumber&&u.granulePosition===a.granulePosition);){let k=u.headerStartPos+u.totalSize,T=this.demuxer.reader.requestSliceRange(k,fr,Dr);T instanceof Promise&&(T=await T),h(T);let w=yi(T);h(w),u=w,u.serialNumber===this.bitstream.serialNumber&&l.push(u)}h(u.granulePosition!==-1);let m=null,d,f,p=u,b=0;if(u.headerStartPos===i.startPage.headerStartPos)d=this.granulePositionToTimestampInSamples(0),f=!0,m=0;else{d=0,f=!1;for(let w=u.lacingValues.length-1;w>=0;w--)if(u.lacingValues[w]<255){m=w+1;break}if(m===null)throw new Error("Invalid page with granule position: no packets end on this page.");b=m-1;let k={data:de,endPage:p,endSegmentIndex:b};if(await this.demuxer.findNextPacketStart(k)){let w=Cl(l,u,m);h(w);let S=Pl(l,w.page,w.segmentIndex);S&&(u=S.page,m=S.segmentIndex)}else for(;;){let w=Cl(l,u,m);if(!w)break;let S=Pl(l,w.page,w.segmentIndex);if(!S)break;if(u=S.page,m=S.segmentIndex,w.page.headerStartPos!==p.headerStartPos){p=w.page,b=w.segmentIndex;break}}}let g=null,y=null;for(;u!==null;){h(m!==null);let k=await this.demuxer.readPacket(u,m);if(!k)break;if(!(u.headerStartPos===i.startPage.headerStartPos&&m<i.startSegmentIndex)){let S=this.createEncodedPacketFromOggPacket(k,{timestampInSamples:d,vorbisLastBlocksize:y?.vorbisBlockSize??null},e);h(S);let x=this.encodedPacketToMetadata.get(S);if(h(x),!f&&k.endPage.headerStartPos===p.headerStartPos&&k.endSegmentIndex===b?(d=this.granulePositionToTimestampInSamples(u.granulePosition),f=!0,S=this.createEncodedPacketFromOggPacket(k,{timestampInSamples:d-x.durationInSamples,vorbisLastBlocksize:y?.vorbisBlockSize??null},e),h(S),x=this.encodedPacketToMetadata.get(S),h(x)):d+=x.durationInSamples,g=S,y=x,f&&(Math.max(d,0)>r||Math.max(x.timestampInSamples,0)===r))break}let w=await this.demuxer.findNextPacketStart(k);if(!w)break;u=w.startPage,m=w.startSegmentIndex}return g}async getPacketSequential(t,e){let r=await this.sequentialScanMutex.acquire();try{let i=Mt(t*this.internalSampleRate);t=i/this.internalSampleRate;let a=H(this.sequentialScanCache,i,c=>c.timestampInSamples),s;if(a!==-1){let c=this.sequentialScanCache[a];s=this.createEncodedPacketFromOggPacket(c.packet,{timestampInSamples:c.timestampInSamples,vorbisLastBlocksize:c.vorbisLastBlockSize},e)}else s=await this.getFirstPacket(e);let o=0;for(;s&&s.timestamp<t;){let c=await this.getNextPacket(s,e);if(!c||c.timestamp>t)break;if(s=c,o++,o===100){o=0;let u=this.encodedPacketToMetadata.get(s);h(u),this.sequentialScanCache.length>0&&h(X(this.sequentialScanCache).timestampInSamples<=u.timestampInSamples),this.sequentialScanCache.push(u)}}return s}finally{r()}}getKeyPacket(t,e){return this.getPacket(t,e)}getNextKeyPacket(t,e){return this.getNextPacket(t,e)}},Pl=(n,t,e)=>{let r=t,i=e;e:for(;;){for(i--,i;i>=0;i--)if(r.lacingValues[i]<255){i++;break e}if(h(i===-1),!(r.headerType&1)){i=0;break}let s=mo(n,o=>o.headerStartPos<r.headerStartPos);if(!s)return null;r=s,i=r.lacingValues.length}if(h(i!==-1),i===r.lacingValues.length){let a=n[n.indexOf(r)+1];h(a),r=a,i=0}return{page:r,segmentIndex:i}},Cl=(n,t,e)=>{if(e>0)return{page:t,segmentIndex:e-1};let r=mo(n,i=>i.headerStartPos<t.headerStartPos);return r?{page:r,segmentIndex:r.lacingValues.length-1}:null};var Ma=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.dataStart=-1;this.dataSize=-1;this.audioInfo=null;this.trackBackings=[];this.lastKnownPacketIndex=0;this.metadataTags={};this.reader=e._reader}async readMetadata(){return this.metadataPromise??=(async()=>{let e=this.reader.requestSlice(0,12);e instanceof Promise&&(e=await e),h(e);let r=te(e,4),i=r!=="RIFX",a=r==="RF64",s=Kt(e,i),o=a?this.reader.fileSize:Math.min(s+8,this.reader.fileSize??1/0);if(te(e,4)!=="WAVE")throw new Error("Invalid WAVE file - wrong format");let u=0,l=null,m=e.filePos;for(;o===null||m<o;){let f=this.reader.requestSlice(m,8);if(f instanceof Promise&&(f=await f),!f)break;let p=te(f,4),b=Kt(f,i),g=f.filePos;if(a&&u===0&&p!=="ds64")throw new Error('Invalid RF64 file: First chunk must be "ds64".');if(p==="fmt ")await this.parseFmtChunk(g,b,i);else if(p==="data"){if(l??=b,this.dataStart=f.filePos,this.dataSize=Math.min(l,(o??1/0)-this.dataStart),this.reader.fileSize===null)break}else if(p==="ds64"){let y=this.reader.requestSlice(g,b);if(y instanceof Promise&&(y=await y),!y)break;let k=Mo(y,i);l=Mo(y,i),o=Math.min(k+8,this.reader.fileSize??1/0)}else p==="LIST"?await this.parseListChunk(g,b,i):(p==="ID3 "||p==="id3 ")&&await this.parseId3Chunk(g,b);m=g+b+(b&1),u++}if(!this.audioInfo)throw new Error('Invalid WAVE file - missing "fmt " chunk');if(this.dataStart===-1)throw new Error('Invalid WAVE file - missing "data" chunk');let d=this.audioInfo.blockSizeInBytes;this.dataSize=Math.floor(this.dataSize/d)*d,this.trackBackings.push(new Fo(this))})()}async parseFmtChunk(e,r,i){let a=this.reader.requestSlice(e,r);if(a instanceof Promise&&(a=await a),!a)return;let s=Ti(a,i),o=Ti(a,i),c=Kt(a,i);a.skip(4);let u=Ti(a,i),l;if(r===14?l=8:l=Ti(a,i),r>=18&&s!==357){let m=Ti(a,i),d=r-18;if(Math.min(d,m)>=22&&s===65534){a.skip(6);let p=B(a,16);s=p[0]|p[1]<<8}}(s===7||s===6)&&(l=8),this.audioInfo={format:s,numberOfChannels:o,sampleRate:c,sampleSizeInBytes:Math.ceil(l/8),blockSizeInBytes:u}}async parseListChunk(e,r,i){let a=this.reader.requestSlice(e,r);if(a instanceof Promise&&(a=await a),!a)return;let s=te(a,4);if(s!=="INFO"&&s!=="INF0")return;let o=a.filePos;for(;o<=e+r-8;){a.filePos=o;let c=te(a,4),u=Kt(a,i),l=B(a,u),m=0;for(let f=0;f<l.length&&l[f]!==0;f++)m++;let d=String.fromCharCode(...l.subarray(0,m));switch(this.metadataTags.raw??={},this.metadataTags.raw[c]=d,c){case"INAM":case"TITL":this.metadataTags.title??=d;break;case"TIT3":this.metadataTags.description??=d;break;case"IART":this.metadataTags.artist??=d;break;case"IPRD":this.metadataTags.album??=d;break;case"IPRT":case"ITRK":case"TRCK":{let f=d.split("/"),p=Number.parseInt(f[0],10),b=f[1]&&Number.parseInt(f[1],10);Number.isInteger(p)&&p>0&&(this.metadataTags.trackNumber??=p),b&&Number.isInteger(b)&&b>0&&(this.metadataTags.tracksTotal??=b)}break;case"ICRD":case"IDIT":{let f=new Date(d);Number.isNaN(f.getTime())||(this.metadataTags.date??=f)}break;case"YEAR":{let f=Number.parseInt(d,10);Number.isInteger(f)&&f>0&&(this.metadataTags.date??=new Date(f,0,1))}break;case"IGNR":case"GENR":this.metadataTags.genre??=d;break;case"ICMT":case"CMNT":case"COMM":this.metadataTags.comment??=d;break}o+=8+u+(u&1)}}async parseId3Chunk(e,r){let i=this.reader.requestSlice(e,r);if(i instanceof Promise&&(i=await i),!i)return;let a=ft(i);if(a){let s=r-ze;if(a.size=Math.min(a.size,s),a.size>0){let o=i.slice(e+ze,a.size);bi(o,a,this.metadataTags)}}}getCodec(){if(h(this.audioInfo),this.audioInfo.format===7)return"ulaw";if(this.audioInfo.format===6)return"alaw";if(this.audioInfo.format===1){if(this.audioInfo.sampleSizeInBytes===1)return"pcm-u8";if(this.audioInfo.sampleSizeInBytes===2)return"pcm-s16";if(this.audioInfo.sampleSizeInBytes===3)return"pcm-s24";if(this.audioInfo.sampleSizeInBytes===4)return"pcm-s32"}return this.audioInfo.format===3&&this.audioInfo.sampleSizeInBytes===4?"pcm-f32":null}async getMimeType(){return"audio/wav"}async getTrackBackings(){return await this.readMetadata(),this.trackBackings}async getMetadataTags(){return await this.readMetadata(),this.metadataTags}},ki=2048,Fo=class{constructor(t){this.demuxer=t}getType(){return"audio"}getId(){return 1}getNumber(){return 1}getCodec(){return this.demuxer.getCodec()}getInternalCodecId(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.format}async getDecoderConfig(){let t=this.demuxer.getCodec();return t?(h(this.demuxer.audioInfo),{codec:t,numberOfChannels:this.demuxer.audioInfo.numberOfChannels,sampleRate:this.demuxer.audioInfo.sampleRate}):null}getNumberOfChannels(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.numberOfChannels}getSampleRate(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.sampleRate}getTimeResolution(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.sampleRate}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){return h(this.demuxer.dataSize!==-1),this.demuxer.dataSize/this.demuxer.audioInfo.blockSizeInBytes/this.demuxer.audioInfo.sampleRate}async getLiveRefreshInterval(){return null}getName(){return null}getLanguageCode(){return J}getDisposition(){return{...we}}async getPacketAtIndex(t,e){h(t>=0),h(this.demuxer.audioInfo);let r=t*ki*this.demuxer.audioInfo.blockSizeInBytes;if(r>=this.demuxer.dataSize)return null;let i=Math.min(ki*this.demuxer.audioInfo.blockSizeInBytes,this.demuxer.dataSize-r);if(this.demuxer.reader.fileSize===null){let c=this.demuxer.reader.requestSlice(this.demuxer.dataStart+r,i);if(c instanceof Promise&&(c=await c),!c)return null}let a;if(e.metadataOnly)a=de;else{let c=this.demuxer.reader.requestSlice(this.demuxer.dataStart+r,i);c instanceof Promise&&(c=await c),h(c),a=B(c,i)}let s=t*ki/this.demuxer.audioInfo.sampleRate,o=i/this.demuxer.audioInfo.blockSizeInBytes/this.demuxer.audioInfo.sampleRate;return this.demuxer.lastKnownPacketIndex=Math.max(t,this.demuxer.lastKnownPacketIndex),new Q(a,"key",s,o,t,i)}getFirstPacket(t){return this.getPacketAtIndex(0,t)}async getPacket(t,e){h(this.demuxer.audioInfo);let r=Math.floor(Math.min(t*this.demuxer.audioInfo.sampleRate/ki,(this.demuxer.dataSize-1)/(ki*this.demuxer.audioInfo.blockSizeInBytes)));if(r<0)return null;let i=await this.getPacketAtIndex(r,e);if(i)return i;if(r===0)return null;h(this.demuxer.reader.fileSize===null);let a=await this.getPacketAtIndex(this.demuxer.lastKnownPacketIndex,e);for(;a;){let s=await this.getNextPacket(a,e);if(!s)break;a=s}return a}getNextPacket(t,e){h(this.demuxer.audioInfo);let r=Math.round(t.timestamp*this.demuxer.audioInfo.sampleRate/ki);return this.getPacketAtIndex(r+1,e)}getKeyPacket(t,e){return this.getPacket(t,e)}getNextKeyPacket(t,e){return this.getNextPacket(t,e)}};var Qt=7,Ne=9,De=n=>{let t=n.filePos,e=B(n,9),r=new W(e);if(r.readBits(12)!==4095||(r.skipBits(1),r.readBits(2)!==0))return null;let s=r.readBits(1),o=r.readBits(2)+1,c=r.readBits(4);if(c===15)return null;r.skipBits(1);let u=r.readBits(3);if(u===0)throw new Error("ADTS frames with channel configuration 0 are not supported.");r.skipBits(1),r.skipBits(1),r.skipBits(1),r.skipBits(1);let l=r.readBits(13);r.skipBits(11);let m=r.readBits(2)+1;if(m!==1)throw new Error("ADTS frames with more than one AAC frame are not supported.");let d=null;return s===1?n.filePos-=2:d=r.readBits(16),{objectType:o,samplingFrequencyIndex:c,channelConfiguration:u,frameLength:l,numberOfAacFrames:m,crcCheck:d,startPos:t}};var hn=1024,Ba=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.firstFrameHeader=null;this.loadedSamples=[];this.metadataTags=null;this.trackBackings=[];this.readingMutex=new Ie;this.lastSampleLoaded=!1;this.lastLoadedPos=0;this.nextTimestampInSamples=0;this.reader=e._reader}async readMetadata(){return this.metadataPromise??=(async()=>{for(;!this.firstFrameHeader&&!this.lastSampleLoaded;)await this.advanceReader();h(this.firstFrameHeader),this.trackBackings=[new Bo(this)]})()}async advanceReader(){if(this.lastLoadedPos===0)for(;;){let o=this.reader.requestSlice(this.lastLoadedPos,ze);if(o instanceof Promise&&(o=await o),!o){this.lastSampleLoaded=!0;return}let c=ft(o);if(!c)break;this.lastLoadedPos=o.filePos+c.size}let e=this.reader.requestSliceRange(this.lastLoadedPos,Qt,Ne);if(e instanceof Promise&&(e=await e),!e){this.lastSampleLoaded=!0;return}let r=De(e);if(!r){this.lastSampleLoaded=!0;return}if(this.reader.fileSize!==null&&r.startPos+r.frameLength>this.reader.fileSize){this.lastSampleLoaded=!0;return}this.firstFrameHeader||(this.firstFrameHeader=r);let i=qe[r.samplingFrequencyIndex];h(i!==void 0);let a=hn/i,s={timestamp:this.nextTimestampInSamples/i,duration:a,dataStart:r.startPos,dataSize:r.frameLength};this.loadedSamples.push(s),this.nextTimestampInSamples+=hn,this.lastLoadedPos=r.startPos+r.frameLength}async getMimeType(){return"audio/aac"}async getTrackBackings(){return await this.readMetadata(),this.trackBackings}async getMetadataTags(){let e=await this.readingMutex.acquire();try{if(await this.readMetadata(),this.metadataTags)return this.metadataTags;this.metadataTags={};let r=0;for(;;){let i=this.reader.requestSlice(r,ze);if(i instanceof Promise&&(i=await i),!i)break;let a=ft(i);if(!a)break;let s=this.reader.requestSlice(i.filePos,a.size);if(s instanceof Promise&&(s=await s),!s)break;bi(s,a,this.metadataTags),r=i.filePos+a.size}return this.metadataTags}finally{e()}}},Bo=class{constructor(t){this.demuxer=t}getType(){return"audio"}getId(){return 1}getNumber(){return 1}getTimeResolution(){return this.getSampleRate()/hn}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){return null}async getLiveRefreshInterval(){return null}getName(){return null}getLanguageCode(){return J}getCodec(){return"aac"}getInternalCodecId(){return h(this.demuxer.firstFrameHeader),this.demuxer.firstFrameHeader.objectType}getNumberOfChannels(){h(this.demuxer.firstFrameHeader);let t=kt[this.demuxer.firstFrameHeader.channelConfiguration];return h(t!==void 0),t}getSampleRate(){h(this.demuxer.firstFrameHeader);let t=qe[this.demuxer.firstFrameHeader.samplingFrequencyIndex];return h(t!==void 0),t}getDisposition(){return{...we}}async getDecoderConfig(){return h(this.demuxer.firstFrameHeader),{codec:`mp4a.40.${this.demuxer.firstFrameHeader.objectType}`,numberOfChannels:this.getNumberOfChannels(),sampleRate:this.getSampleRate()}}async getPacketAtIndex(t,e){if(t===-1)return null;let r=this.demuxer.loadedSamples[t];if(!r)return null;let i;if(e.metadataOnly)i=de;else{let a=this.demuxer.reader.requestSlice(r.dataStart,r.dataSize);if(a instanceof Promise&&(a=await a),!a)return null;i=B(a,r.dataSize)}return new Q(i,"key",r.timestamp,r.duration,t,r.dataSize)}getFirstPacket(t){return this.getPacketAtIndex(0,t)}async getNextPacket(t,e){let r=await this.demuxer.readingMutex.acquire();try{let i=Ft(this.demuxer.loadedSamples,t.timestamp,s=>s.timestamp);if(i===-1)throw new Error("Packet was not created from this track.");let a=i+1;for(;a>=this.demuxer.loadedSamples.length&&!this.demuxer.lastSampleLoaded;)await this.demuxer.advanceReader();return this.getPacketAtIndex(a,e)}finally{r()}}async getPacket(t,e){let r=await this.demuxer.readingMutex.acquire();try{for(;;){let i=H(this.demuxer.loadedSamples,t,a=>a.timestamp);if(i===-1&&this.demuxer.loadedSamples.length>0)return null;if(this.demuxer.lastSampleLoaded)return this.getPacketAtIndex(i,e);if(i>=0&&i+1<this.demuxer.loadedSamples.length)return this.getPacketAtIndex(i,e);await this.demuxer.advanceReader()}}finally{r()}}getKeyPacket(t,e){return this.getPacket(t,e)}getNextKeyPacket(t,e){return this.getNextPacket(t,e)}};var Oa=n=>n===0?null:n===1?192:n>=2&&n<=5?144*2**n:n===6?"uncommon-u8":n===7?"uncommon-u16":n>=8&&n<=15?2**n:null,Il=(n,t)=>{switch(n){case 0:return t;case 1:return 88200;case 2:return 176400;case 3:return 192e3;case 4:return 8e3;case 5:return 16e3;case 6:return 22050;case 7:return 24e3;case 8:return 32e3;case 9:return 44100;case 10:return 48e3;case 11:return 96e3;case 12:return"uncommon-u8";case 13:return"uncommon-u16";case 14:return"uncommon-u16-10";default:return null}},Da=n=>{let t=0,e=new W(B(n,1));for(;e.readBits(1)===1;)t++;if(t===0)return e.readBits(7);let r=[],i=t-1,a=new W(B(n,i)),s=8-t-1;for(let c=0;c<s;c++)r.unshift(e.readBits(1));for(let c=0;c<i;c++)for(let u=0;u<8;u++){let l=a.readBits(1);u<2||r.unshift(l)}return r.reduce((c,u,l)=>c|u<<l,0)},Va=(n,t)=>{if(t==="uncommon-u16")return se(n)+1;if(t==="uncommon-u8")return O(n)+1;if(typeof t=="number")return t;ne(t),h(!1)},El=(n,t)=>t==="uncommon-u16"?se(n):t==="uncommon-u16-10"?se(n)*10:t==="uncommon-u8"?O(n):typeof t=="number"?t:null,vl=n=>{let e=0;for(let r of n){e^=r;for(let i=0;i<8;i++)(e&128)!==0?e=e<<1^7:e<<=1,e&=255}return e};var Ua=class extends pe{constructor(e){super(e);this.loadedSamples=[];this.metadataPromise=null;this.trackBacking=null;this.metadataTags={};this.audioInfo=null;this.lastLoadedPos=null;this.blockingBit=null;this.readingMutex=new Ie;this.lastSampleLoaded=!1;this.reader=e._reader}async getMetadataTags(){return await this.readMetadata(),this.metadataTags}async getTrackBackings(){return await this.readMetadata(),h(this.trackBacking),[this.trackBacking]}async getMimeType(){return"audio/flac"}async readMetadata(){let e=4;return this.metadataPromise??=(async()=>{for(;this.reader.fileSize===null||e<this.reader.fileSize;){let r=this.reader.requestSlice(e,4);if(r instanceof Promise&&(r=await r),e+=4,r===null)throw new Error(`Metadata block at position ${e} is too small! Corrupted file.`);h(r);let i=O(r),a=ut(r),s=(i&128)!==0;switch(i&127){case 0:{let c=this.reader.requestSlice(e,a);if(c instanceof Promise&&(c=await c),h(c),c===null)throw new Error(`StreamInfo block at position ${e} is too small! Corrupted file.`);let u=B(c,34),l=new W(u),m=l.readBits(16),d=l.readBits(16),f=l.readBits(24),p=l.readBits(24),b=l.readBits(20),g=l.readBits(3)+1;l.readBits(5);let y=l.readBits(36);l.skipBits(16*8);let k=new Uint8Array(42);k.set(new Uint8Array([102,76,97,67]),0),k.set(new Uint8Array([128,0,0,34]),4),k.set(u,8),this.audioInfo={numberOfChannels:g,sampleRate:b,totalSamples:y,minimumBlockSize:m,maximumBlockSize:d,minimumFrameSize:f,maximumFrameSize:p,description:k},this.trackBacking=new Oo(this);break}case 4:{let c=this.reader.requestSlice(e,a);c instanceof Promise&&(c=await c),h(c),rn(B(c,a),this.metadataTags);break}case 6:{let c=this.reader.requestSlice(e,a);c instanceof Promise&&(c=await c),h(c);let u=F(c),l=F(c),m=me.decode(B(c,l)),d=F(c),f=me.decode(B(c,d));c.skip(16);let p=F(c),b=B(c,p);this.metadataTags.images??=[],this.metadataTags.images.push({data:b,mimeType:m,kind:u===3?"coverFront":u===4?"coverBack":"unknown",description:f});break}default:break}if(e+=a,s){this.lastLoadedPos=e;break}}if(!this.audioInfo)throw new Error("Missing STREAMINFO metadata block! Corrupted FLAC file.")})()}async readNextFlacFrame({startPos:e,isFirstPacket:r}){h(this.audioInfo);let i=6,a=16,s=10,o=this.audioInfo.maximumBlockSize*this.audioInfo.numberOfChannels*4+a+2,c=this.audioInfo.minimumFrameSize||s,l=(this.audioInfo.maximumFrameSize||o)+a,m=await this.reader.requestSliceRange(e,a,l);if(!m)return null;let d=this.readFlacFrameHeader({slice:m,isFirstPacket:r});if(!d)return null;for(m.filePos=e+c;;){if(m.filePos>m.end-i)return{num:d.num,blockSize:d.blockSize,sampleRate:d.sampleRate,size:m.end-e,isLastFrame:!0};if(O(m)===255){let p=m.filePos,b=O(m),g=this.blockingBit===1?249:248;if(b!==g){m.filePos=p;continue}m.skip(-2);let y=m.filePos-e,k=this.readFlacFrameHeader({slice:m,isFirstPacket:!1});if(!k){m.filePos=p;continue}if(this.blockingBit===0){if(k.num-d.num!==1){m.filePos=p;continue}}else if(k.num-d.num!==d.blockSize){m.filePos=p;continue}return{num:d.num,blockSize:d.blockSize,sampleRate:d.sampleRate,size:y,isLastFrame:!1}}}}readFlacFrameHeader({slice:e,isFirstPacket:r}){let i=e.filePos,a=B(e,4),s=new W(a);if(s.readBits(15)!==32764)return null;if(this.blockingBit===null){h(r);let y=s.readBits(1);this.blockingBit=y}else if(this.blockingBit===1){if(h(!r),s.readBits(1)!==1)return null}else if(this.blockingBit===0){if(h(!r),s.readBits(1)!==0)return null}else throw new Error("Invalid blocking bit");let c=Oa(s.readBits(4));if(!c)return null;h(this.audioInfo);let u=Il(s.readBits(4),this.audioInfo.sampleRate);if(!u||(s.readBits(4),s.readBits(3),s.readBits(1)!==0))return null;let m=Da(e),d=Va(e,c),f=El(e,u);if(f===null||f!==this.audioInfo.sampleRate)return null;let p=e.filePos-i,b=O(e);e.skip(-p),e.skip(-1);let g=vl(B(e,p));return b!==g?null:{num:m,blockSize:d,sampleRate:f}}async advanceReader(){await this.readMetadata(),h(this.lastLoadedPos!==null),h(this.audioInfo);let e=this.lastLoadedPos,r=await this.readNextFlacFrame({startPos:e,isFirstPacket:this.loadedSamples.length===0});if(!r){this.lastSampleLoaded=!0;return}let i=this.loadedSamples[this.loadedSamples.length-1],s={blockOffset:i?i.blockOffset+i.blockSize:0,blockSize:r.blockSize,byteOffset:e,byteSize:r.size};if(this.lastLoadedPos=this.lastLoadedPos+r.size,this.loadedSamples.push(s),r.isLastFrame){this.lastSampleLoaded=!0;return}}},Oo=class{constructor(t){this.demuxer=t}getType(){return"audio"}getId(){return 1}getNumber(){return 1}getCodec(){return"flac"}getInternalCodecId(){return null}getNumberOfChannels(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.numberOfChannels}getSampleRate(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.sampleRate}getName(){return null}getLanguageCode(){return J}getTimeResolution(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.sampleRate}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.totalSamples===0?null:this.demuxer.audioInfo.totalSamples/this.demuxer.audioInfo.sampleRate}async getLiveRefreshInterval(){return null}getDisposition(){return{...we}}async getDecoderConfig(){return h(this.demuxer.audioInfo),{codec:"flac",numberOfChannels:this.demuxer.audioInfo.numberOfChannels,sampleRate:this.demuxer.audioInfo.sampleRate,description:this.demuxer.audioInfo.description}}async getPacket(t,e){if(h(this.demuxer.audioInfo),t<0)return null;let r=await this.demuxer.readingMutex.acquire();try{for(;;){let i=H(this.demuxer.loadedSamples,t,c=>c.blockOffset/this.demuxer.audioInfo.sampleRate);if(i===-1){await this.demuxer.advanceReader();continue}let a=this.demuxer.loadedSamples[i],s=a.blockOffset/this.demuxer.audioInfo.sampleRate,o=a.blockSize/this.demuxer.audioInfo.sampleRate;if(s+o<=t){if(this.demuxer.lastSampleLoaded)return this.getPacketAtIndex(this.demuxer.loadedSamples.length-1,e);await this.demuxer.advanceReader();continue}return this.getPacketAtIndex(i,e)}}finally{r()}}async getNextPacket(t,e){let r=await this.demuxer.readingMutex.acquire();try{let i=t.sequenceNumber+1;if(this.demuxer.lastSampleLoaded&&i>=this.demuxer.loadedSamples.length)return null;for(;i>=this.demuxer.loadedSamples.length&&!this.demuxer.lastSampleLoaded;)await this.demuxer.advanceReader();return this.getPacketAtIndex(i,e)}finally{r()}}getKeyPacket(t,e){return this.getPacket(t,e)}getNextKeyPacket(t,e){return this.getNextPacket(t,e)}async getPacketAtIndex(t,e){let r=this.demuxer.loadedSamples[t];if(!r)return null;let i;if(e.metadataOnly)i=de;else{let o=this.demuxer.reader.requestSlice(r.byteOffset,r.byteSize);if(o instanceof Promise&&(o=await o),!o)return null;i=B(o,r.byteSize)}h(this.demuxer.audioInfo);let a=r.blockOffset/this.demuxer.audioInfo.sampleRate,s=r.blockSize/this.demuxer.audioInfo.sampleRate;return new Q(i,"key",a,s,t,r.byteSize)}async getFirstPacket(t){for(;this.demuxer.loadedSamples.length===0&&!this.demuxer.lastSampleLoaded;)await this.demuxer.advanceReader();return this.getPacketAtIndex(0,t)}};var za=n=>{let t="video/MP2T",e=[...new Set(n.filter(Boolean))];return e.length>0&&(t+=`; codecs="${e.join(", ")}"`),t};var Fl="PES packet is missing PTS where it was expected. PES packets without PTS are not currently supported. If you think this file should be supported, please report it.",_l=new Set,Na=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.elementaryStreams=[];this.trackBackingEntries=[];this.packetOffset=0;this.packetStride=-1;this.sectionEndPositions=[];this.seekChunkSize=5*1024*1024;this.minReferencePointByteDistance=-1;this.reader=e._reader}async readMetadata(){return this.metadataPromise??=(async()=>{let e=205,r=this.reader.requestSlice(0,e);r instanceof Promise&&(r=await r),h(r);let i=B(r,e);if(i[0]===71&&i[188]===71)this.packetOffset=0,this.packetStride=188;else if(i[0]===71&&i[204]===71)this.packetOffset=0,this.packetStride=204;else if(i[4]===71&&i[196]===71)this.packetOffset=4,this.packetStride=192;else throw new Error("Unreachable.");let a=256;this.minReferencePointByteDistance=a*this.packetStride;let s=this.packetOffset,o=null,c=!1,u=!1;for(;;){let l=await this.readPacketHeader(s);if(!l)break;if(l.payloadUnitStartIndicator===0){s+=this.packetStride;continue}let m=await this.readSection(s,!0,!u);if(!m)break;let d=3,f=32,p=!1;if(!u&&m.pid!==0&&!(m.payload[0]===0&&m.payload[1]===0&&m.payload[2]===1)){let y=new W(m.payload),k=y.readAlignedByte();y.skipBits(8*k),p=y.readBits(8)===2}if(m.pid===0&&!c){let g=new W(m.payload),y=g.readAlignedByte();g.skipBits(8*y),g.skipBits(14);let k=g.readBits(10);for(g.skipBits(40);8*(k+d)-g.pos>f;){let T=g.readBits(16);g.skipBits(3);let w=g.readBits(13);if(T!==0){if(o!==null)throw new Error("Only files with a single program are supported.");o=w}}if(o===null)throw new Error("Program Association Table must link to a Program Map Table.");c=!0}else if((m.pid===o||p)&&!u){let g=new W(m.payload),y=g.readAlignedByte();g.skipBits(8*y),g.skipBits(12);let k=g.readBits(12);g.skipBits(43);let T=g.readBits(13);g.skipBits(6);let w=g.readBits(10);for(g.skipBits(8*w);8*(k+d)-g.pos>f;){let S=g.readBits(8);g.skipBits(3);let x=g.readBits(13);g.skipBits(6);let P=g.readBits(10),A=g.pos+8*P,C=!1,I=!1;for(;g.pos<A;){let v=g.readBits(8),E=g.readBits(8);v===106?C=!0:(v===122||v===204)&&(I=!0),g.skipBits(8*E)}let R=null;switch(S){case 27:case 36:R={type:"video",codec:S===27?"avc":"hevc",decoderConfig:null,avcCodecInfo:null,hevcCodecInfo:null,colorSpace:{primaries:null,transfer:null,matrix:null,fullRange:null},width:-1,height:-1,squarePixelWidth:-1,squarePixelHeight:-1,reorderSize:-1};break;case 3:case 4:case 15:case 129:case 135:{let v;if(S===3||S===4)v="mp3";else if(S===15)v="aac";else if(S===129)v="ac3";else if(S===135)v="eac3";else throw new Error("Unreachable.");R={type:"audio",codec:v,decoderConfig:null,aacCodecInfo:null,numberOfChannels:-1,sampleRate:-1}}break;case 6:I?R={type:"audio",codec:"eac3",decoderConfig:null,aacCodecInfo:null,numberOfChannels:-1,sampleRate:-1}:C&&(R={type:"audio",codec:"ac3",decoderConfig:null,aacCodecInfo:null,numberOfChannels:-1,sampleRate:-1});break;default:_l.has(S)||(console.warn(`Note: MPEG-TS streams with stream_type 0x${S.toString(16)} are not currently supported.`),_l.add(S))}R&&this.elementaryStreams.push({demuxer:this,pid:x,streamType:S,initialized:!1,firstSection:null,canBeTrustedWithKeyPackets:!1,info:R,referencePesPackets:[]})}u=!0}else{let g=this.elementaryStreams.find(y=>y.pid===m.pid);e:if(g&&!g.initialized){let y=wi(m,!0);if(!y)throw new Error(`Couldn't read first PES packet for Elementary Stream with PID ${g.pid}`);if(g.firstSection=m,g.canBeTrustedWithKeyPackets=m.randomAccessIndicator===1,this.input._initInput){let w=(await this.input._initInput._getDemuxer()).elementaryStreams.find(S=>S.pid===m.pid&&S.info.codec===g.info.codec);if(w){g.info=w.info,g.initialized=!0;break e}}let k=new Ur(g,y);if(g.info.type==="video"){for(;;){let T=k;if(T.suppliedPacket=null,await k.markNextPacket(),g.info.codec==="avc"){if(!k.suppliedPacket)throw new Error("Invalid AVC video stream; could not extract AVCDecoderConfigurationRecord from any packet.");if(g.info.avcCodecInfo=ai(k.suppliedPacket.data),!g.info.avcCodecInfo)continue;let w=g.info.avcCodecInfo.sequenceParameterSets[0];h(w);let S=tn(w);g.info.width=S.displayWidth,g.info.height=S.displayHeight;let x=S.pixelAspectRatio.num,P=S.pixelAspectRatio.den;x>0&&P>0&&(x>P?(g.info.squarePixelWidth=Math.round(g.info.width*x/P),g.info.squarePixelHeight=g.info.height):(g.info.squarePixelWidth=g.info.width,g.info.squarePixelHeight=Math.round(g.info.height*P/x))),g.info.colorSpace={primaries:Ar[S.colourPrimaries],transfer:xr[S.transferCharacteristics],matrix:Pr[S.matrixCoefficients],fullRange:!!S.fullRangeFlag},g.info.reorderSize=S.maxDecFrameBuffering;break}else if(g.info.codec==="hevc"){if(!k.suppliedPacket)throw new Error("Invalid HEVC video stream; could not extract HVCDecoderConfigurationRecord from first packet.");if(g.info.hevcCodecInfo=si(k.suppliedPacket.data),!g.info.hevcCodecInfo)continue;let S=g.info.hevcCodecInfo.arrays.find(P=>P.nalUnitType===33).nalUnits[0];h(S);let x=ko(S);g.info.width=x.displayWidth,g.info.height=x.displayHeight,x.pixelAspectRatio.num>x.pixelAspectRatio.den?(g.info.squarePixelWidth=Math.round(g.info.width*x.pixelAspectRatio.num/x.pixelAspectRatio.den),g.info.squarePixelHeight=g.info.height):(g.info.squarePixelWidth=g.info.width,g.info.squarePixelHeight=Math.round(g.info.height*x.pixelAspectRatio.den/x.pixelAspectRatio.num)),g.info.colorSpace={primaries:Ar[x.colourPrimaries],transfer:xr[x.transferCharacteristics],matrix:Pr[x.matrixCoefficients],fullRange:!!x.fullRangeFlag},g.info.reorderSize=x.maxDecFrameBuffering;break}else throw new Error("Unhandled.")}g.info.decoderConfig={codec:ri({width:g.info.width,height:g.info.height,codec:g.info.codec,codecDescription:null,colorSpace:g.info.colorSpace,avcType:1,avcCodecInfo:g.info.avcCodecInfo,hevcCodecInfo:g.info.hevcCodecInfo,vp9CodecInfo:null,av1CodecInfo:null}),codedWidth:g.info.width,codedHeight:g.info.height,colorSpace:g.info.colorSpace},(g.info.width!==g.info.squarePixelWidth||g.info.height!==g.info.squarePixelHeight)&&(g.info.decoderConfig.displayAspectWidth=g.info.squarePixelWidth,g.info.decoderConfig.displayAspectHeight=g.info.squarePixelHeight),g.initialized=!0}else{if(await k.markNextPacket(),!k.suppliedPacket)throw new Error(`Couldn't parse first media packet for Elementary Stream with PID ${g.pid}`);if(g.info.codec==="aac"){let T=he.tempFromBytes(k.suppliedPacket.data),w=De(T);if(!w)throw new Error("Invalid AAC audio stream; could not read ADTS frame header from first packet.");g.info.aacCodecInfo={isMpeg2:!1,objectType:w.objectType},g.info.numberOfChannels=kt[w.channelConfiguration],g.info.sampleRate=qe[w.samplingFrequencyIndex]}else if(g.info.codec==="mp3"){let T=F(he.tempFromBytes(k.suppliedPacket.data)),w=Mr(T,k.suppliedPacket.data.byteLength);if(!w.header)throw new Error("Invalid MP3 audio stream; could not read frame header from first packet.");g.info.numberOfChannels=w.header.channel===3?1:2,g.info.sampleRate=w.header.sampleRate}else if(g.info.codec==="ac3"){let T=sa(k.suppliedPacket.data);if(!T)throw new Error("Invalid AC-3 audio stream; could not read sync frame from first packet.");if(T.fscod===3)throw new Error("Invalid AC-3 audio stream; reserved sample rate code found in first packet.");g.info.numberOfChannels=an[T.acmod]+T.lfeon,g.info.sampleRate=Br[T.fscod]}else if(g.info.codec==="eac3"){let T=oa(k.suppliedPacket.data);if(!T)throw new Error("Invalid E-AC-3 audio stream; could not read sync frame from first packet.");let w=ca(T);if(w===null)throw new Error("Invalid E-AC-3 audio stream; reserved sample rate code found in first packet.");g.info.numberOfChannels=ua(T),g.info.sampleRate=w}else throw new Error("Unhandled.");g.info.decoderConfig={codec:ii({codec:g.info.codec,codecDescription:null,aacCodecInfo:g.info.aacCodecInfo}),numberOfChannels:g.info.numberOfChannels,sampleRate:g.info.sampleRate},g.initialized=!0}}}if(u&&this.elementaryStreams.every(g=>g.initialized))break;s+=this.packetStride}if(!u)throw c?new Error("No Program Map Table found in the file."):new Error("No Program Association Table found in the file.");for(let l of this.elementaryStreams)l.info.type==="video"?this.trackBackingEntries.push(new Do(l)):this.trackBackingEntries.push(new Vo(l))})()}async getTrackBackings(){return await this.readMetadata(),this.trackBackingEntries}async getMetadataTags(){return{}}async getMimeType(){await this.readMetadata();let e=await Promise.all(this.trackBackingEntries.map(r=>r.getDecoderConfig().then(i=>i?.codec??null)));return za(e)}async readSection(e,r,i=!1){let a=e,s=e,o=[],c=0,u=null,l=!0,m=0;for(;;){let f=await this.readPacket(s);if(s+=this.packetStride,!f)break;if(u){if(f.pid!==u.pid){if(i)break;continue}if(f.payloadUnitStartIndicator===1)break}else{if(f.payloadUnitStartIndicator===0)break;u=f}let p=!!(f.adaptationFieldControl&2),b=!!(f.adaptationFieldControl&1),g=0;if(p&&(g=1+f.body[0],f===u&&g>1&&(m=f.body[1]>>6&1)),b&&(g===0?(o.push(f.body),c+=f.body.byteLength):(o.push(f.body.subarray(g)),c+=f.body.byteLength-g)),a=s,!r&&c>=64){l=!1;break}if(Ft(this.sectionEndPositions,a,k=>k)!==-1){l=!1;break}}if(l){let f=H(this.sectionEndPositions,a,p=>p);this.sectionEndPositions.splice(f+1,0,a)}if(!u)return null;let d;if(o.length===1)d=o[0];else{let f=o.reduce((b,g)=>b+g.length,0);d=new Uint8Array(f);let p=0;for(let b of o)d.set(b,p),p+=b.length}return{startPos:e,endPos:r?a:null,pid:u.pid,payload:d,randomAccessIndicator:m}}async readPacketHeader(e){let r=this.reader.requestSlice(e,4);if(r instanceof Promise&&(r=await r),!r)return null;if(O(r)!==71)throw new Error("Invalid TS packet sync byte. Likely an internal bug, please report this file.");let a=se(r),s=a>>15,o=a>>14&1,c=a>>13&1,u=a&8191,l=O(r),m=l>>6,d=l>>4&3,f=l&15;return{payloadUnitStartIndicator:o,pid:u,adaptationFieldControl:d}}async readPacket(e){let r=this.reader.requestSlice(e,188);if(r instanceof Promise&&(r=await r),!r)return null;let i=B(r,188);if(i[0]!==71)throw new Error("Invalid TS packet sync byte. Likely an internal bug, please report this file.");let s=(i[1]<<8)+i[2],o=s>>15,c=s>>14&1,u=s>>13&1,l=s&8191,m=i[3],d=m>>6,f=m>>4&3,p=m&15;return{payloadUnitStartIndicator:c,pid:l,adaptationFieldControl:f,body:i.subarray(4)}}},Vr=(n,t)=>{if(n.payload.byteLength<3)return null;let e=new W(n.payload);if(e.readBits(24)!==1)return null;let i=e.readBits(8);if(e.skipBits(16),i===188||i===190||i===191||i===240||i===241||i===255||i===242||i===248)return null;e.skipBits(8);let a=e.readBits(2);e.skipBits(14);let s=null;if(a===2||a===3)s=0,e.skipBits(4),s+=e.readBits(3)*(1<<30),e.skipBits(1),s+=e.readBits(15)*32768,e.skipBits(1),s+=e.readBits(15);else if(t)throw new Error(Fl);return{sectionStartPos:n.startPos,sectionEndPos:n.endPos,pts:s,randomAccessIndicator:n.randomAccessIndicator}},wi=(n,t)=>{h(n.endPos!==null);let e=Vr(n,t);if(!e)return null;let r=new W(n.payload);r.skipBits(32);let i=r.readBits(16),a=6;r.skipBits(16);let s=r.readBits(8),o=r.pos+8*s;r.pos=o;let c=o/8;h(Number.isInteger(c));let u=n.payload.subarray(c,i>0?a+i:n.payload.byteLength);return{...e,data:u}},La=class n{constructor(t){this.elementaryStream=t;this.packetBuffers=new WeakMap;this.packetSectionStarts=new WeakMap}getId(){return this.elementaryStream.pid}getNumber(){let t=this.elementaryStream.demuxer,e=this.elementaryStream.info.type,r=0;for(let i of t.trackBackingEntries)if(i.getType()===e&&r++,h(i instanceof n),i.elementaryStream===this.elementaryStream)break;return r}getCodec(){throw new Error("Not implemented on base class.")}getInternalCodecId(){return this.elementaryStream.streamType}getName(){return null}getLanguageCode(){return J}getDisposition(){return{...we,primary:!1}}getTimeResolution(){return 9e4}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){return null}async getLiveRefreshInterval(){return null}createEncodedPacket(t,e,r){let i;return this.allPacketsAreKeyPackets()?i="key":i=t.randomAccessIndicator===1?"key":"delta",new Q(r.metadataOnly?de:t.data,i,t.pts/9e4,Math.max(e/9e4,0),t.sequenceNumber,t.data.byteLength)}async getFirstPacket(t){let e=this.elementaryStream.firstSection;h(e);let r=wi(e,!0);h(r);let i=new Ur(this.elementaryStream,r),a=new gn(this,i),s=await a.readNext();if(!s)return null;let o=this.createEncodedPacket(s.packet,s.duration,t);return this.packetBuffers.set(o,a),this.packetSectionStarts.set(o,s.packet.sectionStartPos),o}async getNextPacket(t,e){let r=this.packetBuffers.get(t);if(r){let l=await r.readNext();if(!l)return null;this.packetBuffers.delete(t);let m=this.createEncodedPacket(l.packet,l.duration,e);return this.packetBuffers.set(m,r),this.packetSectionStarts.set(m,l.packet.sectionStartPos),m}let i=this.packetSectionStarts.get(t);if(i===void 0)throw new Error("Packet was not created from this track.");let s=await this.elementaryStream.demuxer.readSection(i,!0);h(s);let o=wi(s,!0);h(o);let c=new Ur(this.elementaryStream,o);r=new gn(this,c);let u=t.sequenceNumber;for(;;){let l=await r.readNext();if(!l)return null;if(l.packet.sequenceNumber>u){let m=this.createEncodedPacket(l.packet,l.duration,e);return this.packetBuffers.set(m,r),this.packetSectionStarts.set(m,l.packet.sectionStartPos),m}}}async getNextKeyPacket(t,e){let r=t;for(;;){if(r=await this.getNextPacket(r,e),!r)return null;if(r.type==="key")return r}}getPacket(t,e){return this.doPacketLookup(t,!1,e)}getKeyPacket(t,e){return this.doPacketLookup(t,!0,e)}async doPacketLookup(t,e,r){let i=Mt(t*9e4),a=this.elementaryStream.demuxer,{reader:s,seekChunkSize:o}=a,c=this.elementaryStream.pid,u=async(w,S,x)=>{let P=w;for(;P<S;){let A=await a.readPacketHeader(P);if(!A)return null;if(A.pid===c&&A.payloadUnitStartIndicator===1){let C=await a.readSection(P,x);if(!C)return null;let I=Vr(C,!1);if(I&&I.pts!==null)return{pesPacketHeader:I,section:C}}P+=a.packetStride}return null},l=this.elementaryStream.firstSection;h(l);let m=Vr(l,!0);if(h(m),i<m.pts)return null;let d,f=this.elementaryStream.referencePesPackets,p=H(f,i,w=>w.pts),b=p!==-1?f[p]:null;if(b&&i-b.pts<9e4/2)d=b.sectionStartPos;else{let w=0;if(s.fileSize!==null){let S=Math.ceil(s.fileSize/o);if(S>1){let x=0,P=S-1;for(w=x;x<=P;){let A=Math.floor((x+P)/2),C=Nn(A*o,a.packetStride)+m.sectionStartPos,I=C+o,R=await u(C,I,!1);if(!R){P=A-1;continue}R.pesPacketHeader.pts<=i?(w=A,x=A+1):P=A-1}}}d=Nn(w*o,a.packetStride)+m.sectionStartPos}let y=(await u(d,s.fileSize??1/0,!1))?.pesPacketHeader??null;y||(y=m);let k=this.getReorderSize(),T=async(w,S)=>{let x=await a.readSection(w,!0);h(x);let P=wi(x,!0);h(P);let A=new Ur(this.elementaryStream,P),C=new gn(this,A);for(;!((X(C.presentationOrderPackets)?.pts??-1/0)>=i||!await C.readNextPacket()););let I=rr(C.presentationOrderPackets,S);if(I===-1)return null;let R=C.presentationOrderPackets[I],v=I===0?0:R.pts-C.presentationOrderPackets[I-1].pts;for(;C.decodeOrderPackets[0]!==R;)C.decodeOrderPackets.shift();C.lastDuration=v;let E=await C.readNext();h(E);let D=this.createEncodedPacket(E.packet,E.duration,r);return this.packetBuffers.set(D,C),this.packetSectionStarts.set(D,E.packet.sectionStartPos),D};if(!e||this.allPacketsAreKeyPackets()){e:for(;;){let w=y.sectionStartPos+a.packetStride;for(;;){let S=await a.readPacketHeader(w);if(!S)break e;if(S.pid===c&&S.payloadUnitStartIndicator===1){let x=await a.readSection(w,!1);if(x){let P=Vr(x,!1);if(P&&P.pts!==null){if(P.pts>i)break e;y=P,Uo(this.elementaryStream,y);break}}}w+=a.packetStride}}e:for(let w=0;w<k+1;w++){let S=y.sectionStartPos-a.packetStride;for(;S>=a.packetOffset;){let x=await a.readPacketHeader(S);if(!x)break e;if(x.pid===c&&x.payloadUnitStartIndicator===1){let P=await a.readSection(S,!1);if(P){let A=Vr(P,!1);if(A&&A.pts!==null){y=A;break}}}S-=a.packetStride}}return T(y.sectionStartPos,w=>w.pts<=i)}else{let w=d,S=null,x=!this.elementaryStream.canBeTrustedWithKeyPackets;for(;;){let P=null,A=w<=m.sectionStartPos,C,I=null;if(A)C=m,I=l;else{let E=await u(w,s.fileSize??1/0,x);C=E?.pesPacketHeader??null,I=E?.section??null}let R=!1,v=0;e:for(;C&&!(S!==null&&C.sectionStartPos>=S);){if(C.pts<=i){let D;if(this.elementaryStream.canBeTrustedWithKeyPackets)D=C.randomAccessIndicator===1;else{h(I);let U=wi(I,!0);h(U);let z=new Ur(this.elementaryStream,U);await z.markNextPacket(),D=z.suppliedPacket?.randomAccessIndicator===1}D&&(P=C)}if(C.pts>i&&(R=!0),R&&(v++,v>k))break;let E=C.sectionStartPos+a.packetStride;for(;;){let D=await a.readPacketHeader(E);if(!D)break e;if(D.pid===c&&D.payloadUnitStartIndicator===1){let U=await a.readSection(E,x);if(U){let z=Vr(U,!1);if(z&&z.pts!==null){C=z,I=U,Uo(this.elementaryStream,C);break}}}E+=a.packetStride}}if(P){let E=P;if(v===0)e:for(let U=0;U<k;U++){let z=E.sectionStartPos-a.packetStride;for(;z>=a.packetOffset;){let q=await a.readPacketHeader(z);if(!q)break e;if(q.pid===c&&q.payloadUnitStartIndicator===1){let ke=await a.readSection(z,x);if(ke){let Te=Vr(ke,!1);if(Te&&Te.pts!==null){E=Te;break}}}z-=a.packetStride}}let D=await T(E.sectionStartPos,U=>U.pts<=i&&U.randomAccessIndicator===1);return h(D),D}if(A)return null;S=w,w=Math.max(Nn(w-m.sectionStartPos-o,a.packetStride)+m.sectionStartPos,m.sectionStartPos)}}}},Do=class extends La{getType(){return"video"}getCodec(){return this.elementaryStream.info.codec}getCodedWidth(){return this.elementaryStream.info.width}getCodedHeight(){return this.elementaryStream.info.height}getSquarePixelWidth(){return this.elementaryStream.info.squarePixelWidth}getSquarePixelHeight(){return this.elementaryStream.info.squarePixelHeight}getRotation(){return 0}async getColorSpace(){return this.elementaryStream.info.colorSpace}async canBeTransparent(){return!1}async getDecoderConfig(){return h(this.elementaryStream.info.decoderConfig),this.elementaryStream.info.decoderConfig}allPacketsAreKeyPackets(){return!1}getReorderSize(){return this.elementaryStream.info.reorderSize}},Vo=class extends La{getType(){return"audio"}getCodec(){return this.elementaryStream.info.codec}getNumberOfChannels(){return this.elementaryStream.info.numberOfChannels}getSampleRate(){return this.elementaryStream.info.sampleRate}async getDecoderConfig(){return h(this.elementaryStream.info.decoderConfig),this.elementaryStream.info.decoderConfig}allPacketsAreKeyPackets(){return!0}getReorderSize(){return 0}},Uo=(n,t)=>{let e=n.referencePesPackets,r=H(e,t.sectionStartPos,i=>i.sectionStartPos);if(r>=0){let i=e[r];if(t.pts<=i.pts)return!1;let a=n.demuxer.minReferencePointByteDistance;if(t.sectionStartPos-i.sectionStartPos<a)return!1;if(r<e.length-1){let s=e[r+1];if(s.pts<t.pts||s.sectionStartPos-t.sectionStartPos<a)return!1}}return e.splice(r+1,0,t),!0},Ur=class{constructor(t,e){this.currentPos=0;this.pesPackets=[];this.currentPesPacketIndex=0;this.currentPesPacketPos=0;this.endPos=0;this.lastSuppliedPesPacket=null;this.nextPts=null;this.suppliedPacket=null;this.elementaryStream=t,this.pid=t.pid,this.demuxer=t.demuxer,this.startingPesPacket=e}ensureBuffered(t){let e=this.endPos-this.currentPos;return e>=t?t:this.bufferData(t-e).then(()=>Math.min(this.endPos-this.currentPos,t))}getCurrentPesPacket(){let t=this.pesPackets[this.currentPesPacketIndex];return h(t),t}async bufferData(t){let e=this.endPos+t;for(;this.endPos<e;){let r;if(this.pesPackets.length===0)r=this.startingPesPacket;else{let i=X(this.pesPackets).sectionEndPos;for(h(i!==null);;){let a=await this.demuxer.readPacketHeader(i);if(!a)return;if(a.pid===this.pid){let s=await this.demuxer.readSection(i,!0);if(!s)return;let o=wi(s,!1);if(o){r=o;break}}i+=this.demuxer.packetStride}}this.pesPackets.push(r),this.endPos+=r.data.byteLength}}readBytes(t){let e=this.getCurrentPesPacket(),r=this.currentPos-this.currentPesPacketPos,i=r+t;if(this.currentPos+=t,i<=e.data.byteLength)return e.data.subarray(r,i);let a=new Uint8Array(t);a.set(e.data.subarray(r));let s=e.data.byteLength-r;for(;;){this.advanceCurrentPacket();let o=this.getCurrentPesPacket(),c=t-s;if(c<=o.data.byteLength){a.set(o.data.subarray(0,c),s);break}a.set(o.data,s),s+=o.data.byteLength}return a}readU8(){let t=this.getCurrentPesPacket(),e=this.currentPos-this.currentPesPacketPos;return this.currentPos++,e<t.data.byteLength?t.data[e]:(this.advanceCurrentPacket(),t=this.getCurrentPesPacket(),t.data[0])}seekTo(t){if(t!==this.currentPos){if(t<this.currentPos)for(;t<this.currentPesPacketPos;){this.currentPesPacketIndex--;let e=this.getCurrentPesPacket();this.currentPesPacketPos-=e.data.byteLength}else for(;;){let e=this.getCurrentPesPacket(),r=this.currentPesPacketPos+e.data.byteLength;if(t<r)break;this.currentPesPacketPos+=e.data.byteLength,this.currentPesPacketIndex++}this.currentPos=t}}skip(t){this.seekTo(this.currentPos+t)}advanceCurrentPacket(){this.currentPesPacketPos+=this.getCurrentPesPacket().data.byteLength,this.currentPesPacketIndex++}async markNextPacket(){h(!this.suppliedPacket);let t=this.elementaryStream;if(t.info.type==="video"){let e=t.info.codec,r=1024;if(e!=="avc"&&e!=="hevc")throw new Error("Unhandled.");let i=null;for(;;){let a=this.ensureBuffered(r);if(a instanceof Promise&&(a=await a),a===0)break;let s=this.currentPos,o=this.readBytes(a),c=o.byteLength,u=0;for(;u<c;){let l=o.indexOf(0,u);if(l===-1||l>=c)break;u=l;let m=s+u;if(u+4>=c){this.seekTo(m);break}let d=o[u+1],f=o[u+2],p=o[u+3],b=0,g=null;if(d===0&&f===0&&p===1?(b=4,g=o[u+4]):d===0&&f===1&&(b=3,g=p),b===0){u++;continue}let y=m;if(i===null){i=y,u+=b;continue}if(g!==null){let k=e==="avc"?zt(g):at(g);if(e==="avc"?k===9:k===35){let w=y-i;return this.seekTo(i),this.supplyPacket(w,0)}}u+=b}if(a<r)break}if(i!==null){let a=this.endPos-i;return this.seekTo(i),this.supplyPacket(a,0)}}else{let e=t.info.codec,r=128;for(;;){let i=this.ensureBuffered(r);i instanceof Promise&&(i=await i);let a=this.currentPos;for(;this.currentPos-a<i;){let s=this.readU8();if(e==="aac"){if(s!==255)continue;this.skip(-1);let o=this.currentPos,c=this.ensureBuffered(Ne);if(c instanceof Promise&&(c=await c),c<Ne)return;let u=this.readBytes(Ne),l=De(he.tempFromBytes(u));if(l){this.seekTo(o);let m=this.ensureBuffered(l.frameLength);return m instanceof Promise&&(m=await m),this.supplyPacket(m,Math.round(hn*9e4/t.info.sampleRate))}else this.seekTo(o+1)}else if(e==="mp3"){if(s!==255)continue;this.skip(-1);let o=this.currentPos,c=this.ensureBuffered(4);if(c instanceof Promise&&(c=await c),c<4)return;let u=this.readBytes(4),l=V(u).getUint32(0),m=Mr(l,null);if(m.header){this.seekTo(o);let d=this.ensureBuffered(m.header.totalSize);d instanceof Promise&&(d=await d);let f=m.header.audioSamplesInFrame*9e4/t.info.sampleRate;return this.supplyPacket(d,Math.round(f))}else this.seekTo(o+1)}else if(e==="ac3"){if(s!==11)continue;this.skip(-1);let o=this.currentPos,c=this.ensureBuffered(5);if(c instanceof Promise&&(c=await c),c<5)return;let u=this.readBytes(5);if(u[0]!==11||u[1]!==119){this.seekTo(o+1);continue}let l=u[4]>>6,m=u[4]&63;if(l===3||m>37){this.seekTo(o+1);continue}let d=el[3*m+l];h(d!==void 0),this.seekTo(o),c=this.ensureBuffered(d),c instanceof Promise&&(c=await c);let f=Math.round(tl*9e4/t.info.sampleRate);return this.supplyPacket(c,f)}else if(e==="eac3"){if(s!==11)continue;this.skip(-1);let o=this.currentPos,c=this.ensureBuffered(5);if(c instanceof Promise&&(c=await c),c<5)return;let u=this.readBytes(5);if(u[0]!==11||u[1]!==119){this.seekTo(o+1);continue}let m=(((u[2]&7)<<8|u[3])+1)*2,f=u[4]>>6===3?3:u[4]>>4&3,p=To[f];this.seekTo(o),c=this.ensureBuffered(m),c instanceof Promise&&(c=await c);let b=p*256,g=Math.round(b*9e4/t.info.sampleRate);return this.supplyPacket(c,g)}else throw new Error("Unhandled.")}if(i<r)break}}}supplyPacket(t,e){let r=this.getCurrentPesPacket(),i;if(this.lastSuppliedPesPacket===r)h(this.nextPts!==null),i=this.nextPts;else{if(r.pts===null)throw new Error(Fl);i=r.pts,Uo(this.elementaryStream,r)}this.lastSuppliedPesPacket=r,this.nextPts=i+e;let a=r.sectionStartPos,s=a+(this.currentPos-this.currentPesPacketPos),o=this.readBytes(t),c=r.randomAccessIndicator;if(c===0&&!this.elementaryStream.canBeTrustedWithKeyPackets){if(this.elementaryStream.info.type==="audio")c=1;else if(this.elementaryStream.info.decoderConfig){let u=oi(this.elementaryStream.info.codec,this.elementaryStream.info.decoderConfig,o)==="key";c=Number(u)}}this.suppliedPacket={pts:i,data:o,sequenceNumber:s,sectionStartPos:a,randomAccessIndicator:c},this.pesPackets.splice(0,this.currentPesPacketIndex),this.currentPesPacketIndex=0}},gn=class{constructor(t,e){this.decodeOrderPackets=[];this.reorderBuffer=[];this.presentationOrderPackets=[];this.reachedEnd=!1;this.lastDuration=0;this.backing=t,this.context=e,this.reorderSize=t.getReorderSize(),h(this.reorderSize>=0)}async readNext(){if(this.decodeOrderPackets.length===0&&!await this.readNextPacket())return null;await this.ensureCurrentPacketHasNext();let t=this.decodeOrderPackets[0],e=this.presentationOrderPackets.indexOf(t);h(e!==-1);let r;for(e===this.presentationOrderPackets.length-1?r=this.lastDuration:(r=this.presentationOrderPackets[e+1].pts-t.pts,this.lastDuration=r),this.decodeOrderPackets.shift();this.presentationOrderPackets.length>0;){let i=this.presentationOrderPackets[0];if(this.decodeOrderPackets.includes(i))break;this.presentationOrderPackets.shift()}return{packet:t,duration:r}}async readNextPacket(){if(this.reachedEnd)return!1;let t;return this.context.suppliedPacket?t=this.context.suppliedPacket:(await this.context.markNextPacket(),t=this.context.suppliedPacket),this.context.suppliedPacket=null,t?(this.decodeOrderPackets.push(t),this.processPacketThroughReorderBuffer(t),!0):(this.reachedEnd=!0,this.flushReorderBuffer(),!1)}async ensureCurrentPacketHasNext(){let t=this.decodeOrderPackets[0];for(h(t);;){let e=this.presentationOrderPackets.indexOf(t);if(e!==-1&&e<=this.presentationOrderPackets.length-2||!await this.readNextPacket())break}}processPacketThroughReorderBuffer(t){if(this.reorderBuffer.push(t),this.reorderBuffer.length>this.reorderSize){let e=0;for(let i=1;i<this.reorderBuffer.length;i++)this.reorderBuffer[i].pts<this.reorderBuffer[e].pts&&(e=i);let r=this.reorderBuffer[e];this.presentationOrderPackets.push(r),this.reorderBuffer.splice(e,1)}}flushReorderBuffer(){this.reorderBuffer.sort((t,e)=>t.pts-e.pts),this.presentationOrderPackets.push(...this.reorderBuffer),this.reorderBuffer.length=0}};var St="application/vnd.apple.mpegurl",zo="#EXT-X-STREAM-INF:",No="#EXT-X-I-FRAME-STREAM-INF:",Lo="#EXT-X-MEDIA:",bn="#EXTINF:",Wo="#EXT-X-MAP:",Ho="#EXT-X-KEY:",qo="#EXT-X-MEDIA-SEQUENCE:",Go="#EXT-X-BYTERANGE:",Ko="#EXT-X-PROGRAM-DATE-TIME:",Ml="#EXT-X-DISCONTINUITY",Qo="#EXT-X-TARGETDURATION:",Bl="#EXT-X-ENDLIST",jo="#EXT-X-PLAYLIST-TYPE:",Ol="#EXT-X-I-FRAMES-ONLY",Wa=n=>n.length===0||n.startsWith("#")&&!n.startsWith("#EXT"),jt=class{constructor(t){this._attributes={};let e="",r="",i=!1,a=!1;for(let s=0;s<t.length;s++){let o=t[s];o==='"'?a=!a:o==="="&&!i&&!a?i=!0:o===","&&!a?(e&&(this._attributes[e.trim().toLowerCase()]=r),e="",r="",i=!1):i?r+=o:e+=o}e&&(this._attributes[e.trim().toLowerCase()]=r)}get(t){return this._attributes[t.toLowerCase()]??null}getAsNumber(t){let e=this.get(t);if(e===null)return null;let r=Number(e);return Number.isFinite(r)?r:null}merge(t){Object.assign(this._attributes,t._attributes)}};var Ha=class{constructor(t,e,r){this.nextInputCacheAge=0;this.inputCache=[];this.trackBackingsPromise=null;this.firstSegment=null;this.firstSegmentFirstTimestamps=new WeakMap;this.firstTimestampCache=new WeakMap;this.input=t,this.path=e,this.trackDeclarations=r}async getDurationFromMetadata(t){let e=await this.getSegmentAt(1/0,{skipLiveWait:t.skipLiveWait});return e?e.timestamp+e.duration:null}async getTrackBackings(){return this.trackBackingsPromise??=(async()=>{let t=[];if(this.trackDeclarations){for(let e of this.trackDeclarations)if(e.type==="video"){let r=Rr(t,i=>i.getType()==="video")+1;t.push(new Ga(this,e,r))}else if(e.type==="audio"){let r=Rr(t,i=>i.getType()==="audio")+1;t.push(new Ka(this,e,r))}}else{if(this.firstSegment=await this.getFirstSegment({}),!this.firstSegment)return[];let r=await this.getInputForSegment(this.firstSegment).getTracks();for(let i of r)if(i.type==="video"){let a=Rr(t,s=>s.getType()==="video")+1;t.push(new Ga(this,{id:t.length+1,type:"video"},a))}else if(i.type==="audio"){let a=Rr(t,s=>s.getType()==="audio")+1;t.push(new Ka(this,{id:t.length+1,type:"audio"},a))}}return t})()}async getFirstTimestampForInput(t){let e=this.firstTimestampCache.get(t);if(e!==void 0)return e;let r=await t.getFirstTimestamp();return this.firstTimestampCache.set(t,r),r}async getMediaOffset(t,e){let r=t.firstSegment??t,i;if(this.firstSegmentFirstTimestamps.has(r))i=this.firstSegmentFirstTimestamps.get(r);else{let u=this.getInputForSegment(r);i=await this.getFirstTimestampForInput(u),this.firstSegmentFirstTimestamps.set(r,i)}if(r===t)return r.timestamp-i;let a=await this.getFirstTimestampForInput(e),s=t.timestamp-r.timestamp,c=a-i-s;return Math.abs(c)<=Math.min(.25,s)?r.timestamp-i:t.timestamp-a}dispose(){for(let t of this.inputCache)t.input.dispose();this.inputCache.length=0}},qa=class{constructor(t,e,r){this.packetInfos=new WeakMap;this.hydrationPromise=null;this.firstInputTrack=null;this.segmentedInput=t,this.decl=e,this.number=r}hydrate(){return this.hydrationPromise??=(async()=>{if(this.segmentedInput.firstSegment??=await this.segmentedInput.getFirstSegment({}),!this.segmentedInput.firstSegment)throw new Error("Missing first segment, can't retrieve track.");let r=(await this.segmentedInput.getInputForSegment(this.segmentedInput.firstSegment).getTracks()).find(i=>i.type===this.decl.type&&i.number===this.number);if(!r)throw new Error("No matching track found in underlying media data.");this.firstInputTrack=r})()}getId(){return this.decl.id}getType(){return this.decl.type}getNumber(){return this.number}delegate(t){return this.firstInputTrack?t():this.hydrate().then(t)}async getDecoderConfig(){return this.delegate(()=>this.firstInputTrack._backing.getDecoderConfig())}getHasOnlyKeyPackets(){return this.delegate(()=>this.firstInputTrack._backing.getHasOnlyKeyPackets?.()??null)}getPairingMask(){return 1n}getCodec(){return this.delegate(()=>this.firstInputTrack._backing.getCodec())}getInternalCodecId(){return this.delegate(()=>this.firstInputTrack._backing.getInternalCodecId())}getDisposition(){return this.delegate(()=>this.firstInputTrack._backing.getDisposition())}getLanguageCode(){return this.delegate(()=>this.firstInputTrack._backing.getLanguageCode())}getName(){return this.delegate(()=>this.firstInputTrack._backing.getName())}getTimeResolution(){return this.delegate(()=>this.firstInputTrack._backing.getTimeResolution())}async isRelativeToUnixEpoch(){return await this.hydrate(),h(this.segmentedInput.firstSegment),this.segmentedInput.firstSegment.relativeToUnixEpoch}getBitrate(){return this.delegate(()=>this.firstInputTrack._backing.getBitrate())}getAverageBitrate(){return this.delegate(()=>this.firstInputTrack._backing.getAverageBitrate())}getDurationFromMetadata(t){return this.segmentedInput.getDurationFromMetadata(t)}getLiveRefreshInterval(){return this.segmentedInput.getLiveRefreshInterval()}async createAdjustedPacket(t,e,r){h(t.sequenceNumber>=0),h(this.segmentedInput.firstSegment);let i=await this.segmentedInput.getMediaOffset(e,r.input),a=e.timestamp-this.segmentedInput.firstSegment.timestamp,s=t.clone({timestamp:bt(t.timestamp+i,await r.getTimeResolution()),sequenceNumber:Math.floor(1e8*a)+t.sequenceNumber});return this.packetInfos.set(s,{segment:e,track:r,sourcePacket:t}),s}async getFirstPacket(t){await this.hydrate(),h(this.segmentedInput.firstSegment),h(this.firstInputTrack);let e=await this.firstInputTrack._backing.getFirstPacket(t);return e?this.createAdjustedPacket(e,this.segmentedInput.firstSegment,this.firstInputTrack):null}getNextPacket(t,e){return this._getNextInternal(t,e,!1)}getNextKeyPacket(t,e){return this._getNextInternal(t,e,!0)}async _getNextInternal(t,e,r){let i=this.packetInfos.get(t);if(!i)throw new Error("Packet was not created from this track.");let a=r?await i.track._backing.getNextKeyPacket(i.sourcePacket,e):await i.track._backing.getNextPacket(i.sourcePacket,e);if(a)return this.createAdjustedPacket(a,i.segment,i.track);let s=i.segment;for(;;){let o=await this.segmentedInput.getNextSegment(s,{skipLiveWait:e.skipLiveWait});if(!o)return null;let l=(await this.segmentedInput.getInputForSegment(o).getTracks()).find(d=>d.type===i.track.type&&d.number===i.track.number);if(!l){s=o;continue}let m=await l._backing.getFirstPacket(e);return m?this.createAdjustedPacket(m,o,l):null}}getPacket(t,e){return this._getPacketInternal(t,e,!1)}getKeyPacket(t,e){return this._getPacketInternal(t,e,!0)}async _getPacketInternal(t,e,r){let i=await this.segmentedInput.getSegmentAt(t,{skipLiveWait:e.skipLiveWait});if(!i)return null;for(await this.hydrate();i;){let a=this.segmentedInput.getInputForSegment(i),o=(await a.getTracks()).find(m=>m.type===this.firstInputTrack.type&&m.number===this.firstInputTrack.number);if(!o){i=await this.segmentedInput.getPreviousSegment(i,{skipLiveWait:e.skipLiveWait});continue}let c=await this.segmentedInput.getMediaOffset(i,a),u=t-c,l=r?await o._backing.getKeyPacket(u,e):await o._backing.getPacket(u,e);if(!l){i=await this.segmentedInput.getPreviousSegment(i,{skipLiveWait:e.skipLiveWait});continue}return this.createAdjustedPacket(l,i,o)}return null}},Ga=class extends qa{getType(){return"video"}getCodec(){return this.delegate(()=>this.firstInputTrack._backing.getCodec())}getCodedWidth(){return this.delegate(()=>this.firstInputTrack._backing.getCodedWidth())}getCodedHeight(){return this.delegate(()=>this.firstInputTrack._backing.getCodedHeight())}getSquarePixelWidth(){return this.delegate(()=>this.firstInputTrack._backing.getSquarePixelWidth())}getSquarePixelHeight(){return this.delegate(()=>this.firstInputTrack._backing.getSquarePixelHeight())}getRotation(){return this.delegate(()=>this.firstInputTrack._backing.getRotation())}async getColorSpace(){return this.delegate(()=>this.firstInputTrack._backing.getColorSpace())}async canBeTransparent(){return this.delegate(()=>this.firstInputTrack._backing.canBeTransparent())}async getDecoderConfig(){return this.delegate(()=>this.firstInputTrack._backing.getDecoderConfig())}},Ka=class extends qa{getType(){return"audio"}getCodec(){return this.delegate(()=>this.firstInputTrack._backing.getCodec())}getNumberOfChannels(){return this.delegate(()=>this.firstInputTrack._backing.getNumberOfChannels())}getSampleRate(){return this.delegate(()=>this.firstInputTrack._backing.getSampleRate())}async getDecoderConfig(){return this.delegate(()=>this.firstInputTrack._backing.getDecoderConfig())}};var Dl=cu(Xo(),1);Jr();var Vl=typeof Dl<"u"?Dl:void 0,ec=0,tc=1/0,_e=class extends Ye{constructor(){super(...arguments);this._disposed=!1;this._refCount=0;this._usedForHls=!1;this._sizePromise=null;this.onread=null}async getSizeOrNull(){if(this._disposed)throw new be;return this._sizePromise??=(async()=>{let e=this._getFileSize();return e!==void 0||(await this._read(0,1,ec,tc),e=this._getFileSize(),h(e!==void 0)),e})()}async getSize(){if(this._disposed)throw new be;let e=await this.getSizeOrNull();if(e===null)throw new Error("Cannot determine the size of an unsized source.");return e}slice(e,r){if(!Number.isInteger(e)||e<0)throw new TypeError("offset must be a non-negative integer.");if(r!==void 0&&(!Number.isInteger(r)||r<0))throw new TypeError("length, when provided, must be a non-negative integer.");return new ja(this,e,r)}_dispatchRead(e,r){this.onread?.(e,r),this._emit("read",{start:e,end:r})}ref(){return new zr(this)}},zr=class{constructor(t){this._freed=!1;if(t._disposed)throw new Error("Cannot ref a disposed source.");t._refCount++,this._source=t}get source(){if(!this._source)throw new Error("Can't get source; ref has already been freed.");return this._source}get freed(){return this._freed}free(){if(this._freed)throw new Error("Illegal operation: double free on SourceRef.");let t=this.source;h(t._refCount>0),t._refCount--,t._refCount===0&&(t._dispose(),t._disposed=!0),this._freed=!0,this._source=null}[Symbol.dispose](){this.freed||this.free()}},Qe=class extends _e{constructor(e,r){if(typeof e!="string")throw new TypeError("rootPath must be a string.");if(typeof r!="function")throw new TypeError("requestHandler must be a function.");super();this.rootPath=e;this.requestHandler=r}_resolveRequest(e){let r=this.requestHandler(e),i=a=>{if(!(a instanceof _e||a instanceof zr))throw new TypeError("requestHandler must return or resolve to a Source or SourceRef.");let s=a instanceof _e?a.ref():a;return s.source._usedForHls||=this._usedForHls,s};return r instanceof Promise?r.then(i):i(r)}},rc=(n,t)=>n.path===t.path,yn=class extends Qe{constructor(){super(...arguments);this._root=null;this._rootRequest=null}_read(e,r,i,a){if(!this._root){if(!this._rootRequest){let s=this._resolveRequest({path:this.rootPath,isRoot:!0}),o=c=>{let u=c instanceof _e?c.ref():c;return this._root=u,this._rootRequest=null,u};s instanceof Promise?this._rootRequest=s.then(o):(o(s),h(this._root))}if(this._rootRequest)return this._rootRequest.then(s=>s.source._read(e,r,i,a))}return this._root.source._read(e,r,i,a)}_getFileSize(){if(this._root)return this._root.source._getFileSize()}_dispose(){this._root?this._root.free():this._rootRequest&&this._rootRequest.then(e=>e.free())}},$o=class extends _e{constructor(e){if(!(e instanceof ArrayBuffer)&&!(typeof SharedArrayBuffer<"u"&&e instanceof SharedArrayBuffer)&&!ArrayBuffer.isView(e))throw new TypeError("buffer must be an ArrayBuffer, SharedArrayBuffer, or ArrayBufferView.");super();this._onreadCalled=!1;this._bytes=j(e),this._view=V(e)}_getFileSize(){return this._bytes.byteLength}_read(){return this._onreadCalled||(this._dispatchRead(0,this._bytes.byteLength),this._onreadCalled=!0),{bytes:this._bytes,view:this._view,offset:0}}_dispose(){}},Yo=class extends _e{constructor(e,r={}){if(!(e instanceof Blob))throw new TypeError("blob must be a Blob.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(r.maxCacheSize!==void 0&&(!nr(r.maxCacheSize)||r.maxCacheSize<0))throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");if(r.useStreamReader!==void 0&&typeof r.useStreamReader!="boolean")throw new TypeError("options.useStreamReader, when provided, must be a boolean.");super();this._readers=new WeakMap;this._blob=e,this._options=r,this._orchestrator=new Tn({maxCacheSize:r.maxCacheSize??8*2**20,maxWorkerCount:4,runWorker:this._runWorker.bind(this),prefetchProfile:ic.fileSystem}),this._orchestrator.fileSize=e.size}_getFileSize(){return this._orchestrator.fileSize}_read(e,r,i,a){return this._orchestrator.read(e,r,i,a)}async _runWorker(e){h(e.strictTarget);let r=this._readers.get(e);for(r===void 0&&("stream"in this._blob&&!ir()&&this._options.useStreamReader!==!1?r=this._blob.slice(e.currentPos).stream().getReader():r=null,this._readers.set(e,r));e.currentPos<e.targetPos&&!e.aborted;)if(r){let{done:i,value:a}=await r.read();if(i)throw this._orchestrator.onWorkerFinished(e),new Error("Blob reader stopped unexpectedly before all requested data was read.");if(e.aborted)break;this._dispatchRead(e.currentPos,e.currentPos+a.length),this._orchestrator.supplyWorkerData(e,a)}else{let i=await this._blob.slice(e.currentPos,e.targetPos).arrayBuffer();if(e.aborted)break;this._dispatchRead(e.currentPos,e.currentPos+i.byteLength),this._orchestrator.supplyWorkerData(e,new Uint8Array(i))}this._orchestrator.signalWorkerStoppedRunning(e),e.aborted&&await r?.cancel()}_dispose(){this._orchestrator.dispose()}},Mm=.5*2**20,Bm=(n,t,e)=>{if(t instanceof Error&&(t.message.includes("Failed to fetch")||t.message.includes("Load failed")||t.message.includes("NetworkError when attempting to fetch resource"))&&typeof window<"u"){let i=null;try{typeof window<"u"&&typeof window.location<"u"&&(i=new URL(e instanceof Request?e.url:e,window.location.href).origin)}catch{}if((typeof navigator<"u"&&typeof navigator.onLine=="boolean"?navigator.onLine:!0)&&i!==null&&i!==window.location.origin)return console.warn("Request will not be retried because a CORS error was suspected due to different origins. You can modify this behavior by providing your own function for the 'getRetryDelay' option."),null}return Math.min(2**(n-2),16)},Ul=new Set,Zo=class n extends Qe{constructor(e,r={}){if(typeof e!="string"&&!(e instanceof URL)&&!(typeof Request<"u"&&e instanceof Request))throw new TypeError("url must be a string, URL or Request.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(r.requestInit!==void 0&&(!r.requestInit||typeof r.requestInit!="object"))throw new TypeError("options.requestInit, when provided, must be an object.");if(r.getRetryDelay!==void 0&&typeof r.getRetryDelay!="function")throw new TypeError("options.getRetryDelay, when provided, must be a function.");if(r.maxCacheSize!==void 0&&(!nr(r.maxCacheSize)||r.maxCacheSize<0))throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");if(r.parallelism!==void 0&&(!Number.isInteger(r.parallelism)||r.parallelism<1))throw new TypeError("options.parallelism, when provided, must be a positive number.");if(r.fetchFn!==void 0&&typeof r.fetchFn!="function")throw new TypeError("options.fetchFn, when provided, must be a function.");let i=e instanceof Request?e.url:e instanceof URL?e.href:e;super(i,s=>new n(s.path,this._options));this._fileSizeDetermined=!1;this._url=e,this._options=r,this._getRetryDelay=r.getRetryDelay??Bm;let a=2;this._orchestrator=new Tn({maxCacheSize:r.maxCacheSize??64*2**20,maxWorkerCount:r.parallelism??a,runWorker:this._runWorker.bind(this),prefetchProfile:ic.network})}_getFileSize(){return this._fileSizeDetermined?this._orchestrator.fileSize:void 0}_read(e,r,i,a){return this._orchestrator.read(e,r,i,a)}async _runWorker(e){for(;;){let r=new AbortController,i=await wu(this._options.fetchFn??fetch,this._url,Tu(this._options.requestInit??{},{headers:{Range:`bytes=${e.currentPos}-`},signal:r.signal}),this._getRetryDelay,()=>this._disposed);if(!i.ok)throw new Error(`Error fetching ${String(this._url)}: ${i.status} ${i.statusText}`);e:if(this._orchestrator.fileSize===null){let s=i.headers.get("Content-Range");if(s){let c=/\/(\d+)/.exec(s);if(c){this._orchestrator.supplyFileSize(Number(c[1]));break e}}let o=i.headers.get("Content-Length");o&&this._orchestrator.supplyFileSize(e.currentPos+Number(o))}if(this._fileSizeDetermined=!0,i.status!==206){if(!this._usedForHls){let s=new URL(this._url instanceof Request?this._url.url:this._url,typeof window<"u"?window.location.href:void 0);s.origin!=="null"&&!(s.pathname.endsWith(".m3u8")||s.pathname.endsWith(".m3u"))&&(Ul.has(s.origin)||(console.log(this._usedForHls,this._url,s.pathname),console.warn(`HTTP server (origin ${s.origin}) did not respond to a range request with 206 Partial Content, meaning the entire resource will now be downloaded. To enable efficient media file streaming across a network, please make sure your server supports range requests.`),Ul.add(s.origin)))}e.currentPos=0,this._orchestrator.options.maxCacheSize=1/0,this._orchestrator.fileSize!==null?e.targetPos=this._orchestrator.fileSize:(e.targetPos=1/0,e.strictTarget=!1),this._orchestrator.consolidateEverythingIntoOneWorker(e)}if(!i.body)throw new Error("Missing HTTP response body stream. The used fetch function must provide the response body as a ReadableStream.");let a=i.body.getReader();for(;;){if(e.currentPos>=e.targetPos||e.aborted){r.abort(),this._orchestrator.signalWorkerStoppedRunning(e);return}let s;try{s=await a.read()}catch(u){if(this._disposed)throw u;let l=this._getRetryDelay(1,u,this._url);if(l!==null){console.error("Error while reading response stream. Attempting to resume.",u),await $i(1e3*l);break}else throw u}if(e.aborted)continue;let{done:o,value:c}=s;if(o){if(e.currentPos>=e.targetPos){this._orchestrator.onWorkerFinished(e);return}if(e.strictTarget)break;this._orchestrator.onWorkerFinished(e);return}this._dispatchRead(e.currentPos,e.currentPos+c.length),this._orchestrator.supplyWorkerData(e,c)}}}_dispose(){this._orchestrator.dispose()}},Jo=class n extends Qe{constructor(e,r={}){if(typeof e!="string")throw new TypeError("filePath must be a string.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(r.maxCacheSize!==void 0&&(!nr(r.maxCacheSize)||r.maxCacheSize<0))throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");if(!Vl.fs)throw new Error("FilePathSource is only available in server-side environments (Node.js, Bun, Deno).");super(e,i=>new n(i.path,r));this._fileHandle=null;this._streamSource=new Qa({getSize:async()=>(this._fileHandle=await Vl.fs.open(e,"r"),(await this._fileHandle.stat()).size),read:async(i,a)=>{h(this._fileHandle);let s=new Uint8Array(a-i);return await this._fileHandle.read(s,0,a-i,i),s},maxCacheSize:r.maxCacheSize,prefetchProfile:"fileSystem"})}_read(e,r,i,a){return this._streamSource._read(e,r,i,a)}_getFileSize(){return this._streamSource._getFileSize()}_dispose(){this._streamSource._dispose(),this._fileHandle?.close(),this._fileHandle=null}},Qa=class extends _e{constructor(t){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(typeof t.getSize!="function")throw new TypeError("options.getSize must be a function.");if(typeof t.read!="function")throw new TypeError("options.read must be a function.");if(t.dispose!==void 0&&typeof t.dispose!="function")throw new TypeError("options.dispose, when provided, must be a function.");if(t.maxCacheSize!==void 0&&(!nr(t.maxCacheSize)||t.maxCacheSize<0))throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");if(t.prefetchProfile&&!["none","fileSystem","network"].includes(t.prefetchProfile))throw new TypeError("options.prefetchProfile, when provided, must be one of 'none', 'fileSystem' or 'network'.");super(),this._options=t,this._orchestrator=new Tn({maxCacheSize:t.maxCacheSize??8*2**20,maxWorkerCount:2,prefetchProfile:ic[t.prefetchProfile??"none"],runWorker:this._runWorker.bind(this)})}_getFileSize(){return this._orchestrator.fileSize??void 0}_read(t,e,r,i){if(this._orchestrator.fileSize!==null)return this._orchestrator.read(t,e,r,i);let a=this._options.getSize();if(a instanceof Promise)return a.then(s=>{if(!Number.isInteger(s)||s<0)throw new TypeError("options.getSize must return or resolve to a non-negative integer.");return this._orchestrator.fileSize=s,this._orchestrator.read(t,e,r,i)});if(!Number.isInteger(a)||a<0)throw new TypeError("options.getSize must return or resolve to a non-negative integer.");return this._orchestrator.fileSize=a,this._orchestrator.read(t,e,r,i)}async _runWorker(t){for(;t.currentPos<t.targetPos&&!t.aborted;){let e=t.currentPos,r=t.targetPos,i=this._options.read(t.currentPos,r);if(i instanceof Promise&&(i=await i),t.aborted)break;if(i instanceof Uint8Array){if(i=j(i),i.length!==r-t.currentPos)throw new Error(`options.read returned a Uint8Array with unexpected length: Requested ${r-t.currentPos} bytes, but got ${i.length}.`);this._dispatchRead(t.currentPos,t.currentPos+i.length),this._orchestrator.supplyWorkerData(t,i)}else if(i instanceof ReadableStream){let a=i.getReader();for(;t.currentPos<r&&!t.aborted;){let{done:s,value:o}=await a.read();if(s){if(t.currentPos<r)throw new Error(`ReadableStream returned by options.read ended before supplying enough data. Requested ${r-e} bytes, but got ${t.currentPos-e}`);break}if(!(o instanceof Uint8Array))throw new TypeError("ReadableStream returned by options.read must yield Uint8Array chunks.");if(t.aborted)break;let c=j(o);this._dispatchRead(t.currentPos,t.currentPos+c.length),this._orchestrator.supplyWorkerData(t,c)}}else throw new TypeError("options.read must return or resolve to a Uint8Array or a ReadableStream.")}this._orchestrator.signalWorkerStoppedRunning(t)}_dispose(){this._orchestrator.dispose(),this._options.dispose?.()}},kn=class extends _e{constructor(e,r={}){if(!(e instanceof ReadableStream))throw new TypeError("stream must be a ReadableStream.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(r.maxCacheSize!==void 0&&(!nr(r.maxCacheSize)||r.maxCacheSize<0))throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");super();this._reader=null;this._cache=[];this._pendingSlices=[];this._currentIndex=0;this._targetIndex=0;this._maxRequestedIndex=0;this._endIndex=null;this._pulling=!1;this._stream=e,this._maxCacheSize=r.maxCacheSize??32*2**20}_getFileSize(){return this._endIndex}_read(e,r){if(this._endIndex!==null&&r>this._endIndex)return null;this._maxRequestedIndex=Math.max(this._maxRequestedIndex,r);let i=H(this._cache,e,m=>m.start),a=i!==-1?this._cache[i]:null;if(a&&a.start<=e&&r<=a.end)return{bytes:a.bytes,view:a.view,offset:a.start};let s=e,o=new Uint8Array(r-e);if(i!==-1)for(let m=i;m<this._cache.length;m++){let d=this._cache[m];if(d.start>=r)break;let f=Math.max(e,d.start);f>s&&this._throwDueToCacheMiss();let p=Math.min(r,d.end);f<p&&(o.set(d.bytes.subarray(f-d.start,p-d.start),f-e),s=p)}if(s===r)return{bytes:o,view:V(o),offset:e};this._currentIndex>s&&this._throwDueToCacheMiss();let{promise:c,resolve:u,reject:l}=Z();return this._pendingSlices.push({start:e,end:r,bytes:o,resolve:u,reject:l}),this._targetIndex=Math.max(this._targetIndex,r),this._pulling||(this._pulling=!0,this._pull().catch(m=>{if(this._pulling=!1,this._pendingSlices.length>0)this._pendingSlices.forEach(d=>d.reject(m)),this._pendingSlices.length=0;else throw m})),c}_throwDueToCacheMiss(){throw new Error("Read is before the cached region. With ReadableStreamSource, you must access the data more sequentially or increase the size of its cache.")}async _pull(){for(this._reader??=this._stream.getReader();this._currentIndex<this._targetIndex&&!this._disposed;){let{done:e,value:r}=await this._reader.read();if(e){for(let s of this._pendingSlices)s.resolve(null);this._pendingSlices.length=0,this._endIndex=this._currentIndex;break}let i=this._currentIndex,a=this._currentIndex+r.byteLength;this._dispatchRead(i,a);for(let s=0;s<this._pendingSlices.length;s++){let o=this._pendingSlices[s],c=Math.max(i,o.start),u=Math.min(a,o.end);c<u&&(o.bytes.set(r.subarray(c-i,u-i),c-o.start),u===o.end&&(o.resolve({bytes:o.bytes,view:V(o.bytes),offset:o.start}),this._pendingSlices.splice(s,1),s--))}for(this._cache.push({start:i,end:a,bytes:r,view:V(r),age:0});this._cache.length>0;){let s=this._cache[0];if(this._maxRequestedIndex-s.end<=this._maxCacheSize)break;this._cache.shift()}this._currentIndex+=r.byteLength}this._pulling=!1}_dispose(){this._pendingSlices.length=0,this._cache.length=0,this._reader?.cancel()}},ic={none:(n,t)=>({start:n,end:t}),fileSystem:(n,t)=>(n=Math.floor((n-65536)/65536)*65536,t=Math.ceil((t+65536)/65536)*65536,{start:n,end:t}),network:(n,t,e)=>{n=Math.max(0,Math.floor((n-65536)/65536)*65536);for(let i of e){let s=Math.max((i.startPos+i.targetPos)/2,i.targetPos-8388608);if(Ln(n,t,s,i.targetPos)){let o=i.targetPos-i.startPos,c=Math.ceil((o+1)/8388608)*8388608,u=2**Math.ceil(Math.log2(o+1)),l=Math.min(u,c);t=Math.max(t,i.startPos+l)}}return t=Math.max(t,n+Mm),{start:n,end:t}}},Tn=class{constructor(t){this.options=t;this.fileSize=null;this.nextAge=0;this.workers=[];this.cache=[];this.currentCacheSize=0;this.disposed=!1;this.queuedReads=[]}read(t,e,r,i){h(!this.disposed);let a=this.options.prefetchProfile(t,e,this.workers),s=Math.max(a.start,r),o=Math.min(a.end,this.fileSize??1/0,i);h(s<=t&&e<=o);let c=null,u=H(this.cache,t,S=>S.start),l=u!==-1?this.cache[u]:null;l&&l.start<=t&&e<=l.end&&(l.age=this.nextAge++,c={bytes:l.bytes,view:l.view,offset:l.start});let m=H(this.cache,s,S=>S.start),d=c?null:new Uint8Array(e-t),f=0,p=s,b=[];if(m!==-1){for(let S=m;S<this.cache.length;S++){let x=this.cache[S];if(x.start>=o)break;if(x.end<=s)continue;let P=Math.max(s,x.start),A=Math.min(o,x.end);if(h(P<=A),p<P&&b.push({start:p,end:P}),p=A,d){let C=Math.max(t,x.start),I=Math.min(e,x.end);if(C<I){let R=C-t;d.set(x.bytes.subarray(C-x.start,I-x.start),R),R===f&&(f=I-t)}}x.age=this.nextAge++}p<o&&b.push({start:p,end:o})}else b.push({start:s,end:o});if(d&&f>=d.length&&(c={bytes:d,view:V(d),offset:t}),b.length===0)return h(c),c;let{promise:g,resolve:y,reject:k}=Z(),T=[];for(let S of b){let x=Math.max(t,S.start),P=Math.min(e,S.end);x===S.start&&P===S.end?T.push(S):x<P&&T.push({start:x,end:P})}let w=d&&{start:t,bytes:d,holes:T,resolve:y,reject:k};e:for(let S of b){for(let A of this.workers)if(this.checkHoleAgainstWorker(A,S,w?[w]:[])){this.checkQueuedReadsAgainstWorker(A);continue e}let x=S.end<o||this.fileSize!==null,P=this.createWorker(S.start,S.end,x);if(P)w&&(P.pendingSlices=[w]),this.runWorker(P);else{let A=H(this.queuedReads,S.start,I=>I.hole.start),C=A!==-1?this.queuedReads[A]:null;for(C&&S.start<=C.hole.end?(C.hole.end=Math.max(C.hole.end,S.end),C.strictTarget&&=x,w&&C.pendingSlices.push(w)):(A++,C={hole:{start:S.start,end:S.end},strictTarget:x,pendingSlices:w?[w]:[],age:this.nextAge++},this.queuedReads.splice(A,0,C));A+1<this.queuedReads.length;){let I=this.queuedReads[A+1];if(I.hole.start>C.hole.end)break;C.hole.end=Math.max(C.hole.end,I.hole.end),C.pendingSlices.push(...I.pendingSlices),C.strictTarget&&=I.strictTarget,C.age=Math.min(C.age,I.age),this.queuedReads.splice(A+1,1)}}}return c||(h(d),c=g.then(S=>S&&{bytes:S,view:V(S),offset:t})),c}checkHoleAgainstWorker(t,e,r){if(Ln(e.start-131072,e.start,t.currentPos,t.targetPos)){t.targetPos=Math.max(t.targetPos,e.end);for(let a=0;a<r.length;a++){let s=r[a];t.pendingSlices.includes(s)||t.pendingSlices.push(s)}return t.running||this.runWorker(t),!0}return!1}checkQueuedReadsAgainstWorker(t){let e=!1;for(let r=0;r<this.queuedReads.length;r++){let i=this.queuedReads[r];if(this.checkHoleAgainstWorker(t,i.hole,i.pendingSlices))this.queuedReads.splice(r,1),r--,e=!0;else if(e)break}}createWorker(t,e,r){if(this.workers.length>=this.options.maxWorkerCount){let a=null,s=null;for(let o=0;o<this.workers.length;o++){let c=this.workers[o];!c.running&&c.pendingSlices.length===0&&(!a||c.age<a.age)&&(s=o,a=c)}if(a)h(s!==null),h(a.pendingSlices.length===0),this.workers.splice(s,1);else return null}let i={startPos:t,currentPos:t,targetPos:e,strictTarget:r,running:!1,aborted:this.disposed,pendingSlices:[],age:this.nextAge++};return this.workers.push(i),i}runWorker(t){h(!t.running),h(t.currentPos<t.targetPos),t.running=!0,t.age=this.nextAge++,this.options.runWorker(t).catch(e=>{if(t.running=!1,t.pendingSlices.length>0)t.pendingSlices.forEach(r=>r.reject(e)),t.pendingSlices.length=0;else throw e}).finally(()=>{if(!t.running&&this.queuedReads.length>0){let e=0;for(let a=1;a<this.queuedReads.length;a++)this.queuedReads[a].age<this.queuedReads[e].age&&(e=a);let r=this.queuedReads[e];this.queuedReads.splice(e,1);let i=this.createWorker(r.hole.start,r.hole.end,r.strictTarget);h(i),i.pendingSlices=r.pendingSlices,this.runWorker(i)}})}consolidateEverythingIntoOneWorker(t){let e=new Set(t.pendingSlices);for(let r=0;r<this.workers.length;r++){let i=this.workers[r];if(i!==t){for(let a of i.pendingSlices)e.add(a);i.aborted=!0,i.pendingSlices.length=0,this.workers.splice(r,1),r--}}for(let r=0;r<this.queuedReads.length;r++){let i=this.queuedReads[r];for(let a of i.pendingSlices)e.add(a)}t.pendingSlices=[...e],this.queuedReads.length=0}supplyWorkerData(t,e){h(!t.aborted);let r=t.currentPos,i=r+e.length;this.insertIntoCache({start:r,end:i,bytes:e,view:V(e),age:this.nextAge++}),t.currentPos+=e.length,t.currentPos>t.targetPos&&(t.targetPos=t.currentPos,this.checkQueuedReadsAgainstWorker(t));for(let a=0;a<t.pendingSlices.length;a++){let s=t.pendingSlices[a],o=Math.max(r,s.start),c=Math.min(i,s.start+s.bytes.length);o<c&&s.bytes.set(e.subarray(o-r,c-r),o-s.start);for(let u=0;u<s.holes.length;u++){let l=s.holes[u];r<=l.start&&i>l.start&&(l.start=i),l.end<=l.start&&(s.holes.splice(u,1),u--)}s.holes.length===0&&(s.resolve(s.bytes),t.pendingSlices.splice(a,1),a--)}for(let a=0;a<this.workers.length;a++){let s=this.workers[a];t===s||s.running||Ln(r,i,s.currentPos,s.targetPos)&&(this.workers.splice(a,1),a--)}}supplyFileSize(t){h(this.fileSize===null),this.fileSize=t;for(let e of this.workers){e.targetPos=Math.min(e.targetPos,t),e.strictTarget=!0;for(let r=0;r<e.pendingSlices.length;r++){let i=e.pendingSlices[r];for(let a of i.holes)if(a.end>t){i.resolve(null),e.pendingSlices.splice(r,1),r--;break}}}for(let e=0;e<this.queuedReads.length;e++){let r=this.queuedReads[e];if(r.hole.start>=t){for(let i of r.pendingSlices)i.resolve(null);this.queuedReads.splice(e,1),e--}else if(r.hole.end>t){r.hole.end=t,r.strictTarget=!0;for(let i=0;i<r.pendingSlices.length;i++){let a=r.pendingSlices[i];a.start>=t&&(a.resolve(null),r.pendingSlices.splice(i,1),i--)}}}}signalWorkerStoppedRunning(t){t.running=!1,t.pendingSlices.length=0}onWorkerFinished(t){let e=this.workers.indexOf(t);h(e!==-1),t.running=!1,this.workers.splice(e,1),this.fileSize===null&&this.supplyFileSize(t.currentPos);for(let r of t.pendingSlices)r.resolve(null)}insertIntoCache(t){if(this.options.maxCacheSize===0)return;let e=H(this.cache,t.start,r=>r.start)+1;if(e>0){let r=this.cache[e-1];if(r.end>=t.end)return;if(r.end>t.start){let i=new Uint8Array(t.end-r.start);i.set(r.bytes,0),i.set(t.bytes,t.start-r.start),this.currentCacheSize+=t.end-r.end,r.bytes=i,r.view=V(i),r.end=t.end,e--,t=r}else this.cache.splice(e,0,t),this.currentCacheSize+=t.bytes.length}else this.cache.splice(e,0,t),this.currentCacheSize+=t.bytes.length;for(let r=e+1;r<this.cache.length;r++){let i=this.cache[r];if(t.end<=i.start)break;if(t.end>=i.end){this.cache.splice(r,1),this.currentCacheSize-=i.bytes.length,r--;continue}let a=new Uint8Array(i.end-t.start);a.set(t.bytes,0),a.set(i.bytes,i.start-t.start),this.currentCacheSize-=t.end-i.start,t.bytes=a,t.view=V(a),t.end=i.end,this.cache.splice(r,1);break}for(;this.currentCacheSize>this.options.maxCacheSize;){let r=0,i=this.cache[0];for(let a=1;a<this.cache.length;a++){let s=this.cache[a];s.age<i.age&&(r=a,i=s)}if(this.currentCacheSize-i.bytes.length<=this.options.maxCacheSize)break;this.cache.splice(r,1),this.currentCacheSize-=i.bytes.length}}dispose(){for(let t of this.workers)t.aborted=!0;this.workers.length=0,this.cache.length=0,this.disposed=!0}};var ja=class extends _e{constructor(e,r,i){super();this._ref=null;if(e._disposed)throw new Error("Cannot create a slice of a disposed source.");this._baseSource=e,this._offset=r,this._length=i??null}_getFileSize(){let e=this._baseSource._getFileSize();return e===void 0?this._length!==null?this._length:void 0:e===null?this._length!==null?this._length:null:ue(e-this._offset,0,this._length??1/0)}_read(e,r,i,a){if(this._length!==null&&r>this._length)return null;let s=this._baseSource._read(this._offset+e,this._offset+r,this._offset+i,this._offset+a);return s instanceof Promise?s.then(o=>o?(o.offset-=this._offset,o):null):s?(s.offset-=this._offset,s):null}_dispose(){this._ref?.free()}ref(){return this._ref??=this._baseSource.ref(),super.ref()}};var Om=/^0[xX][0-9a-fA-F]+$/,Dm=/^data:.*;base64,/i,wn=class extends Ha{constructor(e,r,i,a){super(e.input,r,i);this.segments=[];this.nextLines=null;this.currentUpdateSegmentsPromise=null;this.streamHasEnded=!1;this.lastSegmentUpdateTime=-1/0;this.refreshInterval=5;this.demuxer=e,this.nextLines=a}runUpdateSegments(){return this.currentUpdateSegmentsPromise??=(async()=>{try{let e=this.getRemainingWaitTimeMs();e>0&&await $i(e),this.lastSegmentUpdateTime=performance.now(),await this.updateSegments()}finally{this.currentUpdateSegmentsPromise=null}})()}getRemainingWaitTimeMs(){let e=performance.now()-this.lastSegmentUpdateTime,r=Math.max(0,1e3*this.refreshInterval-e);return r<=50?0:r}async updateSegments(){let e=this.nextLines;if(this.nextLines=null,!e){var k=[];try{let x=qi(k,await this.demuxer.input._getSourceUncached({path:this.path,isRoot:!1}));let P=new pr(x.source);let A=await P.requestEntireFile();h(A);e=Xa(A,A.length,{ignore:Wa})}catch(T){var w=T,S=!0}finally{Gi(k,w,S)}}let r=!1,i=0,a=null,s=null,o=0,c=null,u=null,l=null,m=null,d=null,f=null,p=!1,b=X(this.segments)??null,g=x=>{let P=x.indexOf("@"),A=Number(P===-1?x:x.slice(0,P));if(!Number.isInteger(A)||A<0)throw new Error(`Invalid #EXT-X-BYTERANGE length '${x}'.`);let C=null;if(P!==-1&&(C=Number(x.slice(P+1)),!Number.isInteger(C)||C<0))throw new Error(`Invalid #EXT-X-BYTERANGE offset '${x}'.`);return{length:A,offset:C}},y=x=>{o=x,b&&(h(b.sequenceNumber!==null),b.sequenceNumber<x&&(i=b.timestamp+b.duration,c=b.firstSegment,u=b.initSegment,d=b.lastProgramDateTimeSeconds,b=null))};for(let x=0;x<e.length;x++){let P=e[x];if(!r){if(P!=="#EXTM3U")throw new Error("Invalid M3U8 file; expected first line to be #EXTM3U.");r=!0;continue}if(!P.startsWith("#")){if(!b){if(a===null)throw new Error("Invalid M3U8 file; a segment must be preceded by an #EXTINF tag.");let A=s;if(A&&A.method==="AES-128"&&!A.iv){let v=new Uint8Array(ct),E=V(v);E.setUint32(8,Math.floor(o/2**32)),E.setUint32(12,o),A={...A,iv:v}}let I={path:fe(this.path,P),offset:m?.offset??0,length:m?.length??null},R={timestamp:i,relativeToUnixEpoch:d!==null,firstSegment:c,sequenceNumber:o,location:I,duration:a,encryption:A,initSegment:u,lastProgramDateTimeSeconds:d};c??=R,i+=a,this.segments.push(R)}a=null,m===null?l=null:m=null,y(o+1)}if(P.startsWith(bn)){if(b){p=!0;continue}p||(d===null&&o>0&&f!==null&&(i=o*f),p=!0);let A=P.slice(bn.length),C=A.indexOf(","),I=C===-1?A:A.slice(0,C),R=Number(I);if(!Number.isFinite(R)||R<0)throw new Error(`Invalid #EXTINF tag duration '${I}'.`);a=R}else if(P.startsWith(Wo)){let A=new jt(P.slice(Wo.length)),C=A.get("uri");if(!C)throw new Error("Invalid #EXT-X-MAP tag; missing URI attribute.");let I=A.get("byterange"),R=null;if(I!==null&&(R=g(I)),R&&R.offset===null)throw new Error("Invalid #EXT-X-MAP tag; BYTERANGE attribute must have a specified offset.");if(!b){let E={path:fe(this.path,C),offset:R?.offset??0,length:R?.length??null};if(s?.method==="AES-128"&&!s.iv)throw new Error("IV attribute must be set on #EXT-X-KEY tag preceding the #EXT-X-MAP tag.");u={timestamp:i,relativeToUnixEpoch:d!==null,firstSegment:null,sequenceNumber:null,location:E,duration:0,encryption:s,initSegment:null,lastProgramDateTimeSeconds:d}}a=null,m===null?l=null:m=null}else if(P.startsWith(Ho)){let A=new jt(P.slice(Ho.length)),C=A.get("method");if(C==="NONE")s=null;else if(C==="AES-128"){let I=A.get("uri");if(!I)throw new Error("Invalid #EXT-X-KEY: AES-128 requires a URI attribute.");let R=null,v=A.get("iv");if(v){if(!Om.test(v))throw new Error(`Unsupported IV format '${v}'.`);let D=v.slice(2);D=D.padStart(ct*2,"0"),R=new Uint8Array(ct);for(let U=0;U<ct;U++){let z=-ct*2+U;R[U]=parseInt(D.slice(z,z+2),16)}}let E=A.get("keyformat")??"identity";if(E!=="identity")throw new Error("For AES-128 encryption, only the 'identity' KEYFORMAT is currently supported. If you think other formats should be supported, please raise an issue.");s={method:"AES-128",keyUri:fe(this.path,I),iv:R,keyFormat:E}}else if(C==="SAMPLE-AES"||C==="SAMPLE-AES-CTR"){let I=A.get("uri");if(!I)throw new Error(`Invalid #EXT-X-KEY: ${C} requires a URI attribute.`);if((A.get("keyformat")??"identity")==="identity")throw new Error("For SAMPLE-AES and SAMPLE-AES-CTR encryption, the 'identity' KEYFORMAT is not supported. If you think this format should be supported, please raise an issue.");let v=null;if(Dm.test(I)){let E=I.indexOf(","),D=_r(I.slice(E+1));if(D.length>=8&&D[4]===112&&D[5]===115&&D[6]===115&&D[7]===104){let U=V(D).getUint32(0);v=da(D.subarray(8,Math.min(U,D.length)))}}s={method:C,psshBox:v}}else throw new Error(`Unsupported encryption method '${C}'. If you think this method should be supported, please raise an issue.`)}else if(P.startsWith(qo)){let A=P.slice(qo.length),C=Number(A);if(!Number.isInteger(C)||C<0)throw new Error(`Invalid EXT-X-MEDIA-SEQUENCE value '${A}'.`);y(C)}else if(P.startsWith(Go)){let A=g(P.slice(Go.length));if(A.offset===null){if(l===null)throw new Error("Invalid M3U8 file; #EXT-X-BYTERANGE without offset requires a previous byte range.");A.offset=l}m=A,l=A.offset+A.length}else if(P.startsWith(Ko)){if(b)continue;let A=P.slice(Ko.length),C=Date.parse(A);if(!Number.isFinite(C))continue;let I=C/1e3;if(d===I)continue;if(d===null&&this.segments.length>0){let R=X(this.segments),v=R.timestamp+R.duration,E=I-v;for(let D of this.segments)D.timestamp+=E,D.relativeToUnixEpoch=!0;i+=E}d=I,i=I}else if(P===Ml)c=null;else if(P.startsWith(Qo)){let A=P.slice(Qo.length),C=Number(A);if(!Number.isFinite(C)||C<0)throw new Error(`Invalid EXT-X-TARGETDURATION value '${A}'.`);this.refreshInterval=C,f=C}else if(P===Bl){this.streamHasEnded=!0;break}else P.startsWith(jo)&&P.slice(jo.length).toLowerCase()==="vod"&&(this.streamHasEnded=!0)}if(!r)throw new Error("Invalid M3U8 file; no #EXTM3U header.")}async getFirstSegment(){return this.segments.length===0&&await this.runUpdateSegments(),this.segments[0]??null}async getSegmentAt(e,r){this.segments.length===0&&await this.runUpdateSegments();let i=!!r.skipLiveWait&&this.getRemainingWaitTimeMs()>0;for(;;){let a=H(this.segments,e,o=>o.timestamp);if(a===-1)return null;if(a<this.segments.length-1||this.streamHasEnded||i)return this.segments[a];let s=this.segments[a];if(e<s.timestamp+s.duration)return s;await this.runUpdateSegments(),r.skipLiveWait&&(i=!0)}}async getNextSegment(e,r){let i=this.segments.indexOf(e);h(i!==-1);let a=i+1,s=!!r.skipLiveWait&&this.getRemainingWaitTimeMs()>0;for(;;){if(a<this.segments.length)return this.segments[a];if(this.streamHasEnded||s)return null;await this.runUpdateSegments(),r.skipLiveWait&&(s=!0)}}async getPreviousSegment(e){let r=this.segments.indexOf(e);return h(r!==-1),this.segments[r-1]??null}getInputForSegment(e){let r=e,i=this.inputCache.find(u=>u.segment===r);if(i)return i.age=this.nextInputCacheAge++,i.input;let a=null;(r.initSegment||r.firstSegment)&&(a=this.getInputForSegment(r.initSegment??r.firstSegment));let s={...this.input._formatOptions,isobmff:{...this.input._formatOptions.isobmff,resolveKeyId:this.input._formatOptions.isobmff?.resolveKeyId&&(u=>{if(!r.encryption||!(r.encryption.method==="SAMPLE-AES"||r.encryption.method==="SAMPLE-AES-CTR")||!r.encryption.psshBox)return this.input._formatOptions.isobmff.resolveKeyId(u);let l=u.psshBoxes,{psshBox:m}=r.encryption;return(m.keyIds===null||m.keyIds.includes(u.keyId))&&!l.some(d=>ma(d,m))&&(l=[...l,m]),this.input._formatOptions.isobmff.resolveKeyId({...u,psshBoxes:l})})}},o=new Nr({source:new yn(r.location.path,async u=>{h(u.isRoot);let l={...u,isRoot:!1},m,d=r.location.offset>0||r.location.length!==null;if(!r.encryption||r.encryption.method==="SAMPLE-AES"||r.encryption.method==="SAMPLE-AES-CTR"){if(m=await this.input._getSourceCached(l),d){let p=m.source.slice(r.location.offset,r.location.length??void 0).ref();m.free(),m=p}}else if(r.encryption.method==="AES-128"){let f=r.encryption;h(f.iv);let p=await this.input._getSourceCached(l);if(d){let k=p.source.slice(r.location.offset,r.location.length??void 0).ref();p.free(),p=k}let b=new pr(p.source),g=sl(b,async()=>{var S=[];try{let y=qi(S,await this.input._getSourceCached({path:f.keyUri,isRoot:!1},zl));let k=new pr(y.source);let T=await k.requestSlice(0,ct);if(!T)throw new Error("Invalid AES-128 key; expected at least 16 bytes of data.");let w=B(T,ct);return{key:w,iv:f.iv}}catch(x){var P=x,A=!0}finally{Gi(S,P,A)}},()=>{p.free()});m=new kn(g).ref()}else h(!1);return m}),formats:this.input._formats.filter(u=>!(u instanceof Si)),initInput:a??void 0,formatOptions:s});if(o._onFormatDetermined=u=>{if((r.encryption?.method==="SAMPLE-AES"||r.encryption?.method==="SAMPLE-AES-CTR")&&!u._isIsobmff)throw new Error("The SAMPLE-AES and SAMPLE-AES-CTR encryption methods are currently only supported for ISOBMFF files.")},this.inputCache.push({segment:r,input:o,age:this.nextInputCacheAge++}),this.inputCache.length>4){let u=ei(this.inputCache,l=>l.age);h(u!==-1),this.inputCache.splice(u,1)}return o}async getLiveRefreshInterval(){return this.getRemainingWaitTimeMs()===0&&await this.runUpdateSegments(),this.streamHasEnded?null:this.refreshInterval}};var Ya=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.trackBackings=null;this.internalTracks=null;this.segmentedInputs=[];this.hasMasterPlaylist=!0}readMetadata(){return this.metadataPromise??=(async()=>{h(this.input._rootSource instanceof Qe);let{rootPath:e}=this.input._rootSource,r=await this.input._reader.requestEntireFile();h(r);let i=Xa(r,r.length,{ignore:Wa}),a=[],s=[];for(let d=1;d<i.length;d++){let f=i[d];if(f.startsWith(zo)){let p=d,b=i[++d];if(b===void 0)throw new Error("Incorrect M3U8 file; a line must follow the #EXT-X-STREAM-INF tag.");let g=fe(e,b),y=new jt(f.slice(zo.length));if(y.getAsNumber("bandwidth")===null)throw new Error("Invalid M3U8 file; #EXT-X-STREAM-INF tag requires a BANDWIDTH attribute with a valid numerical value.");a.push({fullPath:g,attributes:y,lineNumber:p,hasOnlyKeyPackets:!1})}else if(f.startsWith(No)){let p=new jt(f.slice(No.length)),b=p.get("uri");if(b===null)throw new Error("Invalid M3U8 file; #EXT-X-I-FRAME-STREAM-INF tag requires a URI attribute.");if(p.getAsNumber("bandwidth")===null)throw new Error("Invalid M3U8 file; #EXT-X-I-FRAME-STREAM-INF tag requires a BANDWIDTH attribute with a valid numerical value.");let y=fe(e,b);a.push({fullPath:y,attributes:p,lineNumber:d,hasOnlyKeyPackets:!0})}else if(f.startsWith(Lo)){let p=new jt(f.slice(Lo.length));if(p.get("type")===null)throw new Error("Invalid M3U8 file; #EXT-X-MEDIA tag requires a TYPE attribute.");if(p.get("group-id")===null)throw new Error("Invalid M3U8 file; #EXT-X-MEDIA tag requires a GROUP-ID attribute.");let y=null,k=p.get("uri");k!==null&&(y=fe(e,k)),s.push({fullPath:y,attributes:p,lineNumber:d})}else if(f!==Ol){if(f.startsWith(bn)){let p=new wn(this,e,null,i);this.segmentedInputs=[p],this.hasMasterPlaylist=!1,this.trackBackings=await p.getTrackBackings();return}}}let o=[...new Set(s.filter(d=>d.attributes.get("type").toLowerCase()==="video").map(d=>d.attributes.get("group-id")))],c=[...new Set(s.filter(d=>d.attributes.get("type").toLowerCase()==="audio").map(d=>d.attributes.get("group-id")))],u=await Promise.all(a.map(async(d,f)=>{let p=[],b=d.attributes.get("codecs"),g;if(b)g=b.split(",").map(I=>I.trim());else{let R=await this.getSegmentedInputForPath(d.fullPath).getTrackBackings(),v=await Promise.all(R.map(async E=>({track:E,codec:await E.getCodec()})));g=await Promise.all(v.filter(E=>E.codec!==null).map(E=>E.track.getDecoderConfig().then(D=>D.codec)))}let y=d.attributes.get("video"),k=d.attributes.get("audio"),T=g.some(I=>re.includes(Ge(I))),w=g.some(I=>ae.includes(Ge(I)));if(y!==null&&!T){if(!o.includes(y))throw new Error(`Invalid M3U8 file; variant stream references video group "${y}" which is not defined in any #EXT-X-MEDIA tags.`);let I=s.find(R=>{let v=R.attributes.get("group-id"),E=R.attributes.get("type");return v===y&&E.toLowerCase()==="video"});e:if(I){let R=I.attributes.get("uri");if(R===null)break e;let v=fe(e,R),U=(await this.getSegmentedInputForPath(v).getTrackBackings()).find(q=>q.getType()==="video");if(!U||await U.getCodec()===null)break e;let z=await U.getDecoderConfig().then(q=>q?.codec??null);h(z!==null),g.push(z)}}if(k!==null&&!w){if(!c.includes(k))throw new Error(`Invalid M3U8 file; variant stream references audio group "${k}" which is not defined in any #EXT-X-MEDIA tags.`);let I=s.find(R=>{let v=R.attributes.get("group-id"),E=R.attributes.get("type");return v===k&&E.toLowerCase()==="audio"});e:if(I){let R=I.attributes.get("uri");if(R===null)break e;let v=fe(e,R),U=(await this.getSegmentedInputForPath(v).getTrackBackings()).find(q=>q.getType()==="audio");if(!U||await U.getCodec()===null)break e;let z=await U.getDecoderConfig().then(q=>q?.codec??null);h(z!==null),g.push(z)}}g=[...new Set(g)];let S=null,x=null,P=d.attributes.getAsNumber("bandwidth");h(P!==null);let A=d.attributes.getAsNumber("average-bandwidth"),C=d.attributes.get("name");for(let I of g){let R=Ge(I);if(R!==null){if(re.includes(R)){if(S!==null)throw new Error("Unsupported M3U8 file; multiple video codecs found in the CODECS attribute of a variant stream.");S=I;let v=d.attributes.get("video");if(v===null){let E=d.attributes.get("resolution"),D=null,U=null;if(E){let z=E.match(/^(\d+)x(\d+)$/);z&&(D=Number(z[1]),U=Number(z[2]))}p.push({id:-1,demuxer:this,backingTrack:null,default:!0,autoselect:!0,languageCode:J,lineNumber:d.lineNumber,fullPath:d.fullPath,fullCodecString:S,pairingMask:1n<<BigInt(f),peakBitrate:P,averageBitrate:A,name:C,hasOnlyKeyPackets:d.hasOnlyKeyPackets,info:{type:"video",width:D,height:U}})}else{if(!o.includes(v))throw new Error(`Invalid M3U8 file; variant stream references video group "${v}" which is not defined in any #EXT-X-MEDIA tags.`);for(let E of s){let D=E.attributes.get("group-id"),U=E.attributes.get("type");if(D!==v||U.toLowerCase()!=="video")continue;let z=E.attributes.get("resolution")??d.attributes.get("resolution"),q=null,ke=null;if(z){let Te=z.match(/^(\d+)x(\d+)$/);Te&&(q=Number(Te[1]),ke=Number(Te[2]))}p.push({id:-1,demuxer:this,backingTrack:null,default:$a(E.attributes),autoselect:$a(E.attributes)||Nl(E.attributes),languageCode:Ll(E.attributes.get("language")),lineNumber:E.lineNumber,fullPath:E.fullPath??d.fullPath,fullCodecString:S,pairingMask:1n<<BigInt(f),peakBitrate:null,averageBitrate:null,name:E.attributes.get("name"),hasOnlyKeyPackets:d.hasOnlyKeyPackets,info:{type:"video",width:q,height:ke}})}}}else if(ae.includes(R)){if(x!==null)throw new Error("Unsupported M3U8 file; multiple audio codecs found in the CODECS attribute of a variant stream.");x=I;let v=d.attributes.get("audio");if(v===null){let E=d.attributes.get("channels"),D=E!==null?Number(E.split("/")[0]):null;p.push({id:-1,demuxer:this,backingTrack:null,default:!0,autoselect:!0,languageCode:J,lineNumber:d.lineNumber,fullPath:d.fullPath,fullCodecString:x,pairingMask:1n<<BigInt(f),peakBitrate:P,averageBitrate:A,name:C,hasOnlyKeyPackets:d.hasOnlyKeyPackets,info:{type:"audio",numberOfChannels:D!==null&&Number.isInteger(D)&&D>0?D:null}})}else{if(!c.includes(v))throw new Error(`Invalid M3U8 file; variant stream references audio group "${v}" which is not defined in any #EXT-X-MEDIA tags.`);for(let E of s){let D=E.attributes.get("group-id"),U=E.attributes.get("type");if(D!==v||U.toLowerCase()!=="audio")continue;let z=E.attributes.get("channels")??d.attributes.get("channels"),q=z!==null?Number(z.split("/")[0]):null;p.push({id:-1,demuxer:this,backingTrack:null,default:$a(E.attributes),autoselect:$a(E.attributes)||Nl(E.attributes),languageCode:Ll(E.attributes.get("language")),lineNumber:E.lineNumber,fullPath:E.fullPath??d.fullPath,fullCodecString:x,pairingMask:1n<<BigInt(f),peakBitrate:null,averageBitrate:null,name:E.attributes.get("name"),hasOnlyKeyPackets:d.hasOnlyKeyPackets,info:{type:"audio",numberOfChannels:q!==null&&Number.isInteger(q)&&q>0?q:null}})}}}}}return p})),l=[],m=d=>{let f=l.find(p=>p.fullPath===d.fullPath&&p.info.type===d.info.type);f?(f.pairingMask|=d.pairingMask,f.default||=d.default,f.autoselect||=d.autoselect,f.lineNumber=Math.min(f.lineNumber,d.lineNumber),d.peakBitrate!==null&&(f.peakBitrate=Math.max(f.peakBitrate??-1/0,d.peakBitrate)),d.averageBitrate!==null&&(f.averageBitrate=Math.max(f.averageBitrate??-1/0,d.averageBitrate)),f.languageCode===J&&(f.languageCode=d.languageCode)):(d.id=l.length+1,l.push(d))};for(let d of u)for(let f of d)m(f);l.sort((d,f)=>d.lineNumber-f.lineNumber),this.trackBackings=[];for(let d of l)d.info.type==="video"?this.trackBackings.push(new Ja(d)):this.trackBackings.push(new es(d));this.internalTracks=l})()}async getTrackBackings(){return await this.readMetadata(),h(this.trackBackings),this.trackBackings}getSegmentedInputForPath(e){let r=this.segmentedInputs.find(a=>a.path===e);if(r)return r;let i=null;return this.internalTracks&&(i=this.internalTracks.filter(s=>s.fullPath===e).map(s=>({id:s.id,type:s.info.type}))),r=new wn(this,e,i,null),this.segmentedInputs.push(r),r}async getMetadataTags(){return{}}async getMimeType(){return St}dispose(){if(this.segmentedInputs){for(let e of this.segmentedInputs)e.dispose();this.segmentedInputs.length=0}}},Za=class{constructor(t){this.internalTrack=t;this.hydrationPromise=null}hydrate(){return this.hydrationPromise??=(async()=>{let t=this.internalTrack.demuxer.getSegmentedInputForPath(this.internalTrack.fullPath),e=null,i=(await t.getTrackBackings()).filter(a=>a.getType()===this.getType());if(i.length===1)e=i[0];else if(this instanceof Ja){for(let a of i)if(await a.getCodec()===this.getCodec()){e=a;break}}else{h(this instanceof es);for(let a of i)if(await a.getCodec()===this.getCodec()){e=a;break}}if(!e)throw new Error("Could not find matching track in underlying media data.");this.internalTrack.backingTrack=e})()}delegate(t){return this.internalTrack.backingTrack?t():this.hydrate().then(t)}getCodec(){throw new Error("Not implemented on base class.")}getDisposition(){return{...we,default:this.internalTrack.autoselect,primary:this.internalTrack.default}}getId(){return this.internalTrack.id}getPairingMask(){return this.internalTrack.pairingMask}getInternalCodecId(){return null}getLanguageCode(){return this.internalTrack.languageCode}getName(){return this.internalTrack.name}getNumber(){h(this.internalTrack.demuxer.internalTracks);let t=this.internalTrack.info.type,e=0;for(let r of this.internalTrack.demuxer.internalTracks)if(r.info.type===t&&e++,r===this.internalTrack)break;return e}getTimeResolution(){return this.delegate(()=>this.internalTrack.backingTrack.getTimeResolution())}isRelativeToUnixEpoch(){return this.delegate(()=>this.internalTrack.backingTrack.isRelativeToUnixEpoch())}getBitrate(){return this.internalTrack.peakBitrate}getAverageBitrate(){return this.internalTrack.averageBitrate}async getDurationFromMetadata(t){return await this.hydrate(),this.internalTrack.backingTrack.getDurationFromMetadata(t)}async getLiveRefreshInterval(){return await this.hydrate(),this.internalTrack.backingTrack.getLiveRefreshInterval()}getHasOnlyKeyPackets(){return this.internalTrack.hasOnlyKeyPackets||null}async getFirstPacket(t){return await this.hydrate(),this.internalTrack.backingTrack.getFirstPacket(t)}async getPacket(t,e){return await this.hydrate(),this.internalTrack.backingTrack.getPacket(t,e)}async getKeyPacket(t,e){return await this.hydrate(),this.internalTrack.backingTrack.getKeyPacket(t,e)}async getNextPacket(t,e){return await this.hydrate(),this.internalTrack.backingTrack.getNextPacket(t,e)}async getNextKeyPacket(t,e){return await this.hydrate(),this.internalTrack.backingTrack.getNextKeyPacket(t,e)}},Ja=class extends Za{constructor(t){super(t)}get backingVideoTrack(){return this.internalTrack.backingTrack}getType(){return"video"}getCodec(){return Ge(this.internalTrack.fullCodecString)}getCodedWidth(){return this.delegate(()=>this.backingVideoTrack.getCodedWidth())}getCodedHeight(){return this.delegate(()=>this.backingVideoTrack.getCodedHeight())}getSquarePixelWidth(){return this.delegate(()=>this.backingVideoTrack.getSquarePixelWidth())}getSquarePixelHeight(){return this.delegate(()=>this.backingVideoTrack.getSquarePixelHeight())}getMetadataDisplayWidth(){return this.backingVideoTrack?null:this.internalTrack.info.width}getMetadataDisplayHeight(){return this.backingVideoTrack?null:this.internalTrack.info.height}getRotation(){return this.delegate(()=>this.backingVideoTrack.getRotation())}async getColorSpace(){return await this.hydrate(),this.backingVideoTrack.getColorSpace()}async canBeTransparent(){return await this.hydrate(),this.backingVideoTrack.canBeTransparent()}getMetadataCodecParameterString(){return this.backingVideoTrack?null:this.internalTrack.fullCodecString}async getDecoderConfig(){return await this.hydrate(),this.backingVideoTrack.getDecoderConfig()}},es=class extends Za{constructor(t){super(t)}get backingAudioTrack(){return this.internalTrack.backingTrack}getType(){return"audio"}getCodec(){return Ge(this.internalTrack.fullCodecString)}getNumberOfChannels(){return this.internalTrack.info.numberOfChannels!==null?this.internalTrack.info.numberOfChannels:this.delegate(()=>this.backingAudioTrack.getNumberOfChannels())}getSampleRate(){return this.delegate(()=>this.backingAudioTrack.getSampleRate())}getMetadataCodecParameterString(){return this.backingAudioTrack?null:this.internalTrack.fullCodecString}async getDecoderConfig(){return await this.hydrate(),this.backingAudioTrack.getDecoderConfig()}},$a=n=>{let t=n.get("default");if(t===null)return!1;let e=t.toUpperCase();if(e==="YES")return!0;if(e==="NO")return!1;throw new Error(`Invalid M3U8 file; #EXT-X-MEDIA DEFAULT attribute must be YES or NO, got "${t}".`)},Nl=n=>{let t=n.get("autoselect");if(t===null)return!1;let e=t.toUpperCase();if(e==="YES")return!0;if(e==="NO")return!1;throw new Error(`Invalid M3U8 file; #EXT-X-MEDIA AUTOSELECT attribute must be YES or NO, got "${t}".`)},Ll=n=>{if(n===null)return J;let t=n.split("-")[0];return t||J};var Ve=class{constructor(){this._isIsobmff=!1}},Sn=class extends Ve{constructor(){super(...arguments);this._isIsobmff=!0}async _getMajorBrand(e){let r=e._reader.requestSlice(0,12);if(r instanceof Promise&&(r=await r),!r)return null;r.skip(4);let i=te(r,4);return i!=="ftyp"&&i!=="styp"?null:te(r,4)}_createDemuxer(e){return new ha(e)}},ts=class extends Sn{async _canReadInput(t){let e=await this._getMajorBrand(t);if(e!==null)return e!=="qt ";let r=t._reader.requestSlice(4,4);if(r instanceof Promise&&(r=await r),!r)return!1;let i=te(r,4);return i==="moof"||i==="sidx"}get name(){return"MP4"}get mimeType(){return"video/mp4"}},rs=class extends Sn{async _canReadInput(t){return await this._getMajorBrand(t)==="qt "}get name(){return"QuickTime File Format"}get mimeType(){return"video/quicktime"}},An=class extends Ve{async isSupportedEBMLOfDocType(t,e){let r=t._reader.requestSlice(0,dt);if(r instanceof Promise&&(r=await r),!r)return!1;let i=xo(r);if(i===null||i<1||i>8||G(r,i)!==440786851)return!1;let s=Po(r);if(typeof s!="number")return!1;let o=t._reader.requestSlice(r.filePos,s);if(o instanceof Promise&&(o=await o),!o)return!1;let c=r.filePos;for(;o.filePos<=c+s-Oe;){let u=mt(o);if(!u)break;let{id:l,size:m}=u,d=o.filePos;if(m===void 0)return!1;switch(l){case 17030:if(G(o,m)!==1)return!1;break;case 17143:if(G(o,m)!==1)return!1;break;case 17026:if(mr(o,m)!==e)return!1;break;case 17031:if(G(o,m)>4)return!1;break}o.filePos=d+m}return!0}_canReadInput(t){return this.isSupportedEBMLOfDocType(t,"matroska")}_createDemuxer(t){return new Pa(t)}get name(){return"Matroska"}get mimeType(){return"video/x-matroska"}},is=class extends An{_canReadInput(t){return this.isSupportedEBMLOfDocType(t,"webm")}get name(){return"WebM"}get mimeType(){return"video/webm"}},ns=class extends Ve{async _canReadInput(t){let e=0;for(;;){let m=t._reader.requestSlice(e,ze);if(m instanceof Promise&&(m=await m),!m)break;let d=ft(m);if(!d)break;e=m.filePos+d.size}let r=await mn(t._reader,e,e+4096);if(!r)return!1;let i=r.header,a=cr(i.mpegVersionId,i.channel),s=t._reader.requestSlice(r.startPos+a,4);if(s instanceof Promise&&(s=await s),!s)return!1;let o=F(s);if(o===or||o===ni)return!0;e=r.startPos+r.header.totalSize;let u=await mn(t._reader,e,e+4);if(!u)return!1;let l=u.header;return!(i.channel!==l.channel||i.sampleRate!==l.sampleRate)}_createDemuxer(t){return new Ia(t)}get name(){return"MP3"}get mimeType(){return"audio/mpeg"}},as=class extends Ve{async _canReadInput(t){let e=t._reader.requestSlice(0,12);if(e instanceof Promise&&(e=await e),!e)return!1;let r=te(e,4);return r!=="RIFF"&&r!=="RIFX"&&r!=="RF64"?!1:(e.skip(4),te(e,4)==="WAVE")}_createDemuxer(t){return new Ma(t)}get name(){return"WAVE"}get mimeType(){return"audio/wav"}},ss=class extends Ve{async _canReadInput(t){let e=t._reader.requestSlice(0,4);return e instanceof Promise&&(e=await e),e?te(e,4)==="OggS":!1}_createDemuxer(t){return new Fa(t)}get name(){return"Ogg"}get mimeType(){return"application/ogg"}},os=class extends Ve{async _canReadInput(t){let e=t._reader.requestSlice(0,4);return e instanceof Promise&&(e=await e),e?te(e,4)==="fLaC":!1}get name(){return"FLAC"}get mimeType(){return"audio/flac"}_createDemuxer(t){return new Ua(t)}},cs=class extends Ve{async _canReadInput(t){let e=0;for(;;){let s=t._reader.requestSlice(e,ze);if(s instanceof Promise&&(s=await s),!s)break;let o=ft(s);if(!o)break;e=s.filePos+o.size}let r=t._reader.requestSliceRange(e,Qt,Ne);if(r instanceof Promise&&(r=await r),!r)return!1;let i=De(r);if(!i||(e+=i.frameLength,r=t._reader.requestSliceRange(e,Qt,Ne),r instanceof Promise&&(r=await r),!r))return!1;let a=De(r);return a?i.objectType===a.objectType&&i.samplingFrequencyIndex===a.samplingFrequencyIndex&&i.channelConfiguration===a.channelConfiguration:!1}_createDemuxer(t){return new Ba(t)}get name(){return"ADTS"}get mimeType(){return"audio/aac"}},us=class extends Ve{async _canReadInput(t){let e=205,r=t._reader.requestSlice(0,e);if(r instanceof Promise&&(r=await r),!r)return!1;let i=B(r,e);return i[0]===71&&i[188]===71||i[0]===71&&i[204]===71?!0:i[4]===71&&i[196]===71}_createDemuxer(t){return new Na(t)}get name(){return"MPEG Transport Stream"}get mimeType(){return"video/MP2T"}},Si=class extends Ve{async _canReadInput(t){let e=t._reader.requestSlice(0,7);if(e instanceof Promise&&(e=await e),!e||!(te(e,7)==="#EXTM3U"))return!1;if(!(t._rootSource instanceof Qe))throw new TypeError("HLS inputs require `InputOptions.source` to be a PathedSource or a ref to one.");return t._rootSource._usedForHls=!0,!0}_createDemuxer(t){return new Ya(t)}get name(){return"HTTP Live Streaming (HLS)"}get mimeType(){return St}},nc=new ts,ac=new rs,Wl=new An,Hl=new is,sc=new ns,ql=new as,Gl=new ss,oc=new cs,Kl=new os,cc=new us,uc=new Si,Vm=[uc,nc,ac,Wl,Hl,ql,Gl,Kl,sc,oc,cc],Um=[uc,nc,ac,sc,oc,cc],Ql=(n,t)=>{if(!n||typeof n!="object")throw new TypeError(`${t}, when provided, must be an object.`);if(n.isobmff!==void 0){if(!n.isobmff||typeof n.isobmff!="object")throw new TypeError(`${t}.isobmff, when provided, must be an object.`);if(n.isobmff.resolveKeyId!==void 0&&typeof n.isobmff.resolveKeyId!="function")throw new TypeError(`${t}.isobmff.resolveKeyId, when provided, must be a function.`)}};var ls=new Map,ds=new Map,zm=(n,t)=>{if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.codec!==void 0&&typeof t.codec!="string")throw new TypeError("options.codec, when provided, must be a string.");if(t.codec!==void 0&&Ge(t.codec)!==n)throw new TypeError(`options.codec, when provided, must match the specified codec (${n}).`);if(t.codedWidth!==void 0&&(!Number.isInteger(t.codedWidth)||t.codedWidth<=0))throw new TypeError("options.codedWidth, when provided, must be a positive integer.");if(t.codedHeight!==void 0&&(!Number.isInteger(t.codedHeight)||t.codedHeight<=0))throw new TypeError("options.codedHeight, when provided, must be a positive integer.");if(t.displayAspectWidth!==void 0&&(!Number.isInteger(t.displayAspectWidth)||t.displayAspectWidth<=0))throw new TypeError("options.displayAspectWidth, when provided, must be a positive integer.");if(t.displayAspectHeight!==void 0&&(!Number.isInteger(t.displayAspectHeight)||t.displayAspectHeight<=0))throw new TypeError("options.displayAspectHeight, when provided, must be a positive integer.");if(t.description!==void 0&&!Rt(t.description))throw new TypeError("options.description, when provided, must be a buffer source.");if(t.hardwareAcceleration!==void 0&&!["no-preference","prefer-hardware","prefer-software"].includes(t.hardwareAcceleration))throw new TypeError("options.hardwareAcceleration, when provided, must be 'no-preference', 'prefer-hardware' or 'prefer-software'.");if(t.optimizeForLatency!==void 0&&typeof t.optimizeForLatency!="boolean")throw new TypeError("options.optimizeForLatency, when provided, must be a boolean.")},Nm=(n,t)=>{if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.codec!==void 0&&typeof t.codec!="string")throw new TypeError("options.codec, when provided, must be a string.");if(t.codec!==void 0&&Ge(t.codec)!==n)throw new TypeError(`options.codec, when provided, must match the specified codec (${n}).`);if(t.numberOfChannels!==void 0&&(!Number.isInteger(t.numberOfChannels)||t.numberOfChannels<=0))throw new TypeError("options.numberOfChannels, when provided, must be a positive integer.");if(t.sampleRate!==void 0&&(!Number.isInteger(t.sampleRate)||t.sampleRate<=0))throw new TypeError("options.sampleRate, when provided, must be a positive integer.");if(t.description!==void 0&&!Rt(t.description))throw new TypeError("options.description, when provided, must be a buffer source.")},Lm=n=>re.includes(n)?lc(n):ae.includes(n)?dc(n):!1,lc=async(n,t={})=>{if(!re.includes(n))return!1;zm(n,t);let e={...t,codedWidth:t.codedWidth??1280,codedHeight:t.codedHeight??720,codec:t.codec??Kn(n,1280,720,1e6)};e.description??=Vu(e);let r=JSON.stringify(e),i=ls.get(r);if(i)return i;let a=(async()=>hr.some(o=>o.supports(n,e))?!0:typeof VideoDecoder>"u"?!1:(await VideoDecoder.isConfigSupported(e)).supported===!0)();return ls.set(r,a),a},dc=async(n,t={})=>{if(!ae.includes(n))return!1;Nm(n,t);let e={...t,numberOfChannels:t.numberOfChannels??2,sampleRate:t.sampleRate??48e3,codec:t.codec??jn(n,2,48e3)};if(e.description===void 0){let s=Uu(e);if(s===!1)return!1;e.description=s}let r=JSON.stringify(e),i=ds.get(r);if(i)return i;let a=(async()=>gr.some(o=>o.supports(n,e))||ee.includes(n)?!0:typeof AudioDecoder>"u"?!1:(await AudioDecoder.isConfigSupported(e)).supported===!0)();return ds.set(r,a),a},Wm=async()=>{let[n,t]=await Promise.all([jl(),Xl()]);return[...n,...t]},jl=async(n=re,t)=>{let e=await Promise.all(n.map(r=>lc(r,t)));return n.filter((r,i)=>e[i])},Xl=async(n=ae,t)=>{let e=await Promise.all(n.map(r=>dc(r,t)));return n.filter((r,i)=>e[i])};Jr();var $l=-1/0,Yl=-1/0,In=null;typeof FinalizationRegistry<"u"&&(In=new FinalizationRegistry(n=>{let t=performance.now();n.type==="video"?(t-$l>=1e3&&(console.error("A VideoSample was garbage collected without first being closed. For proper resource management, make sure to call close() on all your VideoSamples as soon as you're done using them."),$l=t),typeof VideoFrame<"u"&&n.data instanceof VideoFrame&&n.data.close()):(t-Yl>=1e3&&(console.error("An AudioSample was garbage collected without first being closed. For proper resource management, make sure to call close() on all your AudioSamples as soon as you're done using them."),Yl=t),typeof AudioData<"u"&&n.data instanceof AudioData&&n.data.close())}));var At=class{constructor(){this._referenceCount=0;this._lastAllocationBuffer=null}},fs=["I420","I420P10","I420P12","I420A","I420AP10","I420AP12","I422","I422P10","I422P12","I422A","I422AP10","I422AP12","I444","I444P10","I444P12","I444A","I444AP10","I444AP12","NV12","RGBA","RGBX","BGRA","BGRX"],Hm=new Set(fs),Ae=class n{constructor(t,e){this._closed=!1;if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer||ArrayBuffer.isView(t)){if(!e||typeof e!="object")throw new TypeError("init must be an object.");if(e.format===void 0||!Hm.has(e.format))throw new TypeError("init.format must be one of: "+fs.join(", "));if(!Number.isInteger(e.codedWidth)||e.codedWidth<=0)throw new TypeError("init.codedWidth must be a positive integer.");if(!Number.isInteger(e.codedHeight)||e.codedHeight<=0)throw new TypeError("init.codedHeight must be a positive integer.");if(e.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(!Number.isFinite(e.timestamp))throw new TypeError("init.timestamp must be a number.");if(e.duration!==void 0&&(!Number.isFinite(e.duration)||e.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");if(e.layout!==void 0){if(!Array.isArray(e.layout))throw new TypeError("init.layout, when provided, must be an array.");for(let i of e.layout){if(!i||typeof i!="object"||Array.isArray(i))throw new TypeError("Each entry in init.layout must be an object.");if(!Number.isInteger(i.offset)||i.offset<0)throw new TypeError("plane.offset must be a non-negative integer.");if(!Number.isInteger(i.stride)||i.stride<0)throw new TypeError("plane.stride must be a non-negative integer.")}}if(e.visibleRect!==void 0&&po(e.visibleRect,"init.visibleRect"),e.displayWidth!==void 0&&(!Number.isInteger(e.displayWidth)||e.displayWidth<=0))throw new TypeError("init.displayWidth, when provided, must be a positive integer.");if(e.displayHeight!==void 0&&(!Number.isInteger(e.displayHeight)||e.displayHeight<=0))throw new TypeError("init.displayHeight, when provided, must be a positive integer.");if(e.displayWidth!==void 0!=(e.displayHeight!==void 0))throw new TypeError("init.displayWidth and init.displayHeight must be either both provided or both omitted.");this._data=j(t).slice(),this._layout=e.layout??Km(e.format,e.codedWidth,e.codedHeight),this.format=e.format,this.rotation=e.rotation??0,this.timestamp=e.timestamp,this.duration=e.duration??0;let r=e.colorSpace??null;r===null&&(this.format==="RGBA"||this.format==="RGBX"||this.format==="BGRA"||this.format==="BGRX"?r={primaries:"bt709",transfer:"iec61966-2-1",matrix:"rgb",fullRange:!0}:r={primaries:"bt709",transfer:"bt709",matrix:"bt709",fullRange:!1}),this.colorSpace=new xi(r),this.visibleRect={left:e.visibleRect?.left??0,top:e.visibleRect?.top??0,width:e.visibleRect?.width??e.codedWidth,height:e.visibleRect?.height??e.codedHeight},e.displayWidth!==void 0?(this.squarePixelWidth=this.rotation%180===0?e.displayWidth:e.displayHeight,this.squarePixelHeight=this.rotation%180===0?e.displayHeight:e.displayWidth):(this.squarePixelWidth=this.visibleRect.width,this.squarePixelHeight=this.visibleRect.height)}else if(typeof VideoFrame<"u"&&t instanceof VideoFrame){if(e?.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(e?.timestamp!==void 0&&!Number.isFinite(e?.timestamp))throw new TypeError("init.timestamp, when provided, must be a number.");if(e?.duration!==void 0&&(!Number.isFinite(e.duration)||e.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");e?.visibleRect!==void 0&&po(e.visibleRect,"init.visibleRect"),this._data=t,this._layout=null,this.format=t.format,this.visibleRect={left:t.visibleRect?.x??0,top:t.visibleRect?.y??0,width:t.visibleRect?.width??t.codedWidth,height:t.visibleRect?.height??t.codedHeight},this.rotation=e?.rotation??0,this.squarePixelWidth=t.displayWidth,this.squarePixelHeight=t.displayHeight,this.timestamp=e?.timestamp??t.timestamp/1e6,this.duration=e?.duration??(t.duration??0)/1e6,this.colorSpace=new xi(t.colorSpace)}else if(typeof HTMLImageElement<"u"&&t instanceof HTMLImageElement||typeof SVGImageElement<"u"&&t instanceof SVGImageElement||typeof ImageBitmap<"u"&&t instanceof ImageBitmap||typeof HTMLVideoElement<"u"&&t instanceof HTMLVideoElement||typeof HTMLCanvasElement<"u"&&t instanceof HTMLCanvasElement||typeof OffscreenCanvas<"u"&&t instanceof OffscreenCanvas){if(!e||typeof e!="object")throw new TypeError("init must be an object.");if(e.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(!Number.isFinite(e.timestamp))throw new TypeError("init.timestamp must be a number.");if(e.duration!==void 0&&(!Number.isFinite(e.duration)||e.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");if(typeof VideoFrame<"u")return new n(new VideoFrame(t,{timestamp:Math.trunc(e.timestamp*yt),duration:Math.trunc((e.duration??0)*yt)||void 0}),e);let r=0,i=0;if("naturalWidth"in t?(r=t.naturalWidth,i=t.naturalHeight):"videoWidth"in t?(r=t.videoWidth,i=t.videoHeight):"width"in t&&(r=Number(t.width),i=Number(t.height)),!r||!i)throw new TypeError("Could not determine dimensions.");let a=new OffscreenCanvas(r,i),s=a.getContext("2d",{alpha:Er(),willReadFrequently:!0});h(s),s.drawImage(t,0,0),this._data=a,this._layout=null,this.format="RGBX",this.visibleRect={left:0,top:0,width:r,height:i},this.squarePixelWidth=r,this.squarePixelHeight=i,this.rotation=e.rotation??0,this.timestamp=e.timestamp,this.duration=e.duration??0,this.colorSpace=new xi({matrix:"rgb",primaries:"bt709",transfer:"iec61966-2-1",fullRange:!0})}else if(t instanceof At){if(!e||typeof e!="object")throw new TypeError("init must be an object.");if(e.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(!Number.isFinite(e.timestamp))throw new TypeError("init.timestamp must be a number.");if(e.duration!==void 0&&(!Number.isFinite(e.duration)||e.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");if(this._data=t,t._referenceCount++,this.format=t.getFormat(),this.format!==null&&!fs.includes(this.format))throw new TypeError("getFormat() must return a VideoSamplePixelFormat or null.");if(this.visibleRect={left:0,top:0,width:t.getCodedWidth(),height:t.getCodedHeight()},!Number.isInteger(this.visibleRect.width)||this.visibleRect.width<=0)throw new TypeError("getCodedWidth() must return a positive integer.");if(!Number.isInteger(this.visibleRect.height)||this.visibleRect.height<=0)throw new TypeError("getCodedHeight() must return a positive integer.");if(this.squarePixelWidth=t.getSquarePixelWidth(),!Number.isInteger(this.squarePixelWidth)||this.squarePixelWidth<=0)throw new TypeError("getSquarePixelWidth() must return a positive integer.");if(this.squarePixelHeight=t.getSquarePixelHeight(),!Number.isInteger(this.squarePixelHeight)||this.squarePixelHeight<=0)throw new TypeError("getSquarePixelHeight() must return a positive integer.");this.rotation=e.rotation??0,this.timestamp=e.timestamp,this.duration=e.duration??0,this.colorSpace=t.getColorSpace()}else throw new TypeError("Invalid data type: Must be a BufferSource, CanvasImageSource, or VideoSampleResource.");this.pixelAspectRatio=Ot({num:this.squarePixelWidth*this.codedHeight,den:this.squarePixelHeight*this.codedWidth}),In?.register(this,{type:"video",data:this._data},this)}get codedWidth(){return this.visibleRect.width}get codedHeight(){return this.visibleRect.height}get displayWidth(){return this.rotation%180===0?this.squarePixelWidth:this.squarePixelHeight}get displayHeight(){return this.rotation%180===0?this.squarePixelHeight:this.squarePixelWidth}get microsecondTimestamp(){return Math.trunc(yt*this.timestamp)}get microsecondDuration(){return Math.trunc(yt*this.duration)}get hasAlpha(){return this.format&&this.format.includes("A")}clone(){if(this._closed)throw new Error("VideoSample is closed.");return h(this._data!==null),this._data instanceof At?new n(this._data,{timestamp:this.timestamp,duration:this.duration,rotation:this.rotation}):Pn(this._data)?new n(this._data.clone(),{timestamp:this.timestamp,duration:this.duration,rotation:this.rotation}):this._data instanceof Uint8Array?(h(this._layout),new n(this._data,{format:this.format,layout:this._layout,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.timestamp,duration:this.duration,colorSpace:this.colorSpace,rotation:this.rotation,visibleRect:this.visibleRect,displayWidth:this.displayWidth,displayHeight:this.displayHeight})):new n(this._data,{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.timestamp,duration:this.duration,colorSpace:this.colorSpace,rotation:this.rotation,visibleRect:this.visibleRect,displayWidth:this.displayWidth,displayHeight:this.displayHeight})}close(){this._closed||(In?.unregister(this),this._data instanceof At?(this._data._referenceCount--,this._data._referenceCount===0&&this._data.close()):Pn(this._data)?this._data.close():this._data=null,this._closed=!0)}allocationSize(t={}){if(Jl(t),this._closed)throw new Error("VideoSample is closed.");if((t.format??this.format)==null)throw new Error("Cannot get allocation size when format is null.");return Pn(this._data)?this._data.allocationSize(t):ed(this,t).allocationSize}async copyTo(t,e={}){if(!Rt(t))throw new TypeError("destination must be an ArrayBuffer or an ArrayBuffer view.");if(Jl(e),this._closed)throw new Error("VideoSample is closed.");if((e.format??this.format)==null)throw new Error("Cannot copy video sample data when format is null.");if(h(this._data!==null),Pn(this._data))return this._data.copyTo(t,e);if(e.format&&!["RGBA","RGBX","BGRA","BGRX"].includes(this.format)&&["RGBA","RGBX","BGRA","BGRX"].includes(e.format))if(this._data instanceof At){var u=[];try{let f=qi(u,await this._data.toRgbSample({timestamp:this.timestamp,duration:this.duration,rotation:this.rotation},e.colorSpace??"srgb"));if(!(f instanceof n))throw new TypeError("toRgbSample() must return a VideoSample.");if(!["RGBA","RGBX","BGRA","BGRX"].includes(f.format))throw new Error(`Sample returned by toRgbSample was expected to have an RGB format, got '${f.format}' instead.`);return await f.copyTo(t,e)}catch(l){var m=l,d=!0}finally{Gi(u,m,d)}}else{if(typeof VideoFrame>"u")throw new Error("For this sample, converting from a non-RGB to an RGB format requires VideoFrame to be defined.");let f=this.toVideoFrame(),p=await f.copyTo(t,e);return f.close(),p}let r=ed(this,e);h(this.format);let i=j(t);if(i.byteLength<r.allocationSize)throw new TypeError(`Destination buffer too small. Required: ${r.allocationSize}, Available: ${i.byteLength}`);let a=ps(this.format),s;if(this._data instanceof At){let f=this._data.getDataPlanes();if(f instanceof Promise&&(f=await f),!Array.isArray(f)||f.some(p=>!(p.data instanceof Uint8Array)||!Number.isInteger(p.stride)||p.stride<0))throw new TypeError('getDataPlanes() must return an array of objects with a Uint8Array "data" property and a non-negative integer "stride" property.');s=f}else if(this._data instanceof Uint8Array)h(this._layout),h(this._layout.length===a.length),s=this._layout.map((f,p)=>{let b=Math.ceil(this.codedHeight/a[p].heightDivisor);return{data:this._data.subarray(f.offset,f.offset+f.stride*b),stride:f.stride}});else{let p=this._data.getContext("2d");h(p);let b=p.getImageData(0,0,this.codedWidth,this.codedHeight);s=[{data:j(b.data),stride:4*this.codedWidth}]}let o=[],c=a.length;for(let f=0;f<c;f++){let p=r.computedLayouts[f],b=s[f].stride,g=s[f].data,y=p.sourceTop*b;y+=p.sourceLeftBytes;let k=p.destinationOffset,T=p.sourceWidthBytes,w={offset:k,stride:p.destinationStride};for(let S=0;S<p.sourceHeight;S++){if(y+T>g.byteLength)throw new Error("Source buffer OOB read.");if(k+T>i.byteLength)throw new Error("Destination buffer OOB write.");let x=g.subarray(y,y+T);i.set(x,k),y+=b,k+=p.destinationStride}o.push(w)}if(e.format!==void 0){let f=this.format.startsWith("RGB")!==e.format.startsWith("RGB"),p=this.format.includes("X")&&e.format.includes("A");if(f||p)for(let b=0;b<r.allocationSize;b+=4){if(f){let g=i[b],y=i[b+2];i[b]=y,i[b+2]=g}p&&(i[b+3]=255)}}return o}toVideoFrame(){if(this._closed)throw new Error("VideoSample is closed.");if(h(this._data!==null),this._data instanceof At){if(this.format===null)throw new Error("Cannot convert a VideoSampleResource-backed VideoSample to VideoFrame if format is null.");let t=this._data.getDataPlanes();if(t instanceof Promise)throw new Error("Cannot convert a VideoSampleResource-backed VideoSample to VideoFrame if getDataPlanes() returns a promise.");let e=t.reduce((s,o)=>s+o.data.byteLength,0),r=new Uint8Array(e),i=0,a=[];for(let s of t)r.set(s.data,i),a.push(i),i+=s.data.byteLength;return new VideoFrame(r,{format:this.format,layout:t.map((s,o)=>({offset:a[o],stride:s.stride})),codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration,colorSpace:this.colorSpace,displayWidth:this.squarePixelWidth,displayHeight:this.squarePixelHeight})}else return Pn(this._data)?new VideoFrame(this._data,{timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0}):this._data instanceof Uint8Array?new VideoFrame(this._data,{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0,colorSpace:this.colorSpace,displayWidth:this.squarePixelWidth,displayHeight:this.squarePixelHeight}):new VideoFrame(this._data,{timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0})}draw(t,e,r,i,a,s,o,c,u){let l=0,m=0,d=this.displayWidth,f=this.displayHeight,p=0,b=0,g=this.displayWidth,y=this.displayHeight;if(s!==void 0?(l=e,m=r,d=i,f=a,p=s,b=o,c!==void 0?(g=c,y=u):(g=d,y=f)):(p=e,b=r,i!==void 0&&(g=i,y=a)),!(typeof CanvasRenderingContext2D<"u"&&t instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<"u"&&t instanceof OffscreenCanvasRenderingContext2D))throw new TypeError("context must be a CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D.");if(!Number.isFinite(l))throw new TypeError("sx must be a number.");if(!Number.isFinite(m))throw new TypeError("sy must be a number.");if(!Number.isFinite(d)||d<0)throw new TypeError("sWidth must be a non-negative number.");if(!Number.isFinite(f)||f<0)throw new TypeError("sHeight must be a non-negative number.");if(!Number.isFinite(p))throw new TypeError("dx must be a number.");if(!Number.isFinite(b))throw new TypeError("dy must be a number.");if(!Number.isFinite(g)||g<0)throw new TypeError("dWidth must be a non-negative number.");if(!Number.isFinite(y)||y<0)throw new TypeError("dHeight must be a non-negative number.");if(this._closed)throw new Error("VideoSample is closed.");({sx:l,sy:m,sWidth:d,sHeight:f}=this._rotateSourceRegion(l,m,d,f,this.rotation));let k=this.toCanvasImageSource();t.save();let T=p+g/2,w=b+y/2;t.translate(T,w),t.rotate(this.rotation*Math.PI/180);let S=this.rotation%180===0?1:g/y;t.scale(1/S,S),t.drawImage(k,l,m,d,f,-g/2,-y/2,g,y),t.restore()}drawWithFit(t,e){if(!(typeof CanvasRenderingContext2D<"u"&&t instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<"u"&&t instanceof OffscreenCanvasRenderingContext2D))throw new TypeError("context must be a CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D.");if(!e||typeof e!="object")throw new TypeError("options must be an object.");if(!["fill","contain","cover"].includes(e.fit))throw new TypeError("options.fit must be 'fill', 'contain', or 'cover'.");if(e.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("options.rotation, when provided, must be 0, 90, 180, or 270.");e.crop!==void 0&&yr(e.crop,"options.");let r=t.canvas.width,i=t.canvas.height,a=e.rotation??this.rotation,[s,o]=a%180===0?[this.squarePixelWidth,this.squarePixelHeight]:[this.squarePixelHeight,this.squarePixelWidth],c=e.crop;c&&(c=Pi(c,s,o));let u,l,m,d,{sx:f,sy:p,sWidth:b,sHeight:g}=this._rotateSourceRegion(e.crop?.left??0,e.crop?.top??0,e.crop?.width??s,e.crop?.height??o,a);if(e.fit==="fill")u=0,l=0,m=r,d=i;else{let[k,T]=e.crop?[e.crop.width,e.crop.height]:[s,o],w=e.fit==="contain"?Math.min(r/k,i/T):Math.max(r/k,i/T);m=k*w,d=T*w,u=(r-m)/2,l=(i-d)/2}t.save();let y=a%180===0?1:m/d;t.translate(r/2,i/2),t.rotate(a*Math.PI/180),t.scale(1/y,y),t.translate(-r/2,-i/2),t.drawImage(this.toCanvasImageSource(),f,p,b,g,u,l,m,d),t.restore()}_rotateSourceRegion(t,e,r,i,a){return a===90?[t,e,r,i]=[e,this.squarePixelHeight-t-r,i,r]:a===180?[t,e]=[this.squarePixelWidth-t-r,this.squarePixelHeight-e-i]:a===270&&([t,e,r,i]=[this.squarePixelWidth-e-i,t,i,r]),{sx:t,sy:e,sWidth:r,sHeight:i}}toCanvasImageSource(){if(this._closed)throw new Error("VideoSample is closed.");if(h(this._data!==null),this._data instanceof At||this._data instanceof Uint8Array){let t=this.toVideoFrame();return queueMicrotask(()=>t.close()),t}else return this._data}async transform(t){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.width!==void 0&&(!Number.isInteger(t.width)||t.width<=0))throw new TypeError("options.width, when provided, must be a positive integer.");if(t.height!==void 0&&(!Number.isInteger(t.height)||t.height<=0))throw new TypeError("options.height, when provided, must be a positive integer.");if(t.roundDimensionsTo!==void 0&&(!Number.isInteger(t.roundDimensionsTo)||t.roundDimensionsTo<=0))throw new TypeError("options.roundDimensionsTo, when provided, must be a positive integer.");if(t.fit!==void 0&&!["fill","contain","cover"].includes(t.fit))throw new TypeError('options.fit, when provided, must be one of "fill", "contain", or "cover".');if(t.width!==void 0&&t.height!==void 0&&t.fit===void 0)throw new TypeError("When both options.width and options.height are provided, options.fit must also be provided.");if(t.rotate!==void 0&&![0,90,180,270].includes(t.rotate))throw new TypeError("options.rotate, when provided, must be 0, 90, 180 or 270.");if(t.crop!==void 0&&yr(t.crop,"options."),t.alpha!==void 0&&!["keep","discard"].includes(t.alpha))throw new TypeError("options.alpha, when provided, must be 'keep' or 'discard'.");let e=Ze(this.rotation+(t.rotate??0)),[r,i]=e%180===0?[this.squarePixelWidth,this.squarePixelHeight]:[this.squarePixelHeight,this.squarePixelWidth],a=t.crop;a&&(a=Pi(a,r,i));let s=a?a.width:r,o=a?a.height:i,c=s/o,u,l;t.width!==void 0&&t.height===void 0?(u=t.width,l=u/c):t.width===void 0&&t.height!==void 0?(l=t.height,u=l*c):t.width!==void 0&&t.height!==void 0?(u=t.width,l=t.height):(u=s,l=o),u=ji(u,t.roundDimensionsTo??1),l=ji(l,t.roundDimensionsTo??1);let m={width:u,height:l,fit:t.fit??"fill",rotation:e,crop:a??{left:0,top:0,width:r,height:i},alpha:t.alpha??"keep"};for(let b of mc){let g=b(this,m);if(g instanceof Promise&&(g=await g),g!==null)return g}let d=null,f=!1;for(let b of xn)if(b.canvas.width===m.width&&b.canvas.height===m.height){d=b.canvas,b.age=Zl++;break}if(d===null){if(typeof OffscreenCanvas<"u")d=new OffscreenCanvas(m.width,m.height);else{if(typeof window>"u"||typeof document>"u")throw new Error("Cannot transform VideoSamples in this environment. Either run in an environment with OffscreenCanvas or HTMLCanvasElement, or supply a custom VideoSample transformer using registerVideoSampleTransformer().");d=document.createElement("canvas"),d.width=m.width,d.height=m.height}f=!0,xn.length>=Gm&&xn.splice(ei(xn,b=>b.age),1),xn.push({canvas:d,age:Zl++})}let p=d.getContext("2d",{alpha:!0});return h(p),m.alpha==="discard"?(p.fillStyle="black",p.fillRect(0,0,m.width,m.height)):f||p.clearRect(0,0,m.width,m.height),this.drawWithFit(p,{fit:m.fit,rotation:m.rotation,crop:m.crop}),new n(d,{timestamp:this.timestamp,duration:this.duration,rotation:0})}setRotation(t){if(![0,90,180,270].includes(t))throw new TypeError("newRotation must be 0, 90, 180, or 270.");this.rotation=t}setTimestamp(t){if(!Number.isFinite(t))throw new TypeError("newTimestamp must be a number.");this.timestamp=t}setDuration(t){if(!Number.isFinite(t)||t<0)throw new TypeError("newDuration must be a non-negative number.");this.duration=t}[Symbol.dispose](){this.close()}},mc=[],qm=n=>{mc.includes(n)||mc.push(n)},Gm=3,xn=[],Zl=0,xi=class{constructor(t){if(t!==void 0){if(!t||typeof t!="object")throw new TypeError("init.colorSpace, when provided, must be an object.");let e=Object.keys(et);if(t.primaries!=null&&!e.includes(t.primaries))throw new TypeError(`init.colorSpace.primaries, when provided, must be one of ${e.join(", ")}.`);let r=Object.keys(tt);if(t.transfer!=null&&!r.includes(t.transfer))throw new TypeError(`init.colorSpace.transfer, when provided, must be one of ${r.join(", ")}.`);let i=Object.keys(rt);if(t.matrix!=null&&!i.includes(t.matrix))throw new TypeError(`init.colorSpace.matrix, when provided, must be one of ${i.join(", ")}.`);if(t.fullRange!=null&&typeof t.fullRange!="boolean")throw new TypeError("init.colorSpace.fullRange, when provided, must be a boolean.")}this.primaries=t?.primaries??null,this.transfer=t?.transfer??null,this.matrix=t?.matrix??null,this.fullRange=t?.fullRange??null}toJSON(){return{primaries:this.primaries,transfer:this.transfer,matrix:this.matrix,fullRange:this.fullRange}}},Pn=n=>typeof VideoFrame<"u"&&n instanceof VideoFrame,Pi=(n,t,e)=>{let r=Math.min(n.left,t),i=Math.min(n.top,e),a=Math.min(n.width,t-r),s=Math.min(n.height,e-i);return h(a>=0),h(s>=0),{left:r,top:i,width:a,height:s}},yr=(n,t)=>{if(!n||typeof n!="object")throw new TypeError(t+"crop, when provided, must be an object.");if(!Number.isInteger(n.left)||n.left<0)throw new TypeError(t+"crop.left must be a non-negative integer.");if(!Number.isInteger(n.top)||n.top<0)throw new TypeError(t+"crop.top must be a non-negative integer.");if(!Number.isInteger(n.width)||n.width<0)throw new TypeError(t+"crop.width must be a non-negative integer.");if(!Number.isInteger(n.height)||n.height<0)throw new TypeError(t+"crop.height must be a non-negative integer.")},Jl=n=>{if(!n||typeof n!="object")throw new TypeError("options must be an object.");if(n.colorSpace!==void 0&&!["display-p3","srgb"].includes(n.colorSpace))throw new TypeError("options.colorSpace, when provided, must be 'display-p3' or 'srgb'.");if(n.format!==void 0&&typeof n.format!="string")throw new TypeError("options.format, when provided, must be a string.");if(n.layout!==void 0){if(!Array.isArray(n.layout))throw new TypeError("options.layout, when provided, must be an array.");for(let t of n.layout){if(!t||typeof t!="object")throw new TypeError("Each entry in options.layout must be an object.");if(!Number.isInteger(t.offset)||t.offset<0)throw new TypeError("plane.offset must be a non-negative integer.");if(!Number.isInteger(t.stride)||t.stride<0)throw new TypeError("plane.stride must be a non-negative integer.")}}if(n.rect!==void 0){if(!n.rect||typeof n.rect!="object")throw new TypeError("options.rect, when provided, must be an object.");if(n.rect.x!==void 0&&(!Number.isInteger(n.rect.x)||n.rect.x<0))throw new TypeError("options.rect.x, when provided, must be a non-negative integer.");if(n.rect.y!==void 0&&(!Number.isInteger(n.rect.y)||n.rect.y<0))throw new TypeError("options.rect.y, when provided, must be a non-negative integer.");if(n.rect.width!==void 0&&(!Number.isInteger(n.rect.width)||n.rect.width<0))throw new TypeError("options.rect.width, when provided, must be a non-negative integer.");if(n.rect.height!==void 0&&(!Number.isInteger(n.rect.height)||n.rect.height<0))throw new TypeError("options.rect.height, when provided, must be a non-negative integer.")}},Km=(n,t,e)=>{let r=ps(n),i=[],a=0;for(let s of r){let o=Math.ceil(t/s.widthDivisor),c=Math.ceil(e/s.heightDivisor),u=o*s.sampleBytes,l=u*c;i.push({offset:a,stride:u}),a+=l}return i},ps=n=>{let t=(e,r,i,a,s)=>{let o=[{sampleBytes:e,widthDivisor:1,heightDivisor:1},{sampleBytes:r,widthDivisor:i,heightDivisor:a},{sampleBytes:r,widthDivisor:i,heightDivisor:a}];return s&&o.push({sampleBytes:e,widthDivisor:1,heightDivisor:1}),o};switch(n){case"I420":return t(1,1,2,2,!1);case"I420P10":case"I420P12":return t(2,2,2,2,!1);case"I420A":return t(1,1,2,2,!0);case"I420AP10":case"I420AP12":return t(2,2,2,2,!0);case"I422":return t(1,1,2,1,!1);case"I422P10":case"I422P12":return t(2,2,2,1,!1);case"I422A":return t(1,1,2,1,!0);case"I422AP10":case"I422AP12":return t(2,2,2,1,!0);case"I444":return t(1,1,1,1,!1);case"I444P10":case"I444P12":return t(2,2,1,1,!1);case"I444A":return t(1,1,1,1,!0);case"I444AP10":case"I444AP12":return t(2,2,1,1,!0);case"NV12":return[{sampleBytes:1,widthDivisor:1,heightDivisor:1},{sampleBytes:2,widthDivisor:2,heightDivisor:2}];case"RGBA":case"RGBX":case"BGRA":case"BGRX":return[{sampleBytes:4,widthDivisor:1,heightDivisor:1}];default:ne(n),h(!1)}},ed=(n,t)=>{let e={left:0,top:0,width:n.codedWidth,height:n.codedHeight},r=t.rect,i=Qm(e,r,n.codedWidth,n.codedHeight,n.format),a=t.layout,s;if(!t.format||t.format===n.format)s=n.format;else if(["RGBA","RGBX","BGRA","BGRX"].includes(t.format))s=t.format;else throw new Error("NotSupportedError: Invalid destination format.");return Xm(i,s,a)},Qm=(n,t,e,r,i)=>{let a={...n};if(t!==void 0){if(t.width===0||t.height===0)throw new TypeError("visibleRect dimensions cannot be zero.");if((t.x||0)+(t.width||0)>e)throw new TypeError("visibleRect exceeds codedWidth.");if((t.y||0)+(t.height||0)>r)throw new TypeError("visibleRect exceeds codedHeight.");a.x=t.x||0,a.y=t.y||0,a.width=t.width||0,a.height=t.height||0}if(!jm(i,a))throw new TypeError("visibleRect alignment is invalid for the format.");return a},jm=(n,t)=>{if(n===null)return!0;let e=ps(n);for(let r=0;r<e.length;r++){let i=e[r],a=i.widthDivisor,s=i.heightDivisor;if((t.x||0)%a!==0||(t.y||0)%s!==0)return!1}return!0},Xm=(n,t,e)=>{let r=ps(t),i=r.length;if(e!==void 0&&e.length!==i)throw new TypeError(`Layout must have ${i} planes.`);let a=0,s=[],o=[];for(let c=0;c<i;c++){let u=r[c],l=u.sampleBytes,m=u.widthDivisor,d=u.heightDivisor,f={destinationOffset:0,destinationStride:0,sourceTop:0,sourceHeight:0,sourceLeftBytes:0,sourceWidthBytes:0};if(f.sourceTop=Math.ceil(Math.trunc(n.y||0)/d),f.sourceHeight=Math.ceil(Math.trunc(n.height||0)/d),f.sourceLeftBytes=Math.floor(Math.trunc(n.x||0)/m)*l,f.sourceWidthBytes=Math.floor(Math.trunc(n.width||0)/m)*l,e!==void 0){let g=e[c];if(g.stride<f.sourceWidthBytes)throw new TypeError(`Stride for plane ${c} is too small.`);f.destinationOffset=g.offset,f.destinationStride=g.stride}else f.destinationOffset=a,f.destinationStride=f.sourceWidthBytes;let b=f.destinationStride*f.sourceHeight+f.destinationOffset;if(b>4294967295)throw new TypeError("Allocation size exceeds limit.");o.push(b),a=Math.max(a,b);for(let g=0;g<c;g++){let y=s[g];if(!(o[c]<=y.destinationOffset||o[g]<=f.destinationOffset))throw new TypeError("Planes overlap.")}s.push(f)}return{allocationSize:a,computedLayouts:s}},ms=new Set(["f32","f32-planar","s16","s16-planar","s32","s32-planar","u8","u8-planar"]),br=class{constructor(){this._referenceCount=0}},ye=class n{constructor(t){this._closed=!1;if(Cn(t)){if(t.format===null)throw new TypeError("AudioData with null format is not supported.");this._data=t,this.format=t.format,this.sampleRate=t.sampleRate,this.numberOfFrames=t.numberOfFrames,this.numberOfChannels=t.numberOfChannels,this.timestamp=t.timestamp/1e6,this.duration=t.numberOfFrames/t.sampleRate}else if(t instanceof br){if(this._data=t,t._referenceCount++,this.format=t.getFormat(),!ms.has(this.format))throw new TypeError("getFormat() must return an AudioSampleFormat.");if(this.sampleRate=t.getSampleRate(),!Number.isInteger(this.sampleRate)||this.sampleRate<=0)throw new TypeError("getSampleRate() must return a positive integer.");if(this.numberOfFrames=t.getNumberOfFrames(),!Number.isInteger(this.numberOfFrames)||this.numberOfFrames<0)throw new TypeError("getNumberOfFrames() must return a non-negative integer.");if(this.numberOfChannels=t.getNumberOfChannels(),!Number.isInteger(this.numberOfChannels)||this.numberOfChannels<=0)throw new TypeError("getNumberOfChannels() must return a positive integer.");if(this.timestamp=t.getTimestamp(),!Number.isFinite(this.timestamp))throw new TypeError("getTimestamp() must return a finite number.");this.duration=this.numberOfFrames/this.sampleRate}else{if(!t||typeof t!="object")throw new TypeError("Invalid AudioDataInit: must be an object.");if(!ms.has(t.format))throw new TypeError("Invalid AudioDataInit: invalid format.");if(!Number.isFinite(t.sampleRate)||t.sampleRate<=0)throw new TypeError("Invalid AudioDataInit: sampleRate must be > 0.");if(!Number.isInteger(t.numberOfChannels)||t.numberOfChannels===0)throw new TypeError("Invalid AudioDataInit: numberOfChannels must be an integer > 0.");if(!Number.isFinite(t?.timestamp))throw new TypeError("init.timestamp must be a number.");let e=t.data.byteLength/(Lr(t.format)*t.numberOfChannels);if(!Number.isInteger(e))throw new TypeError("Invalid AudioDataInit: data size is not a multiple of frame size.");this.format=t.format,this.sampleRate=t.sampleRate,this.numberOfFrames=e,this.numberOfChannels=t.numberOfChannels,this.timestamp=t.timestamp,this.duration=e/t.sampleRate;let r;if(t.data instanceof ArrayBuffer)r=new Uint8Array(t.data);else if(ArrayBuffer.isView(t.data))r=new Uint8Array(t.data.buffer,t.data.byteOffset,t.data.byteLength);else throw new TypeError("Invalid AudioDataInit: data is not a BufferSource.");let i=this.numberOfFrames*this.numberOfChannels*Lr(this.format);if(r.byteLength<i)throw new TypeError("Invalid AudioDataInit: insufficient data size.");this._data=r}In?.register(this,{type:"audio",data:this._data},this)}get microsecondTimestamp(){return Math.trunc(yt*this.timestamp)}get microsecondDuration(){return Math.trunc(yt*this.duration)}allocationSize(t){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(!Number.isInteger(t.planeIndex)||t.planeIndex<0)throw new TypeError("planeIndex must be a non-negative integer.");if(t.format!==void 0&&!ms.has(t.format))throw new TypeError("Invalid format.");if(t.frameOffset!==void 0&&(!Number.isInteger(t.frameOffset)||t.frameOffset<0))throw new TypeError("frameOffset must be a non-negative integer.");if(t.frameCount!==void 0&&(!Number.isInteger(t.frameCount)||t.frameCount<0))throw new TypeError("frameCount must be a non-negative integer.");if(this._closed)throw new Error("AudioSample is closed.");let e=t.format??this.format,r=t.frameOffset??0;if(r>=this.numberOfFrames)throw new RangeError("frameOffset out of range");let i=t.frameCount!==void 0?t.frameCount:this.numberOfFrames-r;if(i>this.numberOfFrames-r)throw new RangeError("frameCount out of range");let a=Lr(e),s=Ai(e);if(s&&t.planeIndex>=this.numberOfChannels)throw new RangeError("planeIndex out of range");if(!s&&t.planeIndex!==0)throw new RangeError("planeIndex out of range");return(s?i:i*this.numberOfChannels)*a}copyTo(t,e){if(!Rt(t))throw new TypeError("destination must be an ArrayBuffer or an ArrayBuffer view.");if(!e||typeof e!="object")throw new TypeError("options must be an object.");if(!Number.isInteger(e.planeIndex)||e.planeIndex<0)throw new TypeError("planeIndex must be a non-negative integer.");if(e.format!==void 0&&!ms.has(e.format))throw new TypeError("Invalid format.");if(e.frameOffset!==void 0&&(!Number.isInteger(e.frameOffset)||e.frameOffset<0))throw new TypeError("frameOffset must be a non-negative integer.");if(e.frameCount!==void 0&&(!Number.isInteger(e.frameCount)||e.frameCount<0))throw new TypeError("frameCount must be a non-negative integer.");if(this._closed)throw new Error("AudioSample is closed.");let{format:r,frameCount:i,frameOffset:a}=e,{planeIndex:s}=e,o=this.format,c=r??this.format;if(!c)throw new Error("Destination format not determined");let u=this.numberOfFrames,l=this.numberOfChannels,m=a??0;if(m>=u)throw new RangeError("frameOffset out of range");let d=i!==void 0?i:u-m;if(d>u-m)throw new RangeError("frameCount out of range");let f=Lr(c),p=Ai(c);if(p&&s>=l)throw new RangeError("planeIndex out of range");if(!p&&s!==0)throw new RangeError("planeIndex out of range");let g=(p?d:d*l)*f;if(t.byteLength<g)throw new RangeError("Destination buffer is too small");let y=V(t),k=rd(c);if(Cn(this._data))ir()&&l>2&&c!==o?$m(this._data,y,o,c,l,s,m,d):this._data.copyTo(t,{planeIndex:s,frameOffset:m,frameCount:d,format:c});else{let T=td(o),w=Lr(o),S=Ai(o),x;if(this._data instanceof br){let A=C=>{let I=this._data.getDataPlane(C);if(!(I instanceof Uint8Array))throw new TypeError("getDataPlane() must return a Uint8Array.");let R=u*w*(S?1:l);if(I.byteLength!==R)throw new TypeError(`Data plane ${C} has invalid size. Expected exactly ${R} bytes, got ${I.byteLength} bytes.`);return I};if(S)if(p)x=A(s),s=0;else{x=new Uint8Array(u*w*l);for(let C=0;C<l;C++){let I=A(C);x.set(I,C*u*w)}}else x=A(0)}else x=this._data;let P=V(x);for(let A=0;A<d;A++)if(p){let C=A*f,I;S?I=(s*u+(A+m))*w:I=((A+m)*l+s)*w;let R=T(P,I);k(y,C,R)}else for(let C=0;C<l;C++){let R=(A*l+C)*f,v;S?v=(C*u+(A+m))*w:v=((A+m)*l+C)*w;let E=T(P,v);k(y,R,E)}}}clone(){if(this._closed)throw new Error("AudioSample is closed.");if(this._data instanceof br){let t=new n(this._data);return t.setTimestamp(this.timestamp),t}else if(Cn(this._data)){let t=new n(this._data.clone());return t.setTimestamp(this.timestamp),t}else return new n({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.timestamp,data:this._data})}close(){this._closed||(In?.unregister(this),this._data instanceof br?(this._data._referenceCount--,this._data._referenceCount===0&&this._data.close()):Cn(this._data)?this._data.close():this._data=new Uint8Array(0),this._closed=!0)}toAudioData(){if(this._closed)throw new Error("AudioSample is closed.");return this._data instanceof br?this._createAudioDataFromData():Cn(this._data)?this._data.timestamp===this.microsecondTimestamp?this._data.clone():this._createAudioDataFromData():new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:this._data.buffer instanceof ArrayBuffer?this._data.buffer:this._data.slice()})}_createAudioDataFromData(){if(Ai(this.format)){let t=this.allocationSize({planeIndex:0,format:this.format}),e=new ArrayBuffer(t*this.numberOfChannels);for(let r=0;r<this.numberOfChannels;r++)this.copyTo(new Uint8Array(e,r*t,t),{planeIndex:r,format:this.format});return new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:e})}else{let t=new ArrayBuffer(this.allocationSize({planeIndex:0,format:this.format}));return this.copyTo(t,{planeIndex:0,format:this.format}),new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:t})}}toAudioBuffer(){if(this._closed)throw new Error("AudioSample is closed.");let t=new AudioBuffer({numberOfChannels:this.numberOfChannels,length:this.numberOfFrames,sampleRate:this.sampleRate}),e=new Float32Array(this.allocationSize({planeIndex:0,format:"f32-planar"})/4);for(let r=0;r<this.numberOfChannels;r++)this.copyTo(e,{planeIndex:r,format:"f32-planar"}),t.copyToChannel(e,r);return t}setTimestamp(t){if(!Number.isFinite(t))throw new TypeError("newTimestamp must be a number.");this.timestamp=t}[Symbol.dispose](){this.close()}static*_fromAudioBuffer(t,e){if(!(t instanceof AudioBuffer))throw new TypeError("audioBuffer must be an AudioBuffer.");let r=48e3*5,i=t.numberOfChannels,a=t.sampleRate,s=t.length,o=Math.floor(r/i),c=0,u=s;for(;u>0;){let l=Math.min(o,u),m=new Float32Array(i*l);for(let d=0;d<i;d++)t.copyFromChannel(m.subarray(d*l,(d+1)*l),d,c);yield new n({format:"f32-planar",sampleRate:a,numberOfFrames:l,numberOfChannels:i,timestamp:e+c/a,data:m}),c+=l,u-=l}}static fromAudioBuffer(t,e){if(!(t instanceof AudioBuffer))throw new TypeError("audioBuffer must be an AudioBuffer.");let r=48e3*5,i=t.numberOfChannels,a=t.sampleRate,s=t.length,o=Math.floor(r/i),c=0,u=s,l=[];for(;u>0;){let m=Math.min(o,u),d=new Float32Array(i*m);for(let p=0;p<i;p++)t.copyFromChannel(d.subarray(p*m,(p+1)*m),p,c);let f=new n({format:"f32-planar",sampleRate:a,numberOfFrames:m,numberOfChannels:i,timestamp:e+c/a,data:d});l.push(f),c+=m,u-=m}return l}},Lr=n=>{switch(n){case"u8":case"u8-planar":return 1;case"s16":case"s16-planar":return 2;case"s32":case"s32-planar":return 4;case"f32":case"f32-planar":return 4;default:throw new Error("Unknown AudioSampleFormat")}},Ai=n=>{switch(n){case"u8-planar":case"s16-planar":case"s32-planar":case"f32-planar":return!0;default:return!1}},td=n=>{switch(n){case"u8":case"u8-planar":return(t,e)=>(t.getUint8(e)-128)/128;case"s16":case"s16-planar":return(t,e)=>t.getInt16(e,!0)/32768;case"s32":case"s32-planar":return(t,e)=>t.getInt32(e,!0)/2147483648;case"f32":case"f32-planar":return(t,e)=>t.getFloat32(e,!0)}},rd=n=>{switch(n){case"u8":case"u8-planar":return(t,e,r)=>t.setUint8(e,ue((r+1)*127.5,0,255));case"s16":case"s16-planar":return(t,e,r)=>t.setInt16(e,ue(Math.round(r*32767),-32768,32767),!0);case"s32":case"s32-planar":return(t,e,r)=>t.setInt32(e,ue(Math.round(r*2147483647),-2147483648,2147483647),!0);case"f32":case"f32-planar":return(t,e,r)=>t.setFloat32(e,r,!0)}},Cn=n=>typeof AudioData<"u"&&n instanceof AudioData,hs=n=>{switch(n){case"u8-planar":return"u8";case"s16-planar":return"s16";case"s32-planar":return"s32";case"f32-planar":return"f32";default:return n}},$m=(n,t,e,r,i,a,s,o)=>{let c=td(e),u=rd(r),l=Lr(e),m=Lr(r),d=Ai(e);if(Ai(r))if(d){let p=new ArrayBuffer(o*l),b=V(p);n.copyTo(p,{planeIndex:a,frameOffset:s,frameCount:o,format:e});for(let g=0;g<o;g++){let y=g*l,k=g*m,T=c(b,y);u(t,k,T)}}else{let p=new ArrayBuffer(o*i*l),b=V(p);n.copyTo(p,{planeIndex:0,frameOffset:s,frameCount:o,format:e});for(let g=0;g<o;g++){let y=(g*i+a)*l,k=g*m,T=c(b,y);u(t,k,T)}}else if(d){let p=o*l,b=new ArrayBuffer(p),g=V(b);for(let y=0;y<i;y++){n.copyTo(b,{planeIndex:y,frameOffset:s,frameCount:o,format:e});for(let k=0;k<o;k++){let T=k*l,w=(k*i+y)*m,S=c(g,T);u(t,w,S)}}}else{let p=new ArrayBuffer(o*i*l),b=V(p);n.copyTo(p,{planeIndex:0,frameOffset:s,frameCount:o,format:e});for(let g=0;g<o;g++)for(let y=0;y<i;y++){let k=g*i+y,T=k*l,w=k*m,S=c(b,T);u(t,w,S)}}},gs=(n,t)=>{let e=n.allocationSize({format:t,planeIndex:0}),r=new ArrayBuffer(e);return n.copyTo(r,{format:t,planeIndex:0}),new ye({data:r,format:t,numberOfChannels:n.numberOfChannels,sampleRate:n.sampleRate,timestamp:n.timestamp,duration:n.duration})};var bs=new Map,ys=new Map,ks=n=>{if(!n||typeof n!="object")throw new TypeError("Encoding config must be an object.");if(!re.includes(n.codec))throw new TypeError(`Invalid video codec '${n.codec}'. Must be one of: ${re.join(", ")}.`);if(!(n.bitrate instanceof Pe)&&(!Number.isInteger(n.bitrate)||n.bitrate<=0))throw new TypeError("config.bitrate must be a positive integer or a quality.");if(n.keyFrameInterval!==void 0&&(!Number.isFinite(n.keyFrameInterval)||n.keyFrameInterval<0))throw new TypeError("config.keyFrameInterval, when provided, must be a non-negative number.");if(n.sizeChangeBehavior!==void 0&&!["deny","passThrough","fill","contain","cover"].includes(n.sizeChangeBehavior))throw new TypeError("config.sizeChangeBehavior, when provided, must be 'deny', 'passThrough', 'fill', 'contain' or 'cover'.");if(n.transform!==void 0){if(typeof n.transform!="object"||!n.transform)throw new TypeError("config.transform, when provided, must be an object.");if(n.transform.width!==void 0&&(!Number.isInteger(n.transform.width)||n.transform.width<=0))throw new TypeError("config.transform.width, when provided, must be a positive integer.");if(n.transform.height!==void 0&&(!Number.isInteger(n.transform.height)||n.transform.height<=0))throw new TypeError("config.transform.height, when provided, must be a positive integer.");if(n.transform.fit!==void 0&&!["fill","contain","cover"].includes(n.transform.fit))throw new TypeError('config.transform.fit, when provided, must be one of "fill", "contain", or "cover".');if(n.transform.width!==void 0&&n.transform.height!==void 0&&n.transform.fit===void 0&&!["fill","contain","cover"].includes(n.sizeChangeBehavior))throw new TypeError("When both config.transform.width and config.transform.height are provided, config.transform.fit must also be provided.");if(n.transform.fit!==void 0&&["fill","contain","cover"].includes(n.sizeChangeBehavior)&&n.transform.fit!==n.sizeChangeBehavior)throw new TypeError("config.transform.fit, when provided, cannot differ from config.sizeChangeBehavior when config.sizeChangeBehavior is 'fill', 'contain' or 'cover', as sizeChangeBehavior already determines the fitting algorithm.");if(n.transform.rotate!==void 0&&![0,90,180,270].includes(n.transform.rotate))throw new TypeError("config.transform.rotate, when provided, must be 0, 90, 180 or 270.");if(n.transform.crop!==void 0&&yr(n.transform.crop,"config.transform."),n.transform.process!==void 0&&typeof n.transform.process!="function")throw new TypeError("config.transform.process, when provided, must be a function.");if(n.transform.frameRate!==void 0&&(!Number.isFinite(n.transform.frameRate)||n.transform.frameRate<=0))throw new TypeError("config.transform.frameRate, when provided, must be a finite positive number.");if(n.transform.force!==void 0&&typeof n.transform.force!="boolean")throw new TypeError("config.transform.force, when provided, must be a boolean.")}if(n.onEncodedPacket!==void 0&&typeof n.onEncodedPacket!="function")throw new TypeError("config.onEncodedPacket, when provided, must be a function.");if(n.onEncoderConfig!==void 0&&typeof n.onEncoderConfig!="function")throw new TypeError("config.onEncoderConfig, when provided, must be a function.");id(n.codec,n)},id=(n,t)=>{if(!t||typeof t!="object")throw new TypeError("Encoding options must be an object.");if(t.alpha!==void 0&&!["discard","keep"].includes(t.alpha))throw new TypeError("options.alpha, when provided, must be 'discard' or 'keep'.");if(t.bitrateMode!==void 0&&!["constant","variable"].includes(t.bitrateMode))throw new TypeError("bitrateMode, when provided, must be 'constant' or 'variable'.");if(t.latencyMode!==void 0&&!["quality","realtime"].includes(t.latencyMode))throw new TypeError("latencyMode, when provided, must be 'quality' or 'realtime'.");if(t.fullCodecString!==void 0&&typeof t.fullCodecString!="string")throw new TypeError("fullCodecString, when provided, must be a string.");if(t.fullCodecString!==void 0&&Ge(t.fullCodecString)!==n)throw new TypeError(`fullCodecString, when provided, must be a string that matches the specified codec (${n}).`);if(t.hardwareAcceleration!==void 0&&!["no-preference","prefer-hardware","prefer-software"].includes(t.hardwareAcceleration))throw new TypeError("hardwareAcceleration, when provided, must be 'no-preference', 'prefer-hardware' or 'prefer-software'.");if(t.scalabilityMode!==void 0&&typeof t.scalabilityMode!="string")throw new TypeError("scalabilityMode, when provided, must be a string.");if(t.contentHint!==void 0&&typeof t.contentHint!="string")throw new TypeError("contentHint, when provided, must be a string.")},fc=n=>{let t=n.bitrate instanceof Pe?n.bitrate._toVideoBitrate(n.codec,n.width,n.height):n.bitrate;return{codec:n.fullCodecString??Kn(n.codec,n.width,n.height,t),width:n.width,height:n.height,displayWidth:n.squarePixelWidth,displayHeight:n.squarePixelHeight,bitrate:t,bitrateMode:n.bitrateMode,alpha:n.alpha??"discard",framerate:n.framerate,latencyMode:n.latencyMode,hardwareAcceleration:n.hardwareAcceleration,scalabilityMode:n.scalabilityMode,contentHint:n.contentHint,...Nu(n.codec)}},Ts=n=>{if(!n||typeof n!="object")throw new TypeError("Encoding config must be an object.");if(!ae.includes(n.codec))throw new TypeError(`Invalid audio codec '${n.codec}'. Must be one of: ${ae.join(", ")}.`);if(n.bitrate===void 0&&!(ee.includes(n.codec)||n.codec==="flac"))throw new TypeError("config.bitrate must be provided for compressed audio codecs.");if(n.bitrate!==void 0&&!(n.bitrate instanceof Pe)&&(!Number.isInteger(n.bitrate)||n.bitrate<=0))throw new TypeError("config.bitrate, when provided, must be a positive integer or a quality.");if(n.transform!==void 0){if(typeof n.transform!="object"||!n.transform)throw new TypeError("config.transform, when provided, must be an object.");if(n.transform.numberOfChannels!==void 0&&(!Number.isInteger(n.transform.numberOfChannels)||n.transform.numberOfChannels<=0))throw new TypeError("config.transform.numberOfChannels, when provided, must be a positive integer.");if(n.transform.sampleRate!==void 0&&(!Number.isInteger(n.transform.sampleRate)||n.transform.sampleRate<=0))throw new TypeError("config.transform.sampleRate, when provided, must be a positive integer.");if(n.transform.sampleFormat!==void 0&&!["u8","s16","s32","f32"].includes(n.transform.sampleFormat))throw new TypeError("config.transform.sampleFormat, when provided, must be one of: u8, s16, s32, f32.");if(n.transform.process!==void 0&&typeof n.transform.process!="function")throw new TypeError("config.transform.process, when provided, must be a function.")}if(n.onEncodedPacket!==void 0&&typeof n.onEncodedPacket!="function")throw new TypeError("config.onEncodedPacket, when provided, must be a function.");if(n.onEncoderConfig!==void 0&&typeof n.onEncoderConfig!="function")throw new TypeError("config.onEncoderConfig, when provided, must be a function.");nd(n.codec,n)},nd=(n,t)=>{if(!t||typeof t!="object")throw new TypeError("Encoding options must be an object.");if(t.bitrateMode!==void 0&&!["constant","variable"].includes(t.bitrateMode))throw new TypeError("bitrateMode, when provided, must be 'constant' or 'variable'.");if(t.fullCodecString!==void 0&&typeof t.fullCodecString!="string")throw new TypeError("fullCodecString, when provided, must be a string.");if(t.fullCodecString!==void 0&&Ge(t.fullCodecString)!==n)throw new TypeError(`fullCodecString, when provided, must be a string that matches the specified codec (${n}).`)},pc=n=>{let t=n.bitrate instanceof Pe?n.bitrate._toAudioBitrate(n.codec):n.bitrate;return{codec:n.fullCodecString??jn(n.codec,n.numberOfChannels,n.sampleRate),numberOfChannels:n.numberOfChannels,sampleRate:n.sampleRate,bitrate:t,bitrateMode:n.bitrateMode,...Lu(n.codec)}},Pe=class{constructor(t){this._factor=t}_toVideoBitrate(t,e,r){let i=e*r,a={avc:1,hevc:.6,vp9:.6,av1:.4,vp8:1.2},s=1920*1080,o=3e6,c=Math.pow(i/s,.95),m=o*c*a[t]*this._factor;return Math.ceil(m/1e3)*1e3}_toAudioBitrate(t){if(ee.includes(t)||t==="flac")return;let r={aac:128e3,opus:64e3,mp3:16e4,vorbis:64e3,ac3:384e3,eac3:192e3}[t];if(!r)throw new Error(`Unhandled codec: ${t}`);let i=r*this._factor;return t==="aac"?i=[96e3,128e3,16e4,192e3].reduce((s,o)=>Math.abs(o-i)<Math.abs(s-i)?o:s):t==="opus"||t==="vorbis"?i=Math.max(6e3,i):t==="mp3"&&(i=[8e3,16e3,24e3,32e3,4e4,48e3,64e3,8e4,96e3,112e3,128e3,16e4,192e3,224e3,256e3,32e4].reduce((s,o)=>Math.abs(o-i)<Math.abs(s-i)?o:s)),Math.round(i/1e3)*1e3}},Ym=new Pe(.3),Zm=new Pe(.6),Jm=new Pe(1),ws=new Pe(2),ef=new Pe(4),tf=n=>{if(re.includes(n))return Ss(n);if(ae.includes(n))return As(n);if(Me.includes(n))return xs(n);throw new TypeError(`Unknown codec '${n}'.`)},Ss=async(n,t={})=>{let{width:e=1280,height:r=720,bitrate:i=1e6,...a}=t;if(!re.includes(n))return!1;if(!Number.isInteger(e)||e<=0)throw new TypeError("width must be a positive integer.");if(!Number.isInteger(r)||r<=0)throw new TypeError("height must be a positive integer.");if(!(i instanceof Pe)&&(!Number.isInteger(i)||i<=0))throw new TypeError("bitrate must be a positive integer or a quality.");id(n,a);let s=fc({codec:n,width:e,height:r,bitrate:i,framerate:void 0,...a,alpha:"discard"}),o=JSON.stringify(s),c=bs.get(o);if(c)return c;let u=(async()=>Ci.some(d=>d.supports(n,s))?!0:typeof VideoEncoder>"u"||(e%2===1||r%2===1)&&(n==="avc"||n==="hevc")||!(await VideoEncoder.isConfigSupported(s)).supported?!1:Er()?new Promise(async d=>{try{let f=new VideoEncoder({output:()=>{},error:()=>d(!1)});f.configure(s);let p=new Uint8Array(e*r*4),b=new VideoFrame(p,{format:"RGBA",codedWidth:e,codedHeight:r,timestamp:0});f.encode(b),b.close(),await f.flush(),d(!0)}catch{d(!1)}}):!0)();return bs.set(o,u),u},As=async(n,t={})=>{let{numberOfChannels:e=2,sampleRate:r=48e3,bitrate:i=128e3,...a}=t;if(!ae.includes(n))return!1;if(!Number.isInteger(e)||e<=0)throw new TypeError("numberOfChannels must be a positive integer.");if(!Number.isInteger(r)||r<=0)throw new TypeError("sampleRate must be a positive integer.");if(!(i instanceof Pe)&&(!Number.isInteger(i)||i<=0))throw new TypeError("bitrate must be a positive integer.");nd(n,a);let s=pc({codec:n,numberOfChannels:e,sampleRate:r,bitrate:i,...a}),o=JSON.stringify(s),c=ys.get(o);if(c)return c;let u=(async()=>Ii.some(m=>m.supports(n,s))||ee.includes(n)?!0:typeof AudioEncoder>"u"?!1:(await AudioEncoder.isConfigSupported(s)).supported===!0)();return ys.set(o,u),u},xs=async n=>!!Me.includes(n),rf=async()=>{let[n,t,e]=await Promise.all([ad(),En(),sd()]);return[...n,...t,...e]},ad=async(n=re,t)=>{let e=await Promise.all(n.map(r=>Ss(r,t)));return n.filter((r,i)=>e[i])},En=async(n=ae,t)=>{let e=await Promise.all(n.map(r=>As(r,t)));return n.filter((r,i)=>e[i])},sd=async(n=Me)=>{let t=await Promise.all(n.map(xs));return n.filter((e,r)=>t[r])},hc=async(n,t)=>{for(let e of n)if(await Ss(e,t))return e;return null},nf=async(n,t)=>{for(let e of n)if(await As(e,t))return e;return null},af=async n=>{for(let t of n)if(await xs(t))return t;return null};var Ps=class{static supports(t,e){return!1}},Cs=class{static supports(t,e){return!1}},Is=class{static supports(t,e){return!1}},Es=class{static supports(t,e){return!1}},hr=[],gr=[],Ci=[],Ii=[],sf=n=>{if(n.prototype instanceof Ps){let t=n;if(hr.includes(t)){console.warn("Video decoder already registered.");return}hr.push(t),ls.clear()}else if(n.prototype instanceof Cs){let t=n;if(gr.includes(t)){console.warn("Audio decoder already registered.");return}gr.push(t),ds.clear()}else throw new TypeError("Decoder must be a CustomVideoDecoder or CustomAudioDecoder.")},of=n=>{if(n.prototype instanceof Is){let t=n;if(Ci.includes(t)){console.warn("Video encoder already registered.");return}Ci.push(t),bs.clear()}else if(n.prototype instanceof Es){let t=n;if(Ii.includes(t)){console.warn("Audio encoder already registered.");return}Ii.push(t),ys.clear()}else throw new TypeError("Encoder must be a CustomVideoEncoder or CustomAudioEncoder.")};var od=n=>{let r=n,i=4096,a=0,s=12,o=0;for(r<0&&(r=-r,a=128),r+=33,r>8191&&(r=8191);(r&i)!==i&&s>=5;)i>>=1,s--;return o=r>>s-4&15,~(a|s-5<<4|o)&255},cd=n=>{let e=0,r=0,i=~n;i&128&&(i&=-129,e=-1),r=((i&240)>>4)+5;let a=(1<<r|(i&15)<<r-4|1<<r-5)-33;return e===0?a:-a},ud=n=>{let e=2048,r=0,i=11,a=0,s=n;for(s<0&&(s=-s,r=128),s>4095&&(s=4095);(s&e)!==e&&i>=5;)e>>=1,i--;return a=s>>(i===4?1:i-4)&15,(r|i-4<<4|a)^85},ld=n=>{let t=0,e=0,r=n^85;r&128&&(r&=-129,t=-1),e=((r&240)>>4)+4;let i=0;return e!==4?i=1<<e|(r&15)<<e-4|1<<e-5:i=r<<1|1,t===0?i:-i};var Wr=n=>{if(!n||typeof n!="object")throw new TypeError("options must be an object.");if(n.metadataOnly!==void 0&&typeof n.metadataOnly!="boolean")throw new TypeError("options.metadataOnly, when defined, must be a boolean.");if(n.verifyKeyPackets!==void 0&&typeof n.verifyKeyPackets!="boolean")throw new TypeError("options.verifyKeyPackets, when defined, must be a boolean.");if(n.verifyKeyPackets&&n.metadataOnly)throw new TypeError("options.verifyKeyPackets and options.metadataOnly cannot be enabled together.");if(n.skipLiveWait!==void 0&&typeof n.skipLiveWait!="boolean")throw new TypeError("options.skipLiveWait, when defined, must be a boolean.")},Xt=n=>{if(!nr(n))throw new TypeError("timestamp must be a number.")},gc=(n,t,e)=>e.verifyKeyPackets?t.then(async r=>{if(!r||r.type==="delta")return r;let i=await n.determinePacketType(r);return i&&(r.type=i),r}):t,xt=class{constructor(t){if(!(t instanceof Gr))throw new TypeError("track must be an InputTrack.");this._track=t}async getFirstPacket(t={}){if(Wr(t),this._track.input._disposed)throw new be;return gc(this._track,this._track._backing.getFirstPacket(t),t)}async getFirstKeyPacket(t={}){Wr(t);let e=await this.getFirstPacket(t);return e?e.type==="key"?e:this.getNextKeyPacket(e,t):null}async getPacket(t,e={}){if(Xt(t),Wr(e),this._track.input._disposed)throw new be;return gc(this._track,this._track._backing.getPacket(t,e),e)}async getNextPacket(t,e={}){if(!(t instanceof Q))throw new TypeError("packet must be an EncodedPacket.");if(Wr(e),this._track.input._disposed)throw new be;return gc(this._track,this._track._backing.getNextPacket(t,e),e)}async getKeyPacket(t,e={}){if(Xt(t),Wr(e),this._track.input._disposed)throw new be;if(!e.verifyKeyPackets)return this._track._backing.getKeyPacket(t,e);let r=await this._track._backing.getKeyPacket(t,e);return r&&(h(r.type==="key"),await this._track.determinePacketType(r)==="delta"?this.getKeyPacket(r.timestamp-1/await this._track.getTimeResolution(),e):r)}async getNextKeyPacket(t,e={}){if(!(t instanceof Q))throw new TypeError("packet must be an EncodedPacket.");if(Wr(e),this._track.input._disposed)throw new be;if(!e.verifyKeyPackets)return this._track._backing.getNextKeyPacket(t,e);let r=await this._track._backing.getNextKeyPacket(t,e);return r&&(h(r.type==="key"),await this._track.determinePacketType(r)==="delta"?this.getNextKeyPacket(r,e):r)}packets(t,e,r={}){if(t!==void 0&&!(t instanceof Q))throw new TypeError("startPacket must be an EncodedPacket.");if(t!==void 0&&t.isMetadataOnly&&!r?.metadataOnly)throw new TypeError("startPacket can only be metadata-only if options.metadataOnly is enabled.");if(e!==void 0&&!(e instanceof Q))throw new TypeError("endPacket must be an EncodedPacket.");if(Wr(r),this._track.input._disposed)throw new be;let i=[],{promise:a,resolve:s}=Z(),{promise:o,resolve:c}=Z(),u=!1,l=!1,m=null,d=[],f=()=>Math.max(2,d.length);(async()=>{let b=t??await this.getFirstPacket(r);for(;b&&!l&&!this._track.input._disposed&&!(e&&b.sequenceNumber>=e?.sequenceNumber);){if(i.length>f()){({promise:o,resolve:c}=Z()),await o;continue}i.push(b),s(),{promise:a,resolve:s}=Z(),b=await this.getNextPacket(b,r)}u=!0,s()})().catch(b=>{m||(m=b,s())});let p=this._track;return{async next(){for(;;){if(p.input._disposed)throw new be;if(l)return{value:void 0,done:!0};if(m)throw m;if(i.length>0){let b=i.shift(),g=performance.now();for(d.push(g);d.length>0&&g-d[0]>=1e3;)d.shift();return c(),{value:b,done:!1}}else{if(u)return{value:void 0,done:!0};await a}}},async return(){return l=!0,c(),s(),{value:void 0,done:!0}},async throw(b){throw b},[Symbol.asyncIterator](){return this}}}},vn=class{constructor(t,e){this.onSample=t;this.onError=e}},_n=class{mediaSamplesInRange(t=-1/0,e=1/0,r){Xt(t),Xt(e);let i=[],a=!1,s=null,{promise:o,resolve:c}=Z(),{promise:u,resolve:l}=Z(),m=!1,d=!1,f=!1,p=null,b={...r,verifyKeyPackets:!0,metadataOnly:!1};(async()=>{let k=await this._createDecoder(A=>{if(l(),A.timestamp>=e&&(d=!0),d){A.close();return}s&&(A.timestamp>t?(i.push(s),a=!0):s.close()),A.timestamp>=t&&(i.push(A),a=!0),s=a?null:A,i.length>0&&(c(),{promise:o,resolve:c}=Z())},A=>{p||(p=A,c())}),T=this._createPacketSink(),w=await T.getKeyPacket(t,b)??await T.getFirstKeyPacket(b),S=w,P=T.packets(w??void 0,void 0,b);for(await P.next();S&&!d&&!this._track.input._disposed;){let A=dd(i.length);if(i.length+k.getDecodeQueueSize()>A){({promise:u,resolve:l}=Z()),await u;continue}k.decode(S);let C=await P.next();if(C.done)break;S=C.value}await P.return(),!f&&!this._track.input._disposed&&await k.flush(),k.close(),!a&&s&&i.push(s),m=!0,c()})().catch(k=>{p||(p=k,c())});let g=this._track,y=()=>{s?.close();for(let k of i)k.close()};return{async next(){for(;;){if(g.input._disposed)throw y(),new be;if(f)return{value:void 0,done:!0};if(p)throw y(),p;if(i.length>0){let k=i.shift();return l(),{value:k,done:!1}}else if(!m)await o;else return{value:void 0,done:!0}}},async return(){return f=!0,d=!0,l(),c(),y(),{value:void 0,done:!0}},async throw(k){throw k},[Symbol.asyncIterator](){return this}}}mediaSamplesAtTimestamps(t,e){hu(t);let r=pu(t),i=[],a=[],{promise:s,resolve:o}=Z(),{promise:c,resolve:u}=Z(),l=!1,m=!1,d=null,f=y=>{a.push(y),o(),{promise:s,resolve:o}=Z()},p={...e,verifyKeyPackets:!0,metadataOnly:!1};(async()=>{let y=await this._createDecoder(A=>{if(u(),m){A.close();return}let C=0;for(;i.length>0&&A.timestamp-i[0]>-1e-10;)C++,i.shift();if(C>0)for(let I=0;I<C;I++)f(I<C-1?A.clone():A);else A.close()},A=>{d||(d=A,o())}),k=this._createPacketSink(),T=null,w=null,S=-1,x=async()=>{h(w);let A=w;for(y.decode(A);A.sequenceNumber<S;){let C=dd(a.length);for(;a.length+y.getDecodeQueueSize()>C&&!m;)({promise:c,resolve:u}=Z()),await c;if(m)break;let I=await k.getNextPacket(A,p);h(I),y.decode(I),A=I}S=-1},P=async()=>{await y.flush();for(let A=0;A<i.length;A++)f(null);i.length=0};for await(let A of r){if(Xt(A),m||this._track.input._disposed)break;let C=await k.getPacket(A,p),I=C&&await k.getKeyPacket(A,p);if(!I){S!==-1&&(await x(),await P()),f(null),T=null;continue}T&&(I.sequenceNumber!==w.sequenceNumber||C.timestamp<T.timestamp)&&(await x(),await P()),i.push(C.timestamp),S=Math.max(C.sequenceNumber,S),T=C,w=I}!m&&!this._track.input._disposed&&(S!==-1&&await x(),await P()),y.close(),l=!0,o()})().catch(y=>{d||(d=y,o())});let b=this._track,g=()=>{for(let y of a)y?.close()};return{async next(){for(;;){if(b.input._disposed)throw g(),new be;if(m)return{value:void 0,done:!0};if(d)throw g(),d;if(a.length>0){let y=a.shift();return h(y!==void 0),u(),{value:y,done:!1}}else if(!l)await s;else return{value:void 0,done:!0}}},async return(){return m=!0,u(),o(),g(),{value:void 0,done:!0}},async throw(y){throw y},[Symbol.asyncIterator](){return this}}}},dd=n=>n===0?40:8,bc=class extends vn{constructor(e,r,i,a,s,o){super(e,r);this.codec=i;this.decoderConfig=a;this.rotation=s;this.timeResolution=o;this.decoder=null;this.customDecoder=null;this.customDecoderCallSerializer=new vt;this.customDecoderQueueSize=0;this.inputTimestamps=[];this.sampleQueue=[];this.currentPacketIndex=0;this.raslSkipped=!1;this.alphaDecoder=null;this.alphaHadKeyframe=!1;this.colorQueue=[];this.alphaQueue=[];this.merger=null;this.decodedAlphaChunkCount=0;this.alphaDecoderQueueSize=0;this.nullAlphaFrameQueue=[];this.currentAlphaPacketIndex=0;this.alphaRaslSkipped=!1;this.frameHandlerSerializer=new vt;let c=hr.find(u=>u.supports(i,a));if(c)this.customDecoder=new c,this.customDecoder.codec=i,this.customDecoder.config=a,this.customDecoder.onSample=u=>{if(!(u instanceof Ae))throw new TypeError("The argument passed to onSample must be a VideoSample.");this.finalizeAndEmitSample(u)},this.customDecoderCallSerializer.call(()=>this.customDecoder.init());else{let u=m=>{this.frameHandlerSerializer.call(async()=>{if(this.alphaQueue.length>0){let d=this.alphaQueue.shift();h(d!==void 0),await this.mergeAlpha(m,d)}else this.colorQueue.push(m)}).catch(d=>this.onError(d))};if(i==="avc"&&this.decoderConfig.description&&Xi()){let m=ra(j(this.decoderConfig.description));if(m&&m.sequenceParameterSets.length>0){let d=tn(m.sequenceParameterSets[0]);d&&d.frameMbsOnlyFlag===0&&(this.decoderConfig={...this.decoderConfig,hardwareAcceleration:"prefer-software"})}}let l=new Error("Decoding error").stack;this.decoder=new VideoDecoder({output:m=>{try{u(m)}catch(d){this.onError(d)}},error:m=>{m.stack=l,this.onError(m)}}),this.decoder.configure(this.decoderConfig)}}getDecodeQueueSize(){return this.customDecoder?this.customDecoderQueueSize:(h(this.decoder),Math.max(this.decoder.decodeQueueSize,this.alphaDecoder?.decodeQueueSize??0))}decode(e){if(this.codec==="hevc"&&this.currentPacketIndex>0&&!this.raslSkipped){if(this.hasHevcRaslPicture(e.data))return;this.raslSkipped=!0}if(this.customDecoder)this.customDecoderQueueSize++,this.customDecoderCallSerializer.call(()=>this.customDecoder.decode(e)).then(()=>this.customDecoderQueueSize--);else{if(h(this.decoder),ir()||uo(this.inputTimestamps,e.timestamp,r=>r),Xi()&&this.currentPacketIndex===0){if(this.codec==="avc"){let r=[];for(let a of yo(e.data,this.decoderConfig)){let s=zt(e.data[a.offset]);s>=20&&s<=31||r.push(e.data.subarray(a.offset,a.offset+a.length))}let i=Ku(r,this.decoderConfig);e=new Q(i,e.type,e.timestamp,e.duration)}else if(this.codec==="hevc"){let r=Yu(e.data,this.decoderConfig);r&&(e=new Q(r,e.type,e.timestamp,e.duration))}}this.decoder.decode(e.toEncodedVideoChunk()),this.decodeAlphaData(e)}this.currentPacketIndex++}decodeAlphaData(e){if(!e.sideData.alpha){this.pushNullAlphaFrame();return}if(this.merger||(this.merger=new yc),!this.alphaDecoder){let i=s=>{this.frameHandlerSerializer.call(async()=>{if(this.colorQueue.length>0){let o=this.colorQueue.shift();h(o!==void 0),await this.mergeAlpha(o,s)}else this.alphaQueue.push(s);for(this.decodedAlphaChunkCount++;this.nullAlphaFrameQueue.length>0&&this.nullAlphaFrameQueue[0]===this.decodedAlphaChunkCount;)if(this.nullAlphaFrameQueue.shift(),this.colorQueue.length>0){let o=this.colorQueue.shift();h(o!==void 0),await this.mergeAlpha(o,null)}else this.alphaQueue.push(null);this.alphaDecoderQueueSize--}).catch(o=>this.onError(o))},a=new Error("Decoding error").stack;this.alphaDecoder=new VideoDecoder({output:s=>{try{i(s)}catch(o){this.onError(o)}},error:s=>{s.stack=a,this.onError(s)}}),this.alphaDecoder.configure(this.decoderConfig)}let r=oi(this.codec,this.decoderConfig,e.sideData.alpha);if(this.alphaHadKeyframe||(this.alphaHadKeyframe=r==="key"),this.alphaHadKeyframe){if(this.codec==="hevc"&&this.currentAlphaPacketIndex>0&&!this.alphaRaslSkipped){if(this.hasHevcRaslPicture(e.sideData.alpha)){this.pushNullAlphaFrame();return}this.alphaRaslSkipped=!0}this.currentAlphaPacketIndex++,this.alphaDecoder.decode(e.alphaToEncodedVideoChunk(r??e.type)),this.alphaDecoderQueueSize++}else this.pushNullAlphaFrame()}pushNullAlphaFrame(){this.alphaDecoderQueueSize===0?this.alphaQueue.push(null):this.nullAlphaFrameQueue.push(this.decodedAlphaChunkCount+this.alphaDecoderQueueSize)}hasHevcRaslPicture(e){for(let r of Ji(e,this.decoderConfig)){let i=at(e[r.offset]);if(i===8||i===9)return!0}return!1}sampleHandler(e){if(ir()){if(this.sampleQueue.length>0&&e.timestamp>=X(this.sampleQueue).timestamp){for(let r of this.sampleQueue)this.finalizeAndEmitSample(r);this.sampleQueue.length=0}uo(this.sampleQueue,e,r=>r.timestamp)}else{let r=this.inputTimestamps.shift();h(r!==void 0),e.setTimestamp(r),this.finalizeAndEmitSample(e)}}finalizeAndEmitSample(e){e.setTimestamp(Math.round(e.timestamp*this.timeResolution)/this.timeResolution),e.setDuration(Math.round(e.duration*this.timeResolution)/this.timeResolution),e.setRotation(this.rotation),this.onSample(e)}async mergeAlpha(e,r){if(!r){let s=new Ae(e);this.sampleHandler(s);return}h(this.merger);let i=await this.merger.update(e,r),a=new Ae(i);this.sampleHandler(a)}async flush(){if(this.customDecoder?await this.customDecoderCallSerializer.call(()=>this.customDecoder.flush()):(h(this.decoder),await Promise.all([this.decoder.flush(),this.alphaDecoder?.flush()]),await this.frameHandlerSerializer.currentPromise,this.colorQueue.forEach(e=>e.close()),this.colorQueue.length=0,this.alphaQueue.forEach(e=>e?.close()),this.alphaQueue.length=0,this.alphaHadKeyframe=!1,this.decodedAlphaChunkCount=0,this.alphaDecoderQueueSize=0,this.nullAlphaFrameQueue.length=0,this.currentAlphaPacketIndex=0,this.alphaRaslSkipped=!1),ir()){for(let e of this.sampleQueue)this.finalizeAndEmitSample(e);this.sampleQueue.length=0}this.currentPacketIndex=0,this.raslSkipped=!1}close(){this.customDecoder?this.customDecoderCallSerializer.call(()=>this.customDecoder.close()):(h(this.decoder),this.decoder.close(),this.alphaDecoder?.close(),this.colorQueue.forEach(e=>e.close()),this.colorQueue.length=0,this.alphaQueue.forEach(e=>e?.close()),this.alphaQueue.length=0,this.merger?.close());for(let e of this.sampleQueue)e.close();this.sampleQueue.length=0}},md=!1,vs=class vs{constructor(){this.canvas=null;this.gl=null;this.program=null;this.vao=null;this.colorTexture=null;this.alphaTexture=null;this.worker=null;this.pendingRequests=new Map;this.nextRequestId=0;let t=typeof OffscreenCanvas<"u"||typeof document<"u"&&typeof document.createElement=="function";if(!vs.forceCpu&&t&&!md)try{typeof OffscreenCanvas<"u"?this.canvas=new OffscreenCanvas(300,150):this.canvas=document.createElement("canvas");let e=this.canvas.getContext("webgl2",{premultipliedAlpha:!1});if(!e)throw new Error("Couldn't acquire WebGL 2 context.");this.gl=e,this.program=this.createProgram(),this.vao=this.createVAO(),this.colorTexture=this.createTexture(),this.alphaTexture=this.createTexture(),this.gl.useProgram(this.program),this.gl.uniform1i(this.gl.getUniformLocation(this.program,"u_colorTexture"),0),this.gl.uniform1i(this.gl.getUniformLocation(this.program,"u_alphaTexture"),1)}catch(e){this.gl=null,this.canvas=null,md=!0,console.warn("Falling back to CPU for color/alpha merging.",e)}}async update(t,e){return this.gl?this.updateGpu(t,e):this.updateCpu(t,e)}createProgram(){h(this.gl);let t=this.createShader(this.gl.VERTEX_SHADER,`#version 300 es
8
+ var Xd=Object.create;var ou=Object.defineProperty;var $d=Object.getOwnPropertyDescriptor;var Yd=Object.getOwnPropertyNames;var Zd=Object.getPrototypeOf,Jd=Object.prototype.hasOwnProperty;var au=(n,t)=>(t=Symbol[n])?t:Symbol.for("Symbol."+n),su=n=>{throw TypeError(n)};var em=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var tm=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Yd(t))!Jd.call(n,i)&&i!==e&&ou(n,i,{get:()=>t[i],enumerable:!(r=$d(t,i))||r.enumerable});return n};var cu=(n,t,e)=>(e=n!=null?Xd(Zd(n)):{},tm(t||!n||!n.__esModule?ou(e,"default",{value:n,enumerable:!0}):e,n));var qi=(n,t,e)=>{if(t!=null){typeof t!="object"&&typeof t!="function"&&su("Object expected");var r,i;e&&(r=t[au("asyncDispose")]),r===void 0&&(r=t[au("dispose")],e&&(i=r)),typeof r!="function"&&su("Object not disposable"),i&&(r=function(){try{i.call(this)}catch(a){return Promise.reject(a)}}),n.push([e,r,t])}else e&&n.push([e]);return t},Gi=(n,t,e)=>{var r=typeof SuppressedError=="function"?SuppressedError:function(s,o,c,u){return u=Error(c),u.name="SuppressedError",u.error=s,u.suppressed=o,u},i=s=>t=e?new r(s,t,"An error was suppressed during disposal"):(e=!0,s),a=s=>{for(;s=n.pop();)try{var o=s[1]&&s[1].call(s[2]);if(s[0])return Promise.resolve(o).then(a,c=>(i(c),a()))}catch(c){i(c)}if(e)throw t};return a()};var Xo=em(()=>{});function h(n){if(!n)throw new Error("Assertion failed.")}var Ze=n=>{let t=(n%360+360)%360;if(t===0||t===90||t===180||t===270)return t;throw new Error(`Invalid rotation ${n}.`)},X=n=>n&&n[n.length-1],_t=n=>n>=0&&n<2**32,M=n=>{let t=0;for(;n.readBits(1)===0&&t<32;)t++;if(t>=32)throw new Error("Invalid exponential-Golomb code.");return(1<<t)-1+n.readBits(t)},gt=n=>{let t=M(n);return(t&1)===0?-(t>>1):t+1>>1},du=(n,t,e,r)=>{for(let i=t;i<e;i++){let a=Math.floor(i/8),s=n[a],o=7-(i&7);s&=~(1<<o),s|=(r&1<<e-i-1)>>e-i-1<<o,n[a]=s}},j=n=>n.constructor===Uint8Array?n:ArrayBuffer.isView(n)?new Uint8Array(n.buffer,n.byteOffset,n.byteLength):new Uint8Array(n),V=n=>n.constructor===DataView?n:ArrayBuffer.isView(n)?new DataView(n.buffer,n.byteOffset,n.byteLength):new DataView(n),me=new TextDecoder,$=new TextEncoder,Je=n=>{for(let t=0;t<n.length;t++)if(n.charCodeAt(t)>255)return!1;return!0},oo=n=>Object.fromEntries(Object.entries(n).map(([t,e])=>[e,t])),et={bt709:1,bt470bg:5,smpte170m:6,bt2020:9,smpte432:12},xr=oo(et),tt={bt709:1,smpte170m:6,linear:8,"iec61966-2-1":13,pq:16,hlg:18},Pr=oo(tt),rt={rgb:0,bt709:1,bt470bg:5,smpte170m:6,"bt2020-ncl":9},Cr=oo(rt),zn=n=>!!n&&!!n.primaries&&!!n.transfer&&!!n.matrix&&n.fullRange!==void 0,Rt=n=>n instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&n instanceof SharedArrayBuffer||ArrayBuffer.isView(n),Ie=class{constructor(){this.currentPromise=Promise.resolve();this.pending=0}async acquire(){let t,e=new Promise(i=>{let a=!1;t=()=>{a||(i(),this.pending--,a=!0)}}),r=this.currentPromise;return this.currentPromise=e,this.pending++,await r,t}},mu=/^[0-9a-fA-F]+$/,tr=n=>[...n].map(t=>t.toString(16).padStart(2,"0")).join(""),fu=n=>{h(n.length%2===0);let t=new Uint8Array(n.length/2);for(let e=0;e<n.length;e+=2)t[e/2]=parseInt(n.slice(e,e+2),16);return t},co=n=>(n=n>>1&1431655765|(n&1431655765)<<1,n=n>>2&858993459|(n&858993459)<<2,n=n>>4&252645135|(n&252645135)<<4,n=n>>8&16711935|(n&16711935)<<8,n=n>>16&65535|(n&65535)<<16,n>>>0),Ft=(n,t,e)=>{let r=0,i=n.length-1,a=-1;for(;r<=i;){let s=r+i>>1,o=e(n[s]);o===t?(a=s,i=s-1):o<t?r=s+1:i=s-1}return a},H=(n,t,e)=>{let r=0,i=n.length-1,a=-1;for(;r<=i;){let s=r+(i-r+1)/2|0;e(n[s])<=t?(a=s,r=s+1):i=s-1}return a},uo=(n,t,e)=>{let r=H(n,e(t),e);n.splice(r+1,0,t)},Z=()=>{let n,t;return{promise:new Promise((r,i)=>{n=r,t=i}),resolve:n,reject:t}},lo=(n,t)=>{let e=n.indexOf(t);e!==-1&&n.splice(e,1)},mo=(n,t)=>{for(let e=n.length-1;e>=0;e--)if(t(n[e]))return n[e]},rr=(n,t)=>{for(let e=n.length-1;e>=0;e--)if(t(n[e]))return e;return-1},pu=async function*(n){Symbol.iterator in n?yield*n[Symbol.iterator]():yield*n[Symbol.asyncIterator]()},hu=n=>{if(!(Symbol.iterator in n)&&!(Symbol.asyncIterator in n))throw new TypeError("Argument must be an iterable or async iterable.")},ne=n=>{throw new Error(`Unexpected value: ${n}`)},Ir=(n,t,e)=>{let r=n.getUint8(t),i=n.getUint8(t+1),a=n.getUint8(t+2);return e?r|i<<8|a<<16:r<<16|i<<8|a},gu=(n,t,e)=>Ir(n,t,e)<<8>>8,Er=(n,t,e,r)=>{e=e>>>0,e=e&16777215,r?(n.setUint8(t,e&255),n.setUint8(t+1,e>>>8&255),n.setUint8(t+2,e>>>16&255)):(n.setUint8(t,e>>>16&255),n.setUint8(t+1,e>>>8&255),n.setUint8(t+2,e&255))},bu=(n,t,e,r)=>{e=ue(e,-8388608,8388607),e<0&&(e=e+16777216&16777215),Er(n,t,e,r)},yu=(n,t,e,r)=>{r?(n.setUint32(t+0,e,!0),n.setInt32(t+4,Math.floor(e/2**32),!0)):(n.setInt32(t+0,Math.floor(e/2**32),!0),n.setUint32(t+4,e,!0))},Qi=(n,t)=>({async next(){let e=await n.next();return e.done?{value:void 0,done:!0}:{value:t(e.value),done:!1}},return(){return n.return()},throw(e){return n.throw(e)},[Symbol.asyncIterator](){return this}}),ue=(n,t,e)=>Math.max(t,Math.min(e,n)),J="und",Mt=n=>{let t=Math.round(n);return Math.abs(n/t-1)<10*Number.EPSILON?t:n},ji=(n,t)=>Math.round(n/t)*t,bt=(n,t)=>Math.round(n*t)/t,Nn=(n,t)=>Math.floor(n/t)*t,fo=(n,t)=>Math.floor(n*t)/t,ku=n=>{let t=0;for(;n;)t++,n>>=1;return t},rm=/^[a-z]{3}$/,Bt=n=>rm.test(n),yt=1e6*(1+Number.EPSILON),Tu=(n,t)=>{let e={...n,...t};if(n.headers||t.headers){let r=n.headers?uu(n.headers):{},i=t.headers?uu(t.headers):{},a={...r};Object.entries(i).forEach(([s,o])=>{let c=Object.keys(a).find(u=>u.toLowerCase()===s.toLowerCase());c&&delete a[c],a[s]=o}),e.headers=a}return e},uu=n=>{if(n instanceof Headers){let t={};return n.forEach((e,r)=>{t[r]=e}),t}if(Array.isArray(n)){let t={};return n.forEach(([e,r])=>{t[e]=r}),t}return n},wu=async(n,t,e,r,i)=>{let a=0;for(;;)try{return await n(t,e)}catch(s){if(i())throw s;a++;let o=r(a,s,t);if(o===null)throw s;if(console.error("Retrying failed fetch. Error:",s),!Number.isFinite(o)||o<0)throw new TypeError("Retry delay must be a non-negative finite number.");if(o>0&&await $i(1e3*o),i())throw s}},Su=(n,t)=>{let e=n<0?-1:1;n=Math.abs(n);let r=0,i=1,a=1,s=0,o=n;for(;;){let c=Math.floor(o),u=c*a+r,l=c*s+i;if(l>t)return{num:e*a,den:s};if(r=a,i=s,a=u,s=l,o=1/(o-c),!isFinite(o))break}return{num:e*a,den:s}},vt=class{constructor(){this.currentPromise=Promise.resolve()}call(t){return this.currentPromise=this.currentPromise.then(t)}},ro=null,ir=()=>ro!==null?ro:ro=!!(typeof navigator<"u"&&(navigator.vendor?.match(/apple/i)||/AppleWebKit/.test(navigator.userAgent)&&!/Chrome/.test(navigator.userAgent)||/\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent))),io=null,vr=()=>io!==null?io:io=typeof navigator<"u"&&navigator.userAgent?.includes("Firefox"),no=null,Xi=()=>no!==null?no:no=!!(typeof navigator<"u"&&(navigator.vendor?.includes("Google Inc")||/Chrome/.test(navigator.userAgent))),ao=null,Au=()=>{if(ao!==null)return ao;if(typeof navigator>"u")return null;let n=/\bChrome\/(\d+)/.exec(navigator.userAgent);return n?ao=Number(n[1]):null},_r=(n,t)=>n!==-1?n:t,Ln=(n,t,e,r)=>n<=r&&e<=t,it=function*(n){for(let t in n){let e=n[t];e!==void 0&&(yield{key:t,value:e})}},xu=n=>{switch(n.toLowerCase()){case"image/jpeg":case"image/jpg":return".jpg";case"image/png":return".png";case"image/gif":return".gif";case"image/webp":return".webp";case"image/bmp":return".bmp";case"image/svg+xml":return".svg";case"image/tiff":return".tiff";case"image/avif":return".avif";case"image/x-icon":case"image/vnd.microsoft.icon":return".ico";default:return null}},Rr=n=>{let t=atob(n),e=new Uint8Array(t.length);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r);return e},Pu=n=>{let t="";for(let e=0;e<n.length;e++)t+=String.fromCharCode(n[e]);return btoa(t)},Wn=(n,t)=>{if(n.length!==t.length)return!1;for(let e=0;e<n.length;e++)if(n[e]!==t[e])return!1;return!0},ei=()=>{Symbol.dispose??=Symbol("Symbol.dispose")},nr=n=>typeof n=="number"&&!Number.isNaN(n),fe=(n,t)=>{if(t.includes("://"))return t;if(n.includes("://")){let o=n.indexOf("?");o!==-1&&(n=n.slice(0,o))}let e;if(t.startsWith("/")){let o=n.indexOf("://");if(o===-1)e=t;else{let c=n.indexOf("/",o+3);c===-1?e=n+t:e=n.slice(0,c)+t}}else{let o=n.lastIndexOf("/");o===-1?e=t:e=n.slice(0,o+1)+t}let r="",i=e.indexOf("://");if(i!==-1){let o=e.indexOf("/",i+3);o!==-1&&(r=e.slice(0,o),e=e.slice(o))}let a=e.split("/"),s=[];for(let o of a)o===".."?s.pop():o!=="."&&s.push(o);return r+s.join("/")},Fr=(n,t)=>{let e=0;for(let r=0;r<n.length;r++)t(n[r])&&e++;return e},ti=(n,t)=>{let e=-1,r=1/0;for(let i=0;i<n.length;i++){let a=t(n[i]);a<r&&(r=a,e=i)}return e},Cu=(n,t)=>{let e=-1,r=-1/0;for(let i=0;i<n.length;i++){let a=t(n[i]);a>r&&(r=a,e=i)}return e},Ot=n=>{h(Number.isInteger(n.num)),h(Number.isInteger(n.den)),h(n.den!==0);let t=Math.abs(n.num),e=Math.abs(n.den);for(;e!==0;){let i=t%e;t=e,e=i}let r=t||1;return{num:n.num/r,den:n.den/r}},po=(n,t)=>{if(typeof n!="object"||!n)throw new TypeError(`${t} must be an object.`);if(!Number.isInteger(n.left)||n.left<0)throw new TypeError(`${t}.left must be a non-negative integer.`);if(!Number.isInteger(n.top)||n.top<0)throw new TypeError(`${t}.top must be a non-negative integer.`);if(!Number.isInteger(n.width)||n.width<0)throw new TypeError(`${t}.width must be a non-negative integer.`);if(!Number.isInteger(n.height)||n.height<0)throw new TypeError(`${t}.height must be a non-negative integer.`)},Ki,im=1,lu=new Map,ho=new Map,Iu=()=>typeof window>"u",nm=()=>{let n=new Map,t=new Map;self.onmessage=e=>{let r=e.data;switch(r.type){case"set-timeout":{let i=setTimeout(()=>{n.delete(r.timerId),self.postMessage({type:"fire",timerId:r.timerId})},r.delay);n.set(r.timerId,i)}break;case"set-interval":{let i=setInterval(()=>{self.postMessage({type:"fire",timerId:r.timerId})},r.delay);t.set(r.timerId,i)}break;case"clear-timeout":{let i=n.get(r.timerId);i!==void 0&&(clearTimeout(i),n.delete(r.timerId))}break;case"clear-interval":{let i=t.get(r.timerId);i!==void 0&&(clearInterval(i),t.delete(r.timerId))}break}}},Eu=()=>{if(Ki)return Ki;let n=`(${nm.toString()})();`,t=URL.createObjectURL(new Blob([n],{type:"text/javascript"}));return Ki=new Worker(t),URL.revokeObjectURL(t),Ki.onmessage=e=>{let r=e.data,i=lu.get(r.timerId);if(i){lu.delete(r.timerId),i();return}let a=ho.get(r.timerId);a&&a()},Ki};var vu=(n,t)=>{if(Iu())return{id:setInterval(n,t)};let e=im++;return ho.set(e,()=>{n()}),Eu().postMessage({type:"set-interval",timerId:e,delay:t}),{id:e}},_u=n=>{if(Iu()){clearInterval(n.id);return}h(typeof n.id=="number"),ho.delete(n.id),Eu().postMessage({type:"clear-interval",timerId:n.id})},$i=n=>new Promise(t=>setTimeout(t,n));var Dt=n=>Array.isArray(n)?n:[n],Ye=class{constructor(){this._listeners=new Map}on(t,e,r){this._listeners.has(t)||this._listeners.set(t,new Set);let i={fn:e,once:r?.once??!1};return this._listeners.get(t).add(i),()=>{this._listeners.get(t)?.delete(i)}}_emit(...t){let[e,r]=t,i=this._listeners.get(e);if(i)for(let a of i){try{a.fn(r)}catch(s){console.error(s)}a.once&&i.delete(a)}}},Mr=n=>Math.ceil(n/2)*2,so=class{constructor(t){this._queue=[];this._errored=!1;this.parallelism=t}get errored(){return this._errored}get inFlightCount(){return this._queue.length}async run(t){for(this._errored&&await Promise.race(this._queue);this._queue.length>=this.parallelism;)await Promise.race(this._queue);let e=t();this._queue.push(e),e.then(()=>lo(this._queue,e)).catch(()=>this._errored=!0)}async flush(){await Promise.all(this._queue)}},Hn=n=>n!==null&&typeof n=="object"&&Object.getPrototypeOf(n)===Object.prototype&&Object.values(n).every(t=>typeof t=="string");var He=class{constructor(t,e){this.data=t;this.mimeType=e;if(!(t instanceof Uint8Array))throw new TypeError("data must be a Uint8Array.");if(typeof e!="string")throw new TypeError("mimeType must be a string.")}},ar=class{constructor(t,e,r,i){this.data=t;this.mimeType=e;this.name=r;this.description=i;if(!(t instanceof Uint8Array))throw new TypeError("data must be a Uint8Array.");if(e!==void 0&&typeof e!="string")throw new TypeError("mimeType, when provided, must be a string.");if(r!==void 0&&typeof r!="string")throw new TypeError("name, when provided, must be a string.");if(i!==void 0&&typeof i!="string")throw new TypeError("description, when provided, must be a string.")}},Yi=n=>{if(!n||typeof n!="object")throw new TypeError("tags must be an object.");if(n.title!==void 0&&typeof n.title!="string")throw new TypeError("tags.title, when provided, must be a string.");if(n.description!==void 0&&typeof n.description!="string")throw new TypeError("tags.description, when provided, must be a string.");if(n.artist!==void 0&&typeof n.artist!="string")throw new TypeError("tags.artist, when provided, must be a string.");if(n.album!==void 0&&typeof n.album!="string")throw new TypeError("tags.album, when provided, must be a string.");if(n.albumArtist!==void 0&&typeof n.albumArtist!="string")throw new TypeError("tags.albumArtist, when provided, must be a string.");if(n.trackNumber!==void 0&&(!Number.isInteger(n.trackNumber)||n.trackNumber<=0))throw new TypeError("tags.trackNumber, when provided, must be a positive integer.");if(n.tracksTotal!==void 0&&(!Number.isInteger(n.tracksTotal)||n.tracksTotal<=0))throw new TypeError("tags.tracksTotal, when provided, must be a positive integer.");if(n.discNumber!==void 0&&(!Number.isInteger(n.discNumber)||n.discNumber<=0))throw new TypeError("tags.discNumber, when provided, must be a positive integer.");if(n.discsTotal!==void 0&&(!Number.isInteger(n.discsTotal)||n.discsTotal<=0))throw new TypeError("tags.discsTotal, when provided, must be a positive integer.");if(n.genre!==void 0&&typeof n.genre!="string")throw new TypeError("tags.genre, when provided, must be a string.");if(n.date!==void 0&&(!(n.date instanceof Date)||Number.isNaN(n.date.getTime())))throw new TypeError("tags.date, when provided, must be a valid Date.");if(n.lyrics!==void 0&&typeof n.lyrics!="string")throw new TypeError("tags.lyrics, when provided, must be a string.");if(n.images!==void 0){if(!Array.isArray(n.images))throw new TypeError("tags.images, when provided, must be an array.");for(let t of n.images){if(!t||typeof t!="object")throw new TypeError("Each image in tags.images must be an object.");if(!(t.data instanceof Uint8Array))throw new TypeError("Each image.data must be a Uint8Array.");if(typeof t.mimeType!="string")throw new TypeError("Each image.mimeType must be a string.");if(!["coverFront","coverBack","unknown"].includes(t.kind))throw new TypeError("Each image.kind must be 'coverFront', 'coverBack', or 'unknown'.")}}if(n.comment!==void 0&&typeof n.comment!="string")throw new TypeError("tags.comment, when provided, must be a string.");if(n.raw!==void 0){if(!n.raw||typeof n.raw!="object")throw new TypeError("tags.raw, when provided, must be an object.");for(let t of Object.values(n.raw))if(t!==null&&typeof t!="string"&&!(t instanceof Uint8Array)&&!(t instanceof He)&&!(t instanceof ar)&&!Hn(t))throw new TypeError("Each value in tags.raw must be a string, Uint8Array, RichImageData, AttachedFile, Record<string, string>, or null.")}},Vt=n=>n.title===void 0&&n.description===void 0&&n.artist===void 0&&n.album===void 0&&n.albumArtist===void 0&&n.trackNumber===void 0&&n.tracksTotal===void 0&&n.discNumber===void 0&&n.discsTotal===void 0&&n.genre===void 0&&n.date===void 0&&n.lyrics===void 0&&(!n.images||n.images.length===0)&&n.comment===void 0&&(n.raw===void 0||Object.keys(n.raw).length===0),we={default:!0,primary:!0,forced:!1,original:!1,commentary:!1,hearingImpaired:!1,visuallyImpaired:!1},Ru=n=>{if(!n||typeof n!="object")throw new TypeError("disposition must be an object.");if(n.default!==void 0&&typeof n.default!="boolean")throw new TypeError("disposition.default must be a boolean.");if(n.primary!==void 0&&typeof n.primary!="boolean")throw new TypeError("disposition.primary must be a boolean.");if(n.forced!==void 0&&typeof n.forced!="boolean")throw new TypeError("disposition.forced must be a boolean.");if(n.original!==void 0&&typeof n.original!="boolean")throw new TypeError("disposition.original must be a boolean.");if(n.commentary!==void 0&&typeof n.commentary!="boolean")throw new TypeError("disposition.commentary must be a boolean.");if(n.hearingImpaired!==void 0&&typeof n.hearingImpaired!="boolean")throw new TypeError("disposition.hearingImpaired must be a boolean.");if(n.visuallyImpaired!==void 0&&typeof n.visuallyImpaired!="boolean")throw new TypeError("disposition.visuallyImpaired must be a boolean.")};var W=class n{constructor(t){this.bytes=t;this.pos=0}seekToByte(t){this.pos=8*t}readBit(){let t=Math.floor(this.pos/8),e=this.bytes[t]??0,r=7-(this.pos&7),i=(e&1<<r)>>r;return this.pos++,i}readBits(t){if(t===1)return this.readBit();let e=0;for(let r=0;r<t;r++)e<<=1,e|=this.readBit();return e}writeBits(t,e){let r=this.pos+t;for(let i=this.pos;i<r;i++){let a=Math.floor(i/8),s=this.bytes[a],o=7-(i&7);s&=~(1<<o),s|=(e&1<<r-i-1)>>r-i-1<<o,this.bytes[a]=s}this.pos=r}readAlignedByte(){if(this.pos%8!==0)throw new Error("Bitstream is not byte-aligned.");let t=this.pos/8,e=this.bytes[t]??0;return this.pos+=8,e}skipBits(t){this.pos+=t}getBitsLeft(){return this.bytes.length*8-this.pos}clone(){let t=new n(this.bytes);return t.pos=this.pos,t}};var qe=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],kt=[-1,1,2,3,4,5,6,8],Tt=n=>{if(!n||n.byteLength<2)throw new TypeError("AAC description must be at least 2 bytes long.");let t=new W(n),e=t.readBits(5);e===31&&(e=32+t.readBits(6));let r=t.readBits(4),i=null;r===15?i=t.readBits(24):r<qe.length&&(i=qe[r]);let a=t.readBits(4),s=null;return a>=1&&a<=7&&(s=kt[a]),{objectType:e,frequencyIndex:r,sampleRate:i,channelConfiguration:a,numberOfChannels:s}},ri=n=>{let t=qe.indexOf(n.sampleRate),e=null;t===-1&&(t=15,e=n.sampleRate);let r=kt.indexOf(n.numberOfChannels);if(r===-1)throw new TypeError(`Unsupported number of channels: ${n.numberOfChannels}`);let i=13;n.objectType>=32&&(i+=6),t===15&&(i+=24);let a=Math.ceil(i/8),s=new Uint8Array(a),o=new W(s);return n.objectType<32?o.writeBits(5,n.objectType):(o.writeBits(5,31),o.writeBits(6,n.objectType-32)),o.writeBits(4,t),t===15&&o.writeBits(24,e),o.writeBits(4,r),s},qn=n=>{let t=new Uint8Array(7),e=new W(t),{objectType:r,frequencyIndex:i,channelConfiguration:a}=n,s=r-1;return e.writeBits(12,4095),e.writeBits(1,0),e.writeBits(2,0),e.writeBits(1,1),e.writeBits(2,s),e.writeBits(4,i),e.writeBits(1,0),e.writeBits(3,a),e.writeBits(1,0),e.writeBits(1,0),e.writeBits(1,0),e.writeBits(1,0),e.skipBits(13),e.writeBits(11,2047),e.writeBits(2,0),{header:t,bitstream:e}},Gn=(n,t)=>{n.pos=30,n.writeBits(13,t)};var re=["avc","hevc","vp9","av1","vp8"],ee=["pcm-s16","pcm-s16be","pcm-s24","pcm-s24be","pcm-s32","pcm-s32be","pcm-f32","pcm-f32be","pcm-f64","pcm-f64be","pcm-u8","pcm-s8","ulaw","alaw"],wt=["aac","opus","mp3","vorbis","flac","ac3","eac3"],ae=[...wt,...ee],Me=["webvtt"],Zi=[{maxMacroblocks:99,maxBitrate:64e3,maxDpbMbs:396,level:10},{maxMacroblocks:396,maxBitrate:192e3,maxDpbMbs:900,level:11},{maxMacroblocks:396,maxBitrate:384e3,maxDpbMbs:2376,level:12},{maxMacroblocks:396,maxBitrate:768e3,maxDpbMbs:2376,level:13},{maxMacroblocks:396,maxBitrate:2e6,maxDpbMbs:2376,level:20},{maxMacroblocks:792,maxBitrate:4e6,maxDpbMbs:4752,level:21},{maxMacroblocks:1620,maxBitrate:4e6,maxDpbMbs:8100,level:22},{maxMacroblocks:1620,maxBitrate:1e7,maxDpbMbs:8100,level:30},{maxMacroblocks:3600,maxBitrate:14e6,maxDpbMbs:18e3,level:31},{maxMacroblocks:5120,maxBitrate:2e7,maxDpbMbs:20480,level:32},{maxMacroblocks:8192,maxBitrate:2e7,maxDpbMbs:32768,level:40},{maxMacroblocks:8192,maxBitrate:5e7,maxDpbMbs:32768,level:41},{maxMacroblocks:8704,maxBitrate:5e7,maxDpbMbs:34816,level:42},{maxMacroblocks:22080,maxBitrate:135e6,maxDpbMbs:110400,level:50},{maxMacroblocks:36864,maxBitrate:24e7,maxDpbMbs:184320,level:51},{maxMacroblocks:36864,maxBitrate:24e7,maxDpbMbs:184320,level:52},{maxMacroblocks:139264,maxBitrate:24e7,maxDpbMbs:696320,level:60},{maxMacroblocks:139264,maxBitrate:48e7,maxDpbMbs:696320,level:61},{maxMacroblocks:139264,maxBitrate:8e8,maxDpbMbs:696320,level:62}],Fu=[{maxPictureSize:36864,maxBitrate:128e3,tier:"L",level:30},{maxPictureSize:122880,maxBitrate:15e5,tier:"L",level:60},{maxPictureSize:245760,maxBitrate:3e6,tier:"L",level:63},{maxPictureSize:552960,maxBitrate:6e6,tier:"L",level:90},{maxPictureSize:983040,maxBitrate:1e7,tier:"L",level:93},{maxPictureSize:2228224,maxBitrate:12e6,tier:"L",level:120},{maxPictureSize:2228224,maxBitrate:3e7,tier:"H",level:120},{maxPictureSize:2228224,maxBitrate:2e7,tier:"L",level:123},{maxPictureSize:2228224,maxBitrate:5e7,tier:"H",level:123},{maxPictureSize:8912896,maxBitrate:25e6,tier:"L",level:150},{maxPictureSize:8912896,maxBitrate:1e8,tier:"H",level:150},{maxPictureSize:8912896,maxBitrate:4e7,tier:"L",level:153},{maxPictureSize:8912896,maxBitrate:16e7,tier:"H",level:153},{maxPictureSize:8912896,maxBitrate:6e7,tier:"L",level:156},{maxPictureSize:8912896,maxBitrate:24e7,tier:"H",level:156},{maxPictureSize:35651584,maxBitrate:6e7,tier:"L",level:180},{maxPictureSize:35651584,maxBitrate:24e7,tier:"H",level:180},{maxPictureSize:35651584,maxBitrate:12e7,tier:"L",level:183},{maxPictureSize:35651584,maxBitrate:48e7,tier:"H",level:183},{maxPictureSize:35651584,maxBitrate:24e7,tier:"L",level:186},{maxPictureSize:35651584,maxBitrate:8e8,tier:"H",level:186}],Ut=[{maxPictureSize:36864,maxBitrate:2e5,level:10},{maxPictureSize:73728,maxBitrate:8e5,level:11},{maxPictureSize:122880,maxBitrate:18e5,level:20},{maxPictureSize:245760,maxBitrate:36e5,level:21},{maxPictureSize:552960,maxBitrate:72e5,level:30},{maxPictureSize:983040,maxBitrate:12e6,level:31},{maxPictureSize:2228224,maxBitrate:18e6,level:40},{maxPictureSize:2228224,maxBitrate:3e7,level:41},{maxPictureSize:8912896,maxBitrate:6e7,level:50},{maxPictureSize:8912896,maxBitrate:12e7,level:51},{maxPictureSize:8912896,maxBitrate:18e7,level:52},{maxPictureSize:35651584,maxBitrate:18e7,level:60},{maxPictureSize:35651584,maxBitrate:24e7,level:61},{maxPictureSize:35651584,maxBitrate:48e7,level:62}],Mu=[{maxPictureSize:147456,maxBitrate:15e5,tier:"M",level:0},{maxPictureSize:278784,maxBitrate:3e6,tier:"M",level:1},{maxPictureSize:665856,maxBitrate:6e6,tier:"M",level:4},{maxPictureSize:1065024,maxBitrate:1e7,tier:"M",level:5},{maxPictureSize:2359296,maxBitrate:12e6,tier:"M",level:8},{maxPictureSize:2359296,maxBitrate:3e7,tier:"H",level:8},{maxPictureSize:2359296,maxBitrate:2e7,tier:"M",level:9},{maxPictureSize:2359296,maxBitrate:5e7,tier:"H",level:9},{maxPictureSize:8912896,maxBitrate:3e7,tier:"M",level:12},{maxPictureSize:8912896,maxBitrate:1e8,tier:"H",level:12},{maxPictureSize:8912896,maxBitrate:4e7,tier:"M",level:13},{maxPictureSize:8912896,maxBitrate:16e7,tier:"H",level:13},{maxPictureSize:8912896,maxBitrate:6e7,tier:"M",level:14},{maxPictureSize:8912896,maxBitrate:24e7,tier:"H",level:14},{maxPictureSize:35651584,maxBitrate:6e7,tier:"M",level:15},{maxPictureSize:35651584,maxBitrate:24e7,tier:"H",level:15},{maxPictureSize:35651584,maxBitrate:6e7,tier:"M",level:16},{maxPictureSize:35651584,maxBitrate:24e7,tier:"H",level:16},{maxPictureSize:35651584,maxBitrate:1e8,tier:"M",level:17},{maxPictureSize:35651584,maxBitrate:48e7,tier:"H",level:17},{maxPictureSize:35651584,maxBitrate:16e7,tier:"M",level:18},{maxPictureSize:35651584,maxBitrate:8e8,tier:"H",level:18},{maxPictureSize:35651584,maxBitrate:16e7,tier:"M",level:19},{maxPictureSize:35651584,maxBitrate:8e8,tier:"H",level:19}],Bu=".01.01.01.01.00",Ou=".0.110.01.01.01.0",Kn=(n,t,e,r)=>{if(n==="avc"){let a=Math.ceil(t/16)*Math.ceil(e/16),s=Zi.find(m=>a<=m.maxMacroblocks&&r<=m.maxBitrate)??X(Zi),o=s?s.level:0,c="64".padStart(2,"0"),u="00",l=o.toString(16).padStart(2,"0");return`avc1.${c}${u}${l}`}else if(n==="hevc"){let i="",s="6",o=t*e,c=Fu.find(l=>o<=l.maxPictureSize&&r<=l.maxBitrate)??X(Fu);return`hev1.${i}1.${s}.${c.tier}${c.level}.B0`}else{if(n==="vp8")return"vp8";if(n==="vp9"){let i="00",a=t*e,s=Ut.find(c=>a<=c.maxPictureSize&&r<=c.maxBitrate)??X(Ut);return`vp09.${i}.${s.level.toString().padStart(2,"0")}.08`}else if(n==="av1"){let a=t*e,s=Mu.find(u=>a<=u.maxPictureSize&&r<=u.maxBitrate)??X(Mu);return`av01.0.${s.level.toString().padStart(2,"0")}${s.tier}.08`}}throw new TypeError(`Unhandled codec '${n}'.`)},Du=n=>{let t=n.split("."),e=Number(t[1]),r=Number(t[2]),i=Number(t[3]),a=t[4]?Number(t[4]):1;return[1,1,e,2,1,r,3,1,i,4,1,a]},Qn=n=>{let t=n.split("."),i=(1<<7)+1,a=Number(t[1]),s=t[2],o=Number(s.slice(0,-1)),c=(a<<5)+o,u=s.slice(-1)==="H"?1:0,m=Number(t[3])===8?0:1,d=0,f=t[4]?Number(t[4]):0,p=t[5]?Number(t[5][0]):1,b=t[5]?Number(t[5][1]):1,g=t[5]?Number(t[5][2]):0,y=(u<<7)+(m<<6)+(d<<5)+(f<<4)+(p<<3)+(b<<2)+g;return[i,c,y,0]},ii=n=>{let{codec:t,codecDescription:e,colorSpace:r,avcCodecInfo:i,hevcCodecInfo:a,vp9CodecInfo:s,av1CodecInfo:o}=n;if(t==="avc"){if(h(n.avcType!==null),i){let c=new Uint8Array([i.avcProfileIndication,i.profileCompatibility,i.avcLevelIndication]);return`avc${n.avcType}.${tr(c)}`}if(!e||e.byteLength<4)throw new TypeError("AVC decoder description is not provided or is not at least 4 bytes long.");return`avc${n.avcType}.${tr(e.subarray(1,4))}`}else if(t==="hevc"){let c,u,l,m,d,f;if(a)c=a.generalProfileSpace,u=a.generalProfileIdc,l=co(a.generalProfileCompatibilityFlags),m=a.generalTierFlag,d=a.generalLevelIdc,f=[...a.generalConstraintIndicatorFlags];else{if(!e||e.byteLength<23)throw new TypeError("HEVC decoder description is not provided or is not at least 23 bytes long.");let b=V(e),g=b.getUint8(1);c=g>>6&3,u=g&31,l=co(b.getUint32(2)),m=g>>5&1,d=b.getUint8(12),f=[];for(let y=0;y<6;y++)f.push(b.getUint8(6+y))}let p="hev1.";for(p+=["","A","B","C"][c]+u,p+=".",p+=l.toString(16).toUpperCase(),p+=".",p+=m===0?"L":"H",p+=d;f.length>0&&f[f.length-1]===0;)f.pop();return f.length>0&&(p+=".",p+=f.map(b=>b.toString(16).toUpperCase()).join(".")),p}else{if(t==="vp8")return"vp8";if(t==="vp9"){if(!s){let y=n.width*n.height,k=X(Ut).level;for(let w of Ut)if(y<=w.maxPictureSize){k=w.level;break}return`vp09.00.${k.toString().padStart(2,"0")}.08`}let c=s.profile.toString().padStart(2,"0"),u=s.level.toString().padStart(2,"0"),l=s.bitDepth.toString().padStart(2,"0"),m=s.chromaSubsampling.toString().padStart(2,"0"),d=s.colourPrimaries.toString().padStart(2,"0"),f=s.transferCharacteristics.toString().padStart(2,"0"),p=s.matrixCoefficients.toString().padStart(2,"0"),b=s.videoFullRangeFlag.toString().padStart(2,"0"),g=`vp09.${c}.${u}.${l}.${m}`;return g+=`.${d}.${f}.${p}.${b}`,g.endsWith(Bu)&&(g=g.slice(0,-Bu.length)),g}else if(t==="av1"){if(!o){let w=n.width*n.height,T=X(Ut).level;for(let S of Ut)if(w<=S.maxPictureSize){T=S.level;break}return`av01.0.${T.toString().padStart(2,"0")}M.08`}let c=o.profile,u=o.level.toString().padStart(2,"0"),l=o.tier?"H":"M",m=o.bitDepth.toString().padStart(2,"0"),d=o.monochrome?"1":"0",f=100*o.chromaSubsamplingX+10*o.chromaSubsamplingY+1*(o.chromaSubsamplingX&&o.chromaSubsamplingY?o.chromaSamplePosition:0),p=r?.primaries?et[r.primaries]:1,b=r?.transfer?tt[r.transfer]:1,g=r?.matrix?rt[r.matrix]:1,y=r?.fullRange?1:0,k=`av01.${c}.${u}${l}.${m}`;return k+=`.${d}.${f.toString().padStart(3,"0")}`,k+=`.${p.toString().padStart(2,"0")}`,k+=`.${b.toString().padStart(2,"0")}`,k+=`.${g.toString().padStart(2,"0")}`,k+=`.${y}`,k.endsWith(Ou)&&(k=k.slice(0,-Ou.length)),k}}throw new TypeError(`Unhandled codec '${t}'.`)},jn=(n,t,e)=>{if(n==="aac")return t>=2&&e<=24e3?"mp4a.40.29":e<=24e3?"mp4a.40.5":"mp4a.40.2";if(n==="mp3")return"mp3";if(n==="opus")return"opus";if(n==="vorbis")return"vorbis";if(n==="flac")return"flac";if(n==="ac3")return"ac-3";if(n==="eac3")return"ec-3";if(ee.includes(n))return n;throw new TypeError(`Unhandled codec '${n}'.`)},ni=n=>{let{codec:t,codecDescription:e,aacCodecInfo:r}=n;if(t==="aac"){if(!r)throw new TypeError("AAC codec info must be provided.");if(r.isMpeg2)return"mp4a.67";{let i;return r.objectType!==null?i=r.objectType:i=Tt(e).objectType,`mp4a.40.${i}`}}else{if(t==="mp3")return"mp3";if(t==="opus")return"opus";if(t==="vorbis")return"vorbis";if(t==="flac")return"flac";if(t==="ac3")return"ac-3";if(t==="eac3")return"ec-3";if(t&&ee.includes(t))return t}throw new TypeError(`Unhandled codec '${t}'.`)},Vu=n=>{},Uu=n=>{switch(n.codec){case"flac":{let t=Rr("ZkxhQ4AAACIQABAAAAYtACWtCsRC8AANRBhVFucAcYu5ASE2m1Dxv8tw");return n.sampleRate>=1<<20||n.numberOfChannels>8?!1:(t[18]=n.sampleRate>>>12,t[19]=n.sampleRate>>>4,t[20]=(n.sampleRate&15)<<4|n.numberOfChannels-1<<1,t)}case"vorbis":{let t=Rr("Ah7/AgF2b3JiaXMAAAAAAoC7AAAAAAAAgLUBAAAAAAC4AQN2b3JiaXMNAAAATGF2ZjU4Ljc2LjEwMAgAAAAMAAAAbGFuZ3VhZ2U9dW5kGQAAAGhhbmRsZXJfbmFtZT1Tb3VuZEhhbmRsZXIWAAAAdmVuZG9yX2lkPVswXVswXVswXVswXSAAAABlbmNvZGVyPUxhdmM1OC4xMzQuMTAwIGxpYnZvcmJpcxAAAABtYWpvcl9icmFuZD1pc29tEQAAAG1pbm9yX3ZlcnNpb249NTEyIgAAAGNvbXBhdGlibGVfYnJhbmRzPWlzb21pc28yYXZjMW1wNDEmAAAAREVTQ1JJUFRJT049TWFkZSB3aXRoIFJlbW90aW9uIDQuMC4yNzgBBXZvcmJpcyVCQ1YBAEAAACRzGCpGpXMWhBAaQlAZ4xxCzmvsGUJMEYIcMkxbyyVzkCGkoEKIWyiB0JBVAABAAACHQXgUhIpBCCGEJT1YkoMnPQghhIg5eBSEaUEIIYQQQgghhBBCCCGERTlokoMnQQgdhOMwOAyD5Tj4HIRFOVgQgydB6CCED0K4moOsOQghhCQ1SFCDBjnoHITCLCiKgsQwuBaEBDUojILkMMjUgwtCiJqDSTX4GoRnQXgWhGlBCCGEJEFIkIMGQcgYhEZBWJKDBjm4FITLQagahCo5CB+EIDRkFQCQAACgoiiKoigKEBqyCgDIAAAQQFEUx3EcyZEcybEcCwgNWQUAAAEACAAAoEiKpEiO5EiSJFmSJVmSJVmS5omqLMuyLMuyLMsyEBqyCgBIAABQUQxFcRQHCA1ZBQBkAAAIoDiKpViKpWiK54iOCISGrAIAgAAABAAAEDRDUzxHlETPVFXXtm3btm3btm3btm3btm1blmUZCA1ZBQBAAAAQ0mlmqQaIMAMZBkJDVgEACAAAgBGKMMSA0JBVAABAAACAGEoOogmtOd+c46BZDppKsTkdnEi1eZKbirk555xzzsnmnDHOOeecopxZDJoJrTnnnMSgWQqaCa0555wnsXnQmiqtOeeccc7pYJwRxjnnnCateZCajbU555wFrWmOmkuxOeecSLl5UptLtTnnnHPOOeecc84555zqxekcnBPOOeecqL25lpvQxTnnnE/G6d6cEM4555xzzjnnnHPOOeecIDRkFQAABABAEIaNYdwpCNLnaCBGEWIaMulB9+gwCRqDnELq0ehopJQ6CCWVcVJKJwgNWQUAAAIAQAghhRRSSCGFFFJIIYUUYoghhhhyyimnoIJKKqmooowyyyyzzDLLLLPMOuyssw47DDHEEEMrrcRSU2011lhr7jnnmoO0VlprrbVSSimllFIKQkNWAQAgAAAEQgYZZJBRSCGFFGKIKaeccgoqqIDQkFUAACAAgAAAAABP8hzRER3RER3RER3RER3R8RzPESVREiVREi3TMjXTU0VVdWXXlnVZt31b2IVd933d933d+HVhWJZlWZZlWZZlWZZlWZZlWZYgNGQVAAACAAAghBBCSCGFFFJIKcYYc8w56CSUEAgNWQUAAAIACAAAAHAUR3EcyZEcSbIkS9IkzdIsT/M0TxM9URRF0zRV0RVdUTdtUTZl0zVdUzZdVVZtV5ZtW7Z125dl2/d93/d93/d93/d93/d9XQdCQ1YBABIAADqSIymSIimS4ziOJElAaMgqAEAGAEAAAIriKI7jOJIkSZIlaZJneZaomZrpmZ4qqkBoyCoAABAAQAAAAAAAAIqmeIqpeIqoeI7oiJJomZaoqZoryqbsuq7ruq7ruq7ruq7ruq7ruq7ruq7ruq7ruq7ruq7ruq7ruq4LhIasAgAkAAB0JEdyJEdSJEVSJEdygNCQVQCADACAAAAcwzEkRXIsy9I0T/M0TxM90RM901NFV3SB0JBVAAAgAIAAAAAAAAAMybAUy9EcTRIl1VItVVMt1VJF1VNVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVN0zRNEwgNWQkAkAEAkBBTLS3GmgmLJGLSaqugYwxS7KWxSCpntbfKMYUYtV4ah5RREHupJGOKQcwtpNApJq3WVEKFFKSYYyoVUg5SIDRkhQAQmgHgcBxAsixAsiwAAAAAAAAAkDQN0DwPsDQPAAAAAAAAACRNAyxPAzTPAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABA0jRA8zxA8zwAAAAAAAAA0DwP8DwR8EQRAAAAAAAAACzPAzTRAzxRBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABA0jRA8zxA8zwAAAAAAAAAsDwP8EQR0DwRAAAAAAAAACzPAzxRBDzRAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAEOAAABBgIRQasiIAiBMAcEgSJAmSBM0DSJYFTYOmwTQBkmVB06BpME0AAAAAAAAAAAAAJE2DpkHTIIoASdOgadA0iCIAAAAAAAAAAAAAkqZB06BpEEWApGnQNGgaRBEAAAAAAAAAAAAAzzQhihBFmCbAM02IIkQRpgkAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAGHAAAAgwoQwUGrIiAIgTAHA4imUBAIDjOJYFAACO41gWAABYliWKAABgWZooAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAYcAAACDChDBQashIAiAIAcCiKZQHHsSzgOJYFJMmyAJYF0DyApgFEEQAIAAAocAAACLBBU2JxgEJDVgIAUQAABsWxLE0TRZKkaZoniiRJ0zxPFGma53meacLzPM80IYqiaJoQRVE0TZimaaoqME1VFQAAUOAAABBgg6bE4gCFhqwEAEICAByKYlma5nmeJ4qmqZokSdM8TxRF0TRNU1VJkqZ5niiKommapqqyLE3zPFEURdNUVVWFpnmeKIqiaaqq6sLzPE8URdE0VdV14XmeJ4qiaJqq6roQRVE0TdNUTVV1XSCKpmmaqqqqrgtETxRNU1Vd13WB54miaaqqq7ouEE3TVFVVdV1ZBpimaaqq68oyQFVV1XVdV5YBqqqqruu6sgxQVdd1XVmWZQCu67qyLMsCAAAOHAAAAoygk4wqi7DRhAsPQKEhKwKAKAAAwBimFFPKMCYhpBAaxiSEFEImJaXSUqogpFJSKRWEVEoqJaOUUmopVRBSKamUCkIqJZVSAADYgQMA2IGFUGjISgAgDwCAMEYpxhhzTiKkFGPOOScRUoox55yTSjHmnHPOSSkZc8w556SUzjnnnHNSSuacc845KaVzzjnnnJRSSuecc05KKSWEzkEnpZTSOeecEwAAVOAAABBgo8jmBCNBhYasBABSAQAMjmNZmuZ5omialiRpmud5niiapiZJmuZ5nieKqsnzPE8URdE0VZXneZ4oiqJpqirXFUXTNE1VVV2yLIqmaZqq6rowTdNUVdd1XZimaaqq67oubFtVVdV1ZRm2raqq6rqyDFzXdWXZloEsu67s2rIAAPAEBwCgAhtWRzgpGgssNGQlAJABAEAYg5BCCCFlEEIKIYSUUggJAAAYcAAACDChDBQashIASAUAAIyx1lprrbXWQGettdZaa62AzFprrbXWWmuttdZaa6211lJrrbXWWmuttdZaa6211lprrbXWWmuttdZaa6211lprrbXWWmuttdZaa6211lprrbXWWmstpZRSSimllFJKKaWUUkoppZRSSgUA+lU4APg/2LA6wknRWGChISsBgHAAAMAYpRhzDEIppVQIMeacdFRai7FCiDHnJKTUWmzFc85BKCGV1mIsnnMOQikpxVZjUSmEUlJKLbZYi0qho5JSSq3VWIwxqaTWWoutxmKMSSm01FqLMRYjbE2ptdhqq7EYY2sqLbQYY4zFCF9kbC2m2moNxggjWywt1VprMMYY3VuLpbaaizE++NpSLDHWXAAAd4MDAESCjTOsJJ0VjgYXGrISAAgJACAQUooxxhhzzjnnpFKMOeaccw5CCKFUijHGnHMOQgghlIwx5pxzEEIIIYRSSsaccxBCCCGEkFLqnHMQQgghhBBKKZ1zDkIIIYQQQimlgxBCCCGEEEoopaQUQgghhBBCCKmklEIIIYRSQighlZRSCCGEEEIpJaSUUgohhFJCCKGElFJKKYUQQgillJJSSimlEkoJJYQSUikppRRKCCGUUkpKKaVUSgmhhBJKKSWllFJKIYQQSikFAAAcOAAABBhBJxlVFmGjCRcegEJDVgIAZAAAkKKUUiktRYIipRikGEtGFXNQWoqocgxSzalSziDmJJaIMYSUk1Qy5hRCDELqHHVMKQYtlRhCxhik2HJLoXMOAAAAQQCAgJAAAAMEBTMAwOAA4XMQdAIERxsAgCBEZohEw0JweFAJEBFTAUBigkIuAFRYXKRdXECXAS7o4q4DIQQhCEEsDqCABByccMMTb3jCDU7QKSp1IAAAAAAADADwAACQXAAREdHMYWRobHB0eHyAhIiMkAgAAAAAABcAfAAAJCVAREQ0cxgZGhscHR4fICEiIyQBAIAAAgAAAAAggAAEBAQAAAAAAAIAAAAEBA=="),e=V(t);return e.setUint8(15,n.numberOfChannels),e.setUint32(16,n.sampleRate,!0),t}default:return}},nt=48e3,zu=/^pcm-([usf])(\d+)(be)?$/,Se=n=>{if(h(ee.includes(n)),n==="ulaw")return{dataType:"ulaw",sampleSize:1,littleEndian:!0,silentValue:255};if(n==="alaw")return{dataType:"alaw",sampleSize:1,littleEndian:!0,silentValue:213};let t=zu.exec(n);h(t);let e;t[1]==="u"?e="unsigned":t[1]==="s"?e="signed":e="float";let r=Number(t[2])/8,i=t[3]!=="be",a=n==="pcm-u8"?2**7:0;return{dataType:e,sampleSize:r,littleEndian:i,silentValue:a}},Ge=n=>n.startsWith("avc1")||n.startsWith("avc3")?"avc":n.startsWith("hev1")||n.startsWith("hvc1")?"hevc":n==="vp8"?"vp8":n.startsWith("vp09")?"vp9":n.startsWith("av01")?"av1":n==="mp3"||n==="mp4a.69"||n==="mp4a.6B"||n==="mp4a.6b"||n==="mp4a.40.34"?"mp3":n.startsWith("mp4a.40.")||n==="mp4a.67"?"aac":n==="opus"?"opus":n==="vorbis"?"vorbis":n==="flac"?"flac":n==="ac-3"||n==="ac3"?"ac3":n==="ec-3"||n==="eac3"?"eac3":n==="ulaw"?"ulaw":n==="alaw"?"alaw":zu.test(n)?n:n==="webvtt"?"webvtt":null,Nu=n=>n==="avc"?{avc:{format:"avc"}}:n==="hevc"?{hevc:{format:"hevc"}}:{},Lu=n=>n==="aac"?{aac:{format:"aac"}}:n==="opus"?{opus:{format:"opus"}}:{},am=["avc1","avc3","hev1","hvc1","vp8","vp09","av01"],sm=/^(avc1|avc3)\.[0-9a-fA-F]{6}$/,om=/^(hev1|hvc1)\.(?:[ABC]?\d+)\.[0-9a-fA-F]{1,8}\.[LH]\d+(?:\.[0-9a-fA-F]{1,2}){0,6}$/,cm=/^vp09(?:\.\d{2}){3}(?:(?:\.\d{2}){5})?$/,um=/^av01\.\d\.\d{2}[MH]\.\d{2}(?:\.\d\.\d{3}\.\d{2}\.\d{2}\.\d{2}\.\d)?$/,sr=n=>{if(!n)throw new TypeError("Video chunk metadata must be provided.");if(typeof n!="object")throw new TypeError("Video chunk metadata must be an object.");if(!n.decoderConfig)throw new TypeError("Video chunk metadata must include a decoder configuration.");if(typeof n.decoderConfig!="object")throw new TypeError("Video chunk metadata decoder configuration must be an object.");if(typeof n.decoderConfig.codec!="string")throw new TypeError("Video chunk metadata decoder configuration must specify a codec string.");if(!am.some(t=>n.decoderConfig.codec.startsWith(t)))throw new TypeError("Video chunk metadata decoder configuration codec string must be a valid video codec string as specified in the Mediabunny Codec Registry.");if(!Number.isInteger(n.decoderConfig.codedWidth)||n.decoderConfig.codedWidth<=0)throw new TypeError("Video chunk metadata decoder configuration must specify a valid codedWidth (positive integer).");if(!Number.isInteger(n.decoderConfig.codedHeight)||n.decoderConfig.codedHeight<=0)throw new TypeError("Video chunk metadata decoder configuration must specify a valid codedHeight (positive integer).");if(n.decoderConfig.displayAspectWidth!==void 0&&(!Number.isInteger(n.decoderConfig.displayAspectWidth)||n.decoderConfig.displayAspectWidth<=0))throw new TypeError("Video chunk metadata decoder configuration displayAspectWidth, when defined, must be a positive integer.");if(n.decoderConfig.displayAspectHeight!==void 0&&(!Number.isInteger(n.decoderConfig.displayAspectHeight)||n.decoderConfig.displayAspectHeight<=0))throw new TypeError("Video chunk metadata decoder configuration displayAspectHeight, when defined, must be a positive integer.");if(n.decoderConfig.displayAspectWidth!==void 0!=(n.decoderConfig.displayAspectHeight!==void 0))throw new TypeError("Video chunk metadata decoder configuration must specify both displayAspectWidth and displayAspectHeight, or neither.");if(n.decoderConfig.description!==void 0&&!Rt(n.decoderConfig.description))throw new TypeError("Video chunk metadata decoder configuration description, when defined, must be an ArrayBuffer or an ArrayBuffer view.");if(n.decoderConfig.colorSpace!==void 0){let{colorSpace:t}=n.decoderConfig;if(typeof t!="object")throw new TypeError("Video chunk metadata decoder configuration colorSpace, when provided, must be an object.");let e=Object.keys(et);if(t.primaries!=null&&!e.includes(t.primaries))throw new TypeError(`Video chunk metadata decoder configuration colorSpace primaries, when defined, must be one of ${e.join(", ")}.`);let r=Object.keys(tt);if(t.transfer!=null&&!r.includes(t.transfer))throw new TypeError(`Video chunk metadata decoder configuration colorSpace transfer, when defined, must be one of ${r.join(", ")}.`);let i=Object.keys(rt);if(t.matrix!=null&&!i.includes(t.matrix))throw new TypeError(`Video chunk metadata decoder configuration colorSpace matrix, when defined, must be one of ${i.join(", ")}.`);if(t.fullRange!=null&&typeof t.fullRange!="boolean")throw new TypeError("Video chunk metadata decoder configuration colorSpace fullRange, when defined, must be a boolean.")}if(n.decoderConfig.codec.startsWith("avc1")||n.decoderConfig.codec.startsWith("avc3")){if(!sm.test(n.decoderConfig.codec))throw new TypeError("Video chunk metadata decoder configuration codec string for AVC must be a valid AVC codec string as specified in Section 3.4 of RFC 6381.")}else if(n.decoderConfig.codec.startsWith("hev1")||n.decoderConfig.codec.startsWith("hvc1")){if(!om.test(n.decoderConfig.codec))throw new TypeError("Video chunk metadata decoder configuration codec string for HEVC must be a valid HEVC codec string as specified in Section E.3 of ISO 14496-15.")}else if(n.decoderConfig.codec.startsWith("vp8")){if(n.decoderConfig.codec!=="vp8")throw new TypeError('Video chunk metadata decoder configuration codec string for VP8 must be "vp8".')}else if(n.decoderConfig.codec.startsWith("vp09")){if(!cm.test(n.decoderConfig.codec))throw new TypeError('Video chunk metadata decoder configuration codec string for VP9 must be a valid VP9 codec string as specified in Section "Codecs Parameter String" of https://www.webmproject.org/vp9/mp4/.')}else if(n.decoderConfig.codec.startsWith("av01")&&!um.test(n.decoderConfig.codec))throw new TypeError('Video chunk metadata decoder configuration codec string for AV1 must be a valid AV1 codec string as specified in Section "Codecs Parameter String" of https://aomediacodec.github.io/av1-isobmff/.')},lm=["mp4a","mp3","opus","vorbis","flac","ulaw","alaw","pcm","ac-3","ec-3"],Ee=n=>{if(!n)throw new TypeError("Audio chunk metadata must be provided.");if(typeof n!="object")throw new TypeError("Audio chunk metadata must be an object.");if(!n.decoderConfig)throw new TypeError("Audio chunk metadata must include a decoder configuration.");if(typeof n.decoderConfig!="object")throw new TypeError("Audio chunk metadata decoder configuration must be an object.");if(typeof n.decoderConfig.codec!="string")throw new TypeError("Audio chunk metadata decoder configuration must specify a codec string.");if(!lm.some(t=>n.decoderConfig.codec.startsWith(t)))throw new TypeError("Audio chunk metadata decoder configuration codec string must be a valid audio codec string as specified in the Mediabunny Codec Registry.");if(!Number.isInteger(n.decoderConfig.sampleRate)||n.decoderConfig.sampleRate<=0)throw new TypeError("Audio chunk metadata decoder configuration must specify a valid sampleRate (positive integer).");if(!Number.isInteger(n.decoderConfig.numberOfChannels)||n.decoderConfig.numberOfChannels<=0)throw new TypeError("Audio chunk metadata decoder configuration must specify a valid numberOfChannels (positive integer).");if(n.decoderConfig.description!==void 0&&!Rt(n.decoderConfig.description))throw new TypeError("Audio chunk metadata decoder configuration description, when defined, must be an ArrayBuffer or an ArrayBuffer view.");if(n.decoderConfig.codec.startsWith("mp4a")&&n.decoderConfig.codec!=="mp4a.69"&&n.decoderConfig.codec!=="mp4a.6B"&&n.decoderConfig.codec!=="mp4a.6b"){if(!["mp4a.40.2","mp4a.40.02","mp4a.40.5","mp4a.40.05","mp4a.40.29","mp4a.67"].includes(n.decoderConfig.codec))throw new TypeError("Audio chunk metadata decoder configuration codec string for AAC must be a valid AAC codec string as specified in https://www.w3.org/TR/webcodecs-aac-codec-registration/.")}else if(n.decoderConfig.codec.startsWith("mp3")||n.decoderConfig.codec.startsWith("mp4a")){if(n.decoderConfig.codec!=="mp3"&&n.decoderConfig.codec!=="mp4a.69"&&n.decoderConfig.codec!=="mp4a.6B"&&n.decoderConfig.codec!=="mp4a.6b")throw new TypeError('Audio chunk metadata decoder configuration codec string for MP3 must be "mp3", "mp4a.69" or "mp4a.6B".')}else if(n.decoderConfig.codec.startsWith("opus")){if(n.decoderConfig.codec!=="opus")throw new TypeError('Audio chunk metadata decoder configuration codec string for Opus must be "opus".');if(n.decoderConfig.description&&n.decoderConfig.description.byteLength<18)throw new TypeError("Audio chunk metadata decoder configuration description, when specified, is expected to be an Identification Header as specified in Section 5.1 of RFC 7845.")}else if(n.decoderConfig.codec.startsWith("vorbis")){if(n.decoderConfig.codec!=="vorbis")throw new TypeError('Audio chunk metadata decoder configuration codec string for Vorbis must be "vorbis".');if(!n.decoderConfig.description)throw new TypeError("Audio chunk metadata decoder configuration for Vorbis must include a description, which is expected to adhere to the format described in https://www.w3.org/TR/webcodecs-vorbis-codec-registration/.")}else if(n.decoderConfig.codec.startsWith("flac")){if(n.decoderConfig.codec!=="flac")throw new TypeError('Audio chunk metadata decoder configuration codec string for FLAC must be "flac".');if(!n.decoderConfig.description||n.decoderConfig.description.byteLength<42)throw new TypeError("Audio chunk metadata decoder configuration for FLAC must include a description, which is expected to adhere to the format described in https://www.w3.org/TR/webcodecs-flac-codec-registration/.")}else if(n.decoderConfig.codec.startsWith("ac-3")||n.decoderConfig.codec.startsWith("ac3")){if(n.decoderConfig.codec!=="ac-3")throw new TypeError('Audio chunk metadata decoder configuration codec string for AC-3 must be "ac-3".')}else if(n.decoderConfig.codec.startsWith("ec-3")||n.decoderConfig.codec.startsWith("eac3")){if(n.decoderConfig.codec!=="ec-3")throw new TypeError('Audio chunk metadata decoder configuration codec string for EC-3 must be "ec-3".')}else if((n.decoderConfig.codec.startsWith("pcm")||n.decoderConfig.codec.startsWith("ulaw")||n.decoderConfig.codec.startsWith("alaw"))&&!ee.includes(n.decoderConfig.codec))throw new TypeError(`Audio chunk metadata decoder configuration codec string for PCM must be one of the supported PCM codecs (${ee.join(", ")}).`)},Xn=n=>{if(!n)throw new TypeError("Subtitle metadata must be provided.");if(typeof n!="object")throw new TypeError("Subtitle metadata must be an object.");if(!n.config)throw new TypeError("Subtitle metadata must include a config object.");if(typeof n.config!="object")throw new TypeError("Subtitle metadata config must be an object.");if(typeof n.config.description!="string")throw new TypeError("Subtitle metadata config description must be a string.")};var dm=[44100,48e3,32e3],$n=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,32,40,48,56,64,80,96,112,128,160,192,224,256,320,-1,-1,32,48,56,64,80,96,112,128,160,192,224,256,320,384,-1,-1,32,64,96,128,160,192,224,256,288,320,352,384,416,448,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,8,16,24,32,40,48,56,64,80,96,112,128,144,160,-1,-1,8,16,24,32,40,48,56,64,80,96,112,128,144,160,-1,-1,32,48,56,64,80,96,112,128,144,160,176,192,224,256,-1],or=1483304551,ai=1231971951,Yn=(n,t,e,r,i)=>t===0?0:t===1?Math.floor(144*e/(r<<n))+i:t===2?Math.floor(144*e/r)+i:(Math.floor(12*e/r)+i)*4,Wu=(n,t,e,r)=>t===0?0:t===1?144*e/(r<<n):t===2?144*e/r:12*e/r*4,cr=(n,t)=>n===3?t===3?21:36:t===3?13:21,Br=(n,t)=>{let e=n>>>24,r=n>>>16&255,i=n>>>8&255,a=n&255;if(e!==255&&r!==255&&i!==255&&a!==255)return{header:null,bytesAdvanced:4};if(e!==255)return{header:null,bytesAdvanced:1};if((r&224)!==224)return{header:null,bytesAdvanced:1};let s=0,o=0;r&16?s=r&8?0:1:(s=1,o=1);let c=r>>3&3,u=r>>1&3,l=i>>4&15,m=(i>>2&3)%3,d=i>>1&1,f=a>>6&3,p=a>>4&3,b=a>>3&1,g=a>>2&1,y=a&3,k=$n[s*16*4+u*16+l];if(k===-1)return{header:null,bytesAdvanced:1};let w=k*1e3,T=dm[m]>>s+o,S=Yn(s,u,w,T,d);if(t!==null&&t<S)return{header:null,bytesAdvanced:1};let x;return c===3?x=u===3?384:1152:u===3?x=384:u===2?x=1152:x=576,{header:{totalSize:S,mpegVersionId:c,lowSamplingFrequency:s,layer:u,bitrate:w,frequencyIndex:m,sampleRate:T,channel:f,modeExtension:p,copyright:b,original:g,emphasis:y,audioSamplesInFrame:x},bytesAdvanced:1}},Hu=n=>{let t=127,e=0,r=n;for(;(t^2147483647)!==0;)e=r&~t,e<<=1,e|=r&t,t=(t+1<<8)-1,r=e;return e},Zn=n=>{let t=2130706432,e=0;for(;t!==0;)e>>=1,e|=n&t,t>>=8;return e};var Or=[48e3,44100,32e3],go=[24e3,22050,16e3];var ur=function*(n){let t=0,e=-1;for(;t<n.length-2;){let r=n.indexOf(0,t);if(r===-1||r>=n.length-2)break;t=r;let i=0;if(t+3<n.length&&n[t+1]===0&&n[t+2]===0&&n[t+3]===1?i=4:n[t+1]===0&&n[t+2]===1&&(i=3),i===0){t++;continue}e!==-1&&t>e&&(yield{offset:e,length:t-e}),e=t+i,t=e}e!==-1&&e<n.length&&(yield{offset:e,length:n.length-e})},Jn=function*(n,t){let e=0,r=new DataView(n.buffer,n.byteOffset,n.byteLength);for(;e+t<=n.length;){let i;t===1?i=r.getUint8(e):t===2?i=r.getUint16(e,!1):t===3?i=Ir(r,e,!1):(h(t===4),i=r.getUint32(e,!1)),e+=t,yield{offset:e,length:i},e+=i}},yo=(n,t)=>{if(t.description){let i=(j(t.description)[4]&3)+1;return Jn(n,i)}else return ur(n)},zt=n=>n&31,ea=n=>{let t=[],e=n.length;for(let r=0;r<e;r++)r+2<e&&n[r]===0&&n[r+1]===0&&n[r+2]===3?(t.push(0,0),r+=2):t.push(n[r]);return new Uint8Array(t)},bo=new Uint8Array([0,0,0,1]),en=n=>{let t=n.reduce((i,a)=>i+bo.byteLength+a.byteLength,0),e=new Uint8Array(t),r=0;for(let i of n)e.set(bo,r),r+=bo.byteLength,e.set(i,r),r+=i.byteLength;return e},ta=(n,t)=>{let e=n.reduce((a,s)=>a+t+s.byteLength,0),r=new Uint8Array(e),i=0;for(let a of n){let s=new DataView(r.buffer,r.byteOffset,r.byteLength);switch(t){case 1:s.setUint8(i,a.byteLength);break;case 2:s.setUint16(i,a.byteLength,!1);break;case 3:Er(s,i,a.byteLength,!1);break;case 4:s.setUint32(i,a.byteLength,!1);break}i+=t,r.set(a,i),i+=a.byteLength}return r},Ku=(n,t)=>{if(t.description){let i=(j(t.description)[4]&3)+1;return ta(n,i)}else return en(n)},si=n=>{try{let t=[],e=[],r=[];for(let o of ur(n)){let c=n.subarray(o.offset,o.offset+o.length),u=zt(c[0]);u===7?t.push(c):u===8?e.push(c):u===13&&r.push(c)}if(t.length===0||e.length===0)return null;let i=t[0],a=tn(i);h(a!==null);let s=a.profileIdc===100||a.profileIdc===110||a.profileIdc===122||a.profileIdc===144;return{configurationVersion:1,avcProfileIndication:a.profileIdc,profileCompatibility:a.constraintFlags,avcLevelIndication:a.levelIdc,lengthSizeMinusOne:3,sequenceParameterSets:t,pictureParameterSets:e,chromaFormat:s?a.chromaFormatIdc:null,bitDepthLumaMinus8:s?a.bitDepthLumaMinus8:null,bitDepthChromaMinus8:s?a.bitDepthChromaMinus8:null,sequenceParameterSetExt:s?r:null}}catch(t){return console.error("Error building AVC Decoder Configuration Record:",t),null}},Qu=n=>{let t=[];t.push(n.configurationVersion),t.push(n.avcProfileIndication),t.push(n.profileCompatibility),t.push(n.avcLevelIndication),t.push(252|n.lengthSizeMinusOne&3),t.push(224|n.sequenceParameterSets.length&31);for(let e of n.sequenceParameterSets){let r=e.byteLength;t.push(r>>8),t.push(r&255);for(let i=0;i<r;i++)t.push(e[i])}t.push(n.pictureParameterSets.length);for(let e of n.pictureParameterSets){let r=e.byteLength;t.push(r>>8),t.push(r&255);for(let i=0;i<r;i++)t.push(e[i])}if(n.avcProfileIndication===100||n.avcProfileIndication===110||n.avcProfileIndication===122||n.avcProfileIndication===144){h(n.chromaFormat!==null),h(n.bitDepthLumaMinus8!==null),h(n.bitDepthChromaMinus8!==null),h(n.sequenceParameterSetExt!==null),t.push(252|n.chromaFormat&3),t.push(248|n.bitDepthLumaMinus8&7),t.push(248|n.bitDepthChromaMinus8&7),t.push(n.sequenceParameterSetExt.length);for(let e of n.sequenceParameterSetExt){let r=e.byteLength;t.push(r>>8),t.push(r&255);for(let i=0;i<r;i++)t.push(e[i])}}return new Uint8Array(t)},ra=n=>{try{let t=V(n),e=0,r=t.getUint8(e++),i=t.getUint8(e++),a=t.getUint8(e++),s=t.getUint8(e++),o=t.getUint8(e++)&3,c=t.getUint8(e++)&31,u=[];for(let f=0;f<c;f++){let p=t.getUint16(e,!1);e+=2,u.push(n.subarray(e,e+p)),e+=p}let l=t.getUint8(e++),m=[];for(let f=0;f<l;f++){let p=t.getUint16(e,!1);e+=2,m.push(n.subarray(e,e+p)),e+=p}let d={configurationVersion:r,avcProfileIndication:i,profileCompatibility:a,avcLevelIndication:s,lengthSizeMinusOne:o,sequenceParameterSets:u,pictureParameterSets:m,chromaFormat:null,bitDepthLumaMinus8:null,bitDepthChromaMinus8:null,sequenceParameterSetExt:null};if((i===100||i===110||i===122||i===144)&&e+4<=n.length){let f=t.getUint8(e++)&3,p=t.getUint8(e++)&7,b=t.getUint8(e++)&7,g=t.getUint8(e++);d.chromaFormat=f,d.bitDepthLumaMinus8=p,d.bitDepthChromaMinus8=b;let y=[];for(let k=0;k<g;k++){let w=t.getUint16(e,!1);e+=2,y.push(n.subarray(e,e+w)),e+=w}d.sequenceParameterSetExt=y}return d}catch(t){return console.error("Error deserializing AVC Decoder Configuration Record:",t),null}},ju={1:{num:1,den:1},2:{num:12,den:11},3:{num:10,den:11},4:{num:16,den:11},5:{num:40,den:33},6:{num:24,den:11},7:{num:20,den:11},8:{num:32,den:11},9:{num:80,den:33},10:{num:18,den:11},11:{num:15,den:11},12:{num:64,den:33},13:{num:160,den:99},14:{num:4,den:3},15:{num:3,den:2},16:{num:2,den:1}},tn=n=>{try{let t=new W(ea(n));if(t.skipBits(1),t.skipBits(2),t.readBits(5)!==7)return null;let r=t.readAlignedByte(),i=t.readAlignedByte(),a=t.readAlignedByte();M(t);let s=1,o=0,c=0,u=0;if((r===100||r===110||r===122||r===244||r===44||r===83||r===86||r===118||r===128)&&(s=M(t),s===3&&(u=t.readBits(1)),o=M(t),c=M(t),t.skipBits(1),t.readBits(1))){for(let v=0;v<(s!==3?8:12);v++)if(t.readBits(1)){let D=v<6?16:64,U=8,z=8;for(let q=0;q<D;q++){if(z!==0){let ke=gt(t);z=(U+ke+256)%256}U=z===0?U:z}}}M(t);let l=M(t);if(l===0)M(t);else if(l===1){t.skipBits(1),gt(t),gt(t);let R=M(t);for(let v=0;v<R;v++)gt(t)}M(t),t.skipBits(1);let m=M(t),d=M(t),f=16*(m+1),p=16*(d+1),b=f,g=p,y=t.readBits(1);if(y||t.skipBits(1),t.skipBits(1),t.readBits(1)){let R=M(t),v=M(t),E=M(t),D=M(t),U,z;if((u===0?s:0)===0)U=1,z=2-y;else{let ke=s===3?1:2,Te=s===1?2:1;U=ke,z=Te*(2-y)}b-=U*(R+v),g-=z*(E+D)}let w=2,T=2,S=2,x=0,P={num:1,den:1},A=null,C=null;if(t.readBits(1)){if(t.readBits(1)){let Te=t.readBits(8);if(Te===255)P={num:t.readBits(16),den:t.readBits(16)};else{let Un=ju[Te];Un&&(P=Un)}}t.readBits(1)&&t.skipBits(1),t.readBits(1)&&(t.skipBits(3),x=t.readBits(1),t.readBits(1)&&(w=t.readBits(8),T=t.readBits(8),S=t.readBits(8))),t.readBits(1)&&(M(t),M(t)),t.readBits(1)&&(t.skipBits(32),t.skipBits(32),t.skipBits(1));let z=t.readBits(1);z&&qu(t);let q=t.readBits(1);q&&qu(t),(z||q)&&t.skipBits(1),t.skipBits(1),t.readBits(1)&&(t.skipBits(1),M(t),M(t),M(t),M(t),A=M(t),C=M(t))}if(A===null){h(C===null);let R=i&16;if((r===44||r===86||r===100||r===110||r===122||r===244)&&R)A=0,C=0;else{let v=m+1,E=d+1,D=(2-y)*E,U=Zi.find(q=>q.level>=a)??X(Zi),z=Math.min(Math.floor(U.maxDpbMbs/(v*D)),16);A=z,C=z}}return h(C!==null),{profileIdc:r,constraintFlags:i,levelIdc:a,frameMbsOnlyFlag:y,chromaFormatIdc:s,bitDepthLumaMinus8:o,bitDepthChromaMinus8:c,codedWidth:f,codedHeight:p,displayWidth:b,displayHeight:g,pixelAspectRatio:P,colourPrimaries:w,matrixCoefficients:S,transferCharacteristics:T,fullRangeFlag:x,numReorderFrames:A,maxDecFrameBuffering:C}}catch(t){return console.error("Error parsing AVC SPS:",t),null}},qu=n=>{let t=M(n);n.skipBits(4),n.skipBits(4);for(let e=0;e<=t;e++)M(n),M(n),n.skipBits(1);n.skipBits(5),n.skipBits(5),n.skipBits(5),n.skipBits(5)},mm=(n,t)=>{if(t.description){let i=(j(t.description)[21]&3)+1;return ta(n,i)}else return en(n)},Ji=(n,t)=>{if(t.description){let i=(j(t.description)[21]&3)+1;return Jn(n,i)}else return ur(n)},at=n=>n>>1&63,ko=n=>{try{let t=new W(ea(n));t.skipBits(16),t.readBits(4);let e=t.readBits(3),r=t.readBits(1),{general_profile_space:i,general_tier_flag:a,general_profile_idc:s,general_profile_compatibility_flags:o,general_constraint_indicator_flags:c,general_level_idc:u}=fm(t,e);M(t);let l=M(t),m=0;l===3&&(m=t.readBits(1));let d=M(t),f=M(t),p=d,b=f;if(t.readBits(1)){let v=M(t),E=M(t),D=M(t),U=M(t),z=1,q=1,ke=m===0?l:0;ke===1?(z=2,q=2):ke===2&&(z=2,q=1),p-=(v+E)*z,b-=(D+U)*q}let g=M(t),y=M(t);M(t);let w=t.readBits(1)?0:e,T=0;for(let v=w;v<=e;v++)M(t),T=M(t),M(t);M(t),M(t),M(t),M(t),M(t),M(t),t.readBits(1)&&t.readBits(1)&&pm(t),t.skipBits(1),t.skipBits(1),t.readBits(1)&&(t.skipBits(4),t.skipBits(4),M(t),M(t),t.skipBits(1));let S=M(t);if(hm(t,S),t.readBits(1)){let v=M(t);for(let E=0;E<v;E++)M(t),t.skipBits(1)}t.skipBits(1),t.skipBits(1);let x=2,P=2,A=2,C=0,I=0,R={num:1,den:1};if(t.readBits(1)){let v=bm(t,e);R=v.pixelAspectRatio,x=v.colourPrimaries,P=v.transferCharacteristics,A=v.matrixCoefficients,C=v.fullRangeFlag,I=v.minSpatialSegmentationIdc}return{displayWidth:p,displayHeight:b,pixelAspectRatio:R,colourPrimaries:x,transferCharacteristics:P,matrixCoefficients:A,fullRangeFlag:C,maxDecFrameBuffering:T+1,spsMaxSubLayersMinus1:e,spsTemporalIdNestingFlag:r,generalProfileSpace:i,generalTierFlag:a,generalProfileIdc:s,generalProfileCompatibilityFlags:o,generalConstraintIndicatorFlags:c,generalLevelIdc:u,chromaFormatIdc:l,bitDepthLumaMinus8:g,bitDepthChromaMinus8:y,minSpatialSegmentationIdc:I}}catch(t){return console.error("Error parsing HEVC SPS:",t),null}},oi=n=>{try{let t=[],e=[],r=[],i=[];for(let u of ur(n)){let l=n.subarray(u.offset,u.offset+u.length),m=at(l[0]);m===32?t.push(l):m===33?e.push(l):m===34?r.push(l):(m===39||m===40)&&i.push(l)}if(e.length===0||r.length===0)return null;let a=ko(e[0]);if(!a)return null;let s=0;if(r.length>0){let u=r[0],l=new W(ea(u));l.skipBits(16),M(l),M(l),l.skipBits(1),l.skipBits(1),l.skipBits(3),l.skipBits(1),l.skipBits(1),M(l),M(l),gt(l),l.skipBits(1),l.skipBits(1),l.readBits(1)&&M(l),gt(l),gt(l),l.skipBits(1),l.skipBits(1),l.skipBits(1),l.skipBits(1);let m=l.readBits(1),d=l.readBits(1);!m&&!d?s=0:m&&!d?s=2:!m&&d?s=3:s=0}let o=[...t.length?[{arrayCompleteness:1,nalUnitType:32,nalUnits:t}]:[],...e.length?[{arrayCompleteness:1,nalUnitType:33,nalUnits:e}]:[],...r.length?[{arrayCompleteness:1,nalUnitType:34,nalUnits:r}]:[],...i.length?[{arrayCompleteness:1,nalUnitType:at(i[0][0]),nalUnits:i}]:[]];return{configurationVersion:1,generalProfileSpace:a.generalProfileSpace,generalTierFlag:a.generalTierFlag,generalProfileIdc:a.generalProfileIdc,generalProfileCompatibilityFlags:a.generalProfileCompatibilityFlags,generalConstraintIndicatorFlags:a.generalConstraintIndicatorFlags,generalLevelIdc:a.generalLevelIdc,minSpatialSegmentationIdc:a.minSpatialSegmentationIdc,parallelismType:s,chromaFormatIdc:a.chromaFormatIdc,bitDepthLumaMinus8:a.bitDepthLumaMinus8,bitDepthChromaMinus8:a.bitDepthChromaMinus8,avgFrameRate:0,constantFrameRate:0,numTemporalLayers:a.spsMaxSubLayersMinus1+1,temporalIdNested:a.spsTemporalIdNestingFlag,lengthSizeMinusOne:3,arrays:o}}catch(t){return console.error("Error building HEVC Decoder Configuration Record:",t),null}},fm=(n,t)=>{let e=n.readBits(2),r=n.readBits(1),i=n.readBits(5),a=0;for(let l=0;l<32;l++)a=a<<1|n.readBits(1);let s=new Uint8Array(6);for(let l=0;l<6;l++)s[l]=n.readBits(8);let o=n.readBits(8),c=[],u=[];for(let l=0;l<t;l++)c.push(n.readBits(1)),u.push(n.readBits(1));if(t>0)for(let l=t;l<8;l++)n.skipBits(2);for(let l=0;l<t;l++)c[l]&&n.skipBits(88),u[l]&&n.skipBits(8);return{general_profile_space:e,general_tier_flag:r,general_profile_idc:i,general_profile_compatibility_flags:a,general_constraint_indicator_flags:s,general_level_idc:o}},pm=n=>{for(let t=0;t<4;t++)for(let e=0;e<(t===3?2:6);e++)if(!n.readBits(1))M(n);else{let i=Math.min(64,1<<4+(t<<1));t>1&&gt(n);for(let a=0;a<i;a++)gt(n)}},hm=(n,t)=>{let e=[];for(let r=0;r<t;r++)e[r]=gm(n,r,t,e)},gm=(n,t,e,r)=>{let i=0,a=0,s=0;if(t!==0&&(a=n.readBits(1)),a){if(t===e){let c=M(n);s=t-(c+1)}else s=t-1;n.readBits(1),M(n);let o=r[s]??0;for(let c=0;c<=o;c++)n.readBits(1)||n.readBits(1);i=r[s]}else{let o=M(n),c=M(n);for(let u=0;u<o;u++)M(n),n.readBits(1);for(let u=0;u<c;u++)M(n),n.readBits(1);i=o+c}return i},bm=(n,t)=>{let e=2,r=2,i=2,a=0,s=0,o={num:1,den:1};if(n.readBits(1)){let c=n.readBits(8);if(c===255)o={num:n.readBits(16),den:n.readBits(16)};else{let u=ju[c];u&&(o=u)}}return n.readBits(1)&&n.readBits(1),n.readBits(1)&&(n.readBits(3),a=n.readBits(1),n.readBits(1)&&(e=n.readBits(8),r=n.readBits(8),i=n.readBits(8))),n.readBits(1)&&(M(n),M(n)),n.readBits(1),n.readBits(1),n.readBits(1),n.readBits(1)&&(M(n),M(n),M(n),M(n)),n.readBits(1)&&(n.readBits(32),n.readBits(32),n.readBits(1)&&M(n),n.readBits(1)&&ym(n,!0,t)),n.readBits(1)&&(n.readBits(1),n.readBits(1),n.readBits(1),s=M(n),M(n),M(n),M(n),M(n)),{pixelAspectRatio:o,colourPrimaries:e,transferCharacteristics:r,matrixCoefficients:i,fullRangeFlag:a,minSpatialSegmentationIdc:s}},ym=(n,t,e)=>{let r=!1,i=!1,a=!1;t&&(r=n.readBits(1)===1,i=n.readBits(1)===1,(r||i)&&(a=n.readBits(1)===1,a&&(n.readBits(8),n.readBits(5),n.readBits(1),n.readBits(5)),n.readBits(4),n.readBits(4),a&&n.readBits(4),n.readBits(5),n.readBits(5),n.readBits(5)));for(let s=0;s<=e;s++){let o=n.readBits(1)===1,c=!0;o||(c=n.readBits(1)===1);let u=!1;c?M(n):u=n.readBits(1)===1;let l=1;u||(l=M(n)+1),r&&Gu(n,l,a),i&&Gu(n,l,a)}},Gu=(n,t,e)=>{for(let r=0;r<t;r++)M(n),M(n),e&&(M(n),M(n)),n.readBits(1)},Xu=n=>{let t=[];t.push(n.configurationVersion),t.push((n.generalProfileSpace&3)<<6|(n.generalTierFlag&1)<<5|n.generalProfileIdc&31),t.push(n.generalProfileCompatibilityFlags>>>24&255),t.push(n.generalProfileCompatibilityFlags>>>16&255),t.push(n.generalProfileCompatibilityFlags>>>8&255),t.push(n.generalProfileCompatibilityFlags&255),t.push(...n.generalConstraintIndicatorFlags),t.push(n.generalLevelIdc&255),t.push(240|n.minSpatialSegmentationIdc>>8&15),t.push(n.minSpatialSegmentationIdc&255),t.push(252|n.parallelismType&3),t.push(252|n.chromaFormatIdc&3),t.push(248|n.bitDepthLumaMinus8&7),t.push(248|n.bitDepthChromaMinus8&7),t.push(n.avgFrameRate>>8&255),t.push(n.avgFrameRate&255),t.push((n.constantFrameRate&3)<<6|(n.numTemporalLayers&7)<<3|(n.temporalIdNested&1)<<2|n.lengthSizeMinusOne&3),t.push(n.arrays.length&255);for(let e of n.arrays){t.push((e.arrayCompleteness&1)<<7|0|e.nalUnitType&63),t.push(e.nalUnits.length>>8&255),t.push(e.nalUnits.length&255);for(let r of e.nalUnits){t.push(r.length>>8&255),t.push(r.length&255);for(let i=0;i<r.length;i++)t.push(r[i])}}return new Uint8Array(t)},$u=n=>{try{let t=V(n),e=0,r=t.getUint8(e++),i=t.getUint8(e++),a=i>>6&3,s=i>>5&1,o=i&31,c=t.getUint32(e,!1);e+=4;let u=n.subarray(e,e+6);e+=6;let l=t.getUint8(e++),m=(t.getUint8(e++)&15)<<8|t.getUint8(e++),d=t.getUint8(e++)&3,f=t.getUint8(e++)&3,p=t.getUint8(e++)&7,b=t.getUint8(e++)&7,g=t.getUint16(e,!1);e+=2;let y=t.getUint8(e++),k=y>>6&3,w=y>>3&7,T=y>>2&1,S=y&3,x=t.getUint8(e++),P=[];for(let A=0;A<x;A++){let C=t.getUint8(e++),I=C>>7&1,R=C&63,v=t.getUint16(e,!1);e+=2;let E=[];for(let D=0;D<v;D++){let U=t.getUint16(e,!1);e+=2,E.push(n.subarray(e,e+U)),e+=U}P.push({arrayCompleteness:I,nalUnitType:R,nalUnits:E})}return{configurationVersion:r,generalProfileSpace:a,generalTierFlag:s,generalProfileIdc:o,generalProfileCompatibilityFlags:c,generalConstraintIndicatorFlags:u,generalLevelIdc:l,minSpatialSegmentationIdc:m,parallelismType:d,chromaFormatIdc:f,bitDepthLumaMinus8:p,bitDepthChromaMinus8:b,avgFrameRate:g,constantFrameRate:k,numTemporalLayers:w,temporalIdNested:T,lengthSizeMinusOne:S,arrays:P}}catch(t){return console.error("Error deserializing HEVC Decoder Configuration Record:",t),null}};var Yu=(n,t)=>{let e=new Set,r=0;for(let a of Ji(n,t)){if(r===4){e.add(a.offset);continue}let s=at(n[a.offset]);if(r===3&&s!==37){e.add(a.offset);continue}let o=!1;s===35?r>0?o=!0:r=1:s<=31?r>2?o=!0:r=2:s===36?r!==2?o=!0:r=3:s===37?r<2?o=!0:r=4:s===32||s===33||s===34||s===39||s>=41&&s<=44||s>=48&&s<=55?r>1?o=!0:r=1:(s===38||s===40||s>=45&&s<=47||s>=56&&s<=63)&&r<2&&(o=!0),o&&e.add(a.offset)}if(e.size===0)return null;let i=[];for(let a of Ji(n,t))e.has(a.offset)||i.push(n.subarray(a.offset,a.offset+a.length));return mm(i,t)},ia=n=>{let t=new W(n);if(t.readBits(2)!==2)return null;let r=t.readBits(1),a=(t.readBits(1)<<1)+r;if(a===3&&t.skipBits(1),t.readBits(1)===1||t.readBits(1)!==0||(t.skipBits(2),t.readBits(24)!==4817730))return null;let u=8;a>=2&&(u=t.readBits(1)?12:10);let l=t.readBits(3),m=0,d=0;if(l!==7)if(d=t.readBits(1),a===1||a===3){let P=t.readBits(1),A=t.readBits(1);m=!P&&!A?3:P&&!A?2:1,t.skipBits(1)}else m=1;else m=3,d=1;let f=t.readBits(16),p=t.readBits(16),b=f+1,g=p+1,y=b*g,k=X(Ut).level;for(let x of Ut)if(y<=x.maxPictureSize){k=x.level;break}return{profile:a,level:k,bitDepth:u,chromaSubsampling:m,videoFullRangeFlag:d,colourPrimaries:l===2?1:l===1?6:2,transferCharacteristics:l===2?1:l===1?6:2,matrixCoefficients:l===7?0:l===2?1:l===1?6:2}},Zu=function*(n){let t=new W(n),e=()=>{let r=0;for(let i=0;i<8;i++){let a=t.readAlignedByte();if(r|=(a&127)<<i*7,!(a&128))break;if(i===7&&a&128)return null}return r>=2**32-1?null:r};for(;t.getBitsLeft()>=8;){t.skipBits(1);let r=t.readBits(4),i=t.readBits(1),a=t.readBits(1);t.skipBits(1),i&&t.skipBits(8);let s;if(a){let o=e();if(o===null)return;s=o}else s=Math.floor(t.getBitsLeft()/8);h(t.pos%8===0),yield{type:r,data:n.subarray(t.pos/8,t.pos/8+s)},t.skipBits(s*8)}},na=n=>{for(let{type:t,data:e}of Zu(n)){if(t!==1)continue;let r=new W(e),i=r.readBits(3),a=r.readBits(1),s=r.readBits(1),o=0,c=0,u=0;if(s)o=r.readBits(5);else{if(r.readBits(1)&&(r.skipBits(32),r.skipBits(32),r.readBits(1)))return null;let x=r.readBits(1);x&&(u=r.readBits(5),r.skipBits(32),r.skipBits(5),r.skipBits(5));let P=r.readBits(5);for(let A=0;A<=P;A++){r.skipBits(12);let C=r.readBits(5);if(A===0&&(o=C),C>7){let R=r.readBits(1);A===0&&(c=R)}if(x&&r.readBits(1)){let v=u+1;r.skipBits(v),r.skipBits(v),r.skipBits(1)}r.readBits(1)&&r.skipBits(4)}}let l=r.readBits(4),m=r.readBits(4),d=l+1;r.skipBits(d);let f=m+1;r.skipBits(f);let p=0;if(s?p=0:p=r.readBits(1),p&&(r.skipBits(4),r.skipBits(3)),r.skipBits(1),r.skipBits(1),r.skipBits(1),!s){r.skipBits(1),r.skipBits(1),r.skipBits(1),r.skipBits(1);let S=r.readBits(1);S&&(r.skipBits(1),r.skipBits(1));let x=r.readBits(1),P=0;x?P=2:P=r.readBits(1),P>0&&(r.readBits(1)||r.skipBits(1)),S&&r.skipBits(3)}r.skipBits(1),r.skipBits(1),r.skipBits(1);let b=r.readBits(1),g=8;i===2&&b?g=r.readBits(1)?12:10:i<=2&&(g=b?10:8);let y=0;i!==1&&(y=r.readBits(1));let k=1,w=1,T=0;return y||(i===0?(k=1,w=1):i===1?(k=0,w=0):g===12&&(k=r.readBits(1),k&&(w=r.readBits(1))),k&&w&&(T=r.readBits(2))),{profile:i,level:o,tier:c,bitDepth:g,monochrome:y,chromaSubsamplingX:k,chromaSubsamplingY:w,chromaSamplePosition:T}}return null},lr=n=>{let t=V(n),e=t.getUint8(9),r=t.getUint16(10,!0),i=t.getUint32(12,!0),a=t.getInt16(16,!0),s=t.getUint8(18),o=null;return s&&(o=n.subarray(19,21+e)),{outputChannelCount:e,preSkip:r,inputSampleRate:i,outputGain:a,channelMappingFamily:s,channelMappingTable:o}},km=[480,960,1920,2880,480,960,1920,2880,480,960,1920,2880,480,960,480,960,120,240,480,960,120,240,480,960,120,240,480,960,120,240,480,960],Ju=n=>{let t=n[0]>>3;return{durationInSamples:km[t]}},aa=n=>{if(n.length<7)throw new Error("Setup header is too short.");if(n[0]!==5)throw new Error("Wrong packet type in Setup header.");if(String.fromCharCode(...n.slice(1,7))!=="vorbis")throw new Error("Invalid packet signature in Setup header.");let e=n.length,r=new Uint8Array(e);for(let m=0;m<e;m++)r[m]=n[e-1-m];let i=new W(r),a=0;for(;i.getBitsLeft()>97;)if(i.readBits(1)===1){a=i.pos;break}if(a===0)throw new Error("Invalid Setup header: framing bit not found.");let s=0,o=!1,c=0;for(;i.getBitsLeft()>=97;){let m=i.pos,d=i.readBits(8),f=i.readBits(16),p=i.readBits(16);if(d>63||f!==0||p!==0){i.pos=m;break}if(i.skipBits(1),s++,s>64)break;i.clone().readBits(6)+1===s&&(o=!0,c=s)}if(!o)throw new Error("Invalid Setup header: mode header not found.");if(c>63)throw new Error(`Unsupported mode count: ${c}.`);let u=c;i.pos=0,i.skipBits(a);let l=Array(u).fill(0);for(let m=u-1;m>=0;m--)i.skipBits(40),l[m]=i.readBits(1);return{modeBlockflags:l}},dr=(n,t,e)=>{switch(n){case"avc":{for(let r of yo(e,t)){let i=e[r.offset],a=zt(i);if(a>=1&&a<=4)return"delta";if(a===5)return"key";if(a===6&&(!Xi()||Au()>=144)){let s=e.subarray(r.offset,r.offset+r.length),o=ea(s),c=1;do{let u=0;for(;;){let d=o[c++];if(d===void 0||(u+=d,d<255))break}let l=0;for(;;){let d=o[c++];if(d===void 0||(l+=d,d<255))break}if(u===6){let d=new W(o);d.pos=8*c;let f=M(d),p=d.readBits(1);if(f===0&&p===1)return"key"}c+=l}while(c<o.length-1)}}return"delta"}case"hevc":{for(let r of Ji(e,t)){let i=at(e[r.offset]);if(i<16)return"delta";if(i<=23)return"key"}return"delta"}case"vp8":return(e[0]&1)===0?"key":"delta";case"vp9":{let r=new W(e);if(r.readBits(2)!==2)return null;let i=r.readBits(1);return(r.readBits(1)<<1)+i===3&&r.skipBits(1),r.readBits(1)?null:r.readBits(1)===0?"key":"delta"}case"av1":{let r=!1;for(let{type:i,data:a}of Zu(e))if(i===1){let s=new W(a);s.skipBits(4),r=!!s.readBits(1)}else if(i===3||i===6||i===7){if(r)return"key";let s=new W(a);return s.readBits(1)?null:s.readBits(2)===0?"key":"delta"}return null}default:ne(n),h(!1)}};var rn=(n,t)=>{let e=V(n),r=0,i=e.getUint32(r,!0);r+=4;let a=me.decode(n.subarray(r,r+i));r+=i,i>0&&(t.raw??={},t.raw.vendor??=a);let s=e.getUint32(r,!0);r+=4;for(let o=0;o<s;o++){let c=e.getUint32(r,!0);r+=4;let u=me.decode(n.subarray(r,r+c));r+=c;let l=u.indexOf("=");if(l===-1)continue;let m=u.slice(0,l).toUpperCase(),d=u.slice(l+1);switch(t.raw??={},t.raw[m]??=d,m){case"TITLE":t.title??=d;break;case"DESCRIPTION":t.description??=d;break;case"ARTIST":t.artist??=d;break;case"ALBUM":t.album??=d;break;case"ALBUMARTIST":t.albumArtist??=d;break;case"COMMENT":t.comment??=d;break;case"LYRICS":t.lyrics??=d;break;case"TRACKNUMBER":{let f=d.split("/"),p=Number.parseInt(f[0],10),b=f[1]&&Number.parseInt(f[1],10);Number.isInteger(p)&&p>0&&(t.trackNumber??=p),b&&Number.isInteger(b)&&b>0&&(t.tracksTotal??=b)}break;case"TRACKTOTAL":{let f=Number.parseInt(d,10);Number.isInteger(f)&&f>0&&(t.tracksTotal??=f)}break;case"DISCNUMBER":{let f=d.split("/"),p=Number.parseInt(f[0],10),b=f[1]&&Number.parseInt(f[1],10);Number.isInteger(p)&&p>0&&(t.discNumber??=p),b&&Number.isInteger(b)&&b>0&&(t.discsTotal??=b)}break;case"DISCTOTAL":{let f=Number.parseInt(d,10);Number.isInteger(f)&&f>0&&(t.discsTotal??=f)}break;case"DATE":{let f=new Date(d);Number.isNaN(f.getTime())||(t.date??=f)}break;case"GENRE":t.genre??=d;break;case"METADATA_BLOCK_PICTURE":{let f=Rr(d),p=V(f),b=p.getUint32(0,!1),g=p.getUint32(4,!1),y=String.fromCharCode(...f.subarray(8,8+g)),k=p.getUint32(8+g,!1),w=me.decode(f.subarray(12+g,12+g+k)),T=p.getUint32(g+k+28),S=f.subarray(g+k+32,g+k+32+T);t.images??=[],t.images.push({data:S,mimeType:y,kind:b===3?"coverFront":b===4?"coverBack":"unknown",name:void 0,description:w||void 0})}break}}},nn=(n,t,e)=>{let r=[n],a=$.encode("Mediabunny"),s=new Uint8Array(4+a.length),o=new DataView(s.buffer);o.setUint32(0,a.length,!0),s.set(a,4),r.push(s);let c=new Set,u=(p,b)=>{let g=`${p}=${b}`,y=$.encode(g);s=new Uint8Array(4+y.length),o=new DataView(s.buffer),o.setUint32(0,y.length,!0),s.set(y,4),r.push(s),c.add(p)};for(let{key:p,value:b}of it(t))switch(p){case"title":u("TITLE",b);break;case"description":u("DESCRIPTION",b);break;case"artist":u("ARTIST",b);break;case"album":u("ALBUM",b);break;case"albumArtist":u("ALBUMARTIST",b);break;case"genre":u("GENRE",b);break;case"date":{let g=t.raw?.DATE??t.raw?.date;g&&typeof g=="string"?u("DATE",g):u("DATE",b.toISOString().slice(0,10))}break;case"comment":u("COMMENT",b);break;case"lyrics":u("LYRICS",b);break;case"trackNumber":u("TRACKNUMBER",b.toString());break;case"tracksTotal":u("TRACKTOTAL",b.toString());break;case"discNumber":u("DISCNUMBER",b.toString());break;case"discsTotal":u("DISCTOTAL",b.toString());break;case"images":{if(!e)break;for(let g of b){let y=g.kind==="coverFront"?3:g.kind==="coverBack"?4:0,k=new Uint8Array(g.mimeType.length);for(let P=0;P<g.mimeType.length;P++)k[P]=g.mimeType.charCodeAt(P);let w=$.encode(g.description??""),T=new Uint8Array(8+k.length+4+w.length+16+4+g.data.length),S=V(T);S.setUint32(0,y,!1),S.setUint32(4,k.length,!1),T.set(k,8),S.setUint32(8+k.length,w.length,!1),T.set(w,12+k.length),S.setUint32(28+k.length+w.length,g.data.length,!1),T.set(g.data,32+k.length+w.length);let x=Pu(T);u("METADATA_BLOCK_PICTURE",x)}}break;case"raw":break;default:ne(p)}if(t.raw)for(let p in t.raw){let b=t.raw[p]??t.raw[p.toLowerCase()];p==="vendor"||b==null||c.has(p)||typeof b=="string"&&u(p,b)}let l=new Uint8Array(4);V(l).setUint32(0,c.size,!0),r.splice(2,0,l);let m=r.reduce((p,b)=>p+b.length,0),d=new Uint8Array(m),f=0;for(let p of r)d.set(p,f),f+=p.length;return d},an=[2,1,2,3,3,4,4,5],sa=n=>{if(n.length<7||n[0]!==11||n[1]!==119)return null;let t=new W(n);t.skipBits(16),t.skipBits(16);let e=t.readBits(2);if(e===3)return null;let r=t.readBits(6),i=t.readBits(5);if(i>8)return null;let a=t.readBits(3),s=t.readBits(3);(s&1)!==0&&s!==1&&t.skipBits(2),(s&4)!==0&&t.skipBits(2),s===2&&t.skipBits(2);let o=t.readBits(1),c=Math.floor(r/2);return{fscod:e,bsid:i,bsmod:a,acmod:s,lfeon:o,bitRateCode:c}},el=[64*2,69*2,96*2,64*2,70*2,96*2,80*2,87*2,120*2,80*2,88*2,120*2,96*2,104*2,144*2,96*2,105*2,144*2,112*2,121*2,168*2,112*2,122*2,168*2,128*2,139*2,192*2,128*2,140*2,192*2,160*2,174*2,240*2,160*2,175*2,240*2,192*2,208*2,288*2,192*2,209*2,288*2,224*2,243*2,336*2,224*2,244*2,336*2,256*2,278*2,384*2,256*2,279*2,384*2,320*2,348*2,480*2,320*2,349*2,480*2,384*2,417*2,576*2,384*2,418*2,576*2,448*2,487*2,672*2,448*2,488*2,672*2,512*2,557*2,768*2,512*2,558*2,768*2,640*2,696*2,960*2,640*2,697*2,960*2,768*2,835*2,1152*2,768*2,836*2,1152*2,896*2,975*2,1344*2,896*2,976*2,1344*2,1024*2,1114*2,1536*2,1024*2,1115*2,1536*2,1152*2,1253*2,1728*2,1152*2,1254*2,1728*2,1280*2,1393*2,1920*2,1280*2,1394*2,1920*2],tl=1536,sn=new Uint8Array([5,4,65,67,45,51]),on=new Uint8Array([5,4,69,65,67,51]),To=[1,2,3,6],oa=n=>{if(n.length<6||n[0]!==11||n[1]!==119)return null;let t=new W(n);t.skipBits(16);let e=t.readBits(2);if(t.skipBits(3),e!==0&&e!==2)return null;let r=t.readBits(11),i=t.readBits(2),a=0,s;i===3?(a=t.readBits(2),s=3):s=t.readBits(2);let o=t.readBits(3),c=t.readBits(1),u=t.readBits(5);if(u<11||u>16)return null;let l=To[s],m;return i<3?m=Or[i]/1e3:m=go[a]/1e3,{dataRate:Math.round((r+1)*m/(l*16)),substreams:[{fscod:i,fscod2:a,bsid:u,bsmod:0,acmod:o,lfeon:c,numDepSub:0,chanLoc:0}]}},rl=n=>{if(n.length<2)return null;let t=new W(n),e=t.readBits(13),r=t.readBits(3),i=[];for(let a=0;a<=r&&!(Math.ceil(t.pos/8)+3>n.length);a++){let s=t.readBits(2),o=t.readBits(5);t.skipBits(1),t.skipBits(1);let c=t.readBits(3),u=t.readBits(3),l=t.readBits(1);t.skipBits(3);let m=t.readBits(4),d=0;m>0?d=t.readBits(9):t.skipBits(1),i.push({fscod:s,fscod2:null,bsid:o,bsmod:c,acmod:u,lfeon:l,numDepSub:m,chanLoc:d})}return i.length===0?null:{dataRate:e,substreams:i}},ca=n=>{let t=n.substreams[0];return h(t),t.fscod<3?Or[t.fscod]:t.fscod2!==null&&t.fscod2<3?go[t.fscod2]:null},ua=n=>{let t=n.substreams[0];h(t);let e=an[t.acmod]+t.lfeon;if(t.numDepSub>0){let r=[2,2,1,1,2,2,2,1,1];for(let i=0;i<9;i++)t.chanLoc&1<<8-i&&(e+=r[i])}return e};var pe=class{constructor(t){this.input=t}dispose(){}};var de=new Uint8Array(0),Q=class n{constructor(t,e,r,i,a=-1,s,o){this.data=t;this.type=e;this.timestamp=r;this.duration=i;this.sequenceNumber=a;if(t===de&&s===void 0)throw new Error("Internal error: byteLength must be explicitly provided when constructing metadata-only packets.");if(s===void 0&&(s=t.byteLength),!(t instanceof Uint8Array))throw new TypeError("data must be a Uint8Array.");if(e!=="key"&&e!=="delta")throw new TypeError('type must be either "key" or "delta".');if(!Number.isFinite(r))throw new TypeError("timestamp must be a number.");if(!Number.isFinite(i)||i<0)throw new TypeError("duration must be a non-negative number.");if(!Number.isFinite(a))throw new TypeError("sequenceNumber must be a number.");if(!Number.isInteger(s)||s<0)throw new TypeError("byteLength must be a non-negative integer.");if(o!==void 0&&(typeof o!="object"||!o))throw new TypeError("sideData, when provided, must be an object.");if(o?.alpha!==void 0&&!(o.alpha instanceof Uint8Array))throw new TypeError("sideData.alpha, when provided, must be a Uint8Array.");if(o?.alphaByteLength!==void 0&&(!Number.isInteger(o.alphaByteLength)||o.alphaByteLength<0))throw new TypeError("sideData.alphaByteLength, when provided, must be a non-negative integer.");this.byteLength=s,this.sideData=o??{},this.sideData.alpha&&this.sideData.alphaByteLength===void 0&&(this.sideData.alphaByteLength=this.sideData.alpha.byteLength)}get isMetadataOnly(){return this.data===de}get microsecondTimestamp(){return Math.trunc(yt*this.timestamp)}get microsecondDuration(){return Math.trunc(yt*this.duration)}toEncodedVideoChunk(){if(this.isMetadataOnly)throw new TypeError("Metadata-only packets cannot be converted to a video chunk.");if(typeof EncodedVideoChunk>"u")throw new Error("Your browser does not support EncodedVideoChunk.");return new EncodedVideoChunk({data:this.data,type:this.type,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration})}alphaToEncodedVideoChunk(t=this.type){if(!this.sideData.alpha)throw new TypeError("This packet does not contain alpha side data.");if(this.isMetadataOnly)throw new TypeError("Metadata-only packets cannot be converted to a video chunk.");if(typeof EncodedVideoChunk>"u")throw new Error("Your browser does not support EncodedVideoChunk.");return new EncodedVideoChunk({data:this.sideData.alpha,type:t,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration})}toEncodedAudioChunk(){if(this.isMetadataOnly)throw new TypeError("Metadata-only packets cannot be converted to an audio chunk.");if(typeof EncodedAudioChunk>"u")throw new Error("Your browser does not support EncodedAudioChunk.");return new EncodedAudioChunk({data:this.data,type:this.type,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration})}static fromEncodedChunk(t,e){if(!(t instanceof EncodedVideoChunk||t instanceof EncodedAudioChunk))throw new TypeError("chunk must be an EncodedVideoChunk or EncodedAudioChunk.");let r=new Uint8Array(t.byteLength);return t.copyTo(r),new n(r,t.type,t.timestamp/1e6,(t.duration??0)/1e6,void 0,void 0,e)}clone(t){if(t!==void 0&&(typeof t!="object"||t===null))throw new TypeError("options, when provided, must be an object.");if(t?.data!==void 0&&!(t.data instanceof Uint8Array))throw new TypeError("options.data, when provided, must be a Uint8Array.");if(t?.type!==void 0&&t.type!=="key"&&t.type!=="delta")throw new TypeError('options.type, when provided, must be either "key" or "delta".');if(t?.timestamp!==void 0&&!Number.isFinite(t.timestamp))throw new TypeError("options.timestamp, when provided, must be a number.");if(t?.duration!==void 0&&!Number.isFinite(t.duration))throw new TypeError("options.duration, when provided, must be a number.");if(t?.sequenceNumber!==void 0&&!Number.isFinite(t.sequenceNumber))throw new TypeError("options.sequenceNumber, when provided, must be a number.");if(t?.sideData!==void 0&&(typeof t.sideData!="object"||t.sideData===null))throw new TypeError("options.sideData, when provided, must be an object.");return new n(t?.data??this.data,t?.type??this.type,t?.timestamp??this.timestamp,t?.duration??this.duration,t?.sequenceNumber??this.sequenceNumber,this.byteLength,t?.sideData??this.sideData)}};var la=n=>{let e=(n.hasVideo?"video/":n.hasAudio?"audio/":"application/")+(n.isQuickTime?"quicktime":"mp4");if(n.codecStrings.length>0){let r=[...new Set(n.codecStrings)];e+=`; codecs="${r.join(", ")}"`}return e},da=n=>{let t=V(n),e=0,r=t.getUint8(e);e+=1,e+=3;let i=tr(n.subarray(e,e+16));e+=16;let a=null;if(r>0){let o=t.getUint32(e);if(e+=4,o>0){a=[];for(let c=0;c<o;c++)a.push(tr(n.subarray(e,e+16))),e+=16}}let s=t.getUint32(e);return e+=4,{systemId:i,keyIds:a,data:n.slice(e,e+s)}},ma=(n,t)=>n.systemId===t.systemId&&Wn(n.data,t.data);var st=8,Nt=16,Lt=n=>{let t=F(n),e=te(n,4),r=8;t===1&&(t=Be(n),r=16);let a=t-r;return a<0?null:{name:e,totalSize:t,headerSize:r,contentSize:a}},mr=n=>Wt(n)/65536,fa=n=>Wt(n)/1073741824,pa=n=>{let t=0;for(let e=0;e<4;e++){t<<=7;let r=O(n);if(t|=r&127,(r&128)===0)break}return t},ot=n=>{let t=se(n);return n.skip(2),t=Math.min(t,n.remainingLength),me.decode(B(n,t))},il=n=>{let t=Lt(n);if(!t||t.name!=="data"||n.remainingLength<8)return null;let e=F(n);n.skip(4);let r=B(n,t.contentSize-8);switch(e){case 1:return me.decode(r);case 2:return new TextDecoder("utf-16be").decode(r);case 13:return new He(r,"image/jpeg");case 14:return new He(r,"image/png");case 27:return new He(r,"image/bmp");default:return r}};var ct=16,Ht=new Uint32Array(256),ci=new Uint32Array(256),ui=new Uint32Array(256),li=new Uint32Array(256),di=new Uint32Array(256),xe=new Uint32Array(256),nl=new Uint32Array(10),al=!1,Tm=()=>{let n=new Uint8Array(256),t=new Uint8Array(256),e=new Uint8Array(256);for(let a=0,s=1;a<256;a++)e[a]=s,t[s]=a,s=s^s<<1^(s&128?283:0);let r=(a,s)=>a&&s?e[(t[a]+t[s])%255]:0;n[0]=99;for(let a=1;a<256;a++){let s=e[255-t[a]],o=s^s<<1^s<<2^s<<3^s<<4;o=o>>>8^o&255^99,n[a]=o}for(let a=0;a<256;a++){let s=n[a],o=n.indexOf(a);Ht[a]=s<<24|s<<16|s<<8|s,xe[a]=o<<24|o<<16|o<<8|o;let c=r(o,14),u=r(o,9),l=r(o,13),m=r(o,11),d=c<<24|u<<16|l<<8|m;ci[a]=d,ui[a]=d>>>8|d<<24,li[a]=d>>>16|d<<16,di[a]=d>>>24|d<<8}let i=1;for(let a=0;a<10;a++)nl[a]=i<<24,i=i<<1^(i&128?283:0);al=!0},cn=class{constructor(){this.roundkey=new Uint32Array(44);this.iv=new Uint32Array(ct/Uint32Array.BYTES_PER_ELEMENT);this.in=new Uint8Array(ct);this.out=new Uint8Array(ct);this.inView=new DataView(this.in.buffer);this.outView=new DataView(this.out.buffer)}init({key:t,iv:e}){h(t.byteLength===16),h(e.byteLength===16),al||Tm();let r=new DataView(t.buffer,t.byteOffset,t.byteLength),i=new DataView(e.buffer,e.byteOffset,e.byteLength);this.roundkey[0]=r.getUint32(0,!1),this.roundkey[1]=r.getUint32(4,!1),this.roundkey[2]=r.getUint32(8,!1),this.roundkey[3]=r.getUint32(12,!1),this.iv[0]=i.getUint32(0,!1),this.iv[1]=i.getUint32(4,!1),this.iv[2]=i.getUint32(8,!1),this.iv[3]=i.getUint32(12,!1);for(let a=4;a<44;a+=4){let s=this.roundkey[a-1];this.roundkey[a]=this.roundkey[a-4]^Ht[s>>>16&255]&4278190080^Ht[s>>>8&255]&16711680^Ht[s>>>0&255]&65280^Ht[s>>>24&255]&255^nl[a/4-1],this.roundkey[a+1]=this.roundkey[a-3]^this.roundkey[a],this.roundkey[a+2]=this.roundkey[a-2]^this.roundkey[a+1],this.roundkey[a+3]=this.roundkey[a-1]^this.roundkey[a+2]}for(let a=0,s=40;a<s;a+=4,s-=4)for(let o=0;o<4;o++){let c=this.roundkey[a+o];this.roundkey[a+o]=this.roundkey[s+o],this.roundkey[s+o]=c}for(let a=4;a<40;a+=4)for(let s=0;s<4;s++){let o=this.roundkey[a+s];this.roundkey[a+s]=ci[Ht[o>>>24&255]&255]^ui[Ht[o>>>16&255]&255]^li[Ht[o>>>8&255]&255]^di[Ht[o>>>0&255]&255]}}decrypt(){let t=this.inView.getUint32(0,!1)^this.roundkey[0],e=this.inView.getUint32(4,!1)^this.roundkey[1],r=this.inView.getUint32(8,!1)^this.roundkey[2],i=this.inView.getUint32(12,!1)^this.roundkey[3],a=this.inView.getUint32(0,!1),s=this.inView.getUint32(4,!1),o=this.inView.getUint32(8,!1),c=this.inView.getUint32(12,!1),u,l,m,d;for(let y=1;y<10;y++){let k=y*4;u=ci[t>>>24]^ui[i>>>16&255]^li[r>>>8&255]^di[e&255]^this.roundkey[k],l=ci[e>>>24]^ui[t>>>16&255]^li[i>>>8&255]^di[r&255]^this.roundkey[k+1],m=ci[r>>>24]^ui[e>>>16&255]^li[t>>>8&255]^di[i&255]^this.roundkey[k+2],d=ci[i>>>24]^ui[r>>>16&255]^li[e>>>8&255]^di[t&255]^this.roundkey[k+3],t=u,e=l,r=m,i=d}let f=xe[t>>>24&255]&4278190080^xe[i>>>16&255]&16711680^xe[r>>>8&255]&65280^xe[e>>>0&255]&255^this.roundkey[40],p=xe[e>>>24&255]&4278190080^xe[t>>>16&255]&16711680^xe[i>>>8&255]&65280^xe[r>>>0&255]&255^this.roundkey[41],b=xe[r>>>24&255]&4278190080^xe[e>>>16&255]&16711680^xe[t>>>8&255]&65280^xe[i>>>0&255]&255^this.roundkey[42],g=xe[i>>>24&255]&4278190080^xe[r>>>16&255]&16711680^xe[e>>>8&255]&65280^xe[t>>>0&255]&255^this.roundkey[43];this.outView.setUint32(0,f^this.iv[0],!1),this.outView.setUint32(4,p^this.iv[1],!1),this.outView.setUint32(8,b^this.iv[2],!1),this.outView.setUint32(12,g^this.iv[3],!1),this.iv[0]=a,this.iv[1]=s,this.iv[2]=o,this.iv[3]=c}},sl=(n,t,e)=>{let r=!1,i=0,a=2**16,s=16,o=new cn;return new ReadableStream({pull:async c=>{r||(o.init(await t()),r=!0);let u=a+s,l=n.requestSliceRange(i,0,u);if(l instanceof Promise&&(l=await l),!l||l.length===0)throw new Error("Invalid ciphertext.");let m=l.length;if(m%16!==0)throw new Error("Invalid ciphertext.");let d=m===u?m-s:m,f=B(l,d),p=new Uint8Array(d);for(let b=0;b<d;b+=16)o.in.set(f.subarray(b,b+16)),o.decrypt(),p.set(o.out,b);if(d<m)c.enqueue(p),i+=d;else{let b=p[d-1];if(b===0||b>16)throw new Error("Invalid PKCS#7 padding. Incorrect key or corrupted data.");let g=p.subarray(0,d-b);c.enqueue(g),c.close(),e()}},cancel:()=>{e()}})};var ha=class n extends pe{constructor(e){super(e);this.moovSlice=null;this.currentTrack=null;this.tracks=[];this.metadataPromise=null;this.movieTimescale=-1;this.movieDurationInTimescale=-1;this.isQuickTime=!1;this.metadataTags={};this.currentMetadataKeys=null;this.isFragmented=!1;this.fragmentTrackDefaults=[];this.psshBoxes=[];this.currentFragment=null;this.lastReadFragment=null;this.decryptionKeyCache=new Map;this.reader=e._reader}async getTrackBackings(){return await this.readMetadata(),this.tracks.map(e=>e.trackBacking)}async getMimeType(){await this.readMetadata();let e=await this.getTrackBackings(),r=await Promise.all(e.map(i=>i.getDecoderConfig().then(a=>a?.codec??null)));return la({isQuickTime:this.isQuickTime,hasVideo:this.tracks.some(i=>i.info?.type==="video"),hasAudio:this.tracks.some(i=>i.info?.type==="audio"),codecStrings:r.filter(Boolean)})}async getMetadataTags(){return await this.readMetadata(),this.metadataTags}readMetadata(){return this.metadataPromise??=(async()=>{let e=0,r=!1;for(;;){let i=this.reader.requestSliceRange(e,st,Nt);if(i instanceof Promise&&(i=await i),!i)break;let a=e,s=Lt(i);if(!s)break;if(s.name==="ftyp"||s.name==="styp"){let o=te(i,4);this.isQuickTime=o==="qt "}else if(s.name==="moov"){let o=this.reader.requestSlice(i.filePos,s.contentSize);if(o instanceof Promise&&(o=await o),!o)break;this.moovSlice=o,this.readContiguousBoxes(this.moovSlice);for(let c of this.tracks){let u=c.editListPreviousSegmentDurations/this.movieTimescale;c.editListOffset-=Math.round(u*c.timescale)}r=this.isFragmented&&this.reader.fileSize!==null&&this.reader.fileSize>a+s.totalSize;break}else if(s.name==="moof"){if(!this.input._initInput)throw new Error('"moof" box encountered with no "moov" box present; this file is likely a Segment as described in ISO/IEC 14496-12 Section 8.16. A separate init file that contains a "moov" box is required to read this file, please provide it using InputOptions.initInput.');let o=await this.input._initInput._getDemuxer();if(o.constructor!==n)throw new Error("Init input must match the input's format.");await o.readMetadata(),this.movieTimescale=o.movieTimescale,this.movieDurationInTimescale=o.movieDurationInTimescale,this.metadataTags=o.metadataTags,this.isFragmented=!0,this.fragmentTrackDefaults=o.fragmentTrackDefaults,this.psshBoxes=o.psshBoxes;for(let c of o.tracks){let u={id:c.id,demuxer:this,trackBacking:null,disposition:c.disposition,timescale:c.timescale,durationInMediaTimescale:c.durationInMediaTimescale,durationInMovieTimescale:c.durationInMovieTimescale,rotation:c.rotation,internalCodecId:c.internalCodecId,name:c.name,languageCode:c.languageCode,sampleTableByteOffset:null,sampleTable:null,fragmentLookupTable:[],currentFragmentState:null,fragmentPositionCache:[],editListPreviousSegmentDurations:c.editListPreviousSegmentDurations,editListOffset:c.editListOffset,encryptionInfo:c.encryptionInfo,encryptionAuxInfo:null,frmaCodecString:null,info:c.info};if(c.trackBacking){if(h(u.info),u.info.type==="video"&&u.info.width!==-1){let l=u;u.trackBacking=new ba(l),this.tracks.push(u)}else if(u.info.type==="audio"&&u.info.numberOfChannels!==-1){let l=u;u.trackBacking=new ya(l),this.tracks.push(u)}}}r=!1;break}e=a+s.totalSize}if(r){h(this.reader.fileSize!==null);let i=this.reader.requestSlice(this.reader.fileSize-4,4);i instanceof Promise&&(i=await i),h(i);let a=F(i),s=this.reader.fileSize-a;if(s>=0&&s<=this.reader.fileSize-Nt){let o=this.reader.requestSliceRange(s,st,Nt);if(o instanceof Promise&&(o=await o),o){let c=Lt(o);if(c&&c.name==="mfra"){let u=this.reader.requestSlice(o.filePos,c.contentSize);u instanceof Promise&&(u=await u),u&&this.readContiguousBoxes(u)}}}}})()}getSampleTableForTrack(e){if(e.sampleTable)return e.sampleTable;let r={sampleTimingEntries:[],sampleCompositionTimeOffsets:[],sampleSizes:[],keySampleIndices:null,chunkOffsets:[],sampleToChunk:[],presentationTimestamps:null,presentationTimestampIndexMap:null};if(e.sampleTable=r,e.sampleTableByteOffset===null)return r;h(this.moovSlice);let i=this.moovSlice.slice(e.sampleTableByteOffset);if(this.currentTrack=e,this.traverseBox(i),this.currentTrack=null,e.info?.type==="audio"&&e.info.codec&&ee.includes(e.info.codec)&&r.sampleCompositionTimeOffsets.length===0){h(e.info?.type==="audio");let s=Se(e.info.codec),o=[],c=[];for(let u=0;u<r.sampleToChunk.length;u++){let l=r.sampleToChunk[u],m=r.sampleToChunk[u+1],d=(m?m.startChunkIndex:r.chunkOffsets.length)-l.startChunkIndex;for(let f=0;f<d;f++){let p=l.startSampleIndex+f*l.samplesPerChunk,b=p+l.samplesPerChunk,g=H(r.sampleTimingEntries,p,C=>C.startIndex),y=r.sampleTimingEntries[g],k=H(r.sampleTimingEntries,b,C=>C.startIndex),w=r.sampleTimingEntries[k],T=y.startDecodeTimestamp+(p-y.startIndex)*y.delta,x=w.startDecodeTimestamp+(b-w.startIndex)*w.delta-T,P=X(o);P&&P.delta===x?P.count++:o.push({startIndex:l.startChunkIndex+f,startDecodeTimestamp:T,count:1,delta:x});let A=l.samplesPerChunk*s.sampleSize*e.info.numberOfChannels;c.push(A)}l.startSampleIndex=l.startChunkIndex,l.samplesPerChunk=1}r.sampleTimingEntries=o,r.sampleSizes=c}if(r.sampleCompositionTimeOffsets.length>0){r.presentationTimestamps=[];for(let s of r.sampleTimingEntries)for(let o=0;o<s.count;o++)r.presentationTimestamps.push({presentationTimestamp:s.startDecodeTimestamp+o*s.delta,sampleIndex:s.startIndex+o});for(let s of r.sampleCompositionTimeOffsets)for(let o=0;o<s.count;o++){let c=s.startIndex+o,u=r.presentationTimestamps[c];u&&(u.presentationTimestamp+=s.offset)}r.presentationTimestamps.sort((s,o)=>s.presentationTimestamp-o.presentationTimestamp),r.presentationTimestampIndexMap=Array(r.presentationTimestamps.length).fill(-1);for(let s=0;s<r.presentationTimestamps.length;s++)r.presentationTimestampIndexMap[r.presentationTimestamps[s].sampleIndex]=s}return r}async readFragment(e){if(this.lastReadFragment?.moofOffset===e)return this.lastReadFragment;let r=this.reader.requestSliceRange(e,st,Nt);r instanceof Promise&&(r=await r),h(r);let i=Lt(r);h(i?.name==="moof");let a=this.reader.requestSlice(e,i.totalSize);a instanceof Promise&&(a=await a),h(a),this.traverseBox(a);let s=this.lastReadFragment;h(s&&s.moofOffset===e);for(let[,o]of s.trackData){let c=o.track,{fragmentPositionCache:u}=c;if(!o.startTimestampIsFinal){let m=c.fragmentLookupTable.find(d=>d.moofOffset===s.moofOffset);if(m)wo(o,m.timestamp);else{let d=H(u,s.moofOffset-1,f=>f.moofOffset);if(d!==-1){let f=u[d];wo(o,f.endTimestamp)}}o.startTimestampIsFinal=!0}let l=H(u,o.startTimestamp,m=>m.startTimestamp);if((l===-1||u[l].moofOffset!==s.moofOffset)&&u.splice(l+1,0,{moofOffset:s.moofOffset,startTimestamp:o.startTimestamp,endTimestamp:o.endTimestamp}),o.encryptionAuxInfo&&c.encryptionInfo){let m=await dl(this.reader,c.encryptionInfo,o.encryptionAuxInfo);for(let d=0;d<Math.min(o.samples.length,m.length);d++){let f=m[d];o.samples[d].encryption=f}}}return s}readContiguousBoxes(e){let r=e.filePos;for(;e.filePos-r<=e.length-st&&this.traverseBox(e););}*iterateContiguousBoxes(e){let r=e.filePos;for(;e.filePos-r<=e.length-st;){let i=e.filePos,a=Lt(e);if(!a)break;yield{boxInfo:a,slice:e},e.filePos=i+a.totalSize}}traverseBox(e){let r=e.filePos,i=Lt(e);if(!i)return!1;let a=e.filePos,s=r+i.totalSize;switch(i.name){case"mdia":case"minf":case"dinf":case"mfra":case"edts":case"sinf":case"schi":this.readContiguousBoxes(e.slice(a,i.contentSize));break;case"mvhd":{let o=O(e);e.skip(3),o===1?(e.skip(16),this.movieTimescale=F(e),this.movieDurationInTimescale=Be(e)):(e.skip(8),this.movieTimescale=F(e),this.movieDurationInTimescale=F(e))}break;case"trak":{let o={id:-1,demuxer:this,trackBacking:null,disposition:{...we,primary:!1},info:null,timescale:-1,durationInMovieTimescale:-1,durationInMediaTimescale:-1,rotation:0,internalCodecId:null,name:null,languageCode:J,sampleTableByteOffset:-1,sampleTable:null,fragmentLookupTable:[],currentFragmentState:null,fragmentPositionCache:[],editListPreviousSegmentDurations:0,editListOffset:0,encryptionInfo:null,encryptionAuxInfo:null,frmaCodecString:null};if(this.currentTrack=o,this.readContiguousBoxes(e.slice(a,i.contentSize)),o.id!==-1&&o.timescale!==-1&&o.info!==null){if(o.info.type==="video"&&o.info.width!==-1){let c=o;o.trackBacking=new ba(c),this.tracks.push(o)}else if(o.info.type==="audio"&&o.info.numberOfChannels!==-1){let c=o;o.trackBacking=new ya(c),this.tracks.push(o)}}this.currentTrack=null}break;case"tkhd":{let o=this.currentTrack;if(!o)break;let c=O(e),l=!!(ut(e)&1);if(o.disposition.default=l,c===0)e.skip(8),o.id=F(e),e.skip(4),o.durationInMovieTimescale=F(e);else if(c===1)e.skip(16),o.id=F(e),e.skip(4),o.durationInMovieTimescale=Be(e);else throw new Error(`Incorrect track header version ${c}.`);e.skip(2*4+2+2+2+2);let m=[mr(e),mr(e),fa(e),mr(e),mr(e),fa(e),mr(e),mr(e),fa(e)],d=Ze(ji(xm(m),90));h(d===0||d===90||d===180||d===270),o.rotation=d}break;case"elst":{let o=this.currentTrack;if(!o)break;let c=O(e);e.skip(3);let u=!1,l=0,m=F(e);for(let d=0;d<m;d++){let f=c===1?Be(e):F(e),p=c===1?fl(e):Wt(e),b=mr(e);if(f!==0){if(u){console.warn("Unsupported edit list: multiple edits are not currently supported. Only using first edit.");break}if(p===-1){l+=f;continue}if(b!==1){console.warn("Unsupported edit list entry: media rate must be 1.");break}o.editListPreviousSegmentDurations=l,o.editListOffset=p,u=!0}}}break;case"mdhd":{let o=this.currentTrack;if(!o)break;let c=O(e);e.skip(3),c===0?(e.skip(8),o.timescale=F(e),o.durationInMediaTimescale=F(e)):c===1&&(e.skip(16),o.timescale=F(e),o.durationInMediaTimescale=Be(e));let u=se(e);if(u>0){o.languageCode="";for(let l=0;l<3;l++)o.languageCode=String.fromCharCode(96+(u&31))+o.languageCode,u>>=5;Bt(o.languageCode)||(o.languageCode=J)}}break;case"hdlr":{let o=this.currentTrack;if(!o)break;e.skip(8);let c=te(e,4);c==="vide"?o.info={type:"video",width:-1,height:-1,squarePixelWidth:-1,squarePixelHeight:-1,codec:null,codecDescription:null,colorSpace:null,avcType:null,avcCodecInfo:null,hevcCodecInfo:null,vp9CodecInfo:null,av1CodecInfo:null}:c==="soun"&&(o.info={type:"audio",numberOfChannels:-1,sampleRate:-1,codec:null,codecDescription:null,aacCodecInfo:null,pcmLittleEndian:!1,pcmSampleSize:null})}break;case"stbl":{let o=this.currentTrack;if(!o)break;o.sampleTableByteOffset=r,this.readContiguousBoxes(e.slice(a,i.contentSize))}break;case"stsd":{let o=this.currentTrack;if(!o||o.info===null||o.sampleTable)break;let c=O(e);e.skip(3);let u=F(e);for(let l=0;l<u;l++){let m=e.filePos,d=Lt(e);if(!d)break;o.internalCodecId=d.name;let f=d.name.toLowerCase();if(o.info.type==="video"){e.skip(6*1+2+2+2+3*4),o.info.width=se(e),o.info.height=se(e),o.info.squarePixelWidth=o.info.width,o.info.squarePixelHeight=o.info.height,e.skip(50),o.frmaCodecString=null,this.readContiguousBoxes(e.slice(e.filePos,m+d.totalSize-e.filePos));let p=f==="encv"?o.frmaCodecString:f;o.frmaCodecString=null,p==="avc1"||p==="avc3"?(o.info.codec="avc",o.info.avcType=p==="avc1"?1:3):p==="hvc1"||p==="hev1"?o.info.codec="hevc":p==="vp08"?o.info.codec="vp8":p==="vp09"?o.info.codec="vp9":p==="av01"?o.info.codec="av1":console.warn(p===null?"Unknown encrypted video codec due to missing frma box.":`Unsupported video codec (sample entry type '${d.name}').`)}else{e.skip(6*1+2);let p=se(e);e.skip(3*2);let b=se(e),g=se(e);e.skip(2*2);let y=F(e)/65536,k=null;c===0&&p>0&&(p===1?(e.skip(4),g=8*F(e),e.skip(2*4)):p===2&&(e.skip(4),y=ka(e),b=F(e),e.skip(4),g=F(e),k=F(e),e.skip(2*4))),o.info.numberOfChannels=b,o.info.sampleRate=y,o.frmaCodecString=null,this.readContiguousBoxes(e.slice(e.filePos,m+d.totalSize-e.filePos));let w=f==="enca"?o.frmaCodecString:f;if(o.frmaCodecString=null,w!=="mp4a")if(w==="opus")o.info.codec="opus",o.info.sampleRate=nt;else if(w==="flac")o.info.codec="flac";else if(w==="ulaw")o.info.codec="ulaw";else if(w==="alaw")o.info.codec="alaw";else if(w==="ac-3")o.info.codec="ac3";else if(w==="ec-3")o.info.codec="eac3";else if(w==="twos")g===8?o.info.codec="pcm-s8":g===16?o.info.codec=o.info.pcmLittleEndian?"pcm-s16":"pcm-s16be":(console.warn(`Unsupported sample size ${g} for codec 'twos'.`),o.info.codec=null);else if(w==="sowt")g===8?o.info.codec="pcm-s8":g===16?o.info.codec="pcm-s16":(console.warn(`Unsupported sample size ${g} for codec 'sowt'.`),o.info.codec=null);else if(w==="raw ")o.info.codec="pcm-u8";else if(w==="in24")o.info.codec=o.info.pcmLittleEndian?"pcm-s24":"pcm-s24be";else if(w==="in32")o.info.codec=o.info.pcmLittleEndian?"pcm-s32":"pcm-s32be";else if(w==="fl32")o.info.codec=o.info.pcmLittleEndian?"pcm-f32":"pcm-f32be";else if(w==="fl64")o.info.codec=o.info.pcmLittleEndian?"pcm-f64":"pcm-f64be";else if(w==="ipcm"){let T=o.info.pcmSampleSize;o.info.pcmLittleEndian?T===16?o.info.codec="pcm-s16":T===24?o.info.codec="pcm-s24":T===32?o.info.codec="pcm-s32":(console.warn(`Invalid ipcm sample size ${T}.`),o.info.codec=null):T===16?o.info.codec="pcm-s16be":T===24?o.info.codec="pcm-s24be":T===32?o.info.codec="pcm-s32be":(console.warn(`Invalid ipcm sample size ${T}.`),o.info.codec=null)}else if(w==="fpcm"){let T=o.info.pcmSampleSize;o.info.pcmLittleEndian?T===32?o.info.codec="pcm-f32":T===64?o.info.codec="pcm-f64":(console.warn(`Invalid fpcm sample size ${T}.`),o.info.codec=null):T===32?o.info.codec="pcm-f32be":T===64?o.info.codec="pcm-f64be":(console.warn(`Invalid fpcm sample size ${T}.`),o.info.codec=null)}else if(w==="lpcm"&&k!==null){let T=g+7>>3,S=!!(k&1),x=!!(k&2),P=k&4?-1:0;g>0&&g<=64&&(S?g===32&&(o.info.codec=x?"pcm-f32be":"pcm-f32"):P&1<<T-1?T===1?o.info.codec="pcm-s8":T===2?o.info.codec=x?"pcm-s16be":"pcm-s16":T===3?o.info.codec=x?"pcm-s24be":"pcm-s24":T===4&&(o.info.codec=x?"pcm-s32be":"pcm-s32"):T===1&&(o.info.codec="pcm-u8")),o.info.codec===null&&console.warn("Unsupported PCM format.")}else console.warn(w===null?"Unknown encrypted audio codec due to missing frma box.":`Unsupported audio codec (sample entry type '${d.name}').`)}e.filePos=m+d.totalSize}}break;case"frma":{let o=this.currentTrack;if(!o)break;let u=te(e,4).toLowerCase();o.frmaCodecString=u}break;case"schm":{let o=this.currentTrack;if(!o)break;e.skip(4);let c=te(e,4);c==="cenc"||c==="cens"||c==="cbcs"?o.encryptionInfo={scheme:c,defaultKid:null,defaultIsProtected:null,defaultPerSampleIvSize:null,defaultConstantIv:null,defaultCryptByteBlock:null,defaultSkipByteBlock:null}:console.warn(`Unsupported encryption scheme '${c}'.`)}break;case"tenc":{let o=this.currentTrack;if(!o||!o.encryptionInfo)break;let c=O(e);e.skip(3),e.skip(1);let u=O(e);if(c>0?(o.encryptionInfo.defaultCryptByteBlock=u>>4,o.encryptionInfo.defaultSkipByteBlock=u&15):(o.encryptionInfo.defaultCryptByteBlock=0,o.encryptionInfo.defaultSkipByteBlock=0),o.encryptionInfo.defaultIsProtected=O(e)!==0,o.encryptionInfo.defaultPerSampleIvSize=O(e),o.encryptionInfo.defaultKid=tr(B(e,16)),o.encryptionInfo.defaultIsProtected&&o.encryptionInfo.defaultPerSampleIvSize===0){let l=O(e),m=new Uint8Array(16);m.set(B(e,l),0),o.encryptionInfo.defaultConstantIv=m}}break;case"avcC":{let o=this.currentTrack;if(!o)break;h(o.info),o.info.codecDescription=B(e,i.contentSize)}break;case"hvcC":{let o=this.currentTrack;if(!o)break;h(o.info),o.info.codecDescription=B(e,i.contentSize)}break;case"vpcC":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="video"),e.skip(4);let c=O(e),u=O(e),l=O(e),m=l>>4,d=l>>1&7,f=l&1,p=O(e),b=O(e),g=O(e);o.info.vp9CodecInfo={profile:c,level:u,bitDepth:m,chromaSubsampling:d,videoFullRangeFlag:f,colourPrimaries:p,transferCharacteristics:b,matrixCoefficients:g}}break;case"av1C":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="video"),e.skip(1);let c=O(e),u=c>>5,l=c&31,m=O(e),d=m>>7,f=m>>6&1,p=m>>5&1,b=m>>4&1,g=m>>3&1,y=m>>2&1,k=m&3,w=u===2&&f?p?12:10:f?10:8;o.info.av1CodecInfo={profile:u,level:l,tier:d,bitDepth:w,monochrome:b,chromaSubsamplingX:g,chromaSubsamplingY:y,chromaSamplePosition:k}}break;case"colr":{let o=this.currentTrack;if(!o||(h(o.info?.type==="video"),te(e,4)!=="nclx"))break;let u=se(e),l=se(e),m=se(e),d=!!(O(e)&128);o.info.colorSpace={primaries:xr[u],transfer:Pr[l],matrix:Cr[m],fullRange:d}}break;case"pasp":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="video");let c=F(e),u=F(e);c>0&&u>0&&(c>u?o.info.squarePixelWidth=Math.round(o.info.width*c/u):o.info.squarePixelHeight=Math.round(o.info.height*u/c))}break;case"wave":this.readContiguousBoxes(e.slice(a,i.contentSize));break;case"esds":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio"),e.skip(4);let c=O(e);h(c===3),pa(e),e.skip(2);let u=O(e),l=(u&128)!==0,m=(u&64)!==0,d=(u&32)!==0;if(l&&e.skip(2),m){let y=O(e);e.skip(y)}d&&e.skip(2);let f=O(e);h(f===4);let p=pa(e),b=e.filePos,g=O(e);if(g===64||g===103?(o.info.codec="aac",o.info.aacCodecInfo={isMpeg2:g===103,objectType:null}):g===105||g===107?o.info.codec="mp3":g===221?o.info.codec="vorbis":console.warn(`Unsupported audio codec (objectTypeIndication ${g}) - discarding track.`),e.skip(12),p>e.filePos-b){let y=O(e);h(y===5);let k=pa(e);if(o.info.codecDescription=B(e,k),o.info.codec==="aac"){let w=Tt(o.info.codecDescription);w.numberOfChannels!==null&&(o.info.numberOfChannels=w.numberOfChannels),w.sampleRate!==null&&(o.info.sampleRate=w.sampleRate)}}}break;case"enda":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio"),o.info.pcmLittleEndian=!!(se(e)&255)}break;case"pcmC":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio"),e.skip(4);let c=O(e);o.info.pcmLittleEndian=!!(c&1),o.info.pcmSampleSize=O(e)}break;case"dOps":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio"),e.skip(1);let c=O(e),u=se(e),l=F(e),m=un(e),d=O(e),f;d!==0?f=B(e,2+c):f=new Uint8Array(0);let p=new Uint8Array(19+f.byteLength),b=new DataView(p.buffer);b.setUint32(0,1332770163,!1),b.setUint32(4,1214603620,!1),b.setUint8(8,1),b.setUint8(9,c),b.setUint16(10,u,!0),b.setUint32(12,l,!0),b.setInt16(16,m,!0),b.setUint8(18,d),p.set(f,19),o.info.codecDescription=p,o.info.numberOfChannels=c}break;case"dfLa":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio"),e.skip(4);let c=127,u=128,l=e.filePos;for(;e.filePos<s;){let b=O(e),g=ut(e);if((b&c)===0){e.skip(10);let k=F(e),w=k>>>12,T=(k>>9&7)+1;o.info.sampleRate=w,o.info.numberOfChannels=T,e.skip(20)}else e.skip(g);if(b&u)break}let m=e.filePos;e.filePos=l;let d=B(e,m-l),f=new Uint8Array(4+d.byteLength);new DataView(f.buffer).setUint32(0,1716281667,!1),f.set(d,4),o.info.codecDescription=f}break;case"dac3":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio");let c=B(e,3),u=new W(c),l=u.readBits(2);u.skipBits(8);let m=u.readBits(3),d=u.readBits(1);l<3&&(o.info.sampleRate=Or[l]),o.info.numberOfChannels=an[m]+d}break;case"dec3":{let o=this.currentTrack;if(!o)break;h(o.info?.type==="audio");let c=B(e,i.contentSize),u=rl(c);if(!u){console.warn("Invalid dec3 box contents, ignoring.");break}let l=ca(u);l!==null&&(o.info.sampleRate=l),o.info.numberOfChannels=ua(u)}break;case"stts":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e),u=0,l=0;for(let m=0;m<c;m++){let d=F(e),f=F(e);o.sampleTable.sampleTimingEntries.push({startIndex:u,startDecodeTimestamp:l,count:d,delta:f}),u+=d,l+=d*f}}break;case"ctts":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e),u=0;for(let l=0;l<c;l++){let m=F(e),d=Wt(e);o.sampleTable.sampleCompositionTimeOffsets.push({startIndex:u,count:m,offset:d}),u+=m}}break;case"stsz":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e),u=F(e);if(c===0)for(let l=0;l<u;l++){let m=F(e);o.sampleTable.sampleSizes.push(m)}else o.sampleTable.sampleSizes.push(c)}break;case"stz2":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4),e.skip(3);let c=O(e),u=F(e),l=B(e,Math.ceil(u*c/8)),m=new W(l);for(let d=0;d<u;d++){let f=m.readBits(c);o.sampleTable.sampleSizes.push(f)}}break;case"stss":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4),o.sampleTable.keySampleIndices=[];let c=F(e);for(let u=0;u<c;u++){let l=F(e)-1;o.sampleTable.keySampleIndices.push(l)}o.sampleTable.keySampleIndices[0]!==0&&o.sampleTable.keySampleIndices.unshift(0)}break;case"stsc":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e);for(let l=0;l<c;l++){let m=F(e)-1,d=F(e),f=F(e);o.sampleTable.sampleToChunk.push({startSampleIndex:-1,startChunkIndex:m,samplesPerChunk:d,sampleDescriptionIndex:f})}let u=0;for(let l=0;l<o.sampleTable.sampleToChunk.length;l++)if(o.sampleTable.sampleToChunk[l].startSampleIndex=u,l<o.sampleTable.sampleToChunk.length-1){let d=o.sampleTable.sampleToChunk[l+1].startChunkIndex-o.sampleTable.sampleToChunk[l].startChunkIndex;u+=d*o.sampleTable.sampleToChunk[l].samplesPerChunk}}break;case"stco":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e);for(let u=0;u<c;u++){let l=F(e);o.sampleTable.chunkOffsets.push(l)}}break;case"co64":{let o=this.currentTrack;if(!o||!o.sampleTable)break;e.skip(4);let c=F(e);for(let u=0;u<c;u++){let l=Be(e);o.sampleTable.chunkOffsets.push(l)}}break;case"mvex":this.isFragmented=!0,this.readContiguousBoxes(e.slice(a,i.contentSize));break;case"mehd":{let o=O(e);e.skip(3);let c=o===1?Be(e):F(e);this.movieDurationInTimescale=c}break;case"trex":{e.skip(4);let o=F(e),c=F(e),u=F(e),l=F(e),m=F(e);this.fragmentTrackDefaults.push({trackId:o,defaultSampleDescriptionIndex:c,defaultSampleDuration:u,defaultSampleSize:l,defaultSampleFlags:m})}break;case"tfra":{let o=O(e);e.skip(3);let c=F(e),u=this.tracks.find(w=>w.id===c);if(!u)break;let l=F(e),m=(l&48)>>4,d=(l&12)>>2,f=l&3,p=[O,se,ut,F],b=p[m],g=p[d],y=p[f],k=F(e);for(let w=0;w<k;w++){let T=o===1?Be(e):F(e),S=o===1?Be(e):F(e);b(e),g(e),y(e),u.fragmentLookupTable.push({timestamp:T,moofOffset:S})}u.fragmentLookupTable.sort((w,T)=>w.timestamp-T.timestamp);for(let w=0;w<u.fragmentLookupTable.length-1;w++){let T=u.fragmentLookupTable[w],S=u.fragmentLookupTable[w+1];T.timestamp===S.timestamp&&(u.fragmentLookupTable.splice(w+1,1),w--)}}break;case"moof":this.currentFragment={moofOffset:r,moofSize:i.totalSize,implicitBaseDataOffset:r,trackData:new Map,psshBoxes:[]},this.readContiguousBoxes(e.slice(a,i.contentSize)),this.lastReadFragment=this.currentFragment,this.currentFragment=null;break;case"traf":if(h(this.currentFragment),this.readContiguousBoxes(e.slice(a,i.contentSize)),this.currentTrack){let o=this.currentFragment.trackData.get(this.currentTrack.id);if(o){this.currentFragment.implicitBaseDataOffset=o.currentOffset,o.presentationTimestamps=o.samples.map((m,d)=>({presentationTimestamp:m.presentationTimestamp,sampleIndex:d})).sort((m,d)=>m.presentationTimestamp-d.presentationTimestamp);for(let m=0;m<o.presentationTimestamps.length;m++){let d=o.presentationTimestamps[m],f=o.samples[d.sampleIndex];if(o.firstKeyFrameTimestamp===null&&f.isKeyFrame&&(o.firstKeyFrameTimestamp=f.presentationTimestamp),m<o.presentationTimestamps.length-1){let b=o.presentationTimestamps[m+1].presentationTimestamp-d.presentationTimestamp;f.duration=b}}let c=o.samples[o.presentationTimestamps[0].sampleIndex],u=o.samples[X(o.presentationTimestamps).sampleIndex];o.startTimestamp=c.presentationTimestamp,o.endTimestamp=u.presentationTimestamp+u.duration;let{currentFragmentState:l}=this.currentTrack;h(l),l.startTimestamp!==null&&(wo(o,l.startTimestamp),o.startTimestampIsFinal=!0),l.encryptionAuxInfo&&!o.samples[0].encryption&&(o.encryptionAuxInfo=l.encryptionAuxInfo)}this.currentTrack.currentFragmentState=null,this.currentTrack=null}break;case"pssh":{if(this.input._formatOptions.isobmff?._suppressPsshParsing)break;let o=da(B(e,i.contentSize));this.currentFragment?this.currentFragment.psshBoxes.push(o):this.currentTrack||this.psshBoxes.push(o)}break;case"tfhd":{h(this.currentFragment),e.skip(1);let o=ut(e),c=!!(o&1),u=!!(o&2),l=!!(o&8),m=!!(o&16),d=!!(o&32),f=!!(o&65536),p=!!(o&131072),b=F(e),g=this.tracks.find(k=>k.id===b);if(!g)break;let y=this.fragmentTrackDefaults.find(k=>k.trackId===b);this.currentTrack=g,g.currentFragmentState={baseDataOffset:this.currentFragment.implicitBaseDataOffset,sampleDescriptionIndex:y?.defaultSampleDescriptionIndex??null,defaultSampleDuration:y?.defaultSampleDuration??null,defaultSampleSize:y?.defaultSampleSize??null,defaultSampleFlags:y?.defaultSampleFlags??null,startTimestamp:null,encryptionAuxInfo:null},c?g.currentFragmentState.baseDataOffset=Be(e):p&&(g.currentFragmentState.baseDataOffset=this.currentFragment.moofOffset),u&&(g.currentFragmentState.sampleDescriptionIndex=F(e)),l&&(g.currentFragmentState.defaultSampleDuration=F(e)),m&&(g.currentFragmentState.defaultSampleSize=F(e)),d&&(g.currentFragmentState.defaultSampleFlags=F(e)),f&&(g.currentFragmentState.defaultSampleDuration=0)}break;case"tfdt":{let o=this.currentTrack;if(!o)break;h(o.currentFragmentState);let c=O(e);e.skip(3);let u=c===0?F(e):Be(e);o.currentFragmentState.startTimestamp=u}break;case"trun":{let o=this.currentTrack;if(!o)break;h(this.currentFragment),h(o.currentFragmentState);let c=O(e),u=ut(e),l=!!(u&1),m=!!(u&4),d=!!(u&256),f=!!(u&512),p=!!(u&1024),b=!!(u&2048),g=F(e),y=null;l&&(y=Wt(e));let k=null;m&&(k=F(e));let w;if(this.currentFragment.trackData.has(o.id)?(w=this.currentFragment.trackData.get(o.id),y!==null&&(w.currentOffset=o.currentFragmentState.baseDataOffset+y)):(w={track:o,currentTimestamp:0,currentOffset:o.currentFragmentState.baseDataOffset+(y??0),startTimestamp:0,endTimestamp:0,firstKeyFrameTimestamp:null,samples:[],presentationTimestamps:[],startTimestampIsFinal:!1,encryptionAuxInfo:null},this.currentFragment.trackData.set(o.id,w)),g===0){this.currentFragment.implicitBaseDataOffset=w.currentOffset;break}for(let T=0;T<g;T++){let S;d?S=F(e):(h(o.currentFragmentState.defaultSampleDuration!==null),S=o.currentFragmentState.defaultSampleDuration);let x;f?x=F(e):(h(o.currentFragmentState.defaultSampleSize!==null),x=o.currentFragmentState.defaultSampleSize);let P;p?P=F(e):(h(o.currentFragmentState.defaultSampleFlags!==null),P=o.currentFragmentState.defaultSampleFlags),T===0&&k!==null&&(P=k);let A=0;b&&(c===0?A=F(e):A=Wt(e));let C=!(P&65536);w.samples.push({presentationTimestamp:w.currentTimestamp+A,duration:S,byteOffset:w.currentOffset,byteSize:x,isKeyFrame:C,encryption:null}),w.currentOffset+=x,w.currentTimestamp+=S}}break;case"saiz":{let o=this.currentTrack;if(!o||!o.encryptionInfo)break;if(e.skip(1),ut(e)&1){let f=te(e,4),p=F(e);if(f!==o.encryptionInfo.scheme||p!==0)break}let u=O(e),l=F(e),m=null;u===0&&l>0&&(m=B(e,l));let d=cl(o);d.defaultSampleInfoSize=u,d.sampleSizes=m,d.sampleCount=l}break;case"saio":{let o=this.currentTrack;if(!o||!o.encryptionInfo)break;let c=O(e);if(ut(e)&1){let f=te(e,4),p=F(e);if(f!==o.encryptionInfo.scheme||p!==0)break}let l=F(e);if(l===0)break;l>1&&console.warn("Multiple saio entries are not supported; using the first offset only.");let m=c===0?F(e):Number(Be(e));this.currentFragment&&(m+=this.currentFragment.moofOffset);let d=cl(o);d.offset=m}break;case"senc":{let o=this.currentTrack;if(!o||!o.encryptionInfo)break;h(this.currentFragment);let c=this.currentFragment.trackData.get(o.id);if(!c)break;e.skip(1);let l=!!(ut(e)&2),m=F(e),d=o.encryptionInfo.defaultPerSampleIvSize;h(d!==null);for(let f=0;f<Math.min(m,c.samples.length);f++){let p=new Uint8Array(16);d>0?p.set(B(e,d),0):p.set(o.encryptionInfo.defaultConstantIv,0);let b=null;if(l){let y=se(e);b=[];for(let k=0;k<y;k++){let w=se(e),T=F(e);b.push({clearLen:w,protectedLen:T})}}let g=c.samples[f];g.encryption={iv:p,subsamples:b}}}break;case"udta":{let o=this.iterateContiguousBoxes(e.slice(a,i.contentSize));for(let{boxInfo:c,slice:u}of o){if(c.name!=="meta"&&!this.currentTrack){let l=u.filePos;this.metadataTags.raw??={},c.name[0]==="\xA9"?this.metadataTags.raw[c.name]??=ot(u):this.metadataTags.raw[c.name]??=B(u,c.contentSize),u.filePos=l}switch(c.name){case"meta":u.skip(-c.headerSize),this.traverseBox(u);break;case"\xA9nam":case"name":this.currentTrack?this.currentTrack.name=me.decode(B(u,c.contentSize)):this.metadataTags.title??=ot(u);break;case"\xA9des":this.currentTrack||(this.metadataTags.description??=ot(u));break;case"\xA9ART":this.currentTrack||(this.metadataTags.artist??=ot(u));break;case"\xA9alb":this.currentTrack||(this.metadataTags.album??=ot(u));break;case"albr":this.currentTrack||(this.metadataTags.albumArtist??=ot(u));break;case"\xA9gen":this.currentTrack||(this.metadataTags.genre??=ot(u));break;case"\xA9day":if(!this.currentTrack){let l=new Date(ot(u));Number.isNaN(l.getTime())||(this.metadataTags.date??=l)}break;case"\xA9cmt":this.currentTrack||(this.metadataTags.comment??=ot(u));break;case"\xA9lyr":this.currentTrack||(this.metadataTags.lyrics??=ot(u));break}}}break;case"meta":{if(this.currentTrack)break;let c=F(e)!==0;this.currentMetadataKeys=new Map,c?this.readContiguousBoxes(e.slice(a,i.contentSize)):this.readContiguousBoxes(e.slice(a+4,i.contentSize-4)),this.currentMetadataKeys=null}break;case"keys":{if(!this.currentMetadataKeys)break;e.skip(4);let o=F(e);for(let c=0;c<o;c++){let u=F(e);e.skip(4);let l=me.decode(B(e,u-8));this.currentMetadataKeys.set(c+1,l)}}break;case"ilst":{if(!this.currentMetadataKeys)break;let o=this.iterateContiguousBoxes(e.slice(a,i.contentSize));for(let{boxInfo:c,slice:u}of o){let l=c.name,m=(l.charCodeAt(0)<<24)+(l.charCodeAt(1)<<16)+(l.charCodeAt(2)<<8)+l.charCodeAt(3);this.currentMetadataKeys.has(m)&&(l=this.currentMetadataKeys.get(m));let d=il(u);switch(this.metadataTags.raw??={},this.metadataTags.raw[l]??=d,l){case"\xA9nam":case"titl":case"com.apple.quicktime.title":case"title":typeof d=="string"&&(this.metadataTags.title??=d);break;case"\xA9des":case"desc":case"dscp":case"com.apple.quicktime.description":case"description":typeof d=="string"&&(this.metadataTags.description??=d);break;case"\xA9ART":case"com.apple.quicktime.artist":case"artist":typeof d=="string"&&(this.metadataTags.artist??=d);break;case"\xA9alb":case"albm":case"com.apple.quicktime.album":case"album":typeof d=="string"&&(this.metadataTags.album??=d);break;case"aART":case"album_artist":typeof d=="string"&&(this.metadataTags.albumArtist??=d);break;case"\xA9cmt":case"com.apple.quicktime.comment":case"comment":typeof d=="string"&&(this.metadataTags.comment??=d);break;case"\xA9gen":case"gnre":case"com.apple.quicktime.genre":case"genre":typeof d=="string"&&(this.metadataTags.genre??=d);break;case"\xA9lyr":case"lyrics":typeof d=="string"&&(this.metadataTags.lyrics??=d);break;case"\xA9day":case"rldt":case"com.apple.quicktime.creationdate":case"date":if(typeof d=="string"){let f=new Date(d);Number.isNaN(f.getTime())||(this.metadataTags.date??=f)}break;case"covr":case"com.apple.quicktime.artwork":d instanceof He?(this.metadataTags.images??=[],this.metadataTags.images.push({data:d.data,kind:"coverFront",mimeType:d.mimeType})):d instanceof Uint8Array&&(this.metadataTags.images??=[],this.metadataTags.images.push({data:d,kind:"coverFront",mimeType:"image/*"}));break;case"track":if(typeof d=="string"){let f=d.split("/"),p=Number.parseInt(f[0],10),b=f[1]&&Number.parseInt(f[1],10);Number.isInteger(p)&&p>0&&(this.metadataTags.trackNumber??=p),b&&Number.isInteger(b)&&b>0&&(this.metadataTags.tracksTotal??=b)}break;case"trkn":if(d instanceof Uint8Array&&d.length>=6){let f=V(d),p=f.getUint16(2,!1),b=f.getUint16(4,!1);p>0&&(this.metadataTags.trackNumber??=p),b>0&&(this.metadataTags.tracksTotal??=b)}break;case"disc":case"disk":if(d instanceof Uint8Array&&d.length>=6){let f=V(d),p=f.getUint16(2,!1),b=f.getUint16(4,!1);p>0&&(this.metadataTags.discNumber??=p),b>0&&(this.metadataTags.discsTotal??=b)}break}}}break}return e.filePos=s,!0}},ga=class{constructor(t){this.internalTrack=t;this.packetToSampleIndex=new WeakMap;this.packetToFragmentLocation=new WeakMap}getId(){return this.internalTrack.id}getNumber(){let t=this.internalTrack.demuxer,e=this.internalTrack.trackBacking.getType(),r=0;for(let i of t.tracks)if(i.trackBacking.getType()===e&&r++,i===this.internalTrack)break;return r}getCodec(){throw new Error("Not implemented on base class.")}getInternalCodecId(){return this.internalTrack.internalCodecId}getName(){return this.internalTrack.name}getLanguageCode(){return this.internalTrack.languageCode}getTimeResolution(){return this.internalTrack.timescale}isRelativeToUnixEpoch(){return!1}getDisposition(){return this.internalTrack.disposition}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){let t=this.internalTrack;return t.durationInMediaTimescale<=0?null:(h(t.trackBacking),((await t.trackBacking.getFirstPacket({metadataOnly:!0}))?.timestamp??0)+t.durationInMediaTimescale/t.timescale)}async getLiveRefreshInterval(){return null}async getFirstPacket(t){let e=await this.fetchPacketForSampleIndex(0,t);return e||!this.internalTrack.demuxer.isFragmented?e:this.performFragmentedLookup(null,r=>r.trackData.get(this.internalTrack.id)?{sampleIndex:0,correctSampleFound:!0}:{sampleIndex:-1,correctSampleFound:!1},-1/0,1/0,t)}mapTimestampIntoTimescale(t){return Mt(t*this.internalTrack.timescale)+this.internalTrack.editListOffset}async getPacket(t,e){let r=this.mapTimestampIntoTimescale(t),i=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),a=So(i,r),s=await this.fetchPacketForSampleIndex(a,e);return!ol(i)||!this.internalTrack.demuxer.isFragmented?s:this.performFragmentedLookup(null,o=>{let c=o.trackData.get(this.internalTrack.id);if(!c)return{sampleIndex:-1,correctSampleFound:!1};let u=H(c.presentationTimestamps,r,d=>d.presentationTimestamp),l=u!==-1?c.presentationTimestamps[u].sampleIndex:-1,m=u!==-1&&r<c.endTimestamp;return{sampleIndex:l,correctSampleFound:m}},r,r,e)}async getNextPacket(t,e){let r=this.packetToSampleIndex.get(t);if(r!==void 0)return this.fetchPacketForSampleIndex(r+1,e);let i=this.packetToFragmentLocation.get(t);if(i===void 0)throw new Error("Packet was not created from this track.");return this.performFragmentedLookup(i.fragment,a=>{if(a===i.fragment){let s=a.trackData.get(this.internalTrack.id);if(i.sampleIndex+1<s.samples.length)return{sampleIndex:i.sampleIndex+1,correctSampleFound:!0}}else if(a.trackData.get(this.internalTrack.id))return{sampleIndex:0,correctSampleFound:!0};return{sampleIndex:-1,correctSampleFound:!1}},-1/0,1/0,e)}async getKeyPacket(t,e){let r=this.mapTimestampIntoTimescale(t),i=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),a=wm(i,r),s=await this.fetchPacketForSampleIndex(a,e);return!ol(i)||!this.internalTrack.demuxer.isFragmented?s:this.performFragmentedLookup(null,o=>{let c=o.trackData.get(this.internalTrack.id);if(!c)return{sampleIndex:-1,correctSampleFound:!1};let u=rr(c.presentationTimestamps,d=>c.samples[d.sampleIndex].isKeyFrame&&d.presentationTimestamp<=r),l=u!==-1?c.presentationTimestamps[u].sampleIndex:-1,m=u!==-1&&r<c.endTimestamp;return{sampleIndex:l,correctSampleFound:m}},r,r,e)}async getNextKeyPacket(t,e){let r=this.packetToSampleIndex.get(t);if(r!==void 0){let a=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),s=Am(a,r);return this.fetchPacketForSampleIndex(s,e)}let i=this.packetToFragmentLocation.get(t);if(i===void 0)throw new Error("Packet was not created from this track.");return this.performFragmentedLookup(i.fragment,a=>{if(a===i.fragment){let o=a.trackData.get(this.internalTrack.id).samples.findIndex((c,u)=>c.isKeyFrame&&u>i.sampleIndex);if(o!==-1)return{sampleIndex:o,correctSampleFound:!0}}else{let s=a.trackData.get(this.internalTrack.id);if(s&&s.firstKeyFrameTimestamp!==null){let o=s.samples.findIndex(c=>c.isKeyFrame);return h(o!==-1),{sampleIndex:o,correctSampleFound:!0}}}return{sampleIndex:-1,correctSampleFound:!1}},-1/0,1/0,e)}async fetchPacketForSampleIndex(t,e){if(t===-1)return null;let r=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),i=Sm(r,t);if(!i)return null;let a;if(e.metadataOnly)a=de;else{let u=this.internalTrack.demuxer.reader.requestSlice(i.sampleOffset,i.sampleSize);if(u instanceof Promise&&(u=await u),!u)return null;if(a=B(u,i.sampleSize),this.internalTrack.encryptionAuxInfo){h(this.internalTrack.encryptionInfo);let l=await dl(this.internalTrack.demuxer.reader,this.internalTrack.encryptionInfo,this.internalTrack.encryptionAuxInfo);t<l.length&&(a=await ul(this.internalTrack,l[t],a,null))}}let s=(i.presentationTimestamp-this.internalTrack.editListOffset)/this.internalTrack.timescale,o=i.duration/this.internalTrack.timescale,c=new Q(a,i.isKeyFrame?"key":"delta",s,o,t,i.sampleSize);return this.packetToSampleIndex.set(c,t),c}async fetchPacketInFragment(t,e,r){if(e===-1)return null;let a=t.trackData.get(this.internalTrack.id).samples[e];h(a);let s;if(r.metadataOnly)s=de;else{let l=this.internalTrack.demuxer.reader.requestSlice(a.byteOffset,a.byteSize);if(l instanceof Promise&&(l=await l),!l)return null;s=B(l,a.byteSize),a.encryption&&(s=await ul(this.internalTrack,a.encryption,s,t))}let o=(a.presentationTimestamp-this.internalTrack.editListOffset)/this.internalTrack.timescale,c=a.duration/this.internalTrack.timescale,u=new Q(s,a.isKeyFrame?"key":"delta",o,c,t.moofOffset+e,a.byteSize);return this.packetToFragmentLocation.set(u,{fragment:t,sampleIndex:e}),u}async performFragmentedLookup(t,e,r,i,a){let s=this.internalTrack.demuxer,o=null,c=null,u=-1;if(t){let{sampleIndex:g,correctSampleFound:y}=e(t);if(y)return this.fetchPacketInFragment(t,g,a);g!==-1&&(c=t,u=g)}let l=H(this.internalTrack.fragmentLookupTable,r,g=>g.timestamp),m=l!==-1?this.internalTrack.fragmentLookupTable[l]:null,d=H(this.internalTrack.fragmentPositionCache,r,g=>g.startTimestamp),f=d!==-1?this.internalTrack.fragmentPositionCache[d]:null,p=Math.max(m?.moofOffset??0,f?.moofOffset??0)||null,b;for(t?p===null||t.moofOffset>=p?(b=t.moofOffset+t.moofSize,o=t):b=p:b=p??0;;){if(o){let w=o.trackData.get(this.internalTrack.id);if(w&&w.startTimestamp>i)break}let g=s.reader.requestSliceRange(b,st,Nt);if(g instanceof Promise&&(g=await g),!g)break;let y=b,k=Lt(g);if(!k)break;if(k.name==="moof"){o=await s.readFragment(y);let{sampleIndex:w,correctSampleFound:T}=e(o);if(T)return this.fetchPacketInFragment(o,w,a);w!==-1&&(c=o,u=w)}b=y+k.totalSize}if(m&&(!c||c.moofOffset<m.moofOffset)){let g=this.internalTrack.fragmentLookupTable[l-1];h(!g||g.timestamp<m.timestamp);let y=g?.timestamp??-1/0;return this.performFragmentedLookup(null,e,y,i,a)}return c?this.fetchPacketInFragment(c,u,a):null}},ba=class extends ga{constructor(e){super(e);this.decoderConfigPromise=null;this.internalTrack=e}getType(){return"video"}getCodec(){return this.internalTrack.info.codec}getCodedWidth(){return this.internalTrack.info.width}getCodedHeight(){return this.internalTrack.info.height}getSquarePixelWidth(){return this.internalTrack.info.squarePixelWidth}getSquarePixelHeight(){return this.internalTrack.info.squarePixelHeight}getRotation(){return this.internalTrack.rotation}async getColorSpace(){return{primaries:this.internalTrack.info.colorSpace?.primaries,transfer:this.internalTrack.info.colorSpace?.transfer,matrix:this.internalTrack.info.colorSpace?.matrix,fullRange:this.internalTrack.info.colorSpace?.fullRange}}async canBeTransparent(){return!1}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfigPromise??=(async()=>{if(this.internalTrack.info.codec==="vp9"&&!this.internalTrack.info.vp9CodecInfo){let r=await this.getFirstPacket({});this.internalTrack.info.vp9CodecInfo=r&&ia(r.data)}else if(this.internalTrack.info.codec==="av1"&&!this.internalTrack.info.av1CodecInfo){let r=await this.getFirstPacket({});this.internalTrack.info.av1CodecInfo=r&&na(r.data)}let e={codec:ii(this.internalTrack.info),codedWidth:this.internalTrack.info.width,codedHeight:this.internalTrack.info.height,description:this.internalTrack.info.codecDescription??void 0,colorSpace:this.internalTrack.info.colorSpace??void 0};return(this.internalTrack.info.width!==this.internalTrack.info.squarePixelWidth||this.internalTrack.info.height!==this.internalTrack.info.squarePixelHeight)&&(e.displayAspectWidth=this.internalTrack.info.squarePixelWidth,e.displayAspectHeight=this.internalTrack.info.squarePixelHeight),e})():null}},ya=class extends ga{constructor(e){super(e);this.decoderConfig=null;this.internalTrack=e}getType(){return"audio"}getCodec(){return this.internalTrack.info.codec}getNumberOfChannels(){return this.internalTrack.info.numberOfChannels}getSampleRate(){return this.internalTrack.info.sampleRate}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfig??={codec:ni(this.internalTrack.info),numberOfChannels:this.internalTrack.info.numberOfChannels,sampleRate:this.internalTrack.info.sampleRate,description:this.internalTrack.info.codecDescription??void 0}:null}},So=(n,t)=>{if(n.presentationTimestamps){let e=H(n.presentationTimestamps,t,r=>r.presentationTimestamp);return e===-1?-1:n.presentationTimestamps[e].sampleIndex}else{let e=H(n.sampleTimingEntries,t,i=>i.startDecodeTimestamp);if(e===-1)return-1;let r=n.sampleTimingEntries[e];return r.startIndex+Math.min(Math.floor((t-r.startDecodeTimestamp)/r.delta),r.count-1)}},wm=(n,t)=>{if(!n.keySampleIndices)return So(n,t);if(n.presentationTimestamps){let e=H(n.presentationTimestamps,t,r=>r.presentationTimestamp);if(e===-1)return-1;for(let r=e;r>=0;r--){let i=n.presentationTimestamps[r].sampleIndex;if(Ft(n.keySampleIndices,i,s=>s)!==-1)return i}return-1}else{let e=So(n,t),r=H(n.keySampleIndices,e,i=>i);return n.keySampleIndices[r]??-1}},Sm=(n,t)=>{let e=H(n.sampleTimingEntries,t,y=>y.startIndex),r=n.sampleTimingEntries[e];if(!r||r.startIndex+r.count<=t)return null;let a=r.startDecodeTimestamp+(t-r.startIndex)*r.delta,s=H(n.sampleCompositionTimeOffsets,t,y=>y.startIndex),o=n.sampleCompositionTimeOffsets[s];o&&t-o.startIndex<o.count&&(a+=o.offset);let c=n.sampleSizes[Math.min(t,n.sampleSizes.length-1)],u=H(n.sampleToChunk,t,y=>y.startSampleIndex),l=n.sampleToChunk[u];h(l);let m=l.startChunkIndex+Math.floor((t-l.startSampleIndex)/l.samplesPerChunk),d=n.chunkOffsets[m],f=l.startSampleIndex+(m-l.startChunkIndex)*l.samplesPerChunk,p=0,b=d;if(n.sampleSizes.length===1)b+=c*(t-f),p+=c*l.samplesPerChunk;else for(let y=f;y<f+l.samplesPerChunk;y++){let k=n.sampleSizes[y];y<t&&(b+=k),p+=k}let g=r.delta;if(n.presentationTimestamps){let y=n.presentationTimestampIndexMap[t];h(y!==void 0),y<n.presentationTimestamps.length-1&&(g=n.presentationTimestamps[y+1].presentationTimestamp-a)}return{presentationTimestamp:a,duration:g,sampleOffset:b,sampleSize:c,chunkOffset:d,chunkSize:p,isKeyFrame:n.keySampleIndices?Ft(n.keySampleIndices,t,y=>y)!==-1:!0}},Am=(n,t)=>{if(!n.keySampleIndices)return t+1;let e=H(n.keySampleIndices,t,r=>r);return n.keySampleIndices[e+1]??-1},wo=(n,t)=>{n.startTimestamp+=t,n.endTimestamp+=t;for(let e of n.samples)e.presentationTimestamp+=t;for(let e of n.presentationTimestamps)e.presentationTimestamp+=t},xm=n=>{let[t,e]=n,r=Math.atan2(e,t);return Number.isFinite(r)?r*(180/Math.PI):0},ol=n=>n.sampleSizes.length===0,cl=n=>n.currentFragmentState?n.currentFragmentState.encryptionAuxInfo??={defaultSampleInfoSize:0,sampleSizes:null,sampleCount:0,offset:null,resolved:null}:n.encryptionAuxInfo??={defaultSampleInfoSize:0,sampleSizes:null,sampleCount:0,offset:null,resolved:null},dl=async(n,t,e)=>{if(e.resolved)return e.resolved;if(e.offset===null||e.sampleCount===0)throw new Error("Incomplete saiz/saio info; cannot resolve encryption data.");let r=0;if(e.defaultSampleInfoSize>0)r=e.defaultSampleInfoSize*e.sampleCount;else{h(e.sampleSizes);for(let o=0;o<e.sampleCount;o++)r+=e.sampleSizes[o]}let i=n.requestSlice(e.offset,r);if(i instanceof Promise&&(i=await i),!i)throw new Error("Failed to read auxiliary encryption info.");let a=t.defaultPerSampleIvSize;h(a!==null);let s=[];for(let o=0;o<e.sampleCount;o++){let c=e.defaultSampleInfoSize>0?e.defaultSampleInfoSize:e.sampleSizes[o],u=new Uint8Array(16);a>0?u.set(B(i,a),0):u.set(t.defaultConstantIv,0);let l=null;if(c>a){let m=se(i);l=[];for(let d=0;d<m;d++){let f=se(i),p=F(i);l.push({clearLen:f,protectedLen:p})}}s.push({iv:u,subsamples:l})}return e.resolved=s,s},ul=async(n,t,e,r)=>{h(n.encryptionInfo);let i=n.encryptionInfo;h(i.defaultKid!==null);let a=i.defaultKid,s,o=n.demuxer.decryptionKeyCache.get(a);if(o)s=await o;else{if(!n.demuxer.input._formatOptions.isobmff?.resolveKeyId)throw new Error("Encrypted media samples encountered. To decrypt them, please provide a callback for InputOptions.formatOptions.isobmff.resolveKeyId.");let c=(async()=>{let u=n.demuxer.psshBoxes;if(r){u=[...u,...r.psshBoxes].filter(m=>m.keyIds===null||m.keyIds.includes(a));for(let m=0;m<u.length-1;m++)for(let d=m+1;d<u.length;d++)ma(u[m],u[d])&&(u.splice(d,1),d--)}let l=await n.demuxer.input._formatOptions.isobmff.resolveKeyId({keyId:a,psshBoxes:u});if(!(typeof l=="string"&&l.length===32&&mu.test(l)||l instanceof Uint8Array&&l.byteLength===16))throw new TypeError("resolveKeyId must return a 32-character hex string or a 16-byte Uint8Array containing the decryption key.");return l instanceof Uint8Array?l:fu(l)})();n.demuxer.decryptionKeyCache.set(a,c),s=await c}return i.scheme==="cenc"||i.scheme==="cens"?Pm(s,i,t,e):Cm(s,i,t,e)},Pm=async(n,t,e,r)=>{let i=new Uint8Array(16);i.set(e.iv,0);let a=await crypto.subtle.importKey("raw",n,{name:"AES-CTR"},!1,["decrypt"]),s=async p=>{let b=await crypto.subtle.decrypt({name:"AES-CTR",counter:i,length:64},a,p);return new Uint8Array(b)};if(!e.subsamples)return s(r);h(t.defaultCryptByteBlock!==null&&t.defaultSkipByteBlock!==null);let o=ml(e.subsamples,t.defaultCryptByteBlock,t.defaultSkipByteBlock),c=0;for(let p of o)for(let b of p.perSubsample)c+=b.length;let u=new Uint8Array(c),l=0;for(let p of o)for(let b of p.perSubsample)u.set(r.subarray(b.offset,b.offset+b.length),l),l+=b.length;let m=await s(u),d=new Uint8Array(r),f=0;for(let p of o)for(let b of p.perSubsample)d.set(m.subarray(f,f+b.length),b.offset),f+=b.length;return d},Cm=(n,t,e,r)=>{let i=new cn;i.init({key:n,iv:e.iv});let a=t.defaultCryptByteBlock,s=t.defaultSkipByteBlock;if(h(a!==null&&s!==null),!e.subsamples){let l=new Uint8Array(r),m=Math.floor(r.length/16);for(let d=0;d<m;d++){let f=d*16;i.in.set(r.subarray(f,f+16)),i.decrypt(),l.set(i.out,f)}return l}if(a===0&&s===0)throw new Error("cbcs with subsamples requires pattern encryption.");let o=new Uint8Array(r),c=ml(e.subsamples,a,s),u=new DataView(e.iv.buffer,e.iv.byteOffset,16);for(let l of c){i.iv[0]=u.getUint32(0,!1),i.iv[1]=u.getUint32(4,!1),i.iv[2]=u.getUint32(8,!1),i.iv[3]=u.getUint32(12,!1);for(let m of l.perSubsample){let d=m.length/16;for(let f=0;f<d;f++){let p=m.offset+f*16;i.in.set(r.subarray(p,p+16)),i.decrypt(),o.set(i.out,p)}}}return o},ml=(n,t,e)=>{let r=[],i=t!==0||e!==0,a=0;for(let s of n){a+=s.clearLen;let o=[];if(!i)s.protectedLen>0&&o.push({offset:a,length:s.protectedLen}),a+=s.protectedLen;else{let c=s.protectedLen,u=a;for(;c>0&&!(c<16*t);){let l=16*t;o.push({offset:u,length:l}),u+=l,c-=l;let m=Math.min(16*e,c);u+=m,c-=m}a+=s.protectedLen}r.push({perSubsample:o})}return r};var mi=class{constructor(t){this.value=t}},fi=class{constructor(t){this.value=t}},ln=class{constructor(t){this.value=t}},lt=class{constructor(t){this.value=t}};var Im=[440786851,408125543],pi=[290298740,357149030,524531317,374648427,475249515,423732329,272869232,307544935],dn=[...Im,...pi],pl=n=>n<256?1:n<65536?2:n<1<<24?3:n<2**32?4:n<2**40?5:6,hl=n=>n<1n<<8n?1:n<1n<<16n?2:n<1n<<24n?3:n<1n<<32n?4:n<1n<<40n?5:n<1n<<48n?6:n<1n<<56n?7:8,gl=n=>n>=-64&&n<64?1:n>=-8192&&n<8192?2:n>=-(1<<20)&&n<1<<20?3:n>=-(1<<27)&&n<1<<27?4:n>=-(2**34)&&n<2**34?5:6,Em=n=>{if(n<127)return 1;if(n<16383)return 2;if(n<(1<<21)-1)return 3;if(n<(1<<28)-1)return 4;if(n<2**35-1)return 5;if(n<2**42-1)return 6;throw new Error("EBML varint size not supported "+n)},Ta=class{constructor(t){this.writer=t;this.helper=new Uint8Array(8);this.helperView=new DataView(this.helper.buffer);this.offsets=new WeakMap;this.dataOffsets=new WeakMap}writeByte(t){this.helperView.setUint8(0,t),this.writer.write(this.helper.subarray(0,1))}writeFloat32(t){this.helperView.setFloat32(0,t,!1),this.writer.write(this.helper.subarray(0,4))}writeFloat64(t){this.helperView.setFloat64(0,t,!1),this.writer.write(this.helper)}writeUnsignedInt(t,e=pl(t)){let r=0;switch(e){case 6:this.helperView.setUint8(r++,t/2**40|0);case 5:this.helperView.setUint8(r++,t/2**32|0);case 4:this.helperView.setUint8(r++,t>>24);case 3:this.helperView.setUint8(r++,t>>16);case 2:this.helperView.setUint8(r++,t>>8);case 1:this.helperView.setUint8(r++,t);break;default:throw new Error("Bad unsigned int size "+e)}this.writer.write(this.helper.subarray(0,r))}writeUnsignedBigInt(t,e=hl(t)){let r=0;for(let i=e-1;i>=0;i--)this.helperView.setUint8(r++,Number(t>>BigInt(i*8)&0xffn));this.writer.write(this.helper.subarray(0,r))}writeSignedInt(t,e=gl(t)){t<0&&(t+=2**(e*8)),this.writeUnsignedInt(t,e)}writeVarInt(t,e=Em(t)){let r=0;switch(e){case 1:this.helperView.setUint8(r++,128|t);break;case 2:this.helperView.setUint8(r++,64|t>>8),this.helperView.setUint8(r++,t);break;case 3:this.helperView.setUint8(r++,32|t>>16),this.helperView.setUint8(r++,t>>8),this.helperView.setUint8(r++,t);break;case 4:this.helperView.setUint8(r++,16|t>>24),this.helperView.setUint8(r++,t>>16),this.helperView.setUint8(r++,t>>8),this.helperView.setUint8(r++,t);break;case 5:this.helperView.setUint8(r++,8|t/2**32&7),this.helperView.setUint8(r++,t>>24),this.helperView.setUint8(r++,t>>16),this.helperView.setUint8(r++,t>>8),this.helperView.setUint8(r++,t);break;case 6:this.helperView.setUint8(r++,4|t/2**40&3),this.helperView.setUint8(r++,t/2**32|0),this.helperView.setUint8(r++,t>>24),this.helperView.setUint8(r++,t>>16),this.helperView.setUint8(r++,t>>8),this.helperView.setUint8(r++,t);break;default:throw new Error("Bad EBML varint size "+e)}this.writer.write(this.helper.subarray(0,r))}writeAsciiString(t){this.writer.write(new Uint8Array(t.split("").map(e=>e.charCodeAt(0))))}writeEBML(t){if(t!==null)if(t instanceof Uint8Array)this.writer.write(t);else if(Array.isArray(t))for(let e of t)this.writeEBML(e);else if(this.offsets.set(t,this.writer.getPos()),this.writeUnsignedInt(t.id),Array.isArray(t.data)){let e=this.writer.getPos(),r=t.size===-1?1:t.size??4;t.size===-1?this.writeByte(255):this.writer.seek(this.writer.getPos()+r);let i=this.writer.getPos();if(this.dataOffsets.set(t,i),this.writeEBML(t.data),t.size!==-1){let a=this.writer.getPos()-i,s=this.writer.getPos();this.writer.seek(e),this.writeVarInt(a,r),this.writer.seek(s)}}else if(typeof t.data=="number"){let e=t.size??pl(t.data);this.writeVarInt(e),this.writeUnsignedInt(t.data,e)}else if(typeof t.data=="bigint"){let e=t.size??hl(t.data);this.writeVarInt(e),this.writeUnsignedBigInt(t.data,e)}else if(typeof t.data=="string")this.writeVarInt(t.data.length),this.writeAsciiString(t.data);else if(t.data instanceof Uint8Array)this.writeVarInt(t.data.byteLength,t.size),this.writer.write(t.data);else if(t.data instanceof mi)this.writeVarInt(4),this.writeFloat32(t.data.value);else if(t.data instanceof fi)this.writeVarInt(8),this.writeFloat64(t.data.value);else if(t.data instanceof ln){let e=t.size??gl(t.data.value);this.writeVarInt(e),this.writeSignedInt(t.data.value,e)}else if(t.data instanceof lt){let e=$.encode(t.data.value);this.writeVarInt(e.length),this.writer.write(e)}else ne(t.data)}},Ao=8,Oe=2,dt=2*Ao,xo=n=>{if(n.remainingLength<1)return null;let t=O(n);if(n.skip(-1),t===0)return null;let e=1,r=128;for(;(t&r)===0;)e++,r>>=1;return n.remainingLength<e?null:e},hi=n=>{if(n.remainingLength<1)return null;let t=O(n);if(t===0)return null;let e=1,r=128;for(;(t&r)===0;)e++,r>>=1;if(n.remainingLength<e-1)return null;let i=t&r-1;for(let a=1;a<e;a++)i*=256,i+=O(n);return i},G=(n,t)=>{if(t<1||t>8)throw new Error("Bad unsigned int size "+t);let e=0;for(let r=0;r<t;r++)e*=256,e+=O(n);return e},bl=(n,t)=>{if(t<1)throw new Error("Bad unsigned int size "+t);let e=0n;for(let r=0;r<t;r++)e<<=8n,e+=BigInt(O(n));return e};var wa=n=>{let t=xo(n);return t===null||n.remainingLength<t?null:G(n,t)},Po=n=>{if(n.remainingLength<1)return null;if(O(n)===255)return;n.skip(-1);let e=hi(n);if(e===null)return null;if(e!==72057594037927940)return e},mt=n=>{h(n.remainingLength>=Oe);let t=wa(n);if(t===null)return null;let e=Po(n);return e===null?null:{id:t,size:e}},fr=(n,t)=>{let e=B(n,t),r=0;for(;r<t&&e[r]!==0;)r+=1;return String.fromCharCode(...e.subarray(0,r))},gi=(n,t)=>{let e=B(n,t),r=0;for(;r<t&&e[r]!==0;)r+=1;return me.decode(e.subarray(0,r))},Sa=(n,t)=>{if(t===0)return 0;if(t!==4&&t!==8)throw new Error("Bad float size "+t);return t===4?yl(n):ka(n)},Aa=async(n,t,e,r)=>{let i=new Set(e),a=t;for(;r===null||a<r;){let s=n.requestSliceRange(a,Oe,dt);if(s instanceof Promise&&(s=await s),!s)break;let o=mt(s);if(!o)break;if(i.has(o.id))return{pos:a,found:!0};qt(o.size),a=s.filePos+o.size}return{pos:r!==null&&r>a?r:a,found:!1}},Co=async(n,t,e,r)=>{let a=new Set(e),s=t;for(;s<r;){let o=n.requestSliceRange(s,0,Math.min(65536,r-s));if(o instanceof Promise&&(o=await o),!o||o.length<Ao)break;for(let c=0;c<o.length-Ao;c++){o.filePos=s;let u=wa(o);if(u!==null&&a.has(u))return s;s++}}return null},Fe={avc:"V_MPEG4/ISO/AVC",hevc:"V_MPEGH/ISO/HEVC",vp8:"V_VP8",vp9:"V_VP9",av1:"V_AV1",aac:"A_AAC",mp3:"A_MPEG/L3",opus:"A_OPUS",vorbis:"A_VORBIS",flac:"A_FLAC",ac3:"A_AC3",eac3:"A_EAC3","pcm-u8":"A_PCM/INT/LIT","pcm-s16":"A_PCM/INT/LIT","pcm-s16be":"A_PCM/INT/BIG","pcm-s24":"A_PCM/INT/LIT","pcm-s24be":"A_PCM/INT/BIG","pcm-s32":"A_PCM/INT/LIT","pcm-s32be":"A_PCM/INT/BIG","pcm-f32":"A_PCM/FLOAT/IEEE","pcm-f64":"A_PCM/FLOAT/IEEE",webvtt:"S_TEXT/WEBVTT"};function qt(n){if(n===void 0)throw new Error("Undefined element size is used in a place where it is not supported.")}var xa=n=>{let e=(n.hasVideo?"video/":n.hasAudio?"audio/":"application/")+(n.isWebM?"webm":"x-matroska");if(n.codecStrings.length>0){let r=[...new Set(n.codecStrings.filter(Boolean))];e+=`; codecs="${r.join(", ")}"`}return e};var Io=[{id:290298740,flag:"seekHeadSeen"},{id:357149030,flag:"infoSeen"},{id:374648427,flag:"tracksSeen"},{id:475249515,flag:"cuesSeen"}],Tl=10*2**20,Pa=class extends pe{constructor(e){super(e);this.readMetadataPromise=null;this.segments=[];this.currentSegment=null;this.currentTrack=null;this.currentCluster=null;this.currentBlock=null;this.currentBlockAdditional=null;this.currentCueTime=null;this.currentDecodingInstruction=null;this.currentTagTargetIsMovie=!0;this.currentSimpleTagName=null;this.currentAttachedFile=null;this.isWebM=!1;this.reader=e._reader}async getTrackBackings(){return await this.readMetadata(),this.segments.flatMap(e=>e.tracks.map(r=>r.trackBacking))}async getMimeType(){await this.readMetadata();let e=await this.getTrackBackings(),r=await Promise.all(e.map(i=>i.getDecoderConfig().then(a=>a?.codec??null)));return xa({isWebM:this.isWebM,hasVideo:this.segments.some(i=>i.tracks.some(a=>a.info?.type==="video")),hasAudio:this.segments.some(i=>i.tracks.some(a=>a.info?.type==="audio")),codecStrings:r.filter(Boolean)})}async getMetadataTags(){await this.readMetadata();for(let r of this.segments)r.metadataTagsCollected||(this.reader.fileSize!==null&&await this.loadSegmentMetadata(r),r.metadataTagsCollected=!0);let e={};for(let r of this.segments)e={...e,...r.metadataTags};return e}readMetadata(){return this.readMetadataPromise??=(async()=>{let e=0;for(;;){let r=this.reader.requestSliceRange(e,Oe,dt);if(r instanceof Promise&&(r=await r),!r)break;let i=mt(r);if(!i)break;let a=i.id,s=i.size,o=r.filePos;if(a===440786851){qt(s);let c=this.reader.requestSlice(o,s);if(c instanceof Promise&&(c=await c),!c)break;this.readContiguousElements(c)}else if(a===408125543){if(await this.readSegment(o,s),s===void 0||this.reader.fileSize===null)break}else if(a===524531317){if(this.reader.fileSize===null)break;s===void 0&&(s=(await Aa(this.reader,o,dn,this.reader.fileSize)).pos-o);let c=X(this.segments);c&&(c.elementEndPos=o+s)}qt(s),e=o+s}})()}async readSegment(e,r){this.currentSegment={seekHeadSeen:!1,infoSeen:!1,tracksSeen:!1,cuesSeen:!1,tagsSeen:!1,attachmentsSeen:!1,timestampScale:-1,timestampFactor:-1,duration:-1,seekEntries:[],tracks:[],cuePoints:[],dataStartPos:e,elementEndPos:r===void 0?null:e+r,clusterSeekStartPos:e,lastReadCluster:null,metadataTags:{},metadataTagsCollected:!1},this.segments.push(this.currentSegment);let i=e;for(;this.currentSegment.elementEndPos===null||i<this.currentSegment.elementEndPos;){let c=this.reader.requestSliceRange(i,Oe,dt);if(c instanceof Promise&&(c=await c),!c)break;let u=i,l=mt(c);if(!l||!pi.includes(l.id)&&l.id!==236){let b=await Co(this.reader,u,pi,Math.min(this.currentSegment.elementEndPos??1/0,u+Tl));if(b){i=b;continue}else break}let{id:m,size:d}=l,f=c.filePos,p=Io.findIndex(b=>b.id===m);if(p!==-1){let b=Io[p].flag;this.currentSegment[b]=!0,qt(d);let g=this.reader.requestSlice(f,d);g instanceof Promise&&(g=await g),g&&this.readContiguousElements(g)}else if(m===307544935||m===423732329){m===307544935?this.currentSegment.tagsSeen=!0:this.currentSegment.attachmentsSeen=!0,qt(d);let b=this.reader.requestSlice(f,d);b instanceof Promise&&(b=await b),b&&this.readContiguousElements(b)}else if(m===524531317){this.currentSegment.clusterSeekStartPos=u;break}if(d===void 0)break;i=f+d}if(this.currentSegment.seekEntries.sort((c,u)=>c.segmentPosition-u.segmentPosition),this.reader.fileSize!==null)for(let c of this.currentSegment.seekEntries){let u=Io.find(b=>b.id===c.id);if(!u||this.currentSegment[u.flag])continue;let l=this.reader.requestSliceRange(e+c.segmentPosition,Oe,dt);if(l instanceof Promise&&(l=await l),!l)continue;let m=mt(l);if(!m)continue;let{id:d,size:f}=m;if(d!==u.id)continue;qt(f),this.currentSegment[u.flag]=!0;let p=this.reader.requestSlice(l.filePos,f);p instanceof Promise&&(p=await p),p&&this.readContiguousElements(p)}this.currentSegment.timestampScale===-1&&(this.currentSegment.timestampScale=1e6,this.currentSegment.timestampFactor=1e9/1e6);for(let c of this.currentSegment.tracks)c.defaultDurationNs!==null&&(c.defaultDuration=this.currentSegment.timestampFactor*c.defaultDurationNs/1e9);let a=new Map(this.currentSegment.tracks.map(c=>[c.id,c]));for(let c of this.currentSegment.cuePoints){let u=a.get(c.trackId);u&&u.cuePoints.push(c)}for(let c of this.currentSegment.tracks){c.cuePoints.sort((u,l)=>u.time-l.time);for(let u=0;u<c.cuePoints.length-1;u++){let l=c.cuePoints[u],m=c.cuePoints[u+1];l.time===m.time&&(c.cuePoints.splice(u+1,1),u--)}}let s=null,o=-1/0;for(let c of this.currentSegment.tracks)c.cuePoints.length>o&&(o=c.cuePoints.length,s=c);for(let c of this.currentSegment.tracks)c.cuePoints.length===0&&(c.cuePoints=s.cuePoints);this.currentSegment=null}async readCluster(e,r){if(r.lastReadCluster?.elementStartPos===e)return r.lastReadCluster;let i=this.reader.requestSliceRange(e,Oe,dt);i instanceof Promise&&(i=await i),h(i);let a=e,s=mt(i);h(s);let o=s.id;h(o===524531317);let c=s.size,u=i.filePos;c===void 0&&(c=(await Aa(this.reader,u,dn,r.elementEndPos)).pos-u);let l=this.reader.requestSlice(u,c);l instanceof Promise&&(l=await l);let m={segment:r,elementStartPos:a,elementEndPos:u+c,dataStartPos:u,timestamp:-1,trackData:new Map};if(this.currentCluster=m,l){let d=this.readContiguousElements(l,dn);m.elementEndPos=d}for(let[,d]of m.trackData){let f=d.track;h(d.blocks.length>0);let p=!1;for(let k=0;k<d.blocks.length;k++){let w=d.blocks[k];w.timestamp+=m.timestamp,p||=w.lacing!==0}d.presentationTimestamps=d.blocks.map((k,w)=>({timestamp:k.timestamp,blockIndex:w})).sort((k,w)=>k.timestamp-w.timestamp);for(let k=0;k<d.presentationTimestamps.length;k++){let w=d.presentationTimestamps[k],T=d.blocks[w.blockIndex];if(d.firstKeyFrameTimestamp===null&&T.isKeyFrame&&(d.firstKeyFrameTimestamp=T.timestamp),k<d.presentationTimestamps.length-1){let S=d.presentationTimestamps[k+1];T.duration=S.timestamp-T.timestamp}else T.duration===0&&f.defaultDuration!=null&&T.lacing===0&&(T.duration=f.defaultDuration)}p&&(this.expandLacedBlocks(d.blocks,f),d.presentationTimestamps=d.blocks.map((k,w)=>({timestamp:k.timestamp,blockIndex:w})).sort((k,w)=>k.timestamp-w.timestamp));let b=d.blocks[d.presentationTimestamps[0].blockIndex],g=d.blocks[X(d.presentationTimestamps).blockIndex];d.startTimestamp=b.timestamp,d.endTimestamp=g.timestamp+g.duration;let y=H(f.clusterPositionCache,d.startTimestamp,k=>k.startTimestamp);(y===-1||f.clusterPositionCache[y].elementStartPos!==a)&&f.clusterPositionCache.splice(y+1,0,{elementStartPos:m.elementStartPos,startTimestamp:d.startTimestamp})}return r.lastReadCluster=m,m}getTrackDataInCluster(e,r){let i=e.trackData.get(r);if(!i){let a=e.segment.tracks.find(s=>s.id===r);if(!a)return null;i={track:a,startTimestamp:0,endTimestamp:0,firstKeyFrameTimestamp:null,blocks:[],presentationTimestamps:[]},e.trackData.set(r,i)}return i}expandLacedBlocks(e,r){for(let i=0;i<e.length;i++){let a=e[i];if(a.lacing===0)continue;a.decoded||(a.data=this.decodeBlockData(r,a.data),a.decoded=!0);let s=he.tempFromBytes(a.data),o=[],c=O(s)+1;switch(a.lacing){case 1:{let l=0;for(let m=0;m<c-1;m++){let d=0;for(;s.bufferPos<s.length;){let f=O(s);if(d+=f,f<255){o.push(d),l+=d;break}}}o.push(s.length-(s.bufferPos+l))}break;case 2:{let l=s.length-1,m=Math.floor(l/c);for(let d=0;d<c;d++)o.push(m)}break;case 3:{let l=hi(s);h(l!==null);let m=l;o.push(m);let d=m;for(let f=1;f<c-1;f++){let p=s.bufferPos,b=hi(s);h(b!==null);let g=b,k=(1<<(s.bufferPos-p)*7-1)-1,w=g-k;m+=w,o.push(m),d+=m}o.push(s.length-(s.bufferPos+d))}break;default:h(!1)}h(o.length===c),e.splice(i,1);let u=a.duration||c*(r.defaultDuration??0);for(let l=0;l<c;l++){let m=o[l],d=B(s,m),f=a.timestamp+u*l/c,p=u/c;e.splice(i+l,0,{timestamp:f,duration:p,isKeyFrame:a.isKeyFrame,data:d,lacing:0,decoded:!0,mainAdditional:a.mainAdditional})}i+=c,i--}}async loadSegmentMetadata(e){for(let r of e.seekEntries){if(!(r.id===307544935&&!e.tagsSeen)){if(!(r.id===423732329&&!e.attachmentsSeen))continue}let i=this.reader.requestSliceRange(e.dataStartPos+r.segmentPosition,Oe,dt);if(i instanceof Promise&&(i=await i),!i)continue;let a=mt(i);if(!a||a.id!==r.id)continue;let{size:s}=a;qt(s),h(!this.currentSegment),this.currentSegment=e;let o=this.reader.requestSlice(i.filePos,s);o instanceof Promise&&(o=await o),o&&this.readContiguousElements(o),this.currentSegment=null,r.id===307544935?e.tagsSeen=!0:r.id===423732329&&(e.attachmentsSeen=!0)}}readContiguousElements(e,r){for(;e.remainingLength>=Oe;){let i=e.filePos;if(!this.traverseElement(e,r))return i}return e.filePos}traverseElement(e,r){let i=mt(e);if(!i||r&&r.includes(i.id))return!1;let{id:a,size:s}=i,o=e.filePos;switch(qt(s),a){case 17026:this.isWebM=fr(e,s)==="webm";break;case 19899:{if(!this.currentSegment)break;let c={id:-1,segmentPosition:-1};this.currentSegment.seekEntries.push(c),this.readContiguousElements(e.slice(o,s)),(c.id===-1||c.segmentPosition===-1)&&this.currentSegment.seekEntries.pop()}break;case 21419:{let c=this.currentSegment?.seekEntries[this.currentSegment.seekEntries.length-1];if(!c)break;c.id=G(e,s)}break;case 21420:{let c=this.currentSegment?.seekEntries[this.currentSegment.seekEntries.length-1];if(!c)break;c.segmentPosition=G(e,s)}break;case 2807729:{if(!this.currentSegment)break;this.currentSegment.timestampScale=G(e,s),this.currentSegment.timestampFactor=1e9/this.currentSegment.timestampScale}break;case 17545:{if(!this.currentSegment)break;this.currentSegment.duration=Sa(e,s)}break;case 174:{if(!this.currentSegment||(this.currentTrack={id:-1,segment:this.currentSegment,demuxer:this,clusterPositionCache:[],cuePoints:[],disposition:{...we,primary:!1},trackBacking:null,codecId:null,codecPrivate:null,defaultDuration:null,defaultDurationNs:null,name:null,languageCode:"eng",hasLanguageBcp47:!1,decodingInstructions:[],info:null},this.readContiguousElements(e.slice(o,s)),!this.currentTrack))break;if(this.currentTrack.decodingInstructions.some(c=>c.data?.type!=="decompress"||c.scope!==1||c.data.algorithm!==3)&&(console.warn(`Track #${this.currentTrack.id} has an unsupported content encoding; dropping.`),this.currentTrack=null),this.currentTrack&&this.currentTrack.id!==-1&&this.currentTrack.codecId&&this.currentTrack.info){let c=this.currentTrack.codecId.indexOf("/"),u=c===-1?this.currentTrack.codecId:this.currentTrack.codecId.slice(0,c);if(this.currentTrack.info.type==="video"&&this.currentTrack.info.width!==-1&&this.currentTrack.info.height!==-1){if(this.currentTrack.info.squarePixelWidth=this.currentTrack.info.width,this.currentTrack.info.squarePixelHeight=this.currentTrack.info.height,this.currentTrack.info.displayWidth!==null&&this.currentTrack.info.displayHeight!==null){let m=this.currentTrack.info.displayWidth*this.currentTrack.info.height,d=this.currentTrack.info.displayHeight*this.currentTrack.info.width;m>0&&d>0&&(m>d?this.currentTrack.info.squarePixelWidth=Math.round(this.currentTrack.info.width*m/d):this.currentTrack.info.squarePixelHeight=Math.round(this.currentTrack.info.height*d/m))}this.currentTrack.codecId===Fe.avc?(this.currentTrack.info.codec="avc",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):this.currentTrack.codecId===Fe.hevc?(this.currentTrack.info.codec="hevc",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):u===Fe.vp8?this.currentTrack.info.codec="vp8":u===Fe.vp9?this.currentTrack.info.codec="vp9":u===Fe.av1&&(this.currentTrack.info.codec="av1");let l=this.currentTrack;this.currentTrack.trackBacking=new Eo(l),this.currentSegment.tracks.push(this.currentTrack)}else if(this.currentTrack.info.type==="audio"){u===Fe.aac?(this.currentTrack.info.codec="aac",this.currentTrack.info.aacCodecInfo={isMpeg2:this.currentTrack.codecId.includes("MPEG2"),objectType:null},this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):this.currentTrack.codecId===Fe.mp3?this.currentTrack.info.codec="mp3":u===Fe.opus?(this.currentTrack.info.codec="opus",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate,this.currentTrack.info.sampleRate=nt):u===Fe.vorbis?(this.currentTrack.info.codec="vorbis",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):u===Fe.flac?(this.currentTrack.info.codec="flac",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):u===Fe.ac3?(this.currentTrack.info.codec="ac3",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):u===Fe.eac3?(this.currentTrack.info.codec="eac3",this.currentTrack.info.codecDescription=this.currentTrack.codecPrivate):this.currentTrack.codecId==="A_PCM/INT/LIT"?this.currentTrack.info.bitDepth===8?this.currentTrack.info.codec="pcm-u8":this.currentTrack.info.bitDepth===16?this.currentTrack.info.codec="pcm-s16":this.currentTrack.info.bitDepth===24?this.currentTrack.info.codec="pcm-s24":this.currentTrack.info.bitDepth===32&&(this.currentTrack.info.codec="pcm-s32"):this.currentTrack.codecId==="A_PCM/INT/BIG"?this.currentTrack.info.bitDepth===8?this.currentTrack.info.codec="pcm-u8":this.currentTrack.info.bitDepth===16?this.currentTrack.info.codec="pcm-s16be":this.currentTrack.info.bitDepth===24?this.currentTrack.info.codec="pcm-s24be":this.currentTrack.info.bitDepth===32&&(this.currentTrack.info.codec="pcm-s32be"):this.currentTrack.codecId==="A_PCM/FLOAT/IEEE"&&(this.currentTrack.info.bitDepth===32?this.currentTrack.info.codec="pcm-f32":this.currentTrack.info.bitDepth===64&&(this.currentTrack.info.codec="pcm-f64"));let l=this.currentTrack;this.currentTrack.trackBacking=new vo(l),this.currentSegment.tracks.push(this.currentTrack)}}this.currentTrack=null}break;case 215:{if(!this.currentTrack)break;this.currentTrack.id=G(e,s)}break;case 131:{if(!this.currentTrack)break;let c=G(e,s);c===1?this.currentTrack.info={type:"video",width:-1,height:-1,displayWidth:null,displayHeight:null,displayUnit:null,squarePixelWidth:-1,squarePixelHeight:-1,rotation:0,codec:null,codecDescription:null,colorSpace:null,alphaMode:!1}:c===2&&(this.currentTrack.info={type:"audio",numberOfChannels:1,sampleRate:8e3,bitDepth:-1,codec:null,codecDescription:null,aacCodecInfo:null})}break;case 185:{if(!this.currentTrack)break;G(e,s)||(this.currentTrack=null)}break;case 136:{if(!this.currentTrack)break;this.currentTrack.disposition.default=!!G(e,s)}break;case 21930:{if(!this.currentTrack)break;this.currentTrack.disposition.forced=!!G(e,s)}break;case 21934:{if(!this.currentTrack)break;this.currentTrack.disposition.original=!!G(e,s)}break;case 21931:{if(!this.currentTrack)break;this.currentTrack.disposition.hearingImpaired=!!G(e,s)}break;case 21932:{if(!this.currentTrack)break;this.currentTrack.disposition.visuallyImpaired=!!G(e,s)}break;case 21935:{if(!this.currentTrack)break;this.currentTrack.disposition.commentary=!!G(e,s)}break;case 134:{if(!this.currentTrack)break;this.currentTrack.codecId=fr(e,s)}break;case 25506:{if(!this.currentTrack)break;this.currentTrack.codecPrivate=B(e,s)}break;case 2352003:{if(!this.currentTrack)break;this.currentTrack.defaultDurationNs=G(e,s)}break;case 21358:{if(!this.currentTrack)break;this.currentTrack.name=gi(e,s)}break;case 2274716:{if(!this.currentTrack||this.currentTrack.hasLanguageBcp47)break;this.currentTrack.languageCode=fr(e,s),Bt(this.currentTrack.languageCode)||(this.currentTrack.languageCode=J)}break;case 2274717:{if(!this.currentTrack)break;let u=fr(e,s).split("-")[0];u?this.currentTrack.languageCode=u:this.currentTrack.languageCode=J,this.currentTrack.hasLanguageBcp47=!0}break;case 224:{if(this.currentTrack?.info?.type!=="video")break;this.readContiguousElements(e.slice(o,s))}break;case 176:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.width=G(e,s)}break;case 186:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.height=G(e,s)}break;case 21680:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.displayWidth=G(e,s)}break;case 21690:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.displayHeight=G(e,s)}break;case 21682:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.displayUnit=G(e,s)}break;case 21440:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.alphaMode=G(e,s)===1}break;case 21936:{if(this.currentTrack?.info?.type!=="video")break;this.currentTrack.info.colorSpace={},this.readContiguousElements(e.slice(o,s))}break;case 21937:{if(this.currentTrack?.info?.type!=="video"||!this.currentTrack.info.colorSpace)break;let c=G(e,s),u=Cr[c]??null;this.currentTrack.info.colorSpace.matrix=u}break;case 21945:{if(this.currentTrack?.info?.type!=="video"||!this.currentTrack.info.colorSpace)break;this.currentTrack.info.colorSpace.fullRange=G(e,s)===2}break;case 21946:{if(this.currentTrack?.info?.type!=="video"||!this.currentTrack.info.colorSpace)break;let c=G(e,s),u=Pr[c]??null;this.currentTrack.info.colorSpace.transfer=u}break;case 21947:{if(this.currentTrack?.info?.type!=="video"||!this.currentTrack.info.colorSpace)break;let c=G(e,s),u=xr[c]??null;this.currentTrack.info.colorSpace.primaries=u}break;case 30320:{if(this.currentTrack?.info?.type!=="video")break;this.readContiguousElements(e.slice(o,s))}break;case 30325:{if(this.currentTrack?.info?.type!=="video")break;let u=-Sa(e,s);try{this.currentTrack.info.rotation=Ze(u)}catch{}}break;case 225:{if(this.currentTrack?.info?.type!=="audio")break;this.readContiguousElements(e.slice(o,s))}break;case 181:{if(this.currentTrack?.info?.type!=="audio")break;this.currentTrack.info.sampleRate=Sa(e,s)}break;case 159:{if(this.currentTrack?.info?.type!=="audio")break;this.currentTrack.info.numberOfChannels=G(e,s)}break;case 25188:{if(this.currentTrack?.info?.type!=="audio")break;this.currentTrack.info.bitDepth=G(e,s)}break;case 187:{if(!this.currentSegment)break;this.readContiguousElements(e.slice(o,s)),this.currentCueTime=null}break;case 179:this.currentCueTime=G(e,s);break;case 183:{if(this.currentCueTime===null)break;h(this.currentSegment);let c={time:this.currentCueTime,trackId:-1,clusterPosition:-1};this.currentSegment.cuePoints.push(c),this.readContiguousElements(e.slice(o,s)),(c.trackId===-1||c.clusterPosition===-1)&&this.currentSegment.cuePoints.pop()}break;case 247:{let c=this.currentSegment?.cuePoints[this.currentSegment.cuePoints.length-1];if(!c)break;c.trackId=G(e,s)}break;case 241:{let c=this.currentSegment?.cuePoints[this.currentSegment.cuePoints.length-1];if(!c)break;h(this.currentSegment),c.clusterPosition=this.currentSegment.dataStartPos+G(e,s)}break;case 231:{if(!this.currentCluster)break;this.currentCluster.timestamp=G(e,s)}break;case 163:{if(!this.currentCluster)break;let c=hi(e);if(c===null)break;let u=this.getTrackDataInCluster(this.currentCluster,c);if(!u)break;let l=un(e),m=O(e),d=m>>1&3,f=!!(m&128);u.track.info?.type==="audio"&&u.track.info.codec&&(f=!0);let p=B(e,s-(e.filePos-o)),b=u.track.decodingInstructions.length>0;u.blocks.push({timestamp:l,duration:0,isKeyFrame:f,data:p,lacing:d,decoded:!b,mainAdditional:null})}break;case 160:{if(!this.currentCluster)break;this.readContiguousElements(e.slice(o,s)),this.currentBlock=null}break;case 161:{if(!this.currentCluster)break;let c=hi(e);if(c===null)break;let u=this.getTrackDataInCluster(this.currentCluster,c);if(!u)break;let l=un(e),d=O(e)>>1&3,f=B(e,s-(e.filePos-o)),p=u.track.decodingInstructions.length>0;this.currentBlock={timestamp:l,duration:0,isKeyFrame:!0,data:f,lacing:d,decoded:!p,mainAdditional:null},u.blocks.push(this.currentBlock)}break;case 30113:this.readContiguousElements(e.slice(o,s));break;case 166:{if(!this.currentBlock)break;this.currentBlockAdditional={addId:1,data:null},this.readContiguousElements(e.slice(o,s)),this.currentBlockAdditional.data&&this.currentBlockAdditional.addId===1&&(this.currentBlock.mainAdditional=this.currentBlockAdditional.data),this.currentBlockAdditional=null}break;case 165:{if(!this.currentBlockAdditional)break;this.currentBlockAdditional.data=B(e,s)}break;case 238:{if(!this.currentBlockAdditional)break;this.currentBlockAdditional.addId=G(e,s)}break;case 155:{if(!this.currentBlock)break;this.currentBlock.duration=G(e,s)}break;case 251:{if(!this.currentBlock)break;this.currentBlock.isKeyFrame=!1}break;case 29555:this.currentTagTargetIsMovie=!0,this.readContiguousElements(e.slice(o,s));break;case 25536:this.readContiguousElements(e.slice(o,s));break;case 26826:G(e,s)!==50&&(this.currentTagTargetIsMovie=!1);break;case 25541:case 25545:case 25540:case 25542:this.currentTagTargetIsMovie=!1;break;case 26568:{if(!this.currentTagTargetIsMovie)break;this.currentSimpleTagName=null,this.readContiguousElements(e.slice(o,s))}break;case 17827:this.currentSimpleTagName=gi(e,s);break;case 17543:{if(!this.currentSimpleTagName)break;let c=gi(e,s);this.processTagValue(this.currentSimpleTagName,c)}break;case 17541:{if(!this.currentSimpleTagName)break;let c=B(e,s);this.processTagValue(this.currentSimpleTagName,c)}break;case 24999:{if(!this.currentSegment)break;this.currentAttachedFile={fileUid:null,fileName:null,fileMediaType:null,fileData:null,fileDescription:null},this.readContiguousElements(e.slice(o,s));let c=this.currentSegment.metadataTags;if(this.currentAttachedFile.fileUid&&this.currentAttachedFile.fileData&&(c.raw??={},c.raw[this.currentAttachedFile.fileUid.toString()]=new ar(this.currentAttachedFile.fileData,this.currentAttachedFile.fileMediaType??void 0,this.currentAttachedFile.fileName??void 0,this.currentAttachedFile.fileDescription??void 0)),this.currentAttachedFile.fileMediaType?.startsWith("image/")&&this.currentAttachedFile.fileData){let u=this.currentAttachedFile.fileName,l="unknown";if(u){let m=u.toLowerCase();m.startsWith("cover.")?l="coverFront":m.startsWith("back.")&&(l="coverBack")}c.images??=[],c.images.push({data:this.currentAttachedFile.fileData,mimeType:this.currentAttachedFile.fileMediaType,kind:l,name:this.currentAttachedFile.fileName??void 0,description:this.currentAttachedFile.fileDescription??void 0})}this.currentAttachedFile=null}break;case 18094:{if(!this.currentAttachedFile)break;this.currentAttachedFile.fileUid=bl(e,s)}break;case 18030:{if(!this.currentAttachedFile)break;this.currentAttachedFile.fileName=gi(e,s)}break;case 18016:{if(!this.currentAttachedFile)break;this.currentAttachedFile.fileMediaType=fr(e,s)}break;case 18012:{if(!this.currentAttachedFile)break;this.currentAttachedFile.fileData=B(e,s)}break;case 18046:{if(!this.currentAttachedFile)break;this.currentAttachedFile.fileDescription=gi(e,s)}break;case 28032:{if(!this.currentTrack)break;this.readContiguousElements(e.slice(o,s)),this.currentTrack.decodingInstructions.sort((c,u)=>u.order-c.order)}break;case 25152:this.currentDecodingInstruction={order:0,scope:1,data:null},this.readContiguousElements(e.slice(o,s)),this.currentDecodingInstruction.data&&this.currentTrack.decodingInstructions.push(this.currentDecodingInstruction),this.currentDecodingInstruction=null;break;case 20529:{if(!this.currentDecodingInstruction)break;this.currentDecodingInstruction.order=G(e,s)}break;case 20530:{if(!this.currentDecodingInstruction)break;this.currentDecodingInstruction.scope=G(e,s)}break;case 20532:{if(!this.currentDecodingInstruction)break;this.currentDecodingInstruction.data={type:"decompress",algorithm:0,settings:null},this.readContiguousElements(e.slice(o,s))}break;case 16980:{if(this.currentDecodingInstruction?.data?.type!=="decompress")break;this.currentDecodingInstruction.data.algorithm=G(e,s)}break;case 16981:{if(this.currentDecodingInstruction?.data?.type!=="decompress")break;this.currentDecodingInstruction.data.settings=B(e,s)}break;case 20533:{if(!this.currentDecodingInstruction)break;this.currentDecodingInstruction.data={type:"decrypt"}}break}return e.filePos=o+s,!0}decodeBlockData(e,r){h(e.decodingInstructions.length>0);let i=r;for(let a of e.decodingInstructions)switch(h(a.data),a.data.type){case"decompress":switch(a.data.algorithm){case 3:if(a.data.settings&&a.data.settings.length>0){let s=a.data.settings,o=new Uint8Array(s.length+i.length);o.set(s,0),o.set(i,s.length),i=o}break;default:}break;default:}return i}processTagValue(e,r){if(!this.currentSegment?.metadataTags)return;let i=this.currentSegment.metadataTags;if(i.raw??={},i.raw[e]??=r,typeof r=="string")switch(e.toLowerCase()){case"title":i.title??=r;break;case"description":i.description??=r;break;case"artist":i.artist??=r;break;case"album":i.album??=r;break;case"album_artist":i.albumArtist??=r;break;case"genre":i.genre??=r;break;case"comment":i.comment??=r;break;case"lyrics":i.lyrics??=r;break;case"date":{let a=new Date(r);Number.isNaN(a.getTime())||(i.date??=a)}break;case"track_number":case"part_number":{let a=r.split("/"),s=Number.parseInt(a[0],10),o=a[1]&&Number.parseInt(a[1],10);Number.isInteger(s)&&s>0&&(i.trackNumber??=s),o&&Number.isInteger(o)&&o>0&&(i.tracksTotal??=o)}break;case"disc_number":case"disc":{let a=r.split("/"),s=Number.parseInt(a[0],10),o=a[1]&&Number.parseInt(a[1],10);Number.isInteger(s)&&s>0&&(i.discNumber??=s),o&&Number.isInteger(o)&&o>0&&(i.discsTotal??=o)}break}}},Ca=class{constructor(t){this.internalTrack=t;this.packetToClusterLocation=new WeakMap}getId(){return this.internalTrack.id}getNumber(){let t=this.internalTrack.demuxer,e=this.internalTrack.trackBacking.getType(),r=0;for(let i of t.segments)for(let a of i.tracks)if(a.trackBacking.getType()===e&&r++,a===this.internalTrack)break;return r}getCodec(){throw new Error("Not implemented on base class.")}getInternalCodecId(){return this.internalTrack.codecId}getName(){return this.internalTrack.name}getLanguageCode(){return this.internalTrack.languageCode}getTimeResolution(){return this.internalTrack.segment.timestampFactor}isRelativeToUnixEpoch(){return!1}getDisposition(){return this.internalTrack.disposition}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){let t=this.internalTrack.segment;if(t.duration<=0)return null;let e=t.duration/t.timestampFactor,r=await this.getFirstPacket({metadataOnly:!0});return e+=r?.timestamp??0,e}async getLiveRefreshInterval(){return null}async getFirstPacket(t){return this.performClusterLookup(null,e=>e.trackData.get(this.internalTrack.id)?{blockIndex:0,correctBlockFound:!0}:{blockIndex:-1,correctBlockFound:!1},-1/0,1/0,t)}intoTimescale(t){return Mt(t*this.internalTrack.segment.timestampFactor)}async getPacket(t,e){let r=this.intoTimescale(t);return this.performClusterLookup(null,i=>{let a=i.trackData.get(this.internalTrack.id);if(!a)return{blockIndex:-1,correctBlockFound:!1};let s=H(a.presentationTimestamps,r,u=>u.timestamp),o=s!==-1?a.presentationTimestamps[s].blockIndex:-1,c=s!==-1&&r<a.endTimestamp;return{blockIndex:o,correctBlockFound:c}},r,r,e)}async getNextPacket(t,e){let r=this.packetToClusterLocation.get(t);if(r===void 0)throw new Error("Packet was not created from this track.");return this.performClusterLookup(r.cluster,i=>{if(i===r.cluster){let a=i.trackData.get(this.internalTrack.id);if(r.blockIndex+1<a.blocks.length)return{blockIndex:r.blockIndex+1,correctBlockFound:!0}}else if(i.trackData.get(this.internalTrack.id))return{blockIndex:0,correctBlockFound:!0};return{blockIndex:-1,correctBlockFound:!1}},-1/0,1/0,e)}async getKeyPacket(t,e){let r=this.intoTimescale(t);return this.performClusterLookup(null,i=>{let a=i.trackData.get(this.internalTrack.id);if(!a)return{blockIndex:-1,correctBlockFound:!1};let s=rr(a.presentationTimestamps,u=>a.blocks[u.blockIndex].isKeyFrame&&u.timestamp<=r),o=s!==-1?a.presentationTimestamps[s].blockIndex:-1,c=s!==-1&&r<a.endTimestamp;return{blockIndex:o,correctBlockFound:c}},r,r,e)}async getNextKeyPacket(t,e){let r=this.packetToClusterLocation.get(t);if(r===void 0)throw new Error("Packet was not created from this track.");return this.performClusterLookup(r.cluster,i=>{if(i===r.cluster){let s=i.trackData.get(this.internalTrack.id).blocks.findIndex((o,c)=>o.isKeyFrame&&c>r.blockIndex);if(s!==-1)return{blockIndex:s,correctBlockFound:!0}}else{let a=i.trackData.get(this.internalTrack.id);if(a&&a.firstKeyFrameTimestamp!==null){let s=a.blocks.findIndex(o=>o.isKeyFrame);return h(s!==-1),{blockIndex:s,correctBlockFound:!0}}}return{blockIndex:-1,correctBlockFound:!1}},-1/0,1/0,e)}async fetchPacketInCluster(t,e,r){if(e===-1)return null;let a=t.trackData.get(this.internalTrack.id).blocks[e];h(a),a.decoded||(a.data=this.internalTrack.demuxer.decodeBlockData(this.internalTrack,a.data),a.decoded=!0);let s=r.metadataOnly?de:a.data,o=a.timestamp/this.internalTrack.segment.timestampFactor,c=a.duration/this.internalTrack.segment.timestampFactor,u={};a.mainAdditional&&this.internalTrack.info?.type==="video"&&this.internalTrack.info.alphaMode&&(u.alpha=r.metadataOnly?de:a.mainAdditional,u.alphaByteLength=a.mainAdditional.byteLength);let l=new Q(s,a.isKeyFrame?"key":"delta",o,c,t.dataStartPos+e,a.data.byteLength,u);return this.packetToClusterLocation.set(l,{cluster:t,blockIndex:e}),l}async performClusterLookup(t,e,r,i,a){let{demuxer:s,segment:o}=this.internalTrack,c=null,u=null,l=-1;if(t){let{blockIndex:y,correctBlockFound:k}=e(t);if(k)return this.fetchPacketInCluster(t,y,a);y!==-1&&(u=t,l=y)}let m=H(this.internalTrack.cuePoints,r,y=>y.time),d=m!==-1?this.internalTrack.cuePoints[m]:null,f=H(this.internalTrack.clusterPositionCache,r,y=>y.startTimestamp),p=f!==-1?this.internalTrack.clusterPositionCache[f]:null,b=Math.max(d?.clusterPosition??0,p?.elementStartPos??0)||null,g;for(t?b===null||t.elementStartPos>=b?(g=t.elementEndPos,c=t):g=b:g=b??o.clusterSeekStartPos;o.elementEndPos===null||g<=o.elementEndPos-Oe;){if(c){let A=c.trackData.get(this.internalTrack.id);if(A&&A.startTimestamp>i)break}let y=s.reader.requestSliceRange(g,Oe,dt);if(y instanceof Promise&&(y=await y),!y)break;let k=g,w=mt(y);if(!w||!pi.includes(w.id)&&w.id!==236){let A=await Co(s.reader,k,pi,Math.min(o.elementEndPos??1/0,k+Tl));if(A){g=A;continue}else break}let T=w.id,S=w.size,x=y.filePos;if(T===524531317){c=await s.readCluster(k,o),S=c.elementEndPos-x;let{blockIndex:A,correctBlockFound:C}=e(c);if(C)return this.fetchPacketInCluster(c,A,a);A!==-1&&(u=c,l=A)}S===void 0&&(h(T!==524531317),S=(await Aa(s.reader,x,dn,o.elementEndPos)).pos-x);let P=x+S;if(o.elementEndPos===null){let A=s.reader.requestSliceRange(P,Oe,dt);if(A instanceof Promise&&(A=await A),!A)break;if(wa(A)===408125543){o.elementEndPos=P;break}}g=P}if(d&&(!u||u.elementStartPos<d.clusterPosition)){let y=this.internalTrack.cuePoints[m-1];h(!y||y.time<d.time);let k=y?.time??-1/0;return this.performClusterLookup(null,e,k,i,a)}return u?this.fetchPacketInCluster(u,l,a):null}},Eo=class extends Ca{constructor(e){super(e);this.decoderConfigPromise=null;this.internalTrack=e}getType(){return"video"}getCodec(){return this.internalTrack.info.codec}getCodedWidth(){return this.internalTrack.info.width}getCodedHeight(){return this.internalTrack.info.height}getSquarePixelWidth(){return this.internalTrack.info.squarePixelWidth}getSquarePixelHeight(){return this.internalTrack.info.squarePixelHeight}getRotation(){return this.internalTrack.info.rotation}async getColorSpace(){return{primaries:this.internalTrack.info.colorSpace?.primaries,transfer:this.internalTrack.info.colorSpace?.transfer,matrix:this.internalTrack.info.colorSpace?.matrix,fullRange:this.internalTrack.info.colorSpace?.fullRange}}async canBeTransparent(){return this.internalTrack.info.alphaMode}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfigPromise??=(async()=>{let e=null;(this.internalTrack.info.codec==="vp9"||this.internalTrack.info.codec==="av1"||this.internalTrack.info.codec==="avc"&&!this.internalTrack.info.codecDescription||this.internalTrack.info.codec==="hevc"&&!this.internalTrack.info.codecDescription)&&(e=await this.getFirstPacket({}));let i={codec:ii({width:this.internalTrack.info.width,height:this.internalTrack.info.height,codec:this.internalTrack.info.codec,codecDescription:this.internalTrack.info.codecDescription,colorSpace:this.internalTrack.info.colorSpace,avcType:1,avcCodecInfo:this.internalTrack.info.codec==="avc"&&e?si(e.data):null,hevcCodecInfo:this.internalTrack.info.codec==="hevc"&&e?oi(e.data):null,vp9CodecInfo:this.internalTrack.info.codec==="vp9"&&e?ia(e.data):null,av1CodecInfo:this.internalTrack.info.codec==="av1"&&e?na(e.data):null}),codedWidth:this.internalTrack.info.width,codedHeight:this.internalTrack.info.height,description:this.internalTrack.info.codecDescription??void 0,colorSpace:this.internalTrack.info.colorSpace??void 0};return(this.internalTrack.info.width!==this.internalTrack.info.squarePixelWidth||this.internalTrack.info.height!==this.internalTrack.info.squarePixelHeight)&&(i.displayAspectWidth=this.internalTrack.info.squarePixelWidth,i.displayAspectHeight=this.internalTrack.info.squarePixelHeight),i})():null}},vo=class extends Ca{constructor(e){super(e);this.decoderConfig=null;this.internalTrack=e}getType(){return"audio"}getCodec(){return this.internalTrack.info.codec}getNumberOfChannels(){return this.internalTrack.info.numberOfChannels}getSampleRate(){return this.internalTrack.info.sampleRate}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfig??={codec:ni({codec:this.internalTrack.info.codec,codecDescription:this.internalTrack.info.codecDescription,aacCodecInfo:this.internalTrack.info.aacCodecInfo}),numberOfChannels:this.internalTrack.info.numberOfChannels,sampleRate:this.internalTrack.info.sampleRate,description:this.internalTrack.info.codecDescription??void 0}:null}};var mn=async(n,t,e)=>{let i=t;for(;e===null||i<e;){let a=e!==null?Math.min(65536,e-i):65536,s=n.requestSliceRange(i,4,a);if(s instanceof Promise&&(s=await s),!s||s.length<4)break;for(;s.remainingLength>=4;){let o=s.filePos,c=F(s),u=n.fileSize!==null?n.fileSize-i:null,l=Br(c,u);if(l.header)return{header:l.header,startPos:i};s.filePos=o+l.bytesAdvanced,i=s.filePos}}return null};var Ia=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.firstFrameHeader=null;this.firstFrameHeaderPos=null;this.loadedSamples=[];this.metadataTags=null;this.xingData=null;this.trackBackings=[];this.readingMutex=new Ie;this.lastSampleLoaded=!1;this.lastLoadedPos=0;this.nextTimestampInSamples=0;this.reader=e._reader}async readMetadata(){return this.metadataPromise??=(async()=>{for(;!this.firstFrameHeader&&!this.lastSampleLoaded;)await this.advanceReader();if(!this.firstFrameHeader)throw new Error("No valid MP3 frame found.");this.trackBackings=[new _o(this)]})()}async advanceReader(){if(this.lastLoadedPos===0)for(;;){let c=this.reader.requestSlice(this.lastLoadedPos,ze);if(c instanceof Promise&&(c=await c),!c){this.lastSampleLoaded=!0;return}let u=ft(c);if(!u)break;this.lastLoadedPos=c.filePos+u.size}let e=await mn(this.reader,this.lastLoadedPos,this.reader.fileSize);if(!e){this.lastSampleLoaded=!0;return}let r=e.header;this.lastLoadedPos=e.startPos+r.totalSize-1;let i=cr(r.mpegVersionId,r.channel),a=this.reader.requestSlice(e.startPos+i,4);if(a instanceof Promise&&(a=await a),a){let c=F(a);if(c===or||c===ai){if(!this.xingData){let l=this.reader.requestSlice(e.startPos+i+4,12);if(l instanceof Promise&&(l=await l),l){let m=B(l,12),d=V(m),f=d.getUint32(0,!1);this.xingData={frameCount:f&1?d.getUint32(4,!1):null,fileSize:f&2?d.getUint32(8,!1):null}}}return}}this.firstFrameHeader||(this.firstFrameHeader=r,this.firstFrameHeaderPos=e.startPos),r.sampleRate!==this.firstFrameHeader.sampleRate&&console.warn(`MP3 changed sample rate mid-file: ${this.firstFrameHeader.sampleRate} Hz to ${r.sampleRate} Hz. Might be a bug, so please report this file.`);let s=r.audioSamplesInFrame/this.firstFrameHeader.sampleRate,o={timestamp:this.nextTimestampInSamples/this.firstFrameHeader.sampleRate,duration:s,dataStart:e.startPos,dataSize:r.totalSize};this.loadedSamples.push(o),this.nextTimestampInSamples+=r.audioSamplesInFrame}async getMimeType(){return"audio/mpeg"}async getTrackBackings(){return await this.readMetadata(),this.trackBackings}async getMetadataTags(){let e=await this.readingMutex.acquire();try{if(await this.readMetadata(),this.metadataTags)return this.metadataTags;this.metadataTags={};let r=0,i=!1;for(;;){let a=this.reader.requestSlice(r,ze);if(a instanceof Promise&&(a=await a),!a)break;let s=ft(a);if(!s)break;i=!0;let o=this.reader.requestSlice(a.filePos,s.size);if(o instanceof Promise&&(o=await o),!o)break;bi(o,s,this.metadataTags),r=a.filePos+s.size}if(!i&&this.reader.fileSize!==null&&this.reader.fileSize>=fn){let a=this.reader.requestSlice(this.reader.fileSize-fn,fn);a instanceof Promise&&(a=await a),h(a),te(a,3)==="TAG"&&wl(a,this.metadataTags)}return this.metadataTags}finally{e()}}},_o=class{constructor(t){this.demuxer=t}getType(){return"audio"}getId(){return 1}getNumber(){return 1}getTimeResolution(){return h(this.demuxer.firstFrameHeader),this.demuxer.firstFrameHeader.sampleRate/this.demuxer.firstFrameHeader.audioSamplesInFrame}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){let t=this.demuxer;if(h(t.firstFrameHeader!==null),h(t.firstFrameHeaderPos!==null),t.xingData){if(t.xingData.frameCount!==null)return t.xingData.frameCount*t.firstFrameHeader.audioSamplesInFrame/t.firstFrameHeader.sampleRate}else if(t.reader.fileSize!==null){let e=Wu(t.firstFrameHeader.lowSamplingFrequency,t.firstFrameHeader.layer,t.firstFrameHeader.bitrate,t.firstFrameHeader.sampleRate),r=(t.reader.fileSize-t.firstFrameHeaderPos)/e;return Math.round(r)*t.firstFrameHeader.audioSamplesInFrame/t.firstFrameHeader.sampleRate}return null}async getLiveRefreshInterval(){return null}getName(){return null}getLanguageCode(){return J}getCodec(){return"mp3"}getInternalCodecId(){return null}getNumberOfChannels(){return h(this.demuxer.firstFrameHeader),this.demuxer.firstFrameHeader.channel===3?1:2}getSampleRate(){return h(this.demuxer.firstFrameHeader),this.demuxer.firstFrameHeader.sampleRate}getDisposition(){return{...we}}async getDecoderConfig(){return h(this.demuxer.firstFrameHeader),{codec:"mp3",numberOfChannels:this.demuxer.firstFrameHeader.channel===3?1:2,sampleRate:this.demuxer.firstFrameHeader.sampleRate}}async getPacketAtIndex(t,e){if(t===-1)return null;let r=this.demuxer.loadedSamples[t];if(!r)return null;let i;if(e.metadataOnly)i=de;else{let a=this.demuxer.reader.requestSlice(r.dataStart,r.dataSize);if(a instanceof Promise&&(a=await a),!a)return null;i=B(a,r.dataSize)}return new Q(i,"key",r.timestamp,r.duration,t,r.dataSize)}getFirstPacket(t){return this.getPacketAtIndex(0,t)}async getNextPacket(t,e){let r=await this.demuxer.readingMutex.acquire();try{let i=Ft(this.demuxer.loadedSamples,t.timestamp,s=>s.timestamp);if(i===-1)throw new Error("Packet was not created from this track.");let a=i+1;for(;a>=this.demuxer.loadedSamples.length&&!this.demuxer.lastSampleLoaded;)await this.demuxer.advanceReader();return this.getPacketAtIndex(a,e)}finally{r()}}async getPacket(t,e){let r=await this.demuxer.readingMutex.acquire();try{for(;;){let i=H(this.demuxer.loadedSamples,t,a=>a.timestamp);if(i===-1&&this.demuxer.loadedSamples.length>0)return null;if(this.demuxer.lastSampleLoaded)return this.getPacketAtIndex(i,e);if(i>=0&&i+1<this.demuxer.loadedSamples.length)return this.getPacketAtIndex(i,e);await this.demuxer.advanceReader()}}finally{r()}}getKeyPacket(t,e){return this.getPacket(t,e)}getNextKeyPacket(t,e){return this.getNextPacket(t,e)}};var pn=1399285583,_m=79764919,Sl=new Uint32Array(256);for(let n=0;n<256;n++){let t=n<<24;for(let e=0;e<8;e++)t=t&2147483648?t<<1^_m:t<<1;Sl[n]=t>>>0&4294967295}var Ea=n=>{let t=V(n),e=t.getUint32(22,!0);t.setUint32(22,0,!0);let r=0;for(let i=0;i<n.length;i++){let a=n[i];r=(r<<8^Sl[r>>>24^a])>>>0}return t.setUint32(22,e,!0),r},va=(n,t,e)=>{let r=0,i=null;if(n.length>0)if(t.codec==="vorbis"){h(t.vorbisInfo);let a=t.vorbisInfo.modeBlockflags.length,o=(1<<ku(a-1))-1<<1,c=(n[0]&o)>>1;if(c>=t.vorbisInfo.modeBlockflags.length)throw new Error("Invalid mode number.");let u=e,l=t.vorbisInfo.modeBlockflags[c];if(i=t.vorbisInfo.blocksizes[l],l===1){let m=(o|1)+1,d=n[0]&m?1:0;u=t.vorbisInfo.blocksizes[d]}r=u!==null?u+i>>2:0}else t.codec==="opus"&&(r=Ju(n).durationInSamples);return{durationInSamples:r,vorbisBlockSize:i}},_a=n=>{let t="audio/ogg";if(n.codecStrings){let e=[...new Set(n.codecStrings)];t+=`; codecs="${e.join(", ")}"`}return t};var pr=27,Vr=282,Ra=Vr+255*255,yi=n=>{let t=n.filePos;if(Dr(n)!==pn)return null;n.skip(1);let r=O(n),i=xl(n),a=Dr(n),s=Dr(n),o=Dr(n),c=O(n),u=new Uint8Array(c);for(let f=0;f<c;f++)u[f]=O(n);let l=27+c,m=u.reduce((f,p)=>f+p,0),d=l+m;return{headerStartPos:t,totalSize:d,dataStartPos:t+l,dataSize:m,headerType:r,granulePosition:i,serialNumber:a,sequenceNumber:s,checksum:o,lacingValues:u}},Al=(n,t)=>{for(;n.filePos<t-3;){let e=Dr(n),r=e&255,i=e>>>8&255,a=e>>>16&255,s=e>>>24&255,o=79;if(!(r!==o&&i!==o&&a!==o&&s!==o)){if(n.skip(-4),e===pn)return!0;n.skip(1)}}return!1};var Fa=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.bitstreams=[];this.trackBackings=[];this.metadataTags={};this.reader=e._reader}async readMetadata(){return this.metadataPromise??=(async()=>{let e=0;for(;;){let r=this.reader.requestSliceRange(e,pr,Vr);if(r instanceof Promise&&(r=await r),!r)break;let i=yi(r);if(!i||!!!(i.headerType&2))break;this.bitstreams.push({serialNumber:i.serialNumber,bosPage:i,description:null,numberOfChannels:-1,sampleRate:-1,codecInfo:{codec:null,vorbisInfo:null,opusInfo:null},lastMetadataPacket:null}),e=i.headerStartPos+i.totalSize}for(let r of this.bitstreams){let i=await this.readPacket(r.bosPage,0);i&&(i.data.byteLength>=7&&i.data[0]===1&&i.data[1]===118&&i.data[2]===111&&i.data[3]===114&&i.data[4]===98&&i.data[5]===105&&i.data[6]===115?await this.readVorbisMetadata(i,r):i.data.byteLength>=8&&i.data[0]===79&&i.data[1]===112&&i.data[2]===117&&i.data[3]===115&&i.data[4]===72&&i.data[5]===101&&i.data[6]===97&&i.data[7]===100&&await this.readOpusMetadata(i,r),r.codecInfo.codec!==null&&this.trackBackings.push(new Ro(r,this)))}})()}async readVorbisMetadata(e,r){let i=await this.findNextPacketStart(e);if(!i)return;let a=await this.readPacket(i.startPage,i.startSegmentIndex);if(!a||(i=await this.findNextPacketStart(a),!i))return;let s=await this.readPacket(i.startPage,i.startSegmentIndex);if(!s||a.data[0]!==3||s.data[0]!==5)return;let o=[],c=d=>{for(;o.push(Math.min(255,d)),!(d<255);)d-=255};c(e.data.length),c(a.data.length);let u=new Uint8Array(1+o.length+e.data.length+a.data.length+s.data.length);u[0]=2,u.set(o,1),u.set(e.data,1+o.length),u.set(a.data,1+o.length+e.data.length),u.set(s.data,1+o.length+e.data.length+a.data.length),r.codecInfo.codec="vorbis",r.description=u,r.lastMetadataPacket=s;let l=V(e.data);r.numberOfChannels=l.getUint8(11),r.sampleRate=l.getUint32(12,!0);let m=l.getUint8(28);r.codecInfo.vorbisInfo={blocksizes:[1<<(m&15),1<<(m>>4)],modeBlockflags:aa(s.data).modeBlockflags},rn(a.data.subarray(7),this.metadataTags)}async readOpusMetadata(e,r){let i=await this.findNextPacketStart(e);if(!i)return;let a=await this.readPacket(i.startPage,i.startSegmentIndex);if(!a)return;r.codecInfo.codec="opus",r.description=e.data,r.lastMetadataPacket=a;let s=lr(e.data);r.numberOfChannels=s.outputChannelCount,r.sampleRate=nt,r.codecInfo.opusInfo={preSkip:s.preSkip},rn(a.data.subarray(8),this.metadataTags)}async readPacket(e,r){h(r<e.lacingValues.length);let i=0;for(let d=0;d<r;d++)i+=e.lacingValues[d];let a=e,s=i,o=r,c=[];e:for(;;){let d=this.reader.requestSlice(a.dataStartPos,a.dataSize);d instanceof Promise&&(d=await d),h(d);let f=B(d,a.dataSize);for(;;){if(o===a.lacingValues.length){c.push(f.subarray(i,s));break}let b=a.lacingValues[o];if(s+=b,b<255){c.push(f.subarray(i,s));break e}o++}let p=a.headerStartPos+a.totalSize;for(;;){let b=this.reader.requestSliceRange(p,pr,Vr);if(b instanceof Promise&&(b=await b),!b)return null;let g=yi(b);if(!g)return null;if(a=g,a.serialNumber===e.serialNumber)break;p=a.headerStartPos+a.totalSize}i=0,s=0,o=0}let u=c.reduce((d,f)=>d+f.length,0);if(u===0)return null;let l=new Uint8Array(u),m=0;for(let d=0;d<c.length;d++){let f=c[d];l.set(f,m),m+=f.length}return{data:l,endPage:a,endSegmentIndex:o}}async findNextPacketStart(e){if(e.endSegmentIndex<e.endPage.lacingValues.length-1)return{startPage:e.endPage,startSegmentIndex:e.endSegmentIndex+1};if(!!(e.endPage.headerType&4))return null;let i=e.endPage.headerStartPos+e.endPage.totalSize;for(;;){let a=this.reader.requestSliceRange(i,pr,Vr);if(a instanceof Promise&&(a=await a),!a)return null;let s=yi(a);if(!s)return null;if(s.serialNumber===e.endPage.serialNumber)return{startPage:s,startSegmentIndex:0};i=s.headerStartPos+s.totalSize}}async getMimeType(){await this.readMetadata();let e=await Promise.all(this.trackBackings.map(r=>r.getDecoderConfig().then(i=>i?.codec??null)));return _a({codecStrings:e.filter(Boolean)})}async getTrackBackings(){return await this.readMetadata(),this.trackBackings}async getMetadataTags(){return await this.readMetadata(),this.metadataTags}},Ro=class{constructor(t,e){this.bitstream=t;this.demuxer=e;this.encodedPacketToMetadata=new WeakMap;this.sequentialScanCache=[];this.sequentialScanMutex=new Ie;this.internalSampleRate=t.codecInfo.codec==="opus"?nt:t.sampleRate}getType(){return"audio"}getId(){return this.bitstream.serialNumber}getNumber(){let t=this.demuxer.trackBackings.findIndex(e=>e.bitstream===this.bitstream);return h(t!==-1),t+1}getNumberOfChannels(){return this.bitstream.numberOfChannels}getSampleRate(){return this.bitstream.sampleRate}getTimeResolution(){return this.bitstream.sampleRate}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){return null}async getLiveRefreshInterval(){return null}getCodec(){return this.bitstream.codecInfo.codec}getInternalCodecId(){return null}async getDecoderConfig(){return h(this.bitstream.codecInfo.codec),{codec:this.bitstream.codecInfo.codec,numberOfChannels:this.bitstream.numberOfChannels,sampleRate:this.bitstream.sampleRate,description:this.bitstream.description??void 0}}getName(){return null}getLanguageCode(){return J}getDisposition(){return{...we,primary:!1}}granulePositionToTimestampInSamples(t){return this.bitstream.codecInfo.codec==="opus"?(h(this.bitstream.codecInfo.opusInfo),t-this.bitstream.codecInfo.opusInfo.preSkip):t}createEncodedPacketFromOggPacket(t,e,r){if(!t)return null;let{durationInSamples:i,vorbisBlockSize:a}=va(t.data,this.bitstream.codecInfo,e.vorbisLastBlocksize),s=new Q(r.metadataOnly?de:t.data,"key",Math.max(0,e.timestampInSamples)/this.internalSampleRate,i/this.internalSampleRate,t.endPage.headerStartPos+t.endSegmentIndex,t.data.byteLength);return this.encodedPacketToMetadata.set(s,{packet:t,timestampInSamples:e.timestampInSamples,durationInSamples:i,vorbisLastBlockSize:e.vorbisLastBlocksize,vorbisBlockSize:a}),s}async getFirstPacket(t){h(this.bitstream.lastMetadataPacket);let e=await this.demuxer.findNextPacketStart(this.bitstream.lastMetadataPacket);if(!e)return null;let r=0;this.bitstream.codecInfo.codec==="opus"&&(h(this.bitstream.codecInfo.opusInfo),r-=this.bitstream.codecInfo.opusInfo.preSkip);let i=await this.demuxer.readPacket(e.startPage,e.startSegmentIndex);return this.createEncodedPacketFromOggPacket(i,{timestampInSamples:r,vorbisLastBlocksize:null},t)}async getNextPacket(t,e){let r=this.encodedPacketToMetadata.get(t);if(!r)throw new Error("Packet was not created from this track.");let i=await this.demuxer.findNextPacketStart(r.packet);if(!i)return null;let a=r.timestampInSamples+r.durationInSamples,s=await this.demuxer.readPacket(i.startPage,i.startSegmentIndex);return this.createEncodedPacketFromOggPacket(s,{timestampInSamples:a,vorbisLastBlocksize:r.vorbisBlockSize},e)}async getPacket(t,e){if(this.demuxer.reader.fileSize===null)return this.getPacketSequential(t,e);let r=Mt(t*this.internalSampleRate);if(r===0)return this.getFirstPacket(e);if(r<0)return null;h(this.bitstream.lastMetadataPacket);let i=await this.demuxer.findNextPacketStart(this.bitstream.lastMetadataPacket);if(!i)return null;let a=i.startPage,s=this.demuxer.reader.fileSize,o=[a];e:for(;a.headerStartPos+a.totalSize<s;){let k=a.headerStartPos,w=Math.floor((k+s)/2),T=w;for(;;){let S=Math.min(T+Ra,s-pr),x=this.demuxer.reader.requestSlice(T,S-T);if(x instanceof Promise&&(x=await x),h(x),!Al(x,S)){s=w+pr;continue e}let A=this.demuxer.reader.requestSliceRange(x.filePos,pr,Vr);A instanceof Promise&&(A=await A),h(A);let C=yi(A);h(C);let I=!1;if(C.serialNumber===this.bitstream.serialNumber)I=!0;else{let v=this.demuxer.reader.requestSlice(C.headerStartPos,C.totalSize);v instanceof Promise&&(v=await v),h(v);let E=B(v,C.totalSize);I=Ea(E)===C.checksum}if(!I){T=C.headerStartPos+4;continue}if(I&&C.serialNumber!==this.bitstream.serialNumber){T=C.headerStartPos+C.totalSize;continue}if(C.granulePosition===-1){T=C.headerStartPos+C.totalSize;continue}this.granulePositionToTimestampInSamples(C.granulePosition)>r?s=C.headerStartPos:(a=C,o.push(C));continue e}}let c=i.startPage;for(let k of o){if(k.granulePosition===a.granulePosition)break;(!c||k.headerStartPos>c.headerStartPos)&&(c=k)}let u=c,l=[u];for(;!(u.serialNumber===this.bitstream.serialNumber&&u.granulePosition===a.granulePosition);){let k=u.headerStartPos+u.totalSize,w=this.demuxer.reader.requestSliceRange(k,pr,Vr);w instanceof Promise&&(w=await w),h(w);let T=yi(w);h(T),u=T,u.serialNumber===this.bitstream.serialNumber&&l.push(u)}h(u.granulePosition!==-1);let m=null,d,f,p=u,b=0;if(u.headerStartPos===i.startPage.headerStartPos)d=this.granulePositionToTimestampInSamples(0),f=!0,m=0;else{d=0,f=!1;for(let T=u.lacingValues.length-1;T>=0;T--)if(u.lacingValues[T]<255){m=T+1;break}if(m===null)throw new Error("Invalid page with granule position: no packets end on this page.");b=m-1;let k={data:de,endPage:p,endSegmentIndex:b};if(await this.demuxer.findNextPacketStart(k)){let T=Cl(l,u,m);h(T);let S=Pl(l,T.page,T.segmentIndex);S&&(u=S.page,m=S.segmentIndex)}else for(;;){let T=Cl(l,u,m);if(!T)break;let S=Pl(l,T.page,T.segmentIndex);if(!S)break;if(u=S.page,m=S.segmentIndex,T.page.headerStartPos!==p.headerStartPos){p=T.page,b=T.segmentIndex;break}}}let g=null,y=null;for(;u!==null;){h(m!==null);let k=await this.demuxer.readPacket(u,m);if(!k)break;if(!(u.headerStartPos===i.startPage.headerStartPos&&m<i.startSegmentIndex)){let S=this.createEncodedPacketFromOggPacket(k,{timestampInSamples:d,vorbisLastBlocksize:y?.vorbisBlockSize??null},e);h(S);let x=this.encodedPacketToMetadata.get(S);if(h(x),!f&&k.endPage.headerStartPos===p.headerStartPos&&k.endSegmentIndex===b?(d=this.granulePositionToTimestampInSamples(u.granulePosition),f=!0,S=this.createEncodedPacketFromOggPacket(k,{timestampInSamples:d-x.durationInSamples,vorbisLastBlocksize:y?.vorbisBlockSize??null},e),h(S),x=this.encodedPacketToMetadata.get(S),h(x)):d+=x.durationInSamples,g=S,y=x,f&&(Math.max(d,0)>r||Math.max(x.timestampInSamples,0)===r))break}let T=await this.demuxer.findNextPacketStart(k);if(!T)break;u=T.startPage,m=T.startSegmentIndex}return g}async getPacketSequential(t,e){let r=await this.sequentialScanMutex.acquire();try{let i=Mt(t*this.internalSampleRate);t=i/this.internalSampleRate;let a=H(this.sequentialScanCache,i,c=>c.timestampInSamples),s;if(a!==-1){let c=this.sequentialScanCache[a];s=this.createEncodedPacketFromOggPacket(c.packet,{timestampInSamples:c.timestampInSamples,vorbisLastBlocksize:c.vorbisLastBlockSize},e)}else s=await this.getFirstPacket(e);let o=0;for(;s&&s.timestamp<t;){let c=await this.getNextPacket(s,e);if(!c||c.timestamp>t)break;if(s=c,o++,o===100){o=0;let u=this.encodedPacketToMetadata.get(s);h(u),this.sequentialScanCache.length>0&&h(X(this.sequentialScanCache).timestampInSamples<=u.timestampInSamples),this.sequentialScanCache.push(u)}}return s}finally{r()}}getKeyPacket(t,e){return this.getPacket(t,e)}getNextKeyPacket(t,e){return this.getNextPacket(t,e)}},Pl=(n,t,e)=>{let r=t,i=e;e:for(;;){for(i--,i;i>=0;i--)if(r.lacingValues[i]<255){i++;break e}if(h(i===-1),!(r.headerType&1)){i=0;break}let s=mo(n,o=>o.headerStartPos<r.headerStartPos);if(!s)return null;r=s,i=r.lacingValues.length}if(h(i!==-1),i===r.lacingValues.length){let a=n[n.indexOf(r)+1];h(a),r=a,i=0}return{page:r,segmentIndex:i}},Cl=(n,t,e)=>{if(e>0)return{page:t,segmentIndex:e-1};let r=mo(n,i=>i.headerStartPos<t.headerStartPos);return r?{page:r,segmentIndex:r.lacingValues.length-1}:null};var Ma=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.dataStart=-1;this.dataSize=-1;this.audioInfo=null;this.trackBackings=[];this.lastKnownPacketIndex=0;this.metadataTags={};this.reader=e._reader}async readMetadata(){return this.metadataPromise??=(async()=>{let e=this.reader.requestSlice(0,12);e instanceof Promise&&(e=await e),h(e);let r=te(e,4),i=r!=="RIFX",a=r==="RF64",s=Kt(e,i),o=a?this.reader.fileSize:Math.min(s+8,this.reader.fileSize??1/0);if(te(e,4)!=="WAVE")throw new Error("Invalid WAVE file - wrong format");let u=0,l=null,m=e.filePos;for(;o===null||m<o;){let f=this.reader.requestSlice(m,8);if(f instanceof Promise&&(f=await f),!f)break;let p=te(f,4),b=Kt(f,i),g=f.filePos;if(a&&u===0&&p!=="ds64")throw new Error('Invalid RF64 file: First chunk must be "ds64".');if(p==="fmt ")await this.parseFmtChunk(g,b,i);else if(p==="data"){if(l??=b,this.dataStart=f.filePos,this.dataSize=Math.min(l,(o??1/0)-this.dataStart),this.reader.fileSize===null)break}else if(p==="ds64"){let y=this.reader.requestSlice(g,b);if(y instanceof Promise&&(y=await y),!y)break;let k=Mo(y,i);l=Mo(y,i),o=Math.min(k+8,this.reader.fileSize??1/0)}else p==="LIST"?await this.parseListChunk(g,b,i):(p==="ID3 "||p==="id3 ")&&await this.parseId3Chunk(g,b);m=g+b+(b&1),u++}if(!this.audioInfo)throw new Error('Invalid WAVE file - missing "fmt " chunk');if(this.dataStart===-1)throw new Error('Invalid WAVE file - missing "data" chunk');let d=this.audioInfo.blockSizeInBytes;this.dataSize=Math.floor(this.dataSize/d)*d,this.trackBackings.push(new Fo(this))})()}async parseFmtChunk(e,r,i){let a=this.reader.requestSlice(e,r);if(a instanceof Promise&&(a=await a),!a)return;let s=Ti(a,i),o=Ti(a,i),c=Kt(a,i);a.skip(4);let u=Ti(a,i),l;if(r===14?l=8:l=Ti(a,i),r>=18&&s!==357){let m=Ti(a,i),d=r-18;if(Math.min(d,m)>=22&&s===65534){a.skip(6);let p=B(a,16);s=p[0]|p[1]<<8}}(s===7||s===6)&&(l=8),this.audioInfo={format:s,numberOfChannels:o,sampleRate:c,sampleSizeInBytes:Math.ceil(l/8),blockSizeInBytes:u}}async parseListChunk(e,r,i){let a=this.reader.requestSlice(e,r);if(a instanceof Promise&&(a=await a),!a)return;let s=te(a,4);if(s!=="INFO"&&s!=="INF0")return;let o=a.filePos;for(;o<=e+r-8;){a.filePos=o;let c=te(a,4),u=Kt(a,i),l=B(a,u),m=0;for(let f=0;f<l.length&&l[f]!==0;f++)m++;let d=String.fromCharCode(...l.subarray(0,m));switch(this.metadataTags.raw??={},this.metadataTags.raw[c]=d,c){case"INAM":case"TITL":this.metadataTags.title??=d;break;case"TIT3":this.metadataTags.description??=d;break;case"IART":this.metadataTags.artist??=d;break;case"IPRD":this.metadataTags.album??=d;break;case"IPRT":case"ITRK":case"TRCK":{let f=d.split("/"),p=Number.parseInt(f[0],10),b=f[1]&&Number.parseInt(f[1],10);Number.isInteger(p)&&p>0&&(this.metadataTags.trackNumber??=p),b&&Number.isInteger(b)&&b>0&&(this.metadataTags.tracksTotal??=b)}break;case"ICRD":case"IDIT":{let f=new Date(d);Number.isNaN(f.getTime())||(this.metadataTags.date??=f)}break;case"YEAR":{let f=Number.parseInt(d,10);Number.isInteger(f)&&f>0&&(this.metadataTags.date??=new Date(f,0,1))}break;case"IGNR":case"GENR":this.metadataTags.genre??=d;break;case"ICMT":case"CMNT":case"COMM":this.metadataTags.comment??=d;break}o+=8+u+(u&1)}}async parseId3Chunk(e,r){let i=this.reader.requestSlice(e,r);if(i instanceof Promise&&(i=await i),!i)return;let a=ft(i);if(a){let s=r-ze;if(a.size=Math.min(a.size,s),a.size>0){let o=i.slice(e+ze,a.size);bi(o,a,this.metadataTags)}}}getCodec(){if(h(this.audioInfo),this.audioInfo.format===7)return"ulaw";if(this.audioInfo.format===6)return"alaw";if(this.audioInfo.format===1){if(this.audioInfo.sampleSizeInBytes===1)return"pcm-u8";if(this.audioInfo.sampleSizeInBytes===2)return"pcm-s16";if(this.audioInfo.sampleSizeInBytes===3)return"pcm-s24";if(this.audioInfo.sampleSizeInBytes===4)return"pcm-s32"}return this.audioInfo.format===3&&this.audioInfo.sampleSizeInBytes===4?"pcm-f32":null}async getMimeType(){return"audio/wav"}async getTrackBackings(){return await this.readMetadata(),this.trackBackings}async getMetadataTags(){return await this.readMetadata(),this.metadataTags}},ki=2048,Fo=class{constructor(t){this.demuxer=t}getType(){return"audio"}getId(){return 1}getNumber(){return 1}getCodec(){return this.demuxer.getCodec()}getInternalCodecId(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.format}async getDecoderConfig(){let t=this.demuxer.getCodec();return t?(h(this.demuxer.audioInfo),{codec:t,numberOfChannels:this.demuxer.audioInfo.numberOfChannels,sampleRate:this.demuxer.audioInfo.sampleRate}):null}getNumberOfChannels(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.numberOfChannels}getSampleRate(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.sampleRate}getTimeResolution(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.sampleRate}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){return h(this.demuxer.dataSize!==-1),this.demuxer.dataSize/this.demuxer.audioInfo.blockSizeInBytes/this.demuxer.audioInfo.sampleRate}async getLiveRefreshInterval(){return null}getName(){return null}getLanguageCode(){return J}getDisposition(){return{...we}}async getPacketAtIndex(t,e){h(t>=0),h(this.demuxer.audioInfo);let r=t*ki*this.demuxer.audioInfo.blockSizeInBytes;if(r>=this.demuxer.dataSize)return null;let i=Math.min(ki*this.demuxer.audioInfo.blockSizeInBytes,this.demuxer.dataSize-r);if(this.demuxer.reader.fileSize===null){let c=this.demuxer.reader.requestSlice(this.demuxer.dataStart+r,i);if(c instanceof Promise&&(c=await c),!c)return null}let a;if(e.metadataOnly)a=de;else{let c=this.demuxer.reader.requestSlice(this.demuxer.dataStart+r,i);c instanceof Promise&&(c=await c),h(c),a=B(c,i)}let s=t*ki/this.demuxer.audioInfo.sampleRate,o=i/this.demuxer.audioInfo.blockSizeInBytes/this.demuxer.audioInfo.sampleRate;return this.demuxer.lastKnownPacketIndex=Math.max(t,this.demuxer.lastKnownPacketIndex),new Q(a,"key",s,o,t,i)}getFirstPacket(t){return this.getPacketAtIndex(0,t)}async getPacket(t,e){h(this.demuxer.audioInfo);let r=Math.floor(Math.min(t*this.demuxer.audioInfo.sampleRate/ki,(this.demuxer.dataSize-1)/(ki*this.demuxer.audioInfo.blockSizeInBytes)));if(r<0)return null;let i=await this.getPacketAtIndex(r,e);if(i)return i;if(r===0)return null;h(this.demuxer.reader.fileSize===null);let a=await this.getPacketAtIndex(this.demuxer.lastKnownPacketIndex,e);for(;a;){let s=await this.getNextPacket(a,e);if(!s)break;a=s}return a}getNextPacket(t,e){h(this.demuxer.audioInfo);let r=Math.round(t.timestamp*this.demuxer.audioInfo.sampleRate/ki);return this.getPacketAtIndex(r+1,e)}getKeyPacket(t,e){return this.getPacket(t,e)}getNextKeyPacket(t,e){return this.getNextPacket(t,e)}};var Qt=7,Ne=9,De=n=>{let t=n.filePos,e=B(n,9),r=new W(e);if(r.readBits(12)!==4095||(r.skipBits(1),r.readBits(2)!==0))return null;let s=r.readBits(1),o=r.readBits(2)+1,c=r.readBits(4);if(c===15)return null;r.skipBits(1);let u=r.readBits(3);if(u===0)throw new Error("ADTS frames with channel configuration 0 are not supported.");r.skipBits(1),r.skipBits(1),r.skipBits(1),r.skipBits(1);let l=r.readBits(13);r.skipBits(11);let m=r.readBits(2)+1;if(m!==1)throw new Error("ADTS frames with more than one AAC frame are not supported.");let d=null;return s===1?n.filePos-=2:d=r.readBits(16),{objectType:o,samplingFrequencyIndex:c,channelConfiguration:u,frameLength:l,numberOfAacFrames:m,crcCheck:d,startPos:t}};var hn=1024,Ba=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.firstFrameHeader=null;this.loadedSamples=[];this.metadataTags=null;this.trackBackings=[];this.readingMutex=new Ie;this.lastSampleLoaded=!1;this.lastLoadedPos=0;this.nextTimestampInSamples=0;this.reader=e._reader}async readMetadata(){return this.metadataPromise??=(async()=>{for(;!this.firstFrameHeader&&!this.lastSampleLoaded;)await this.advanceReader();h(this.firstFrameHeader),this.trackBackings=[new Bo(this)]})()}async advanceReader(){if(this.lastLoadedPos===0)for(;;){let o=this.reader.requestSlice(this.lastLoadedPos,ze);if(o instanceof Promise&&(o=await o),!o){this.lastSampleLoaded=!0;return}let c=ft(o);if(!c)break;this.lastLoadedPos=o.filePos+c.size}let e=this.reader.requestSliceRange(this.lastLoadedPos,Qt,Ne);if(e instanceof Promise&&(e=await e),!e){this.lastSampleLoaded=!0;return}let r=De(e);if(!r){this.lastSampleLoaded=!0;return}if(this.reader.fileSize!==null&&r.startPos+r.frameLength>this.reader.fileSize){this.lastSampleLoaded=!0;return}this.firstFrameHeader||(this.firstFrameHeader=r);let i=qe[r.samplingFrequencyIndex];h(i!==void 0);let a=hn/i,s={timestamp:this.nextTimestampInSamples/i,duration:a,dataStart:r.startPos,dataSize:r.frameLength};this.loadedSamples.push(s),this.nextTimestampInSamples+=hn,this.lastLoadedPos=r.startPos+r.frameLength}async getMimeType(){return"audio/aac"}async getTrackBackings(){return await this.readMetadata(),this.trackBackings}async getMetadataTags(){let e=await this.readingMutex.acquire();try{if(await this.readMetadata(),this.metadataTags)return this.metadataTags;this.metadataTags={};let r=0;for(;;){let i=this.reader.requestSlice(r,ze);if(i instanceof Promise&&(i=await i),!i)break;let a=ft(i);if(!a)break;let s=this.reader.requestSlice(i.filePos,a.size);if(s instanceof Promise&&(s=await s),!s)break;bi(s,a,this.metadataTags),r=i.filePos+a.size}return this.metadataTags}finally{e()}}},Bo=class{constructor(t){this.demuxer=t}getType(){return"audio"}getId(){return 1}getNumber(){return 1}getTimeResolution(){return this.getSampleRate()/hn}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){return null}async getLiveRefreshInterval(){return null}getName(){return null}getLanguageCode(){return J}getCodec(){return"aac"}getInternalCodecId(){return h(this.demuxer.firstFrameHeader),this.demuxer.firstFrameHeader.objectType}getNumberOfChannels(){h(this.demuxer.firstFrameHeader);let t=kt[this.demuxer.firstFrameHeader.channelConfiguration];return h(t!==void 0),t}getSampleRate(){h(this.demuxer.firstFrameHeader);let t=qe[this.demuxer.firstFrameHeader.samplingFrequencyIndex];return h(t!==void 0),t}getDisposition(){return{...we}}async getDecoderConfig(){return h(this.demuxer.firstFrameHeader),{codec:`mp4a.40.${this.demuxer.firstFrameHeader.objectType}`,numberOfChannels:this.getNumberOfChannels(),sampleRate:this.getSampleRate()}}async getPacketAtIndex(t,e){if(t===-1)return null;let r=this.demuxer.loadedSamples[t];if(!r)return null;let i;if(e.metadataOnly)i=de;else{let a=this.demuxer.reader.requestSlice(r.dataStart,r.dataSize);if(a instanceof Promise&&(a=await a),!a)return null;i=B(a,r.dataSize)}return new Q(i,"key",r.timestamp,r.duration,t,r.dataSize)}getFirstPacket(t){return this.getPacketAtIndex(0,t)}async getNextPacket(t,e){let r=await this.demuxer.readingMutex.acquire();try{let i=Ft(this.demuxer.loadedSamples,t.timestamp,s=>s.timestamp);if(i===-1)throw new Error("Packet was not created from this track.");let a=i+1;for(;a>=this.demuxer.loadedSamples.length&&!this.demuxer.lastSampleLoaded;)await this.demuxer.advanceReader();return this.getPacketAtIndex(a,e)}finally{r()}}async getPacket(t,e){let r=await this.demuxer.readingMutex.acquire();try{for(;;){let i=H(this.demuxer.loadedSamples,t,a=>a.timestamp);if(i===-1&&this.demuxer.loadedSamples.length>0)return null;if(this.demuxer.lastSampleLoaded)return this.getPacketAtIndex(i,e);if(i>=0&&i+1<this.demuxer.loadedSamples.length)return this.getPacketAtIndex(i,e);await this.demuxer.advanceReader()}}finally{r()}}getKeyPacket(t,e){return this.getPacket(t,e)}getNextKeyPacket(t,e){return this.getNextPacket(t,e)}};var Oa=n=>n===0?null:n===1?192:n>=2&&n<=5?144*2**n:n===6?"uncommon-u8":n===7?"uncommon-u16":n>=8&&n<=15?2**n:null,Il=(n,t)=>{switch(n){case 0:return t;case 1:return 88200;case 2:return 176400;case 3:return 192e3;case 4:return 8e3;case 5:return 16e3;case 6:return 22050;case 7:return 24e3;case 8:return 32e3;case 9:return 44100;case 10:return 48e3;case 11:return 96e3;case 12:return"uncommon-u8";case 13:return"uncommon-u16";case 14:return"uncommon-u16-10";default:return null}},Da=n=>{let t=0,e=new W(B(n,1));for(;e.readBits(1)===1;)t++;if(t===0)return e.readBits(7);let r=[],i=t-1,a=new W(B(n,i)),s=8-t-1;for(let c=0;c<s;c++)r.unshift(e.readBits(1));for(let c=0;c<i;c++)for(let u=0;u<8;u++){let l=a.readBits(1);u<2||r.unshift(l)}return r.reduce((c,u,l)=>c|u<<l,0)},Va=(n,t)=>{if(t==="uncommon-u16")return se(n)+1;if(t==="uncommon-u8")return O(n)+1;if(typeof t=="number")return t;ne(t),h(!1)},El=(n,t)=>t==="uncommon-u16"?se(n):t==="uncommon-u16-10"?se(n)*10:t==="uncommon-u8"?O(n):typeof t=="number"?t:null,vl=n=>{let e=0;for(let r of n){e^=r;for(let i=0;i<8;i++)(e&128)!==0?e=e<<1^7:e<<=1,e&=255}return e};var Ua=class extends pe{constructor(e){super(e);this.loadedSamples=[];this.metadataPromise=null;this.trackBacking=null;this.metadataTags={};this.audioInfo=null;this.lastLoadedPos=null;this.blockingBit=null;this.readingMutex=new Ie;this.lastSampleLoaded=!1;this.reader=e._reader}async getMetadataTags(){return await this.readMetadata(),this.metadataTags}async getTrackBackings(){return await this.readMetadata(),h(this.trackBacking),[this.trackBacking]}async getMimeType(){return"audio/flac"}async readMetadata(){let e=4;return this.metadataPromise??=(async()=>{for(;this.reader.fileSize===null||e<this.reader.fileSize;){let r=this.reader.requestSlice(e,4);if(r instanceof Promise&&(r=await r),e+=4,r===null)throw new Error(`Metadata block at position ${e} is too small! Corrupted file.`);h(r);let i=O(r),a=ut(r),s=(i&128)!==0;switch(i&127){case 0:{let c=this.reader.requestSlice(e,a);if(c instanceof Promise&&(c=await c),h(c),c===null)throw new Error(`StreamInfo block at position ${e} is too small! Corrupted file.`);let u=B(c,34),l=new W(u),m=l.readBits(16),d=l.readBits(16),f=l.readBits(24),p=l.readBits(24),b=l.readBits(20),g=l.readBits(3)+1;l.readBits(5);let y=l.readBits(36);l.skipBits(16*8);let k=new Uint8Array(42);k.set(new Uint8Array([102,76,97,67]),0),k.set(new Uint8Array([128,0,0,34]),4),k.set(u,8),this.audioInfo={numberOfChannels:g,sampleRate:b,totalSamples:y,minimumBlockSize:m,maximumBlockSize:d,minimumFrameSize:f,maximumFrameSize:p,description:k},this.trackBacking=new Oo(this);break}case 4:{let c=this.reader.requestSlice(e,a);c instanceof Promise&&(c=await c),h(c),rn(B(c,a),this.metadataTags);break}case 6:{let c=this.reader.requestSlice(e,a);c instanceof Promise&&(c=await c),h(c);let u=F(c),l=F(c),m=me.decode(B(c,l)),d=F(c),f=me.decode(B(c,d));c.skip(16);let p=F(c),b=B(c,p);this.metadataTags.images??=[],this.metadataTags.images.push({data:b,mimeType:m,kind:u===3?"coverFront":u===4?"coverBack":"unknown",description:f});break}default:break}if(e+=a,s){this.lastLoadedPos=e;break}}if(!this.audioInfo)throw new Error("Missing STREAMINFO metadata block! Corrupted FLAC file.")})()}async readNextFlacFrame({startPos:e,isFirstPacket:r}){h(this.audioInfo);let i=6,a=16,s=10,o=this.audioInfo.maximumBlockSize*this.audioInfo.numberOfChannels*4+a+2,c=this.audioInfo.minimumFrameSize||s,l=(this.audioInfo.maximumFrameSize||o)+a,m=await this.reader.requestSliceRange(e,a,l);if(!m)return null;let d=this.readFlacFrameHeader({slice:m,isFirstPacket:r});if(!d)return null;for(m.filePos=e+c;;){if(m.filePos>m.end-i)return{num:d.num,blockSize:d.blockSize,sampleRate:d.sampleRate,size:m.end-e,isLastFrame:!0};if(O(m)===255){let p=m.filePos,b=O(m),g=this.blockingBit===1?249:248;if(b!==g){m.filePos=p;continue}m.skip(-2);let y=m.filePos-e,k=this.readFlacFrameHeader({slice:m,isFirstPacket:!1});if(!k){m.filePos=p;continue}if(this.blockingBit===0){if(k.num-d.num!==1){m.filePos=p;continue}}else if(k.num-d.num!==d.blockSize){m.filePos=p;continue}return{num:d.num,blockSize:d.blockSize,sampleRate:d.sampleRate,size:y,isLastFrame:!1}}}}readFlacFrameHeader({slice:e,isFirstPacket:r}){let i=e.filePos,a=B(e,4),s=new W(a);if(s.readBits(15)!==32764)return null;if(this.blockingBit===null){h(r);let y=s.readBits(1);this.blockingBit=y}else if(this.blockingBit===1){if(h(!r),s.readBits(1)!==1)return null}else if(this.blockingBit===0){if(h(!r),s.readBits(1)!==0)return null}else throw new Error("Invalid blocking bit");let c=Oa(s.readBits(4));if(!c)return null;h(this.audioInfo);let u=Il(s.readBits(4),this.audioInfo.sampleRate);if(!u||(s.readBits(4),s.readBits(3),s.readBits(1)!==0))return null;let m=Da(e),d=Va(e,c),f=El(e,u);if(f===null||f!==this.audioInfo.sampleRate)return null;let p=e.filePos-i,b=O(e);e.skip(-p),e.skip(-1);let g=vl(B(e,p));return b!==g?null:{num:m,blockSize:d,sampleRate:f}}async advanceReader(){await this.readMetadata(),h(this.lastLoadedPos!==null),h(this.audioInfo);let e=this.lastLoadedPos,r=await this.readNextFlacFrame({startPos:e,isFirstPacket:this.loadedSamples.length===0});if(!r){this.lastSampleLoaded=!0;return}let i=this.loadedSamples[this.loadedSamples.length-1],s={blockOffset:i?i.blockOffset+i.blockSize:0,blockSize:r.blockSize,byteOffset:e,byteSize:r.size};if(this.lastLoadedPos=this.lastLoadedPos+r.size,this.loadedSamples.push(s),r.isLastFrame){this.lastSampleLoaded=!0;return}}},Oo=class{constructor(t){this.demuxer=t}getType(){return"audio"}getId(){return 1}getNumber(){return 1}getCodec(){return"flac"}getInternalCodecId(){return null}getNumberOfChannels(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.numberOfChannels}getSampleRate(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.sampleRate}getName(){return null}getLanguageCode(){return J}getTimeResolution(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.sampleRate}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){return h(this.demuxer.audioInfo),this.demuxer.audioInfo.totalSamples===0?null:this.demuxer.audioInfo.totalSamples/this.demuxer.audioInfo.sampleRate}async getLiveRefreshInterval(){return null}getDisposition(){return{...we}}async getDecoderConfig(){return h(this.demuxer.audioInfo),{codec:"flac",numberOfChannels:this.demuxer.audioInfo.numberOfChannels,sampleRate:this.demuxer.audioInfo.sampleRate,description:this.demuxer.audioInfo.description}}async getPacket(t,e){if(h(this.demuxer.audioInfo),t<0)return null;let r=await this.demuxer.readingMutex.acquire();try{for(;;){let i=H(this.demuxer.loadedSamples,t,c=>c.blockOffset/this.demuxer.audioInfo.sampleRate);if(i===-1){await this.demuxer.advanceReader();continue}let a=this.demuxer.loadedSamples[i],s=a.blockOffset/this.demuxer.audioInfo.sampleRate,o=a.blockSize/this.demuxer.audioInfo.sampleRate;if(s+o<=t){if(this.demuxer.lastSampleLoaded)return this.getPacketAtIndex(this.demuxer.loadedSamples.length-1,e);await this.demuxer.advanceReader();continue}return this.getPacketAtIndex(i,e)}}finally{r()}}async getNextPacket(t,e){let r=await this.demuxer.readingMutex.acquire();try{let i=t.sequenceNumber+1;if(this.demuxer.lastSampleLoaded&&i>=this.demuxer.loadedSamples.length)return null;for(;i>=this.demuxer.loadedSamples.length&&!this.demuxer.lastSampleLoaded;)await this.demuxer.advanceReader();return this.getPacketAtIndex(i,e)}finally{r()}}getKeyPacket(t,e){return this.getPacket(t,e)}getNextKeyPacket(t,e){return this.getNextPacket(t,e)}async getPacketAtIndex(t,e){let r=this.demuxer.loadedSamples[t];if(!r)return null;let i;if(e.metadataOnly)i=de;else{let o=this.demuxer.reader.requestSlice(r.byteOffset,r.byteSize);if(o instanceof Promise&&(o=await o),!o)return null;i=B(o,r.byteSize)}h(this.demuxer.audioInfo);let a=r.blockOffset/this.demuxer.audioInfo.sampleRate,s=r.blockSize/this.demuxer.audioInfo.sampleRate;return new Q(i,"key",a,s,t,r.byteSize)}async getFirstPacket(t){for(;this.demuxer.loadedSamples.length===0&&!this.demuxer.lastSampleLoaded;)await this.demuxer.advanceReader();return this.getPacketAtIndex(0,t)}};var za=n=>{let t="video/MP2T",e=[...new Set(n.filter(Boolean))];return e.length>0&&(t+=`; codecs="${e.join(", ")}"`),t};var Fl="PES packet is missing PTS where it was expected. PES packets without PTS are not currently supported. If you think this file should be supported, please report it.",_l=new Set,Na=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.elementaryStreams=[];this.trackBackingEntries=[];this.packetOffset=0;this.packetStride=-1;this.sectionEndPositions=[];this.seekChunkSize=5*1024*1024;this.minReferencePointByteDistance=-1;this.reader=e._reader}async readMetadata(){return this.metadataPromise??=(async()=>{let e=205,r=this.reader.requestSlice(0,e);r instanceof Promise&&(r=await r),h(r);let i=B(r,e);if(i[0]===71&&i[188]===71)this.packetOffset=0,this.packetStride=188;else if(i[0]===71&&i[204]===71)this.packetOffset=0,this.packetStride=204;else if(i[4]===71&&i[196]===71)this.packetOffset=4,this.packetStride=192;else throw new Error("Unreachable.");let a=256;this.minReferencePointByteDistance=a*this.packetStride;let s=this.packetOffset,o=null,c=!1,u=!1;for(;;){let l=await this.readPacketHeader(s);if(!l)break;if(l.payloadUnitStartIndicator===0){s+=this.packetStride;continue}let m=await this.readSection(s,!0,!u);if(!m)break;let d=3,f=32,p=!1;if(!u&&m.pid!==0&&!(m.payload[0]===0&&m.payload[1]===0&&m.payload[2]===1)){let y=new W(m.payload),k=y.readAlignedByte();y.skipBits(8*k),p=y.readBits(8)===2}if(m.pid===0&&!c){let g=new W(m.payload),y=g.readAlignedByte();g.skipBits(8*y),g.skipBits(14);let k=g.readBits(10);for(g.skipBits(40);8*(k+d)-g.pos>f;){let w=g.readBits(16);g.skipBits(3);let T=g.readBits(13);if(w!==0){if(o!==null)throw new Error("Only files with a single program are supported.");o=T}}if(o===null)throw new Error("Program Association Table must link to a Program Map Table.");c=!0}else if((m.pid===o||p)&&!u){let g=new W(m.payload),y=g.readAlignedByte();g.skipBits(8*y),g.skipBits(12);let k=g.readBits(12);g.skipBits(43);let w=g.readBits(13);g.skipBits(6);let T=g.readBits(10);for(g.skipBits(8*T);8*(k+d)-g.pos>f;){let S=g.readBits(8);g.skipBits(3);let x=g.readBits(13);g.skipBits(6);let P=g.readBits(10),A=g.pos+8*P,C=!1,I=!1;for(;g.pos<A;){let v=g.readBits(8),E=g.readBits(8);v===106?C=!0:(v===122||v===204)&&(I=!0),g.skipBits(8*E)}let R=null;switch(S){case 27:case 36:R={type:"video",codec:S===27?"avc":"hevc",decoderConfig:null,avcCodecInfo:null,hevcCodecInfo:null,colorSpace:{primaries:null,transfer:null,matrix:null,fullRange:null},width:-1,height:-1,squarePixelWidth:-1,squarePixelHeight:-1,reorderSize:-1};break;case 3:case 4:case 15:case 129:case 135:{let v;if(S===3||S===4)v="mp3";else if(S===15)v="aac";else if(S===129)v="ac3";else if(S===135)v="eac3";else throw new Error("Unreachable.");R={type:"audio",codec:v,decoderConfig:null,aacCodecInfo:null,numberOfChannels:-1,sampleRate:-1}}break;case 6:I?R={type:"audio",codec:"eac3",decoderConfig:null,aacCodecInfo:null,numberOfChannels:-1,sampleRate:-1}:C&&(R={type:"audio",codec:"ac3",decoderConfig:null,aacCodecInfo:null,numberOfChannels:-1,sampleRate:-1});break;default:_l.has(S)||(console.warn(`Note: MPEG-TS streams with stream_type 0x${S.toString(16)} are not currently supported.`),_l.add(S))}R&&this.elementaryStreams.push({demuxer:this,pid:x,streamType:S,initialized:!1,firstSection:null,canBeTrustedWithKeyPackets:!1,info:R,referencePesPackets:[]})}u=!0}else{let g=this.elementaryStreams.find(y=>y.pid===m.pid);e:if(g&&!g.initialized){let y=wi(m,!0);if(!y)throw new Error(`Couldn't read first PES packet for Elementary Stream with PID ${g.pid}`);if(g.firstSection=m,g.canBeTrustedWithKeyPackets=m.randomAccessIndicator===1,this.input._initInput){let T=(await this.input._initInput._getDemuxer()).elementaryStreams.find(S=>S.pid===m.pid&&S.info.codec===g.info.codec);if(T){g.info=T.info,g.initialized=!0;break e}}let k=new zr(g,y);if(g.info.type==="video"){for(;;){let w=k;if(w.suppliedPacket=null,await k.markNextPacket(),g.info.codec==="avc"){if(!k.suppliedPacket)throw new Error("Invalid AVC video stream; could not extract AVCDecoderConfigurationRecord from any packet.");if(g.info.avcCodecInfo=si(k.suppliedPacket.data),!g.info.avcCodecInfo)continue;let T=g.info.avcCodecInfo.sequenceParameterSets[0];h(T);let S=tn(T);g.info.width=S.displayWidth,g.info.height=S.displayHeight;let x=S.pixelAspectRatio.num,P=S.pixelAspectRatio.den;x>0&&P>0&&(x>P?(g.info.squarePixelWidth=Math.round(g.info.width*x/P),g.info.squarePixelHeight=g.info.height):(g.info.squarePixelWidth=g.info.width,g.info.squarePixelHeight=Math.round(g.info.height*P/x))),g.info.colorSpace={primaries:xr[S.colourPrimaries],transfer:Pr[S.transferCharacteristics],matrix:Cr[S.matrixCoefficients],fullRange:!!S.fullRangeFlag},g.info.reorderSize=S.maxDecFrameBuffering;break}else if(g.info.codec==="hevc"){if(!k.suppliedPacket)throw new Error("Invalid HEVC video stream; could not extract HVCDecoderConfigurationRecord from first packet.");if(g.info.hevcCodecInfo=oi(k.suppliedPacket.data),!g.info.hevcCodecInfo)continue;let S=g.info.hevcCodecInfo.arrays.find(P=>P.nalUnitType===33).nalUnits[0];h(S);let x=ko(S);g.info.width=x.displayWidth,g.info.height=x.displayHeight,x.pixelAspectRatio.num>x.pixelAspectRatio.den?(g.info.squarePixelWidth=Math.round(g.info.width*x.pixelAspectRatio.num/x.pixelAspectRatio.den),g.info.squarePixelHeight=g.info.height):(g.info.squarePixelWidth=g.info.width,g.info.squarePixelHeight=Math.round(g.info.height*x.pixelAspectRatio.den/x.pixelAspectRatio.num)),g.info.colorSpace={primaries:xr[x.colourPrimaries],transfer:Pr[x.transferCharacteristics],matrix:Cr[x.matrixCoefficients],fullRange:!!x.fullRangeFlag},g.info.reorderSize=x.maxDecFrameBuffering;break}else throw new Error("Unhandled.")}g.info.decoderConfig={codec:ii({width:g.info.width,height:g.info.height,codec:g.info.codec,codecDescription:null,colorSpace:g.info.colorSpace,avcType:1,avcCodecInfo:g.info.avcCodecInfo,hevcCodecInfo:g.info.hevcCodecInfo,vp9CodecInfo:null,av1CodecInfo:null}),codedWidth:g.info.width,codedHeight:g.info.height,colorSpace:g.info.colorSpace},(g.info.width!==g.info.squarePixelWidth||g.info.height!==g.info.squarePixelHeight)&&(g.info.decoderConfig.displayAspectWidth=g.info.squarePixelWidth,g.info.decoderConfig.displayAspectHeight=g.info.squarePixelHeight),g.initialized=!0}else{if(await k.markNextPacket(),!k.suppliedPacket)throw new Error(`Couldn't parse first media packet for Elementary Stream with PID ${g.pid}`);if(g.info.codec==="aac"){let w=he.tempFromBytes(k.suppliedPacket.data),T=De(w);if(!T)throw new Error("Invalid AAC audio stream; could not read ADTS frame header from first packet.");g.info.aacCodecInfo={isMpeg2:!1,objectType:T.objectType},g.info.numberOfChannels=kt[T.channelConfiguration],g.info.sampleRate=qe[T.samplingFrequencyIndex]}else if(g.info.codec==="mp3"){let w=F(he.tempFromBytes(k.suppliedPacket.data)),T=Br(w,k.suppliedPacket.data.byteLength);if(!T.header)throw new Error("Invalid MP3 audio stream; could not read frame header from first packet.");g.info.numberOfChannels=T.header.channel===3?1:2,g.info.sampleRate=T.header.sampleRate}else if(g.info.codec==="ac3"){let w=sa(k.suppliedPacket.data);if(!w)throw new Error("Invalid AC-3 audio stream; could not read sync frame from first packet.");if(w.fscod===3)throw new Error("Invalid AC-3 audio stream; reserved sample rate code found in first packet.");g.info.numberOfChannels=an[w.acmod]+w.lfeon,g.info.sampleRate=Or[w.fscod]}else if(g.info.codec==="eac3"){let w=oa(k.suppliedPacket.data);if(!w)throw new Error("Invalid E-AC-3 audio stream; could not read sync frame from first packet.");let T=ca(w);if(T===null)throw new Error("Invalid E-AC-3 audio stream; reserved sample rate code found in first packet.");g.info.numberOfChannels=ua(w),g.info.sampleRate=T}else throw new Error("Unhandled.");g.info.decoderConfig={codec:ni({codec:g.info.codec,codecDescription:null,aacCodecInfo:g.info.aacCodecInfo}),numberOfChannels:g.info.numberOfChannels,sampleRate:g.info.sampleRate},g.initialized=!0}}}if(u&&this.elementaryStreams.every(g=>g.initialized))break;s+=this.packetStride}if(!u)throw c?new Error("No Program Map Table found in the file."):new Error("No Program Association Table found in the file.");for(let l of this.elementaryStreams)l.info.type==="video"?this.trackBackingEntries.push(new Do(l)):this.trackBackingEntries.push(new Vo(l))})()}async getTrackBackings(){return await this.readMetadata(),this.trackBackingEntries}async getMetadataTags(){return{}}async getMimeType(){await this.readMetadata();let e=await Promise.all(this.trackBackingEntries.map(r=>r.getDecoderConfig().then(i=>i?.codec??null)));return za(e)}async readSection(e,r,i=!1){let a=e,s=e,o=[],c=0,u=null,l=!0,m=0;for(;;){let f=await this.readPacket(s);if(s+=this.packetStride,!f)break;if(u){if(f.pid!==u.pid){if(i)break;continue}if(f.payloadUnitStartIndicator===1)break}else{if(f.payloadUnitStartIndicator===0)break;u=f}let p=!!(f.adaptationFieldControl&2),b=!!(f.adaptationFieldControl&1),g=0;if(p&&(g=1+f.body[0],f===u&&g>1&&(m=f.body[1]>>6&1)),b&&(g===0?(o.push(f.body),c+=f.body.byteLength):(o.push(f.body.subarray(g)),c+=f.body.byteLength-g)),a=s,!r&&c>=64){l=!1;break}if(Ft(this.sectionEndPositions,a,k=>k)!==-1){l=!1;break}}if(l){let f=H(this.sectionEndPositions,a,p=>p);this.sectionEndPositions.splice(f+1,0,a)}if(!u)return null;let d;if(o.length===1)d=o[0];else{let f=o.reduce((b,g)=>b+g.length,0);d=new Uint8Array(f);let p=0;for(let b of o)d.set(b,p),p+=b.length}return{startPos:e,endPos:r?a:null,pid:u.pid,payload:d,randomAccessIndicator:m}}async readPacketHeader(e){let r=this.reader.requestSlice(e,4);if(r instanceof Promise&&(r=await r),!r)return null;if(O(r)!==71)throw new Error("Invalid TS packet sync byte. Likely an internal bug, please report this file.");let a=se(r),s=a>>15,o=a>>14&1,c=a>>13&1,u=a&8191,l=O(r),m=l>>6,d=l>>4&3,f=l&15;return{payloadUnitStartIndicator:o,pid:u,adaptationFieldControl:d}}async readPacket(e){let r=this.reader.requestSlice(e,188);if(r instanceof Promise&&(r=await r),!r)return null;let i=B(r,188);if(i[0]!==71)throw new Error("Invalid TS packet sync byte. Likely an internal bug, please report this file.");let s=(i[1]<<8)+i[2],o=s>>15,c=s>>14&1,u=s>>13&1,l=s&8191,m=i[3],d=m>>6,f=m>>4&3,p=m&15;return{payloadUnitStartIndicator:c,pid:l,adaptationFieldControl:f,body:i.subarray(4)}}},Ur=(n,t)=>{if(n.payload.byteLength<3)return null;let e=new W(n.payload);if(e.readBits(24)!==1)return null;let i=e.readBits(8);if(e.skipBits(16),i===188||i===190||i===191||i===240||i===241||i===255||i===242||i===248)return null;e.skipBits(8);let a=e.readBits(2);e.skipBits(14);let s=null;if(a===2||a===3)s=0,e.skipBits(4),s+=e.readBits(3)*(1<<30),e.skipBits(1),s+=e.readBits(15)*32768,e.skipBits(1),s+=e.readBits(15);else if(t)throw new Error(Fl);return{sectionStartPos:n.startPos,sectionEndPos:n.endPos,pts:s,randomAccessIndicator:n.randomAccessIndicator}},wi=(n,t)=>{h(n.endPos!==null);let e=Ur(n,t);if(!e)return null;let r=new W(n.payload);r.skipBits(32);let i=r.readBits(16),a=6;r.skipBits(16);let s=r.readBits(8),o=r.pos+8*s;r.pos=o;let c=o/8;h(Number.isInteger(c));let u=n.payload.subarray(c,i>0?a+i:n.payload.byteLength);return{...e,data:u}},La=class n{constructor(t){this.elementaryStream=t;this.packetBuffers=new WeakMap;this.packetSectionStarts=new WeakMap}getId(){return this.elementaryStream.pid}getNumber(){let t=this.elementaryStream.demuxer,e=this.elementaryStream.info.type,r=0;for(let i of t.trackBackingEntries)if(i.getType()===e&&r++,h(i instanceof n),i.elementaryStream===this.elementaryStream)break;return r}getCodec(){throw new Error("Not implemented on base class.")}getInternalCodecId(){return this.elementaryStream.streamType}getName(){return null}getLanguageCode(){return J}getDisposition(){return{...we,primary:!1}}getTimeResolution(){return 9e4}isRelativeToUnixEpoch(){return!1}getPairingMask(){return 1n}getBitrate(){return null}getAverageBitrate(){return null}async getDurationFromMetadata(){return null}async getLiveRefreshInterval(){return null}createEncodedPacket(t,e,r){let i;return this.allPacketsAreKeyPackets()?i="key":i=t.randomAccessIndicator===1?"key":"delta",new Q(r.metadataOnly?de:t.data,i,t.pts/9e4,Math.max(e/9e4,0),t.sequenceNumber,t.data.byteLength)}async getFirstPacket(t){let e=this.elementaryStream.firstSection;h(e);let r=wi(e,!0);h(r);let i=new zr(this.elementaryStream,r),a=new gn(this,i),s=await a.readNext();if(!s)return null;let o=this.createEncodedPacket(s.packet,s.duration,t);return this.packetBuffers.set(o,a),this.packetSectionStarts.set(o,s.packet.sectionStartPos),o}async getNextPacket(t,e){let r=this.packetBuffers.get(t);if(r){let l=await r.readNext();if(!l)return null;this.packetBuffers.delete(t);let m=this.createEncodedPacket(l.packet,l.duration,e);return this.packetBuffers.set(m,r),this.packetSectionStarts.set(m,l.packet.sectionStartPos),m}let i=this.packetSectionStarts.get(t);if(i===void 0)throw new Error("Packet was not created from this track.");let s=await this.elementaryStream.demuxer.readSection(i,!0);h(s);let o=wi(s,!0);h(o);let c=new zr(this.elementaryStream,o);r=new gn(this,c);let u=t.sequenceNumber;for(;;){let l=await r.readNext();if(!l)return null;if(l.packet.sequenceNumber>u){let m=this.createEncodedPacket(l.packet,l.duration,e);return this.packetBuffers.set(m,r),this.packetSectionStarts.set(m,l.packet.sectionStartPos),m}}}async getNextKeyPacket(t,e){let r=t;for(;;){if(r=await this.getNextPacket(r,e),!r)return null;if(r.type==="key")return r}}getPacket(t,e){return this.doPacketLookup(t,!1,e)}getKeyPacket(t,e){return this.doPacketLookup(t,!0,e)}async doPacketLookup(t,e,r){let i=Mt(t*9e4),a=this.elementaryStream.demuxer,{reader:s,seekChunkSize:o}=a,c=this.elementaryStream.pid,u=async(T,S,x)=>{let P=T;for(;P<S;){let A=await a.readPacketHeader(P);if(!A)return null;if(A.pid===c&&A.payloadUnitStartIndicator===1){let C=await a.readSection(P,x);if(!C)return null;let I=Ur(C,!1);if(I&&I.pts!==null)return{pesPacketHeader:I,section:C}}P+=a.packetStride}return null},l=this.elementaryStream.firstSection;h(l);let m=Ur(l,!0);if(h(m),i<m.pts)return null;let d,f=this.elementaryStream.referencePesPackets,p=H(f,i,T=>T.pts),b=p!==-1?f[p]:null;if(b&&i-b.pts<9e4/2)d=b.sectionStartPos;else{let T=0;if(s.fileSize!==null){let S=Math.ceil(s.fileSize/o);if(S>1){let x=0,P=S-1;for(T=x;x<=P;){let A=Math.floor((x+P)/2),C=Nn(A*o,a.packetStride)+m.sectionStartPos,I=C+o,R=await u(C,I,!1);if(!R){P=A-1;continue}R.pesPacketHeader.pts<=i?(T=A,x=A+1):P=A-1}}}d=Nn(T*o,a.packetStride)+m.sectionStartPos}let y=(await u(d,s.fileSize??1/0,!1))?.pesPacketHeader??null;y||(y=m);let k=this.getReorderSize(),w=async(T,S)=>{let x=await a.readSection(T,!0);h(x);let P=wi(x,!0);h(P);let A=new zr(this.elementaryStream,P),C=new gn(this,A);for(;!((X(C.presentationOrderPackets)?.pts??-1/0)>=i||!await C.readNextPacket()););let I=rr(C.presentationOrderPackets,S);if(I===-1)return null;let R=C.presentationOrderPackets[I],v=I===0?0:R.pts-C.presentationOrderPackets[I-1].pts;for(;C.decodeOrderPackets[0]!==R;)C.decodeOrderPackets.shift();C.lastDuration=v;let E=await C.readNext();h(E);let D=this.createEncodedPacket(E.packet,E.duration,r);return this.packetBuffers.set(D,C),this.packetSectionStarts.set(D,E.packet.sectionStartPos),D};if(!e||this.allPacketsAreKeyPackets()){e:for(;;){let T=y.sectionStartPos+a.packetStride;for(;;){let S=await a.readPacketHeader(T);if(!S)break e;if(S.pid===c&&S.payloadUnitStartIndicator===1){let x=await a.readSection(T,!1);if(x){let P=Ur(x,!1);if(P&&P.pts!==null){if(P.pts>i)break e;y=P,Uo(this.elementaryStream,y);break}}}T+=a.packetStride}}e:for(let T=0;T<k+1;T++){let S=y.sectionStartPos-a.packetStride;for(;S>=a.packetOffset;){let x=await a.readPacketHeader(S);if(!x)break e;if(x.pid===c&&x.payloadUnitStartIndicator===1){let P=await a.readSection(S,!1);if(P){let A=Ur(P,!1);if(A&&A.pts!==null){y=A;break}}}S-=a.packetStride}}return w(y.sectionStartPos,T=>T.pts<=i)}else{let T=d,S=null,x=!this.elementaryStream.canBeTrustedWithKeyPackets;for(;;){let P=null,A=T<=m.sectionStartPos,C,I=null;if(A)C=m,I=l;else{let E=await u(T,s.fileSize??1/0,x);C=E?.pesPacketHeader??null,I=E?.section??null}let R=!1,v=0;e:for(;C&&!(S!==null&&C.sectionStartPos>=S);){if(C.pts<=i){let D;if(this.elementaryStream.canBeTrustedWithKeyPackets)D=C.randomAccessIndicator===1;else{h(I);let U=wi(I,!0);h(U);let z=new zr(this.elementaryStream,U);await z.markNextPacket(),D=z.suppliedPacket?.randomAccessIndicator===1}D&&(P=C)}if(C.pts>i&&(R=!0),R&&(v++,v>k))break;let E=C.sectionStartPos+a.packetStride;for(;;){let D=await a.readPacketHeader(E);if(!D)break e;if(D.pid===c&&D.payloadUnitStartIndicator===1){let U=await a.readSection(E,x);if(U){let z=Ur(U,!1);if(z&&z.pts!==null){C=z,I=U,Uo(this.elementaryStream,C);break}}}E+=a.packetStride}}if(P){let E=P;if(v===0)e:for(let U=0;U<k;U++){let z=E.sectionStartPos-a.packetStride;for(;z>=a.packetOffset;){let q=await a.readPacketHeader(z);if(!q)break e;if(q.pid===c&&q.payloadUnitStartIndicator===1){let ke=await a.readSection(z,x);if(ke){let Te=Ur(ke,!1);if(Te&&Te.pts!==null){E=Te;break}}}z-=a.packetStride}}let D=await w(E.sectionStartPos,U=>U.pts<=i&&U.randomAccessIndicator===1);return h(D),D}if(A)return null;S=T,T=Math.max(Nn(T-m.sectionStartPos-o,a.packetStride)+m.sectionStartPos,m.sectionStartPos)}}}},Do=class extends La{getType(){return"video"}getCodec(){return this.elementaryStream.info.codec}getCodedWidth(){return this.elementaryStream.info.width}getCodedHeight(){return this.elementaryStream.info.height}getSquarePixelWidth(){return this.elementaryStream.info.squarePixelWidth}getSquarePixelHeight(){return this.elementaryStream.info.squarePixelHeight}getRotation(){return 0}async getColorSpace(){return this.elementaryStream.info.colorSpace}async canBeTransparent(){return!1}async getDecoderConfig(){return h(this.elementaryStream.info.decoderConfig),this.elementaryStream.info.decoderConfig}allPacketsAreKeyPackets(){return!1}getReorderSize(){return this.elementaryStream.info.reorderSize}},Vo=class extends La{getType(){return"audio"}getCodec(){return this.elementaryStream.info.codec}getNumberOfChannels(){return this.elementaryStream.info.numberOfChannels}getSampleRate(){return this.elementaryStream.info.sampleRate}async getDecoderConfig(){return h(this.elementaryStream.info.decoderConfig),this.elementaryStream.info.decoderConfig}allPacketsAreKeyPackets(){return!0}getReorderSize(){return 0}},Uo=(n,t)=>{let e=n.referencePesPackets,r=H(e,t.sectionStartPos,i=>i.sectionStartPos);if(r>=0){let i=e[r];if(t.pts<=i.pts)return!1;let a=n.demuxer.minReferencePointByteDistance;if(t.sectionStartPos-i.sectionStartPos<a)return!1;if(r<e.length-1){let s=e[r+1];if(s.pts<t.pts||s.sectionStartPos-t.sectionStartPos<a)return!1}}return e.splice(r+1,0,t),!0},zr=class{constructor(t,e){this.currentPos=0;this.pesPackets=[];this.currentPesPacketIndex=0;this.currentPesPacketPos=0;this.endPos=0;this.lastSuppliedPesPacket=null;this.nextPts=null;this.suppliedPacket=null;this.elementaryStream=t,this.pid=t.pid,this.demuxer=t.demuxer,this.startingPesPacket=e}ensureBuffered(t){let e=this.endPos-this.currentPos;return e>=t?t:this.bufferData(t-e).then(()=>Math.min(this.endPos-this.currentPos,t))}getCurrentPesPacket(){let t=this.pesPackets[this.currentPesPacketIndex];return h(t),t}async bufferData(t){let e=this.endPos+t;for(;this.endPos<e;){let r;if(this.pesPackets.length===0)r=this.startingPesPacket;else{let i=X(this.pesPackets).sectionEndPos;for(h(i!==null);;){let a=await this.demuxer.readPacketHeader(i);if(!a)return;if(a.pid===this.pid){let s=await this.demuxer.readSection(i,!0);if(!s)return;let o=wi(s,!1);if(o){r=o;break}}i+=this.demuxer.packetStride}}this.pesPackets.push(r),this.endPos+=r.data.byteLength}}readBytes(t){let e=this.getCurrentPesPacket(),r=this.currentPos-this.currentPesPacketPos,i=r+t;if(this.currentPos+=t,i<=e.data.byteLength)return e.data.subarray(r,i);let a=new Uint8Array(t);a.set(e.data.subarray(r));let s=e.data.byteLength-r;for(;;){this.advanceCurrentPacket();let o=this.getCurrentPesPacket(),c=t-s;if(c<=o.data.byteLength){a.set(o.data.subarray(0,c),s);break}a.set(o.data,s),s+=o.data.byteLength}return a}readU8(){let t=this.getCurrentPesPacket(),e=this.currentPos-this.currentPesPacketPos;return this.currentPos++,e<t.data.byteLength?t.data[e]:(this.advanceCurrentPacket(),t=this.getCurrentPesPacket(),t.data[0])}seekTo(t){if(t!==this.currentPos){if(t<this.currentPos)for(;t<this.currentPesPacketPos;){this.currentPesPacketIndex--;let e=this.getCurrentPesPacket();this.currentPesPacketPos-=e.data.byteLength}else for(;;){let e=this.getCurrentPesPacket(),r=this.currentPesPacketPos+e.data.byteLength;if(t<r)break;this.currentPesPacketPos+=e.data.byteLength,this.currentPesPacketIndex++}this.currentPos=t}}skip(t){this.seekTo(this.currentPos+t)}advanceCurrentPacket(){this.currentPesPacketPos+=this.getCurrentPesPacket().data.byteLength,this.currentPesPacketIndex++}async markNextPacket(){h(!this.suppliedPacket);let t=this.elementaryStream;if(t.info.type==="video"){let e=t.info.codec,r=1024;if(e!=="avc"&&e!=="hevc")throw new Error("Unhandled.");let i=null;for(;;){let a=this.ensureBuffered(r);if(a instanceof Promise&&(a=await a),a===0)break;let s=this.currentPos,o=this.readBytes(a),c=o.byteLength,u=0;for(;u<c;){let l=o.indexOf(0,u);if(l===-1||l>=c)break;u=l;let m=s+u;if(u+4>=c){this.seekTo(m);break}let d=o[u+1],f=o[u+2],p=o[u+3],b=0,g=null;if(d===0&&f===0&&p===1?(b=4,g=o[u+4]):d===0&&f===1&&(b=3,g=p),b===0){u++;continue}let y=m;if(i===null){i=y,u+=b;continue}if(g!==null){let k=e==="avc"?zt(g):at(g);if(e==="avc"?k===9:k===35){let T=y-i;return this.seekTo(i),this.supplyPacket(T,0)}}u+=b}if(a<r)break}if(i!==null){let a=this.endPos-i;return this.seekTo(i),this.supplyPacket(a,0)}}else{let e=t.info.codec,r=128;for(;;){let i=this.ensureBuffered(r);i instanceof Promise&&(i=await i);let a=this.currentPos;for(;this.currentPos-a<i;){let s=this.readU8();if(e==="aac"){if(s!==255)continue;this.skip(-1);let o=this.currentPos,c=this.ensureBuffered(Ne);if(c instanceof Promise&&(c=await c),c<Ne)return;let u=this.readBytes(Ne),l=De(he.tempFromBytes(u));if(l){this.seekTo(o);let m=this.ensureBuffered(l.frameLength);return m instanceof Promise&&(m=await m),this.supplyPacket(m,Math.round(hn*9e4/t.info.sampleRate))}else this.seekTo(o+1)}else if(e==="mp3"){if(s!==255)continue;this.skip(-1);let o=this.currentPos,c=this.ensureBuffered(4);if(c instanceof Promise&&(c=await c),c<4)return;let u=this.readBytes(4),l=V(u).getUint32(0),m=Br(l,null);if(m.header){this.seekTo(o);let d=this.ensureBuffered(m.header.totalSize);d instanceof Promise&&(d=await d);let f=m.header.audioSamplesInFrame*9e4/t.info.sampleRate;return this.supplyPacket(d,Math.round(f))}else this.seekTo(o+1)}else if(e==="ac3"){if(s!==11)continue;this.skip(-1);let o=this.currentPos,c=this.ensureBuffered(5);if(c instanceof Promise&&(c=await c),c<5)return;let u=this.readBytes(5);if(u[0]!==11||u[1]!==119){this.seekTo(o+1);continue}let l=u[4]>>6,m=u[4]&63;if(l===3||m>37){this.seekTo(o+1);continue}let d=el[3*m+l];h(d!==void 0),this.seekTo(o),c=this.ensureBuffered(d),c instanceof Promise&&(c=await c);let f=Math.round(tl*9e4/t.info.sampleRate);return this.supplyPacket(c,f)}else if(e==="eac3"){if(s!==11)continue;this.skip(-1);let o=this.currentPos,c=this.ensureBuffered(5);if(c instanceof Promise&&(c=await c),c<5)return;let u=this.readBytes(5);if(u[0]!==11||u[1]!==119){this.seekTo(o+1);continue}let m=(((u[2]&7)<<8|u[3])+1)*2,f=u[4]>>6===3?3:u[4]>>4&3,p=To[f];this.seekTo(o),c=this.ensureBuffered(m),c instanceof Promise&&(c=await c);let b=p*256,g=Math.round(b*9e4/t.info.sampleRate);return this.supplyPacket(c,g)}else throw new Error("Unhandled.")}if(i<r)break}}}supplyPacket(t,e){let r=this.getCurrentPesPacket(),i;if(this.lastSuppliedPesPacket===r)h(this.nextPts!==null),i=this.nextPts;else{if(r.pts===null)throw new Error(Fl);i=r.pts,Uo(this.elementaryStream,r)}this.lastSuppliedPesPacket=r,this.nextPts=i+e;let a=r.sectionStartPos,s=a+(this.currentPos-this.currentPesPacketPos),o=this.readBytes(t),c=r.randomAccessIndicator;if(c===0&&!this.elementaryStream.canBeTrustedWithKeyPackets){if(this.elementaryStream.info.type==="audio")c=1;else if(this.elementaryStream.info.decoderConfig){let u=dr(this.elementaryStream.info.codec,this.elementaryStream.info.decoderConfig,o)==="key";c=Number(u)}}this.suppliedPacket={pts:i,data:o,sequenceNumber:s,sectionStartPos:a,randomAccessIndicator:c},this.pesPackets.splice(0,this.currentPesPacketIndex),this.currentPesPacketIndex=0}},gn=class{constructor(t,e){this.decodeOrderPackets=[];this.reorderBuffer=[];this.presentationOrderPackets=[];this.reachedEnd=!1;this.lastDuration=0;this.backing=t,this.context=e,this.reorderSize=t.getReorderSize(),h(this.reorderSize>=0)}async readNext(){if(this.decodeOrderPackets.length===0&&!await this.readNextPacket())return null;await this.ensureCurrentPacketHasNext();let t=this.decodeOrderPackets[0],e=this.presentationOrderPackets.indexOf(t);h(e!==-1);let r;for(e===this.presentationOrderPackets.length-1?r=this.lastDuration:(r=this.presentationOrderPackets[e+1].pts-t.pts,this.lastDuration=r),this.decodeOrderPackets.shift();this.presentationOrderPackets.length>0;){let i=this.presentationOrderPackets[0];if(this.decodeOrderPackets.includes(i))break;this.presentationOrderPackets.shift()}return{packet:t,duration:r}}async readNextPacket(){if(this.reachedEnd)return!1;let t;return this.context.suppliedPacket?t=this.context.suppliedPacket:(await this.context.markNextPacket(),t=this.context.suppliedPacket),this.context.suppliedPacket=null,t?(this.decodeOrderPackets.push(t),this.processPacketThroughReorderBuffer(t),!0):(this.reachedEnd=!0,this.flushReorderBuffer(),!1)}async ensureCurrentPacketHasNext(){let t=this.decodeOrderPackets[0];for(h(t);;){let e=this.presentationOrderPackets.indexOf(t);if(e!==-1&&e<=this.presentationOrderPackets.length-2||!await this.readNextPacket())break}}processPacketThroughReorderBuffer(t){if(this.reorderBuffer.push(t),this.reorderBuffer.length>this.reorderSize){let e=0;for(let i=1;i<this.reorderBuffer.length;i++)this.reorderBuffer[i].pts<this.reorderBuffer[e].pts&&(e=i);let r=this.reorderBuffer[e];this.presentationOrderPackets.push(r),this.reorderBuffer.splice(e,1)}}flushReorderBuffer(){this.reorderBuffer.sort((t,e)=>t.pts-e.pts),this.presentationOrderPackets.push(...this.reorderBuffer),this.reorderBuffer.length=0}};var St="application/vnd.apple.mpegurl",zo="#EXT-X-STREAM-INF:",No="#EXT-X-I-FRAME-STREAM-INF:",Lo="#EXT-X-MEDIA:",bn="#EXTINF:",Wo="#EXT-X-MAP:",Ho="#EXT-X-KEY:",qo="#EXT-X-MEDIA-SEQUENCE:",Go="#EXT-X-BYTERANGE:",Ko="#EXT-X-PROGRAM-DATE-TIME:",Ml="#EXT-X-DISCONTINUITY",Qo="#EXT-X-TARGETDURATION:",Bl="#EXT-X-ENDLIST",jo="#EXT-X-PLAYLIST-TYPE:",Ol="#EXT-X-I-FRAMES-ONLY",Wa=n=>n.length===0||n.startsWith("#")&&!n.startsWith("#EXT"),jt=class{constructor(t){this._attributes={};let e="",r="",i=!1,a=!1;for(let s=0;s<t.length;s++){let o=t[s];o==='"'?a=!a:o==="="&&!i&&!a?i=!0:o===","&&!a?(e&&(this._attributes[e.trim().toLowerCase()]=r),e="",r="",i=!1):i?r+=o:e+=o}e&&(this._attributes[e.trim().toLowerCase()]=r)}get(t){return this._attributes[t.toLowerCase()]??null}getAsNumber(t){let e=this.get(t);if(e===null)return null;let r=Number(e);return Number.isFinite(r)?r:null}merge(t){Object.assign(this._attributes,t._attributes)}};var Ha=class{constructor(t,e,r){this.nextInputCacheAge=0;this.inputCache=[];this.trackBackingsPromise=null;this.firstSegment=null;this.firstSegmentFirstTimestamps=new WeakMap;this.firstTimestampCache=new WeakMap;this.input=t,this.path=e,this.trackDeclarations=r}async getDurationFromMetadata(t){let e=await this.getSegmentAt(1/0,{skipLiveWait:t.skipLiveWait});return e?e.timestamp+e.duration:null}async getTrackBackings(){return this.trackBackingsPromise??=(async()=>{let t=[];if(this.trackDeclarations){for(let e of this.trackDeclarations)if(e.type==="video"){let r=Fr(t,i=>i.getType()==="video")+1;t.push(new Ga(this,e,r))}else if(e.type==="audio"){let r=Fr(t,i=>i.getType()==="audio")+1;t.push(new Ka(this,e,r))}}else{if(this.firstSegment=await this.getFirstSegment({}),!this.firstSegment)return[];let r=await this.getInputForSegment(this.firstSegment).getTracks();for(let i of r)if(i.type==="video"){let a=Fr(t,s=>s.getType()==="video")+1;t.push(new Ga(this,{id:t.length+1,type:"video"},a))}else if(i.type==="audio"){let a=Fr(t,s=>s.getType()==="audio")+1;t.push(new Ka(this,{id:t.length+1,type:"audio"},a))}}return t})()}async getFirstTimestampForInput(t){let e=this.firstTimestampCache.get(t);if(e!==void 0)return e;let r=await t.getFirstTimestamp();return this.firstTimestampCache.set(t,r),r}async getMediaOffset(t,e){let r=t.firstSegment??t,i;if(this.firstSegmentFirstTimestamps.has(r))i=this.firstSegmentFirstTimestamps.get(r);else{let u=this.getInputForSegment(r);i=await this.getFirstTimestampForInput(u),this.firstSegmentFirstTimestamps.set(r,i)}if(r===t)return r.timestamp-i;let a=await this.getFirstTimestampForInput(e),s=t.timestamp-r.timestamp,c=a-i-s;return Math.abs(c)<=Math.min(.25,s)?r.timestamp-i:t.timestamp-a}dispose(){for(let t of this.inputCache)t.input.dispose();this.inputCache.length=0}},qa=class{constructor(t,e,r){this.packetInfos=new WeakMap;this.hydrationPromise=null;this.firstInputTrack=null;this.segmentedInput=t,this.decl=e,this.number=r}hydrate(){return this.hydrationPromise??=(async()=>{if(this.segmentedInput.firstSegment??=await this.segmentedInput.getFirstSegment({}),!this.segmentedInput.firstSegment)throw new Error("Missing first segment, can't retrieve track.");let r=(await this.segmentedInput.getInputForSegment(this.segmentedInput.firstSegment).getTracks()).find(i=>i.type===this.decl.type&&i.number===this.number);if(!r)throw new Error("No matching track found in underlying media data.");this.firstInputTrack=r})()}getId(){return this.decl.id}getType(){return this.decl.type}getNumber(){return this.number}delegate(t){return this.firstInputTrack?t():this.hydrate().then(t)}async getDecoderConfig(){return this.delegate(()=>this.firstInputTrack._backing.getDecoderConfig())}getHasOnlyKeyPackets(){return this.delegate(()=>this.firstInputTrack._backing.getHasOnlyKeyPackets?.()??null)}getPairingMask(){return 1n}getCodec(){return this.delegate(()=>this.firstInputTrack._backing.getCodec())}getInternalCodecId(){return this.delegate(()=>this.firstInputTrack._backing.getInternalCodecId())}getDisposition(){return this.delegate(()=>this.firstInputTrack._backing.getDisposition())}getLanguageCode(){return this.delegate(()=>this.firstInputTrack._backing.getLanguageCode())}getName(){return this.delegate(()=>this.firstInputTrack._backing.getName())}getTimeResolution(){return this.delegate(()=>this.firstInputTrack._backing.getTimeResolution())}async isRelativeToUnixEpoch(){return await this.hydrate(),h(this.segmentedInput.firstSegment),this.segmentedInput.firstSegment.relativeToUnixEpoch}getBitrate(){return this.delegate(()=>this.firstInputTrack._backing.getBitrate())}getAverageBitrate(){return this.delegate(()=>this.firstInputTrack._backing.getAverageBitrate())}getDurationFromMetadata(t){return this.segmentedInput.getDurationFromMetadata(t)}getLiveRefreshInterval(){return this.segmentedInput.getLiveRefreshInterval()}async createAdjustedPacket(t,e,r){h(t.sequenceNumber>=0),h(this.segmentedInput.firstSegment);let i=await this.segmentedInput.getMediaOffset(e,r.input),a=e.timestamp-this.segmentedInput.firstSegment.timestamp,s=t.clone({timestamp:bt(t.timestamp+i,await r.getTimeResolution()),sequenceNumber:Math.floor(1e8*a)+t.sequenceNumber});return this.packetInfos.set(s,{segment:e,track:r,sourcePacket:t}),s}async getFirstPacket(t){await this.hydrate(),h(this.segmentedInput.firstSegment),h(this.firstInputTrack);let e=await this.firstInputTrack._backing.getFirstPacket(t);return e?this.createAdjustedPacket(e,this.segmentedInput.firstSegment,this.firstInputTrack):null}getNextPacket(t,e){return this._getNextInternal(t,e,!1)}getNextKeyPacket(t,e){return this._getNextInternal(t,e,!0)}async _getNextInternal(t,e,r){let i=this.packetInfos.get(t);if(!i)throw new Error("Packet was not created from this track.");let a=r?await i.track._backing.getNextKeyPacket(i.sourcePacket,e):await i.track._backing.getNextPacket(i.sourcePacket,e);if(a)return this.createAdjustedPacket(a,i.segment,i.track);let s=i.segment;for(;;){let o=await this.segmentedInput.getNextSegment(s,{skipLiveWait:e.skipLiveWait});if(!o)return null;let l=(await this.segmentedInput.getInputForSegment(o).getTracks()).find(d=>d.type===i.track.type&&d.number===i.track.number);if(!l){s=o;continue}let m=await l._backing.getFirstPacket(e);return m?this.createAdjustedPacket(m,o,l):null}}getPacket(t,e){return this._getPacketInternal(t,e,!1)}getKeyPacket(t,e){return this._getPacketInternal(t,e,!0)}async _getPacketInternal(t,e,r){let i=await this.segmentedInput.getSegmentAt(t,{skipLiveWait:e.skipLiveWait});if(!i)return null;for(await this.hydrate();i;){let a=this.segmentedInput.getInputForSegment(i),o=(await a.getTracks()).find(m=>m.type===this.firstInputTrack.type&&m.number===this.firstInputTrack.number);if(!o){i=await this.segmentedInput.getPreviousSegment(i,{skipLiveWait:e.skipLiveWait});continue}let c=await this.segmentedInput.getMediaOffset(i,a),u=t-c,l=r?await o._backing.getKeyPacket(u,e):await o._backing.getPacket(u,e);if(!l){i=await this.segmentedInput.getPreviousSegment(i,{skipLiveWait:e.skipLiveWait});continue}return this.createAdjustedPacket(l,i,o)}return null}},Ga=class extends qa{getType(){return"video"}getCodec(){return this.delegate(()=>this.firstInputTrack._backing.getCodec())}getCodedWidth(){return this.delegate(()=>this.firstInputTrack._backing.getCodedWidth())}getCodedHeight(){return this.delegate(()=>this.firstInputTrack._backing.getCodedHeight())}getSquarePixelWidth(){return this.delegate(()=>this.firstInputTrack._backing.getSquarePixelWidth())}getSquarePixelHeight(){return this.delegate(()=>this.firstInputTrack._backing.getSquarePixelHeight())}getRotation(){return this.delegate(()=>this.firstInputTrack._backing.getRotation())}async getColorSpace(){return this.delegate(()=>this.firstInputTrack._backing.getColorSpace())}async canBeTransparent(){return this.delegate(()=>this.firstInputTrack._backing.canBeTransparent())}async getDecoderConfig(){return this.delegate(()=>this.firstInputTrack._backing.getDecoderConfig())}},Ka=class extends qa{getType(){return"audio"}getCodec(){return this.delegate(()=>this.firstInputTrack._backing.getCodec())}getNumberOfChannels(){return this.delegate(()=>this.firstInputTrack._backing.getNumberOfChannels())}getSampleRate(){return this.delegate(()=>this.firstInputTrack._backing.getSampleRate())}async getDecoderConfig(){return this.delegate(()=>this.firstInputTrack._backing.getDecoderConfig())}};var Dl=cu(Xo(),1);ei();var Vl=typeof Dl<"u"?Dl:void 0,ec=0,tc=1/0,_e=class extends Ye{constructor(){super(...arguments);this._disposed=!1;this._refCount=0;this._usedForHls=!1;this._sizePromise=null;this.onread=null}async getSizeOrNull(){if(this._disposed)throw new be;return this._sizePromise??=(async()=>{let e=this._getFileSize();return e!==void 0||(await this._read(0,1,ec,tc),e=this._getFileSize(),h(e!==void 0)),e})()}async getSize(){if(this._disposed)throw new be;let e=await this.getSizeOrNull();if(e===null)throw new Error("Cannot determine the size of an unsized source.");return e}slice(e,r){if(!Number.isInteger(e)||e<0)throw new TypeError("offset must be a non-negative integer.");if(r!==void 0&&(!Number.isInteger(r)||r<0))throw new TypeError("length, when provided, must be a non-negative integer.");return new ja(this,e,r)}_dispatchRead(e,r){this.onread?.(e,r),this._emit("read",{start:e,end:r})}ref(){return new Nr(this)}},Nr=class{constructor(t){this._freed=!1;if(t._disposed)throw new Error("Cannot ref a disposed source.");t._refCount++,this._source=t}get source(){if(!this._source)throw new Error("Can't get source; ref has already been freed.");return this._source}get freed(){return this._freed}free(){if(this._freed)throw new Error("Illegal operation: double free on SourceRef.");let t=this.source;h(t._refCount>0),t._refCount--,t._refCount===0&&(t._dispose(),t._disposed=!0),this._freed=!0,this._source=null}[Symbol.dispose](){this.freed||this.free()}},Qe=class extends _e{constructor(e,r){if(typeof e!="string")throw new TypeError("rootPath must be a string.");if(typeof r!="function")throw new TypeError("requestHandler must be a function.");super();this.rootPath=e;this.requestHandler=r}_resolveRequest(e){let r=this.requestHandler(e),i=a=>{if(!(a instanceof _e||a instanceof Nr))throw new TypeError("requestHandler must return or resolve to a Source or SourceRef.");let s=a instanceof _e?a.ref():a;return s.source._usedForHls||=this._usedForHls,s};return r instanceof Promise?r.then(i):i(r)}},rc=(n,t)=>n.path===t.path,yn=class extends Qe{constructor(){super(...arguments);this._root=null;this._rootRequest=null}_read(e,r,i,a){if(!this._root){if(!this._rootRequest){let s=this._resolveRequest({path:this.rootPath,isRoot:!0}),o=c=>{let u=c instanceof _e?c.ref():c;return this._root=u,this._rootRequest=null,u};s instanceof Promise?this._rootRequest=s.then(o):(o(s),h(this._root))}if(this._rootRequest)return this._rootRequest.then(s=>s.source._read(e,r,i,a))}return this._root.source._read(e,r,i,a)}_getFileSize(){if(this._root)return this._root.source._getFileSize()}_dispose(){this._root?this._root.free():this._rootRequest&&this._rootRequest.then(e=>e.free())}},$o=class extends _e{constructor(e){if(!(e instanceof ArrayBuffer)&&!(typeof SharedArrayBuffer<"u"&&e instanceof SharedArrayBuffer)&&!ArrayBuffer.isView(e))throw new TypeError("buffer must be an ArrayBuffer, SharedArrayBuffer, or ArrayBufferView.");super();this._onreadCalled=!1;this._bytes=j(e),this._view=V(e)}_getFileSize(){return this._bytes.byteLength}_read(){return this._onreadCalled||(this._dispatchRead(0,this._bytes.byteLength),this._onreadCalled=!0),{bytes:this._bytes,view:this._view,offset:0}}_dispose(){}},Yo=class extends _e{constructor(e,r={}){if(!(e instanceof Blob))throw new TypeError("blob must be a Blob.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(r.maxCacheSize!==void 0&&(!nr(r.maxCacheSize)||r.maxCacheSize<0))throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");if(r.useStreamReader!==void 0&&typeof r.useStreamReader!="boolean")throw new TypeError("options.useStreamReader, when provided, must be a boolean.");super();this._readers=new WeakMap;this._blob=e,this._options=r,this._orchestrator=new Tn({maxCacheSize:r.maxCacheSize??8*2**20,maxWorkerCount:4,runWorker:this._runWorker.bind(this),prefetchProfile:ic.fileSystem}),this._orchestrator.fileSize=e.size}_getFileSize(){return this._orchestrator.fileSize}_read(e,r,i,a){return this._orchestrator.read(e,r,i,a)}async _runWorker(e){h(e.strictTarget);let r=this._readers.get(e);for(r===void 0&&("stream"in this._blob&&!ir()&&this._options.useStreamReader!==!1?r=this._blob.slice(e.currentPos).stream().getReader():r=null,this._readers.set(e,r));e.currentPos<e.targetPos&&!e.aborted;)if(r){let{done:i,value:a}=await r.read();if(i)throw this._orchestrator.onWorkerFinished(e),new Error("Blob reader stopped unexpectedly before all requested data was read.");if(e.aborted)break;this._dispatchRead(e.currentPos,e.currentPos+a.length),this._orchestrator.supplyWorkerData(e,a)}else{let i=await this._blob.slice(e.currentPos,e.targetPos).arrayBuffer();if(e.aborted)break;this._dispatchRead(e.currentPos,e.currentPos+i.byteLength),this._orchestrator.supplyWorkerData(e,new Uint8Array(i))}this._orchestrator.signalWorkerStoppedRunning(e),e.aborted&&await r?.cancel()}_dispose(){this._orchestrator.dispose()}},Mm=.5*2**20,Bm=(n,t,e)=>{if(t instanceof Error&&(t.message.includes("Failed to fetch")||t.message.includes("Load failed")||t.message.includes("NetworkError when attempting to fetch resource"))&&typeof window<"u"){let i=null;try{typeof window<"u"&&typeof window.location<"u"&&(i=new URL(e instanceof Request?e.url:e,window.location.href).origin)}catch{}if((typeof navigator<"u"&&typeof navigator.onLine=="boolean"?navigator.onLine:!0)&&i!==null&&i!==window.location.origin)return console.warn("Request will not be retried because a CORS error was suspected due to different origins. You can modify this behavior by providing your own function for the 'getRetryDelay' option."),null}return Math.min(2**(n-2),16)},Ul=new Set,Zo=class n extends Qe{constructor(e,r={}){if(typeof e!="string"&&!(e instanceof URL)&&!(typeof Request<"u"&&e instanceof Request))throw new TypeError("url must be a string, URL or Request.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(r.requestInit!==void 0&&(!r.requestInit||typeof r.requestInit!="object"))throw new TypeError("options.requestInit, when provided, must be an object.");if(r.getRetryDelay!==void 0&&typeof r.getRetryDelay!="function")throw new TypeError("options.getRetryDelay, when provided, must be a function.");if(r.maxCacheSize!==void 0&&(!nr(r.maxCacheSize)||r.maxCacheSize<0))throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");if(r.parallelism!==void 0&&(!Number.isInteger(r.parallelism)||r.parallelism<1))throw new TypeError("options.parallelism, when provided, must be a positive number.");if(r.fetchFn!==void 0&&typeof r.fetchFn!="function")throw new TypeError("options.fetchFn, when provided, must be a function.");let i=e instanceof Request?e.url:e instanceof URL?e.href:e;super(i,s=>new n(s.path,this._options));this._fileSizeDetermined=!1;this._url=e,this._options=r,this._getRetryDelay=r.getRetryDelay??Bm;let a=2;this._orchestrator=new Tn({maxCacheSize:r.maxCacheSize??64*2**20,maxWorkerCount:r.parallelism??a,runWorker:this._runWorker.bind(this),prefetchProfile:ic.network})}_getFileSize(){return this._fileSizeDetermined?this._orchestrator.fileSize:void 0}_read(e,r,i,a){return this._orchestrator.read(e,r,i,a)}async _runWorker(e){for(;;){let r=new AbortController,i=await wu(this._options.fetchFn??fetch,this._url,Tu(this._options.requestInit??{},{headers:{Range:`bytes=${e.currentPos}-`},signal:r.signal}),this._getRetryDelay,()=>this._disposed);if(!i.ok)throw new Error(`Error fetching ${String(this._url)}: ${i.status} ${i.statusText}`);e:if(this._orchestrator.fileSize===null){let s=i.headers.get("Content-Range");if(s){let c=/\/(\d+)/.exec(s);if(c){this._orchestrator.supplyFileSize(Number(c[1]));break e}}let o=i.headers.get("Content-Length");o&&this._orchestrator.supplyFileSize(e.currentPos+Number(o))}if(this._fileSizeDetermined=!0,i.status!==206){if(!this._usedForHls){let s=new URL(this._url instanceof Request?this._url.url:this._url,typeof window<"u"?window.location.href:void 0);s.origin!=="null"&&!(s.pathname.endsWith(".m3u8")||s.pathname.endsWith(".m3u"))&&(Ul.has(s.origin)||(console.log(this._usedForHls,this._url,s.pathname),console.warn(`HTTP server (origin ${s.origin}) did not respond to a range request with 206 Partial Content, meaning the entire resource will now be downloaded. To enable efficient media file streaming across a network, please make sure your server supports range requests.`),Ul.add(s.origin)))}e.currentPos=0,this._orchestrator.options.maxCacheSize=1/0,this._orchestrator.fileSize!==null?e.targetPos=this._orchestrator.fileSize:(e.targetPos=1/0,e.strictTarget=!1),this._orchestrator.consolidateEverythingIntoOneWorker(e)}if(!i.body)throw new Error("Missing HTTP response body stream. The used fetch function must provide the response body as a ReadableStream.");let a=i.body.getReader();for(;;){if(e.currentPos>=e.targetPos||e.aborted){r.abort(),this._orchestrator.signalWorkerStoppedRunning(e);return}let s;try{s=await a.read()}catch(u){if(this._disposed)throw u;let l=this._getRetryDelay(1,u,this._url);if(l!==null){console.error("Error while reading response stream. Attempting to resume.",u),await $i(1e3*l);break}else throw u}if(e.aborted)continue;let{done:o,value:c}=s;if(o){if(e.currentPos>=e.targetPos){this._orchestrator.onWorkerFinished(e);return}if(e.strictTarget)break;this._orchestrator.onWorkerFinished(e);return}this._dispatchRead(e.currentPos,e.currentPos+c.length),this._orchestrator.supplyWorkerData(e,c)}}}_dispose(){this._orchestrator.dispose()}},Jo=class n extends Qe{constructor(e,r={}){if(typeof e!="string")throw new TypeError("filePath must be a string.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(r.maxCacheSize!==void 0&&(!nr(r.maxCacheSize)||r.maxCacheSize<0))throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");if(!Vl.fs)throw new Error("FilePathSource is only available in server-side environments (Node.js, Bun, Deno).");super(e,i=>new n(i.path,r));this._fileHandle=null;this._streamSource=new Qa({getSize:async()=>(this._fileHandle=await Vl.fs.open(e,"r"),(await this._fileHandle.stat()).size),read:async(i,a)=>{h(this._fileHandle);let s=new Uint8Array(a-i);return await this._fileHandle.read(s,0,a-i,i),s},maxCacheSize:r.maxCacheSize,prefetchProfile:"fileSystem"})}_read(e,r,i,a){return this._streamSource._read(e,r,i,a)}_getFileSize(){return this._streamSource._getFileSize()}_dispose(){this._streamSource._dispose(),this._fileHandle?.close(),this._fileHandle=null}},Qa=class extends _e{constructor(t){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(typeof t.getSize!="function")throw new TypeError("options.getSize must be a function.");if(typeof t.read!="function")throw new TypeError("options.read must be a function.");if(t.dispose!==void 0&&typeof t.dispose!="function")throw new TypeError("options.dispose, when provided, must be a function.");if(t.maxCacheSize!==void 0&&(!nr(t.maxCacheSize)||t.maxCacheSize<0))throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");if(t.prefetchProfile&&!["none","fileSystem","network"].includes(t.prefetchProfile))throw new TypeError("options.prefetchProfile, when provided, must be one of 'none', 'fileSystem' or 'network'.");super(),this._options=t,this._orchestrator=new Tn({maxCacheSize:t.maxCacheSize??8*2**20,maxWorkerCount:2,prefetchProfile:ic[t.prefetchProfile??"none"],runWorker:this._runWorker.bind(this)})}_getFileSize(){return this._orchestrator.fileSize??void 0}_read(t,e,r,i){if(this._orchestrator.fileSize!==null)return this._orchestrator.read(t,e,r,i);let a=this._options.getSize();if(a instanceof Promise)return a.then(s=>{if(!Number.isInteger(s)||s<0)throw new TypeError("options.getSize must return or resolve to a non-negative integer.");return this._orchestrator.fileSize=s,this._orchestrator.read(t,e,r,i)});if(!Number.isInteger(a)||a<0)throw new TypeError("options.getSize must return or resolve to a non-negative integer.");return this._orchestrator.fileSize=a,this._orchestrator.read(t,e,r,i)}async _runWorker(t){for(;t.currentPos<t.targetPos&&!t.aborted;){let e=t.currentPos,r=t.targetPos,i=this._options.read(t.currentPos,r);if(i instanceof Promise&&(i=await i),t.aborted)break;if(i instanceof Uint8Array){if(i=j(i),i.length!==r-t.currentPos)throw new Error(`options.read returned a Uint8Array with unexpected length: Requested ${r-t.currentPos} bytes, but got ${i.length}.`);this._dispatchRead(t.currentPos,t.currentPos+i.length),this._orchestrator.supplyWorkerData(t,i)}else if(i instanceof ReadableStream){let a=i.getReader();for(;t.currentPos<r&&!t.aborted;){let{done:s,value:o}=await a.read();if(s){if(t.currentPos<r)throw new Error(`ReadableStream returned by options.read ended before supplying enough data. Requested ${r-e} bytes, but got ${t.currentPos-e}`);break}if(!(o instanceof Uint8Array))throw new TypeError("ReadableStream returned by options.read must yield Uint8Array chunks.");if(t.aborted)break;let c=j(o);this._dispatchRead(t.currentPos,t.currentPos+c.length),this._orchestrator.supplyWorkerData(t,c)}}else throw new TypeError("options.read must return or resolve to a Uint8Array or a ReadableStream.")}this._orchestrator.signalWorkerStoppedRunning(t)}_dispose(){this._orchestrator.dispose(),this._options.dispose?.()}},kn=class extends _e{constructor(e,r={}){if(!(e instanceof ReadableStream))throw new TypeError("stream must be a ReadableStream.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(r.maxCacheSize!==void 0&&(!nr(r.maxCacheSize)||r.maxCacheSize<0))throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");super();this._reader=null;this._cache=[];this._pendingSlices=[];this._currentIndex=0;this._targetIndex=0;this._maxRequestedIndex=0;this._endIndex=null;this._pulling=!1;this._stream=e,this._maxCacheSize=r.maxCacheSize??32*2**20}_getFileSize(){return this._endIndex}_read(e,r){if(this._endIndex!==null&&r>this._endIndex)return null;this._maxRequestedIndex=Math.max(this._maxRequestedIndex,r);let i=H(this._cache,e,m=>m.start),a=i!==-1?this._cache[i]:null;if(a&&a.start<=e&&r<=a.end)return{bytes:a.bytes,view:a.view,offset:a.start};let s=e,o=new Uint8Array(r-e);if(i!==-1)for(let m=i;m<this._cache.length;m++){let d=this._cache[m];if(d.start>=r)break;let f=Math.max(e,d.start);f>s&&this._throwDueToCacheMiss();let p=Math.min(r,d.end);f<p&&(o.set(d.bytes.subarray(f-d.start,p-d.start),f-e),s=p)}if(s===r)return{bytes:o,view:V(o),offset:e};this._currentIndex>s&&this._throwDueToCacheMiss();let{promise:c,resolve:u,reject:l}=Z();return this._pendingSlices.push({start:e,end:r,bytes:o,resolve:u,reject:l}),this._targetIndex=Math.max(this._targetIndex,r),this._pulling||(this._pulling=!0,this._pull().catch(m=>{if(this._pulling=!1,this._pendingSlices.length>0)this._pendingSlices.forEach(d=>d.reject(m)),this._pendingSlices.length=0;else throw m})),c}_throwDueToCacheMiss(){throw new Error("Read is before the cached region. With ReadableStreamSource, you must access the data more sequentially or increase the size of its cache.")}async _pull(){for(this._reader??=this._stream.getReader();this._currentIndex<this._targetIndex&&!this._disposed;){let{done:e,value:r}=await this._reader.read();if(e){for(let s of this._pendingSlices)s.resolve(null);this._pendingSlices.length=0,this._endIndex=this._currentIndex;break}let i=this._currentIndex,a=this._currentIndex+r.byteLength;this._dispatchRead(i,a);for(let s=0;s<this._pendingSlices.length;s++){let o=this._pendingSlices[s],c=Math.max(i,o.start),u=Math.min(a,o.end);c<u&&(o.bytes.set(r.subarray(c-i,u-i),c-o.start),u===o.end&&(o.resolve({bytes:o.bytes,view:V(o.bytes),offset:o.start}),this._pendingSlices.splice(s,1),s--))}for(this._cache.push({start:i,end:a,bytes:r,view:V(r),age:0});this._cache.length>0;){let s=this._cache[0];if(this._maxRequestedIndex-s.end<=this._maxCacheSize)break;this._cache.shift()}this._currentIndex+=r.byteLength}this._pulling=!1}_dispose(){this._pendingSlices.length=0,this._cache.length=0,this._reader?.cancel()}},ic={none:(n,t)=>({start:n,end:t}),fileSystem:(n,t)=>(n=Math.floor((n-65536)/65536)*65536,t=Math.ceil((t+65536)/65536)*65536,{start:n,end:t}),network:(n,t,e)=>{n=Math.max(0,Math.floor((n-65536)/65536)*65536);for(let i of e){let s=Math.max((i.startPos+i.targetPos)/2,i.targetPos-8388608);if(Ln(n,t,s,i.targetPos)){let o=i.targetPos-i.startPos,c=Math.ceil((o+1)/8388608)*8388608,u=2**Math.ceil(Math.log2(o+1)),l=Math.min(u,c);t=Math.max(t,i.startPos+l)}}return t=Math.max(t,n+Mm),{start:n,end:t}}},Tn=class{constructor(t){this.options=t;this.fileSize=null;this.nextAge=0;this.workers=[];this.cache=[];this.currentCacheSize=0;this.disposed=!1;this.queuedReads=[]}read(t,e,r,i){h(!this.disposed);let a=this.options.prefetchProfile(t,e,this.workers),s=Math.max(a.start,r),o=Math.min(a.end,this.fileSize??1/0,i);h(s<=t&&e<=o);let c=null,u=H(this.cache,t,S=>S.start),l=u!==-1?this.cache[u]:null;l&&l.start<=t&&e<=l.end&&(l.age=this.nextAge++,c={bytes:l.bytes,view:l.view,offset:l.start});let m=H(this.cache,s,S=>S.start),d=c?null:new Uint8Array(e-t),f=0,p=s,b=[];if(m!==-1){for(let S=m;S<this.cache.length;S++){let x=this.cache[S];if(x.start>=o)break;if(x.end<=s)continue;let P=Math.max(s,x.start),A=Math.min(o,x.end);if(h(P<=A),p<P&&b.push({start:p,end:P}),p=A,d){let C=Math.max(t,x.start),I=Math.min(e,x.end);if(C<I){let R=C-t;d.set(x.bytes.subarray(C-x.start,I-x.start),R),R===f&&(f=I-t)}}x.age=this.nextAge++}p<o&&b.push({start:p,end:o})}else b.push({start:s,end:o});if(d&&f>=d.length&&(c={bytes:d,view:V(d),offset:t}),b.length===0)return h(c),c;let{promise:g,resolve:y,reject:k}=Z(),w=[];for(let S of b){let x=Math.max(t,S.start),P=Math.min(e,S.end);x===S.start&&P===S.end?w.push(S):x<P&&w.push({start:x,end:P})}let T=d&&{start:t,bytes:d,holes:w,resolve:y,reject:k};e:for(let S of b){for(let A of this.workers)if(this.checkHoleAgainstWorker(A,S,T?[T]:[])){this.checkQueuedReadsAgainstWorker(A);continue e}let x=S.end<o||this.fileSize!==null,P=this.createWorker(S.start,S.end,x);if(P)T&&(P.pendingSlices=[T]),this.runWorker(P);else{let A=H(this.queuedReads,S.start,I=>I.hole.start),C=A!==-1?this.queuedReads[A]:null;for(C&&S.start<=C.hole.end?(C.hole.end=Math.max(C.hole.end,S.end),C.strictTarget&&=x,T&&C.pendingSlices.push(T)):(A++,C={hole:{start:S.start,end:S.end},strictTarget:x,pendingSlices:T?[T]:[],age:this.nextAge++},this.queuedReads.splice(A,0,C));A+1<this.queuedReads.length;){let I=this.queuedReads[A+1];if(I.hole.start>C.hole.end)break;C.hole.end=Math.max(C.hole.end,I.hole.end),C.pendingSlices.push(...I.pendingSlices),C.strictTarget&&=I.strictTarget,C.age=Math.min(C.age,I.age),this.queuedReads.splice(A+1,1)}}}return c||(h(d),c=g.then(S=>S&&{bytes:S,view:V(S),offset:t})),c}checkHoleAgainstWorker(t,e,r){if(Ln(e.start-131072,e.start,t.currentPos,t.targetPos)){t.targetPos=Math.max(t.targetPos,e.end);for(let a=0;a<r.length;a++){let s=r[a];t.pendingSlices.includes(s)||t.pendingSlices.push(s)}return t.running||this.runWorker(t),!0}return!1}checkQueuedReadsAgainstWorker(t){let e=!1;for(let r=0;r<this.queuedReads.length;r++){let i=this.queuedReads[r];if(this.checkHoleAgainstWorker(t,i.hole,i.pendingSlices))this.queuedReads.splice(r,1),r--,e=!0;else if(e)break}}createWorker(t,e,r){if(this.workers.length>=this.options.maxWorkerCount){let a=null,s=null;for(let o=0;o<this.workers.length;o++){let c=this.workers[o];!c.running&&c.pendingSlices.length===0&&(!a||c.age<a.age)&&(s=o,a=c)}if(a)h(s!==null),h(a.pendingSlices.length===0),this.workers.splice(s,1);else return null}let i={startPos:t,currentPos:t,targetPos:e,strictTarget:r,running:!1,aborted:this.disposed,pendingSlices:[],age:this.nextAge++};return this.workers.push(i),i}runWorker(t){h(!t.running),h(t.currentPos<t.targetPos),t.running=!0,t.age=this.nextAge++,this.options.runWorker(t).catch(e=>{if(t.running=!1,t.pendingSlices.length>0)t.pendingSlices.forEach(r=>r.reject(e)),t.pendingSlices.length=0;else throw e}).finally(()=>{if(!t.running&&this.queuedReads.length>0){let e=0;for(let a=1;a<this.queuedReads.length;a++)this.queuedReads[a].age<this.queuedReads[e].age&&(e=a);let r=this.queuedReads[e];this.queuedReads.splice(e,1);let i=this.createWorker(r.hole.start,r.hole.end,r.strictTarget);h(i),i.pendingSlices=r.pendingSlices,this.runWorker(i)}})}consolidateEverythingIntoOneWorker(t){let e=new Set(t.pendingSlices);for(let r=0;r<this.workers.length;r++){let i=this.workers[r];if(i!==t){for(let a of i.pendingSlices)e.add(a);i.aborted=!0,i.pendingSlices.length=0,this.workers.splice(r,1),r--}}for(let r=0;r<this.queuedReads.length;r++){let i=this.queuedReads[r];for(let a of i.pendingSlices)e.add(a)}t.pendingSlices=[...e],this.queuedReads.length=0}supplyWorkerData(t,e){h(!t.aborted);let r=t.currentPos,i=r+e.length;this.insertIntoCache({start:r,end:i,bytes:e,view:V(e),age:this.nextAge++}),t.currentPos+=e.length,t.currentPos>t.targetPos&&(t.targetPos=t.currentPos,this.checkQueuedReadsAgainstWorker(t));for(let a=0;a<t.pendingSlices.length;a++){let s=t.pendingSlices[a],o=Math.max(r,s.start),c=Math.min(i,s.start+s.bytes.length);o<c&&s.bytes.set(e.subarray(o-r,c-r),o-s.start);for(let u=0;u<s.holes.length;u++){let l=s.holes[u];r<=l.start&&i>l.start&&(l.start=i),l.end<=l.start&&(s.holes.splice(u,1),u--)}s.holes.length===0&&(s.resolve(s.bytes),t.pendingSlices.splice(a,1),a--)}for(let a=0;a<this.workers.length;a++){let s=this.workers[a];t===s||s.running||Ln(r,i,s.currentPos,s.targetPos)&&(this.workers.splice(a,1),a--)}}supplyFileSize(t){h(this.fileSize===null),this.fileSize=t;for(let e of this.workers){e.targetPos=Math.min(e.targetPos,t),e.strictTarget=!0;for(let r=0;r<e.pendingSlices.length;r++){let i=e.pendingSlices[r];for(let a of i.holes)if(a.end>t){i.resolve(null),e.pendingSlices.splice(r,1),r--;break}}}for(let e=0;e<this.queuedReads.length;e++){let r=this.queuedReads[e];if(r.hole.start>=t){for(let i of r.pendingSlices)i.resolve(null);this.queuedReads.splice(e,1),e--}else if(r.hole.end>t){r.hole.end=t,r.strictTarget=!0;for(let i=0;i<r.pendingSlices.length;i++){let a=r.pendingSlices[i];a.start>=t&&(a.resolve(null),r.pendingSlices.splice(i,1),i--)}}}}signalWorkerStoppedRunning(t){t.running=!1,t.pendingSlices.length=0}onWorkerFinished(t){let e=this.workers.indexOf(t);h(e!==-1),t.running=!1,this.workers.splice(e,1),this.fileSize===null&&this.supplyFileSize(t.currentPos);for(let r of t.pendingSlices)r.resolve(null)}insertIntoCache(t){if(this.options.maxCacheSize===0)return;let e=H(this.cache,t.start,r=>r.start)+1;if(e>0){let r=this.cache[e-1];if(r.end>=t.end)return;if(r.end>t.start){let i=new Uint8Array(t.end-r.start);i.set(r.bytes,0),i.set(t.bytes,t.start-r.start),this.currentCacheSize+=t.end-r.end,r.bytes=i,r.view=V(i),r.end=t.end,e--,t=r}else this.cache.splice(e,0,t),this.currentCacheSize+=t.bytes.length}else this.cache.splice(e,0,t),this.currentCacheSize+=t.bytes.length;for(let r=e+1;r<this.cache.length;r++){let i=this.cache[r];if(t.end<=i.start)break;if(t.end>=i.end){this.cache.splice(r,1),this.currentCacheSize-=i.bytes.length,r--;continue}let a=new Uint8Array(i.end-t.start);a.set(t.bytes,0),a.set(i.bytes,i.start-t.start),this.currentCacheSize-=t.end-i.start,t.bytes=a,t.view=V(a),t.end=i.end,this.cache.splice(r,1);break}for(;this.currentCacheSize>this.options.maxCacheSize;){let r=0,i=this.cache[0];for(let a=1;a<this.cache.length;a++){let s=this.cache[a];s.age<i.age&&(r=a,i=s)}if(this.currentCacheSize-i.bytes.length<=this.options.maxCacheSize)break;this.cache.splice(r,1),this.currentCacheSize-=i.bytes.length}}dispose(){for(let t of this.workers)t.aborted=!0;this.workers.length=0,this.cache.length=0,this.disposed=!0}};var ja=class extends _e{constructor(e,r,i){super();this._ref=null;if(e._disposed)throw new Error("Cannot create a slice of a disposed source.");this._baseSource=e,this._offset=r,this._length=i??null}_getFileSize(){let e=this._baseSource._getFileSize();return e===void 0?this._length!==null?this._length:void 0:e===null?this._length!==null?this._length:null:ue(e-this._offset,0,this._length??1/0)}_read(e,r,i,a){if(this._length!==null&&r>this._length)return null;let s=this._baseSource._read(this._offset+e,this._offset+r,this._offset+i,this._offset+a);return s instanceof Promise?s.then(o=>o?(o.offset-=this._offset,o):null):s?(s.offset-=this._offset,s):null}_dispose(){this._ref?.free()}ref(){return this._ref??=this._baseSource.ref(),super.ref()}};var Om=/^0[xX][0-9a-fA-F]+$/,Dm=/^data:.*;base64,/i,wn=class extends Ha{constructor(e,r,i,a){super(e.input,r,i);this.segments=[];this.nextLines=null;this.currentUpdateSegmentsPromise=null;this.streamHasEnded=!1;this.lastSegmentUpdateTime=-1/0;this.refreshInterval=5;this.demuxer=e,this.nextLines=a}runUpdateSegments(){return this.currentUpdateSegmentsPromise??=(async()=>{try{let e=this.getRemainingWaitTimeMs();e>0&&await $i(e),this.lastSegmentUpdateTime=performance.now(),await this.updateSegments()}finally{this.currentUpdateSegmentsPromise=null}})()}getRemainingWaitTimeMs(){let e=performance.now()-this.lastSegmentUpdateTime,r=Math.max(0,1e3*this.refreshInterval-e);return r<=50?0:r}async updateSegments(){let e=this.nextLines;if(this.nextLines=null,!e){var k=[];try{let x=qi(k,await this.demuxer.input._getSourceUncached({path:this.path,isRoot:!1}));let P=new hr(x.source);let A=await P.requestEntireFile();h(A);e=Xa(A,A.length,{ignore:Wa})}catch(w){var T=w,S=!0}finally{Gi(k,T,S)}}let r=!1,i=0,a=null,s=null,o=0,c=null,u=null,l=null,m=null,d=null,f=null,p=!1,b=X(this.segments)??null,g=x=>{let P=x.indexOf("@"),A=Number(P===-1?x:x.slice(0,P));if(!Number.isInteger(A)||A<0)throw new Error(`Invalid #EXT-X-BYTERANGE length '${x}'.`);let C=null;if(P!==-1&&(C=Number(x.slice(P+1)),!Number.isInteger(C)||C<0))throw new Error(`Invalid #EXT-X-BYTERANGE offset '${x}'.`);return{length:A,offset:C}},y=x=>{o=x,b&&(h(b.sequenceNumber!==null),b.sequenceNumber<x&&(i=b.timestamp+b.duration,c=b.firstSegment,u=b.initSegment,d=b.lastProgramDateTimeSeconds,b=null))};for(let x=0;x<e.length;x++){let P=e[x];if(!r){if(P!=="#EXTM3U")throw new Error("Invalid M3U8 file; expected first line to be #EXTM3U.");r=!0;continue}if(!P.startsWith("#")){if(!b){if(a===null)throw new Error("Invalid M3U8 file; a segment must be preceded by an #EXTINF tag.");let A=s;if(A&&A.method==="AES-128"&&!A.iv){let v=new Uint8Array(ct),E=V(v);E.setUint32(8,Math.floor(o/2**32)),E.setUint32(12,o),A={...A,iv:v}}let I={path:fe(this.path,P),offset:m?.offset??0,length:m?.length??null},R={timestamp:i,relativeToUnixEpoch:d!==null,firstSegment:c,sequenceNumber:o,location:I,duration:a,encryption:A,initSegment:u,lastProgramDateTimeSeconds:d};c??=R,i+=a,this.segments.push(R)}a=null,m===null?l=null:m=null,y(o+1)}if(P.startsWith(bn)){if(b){p=!0;continue}p||(d===null&&o>0&&f!==null&&(i=o*f),p=!0);let A=P.slice(bn.length),C=A.indexOf(","),I=C===-1?A:A.slice(0,C),R=Number(I);if(!Number.isFinite(R)||R<0)throw new Error(`Invalid #EXTINF tag duration '${I}'.`);a=R}else if(P.startsWith(Wo)){let A=new jt(P.slice(Wo.length)),C=A.get("uri");if(!C)throw new Error("Invalid #EXT-X-MAP tag; missing URI attribute.");let I=A.get("byterange"),R=null;if(I!==null&&(R=g(I)),R&&R.offset===null)throw new Error("Invalid #EXT-X-MAP tag; BYTERANGE attribute must have a specified offset.");if(!b){let E={path:fe(this.path,C),offset:R?.offset??0,length:R?.length??null};if(s?.method==="AES-128"&&!s.iv)throw new Error("IV attribute must be set on #EXT-X-KEY tag preceding the #EXT-X-MAP tag.");u={timestamp:i,relativeToUnixEpoch:d!==null,firstSegment:null,sequenceNumber:null,location:E,duration:0,encryption:s,initSegment:null,lastProgramDateTimeSeconds:d}}a=null,m===null?l=null:m=null}else if(P.startsWith(Ho)){let A=new jt(P.slice(Ho.length)),C=A.get("method");if(C==="NONE")s=null;else if(C==="AES-128"){let I=A.get("uri");if(!I)throw new Error("Invalid #EXT-X-KEY: AES-128 requires a URI attribute.");let R=null,v=A.get("iv");if(v){if(!Om.test(v))throw new Error(`Unsupported IV format '${v}'.`);let D=v.slice(2);D=D.padStart(ct*2,"0"),R=new Uint8Array(ct);for(let U=0;U<ct;U++){let z=-ct*2+U;R[U]=parseInt(D.slice(z,z+2),16)}}let E=A.get("keyformat")??"identity";if(E!=="identity")throw new Error("For AES-128 encryption, only the 'identity' KEYFORMAT is currently supported. If you think other formats should be supported, please raise an issue.");s={method:"AES-128",keyUri:fe(this.path,I),iv:R,keyFormat:E}}else if(C==="SAMPLE-AES"||C==="SAMPLE-AES-CTR"){let I=A.get("uri");if(!I)throw new Error(`Invalid #EXT-X-KEY: ${C} requires a URI attribute.`);if((A.get("keyformat")??"identity")==="identity")throw new Error("For SAMPLE-AES and SAMPLE-AES-CTR encryption, the 'identity' KEYFORMAT is not supported. If you think this format should be supported, please raise an issue.");let v=null;if(Dm.test(I)){let E=I.indexOf(","),D=Rr(I.slice(E+1));if(D.length>=8&&D[4]===112&&D[5]===115&&D[6]===115&&D[7]===104){let U=V(D).getUint32(0);v=da(D.subarray(8,Math.min(U,D.length)))}}s={method:C,psshBox:v}}else throw new Error(`Unsupported encryption method '${C}'. If you think this method should be supported, please raise an issue.`)}else if(P.startsWith(qo)){let A=P.slice(qo.length),C=Number(A);if(!Number.isInteger(C)||C<0)throw new Error(`Invalid EXT-X-MEDIA-SEQUENCE value '${A}'.`);y(C)}else if(P.startsWith(Go)){let A=g(P.slice(Go.length));if(A.offset===null){if(l===null)throw new Error("Invalid M3U8 file; #EXT-X-BYTERANGE without offset requires a previous byte range.");A.offset=l}m=A,l=A.offset+A.length}else if(P.startsWith(Ko)){if(b)continue;let A=P.slice(Ko.length),C=Date.parse(A);if(!Number.isFinite(C))continue;let I=C/1e3;if(d===I)continue;if(d===null&&this.segments.length>0){let R=X(this.segments),v=R.timestamp+R.duration,E=I-v;for(let D of this.segments)D.timestamp+=E,D.relativeToUnixEpoch=!0;i+=E}d=I,i=I}else if(P===Ml)c=null;else if(P.startsWith(Qo)){let A=P.slice(Qo.length),C=Number(A);if(!Number.isFinite(C)||C<0)throw new Error(`Invalid EXT-X-TARGETDURATION value '${A}'.`);this.refreshInterval=C,f=C}else if(P===Bl){this.streamHasEnded=!0;break}else P.startsWith(jo)&&P.slice(jo.length).toLowerCase()==="vod"&&(this.streamHasEnded=!0)}if(!r)throw new Error("Invalid M3U8 file; no #EXTM3U header.")}async getFirstSegment(){return this.segments.length===0&&await this.runUpdateSegments(),this.segments[0]??null}async getSegmentAt(e,r){this.segments.length===0&&await this.runUpdateSegments();let i=!!r.skipLiveWait&&this.getRemainingWaitTimeMs()>0;for(;;){let a=H(this.segments,e,o=>o.timestamp);if(a===-1)return null;if(a<this.segments.length-1||this.streamHasEnded||i)return this.segments[a];let s=this.segments[a];if(e<s.timestamp+s.duration)return s;await this.runUpdateSegments(),r.skipLiveWait&&(i=!0)}}async getNextSegment(e,r){let i=this.segments.indexOf(e);h(i!==-1);let a=i+1,s=!!r.skipLiveWait&&this.getRemainingWaitTimeMs()>0;for(;;){if(a<this.segments.length)return this.segments[a];if(this.streamHasEnded||s)return null;await this.runUpdateSegments(),r.skipLiveWait&&(s=!0)}}async getPreviousSegment(e){let r=this.segments.indexOf(e);return h(r!==-1),this.segments[r-1]??null}getInputForSegment(e){let r=e,i=this.inputCache.find(u=>u.segment===r);if(i)return i.age=this.nextInputCacheAge++,i.input;let a=null;(r.initSegment||r.firstSegment)&&(a=this.getInputForSegment(r.initSegment??r.firstSegment));let s={...this.input._formatOptions,isobmff:{...this.input._formatOptions.isobmff,resolveKeyId:this.input._formatOptions.isobmff?.resolveKeyId&&(u=>{if(!r.encryption||!(r.encryption.method==="SAMPLE-AES"||r.encryption.method==="SAMPLE-AES-CTR")||!r.encryption.psshBox)return this.input._formatOptions.isobmff.resolveKeyId(u);let l=u.psshBoxes,{psshBox:m}=r.encryption;return(m.keyIds===null||m.keyIds.includes(u.keyId))&&!l.some(d=>ma(d,m))&&(l=[...l,m]),this.input._formatOptions.isobmff.resolveKeyId({...u,psshBoxes:l})})}},o=new Lr({source:new yn(r.location.path,async u=>{h(u.isRoot);let l={...u,isRoot:!1},m,d=r.location.offset>0||r.location.length!==null;if(!r.encryption||r.encryption.method==="SAMPLE-AES"||r.encryption.method==="SAMPLE-AES-CTR"){if(m=await this.input._getSourceCached(l),d){let p=m.source.slice(r.location.offset,r.location.length??void 0).ref();m.free(),m=p}}else if(r.encryption.method==="AES-128"){let f=r.encryption;h(f.iv);let p=await this.input._getSourceCached(l);if(d){let k=p.source.slice(r.location.offset,r.location.length??void 0).ref();p.free(),p=k}let b=new hr(p.source),g=sl(b,async()=>{var S=[];try{let y=qi(S,await this.input._getSourceCached({path:f.keyUri,isRoot:!1},zl));let k=new hr(y.source);let w=await k.requestSlice(0,ct);if(!w)throw new Error("Invalid AES-128 key; expected at least 16 bytes of data.");let T=B(w,ct);return{key:T,iv:f.iv}}catch(x){var P=x,A=!0}finally{Gi(S,P,A)}},()=>{p.free()});m=new kn(g).ref()}else h(!1);return m}),formats:this.input._formats.filter(u=>!(u instanceof Si)),initInput:a??void 0,formatOptions:s});if(o._onFormatDetermined=u=>{if((r.encryption?.method==="SAMPLE-AES"||r.encryption?.method==="SAMPLE-AES-CTR")&&!u._isIsobmff)throw new Error("The SAMPLE-AES and SAMPLE-AES-CTR encryption methods are currently only supported for ISOBMFF files.")},this.inputCache.push({segment:r,input:o,age:this.nextInputCacheAge++}),this.inputCache.length>4){let u=ti(this.inputCache,l=>l.age);h(u!==-1),this.inputCache.splice(u,1)}return o}async getLiveRefreshInterval(){return this.getRemainingWaitTimeMs()===0&&await this.runUpdateSegments(),this.streamHasEnded?null:this.refreshInterval}};var Ya=class extends pe{constructor(e){super(e);this.metadataPromise=null;this.trackBackings=null;this.internalTracks=null;this.segmentedInputs=[];this.hasMasterPlaylist=!0}readMetadata(){return this.metadataPromise??=(async()=>{h(this.input._rootSource instanceof Qe);let{rootPath:e}=this.input._rootSource,r=await this.input._reader.requestEntireFile();h(r);let i=Xa(r,r.length,{ignore:Wa}),a=[],s=[];for(let d=1;d<i.length;d++){let f=i[d];if(f.startsWith(zo)){let p=d,b=i[++d];if(b===void 0)throw new Error("Incorrect M3U8 file; a line must follow the #EXT-X-STREAM-INF tag.");let g=fe(e,b),y=new jt(f.slice(zo.length));if(y.getAsNumber("bandwidth")===null)throw new Error("Invalid M3U8 file; #EXT-X-STREAM-INF tag requires a BANDWIDTH attribute with a valid numerical value.");a.push({fullPath:g,attributes:y,lineNumber:p,hasOnlyKeyPackets:!1})}else if(f.startsWith(No)){let p=new jt(f.slice(No.length)),b=p.get("uri");if(b===null)throw new Error("Invalid M3U8 file; #EXT-X-I-FRAME-STREAM-INF tag requires a URI attribute.");if(p.getAsNumber("bandwidth")===null)throw new Error("Invalid M3U8 file; #EXT-X-I-FRAME-STREAM-INF tag requires a BANDWIDTH attribute with a valid numerical value.");let y=fe(e,b);a.push({fullPath:y,attributes:p,lineNumber:d,hasOnlyKeyPackets:!0})}else if(f.startsWith(Lo)){let p=new jt(f.slice(Lo.length));if(p.get("type")===null)throw new Error("Invalid M3U8 file; #EXT-X-MEDIA tag requires a TYPE attribute.");if(p.get("group-id")===null)throw new Error("Invalid M3U8 file; #EXT-X-MEDIA tag requires a GROUP-ID attribute.");let y=null,k=p.get("uri");k!==null&&(y=fe(e,k)),s.push({fullPath:y,attributes:p,lineNumber:d})}else if(f!==Ol){if(f.startsWith(bn)){let p=new wn(this,e,null,i);this.segmentedInputs=[p],this.hasMasterPlaylist=!1,this.trackBackings=await p.getTrackBackings();return}}}let o=[...new Set(s.filter(d=>d.attributes.get("type").toLowerCase()==="video").map(d=>d.attributes.get("group-id")))],c=[...new Set(s.filter(d=>d.attributes.get("type").toLowerCase()==="audio").map(d=>d.attributes.get("group-id")))],u=await Promise.all(a.map(async(d,f)=>{let p=[],b=d.attributes.get("codecs"),g;if(b)g=b.split(",").map(I=>I.trim());else{let R=await this.getSegmentedInputForPath(d.fullPath).getTrackBackings(),v=await Promise.all(R.map(async E=>({track:E,codec:await E.getCodec()})));g=await Promise.all(v.filter(E=>E.codec!==null).map(E=>E.track.getDecoderConfig().then(D=>D.codec)))}let y=d.attributes.get("video"),k=d.attributes.get("audio"),w=g.some(I=>re.includes(Ge(I))),T=g.some(I=>ae.includes(Ge(I)));if(y!==null&&!w){if(!o.includes(y))throw new Error(`Invalid M3U8 file; variant stream references video group "${y}" which is not defined in any #EXT-X-MEDIA tags.`);let I=s.find(R=>{let v=R.attributes.get("group-id"),E=R.attributes.get("type");return v===y&&E.toLowerCase()==="video"});e:if(I){let R=I.attributes.get("uri");if(R===null)break e;let v=fe(e,R),U=(await this.getSegmentedInputForPath(v).getTrackBackings()).find(q=>q.getType()==="video");if(!U||await U.getCodec()===null)break e;let z=await U.getDecoderConfig().then(q=>q?.codec??null);h(z!==null),g.push(z)}}if(k!==null&&!T){if(!c.includes(k))throw new Error(`Invalid M3U8 file; variant stream references audio group "${k}" which is not defined in any #EXT-X-MEDIA tags.`);let I=s.find(R=>{let v=R.attributes.get("group-id"),E=R.attributes.get("type");return v===k&&E.toLowerCase()==="audio"});e:if(I){let R=I.attributes.get("uri");if(R===null)break e;let v=fe(e,R),U=(await this.getSegmentedInputForPath(v).getTrackBackings()).find(q=>q.getType()==="audio");if(!U||await U.getCodec()===null)break e;let z=await U.getDecoderConfig().then(q=>q?.codec??null);h(z!==null),g.push(z)}}g=[...new Set(g)];let S=null,x=null,P=d.attributes.getAsNumber("bandwidth");h(P!==null);let A=d.attributes.getAsNumber("average-bandwidth"),C=d.attributes.get("name");for(let I of g){let R=Ge(I);if(R!==null){if(re.includes(R)){if(S!==null)throw new Error("Unsupported M3U8 file; multiple video codecs found in the CODECS attribute of a variant stream.");S=I;let v=d.attributes.get("video");if(v===null){let E=d.attributes.get("resolution"),D=null,U=null;if(E){let z=E.match(/^(\d+)x(\d+)$/);z&&(D=Number(z[1]),U=Number(z[2]))}p.push({id:-1,demuxer:this,backingTrack:null,default:!0,autoselect:!0,languageCode:J,lineNumber:d.lineNumber,fullPath:d.fullPath,fullCodecString:S,pairingMask:1n<<BigInt(f),peakBitrate:P,averageBitrate:A,name:C,hasOnlyKeyPackets:d.hasOnlyKeyPackets,info:{type:"video",width:D,height:U}})}else{if(!o.includes(v))throw new Error(`Invalid M3U8 file; variant stream references video group "${v}" which is not defined in any #EXT-X-MEDIA tags.`);for(let E of s){let D=E.attributes.get("group-id"),U=E.attributes.get("type");if(D!==v||U.toLowerCase()!=="video")continue;let z=E.attributes.get("resolution")??d.attributes.get("resolution"),q=null,ke=null;if(z){let Te=z.match(/^(\d+)x(\d+)$/);Te&&(q=Number(Te[1]),ke=Number(Te[2]))}p.push({id:-1,demuxer:this,backingTrack:null,default:$a(E.attributes),autoselect:$a(E.attributes)||Nl(E.attributes),languageCode:Ll(E.attributes.get("language")),lineNumber:E.lineNumber,fullPath:E.fullPath??d.fullPath,fullCodecString:S,pairingMask:1n<<BigInt(f),peakBitrate:null,averageBitrate:null,name:E.attributes.get("name"),hasOnlyKeyPackets:d.hasOnlyKeyPackets,info:{type:"video",width:q,height:ke}})}}}else if(ae.includes(R)){if(x!==null)throw new Error("Unsupported M3U8 file; multiple audio codecs found in the CODECS attribute of a variant stream.");x=I;let v=d.attributes.get("audio");if(v===null){let E=d.attributes.get("channels"),D=E!==null?Number(E.split("/")[0]):null;p.push({id:-1,demuxer:this,backingTrack:null,default:!0,autoselect:!0,languageCode:J,lineNumber:d.lineNumber,fullPath:d.fullPath,fullCodecString:x,pairingMask:1n<<BigInt(f),peakBitrate:P,averageBitrate:A,name:C,hasOnlyKeyPackets:d.hasOnlyKeyPackets,info:{type:"audio",numberOfChannels:D!==null&&Number.isInteger(D)&&D>0?D:null}})}else{if(!c.includes(v))throw new Error(`Invalid M3U8 file; variant stream references audio group "${v}" which is not defined in any #EXT-X-MEDIA tags.`);for(let E of s){let D=E.attributes.get("group-id"),U=E.attributes.get("type");if(D!==v||U.toLowerCase()!=="audio")continue;let z=E.attributes.get("channels")??d.attributes.get("channels"),q=z!==null?Number(z.split("/")[0]):null;p.push({id:-1,demuxer:this,backingTrack:null,default:$a(E.attributes),autoselect:$a(E.attributes)||Nl(E.attributes),languageCode:Ll(E.attributes.get("language")),lineNumber:E.lineNumber,fullPath:E.fullPath??d.fullPath,fullCodecString:x,pairingMask:1n<<BigInt(f),peakBitrate:null,averageBitrate:null,name:E.attributes.get("name"),hasOnlyKeyPackets:d.hasOnlyKeyPackets,info:{type:"audio",numberOfChannels:q!==null&&Number.isInteger(q)&&q>0?q:null}})}}}}}return p})),l=[],m=d=>{let f=l.find(p=>p.fullPath===d.fullPath&&p.info.type===d.info.type);f?(f.pairingMask|=d.pairingMask,f.default||=d.default,f.autoselect||=d.autoselect,f.lineNumber=Math.min(f.lineNumber,d.lineNumber),d.peakBitrate!==null&&(f.peakBitrate=Math.max(f.peakBitrate??-1/0,d.peakBitrate)),d.averageBitrate!==null&&(f.averageBitrate=Math.max(f.averageBitrate??-1/0,d.averageBitrate)),f.languageCode===J&&(f.languageCode=d.languageCode)):(d.id=l.length+1,l.push(d))};for(let d of u)for(let f of d)m(f);l.sort((d,f)=>d.lineNumber-f.lineNumber),this.trackBackings=[];for(let d of l)d.info.type==="video"?this.trackBackings.push(new Ja(d)):this.trackBackings.push(new es(d));this.internalTracks=l})()}async getTrackBackings(){return await this.readMetadata(),h(this.trackBackings),this.trackBackings}getSegmentedInputForPath(e){let r=this.segmentedInputs.find(a=>a.path===e);if(r)return r;let i=null;return this.internalTracks&&(i=this.internalTracks.filter(s=>s.fullPath===e).map(s=>({id:s.id,type:s.info.type}))),r=new wn(this,e,i,null),this.segmentedInputs.push(r),r}async getMetadataTags(){return{}}async getMimeType(){return St}dispose(){if(this.segmentedInputs){for(let e of this.segmentedInputs)e.dispose();this.segmentedInputs.length=0}}},Za=class{constructor(t){this.internalTrack=t;this.hydrationPromise=null}hydrate(){return this.hydrationPromise??=(async()=>{let t=this.internalTrack.demuxer.getSegmentedInputForPath(this.internalTrack.fullPath),e=null,i=(await t.getTrackBackings()).filter(a=>a.getType()===this.getType());if(i.length===1)e=i[0];else if(this instanceof Ja){for(let a of i)if(await a.getCodec()===this.getCodec()){e=a;break}}else{h(this instanceof es);for(let a of i)if(await a.getCodec()===this.getCodec()){e=a;break}}if(!e)throw new Error("Could not find matching track in underlying media data.");this.internalTrack.backingTrack=e})()}delegate(t){return this.internalTrack.backingTrack?t():this.hydrate().then(t)}getCodec(){throw new Error("Not implemented on base class.")}getDisposition(){return{...we,default:this.internalTrack.autoselect,primary:this.internalTrack.default}}getId(){return this.internalTrack.id}getPairingMask(){return this.internalTrack.pairingMask}getInternalCodecId(){return null}getLanguageCode(){return this.internalTrack.languageCode}getName(){return this.internalTrack.name}getNumber(){h(this.internalTrack.demuxer.internalTracks);let t=this.internalTrack.info.type,e=0;for(let r of this.internalTrack.demuxer.internalTracks)if(r.info.type===t&&e++,r===this.internalTrack)break;return e}getTimeResolution(){return this.delegate(()=>this.internalTrack.backingTrack.getTimeResolution())}isRelativeToUnixEpoch(){return this.delegate(()=>this.internalTrack.backingTrack.isRelativeToUnixEpoch())}getBitrate(){return this.internalTrack.peakBitrate}getAverageBitrate(){return this.internalTrack.averageBitrate}async getDurationFromMetadata(t){return await this.hydrate(),this.internalTrack.backingTrack.getDurationFromMetadata(t)}async getLiveRefreshInterval(){return await this.hydrate(),this.internalTrack.backingTrack.getLiveRefreshInterval()}getHasOnlyKeyPackets(){return this.internalTrack.hasOnlyKeyPackets||null}async getFirstPacket(t){return await this.hydrate(),this.internalTrack.backingTrack.getFirstPacket(t)}async getPacket(t,e){return await this.hydrate(),this.internalTrack.backingTrack.getPacket(t,e)}async getKeyPacket(t,e){return await this.hydrate(),this.internalTrack.backingTrack.getKeyPacket(t,e)}async getNextPacket(t,e){return await this.hydrate(),this.internalTrack.backingTrack.getNextPacket(t,e)}async getNextKeyPacket(t,e){return await this.hydrate(),this.internalTrack.backingTrack.getNextKeyPacket(t,e)}},Ja=class extends Za{constructor(t){super(t)}get backingVideoTrack(){return this.internalTrack.backingTrack}getType(){return"video"}getCodec(){return Ge(this.internalTrack.fullCodecString)}getCodedWidth(){return this.delegate(()=>this.backingVideoTrack.getCodedWidth())}getCodedHeight(){return this.delegate(()=>this.backingVideoTrack.getCodedHeight())}getSquarePixelWidth(){return this.delegate(()=>this.backingVideoTrack.getSquarePixelWidth())}getSquarePixelHeight(){return this.delegate(()=>this.backingVideoTrack.getSquarePixelHeight())}getMetadataDisplayWidth(){return this.backingVideoTrack?null:this.internalTrack.info.width}getMetadataDisplayHeight(){return this.backingVideoTrack?null:this.internalTrack.info.height}getRotation(){return this.delegate(()=>this.backingVideoTrack.getRotation())}async getColorSpace(){return await this.hydrate(),this.backingVideoTrack.getColorSpace()}async canBeTransparent(){return await this.hydrate(),this.backingVideoTrack.canBeTransparent()}getMetadataCodecParameterString(){return this.backingVideoTrack?null:this.internalTrack.fullCodecString}async getDecoderConfig(){return await this.hydrate(),this.backingVideoTrack.getDecoderConfig()}},es=class extends Za{constructor(t){super(t)}get backingAudioTrack(){return this.internalTrack.backingTrack}getType(){return"audio"}getCodec(){return Ge(this.internalTrack.fullCodecString)}getNumberOfChannels(){return this.internalTrack.info.numberOfChannels!==null?this.internalTrack.info.numberOfChannels:this.delegate(()=>this.backingAudioTrack.getNumberOfChannels())}getSampleRate(){return this.delegate(()=>this.backingAudioTrack.getSampleRate())}getMetadataCodecParameterString(){return this.backingAudioTrack?null:this.internalTrack.fullCodecString}async getDecoderConfig(){return await this.hydrate(),this.backingAudioTrack.getDecoderConfig()}},$a=n=>{let t=n.get("default");if(t===null)return!1;let e=t.toUpperCase();if(e==="YES")return!0;if(e==="NO")return!1;throw new Error(`Invalid M3U8 file; #EXT-X-MEDIA DEFAULT attribute must be YES or NO, got "${t}".`)},Nl=n=>{let t=n.get("autoselect");if(t===null)return!1;let e=t.toUpperCase();if(e==="YES")return!0;if(e==="NO")return!1;throw new Error(`Invalid M3U8 file; #EXT-X-MEDIA AUTOSELECT attribute must be YES or NO, got "${t}".`)},Ll=n=>{if(n===null)return J;let t=n.split("-")[0];return t||J};var Ve=class{constructor(){this._isIsobmff=!1}},Sn=class extends Ve{constructor(){super(...arguments);this._isIsobmff=!0}async _getMajorBrand(e){let r=e._reader.requestSlice(0,12);if(r instanceof Promise&&(r=await r),!r)return null;r.skip(4);let i=te(r,4);return i!=="ftyp"&&i!=="styp"?null:te(r,4)}_createDemuxer(e){return new ha(e)}},ts=class extends Sn{async _canReadInput(t){let e=await this._getMajorBrand(t);if(e!==null)return e!=="qt ";let r=t._reader.requestSlice(4,4);if(r instanceof Promise&&(r=await r),!r)return!1;let i=te(r,4);return i==="moof"||i==="sidx"}get name(){return"MP4"}get mimeType(){return"video/mp4"}},rs=class extends Sn{async _canReadInput(t){return await this._getMajorBrand(t)==="qt "}get name(){return"QuickTime File Format"}get mimeType(){return"video/quicktime"}},An=class extends Ve{async isSupportedEBMLOfDocType(t,e){let r=t._reader.requestSlice(0,dt);if(r instanceof Promise&&(r=await r),!r)return!1;let i=xo(r);if(i===null||i<1||i>8||G(r,i)!==440786851)return!1;let s=Po(r);if(typeof s!="number")return!1;let o=t._reader.requestSlice(r.filePos,s);if(o instanceof Promise&&(o=await o),!o)return!1;let c=r.filePos;for(;o.filePos<=c+s-Oe;){let u=mt(o);if(!u)break;let{id:l,size:m}=u,d=o.filePos;if(m===void 0)return!1;switch(l){case 17030:if(G(o,m)!==1)return!1;break;case 17143:if(G(o,m)!==1)return!1;break;case 17026:if(fr(o,m)!==e)return!1;break;case 17031:if(G(o,m)>4)return!1;break}o.filePos=d+m}return!0}_canReadInput(t){return this.isSupportedEBMLOfDocType(t,"matroska")}_createDemuxer(t){return new Pa(t)}get name(){return"Matroska"}get mimeType(){return"video/x-matroska"}},is=class extends An{_canReadInput(t){return this.isSupportedEBMLOfDocType(t,"webm")}get name(){return"WebM"}get mimeType(){return"video/webm"}},ns=class extends Ve{async _canReadInput(t){let e=0;for(;;){let m=t._reader.requestSlice(e,ze);if(m instanceof Promise&&(m=await m),!m)break;let d=ft(m);if(!d)break;e=m.filePos+d.size}let r=await mn(t._reader,e,e+4096);if(!r)return!1;let i=r.header,a=cr(i.mpegVersionId,i.channel),s=t._reader.requestSlice(r.startPos+a,4);if(s instanceof Promise&&(s=await s),!s)return!1;let o=F(s);if(o===or||o===ai)return!0;e=r.startPos+r.header.totalSize;let u=await mn(t._reader,e,e+4);if(!u)return!1;let l=u.header;return!(i.channel!==l.channel||i.sampleRate!==l.sampleRate)}_createDemuxer(t){return new Ia(t)}get name(){return"MP3"}get mimeType(){return"audio/mpeg"}},as=class extends Ve{async _canReadInput(t){let e=t._reader.requestSlice(0,12);if(e instanceof Promise&&(e=await e),!e)return!1;let r=te(e,4);return r!=="RIFF"&&r!=="RIFX"&&r!=="RF64"?!1:(e.skip(4),te(e,4)==="WAVE")}_createDemuxer(t){return new Ma(t)}get name(){return"WAVE"}get mimeType(){return"audio/wav"}},ss=class extends Ve{async _canReadInput(t){let e=t._reader.requestSlice(0,4);return e instanceof Promise&&(e=await e),e?te(e,4)==="OggS":!1}_createDemuxer(t){return new Fa(t)}get name(){return"Ogg"}get mimeType(){return"application/ogg"}},os=class extends Ve{async _canReadInput(t){let e=t._reader.requestSlice(0,4);return e instanceof Promise&&(e=await e),e?te(e,4)==="fLaC":!1}get name(){return"FLAC"}get mimeType(){return"audio/flac"}_createDemuxer(t){return new Ua(t)}},cs=class extends Ve{async _canReadInput(t){let e=0;for(;;){let s=t._reader.requestSlice(e,ze);if(s instanceof Promise&&(s=await s),!s)break;let o=ft(s);if(!o)break;e=s.filePos+o.size}let r=t._reader.requestSliceRange(e,Qt,Ne);if(r instanceof Promise&&(r=await r),!r)return!1;let i=De(r);if(!i||(e+=i.frameLength,r=t._reader.requestSliceRange(e,Qt,Ne),r instanceof Promise&&(r=await r),!r))return!1;let a=De(r);return a?i.objectType===a.objectType&&i.samplingFrequencyIndex===a.samplingFrequencyIndex&&i.channelConfiguration===a.channelConfiguration:!1}_createDemuxer(t){return new Ba(t)}get name(){return"ADTS"}get mimeType(){return"audio/aac"}},us=class extends Ve{async _canReadInput(t){let e=205,r=t._reader.requestSlice(0,e);if(r instanceof Promise&&(r=await r),!r)return!1;let i=B(r,e);return i[0]===71&&i[188]===71||i[0]===71&&i[204]===71?!0:i[4]===71&&i[196]===71}_createDemuxer(t){return new Na(t)}get name(){return"MPEG Transport Stream"}get mimeType(){return"video/MP2T"}},Si=class extends Ve{async _canReadInput(t){let e=t._reader.requestSlice(0,7);if(e instanceof Promise&&(e=await e),!e||!(te(e,7)==="#EXTM3U"))return!1;if(!(t._rootSource instanceof Qe))throw new TypeError("HLS inputs require `InputOptions.source` to be a PathedSource or a ref to one.");return t._rootSource._usedForHls=!0,!0}_createDemuxer(t){return new Ya(t)}get name(){return"HTTP Live Streaming (HLS)"}get mimeType(){return St}},nc=new ts,ac=new rs,Wl=new An,Hl=new is,sc=new ns,ql=new as,Gl=new ss,oc=new cs,Kl=new os,cc=new us,uc=new Si,Vm=[uc,nc,ac,Wl,Hl,ql,Gl,Kl,sc,oc,cc],Um=[uc,nc,ac,sc,oc,cc],Ql=(n,t)=>{if(!n||typeof n!="object")throw new TypeError(`${t}, when provided, must be an object.`);if(n.isobmff!==void 0){if(!n.isobmff||typeof n.isobmff!="object")throw new TypeError(`${t}.isobmff, when provided, must be an object.`);if(n.isobmff.resolveKeyId!==void 0&&typeof n.isobmff.resolveKeyId!="function")throw new TypeError(`${t}.isobmff.resolveKeyId, when provided, must be a function.`)}};var ls=new Map,ds=new Map,zm=(n,t)=>{if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.codec!==void 0&&typeof t.codec!="string")throw new TypeError("options.codec, when provided, must be a string.");if(t.codec!==void 0&&Ge(t.codec)!==n)throw new TypeError(`options.codec, when provided, must match the specified codec (${n}).`);if(t.codedWidth!==void 0&&(!Number.isInteger(t.codedWidth)||t.codedWidth<=0))throw new TypeError("options.codedWidth, when provided, must be a positive integer.");if(t.codedHeight!==void 0&&(!Number.isInteger(t.codedHeight)||t.codedHeight<=0))throw new TypeError("options.codedHeight, when provided, must be a positive integer.");if(t.displayAspectWidth!==void 0&&(!Number.isInteger(t.displayAspectWidth)||t.displayAspectWidth<=0))throw new TypeError("options.displayAspectWidth, when provided, must be a positive integer.");if(t.displayAspectHeight!==void 0&&(!Number.isInteger(t.displayAspectHeight)||t.displayAspectHeight<=0))throw new TypeError("options.displayAspectHeight, when provided, must be a positive integer.");if(t.description!==void 0&&!Rt(t.description))throw new TypeError("options.description, when provided, must be a buffer source.");if(t.hardwareAcceleration!==void 0&&!["no-preference","prefer-hardware","prefer-software"].includes(t.hardwareAcceleration))throw new TypeError("options.hardwareAcceleration, when provided, must be 'no-preference', 'prefer-hardware' or 'prefer-software'.");if(t.optimizeForLatency!==void 0&&typeof t.optimizeForLatency!="boolean")throw new TypeError("options.optimizeForLatency, when provided, must be a boolean.")},Nm=(n,t)=>{if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.codec!==void 0&&typeof t.codec!="string")throw new TypeError("options.codec, when provided, must be a string.");if(t.codec!==void 0&&Ge(t.codec)!==n)throw new TypeError(`options.codec, when provided, must match the specified codec (${n}).`);if(t.numberOfChannels!==void 0&&(!Number.isInteger(t.numberOfChannels)||t.numberOfChannels<=0))throw new TypeError("options.numberOfChannels, when provided, must be a positive integer.");if(t.sampleRate!==void 0&&(!Number.isInteger(t.sampleRate)||t.sampleRate<=0))throw new TypeError("options.sampleRate, when provided, must be a positive integer.");if(t.description!==void 0&&!Rt(t.description))throw new TypeError("options.description, when provided, must be a buffer source.")},Lm=n=>re.includes(n)?lc(n):ae.includes(n)?dc(n):!1,lc=async(n,t={})=>{if(!re.includes(n))return!1;zm(n,t);let e={...t,codedWidth:t.codedWidth??1280,codedHeight:t.codedHeight??720,codec:t.codec??Kn(n,1280,720,1e6)};e.description??=Vu(e);let r=JSON.stringify(e),i=ls.get(r);if(i)return i;let a=(async()=>gr.some(o=>o.supports(n,e))?!0:typeof VideoDecoder>"u"?!1:(await VideoDecoder.isConfigSupported(e)).supported===!0)();return ls.set(r,a),a},dc=async(n,t={})=>{if(!ae.includes(n))return!1;Nm(n,t);let e={...t,numberOfChannels:t.numberOfChannels??2,sampleRate:t.sampleRate??48e3,codec:t.codec??jn(n,2,48e3)};if(e.description===void 0){let s=Uu(e);if(s===!1)return!1;e.description=s}let r=JSON.stringify(e),i=ds.get(r);if(i)return i;let a=(async()=>br.some(o=>o.supports(n,e))||ee.includes(n)?!0:typeof AudioDecoder>"u"?!1:(await AudioDecoder.isConfigSupported(e)).supported===!0)();return ds.set(r,a),a},Wm=async()=>{let[n,t]=await Promise.all([jl(),Xl()]);return[...n,...t]},jl=async(n=re,t)=>{let e=await Promise.all(n.map(r=>lc(r,t)));return n.filter((r,i)=>e[i])},Xl=async(n=ae,t)=>{let e=await Promise.all(n.map(r=>dc(r,t)));return n.filter((r,i)=>e[i])};ei();var $l=-1/0,Yl=-1/0,In=null;typeof FinalizationRegistry<"u"&&(In=new FinalizationRegistry(n=>{let t=performance.now();n.type==="video"?(t-$l>=1e3&&(console.error("A VideoSample was garbage collected without first being closed. For proper resource management, make sure to call close() on all your VideoSamples as soon as you're done using them."),$l=t),typeof VideoFrame<"u"&&n.data instanceof VideoFrame&&n.data.close()):(t-Yl>=1e3&&(console.error("An AudioSample was garbage collected without first being closed. For proper resource management, make sure to call close() on all your AudioSamples as soon as you're done using them."),Yl=t),typeof AudioData<"u"&&n.data instanceof AudioData&&n.data.close())}));var At=class{constructor(){this._referenceCount=0;this._lastAllocationBuffer=null}},fs=["I420","I420P10","I420P12","I420A","I420AP10","I420AP12","I422","I422P10","I422P12","I422A","I422AP10","I422AP12","I444","I444P10","I444P12","I444A","I444AP10","I444AP12","NV12","RGBA","RGBX","BGRA","BGRX"],Hm=new Set(fs),Ae=class n{constructor(t,e){this._closed=!1;if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer||ArrayBuffer.isView(t)){if(!e||typeof e!="object")throw new TypeError("init must be an object.");if(e.format===void 0||!Hm.has(e.format))throw new TypeError("init.format must be one of: "+fs.join(", "));if(!Number.isInteger(e.codedWidth)||e.codedWidth<=0)throw new TypeError("init.codedWidth must be a positive integer.");if(!Number.isInteger(e.codedHeight)||e.codedHeight<=0)throw new TypeError("init.codedHeight must be a positive integer.");if(e.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(!Number.isFinite(e.timestamp))throw new TypeError("init.timestamp must be a number.");if(e.duration!==void 0&&(!Number.isFinite(e.duration)||e.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");if(e.layout!==void 0){if(!Array.isArray(e.layout))throw new TypeError("init.layout, when provided, must be an array.");for(let i of e.layout){if(!i||typeof i!="object"||Array.isArray(i))throw new TypeError("Each entry in init.layout must be an object.");if(!Number.isInteger(i.offset)||i.offset<0)throw new TypeError("plane.offset must be a non-negative integer.");if(!Number.isInteger(i.stride)||i.stride<0)throw new TypeError("plane.stride must be a non-negative integer.")}}if(e.visibleRect!==void 0&&po(e.visibleRect,"init.visibleRect"),e.displayWidth!==void 0&&(!Number.isInteger(e.displayWidth)||e.displayWidth<=0))throw new TypeError("init.displayWidth, when provided, must be a positive integer.");if(e.displayHeight!==void 0&&(!Number.isInteger(e.displayHeight)||e.displayHeight<=0))throw new TypeError("init.displayHeight, when provided, must be a positive integer.");if(e.displayWidth!==void 0!=(e.displayHeight!==void 0))throw new TypeError("init.displayWidth and init.displayHeight must be either both provided or both omitted.");this._data=j(t).slice(),this._layout=e.layout??Km(e.format,e.codedWidth,e.codedHeight),this.format=e.format,this.rotation=e.rotation??0,this.timestamp=e.timestamp,this.duration=e.duration??0;let r=e.colorSpace??null;r===null&&(this.format==="RGBA"||this.format==="RGBX"||this.format==="BGRA"||this.format==="BGRX"?r={primaries:"bt709",transfer:"iec61966-2-1",matrix:"rgb",fullRange:!0}:r={primaries:"bt709",transfer:"bt709",matrix:"bt709",fullRange:!1}),this.colorSpace=new xi(r),this.visibleRect={left:e.visibleRect?.left??0,top:e.visibleRect?.top??0,width:e.visibleRect?.width??e.codedWidth,height:e.visibleRect?.height??e.codedHeight},e.displayWidth!==void 0?(this.squarePixelWidth=this.rotation%180===0?e.displayWidth:e.displayHeight,this.squarePixelHeight=this.rotation%180===0?e.displayHeight:e.displayWidth):(this.squarePixelWidth=this.visibleRect.width,this.squarePixelHeight=this.visibleRect.height)}else if(typeof VideoFrame<"u"&&t instanceof VideoFrame){if(e?.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(e?.timestamp!==void 0&&!Number.isFinite(e?.timestamp))throw new TypeError("init.timestamp, when provided, must be a number.");if(e?.duration!==void 0&&(!Number.isFinite(e.duration)||e.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");e?.visibleRect!==void 0&&po(e.visibleRect,"init.visibleRect"),this._data=t,this._layout=null,this.format=t.format,this.visibleRect={left:t.visibleRect?.x??0,top:t.visibleRect?.y??0,width:t.visibleRect?.width??t.codedWidth,height:t.visibleRect?.height??t.codedHeight},this.rotation=e?.rotation??0,this.squarePixelWidth=t.displayWidth,this.squarePixelHeight=t.displayHeight,this.timestamp=e?.timestamp??t.timestamp/1e6,this.duration=e?.duration??(t.duration??0)/1e6,this.colorSpace=new xi(t.colorSpace)}else if(typeof HTMLImageElement<"u"&&t instanceof HTMLImageElement||typeof SVGImageElement<"u"&&t instanceof SVGImageElement||typeof ImageBitmap<"u"&&t instanceof ImageBitmap||typeof HTMLVideoElement<"u"&&t instanceof HTMLVideoElement||typeof HTMLCanvasElement<"u"&&t instanceof HTMLCanvasElement||typeof OffscreenCanvas<"u"&&t instanceof OffscreenCanvas){if(!e||typeof e!="object")throw new TypeError("init must be an object.");if(e.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(!Number.isFinite(e.timestamp))throw new TypeError("init.timestamp must be a number.");if(e.duration!==void 0&&(!Number.isFinite(e.duration)||e.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");if(typeof VideoFrame<"u")return new n(new VideoFrame(t,{timestamp:Math.trunc(e.timestamp*yt),duration:Math.trunc((e.duration??0)*yt)||void 0}),e);let r=0,i=0;if("naturalWidth"in t?(r=t.naturalWidth,i=t.naturalHeight):"videoWidth"in t?(r=t.videoWidth,i=t.videoHeight):"width"in t&&(r=Number(t.width),i=Number(t.height)),!r||!i)throw new TypeError("Could not determine dimensions.");let a=new OffscreenCanvas(r,i),s=a.getContext("2d",{alpha:vr(),willReadFrequently:!0});h(s),s.drawImage(t,0,0),this._data=a,this._layout=null,this.format="RGBX",this.visibleRect={left:0,top:0,width:r,height:i},this.squarePixelWidth=r,this.squarePixelHeight=i,this.rotation=e.rotation??0,this.timestamp=e.timestamp,this.duration=e.duration??0,this.colorSpace=new xi({matrix:"rgb",primaries:"bt709",transfer:"iec61966-2-1",fullRange:!0})}else if(t instanceof At){if(!e||typeof e!="object")throw new TypeError("init must be an object.");if(e.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(!Number.isFinite(e.timestamp))throw new TypeError("init.timestamp must be a number.");if(e.duration!==void 0&&(!Number.isFinite(e.duration)||e.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");if(this._data=t,t._referenceCount++,this.format=t.getFormat(),this.format!==null&&!fs.includes(this.format))throw new TypeError("getFormat() must return a VideoSamplePixelFormat or null.");if(this.visibleRect={left:0,top:0,width:t.getCodedWidth(),height:t.getCodedHeight()},!Number.isInteger(this.visibleRect.width)||this.visibleRect.width<=0)throw new TypeError("getCodedWidth() must return a positive integer.");if(!Number.isInteger(this.visibleRect.height)||this.visibleRect.height<=0)throw new TypeError("getCodedHeight() must return a positive integer.");if(this.squarePixelWidth=t.getSquarePixelWidth(),!Number.isInteger(this.squarePixelWidth)||this.squarePixelWidth<=0)throw new TypeError("getSquarePixelWidth() must return a positive integer.");if(this.squarePixelHeight=t.getSquarePixelHeight(),!Number.isInteger(this.squarePixelHeight)||this.squarePixelHeight<=0)throw new TypeError("getSquarePixelHeight() must return a positive integer.");this.rotation=e.rotation??0,this.timestamp=e.timestamp,this.duration=e.duration??0,this.colorSpace=t.getColorSpace()}else throw new TypeError("Invalid data type: Must be a BufferSource, CanvasImageSource, or VideoSampleResource.");this.pixelAspectRatio=Ot({num:this.squarePixelWidth*this.codedHeight,den:this.squarePixelHeight*this.codedWidth}),In?.register(this,{type:"video",data:this._data},this)}get codedWidth(){return this.visibleRect.width}get codedHeight(){return this.visibleRect.height}get displayWidth(){return this.rotation%180===0?this.squarePixelWidth:this.squarePixelHeight}get displayHeight(){return this.rotation%180===0?this.squarePixelHeight:this.squarePixelWidth}get microsecondTimestamp(){return Math.trunc(yt*this.timestamp)}get microsecondDuration(){return Math.trunc(yt*this.duration)}get hasAlpha(){return this.format&&this.format.includes("A")}clone(){if(this._closed)throw new Error("VideoSample is closed.");return h(this._data!==null),this._data instanceof At?new n(this._data,{timestamp:this.timestamp,duration:this.duration,rotation:this.rotation}):Pn(this._data)?new n(this._data.clone(),{timestamp:this.timestamp,duration:this.duration,rotation:this.rotation}):this._data instanceof Uint8Array?(h(this._layout),new n(this._data,{format:this.format,layout:this._layout,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.timestamp,duration:this.duration,colorSpace:this.colorSpace,rotation:this.rotation,visibleRect:this.visibleRect,displayWidth:this.displayWidth,displayHeight:this.displayHeight})):new n(this._data,{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.timestamp,duration:this.duration,colorSpace:this.colorSpace,rotation:this.rotation,visibleRect:this.visibleRect,displayWidth:this.displayWidth,displayHeight:this.displayHeight})}close(){this._closed||(In?.unregister(this),this._data instanceof At?(this._data._referenceCount--,this._data._referenceCount===0&&this._data.close()):Pn(this._data)?this._data.close():this._data=null,this._closed=!0)}allocationSize(t={}){if(Jl(t),this._closed)throw new Error("VideoSample is closed.");if((t.format??this.format)==null)throw new Error("Cannot get allocation size when format is null.");return Pn(this._data)?this._data.allocationSize(t):ed(this,t).allocationSize}async copyTo(t,e={}){if(!Rt(t))throw new TypeError("destination must be an ArrayBuffer or an ArrayBuffer view.");if(Jl(e),this._closed)throw new Error("VideoSample is closed.");if((e.format??this.format)==null)throw new Error("Cannot copy video sample data when format is null.");if(h(this._data!==null),Pn(this._data))return this._data.copyTo(t,e);if(e.format&&!["RGBA","RGBX","BGRA","BGRX"].includes(this.format)&&["RGBA","RGBX","BGRA","BGRX"].includes(e.format))if(this._data instanceof At){var u=[];try{let f=qi(u,await this._data.toRgbSample({timestamp:this.timestamp,duration:this.duration,rotation:this.rotation},e.colorSpace??"srgb"));if(!(f instanceof n))throw new TypeError("toRgbSample() must return a VideoSample.");if(!["RGBA","RGBX","BGRA","BGRX"].includes(f.format))throw new Error(`Sample returned by toRgbSample was expected to have an RGB format, got '${f.format}' instead.`);return await f.copyTo(t,e)}catch(l){var m=l,d=!0}finally{Gi(u,m,d)}}else{if(typeof VideoFrame>"u")throw new Error("For this sample, converting from a non-RGB to an RGB format requires VideoFrame to be defined.");let f=this.toVideoFrame(),p=await f.copyTo(t,e);return f.close(),p}let r=ed(this,e);h(this.format);let i=j(t);if(i.byteLength<r.allocationSize)throw new TypeError(`Destination buffer too small. Required: ${r.allocationSize}, Available: ${i.byteLength}`);let a=ps(this.format),s;if(this._data instanceof At){let f=this._data.getDataPlanes();if(f instanceof Promise&&(f=await f),!Array.isArray(f)||f.some(p=>!(p.data instanceof Uint8Array)||!Number.isInteger(p.stride)||p.stride<0))throw new TypeError('getDataPlanes() must return an array of objects with a Uint8Array "data" property and a non-negative integer "stride" property.');s=f}else if(this._data instanceof Uint8Array)h(this._layout),h(this._layout.length===a.length),s=this._layout.map((f,p)=>{let b=Math.ceil(this.codedHeight/a[p].heightDivisor);return{data:this._data.subarray(f.offset,f.offset+f.stride*b),stride:f.stride}});else{let p=this._data.getContext("2d");h(p);let b=p.getImageData(0,0,this.codedWidth,this.codedHeight);s=[{data:j(b.data),stride:4*this.codedWidth}]}let o=[],c=a.length;for(let f=0;f<c;f++){let p=r.computedLayouts[f],b=s[f].stride,g=s[f].data,y=p.sourceTop*b;y+=p.sourceLeftBytes;let k=p.destinationOffset,w=p.sourceWidthBytes,T={offset:k,stride:p.destinationStride};for(let S=0;S<p.sourceHeight;S++){if(y+w>g.byteLength)throw new Error("Source buffer OOB read.");if(k+w>i.byteLength)throw new Error("Destination buffer OOB write.");let x=g.subarray(y,y+w);i.set(x,k),y+=b,k+=p.destinationStride}o.push(T)}if(e.format!==void 0){let f=this.format.startsWith("RGB")!==e.format.startsWith("RGB"),p=this.format.includes("X")&&e.format.includes("A");if(f||p)for(let b=0;b<r.allocationSize;b+=4){if(f){let g=i[b],y=i[b+2];i[b]=y,i[b+2]=g}p&&(i[b+3]=255)}}return o}toVideoFrame(){if(this._closed)throw new Error("VideoSample is closed.");if(h(this._data!==null),this._data instanceof At){if(this.format===null)throw new Error("Cannot convert a VideoSampleResource-backed VideoSample to VideoFrame if format is null.");let t=this._data.getDataPlanes();if(t instanceof Promise)throw new Error("Cannot convert a VideoSampleResource-backed VideoSample to VideoFrame if getDataPlanes() returns a promise.");let e=t.reduce((s,o)=>s+o.data.byteLength,0),r=new Uint8Array(e),i=0,a=[];for(let s of t)r.set(s.data,i),a.push(i),i+=s.data.byteLength;return new VideoFrame(r,{format:this.format,layout:t.map((s,o)=>({offset:a[o],stride:s.stride})),codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration,colorSpace:this.colorSpace,displayWidth:this.squarePixelWidth,displayHeight:this.squarePixelHeight})}else return Pn(this._data)?new VideoFrame(this._data,{timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0}):this._data instanceof Uint8Array?new VideoFrame(this._data,{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0,colorSpace:this.colorSpace,displayWidth:this.squarePixelWidth,displayHeight:this.squarePixelHeight}):new VideoFrame(this._data,{timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0})}draw(t,e,r,i,a,s,o,c,u){let l=0,m=0,d=this.displayWidth,f=this.displayHeight,p=0,b=0,g=this.displayWidth,y=this.displayHeight;if(s!==void 0?(l=e,m=r,d=i,f=a,p=s,b=o,c!==void 0?(g=c,y=u):(g=d,y=f)):(p=e,b=r,i!==void 0&&(g=i,y=a)),!(typeof CanvasRenderingContext2D<"u"&&t instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<"u"&&t instanceof OffscreenCanvasRenderingContext2D))throw new TypeError("context must be a CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D.");if(!Number.isFinite(l))throw new TypeError("sx must be a number.");if(!Number.isFinite(m))throw new TypeError("sy must be a number.");if(!Number.isFinite(d)||d<0)throw new TypeError("sWidth must be a non-negative number.");if(!Number.isFinite(f)||f<0)throw new TypeError("sHeight must be a non-negative number.");if(!Number.isFinite(p))throw new TypeError("dx must be a number.");if(!Number.isFinite(b))throw new TypeError("dy must be a number.");if(!Number.isFinite(g)||g<0)throw new TypeError("dWidth must be a non-negative number.");if(!Number.isFinite(y)||y<0)throw new TypeError("dHeight must be a non-negative number.");if(this._closed)throw new Error("VideoSample is closed.");({sx:l,sy:m,sWidth:d,sHeight:f}=this._rotateSourceRegion(l,m,d,f,this.rotation));let k=this.toCanvasImageSource();t.save();let w=p+g/2,T=b+y/2;t.translate(w,T),t.rotate(this.rotation*Math.PI/180);let S=this.rotation%180===0?1:g/y;t.scale(1/S,S),t.drawImage(k,l,m,d,f,-g/2,-y/2,g,y),t.restore()}drawWithFit(t,e){if(!(typeof CanvasRenderingContext2D<"u"&&t instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<"u"&&t instanceof OffscreenCanvasRenderingContext2D))throw new TypeError("context must be a CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D.");if(!e||typeof e!="object")throw new TypeError("options must be an object.");if(!["fill","contain","cover"].includes(e.fit))throw new TypeError("options.fit must be 'fill', 'contain', or 'cover'.");if(e.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("options.rotation, when provided, must be 0, 90, 180, or 270.");e.crop!==void 0&&kr(e.crop,"options.");let r=t.canvas.width,i=t.canvas.height,a=e.rotation??this.rotation,[s,o]=a%180===0?[this.squarePixelWidth,this.squarePixelHeight]:[this.squarePixelHeight,this.squarePixelWidth],c=e.crop;c&&(c=Pi(c,s,o));let u,l,m,d,{sx:f,sy:p,sWidth:b,sHeight:g}=this._rotateSourceRegion(e.crop?.left??0,e.crop?.top??0,e.crop?.width??s,e.crop?.height??o,a);if(e.fit==="fill")u=0,l=0,m=r,d=i;else{let[k,w]=e.crop?[e.crop.width,e.crop.height]:[s,o],T=e.fit==="contain"?Math.min(r/k,i/w):Math.max(r/k,i/w);m=k*T,d=w*T,u=(r-m)/2,l=(i-d)/2}t.save();let y=a%180===0?1:m/d;t.translate(r/2,i/2),t.rotate(a*Math.PI/180),t.scale(1/y,y),t.translate(-r/2,-i/2),t.drawImage(this.toCanvasImageSource(),f,p,b,g,u,l,m,d),t.restore()}_rotateSourceRegion(t,e,r,i,a){return a===90?[t,e,r,i]=[e,this.squarePixelHeight-t-r,i,r]:a===180?[t,e]=[this.squarePixelWidth-t-r,this.squarePixelHeight-e-i]:a===270&&([t,e,r,i]=[this.squarePixelWidth-e-i,t,i,r]),{sx:t,sy:e,sWidth:r,sHeight:i}}toCanvasImageSource(){if(this._closed)throw new Error("VideoSample is closed.");if(h(this._data!==null),this._data instanceof At||this._data instanceof Uint8Array){let t=this.toVideoFrame();return queueMicrotask(()=>t.close()),t}else return this._data}async transform(t){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.width!==void 0&&(!Number.isInteger(t.width)||t.width<=0))throw new TypeError("options.width, when provided, must be a positive integer.");if(t.height!==void 0&&(!Number.isInteger(t.height)||t.height<=0))throw new TypeError("options.height, when provided, must be a positive integer.");if(t.roundDimensionsTo!==void 0&&(!Number.isInteger(t.roundDimensionsTo)||t.roundDimensionsTo<=0))throw new TypeError("options.roundDimensionsTo, when provided, must be a positive integer.");if(t.fit!==void 0&&!["fill","contain","cover"].includes(t.fit))throw new TypeError('options.fit, when provided, must be one of "fill", "contain", or "cover".');if(t.width!==void 0&&t.height!==void 0&&t.fit===void 0)throw new TypeError("When both options.width and options.height are provided, options.fit must also be provided.");if(t.rotate!==void 0&&![0,90,180,270].includes(t.rotate))throw new TypeError("options.rotate, when provided, must be 0, 90, 180 or 270.");if(t.crop!==void 0&&kr(t.crop,"options."),t.alpha!==void 0&&!["keep","discard"].includes(t.alpha))throw new TypeError("options.alpha, when provided, must be 'keep' or 'discard'.");let e=Ze(this.rotation+(t.rotate??0)),[r,i]=e%180===0?[this.squarePixelWidth,this.squarePixelHeight]:[this.squarePixelHeight,this.squarePixelWidth],a=t.crop;a&&(a=Pi(a,r,i));let s=a?a.width:r,o=a?a.height:i,c=s/o,u,l;t.width!==void 0&&t.height===void 0?(u=t.width,l=u/c):t.width===void 0&&t.height!==void 0?(l=t.height,u=l*c):t.width!==void 0&&t.height!==void 0?(u=t.width,l=t.height):(u=s,l=o),u=ji(u,t.roundDimensionsTo??1),l=ji(l,t.roundDimensionsTo??1);let m={width:u,height:l,fit:t.fit??"fill",rotation:e,crop:a??{left:0,top:0,width:r,height:i},alpha:t.alpha??"keep"};for(let b of mc){let g=b(this,m);if(g instanceof Promise&&(g=await g),g!==null)return g}let d=null,f=!1;for(let b of xn)if(b.canvas.width===m.width&&b.canvas.height===m.height){d=b.canvas,b.age=Zl++;break}if(d===null){if(typeof OffscreenCanvas<"u")d=new OffscreenCanvas(m.width,m.height);else{if(typeof window>"u"||typeof document>"u")throw new Error("Cannot transform VideoSamples in this environment. Either run in an environment with OffscreenCanvas or HTMLCanvasElement, or supply a custom VideoSample transformer using registerVideoSampleTransformer().");d=document.createElement("canvas"),d.width=m.width,d.height=m.height}f=!0,xn.length>=Gm&&xn.splice(ti(xn,b=>b.age),1),xn.push({canvas:d,age:Zl++})}let p=d.getContext("2d",{alpha:!0});return h(p),m.alpha==="discard"?(p.fillStyle="black",p.fillRect(0,0,m.width,m.height)):f||p.clearRect(0,0,m.width,m.height),this.drawWithFit(p,{fit:m.fit,rotation:m.rotation,crop:m.crop}),new n(d,{timestamp:this.timestamp,duration:this.duration,rotation:0})}setRotation(t){if(![0,90,180,270].includes(t))throw new TypeError("newRotation must be 0, 90, 180, or 270.");this.rotation=t}setTimestamp(t){if(!Number.isFinite(t))throw new TypeError("newTimestamp must be a number.");this.timestamp=t}setDuration(t){if(!Number.isFinite(t)||t<0)throw new TypeError("newDuration must be a non-negative number.");this.duration=t}[Symbol.dispose](){this.close()}},mc=[],qm=n=>{mc.includes(n)||mc.push(n)},Gm=3,xn=[],Zl=0,xi=class{constructor(t){if(t!==void 0){if(!t||typeof t!="object")throw new TypeError("init.colorSpace, when provided, must be an object.");let e=Object.keys(et);if(t.primaries!=null&&!e.includes(t.primaries))throw new TypeError(`init.colorSpace.primaries, when provided, must be one of ${e.join(", ")}.`);let r=Object.keys(tt);if(t.transfer!=null&&!r.includes(t.transfer))throw new TypeError(`init.colorSpace.transfer, when provided, must be one of ${r.join(", ")}.`);let i=Object.keys(rt);if(t.matrix!=null&&!i.includes(t.matrix))throw new TypeError(`init.colorSpace.matrix, when provided, must be one of ${i.join(", ")}.`);if(t.fullRange!=null&&typeof t.fullRange!="boolean")throw new TypeError("init.colorSpace.fullRange, when provided, must be a boolean.")}this.primaries=t?.primaries??null,this.transfer=t?.transfer??null,this.matrix=t?.matrix??null,this.fullRange=t?.fullRange??null}toJSON(){return{primaries:this.primaries,transfer:this.transfer,matrix:this.matrix,fullRange:this.fullRange}}},Pn=n=>typeof VideoFrame<"u"&&n instanceof VideoFrame,Pi=(n,t,e)=>{let r=Math.min(n.left,t),i=Math.min(n.top,e),a=Math.min(n.width,t-r),s=Math.min(n.height,e-i);return h(a>=0),h(s>=0),{left:r,top:i,width:a,height:s}},kr=(n,t)=>{if(!n||typeof n!="object")throw new TypeError(t+"crop, when provided, must be an object.");if(!Number.isInteger(n.left)||n.left<0)throw new TypeError(t+"crop.left must be a non-negative integer.");if(!Number.isInteger(n.top)||n.top<0)throw new TypeError(t+"crop.top must be a non-negative integer.");if(!Number.isInteger(n.width)||n.width<0)throw new TypeError(t+"crop.width must be a non-negative integer.");if(!Number.isInteger(n.height)||n.height<0)throw new TypeError(t+"crop.height must be a non-negative integer.")},Jl=n=>{if(!n||typeof n!="object")throw new TypeError("options must be an object.");if(n.colorSpace!==void 0&&!["display-p3","srgb"].includes(n.colorSpace))throw new TypeError("options.colorSpace, when provided, must be 'display-p3' or 'srgb'.");if(n.format!==void 0&&typeof n.format!="string")throw new TypeError("options.format, when provided, must be a string.");if(n.layout!==void 0){if(!Array.isArray(n.layout))throw new TypeError("options.layout, when provided, must be an array.");for(let t of n.layout){if(!t||typeof t!="object")throw new TypeError("Each entry in options.layout must be an object.");if(!Number.isInteger(t.offset)||t.offset<0)throw new TypeError("plane.offset must be a non-negative integer.");if(!Number.isInteger(t.stride)||t.stride<0)throw new TypeError("plane.stride must be a non-negative integer.")}}if(n.rect!==void 0){if(!n.rect||typeof n.rect!="object")throw new TypeError("options.rect, when provided, must be an object.");if(n.rect.x!==void 0&&(!Number.isInteger(n.rect.x)||n.rect.x<0))throw new TypeError("options.rect.x, when provided, must be a non-negative integer.");if(n.rect.y!==void 0&&(!Number.isInteger(n.rect.y)||n.rect.y<0))throw new TypeError("options.rect.y, when provided, must be a non-negative integer.");if(n.rect.width!==void 0&&(!Number.isInteger(n.rect.width)||n.rect.width<0))throw new TypeError("options.rect.width, when provided, must be a non-negative integer.");if(n.rect.height!==void 0&&(!Number.isInteger(n.rect.height)||n.rect.height<0))throw new TypeError("options.rect.height, when provided, must be a non-negative integer.")}},Km=(n,t,e)=>{let r=ps(n),i=[],a=0;for(let s of r){let o=Math.ceil(t/s.widthDivisor),c=Math.ceil(e/s.heightDivisor),u=o*s.sampleBytes,l=u*c;i.push({offset:a,stride:u}),a+=l}return i},ps=n=>{let t=(e,r,i,a,s)=>{let o=[{sampleBytes:e,widthDivisor:1,heightDivisor:1},{sampleBytes:r,widthDivisor:i,heightDivisor:a},{sampleBytes:r,widthDivisor:i,heightDivisor:a}];return s&&o.push({sampleBytes:e,widthDivisor:1,heightDivisor:1}),o};switch(n){case"I420":return t(1,1,2,2,!1);case"I420P10":case"I420P12":return t(2,2,2,2,!1);case"I420A":return t(1,1,2,2,!0);case"I420AP10":case"I420AP12":return t(2,2,2,2,!0);case"I422":return t(1,1,2,1,!1);case"I422P10":case"I422P12":return t(2,2,2,1,!1);case"I422A":return t(1,1,2,1,!0);case"I422AP10":case"I422AP12":return t(2,2,2,1,!0);case"I444":return t(1,1,1,1,!1);case"I444P10":case"I444P12":return t(2,2,1,1,!1);case"I444A":return t(1,1,1,1,!0);case"I444AP10":case"I444AP12":return t(2,2,1,1,!0);case"NV12":return[{sampleBytes:1,widthDivisor:1,heightDivisor:1},{sampleBytes:2,widthDivisor:2,heightDivisor:2}];case"RGBA":case"RGBX":case"BGRA":case"BGRX":return[{sampleBytes:4,widthDivisor:1,heightDivisor:1}];default:ne(n),h(!1)}},ed=(n,t)=>{let e={left:0,top:0,width:n.codedWidth,height:n.codedHeight},r=t.rect,i=Qm(e,r,n.codedWidth,n.codedHeight,n.format),a=t.layout,s;if(!t.format||t.format===n.format)s=n.format;else if(["RGBA","RGBX","BGRA","BGRX"].includes(t.format))s=t.format;else throw new Error("NotSupportedError: Invalid destination format.");return Xm(i,s,a)},Qm=(n,t,e,r,i)=>{let a={...n};if(t!==void 0){if(t.width===0||t.height===0)throw new TypeError("visibleRect dimensions cannot be zero.");if((t.x||0)+(t.width||0)>e)throw new TypeError("visibleRect exceeds codedWidth.");if((t.y||0)+(t.height||0)>r)throw new TypeError("visibleRect exceeds codedHeight.");a.x=t.x||0,a.y=t.y||0,a.width=t.width||0,a.height=t.height||0}if(!jm(i,a))throw new TypeError("visibleRect alignment is invalid for the format.");return a},jm=(n,t)=>{if(n===null)return!0;let e=ps(n);for(let r=0;r<e.length;r++){let i=e[r],a=i.widthDivisor,s=i.heightDivisor;if((t.x||0)%a!==0||(t.y||0)%s!==0)return!1}return!0},Xm=(n,t,e)=>{let r=ps(t),i=r.length;if(e!==void 0&&e.length!==i)throw new TypeError(`Layout must have ${i} planes.`);let a=0,s=[],o=[];for(let c=0;c<i;c++){let u=r[c],l=u.sampleBytes,m=u.widthDivisor,d=u.heightDivisor,f={destinationOffset:0,destinationStride:0,sourceTop:0,sourceHeight:0,sourceLeftBytes:0,sourceWidthBytes:0};if(f.sourceTop=Math.ceil(Math.trunc(n.y||0)/d),f.sourceHeight=Math.ceil(Math.trunc(n.height||0)/d),f.sourceLeftBytes=Math.floor(Math.trunc(n.x||0)/m)*l,f.sourceWidthBytes=Math.floor(Math.trunc(n.width||0)/m)*l,e!==void 0){let g=e[c];if(g.stride<f.sourceWidthBytes)throw new TypeError(`Stride for plane ${c} is too small.`);f.destinationOffset=g.offset,f.destinationStride=g.stride}else f.destinationOffset=a,f.destinationStride=f.sourceWidthBytes;let b=f.destinationStride*f.sourceHeight+f.destinationOffset;if(b>4294967295)throw new TypeError("Allocation size exceeds limit.");o.push(b),a=Math.max(a,b);for(let g=0;g<c;g++){let y=s[g];if(!(o[c]<=y.destinationOffset||o[g]<=f.destinationOffset))throw new TypeError("Planes overlap.")}s.push(f)}return{allocationSize:a,computedLayouts:s}},ms=new Set(["f32","f32-planar","s16","s16-planar","s32","s32-planar","u8","u8-planar"]),yr=class{constructor(){this._referenceCount=0}},ye=class n{constructor(t){this._closed=!1;if(Cn(t)){if(t.format===null)throw new TypeError("AudioData with null format is not supported.");this._data=t,this.format=t.format,this.sampleRate=t.sampleRate,this.numberOfFrames=t.numberOfFrames,this.numberOfChannels=t.numberOfChannels,this.timestamp=t.timestamp/1e6,this.duration=t.numberOfFrames/t.sampleRate}else if(t instanceof yr){if(this._data=t,t._referenceCount++,this.format=t.getFormat(),!ms.has(this.format))throw new TypeError("getFormat() must return an AudioSampleFormat.");if(this.sampleRate=t.getSampleRate(),!Number.isInteger(this.sampleRate)||this.sampleRate<=0)throw new TypeError("getSampleRate() must return a positive integer.");if(this.numberOfFrames=t.getNumberOfFrames(),!Number.isInteger(this.numberOfFrames)||this.numberOfFrames<0)throw new TypeError("getNumberOfFrames() must return a non-negative integer.");if(this.numberOfChannels=t.getNumberOfChannels(),!Number.isInteger(this.numberOfChannels)||this.numberOfChannels<=0)throw new TypeError("getNumberOfChannels() must return a positive integer.");if(this.timestamp=t.getTimestamp(),!Number.isFinite(this.timestamp))throw new TypeError("getTimestamp() must return a finite number.");this.duration=this.numberOfFrames/this.sampleRate}else{if(!t||typeof t!="object")throw new TypeError("Invalid AudioDataInit: must be an object.");if(!ms.has(t.format))throw new TypeError("Invalid AudioDataInit: invalid format.");if(!Number.isFinite(t.sampleRate)||t.sampleRate<=0)throw new TypeError("Invalid AudioDataInit: sampleRate must be > 0.");if(!Number.isInteger(t.numberOfChannels)||t.numberOfChannels===0)throw new TypeError("Invalid AudioDataInit: numberOfChannels must be an integer > 0.");if(!Number.isFinite(t?.timestamp))throw new TypeError("init.timestamp must be a number.");let e=t.data.byteLength/(Wr(t.format)*t.numberOfChannels);if(!Number.isInteger(e))throw new TypeError("Invalid AudioDataInit: data size is not a multiple of frame size.");this.format=t.format,this.sampleRate=t.sampleRate,this.numberOfFrames=e,this.numberOfChannels=t.numberOfChannels,this.timestamp=t.timestamp,this.duration=e/t.sampleRate;let r;if(t.data instanceof ArrayBuffer)r=new Uint8Array(t.data);else if(ArrayBuffer.isView(t.data))r=new Uint8Array(t.data.buffer,t.data.byteOffset,t.data.byteLength);else throw new TypeError("Invalid AudioDataInit: data is not a BufferSource.");let i=this.numberOfFrames*this.numberOfChannels*Wr(this.format);if(r.byteLength<i)throw new TypeError("Invalid AudioDataInit: insufficient data size.");this._data=r}In?.register(this,{type:"audio",data:this._data},this)}get microsecondTimestamp(){return Math.trunc(yt*this.timestamp)}get microsecondDuration(){return Math.trunc(yt*this.duration)}allocationSize(t){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(!Number.isInteger(t.planeIndex)||t.planeIndex<0)throw new TypeError("planeIndex must be a non-negative integer.");if(t.format!==void 0&&!ms.has(t.format))throw new TypeError("Invalid format.");if(t.frameOffset!==void 0&&(!Number.isInteger(t.frameOffset)||t.frameOffset<0))throw new TypeError("frameOffset must be a non-negative integer.");if(t.frameCount!==void 0&&(!Number.isInteger(t.frameCount)||t.frameCount<0))throw new TypeError("frameCount must be a non-negative integer.");if(this._closed)throw new Error("AudioSample is closed.");let e=t.format??this.format,r=t.frameOffset??0;if(r>=this.numberOfFrames)throw new RangeError("frameOffset out of range");let i=t.frameCount!==void 0?t.frameCount:this.numberOfFrames-r;if(i>this.numberOfFrames-r)throw new RangeError("frameCount out of range");let a=Wr(e),s=Ai(e);if(s&&t.planeIndex>=this.numberOfChannels)throw new RangeError("planeIndex out of range");if(!s&&t.planeIndex!==0)throw new RangeError("planeIndex out of range");return(s?i:i*this.numberOfChannels)*a}copyTo(t,e){if(!Rt(t))throw new TypeError("destination must be an ArrayBuffer or an ArrayBuffer view.");if(!e||typeof e!="object")throw new TypeError("options must be an object.");if(!Number.isInteger(e.planeIndex)||e.planeIndex<0)throw new TypeError("planeIndex must be a non-negative integer.");if(e.format!==void 0&&!ms.has(e.format))throw new TypeError("Invalid format.");if(e.frameOffset!==void 0&&(!Number.isInteger(e.frameOffset)||e.frameOffset<0))throw new TypeError("frameOffset must be a non-negative integer.");if(e.frameCount!==void 0&&(!Number.isInteger(e.frameCount)||e.frameCount<0))throw new TypeError("frameCount must be a non-negative integer.");if(this._closed)throw new Error("AudioSample is closed.");let{format:r,frameCount:i,frameOffset:a}=e,{planeIndex:s}=e,o=this.format,c=r??this.format;if(!c)throw new Error("Destination format not determined");let u=this.numberOfFrames,l=this.numberOfChannels,m=a??0;if(m>=u)throw new RangeError("frameOffset out of range");let d=i!==void 0?i:u-m;if(d>u-m)throw new RangeError("frameCount out of range");let f=Wr(c),p=Ai(c);if(p&&s>=l)throw new RangeError("planeIndex out of range");if(!p&&s!==0)throw new RangeError("planeIndex out of range");let g=(p?d:d*l)*f;if(t.byteLength<g)throw new RangeError("Destination buffer is too small");let y=V(t),k=rd(c);if(Cn(this._data))ir()&&l>2&&c!==o?$m(this._data,y,o,c,l,s,m,d):this._data.copyTo(t,{planeIndex:s,frameOffset:m,frameCount:d,format:c});else{let w=td(o),T=Wr(o),S=Ai(o),x;if(this._data instanceof yr){let A=C=>{let I=this._data.getDataPlane(C);if(!(I instanceof Uint8Array))throw new TypeError("getDataPlane() must return a Uint8Array.");let R=u*T*(S?1:l);if(I.byteLength!==R)throw new TypeError(`Data plane ${C} has invalid size. Expected exactly ${R} bytes, got ${I.byteLength} bytes.`);return I};if(S)if(p)x=A(s),s=0;else{x=new Uint8Array(u*T*l);for(let C=0;C<l;C++){let I=A(C);x.set(I,C*u*T)}}else x=A(0)}else x=this._data;let P=V(x);for(let A=0;A<d;A++)if(p){let C=A*f,I;S?I=(s*u+(A+m))*T:I=((A+m)*l+s)*T;let R=w(P,I);k(y,C,R)}else for(let C=0;C<l;C++){let R=(A*l+C)*f,v;S?v=(C*u+(A+m))*T:v=((A+m)*l+C)*T;let E=w(P,v);k(y,R,E)}}}clone(){if(this._closed)throw new Error("AudioSample is closed.");if(this._data instanceof yr){let t=new n(this._data);return t.setTimestamp(this.timestamp),t}else if(Cn(this._data)){let t=new n(this._data.clone());return t.setTimestamp(this.timestamp),t}else return new n({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.timestamp,data:this._data})}close(){this._closed||(In?.unregister(this),this._data instanceof yr?(this._data._referenceCount--,this._data._referenceCount===0&&this._data.close()):Cn(this._data)?this._data.close():this._data=new Uint8Array(0),this._closed=!0)}toAudioData(){if(this._closed)throw new Error("AudioSample is closed.");return this._data instanceof yr?this._createAudioDataFromData():Cn(this._data)?this._data.timestamp===this.microsecondTimestamp?this._data.clone():this._createAudioDataFromData():new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:this._data.buffer instanceof ArrayBuffer?this._data.buffer:this._data.slice()})}_createAudioDataFromData(){if(Ai(this.format)){let t=this.allocationSize({planeIndex:0,format:this.format}),e=new ArrayBuffer(t*this.numberOfChannels);for(let r=0;r<this.numberOfChannels;r++)this.copyTo(new Uint8Array(e,r*t,t),{planeIndex:r,format:this.format});return new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:e})}else{let t=new ArrayBuffer(this.allocationSize({planeIndex:0,format:this.format}));return this.copyTo(t,{planeIndex:0,format:this.format}),new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:t})}}toAudioBuffer(){if(this._closed)throw new Error("AudioSample is closed.");let t=new AudioBuffer({numberOfChannels:this.numberOfChannels,length:this.numberOfFrames,sampleRate:this.sampleRate}),e=new Float32Array(this.allocationSize({planeIndex:0,format:"f32-planar"})/4);for(let r=0;r<this.numberOfChannels;r++)this.copyTo(e,{planeIndex:r,format:"f32-planar"}),t.copyToChannel(e,r);return t}setTimestamp(t){if(!Number.isFinite(t))throw new TypeError("newTimestamp must be a number.");this.timestamp=t}[Symbol.dispose](){this.close()}static*_fromAudioBuffer(t,e){if(!(t instanceof AudioBuffer))throw new TypeError("audioBuffer must be an AudioBuffer.");let r=48e3*5,i=t.numberOfChannels,a=t.sampleRate,s=t.length,o=Math.floor(r/i),c=0,u=s;for(;u>0;){let l=Math.min(o,u),m=new Float32Array(i*l);for(let d=0;d<i;d++)t.copyFromChannel(m.subarray(d*l,(d+1)*l),d,c);yield new n({format:"f32-planar",sampleRate:a,numberOfFrames:l,numberOfChannels:i,timestamp:e+c/a,data:m}),c+=l,u-=l}}static fromAudioBuffer(t,e){if(!(t instanceof AudioBuffer))throw new TypeError("audioBuffer must be an AudioBuffer.");let r=48e3*5,i=t.numberOfChannels,a=t.sampleRate,s=t.length,o=Math.floor(r/i),c=0,u=s,l=[];for(;u>0;){let m=Math.min(o,u),d=new Float32Array(i*m);for(let p=0;p<i;p++)t.copyFromChannel(d.subarray(p*m,(p+1)*m),p,c);let f=new n({format:"f32-planar",sampleRate:a,numberOfFrames:m,numberOfChannels:i,timestamp:e+c/a,data:d});l.push(f),c+=m,u-=m}return l}},Wr=n=>{switch(n){case"u8":case"u8-planar":return 1;case"s16":case"s16-planar":return 2;case"s32":case"s32-planar":return 4;case"f32":case"f32-planar":return 4;default:throw new Error("Unknown AudioSampleFormat")}},Ai=n=>{switch(n){case"u8-planar":case"s16-planar":case"s32-planar":case"f32-planar":return!0;default:return!1}},td=n=>{switch(n){case"u8":case"u8-planar":return(t,e)=>(t.getUint8(e)-128)/128;case"s16":case"s16-planar":return(t,e)=>t.getInt16(e,!0)/32768;case"s32":case"s32-planar":return(t,e)=>t.getInt32(e,!0)/2147483648;case"f32":case"f32-planar":return(t,e)=>t.getFloat32(e,!0)}},rd=n=>{switch(n){case"u8":case"u8-planar":return(t,e,r)=>t.setUint8(e,ue((r+1)*127.5,0,255));case"s16":case"s16-planar":return(t,e,r)=>t.setInt16(e,ue(Math.round(r*32767),-32768,32767),!0);case"s32":case"s32-planar":return(t,e,r)=>t.setInt32(e,ue(Math.round(r*2147483647),-2147483648,2147483647),!0);case"f32":case"f32-planar":return(t,e,r)=>t.setFloat32(e,r,!0)}},Cn=n=>typeof AudioData<"u"&&n instanceof AudioData,hs=n=>{switch(n){case"u8-planar":return"u8";case"s16-planar":return"s16";case"s32-planar":return"s32";case"f32-planar":return"f32";default:return n}},$m=(n,t,e,r,i,a,s,o)=>{let c=td(e),u=rd(r),l=Wr(e),m=Wr(r),d=Ai(e);if(Ai(r))if(d){let p=new ArrayBuffer(o*l),b=V(p);n.copyTo(p,{planeIndex:a,frameOffset:s,frameCount:o,format:e});for(let g=0;g<o;g++){let y=g*l,k=g*m,w=c(b,y);u(t,k,w)}}else{let p=new ArrayBuffer(o*i*l),b=V(p);n.copyTo(p,{planeIndex:0,frameOffset:s,frameCount:o,format:e});for(let g=0;g<o;g++){let y=(g*i+a)*l,k=g*m,w=c(b,y);u(t,k,w)}}else if(d){let p=o*l,b=new ArrayBuffer(p),g=V(b);for(let y=0;y<i;y++){n.copyTo(b,{planeIndex:y,frameOffset:s,frameCount:o,format:e});for(let k=0;k<o;k++){let w=k*l,T=(k*i+y)*m,S=c(g,w);u(t,T,S)}}}else{let p=new ArrayBuffer(o*i*l),b=V(p);n.copyTo(p,{planeIndex:0,frameOffset:s,frameCount:o,format:e});for(let g=0;g<o;g++)for(let y=0;y<i;y++){let k=g*i+y,w=k*l,T=k*m,S=c(b,w);u(t,T,S)}}},gs=(n,t)=>{let e=n.allocationSize({format:t,planeIndex:0}),r=new ArrayBuffer(e);return n.copyTo(r,{format:t,planeIndex:0}),new ye({data:r,format:t,numberOfChannels:n.numberOfChannels,sampleRate:n.sampleRate,timestamp:n.timestamp,duration:n.duration})};var bs=new Map,ys=new Map,ks=n=>{if(!n||typeof n!="object")throw new TypeError("Encoding config must be an object.");if(!re.includes(n.codec))throw new TypeError(`Invalid video codec '${n.codec}'. Must be one of: ${re.join(", ")}.`);if(!(n.bitrate instanceof Pe)&&(!Number.isInteger(n.bitrate)||n.bitrate<=0))throw new TypeError("config.bitrate must be a positive integer or a quality.");if(n.keyFrameInterval!==void 0&&(!Number.isFinite(n.keyFrameInterval)||n.keyFrameInterval<0))throw new TypeError("config.keyFrameInterval, when provided, must be a non-negative number.");if(n.sizeChangeBehavior!==void 0&&!["deny","passThrough","fill","contain","cover"].includes(n.sizeChangeBehavior))throw new TypeError("config.sizeChangeBehavior, when provided, must be 'deny', 'passThrough', 'fill', 'contain' or 'cover'.");if(n.transform!==void 0){if(typeof n.transform!="object"||!n.transform)throw new TypeError("config.transform, when provided, must be an object.");if(n.transform.width!==void 0&&(!Number.isInteger(n.transform.width)||n.transform.width<=0))throw new TypeError("config.transform.width, when provided, must be a positive integer.");if(n.transform.height!==void 0&&(!Number.isInteger(n.transform.height)||n.transform.height<=0))throw new TypeError("config.transform.height, when provided, must be a positive integer.");if(n.transform.fit!==void 0&&!["fill","contain","cover"].includes(n.transform.fit))throw new TypeError('config.transform.fit, when provided, must be one of "fill", "contain", or "cover".');if(n.transform.width!==void 0&&n.transform.height!==void 0&&n.transform.fit===void 0&&!["fill","contain","cover"].includes(n.sizeChangeBehavior))throw new TypeError("When both config.transform.width and config.transform.height are provided, config.transform.fit must also be provided.");if(n.transform.fit!==void 0&&["fill","contain","cover"].includes(n.sizeChangeBehavior)&&n.transform.fit!==n.sizeChangeBehavior)throw new TypeError("config.transform.fit, when provided, cannot differ from config.sizeChangeBehavior when config.sizeChangeBehavior is 'fill', 'contain' or 'cover', as sizeChangeBehavior already determines the fitting algorithm.");if(n.transform.rotate!==void 0&&![0,90,180,270].includes(n.transform.rotate))throw new TypeError("config.transform.rotate, when provided, must be 0, 90, 180 or 270.");if(n.transform.crop!==void 0&&kr(n.transform.crop,"config.transform."),n.transform.process!==void 0&&typeof n.transform.process!="function")throw new TypeError("config.transform.process, when provided, must be a function.");if(n.transform.frameRate!==void 0&&(!Number.isFinite(n.transform.frameRate)||n.transform.frameRate<=0))throw new TypeError("config.transform.frameRate, when provided, must be a finite positive number.");if(n.transform.force!==void 0&&typeof n.transform.force!="boolean")throw new TypeError("config.transform.force, when provided, must be a boolean.")}if(n.onEncodedPacket!==void 0&&typeof n.onEncodedPacket!="function")throw new TypeError("config.onEncodedPacket, when provided, must be a function.");if(n.onEncoderConfig!==void 0&&typeof n.onEncoderConfig!="function")throw new TypeError("config.onEncoderConfig, when provided, must be a function.");id(n.codec,n)},id=(n,t)=>{if(!t||typeof t!="object")throw new TypeError("Encoding options must be an object.");if(t.alpha!==void 0&&!["discard","keep"].includes(t.alpha))throw new TypeError("options.alpha, when provided, must be 'discard' or 'keep'.");if(t.bitrateMode!==void 0&&!["constant","variable"].includes(t.bitrateMode))throw new TypeError("bitrateMode, when provided, must be 'constant' or 'variable'.");if(t.latencyMode!==void 0&&!["quality","realtime"].includes(t.latencyMode))throw new TypeError("latencyMode, when provided, must be 'quality' or 'realtime'.");if(t.fullCodecString!==void 0&&typeof t.fullCodecString!="string")throw new TypeError("fullCodecString, when provided, must be a string.");if(t.fullCodecString!==void 0&&Ge(t.fullCodecString)!==n)throw new TypeError(`fullCodecString, when provided, must be a string that matches the specified codec (${n}).`);if(t.hardwareAcceleration!==void 0&&!["no-preference","prefer-hardware","prefer-software"].includes(t.hardwareAcceleration))throw new TypeError("hardwareAcceleration, when provided, must be 'no-preference', 'prefer-hardware' or 'prefer-software'.");if(t.scalabilityMode!==void 0&&typeof t.scalabilityMode!="string")throw new TypeError("scalabilityMode, when provided, must be a string.");if(t.contentHint!==void 0&&typeof t.contentHint!="string")throw new TypeError("contentHint, when provided, must be a string.")},fc=n=>{let t=n.bitrate instanceof Pe?n.bitrate._toVideoBitrate(n.codec,n.width,n.height):n.bitrate;return{codec:n.fullCodecString??Kn(n.codec,n.width,n.height,t),width:n.width,height:n.height,displayWidth:n.squarePixelWidth,displayHeight:n.squarePixelHeight,bitrate:t,bitrateMode:n.bitrateMode,alpha:n.alpha??"discard",framerate:n.framerate,latencyMode:n.latencyMode,hardwareAcceleration:n.hardwareAcceleration,scalabilityMode:n.scalabilityMode,contentHint:n.contentHint,...Nu(n.codec)}},Ts=n=>{if(!n||typeof n!="object")throw new TypeError("Encoding config must be an object.");if(!ae.includes(n.codec))throw new TypeError(`Invalid audio codec '${n.codec}'. Must be one of: ${ae.join(", ")}.`);if(n.bitrate===void 0&&!(ee.includes(n.codec)||n.codec==="flac"))throw new TypeError("config.bitrate must be provided for compressed audio codecs.");if(n.bitrate!==void 0&&!(n.bitrate instanceof Pe)&&(!Number.isInteger(n.bitrate)||n.bitrate<=0))throw new TypeError("config.bitrate, when provided, must be a positive integer or a quality.");if(n.transform!==void 0){if(typeof n.transform!="object"||!n.transform)throw new TypeError("config.transform, when provided, must be an object.");if(n.transform.numberOfChannels!==void 0&&(!Number.isInteger(n.transform.numberOfChannels)||n.transform.numberOfChannels<=0))throw new TypeError("config.transform.numberOfChannels, when provided, must be a positive integer.");if(n.transform.sampleRate!==void 0&&(!Number.isInteger(n.transform.sampleRate)||n.transform.sampleRate<=0))throw new TypeError("config.transform.sampleRate, when provided, must be a positive integer.");if(n.transform.sampleFormat!==void 0&&!["u8","s16","s32","f32"].includes(n.transform.sampleFormat))throw new TypeError("config.transform.sampleFormat, when provided, must be one of: u8, s16, s32, f32.");if(n.transform.process!==void 0&&typeof n.transform.process!="function")throw new TypeError("config.transform.process, when provided, must be a function.")}if(n.onEncodedPacket!==void 0&&typeof n.onEncodedPacket!="function")throw new TypeError("config.onEncodedPacket, when provided, must be a function.");if(n.onEncoderConfig!==void 0&&typeof n.onEncoderConfig!="function")throw new TypeError("config.onEncoderConfig, when provided, must be a function.");nd(n.codec,n)},nd=(n,t)=>{if(!t||typeof t!="object")throw new TypeError("Encoding options must be an object.");if(t.bitrateMode!==void 0&&!["constant","variable"].includes(t.bitrateMode))throw new TypeError("bitrateMode, when provided, must be 'constant' or 'variable'.");if(t.fullCodecString!==void 0&&typeof t.fullCodecString!="string")throw new TypeError("fullCodecString, when provided, must be a string.");if(t.fullCodecString!==void 0&&Ge(t.fullCodecString)!==n)throw new TypeError(`fullCodecString, when provided, must be a string that matches the specified codec (${n}).`)},pc=n=>{let t=n.bitrate instanceof Pe?n.bitrate._toAudioBitrate(n.codec):n.bitrate;return{codec:n.fullCodecString??jn(n.codec,n.numberOfChannels,n.sampleRate),numberOfChannels:n.numberOfChannels,sampleRate:n.sampleRate,bitrate:t,bitrateMode:n.bitrateMode,...Lu(n.codec)}},Pe=class{constructor(t){this._factor=t}_toVideoBitrate(t,e,r){let i=e*r,a={avc:1,hevc:.6,vp9:.6,av1:.4,vp8:1.2},s=1920*1080,o=3e6,c=Math.pow(i/s,.95),m=o*c*a[t]*this._factor;return Math.ceil(m/1e3)*1e3}_toAudioBitrate(t){if(ee.includes(t)||t==="flac")return;let r={aac:128e3,opus:64e3,mp3:16e4,vorbis:64e3,ac3:384e3,eac3:192e3}[t];if(!r)throw new Error(`Unhandled codec: ${t}`);let i=r*this._factor;return t==="aac"?i=[96e3,128e3,16e4,192e3].reduce((s,o)=>Math.abs(o-i)<Math.abs(s-i)?o:s):t==="opus"||t==="vorbis"?i=Math.max(6e3,i):t==="mp3"&&(i=[8e3,16e3,24e3,32e3,4e4,48e3,64e3,8e4,96e3,112e3,128e3,16e4,192e3,224e3,256e3,32e4].reduce((s,o)=>Math.abs(o-i)<Math.abs(s-i)?o:s)),Math.round(i/1e3)*1e3}},Ym=new Pe(.3),Zm=new Pe(.6),Jm=new Pe(1),ws=new Pe(2),ef=new Pe(4),tf=n=>{if(re.includes(n))return Ss(n);if(ae.includes(n))return As(n);if(Me.includes(n))return xs(n);throw new TypeError(`Unknown codec '${n}'.`)},Ss=async(n,t={})=>{let{width:e=1280,height:r=720,bitrate:i=1e6,...a}=t;if(!re.includes(n))return!1;if(!Number.isInteger(e)||e<=0)throw new TypeError("width must be a positive integer.");if(!Number.isInteger(r)||r<=0)throw new TypeError("height must be a positive integer.");if(!(i instanceof Pe)&&(!Number.isInteger(i)||i<=0))throw new TypeError("bitrate must be a positive integer or a quality.");id(n,a);let s=fc({codec:n,width:e,height:r,bitrate:i,framerate:void 0,...a,alpha:"discard"}),o=JSON.stringify(s),c=bs.get(o);if(c)return c;let u=(async()=>Ci.some(d=>d.supports(n,s))?!0:typeof VideoEncoder>"u"||(e%2===1||r%2===1)&&(n==="avc"||n==="hevc")||!(await VideoEncoder.isConfigSupported(s)).supported?!1:vr()?new Promise(async d=>{try{let f=new VideoEncoder({output:()=>{},error:()=>d(!1)});f.configure(s);let p=new Uint8Array(e*r*4),b=new VideoFrame(p,{format:"RGBA",codedWidth:e,codedHeight:r,timestamp:0});f.encode(b),b.close(),await f.flush(),d(!0)}catch{d(!1)}}):!0)();return bs.set(o,u),u},As=async(n,t={})=>{let{numberOfChannels:e=2,sampleRate:r=48e3,bitrate:i=128e3,...a}=t;if(!ae.includes(n))return!1;if(!Number.isInteger(e)||e<=0)throw new TypeError("numberOfChannels must be a positive integer.");if(!Number.isInteger(r)||r<=0)throw new TypeError("sampleRate must be a positive integer.");if(!(i instanceof Pe)&&(!Number.isInteger(i)||i<=0))throw new TypeError("bitrate must be a positive integer.");nd(n,a);let s=pc({codec:n,numberOfChannels:e,sampleRate:r,bitrate:i,...a}),o=JSON.stringify(s),c=ys.get(o);if(c)return c;let u=(async()=>Ii.some(m=>m.supports(n,s))||ee.includes(n)?!0:typeof AudioEncoder>"u"?!1:(await AudioEncoder.isConfigSupported(s)).supported===!0)();return ys.set(o,u),u},xs=async n=>!!Me.includes(n),rf=async()=>{let[n,t,e]=await Promise.all([ad(),En(),sd()]);return[...n,...t,...e]},ad=async(n=re,t)=>{let e=await Promise.all(n.map(r=>Ss(r,t)));return n.filter((r,i)=>e[i])},En=async(n=ae,t)=>{let e=await Promise.all(n.map(r=>As(r,t)));return n.filter((r,i)=>e[i])},sd=async(n=Me)=>{let t=await Promise.all(n.map(xs));return n.filter((e,r)=>t[r])},hc=async(n,t)=>{for(let e of n)if(await Ss(e,t))return e;return null},nf=async(n,t)=>{for(let e of n)if(await As(e,t))return e;return null},af=async n=>{for(let t of n)if(await xs(t))return t;return null};var Ps=class{static supports(t,e){return!1}},Cs=class{static supports(t,e){return!1}},Is=class{static supports(t,e){return!1}},Es=class{static supports(t,e){return!1}},gr=[],br=[],Ci=[],Ii=[],sf=n=>{if(n.prototype instanceof Ps){let t=n;if(gr.includes(t)){console.warn("Video decoder already registered.");return}gr.push(t),ls.clear()}else if(n.prototype instanceof Cs){let t=n;if(br.includes(t)){console.warn("Audio decoder already registered.");return}br.push(t),ds.clear()}else throw new TypeError("Decoder must be a CustomVideoDecoder or CustomAudioDecoder.")},of=n=>{if(n.prototype instanceof Is){let t=n;if(Ci.includes(t)){console.warn("Video encoder already registered.");return}Ci.push(t),bs.clear()}else if(n.prototype instanceof Es){let t=n;if(Ii.includes(t)){console.warn("Audio encoder already registered.");return}Ii.push(t),ys.clear()}else throw new TypeError("Encoder must be a CustomVideoEncoder or CustomAudioEncoder.")};var od=n=>{let r=n,i=4096,a=0,s=12,o=0;for(r<0&&(r=-r,a=128),r+=33,r>8191&&(r=8191);(r&i)!==i&&s>=5;)i>>=1,s--;return o=r>>s-4&15,~(a|s-5<<4|o)&255},cd=n=>{let e=0,r=0,i=~n;i&128&&(i&=-129,e=-1),r=((i&240)>>4)+5;let a=(1<<r|(i&15)<<r-4|1<<r-5)-33;return e===0?a:-a},ud=n=>{let e=2048,r=0,i=11,a=0,s=n;for(s<0&&(s=-s,r=128),s>4095&&(s=4095);(s&e)!==e&&i>=5;)e>>=1,i--;return a=s>>(i===4?1:i-4)&15,(r|i-4<<4|a)^85},ld=n=>{let t=0,e=0,r=n^85;r&128&&(r&=-129,t=-1),e=((r&240)>>4)+4;let i=0;return e!==4?i=1<<e|(r&15)<<e-4|1<<e-5:i=r<<1|1,t===0?i:-i};var Hr=n=>{if(!n||typeof n!="object")throw new TypeError("options must be an object.");if(n.metadataOnly!==void 0&&typeof n.metadataOnly!="boolean")throw new TypeError("options.metadataOnly, when defined, must be a boolean.");if(n.verifyKeyPackets!==void 0&&typeof n.verifyKeyPackets!="boolean")throw new TypeError("options.verifyKeyPackets, when defined, must be a boolean.");if(n.verifyKeyPackets&&n.metadataOnly)throw new TypeError("options.verifyKeyPackets and options.metadataOnly cannot be enabled together.");if(n.skipLiveWait!==void 0&&typeof n.skipLiveWait!="boolean")throw new TypeError("options.skipLiveWait, when defined, must be a boolean.")},Xt=n=>{if(!nr(n))throw new TypeError("timestamp must be a number.")},gc=(n,t,e)=>e.verifyKeyPackets?t.then(async r=>{if(!r||r.type==="delta")return r;let i=await n.determinePacketType(r);return i&&(r.type=i),r}):t,xt=class{constructor(t){if(!(t instanceof Kr))throw new TypeError("track must be an InputTrack.");this._track=t}async getFirstPacket(t={}){if(Hr(t),this._track.input._disposed)throw new be;return gc(this._track,this._track._backing.getFirstPacket(t),t)}async getFirstKeyPacket(t={}){Hr(t);let e=await this.getFirstPacket(t);return e?e.type==="key"?e:this.getNextKeyPacket(e,t):null}async getPacket(t,e={}){if(Xt(t),Hr(e),this._track.input._disposed)throw new be;return gc(this._track,this._track._backing.getPacket(t,e),e)}async getNextPacket(t,e={}){if(!(t instanceof Q))throw new TypeError("packet must be an EncodedPacket.");if(Hr(e),this._track.input._disposed)throw new be;return gc(this._track,this._track._backing.getNextPacket(t,e),e)}async getKeyPacket(t,e={}){if(Xt(t),Hr(e),this._track.input._disposed)throw new be;if(!e.verifyKeyPackets)return this._track._backing.getKeyPacket(t,e);let r=await this._track._backing.getKeyPacket(t,e);return r&&(h(r.type==="key"),await this._track.determinePacketType(r)==="delta"?this.getKeyPacket(r.timestamp-1/await this._track.getTimeResolution(),e):r)}async getNextKeyPacket(t,e={}){if(!(t instanceof Q))throw new TypeError("packet must be an EncodedPacket.");if(Hr(e),this._track.input._disposed)throw new be;if(!e.verifyKeyPackets)return this._track._backing.getNextKeyPacket(t,e);let r=await this._track._backing.getNextKeyPacket(t,e);return r&&(h(r.type==="key"),await this._track.determinePacketType(r)==="delta"?this.getNextKeyPacket(r,e):r)}packets(t,e,r={}){if(t!==void 0&&!(t instanceof Q))throw new TypeError("startPacket must be an EncodedPacket.");if(t!==void 0&&t.isMetadataOnly&&!r?.metadataOnly)throw new TypeError("startPacket can only be metadata-only if options.metadataOnly is enabled.");if(e!==void 0&&!(e instanceof Q))throw new TypeError("endPacket must be an EncodedPacket.");if(Hr(r),this._track.input._disposed)throw new be;let i=[],{promise:a,resolve:s}=Z(),{promise:o,resolve:c}=Z(),u=!1,l=!1,m=null,d=[],f=()=>Math.max(2,d.length);(async()=>{let b=t??await this.getFirstPacket(r);for(;b&&!l&&!this._track.input._disposed&&!(e&&b.sequenceNumber>=e?.sequenceNumber);){if(i.length>f()){({promise:o,resolve:c}=Z()),await o;continue}i.push(b),s(),{promise:a,resolve:s}=Z(),b=await this.getNextPacket(b,r)}u=!0,s()})().catch(b=>{m||(m=b,s())});let p=this._track;return{async next(){for(;;){if(p.input._disposed)throw new be;if(l)return{value:void 0,done:!0};if(m)throw m;if(i.length>0){let b=i.shift(),g=performance.now();for(d.push(g);d.length>0&&g-d[0]>=1e3;)d.shift();return c(),{value:b,done:!1}}else{if(u)return{value:void 0,done:!0};await a}}},async return(){return l=!0,c(),s(),{value:void 0,done:!0}},async throw(b){throw b},[Symbol.asyncIterator](){return this}}}},vn=class{constructor(t,e){this.onSample=t;this.onError=e}},_n=class{mediaSamplesInRange(t=-1/0,e=1/0,r){Xt(t),Xt(e);let i=[],a=!1,s=null,{promise:o,resolve:c}=Z(),{promise:u,resolve:l}=Z(),m=!1,d=!1,f=!1,p=null,b={...r,verifyKeyPackets:!0,metadataOnly:!1};(async()=>{let k=await this._createDecoder(A=>{if(l(),A.timestamp>=e&&(d=!0),d){A.close();return}s&&(A.timestamp>t?(i.push(s),a=!0):s.close()),A.timestamp>=t&&(i.push(A),a=!0),s=a?null:A,i.length>0&&(c(),{promise:o,resolve:c}=Z())},A=>{p||(p=A,c())}),w=this._createPacketSink(),T=await w.getKeyPacket(t,b)??await w.getFirstKeyPacket(b),S=T,P=w.packets(T??void 0,void 0,b);for(await P.next();S&&!d&&!this._track.input._disposed;){let A=dd(i.length);if(i.length+k.getDecodeQueueSize()>A){({promise:u,resolve:l}=Z()),await u;continue}k.decode(S);let C=await P.next();if(C.done)break;S=C.value}await P.return(),!f&&!this._track.input._disposed&&await k.flush(),k.close(),!a&&s&&i.push(s),m=!0,c()})().catch(k=>{p||(p=k,c())});let g=this._track,y=()=>{s?.close();for(let k of i)k.close()};return{async next(){for(;;){if(g.input._disposed)throw y(),new be;if(f)return{value:void 0,done:!0};if(p)throw y(),p;if(i.length>0){let k=i.shift();return l(),{value:k,done:!1}}else if(!m)await o;else return{value:void 0,done:!0}}},async return(){return f=!0,d=!0,l(),c(),y(),{value:void 0,done:!0}},async throw(k){throw k},[Symbol.asyncIterator](){return this}}}mediaSamplesAtTimestamps(t,e){hu(t);let r=pu(t),i=[],a=[],{promise:s,resolve:o}=Z(),{promise:c,resolve:u}=Z(),l=!1,m=!1,d=null,f=y=>{a.push(y),o(),{promise:s,resolve:o}=Z()},p={...e,verifyKeyPackets:!0,metadataOnly:!1};(async()=>{let y=await this._createDecoder(A=>{if(u(),m){A.close();return}let C=0;for(;i.length>0&&A.timestamp-i[0]>-1e-10;)C++,i.shift();if(C>0)for(let I=0;I<C;I++)f(I<C-1?A.clone():A);else A.close()},A=>{d||(d=A,o())}),k=this._createPacketSink(),w=null,T=null,S=-1,x=async()=>{h(T);let A=T;for(y.decode(A);A.sequenceNumber<S;){let C=dd(a.length);for(;a.length+y.getDecodeQueueSize()>C&&!m;)({promise:c,resolve:u}=Z()),await c;if(m)break;let I=await k.getNextPacket(A,p);h(I),y.decode(I),A=I}S=-1},P=async()=>{await y.flush();for(let A=0;A<i.length;A++)f(null);i.length=0};for await(let A of r){if(Xt(A),m||this._track.input._disposed)break;let C=await k.getPacket(A,p),I=C&&await k.getKeyPacket(A,p);if(!I){S!==-1&&(await x(),await P()),f(null),w=null;continue}w&&(I.sequenceNumber!==T.sequenceNumber||C.timestamp<w.timestamp)&&(await x(),await P()),i.push(C.timestamp),S=Math.max(C.sequenceNumber,S),w=C,T=I}!m&&!this._track.input._disposed&&(S!==-1&&await x(),await P()),y.close(),l=!0,o()})().catch(y=>{d||(d=y,o())});let b=this._track,g=()=>{for(let y of a)y?.close()};return{async next(){for(;;){if(b.input._disposed)throw g(),new be;if(m)return{value:void 0,done:!0};if(d)throw g(),d;if(a.length>0){let y=a.shift();return h(y!==void 0),u(),{value:y,done:!1}}else if(!l)await s;else return{value:void 0,done:!0}}},async return(){return m=!0,u(),o(),g(),{value:void 0,done:!0}},async throw(y){throw y},[Symbol.asyncIterator](){return this}}}},dd=n=>n===0?40:8,bc=class extends vn{constructor(e,r,i,a,s,o){super(e,r);this.codec=i;this.decoderConfig=a;this.rotation=s;this.timeResolution=o;this.decoder=null;this.customDecoder=null;this.customDecoderCallSerializer=new vt;this.customDecoderQueueSize=0;this.inputTimestamps=[];this.sampleQueue=[];this.currentPacketIndex=0;this.raslSkipped=!1;this.alphaDecoder=null;this.alphaHadKeyframe=!1;this.colorQueue=[];this.alphaQueue=[];this.merger=null;this.decodedAlphaChunkCount=0;this.alphaDecoderQueueSize=0;this.nullAlphaFrameQueue=[];this.currentAlphaPacketIndex=0;this.alphaRaslSkipped=!1;this.frameHandlerSerializer=new vt;let c=gr.find(u=>u.supports(i,a));if(c)this.customDecoder=new c,this.customDecoder.codec=i,this.customDecoder.config=a,this.customDecoder.onSample=u=>{if(!(u instanceof Ae))throw new TypeError("The argument passed to onSample must be a VideoSample.");this.finalizeAndEmitSample(u)},this.customDecoderCallSerializer.call(()=>this.customDecoder.init());else{let u=m=>{this.frameHandlerSerializer.call(async()=>{if(this.alphaQueue.length>0){let d=this.alphaQueue.shift();h(d!==void 0),await this.mergeAlpha(m,d)}else this.colorQueue.push(m)}).catch(d=>this.onError(d))};if(i==="avc"&&this.decoderConfig.description&&Xi()){let m=ra(j(this.decoderConfig.description));if(m&&m.sequenceParameterSets.length>0){let d=tn(m.sequenceParameterSets[0]);d&&d.frameMbsOnlyFlag===0&&(this.decoderConfig={...this.decoderConfig,hardwareAcceleration:"prefer-software"})}}let l=new Error("Decoding error").stack;this.decoder=new VideoDecoder({output:m=>{try{u(m)}catch(d){this.onError(d)}},error:m=>{m.stack=l,this.onError(m)}}),this.decoder.configure(this.decoderConfig)}}getDecodeQueueSize(){return this.customDecoder?this.customDecoderQueueSize:(h(this.decoder),Math.max(this.decoder.decodeQueueSize,this.alphaDecoder?.decodeQueueSize??0))}decode(e){if(this.codec==="hevc"&&this.currentPacketIndex>0&&!this.raslSkipped){if(this.hasHevcRaslPicture(e.data))return;this.raslSkipped=!0}if(this.customDecoder)this.customDecoderQueueSize++,this.customDecoderCallSerializer.call(()=>this.customDecoder.decode(e)).then(()=>this.customDecoderQueueSize--);else{if(h(this.decoder),ir()||uo(this.inputTimestamps,e.timestamp,r=>r),Xi()&&this.currentPacketIndex===0){if(this.codec==="avc"){let r=[];for(let a of yo(e.data,this.decoderConfig)){let s=zt(e.data[a.offset]);s>=20&&s<=31||r.push(e.data.subarray(a.offset,a.offset+a.length))}let i=Ku(r,this.decoderConfig);e=new Q(i,e.type,e.timestamp,e.duration)}else if(this.codec==="hevc"){let r=Yu(e.data,this.decoderConfig);r&&(e=new Q(r,e.type,e.timestamp,e.duration))}}this.decoder.decode(e.toEncodedVideoChunk()),this.decodeAlphaData(e)}this.currentPacketIndex++}decodeAlphaData(e){if(!e.sideData.alpha){this.pushNullAlphaFrame();return}if(this.merger||(this.merger=new yc),!this.alphaDecoder){let i=s=>{this.frameHandlerSerializer.call(async()=>{if(this.colorQueue.length>0){let o=this.colorQueue.shift();h(o!==void 0),await this.mergeAlpha(o,s)}else this.alphaQueue.push(s);for(this.decodedAlphaChunkCount++;this.nullAlphaFrameQueue.length>0&&this.nullAlphaFrameQueue[0]===this.decodedAlphaChunkCount;)if(this.nullAlphaFrameQueue.shift(),this.colorQueue.length>0){let o=this.colorQueue.shift();h(o!==void 0),await this.mergeAlpha(o,null)}else this.alphaQueue.push(null);this.alphaDecoderQueueSize--}).catch(o=>this.onError(o))},a=new Error("Decoding error").stack;this.alphaDecoder=new VideoDecoder({output:s=>{try{i(s)}catch(o){this.onError(o)}},error:s=>{s.stack=a,this.onError(s)}}),this.alphaDecoder.configure(this.decoderConfig)}let r=dr(this.codec,this.decoderConfig,e.sideData.alpha);if(this.alphaHadKeyframe||(this.alphaHadKeyframe=r==="key"),this.alphaHadKeyframe){if(this.codec==="hevc"&&this.currentAlphaPacketIndex>0&&!this.alphaRaslSkipped){if(this.hasHevcRaslPicture(e.sideData.alpha)){this.pushNullAlphaFrame();return}this.alphaRaslSkipped=!0}this.currentAlphaPacketIndex++,this.alphaDecoder.decode(e.alphaToEncodedVideoChunk(r??e.type)),this.alphaDecoderQueueSize++}else this.pushNullAlphaFrame()}pushNullAlphaFrame(){this.alphaDecoderQueueSize===0?this.alphaQueue.push(null):this.nullAlphaFrameQueue.push(this.decodedAlphaChunkCount+this.alphaDecoderQueueSize)}hasHevcRaslPicture(e){for(let r of Ji(e,this.decoderConfig)){let i=at(e[r.offset]);if(i===8||i===9)return!0}return!1}sampleHandler(e){if(ir()){if(this.sampleQueue.length>0&&e.timestamp>=X(this.sampleQueue).timestamp){for(let r of this.sampleQueue)this.finalizeAndEmitSample(r);this.sampleQueue.length=0}uo(this.sampleQueue,e,r=>r.timestamp)}else{let r=this.inputTimestamps.shift();h(r!==void 0),e.setTimestamp(r),this.finalizeAndEmitSample(e)}}finalizeAndEmitSample(e){e.setTimestamp(Math.round(e.timestamp*this.timeResolution)/this.timeResolution),e.setDuration(Math.round(e.duration*this.timeResolution)/this.timeResolution),e.setRotation(this.rotation),this.onSample(e)}async mergeAlpha(e,r){if(!r){let s=new Ae(e);this.sampleHandler(s);return}h(this.merger);let i=await this.merger.update(e,r),a=new Ae(i);this.sampleHandler(a)}async flush(){if(this.customDecoder?await this.customDecoderCallSerializer.call(()=>this.customDecoder.flush()):(h(this.decoder),await Promise.all([this.decoder.flush(),this.alphaDecoder?.flush()]),await this.frameHandlerSerializer.currentPromise,this.colorQueue.forEach(e=>e.close()),this.colorQueue.length=0,this.alphaQueue.forEach(e=>e?.close()),this.alphaQueue.length=0,this.alphaHadKeyframe=!1,this.decodedAlphaChunkCount=0,this.alphaDecoderQueueSize=0,this.nullAlphaFrameQueue.length=0,this.currentAlphaPacketIndex=0,this.alphaRaslSkipped=!1),ir()){for(let e of this.sampleQueue)this.finalizeAndEmitSample(e);this.sampleQueue.length=0}this.currentPacketIndex=0,this.raslSkipped=!1}close(){this.customDecoder?this.customDecoderCallSerializer.call(()=>this.customDecoder.close()):(h(this.decoder),this.decoder.close(),this.alphaDecoder?.close(),this.colorQueue.forEach(e=>e.close()),this.colorQueue.length=0,this.alphaQueue.forEach(e=>e?.close()),this.alphaQueue.length=0,this.merger?.close());for(let e of this.sampleQueue)e.close();this.sampleQueue.length=0}},md=!1,vs=class vs{constructor(){this.canvas=null;this.gl=null;this.program=null;this.vao=null;this.colorTexture=null;this.alphaTexture=null;this.worker=null;this.pendingRequests=new Map;this.nextRequestId=0;let t=typeof OffscreenCanvas<"u"||typeof document<"u"&&typeof document.createElement=="function";if(!vs.forceCpu&&t&&!md)try{typeof OffscreenCanvas<"u"?this.canvas=new OffscreenCanvas(300,150):this.canvas=document.createElement("canvas");let e=this.canvas.getContext("webgl2",{premultipliedAlpha:!1});if(!e)throw new Error("Couldn't acquire WebGL 2 context.");this.gl=e,this.program=this.createProgram(),this.vao=this.createVAO(),this.colorTexture=this.createTexture(),this.alphaTexture=this.createTexture(),this.gl.useProgram(this.program),this.gl.uniform1i(this.gl.getUniformLocation(this.program,"u_colorTexture"),0),this.gl.uniform1i(this.gl.getUniformLocation(this.program,"u_alphaTexture"),1)}catch(e){this.gl=null,this.canvas=null,md=!0,console.warn("Falling back to CPU for color/alpha merging.",e)}}async update(t,e){return this.gl?this.updateGpu(t,e):this.updateCpu(t,e)}createProgram(){h(this.gl);let t=this.createShader(this.gl.VERTEX_SHADER,`#version 300 es
9
9
  in vec2 a_position;
10
10
  in vec2 a_texCoord;
11
11
  out vec2 v_texCoord;
@@ -27,16 +27,16 @@ var Xd=Object.create;var ou=Object.defineProperty;var $d=Object.getOwnPropertyDe
27
27
  float alpha = texture(u_alphaTexture, v_texCoord).r;
28
28
  fragColor = vec4(color, alpha);
29
29
  }
30
- `),r=this.gl.createProgram();return this.gl.attachShader(r,t),this.gl.attachShader(r,e),this.gl.linkProgram(r),r}createShader(t,e){h(this.gl);let r=this.gl.createShader(t);return this.gl.shaderSource(r,e),this.gl.compileShader(r),r}createVAO(){h(this.gl),h(this.program);let t=this.gl.createVertexArray();this.gl.bindVertexArray(t);let e=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),r=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,r),this.gl.bufferData(this.gl.ARRAY_BUFFER,e,this.gl.STATIC_DRAW);let i=this.gl.getAttribLocation(this.program,"a_position"),a=this.gl.getAttribLocation(this.program,"a_texCoord");return this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,16,0),this.gl.enableVertexAttribArray(a),this.gl.vertexAttribPointer(a,2,this.gl.FLOAT,!1,16,8),t}createTexture(){h(this.gl);let t=this.gl.createTexture();return this.gl.bindTexture(this.gl.TEXTURE_2D,t),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),t}updateGpu(t,e){h(this.gl),h(this.canvas),(t.displayWidth!==this.canvas.width||t.displayHeight!==this.canvas.height)&&(this.canvas.width=t.displayWidth,this.canvas.height=t.displayHeight),this.gl.activeTexture(this.gl.TEXTURE0),this.gl.bindTexture(this.gl.TEXTURE_2D,this.colorTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,t),this.gl.activeTexture(this.gl.TEXTURE1),this.gl.bindTexture(this.gl.TEXTURE_2D,this.alphaTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,e),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4);let r=new VideoFrame(this.canvas,{timestamp:t.timestamp,duration:t.duration??void 0});return t.close(),e.close(),r}updateCpu(t,e){if(!this.worker){let a=new Blob([`(${cf.toString()})()`],{type:"application/javascript"}),s=URL.createObjectURL(a);this.worker=new Worker(s),URL.revokeObjectURL(s),this.worker.addEventListener("message",o=>{let c=o.data,u=this.pendingRequests.get(c.id);u&&(this.pendingRequests.delete(c.id),"error"in c?u.reject(new Error(c.error)):u.resolve(c.frame))}),this.worker.addEventListener("error",o=>{let c=new Error(o.message||"Color/alpha merge worker error.");for(let u of this.pendingRequests.values())u.reject(c);this.pendingRequests.clear()})}let r=this.nextRequestId++,i=Z();return this.pendingRequests.set(r,i),this.worker.postMessage({id:r,color:t,alpha:e},{transfer:[t,e]}),i.promise}close(){this.gl?.getExtension("WEBGL_lose_context")?.loseContext(),this.gl=null,this.canvas=null,this.worker?.terminate(),this.worker=null;let t=new Error("Color/alpha merger closed.");for(let e of this.pendingRequests.values())e.reject(t);this.pendingRequests.clear()}};vs.forceCpu=!0;var yc=vs,cf=()=>{let n=null,t=null,e=Promise.resolve();self.addEventListener("message",c=>{let{id:u,color:l,alpha:m}=c.data;e=e.then(async()=>{try{let d=await r(l,m);self.postMessage({id:u,frame:d},{transfer:[d]})}catch(d){self.postMessage({id:u,error:d.message})}finally{l.close(),m.close()}})});let r=async(c,u)=>{let l=c.format,m=u.format;if(!l||!m)throw new Error("CPU color/alpha merging requires a known VideoFrame format.");let d=l.includes("P10"),f=l.includes("P12"),p=m.includes("P10"),b=m.includes("P12");if(p!==d||b!==f)throw new Error(`CPU color/alpha merging requires the alpha frame to have the same bit depth as the color frame (color: '${l}', alpha: '${m}').`);let g=c.codedWidth,y=c.codedHeight;if(l==="RGBX"||l==="RGBA"||l==="BGRX"||l==="BGRA")return await i(c,u,g,y,l);if(l==="I420"||l==="I420P10"||l==="I420P12"||l==="I422"||l==="I422P10"||l==="I422P12"||l==="I444"||l==="I444P10"||l==="I444P12")return await a(c,u,g,y,l);if(l==="NV12")return await s(c,u,g,y);throw new Error(`CPU color/alpha merging does not support format '${l}'.`)},i=async(c,u,l,m,d)=>{let f=l*m,p=new Uint8Array(f*4);await c.copyTo(p);let b=await o(u,l,m,1);for(let k=0,T=3;k<f;k++,T+=4)p[T]=b[k];let y={format:d==="RGBX"||d==="RGBA"?"RGBA":"BGRA",codedWidth:l,codedHeight:m,timestamp:c.timestamp,duration:c.duration??void 0,transfer:[p.buffer]};return new VideoFrame(p,y)},a=async(c,u,l,m,d)=>{let f=d.includes("P10"),p=d.includes("P12"),b=f||p?2:1,g,y;d.startsWith("I420")?(g=Math.ceil(l/2),y=Math.ceil(m/2)):d.startsWith("I422")?(g=Math.ceil(l/2),y=m):(g=l,y=m);let k=l*m,T=g*y,w=k*b,S=T*b,x=k*b,P=w+2*S+x,A=new Uint8Array(P);await c.copyTo(A);let C=await o(u,l,m,b),I=w+2*S;A.set(C,I);let v={format:d.slice(0,4)+"A"+d.slice(4),codedWidth:l,codedHeight:m,timestamp:c.timestamp,duration:c.duration??void 0,transfer:[A.buffer]};return new VideoFrame(A,v)},s=async(c,u,l,m)=>{let d=l*m,f=Math.ceil(l/2),p=Math.ceil(m/2),b=f*p,g=c.allocationSize();(!t||t.byteLength!==g)&&(t=new Uint8Array(g)),await c.copyTo(t);let y=new Uint8Array(d+2*b+d);y.set(t.subarray(0,d),0);let k=d,T=d+b,w=d;for(let P=0;P<b;P++)y[k+P]=t[w+P*2],y[T+P]=t[w+P*2+1];let S=await o(u,l,m,1);y.set(S,d+2*b);let x={format:"I420A",codedWidth:l,codedHeight:m,timestamp:c.timestamp,duration:c.duration??void 0,transfer:[y.buffer]};return new VideoFrame(y,x)},o=async(c,u,l,m)=>{let d=c.allocationSize();(!n||n.byteLength!==d)&&(n=new Uint8Array(d)),await c.copyTo(n);let f=c.format;if(f==="RGBA"||f==="BGRA"||f==="RGBX"||f==="BGRX"){let p=f==="RGBA"||f==="RGBX"?0:2,b=u*l;for(let g=0;g<b;g++)n[g]=n[g*4+p];return n.subarray(0,b)}else return n.subarray(0,u*l*m)}},Hr=class extends _n{constructor(t){if(!(t instanceof $t))throw new TypeError("videoTrack must be an InputVideoTrack.");super(),this._track=t}async _createDecoder(t,e){if(!await this._track.canDecode())throw new Error("This video track cannot be decoded by this browser. Make sure to check decodability before using a track.");let r=await this._track.getCodec(),i=await this._track.getRotation(),a=await this._track.getDecoderConfig(),s=await this._track.getTimeResolution();return h(r&&a),new bc(t,e,r,a,i,s)}_createPacketSink(){return new xt(this._track)}async getSample(t,e={}){Xt(t);for await(let r of this.mediaSamplesAtTimestamps([t],e))return r;throw new Error("Internal error: Iterator returned nothing.")}samples(t,e,r={}){return this.mediaSamplesInRange(t,e,r)}samplesAtTimestamps(t,e={}){return this.mediaSamplesAtTimestamps(t,e)}},kc=class{constructor(t,e={}){this._rotation=0;this._initPromise=null;this._nextCanvasIndex=0;if(!(t instanceof $t))throw new TypeError("videoTrack must be an InputVideoTrack.");if(e&&typeof e!="object")throw new TypeError("options must be an object.");if(e.alpha!==void 0&&typeof e.alpha!="boolean")throw new TypeError("options.alpha, when provided, must be a boolean.");if(e.width!==void 0&&(!Number.isInteger(e.width)||e.width<=0))throw new TypeError("options.width, when defined, must be a positive integer.");if(e.height!==void 0&&(!Number.isInteger(e.height)||e.height<=0))throw new TypeError("options.height, when defined, must be a positive integer.");if(e.fit!==void 0&&!["fill","contain","cover"].includes(e.fit))throw new TypeError('options.fit, when provided, must be one of "fill", "contain", or "cover".');if(e.width!==void 0&&e.height!==void 0&&e.fit===void 0)throw new TypeError("When both options.width and options.height are provided, options.fit must also be provided.");if(e.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("options.rotation, when provided, must be 0, 90, 180 or 270.");if(e.crop!==void 0&&yr(e.crop,"options."),e.poolSize!==void 0&&(typeof e.poolSize!="number"||!Number.isInteger(e.poolSize)||e.poolSize<0))throw new TypeError("poolSize must be a non-negative integer.");this._videoTrack=t,this._alpha=e.alpha??!1,this._options=e,this._fit=e.fit??"fill",this._videoSampleSink=new Hr(t),this._canvasPool=Array.from({length:e.poolSize??0},()=>null)}_ensureInit(){return this._initPromise??=(async()=>{let t=this._options,e=this._videoTrack,r=t.rotation??await e.getRotation(),i=await e.getSquarePixelWidth(),a=await e.getSquarePixelHeight(),[s,o]=r%180===0?[i,a]:[a,i],c=t.crop;c&&(c=Pi(c,s,o));let[u,l]=c?[c.width,c.height]:[s,o],m=u/l;t.width!==void 0&&t.height===void 0?(u=t.width,l=Math.round(u/m)):t.width===void 0&&t.height!==void 0?(l=t.height,u=Math.round(l*m)):t.width!==void 0&&t.height!==void 0&&(u=t.width,l=t.height),this._width=u,this._height=l,this._rotation=r,this._crop=c})()}_videoSampleToWrappedCanvas(t){let e=this._width,r=this._height,i=this._canvasPool[this._nextCanvasIndex],a=!1;i||(typeof document<"u"?(i=document.createElement("canvas"),i.width=e,i.height=r):i=new OffscreenCanvas(e,r),this._canvasPool.length>0&&(this._canvasPool[this._nextCanvasIndex]=i),a=!0),this._canvasPool.length>0&&(this._nextCanvasIndex=(this._nextCanvasIndex+1)%this._canvasPool.length);let s=i.getContext("2d",{alpha:this._alpha||Er()});h(s),s.resetTransform(),a||(!this._alpha&&Er()?(s.fillStyle="black",s.fillRect(0,0,e,r)):s.clearRect(0,0,e,r)),t.drawWithFit(s,{fit:this._fit,rotation:this._rotation,crop:this._crop});let o={canvas:i,timestamp:t.timestamp,duration:t.duration};return t.close(),o}async getCanvas(t,e){Xt(t),await this._ensureInit();let r=await this._videoSampleSink.getSample(t,e);return r&&this._videoSampleToWrappedCanvas(r)}async*canvases(t,e,r){await this._ensureInit(),yield*Qi(this._videoSampleSink.samples(t,e,r),i=>this._videoSampleToWrappedCanvas(i))}async*canvasesAtTimestamps(t,e){await this._ensureInit(),yield*Qi(this._videoSampleSink.samplesAtTimestamps(t,e),r=>r&&this._videoSampleToWrappedCanvas(r))}},Tc=class extends vn{constructor(e,r,i,a){super(e,r);this.decoder=null;this.customDecoder=null;this.customDecoderCallSerializer=new vt;this.customDecoderQueueSize=0;this.currentTimestamp=null;this.expectedFirstTimestamp=null;this.timestampOffset=0;let s=c=>{let u=c.timestamp;this.expectedFirstTimestamp&&this.currentTimestamp===null&&(this.timestampOffset=this.expectedFirstTimestamp-u),u+=this.timestampOffset,(this.currentTimestamp===null||Math.abs(u-this.currentTimestamp)>=c.duration)&&(this.currentTimestamp=u);let l=this.currentTimestamp;if(this.currentTimestamp+=c.duration,c.numberOfFrames===0){c.close();return}let m=a.sampleRate;c.setTimestamp(Math.round(l*m)/m),e(c)},o=gr.find(c=>c.supports(i,a));if(o)this.customDecoder=new o,this.customDecoder.codec=i,this.customDecoder.config=a,this.customDecoder.onSample=c=>{if(!(c instanceof ye))throw new TypeError("The argument passed to onSample must be an AudioSample.");s(c)},this.customDecoderCallSerializer.call(()=>this.customDecoder.init());else{let c=new Error("Decoding error").stack;this.decoder=new AudioDecoder({output:u=>{try{s(new ye(u))}catch(l){this.onError(l)}},error:u=>{u.stack=c,this.onError(u)}}),this.decoder.configure(a)}}getDecodeQueueSize(){return this.customDecoder?this.customDecoderQueueSize:(h(this.decoder),this.decoder.decodeQueueSize)}decode(e){this.customDecoder?(this.customDecoderQueueSize++,this.customDecoderCallSerializer.call(()=>this.customDecoder.decode(e)).then(()=>this.customDecoderQueueSize--)):(h(this.decoder),this.expectedFirstTimestamp??=e.timestamp,this.decoder.decode(e.toEncodedAudioChunk()))}async flush(){this.customDecoder?await this.customDecoderCallSerializer.call(()=>this.customDecoder.flush()):(h(this.decoder),await this.decoder.flush()),this.currentTimestamp=null,this.expectedFirstTimestamp=null,this.timestampOffset=0}close(){this.customDecoder?this.customDecoderCallSerializer.call(()=>this.customDecoder.close()):(h(this.decoder),this.decoder.close())}},wc=class extends vn{constructor(e,r,i){super(e,r);this.decoderConfig=i;this.currentTimestamp=null;h(ee.includes(i.codec)),this.codec=i.codec;let{dataType:a,sampleSize:s,littleEndian:o}=Se(this.codec);switch(this.inputSampleSize=s,s){case 1:a==="unsigned"?this.readInputValue=(c,u)=>c.getUint8(u)-2**7:a==="signed"?this.readInputValue=(c,u)=>c.getInt8(u):a==="ulaw"?this.readInputValue=(c,u)=>cd(c.getUint8(u)):a==="alaw"?this.readInputValue=(c,u)=>ld(c.getUint8(u)):h(!1);break;case 2:a==="unsigned"?this.readInputValue=(c,u)=>c.getUint16(u,o)-2**15:a==="signed"?this.readInputValue=(c,u)=>c.getInt16(u,o):h(!1);break;case 3:a==="unsigned"?this.readInputValue=(c,u)=>Cr(c,u,o)-2**23:a==="signed"?this.readInputValue=(c,u)=>gu(c,u,o):h(!1);break;case 4:a==="unsigned"?this.readInputValue=(c,u)=>c.getUint32(u,o)-2**31:a==="signed"?this.readInputValue=(c,u)=>c.getInt32(u,o):a==="float"?this.readInputValue=(c,u)=>c.getFloat32(u,o):h(!1);break;case 8:a==="float"?this.readInputValue=(c,u)=>c.getFloat64(u,o):h(!1);break;default:ne(s),h(!1)}switch(s){case 1:a==="ulaw"||a==="alaw"?(this.outputSampleSize=2,this.outputFormat="s16",this.writeOutputValue=(c,u,l)=>c.setInt16(u,l,!0)):(this.outputSampleSize=1,this.outputFormat="u8",this.writeOutputValue=(c,u,l)=>c.setUint8(u,l+2**7));break;case 2:this.outputSampleSize=2,this.outputFormat="s16",this.writeOutputValue=(c,u,l)=>c.setInt16(u,l,!0);break;case 3:this.outputSampleSize=4,this.outputFormat="s32",this.writeOutputValue=(c,u,l)=>c.setInt32(u,l<<8,!0);break;case 4:this.outputSampleSize=4,a==="float"?(this.outputFormat="f32",this.writeOutputValue=(c,u,l)=>c.setFloat32(u,l,!0)):(this.outputFormat="s32",this.writeOutputValue=(c,u,l)=>c.setInt32(u,l,!0));break;case 8:this.outputSampleSize=4,this.outputFormat="f32",this.writeOutputValue=(c,u,l)=>c.setFloat32(u,l,!0);break;default:ne(s),h(!1)}}getDecodeQueueSize(){return 0}decode(e){let r=V(e.data),i=e.byteLength/this.decoderConfig.numberOfChannels/this.inputSampleSize,a=i*this.decoderConfig.numberOfChannels*this.outputSampleSize,s=new ArrayBuffer(a),o=new DataView(s);for(let m=0;m<i*this.decoderConfig.numberOfChannels;m++){let d=m*this.inputSampleSize,f=m*this.outputSampleSize,p=this.readInputValue(r,d);this.writeOutputValue(o,f,p)}let c=i/this.decoderConfig.sampleRate;(this.currentTimestamp===null||Math.abs(e.timestamp-this.currentTimestamp)>=c)&&(this.currentTimestamp=e.timestamp);let u=this.currentTimestamp;this.currentTimestamp+=c;let l=new ye({format:this.outputFormat,data:s,numberOfChannels:this.decoderConfig.numberOfChannels,sampleRate:this.decoderConfig.sampleRate,numberOfFrames:i,timestamp:u});this.onSample(l)}async flush(){}close(){}},qr=class extends _n{constructor(t){if(!(t instanceof Yt))throw new TypeError("audioTrack must be an InputAudioTrack.");super(),this._track=t}async _createDecoder(t,e){if(!await this._track.canDecode())throw new Error("This audio track cannot be decoded by this browser. Make sure to check decodability before using a track.");let r=await this._track.getCodec(),i=await this._track.getDecoderConfig();return h(r&&i),ee.includes(i.codec)?new wc(t,e,i):new Tc(t,e,r,i)}_createPacketSink(){return new xt(this._track)}async getSample(t,e={}){Xt(t);for await(let r of this.mediaSamplesAtTimestamps([t],e))return r;throw new Error("Internal error: Iterator returned nothing.")}samples(t,e,r={}){return this.mediaSamplesInRange(t,e,r)}samplesAtTimestamps(t,e={}){return this.mediaSamplesAtTimestamps(t,e)}},Sc=class{constructor(t){if(!(t instanceof Yt))throw new TypeError("audioTrack must be an InputAudioTrack.");this._audioSampleSink=new qr(t)}_audioSampleToWrappedArrayBuffer(t){let e={buffer:t.toAudioBuffer(),timestamp:t.timestamp,duration:t.duration};return t.close(),e}async getBuffer(t,e){Xt(t);let r=await this._audioSampleSink.getSample(t,e);return r&&this._audioSampleToWrappedArrayBuffer(r)}buffers(t,e,r){return Qi(this._audioSampleSink.samples(t,e,r),i=>this._audioSampleToWrappedArrayBuffer(i))}buffersAtTimestamps(t,e){return Qi(this._audioSampleSink.samplesAtTimestamps(t,e),r=>r&&this._audioSampleToWrappedArrayBuffer(r))}};var Gr=class n{constructor(t,e){this.input=t,this._backing=e}isVideoTrack(){return this instanceof $t}isAudioTrack(){return this instanceof Yt}get id(){return this._backing.getId()}get number(){return this._backing.getNumber()}async getInternalCodecId(){return this._backing.getInternalCodecId()}get internalCodecId(){return ce(this._backing.getInternalCodecId(),"internalCodecId","getInternalCodecId")}async getLanguageCode(){return this._backing.getLanguageCode()}get languageCode(){return ce(this._backing.getLanguageCode(),"languageCode","getLanguageCode")}async getName(){return this._backing.getName()}get name(){return ce(this._backing.getName(),"name","getName")}async getTimeResolution(){return this._backing.getTimeResolution()}get timeResolution(){return ce(this._backing.getTimeResolution(),"timeResolution","getTimeResolution")}async isRelativeToUnixEpoch(){return this._backing.isRelativeToUnixEpoch()}async getDisposition(){return this._backing.getDisposition()}get disposition(){return ce(this._backing.getDisposition(),"disposition","getDisposition")}async getBitrate(){return this._backing.getBitrate()}async getAverageBitrate(){return this._backing.getAverageBitrate()}async getFirstTimestamp(){return(await this._backing.getFirstPacket({metadataOnly:!0}))?.timestamp??0}async computeDuration(t){let e=await this._backing.getPacket(1/0,{metadataOnly:!0,...t}),r=(e?.timestamp??0)+(e?.duration??0);return bt(r,await this.getTimeResolution())}async getDurationFromMetadata(t={}){return this._backing.getDurationFromMetadata(t)}async computePacketStats(t=1/0,e){let r=new xt(this),i=1/0,a=-1/0,s=0,o=0;for await(let c of r.packets(void 0,void 0,{metadataOnly:!0,...e})){if(s>=t&&c.timestamp>=a)break;i=Math.min(i,c.timestamp),a=Math.max(a,c.timestamp+c.duration),s++,o+=c.byteLength}return{packetCount:s,averagePacketRate:s?Number((s/(a-i)).toPrecision(16)):0,averageBitrate:s?Number((8*o/(a-i)).toPrecision(16)):0}}async isLive(){return await this._backing.getLiveRefreshInterval()!==null}async getLiveRefreshInterval(){return this._backing.getLiveRefreshInterval()}canBePairedWith(t){if(!(t instanceof n))throw new TypeError("other must be an InputTrack.");return this.input!==t.input||this===t?!1:(this._backing.getPairingMask()&t._backing.getPairingMask())!==0n}async getPairableTracks(t){return this.input.getTracks(kr({filter:e=>e.canBePairedWith(this)},t))}async getPairableVideoTracks(t){return this.input.getVideoTracks(kr({filter:e=>e.canBePairedWith(this)},t))}async getPairableAudioTracks(t){return this.input.getAudioTracks(kr({filter:e=>e.canBePairedWith(this)},t))}async getPrimaryPairableVideoTrack(t){return this.input.getPrimaryVideoTrack(kr({filter:e=>e.canBePairedWith(this)},t))}async getPrimaryPairableAudioTrack(t){return this.input.getPrimaryAudioTrack(kr({filter:e=>e.canBePairedWith(this)},t))}async hasPairableTrack(t){t&&=Ac(t);let e=await this.input.getTracks();for(let r of e)if(this.canBePairedWith(r)&&(!t||await t(r)))return!0;return!1}hasPairableVideoTrack(t){return t&&=Ac(t),this.hasPairableTrack(async e=>e.isVideoTrack()&&(!t||await t(e)))}hasPairableAudioTrack(t){return t&&=Ac(t),this.hasPairableTrack(async e=>e.isAudioTrack()&&(!t||await t(e)))}},ce=(n,t,e)=>{if(n instanceof Promise)throw new Error(`'${t}' is deprecated and not available synchronously for this track. Use the preferred '${e}()' instead.`);return n},Ac=n=>{if(n!==void 0&&typeof n!="function")throw new TypeError("predicate, when provided, must be a function.");return n?t=>{let e=i=>{if(typeof i!="boolean")throw new TypeError("predicate must return or resolve to a boolean value.");return i},r=n(t);return r instanceof Promise?r.then(e):e(r)}:void 0},$t=class extends Gr{constructor(e,r){super(e,r);this._pixelAspectRatioCache=null;this._backing=r}get type(){return"video"}async getCodec(){return this._backing.getCodec()}get codec(){return ce(this._backing.getCodec(),"codec","getCodec")}async hasOnlyKeyPackets(){return await this._backing.getHasOnlyKeyPackets?.()??!1}async getCodedWidth(){return this._backing.getCodedWidth()}get codedWidth(){return ce(this._backing.getCodedWidth(),"codedWidth","getCodedWidth")}async getCodedHeight(){return this._backing.getCodedHeight()}get codedHeight(){return ce(this._backing.getCodedHeight(),"codedHeight","getCodedHeight")}async getRotation(){return this._backing.getRotation()}get rotation(){return ce(this._backing.getRotation(),"rotation","getRotation")}async getSquarePixelWidth(){return this._backing.getSquarePixelWidth()}get squarePixelWidth(){return ce(this._backing.getSquarePixelWidth(),"squarePixelWidth","getSquarePixelWidth")}async getSquarePixelHeight(){return this._backing.getSquarePixelHeight()}get squarePixelHeight(){return ce(this._backing.getSquarePixelHeight(),"squarePixelHeight","getSquarePixelHeight")}async getPixelAspectRatio(){return this._pixelAspectRatioCache??=Ot({num:await this.getSquarePixelWidth()*await this.getCodedHeight(),den:await this.getSquarePixelHeight()*await this.getCodedWidth()})}get pixelAspectRatio(){return this._pixelAspectRatioCache??=Ot({num:ce(this._backing.getSquarePixelWidth(),"pixelAspectRatio","getPixelAspectRatio")*ce(this._backing.getCodedHeight(),"pixelAspectRatio","getPixelAspectRatio"),den:ce(this._backing.getSquarePixelHeight(),"pixelAspectRatio","getPixelAspectRatio")*ce(this._backing.getCodedWidth(),"pixelAspectRatio","getPixelAspectRatio")})}async getDisplayWidth(){let e=await this._backing.getMetadataDisplayWidth?.();return e??(await this.getRotation()%180===0?this.getSquarePixelWidth():this.getSquarePixelHeight())}get displayWidth(){let e=this._backing.getMetadataDisplayWidth?.();if(e!==void 0){let a=ce(e,"displayWidth","getDisplayWidth");if(a!==null)return a}let i=ce(this._backing.getRotation(),"displayWidth","getDisplayWidth")%180===0?this._backing.getSquarePixelWidth():this._backing.getSquarePixelHeight();return ce(i,"displayWidth","getDisplayWidth")}async getDisplayHeight(){let e=await this._backing.getMetadataDisplayHeight?.();return e??(await this.getRotation()%180===0?this.getSquarePixelHeight():this.getSquarePixelWidth())}get displayHeight(){let e=this._backing.getMetadataDisplayHeight?.();if(e!==void 0){let a=ce(e,"displayHeight","getDisplayHeight");if(a!==null)return a}let i=ce(this._backing.getRotation(),"displayHeight","getDisplayHeight")%180===0?this._backing.getSquarePixelHeight():this._backing.getSquarePixelWidth();return ce(i,"displayHeight","getDisplayHeight")}async getColorSpace(){return this._backing.getColorSpace()}async hasHighDynamicRange(){let e=await this._backing.getColorSpace();return e.primaries==="bt2020"||e.primaries==="smpte432"||e.transfer==="pg"||e.transfer==="hlg"||e.matrix==="bt2020-ncl"}async canBeTransparent(){return this._backing.canBeTransparent()}async getDecoderConfig(){return this._backing.getDecoderConfig()}async getCodecParameterString(){let e=await this._backing.getMetadataCodecParameterString?.();return e??(await this._backing.getDecoderConfig())?.codec??null}async canDecode(){try{let e=await this._backing.getDecoderConfig();if(!e)return!1;let r=await this._backing.getCodec();return h(r!==null),hr.some(a=>a.supports(r,e))?!0:typeof VideoDecoder>"u"?!1:(await VideoDecoder.isConfigSupported(e)).supported===!0}catch(e){return console.error("Error during decodability check:",e),!1}}async determinePacketType(e){if(!(e instanceof Q))throw new TypeError("packet must be an EncodedPacket.");if(e.isMetadataOnly)throw new TypeError("packet must not be metadata-only to determine its type.");let r=await this.getCodec();if(r===null)return null;let i=await this.getDecoderConfig();return h(i),oi(r,i,e.data)}},Yt=class extends Gr{constructor(t,e){super(t,e),this._backing=e}get type(){return"audio"}async getCodec(){return this._backing.getCodec()}get codec(){return ce(this._backing.getCodec(),"codec","getCodec")}async hasOnlyKeyPackets(){return await this._backing.getHasOnlyKeyPackets?.()??!0}async getNumberOfChannels(){return this._backing.getNumberOfChannels()}get numberOfChannels(){return ce(this._backing.getNumberOfChannels(),"numberOfChannels","getNumberOfChannels")}async getSampleRate(){return this._backing.getSampleRate()}get sampleRate(){return ce(this._backing.getSampleRate(),"sampleRate","getSampleRate")}async getDecoderConfig(){return this._backing.getDecoderConfig()}async getCodecParameterString(){let t=await this._backing.getMetadataCodecParameterString?.();return t??(await this._backing.getDecoderConfig())?.codec??null}async canDecode(){try{let t=await this._backing.getDecoderConfig();if(!t)return!1;let e=await this._backing.getCodec();return h(e!==null),gr.some(r=>r.supports(e,t))||t.codec.startsWith("pcm-")?!0:typeof AudioDecoder>"u"?!1:(await AudioDecoder.isConfigSupported(t)).supported===!0}catch(t){return console.error("Error during decodability check:",t),!1}}async determinePacketType(t){if(!(t instanceof Q))throw new TypeError("packet must be an EncodedPacket.");return await this.getCodec()===null?null:"key"}},uf=n=>n??1/0,_s=n=>-(n??-1/0),Kr=n=>-n,Ei=n=>{if(typeof n!="object"||!n)throw new TypeError("query must be an object.");if(n.filter!==void 0&&typeof n.filter!="function")throw new TypeError("query.filter, when provided, must be a function.");if(n.sortBy!==void 0&&typeof n.sortBy!="function")throw new TypeError("query.sortBy, when provided, must be a function.");return{filter:n.filter?t=>{let e=i=>{if(typeof i!="boolean")throw new TypeError("query.filter must return or resolve to a boolean.");return i},r=n.filter(t);return r instanceof Promise?r.then(e):e(r)}:void 0,sortBy:n.sortBy?t=>{let e=i=>{if(typeof i!="number"&&(!Array.isArray(i)||!i.every(a=>typeof a=="number")))throw new TypeError("query.sortBy must return or resolve to a number or an array of numbers.");return i},r=n.sortBy(t);return r instanceof Promise?r.then(e):e(r)}:void 0}},kr=(n,t)=>({filter:n?.filter||t?.filter?e=>{let r=n?.filter?.(e)??!0,i=a=>a===!1?!1:t?.filter?.(e)??!0;return r instanceof Promise?r.then(i):i(r)}:void 0,sortBy:n?.sortBy||t?.sortBy?e=>{let r=n?.sortBy?.(e)??[],i=t?.sortBy?.(e)??[],a=(s,o)=>[...Array.isArray(s)?s:[s],...Array.isArray(o)?o:[o]];return r instanceof Promise||i instanceof Promise?Promise.all([r,i]).then(([s,o])=>a(s,o)):a(r,i)}:void 0}),Rs=async(n,t)=>{let e=n;if(t?.filter){let s=n.map(c=>t.filter(c));if(s.some(c=>c instanceof Promise)){let c=await Promise.all(s);e=n.filter((u,l)=>c[l])}else e=n.filter((c,u)=>s[u])}if(!t?.sortBy)return e;let r=e.map(s=>t.sortBy(s)),a=r.some(s=>s instanceof Promise)?await Promise.all(r):r;return e.map((s,o)=>({track:s,sortValue:a[o]})).sort((s,o)=>{let c=Array.isArray(s.sortValue)?s.sortValue:[s.sortValue],u=Array.isArray(o.sortValue)?o.sortValue:[o.sortValue],l=Math.max(c.length,u.length);for(let m=0;m<l;m++){let d=c[m]??0,f=u[m]??0;if(d!==f)return d-f}return 0}).map(s=>s.track)};Jr();var lf=1,zl=2,xc=null;typeof FinalizationRegistry<"u"&&(xc=new FinalizationRegistry(n=>{for(let t of n)t.freed||t.free()}));var Nr=class n extends Ye{constructor(e){super();this._demuxerPromise=null;this._format=null;this._trackBackingsCache=null;this._backingToTrack=new Map;this._disposed=!1;this._nextSourceCacheAge=0;this._sourceRefs=[];this._sourceCache=[];this._sourceCachePromises=[];this._onFormatDetermined=null;if(!e||typeof e!="object")throw new TypeError("options must be an object.");if(!Array.isArray(e.formats)||e.formats.some(r=>!(r instanceof Ve)))throw new TypeError("options.formats must be an array of InputFormat.");if(!(e.source instanceof _e||e.source instanceof zr))throw new TypeError("options.source must be a Source or SourceRef.");if(e.source instanceof _e&&e.source._disposed)throw new TypeError("options.source must not be a disposed Source.");if(e.initInput!==void 0&&!(e.initInput instanceof n))throw new TypeError("options.initInput, when provided, must be an Input.");e.formatOptions!==void 0&&Ql(e.formatOptions,"formatOptions"),this._formats=e.formats,this._initInput=e.initInput??null,this._formatOptions=e.formatOptions??{},e.source instanceof _e?this._rootRef=e.source.ref():this._rootRef=e.source,this._sourceRefs.push(this._rootRef),xc?.register(this,this._sourceRefs,this)}get disposed(){return this._disposed}get _rootSource(){return this._rootRef.source}async _getSourceUncached(e){h(this._rootSource instanceof Qe);let r=await this._rootSource._resolveRequest(e);return this._emit("source",{source:r.source,request:e,isRoot:e.isRoot}),r}_getSourceCached(e,r=lf){let i=this._sourceCache.find(o=>o.cacheGroup===r&&rc(o.request,e));if(i)return i.age++,Promise.resolve(i.sourceRef.source.ref());let a=this._sourceCachePromises.find(o=>o.cacheGroup===r&&rc(o.request,e));if(a)return a.promise.then(o=>o.sourceRef.source.ref());let s=(async()=>{let o=await this._getSourceUncached(e);if(Rr(this._sourceCache,d=>d.cacheGroup===r&&d.sourceRef.source._refCount===1)>=4){let d=ei(this._sourceCache,p=>p.cacheGroup===r&&p.sourceRef.source._refCount===1?p.age:1/0);h(d!==-1);let f=this._sourceCache[d];this._sourceCache.splice(d,1),f.sourceRef.free(),lo(this._sourceRefs,f.sourceRef)}this._sourceRefs.push(o);let l=this._sourceCachePromises.findIndex(d=>d.request===e);return h(l!==-1),this._sourceCachePromises.splice(l,1),{request:e,sourceRef:o,age:this._nextSourceCacheAge++,cacheGroup:r}})();return this._sourceCachePromises.push({request:e,cacheGroup:r,promise:s}),s.then(o=>{let c=o.sourceRef.source.ref();return this._sourceCache.push(o),c})}_getDemuxer(){return this._demuxerPromise??=(async()=>{this._reader=new pr(this._rootSource),this._emit("source",{source:this._rootSource,request:null,isRoot:!0});for(let e of this._formats)if(await e._canReadInput(this))return this._format=e,this._onFormatDetermined?.(e),e._createDemuxer(this);throw new Rn})()}get source(){return this._rootSource}async getFormat(){return await this._getDemuxer(),h(this._format),this._format}async canRead(){try{return await this._getDemuxer(),!0}catch(e){if(e instanceof Rn)return!1;throw e}}async getFirstTimestamp(e){e??=await this.getTracks();let r=e.filter(a=>a!==null);if(r.length===0)return 0;let i=await Promise.all(r.map(a=>a.getFirstTimestamp()));return Math.min(...i)}async computeDuration(e,r){e??=await this.getTracks();let i=e.filter(s=>s!==null);if(i.length===0)return 0;let a=await Promise.all(i.map(s=>s.computeDuration(r)));return Math.max(...a)}async getDurationFromMetadata(e,r){e??=await this.getTracks();let i=e.filter(o=>o!==null),s=(await Promise.all(i.map(o=>o.getDurationFromMetadata(r)))).filter(o=>o!==null);return s.length===0?null:Math.max(...s)}async getTracks(e){e&&=Ei(e);let i=(await this._getTrackBackings()).map(a=>this._wrapBackingAsTrack(a));return Rs(i,e)}async getVideoTracks(e){e&&=Ei(e);let i=(await this.getTracks()).filter(a=>a.isVideoTrack());return Rs(i,e)}async getAudioTracks(e){e&&=Ei(e);let i=(await this.getTracks()).filter(a=>a.isAudioTrack());return Rs(i,e)}async getPrimaryVideoTrack(e){e&&=Ei(e);let r=kr(e,{sortBy:async a=>[Kr((await a.getDisposition()).default),Kr(await a.hasPairableAudioTrack()),Kr(!await a.hasOnlyKeyPackets()),_s(await a.getBitrate())]});return(await this.getVideoTracks(r))[0]??null}async getPrimaryAudioTrack(e){e&&=Ei(e);let r=await this.getPrimaryVideoTrack(),i=kr(e,{sortBy:async s=>[Kr(!r||s.canBePairedWith(r)),Kr((await s.getDisposition()).default),_s(await s.getBitrate())]});return(await this.getAudioTracks(i))[0]??null}async _getTrackBackings(){let e=await this._getDemuxer();return this._trackBackingsCache??=await e.getTrackBackings()}_wrapBackingAsTrack(e){let r=this._backingToTrack.get(e);if(r)return r;let a=e.getType()==="video"?new $t(this,e):new Yt(this,e);return this._backingToTrack.set(e,a),a}async getMimeType(){return(await this._getDemuxer()).getMimeType()}async getMetadataTags(){return(await this._getDemuxer()).getMetadataTags()}dispose(){if(!this._disposed){this._disposed=!0;for(let e of this._sourceRefs)e.free();this._sourceRefs.length=0,xc?.unregister(this),this._demuxerPromise?.then(e=>e.dispose())}}[Symbol.dispose](){this.dispose()}},Rn=class extends Error{constructor(t="Input has an unsupported or unrecognizable format."){super(t),this.name="UnsupportedInputFormatError"}},be=class extends Error{constructor(t="Input has been disposed."){super(t),this.name="InputDisposedError"}};var pr=class{constructor(t){this.source=t}get fileSize(){let t=this.source._getFileSize();if(t===void 0)throw new Error("Reading file size too early; read required first.");return t}get fileSizeNonStrict(){return this.source._getFileSize()??null}requestSlice(t,e){if(this.source._disposed)throw new be;if(t<0||this.fileSizeNonStrict!==null&&t+e>this.fileSizeNonStrict)return null;if(e===0){let a=new Uint8Array(0);return new he(a,V(a),0,t,t)}let r=t+e,i=this.source._read(t,r,ec,tc);return i instanceof Promise?i.then(a=>a?new he(a.bytes,a.view,a.offset,t,r):null):i?new he(i.bytes,i.view,i.offset,t,r):null}requestSliceRange(t,e,r){if(this.source._disposed)throw new be;if(t<0)return null;if(this.fileSizeNonStrict!==null)return this.requestSlice(t,ue(this.fileSizeNonStrict-t,e,r));{let i=this.requestSlice(t,r),a=s=>s||(h(this.fileSizeNonStrict!==null),this.requestSlice(t,ue(this.fileSizeNonStrict-t,e,r)));return i instanceof Promise?i.then(a):a(i)}}requestEntireFile(){if(this.fileSizeNonStrict!==null)return this.requestSlice(0,this.fileSizeNonStrict);let t=1024;return(async()=>{let e=[],r=0;for(;;){if(e.length===1&&this.fileSizeNonStrict!==null)return this.requestSlice(0,this.fileSizeNonStrict);let s=e.length*t,o=this.requestSliceRange(s,0,t);if(o instanceof Promise&&(o=await o),!o)break;e.push(B(o,o.length)),r+=o.length}let i=new Uint8Array(r),a=0;for(let s of e)i.set(s,a),a+=s.length;return new he(i,V(i),0,0,r)})()}},he=class n{constructor(t,e,r,i,a){this.bytes=t;this.view=e;this.offset=r;this.start=i;this.end=a;this.bufferPos=i-r}static tempFromBytes(t){return new n(t,V(t),0,0,t.length)}get length(){return this.end-this.start}get filePos(){return this.offset+this.bufferPos}set filePos(t){this.bufferPos=t-this.offset}get remainingLength(){return Math.max(this.end-this.filePos,0)}skip(t){this.bufferPos+=t}slice(t,e=this.end-t){if(t<this.start||t+e>this.end)throw new RangeError("Slicing outside of original slice.");return new n(this.bytes,this.view,this.offset,t,t+e)}},Ue=(n,t)=>{if(n.filePos<n.start||n.filePos+t>n.end)throw new RangeError(`Tried reading [${n.filePos}, ${n.filePos+t}), but slice is [${n.start}, ${n.end}). This is likely an internal error, please report it alongside the file that caused it.`)},B=(n,t)=>{Ue(n,t);let e=n.bytes.subarray(n.bufferPos,n.bufferPos+t);return n.bufferPos+=t,e},O=n=>(Ue(n,1),n.view.getUint8(n.bufferPos++)),Ti=(n,t)=>{Ue(n,2);let e=n.view.getUint16(n.bufferPos,t);return n.bufferPos+=2,e},se=n=>{Ue(n,2);let t=n.view.getUint16(n.bufferPos,!1);return n.bufferPos+=2,t},ut=n=>{Ue(n,3);let t=Cr(n.view,n.bufferPos,!1);return n.bufferPos+=3,t},un=n=>{Ue(n,2);let t=n.view.getInt16(n.bufferPos,!1);return n.bufferPos+=2,t},Kt=(n,t)=>{Ue(n,4);let e=n.view.getUint32(n.bufferPos,t);return n.bufferPos+=4,e},F=n=>{Ue(n,4);let t=n.view.getUint32(n.bufferPos,!1);return n.bufferPos+=4,t},Or=n=>{Ue(n,4);let t=n.view.getUint32(n.bufferPos,!0);return n.bufferPos+=4,t},Wt=n=>{Ue(n,4);let t=n.view.getInt32(n.bufferPos,!1);return n.bufferPos+=4,t},df=n=>{Ue(n,4);let t=n.view.getInt32(n.bufferPos,!0);return n.bufferPos+=4,t},Mo=(n,t)=>{let e,r;return t?(e=Kt(n,!0),r=Kt(n,!0)):(r=Kt(n,!1),e=Kt(n,!1)),r*4294967296+e},Be=n=>{let t=F(n),e=F(n);return t*4294967296+e},fl=n=>{let t=Wt(n),e=F(n);return t*4294967296+e},xl=n=>{let t=Or(n);return df(n)*4294967296+t},yl=n=>{Ue(n,4);let t=n.view.getFloat32(n.bufferPos,!1);return n.bufferPos+=4,t},ka=n=>{Ue(n,8);let t=n.view.getFloat64(n.bufferPos,!1);return n.bufferPos+=8,t},te=(n,t)=>{Ue(n,t);let e="";for(let r=0;r<t;r++)e+=String.fromCharCode(n.bytes[n.bufferPos++]);return e},Xa=(n,t,e)=>me.decode(B(n,t)).split(`
31
- `).map(a=>a.trim()).filter(a=>a.length>0&&!e?.ignore?.(a));var fn=128,ze=10,_i=["Blues","Classic rock","Country","Dance","Disco","Funk","Grunge","Hip-hop","Jazz","Metal","New age","Oldies","Other","Pop","Rhythm and blues","Rap","Reggae","Rock","Techno","Industrial","Alternative","Ska","Death metal","Pranks","Soundtrack","Euro-techno","Ambient","Trip-hop","Vocal","Jazz & funk","Fusion","Trance","Classical","Instrumental","Acid","House","Game","Sound clip","Gospel","Noise","Alternative rock","Bass","Soul","Punk","Space","Meditative","Instrumental pop","Instrumental rock","Ethnic","Gothic","Darkwave","Techno-industrial","Electronic","Pop-folk","Eurodance","Dream","Southern rock","Comedy","Cult","Gangsta","Top 40","Christian rap","Pop/funk","Jungle music","Native US","Cabaret","New wave","Psychedelic","Rave","Showtunes","Trailer","Lo-fi","Tribal","Acid punk","Acid jazz","Polka","Retro","Musical","Rock 'n' roll","Hard rock","Folk","Folk rock","National folk","Swing","Fast fusion","Bebop","Latin","Revival","Celtic","Bluegrass","Avantgarde","Gothic rock","Progressive rock","Psychedelic rock","Symphonic rock","Slow rock","Big band","Chorus","Easy listening","Acoustic","Humour","Speech","Chanson","Opera","Chamber music","Sonata","Symphony","Booty bass","Primus","Porn groove","Satire","Slow jam","Club","Tango","Samba","Folklore","Ballad","Power ballad","Rhythmic Soul","Freestyle","Duet","Punk rock","Drum solo","A cappella","Euro-house","Dance hall","Goa music","Drum & bass","Club-house","Hardcore techno","Terror","Indie","Britpop","Negerpunk","Polsk punk","Beat","Christian gangsta rap","Heavy metal","Black metal","Crossover","Contemporary Christian","Christian rock","Merengue","Salsa","Thrash metal","Anime","Jpop","Synthpop","Christmas","Art rock","Baroque","Bhangra","Big beat","Breakbeat","Chillout","Downtempo","Dub","EBM","Eclectic","Electro","Electroclash","Emo","Experimental","Garage","Global","IDM","Illbient","Industro-Goth","Jam Band","Krautrock","Leftfield","Lounge","Math rock","New romantic","Nu-breakz","Post-punk","Post-rock","Psytrance","Shoegaze","Space rock","Trop rock","World music","Neoclassical","Audiobook","Audio theatre","Neue Deutsche Welle","Podcast","Indie rock","G-Funk","Dubstep","Garage rock","Psybient"],wl=(n,t)=>{let e=n.filePos;t.raw??={},t.raw.TAG??=B(n,fn-3),n.filePos=e;let r=vi(n,30);r&&(t.title??=r);let i=vi(n,30);i&&(t.artist??=i);let a=vi(n,30);a&&(t.album??=a);let s=vi(n,4),o=Number.parseInt(s,10);Number.isInteger(o)&&o>0&&(t.date??=new Date(o,0,1));let c=B(n,30),u;if(c[28]===0&&c[29]!==0){let m=c[29];m>0&&(t.trackNumber??=m),n.skip(-30),u=vi(n,28),n.skip(2)}else n.skip(-30),u=vi(n,30);u&&(t.comment??=u);let l=O(n);l<_i.length&&(t.genre??=_i[l])},vi=(n,t)=>{let e=B(n,t),r=vr(e.indexOf(0),e.length),i=e.subarray(0,r),a="";for(let s=0;s<i.length;s++)a+=String.fromCharCode(i[s]);return a.trimEnd()},ft=n=>{let t=n.filePos,e=te(n,3),r=O(n),i=O(n),a=O(n),s=F(n);if(e!=="ID3"||r===255||i===255||(s&2155905152)!==0)return n.filePos=t,null;let o=Zn(s);return{majorVersion:r,revision:i,flags:a,size:o}},bi=(n,t,e)=>{if(![2,3,4].includes(t.majorVersion)){console.warn(`Unsupported ID3v2 major version: ${t.majorVersion}`);return}let r=B(n,t.size),i=new Pc(t,r);if(t.flags&16&&i.removeFooter(),t.flags&128&&t.majorVersion===3&&i.ununsynchronizeAll(),t.flags&64){let a=i.readU32();t.majorVersion===3?i.pos+=a:i.pos+=a-4}for(;i.pos<=i.bytes.length-i.frameHeaderSize();){let a=i.readId3V2Frame();if(!a)break;let s=i.pos,o=i.pos+a.size,c=!1,u=!1,l=!1;if(t.majorVersion===3?(c=!!(a.flags&64),u=!!(a.flags&128)):t.majorVersion===4&&(c=!!(a.flags&4),u=!!(a.flags&8),l=!!(a.flags&2)||!!(t.flags&128)),c){console.warn(`Skipping encrypted ID3v2 frame ${a.id}`),i.pos=o;continue}if(u){console.warn(`Skipping compressed ID3v2 frame ${a.id}`),i.pos=o;continue}if(l&&i.ununsynchronizeRegion(i.pos,o),e.raw??={},a.id==="TXXX"){let m=e.raw.TXXX??={},d=i.readId3V2TextEncoding(),f=i.readId3V2Text(d,o),p=i.readId3V2Text(d,o);m[f]??=p}else a.id[0]==="T"?e.raw[a.id]??=i.readId3V2EncodingAndText(o):e.raw[a.id]??=i.readBytes(a.size);switch(i.pos=s,a.id){case"TIT2":case"TT2":e.title??=i.readId3V2EncodingAndText(o);break;case"TIT3":case"TT3":e.description??=i.readId3V2EncodingAndText(o);break;case"TPE1":case"TP1":e.artist??=i.readId3V2EncodingAndText(o);break;case"TALB":case"TAL":e.album??=i.readId3V2EncodingAndText(o);break;case"TPE2":case"TP2":e.albumArtist??=i.readId3V2EncodingAndText(o);break;case"TRCK":case"TRK":{let d=i.readId3V2EncodingAndText(o).split("/"),f=Number.parseInt(d[0],10),p=d[1]&&Number.parseInt(d[1],10);Number.isInteger(f)&&f>0&&(e.trackNumber??=f),p&&Number.isInteger(p)&&p>0&&(e.tracksTotal??=p)}break;case"TPOS":case"TPA":{let d=i.readId3V2EncodingAndText(o).split("/"),f=Number.parseInt(d[0],10),p=d[1]&&Number.parseInt(d[1],10);Number.isInteger(f)&&f>0&&(e.discNumber??=f),p&&Number.isInteger(p)&&p>0&&(e.discsTotal??=p)}break;case"TCON":case"TCO":{let m=i.readId3V2EncodingAndText(o),d=/^\((\d+)\)/.exec(m);if(d){let f=Number.parseInt(d[1]);if(_i[f]!==void 0){e.genre??=_i[f];break}}if(d=/^\d+$/.exec(m),d){let f=Number.parseInt(d[0]);if(_i[f]!==void 0){e.genre??=_i[f];break}}e.genre??=m}break;case"TDRC":case"TDAT":{let m=i.readId3V2EncodingAndText(o),d=new Date(m);Number.isNaN(d.getTime())||(e.date??=d)}break;case"TYER":case"TYE":{let m=i.readId3V2EncodingAndText(o),d=Number.parseInt(m,10);Number.isInteger(d)&&(e.date??=new Date(d,0,1))}break;case"USLT":case"ULT":{let m=i.readU8();i.pos+=3,i.readId3V2Text(m,o),e.lyrics??=i.readId3V2Text(m,o)}break;case"COMM":case"COM":{let m=i.readU8();i.pos+=3,i.readId3V2Text(m,o),e.comment??=i.readId3V2Text(m,o)}break;case"APIC":case"PIC":{let m=i.readId3V2TextEncoding(),d;if(t.majorVersion===2){let g=i.readAscii(3);d=g==="PNG"?"image/png":g==="JPG"?"image/jpeg":"image/*"}else d=i.readId3V2Text(m,o);let f=i.readU8(),p=i.readId3V2Text(m,o).trimEnd(),b=o-i.pos;if(b>=0){let g=i.readBytes(b);e.images||(e.images=[]),e.images.push({data:g,mimeType:d,kind:f===3?"coverFront":f===4?"coverBack":"unknown",description:p})}}break;default:i.pos+=a.size;break}i.pos=o}},Pc=class{constructor(t,e){this.header=t;this.bytes=e;this.pos=0;this.view=new DataView(e.buffer,e.byteOffset,e.byteLength)}frameHeaderSize(){return this.header.majorVersion===2?6:10}ununsynchronizeAll(){let t=[];for(let e=0;e<this.bytes.length;e++){let r=this.bytes[e];t.push(r),r===255&&e!==this.bytes.length-1&&this.bytes[e]===0&&e++}this.bytes=new Uint8Array(t),this.view=new DataView(this.bytes.buffer)}ununsynchronizeRegion(t,e){let r=[];for(let s=t;s<e;s++){let o=this.bytes[s];r.push(o),o===255&&s!==e-1&&this.bytes[s+1]===0&&s++}let i=this.bytes.subarray(0,t),a=this.bytes.subarray(e);this.bytes=new Uint8Array(i.length+r.length+a.length),this.bytes.set(i,0),this.bytes.set(r,i.length),this.bytes.set(a,i.length+r.length),this.view=new DataView(this.bytes.buffer)}removeFooter(){this.bytes=this.bytes.subarray(0,this.bytes.length-ze),this.view=new DataView(this.bytes.buffer)}readBytes(t){let e=this.bytes.subarray(this.pos,this.pos+t);return this.pos+=t,e}readU8(){let t=this.view.getUint8(this.pos);return this.pos+=1,t}readU16(){let t=this.view.getUint16(this.pos,!1);return this.pos+=2,t}readU24(){let t=this.view.getUint16(this.pos,!1),e=this.view.getUint8(this.pos+2);return this.pos+=3,t*256+e}readU32(){let t=this.view.getUint32(this.pos,!1);return this.pos+=4,t}readAscii(t){let e="";for(let r=0;r<t;r++)e+=String.fromCharCode(this.view.getUint8(this.pos+r));return this.pos+=t,e}readId3V2Frame(){if(this.header.majorVersion===2){let t=this.readAscii(3);if(t==="\0\0\0")return null;let e=this.readU24();return{id:t,size:e,flags:0}}else{let t=this.readAscii(4);if(t==="\0\0\0\0")return null;let e=this.readU32(),r=this.header.majorVersion===4?Zn(e):e,i=this.readU16(),a=this.pos,s=o=>{let c=this.pos+o;if(c>this.bytes.length)return!1;if(c<=this.bytes.length-this.frameHeaderSize()){this.pos+=o;let u=this.readAscii(4);if(u!=="\0\0\0\0"&&!/[0-9A-Z]{4}/.test(u))return!1}return!0};if(!s(r)){let o=this.header.majorVersion===4?e:Zn(e);s(o)&&(r=o)}return this.pos=a,{id:t,size:r,flags:i}}}readId3V2TextEncoding(){let t=this.readU8();if(t>3)throw new Error(`Unsupported text encoding: ${t}`);return t}readId3V2Text(t,e){let r=this.pos,i=this.readBytes(e-this.pos);switch(t){case 0:{let a="";for(let s=0;s<i.length;s++){let o=i[s];if(o===0){this.pos=r+s+1;break}a+=String.fromCharCode(o)}return a}case 1:if(i[0]===255&&i[1]===254){let a=new TextDecoder("utf-16le"),s=vr(i.findIndex((o,c)=>o===0&&i[c+1]===0&&c%2===0),i.length);return this.pos=r+Math.min(s+2,i.length),a.decode(i.subarray(2,s))}else if(i[0]===254&&i[1]===255){let a=new TextDecoder("utf-16be"),s=vr(i.findIndex((o,c)=>o===0&&i[c+1]===0&&c%2===0),i.length);return this.pos=r+Math.min(s+2,i.length),a.decode(i.subarray(2,s))}else{let a=vr(i.findIndex(s=>s===0),i.length);return this.pos=r+Math.min(a+1,i.length),me.decode(i.subarray(0,a))}case 2:{let a=new TextDecoder("utf-16be"),s=vr(i.findIndex((o,c)=>o===0&&i[c+1]===0&&c%2===0),i.length);return this.pos=r+Math.min(s+2,i.length),a.decode(i.subarray(0,s))}case 3:{let a=vr(i.findIndex(s=>s===0),i.length);return this.pos=r+Math.min(a+1,i.length),me.decode(i.subarray(0,a))}}}readId3V2EncodingAndText(t){if(this.pos>=t)return"";let e=this.readId3V2TextEncoding();return this.readId3V2Text(e,t)}},Tr=class{constructor(t){this.helper=new Uint8Array(8);this.helperView=V(this.helper);this.writer=t}writeId3V2Tag(t){let e=this.writer.getPos();this.writeAscii("ID3"),this.writeU8(4),this.writeU8(0),this.writeU8(0),this.writeSynchsafeU32(0);let r=this.writer.getPos(),i=new Set;for(let{key:o,value:c}of it(t))switch(o){case"title":this.writeId3V2TextFrame("TIT2",c),i.add("TIT2");break;case"description":this.writeId3V2TextFrame("TIT3",c),i.add("TIT3");break;case"artist":this.writeId3V2TextFrame("TPE1",c),i.add("TPE1");break;case"album":this.writeId3V2TextFrame("TALB",c),i.add("TALB");break;case"albumArtist":this.writeId3V2TextFrame("TPE2",c),i.add("TPE2");break;case"trackNumber":{let u=t.tracksTotal!==void 0?`${c}/${t.tracksTotal}`:c.toString();this.writeId3V2TextFrame("TRCK",u),i.add("TRCK")}break;case"discNumber":{let u=t.discsTotal!==void 0?`${c}/${t.discsTotal}`:c.toString();this.writeId3V2TextFrame("TPOS",u),i.add("TPOS")}break;case"genre":this.writeId3V2TextFrame("TCON",c),i.add("TCON");break;case"date":this.writeId3V2TextFrame("TDRC",c.toISOString().slice(0,10)),i.add("TDRC");break;case"lyrics":this.writeId3V2LyricsFrame(c),i.add("USLT");break;case"comment":this.writeId3V2CommentFrame(c),i.add("COMM");break;case"images":{let u={coverFront:3,coverBack:4,unknown:0};for(let l of c){let m=u[l.kind]??0,d=l.description??"";this.writeId3V2ApicFrame(l.mimeType,m,d,l.data)}}break;case"tracksTotal":case"discsTotal":break;case"raw":break;default:ne(o)}if(t.raw)for(let o in t.raw){let c=t.raw[o];if(c==null||o.length!==4||i.has(o))continue;let u;if(typeof c=="string")if(Je(c)){u=new Uint8Array(c.length+2),u[0]=0;for(let m=0;m<c.length;m++)u[m+1]=c.charCodeAt(m)}else{let m=$.encode(c);u=new Uint8Array(m.byteLength+2),u[0]=3,u.set(m,1)}else if(c instanceof Uint8Array)u=c;else if(o==="TXXX"&&Hn(c)){for(let l in c){let m=c[l],d=Je(l)&&Je(m),f=d?null:$.encode(l),p=d?null:$.encode(m),b=d?l.length:f.byteLength,g=d?m.length:p.byteLength,y=1+b+1+g+1;this.writeAscii("TXXX"),this.writeSynchsafeU32(y),this.writeU16(0),this.writeU8(d?0:3),d?(this.writeIsoString(l),this.writeIsoString(m)):(this.writer.write(f),this.writeU8(0),this.writer.write(p),this.writeU8(0))}continue}else continue;this.writeAscii(o),this.writeSynchsafeU32(u.byteLength),this.writeU16(0),this.writer.write(u)}let a=this.writer.getPos(),s=a-r;return this.writer.seek(e+6),this.writeSynchsafeU32(s),this.writer.seek(a),s+10}writeU8(t){this.helper[0]=t,this.writer.write(this.helper.subarray(0,1))}writeU16(t){this.helperView.setUint16(0,t,!1),this.writer.write(this.helper.subarray(0,2))}writeU32(t){this.helperView.setUint32(0,t,!1),this.writer.write(this.helper.subarray(0,4))}writeAscii(t){for(let e=0;e<t.length;e++)this.helper[e]=t.charCodeAt(e);this.writer.write(this.helper.subarray(0,t.length))}writeSynchsafeU32(t){this.writeU32(Hu(t))}writeIsoString(t){let e=new Uint8Array(t.length+1);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r);this.writer.write(e)}writeUtf8String(t){let e=$.encode(t);this.writer.write(e),this.writeU8(0)}writeId3V2TextFrame(t,e){let r=Je(e),a=1+(r?e.length:$.encode(e).byteLength)+1;this.writeAscii(t),this.writeSynchsafeU32(a),this.writeU16(0),this.writeU8(r?0:3),r?this.writeIsoString(e):this.writeUtf8String(e)}writeId3V2LyricsFrame(t){let e=Je(t),r="",i=4+r.length+1+t.length+1;this.writeAscii("USLT"),this.writeSynchsafeU32(i),this.writeU16(0),this.writeU8(e?0:3),this.writeAscii("und"),e?(this.writeIsoString(r),this.writeIsoString(t)):(this.writeUtf8String(r),this.writeUtf8String(t))}writeId3V2CommentFrame(t){let e=Je(t),r=e?t.length:$.encode(t).byteLength,i="",a=4+i.length+1+r+1;this.writeAscii("COMM"),this.writeSynchsafeU32(a),this.writeU16(0),this.writeU8(e?0:3),this.writeU8(117),this.writeU8(110),this.writeU8(100),e?(this.writeIsoString(i),this.writeIsoString(t)):(this.writeUtf8String(i),this.writeUtf8String(t))}writeId3V2ApicFrame(t,e,r,i){let a=Je(t)&&Je(r),s=a?r.length:$.encode(r).byteLength,o=1+t.length+1+1+s+1+i.byteLength;this.writeAscii("APIC"),this.writeSynchsafeU32(o),this.writeU16(0),this.writeU8(a?0:3),a?this.writeIsoString(t):this.writeUtf8String(t),this.writeU8(e),a?this.writeIsoString(r):this.writeUtf8String(r),this.writer.write(i)}};var ge=class{constructor(t){this.mutex=new Ie;this.trackTimestampInfo=new WeakMap;this.output=t}onTrackClose(t){}validateTimestamp(t,e,r){if(e<0)throw new Error(`Timestamps must be non-negative (got ${e}s).`);let i=this.trackTimestampInfo.get(t);if(i){if(r&&(i.maxTimestampBeforeLastKeyPacket=i.maxTimestamp),i.maxTimestampBeforeLastKeyPacket!==null&&e<i.maxTimestampBeforeLastKeyPacket)throw new Error(`Timestamps cannot be smaller than the largest timestamp of the previous GOP (a GOP begins with a key packet and ends right before the next key packet). Got ${e}s, but largest timestamp is ${i.maxTimestampBeforeLastKeyPacket}s.`);i.maxTimestamp=Math.max(i.maxTimestamp,e)}else{if(!r)throw new Error("First packet must be a key packet.");i={maxTimestamp:e,maxTimestampBeforeLastKeyPacket:null},this.trackTimestampInfo.set(t,i)}}};var Fs=class extends ge{constructor(e,r){super(e);this.header=null;this.headerBitstream=null;this.inputIsAdts=null;this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!0),Vt(this.output._metadataTags)||new Tr(this.writer).writeId3V2Tag(this.output._metadataTags),e()}async getMimeType(){return"audio/aac"}async addEncodedVideoPacket(){throw new Error("ADTS does not support video.")}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{if(this.validateTimestamp(e,r.timestamp,r.type==="key"),this.inputIsAdts===null){Ee(i);let s=i?.decoderConfig?.description;if(this.inputIsAdts=!s,!this.inputIsAdts){let o=Tt(j(s)),c=qn(o);this.header=c.header,this.headerBitstream=c.bitstream}}if(this.inputIsAdts){let s=this.writer.getPos();this.writer.write(r.data),this.format._options.onFrame&&this.format._options.onFrame(r.data,s)}else{h(this.header);let s=r.data.byteLength+this.header.byteLength;Gn(this.headerBitstream,s);let o=this.writer.getPos();if(this.writer.write(this.header),this.writer.write(r.data),this.format._options.onFrame){let c=new Uint8Array(s);c.set(this.header,0),c.set(r.data,this.header.byteLength),this.format._options.onFrame(c,o)}}await this.writer.flush()}finally{a()}}async addSubtitleCue(){throw new Error("ADTS does not support subtitles.")}async finalize(){(await this.mutex.acquire())()}};var fd=new Uint8Array([102,76,97,67]),mf=38,ff=34,Ms=class extends ge{constructor(e,r){super(e);this.metadataWritten=!1;this.blockSizes=[];this.frameSizes=[];this.sampleRate=null;this.channels=null;this.bitsPerSample=null;this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!!this.format._options.appendOnly),this.writer.write(fd),e()}writeHeader({bitsPerSample:e,minimumBlockSize:r,maximumBlockSize:i,minimumFrameSize:a,maximumFrameSize:s,sampleRate:o,channels:c,totalSamples:u}){h(this.writer.getPos()===4);let l=!Vt(this.output._metadataTags),m=new W(new Uint8Array(4));m.writeBits(1,+!l),m.writeBits(7,0),m.writeBits(24,ff),this.writer.write(m.bytes);let d=new W(new Uint8Array(18));if(d.writeBits(16,r),d.writeBits(16,i),d.writeBits(24,a),d.writeBits(24,s),d.writeBits(20,o),d.writeBits(3,c-1),d.writeBits(5,e-1),u>=2**32)throw new Error("This muxer only supports writing up to 2 ** 32 samples");d.writeBits(4,0),d.writeBits(32,u),this.writer.write(d.bytes),this.writer.write(new Uint8Array(16))}writePictureBlock(e){let r=32+e.mimeType.length+(e.description?.length??0)+e.data.length,i=new Uint8Array(r),a=0,s=V(i);s.setUint32(a,e.kind==="coverFront"?3:e.kind==="coverBack"?4:0),a+=4,s.setUint32(a,e.mimeType.length),a+=4,i.set($.encode(e.mimeType),8),a+=e.mimeType.length,s.setUint32(a,e.description?.length??0),a+=4,i.set($.encode(e.description??""),a),a+=e.description?.length??0,a+=16,s.setUint32(a,e.data.length),a+=4,i.set(e.data,a),a+=e.data.length,h(a===r);let o=new W(new Uint8Array(4));o.writeBits(1,0),o.writeBits(7,6),o.writeBits(24,r),this.writer.write(o.bytes),this.writer.write(i)}writeVorbisCommentAndPictureBlock(){if(this.format._options.appendOnly||this.writer.seek(mf+fd.byteLength),Vt(this.output._metadataTags)){this.metadataWritten=!0;return}let e=this.output._metadataTags.images??[];for(let a of e)this.writePictureBlock(a);let r=nn(new Uint8Array(0),this.output._metadataTags,!1),i=new W(new Uint8Array(4));i.writeBits(1,1),i.writeBits(7,4),i.writeBits(24,r.length),this.writer.write(i.bytes),this.writer.write(r),this.metadataWritten=!0}async getMimeType(){return"audio/flac"}async addEncodedVideoPacket(){throw new Error("FLAC does not support video.")}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{if(this.validateTimestamp(e,r.timestamp,r.type==="key"),this.sampleRate===null){Ee(i),h(i),h(i.decoderConfig),h(i.decoderConfig.description),this.sampleRate=i.decoderConfig.sampleRate,this.channels=i.decoderConfig.numberOfChannels;let d=new W(j(i.decoderConfig.description));d.skipBits(167);let f=d.readBits(5)+1;this.bitsPerSample=f,this.format._options.appendOnly&&this.writeHeader({minimumBlockSize:16,maximumBlockSize:65535,minimumFrameSize:0,maximumFrameSize:0,sampleRate:this.sampleRate,channels:this.channels,bitsPerSample:this.bitsPerSample,totalSamples:0})}this.metadataWritten||this.writeVorbisCommentAndPictureBlock();let s=he.tempFromBytes(r.data);s.skip(2);let o=B(s,2),c=new W(o),u=Oa(c.readBits(4));if(u===null)throw new Error("Invalid FLAC frame: Invalid block size.");Da(s);let l=Va(s,u);this.format._options.appendOnly||(this.blockSizes.push(l),this.frameSizes.push(r.data.length));let m=this.writer.getPos();this.writer.write(r.data),this.format._options.onFrame&&this.format._options.onFrame(r.data,m),await this.writer.flush()}finally{a()}}addSubtitleCue(){throw new Error("FLAC does not support subtitles.")}async finalize(){let e=await this.mutex.acquire();if(!this.format._options.appendOnly){let r=1/0,i=0,a=1/0,s=0,o=0;for(let c=0;c<this.blockSizes.length;c++)a=Math.min(a,this.frameSizes[c]),s=Math.max(s,this.frameSizes[c]),i=Math.max(i,this.blockSizes[c]),o+=this.blockSizes[c],c!==this.blockSizes.length-1&&(r=Math.min(r,this.blockSizes[c]));h(this.sampleRate!==null),h(this.channels!==null),h(this.bitsPerSample!==null),this.writer.seek(4),this.writeHeader({minimumBlockSize:r,maximumBlockSize:i,minimumFrameSize:a,maximumFrameSize:s,sampleRate:this.sampleRate,channels:this.channels,bitsPerSample:this.bitsPerSample,totalSamples:o})}e()}};var Fn=/(?:(.+?)\n)?((?:\d{2}:)?\d{2}:\d{2}.\d{3})\s+-->\s+((?:\d{2}:)?\d{2}:\d{2}.\d{3})/g,pf=/^WEBVTT(.|\n)*?\n{2}/,Ri=/<(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})>/g,Bs=class{constructor(t){this.preambleText=null;this.preambleEmitted=!1;this.options=t}parse(t){t=t.replaceAll(`\r
30
+ `),r=this.gl.createProgram();return this.gl.attachShader(r,t),this.gl.attachShader(r,e),this.gl.linkProgram(r),r}createShader(t,e){h(this.gl);let r=this.gl.createShader(t);return this.gl.shaderSource(r,e),this.gl.compileShader(r),r}createVAO(){h(this.gl),h(this.program);let t=this.gl.createVertexArray();this.gl.bindVertexArray(t);let e=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),r=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,r),this.gl.bufferData(this.gl.ARRAY_BUFFER,e,this.gl.STATIC_DRAW);let i=this.gl.getAttribLocation(this.program,"a_position"),a=this.gl.getAttribLocation(this.program,"a_texCoord");return this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,16,0),this.gl.enableVertexAttribArray(a),this.gl.vertexAttribPointer(a,2,this.gl.FLOAT,!1,16,8),t}createTexture(){h(this.gl);let t=this.gl.createTexture();return this.gl.bindTexture(this.gl.TEXTURE_2D,t),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),t}updateGpu(t,e){h(this.gl),h(this.canvas),(t.displayWidth!==this.canvas.width||t.displayHeight!==this.canvas.height)&&(this.canvas.width=t.displayWidth,this.canvas.height=t.displayHeight),this.gl.activeTexture(this.gl.TEXTURE0),this.gl.bindTexture(this.gl.TEXTURE_2D,this.colorTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,t),this.gl.activeTexture(this.gl.TEXTURE1),this.gl.bindTexture(this.gl.TEXTURE_2D,this.alphaTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,e),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4);let r=new VideoFrame(this.canvas,{timestamp:t.timestamp,duration:t.duration??void 0});return t.close(),e.close(),r}updateCpu(t,e){if(!this.worker){let a=new Blob([`(${cf.toString()})()`],{type:"application/javascript"}),s=URL.createObjectURL(a);this.worker=new Worker(s),URL.revokeObjectURL(s),this.worker.addEventListener("message",o=>{let c=o.data,u=this.pendingRequests.get(c.id);u&&(this.pendingRequests.delete(c.id),"error"in c?u.reject(new Error(c.error)):u.resolve(c.frame))}),this.worker.addEventListener("error",o=>{let c=new Error(o.message||"Color/alpha merge worker error.");for(let u of this.pendingRequests.values())u.reject(c);this.pendingRequests.clear()})}let r=this.nextRequestId++,i=Z();return this.pendingRequests.set(r,i),this.worker.postMessage({id:r,color:t,alpha:e},{transfer:[t,e]}),i.promise}close(){this.gl?.getExtension("WEBGL_lose_context")?.loseContext(),this.gl=null,this.canvas=null,this.worker?.terminate(),this.worker=null;let t=new Error("Color/alpha merger closed.");for(let e of this.pendingRequests.values())e.reject(t);this.pendingRequests.clear()}};vs.forceCpu=!0;var yc=vs,cf=()=>{let n=null,t=null,e=Promise.resolve();self.addEventListener("message",c=>{let{id:u,color:l,alpha:m}=c.data;e=e.then(async()=>{try{let d=await r(l,m);self.postMessage({id:u,frame:d},{transfer:[d]})}catch(d){self.postMessage({id:u,error:d.message})}finally{l.close(),m.close()}})});let r=async(c,u)=>{let l=c.format,m=u.format;if(!l||!m)throw new Error("CPU color/alpha merging requires a known VideoFrame format.");let d=l.includes("P10"),f=l.includes("P12"),p=m.includes("P10"),b=m.includes("P12");if(p!==d||b!==f)throw new Error(`CPU color/alpha merging requires the alpha frame to have the same bit depth as the color frame (color: '${l}', alpha: '${m}').`);let g=c.codedWidth,y=c.codedHeight;if(l==="RGBX"||l==="RGBA"||l==="BGRX"||l==="BGRA")return await i(c,u,g,y,l);if(l==="I420"||l==="I420P10"||l==="I420P12"||l==="I422"||l==="I422P10"||l==="I422P12"||l==="I444"||l==="I444P10"||l==="I444P12")return await a(c,u,g,y,l);if(l==="NV12")return await s(c,u,g,y);throw new Error(`CPU color/alpha merging does not support format '${l}'.`)},i=async(c,u,l,m,d)=>{let f=l*m,p=new Uint8Array(f*4);await c.copyTo(p);let b=await o(u,l,m,1);for(let k=0,w=3;k<f;k++,w+=4)p[w]=b[k];let y={format:d==="RGBX"||d==="RGBA"?"RGBA":"BGRA",codedWidth:l,codedHeight:m,timestamp:c.timestamp,duration:c.duration??void 0,transfer:[p.buffer]};return new VideoFrame(p,y)},a=async(c,u,l,m,d)=>{let f=d.includes("P10"),p=d.includes("P12"),b=f||p?2:1,g,y;d.startsWith("I420")?(g=Math.ceil(l/2),y=Math.ceil(m/2)):d.startsWith("I422")?(g=Math.ceil(l/2),y=m):(g=l,y=m);let k=l*m,w=g*y,T=k*b,S=w*b,x=k*b,P=T+2*S+x,A=new Uint8Array(P);await c.copyTo(A);let C=await o(u,l,m,b),I=T+2*S;A.set(C,I);let v={format:d.slice(0,4)+"A"+d.slice(4),codedWidth:l,codedHeight:m,timestamp:c.timestamp,duration:c.duration??void 0,transfer:[A.buffer]};return new VideoFrame(A,v)},s=async(c,u,l,m)=>{let d=l*m,f=Math.ceil(l/2),p=Math.ceil(m/2),b=f*p,g=c.allocationSize();(!t||t.byteLength!==g)&&(t=new Uint8Array(g)),await c.copyTo(t);let y=new Uint8Array(d+2*b+d);y.set(t.subarray(0,d),0);let k=d,w=d+b,T=d;for(let P=0;P<b;P++)y[k+P]=t[T+P*2],y[w+P]=t[T+P*2+1];let S=await o(u,l,m,1);y.set(S,d+2*b);let x={format:"I420A",codedWidth:l,codedHeight:m,timestamp:c.timestamp,duration:c.duration??void 0,transfer:[y.buffer]};return new VideoFrame(y,x)},o=async(c,u,l,m)=>{let d=c.allocationSize();(!n||n.byteLength!==d)&&(n=new Uint8Array(d)),await c.copyTo(n);let f=c.format;if(f==="RGBA"||f==="BGRA"||f==="RGBX"||f==="BGRX"){let p=f==="RGBA"||f==="RGBX"?0:2,b=u*l;for(let g=0;g<b;g++)n[g]=n[g*4+p];return n.subarray(0,b)}else return n.subarray(0,u*l*m)}},qr=class extends _n{constructor(t){if(!(t instanceof $t))throw new TypeError("videoTrack must be an InputVideoTrack.");super(),this._track=t}async _createDecoder(t,e){if(!await this._track.canDecode())throw new Error("This video track cannot be decoded by this browser. Make sure to check decodability before using a track.");let r=await this._track.getCodec(),i=await this._track.getRotation(),a=await this._track.getDecoderConfig(),s=await this._track.getTimeResolution();return h(r&&a),new bc(t,e,r,a,i,s)}_createPacketSink(){return new xt(this._track)}async getSample(t,e={}){Xt(t);for await(let r of this.mediaSamplesAtTimestamps([t],e))return r;throw new Error("Internal error: Iterator returned nothing.")}samples(t,e,r={}){return this.mediaSamplesInRange(t,e,r)}samplesAtTimestamps(t,e={}){return this.mediaSamplesAtTimestamps(t,e)}},kc=class{constructor(t,e={}){this._rotation=0;this._initPromise=null;this._nextCanvasIndex=0;if(!(t instanceof $t))throw new TypeError("videoTrack must be an InputVideoTrack.");if(e&&typeof e!="object")throw new TypeError("options must be an object.");if(e.alpha!==void 0&&typeof e.alpha!="boolean")throw new TypeError("options.alpha, when provided, must be a boolean.");if(e.width!==void 0&&(!Number.isInteger(e.width)||e.width<=0))throw new TypeError("options.width, when defined, must be a positive integer.");if(e.height!==void 0&&(!Number.isInteger(e.height)||e.height<=0))throw new TypeError("options.height, when defined, must be a positive integer.");if(e.fit!==void 0&&!["fill","contain","cover"].includes(e.fit))throw new TypeError('options.fit, when provided, must be one of "fill", "contain", or "cover".');if(e.width!==void 0&&e.height!==void 0&&e.fit===void 0)throw new TypeError("When both options.width and options.height are provided, options.fit must also be provided.");if(e.rotation!==void 0&&![0,90,180,270].includes(e.rotation))throw new TypeError("options.rotation, when provided, must be 0, 90, 180 or 270.");if(e.crop!==void 0&&kr(e.crop,"options."),e.poolSize!==void 0&&(typeof e.poolSize!="number"||!Number.isInteger(e.poolSize)||e.poolSize<0))throw new TypeError("poolSize must be a non-negative integer.");this._videoTrack=t,this._alpha=e.alpha??!1,this._options=e,this._fit=e.fit??"fill",this._videoSampleSink=new qr(t),this._canvasPool=Array.from({length:e.poolSize??0},()=>null)}_ensureInit(){return this._initPromise??=(async()=>{let t=this._options,e=this._videoTrack,r=t.rotation??await e.getRotation(),i=await e.getSquarePixelWidth(),a=await e.getSquarePixelHeight(),[s,o]=r%180===0?[i,a]:[a,i],c=t.crop;c&&(c=Pi(c,s,o));let[u,l]=c?[c.width,c.height]:[s,o],m=u/l;t.width!==void 0&&t.height===void 0?(u=t.width,l=Math.round(u/m)):t.width===void 0&&t.height!==void 0?(l=t.height,u=Math.round(l*m)):t.width!==void 0&&t.height!==void 0&&(u=t.width,l=t.height),this._width=u,this._height=l,this._rotation=r,this._crop=c})()}_videoSampleToWrappedCanvas(t){let e=this._width,r=this._height,i=this._canvasPool[this._nextCanvasIndex],a=!1;i||(typeof document<"u"?(i=document.createElement("canvas"),i.width=e,i.height=r):i=new OffscreenCanvas(e,r),this._canvasPool.length>0&&(this._canvasPool[this._nextCanvasIndex]=i),a=!0),this._canvasPool.length>0&&(this._nextCanvasIndex=(this._nextCanvasIndex+1)%this._canvasPool.length);let s=i.getContext("2d",{alpha:this._alpha||vr()});h(s),s.resetTransform(),a||(!this._alpha&&vr()?(s.fillStyle="black",s.fillRect(0,0,e,r)):s.clearRect(0,0,e,r)),t.drawWithFit(s,{fit:this._fit,rotation:this._rotation,crop:this._crop});let o={canvas:i,timestamp:t.timestamp,duration:t.duration};return t.close(),o}async getCanvas(t,e){Xt(t),await this._ensureInit();let r=await this._videoSampleSink.getSample(t,e);return r&&this._videoSampleToWrappedCanvas(r)}async*canvases(t,e,r){await this._ensureInit(),yield*Qi(this._videoSampleSink.samples(t,e,r),i=>this._videoSampleToWrappedCanvas(i))}async*canvasesAtTimestamps(t,e){await this._ensureInit(),yield*Qi(this._videoSampleSink.samplesAtTimestamps(t,e),r=>r&&this._videoSampleToWrappedCanvas(r))}},Tc=class extends vn{constructor(e,r,i,a){super(e,r);this.decoder=null;this.customDecoder=null;this.customDecoderCallSerializer=new vt;this.customDecoderQueueSize=0;this.currentTimestamp=null;this.expectedFirstTimestamp=null;this.timestampOffset=0;let s=c=>{let u=c.timestamp;this.expectedFirstTimestamp&&this.currentTimestamp===null&&(this.timestampOffset=this.expectedFirstTimestamp-u),u+=this.timestampOffset,(this.currentTimestamp===null||Math.abs(u-this.currentTimestamp)>=c.duration)&&(this.currentTimestamp=u);let l=this.currentTimestamp;if(this.currentTimestamp+=c.duration,c.numberOfFrames===0){c.close();return}let m=a.sampleRate;c.setTimestamp(Math.round(l*m)/m),e(c)},o=br.find(c=>c.supports(i,a));if(o)this.customDecoder=new o,this.customDecoder.codec=i,this.customDecoder.config=a,this.customDecoder.onSample=c=>{if(!(c instanceof ye))throw new TypeError("The argument passed to onSample must be an AudioSample.");s(c)},this.customDecoderCallSerializer.call(()=>this.customDecoder.init());else{let c=new Error("Decoding error").stack;this.decoder=new AudioDecoder({output:u=>{try{s(new ye(u))}catch(l){this.onError(l)}},error:u=>{u.stack=c,this.onError(u)}}),this.decoder.configure(a)}}getDecodeQueueSize(){return this.customDecoder?this.customDecoderQueueSize:(h(this.decoder),this.decoder.decodeQueueSize)}decode(e){this.customDecoder?(this.customDecoderQueueSize++,this.customDecoderCallSerializer.call(()=>this.customDecoder.decode(e)).then(()=>this.customDecoderQueueSize--)):(h(this.decoder),this.expectedFirstTimestamp??=e.timestamp,this.decoder.decode(e.toEncodedAudioChunk()))}async flush(){this.customDecoder?await this.customDecoderCallSerializer.call(()=>this.customDecoder.flush()):(h(this.decoder),await this.decoder.flush()),this.currentTimestamp=null,this.expectedFirstTimestamp=null,this.timestampOffset=0}close(){this.customDecoder?this.customDecoderCallSerializer.call(()=>this.customDecoder.close()):(h(this.decoder),this.decoder.close())}},wc=class extends vn{constructor(e,r,i){super(e,r);this.decoderConfig=i;this.currentTimestamp=null;h(ee.includes(i.codec)),this.codec=i.codec;let{dataType:a,sampleSize:s,littleEndian:o}=Se(this.codec);switch(this.inputSampleSize=s,s){case 1:a==="unsigned"?this.readInputValue=(c,u)=>c.getUint8(u)-2**7:a==="signed"?this.readInputValue=(c,u)=>c.getInt8(u):a==="ulaw"?this.readInputValue=(c,u)=>cd(c.getUint8(u)):a==="alaw"?this.readInputValue=(c,u)=>ld(c.getUint8(u)):h(!1);break;case 2:a==="unsigned"?this.readInputValue=(c,u)=>c.getUint16(u,o)-2**15:a==="signed"?this.readInputValue=(c,u)=>c.getInt16(u,o):h(!1);break;case 3:a==="unsigned"?this.readInputValue=(c,u)=>Ir(c,u,o)-2**23:a==="signed"?this.readInputValue=(c,u)=>gu(c,u,o):h(!1);break;case 4:a==="unsigned"?this.readInputValue=(c,u)=>c.getUint32(u,o)-2**31:a==="signed"?this.readInputValue=(c,u)=>c.getInt32(u,o):a==="float"?this.readInputValue=(c,u)=>c.getFloat32(u,o):h(!1);break;case 8:a==="float"?this.readInputValue=(c,u)=>c.getFloat64(u,o):h(!1);break;default:ne(s),h(!1)}switch(s){case 1:a==="ulaw"||a==="alaw"?(this.outputSampleSize=2,this.outputFormat="s16",this.writeOutputValue=(c,u,l)=>c.setInt16(u,l,!0)):(this.outputSampleSize=1,this.outputFormat="u8",this.writeOutputValue=(c,u,l)=>c.setUint8(u,l+2**7));break;case 2:this.outputSampleSize=2,this.outputFormat="s16",this.writeOutputValue=(c,u,l)=>c.setInt16(u,l,!0);break;case 3:this.outputSampleSize=4,this.outputFormat="s32",this.writeOutputValue=(c,u,l)=>c.setInt32(u,l<<8,!0);break;case 4:this.outputSampleSize=4,a==="float"?(this.outputFormat="f32",this.writeOutputValue=(c,u,l)=>c.setFloat32(u,l,!0)):(this.outputFormat="s32",this.writeOutputValue=(c,u,l)=>c.setInt32(u,l,!0));break;case 8:this.outputSampleSize=4,this.outputFormat="f32",this.writeOutputValue=(c,u,l)=>c.setFloat32(u,l,!0);break;default:ne(s),h(!1)}}getDecodeQueueSize(){return 0}decode(e){let r=V(e.data),i=e.byteLength/this.decoderConfig.numberOfChannels/this.inputSampleSize,a=i*this.decoderConfig.numberOfChannels*this.outputSampleSize,s=new ArrayBuffer(a),o=new DataView(s);for(let m=0;m<i*this.decoderConfig.numberOfChannels;m++){let d=m*this.inputSampleSize,f=m*this.outputSampleSize,p=this.readInputValue(r,d);this.writeOutputValue(o,f,p)}let c=i/this.decoderConfig.sampleRate;(this.currentTimestamp===null||Math.abs(e.timestamp-this.currentTimestamp)>=c)&&(this.currentTimestamp=e.timestamp);let u=this.currentTimestamp;this.currentTimestamp+=c;let l=new ye({format:this.outputFormat,data:s,numberOfChannels:this.decoderConfig.numberOfChannels,sampleRate:this.decoderConfig.sampleRate,numberOfFrames:i,timestamp:u});this.onSample(l)}async flush(){}close(){}},Gr=class extends _n{constructor(t){if(!(t instanceof Yt))throw new TypeError("audioTrack must be an InputAudioTrack.");super(),this._track=t}async _createDecoder(t,e){if(!await this._track.canDecode())throw new Error("This audio track cannot be decoded by this browser. Make sure to check decodability before using a track.");let r=await this._track.getCodec(),i=await this._track.getDecoderConfig();return h(r&&i),ee.includes(i.codec)?new wc(t,e,i):new Tc(t,e,r,i)}_createPacketSink(){return new xt(this._track)}async getSample(t,e={}){Xt(t);for await(let r of this.mediaSamplesAtTimestamps([t],e))return r;throw new Error("Internal error: Iterator returned nothing.")}samples(t,e,r={}){return this.mediaSamplesInRange(t,e,r)}samplesAtTimestamps(t,e={}){return this.mediaSamplesAtTimestamps(t,e)}},Sc=class{constructor(t){if(!(t instanceof Yt))throw new TypeError("audioTrack must be an InputAudioTrack.");this._audioSampleSink=new Gr(t)}_audioSampleToWrappedArrayBuffer(t){let e={buffer:t.toAudioBuffer(),timestamp:t.timestamp,duration:t.duration};return t.close(),e}async getBuffer(t,e){Xt(t);let r=await this._audioSampleSink.getSample(t,e);return r&&this._audioSampleToWrappedArrayBuffer(r)}buffers(t,e,r){return Qi(this._audioSampleSink.samples(t,e,r),i=>this._audioSampleToWrappedArrayBuffer(i))}buffersAtTimestamps(t,e){return Qi(this._audioSampleSink.samplesAtTimestamps(t,e),r=>r&&this._audioSampleToWrappedArrayBuffer(r))}};var Kr=class n{constructor(t,e){this.input=t,this._backing=e}isVideoTrack(){return this instanceof $t}isAudioTrack(){return this instanceof Yt}get id(){return this._backing.getId()}get number(){return this._backing.getNumber()}async getInternalCodecId(){return this._backing.getInternalCodecId()}get internalCodecId(){return ce(this._backing.getInternalCodecId(),"internalCodecId","getInternalCodecId")}async getLanguageCode(){return this._backing.getLanguageCode()}get languageCode(){return ce(this._backing.getLanguageCode(),"languageCode","getLanguageCode")}async getName(){return this._backing.getName()}get name(){return ce(this._backing.getName(),"name","getName")}async getTimeResolution(){return this._backing.getTimeResolution()}get timeResolution(){return ce(this._backing.getTimeResolution(),"timeResolution","getTimeResolution")}async isRelativeToUnixEpoch(){return this._backing.isRelativeToUnixEpoch()}async getDisposition(){return this._backing.getDisposition()}get disposition(){return ce(this._backing.getDisposition(),"disposition","getDisposition")}async getBitrate(){return this._backing.getBitrate()}async getAverageBitrate(){return this._backing.getAverageBitrate()}async getFirstTimestamp(){return(await this._backing.getFirstPacket({metadataOnly:!0}))?.timestamp??0}async computeDuration(t){let e=await this._backing.getPacket(1/0,{metadataOnly:!0,...t}),r=(e?.timestamp??0)+(e?.duration??0);return bt(r,await this.getTimeResolution())}async getDurationFromMetadata(t={}){return this._backing.getDurationFromMetadata(t)}async computePacketStats(t=1/0,e){let r=new xt(this),i=1/0,a=-1/0,s=0,o=0;for await(let c of r.packets(void 0,void 0,{metadataOnly:!0,...e})){if(s>=t&&c.timestamp>=a)break;i=Math.min(i,c.timestamp),a=Math.max(a,c.timestamp+c.duration),s++,o+=c.byteLength}return{packetCount:s,averagePacketRate:s?Number((s/(a-i)).toPrecision(16)):0,averageBitrate:s?Number((8*o/(a-i)).toPrecision(16)):0}}async isLive(){return await this._backing.getLiveRefreshInterval()!==null}async getLiveRefreshInterval(){return this._backing.getLiveRefreshInterval()}canBePairedWith(t){if(!(t instanceof n))throw new TypeError("other must be an InputTrack.");return this.input!==t.input||this===t?!1:(this._backing.getPairingMask()&t._backing.getPairingMask())!==0n}async getPairableTracks(t){return this.input.getTracks(Tr({filter:e=>e.canBePairedWith(this)},t))}async getPairableVideoTracks(t){return this.input.getVideoTracks(Tr({filter:e=>e.canBePairedWith(this)},t))}async getPairableAudioTracks(t){return this.input.getAudioTracks(Tr({filter:e=>e.canBePairedWith(this)},t))}async getPrimaryPairableVideoTrack(t){return this.input.getPrimaryVideoTrack(Tr({filter:e=>e.canBePairedWith(this)},t))}async getPrimaryPairableAudioTrack(t){return this.input.getPrimaryAudioTrack(Tr({filter:e=>e.canBePairedWith(this)},t))}async hasPairableTrack(t){t&&=Ac(t);let e=await this.input.getTracks();for(let r of e)if(this.canBePairedWith(r)&&(!t||await t(r)))return!0;return!1}hasPairableVideoTrack(t){return t&&=Ac(t),this.hasPairableTrack(async e=>e.isVideoTrack()&&(!t||await t(e)))}hasPairableAudioTrack(t){return t&&=Ac(t),this.hasPairableTrack(async e=>e.isAudioTrack()&&(!t||await t(e)))}},ce=(n,t,e)=>{if(n instanceof Promise)throw new Error(`'${t}' is deprecated and not available synchronously for this track. Use the preferred '${e}()' instead.`);return n},Ac=n=>{if(n!==void 0&&typeof n!="function")throw new TypeError("predicate, when provided, must be a function.");return n?t=>{let e=i=>{if(typeof i!="boolean")throw new TypeError("predicate must return or resolve to a boolean value.");return i},r=n(t);return r instanceof Promise?r.then(e):e(r)}:void 0},$t=class extends Kr{constructor(e,r){super(e,r);this._pixelAspectRatioCache=null;this._backing=r}get type(){return"video"}async getCodec(){return this._backing.getCodec()}get codec(){return ce(this._backing.getCodec(),"codec","getCodec")}async hasOnlyKeyPackets(){return await this._backing.getHasOnlyKeyPackets?.()??!1}async getCodedWidth(){return this._backing.getCodedWidth()}get codedWidth(){return ce(this._backing.getCodedWidth(),"codedWidth","getCodedWidth")}async getCodedHeight(){return this._backing.getCodedHeight()}get codedHeight(){return ce(this._backing.getCodedHeight(),"codedHeight","getCodedHeight")}async getRotation(){return this._backing.getRotation()}get rotation(){return ce(this._backing.getRotation(),"rotation","getRotation")}async getSquarePixelWidth(){return this._backing.getSquarePixelWidth()}get squarePixelWidth(){return ce(this._backing.getSquarePixelWidth(),"squarePixelWidth","getSquarePixelWidth")}async getSquarePixelHeight(){return this._backing.getSquarePixelHeight()}get squarePixelHeight(){return ce(this._backing.getSquarePixelHeight(),"squarePixelHeight","getSquarePixelHeight")}async getPixelAspectRatio(){return this._pixelAspectRatioCache??=Ot({num:await this.getSquarePixelWidth()*await this.getCodedHeight(),den:await this.getSquarePixelHeight()*await this.getCodedWidth()})}get pixelAspectRatio(){return this._pixelAspectRatioCache??=Ot({num:ce(this._backing.getSquarePixelWidth(),"pixelAspectRatio","getPixelAspectRatio")*ce(this._backing.getCodedHeight(),"pixelAspectRatio","getPixelAspectRatio"),den:ce(this._backing.getSquarePixelHeight(),"pixelAspectRatio","getPixelAspectRatio")*ce(this._backing.getCodedWidth(),"pixelAspectRatio","getPixelAspectRatio")})}async getDisplayWidth(){let e=await this._backing.getMetadataDisplayWidth?.();return e??(await this.getRotation()%180===0?this.getSquarePixelWidth():this.getSquarePixelHeight())}get displayWidth(){let e=this._backing.getMetadataDisplayWidth?.();if(e!==void 0){let a=ce(e,"displayWidth","getDisplayWidth");if(a!==null)return a}let i=ce(this._backing.getRotation(),"displayWidth","getDisplayWidth")%180===0?this._backing.getSquarePixelWidth():this._backing.getSquarePixelHeight();return ce(i,"displayWidth","getDisplayWidth")}async getDisplayHeight(){let e=await this._backing.getMetadataDisplayHeight?.();return e??(await this.getRotation()%180===0?this.getSquarePixelHeight():this.getSquarePixelWidth())}get displayHeight(){let e=this._backing.getMetadataDisplayHeight?.();if(e!==void 0){let a=ce(e,"displayHeight","getDisplayHeight");if(a!==null)return a}let i=ce(this._backing.getRotation(),"displayHeight","getDisplayHeight")%180===0?this._backing.getSquarePixelHeight():this._backing.getSquarePixelWidth();return ce(i,"displayHeight","getDisplayHeight")}async getColorSpace(){return this._backing.getColorSpace()}async hasHighDynamicRange(){let e=await this._backing.getColorSpace();return e.primaries==="bt2020"||e.primaries==="smpte432"||e.transfer==="pg"||e.transfer==="hlg"||e.matrix==="bt2020-ncl"}async canBeTransparent(){return this._backing.canBeTransparent()}async getDecoderConfig(){return this._backing.getDecoderConfig()}async getCodecParameterString(){let e=await this._backing.getMetadataCodecParameterString?.();return e??(await this._backing.getDecoderConfig())?.codec??null}async canDecode(){try{let e=await this._backing.getDecoderConfig();if(!e)return!1;let r=await this._backing.getCodec();return h(r!==null),gr.some(a=>a.supports(r,e))?!0:typeof VideoDecoder>"u"?!1:(await VideoDecoder.isConfigSupported(e)).supported===!0}catch(e){return console.error("Error during decodability check:",e),!1}}async determinePacketType(e){if(!(e instanceof Q))throw new TypeError("packet must be an EncodedPacket.");if(e.isMetadataOnly)throw new TypeError("packet must not be metadata-only to determine its type.");let r=await this.getCodec();if(r===null)return null;let i=await this.getDecoderConfig();return h(i),dr(r,i,e.data)}},Yt=class extends Kr{constructor(t,e){super(t,e),this._backing=e}get type(){return"audio"}async getCodec(){return this._backing.getCodec()}get codec(){return ce(this._backing.getCodec(),"codec","getCodec")}async hasOnlyKeyPackets(){return await this._backing.getHasOnlyKeyPackets?.()??!0}async getNumberOfChannels(){return this._backing.getNumberOfChannels()}get numberOfChannels(){return ce(this._backing.getNumberOfChannels(),"numberOfChannels","getNumberOfChannels")}async getSampleRate(){return this._backing.getSampleRate()}get sampleRate(){return ce(this._backing.getSampleRate(),"sampleRate","getSampleRate")}async getDecoderConfig(){return this._backing.getDecoderConfig()}async getCodecParameterString(){let t=await this._backing.getMetadataCodecParameterString?.();return t??(await this._backing.getDecoderConfig())?.codec??null}async canDecode(){try{let t=await this._backing.getDecoderConfig();if(!t)return!1;let e=await this._backing.getCodec();return h(e!==null),br.some(r=>r.supports(e,t))||t.codec.startsWith("pcm-")?!0:typeof AudioDecoder>"u"?!1:(await AudioDecoder.isConfigSupported(t)).supported===!0}catch(t){return console.error("Error during decodability check:",t),!1}}async determinePacketType(t){if(!(t instanceof Q))throw new TypeError("packet must be an EncodedPacket.");return await this.getCodec()===null?null:"key"}},uf=n=>n??1/0,_s=n=>-(n??-1/0),Qr=n=>-n,Ei=n=>{if(typeof n!="object"||!n)throw new TypeError("query must be an object.");if(n.filter!==void 0&&typeof n.filter!="function")throw new TypeError("query.filter, when provided, must be a function.");if(n.sortBy!==void 0&&typeof n.sortBy!="function")throw new TypeError("query.sortBy, when provided, must be a function.");return{filter:n.filter?t=>{let e=i=>{if(typeof i!="boolean")throw new TypeError("query.filter must return or resolve to a boolean.");return i},r=n.filter(t);return r instanceof Promise?r.then(e):e(r)}:void 0,sortBy:n.sortBy?t=>{let e=i=>{if(typeof i!="number"&&(!Array.isArray(i)||!i.every(a=>typeof a=="number")))throw new TypeError("query.sortBy must return or resolve to a number or an array of numbers.");return i},r=n.sortBy(t);return r instanceof Promise?r.then(e):e(r)}:void 0}},Tr=(n,t)=>({filter:n?.filter||t?.filter?e=>{let r=n?.filter?.(e)??!0,i=a=>a===!1?!1:t?.filter?.(e)??!0;return r instanceof Promise?r.then(i):i(r)}:void 0,sortBy:n?.sortBy||t?.sortBy?e=>{let r=n?.sortBy?.(e)??[],i=t?.sortBy?.(e)??[],a=(s,o)=>[...Array.isArray(s)?s:[s],...Array.isArray(o)?o:[o]];return r instanceof Promise||i instanceof Promise?Promise.all([r,i]).then(([s,o])=>a(s,o)):a(r,i)}:void 0}),Rs=async(n,t)=>{let e=n;if(t?.filter){let s=n.map(c=>t.filter(c));if(s.some(c=>c instanceof Promise)){let c=await Promise.all(s);e=n.filter((u,l)=>c[l])}else e=n.filter((c,u)=>s[u])}if(!t?.sortBy)return e;let r=e.map(s=>t.sortBy(s)),a=r.some(s=>s instanceof Promise)?await Promise.all(r):r;return e.map((s,o)=>({track:s,sortValue:a[o]})).sort((s,o)=>{let c=Array.isArray(s.sortValue)?s.sortValue:[s.sortValue],u=Array.isArray(o.sortValue)?o.sortValue:[o.sortValue],l=Math.max(c.length,u.length);for(let m=0;m<l;m++){let d=c[m]??0,f=u[m]??0;if(d!==f)return d-f}return 0}).map(s=>s.track)};ei();var lf=1,zl=2,xc=null;typeof FinalizationRegistry<"u"&&(xc=new FinalizationRegistry(n=>{for(let t of n)t.freed||t.free()}));var Lr=class n extends Ye{constructor(e){super();this._demuxerPromise=null;this._format=null;this._trackBackingsCache=null;this._backingToTrack=new Map;this._disposed=!1;this._nextSourceCacheAge=0;this._sourceRefs=[];this._sourceCache=[];this._sourceCachePromises=[];this._onFormatDetermined=null;if(!e||typeof e!="object")throw new TypeError("options must be an object.");if(!Array.isArray(e.formats)||e.formats.some(r=>!(r instanceof Ve)))throw new TypeError("options.formats must be an array of InputFormat.");if(!(e.source instanceof _e||e.source instanceof Nr))throw new TypeError("options.source must be a Source or SourceRef.");if(e.source instanceof _e&&e.source._disposed)throw new TypeError("options.source must not be a disposed Source.");if(e.initInput!==void 0&&!(e.initInput instanceof n))throw new TypeError("options.initInput, when provided, must be an Input.");e.formatOptions!==void 0&&Ql(e.formatOptions,"formatOptions"),this._formats=e.formats,this._initInput=e.initInput??null,this._formatOptions=e.formatOptions??{},e.source instanceof _e?this._rootRef=e.source.ref():this._rootRef=e.source,this._sourceRefs.push(this._rootRef),xc?.register(this,this._sourceRefs,this)}get disposed(){return this._disposed}get _rootSource(){return this._rootRef.source}async _getSourceUncached(e){h(this._rootSource instanceof Qe);let r=await this._rootSource._resolveRequest(e);return this._emit("source",{source:r.source,request:e,isRoot:e.isRoot}),r}_getSourceCached(e,r=lf){let i=this._sourceCache.find(o=>o.cacheGroup===r&&rc(o.request,e));if(i)return i.age++,Promise.resolve(i.sourceRef.source.ref());let a=this._sourceCachePromises.find(o=>o.cacheGroup===r&&rc(o.request,e));if(a)return a.promise.then(o=>o.sourceRef.source.ref());let s=(async()=>{let o=await this._getSourceUncached(e);if(Fr(this._sourceCache,d=>d.cacheGroup===r&&d.sourceRef.source._refCount===1)>=4){let d=ti(this._sourceCache,p=>p.cacheGroup===r&&p.sourceRef.source._refCount===1?p.age:1/0);h(d!==-1);let f=this._sourceCache[d];this._sourceCache.splice(d,1),f.sourceRef.free(),lo(this._sourceRefs,f.sourceRef)}this._sourceRefs.push(o);let l=this._sourceCachePromises.findIndex(d=>d.request===e);return h(l!==-1),this._sourceCachePromises.splice(l,1),{request:e,sourceRef:o,age:this._nextSourceCacheAge++,cacheGroup:r}})();return this._sourceCachePromises.push({request:e,cacheGroup:r,promise:s}),s.then(o=>{let c=o.sourceRef.source.ref();return this._sourceCache.push(o),c})}_getDemuxer(){return this._demuxerPromise??=(async()=>{this._reader=new hr(this._rootSource),this._emit("source",{source:this._rootSource,request:null,isRoot:!0});for(let e of this._formats)if(await e._canReadInput(this))return this._format=e,this._onFormatDetermined?.(e),e._createDemuxer(this);throw new Rn})()}get source(){return this._rootSource}async getFormat(){return await this._getDemuxer(),h(this._format),this._format}async canRead(){try{return await this._getDemuxer(),!0}catch(e){if(e instanceof Rn)return!1;throw e}}async getFirstTimestamp(e){e??=await this.getTracks();let r=e.filter(a=>a!==null);if(r.length===0)return 0;let i=await Promise.all(r.map(a=>a.getFirstTimestamp()));return Math.min(...i)}async computeDuration(e,r){e??=await this.getTracks();let i=e.filter(s=>s!==null);if(i.length===0)return 0;let a=await Promise.all(i.map(s=>s.computeDuration(r)));return Math.max(...a)}async getDurationFromMetadata(e,r){e??=await this.getTracks();let i=e.filter(o=>o!==null),s=(await Promise.all(i.map(o=>o.getDurationFromMetadata(r)))).filter(o=>o!==null);return s.length===0?null:Math.max(...s)}async getTracks(e){e&&=Ei(e);let i=(await this._getTrackBackings()).map(a=>this._wrapBackingAsTrack(a));return Rs(i,e)}async getVideoTracks(e){e&&=Ei(e);let i=(await this.getTracks()).filter(a=>a.isVideoTrack());return Rs(i,e)}async getAudioTracks(e){e&&=Ei(e);let i=(await this.getTracks()).filter(a=>a.isAudioTrack());return Rs(i,e)}async getPrimaryVideoTrack(e){e&&=Ei(e);let r=Tr(e,{sortBy:async a=>[Qr((await a.getDisposition()).default),Qr(await a.hasPairableAudioTrack()),Qr(!await a.hasOnlyKeyPackets()),_s(await a.getBitrate())]});return(await this.getVideoTracks(r))[0]??null}async getPrimaryAudioTrack(e){e&&=Ei(e);let r=await this.getPrimaryVideoTrack(),i=Tr(e,{sortBy:async s=>[Qr(!r||s.canBePairedWith(r)),Qr((await s.getDisposition()).default),_s(await s.getBitrate())]});return(await this.getAudioTracks(i))[0]??null}async _getTrackBackings(){let e=await this._getDemuxer();return this._trackBackingsCache??=await e.getTrackBackings()}_wrapBackingAsTrack(e){let r=this._backingToTrack.get(e);if(r)return r;let a=e.getType()==="video"?new $t(this,e):new Yt(this,e);return this._backingToTrack.set(e,a),a}async getMimeType(){return(await this._getDemuxer()).getMimeType()}async getMetadataTags(){return(await this._getDemuxer()).getMetadataTags()}dispose(){if(!this._disposed){this._disposed=!0;for(let e of this._sourceRefs)e.free();this._sourceRefs.length=0,xc?.unregister(this),this._demuxerPromise?.then(e=>e.dispose())}}[Symbol.dispose](){this.dispose()}},Rn=class extends Error{constructor(t="Input has an unsupported or unrecognizable format."){super(t),this.name="UnsupportedInputFormatError"}},be=class extends Error{constructor(t="Input has been disposed."){super(t),this.name="InputDisposedError"}};var hr=class{constructor(t){this.source=t}get fileSize(){let t=this.source._getFileSize();if(t===void 0)throw new Error("Reading file size too early; read required first.");return t}get fileSizeNonStrict(){return this.source._getFileSize()??null}requestSlice(t,e){if(this.source._disposed)throw new be;if(t<0||this.fileSizeNonStrict!==null&&t+e>this.fileSizeNonStrict)return null;if(e===0){let a=new Uint8Array(0);return new he(a,V(a),0,t,t)}let r=t+e,i=this.source._read(t,r,ec,tc);return i instanceof Promise?i.then(a=>a?new he(a.bytes,a.view,a.offset,t,r):null):i?new he(i.bytes,i.view,i.offset,t,r):null}requestSliceRange(t,e,r){if(this.source._disposed)throw new be;if(t<0)return null;if(this.fileSizeNonStrict!==null)return this.requestSlice(t,ue(this.fileSizeNonStrict-t,e,r));{let i=this.requestSlice(t,r),a=s=>s||(h(this.fileSizeNonStrict!==null),this.requestSlice(t,ue(this.fileSizeNonStrict-t,e,r)));return i instanceof Promise?i.then(a):a(i)}}requestEntireFile(){if(this.fileSizeNonStrict!==null)return this.requestSlice(0,this.fileSizeNonStrict);let t=1024;return(async()=>{let e=[],r=0;for(;;){if(e.length===1&&this.fileSizeNonStrict!==null)return this.requestSlice(0,this.fileSizeNonStrict);let s=e.length*t,o=this.requestSliceRange(s,0,t);if(o instanceof Promise&&(o=await o),!o)break;e.push(B(o,o.length)),r+=o.length}let i=new Uint8Array(r),a=0;for(let s of e)i.set(s,a),a+=s.length;return new he(i,V(i),0,0,r)})()}},he=class n{constructor(t,e,r,i,a){this.bytes=t;this.view=e;this.offset=r;this.start=i;this.end=a;this.bufferPos=i-r}static tempFromBytes(t){return new n(t,V(t),0,0,t.length)}get length(){return this.end-this.start}get filePos(){return this.offset+this.bufferPos}set filePos(t){this.bufferPos=t-this.offset}get remainingLength(){return Math.max(this.end-this.filePos,0)}skip(t){this.bufferPos+=t}slice(t,e=this.end-t){if(t<this.start||t+e>this.end)throw new RangeError("Slicing outside of original slice.");return new n(this.bytes,this.view,this.offset,t,t+e)}},Ue=(n,t)=>{if(n.filePos<n.start||n.filePos+t>n.end)throw new RangeError(`Tried reading [${n.filePos}, ${n.filePos+t}), but slice is [${n.start}, ${n.end}). This is likely an internal error, please report it alongside the file that caused it.`)},B=(n,t)=>{Ue(n,t);let e=n.bytes.subarray(n.bufferPos,n.bufferPos+t);return n.bufferPos+=t,e},O=n=>(Ue(n,1),n.view.getUint8(n.bufferPos++)),Ti=(n,t)=>{Ue(n,2);let e=n.view.getUint16(n.bufferPos,t);return n.bufferPos+=2,e},se=n=>{Ue(n,2);let t=n.view.getUint16(n.bufferPos,!1);return n.bufferPos+=2,t},ut=n=>{Ue(n,3);let t=Ir(n.view,n.bufferPos,!1);return n.bufferPos+=3,t},un=n=>{Ue(n,2);let t=n.view.getInt16(n.bufferPos,!1);return n.bufferPos+=2,t},Kt=(n,t)=>{Ue(n,4);let e=n.view.getUint32(n.bufferPos,t);return n.bufferPos+=4,e},F=n=>{Ue(n,4);let t=n.view.getUint32(n.bufferPos,!1);return n.bufferPos+=4,t},Dr=n=>{Ue(n,4);let t=n.view.getUint32(n.bufferPos,!0);return n.bufferPos+=4,t},Wt=n=>{Ue(n,4);let t=n.view.getInt32(n.bufferPos,!1);return n.bufferPos+=4,t},df=n=>{Ue(n,4);let t=n.view.getInt32(n.bufferPos,!0);return n.bufferPos+=4,t},Mo=(n,t)=>{let e,r;return t?(e=Kt(n,!0),r=Kt(n,!0)):(r=Kt(n,!1),e=Kt(n,!1)),r*4294967296+e},Be=n=>{let t=F(n),e=F(n);return t*4294967296+e},fl=n=>{let t=Wt(n),e=F(n);return t*4294967296+e},xl=n=>{let t=Dr(n);return df(n)*4294967296+t},yl=n=>{Ue(n,4);let t=n.view.getFloat32(n.bufferPos,!1);return n.bufferPos+=4,t},ka=n=>{Ue(n,8);let t=n.view.getFloat64(n.bufferPos,!1);return n.bufferPos+=8,t},te=(n,t)=>{Ue(n,t);let e="";for(let r=0;r<t;r++)e+=String.fromCharCode(n.bytes[n.bufferPos++]);return e},Xa=(n,t,e)=>me.decode(B(n,t)).split(`
31
+ `).map(a=>a.trim()).filter(a=>a.length>0&&!e?.ignore?.(a));var fn=128,ze=10,_i=["Blues","Classic rock","Country","Dance","Disco","Funk","Grunge","Hip-hop","Jazz","Metal","New age","Oldies","Other","Pop","Rhythm and blues","Rap","Reggae","Rock","Techno","Industrial","Alternative","Ska","Death metal","Pranks","Soundtrack","Euro-techno","Ambient","Trip-hop","Vocal","Jazz & funk","Fusion","Trance","Classical","Instrumental","Acid","House","Game","Sound clip","Gospel","Noise","Alternative rock","Bass","Soul","Punk","Space","Meditative","Instrumental pop","Instrumental rock","Ethnic","Gothic","Darkwave","Techno-industrial","Electronic","Pop-folk","Eurodance","Dream","Southern rock","Comedy","Cult","Gangsta","Top 40","Christian rap","Pop/funk","Jungle music","Native US","Cabaret","New wave","Psychedelic","Rave","Showtunes","Trailer","Lo-fi","Tribal","Acid punk","Acid jazz","Polka","Retro","Musical","Rock 'n' roll","Hard rock","Folk","Folk rock","National folk","Swing","Fast fusion","Bebop","Latin","Revival","Celtic","Bluegrass","Avantgarde","Gothic rock","Progressive rock","Psychedelic rock","Symphonic rock","Slow rock","Big band","Chorus","Easy listening","Acoustic","Humour","Speech","Chanson","Opera","Chamber music","Sonata","Symphony","Booty bass","Primus","Porn groove","Satire","Slow jam","Club","Tango","Samba","Folklore","Ballad","Power ballad","Rhythmic Soul","Freestyle","Duet","Punk rock","Drum solo","A cappella","Euro-house","Dance hall","Goa music","Drum & bass","Club-house","Hardcore techno","Terror","Indie","Britpop","Negerpunk","Polsk punk","Beat","Christian gangsta rap","Heavy metal","Black metal","Crossover","Contemporary Christian","Christian rock","Merengue","Salsa","Thrash metal","Anime","Jpop","Synthpop","Christmas","Art rock","Baroque","Bhangra","Big beat","Breakbeat","Chillout","Downtempo","Dub","EBM","Eclectic","Electro","Electroclash","Emo","Experimental","Garage","Global","IDM","Illbient","Industro-Goth","Jam Band","Krautrock","Leftfield","Lounge","Math rock","New romantic","Nu-breakz","Post-punk","Post-rock","Psytrance","Shoegaze","Space rock","Trop rock","World music","Neoclassical","Audiobook","Audio theatre","Neue Deutsche Welle","Podcast","Indie rock","G-Funk","Dubstep","Garage rock","Psybient"],wl=(n,t)=>{let e=n.filePos;t.raw??={},t.raw.TAG??=B(n,fn-3),n.filePos=e;let r=vi(n,30);r&&(t.title??=r);let i=vi(n,30);i&&(t.artist??=i);let a=vi(n,30);a&&(t.album??=a);let s=vi(n,4),o=Number.parseInt(s,10);Number.isInteger(o)&&o>0&&(t.date??=new Date(o,0,1));let c=B(n,30),u;if(c[28]===0&&c[29]!==0){let m=c[29];m>0&&(t.trackNumber??=m),n.skip(-30),u=vi(n,28),n.skip(2)}else n.skip(-30),u=vi(n,30);u&&(t.comment??=u);let l=O(n);l<_i.length&&(t.genre??=_i[l])},vi=(n,t)=>{let e=B(n,t),r=_r(e.indexOf(0),e.length),i=e.subarray(0,r),a="";for(let s=0;s<i.length;s++)a+=String.fromCharCode(i[s]);return a.trimEnd()},ft=n=>{let t=n.filePos,e=te(n,3),r=O(n),i=O(n),a=O(n),s=F(n);if(e!=="ID3"||r===255||i===255||(s&2155905152)!==0)return n.filePos=t,null;let o=Zn(s);return{majorVersion:r,revision:i,flags:a,size:o}},bi=(n,t,e)=>{if(![2,3,4].includes(t.majorVersion)){console.warn(`Unsupported ID3v2 major version: ${t.majorVersion}`);return}let r=B(n,t.size),i=new Pc(t,r);if(t.flags&16&&i.removeFooter(),t.flags&128&&t.majorVersion===3&&i.ununsynchronizeAll(),t.flags&64){let a=i.readU32();t.majorVersion===3?i.pos+=a:i.pos+=a-4}for(;i.pos<=i.bytes.length-i.frameHeaderSize();){let a=i.readId3V2Frame();if(!a)break;let s=i.pos,o=i.pos+a.size,c=!1,u=!1,l=!1;if(t.majorVersion===3?(c=!!(a.flags&64),u=!!(a.flags&128)):t.majorVersion===4&&(c=!!(a.flags&4),u=!!(a.flags&8),l=!!(a.flags&2)||!!(t.flags&128)),c){console.warn(`Skipping encrypted ID3v2 frame ${a.id}`),i.pos=o;continue}if(u){console.warn(`Skipping compressed ID3v2 frame ${a.id}`),i.pos=o;continue}if(l&&i.ununsynchronizeRegion(i.pos,o),e.raw??={},a.id==="TXXX"){let m=e.raw.TXXX??={},d=i.readId3V2TextEncoding(),f=i.readId3V2Text(d,o),p=i.readId3V2Text(d,o);m[f]??=p}else a.id[0]==="T"?e.raw[a.id]??=i.readId3V2EncodingAndText(o):e.raw[a.id]??=i.readBytes(a.size);switch(i.pos=s,a.id){case"TIT2":case"TT2":e.title??=i.readId3V2EncodingAndText(o);break;case"TIT3":case"TT3":e.description??=i.readId3V2EncodingAndText(o);break;case"TPE1":case"TP1":e.artist??=i.readId3V2EncodingAndText(o);break;case"TALB":case"TAL":e.album??=i.readId3V2EncodingAndText(o);break;case"TPE2":case"TP2":e.albumArtist??=i.readId3V2EncodingAndText(o);break;case"TRCK":case"TRK":{let d=i.readId3V2EncodingAndText(o).split("/"),f=Number.parseInt(d[0],10),p=d[1]&&Number.parseInt(d[1],10);Number.isInteger(f)&&f>0&&(e.trackNumber??=f),p&&Number.isInteger(p)&&p>0&&(e.tracksTotal??=p)}break;case"TPOS":case"TPA":{let d=i.readId3V2EncodingAndText(o).split("/"),f=Number.parseInt(d[0],10),p=d[1]&&Number.parseInt(d[1],10);Number.isInteger(f)&&f>0&&(e.discNumber??=f),p&&Number.isInteger(p)&&p>0&&(e.discsTotal??=p)}break;case"TCON":case"TCO":{let m=i.readId3V2EncodingAndText(o),d=/^\((\d+)\)/.exec(m);if(d){let f=Number.parseInt(d[1]);if(_i[f]!==void 0){e.genre??=_i[f];break}}if(d=/^\d+$/.exec(m),d){let f=Number.parseInt(d[0]);if(_i[f]!==void 0){e.genre??=_i[f];break}}e.genre??=m}break;case"TDRC":case"TDAT":{let m=i.readId3V2EncodingAndText(o),d=new Date(m);Number.isNaN(d.getTime())||(e.date??=d)}break;case"TYER":case"TYE":{let m=i.readId3V2EncodingAndText(o),d=Number.parseInt(m,10);Number.isInteger(d)&&(e.date??=new Date(d,0,1))}break;case"USLT":case"ULT":{let m=i.readU8();i.pos+=3,i.readId3V2Text(m,o),e.lyrics??=i.readId3V2Text(m,o)}break;case"COMM":case"COM":{let m=i.readU8();i.pos+=3,i.readId3V2Text(m,o),e.comment??=i.readId3V2Text(m,o)}break;case"APIC":case"PIC":{let m=i.readId3V2TextEncoding(),d;if(t.majorVersion===2){let g=i.readAscii(3);d=g==="PNG"?"image/png":g==="JPG"?"image/jpeg":"image/*"}else d=i.readId3V2Text(m,o);let f=i.readU8(),p=i.readId3V2Text(m,o).trimEnd(),b=o-i.pos;if(b>=0){let g=i.readBytes(b);e.images||(e.images=[]),e.images.push({data:g,mimeType:d,kind:f===3?"coverFront":f===4?"coverBack":"unknown",description:p})}}break;default:i.pos+=a.size;break}i.pos=o}},Pc=class{constructor(t,e){this.header=t;this.bytes=e;this.pos=0;this.view=new DataView(e.buffer,e.byteOffset,e.byteLength)}frameHeaderSize(){return this.header.majorVersion===2?6:10}ununsynchronizeAll(){let t=[];for(let e=0;e<this.bytes.length;e++){let r=this.bytes[e];t.push(r),r===255&&e!==this.bytes.length-1&&this.bytes[e]===0&&e++}this.bytes=new Uint8Array(t),this.view=new DataView(this.bytes.buffer)}ununsynchronizeRegion(t,e){let r=[];for(let s=t;s<e;s++){let o=this.bytes[s];r.push(o),o===255&&s!==e-1&&this.bytes[s+1]===0&&s++}let i=this.bytes.subarray(0,t),a=this.bytes.subarray(e);this.bytes=new Uint8Array(i.length+r.length+a.length),this.bytes.set(i,0),this.bytes.set(r,i.length),this.bytes.set(a,i.length+r.length),this.view=new DataView(this.bytes.buffer)}removeFooter(){this.bytes=this.bytes.subarray(0,this.bytes.length-ze),this.view=new DataView(this.bytes.buffer)}readBytes(t){let e=this.bytes.subarray(this.pos,this.pos+t);return this.pos+=t,e}readU8(){let t=this.view.getUint8(this.pos);return this.pos+=1,t}readU16(){let t=this.view.getUint16(this.pos,!1);return this.pos+=2,t}readU24(){let t=this.view.getUint16(this.pos,!1),e=this.view.getUint8(this.pos+2);return this.pos+=3,t*256+e}readU32(){let t=this.view.getUint32(this.pos,!1);return this.pos+=4,t}readAscii(t){let e="";for(let r=0;r<t;r++)e+=String.fromCharCode(this.view.getUint8(this.pos+r));return this.pos+=t,e}readId3V2Frame(){if(this.header.majorVersion===2){let t=this.readAscii(3);if(t==="\0\0\0")return null;let e=this.readU24();return{id:t,size:e,flags:0}}else{let t=this.readAscii(4);if(t==="\0\0\0\0")return null;let e=this.readU32(),r=this.header.majorVersion===4?Zn(e):e,i=this.readU16(),a=this.pos,s=o=>{let c=this.pos+o;if(c>this.bytes.length)return!1;if(c<=this.bytes.length-this.frameHeaderSize()){this.pos+=o;let u=this.readAscii(4);if(u!=="\0\0\0\0"&&!/[0-9A-Z]{4}/.test(u))return!1}return!0};if(!s(r)){let o=this.header.majorVersion===4?e:Zn(e);s(o)&&(r=o)}return this.pos=a,{id:t,size:r,flags:i}}}readId3V2TextEncoding(){let t=this.readU8();if(t>3)throw new Error(`Unsupported text encoding: ${t}`);return t}readId3V2Text(t,e){let r=this.pos,i=this.readBytes(e-this.pos);switch(t){case 0:{let a="";for(let s=0;s<i.length;s++){let o=i[s];if(o===0){this.pos=r+s+1;break}a+=String.fromCharCode(o)}return a}case 1:if(i[0]===255&&i[1]===254){let a=new TextDecoder("utf-16le"),s=_r(i.findIndex((o,c)=>o===0&&i[c+1]===0&&c%2===0),i.length);return this.pos=r+Math.min(s+2,i.length),a.decode(i.subarray(2,s))}else if(i[0]===254&&i[1]===255){let a=new TextDecoder("utf-16be"),s=_r(i.findIndex((o,c)=>o===0&&i[c+1]===0&&c%2===0),i.length);return this.pos=r+Math.min(s+2,i.length),a.decode(i.subarray(2,s))}else{let a=_r(i.findIndex(s=>s===0),i.length);return this.pos=r+Math.min(a+1,i.length),me.decode(i.subarray(0,a))}case 2:{let a=new TextDecoder("utf-16be"),s=_r(i.findIndex((o,c)=>o===0&&i[c+1]===0&&c%2===0),i.length);return this.pos=r+Math.min(s+2,i.length),a.decode(i.subarray(0,s))}case 3:{let a=_r(i.findIndex(s=>s===0),i.length);return this.pos=r+Math.min(a+1,i.length),me.decode(i.subarray(0,a))}}}readId3V2EncodingAndText(t){if(this.pos>=t)return"";let e=this.readId3V2TextEncoding();return this.readId3V2Text(e,t)}},wr=class{constructor(t){this.helper=new Uint8Array(8);this.helperView=V(this.helper);this.writer=t}writeId3V2Tag(t){let e=this.writer.getPos();this.writeAscii("ID3"),this.writeU8(4),this.writeU8(0),this.writeU8(0),this.writeSynchsafeU32(0);let r=this.writer.getPos(),i=new Set;for(let{key:o,value:c}of it(t))switch(o){case"title":this.writeId3V2TextFrame("TIT2",c),i.add("TIT2");break;case"description":this.writeId3V2TextFrame("TIT3",c),i.add("TIT3");break;case"artist":this.writeId3V2TextFrame("TPE1",c),i.add("TPE1");break;case"album":this.writeId3V2TextFrame("TALB",c),i.add("TALB");break;case"albumArtist":this.writeId3V2TextFrame("TPE2",c),i.add("TPE2");break;case"trackNumber":{let u=t.tracksTotal!==void 0?`${c}/${t.tracksTotal}`:c.toString();this.writeId3V2TextFrame("TRCK",u),i.add("TRCK")}break;case"discNumber":{let u=t.discsTotal!==void 0?`${c}/${t.discsTotal}`:c.toString();this.writeId3V2TextFrame("TPOS",u),i.add("TPOS")}break;case"genre":this.writeId3V2TextFrame("TCON",c),i.add("TCON");break;case"date":this.writeId3V2TextFrame("TDRC",c.toISOString().slice(0,10)),i.add("TDRC");break;case"lyrics":this.writeId3V2LyricsFrame(c),i.add("USLT");break;case"comment":this.writeId3V2CommentFrame(c),i.add("COMM");break;case"images":{let u={coverFront:3,coverBack:4,unknown:0};for(let l of c){let m=u[l.kind]??0,d=l.description??"";this.writeId3V2ApicFrame(l.mimeType,m,d,l.data)}}break;case"tracksTotal":case"discsTotal":break;case"raw":break;default:ne(o)}if(t.raw)for(let o in t.raw){let c=t.raw[o];if(c==null||o.length!==4||i.has(o))continue;let u;if(typeof c=="string")if(Je(c)){u=new Uint8Array(c.length+2),u[0]=0;for(let m=0;m<c.length;m++)u[m+1]=c.charCodeAt(m)}else{let m=$.encode(c);u=new Uint8Array(m.byteLength+2),u[0]=3,u.set(m,1)}else if(c instanceof Uint8Array)u=c;else if(o==="TXXX"&&Hn(c)){for(let l in c){let m=c[l],d=Je(l)&&Je(m),f=d?null:$.encode(l),p=d?null:$.encode(m),b=d?l.length:f.byteLength,g=d?m.length:p.byteLength,y=1+b+1+g+1;this.writeAscii("TXXX"),this.writeSynchsafeU32(y),this.writeU16(0),this.writeU8(d?0:3),d?(this.writeIsoString(l),this.writeIsoString(m)):(this.writer.write(f),this.writeU8(0),this.writer.write(p),this.writeU8(0))}continue}else continue;this.writeAscii(o),this.writeSynchsafeU32(u.byteLength),this.writeU16(0),this.writer.write(u)}let a=this.writer.getPos(),s=a-r;return this.writer.seek(e+6),this.writeSynchsafeU32(s),this.writer.seek(a),s+10}writeU8(t){this.helper[0]=t,this.writer.write(this.helper.subarray(0,1))}writeU16(t){this.helperView.setUint16(0,t,!1),this.writer.write(this.helper.subarray(0,2))}writeU32(t){this.helperView.setUint32(0,t,!1),this.writer.write(this.helper.subarray(0,4))}writeAscii(t){for(let e=0;e<t.length;e++)this.helper[e]=t.charCodeAt(e);this.writer.write(this.helper.subarray(0,t.length))}writeSynchsafeU32(t){this.writeU32(Hu(t))}writeIsoString(t){let e=new Uint8Array(t.length+1);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r);this.writer.write(e)}writeUtf8String(t){let e=$.encode(t);this.writer.write(e),this.writeU8(0)}writeId3V2TextFrame(t,e){let r=Je(e),a=1+(r?e.length:$.encode(e).byteLength)+1;this.writeAscii(t),this.writeSynchsafeU32(a),this.writeU16(0),this.writeU8(r?0:3),r?this.writeIsoString(e):this.writeUtf8String(e)}writeId3V2LyricsFrame(t){let e=Je(t),r="",i=4+r.length+1+t.length+1;this.writeAscii("USLT"),this.writeSynchsafeU32(i),this.writeU16(0),this.writeU8(e?0:3),this.writeAscii("und"),e?(this.writeIsoString(r),this.writeIsoString(t)):(this.writeUtf8String(r),this.writeUtf8String(t))}writeId3V2CommentFrame(t){let e=Je(t),r=e?t.length:$.encode(t).byteLength,i="",a=4+i.length+1+r+1;this.writeAscii("COMM"),this.writeSynchsafeU32(a),this.writeU16(0),this.writeU8(e?0:3),this.writeU8(117),this.writeU8(110),this.writeU8(100),e?(this.writeIsoString(i),this.writeIsoString(t)):(this.writeUtf8String(i),this.writeUtf8String(t))}writeId3V2ApicFrame(t,e,r,i){let a=Je(t)&&Je(r),s=a?r.length:$.encode(r).byteLength,o=1+t.length+1+1+s+1+i.byteLength;this.writeAscii("APIC"),this.writeSynchsafeU32(o),this.writeU16(0),this.writeU8(a?0:3),a?this.writeIsoString(t):this.writeUtf8String(t),this.writeU8(e),a?this.writeIsoString(r):this.writeUtf8String(r),this.writer.write(i)}};var ge=class{constructor(t){this.mutex=new Ie;this.trackTimestampInfo=new WeakMap;this.output=t}onTrackClose(t){}validateTimestamp(t,e,r){if(e<0)throw new Error(`Timestamps must be non-negative (got ${e}s).`);let i=this.trackTimestampInfo.get(t);if(i){if(r&&(i.maxTimestampBeforeLastKeyPacket=i.maxTimestamp),i.maxTimestampBeforeLastKeyPacket!==null&&e<i.maxTimestampBeforeLastKeyPacket)throw new Error(`Timestamps cannot be smaller than the largest timestamp of the previous GOP (a GOP begins with a key packet and ends right before the next key packet). Got ${e}s, but largest timestamp is ${i.maxTimestampBeforeLastKeyPacket}s.`);i.maxTimestamp=Math.max(i.maxTimestamp,e)}else{if(!r)throw new Error("First packet must be a key packet.");i={maxTimestamp:e,maxTimestampBeforeLastKeyPacket:null},this.trackTimestampInfo.set(t,i)}}};var Fs=class extends ge{constructor(e,r){super(e);this.header=null;this.headerBitstream=null;this.inputIsAdts=null;this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!0),Vt(this.output._metadataTags)||new wr(this.writer).writeId3V2Tag(this.output._metadataTags),e()}async getMimeType(){return"audio/aac"}async addEncodedVideoPacket(){throw new Error("ADTS does not support video.")}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{if(this.validateTimestamp(e,r.timestamp,r.type==="key"),this.inputIsAdts===null){Ee(i);let s=i?.decoderConfig?.description;if(this.inputIsAdts=!s,!this.inputIsAdts){let o=Tt(j(s)),c=qn(o);this.header=c.header,this.headerBitstream=c.bitstream}}if(this.inputIsAdts){let s=this.writer.getPos();this.writer.write(r.data),this.format._options.onFrame&&this.format._options.onFrame(r.data,s)}else{h(this.header);let s=r.data.byteLength+this.header.byteLength;Gn(this.headerBitstream,s);let o=this.writer.getPos();if(this.writer.write(this.header),this.writer.write(r.data),this.format._options.onFrame){let c=new Uint8Array(s);c.set(this.header,0),c.set(r.data,this.header.byteLength),this.format._options.onFrame(c,o)}}await this.writer.flush()}finally{a()}}async addSubtitleCue(){throw new Error("ADTS does not support subtitles.")}async finalize(){(await this.mutex.acquire())()}};var fd=new Uint8Array([102,76,97,67]),mf=38,ff=34,Ms=class extends ge{constructor(e,r){super(e);this.metadataWritten=!1;this.blockSizes=[];this.frameSizes=[];this.sampleRate=null;this.channels=null;this.bitsPerSample=null;this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!!this.format._options.appendOnly),this.writer.write(fd),e()}writeHeader({bitsPerSample:e,minimumBlockSize:r,maximumBlockSize:i,minimumFrameSize:a,maximumFrameSize:s,sampleRate:o,channels:c,totalSamples:u}){h(this.writer.getPos()===4);let l=!Vt(this.output._metadataTags),m=new W(new Uint8Array(4));m.writeBits(1,+!l),m.writeBits(7,0),m.writeBits(24,ff),this.writer.write(m.bytes);let d=new W(new Uint8Array(18));if(d.writeBits(16,r),d.writeBits(16,i),d.writeBits(24,a),d.writeBits(24,s),d.writeBits(20,o),d.writeBits(3,c-1),d.writeBits(5,e-1),u>=2**32)throw new Error("This muxer only supports writing up to 2 ** 32 samples");d.writeBits(4,0),d.writeBits(32,u),this.writer.write(d.bytes),this.writer.write(new Uint8Array(16))}writePictureBlock(e){let r=32+e.mimeType.length+(e.description?.length??0)+e.data.length,i=new Uint8Array(r),a=0,s=V(i);s.setUint32(a,e.kind==="coverFront"?3:e.kind==="coverBack"?4:0),a+=4,s.setUint32(a,e.mimeType.length),a+=4,i.set($.encode(e.mimeType),8),a+=e.mimeType.length,s.setUint32(a,e.description?.length??0),a+=4,i.set($.encode(e.description??""),a),a+=e.description?.length??0,a+=16,s.setUint32(a,e.data.length),a+=4,i.set(e.data,a),a+=e.data.length,h(a===r);let o=new W(new Uint8Array(4));o.writeBits(1,0),o.writeBits(7,6),o.writeBits(24,r),this.writer.write(o.bytes),this.writer.write(i)}writeVorbisCommentAndPictureBlock(){if(this.format._options.appendOnly||this.writer.seek(mf+fd.byteLength),Vt(this.output._metadataTags)){this.metadataWritten=!0;return}let e=this.output._metadataTags.images??[];for(let a of e)this.writePictureBlock(a);let r=nn(new Uint8Array(0),this.output._metadataTags,!1),i=new W(new Uint8Array(4));i.writeBits(1,1),i.writeBits(7,4),i.writeBits(24,r.length),this.writer.write(i.bytes),this.writer.write(r),this.metadataWritten=!0}async getMimeType(){return"audio/flac"}async addEncodedVideoPacket(){throw new Error("FLAC does not support video.")}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{if(this.validateTimestamp(e,r.timestamp,r.type==="key"),this.sampleRate===null){Ee(i),h(i),h(i.decoderConfig),h(i.decoderConfig.description),this.sampleRate=i.decoderConfig.sampleRate,this.channels=i.decoderConfig.numberOfChannels;let d=new W(j(i.decoderConfig.description));d.skipBits(167);let f=d.readBits(5)+1;this.bitsPerSample=f,this.format._options.appendOnly&&this.writeHeader({minimumBlockSize:16,maximumBlockSize:65535,minimumFrameSize:0,maximumFrameSize:0,sampleRate:this.sampleRate,channels:this.channels,bitsPerSample:this.bitsPerSample,totalSamples:0})}this.metadataWritten||this.writeVorbisCommentAndPictureBlock();let s=he.tempFromBytes(r.data);s.skip(2);let o=B(s,2),c=new W(o),u=Oa(c.readBits(4));if(u===null)throw new Error("Invalid FLAC frame: Invalid block size.");Da(s);let l=Va(s,u);this.format._options.appendOnly||(this.blockSizes.push(l),this.frameSizes.push(r.data.length));let m=this.writer.getPos();this.writer.write(r.data),this.format._options.onFrame&&this.format._options.onFrame(r.data,m),await this.writer.flush()}finally{a()}}addSubtitleCue(){throw new Error("FLAC does not support subtitles.")}async finalize(){let e=await this.mutex.acquire();if(!this.format._options.appendOnly){let r=1/0,i=0,a=1/0,s=0,o=0;for(let c=0;c<this.blockSizes.length;c++)a=Math.min(a,this.frameSizes[c]),s=Math.max(s,this.frameSizes[c]),i=Math.max(i,this.blockSizes[c]),o+=this.blockSizes[c],c!==this.blockSizes.length-1&&(r=Math.min(r,this.blockSizes[c]));h(this.sampleRate!==null),h(this.channels!==null),h(this.bitsPerSample!==null),this.writer.seek(4),this.writeHeader({minimumBlockSize:r,maximumBlockSize:i,minimumFrameSize:a,maximumFrameSize:s,sampleRate:this.sampleRate,channels:this.channels,bitsPerSample:this.bitsPerSample,totalSamples:o})}e()}};var Fn=/(?:(.+?)\n)?((?:\d{2}:)?\d{2}:\d{2}.\d{3})\s+-->\s+((?:\d{2}:)?\d{2}:\d{2}.\d{3})/g,pf=/^WEBVTT(.|\n)*?\n{2}/,Ri=/<(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})>/g,Bs=class{constructor(t){this.preambleText=null;this.preambleEmitted=!1;this.options=t}parse(t){t=t.replaceAll(`\r
32
32
  `,`
33
33
  `).replaceAll("\r",`
34
34
  `),Fn.lastIndex=0;let e;if(!this.preambleText){if(!pf.test(t))throw new Error("WebVTT preamble incorrect.");e=Fn.exec(t);let r=t.slice(0,e?.index??t.length).trimEnd();if(!r)throw new Error("No WebVTT preamble provided.");this.preambleText=r,e&&(t=t.slice(e.index),Fn.lastIndex=0)}for(;e=Fn.exec(t);){let r=t.slice(0,e.index),i=e[1],a=e.index+e[0].length,s=t.indexOf(`
35
35
  `,a)+1,o=t.slice(a,s).trim(),c=t.indexOf(`
36
36
 
37
- `,a);c===-1&&(c=t.length);let u=Os(e[2]),m=Os(e[3])-u,d=t.slice(s,c).trim();t=t.slice(c).trimStart(),Fn.lastIndex=0;let f={timestamp:u/1e3,duration:m/1e3,text:d,identifier:i,settings:o,notes:r},p={};this.preambleEmitted||(p.config={description:this.preambleText},this.preambleEmitted=!0),this.options.output(f,p)}}},hf=/(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})/,Os=n=>{let t=hf.exec(n);if(!t)throw new Error("Expected match.");return 60*60*1e3*Number(t[1]||"0")+60*1e3*Number(t[2])+1e3*Number(t[3])+Number(t[4])},Ds=n=>{let t=Math.floor(n/36e5),e=Math.floor(n%(60*60*1e3)/(60*1e3)),r=Math.floor(n%(60*1e3)/1e3),i=n%1e3;return t.toString().padStart(2,"0")+":"+e.toString().padStart(2,"0")+":"+r.toString().padStart(2,"0")+"."+i.toString().padStart(3,"0")};var Qr=class{constructor(t){this.writer=t;this.helper=new Uint8Array(8);this.helperView=new DataView(this.helper.buffer);this.offsets=new WeakMap}writeU32(t){this.helperView.setUint32(0,t,!1),this.writer.write(this.helper.subarray(0,4))}writeU64(t){this.helperView.setUint32(0,Math.floor(t/2**32),!1),this.helperView.setUint32(4,t,!1),this.writer.write(this.helper.subarray(0,8))}writeAscii(t){for(let e=0;e<t.length;e++)this.helperView.setUint8(e%8,t.charCodeAt(e)),e%8===7&&this.writer.write(this.helper);t.length%8!==0&&this.writer.write(this.helper.subarray(0,t.length%8))}writeBox(t){if(this.offsets.set(t,this.writer.getPos()),t.contents&&!t.children)this.writeBoxHeader(t,t.size??t.contents.byteLength+8),this.writer.write(t.contents);else{let e=this.writer.getPos();if(this.writeBoxHeader(t,0),t.contents&&this.writer.write(t.contents),t.children)for(let a of t.children)a&&this.writeBox(a);let r=this.writer.getPos(),i=t.size??r-e;this.writer.seek(e),this.writeBoxHeader(t,i),this.writer.seek(r)}}writeBoxHeader(t,e){this.writeU32(t.largeSize?1:e),this.writeAscii(t.type),t.largeSize&&this.writeU64(e)}measureBoxHeader(t){return 8+(t.largeSize?8:0)}patchBox(t){let e=this.offsets.get(t);h(e!==void 0);let r=this.writer.getPos();this.writer.seek(e),this.writeBox(t),this.writer.seek(r)}measureBox(t){if(t.contents&&!t.children)return this.measureBoxHeader(t)+t.contents.byteLength;{let e=this.measureBoxHeader(t);if(t.contents&&(e+=t.contents.byteLength),t.children)for(let r of t.children)r&&(e+=this.measureBox(r));return e}}},K=new Uint8Array(8),Xe=new DataView(K.buffer),le=n=>[(n%256+256)%256],L=n=>(Xe.setUint16(0,n,!1),[K[0],K[1]]),vc=n=>(Xe.setInt16(0,n,!1),[K[0],K[1]]),gd=n=>(Xe.setUint32(0,n,!1),[K[1],K[2],K[3]]),_=n=>(Xe.setUint32(0,n,!1),[K[0],K[1],K[2],K[3]]),Zt=n=>(Xe.setInt32(0,n,!1),[K[0],K[1],K[2],K[3]]),It=n=>(Xe.setUint32(0,Math.floor(n/2**32),!1),Xe.setUint32(4,n,!1),[K[0],K[1],K[2],K[3],K[4],K[5],K[6],K[7]]),gf=n=>(Xe.setInt32(0,Math.floor(n/2**32),!1),Xe.setUint32(4,n,!1),[K[0],K[1],K[2],K[3],K[4],K[5],K[6],K[7]]),bd=n=>(Xe.setInt16(0,2**8*n,!1),[K[0],K[1]]),ht=n=>(Xe.setInt32(0,2**16*n,!1),[K[0],K[1],K[2],K[3]]),Cc=n=>(Xe.setInt32(0,2**30*n,!1),[K[0],K[1],K[2],K[3]]),Ic=(n,t)=>{let e=[],r=n;do{let i=r&127;r>>=7,e.length>0&&(i|=128),e.push(i),t!==void 0&&t--}while(r>0||t);return e.reverse()},ie=(n,t=!1)=>{let e=Array(n.length).fill(null).map((r,i)=>n.charCodeAt(i));return t&&e.push(0),e},yd=n=>{let t=n*(Math.PI/180),e=Math.round(Math.cos(t)),r=Math.round(Math.sin(t));return[e,r,0,-r,e,0,0,0,1]},kd=yd(0),Td=n=>[ht(n[0]),ht(n[1]),Cc(n[2]),ht(n[3]),ht(n[4]),Cc(n[5]),ht(n[6]),ht(n[7]),Cc(n[8])],N=(n,t,e)=>({type:n,contents:t&&new Uint8Array(t.flat(10)),children:e}),Y=(n,t,e,r,i)=>N(n,[le(t),gd(e),r??[]],i),wd=n=>n.isQuickTime?N("ftyp",[ie("qt "),_(512),ie("qt ")]):n.fragmented?n.cmaf?N("ftyp",[ie("iso5"),_(512),ie("iso5"),ie("iso6"),ie("mp41"),ie("cmfc"),ie("dash")]):N("ftyp",[ie("iso5"),_(512),ie("iso5"),ie("iso6"),ie("mp41")]):N("ftyp",[ie("isom"),_(512),ie("isom"),n.holdsAvc?ie("avc1"):[],ie("mp41")]),_c=()=>N("styp",[ie("iso5"),_(0),ie("iso5"),ie("iso6"),ie("mp41"),ie("cmfc"),ie("dash")]),Rc=(n,t)=>{let e=n.maxWrittenEndTimestamp-n.minWrittenTimestamp;return Number.isFinite(e)||(e=0),Y("sidx",1,0,[_(1),_(je),It(oe(n.minWrittenTimestamp,je)),It(0),L(0),L(1),_(t&2147483647),_(oe(e,je)),_(0)])},Mn=n=>({type:"mdat",largeSize:n}),Sd=n=>({type:"free",size:n}),Fi=n=>N("moov",void 0,[bf(n.creationTime,n.trackDatas),...n.trackDatas.map(t=>yf(t,n.creationTime)),n.isFragmented?ip(n.trackDatas):null,mp(n)]),bf=(n,t)=>{let e=Math.max(0,...t.map(s=>oe(Vs(s),je)+oe(s.startTimestampOffset??0,je))),r=Math.max(0,...t.map(s=>s.track.id))+1,i=!_t(n)||!_t(e),a=i?It:_;return Y("mvhd",+i,0,[a(n),a(n),_(je),a(e),ht(1),bd(1),Array(10).fill(0),Td(kd),Array(24).fill(0),_(r)])},Vs=n=>{if(n.samples.length===0)return 0;let t=1/0,e=-1/0;for(let r=0;r<n.samples.length;r++){let i=n.samples[r];i.timestamp<t&&(t=i.timestamp),i.timestamp+i.duration>e&&(e=i.timestamp+i.duration)}return t===1/0?0:e-t},yf=(n,t)=>{let e=Rd(n),r=n.startTimestampOffset!==null&&n.startTimestampOffset>0;return N("trak",void 0,[kf(n,t),r?Tf(n,n.startTimestampOffset):null,wf(n,t),e.name!==void 0?N("udta",void 0,[N("name",[...$.encode(e.name)])]):null])},kf=(n,t)=>{let e=oe(Vs(n),je)+oe(n.startTimestampOffset??0,je),r=!_t(t)||!_t(e),i=r?It:_,a;if(n.type==="video"){let o=n.track.metadata.rotation;a=yd(o??0)}else a=kd;let s=2;return n.track.metadata.disposition?.default!==!1&&(s|=1),Y("tkhd",+r,s,[i(t),i(t),_(n.track.id),_(0),i(e),Array(8).fill(0),L(0),L(n.track.id),bd(n.type==="audio"?1:0),L(0),Td(a),ht(n.type==="video"?n.info.width:0),ht(n.type==="video"?n.info.height:0)])},Tf=(n,t)=>{let e=oe(t,je),r=oe(Vs(n),je),i=!_t(e)||!_t(r),a=i?It:_,s=i?gf:Zt;return N("edts",void 0,[Y("elst",i?1:0,0,[_(2),a(e),s(-1),ht(1),a(r),s(0),ht(1)])])},wf=(n,t)=>N("mdia",void 0,[Sf(n,t),Fc(!0,Af[n.type],xf[n.type]),Pf(n)]),Sf=(n,t)=>{let e=oe(Vs(n),n.timescale),r=!_t(t)||!_t(e),i=r?It:_;return Y("mdhd",+r,0,[i(t),i(t),_(n.timescale),i(e),L(_d(n.track.metadata.languageCode??J)),L(0)])},Af={video:"vide",audio:"soun",subtitle:"text"},xf={video:"MediabunnyVideoHandler",audio:"MediabunnySoundHandler",subtitle:"MediabunnyTextHandler"},Fc=(n,t,e,r="\0\0\0\0")=>Y("hdlr",0,0,[n?ie("mhlr"):_(0),ie(t),ie(r),_(0),_(0),ie(e,!0)]),Pf=n=>N("minf",void 0,[vf[n.type](),_f(),Mf(n)]),Cf=()=>Y("vmhd",0,1,[L(0),L(0),L(0),L(0)]),If=()=>Y("smhd",0,0,[L(0),L(0)]),Ef=()=>Y("nmhd",0,0),vf={video:Cf,audio:If,subtitle:Ef},_f=()=>N("dinf",void 0,[Rf()]),Rf=()=>Y("dref",0,0,[_(1)],[Ff()]),Ff=()=>Y("url ",0,1),Mf=n=>{let t=n.compositionTimeOffsetTable.length>1||n.compositionTimeOffsetTable.some(e=>e.sampleCompositionTimeOffset!==0);return N("stbl",void 0,[Bf(n),$f(n),t?tp(n):null,t?rp(n):null,Zf(n),Jf(n),ep(n),Yf(n)])},Bf=n=>{let t;if(n.type==="video")t=Of(gp(n.track.source._codec,n.info.decoderConfig.codec),n);else if(n.type==="audio"){let e=vd(n.track.source._codec,n.muxer.isQuickTime);h(e),t=Lf(e,n)}else n.type==="subtitle"&&(t=jf(kp[n.track.source._codec],n));return h(t),Y("stsd",0,0,[_(1)],[t])},Of=(n,t)=>N(n,[Array(6).fill(0),L(1),L(0),L(0),Array(12).fill(0),L(t.info.width),L(t.info.height),_(4718592),_(4718592),_(0),L(1),Array(32).fill(0),L(24),vc(65535)],[bp[t.track.source._codec](t),Df(t),zn(t.info.decoderConfig.colorSpace)?Vf(t):null]),Df=n=>n.info.pixelAspectRatio.num===n.info.pixelAspectRatio.den?null:N("pasp",[_(n.info.pixelAspectRatio.num),_(n.info.pixelAspectRatio.den)]),Vf=n=>N("colr",[ie("nclx"),L(et[n.info.decoderConfig.colorSpace.primaries]),L(tt[n.info.decoderConfig.colorSpace.transfer]),L(rt[n.info.decoderConfig.colorSpace.matrix]),le((n.info.decoderConfig.colorSpace.fullRange?1:0)<<7)]),Uf=n=>n.info.decoderConfig&&N("avcC",[...j(n.info.decoderConfig.description)]),zf=n=>n.info.decoderConfig&&N("hvcC",[...j(n.info.decoderConfig.description)]),pd=n=>{if(!n.info.decoderConfig)return null;let t=n.info.decoderConfig,e=t.codec.split("."),r=Number(e[1]),i=Number(e[2]),a=Number(e[3]),s=e[4]?Number(e[4]):1,o=e[8]?Number(e[8]):Number(t.colorSpace?.fullRange??0),c=(a<<4)+(s<<1)+o,u=e[5]?Number(e[5]):t.colorSpace?.primaries?et[t.colorSpace.primaries]:2,l=e[6]?Number(e[6]):t.colorSpace?.transfer?tt[t.colorSpace.transfer]:2,m=e[7]?Number(e[7]):t.colorSpace?.matrix?rt[t.colorSpace.matrix]:2;return Y("vpcC",1,0,[le(r),le(i),le(c),le(u),le(l),le(m),L(0)])},Nf=n=>N("av1C",Qn(n.info.decoderConfig.codec)),Lf=(n,t)=>{let e=0,r,i=16,a=ee.includes(t.track.source._codec);if(a){let s=t.track.source._codec,{sampleSize:o}=Se(s);i=8*o,i>16&&(e=1)}if(t.muxer.isQuickTime&&(e=1),e===0)r=[Array(6).fill(0),L(1),L(e),L(0),_(0),L(t.info.numberOfChannels),L(i),L(0),L(0),L(t.info.sampleRate<2**16?t.info.sampleRate:0),L(0)];else{let s=a?0:-2;r=[Array(6).fill(0),L(1),L(e),L(0),_(0),L(t.info.numberOfChannels),L(Math.min(i,16)),vc(s),L(0),L(t.info.sampleRate<2**16?t.info.sampleRate:0),L(0),a?[_(1),_(i/8),_(t.info.numberOfChannels*i/8)]:[_(0),_(0),_(0)],_(2)]}return N(n,r,[yp(t.track.source._codec,t.muxer.isQuickTime)?.(t)??null])},Ec=n=>{let t;switch(n.track.source._codec){case"aac":t=64;break;case"mp3":t=107;break;case"vorbis":t=221;break;default:throw new Error(`Unhandled audio codec: ${n.track.source._codec}`)}let e=[...le(t),...le(21),...gd(0),..._(0),..._(0)];if(n.info.decoderConfig.description){let r=j(n.info.decoderConfig.description);e=[...e,...le(5),...Ic(r.byteLength),...r]}return e=[...L(1),...le(0),...le(4),...Ic(e.length),...e,...le(6),...le(1),...le(2)],e=[...le(3),...Ic(e.length),...e],Y("esds",0,0,e)},wr=n=>N("wave",void 0,[Wf(n),Hf(n),N("\0\0\0\0")]),Wf=n=>N("frma",[ie(vd(n.track.source._codec,n.muxer.isQuickTime))]),Hf=n=>{let{littleEndian:t}=Se(n.track.source._codec);return N("enda",[L(+t)])},qf=n=>{let t=n.info.numberOfChannels,e=3840,r=n.info.sampleRate,i=0,a=0,s=new Uint8Array(0),o=n.info.decoderConfig?.description;if(o){h(o.byteLength>=18);let c=j(o),u=lr(c);t=u.outputChannelCount,e=u.preSkip,r=u.inputSampleRate,i=u.outputGain,a=u.channelMappingFamily,u.channelMappingTable&&(s=u.channelMappingTable)}return N("dOps",[le(0),le(t),L(e),_(r),vc(i),le(a),...s])},Gf=n=>{let t=n.info.decoderConfig?.description;h(t);let e=j(t);return Y("dfLa",0,0,[...e.subarray(4)])},Pt=n=>{let{littleEndian:t,sampleSize:e}=Se(n.track.source._codec),r=+t;return Y("pcmC",0,0,[le(r),le(8*e)])},Kf=n=>{let t=sa(n.info.firstPacket.data);if(!t)throw new Error("Couldn't extract AC-3 frame info from the audio packet. Ensure the packets contain valid AC-3 sync frames (as specified in ETSI TS 102 366).");let e=new Uint8Array(3),r=new W(e);return r.writeBits(2,t.fscod),r.writeBits(5,t.bsid),r.writeBits(3,t.bsmod),r.writeBits(3,t.acmod),r.writeBits(1,t.lfeon),r.writeBits(5,t.bitRateCode),r.writeBits(5,0),N("dac3",[...e])},Qf=n=>{let t=oa(n.info.firstPacket.data);if(!t)throw new Error("Couldn't extract E-AC-3 frame info from the audio packet. Ensure the packets contain valid E-AC-3 sync frames (as specified in ETSI TS 102 366).");let e=16;for(let s of t.substreams)e+=23,s.numDepSub>0?e+=9:e+=1;let r=Math.ceil(e/8),i=new Uint8Array(r),a=new W(i);a.writeBits(13,t.dataRate),a.writeBits(3,t.substreams.length-1);for(let s of t.substreams)a.writeBits(2,s.fscod),a.writeBits(5,s.bsid),a.writeBits(1,0),a.writeBits(1,0),a.writeBits(3,s.bsmod),a.writeBits(3,s.acmod),a.writeBits(1,s.lfeon),a.writeBits(3,0),a.writeBits(4,s.numDepSub),s.numDepSub>0?a.writeBits(9,s.chanLoc):a.writeBits(1,0);return N("dec3",[...i])},jf=(n,t)=>N(n,[Array(6).fill(0),L(1)],[Tp[t.track.source._codec](t)]),Xf=n=>N("vttC",[...$.encode(n.info.config.description)]);var $f=n=>Y("stts",0,0,[_(n.timeToSampleTable.length),n.timeToSampleTable.map(t=>[_(t.sampleCount),_(t.sampleDelta)])]),Yf=n=>{if(n.samples.every(e=>e.type==="key"))return null;let t=[...n.samples.entries()].filter(([,e])=>e.type==="key");return Y("stss",0,0,[_(t.length),t.map(([e])=>_(e+1))])},Zf=n=>Y("stsc",0,0,[_(n.compactlyCodedChunkTable.length),n.compactlyCodedChunkTable.map(t=>[_(t.firstChunk),_(t.samplesPerChunk),_(1)])]),Jf=n=>{if(n.type==="audio"&&n.info.requiresPcmTransformation){let{sampleSize:t}=Se(n.track.source._codec);return Y("stsz",0,0,[_(t*n.info.numberOfChannels),_(n.samples.reduce((e,r)=>e+oe(r.duration,n.timescale),0))])}return Y("stsz",0,0,[_(0),_(n.samples.length),n.samples.map(t=>_(t.size))])},ep=n=>n.finalizedChunks.length>0&&X(n.finalizedChunks).offset>=2**32?Y("co64",0,0,[_(n.finalizedChunks.length),n.finalizedChunks.map(t=>It(t.offset))]):Y("stco",0,0,[_(n.finalizedChunks.length),n.finalizedChunks.map(t=>_(t.offset))]),tp=n=>Y("ctts",1,0,[_(n.compositionTimeOffsetTable.length),n.compositionTimeOffsetTable.map(t=>[_(t.sampleCount),Zt(t.sampleCompositionTimeOffset)])]),rp=n=>{let t=1/0,e=-1/0,r=1/0,i=-1/0;h(n.compositionTimeOffsetTable.length>0),h(n.samples.length>0);for(let s=0;s<n.compositionTimeOffsetTable.length;s++){let o=n.compositionTimeOffsetTable[s];t=Math.min(t,o.sampleCompositionTimeOffset),e=Math.max(e,o.sampleCompositionTimeOffset)}for(let s=0;s<n.samples.length;s++){let o=n.samples[s];r=Math.min(r,oe(o.timestamp,n.timescale)),i=Math.max(i,oe(o.timestamp+o.duration,n.timescale))}let a=Math.max(-t,0);return i>=2**31?null:Y("cslg",0,0,[Zt(a),Zt(t),Zt(e),Zt(r),Zt(i)])},ip=n=>N("mvex",void 0,n.map(np)),np=n=>Y("trex",0,0,[_(n.track.id),_(1),_(0),_(0),_(0)]),Mc=(n,t)=>N("moof",void 0,[ap(n),...t.map(sp)]),ap=n=>Y("mfhd",0,0,[_(n)]),Ad=n=>{let t=0,e=0,r=0,i=0,a=n.type==="delta";return e|=+a,a?t|=1:t|=2,t<<24|e<<16|r<<8|i},sp=n=>N("traf",void 0,[op(n),cp(n),up(n)]),op=n=>{h(n.currentChunk);let t=0;t|=8,t|=16,t|=32,t|=131072;let e=n.currentChunk.samples[1]??n.currentChunk.samples[0],r={duration:e.timescaleUnitsToNextSample,size:e.size,flags:Ad(e)};return Y("tfhd",0,t,[_(n.track.id),_(r.duration),_(r.size),_(r.flags)])},cp=n=>(h(n.currentChunk),Y("tfdt",1,0,[It(oe(n.currentChunk.startTimestamp,n.timescale))])),up=n=>{h(n.currentChunk);let t=n.currentChunk.samples.map(b=>b.timescaleUnitsToNextSample),e=n.currentChunk.samples.map(b=>b.size),r=n.currentChunk.samples.map(Ad),i=n.currentChunk.samples.map(b=>oe(b.timestamp-b.decodeTimestamp,n.timescale)),a=new Set(t),s=new Set(e),o=new Set(r),c=new Set(i),u=o.size===2&&r[0]!==r[1],l=a.size>1,m=s.size>1,d=!u&&o.size>1,f=c.size>1||[...c].some(b=>b!==0),p=0;return p|=1,p|=4*+u,p|=256*+l,p|=512*+m,p|=1024*+d,p|=2048*+f,Y("trun",1,p,[_(n.currentChunk.samples.length),_(n.currentChunk.offset-n.currentChunk.moofOffset||0),u?_(r[0]):[],n.currentChunk.samples.map((b,g)=>[l?_(t[g]):[],m?_(e[g]):[],d?_(r[g]):[],f?Zt(i[g]):[]])])},xd=n=>N("mfra",void 0,[...n.map(lp),dp()]),lp=(n,t)=>Y("tfra",1,0,[_(n.track.id),_(63),_(n.finalizedChunks.length),n.finalizedChunks.map(r=>[It(oe(r.samples[0].timestamp,n.timescale)),It(r.moofOffset),_(t+1),_(1),_(1)])]),dp=()=>Y("mfro",0,0,[_(0)]),Pd=()=>N("vtte"),Cd=(n,t,e,r,i)=>N("vttc",void 0,[i!==null?N("vsid",[Zt(i)]):null,e!==null?N("iden",[...$.encode(e)]):null,t!==null?N("ctim",[...$.encode(Ds(t))]):null,r!==null?N("sttg",[...$.encode(r)]):null,N("payl",[...$.encode(n)])]),Id=n=>N("vtta",[...$.encode(n)]),mp=n=>{let t=[],e=n.format._options.metadataFormat??"auto",r=n.output._metadataTags;if(e==="mdir"||e==="auto"&&!n.isQuickTime){let i=pp(r);i&&t.push(i)}else if(e==="mdta"){let i=hp(r);i&&t.push(i)}else(e==="udta"||e==="auto"&&n.isQuickTime)&&fp(t,n.output._metadataTags);return t.length===0?null:N("udta",void 0,t)},fp=(n,t)=>{for(let{key:e,value:r}of it(t))switch(e){case"title":n.push(Ct("\xA9nam",r));break;case"description":n.push(Ct("\xA9des",r));break;case"artist":n.push(Ct("\xA9ART",r));break;case"album":n.push(Ct("\xA9alb",r));break;case"albumArtist":n.push(Ct("albr",r));break;case"genre":n.push(Ct("\xA9gen",r));break;case"date":n.push(Ct("\xA9day",r.toISOString().slice(0,10)));break;case"comment":n.push(Ct("\xA9cmt",r));break;case"lyrics":n.push(Ct("\xA9lyr",r));break;case"raw":break;case"discNumber":case"discsTotal":case"trackNumber":case"tracksTotal":case"images":break;default:ne(e)}if(t.raw)for(let e in t.raw){let r=t.raw[e];r==null||e.length!==4||n.some(i=>i.type===e)||(typeof r=="string"?n.push(Ct(e,r)):r instanceof Uint8Array&&n.push(N(e,Array.from(r))))}},Ct=(n,t)=>{let e=$.encode(t);return N(n,[L(e.length),L(_d("und")),Array.from(e)])},hd={"image/jpeg":13,"image/png":14,"image/bmp":27},Ed=(n,t)=>{let e=[];for(let{key:r,value:i}of it(n))switch(r){case"title":e.push({key:t?"title":"\xA9nam",value:pt(i)});break;case"description":e.push({key:t?"description":"\xA9des",value:pt(i)});break;case"artist":e.push({key:t?"artist":"\xA9ART",value:pt(i)});break;case"album":e.push({key:t?"album":"\xA9alb",value:pt(i)});break;case"albumArtist":e.push({key:t?"album_artist":"aART",value:pt(i)});break;case"comment":e.push({key:t?"comment":"\xA9cmt",value:pt(i)});break;case"genre":e.push({key:t?"genre":"\xA9gen",value:pt(i)});break;case"lyrics":e.push({key:t?"lyrics":"\xA9lyr",value:pt(i)});break;case"date":e.push({key:t?"date":"\xA9day",value:pt(i.toISOString().slice(0,10))});break;case"images":for(let a of i)a.kind==="coverFront"&&e.push({key:"covr",value:N("data",[_(hd[a.mimeType]??0),_(0),Array.from(a.data)])});break;case"trackNumber":if(t){let a=n.tracksTotal!==void 0?`${i}/${n.tracksTotal}`:i.toString();e.push({key:"track",value:pt(a)})}else e.push({key:"trkn",value:N("data",[_(0),_(0),L(0),L(i),L(n.tracksTotal??0),L(0)])});break;case"discNumber":t||e.push({key:"disc",value:N("data",[_(0),_(0),L(0),L(i),L(n.discsTotal??0),L(0)])});break;case"tracksTotal":case"discsTotal":break;case"raw":break;default:ne(r)}if(n.raw)for(let r in n.raw){let i=n.raw[r];i==null||!t&&r.length!==4||e.some(a=>a.key===r)||(typeof i=="string"?e.push({key:r,value:pt(i)}):i instanceof Uint8Array?e.push({key:r,value:N("data",[_(0),_(0),Array.from(i)])}):i instanceof He&&e.push({key:r,value:N("data",[_(hd[i.mimeType]??0),_(0),Array.from(i.data)])}))}return e},pp=n=>{let t=Ed(n,!1);return t.length===0?null:Y("meta",0,0,void 0,[Fc(!1,"mdir","","appl"),N("ilst",void 0,t.map(e=>N(e.key,void 0,[e.value])))])},hp=n=>{let t=Ed(n,!0);return t.length===0?null:N("meta",void 0,[Fc(!1,"mdta",""),Y("keys",0,0,[_(t.length)],t.map(e=>N("mdta",[...$.encode(e.key)]))),N("ilst",void 0,t.map((e,r)=>{let i=String.fromCharCode(..._(r+1));return N(i,void 0,[e.value])}))])},pt=n=>N("data",[_(1),_(0),...$.encode(n)]),gp=(n,t)=>{switch(n){case"avc":return t.startsWith("avc3")?"avc3":"avc1";case"hevc":return"hvc1";case"vp8":return"vp08";case"vp9":return"vp09";case"av1":return"av01"}},bp={avc:Uf,hevc:zf,vp8:pd,vp9:pd,av1:Nf},vd=(n,t)=>{switch(n){case"aac":return"mp4a";case"mp3":return"mp4a";case"opus":return"Opus";case"vorbis":return"mp4a";case"flac":return"fLaC";case"ulaw":return"ulaw";case"alaw":return"alaw";case"pcm-u8":return"raw ";case"pcm-s8":return"sowt";case"ac3":return"ac-3";case"eac3":return"ec-3"}if(t)switch(n){case"pcm-s16":return"sowt";case"pcm-s16be":return"twos";case"pcm-s24":return"in24";case"pcm-s24be":return"in24";case"pcm-s32":return"in32";case"pcm-s32be":return"in32";case"pcm-f32":return"fl32";case"pcm-f32be":return"fl32";case"pcm-f64":return"fl64";case"pcm-f64be":return"fl64"}else switch(n){case"pcm-s16":return"ipcm";case"pcm-s16be":return"ipcm";case"pcm-s24":return"ipcm";case"pcm-s24be":return"ipcm";case"pcm-s32":return"ipcm";case"pcm-s32be":return"ipcm";case"pcm-f32":return"fpcm";case"pcm-f32be":return"fpcm";case"pcm-f64":return"fpcm";case"pcm-f64be":return"fpcm"}},yp=(n,t)=>{switch(n){case"aac":return Ec;case"mp3":return Ec;case"opus":return qf;case"vorbis":return Ec;case"flac":return Gf;case"ac3":return Kf;case"eac3":return Qf}if(t)switch(n){case"pcm-s24":return wr;case"pcm-s24be":return wr;case"pcm-s32":return wr;case"pcm-s32be":return wr;case"pcm-f32":return wr;case"pcm-f32be":return wr;case"pcm-f64":return wr;case"pcm-f64be":return wr}else switch(n){case"pcm-s16":return Pt;case"pcm-s16be":return Pt;case"pcm-s24":return Pt;case"pcm-s24be":return Pt;case"pcm-s32":return Pt;case"pcm-s32be":return Pt;case"pcm-f32":return Pt;case"pcm-f32be":return Pt;case"pcm-f64":return Pt;case"pcm-f64be":return Pt}return null},kp={webvtt:"wvtt"},Tp={webvtt:Xf},_d=n=>{h(n.length===3);let t=0;for(let e=0;e<3;e++)t<<=5,t+=n.charCodeAt(e)-96;return t};var Et=class{constructor(t,e){this.finalized=!1;this.started=!1;this.pos=0;this.trackedWrites=null;this.trackedStart=-1;this.trackedEnd=-1;if(t._writerAcquired)throw new Error("Can't have multiple Writers for the same Target.");this.target=t,t._setMonotonicity(e),t._writerAcquired=!0}start(){h(!this.started),this.target._start(),this.started=!0}write(t){h(this.started&&!this.finalized),this.maybeTrackWrites(t),this.target._write(t,this.pos),this.pos+=t.byteLength}seek(t){this.pos=t}getPos(){return this.pos}async flush(){return h(this.started&&!this.finalized),this.target._flush()}async finalize(){h(this.started&&!this.finalized),await this.target._finalize(),this.finalized=!0}maybeTrackWrites(t){if(!this.trackedWrites)return;let e=this.getPos();if(e<this.trackedStart){if(e+t.byteLength<=this.trackedStart)return;t=t.subarray(this.trackedStart-e),e=0}let r=e+t.byteLength-this.trackedStart,i=this.trackedWrites.byteLength;for(;i<r;)i*=2;if(i!==this.trackedWrites.byteLength){let a=new Uint8Array(i);a.set(this.trackedWrites,0),this.trackedWrites=a}this.trackedWrites.set(t,e-this.trackedStart),this.trackedEnd=Math.max(this.trackedEnd,e+t.byteLength)}startTrackingWrites(){this.trackedWrites=new Uint8Array(2**10),this.trackedStart=this.getPos(),this.trackedEnd=this.trackedStart}stopTrackingWrites(){if(!this.trackedWrites)throw new Error("Internal error: Can't get tracked writes since nothing was tracked.");let e={data:this.trackedWrites.subarray(0,this.trackedEnd-this.trackedStart),start:this.trackedStart,end:this.trackedEnd};return this.trackedWrites=null,e}};var Fd=cu(Xo(),1);var Md=typeof Fd<"u"?Fd:void 0,Ce=class extends Ye{constructor(){super(...arguments);this._writerAcquired=!1;this._monotonicity=null;this.onwrite=null}_setMonotonicity(e){this._monotonicity!==!1&&(this._monotonicity=e)}_dispatchWrite(e,r){this.onwrite?.(e,r),this._emit("write",{start:e,end:r})}slice(e){if(!Number.isInteger(e)||e<0)throw new TypeError("offset must be a non-negative integer.");return new Us(this,e)}},Bc=2**16,Oc=2**32,jr=class extends Ce{constructor(e={}){super();this.buffer=null;this._maxPos=0;if(!e||typeof e!="object")throw new TypeError("BufferTarget options, when provided, must be an object.");if(e.onFinalize!==void 0&&typeof e.onFinalize!="function")throw new TypeError("options.onFinalize, when provided, must be a function.");if(this._options=e,this._supportsResize="resize"in new ArrayBuffer(0),this._supportsResize)try{this._buffer=new ArrayBuffer(Bc,{maxByteLength:Oc})}catch{this._buffer=new ArrayBuffer(Bc),this._supportsResize=!1}else this._buffer=new ArrayBuffer(Bc);this._bytes=new Uint8Array(this._buffer)}_ensureSize(e){let r=this._buffer.byteLength;for(;r<e;)r*=2;if(r!==this._buffer.byteLength){if(r>Oc)throw new Error(`ArrayBuffer exceeded maximum size of ${Oc} bytes. Please consider using another target.`);if(this._supportsResize)this._buffer.resize(r);else{let i=new ArrayBuffer(r),a=new Uint8Array(i);a.set(this._bytes,0),this._buffer=i,this._bytes=a}}}_start(){}_write(e,r){this._ensureSize(r+e.byteLength),this._bytes.set(e,r),this._maxPos=Math.max(this._maxPos,r+e.byteLength),this._dispatchWrite(r,r+e.byteLength)}async _flush(){}async _finalize(){this.buffer=this._buffer.slice(0,this._maxPos),this._options.onFinalize&&await this._options.onFinalize(this.buffer),this._emit("finalized")}async _close(){}_getSlice(e,r){return this._bytes.slice(e,r)}},wp=2**24,Sp=2,Bn=class extends Ce{constructor(e,r={}){super();this._sections=[];this._lastWriteEnd=0;this._lastFlushEnd=0;this._streamWriter=null;this._writeError=null;this._chunks=[];if(!(e instanceof WritableStream))throw new TypeError("StreamTarget requires a WritableStream instance.");if(r!=null&&typeof r!="object")throw new TypeError("StreamTarget options, when provided, must be an object.");if(r.chunked!==void 0&&typeof r.chunked!="boolean")throw new TypeError("options.chunked, when provided, must be a boolean.");if(r.chunkSize!==void 0&&(!Number.isInteger(r.chunkSize)||r.chunkSize<1024))throw new TypeError("options.chunkSize, when provided, must be an integer and not smaller than 1024.");this._writable=e,this._options=r,this._chunked=r.chunked??!1,this._chunkSize=r.chunkSize??wp}_start(){this._streamWriter=this._writable.getWriter()}_write(e,r){if(r>this._lastWriteEnd){let i=r-this._lastWriteEnd;this._write(new Uint8Array(i),this._lastWriteEnd)}this._sections.push({data:e.slice(),start:r}),this._lastWriteEnd=Math.max(this._lastWriteEnd,r+e.byteLength),this._dispatchWrite(r,r+e.byteLength)}async _flush(){if(this._writeError!==null)throw this._writeError;if(h(this._streamWriter),this._sections.length===0)return;let e=[],r=[...this._sections].sort((i,a)=>i.start-a.start);e.push({start:r[0].start,size:r[0].data.byteLength});for(let i=1;i<r.length;i++){let a=e[e.length-1],s=r[i];s.start<=a.start+a.size?a.size=Math.max(a.size,s.start+s.data.byteLength-a.start):e.push({start:s.start,size:s.data.byteLength})}for(let i of e){i.data=new Uint8Array(i.size);for(let a of this._sections)i.start<=a.start&&a.start<i.start+i.size&&i.data.set(a.data,a.start-i.start);if(this._streamWriter.desiredSize!==null&&this._streamWriter.desiredSize<=0&&await this._streamWriter.ready,this._chunked)this._writeDataIntoChunks(i.data,i.start),this._tryToFlushChunks();else{if(this._monotonicity===!0&&i.start!==this._lastFlushEnd)throw new Error("Internal error: Monotonicity violation.");this._streamWriter.write({type:"write",data:i.data,position:i.start}).catch(a=>{this._writeError??=a}),this._lastFlushEnd=i.start+i.data.byteLength}}this._sections.length=0}_writeDataIntoChunks(e,r){let i=this._chunks.findIndex(u=>u.start<=r&&r<u.start+this._chunkSize);i===-1&&(i=this._createChunk(r));let a=this._chunks[i],s=r-a.start,o=e.subarray(0,Math.min(this._chunkSize-s,e.byteLength));a.data.set(o,s);let c={start:s,end:s+o.byteLength};if(this._insertSectionIntoChunk(a,c),a.written[0].start===0&&a.written[0].end===this._chunkSize&&(a.shouldFlush=!0),this._chunks.length>Sp){for(let u=0;u<this._chunks.length-1;u++)this._chunks[u].shouldFlush=!0;this._tryToFlushChunks()}o.byteLength<e.byteLength&&this._writeDataIntoChunks(e.subarray(o.byteLength),r+o.byteLength)}_insertSectionIntoChunk(e,r){let i=0,a=e.written.length-1,s=-1;for(;i<=a;){let o=Math.floor(i+(a-i+1)/2);e.written[o].start<=r.start?(i=o+1,s=o):a=o-1}for(e.written.splice(s+1,0,r),(s===-1||e.written[s].end<r.start)&&s++;s<e.written.length-1&&e.written[s].end>=e.written[s+1].start;)e.written[s].end=Math.max(e.written[s].end,e.written[s+1].end),e.written.splice(s+1,1)}_createChunk(e){let i={start:Math.floor(e/this._chunkSize)*this._chunkSize,data:new Uint8Array(this._chunkSize),written:[],shouldFlush:!1};return this._chunks.push(i),this._chunks.sort((a,s)=>a.start-s.start),this._chunks.indexOf(i)}_tryToFlushChunks(e=!1){h(this._streamWriter);for(let r=0;r<this._chunks.length;r++){let i=this._chunks[r];if(!(!i.shouldFlush&&!e)){for(let a of i.written){let s=i.start+a.start;if(this._monotonicity===!0&&s!==this._lastFlushEnd)throw new Error("Internal error: Monotonicity violation.");this._streamWriter.write({type:"write",data:i.data.subarray(a.start,a.end),position:s}).catch(o=>{this._writeError??=o}),this._lastFlushEnd=i.start+a.end}this._chunks.splice(r--,1)}}}async _finalize(){if(this._chunked&&this._tryToFlushChunks(!0),this._writeError!==null)throw this._writeError;h(this._streamWriter),await this._streamWriter.ready,await this._streamWriter.close(),this._emit("finalized")}async _close(){return this._streamWriter?.close()}},Dc=class extends Ce{constructor(e){super();this._writer=null;this._nextWritePos=0;this._writable=e,this._streamTarget=new Bn(new WritableStream({start:()=>{this._writer=this._writable.getWriter()},write:r=>{if(this._monotonicity!==!0)throw new Error("AppendOnlyStreamTarget requires that data be written monotonically (always appended to the end). You must use a format that guarantees this behavior.");return h(r.position===this._nextWritePos),this._nextWritePos+=r.data.byteLength,h(this._writer),this._writer.write(r.data)},close:()=>this._writer?.close()}))}_start(){this._streamTarget._start()}_write(e,r){this._streamTarget._write(e,r)}_flush(){return this._streamTarget._flush()}_finalize(){return this._streamTarget._finalize()}_close(){return this._streamTarget._close()}_setMonotonicity(e){super._setMonotonicity(e),this._streamTarget._setMonotonicity(e)}},Vc=class extends Ce{constructor(e,r={}){if(typeof e!="string")throw new TypeError("filePath must be a string.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(!Md.fs)throw new Error("FilePathTarget is only available in server-side environments (Node.js, Bun, Deno).");super();this._fileHandle=null;let i=new WritableStream({start:async()=>{this._fileHandle=await Md.fs.open(e,"w")},write:async a=>{h(this._fileHandle),await this._fileHandle.write(a.data,0,a.data.byteLength,a.position)},close:async()=>{this._fileHandle&&(await this._fileHandle.close(),this._fileHandle=null)}});this._streamTarget=new Bn(i,{chunked:!0,...r})}_start(){this._streamTarget._start()}_write(e,r){this._streamTarget._write(e,r),this._dispatchWrite(r,r+e.byteLength)}async _flush(){return this._streamTarget._flush()}async _finalize(){await this._streamTarget._finalize(),this._emit("finalized")}async _close(){return this._streamTarget._close()}_setMonotonicity(e){super._setMonotonicity(e),this._streamTarget._setMonotonicity(e)}},Xr=class extends Ce{_start(){}_write(t,e){this._dispatchWrite(e,e+t.byteLength)}async _flush(){}async _finalize(){this._emit("finalized")}async _close(){}},Us=class extends Ce{constructor(t,e){super(),this._baseTarget=t,this._offset=e}_start(){}_write(t,e){this._baseTarget._write(t,this._offset+e),this._dispatchWrite(e,e+t.byteLength)}_flush(){return this._baseTarget._flush()}async _finalize(){this._emit("finalized")}async _close(){}_setMonotonicity(t){super._setMonotonicity(t),this._baseTarget._setMonotonicity(t)}},Le=class{constructor(t,e){this.rootPath=t;this.getTarget=e;if(typeof t!="string")throw new TypeError("rootPath must be a string.");if(typeof e!="function")throw new TypeError("getTarget must be a function.")}};var je=57600,Ap=2082844800,Rd=n=>{let t={},e=n.track;return e.metadata.name!==void 0&&(t.name=e.metadata.name),t},oe=(n,t,e=!0)=>{let r=n*t;return e?Math.round(r):r},zs=class extends ge{constructor(e,r){super(e);this.writer=null;this.boxWriter=null;this.initWriter=null;this.initBoxWriter=null;this.auxTarget=new jr;this.auxWriter=new Et(this.auxTarget,!1);this.auxBoxWriter=new Qr(this.auxWriter);this.mdat=null;this.ftypSize=null;this.trackDatas=[];this.allTracksKnown=Z();this.creationTime=Math.floor(Date.now()/1e3)+Ap;this.finalizedChunks=[];this.nextFragmentNumber=1;this.maxWrittenTimestamp=-1/0;this.minWrittenTimestamp=1/0;this.maxWrittenEndTimestamp=-1/0;this.segmentHeaderSize=null;this.format=r,this.isQuickTime=r instanceof Bi,this.isCmaf=r instanceof Mi,this.minimumFragmentDuration=r._options.minimumFragmentDuration??(r instanceof Mi?1/0:1)}async start(){let e=await this.mutex.acquire();if(this.isCmaf?(this.fastStart="fragmented",this.isFragmented=!0):(this.writer=await this.output._getRootWriter(i=>this.format._options.fastStart!==void 0?this.format._options.fastStart==="fragmented":i instanceof jr),this.boxWriter=new Qr(this.writer),this.fastStart=this.format._options.fastStart??(this.writer.target instanceof jr?"in-memory":!1),this.isFragmented=this.fastStart==="fragmented"),this.isCmaf){if(!this.output._hasInitTarget())throw new Error("CMAF outputs require the initTarget field in OutputOptions to be set; the init segment will be written to it.");let i=await this.output._getInitTarget(),a=new Et(i,!0);a.start(),this.initWriter=a,this.initBoxWriter=new Qr(a)}let r=this.output._tracks.some(i=>i.isVideoTrack()&&i.source._codec==="avc");{let i=this.initBoxWriter??this.boxWriter;if(h(i),this.format._options.onFtyp&&i.writer.startTrackingWrites(),i.writeBox(wd({isQuickTime:this.isQuickTime,holdsAvc:r,fragmented:this.isFragmented,cmaf:this.isCmaf})),this.format._options.onFtyp){let{data:a,start:s}=i.writer.stopTrackingWrites();this.format._options.onFtyp(a,s)}this.ftypSize=i.writer.getPos(),this.isCmaf&&await this.initWriter.flush()}if(this.fastStart!=="in-memory")if(this.fastStart==="reserve"){for(let i of this.output._tracks)if(i.metadata.maximumPacketCount===void 0)throw new Error("All tracks must specify maximumPacketCount in their metadata when using fastStart: 'reserve'.")}else this.isFragmented||(h(this.writer),h(this.boxWriter),this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat=Mn(!0),this.boxWriter.writeBox(this.mdat));await this.writer?.flush(),e()}allTracksAreKnown(){for(let e of this.output._tracks)if(!e.source._closed&&!this.trackDatas.some(r=>r.track===e))return!1;return!0}async getMimeType(){await this.allTracksKnown.promise;let e=this.trackDatas.map(r=>r.type==="video"||r.type==="audio"?r.info.decoderConfig.codec:{webvtt:"wvtt"}[r.track.source._codec]);return la({isQuickTime:this.isQuickTime,hasVideo:this.trackDatas.some(r=>r.type==="video"),hasAudio:this.trackDatas.some(r=>r.type==="audio"),codecStrings:e})}getVideoTrackData(e,r,i){let a=this.trackDatas.find(f=>f.track===e);if(a)return a;sr(i),h(i),h(i.decoderConfig);let s={...i.decoderConfig};h(s.codedWidth!==void 0),h(s.codedHeight!==void 0);let o=!1;if(e.source._codec==="avc"&&!s.description){let f=ai(r.data);if(!f)throw new Error("Couldn't extract an AVCDecoderConfigurationRecord from the AVC packet. Make sure the packets are in Annex B format (as specified in ITU-T-REC-H.264) when not providing a description, or provide a description (must be an AVCDecoderConfigurationRecord as specified in ISO 14496-15) and ensure the packets are in AVCC format.");s.description=Qu(f),o=!0}else if(e.source._codec==="hevc"&&!s.description){let f=si(r.data);if(!f)throw new Error("Couldn't extract an HEVCDecoderConfigurationRecord from the HEVC packet. Make sure the packets are in Annex B format (as specified in ITU-T-REC-H.265) when not providing a description, or provide a description (must be an HEVCDecoderConfigurationRecord as specified in ISO 14496-15) and ensure the packets are in HEVC format.");s.description=Xu(f),o=!0}let c=Su(1/(e.metadata.frameRate??je),1e6).den,u=s.displayAspectWidth,l=s.displayAspectHeight,m=u===void 0||l===void 0?{num:1,den:1}:Ot({num:u*s.codedHeight,den:l*s.codedWidth}),d={muxer:this,track:e,type:"video",info:{width:s.codedWidth,height:s.codedHeight,pixelAspectRatio:m,decoderConfig:s,requiresAnnexBTransformation:o},timescale:c,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,startTimestampOffset:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[],closed:!1};return this.trackDatas.push(d),this.trackDatas.sort((f,p)=>f.track.id-p.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),d}getAudioTrackData(e,r,i){let a=this.trackDatas.find(u=>u.track===e);if(a)return a;Ee(i),h(i),h(i.decoderConfig);let s={...i.decoderConfig},o=!1;if(e.source._codec==="aac"&&!s.description){let u=De(he.tempFromBytes(r.data));if(!u)throw new Error("Couldn't parse ADTS header from the AAC packet. Make sure the packets are in ADTS format (as specified in ISO 13818-7) when not providing a description, or provide a description (must be an AudioSpecificConfig as specified in ISO 14496-3) and ensure the packets are raw AAC data.");let l=qe[u.samplingFrequencyIndex],m=kt[u.channelConfiguration];if(l===void 0||m===void 0)throw new Error("Invalid ADTS frame header.");s.description=ti({objectType:u.objectType,sampleRate:l,numberOfChannels:m}),o=!0}let c={muxer:this,track:e,type:"audio",info:{numberOfChannels:i.decoderConfig.numberOfChannels,sampleRate:i.decoderConfig.sampleRate,decoderConfig:s,requiresPcmTransformation:!this.isFragmented&&ee.includes(e.source._codec),expectedNextPcmPacketTimestamp:null,requiresAdtsStripping:o,firstPacket:r},timescale:s.sampleRate,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,startTimestampOffset:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[],closed:!1};return this.trackDatas.push(c),this.trackDatas.sort((u,l)=>u.track.id-l.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),c}getSubtitleTrackData(e,r){let i=this.trackDatas.find(s=>s.track===e);if(i)return i;Xn(r),h(r),h(r.config);let a={muxer:this,track:e,type:"subtitle",info:{config:r.config},timescale:1e3,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,startTimestampOffset:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[],closed:!1,lastCueEndTimestamp:0,cueQueue:[],nextSourceId:0,cueToSourceId:new WeakMap};return this.trackDatas.push(a),this.trackDatas.sort((s,o)=>s.track.id-o.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),a}async addEncodedVideoPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getVideoTrackData(e,r,i),o=r.data;if(s.info.requiresAnnexBTransformation){let u=[...ur(o)].map(l=>o.subarray(l.offset,l.offset+l.length));if(u.length===0)throw new Error("Failed to transform packet data. Make sure all packets are provided in Annex B format, as specified in ITU-T-REC-H.264 and ITU-T-REC-H.265.");o=ta(u,4)}this.validateTimestamp(s.track,r.timestamp,r.type==="key");let c=this.createSampleForTrack(s,o,r.timestamp,r.duration,r.type);await this.registerSample(s,c)}finally{a()}}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getAudioTrackData(e,r,i),o=r.data;if(s.info.requiresAdtsStripping){let m=De(he.tempFromBytes(o));if(!m)throw new Error("Expected ADTS frame, didn't get one.");let d=m.crcCheck===null?Qt:Ne;o=o.subarray(d)}this.validateTimestamp(s.track,r.timestamp,r.type==="key");let c=r.timestamp,u=r.duration;if(s.info.requiresPcmTransformation){let d=Se(s.info.decoderConfig.codec).sampleSize*s.info.numberOfChannels;if(u=o.byteLength/d/s.info.sampleRate,s.info.expectedNextPcmPacketTimestamp!==null){let f=c-s.info.expectedNextPcmPacketTimestamp;if(f<.01)c=s.info.expectedNextPcmPacketTimestamp;else{let p=await this.padWithSilence(s,s.info.expectedNextPcmPacketTimestamp,f);c=s.info.expectedNextPcmPacketTimestamp+p}}s.info.expectedNextPcmPacketTimestamp=c+u}let l=this.createSampleForTrack(s,o,c,u,r.type);await this.registerSample(s,l)}finally{a()}}async padWithSilence(e,r,i){let a=oe(i,e.timescale);if(i=a/e.timescale,a>0){let{sampleSize:s,silentValue:o}=Se(e.info.decoderConfig.codec),c=a*e.info.numberOfChannels,u=new Uint8Array(s*c).fill(o),l=this.createSampleForTrack(e,new Uint8Array(u.buffer),r,i,"key");await this.registerSample(e,l)}return i}async addSubtitleCue(e,r,i){let a=await this.mutex.acquire();try{let s=this.getSubtitleTrackData(e,i);this.validateTimestamp(s.track,r.timestamp,!0),e.source._codec==="webvtt"&&(s.cueQueue.push(r),await this.processWebVTTCues(s,r.timestamp))}finally{a()}}async processWebVTTCues(e,r){for(;e.cueQueue.length>0;){let i=new Set([]);for(let l of e.cueQueue)h(l.timestamp<=r),h(e.lastCueEndTimestamp<=l.timestamp+l.duration),i.add(Math.max(l.timestamp,e.lastCueEndTimestamp)),i.add(l.timestamp+l.duration);let a=[...i].sort((l,m)=>l-m),s=a[0],o=a[1]??s;if(r<o)break;if(e.lastCueEndTimestamp<s){this.auxWriter.seek(0);let l=Pd();this.auxBoxWriter.writeBox(l);let m=this.auxTarget._getSlice(0,this.auxWriter.getPos()),d=this.createSampleForTrack(e,m,e.lastCueEndTimestamp,s-e.lastCueEndTimestamp,"key");await this.registerSample(e,d),e.lastCueEndTimestamp=s}this.auxWriter.seek(0);for(let l=0;l<e.cueQueue.length;l++){let m=e.cueQueue[l];if(m.timestamp>=o)break;Ri.lastIndex=0;let d=Ri.test(m.text),f=m.timestamp+m.duration,p=e.cueToSourceId.get(m);if(p===void 0&&o<f&&(p=e.nextSourceId++,e.cueToSourceId.set(m,p)),m.notes){let g=Id(m.notes);this.auxBoxWriter.writeBox(g)}let b=Cd(m.text,d?s:null,m.identifier??null,m.settings??null,p??null);this.auxBoxWriter.writeBox(b),f===o&&e.cueQueue.splice(l--,1)}let c=this.auxTarget._getSlice(0,this.auxWriter.getPos()),u=this.createSampleForTrack(e,c,s,o-s,"key");await this.registerSample(e,u),e.lastCueEndTimestamp=o}}createSampleForTrack(e,r,i,a,s){return{timestamp:i,decodeTimestamp:i,duration:a,data:r,size:r.byteLength,type:s,timescaleUnitsToNextSample:oe(a,e.timescale)}}processTimestamps(e,r){if(e.timestampProcessingQueue.length===0)return;if(e.type==="audio"&&e.info.requiresPcmTransformation){this.isFragmented||(e.startTimestampOffset??=e.timestampProcessingQueue[0].timestamp);let a=0;for(let s=0;s<e.timestampProcessingQueue.length;s++){let o=e.timestampProcessingQueue[s],c=oe(o.duration,e.timescale);a+=c}if(e.timeToSampleTable.length===0)e.timeToSampleTable.push({sampleCount:a,sampleDelta:1});else{let s=X(e.timeToSampleTable);s.sampleCount+=a}e.timestampProcessingQueue.length=0;return}let i=e.timestampProcessingQueue.map(a=>a.timestamp).sort((a,s)=>a-s);this.isFragmented||(e.startTimestampOffset??=i[0]);for(let a=0;a<e.timestampProcessingQueue.length;a++){let s=e.timestampProcessingQueue[a];s.decodeTimestamp=i[a];let o=oe(s.timestamp-s.decodeTimestamp,e.timescale),c=oe(s.duration,e.timescale);if(e.lastTimescaleUnits!==null){h(e.lastSample);let u=oe(s.decodeTimestamp,e.timescale,!1),l=Math.round(u-e.lastTimescaleUnits);if(h(l>=0),e.lastTimescaleUnits+=l,e.lastSample.timescaleUnitsToNextSample=l,!this.isFragmented){let m=X(e.timeToSampleTable);if(h(m),m.sampleCount===1){m.sampleDelta=l;let f=e.timeToSampleTable[e.timeToSampleTable.length-2];f&&f.sampleDelta===l&&(f.sampleCount++,e.timeToSampleTable.pop(),m=f)}else m.sampleDelta!==l&&(m.sampleCount--,e.timeToSampleTable.push(m={sampleCount:1,sampleDelta:l}));m.sampleDelta===c?m.sampleCount++:e.timeToSampleTable.push({sampleCount:1,sampleDelta:c});let d=X(e.compositionTimeOffsetTable);h(d),d.sampleCompositionTimeOffset===o?d.sampleCount++:e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:o})}}else e.lastTimescaleUnits=oe(s.decodeTimestamp,e.timescale,!1),this.isFragmented||(e.timeToSampleTable.push({sampleCount:1,sampleDelta:c}),e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:o}));e.lastSample=s}if(e.timestampProcessingQueue.length=0,h(e.lastSample),h(e.lastTimescaleUnits!==null),r!==void 0&&e.lastSample.timescaleUnitsToNextSample===0){h(r.type==="key");let a=oe(r.timestamp,e.timescale,!1),s=Math.round(a-e.lastTimescaleUnits);e.lastSample.timescaleUnitsToNextSample=s}}async registerSample(e,r){r.type==="key"&&this.processTimestamps(e,r),e.timestampProcessingQueue.push(r),this.isFragmented?(e.sampleQueue.push(r),await this.interleaveSamples()):this.fastStart==="reserve"?await this.registerSampleFastStartReserve(e,r):await this.addSampleToTrack(e,r)}async addSampleToTrack(e,r){if(!this.isFragmented&&(e.samples.push(r),this.fastStart==="reserve")){let a=e.track.metadata.maximumPacketCount;if(h(a!==void 0),e.samples.length>a)throw new Error(`Track #${e.track.id} has already reached the maximum packet count (${a}). Either add less packets or increase the maximum packet count.`)}let i=!1;if(!e.currentChunk)i=!0;else{e.currentChunk.startTimestamp=Math.min(e.currentChunk.startTimestamp,r.timestamp);let a=r.timestamp-e.currentChunk.startTimestamp;if(this.isFragmented){let s=this.trackDatas.every(o=>{if(e===o)return r.type==="key";let c=o.sampleQueue[0];return c?c.type==="key":o.closed});a>=this.minimumFragmentDuration&&s&&r.timestamp>this.maxWrittenTimestamp&&(i=!0,await this.finalizeFragment())}else i=a>=.5}i&&(e.currentChunk&&await this.finalizeCurrentChunk(e),e.currentChunk={startTimestamp:r.timestamp,samples:[],offset:null,moofOffset:null}),h(e.currentChunk),e.currentChunk.samples.push(r),this.isFragmented&&(this.maxWrittenTimestamp=Math.max(this.maxWrittenTimestamp,r.timestamp),this.maxWrittenEndTimestamp=Math.max(this.maxWrittenEndTimestamp,r.timestamp+r.duration),this.minWrittenTimestamp=Math.min(this.minWrittenTimestamp,r.timestamp))}async finalizeCurrentChunk(e){if(h(!this.isFragmented),h(this.writer),!e.currentChunk)return;e.finalizedChunks.push(e.currentChunk),this.finalizedChunks.push(e.currentChunk);let r=e.currentChunk.samples.length;if(e.type==="audio"&&e.info.requiresPcmTransformation&&(r=e.currentChunk.samples.reduce((i,a)=>i+oe(a.duration,e.timescale),0)),(e.compactlyCodedChunkTable.length===0||X(e.compactlyCodedChunkTable).samplesPerChunk!==r)&&e.compactlyCodedChunkTable.push({firstChunk:e.finalizedChunks.length,samplesPerChunk:r}),this.fastStart==="in-memory"){e.currentChunk.offset=0;return}e.currentChunk.offset=this.writer.getPos();for(let i of e.currentChunk.samples)h(i.data),this.writer.write(i.data),i.data=null;await this.writer.flush()}async interleaveSamples(e=!1){if(h(this.isFragmented),!(!e&&!this.allTracksAreKnown()))e:for(;;){let r=null,i=1/0;for(let s of this.trackDatas){if(!e&&s.sampleQueue.length===0&&!s.closed)break e;s.sampleQueue.length>0&&s.sampleQueue[0].timestamp<i&&(r=s,i=s.sampleQueue[0].timestamp)}if(!r)break;let a=r.sampleQueue.shift();await this.addSampleToTrack(r,a)}}async finalizeFragment(e=!this.isCmaf){h(this.isFragmented);let r=this.nextFragmentNumber++;if(r===1){let p=this.initBoxWriter??this.boxWriter;h(p),this.format._options.onMoov&&p.writer.startTrackingWrites();let b=Fi(this);if(p.writeBox(b),this.format._options.onMoov){let{data:g,start:y}=p.writer.stopTrackingWrites();this.format._options.onMoov(g,y)}if(this.isCmaf){h(this.initWriter),await this.initWriter.flush(),await this.initWriter.finalize(),this.writer=await this.output._getRootWriter(!0),this.boxWriter=new Qr(this.writer);let g=this.boxWriter.measureBox(_c()),y=this.boxWriter.measureBox(Rc(this,0));this.segmentHeaderSize=g+y,this.writer.seek(this.segmentHeaderSize)}}h(this.writer),h(this.boxWriter);let i=this.trackDatas.filter(p=>p.currentChunk),a=Mc(r,i),s=this.writer.getPos(),o=s+this.boxWriter.measureBox(a),c=o+st,u=1/0;for(let p of i){p.currentChunk.offset=c,p.currentChunk.moofOffset=s;for(let b of p.currentChunk.samples)c+=b.size;u=Math.min(u,p.currentChunk.startTimestamp)}let l=c-o,m=l>=2**32;if(m)for(let p of i)p.currentChunk.offset+=Nt-st;this.format._options.onMoof&&this.writer.startTrackingWrites();let d=Mc(r,i);if(this.boxWriter.writeBox(d),this.format._options.onMoof){let{data:p,start:b}=this.writer.stopTrackingWrites();this.format._options.onMoof(p,b,u)}h(this.writer.getPos()===o),this.format._options.onMdat&&this.writer.startTrackingWrites();let f=Mn(m);f.size=l,this.boxWriter.writeBox(f),this.writer.seek(o+(m?Nt:st));for(let p of i)for(let b of p.currentChunk.samples)this.writer.write(b.data),b.data=null;if(this.format._options.onMdat){let{data:p,start:b}=this.writer.stopTrackingWrites();this.format._options.onMdat(p,b)}for(let p of i)p.finalizedChunks.push(p.currentChunk),this.finalizedChunks.push(p.currentChunk),p.currentChunk=null;e&&await this.writer.flush()}async registerSampleFastStartReserve(e,r){if(h(this.writer),h(this.boxWriter),this.allTracksAreKnown()){if(!this.mdat){let i=Fi(this),s=this.boxWriter.measureBox(i)+this.computeSampleTableSizeUpperBound()+4096;h(this.ftypSize!==null),this.writer.seek(this.ftypSize+s),this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat=Mn(!0),this.boxWriter.writeBox(this.mdat);for(let o of this.trackDatas){for(let c of o.sampleQueue)await this.addSampleToTrack(o,c);o.sampleQueue.length=0}}await this.addSampleToTrack(e,r)}else e.sampleQueue.push(r)}computeSampleTableSizeUpperBound(){h(this.fastStart==="reserve");let e=0;for(let r of this.trackDatas){let i=r.track.metadata.maximumPacketCount;h(i!==void 0),e+=8*Math.ceil(2/3*i),e+=4*i,e+=8*Math.ceil(2/3*i),e+=12*Math.ceil(2/3*i),e+=4*i,e+=8*i}return e}async onTrackClose(e){let r=await this.mutex.acquire(),i=this.trackDatas.find(a=>a.track===e);i&&(i.closed=!0,i.type==="subtitle"&&e.source._codec==="webvtt"&&await this.processWebVTTCues(i,1/0),this.processTimestamps(i)),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),this.isFragmented&&await this.interleaveSamples(),r()}async finalize(){let e=await this.mutex.acquire();this.allTracksKnown.resolve();for(let r of this.trackDatas)r.closed=!0,r.type==="subtitle"&&r.track.source._codec==="webvtt"&&await this.processWebVTTCues(r,1/0),this.processTimestamps(r);if(this.isFragmented)await this.interleaveSamples(!0),await this.finalizeFragment(!1);else for(let r of this.trackDatas){await this.finalizeCurrentChunk(r),h(r.startTimestampOffset!==null);for(let i=0;i<r.samples.length;i++){let a=r.samples[i];a.timestamp-=r.startTimestampOffset,a.decodeTimestamp-=r.startTimestampOffset}}if(h(this.writer),h(this.boxWriter),this.fastStart==="in-memory"){this.mdat=Mn(!1);let r;for(let a=0;a<2;a++){let s=Fi(this),o=this.boxWriter.measureBox(s);r=this.boxWriter.measureBox(this.mdat);let c=this.writer.getPos()+o+r;for(let u of this.finalizedChunks){u.offset=c;for(let{data:l}of u.samples)h(l),c+=l.byteLength,r+=l.byteLength}if(c<2**32)break;r>=2**32&&(this.mdat.largeSize=!0)}this.format._options.onMoov&&this.writer.startTrackingWrites();let i=Fi(this);if(this.boxWriter.writeBox(i),this.format._options.onMoov){let{data:a,start:s}=this.writer.stopTrackingWrites();this.format._options.onMoov(a,s)}this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat.size=r,this.boxWriter.writeBox(this.mdat);for(let a of this.finalizedChunks)for(let s of a.samples)h(s.data),this.writer.write(s.data),s.data=null;if(this.format._options.onMdat){let{data:a,start:s}=this.writer.stopTrackingWrites();this.format._options.onMdat(a,s)}}else if(this.isFragmented)if(this.isCmaf){let r=this.segmentHeaderSize!==null?this.writer.getPos()-this.segmentHeaderSize:0;this.writer.seek(0),this.boxWriter.writeBox(_c()),this.boxWriter.writeBox(Rc(this,r))}else{let r=this.writer.getPos(),i=xd(this.trackDatas);this.boxWriter.writeBox(i);let a=this.writer.getPos()-r;this.writer.seek(this.writer.getPos()-4),this.boxWriter.writeU32(a)}else{h(this.mdat);let r=this.boxWriter.offsets.get(this.mdat);h(r!==void 0);let i=this.writer.getPos()-r;if(this.mdat.size=i,this.mdat.largeSize=i>=2**32,this.boxWriter.patchBox(this.mdat),this.format._options.onMdat){let{data:s,start:o}=this.writer.stopTrackingWrites();this.format._options.onMdat(s,o)}let a=Fi(this);if(this.fastStart==="reserve"){h(this.ftypSize!==null),this.writer.seek(this.ftypSize),this.format._options.onMoov&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(a);let s=this.boxWriter.offsets.get(this.mdat)-this.writer.getPos();this.boxWriter.writeBox(Sd(s))}else this.format._options.onMoov&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(a);if(this.format._options.onMoov){let{data:s,start:o}=this.writer.stopTrackingWrites();this.format._options.onMoov(s,o)}}e()}};var xp=-(2**15),Pp=2**15-1,Bd="Mediabunny",Od=6,Dd=5,Cp={video:1,audio:2,subtitle:17},Ns=class extends ge{constructor(e,r){super(e);this.trackDatas=[];this.allTracksKnown=Z();this.segment=null;this.segmentInfo=null;this.seekHead=null;this.tracksElement=null;this.tagsElement=null;this.attachmentsElement=null;this.segmentDuration=null;this.cues=null;this.currentCluster=null;this.currentClusterStartMsTimestamp=null;this.currentClusterMaxMsTimestamp=null;this.trackDatasInCurrentCluster=new Map;this.startTimestamp=1/0;this.endTimestamp=-1/0;this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!!this.format._options.appendOnly),this.ebmlWriter=new Ta(this.writer),this.writeEBMLHeader(),this.createSegmentInfo(),this.createCues(),await this.writer.flush(),e()}writeEBMLHeader(){this.format._options.onEbmlHeader&&this.writer.startTrackingWrites();let e={id:440786851,data:[{id:17030,data:1},{id:17143,data:1},{id:17138,data:4},{id:17139,data:8},{id:17026,data:this.format instanceof Oi?"webm":"matroska"},{id:17031,data:2},{id:17029,data:2}]};if(this.ebmlWriter.writeEBML(e),this.format._options.onEbmlHeader){let{data:r,start:i}=this.writer.stopTrackingWrites();this.format._options.onEbmlHeader(r,i)}}maybeCreateSeekHead(e){if(this.format._options.appendOnly)return;let r=new Uint8Array([28,83,187,107]),i=new Uint8Array([21,73,169,102]),a=new Uint8Array([22,84,174,107]),s=new Uint8Array([25,65,164,105]),o=new Uint8Array([18,84,195,103]),c={id:290298740,data:[{id:19899,data:[{id:21419,data:r},{id:21420,size:5,data:e?this.ebmlWriter.offsets.get(this.cues)-this.segmentDataOffset:0}]},{id:19899,data:[{id:21419,data:i},{id:21420,size:5,data:e?this.ebmlWriter.offsets.get(this.segmentInfo)-this.segmentDataOffset:0}]},{id:19899,data:[{id:21419,data:a},{id:21420,size:5,data:e?this.ebmlWriter.offsets.get(this.tracksElement)-this.segmentDataOffset:0}]},this.attachmentsElement?{id:19899,data:[{id:21419,data:s},{id:21420,size:5,data:e?this.ebmlWriter.offsets.get(this.attachmentsElement)-this.segmentDataOffset:0}]}:null,this.tagsElement?{id:19899,data:[{id:21419,data:o},{id:21420,size:5,data:e?this.ebmlWriter.offsets.get(this.tagsElement)-this.segmentDataOffset:0}]}:null]};this.seekHead=c}createSegmentInfo(){let e={id:17545,data:new fi(0)};this.segmentDuration=e;let r={id:357149030,data:[{id:2807729,data:1e6},{id:19840,data:Bd},{id:22337,data:Bd},this.format._options.appendOnly?null:e]};this.segmentInfo=r}createTracks(){let e={id:374648427,data:[]};this.tracksElement=e;for(let r of this.trackDatas){let i=Fe[r.track.source._codec];h(i);let a=0;if(r.type==="audio"&&r.track.source._codec==="opus"){a=1e6*80;let s=r.info.decoderConfig.description;if(s){let o=j(s),c=lr(o);a=Math.round(1e9*(c.preSkip/nt))}}e.data.push({id:174,data:[{id:215,data:r.track.id},{id:29637,data:r.track.id},{id:131,data:Cp[r.type]},r.track.metadata.disposition?.default===!1?{id:136,data:0}:null,r.track.metadata.disposition?.forced?{id:21930,data:1}:null,r.track.metadata.disposition?.hearingImpaired?{id:21931,data:1}:null,r.track.metadata.disposition?.visuallyImpaired?{id:21932,data:1}:null,r.track.metadata.disposition?.original?{id:21934,data:1}:null,r.track.metadata.disposition?.commentary?{id:21935,data:1}:null,{id:156,data:0},{id:2274716,data:r.track.metadata.languageCode??J},{id:134,data:i},{id:22186,data:0},{id:22203,data:a},r.track.metadata.name!==void 0?{id:21358,data:new lt(r.track.metadata.name)}:null,r.type==="video"?this.videoSpecificTrackInfo(r):null,r.type==="audio"?this.audioSpecificTrackInfo(r):null,r.type==="subtitle"?this.subtitleSpecificTrackInfo(r):null]})}}videoSpecificTrackInfo(e){let{frameRate:r,rotation:i}=e.track.metadata,a=[e.info.decoderConfig.description?{id:25506,data:j(e.info.decoderConfig.description)}:null,r?{id:2352003,data:1e9/r}:null],s=i?Ze(-i):0,o=!!e.info.aspectRatio&&e.info.aspectRatio.num*e.info.height!==e.info.aspectRatio.den*e.info.width,c=e.info.decoderConfig.colorSpace,u={id:224,data:[{id:176,data:e.info.width},{id:186,data:e.info.height},o?{id:21680,data:e.info.aspectRatio.num}:null,o?{id:21690,data:e.info.aspectRatio.den}:null,o?{id:21682,data:3}:null,e.info.alphaMode?{id:21440,data:1}:null,zn(c)?{id:21936,data:[{id:21937,data:rt[c.matrix]},{id:21946,data:tt[c.transfer]},{id:21947,data:et[c.primaries]},{id:21945,data:c.fullRange?2:1}]}:null,s?{id:30320,data:[{id:30321,data:0},{id:30325,data:new mi((s+180)%360-180)}]}:null]};return a.push(u),a}audioSpecificTrackInfo(e){let r=ee.includes(e.track.source._codec)?Se(e.track.source._codec):null;return[e.info.decoderConfig.description?{id:25506,data:j(e.info.decoderConfig.description)}:null,{id:225,data:[{id:181,data:new mi(e.info.sampleRate)},{id:159,data:e.info.numberOfChannels},r?{id:25188,data:8*r.sampleSize}:null]}]}subtitleSpecificTrackInfo(e){return[{id:25506,data:$.encode(e.info.config.description)}]}maybeCreateTags(){let e=[],r=(s,o)=>{e.push({id:26568,data:[{id:17827,data:new lt(s)},typeof o=="string"?{id:17543,data:new lt(o)}:{id:17541,data:o}]})},i=this.output._metadataTags,a=new Set;for(let{key:s,value:o}of it(i))switch(s){case"title":r("TITLE",o),a.add("TITLE");break;case"description":r("DESCRIPTION",o),a.add("DESCRIPTION");break;case"artist":r("ARTIST",o),a.add("ARTIST");break;case"album":r("ALBUM",o),a.add("ALBUM");break;case"albumArtist":r("ALBUM_ARTIST",o),a.add("ALBUM_ARTIST");break;case"genre":r("GENRE",o),a.add("GENRE");break;case"comment":r("COMMENT",o),a.add("COMMENT");break;case"lyrics":r("LYRICS",o),a.add("LYRICS");break;case"date":r("DATE",o.toISOString().slice(0,10)),a.add("DATE");break;case"trackNumber":{let c=i.tracksTotal!==void 0?`${o}/${i.tracksTotal}`:o.toString();r("PART_NUMBER",c),a.add("PART_NUMBER")}break;case"discNumber":{let c=i.discsTotal!==void 0?`${o}/${i.discsTotal}`:o.toString();r("DISC",c),a.add("DISC")}break;case"tracksTotal":case"discsTotal":break;case"images":case"raw":break;default:ne(s)}if(i.raw)for(let s in i.raw){let o=i.raw[s];o==null||a.has(s)||(typeof o=="string"||o instanceof Uint8Array)&&r(s,o)}e.length!==0&&(this.tagsElement={id:307544935,data:[{id:29555,data:[{id:25536,data:[{id:26826,data:50},{id:25546,data:"MOVIE"}]},...e]}]})}maybeCreateAttachments(){let e=this.output._metadataTags,r=[],i=new Set,a=e.images??[];for(let s of a){let o=s.name;o===void 0&&(o=(s.kind==="coverFront"?"cover":s.kind==="coverBack"?"back":"image")+(xu(s.mimeType)??""));let c;for(;;){c=0n;for(let u=0;u<8;u++)c<<=8n,c|=BigInt(Math.floor(Math.random()*256));if(c!==0n&&!i.has(c))break}i.add(c),r.push({id:24999,data:[s.description!==void 0?{id:18046,data:new lt(s.description)}:null,{id:18030,data:new lt(o)},{id:18016,data:s.mimeType},{id:18012,data:s.data},{id:18094,data:c}]})}for(let[s,o]of Object.entries(e.raw??{}))!(o instanceof ar)||!/^\d+$/.test(s)||a.find(u=>u.mimeType===o.mimeType&&Wn(u.data,o.data))||r.push({id:24999,data:[o.description!==void 0?{id:18046,data:new lt(o.description)}:null,{id:18030,data:new lt(o.name??"")},{id:18016,data:o.mimeType??""},{id:18012,data:o.data},{id:18094,data:BigInt(s)}]});r.length!==0&&(this.attachmentsElement={id:423732329,data:r})}createSegment(){this.createTracks(),this.maybeCreateTags(),this.maybeCreateAttachments(),this.maybeCreateSeekHead(!1);let e={id:408125543,size:this.format._options.appendOnly?-1:Od,data:[this.seekHead,this.segmentInfo,this.tracksElement,this.attachmentsElement,this.tagsElement]};if(this.segment=e,this.format._options.onSegmentHeader&&this.writer.startTrackingWrites(),this.ebmlWriter.writeEBML(e),this.format._options.onSegmentHeader){let{data:r,start:i}=this.writer.stopTrackingWrites();this.format._options.onSegmentHeader(r,i)}}createCues(){this.cues={id:475249515,data:[]}}get segmentDataOffset(){return h(this.segment),this.ebmlWriter.dataOffsets.get(this.segment)}allTracksAreKnown(){for(let e of this.output._tracks)if(!e.source._closed&&!this.trackDatas.some(r=>r.track===e))return!1;return!0}async getMimeType(){await this.allTracksKnown.promise;let e=this.trackDatas.map(r=>r.type==="video"||r.type==="audio"?r.info.decoderConfig.codec:{webvtt:"wvtt"}[r.track.source._codec]);return xa({isWebM:this.format instanceof Oi,hasVideo:this.trackDatas.some(r=>r.type==="video"),hasAudio:this.trackDatas.some(r=>r.type==="audio"),codecStrings:e})}getVideoTrackData(e,r,i){let a=this.trackDatas.find(l=>l.track===e);if(a)return a;sr(i),h(i),h(i.decoderConfig),h(i.decoderConfig.codedWidth!==void 0),h(i.decoderConfig.codedHeight!==void 0);let s=i.decoderConfig.displayAspectWidth,o=i.decoderConfig.displayAspectHeight,c=s===void 0||o===void 0?null:Ot({num:s,den:o}),u={track:e,type:"video",info:{width:i.decoderConfig.codedWidth,height:i.decoderConfig.codedHeight,aspectRatio:c,decoderConfig:i.decoderConfig,alphaMode:!!r.sideData.alpha},chunkQueue:[],lastWrittenMsTimestamp:null,closed:!1};return e.source._codec==="vp9"?u.info.decoderConfig={...u.info.decoderConfig,description:new Uint8Array(Du(u.info.decoderConfig.codec))}:e.source._codec==="av1"&&(u.info.decoderConfig={...u.info.decoderConfig,description:new Uint8Array(Qn(u.info.decoderConfig.codec))}),this.trackDatas.push(u),this.trackDatas.sort((l,m)=>l.track.id-m.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),u}getAudioTrackData(e,r,i){let a=this.trackDatas.find(u=>u.track===e);if(a)return a;Ee(i),h(i),h(i.decoderConfig);let s={...i.decoderConfig},o=!1;if(e.source._codec==="aac"&&!s.description){let u=De(he.tempFromBytes(r.data));if(!u)throw new Error("Couldn't parse ADTS header from the AAC packet. Make sure the packets are in ADTS format (as specified in ISO 13818-7) when not providing a description, or provide a description (must be an AudioSpecificConfig as specified in ISO 14496-3) and ensure the packets are raw AAC data.");let l=qe[u.samplingFrequencyIndex],m=kt[u.channelConfiguration];if(l===void 0||m===void 0)throw new Error("Invalid ADTS frame header.");s.description=ti({objectType:u.objectType,sampleRate:l,numberOfChannels:m}),o=!0}let c={track:e,type:"audio",info:{numberOfChannels:i.decoderConfig.numberOfChannels,sampleRate:i.decoderConfig.sampleRate,decoderConfig:s,requiresAdtsStripping:o},chunkQueue:[],lastWrittenMsTimestamp:null,closed:!1};return this.trackDatas.push(c),this.trackDatas.sort((u,l)=>u.track.id-l.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),c}getSubtitleTrackData(e,r){let i=this.trackDatas.find(s=>s.track===e);if(i)return i;Xn(r),h(r),h(r.config);let a={track:e,type:"subtitle",info:{config:r.config},chunkQueue:[],lastWrittenMsTimestamp:null,closed:!1};return this.trackDatas.push(a),this.trackDatas.sort((s,o)=>s.track.id-o.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),a}async addEncodedVideoPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getVideoTrackData(e,r,i),o=r.type==="key";this.validateTimestamp(s.track,r.timestamp,o);let c=r.timestamp,u=r.duration;e.metadata.frameRate!==void 0&&(c=bt(c,e.metadata.frameRate),u=bt(u,e.metadata.frameRate));let l=s.info.alphaMode?r.sideData.alpha??null:null,m=this.createInternalChunk(r.data,c,u,r.type,l);e.source._codec==="vp9"&&this.fixVP9ColorSpace(s,m),s.chunkQueue.push(m),await this.interleaveChunks()}finally{a()}}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getAudioTrackData(e,r,i),o=r.data;if(s.info.requiresAdtsStripping){let l=De(he.tempFromBytes(o));if(!l)throw new Error("Expected ADTS frame, didn't get one.");let m=l.crcCheck===null?Qt:Ne;o=o.subarray(m)}let c=r.type==="key";this.validateTimestamp(s.track,r.timestamp,c);let u=this.createInternalChunk(o,r.timestamp,r.duration,r.type);s.chunkQueue.push(u),await this.interleaveChunks()}finally{a()}}async addSubtitleCue(e,r,i){let a=await this.mutex.acquire();try{let s=this.getSubtitleTrackData(e,i);this.validateTimestamp(s.track,r.timestamp,!0);let o=r.text,c=Math.round(r.timestamp*1e3);Ri.lastIndex=0,o=o.replace(Ri,d=>{let p=Os(d.slice(1,-1))-c;return`<${Ds(p)}>`});let u=$.encode(o),l=`${r.settings??""}
37
+ `,a);c===-1&&(c=t.length);let u=Os(e[2]),m=Os(e[3])-u,d=t.slice(s,c).trim();t=t.slice(c).trimStart(),Fn.lastIndex=0;let f={timestamp:u/1e3,duration:m/1e3,text:d,identifier:i,settings:o,notes:r},p={};this.preambleEmitted||(p.config={description:this.preambleText},this.preambleEmitted=!0),this.options.output(f,p)}}},hf=/(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})/,Os=n=>{let t=hf.exec(n);if(!t)throw new Error("Expected match.");return 60*60*1e3*Number(t[1]||"0")+60*1e3*Number(t[2])+1e3*Number(t[3])+Number(t[4])},Ds=n=>{let t=Math.floor(n/36e5),e=Math.floor(n%(60*60*1e3)/(60*1e3)),r=Math.floor(n%(60*1e3)/1e3),i=n%1e3;return t.toString().padStart(2,"0")+":"+e.toString().padStart(2,"0")+":"+r.toString().padStart(2,"0")+"."+i.toString().padStart(3,"0")};var jr=class{constructor(t){this.writer=t;this.helper=new Uint8Array(8);this.helperView=new DataView(this.helper.buffer);this.offsets=new WeakMap}writeU32(t){this.helperView.setUint32(0,t,!1),this.writer.write(this.helper.subarray(0,4))}writeU64(t){this.helperView.setUint32(0,Math.floor(t/2**32),!1),this.helperView.setUint32(4,t,!1),this.writer.write(this.helper.subarray(0,8))}writeAscii(t){for(let e=0;e<t.length;e++)this.helperView.setUint8(e%8,t.charCodeAt(e)),e%8===7&&this.writer.write(this.helper);t.length%8!==0&&this.writer.write(this.helper.subarray(0,t.length%8))}writeBox(t){if(this.offsets.set(t,this.writer.getPos()),t.contents&&!t.children)this.writeBoxHeader(t,t.size??t.contents.byteLength+8),this.writer.write(t.contents);else{let e=this.writer.getPos();if(this.writeBoxHeader(t,0),t.contents&&this.writer.write(t.contents),t.children)for(let a of t.children)a&&this.writeBox(a);let r=this.writer.getPos(),i=t.size??r-e;this.writer.seek(e),this.writeBoxHeader(t,i),this.writer.seek(r)}}writeBoxHeader(t,e){this.writeU32(t.largeSize?1:e),this.writeAscii(t.type),t.largeSize&&this.writeU64(e)}measureBoxHeader(t){return 8+(t.largeSize?8:0)}patchBox(t){let e=this.offsets.get(t);h(e!==void 0);let r=this.writer.getPos();this.writer.seek(e),this.writeBox(t),this.writer.seek(r)}measureBox(t){if(t.contents&&!t.children)return this.measureBoxHeader(t)+t.contents.byteLength;{let e=this.measureBoxHeader(t);if(t.contents&&(e+=t.contents.byteLength),t.children)for(let r of t.children)r&&(e+=this.measureBox(r));return e}}},K=new Uint8Array(8),Xe=new DataView(K.buffer),le=n=>[(n%256+256)%256],L=n=>(Xe.setUint16(0,n,!1),[K[0],K[1]]),vc=n=>(Xe.setInt16(0,n,!1),[K[0],K[1]]),gd=n=>(Xe.setUint32(0,n,!1),[K[1],K[2],K[3]]),_=n=>(Xe.setUint32(0,n,!1),[K[0],K[1],K[2],K[3]]),Zt=n=>(Xe.setInt32(0,n,!1),[K[0],K[1],K[2],K[3]]),It=n=>(Xe.setUint32(0,Math.floor(n/2**32),!1),Xe.setUint32(4,n,!1),[K[0],K[1],K[2],K[3],K[4],K[5],K[6],K[7]]),gf=n=>(Xe.setInt32(0,Math.floor(n/2**32),!1),Xe.setUint32(4,n,!1),[K[0],K[1],K[2],K[3],K[4],K[5],K[6],K[7]]),bd=n=>(Xe.setInt16(0,2**8*n,!1),[K[0],K[1]]),ht=n=>(Xe.setInt32(0,2**16*n,!1),[K[0],K[1],K[2],K[3]]),Cc=n=>(Xe.setInt32(0,2**30*n,!1),[K[0],K[1],K[2],K[3]]),Ic=(n,t)=>{let e=[],r=n;do{let i=r&127;r>>=7,e.length>0&&(i|=128),e.push(i),t!==void 0&&t--}while(r>0||t);return e.reverse()},ie=(n,t=!1)=>{let e=Array(n.length).fill(null).map((r,i)=>n.charCodeAt(i));return t&&e.push(0),e},yd=n=>{let t=n*(Math.PI/180),e=Math.round(Math.cos(t)),r=Math.round(Math.sin(t));return[e,r,0,-r,e,0,0,0,1]},kd=yd(0),Td=n=>[ht(n[0]),ht(n[1]),Cc(n[2]),ht(n[3]),ht(n[4]),Cc(n[5]),ht(n[6]),ht(n[7]),Cc(n[8])],N=(n,t,e)=>({type:n,contents:t&&new Uint8Array(t.flat(10)),children:e}),Y=(n,t,e,r,i)=>N(n,[le(t),gd(e),r??[]],i),wd=n=>n.isQuickTime?N("ftyp",[ie("qt "),_(512),ie("qt ")]):n.fragmented?n.cmaf?N("ftyp",[ie("iso5"),_(512),ie("iso5"),ie("iso6"),ie("mp41"),ie("cmfc"),ie("dash")]):N("ftyp",[ie("iso5"),_(512),ie("iso5"),ie("iso6"),ie("mp41")]):N("ftyp",[ie("isom"),_(512),ie("isom"),n.holdsAvc?ie("avc1"):[],ie("mp41")]),_c=()=>N("styp",[ie("iso5"),_(0),ie("iso5"),ie("iso6"),ie("mp41"),ie("cmfc"),ie("dash")]),Rc=(n,t)=>{let e=n.maxWrittenEndTimestamp-n.minWrittenTimestamp;return Number.isFinite(e)||(e=0),Y("sidx",1,0,[_(1),_(je),It(oe(n.minWrittenTimestamp,je)),It(0),L(0),L(1),_(t&2147483647),_(oe(e,je)),_(0)])},Mn=n=>({type:"mdat",largeSize:n}),Sd=n=>({type:"free",size:n}),Fi=n=>N("moov",void 0,[bf(n.creationTime,n.trackDatas),...n.trackDatas.map(t=>yf(t,n.creationTime)),n.isFragmented?ip(n.trackDatas):null,mp(n)]),bf=(n,t)=>{let e=Math.max(0,...t.map(s=>oe(Vs(s),je)+oe(s.startTimestampOffset??0,je))),r=Math.max(0,...t.map(s=>s.track.id))+1,i=!_t(n)||!_t(e),a=i?It:_;return Y("mvhd",+i,0,[a(n),a(n),_(je),a(e),ht(1),bd(1),Array(10).fill(0),Td(kd),Array(24).fill(0),_(r)])},Vs=n=>{if(n.samples.length===0)return 0;let t=1/0,e=-1/0;for(let r=0;r<n.samples.length;r++){let i=n.samples[r];i.timestamp<t&&(t=i.timestamp),i.timestamp+i.duration>e&&(e=i.timestamp+i.duration)}return t===1/0?0:e-t},yf=(n,t)=>{let e=Rd(n),r=n.startTimestampOffset!==null&&n.startTimestampOffset>0;return N("trak",void 0,[kf(n,t),r?Tf(n,n.startTimestampOffset):null,wf(n,t),e.name!==void 0?N("udta",void 0,[N("name",[...$.encode(e.name)])]):null])},kf=(n,t)=>{let e=oe(Vs(n),je)+oe(n.startTimestampOffset??0,je),r=!_t(t)||!_t(e),i=r?It:_,a;if(n.type==="video"){let o=n.track.metadata.rotation;a=yd(o??0)}else a=kd;let s=2;return n.track.metadata.disposition?.default!==!1&&(s|=1),Y("tkhd",+r,s,[i(t),i(t),_(n.track.id),_(0),i(e),Array(8).fill(0),L(0),L(n.track.id),bd(n.type==="audio"?1:0),L(0),Td(a),ht(n.type==="video"?n.info.width:0),ht(n.type==="video"?n.info.height:0)])},Tf=(n,t)=>{let e=oe(t,je),r=oe(Vs(n),je),i=!_t(e)||!_t(r),a=i?It:_,s=i?gf:Zt;return N("edts",void 0,[Y("elst",i?1:0,0,[_(2),a(e),s(-1),ht(1),a(r),s(0),ht(1)])])},wf=(n,t)=>N("mdia",void 0,[Sf(n,t),Fc(!0,Af[n.type],xf[n.type]),Pf(n)]),Sf=(n,t)=>{let e=oe(Vs(n),n.timescale),r=!_t(t)||!_t(e),i=r?It:_;return Y("mdhd",+r,0,[i(t),i(t),_(n.timescale),i(e),L(_d(n.track.metadata.languageCode??J)),L(0)])},Af={video:"vide",audio:"soun",subtitle:"text"},xf={video:"MediabunnyVideoHandler",audio:"MediabunnySoundHandler",subtitle:"MediabunnyTextHandler"},Fc=(n,t,e,r="\0\0\0\0")=>Y("hdlr",0,0,[n?ie("mhlr"):_(0),ie(t),ie(r),_(0),_(0),ie(e,!0)]),Pf=n=>N("minf",void 0,[vf[n.type](),_f(),Mf(n)]),Cf=()=>Y("vmhd",0,1,[L(0),L(0),L(0),L(0)]),If=()=>Y("smhd",0,0,[L(0),L(0)]),Ef=()=>Y("nmhd",0,0),vf={video:Cf,audio:If,subtitle:Ef},_f=()=>N("dinf",void 0,[Rf()]),Rf=()=>Y("dref",0,0,[_(1)],[Ff()]),Ff=()=>Y("url ",0,1),Mf=n=>{let t=n.compositionTimeOffsetTable.length>1||n.compositionTimeOffsetTable.some(e=>e.sampleCompositionTimeOffset!==0);return N("stbl",void 0,[Bf(n),$f(n),t?tp(n):null,t?rp(n):null,Zf(n),Jf(n),ep(n),Yf(n)])},Bf=n=>{let t;if(n.type==="video")t=Of(gp(n.track.source._codec,n.info.decoderConfig.codec),n);else if(n.type==="audio"){let e=vd(n.track.source._codec,n.muxer.isQuickTime);h(e),t=Lf(e,n)}else n.type==="subtitle"&&(t=jf(kp[n.track.source._codec],n));return h(t),Y("stsd",0,0,[_(1)],[t])},Of=(n,t)=>N(n,[Array(6).fill(0),L(1),L(0),L(0),Array(12).fill(0),L(t.info.width),L(t.info.height),_(4718592),_(4718592),_(0),L(1),Array(32).fill(0),L(24),vc(65535)],[bp[t.track.source._codec](t),Df(t),zn(t.info.decoderConfig.colorSpace)?Vf(t):null]),Df=n=>n.info.pixelAspectRatio.num===n.info.pixelAspectRatio.den?null:N("pasp",[_(n.info.pixelAspectRatio.num),_(n.info.pixelAspectRatio.den)]),Vf=n=>N("colr",[ie("nclx"),L(et[n.info.decoderConfig.colorSpace.primaries]),L(tt[n.info.decoderConfig.colorSpace.transfer]),L(rt[n.info.decoderConfig.colorSpace.matrix]),le((n.info.decoderConfig.colorSpace.fullRange?1:0)<<7)]),Uf=n=>n.info.decoderConfig&&N("avcC",[...j(n.info.decoderConfig.description)]),zf=n=>n.info.decoderConfig&&N("hvcC",[...j(n.info.decoderConfig.description)]),pd=n=>{if(!n.info.decoderConfig)return null;let t=n.info.decoderConfig,e=t.codec.split("."),r=Number(e[1]),i=Number(e[2]),a=Number(e[3]),s=e[4]?Number(e[4]):1,o=e[8]?Number(e[8]):Number(t.colorSpace?.fullRange??0),c=(a<<4)+(s<<1)+o,u=e[5]?Number(e[5]):t.colorSpace?.primaries?et[t.colorSpace.primaries]:2,l=e[6]?Number(e[6]):t.colorSpace?.transfer?tt[t.colorSpace.transfer]:2,m=e[7]?Number(e[7]):t.colorSpace?.matrix?rt[t.colorSpace.matrix]:2;return Y("vpcC",1,0,[le(r),le(i),le(c),le(u),le(l),le(m),L(0)])},Nf=n=>N("av1C",Qn(n.info.decoderConfig.codec)),Lf=(n,t)=>{let e=0,r,i=16,a=ee.includes(t.track.source._codec);if(a){let s=t.track.source._codec,{sampleSize:o}=Se(s);i=8*o,i>16&&(e=1)}if(t.muxer.isQuickTime&&(e=1),e===0)r=[Array(6).fill(0),L(1),L(e),L(0),_(0),L(t.info.numberOfChannels),L(i),L(0),L(0),L(t.info.sampleRate<2**16?t.info.sampleRate:0),L(0)];else{let s=a?0:-2;r=[Array(6).fill(0),L(1),L(e),L(0),_(0),L(t.info.numberOfChannels),L(Math.min(i,16)),vc(s),L(0),L(t.info.sampleRate<2**16?t.info.sampleRate:0),L(0),a?[_(1),_(i/8),_(t.info.numberOfChannels*i/8)]:[_(0),_(0),_(0)],_(2)]}return N(n,r,[yp(t.track.source._codec,t.muxer.isQuickTime)?.(t)??null])},Ec=n=>{let t;switch(n.track.source._codec){case"aac":t=64;break;case"mp3":t=107;break;case"vorbis":t=221;break;default:throw new Error(`Unhandled audio codec: ${n.track.source._codec}`)}let e=[...le(t),...le(21),...gd(0),..._(0),..._(0)];if(n.info.decoderConfig.description){let r=j(n.info.decoderConfig.description);e=[...e,...le(5),...Ic(r.byteLength),...r]}return e=[...L(1),...le(0),...le(4),...Ic(e.length),...e,...le(6),...le(1),...le(2)],e=[...le(3),...Ic(e.length),...e],Y("esds",0,0,e)},Sr=n=>N("wave",void 0,[Wf(n),Hf(n),N("\0\0\0\0")]),Wf=n=>N("frma",[ie(vd(n.track.source._codec,n.muxer.isQuickTime))]),Hf=n=>{let{littleEndian:t}=Se(n.track.source._codec);return N("enda",[L(+t)])},qf=n=>{let t=n.info.numberOfChannels,e=3840,r=n.info.sampleRate,i=0,a=0,s=new Uint8Array(0),o=n.info.decoderConfig?.description;if(o){h(o.byteLength>=18);let c=j(o),u=lr(c);t=u.outputChannelCount,e=u.preSkip,r=u.inputSampleRate,i=u.outputGain,a=u.channelMappingFamily,u.channelMappingTable&&(s=u.channelMappingTable)}return N("dOps",[le(0),le(t),L(e),_(r),vc(i),le(a),...s])},Gf=n=>{let t=n.info.decoderConfig?.description;h(t);let e=j(t);return Y("dfLa",0,0,[...e.subarray(4)])},Pt=n=>{let{littleEndian:t,sampleSize:e}=Se(n.track.source._codec),r=+t;return Y("pcmC",0,0,[le(r),le(8*e)])},Kf=n=>{let t=sa(n.info.firstPacket.data);if(!t)throw new Error("Couldn't extract AC-3 frame info from the audio packet. Ensure the packets contain valid AC-3 sync frames (as specified in ETSI TS 102 366).");let e=new Uint8Array(3),r=new W(e);return r.writeBits(2,t.fscod),r.writeBits(5,t.bsid),r.writeBits(3,t.bsmod),r.writeBits(3,t.acmod),r.writeBits(1,t.lfeon),r.writeBits(5,t.bitRateCode),r.writeBits(5,0),N("dac3",[...e])},Qf=n=>{let t=oa(n.info.firstPacket.data);if(!t)throw new Error("Couldn't extract E-AC-3 frame info from the audio packet. Ensure the packets contain valid E-AC-3 sync frames (as specified in ETSI TS 102 366).");let e=16;for(let s of t.substreams)e+=23,s.numDepSub>0?e+=9:e+=1;let r=Math.ceil(e/8),i=new Uint8Array(r),a=new W(i);a.writeBits(13,t.dataRate),a.writeBits(3,t.substreams.length-1);for(let s of t.substreams)a.writeBits(2,s.fscod),a.writeBits(5,s.bsid),a.writeBits(1,0),a.writeBits(1,0),a.writeBits(3,s.bsmod),a.writeBits(3,s.acmod),a.writeBits(1,s.lfeon),a.writeBits(3,0),a.writeBits(4,s.numDepSub),s.numDepSub>0?a.writeBits(9,s.chanLoc):a.writeBits(1,0);return N("dec3",[...i])},jf=(n,t)=>N(n,[Array(6).fill(0),L(1)],[Tp[t.track.source._codec](t)]),Xf=n=>N("vttC",[...$.encode(n.info.config.description)]);var $f=n=>Y("stts",0,0,[_(n.timeToSampleTable.length),n.timeToSampleTable.map(t=>[_(t.sampleCount),_(t.sampleDelta)])]),Yf=n=>{if(n.samples.every(e=>e.type==="key"))return null;let t=[...n.samples.entries()].filter(([,e])=>e.type==="key");return Y("stss",0,0,[_(t.length),t.map(([e])=>_(e+1))])},Zf=n=>Y("stsc",0,0,[_(n.compactlyCodedChunkTable.length),n.compactlyCodedChunkTable.map(t=>[_(t.firstChunk),_(t.samplesPerChunk),_(1)])]),Jf=n=>{if(n.type==="audio"&&n.info.requiresPcmTransformation){let{sampleSize:t}=Se(n.track.source._codec);return Y("stsz",0,0,[_(t*n.info.numberOfChannels),_(n.samples.reduce((e,r)=>e+oe(r.duration,n.timescale),0))])}return Y("stsz",0,0,[_(0),_(n.samples.length),n.samples.map(t=>_(t.size))])},ep=n=>n.finalizedChunks.length>0&&X(n.finalizedChunks).offset>=2**32?Y("co64",0,0,[_(n.finalizedChunks.length),n.finalizedChunks.map(t=>It(t.offset))]):Y("stco",0,0,[_(n.finalizedChunks.length),n.finalizedChunks.map(t=>_(t.offset))]),tp=n=>Y("ctts",1,0,[_(n.compositionTimeOffsetTable.length),n.compositionTimeOffsetTable.map(t=>[_(t.sampleCount),Zt(t.sampleCompositionTimeOffset)])]),rp=n=>{let t=1/0,e=-1/0,r=1/0,i=-1/0;h(n.compositionTimeOffsetTable.length>0),h(n.samples.length>0);for(let s=0;s<n.compositionTimeOffsetTable.length;s++){let o=n.compositionTimeOffsetTable[s];t=Math.min(t,o.sampleCompositionTimeOffset),e=Math.max(e,o.sampleCompositionTimeOffset)}for(let s=0;s<n.samples.length;s++){let o=n.samples[s];r=Math.min(r,oe(o.timestamp,n.timescale)),i=Math.max(i,oe(o.timestamp+o.duration,n.timescale))}let a=Math.max(-t,0);return i>=2**31?null:Y("cslg",0,0,[Zt(a),Zt(t),Zt(e),Zt(r),Zt(i)])},ip=n=>N("mvex",void 0,n.map(np)),np=n=>Y("trex",0,0,[_(n.track.id),_(1),_(0),_(0),_(0)]),Mc=(n,t)=>N("moof",void 0,[ap(n),...t.map(sp)]),ap=n=>Y("mfhd",0,0,[_(n)]),Ad=n=>{let t=0,e=0,r=0,i=0,a=n.type==="delta";return e|=+a,a?t|=1:t|=2,t<<24|e<<16|r<<8|i},sp=n=>N("traf",void 0,[op(n),cp(n),up(n)]),op=n=>{h(n.currentChunk);let t=0;t|=8,t|=16,t|=32,t|=131072;let e=n.currentChunk.samples[1]??n.currentChunk.samples[0],r={duration:e.timescaleUnitsToNextSample,size:e.size,flags:Ad(e)};return Y("tfhd",0,t,[_(n.track.id),_(r.duration),_(r.size),_(r.flags)])},cp=n=>(h(n.currentChunk),Y("tfdt",1,0,[It(oe(n.currentChunk.startTimestamp,n.timescale))])),up=n=>{h(n.currentChunk);let t=n.currentChunk.samples.map(b=>b.timescaleUnitsToNextSample),e=n.currentChunk.samples.map(b=>b.size),r=n.currentChunk.samples.map(Ad),i=n.currentChunk.samples.map(b=>oe(b.timestamp-b.decodeTimestamp,n.timescale)),a=new Set(t),s=new Set(e),o=new Set(r),c=new Set(i),u=o.size===2&&r[0]!==r[1],l=a.size>1,m=s.size>1,d=!u&&o.size>1,f=c.size>1||[...c].some(b=>b!==0),p=0;return p|=1,p|=4*+u,p|=256*+l,p|=512*+m,p|=1024*+d,p|=2048*+f,Y("trun",1,p,[_(n.currentChunk.samples.length),_(n.currentChunk.offset-n.currentChunk.moofOffset||0),u?_(r[0]):[],n.currentChunk.samples.map((b,g)=>[l?_(t[g]):[],m?_(e[g]):[],d?_(r[g]):[],f?Zt(i[g]):[]])])},xd=n=>N("mfra",void 0,[...n.map(lp),dp()]),lp=(n,t)=>Y("tfra",1,0,[_(n.track.id),_(63),_(n.finalizedChunks.length),n.finalizedChunks.map(r=>[It(oe(r.samples[0].timestamp,n.timescale)),It(r.moofOffset),_(t+1),_(1),_(1)])]),dp=()=>Y("mfro",0,0,[_(0)]),Pd=()=>N("vtte"),Cd=(n,t,e,r,i)=>N("vttc",void 0,[i!==null?N("vsid",[Zt(i)]):null,e!==null?N("iden",[...$.encode(e)]):null,t!==null?N("ctim",[...$.encode(Ds(t))]):null,r!==null?N("sttg",[...$.encode(r)]):null,N("payl",[...$.encode(n)])]),Id=n=>N("vtta",[...$.encode(n)]),mp=n=>{let t=[],e=n.format._options.metadataFormat??"auto",r=n.output._metadataTags;if(e==="mdir"||e==="auto"&&!n.isQuickTime){let i=pp(r);i&&t.push(i)}else if(e==="mdta"){let i=hp(r);i&&t.push(i)}else(e==="udta"||e==="auto"&&n.isQuickTime)&&fp(t,n.output._metadataTags);return t.length===0?null:N("udta",void 0,t)},fp=(n,t)=>{for(let{key:e,value:r}of it(t))switch(e){case"title":n.push(Ct("\xA9nam",r));break;case"description":n.push(Ct("\xA9des",r));break;case"artist":n.push(Ct("\xA9ART",r));break;case"album":n.push(Ct("\xA9alb",r));break;case"albumArtist":n.push(Ct("albr",r));break;case"genre":n.push(Ct("\xA9gen",r));break;case"date":n.push(Ct("\xA9day",r.toISOString().slice(0,10)));break;case"comment":n.push(Ct("\xA9cmt",r));break;case"lyrics":n.push(Ct("\xA9lyr",r));break;case"raw":break;case"discNumber":case"discsTotal":case"trackNumber":case"tracksTotal":case"images":break;default:ne(e)}if(t.raw)for(let e in t.raw){let r=t.raw[e];r==null||e.length!==4||n.some(i=>i.type===e)||(typeof r=="string"?n.push(Ct(e,r)):r instanceof Uint8Array&&n.push(N(e,Array.from(r))))}},Ct=(n,t)=>{let e=$.encode(t);return N(n,[L(e.length),L(_d("und")),Array.from(e)])},hd={"image/jpeg":13,"image/png":14,"image/bmp":27},Ed=(n,t)=>{let e=[];for(let{key:r,value:i}of it(n))switch(r){case"title":e.push({key:t?"title":"\xA9nam",value:pt(i)});break;case"description":e.push({key:t?"description":"\xA9des",value:pt(i)});break;case"artist":e.push({key:t?"artist":"\xA9ART",value:pt(i)});break;case"album":e.push({key:t?"album":"\xA9alb",value:pt(i)});break;case"albumArtist":e.push({key:t?"album_artist":"aART",value:pt(i)});break;case"comment":e.push({key:t?"comment":"\xA9cmt",value:pt(i)});break;case"genre":e.push({key:t?"genre":"\xA9gen",value:pt(i)});break;case"lyrics":e.push({key:t?"lyrics":"\xA9lyr",value:pt(i)});break;case"date":e.push({key:t?"date":"\xA9day",value:pt(i.toISOString().slice(0,10))});break;case"images":for(let a of i)a.kind==="coverFront"&&e.push({key:"covr",value:N("data",[_(hd[a.mimeType]??0),_(0),Array.from(a.data)])});break;case"trackNumber":if(t){let a=n.tracksTotal!==void 0?`${i}/${n.tracksTotal}`:i.toString();e.push({key:"track",value:pt(a)})}else e.push({key:"trkn",value:N("data",[_(0),_(0),L(0),L(i),L(n.tracksTotal??0),L(0)])});break;case"discNumber":t||e.push({key:"disc",value:N("data",[_(0),_(0),L(0),L(i),L(n.discsTotal??0),L(0)])});break;case"tracksTotal":case"discsTotal":break;case"raw":break;default:ne(r)}if(n.raw)for(let r in n.raw){let i=n.raw[r];i==null||!t&&r.length!==4||e.some(a=>a.key===r)||(typeof i=="string"?e.push({key:r,value:pt(i)}):i instanceof Uint8Array?e.push({key:r,value:N("data",[_(0),_(0),Array.from(i)])}):i instanceof He&&e.push({key:r,value:N("data",[_(hd[i.mimeType]??0),_(0),Array.from(i.data)])}))}return e},pp=n=>{let t=Ed(n,!1);return t.length===0?null:Y("meta",0,0,void 0,[Fc(!1,"mdir","","appl"),N("ilst",void 0,t.map(e=>N(e.key,void 0,[e.value])))])},hp=n=>{let t=Ed(n,!0);return t.length===0?null:N("meta",void 0,[Fc(!1,"mdta",""),Y("keys",0,0,[_(t.length)],t.map(e=>N("mdta",[...$.encode(e.key)]))),N("ilst",void 0,t.map((e,r)=>{let i=String.fromCharCode(..._(r+1));return N(i,void 0,[e.value])}))])},pt=n=>N("data",[_(1),_(0),...$.encode(n)]),gp=(n,t)=>{switch(n){case"avc":return t.startsWith("avc3")?"avc3":"avc1";case"hevc":return"hvc1";case"vp8":return"vp08";case"vp9":return"vp09";case"av1":return"av01"}},bp={avc:Uf,hevc:zf,vp8:pd,vp9:pd,av1:Nf},vd=(n,t)=>{switch(n){case"aac":return"mp4a";case"mp3":return"mp4a";case"opus":return"Opus";case"vorbis":return"mp4a";case"flac":return"fLaC";case"ulaw":return"ulaw";case"alaw":return"alaw";case"pcm-u8":return"raw ";case"pcm-s8":return"sowt";case"ac3":return"ac-3";case"eac3":return"ec-3"}if(t)switch(n){case"pcm-s16":return"sowt";case"pcm-s16be":return"twos";case"pcm-s24":return"in24";case"pcm-s24be":return"in24";case"pcm-s32":return"in32";case"pcm-s32be":return"in32";case"pcm-f32":return"fl32";case"pcm-f32be":return"fl32";case"pcm-f64":return"fl64";case"pcm-f64be":return"fl64"}else switch(n){case"pcm-s16":return"ipcm";case"pcm-s16be":return"ipcm";case"pcm-s24":return"ipcm";case"pcm-s24be":return"ipcm";case"pcm-s32":return"ipcm";case"pcm-s32be":return"ipcm";case"pcm-f32":return"fpcm";case"pcm-f32be":return"fpcm";case"pcm-f64":return"fpcm";case"pcm-f64be":return"fpcm"}},yp=(n,t)=>{switch(n){case"aac":return Ec;case"mp3":return Ec;case"opus":return qf;case"vorbis":return Ec;case"flac":return Gf;case"ac3":return Kf;case"eac3":return Qf}if(t)switch(n){case"pcm-s24":return Sr;case"pcm-s24be":return Sr;case"pcm-s32":return Sr;case"pcm-s32be":return Sr;case"pcm-f32":return Sr;case"pcm-f32be":return Sr;case"pcm-f64":return Sr;case"pcm-f64be":return Sr}else switch(n){case"pcm-s16":return Pt;case"pcm-s16be":return Pt;case"pcm-s24":return Pt;case"pcm-s24be":return Pt;case"pcm-s32":return Pt;case"pcm-s32be":return Pt;case"pcm-f32":return Pt;case"pcm-f32be":return Pt;case"pcm-f64":return Pt;case"pcm-f64be":return Pt}return null},kp={webvtt:"wvtt"},Tp={webvtt:Xf},_d=n=>{h(n.length===3);let t=0;for(let e=0;e<3;e++)t<<=5,t+=n.charCodeAt(e)-96;return t};var Et=class{constructor(t,e){this.finalized=!1;this.started=!1;this.pos=0;this.trackedWrites=null;this.trackedStart=-1;this.trackedEnd=-1;if(t._writerAcquired)throw new Error("Can't have multiple Writers for the same Target.");this.target=t,t._setMonotonicity(e),t._writerAcquired=!0}start(){h(!this.started),this.target._start(),this.started=!0}write(t){h(this.started&&!this.finalized),this.maybeTrackWrites(t),this.target._write(t,this.pos),this.pos+=t.byteLength}seek(t){this.pos=t}getPos(){return this.pos}async flush(){return h(this.started&&!this.finalized),this.target._flush()}async finalize(){h(this.started&&!this.finalized),await this.target._finalize(),this.finalized=!0}maybeTrackWrites(t){if(!this.trackedWrites)return;let e=this.getPos();if(e<this.trackedStart){if(e+t.byteLength<=this.trackedStart)return;t=t.subarray(this.trackedStart-e),e=0}let r=e+t.byteLength-this.trackedStart,i=this.trackedWrites.byteLength;for(;i<r;)i*=2;if(i!==this.trackedWrites.byteLength){let a=new Uint8Array(i);a.set(this.trackedWrites,0),this.trackedWrites=a}this.trackedWrites.set(t,e-this.trackedStart),this.trackedEnd=Math.max(this.trackedEnd,e+t.byteLength)}startTrackingWrites(){this.trackedWrites=new Uint8Array(2**10),this.trackedStart=this.getPos(),this.trackedEnd=this.trackedStart}stopTrackingWrites(){if(!this.trackedWrites)throw new Error("Internal error: Can't get tracked writes since nothing was tracked.");let e={data:this.trackedWrites.subarray(0,this.trackedEnd-this.trackedStart),start:this.trackedStart,end:this.trackedEnd};return this.trackedWrites=null,e}};var Fd=cu(Xo(),1);var Md=typeof Fd<"u"?Fd:void 0,Ce=class extends Ye{constructor(){super(...arguments);this._writerAcquired=!1;this._monotonicity=null;this.onwrite=null}_setMonotonicity(e){this._monotonicity!==!1&&(this._monotonicity=e)}_dispatchWrite(e,r){this.onwrite?.(e,r),this._emit("write",{start:e,end:r})}slice(e){if(!Number.isInteger(e)||e<0)throw new TypeError("offset must be a non-negative integer.");return new Us(this,e)}},Bc=2**16,Oc=2**32,Xr=class extends Ce{constructor(e={}){super();this.buffer=null;this._maxPos=0;if(!e||typeof e!="object")throw new TypeError("BufferTarget options, when provided, must be an object.");if(e.onFinalize!==void 0&&typeof e.onFinalize!="function")throw new TypeError("options.onFinalize, when provided, must be a function.");if(this._options=e,this._supportsResize="resize"in new ArrayBuffer(0),this._supportsResize)try{this._buffer=new ArrayBuffer(Bc,{maxByteLength:Oc})}catch{this._buffer=new ArrayBuffer(Bc),this._supportsResize=!1}else this._buffer=new ArrayBuffer(Bc);this._bytes=new Uint8Array(this._buffer)}_ensureSize(e){let r=this._buffer.byteLength;for(;r<e;)r*=2;if(r!==this._buffer.byteLength){if(r>Oc)throw new Error(`ArrayBuffer exceeded maximum size of ${Oc} bytes. Please consider using another target.`);if(this._supportsResize)this._buffer.resize(r);else{let i=new ArrayBuffer(r),a=new Uint8Array(i);a.set(this._bytes,0),this._buffer=i,this._bytes=a}}}_start(){}_write(e,r){this._ensureSize(r+e.byteLength),this._bytes.set(e,r),this._maxPos=Math.max(this._maxPos,r+e.byteLength),this._dispatchWrite(r,r+e.byteLength)}async _flush(){}async _finalize(){this.buffer=this._buffer.slice(0,this._maxPos),this._options.onFinalize&&await this._options.onFinalize(this.buffer),this._emit("finalized")}async _close(){}_getSlice(e,r){return this._bytes.slice(e,r)}},wp=2**24,Sp=2,Bn=class extends Ce{constructor(e,r={}){super();this._sections=[];this._lastWriteEnd=0;this._lastFlushEnd=0;this._streamWriter=null;this._writeError=null;this._chunks=[];if(!(e instanceof WritableStream))throw new TypeError("StreamTarget requires a WritableStream instance.");if(r!=null&&typeof r!="object")throw new TypeError("StreamTarget options, when provided, must be an object.");if(r.chunked!==void 0&&typeof r.chunked!="boolean")throw new TypeError("options.chunked, when provided, must be a boolean.");if(r.chunkSize!==void 0&&(!Number.isInteger(r.chunkSize)||r.chunkSize<1024))throw new TypeError("options.chunkSize, when provided, must be an integer and not smaller than 1024.");this._writable=e,this._options=r,this._chunked=r.chunked??!1,this._chunkSize=r.chunkSize??wp}_start(){this._streamWriter=this._writable.getWriter()}_write(e,r){if(r>this._lastWriteEnd){let i=r-this._lastWriteEnd;this._write(new Uint8Array(i),this._lastWriteEnd)}this._sections.push({data:e.slice(),start:r}),this._lastWriteEnd=Math.max(this._lastWriteEnd,r+e.byteLength),this._dispatchWrite(r,r+e.byteLength)}async _flush(){if(this._writeError!==null)throw this._writeError;if(h(this._streamWriter),this._sections.length===0)return;let e=[],r=[...this._sections].sort((i,a)=>i.start-a.start);e.push({start:r[0].start,size:r[0].data.byteLength});for(let i=1;i<r.length;i++){let a=e[e.length-1],s=r[i];s.start<=a.start+a.size?a.size=Math.max(a.size,s.start+s.data.byteLength-a.start):e.push({start:s.start,size:s.data.byteLength})}for(let i of e){i.data=new Uint8Array(i.size);for(let a of this._sections)i.start<=a.start&&a.start<i.start+i.size&&i.data.set(a.data,a.start-i.start);if(this._streamWriter.desiredSize!==null&&this._streamWriter.desiredSize<=0&&await this._streamWriter.ready,this._chunked)this._writeDataIntoChunks(i.data,i.start),this._tryToFlushChunks();else{if(this._monotonicity===!0&&i.start!==this._lastFlushEnd)throw new Error("Internal error: Monotonicity violation.");this._streamWriter.write({type:"write",data:i.data,position:i.start}).catch(a=>{this._writeError??=a}),this._lastFlushEnd=i.start+i.data.byteLength}}this._sections.length=0}_writeDataIntoChunks(e,r){let i=this._chunks.findIndex(u=>u.start<=r&&r<u.start+this._chunkSize);i===-1&&(i=this._createChunk(r));let a=this._chunks[i],s=r-a.start,o=e.subarray(0,Math.min(this._chunkSize-s,e.byteLength));a.data.set(o,s);let c={start:s,end:s+o.byteLength};if(this._insertSectionIntoChunk(a,c),a.written[0].start===0&&a.written[0].end===this._chunkSize&&(a.shouldFlush=!0),this._chunks.length>Sp){for(let u=0;u<this._chunks.length-1;u++)this._chunks[u].shouldFlush=!0;this._tryToFlushChunks()}o.byteLength<e.byteLength&&this._writeDataIntoChunks(e.subarray(o.byteLength),r+o.byteLength)}_insertSectionIntoChunk(e,r){let i=0,a=e.written.length-1,s=-1;for(;i<=a;){let o=Math.floor(i+(a-i+1)/2);e.written[o].start<=r.start?(i=o+1,s=o):a=o-1}for(e.written.splice(s+1,0,r),(s===-1||e.written[s].end<r.start)&&s++;s<e.written.length-1&&e.written[s].end>=e.written[s+1].start;)e.written[s].end=Math.max(e.written[s].end,e.written[s+1].end),e.written.splice(s+1,1)}_createChunk(e){let i={start:Math.floor(e/this._chunkSize)*this._chunkSize,data:new Uint8Array(this._chunkSize),written:[],shouldFlush:!1};return this._chunks.push(i),this._chunks.sort((a,s)=>a.start-s.start),this._chunks.indexOf(i)}_tryToFlushChunks(e=!1){h(this._streamWriter);for(let r=0;r<this._chunks.length;r++){let i=this._chunks[r];if(!(!i.shouldFlush&&!e)){for(let a of i.written){let s=i.start+a.start;if(this._monotonicity===!0&&s!==this._lastFlushEnd)throw new Error("Internal error: Monotonicity violation.");this._streamWriter.write({type:"write",data:i.data.subarray(a.start,a.end),position:s}).catch(o=>{this._writeError??=o}),this._lastFlushEnd=i.start+a.end}this._chunks.splice(r--,1)}}}async _finalize(){if(this._chunked&&this._tryToFlushChunks(!0),this._writeError!==null)throw this._writeError;h(this._streamWriter),await this._streamWriter.ready,await this._streamWriter.close(),this._emit("finalized")}async _close(){return this._streamWriter?.close()}},Dc=class extends Ce{constructor(e){super();this._writer=null;this._nextWritePos=0;this._writable=e,this._streamTarget=new Bn(new WritableStream({start:()=>{this._writer=this._writable.getWriter()},write:r=>{if(this._monotonicity!==!0)throw new Error("AppendOnlyStreamTarget requires that data be written monotonically (always appended to the end). You must use a format that guarantees this behavior.");return h(r.position===this._nextWritePos),this._nextWritePos+=r.data.byteLength,h(this._writer),this._writer.write(r.data)},close:()=>this._writer?.close()}))}_start(){this._streamTarget._start()}_write(e,r){this._streamTarget._write(e,r)}_flush(){return this._streamTarget._flush()}_finalize(){return this._streamTarget._finalize()}_close(){return this._streamTarget._close()}_setMonotonicity(e){super._setMonotonicity(e),this._streamTarget._setMonotonicity(e)}},Vc=class extends Ce{constructor(e,r={}){if(typeof e!="string")throw new TypeError("filePath must be a string.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(!Md.fs)throw new Error("FilePathTarget is only available in server-side environments (Node.js, Bun, Deno).");super();this._fileHandle=null;let i=new WritableStream({start:async()=>{this._fileHandle=await Md.fs.open(e,"w")},write:async a=>{h(this._fileHandle),await this._fileHandle.write(a.data,0,a.data.byteLength,a.position)},close:async()=>{this._fileHandle&&(await this._fileHandle.close(),this._fileHandle=null)}});this._streamTarget=new Bn(i,{chunked:!0,...r})}_start(){this._streamTarget._start()}_write(e,r){this._streamTarget._write(e,r),this._dispatchWrite(r,r+e.byteLength)}async _flush(){return this._streamTarget._flush()}async _finalize(){await this._streamTarget._finalize(),this._emit("finalized")}async _close(){return this._streamTarget._close()}_setMonotonicity(e){super._setMonotonicity(e),this._streamTarget._setMonotonicity(e)}},$r=class extends Ce{_start(){}_write(t,e){this._dispatchWrite(e,e+t.byteLength)}async _flush(){}async _finalize(){this._emit("finalized")}async _close(){}},Us=class extends Ce{constructor(t,e){super(),this._baseTarget=t,this._offset=e}_start(){}_write(t,e){this._baseTarget._write(t,this._offset+e),this._dispatchWrite(e,e+t.byteLength)}_flush(){return this._baseTarget._flush()}async _finalize(){this._emit("finalized")}async _close(){}_setMonotonicity(t){super._setMonotonicity(t),this._baseTarget._setMonotonicity(t)}},Le=class{constructor(t,e){this.rootPath=t;this.getTarget=e;if(typeof t!="string")throw new TypeError("rootPath must be a string.");if(typeof e!="function")throw new TypeError("getTarget must be a function.")}};var je=57600,Ap=2082844800,Rd=n=>{let t={},e=n.track;return e.metadata.name!==void 0&&(t.name=e.metadata.name),t},oe=(n,t,e=!0)=>{let r=n*t;return e?Math.round(r):r},zs=class extends ge{constructor(e,r){super(e);this.writer=null;this.boxWriter=null;this.initWriter=null;this.initBoxWriter=null;this.auxTarget=new Xr;this.auxWriter=new Et(this.auxTarget,!1);this.auxBoxWriter=new jr(this.auxWriter);this.mdat=null;this.ftypSize=null;this.trackDatas=[];this.allTracksKnown=Z();this.creationTime=Math.floor(Date.now()/1e3)+Ap;this.finalizedChunks=[];this.nextFragmentNumber=1;this.maxWrittenTimestamp=-1/0;this.minWrittenTimestamp=1/0;this.maxWrittenEndTimestamp=-1/0;this.segmentHeaderSize=null;this.format=r,this.isQuickTime=r instanceof Bi,this.isCmaf=r instanceof Mi,this.minimumFragmentDuration=r._options.minimumFragmentDuration??(r instanceof Mi?1/0:1)}async start(){let e=await this.mutex.acquire();if(this.isCmaf?(this.fastStart="fragmented",this.isFragmented=!0):(this.writer=await this.output._getRootWriter(i=>this.format._options.fastStart!==void 0?this.format._options.fastStart==="fragmented":i instanceof Xr),this.boxWriter=new jr(this.writer),this.fastStart=this.format._options.fastStart??(this.writer.target instanceof Xr?"in-memory":!1),this.isFragmented=this.fastStart==="fragmented"),this.isCmaf){if(!this.output._hasInitTarget())throw new Error("CMAF outputs require the initTarget field in OutputOptions to be set; the init segment will be written to it.");let i=await this.output._getInitTarget(),a=new Et(i,!0);a.start(),this.initWriter=a,this.initBoxWriter=new jr(a)}let r=this.output._tracks.some(i=>i.isVideoTrack()&&i.source._codec==="avc");{let i=this.initBoxWriter??this.boxWriter;if(h(i),this.format._options.onFtyp&&i.writer.startTrackingWrites(),i.writeBox(wd({isQuickTime:this.isQuickTime,holdsAvc:r,fragmented:this.isFragmented,cmaf:this.isCmaf})),this.format._options.onFtyp){let{data:a,start:s}=i.writer.stopTrackingWrites();this.format._options.onFtyp(a,s)}this.ftypSize=i.writer.getPos(),this.isCmaf&&await this.initWriter.flush()}if(this.fastStart!=="in-memory")if(this.fastStart==="reserve"){for(let i of this.output._tracks)if(i.metadata.maximumPacketCount===void 0)throw new Error("All tracks must specify maximumPacketCount in their metadata when using fastStart: 'reserve'.")}else this.isFragmented||(h(this.writer),h(this.boxWriter),this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat=Mn(!0),this.boxWriter.writeBox(this.mdat));await this.writer?.flush(),e()}allTracksAreKnown(){for(let e of this.output._tracks)if(!e.source._closed&&!this.trackDatas.some(r=>r.track===e))return!1;return!0}async getMimeType(){await this.allTracksKnown.promise;let e=this.trackDatas.map(r=>r.type==="video"||r.type==="audio"?r.info.decoderConfig.codec:{webvtt:"wvtt"}[r.track.source._codec]);return la({isQuickTime:this.isQuickTime,hasVideo:this.trackDatas.some(r=>r.type==="video"),hasAudio:this.trackDatas.some(r=>r.type==="audio"),codecStrings:e})}getVideoTrackData(e,r,i){let a=this.trackDatas.find(f=>f.track===e);if(a)return a;sr(i),h(i),h(i.decoderConfig);let s={...i.decoderConfig};h(s.codedWidth!==void 0),h(s.codedHeight!==void 0);let o=!1;if(e.source._codec==="avc"&&!s.description){let f=si(r.data);if(!f)throw new Error("Couldn't extract an AVCDecoderConfigurationRecord from the AVC packet. Make sure the packets are in Annex B format (as specified in ITU-T-REC-H.264) when not providing a description, or provide a description (must be an AVCDecoderConfigurationRecord as specified in ISO 14496-15) and ensure the packets are in AVCC format.");s.description=Qu(f),o=!0}else if(e.source._codec==="hevc"&&!s.description){let f=oi(r.data);if(!f)throw new Error("Couldn't extract an HEVCDecoderConfigurationRecord from the HEVC packet. Make sure the packets are in Annex B format (as specified in ITU-T-REC-H.265) when not providing a description, or provide a description (must be an HEVCDecoderConfigurationRecord as specified in ISO 14496-15) and ensure the packets are in HEVC format.");s.description=Xu(f),o=!0}let c=Su(1/(e.metadata.frameRate??je),1e6).den,u=s.displayAspectWidth,l=s.displayAspectHeight,m=u===void 0||l===void 0?{num:1,den:1}:Ot({num:u*s.codedHeight,den:l*s.codedWidth}),d={muxer:this,track:e,type:"video",info:{width:s.codedWidth,height:s.codedHeight,pixelAspectRatio:m,decoderConfig:s,requiresAnnexBTransformation:o},timescale:c,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,startTimestampOffset:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[],closed:!1};return this.trackDatas.push(d),this.trackDatas.sort((f,p)=>f.track.id-p.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),d}getAudioTrackData(e,r,i){let a=this.trackDatas.find(u=>u.track===e);if(a)return a;Ee(i),h(i),h(i.decoderConfig);let s={...i.decoderConfig},o=!1;if(e.source._codec==="aac"&&!s.description){let u=De(he.tempFromBytes(r.data));if(!u)throw new Error("Couldn't parse ADTS header from the AAC packet. Make sure the packets are in ADTS format (as specified in ISO 13818-7) when not providing a description, or provide a description (must be an AudioSpecificConfig as specified in ISO 14496-3) and ensure the packets are raw AAC data.");let l=qe[u.samplingFrequencyIndex],m=kt[u.channelConfiguration];if(l===void 0||m===void 0)throw new Error("Invalid ADTS frame header.");s.description=ri({objectType:u.objectType,sampleRate:l,numberOfChannels:m}),o=!0}let c={muxer:this,track:e,type:"audio",info:{numberOfChannels:i.decoderConfig.numberOfChannels,sampleRate:i.decoderConfig.sampleRate,decoderConfig:s,requiresPcmTransformation:!this.isFragmented&&ee.includes(e.source._codec),expectedNextPcmPacketTimestamp:null,requiresAdtsStripping:o,firstPacket:r},timescale:s.sampleRate,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,startTimestampOffset:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[],closed:!1};return this.trackDatas.push(c),this.trackDatas.sort((u,l)=>u.track.id-l.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),c}getSubtitleTrackData(e,r){let i=this.trackDatas.find(s=>s.track===e);if(i)return i;Xn(r),h(r),h(r.config);let a={muxer:this,track:e,type:"subtitle",info:{config:r.config},timescale:1e3,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,startTimestampOffset:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[],closed:!1,lastCueEndTimestamp:0,cueQueue:[],nextSourceId:0,cueToSourceId:new WeakMap};return this.trackDatas.push(a),this.trackDatas.sort((s,o)=>s.track.id-o.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),a}async addEncodedVideoPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getVideoTrackData(e,r,i),o=r.data;if(s.info.requiresAnnexBTransformation){let u=[...ur(o)].map(l=>o.subarray(l.offset,l.offset+l.length));if(u.length===0)throw new Error("Failed to transform packet data. Make sure all packets are provided in Annex B format, as specified in ITU-T-REC-H.264 and ITU-T-REC-H.265.");o=ta(u,4)}this.validateTimestamp(s.track,r.timestamp,r.type==="key");let c=this.createSampleForTrack(s,o,r.timestamp,r.duration,r.type);await this.registerSample(s,c)}finally{a()}}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getAudioTrackData(e,r,i),o=r.data;if(s.info.requiresAdtsStripping){let m=De(he.tempFromBytes(o));if(!m)throw new Error("Expected ADTS frame, didn't get one.");let d=m.crcCheck===null?Qt:Ne;o=o.subarray(d)}this.validateTimestamp(s.track,r.timestamp,r.type==="key");let c=r.timestamp,u=r.duration;if(s.info.requiresPcmTransformation){let d=Se(s.info.decoderConfig.codec).sampleSize*s.info.numberOfChannels;if(u=o.byteLength/d/s.info.sampleRate,s.info.expectedNextPcmPacketTimestamp!==null){let f=c-s.info.expectedNextPcmPacketTimestamp;if(f<.01)c=s.info.expectedNextPcmPacketTimestamp;else{let p=await this.padWithSilence(s,s.info.expectedNextPcmPacketTimestamp,f);c=s.info.expectedNextPcmPacketTimestamp+p}}s.info.expectedNextPcmPacketTimestamp=c+u}let l=this.createSampleForTrack(s,o,c,u,r.type);await this.registerSample(s,l)}finally{a()}}async padWithSilence(e,r,i){let a=oe(i,e.timescale);if(i=a/e.timescale,a>0){let{sampleSize:s,silentValue:o}=Se(e.info.decoderConfig.codec),c=a*e.info.numberOfChannels,u=new Uint8Array(s*c).fill(o),l=this.createSampleForTrack(e,new Uint8Array(u.buffer),r,i,"key");await this.registerSample(e,l)}return i}async addSubtitleCue(e,r,i){let a=await this.mutex.acquire();try{let s=this.getSubtitleTrackData(e,i);this.validateTimestamp(s.track,r.timestamp,!0),e.source._codec==="webvtt"&&(s.cueQueue.push(r),await this.processWebVTTCues(s,r.timestamp))}finally{a()}}async processWebVTTCues(e,r){for(;e.cueQueue.length>0;){let i=new Set([]);for(let l of e.cueQueue)h(l.timestamp<=r),h(e.lastCueEndTimestamp<=l.timestamp+l.duration),i.add(Math.max(l.timestamp,e.lastCueEndTimestamp)),i.add(l.timestamp+l.duration);let a=[...i].sort((l,m)=>l-m),s=a[0],o=a[1]??s;if(r<o)break;if(e.lastCueEndTimestamp<s){this.auxWriter.seek(0);let l=Pd();this.auxBoxWriter.writeBox(l);let m=this.auxTarget._getSlice(0,this.auxWriter.getPos()),d=this.createSampleForTrack(e,m,e.lastCueEndTimestamp,s-e.lastCueEndTimestamp,"key");await this.registerSample(e,d),e.lastCueEndTimestamp=s}this.auxWriter.seek(0);for(let l=0;l<e.cueQueue.length;l++){let m=e.cueQueue[l];if(m.timestamp>=o)break;Ri.lastIndex=0;let d=Ri.test(m.text),f=m.timestamp+m.duration,p=e.cueToSourceId.get(m);if(p===void 0&&o<f&&(p=e.nextSourceId++,e.cueToSourceId.set(m,p)),m.notes){let g=Id(m.notes);this.auxBoxWriter.writeBox(g)}let b=Cd(m.text,d?s:null,m.identifier??null,m.settings??null,p??null);this.auxBoxWriter.writeBox(b),f===o&&e.cueQueue.splice(l--,1)}let c=this.auxTarget._getSlice(0,this.auxWriter.getPos()),u=this.createSampleForTrack(e,c,s,o-s,"key");await this.registerSample(e,u),e.lastCueEndTimestamp=o}}createSampleForTrack(e,r,i,a,s){return{timestamp:i,decodeTimestamp:i,duration:a,data:r,size:r.byteLength,type:s,timescaleUnitsToNextSample:oe(a,e.timescale)}}processTimestamps(e,r){if(e.timestampProcessingQueue.length===0)return;if(e.type==="audio"&&e.info.requiresPcmTransformation){this.isFragmented||(e.startTimestampOffset??=e.timestampProcessingQueue[0].timestamp);let a=0;for(let s=0;s<e.timestampProcessingQueue.length;s++){let o=e.timestampProcessingQueue[s],c=oe(o.duration,e.timescale);a+=c}if(e.timeToSampleTable.length===0)e.timeToSampleTable.push({sampleCount:a,sampleDelta:1});else{let s=X(e.timeToSampleTable);s.sampleCount+=a}e.timestampProcessingQueue.length=0;return}let i=e.timestampProcessingQueue.map(a=>a.timestamp).sort((a,s)=>a-s);this.isFragmented||(e.startTimestampOffset??=i[0]);for(let a=0;a<e.timestampProcessingQueue.length;a++){let s=e.timestampProcessingQueue[a];s.decodeTimestamp=i[a];let o=oe(s.timestamp-s.decodeTimestamp,e.timescale),c=oe(s.duration,e.timescale);if(e.lastTimescaleUnits!==null){h(e.lastSample);let u=oe(s.decodeTimestamp,e.timescale,!1),l=Math.round(u-e.lastTimescaleUnits);if(h(l>=0),e.lastTimescaleUnits+=l,e.lastSample.timescaleUnitsToNextSample=l,!this.isFragmented){let m=X(e.timeToSampleTable);if(h(m),m.sampleCount===1){m.sampleDelta=l;let f=e.timeToSampleTable[e.timeToSampleTable.length-2];f&&f.sampleDelta===l&&(f.sampleCount++,e.timeToSampleTable.pop(),m=f)}else m.sampleDelta!==l&&(m.sampleCount--,e.timeToSampleTable.push(m={sampleCount:1,sampleDelta:l}));m.sampleDelta===c?m.sampleCount++:e.timeToSampleTable.push({sampleCount:1,sampleDelta:c});let d=X(e.compositionTimeOffsetTable);h(d),d.sampleCompositionTimeOffset===o?d.sampleCount++:e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:o})}}else e.lastTimescaleUnits=oe(s.decodeTimestamp,e.timescale,!1),this.isFragmented||(e.timeToSampleTable.push({sampleCount:1,sampleDelta:c}),e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:o}));e.lastSample=s}if(e.timestampProcessingQueue.length=0,h(e.lastSample),h(e.lastTimescaleUnits!==null),r!==void 0&&e.lastSample.timescaleUnitsToNextSample===0){h(r.type==="key");let a=oe(r.timestamp,e.timescale,!1),s=Math.round(a-e.lastTimescaleUnits);e.lastSample.timescaleUnitsToNextSample=s}}async registerSample(e,r){r.type==="key"&&this.processTimestamps(e,r),e.timestampProcessingQueue.push(r),this.isFragmented?(e.sampleQueue.push(r),await this.interleaveSamples()):this.fastStart==="reserve"?await this.registerSampleFastStartReserve(e,r):await this.addSampleToTrack(e,r)}async addSampleToTrack(e,r){if(!this.isFragmented&&(e.samples.push(r),this.fastStart==="reserve")){let a=e.track.metadata.maximumPacketCount;if(h(a!==void 0),e.samples.length>a)throw new Error(`Track #${e.track.id} has already reached the maximum packet count (${a}). Either add less packets or increase the maximum packet count.`)}let i=!1;if(!e.currentChunk)i=!0;else{e.currentChunk.startTimestamp=Math.min(e.currentChunk.startTimestamp,r.timestamp);let a=r.timestamp-e.currentChunk.startTimestamp;if(this.isFragmented){let s=this.trackDatas.every(o=>{if(e===o)return r.type==="key";let c=o.sampleQueue[0];return c?c.type==="key":o.closed});a>=this.minimumFragmentDuration&&s&&r.timestamp>this.maxWrittenTimestamp&&(i=!0,await this.finalizeFragment())}else i=a>=.5}i&&(e.currentChunk&&await this.finalizeCurrentChunk(e),e.currentChunk={startTimestamp:r.timestamp,samples:[],offset:null,moofOffset:null}),h(e.currentChunk),e.currentChunk.samples.push(r),this.isFragmented&&(this.maxWrittenTimestamp=Math.max(this.maxWrittenTimestamp,r.timestamp),this.maxWrittenEndTimestamp=Math.max(this.maxWrittenEndTimestamp,r.timestamp+r.duration),this.minWrittenTimestamp=Math.min(this.minWrittenTimestamp,r.timestamp))}async finalizeCurrentChunk(e){if(h(!this.isFragmented),h(this.writer),!e.currentChunk)return;e.finalizedChunks.push(e.currentChunk),this.finalizedChunks.push(e.currentChunk);let r=e.currentChunk.samples.length;if(e.type==="audio"&&e.info.requiresPcmTransformation&&(r=e.currentChunk.samples.reduce((i,a)=>i+oe(a.duration,e.timescale),0)),(e.compactlyCodedChunkTable.length===0||X(e.compactlyCodedChunkTable).samplesPerChunk!==r)&&e.compactlyCodedChunkTable.push({firstChunk:e.finalizedChunks.length,samplesPerChunk:r}),this.fastStart==="in-memory"){e.currentChunk.offset=0;return}e.currentChunk.offset=this.writer.getPos();for(let i of e.currentChunk.samples)h(i.data),this.writer.write(i.data),i.data=null;await this.writer.flush()}async interleaveSamples(e=!1){if(h(this.isFragmented),!(!e&&!this.allTracksAreKnown()))e:for(;;){let r=null,i=1/0;for(let s of this.trackDatas){if(!e&&s.sampleQueue.length===0&&!s.closed)break e;s.sampleQueue.length>0&&s.sampleQueue[0].timestamp<i&&(r=s,i=s.sampleQueue[0].timestamp)}if(!r)break;let a=r.sampleQueue.shift();await this.addSampleToTrack(r,a)}}async finalizeFragment(e=!this.isCmaf){h(this.isFragmented);let r=this.nextFragmentNumber++;if(r===1){let p=this.initBoxWriter??this.boxWriter;h(p),this.format._options.onMoov&&p.writer.startTrackingWrites();let b=Fi(this);if(p.writeBox(b),this.format._options.onMoov){let{data:g,start:y}=p.writer.stopTrackingWrites();this.format._options.onMoov(g,y)}if(this.isCmaf){h(this.initWriter),await this.initWriter.flush(),await this.initWriter.finalize(),this.writer=await this.output._getRootWriter(!0),this.boxWriter=new jr(this.writer);let g=this.boxWriter.measureBox(_c()),y=this.boxWriter.measureBox(Rc(this,0));this.segmentHeaderSize=g+y,this.writer.seek(this.segmentHeaderSize)}}h(this.writer),h(this.boxWriter);let i=this.trackDatas.filter(p=>p.currentChunk),a=Mc(r,i),s=this.writer.getPos(),o=s+this.boxWriter.measureBox(a),c=o+st,u=1/0;for(let p of i){p.currentChunk.offset=c,p.currentChunk.moofOffset=s;for(let b of p.currentChunk.samples)c+=b.size;u=Math.min(u,p.currentChunk.startTimestamp)}let l=c-o,m=l>=2**32;if(m)for(let p of i)p.currentChunk.offset+=Nt-st;this.format._options.onMoof&&this.writer.startTrackingWrites();let d=Mc(r,i);if(this.boxWriter.writeBox(d),this.format._options.onMoof){let{data:p,start:b}=this.writer.stopTrackingWrites();this.format._options.onMoof(p,b,u)}h(this.writer.getPos()===o),this.format._options.onMdat&&this.writer.startTrackingWrites();let f=Mn(m);f.size=l,this.boxWriter.writeBox(f),this.writer.seek(o+(m?Nt:st));for(let p of i)for(let b of p.currentChunk.samples)this.writer.write(b.data),b.data=null;if(this.format._options.onMdat){let{data:p,start:b}=this.writer.stopTrackingWrites();this.format._options.onMdat(p,b)}for(let p of i)p.finalizedChunks.push(p.currentChunk),this.finalizedChunks.push(p.currentChunk),p.currentChunk=null;e&&await this.writer.flush()}async registerSampleFastStartReserve(e,r){if(h(this.writer),h(this.boxWriter),this.allTracksAreKnown()){if(!this.mdat){let i=Fi(this),s=this.boxWriter.measureBox(i)+this.computeSampleTableSizeUpperBound()+4096;h(this.ftypSize!==null),this.writer.seek(this.ftypSize+s),this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat=Mn(!0),this.boxWriter.writeBox(this.mdat);for(let o of this.trackDatas){for(let c of o.sampleQueue)await this.addSampleToTrack(o,c);o.sampleQueue.length=0}}await this.addSampleToTrack(e,r)}else e.sampleQueue.push(r)}computeSampleTableSizeUpperBound(){h(this.fastStart==="reserve");let e=0;for(let r of this.trackDatas){let i=r.track.metadata.maximumPacketCount;h(i!==void 0),e+=8*Math.ceil(2/3*i),e+=4*i,e+=8*Math.ceil(2/3*i),e+=12*Math.ceil(2/3*i),e+=4*i,e+=8*i}return e}async onTrackClose(e){let r=await this.mutex.acquire(),i=this.trackDatas.find(a=>a.track===e);i&&(i.closed=!0,i.type==="subtitle"&&e.source._codec==="webvtt"&&await this.processWebVTTCues(i,1/0),this.processTimestamps(i)),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),this.isFragmented&&await this.interleaveSamples(),r()}async finalize(){let e=await this.mutex.acquire();this.allTracksKnown.resolve();for(let r of this.trackDatas)r.closed=!0,r.type==="subtitle"&&r.track.source._codec==="webvtt"&&await this.processWebVTTCues(r,1/0),this.processTimestamps(r);if(this.isFragmented)await this.interleaveSamples(!0),await this.finalizeFragment(!1);else for(let r of this.trackDatas){await this.finalizeCurrentChunk(r),h(r.startTimestampOffset!==null);for(let i=0;i<r.samples.length;i++){let a=r.samples[i];a.timestamp-=r.startTimestampOffset,a.decodeTimestamp-=r.startTimestampOffset}}if(h(this.writer),h(this.boxWriter),this.fastStart==="in-memory"){this.mdat=Mn(!1);let r;for(let a=0;a<2;a++){let s=Fi(this),o=this.boxWriter.measureBox(s);r=this.boxWriter.measureBox(this.mdat);let c=this.writer.getPos()+o+r;for(let u of this.finalizedChunks){u.offset=c;for(let{data:l}of u.samples)h(l),c+=l.byteLength,r+=l.byteLength}if(c<2**32)break;r>=2**32&&(this.mdat.largeSize=!0)}this.format._options.onMoov&&this.writer.startTrackingWrites();let i=Fi(this);if(this.boxWriter.writeBox(i),this.format._options.onMoov){let{data:a,start:s}=this.writer.stopTrackingWrites();this.format._options.onMoov(a,s)}this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat.size=r,this.boxWriter.writeBox(this.mdat);for(let a of this.finalizedChunks)for(let s of a.samples)h(s.data),this.writer.write(s.data),s.data=null;if(this.format._options.onMdat){let{data:a,start:s}=this.writer.stopTrackingWrites();this.format._options.onMdat(a,s)}}else if(this.isFragmented)if(this.isCmaf){let r=this.segmentHeaderSize!==null?this.writer.getPos()-this.segmentHeaderSize:0;this.writer.seek(0),this.boxWriter.writeBox(_c()),this.boxWriter.writeBox(Rc(this,r))}else{let r=this.writer.getPos(),i=xd(this.trackDatas);this.boxWriter.writeBox(i);let a=this.writer.getPos()-r;this.writer.seek(this.writer.getPos()-4),this.boxWriter.writeU32(a)}else{h(this.mdat);let r=this.boxWriter.offsets.get(this.mdat);h(r!==void 0);let i=this.writer.getPos()-r;if(this.mdat.size=i,this.mdat.largeSize=i>=2**32,this.boxWriter.patchBox(this.mdat),this.format._options.onMdat){let{data:s,start:o}=this.writer.stopTrackingWrites();this.format._options.onMdat(s,o)}let a=Fi(this);if(this.fastStart==="reserve"){h(this.ftypSize!==null),this.writer.seek(this.ftypSize),this.format._options.onMoov&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(a);let s=this.boxWriter.offsets.get(this.mdat)-this.writer.getPos();this.boxWriter.writeBox(Sd(s))}else this.format._options.onMoov&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(a);if(this.format._options.onMoov){let{data:s,start:o}=this.writer.stopTrackingWrites();this.format._options.onMoov(s,o)}}e()}};var xp=-(2**15),Pp=2**15-1,Bd="Mediabunny",Od=6,Dd=5,Cp={video:1,audio:2,subtitle:17},Ns=class extends ge{constructor(e,r){super(e);this.trackDatas=[];this.allTracksKnown=Z();this.segment=null;this.segmentInfo=null;this.seekHead=null;this.tracksElement=null;this.tagsElement=null;this.attachmentsElement=null;this.segmentDuration=null;this.cues=null;this.currentCluster=null;this.currentClusterStartMsTimestamp=null;this.currentClusterMaxMsTimestamp=null;this.trackDatasInCurrentCluster=new Map;this.startTimestamp=1/0;this.endTimestamp=-1/0;this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!!this.format._options.appendOnly),this.ebmlWriter=new Ta(this.writer),this.writeEBMLHeader(),this.createSegmentInfo(),this.createCues(),await this.writer.flush(),e()}writeEBMLHeader(){this.format._options.onEbmlHeader&&this.writer.startTrackingWrites();let e={id:440786851,data:[{id:17030,data:1},{id:17143,data:1},{id:17138,data:4},{id:17139,data:8},{id:17026,data:this.format instanceof Oi?"webm":"matroska"},{id:17031,data:2},{id:17029,data:2}]};if(this.ebmlWriter.writeEBML(e),this.format._options.onEbmlHeader){let{data:r,start:i}=this.writer.stopTrackingWrites();this.format._options.onEbmlHeader(r,i)}}maybeCreateSeekHead(e){if(this.format._options.appendOnly)return;let r=new Uint8Array([28,83,187,107]),i=new Uint8Array([21,73,169,102]),a=new Uint8Array([22,84,174,107]),s=new Uint8Array([25,65,164,105]),o=new Uint8Array([18,84,195,103]),c={id:290298740,data:[{id:19899,data:[{id:21419,data:r},{id:21420,size:5,data:e?this.ebmlWriter.offsets.get(this.cues)-this.segmentDataOffset:0}]},{id:19899,data:[{id:21419,data:i},{id:21420,size:5,data:e?this.ebmlWriter.offsets.get(this.segmentInfo)-this.segmentDataOffset:0}]},{id:19899,data:[{id:21419,data:a},{id:21420,size:5,data:e?this.ebmlWriter.offsets.get(this.tracksElement)-this.segmentDataOffset:0}]},this.attachmentsElement?{id:19899,data:[{id:21419,data:s},{id:21420,size:5,data:e?this.ebmlWriter.offsets.get(this.attachmentsElement)-this.segmentDataOffset:0}]}:null,this.tagsElement?{id:19899,data:[{id:21419,data:o},{id:21420,size:5,data:e?this.ebmlWriter.offsets.get(this.tagsElement)-this.segmentDataOffset:0}]}:null]};this.seekHead=c}createSegmentInfo(){let e={id:17545,data:new fi(0)};this.segmentDuration=e;let r={id:357149030,data:[{id:2807729,data:1e6},{id:19840,data:Bd},{id:22337,data:Bd},this.format._options.appendOnly?null:e]};this.segmentInfo=r}createTracks(){let e={id:374648427,data:[]};this.tracksElement=e;for(let r of this.trackDatas){let i=Fe[r.track.source._codec];h(i);let a=0;if(r.type==="audio"&&r.track.source._codec==="opus"){a=1e6*80;let s=r.info.decoderConfig.description;if(s){let o=j(s),c=lr(o);a=Math.round(1e9*(c.preSkip/nt))}}e.data.push({id:174,data:[{id:215,data:r.track.id},{id:29637,data:r.track.id},{id:131,data:Cp[r.type]},r.track.metadata.disposition?.default===!1?{id:136,data:0}:null,r.track.metadata.disposition?.forced?{id:21930,data:1}:null,r.track.metadata.disposition?.hearingImpaired?{id:21931,data:1}:null,r.track.metadata.disposition?.visuallyImpaired?{id:21932,data:1}:null,r.track.metadata.disposition?.original?{id:21934,data:1}:null,r.track.metadata.disposition?.commentary?{id:21935,data:1}:null,{id:156,data:0},{id:2274716,data:r.track.metadata.languageCode??J},{id:134,data:i},{id:22186,data:0},{id:22203,data:a},r.track.metadata.name!==void 0?{id:21358,data:new lt(r.track.metadata.name)}:null,r.type==="video"?this.videoSpecificTrackInfo(r):null,r.type==="audio"?this.audioSpecificTrackInfo(r):null,r.type==="subtitle"?this.subtitleSpecificTrackInfo(r):null]})}}videoSpecificTrackInfo(e){let{frameRate:r,rotation:i}=e.track.metadata,a=[e.info.decoderConfig.description?{id:25506,data:j(e.info.decoderConfig.description)}:null,r?{id:2352003,data:1e9/r}:null],s=i?Ze(-i):0,o=!!e.info.aspectRatio&&e.info.aspectRatio.num*e.info.height!==e.info.aspectRatio.den*e.info.width,c=e.info.decoderConfig.colorSpace,u={id:224,data:[{id:176,data:e.info.width},{id:186,data:e.info.height},o?{id:21680,data:e.info.aspectRatio.num}:null,o?{id:21690,data:e.info.aspectRatio.den}:null,o?{id:21682,data:3}:null,e.info.alphaMode?{id:21440,data:1}:null,zn(c)?{id:21936,data:[{id:21937,data:rt[c.matrix]},{id:21946,data:tt[c.transfer]},{id:21947,data:et[c.primaries]},{id:21945,data:c.fullRange?2:1}]}:null,s?{id:30320,data:[{id:30321,data:0},{id:30325,data:new mi((s+180)%360-180)}]}:null]};return a.push(u),a}audioSpecificTrackInfo(e){let r=ee.includes(e.track.source._codec)?Se(e.track.source._codec):null;return[e.info.decoderConfig.description?{id:25506,data:j(e.info.decoderConfig.description)}:null,{id:225,data:[{id:181,data:new mi(e.info.sampleRate)},{id:159,data:e.info.numberOfChannels},r?{id:25188,data:8*r.sampleSize}:null]}]}subtitleSpecificTrackInfo(e){return[{id:25506,data:$.encode(e.info.config.description)}]}maybeCreateTags(){let e=[],r=(s,o)=>{e.push({id:26568,data:[{id:17827,data:new lt(s)},typeof o=="string"?{id:17543,data:new lt(o)}:{id:17541,data:o}]})},i=this.output._metadataTags,a=new Set;for(let{key:s,value:o}of it(i))switch(s){case"title":r("TITLE",o),a.add("TITLE");break;case"description":r("DESCRIPTION",o),a.add("DESCRIPTION");break;case"artist":r("ARTIST",o),a.add("ARTIST");break;case"album":r("ALBUM",o),a.add("ALBUM");break;case"albumArtist":r("ALBUM_ARTIST",o),a.add("ALBUM_ARTIST");break;case"genre":r("GENRE",o),a.add("GENRE");break;case"comment":r("COMMENT",o),a.add("COMMENT");break;case"lyrics":r("LYRICS",o),a.add("LYRICS");break;case"date":r("DATE",o.toISOString().slice(0,10)),a.add("DATE");break;case"trackNumber":{let c=i.tracksTotal!==void 0?`${o}/${i.tracksTotal}`:o.toString();r("PART_NUMBER",c),a.add("PART_NUMBER")}break;case"discNumber":{let c=i.discsTotal!==void 0?`${o}/${i.discsTotal}`:o.toString();r("DISC",c),a.add("DISC")}break;case"tracksTotal":case"discsTotal":break;case"images":case"raw":break;default:ne(s)}if(i.raw)for(let s in i.raw){let o=i.raw[s];o==null||a.has(s)||(typeof o=="string"||o instanceof Uint8Array)&&r(s,o)}e.length!==0&&(this.tagsElement={id:307544935,data:[{id:29555,data:[{id:25536,data:[{id:26826,data:50},{id:25546,data:"MOVIE"}]},...e]}]})}maybeCreateAttachments(){let e=this.output._metadataTags,r=[],i=new Set,a=e.images??[];for(let s of a){let o=s.name;o===void 0&&(o=(s.kind==="coverFront"?"cover":s.kind==="coverBack"?"back":"image")+(xu(s.mimeType)??""));let c;for(;;){c=0n;for(let u=0;u<8;u++)c<<=8n,c|=BigInt(Math.floor(Math.random()*256));if(c!==0n&&!i.has(c))break}i.add(c),r.push({id:24999,data:[s.description!==void 0?{id:18046,data:new lt(s.description)}:null,{id:18030,data:new lt(o)},{id:18016,data:s.mimeType},{id:18012,data:s.data},{id:18094,data:c}]})}for(let[s,o]of Object.entries(e.raw??{}))!(o instanceof ar)||!/^\d+$/.test(s)||a.find(u=>u.mimeType===o.mimeType&&Wn(u.data,o.data))||r.push({id:24999,data:[o.description!==void 0?{id:18046,data:new lt(o.description)}:null,{id:18030,data:new lt(o.name??"")},{id:18016,data:o.mimeType??""},{id:18012,data:o.data},{id:18094,data:BigInt(s)}]});r.length!==0&&(this.attachmentsElement={id:423732329,data:r})}createSegment(){this.createTracks(),this.maybeCreateTags(),this.maybeCreateAttachments(),this.maybeCreateSeekHead(!1);let e={id:408125543,size:this.format._options.appendOnly?-1:Od,data:[this.seekHead,this.segmentInfo,this.tracksElement,this.attachmentsElement,this.tagsElement]};if(this.segment=e,this.format._options.onSegmentHeader&&this.writer.startTrackingWrites(),this.ebmlWriter.writeEBML(e),this.format._options.onSegmentHeader){let{data:r,start:i}=this.writer.stopTrackingWrites();this.format._options.onSegmentHeader(r,i)}}createCues(){this.cues={id:475249515,data:[]}}get segmentDataOffset(){return h(this.segment),this.ebmlWriter.dataOffsets.get(this.segment)}allTracksAreKnown(){for(let e of this.output._tracks)if(!e.source._closed&&!this.trackDatas.some(r=>r.track===e))return!1;return!0}async getMimeType(){await this.allTracksKnown.promise;let e=this.trackDatas.map(r=>r.type==="video"||r.type==="audio"?r.info.decoderConfig.codec:{webvtt:"wvtt"}[r.track.source._codec]);return xa({isWebM:this.format instanceof Oi,hasVideo:this.trackDatas.some(r=>r.type==="video"),hasAudio:this.trackDatas.some(r=>r.type==="audio"),codecStrings:e})}getVideoTrackData(e,r,i){let a=this.trackDatas.find(l=>l.track===e);if(a)return a;sr(i),h(i),h(i.decoderConfig),h(i.decoderConfig.codedWidth!==void 0),h(i.decoderConfig.codedHeight!==void 0);let s=i.decoderConfig.displayAspectWidth,o=i.decoderConfig.displayAspectHeight,c=s===void 0||o===void 0?null:Ot({num:s,den:o}),u={track:e,type:"video",info:{width:i.decoderConfig.codedWidth,height:i.decoderConfig.codedHeight,aspectRatio:c,decoderConfig:i.decoderConfig,alphaMode:!!r.sideData.alpha},chunkQueue:[],lastWrittenMsTimestamp:null,closed:!1};return e.source._codec==="vp9"?u.info.decoderConfig={...u.info.decoderConfig,description:new Uint8Array(Du(u.info.decoderConfig.codec))}:e.source._codec==="av1"&&(u.info.decoderConfig={...u.info.decoderConfig,description:new Uint8Array(Qn(u.info.decoderConfig.codec))}),this.trackDatas.push(u),this.trackDatas.sort((l,m)=>l.track.id-m.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),u}getAudioTrackData(e,r,i){let a=this.trackDatas.find(u=>u.track===e);if(a)return a;Ee(i),h(i),h(i.decoderConfig);let s={...i.decoderConfig},o=!1;if(e.source._codec==="aac"&&!s.description){let u=De(he.tempFromBytes(r.data));if(!u)throw new Error("Couldn't parse ADTS header from the AAC packet. Make sure the packets are in ADTS format (as specified in ISO 13818-7) when not providing a description, or provide a description (must be an AudioSpecificConfig as specified in ISO 14496-3) and ensure the packets are raw AAC data.");let l=qe[u.samplingFrequencyIndex],m=kt[u.channelConfiguration];if(l===void 0||m===void 0)throw new Error("Invalid ADTS frame header.");s.description=ri({objectType:u.objectType,sampleRate:l,numberOfChannels:m}),o=!0}let c={track:e,type:"audio",info:{numberOfChannels:i.decoderConfig.numberOfChannels,sampleRate:i.decoderConfig.sampleRate,decoderConfig:s,requiresAdtsStripping:o},chunkQueue:[],lastWrittenMsTimestamp:null,closed:!1};return this.trackDatas.push(c),this.trackDatas.sort((u,l)=>u.track.id-l.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),c}getSubtitleTrackData(e,r){let i=this.trackDatas.find(s=>s.track===e);if(i)return i;Xn(r),h(r),h(r.config);let a={track:e,type:"subtitle",info:{config:r.config},chunkQueue:[],lastWrittenMsTimestamp:null,closed:!1};return this.trackDatas.push(a),this.trackDatas.sort((s,o)=>s.track.id-o.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),a}async addEncodedVideoPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getVideoTrackData(e,r,i),o=r.type==="key";this.validateTimestamp(s.track,r.timestamp,o);let c=r.timestamp,u=r.duration;e.metadata.frameRate!==void 0&&(c=bt(c,e.metadata.frameRate),u=bt(u,e.metadata.frameRate));let l=s.info.alphaMode?r.sideData.alpha??null:null,m=this.createInternalChunk(r.data,c,u,r.type,l);e.source._codec==="vp9"&&this.fixVP9ColorSpace(s,m),s.chunkQueue.push(m),await this.interleaveChunks()}finally{a()}}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getAudioTrackData(e,r,i),o=r.data;if(s.info.requiresAdtsStripping){let l=De(he.tempFromBytes(o));if(!l)throw new Error("Expected ADTS frame, didn't get one.");let m=l.crcCheck===null?Qt:Ne;o=o.subarray(m)}let c=r.type==="key";this.validateTimestamp(s.track,r.timestamp,c);let u=this.createInternalChunk(o,r.timestamp,r.duration,r.type);s.chunkQueue.push(u),await this.interleaveChunks()}finally{a()}}async addSubtitleCue(e,r,i){let a=await this.mutex.acquire();try{let s=this.getSubtitleTrackData(e,i);this.validateTimestamp(s.track,r.timestamp,!0);let o=r.text,c=Math.round(r.timestamp*1e3);Ri.lastIndex=0,o=o.replace(Ri,d=>{let p=Os(d.slice(1,-1))-c;return`<${Ds(p)}>`});let u=$.encode(o),l=`${r.settings??""}
38
38
  ${r.identifier??""}
39
- ${r.notes??""}`,m=this.createInternalChunk(u,r.timestamp,r.duration,"key",l.trim()?$.encode(l):null);s.chunkQueue.push(m),await this.interleaveChunks()}finally{a()}}async interleaveChunks(e=!1){if(!(!e&&!this.allTracksAreKnown())){e:for(;;){let r=null,i=1/0;for(let s of this.trackDatas){if(!e&&s.chunkQueue.length===0&&!s.closed)break e;s.chunkQueue.length>0&&s.chunkQueue[0].timestamp<i&&(r=s,i=s.chunkQueue[0].timestamp)}if(!r)break;let a=r.chunkQueue.shift();this.writeBlock(r,a)}e||await this.writer.flush()}}fixVP9ColorSpace(e,r){if(r.type!=="key"||!e.info.decoderConfig.colorSpace||!e.info.decoderConfig.colorSpace.matrix)return;let i=new W(r.data);i.skipBits(2);let a=i.readBits(1),o=(i.readBits(1)<<1)+a;if(o===3&&i.skipBits(1),i.readBits(1)||i.readBits(1)!==0||(i.skipBits(2),i.readBits(24)!==4817730))return;o>=2&&i.skipBits(1);let m={rgb:7,bt709:2,bt470bg:1,smpte170m:3}[e.info.decoderConfig.colorSpace.matrix];du(r.data,i.pos,i.pos+3,m)}createInternalChunk(e,r,i,a,s=null){return{data:e,type:a,timestamp:r,duration:i,additions:s}}writeBlock(e,r){this.segment||this.createSegment();let i=Math.round(1e3*r.timestamp),a=this.trackDatas.every(m=>{if(e===m)return r.type==="key";let d=m.chunkQueue[0];return d?d.type==="key":m.closed}),s=!1;if(!this.currentCluster)s=!0;else{h(this.currentClusterStartMsTimestamp!==null),h(this.currentClusterMaxMsTimestamp!==null);let m=i-this.currentClusterStartMsTimestamp;s=a&&i>this.currentClusterMaxMsTimestamp&&m>=1e3*(this.format._options.minimumClusterDuration??1)||m>Pp}s&&this.createNewCluster(i);let o=i-this.currentClusterStartMsTimestamp;if(o<xp)return;let c=new Uint8Array(4),u=new DataView(c.buffer);u.setUint8(0,128|e.track.id),u.setInt16(1,o,!1);let l=Math.round(1e3*r.duration);if(r.additions){let m={id:160,data:[{id:161,data:[c,r.data]},r.type==="delta"?{id:251,data:new ln(e.lastWrittenMsTimestamp-i)}:null,r.additions?{id:30113,data:[{id:166,data:[{id:238,data:1},{id:165,data:r.additions}]}]}:null,l>0?{id:155,data:l}:null]};this.ebmlWriter.writeEBML(m)}else{u.setUint8(3,+(r.type==="key")<<7);let m={id:163,data:[c,r.data]};this.ebmlWriter.writeEBML(m)}this.startTimestamp=Math.min(this.startTimestamp,i),this.endTimestamp=Math.max(this.endTimestamp,i+l),e.lastWrittenMsTimestamp=i,this.trackDatasInCurrentCluster.has(e)||this.trackDatasInCurrentCluster.set(e,{firstMsTimestamp:i}),this.currentClusterMaxMsTimestamp=Math.max(this.currentClusterMaxMsTimestamp,i)}createNewCluster(e){this.currentCluster&&this.finalizeCurrentCluster(),this.format._options.onCluster&&this.writer.startTrackingWrites(),this.currentCluster={id:524531317,size:this.format._options.appendOnly?-1:Dd,data:[{id:231,data:e}]},this.ebmlWriter.writeEBML(this.currentCluster),this.currentClusterStartMsTimestamp=e,this.currentClusterMaxMsTimestamp=e,this.trackDatasInCurrentCluster.clear()}finalizeCurrentCluster(){if(h(this.currentCluster),!this.format._options.appendOnly){let a=this.writer.getPos()-this.ebmlWriter.dataOffsets.get(this.currentCluster),s=this.writer.getPos();this.writer.seek(this.ebmlWriter.offsets.get(this.currentCluster)+4),this.ebmlWriter.writeVarInt(a,Dd),this.writer.seek(s)}if(this.format._options.onCluster){h(this.currentClusterStartMsTimestamp!==null);let{data:a,start:s}=this.writer.stopTrackingWrites();this.format._options.onCluster(a,s,this.currentClusterStartMsTimestamp/1e3)}let e=this.ebmlWriter.offsets.get(this.currentCluster)-this.segmentDataOffset,r=new Map;for(let[a,{firstMsTimestamp:s}]of this.trackDatasInCurrentCluster)r.has(s)||r.set(s,[]),r.get(s).push(a);let i=[...r.entries()].sort((a,s)=>a[0]-s[0]);for(let[a,s]of i)h(this.cues),this.cues.data.push({id:187,data:[{id:179,data:a},...s.map(o=>({id:183,data:[{id:247,data:o.track.id},{id:241,data:e}]}))]})}async onTrackClose(e){let r=await this.mutex.acquire(),i=this.trackDatas.find(a=>a.track===e);i&&(i.closed=!0),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),await this.interleaveChunks(),r()}async finalize(){let e=await this.mutex.acquire();this.allTracksKnown.resolve();for(let r of this.trackDatas)r.closed=!0;if(this.segment||this.createSegment(),await this.interleaveChunks(!0),this.currentCluster&&this.finalizeCurrentCluster(),h(this.cues),this.ebmlWriter.writeEBML(this.cues),!this.format._options.appendOnly){let r=this.writer.getPos()-this.segmentDataOffset;this.writer.seek(this.ebmlWriter.offsets.get(this.segment)+4),this.ebmlWriter.writeVarInt(r,Od);let i=this.startTimestamp===1/0?0:this.endTimestamp-this.startTimestamp;this.segmentDuration.data=new fi(i),this.writer.seek(this.ebmlWriter.offsets.get(this.segmentDuration)),this.ebmlWriter.writeEBML(this.segmentDuration),h(this.seekHead),this.writer.seek(this.ebmlWriter.offsets.get(this.seekHead)),this.maybeCreateSeekHead(!0),this.ebmlWriter.writeEBML(this.seekHead)}e()}};var Ls=class{constructor(t){this.writer=t;this.helper=new Uint8Array(8);this.helperView=new DataView(this.helper.buffer)}writeU32(t){this.helperView.setUint32(0,t,!1),this.writer.write(this.helper.subarray(0,4))}writeXingFrame(t){let e=this.writer.getPos(),r=255,i=224|t.mpegVersionId<<3|t.layer<<1,a;t.mpegVersionId&2?a=t.mpegVersionId&1?0:1:a=1;let s=0,o=155,c=-1,u=a*16*4+t.layer*16;for(let g=0;g<16;g++){let y=$n[u+g];if(Yn(a,t.layer,1e3*y,t.sampleRate,s)>=o){c=g;break}}if(c===-1)throw new Error("No suitable bitrate found.");let l=c<<4|t.frequencyIndex<<2|s<<1,m=t.channel<<6|t.modeExtension<<4|t.copyright<<3|t.original<<2|t.emphasis;this.helper[0]=r,this.helper[1]=i,this.helper[2]=l,this.helper[3]=m,this.writer.write(this.helper.subarray(0,4));let d=cr(t.mpegVersionId,t.channel);this.writer.seek(e+d),this.writeU32(or);let f=0;t.frameCount!==null&&(f|=1),t.fileSize!==null&&(f|=2),t.toc!==null&&(f|=4),this.writeU32(f),this.writeU32(t.frameCount??0),this.writeU32(t.fileSize??0),this.writer.write(t.toc??new Uint8Array(100));let p=$n[u+c],b=Yn(a,t.layer,1e3*p,t.sampleRate,s);this.writer.seek(e+b)}};var Ws=class extends ge{constructor(e,r){super(e);this.xingFrameData=null;this.frameCount=0;this.framePositions=[];this.xingFramePos=null;this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(this.format._options.xingHeader===!1),this.mp3Writer=new Ls(this.writer),Vt(this.output._metadataTags)||new Tr(this.writer).writeId3V2Tag(this.output._metadataTags),e()}async getMimeType(){return"audio/mpeg"}async addEncodedVideoPacket(){throw new Error("MP3 does not support video.")}async addEncodedAudioPacket(e,r){let i=await this.mutex.acquire();try{let a=this.format._options.xingHeader!==!1;if(!this.xingFrameData&&a){let s=V(r.data);if(s.byteLength<4)throw new Error("Invalid MP3 header in sample.");let o=s.getUint32(0,!1),c=Mr(o,null).header;if(!c)throw new Error("Invalid MP3 header in sample.");let u=cr(c.mpegVersionId,c.channel);if(s.byteLength>=u+4){let l=s.getUint32(u,!1);if(l===or||l===ni)return}this.xingFrameData={mpegVersionId:c.mpegVersionId,layer:c.layer,frequencyIndex:c.frequencyIndex,sampleRate:c.sampleRate,channel:c.channel,modeExtension:c.modeExtension,copyright:c.copyright,original:c.original,emphasis:c.emphasis,frameCount:null,fileSize:null,toc:null},this.xingFramePos=this.writer.getPos(),this.mp3Writer.writeXingFrame(this.xingFrameData),this.frameCount++}this.validateTimestamp(e,r.timestamp,r.type==="key"),a&&this.framePositions.push(this.writer.getPos()),this.writer.write(r.data),this.frameCount++,await this.writer.flush()}finally{i()}}async addSubtitleCue(){throw new Error("MP3 does not support subtitles.")}async finalize(){if(!this.xingFrameData||this.xingFramePos===null)return;let e=await this.mutex.acquire(),i=this.writer.getPos()-this.xingFramePos;this.writer.seek(this.xingFramePos);let a=new Uint8Array(100);for(let s=0;s<100;s++){let o=Math.floor(this.framePositions.length*(s/100)),c=this.framePositions[o]-this.xingFramePos;a[s]=256*(c/i)}if(this.xingFrameData.frameCount=this.frameCount,this.xingFrameData.fileSize=i,this.xingFrameData.toc=a,this.format._options.onXingFrame&&this.writer.startTrackingWrites(),this.mp3Writer.writeXingFrame(this.xingFrameData),this.format._options.onXingFrame){let{data:s,start:o}=this.writer.stopTrackingWrites();this.format._options.onXingFrame(s,o)}e()}};var Ip=8192,Hs=class extends ge{constructor(e,r){super(e);this.trackDatas=[];this.bosPagesWritten=!1;this.allTracksKnown=Z();this.pageBytes=new Uint8Array(Ra);this.pageView=new DataView(this.pageBytes.buffer);this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!0),e()}async getMimeType(){return await this.allTracksKnown.promise,_a({codecStrings:this.trackDatas.map(e=>e.codecInfo.codec)})}addEncodedVideoPacket(){throw new Error("Video tracks are not supported.")}getTrackData(e,r){let i=this.trackDatas.find(o=>o.track===e);if(i)return i;let a;do a=Math.floor(2**32*Math.random());while(this.trackDatas.some(o=>o.serialNumber===a));h(e.source._codec==="vorbis"||e.source._codec==="opus"),Ee(r),h(r),h(r.decoderConfig);let s={track:e,serialNumber:a,internalSampleRate:e.source._codec==="opus"?nt:r.decoderConfig.sampleRate,codecInfo:{codec:e.source._codec,vorbisInfo:null,opusInfo:null},vorbisLastBlocksize:null,packetQueue:[],currentTimestampInSamples:0,pagesWritten:0,currentGranulePosition:0,currentLacingValues:[],currentPageData:[],currentPageSize:27,currentPageStartsWithFreshPacket:!0,currentPageStartTimestampInSamples:0,closed:!1};return this.queueHeaderPackets(s,r),this.trackDatas.push(s),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),s}queueHeaderPackets(e,r){if(h(r.decoderConfig),e.track.source._codec==="vorbis"){h(r.decoderConfig.description);let i=j(r.decoderConfig.description);if(i[0]!==2)throw new TypeError("First byte of Vorbis decoder description must be 2.");let a=1,s=()=>{let g=0;for(;;){let y=i[a++];if(y===void 0)throw new TypeError("Vorbis decoder description is too short.");if(g+=y,y<255)return g}},o=s(),c=s();if(i.length-a<=0)throw new TypeError("Vorbis decoder description is too short.");let l=i.subarray(a,a+=o);a+=c;let m=i.subarray(a),d=new Uint8Array(7);d[0]=3,d[1]=118,d[2]=111,d[3]=114,d[4]=98,d[5]=105,d[6]=115;let f=nn(d,this.output._metadataTags,!0);e.packetQueue.push({data:l,timestampInSamples:0,durationInSamples:0,forcePageFlush:!0},{data:f,timestampInSamples:0,durationInSamples:0,forcePageFlush:!1},{data:m,timestampInSamples:0,durationInSamples:0,forcePageFlush:!0});let b=V(l).getUint8(28);e.codecInfo.vorbisInfo={blocksizes:[1<<(b&15),1<<(b>>4)],modeBlockflags:aa(m).modeBlockflags}}else if(e.track.source._codec==="opus"){if(!r.decoderConfig.description)throw new TypeError("For Ogg, Opus decoder description is required.");let i=j(r.decoderConfig.description),a=new Uint8Array(8),s=V(a);s.setUint32(0,1332770163,!1),s.setUint32(4,1415669619,!1);let o=nn(a,this.output._metadataTags,!0);e.packetQueue.push({data:i,timestampInSamples:0,durationInSamples:0,forcePageFlush:!0},{data:o,timestampInSamples:0,durationInSamples:0,forcePageFlush:!0}),e.codecInfo.opusInfo={preSkip:lr(i).preSkip}}}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getTrackData(e,i);this.validateTimestamp(s.track,r.timestamp,r.type==="key");let o=s.currentTimestampInSamples,{durationInSamples:c,vorbisBlockSize:u}=va(r.data,s.codecInfo,s.vorbisLastBlocksize);s.currentTimestampInSamples+=c,s.vorbisLastBlocksize=u,s.packetQueue.push({data:r.data,timestampInSamples:o,durationInSamples:c,forcePageFlush:!1}),await this.interleavePages()}finally{a()}}addSubtitleCue(){throw new Error("Subtitle tracks are not supported.")}allTracksAreKnown(){for(let e of this.output._tracks)if(!e.source._closed&&!this.trackDatas.some(r=>r.track===e))return!1;return!0}async interleavePages(e=!1){if(!this.bosPagesWritten){if(!this.allTracksAreKnown()&&!e)return;for(let r of this.trackDatas)for(;r.packetQueue.length>0;){let i=r.packetQueue.shift();if(this.writePacket(r,i,!1),i.forcePageFlush)break}this.bosPagesWritten=!0}e:for(;;){let r=null,i=1/0;for(let o of this.trackDatas){if(!e&&o.packetQueue.length<=1&&!o.closed)break e;o.packetQueue.length>0&&o.packetQueue[0].timestampInSamples<i&&(r=o,i=o.packetQueue[0].timestampInSamples)}if(!r)break;let a=r.packetQueue.shift(),s=r.packetQueue.length===0;this.writePacket(r,a,s)}e||await this.writer.flush()}writePacket(e,r,i){let a=r.timestampInSamples+r.durationInSamples;if(this.format._options.maximumPageDuration!==void 0){let l=this.format._options.maximumPageDuration*e.internalSampleRate;e.currentLacingValues.length>0&&a-e.currentPageStartTimestampInSamples>l&&this.writePage(e,!1)}let s=r.data.length,o=0,c=0;for(;;){e.currentLacingValues.length===0&&o>0&&(e.currentPageStartsWithFreshPacket=!1);let l=Math.min(255,s);e.currentLacingValues.push(l),e.currentPageSize++,c+=l;let m=s<255;if(e.currentLacingValues.length===255){let d=r.data.subarray(o,c);if(o=c,e.currentPageData.push(d),e.currentPageSize+=d.length,this.writePage(e,i&&m),m)return}if(m)break;s-=255}let u=r.data.subarray(o);e.currentPageData.push(u),e.currentPageSize+=u.length,e.currentGranulePosition=a,(e.currentPageSize>=Ip||r.forcePageFlush)&&this.writePage(e,i)}writePage(e,r){this.pageView.setUint32(0,pn,!0),this.pageView.setUint8(4,0);let i=0;e.currentPageStartsWithFreshPacket||(i|=1),e.pagesWritten===0&&(i|=2),r&&(i|=4),this.pageView.setUint8(5,i);let a=e.currentLacingValues.every(u=>u===255)?-1:e.currentGranulePosition;yu(this.pageView,6,a,!0),this.pageView.setUint32(14,e.serialNumber,!0),this.pageView.setUint32(18,e.pagesWritten,!0),this.pageView.setUint32(22,0,!0),this.pageView.setUint8(26,e.currentLacingValues.length),this.pageBytes.set(e.currentLacingValues,27);let s=27+e.currentLacingValues.length;for(let u of e.currentPageData)this.pageBytes.set(u,s),s+=u.length;let o=this.pageBytes.subarray(0,s),c=Ea(o);if(this.pageView.setUint32(22,c,!0),e.pagesWritten++,e.currentLacingValues.length=0,e.currentPageData.length=0,e.currentPageSize=27,e.currentPageStartsWithFreshPacket=!0,e.currentPageStartTimestampInSamples=e.currentGranulePosition,this.format._options.onPage&&this.writer.startTrackingWrites(),this.writer.write(o),this.format._options.onPage){let{data:u,start:l}=this.writer.stopTrackingWrites();this.format._options.onPage(u,l,e.track.source)}}async onTrackClose(e){let r=await this.mutex.acquire(),i=this.trackDatas.find(a=>a.track===e);i&&(i.closed=!0),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),await this.interleavePages(),r()}async finalize(){let e=await this.mutex.acquire();this.allTracksKnown.resolve();for(let r of this.trackDatas)r.closed=!0;await this.interleavePages(!0);for(let r of this.trackDatas)r.currentLacingValues.length>0&&this.writePage(r,!0);e()}};var Ep=0,Ld=4096,Vd=256,vp=224,Ud=192,zd=new Uint8Array([9,240]),Nd=new Uint8Array([70,1]),qs=class extends ge{constructor(e,r){super(e);this.trackDatas=[];this.tablesWritten=!1;this.continuityCounters=new Map;this.packetBuffer=new Uint8Array(188);this.packetView=V(this.packetBuffer);this.allTracksKnown=Z();this.videoTrackIndex=0;this.audioTrackIndex=0;this.adaptationFieldBuffer=new Uint8Array(184);this.payloadBuffer=new Uint8Array(184);this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!0),e()}async getMimeType(){return await this.allTracksKnown.promise,za(this.trackDatas.map(e=>e.codecString))}getVideoTrackData(e,r){let i=this.trackDatas.find(l=>l.track===e);if(i)return i;sr(r),h(r?.decoderConfig);let a=e.source._codec;h(a==="avc"||a==="hevc");let s=a==="avc"?27:36,o=Vd+this.trackDatas.length,c=vp+this.videoTrackIndex++,u={track:e,pid:o,streamType:s,streamId:c,codecString:r.decoderConfig.codec,timestampProcessingQueue:[],packetQueue:[],inputIsAnnexB:null,inputIsAdts:null,avcDecoderConfig:null,hevcDecoderConfig:null,adtsHeader:null,adtsHeaderBitstream:null,firstPacketWritten:!1,closed:!1};return this.trackDatas.push(u),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),u}getAudioTrackData(e,r){let i=this.trackDatas.find(l=>l.track===e);if(i)return i;Ee(r),h(r?.decoderConfig);let a=e.source._codec;h(a==="aac"||a==="mp3"||a==="ac3"||a==="eac3");let s,o;switch(a){case"aac":s=15,o=Ud+this.audioTrackIndex++;break;case"mp3":s=3,o=Ud+this.audioTrackIndex++;break;case"ac3":s=129,o=189;break;case"eac3":s=135,o=189;break}let c=Vd+this.trackDatas.length,u={track:e,pid:c,streamType:s,streamId:o,codecString:r.decoderConfig.codec,timestampProcessingQueue:[],packetQueue:[],inputIsAnnexB:null,inputIsAdts:null,avcDecoderConfig:null,hevcDecoderConfig:null,adtsHeader:null,adtsHeaderBitstream:null,firstPacketWritten:!1,closed:!1};return this.trackDatas.push(u),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),u}async addEncodedVideoPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getVideoTrackData(e,i);this.validateTimestamp(s.track,r.timestamp,r.type==="key");let o=this.prepareVideoPacket(s,r,i);r.type==="key"&&await this.flushTimestampQueue(s),s.timestampProcessingQueue.push({data:o,presentationTimestamp:r.timestamp,decodeTimestamp:null,isKeyframe:r.type==="key"})}finally{a()}}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getAudioTrackData(e,i);this.validateTimestamp(s.track,r.timestamp,r.type==="key");let o=this.prepareAudioPacket(s,r,i);r.type==="key"&&await this.flushTimestampQueue(s),s.timestampProcessingQueue.push({data:o,presentationTimestamp:r.timestamp,decodeTimestamp:null,isKeyframe:r.type==="key"})}finally{a()}}async addSubtitleCue(){throw new Error("MPEG-TS does not support subtitles.")}prepareVideoPacket(e,r,i){let a=e.track.source._codec;if(e.inputIsAnnexB===null){let s=i?.decoderConfig?.description;if(e.inputIsAnnexB=!s,!e.inputIsAnnexB){let o=j(s);a==="avc"?e.avcDecoderConfig=ra(o):e.hevcDecoderConfig=$u(o)}}return e.inputIsAnnexB?this.prepareAnnexBVideoPacket(r.data,a):this.prepareLengthPrefixedVideoPacket(e,r,a)}prepareAnnexBVideoPacket(e,r){let i=[];for(let s of ur(e)){let o=e.subarray(s.offset,s.offset+s.length);(r==="avc"?zt(o[0])===9:at(o[0])===35)||i.push(o)}let a=r==="avc"?zd:Nd;return i.unshift(a),en(i)}prepareLengthPrefixedVideoPacket(e,r,i){let a=r.data,s=i==="avc"?e.avcDecoderConfig.lengthSizeMinusOne+1:e.hevcDecoderConfig.lengthSizeMinusOne+1,o=[];for(let u of Jn(a,s)){let l=a.subarray(u.offset,u.offset+u.length);(i==="avc"?zt(l[0])===9:at(l[0])===35)||o.push(l)}if(r.type==="key")if(i==="avc"){let u=e.avcDecoderConfig;for(let l of u.pictureParameterSets)o.unshift(l);for(let l of u.sequenceParameterSets)o.unshift(l)}else{let u=e.hevcDecoderConfig;for(let l of u.arrays)if(l.nalUnitType===34)for(let m of l.nalUnits)o.unshift(m);for(let l of u.arrays)if(l.nalUnitType===33)for(let m of l.nalUnits)o.unshift(m);for(let l of u.arrays)if(l.nalUnitType===32)for(let m of l.nalUnits)o.unshift(m)}let c=i==="avc"?zd:Nd;return o.unshift(c),en(o)}prepareAudioPacket(e,r,i){let a=e.track.source._codec;if(a==="mp3"||a==="ac3"||a==="eac3")return r.data;if(e.inputIsAdts===null){let u=i?.decoderConfig?.description;if(e.inputIsAdts=!u,!e.inputIsAdts){let l=Tt(j(u)),m=qn(l);e.adtsHeader=m.header,e.adtsHeaderBitstream=m.bitstream}}if(e.inputIsAdts)return r.data;h(e.adtsHeader),h(e.adtsHeaderBitstream);let s=e.adtsHeader,o=r.data.byteLength+s.byteLength;Gn(e.adtsHeaderBitstream,o);let c=new Uint8Array(o);return c.set(s,0),c.set(r.data,s.byteLength),c}allTracksAreKnown(){for(let e of this.output._tracks)if(!e.source._closed&&!this.trackDatas.some(r=>r.track===e))return!1;return!0}async flushTimestampQueue(e,r=!0){if(e.timestampProcessingQueue.length===0)return;let i=e.timestampProcessingQueue.map(a=>a.presentationTimestamp).sort((a,s)=>a-s);for(let a=0;a<e.timestampProcessingQueue.length;a++){let s=e.timestampProcessingQueue[a];s.decodeTimestamp=i[a],e.packetQueue.push(s)}e.timestampProcessingQueue.length=0,r&&await this.interleavePackets()}async interleavePackets(e=!1){if(!this.tablesWritten){if(!this.allTracksAreKnown()&&!e)return;this.writeTables()}e:for(;;){let r=null,i=1/0;for(let s of this.trackDatas){if(!e&&s.packetQueue.length===0&&!s.closed)break e;s.packetQueue.length>0&&s.packetQueue[0].presentationTimestamp<i&&(r=s,i=s.packetQueue[0].presentationTimestamp)}if(!r)break;let a=r.packetQueue.shift();this.writePesPacket(r,a)}e||await this.writer.flush()}writeTables(){h(!this.tablesWritten),this.writePsiSection(Ep,$r),this.writePsiSection(Ld,Rp(this.trackDatas)),this.tablesWritten=!0}writePsiSection(e,r){let i=0,a=!0;for(;i<r.length;){let o=184-(a?1:0),c=r.length-i,u=Math.min(o,c),l;a?(l=this.payloadBuffer.subarray(0,1+u),l[0]=0,l.set(r.subarray(i,i+u),1)):l=r.subarray(i,i+u),this.writeTsPacket(e,a,null,l),i+=u,a=!1}}writePesPacket(e,r){let i=e.track.type==="video",a=i?10:5,s=new Uint8Array(9+a),o=V(s),c=new W(s.subarray(9));Ir(o,0,1,!1),s[3]=e.streamId;let u=e.track.type==="video"?0:Math.min(8+r.data.length,65535);o.setUint16(4,u,!1),o.setUint8(6,132),o.setUint8(7,i?192:128),o.setUint8(8,a);let l=Math.round(r.presentationTimestamp*9e4);if(c.pos=0,c.writeBits(4,i?3:2),c.writeBits(3,l>>>30&7),c.writeBits(1,1),c.writeBits(15,l>>>15&32767),c.writeBits(1,1),c.writeBits(15,l&32767),c.writeBits(1,1),i){h(r.decodeTimestamp!==null);let p=Math.round(r.decodeTimestamp*9e4);c.writeBits(4,1),c.writeBits(3,p>>>30&7),c.writeBits(1,1),c.writeBits(15,p>>>15&32767),c.writeBits(1,1),c.writeBits(15,p&32767),c.writeBits(1,1)}let m=s.length+r.data.length,d=0,f=!0;for(;d<m;){let p=f,b=m-d,g=f&&r.isKeyframe,y=f&&!e.firstPacketWritten,k=Math.max(0,184-b),T;g||y?T=Math.max(2,k):T=k;let w=null;if(T>0){let I=this.adaptationFieldBuffer;T===1?I[0]=0:(I[0]=T-1,I[1]=Number(y)<<7|Number(g)<<6,I.fill(255,2,T)),w=I.subarray(0,T)}let S=Math.min(184-T,b),x=this.payloadBuffer.subarray(0,S),P=0;if(d<s.length){let I=Math.min(s.length-d,S);x.set(s.subarray(d,d+I),0),P=I}let A=Math.max(0,d-s.length),C=A+(S-P);P<S&&x.set(r.data.subarray(A,C),P),this.writeTsPacket(e.pid,p,w,x),d+=S,f=!1}e.firstPacketWritten=!0}writeTsPacket(e,r,i,a){let s=this.continuityCounters.get(e)??0,o=a.length>0,c=i?o?3:2:o?1:0;this.packetBuffer[0]=71,this.packetView.setUint16(1,(r?16384:0)|e&8191,!1),this.packetBuffer[3]=c<<4|s&15,o&&this.continuityCounters.set(e,s+1&15);let u=4;i&&(this.packetBuffer.set(i,u),u+=i.length),this.packetBuffer.set(a,u),u+=a.length,u<188&&this.packetBuffer.fill(255,u);let l=this.writer.getPos();this.writer.write(this.packetBuffer),this.format._options.onPacket&&this.format._options.onPacket(this.packetBuffer.slice(),l)}async onTrackClose(e){let r=await this.mutex.acquire(),i=this.trackDatas.find(a=>a.track===e);i&&(i.closed=!0,await this.flushTimestampQueue(i,!1)),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),await this.interleavePackets(),r()}async finalize(){let e=await this.mutex.acquire();this.allTracksKnown.resolve();for(let r of this.trackDatas)r.closed=!0,await this.flushTimestampQueue(r,!1);await this.interleavePackets(!0),e()}},_p=79764919,Wd=new Uint32Array(256);for(let n=0;n<256;n++){let t=n<<24;for(let e=0;e<8;e++)t=t&2147483648?t<<1^_p:t<<1;Wd[n]=t>>>0&4294967295}var Hd=n=>{let t=4294967295;for(let e=0;e<n.length;e++){let r=n[e];t=(t<<8^Wd[t>>>24^r])>>>0}return t},$r=new Uint8Array(16);{let n=V($r);$r[0]=0,n.setUint16(1,45069,!1),n.setUint16(3,1,!1),$r[5]=193,$r[6]=0,$r[7]=0,n.setUint16(8,1,!1),n.setUint16(10,57344|Ld&8191,!1),n.setUint32(12,Hd($r.subarray(0,12)),!1)}var Rp=n=>{let t=0;for(let c of n)t+=5,c.streamType===129?t+=sn.length:c.streamType===135&&(t+=on.length);let e=9+t+4,r=new Uint8Array(3+e-4),i=V(r);r[0]=2,i.setUint16(1,45056|e&4095,!1),i.setUint16(3,1,!1),r[5]=193,r[6]=0,r[7]=0,i.setUint16(8,65535,!1),i.setUint16(10,61440,!1);let a=12;for(let c of n)r[a++]=c.streamType,i.setUint16(a,57344|c.pid&8191,!1),a+=2,c.streamType===129?(i.setUint16(a,61440|sn.length,!1),a+=2,r.set(sn,a),a+=sn.length):c.streamType===135?(i.setUint16(a,61440|on.length,!1),a+=2,r.set(on,a),a+=on.length):(i.setUint16(a,61440,!1),a+=2);let s=Hd(r),o=new Uint8Array(r.length+4);return o.set(r,0),V(o).setUint32(r.length,s,!1),o};var Gs=class{constructor(t){this.writer=t;this.helper=new Uint8Array(8);this.helperView=new DataView(this.helper.buffer)}writeU16(t){this.helperView.setUint16(0,t,!0),this.writer.write(this.helper.subarray(0,2))}writeU32(t){this.helperView.setUint32(0,t,!0),this.writer.write(this.helper.subarray(0,4))}writeU64(t){this.helperView.setUint32(0,t,!0),this.helperView.setUint32(4,Math.floor(t/2**32),!0),this.writer.write(this.helper)}writeAscii(t){this.writer.write(new TextEncoder().encode(t))}};var Ks=class extends ge{constructor(e,r){super(e);this.headerWritten=!1;this.dataSize=0;this.sampleRate=null;this.sampleCount=0;this.riffSizePos=null;this.dataSizePos=null;this.ds64RiffSizePos=null;this.ds64DataSizePos=null;this.ds64SampleCountPos=null;this.format=r,this.isRf64=!!r._options.large}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!1),this.riffWriter=new Gs(this.writer),e()}async getMimeType(){return"audio/wav"}async addEncodedVideoPacket(){throw new Error("WAVE does not support video.")}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{if(this.headerWritten||(Ee(i),h(i),h(i.decoderConfig),this.writeHeader(e,i.decoderConfig),this.sampleRate=i.decoderConfig.sampleRate,this.headerWritten=!0),this.validateTimestamp(e,r.timestamp,r.type==="key"),!this.isRf64&&this.writer.getPos()+r.data.byteLength>=2**32)throw new Error("Adding more audio data would exceed the maximum RIFF size of 4 GiB. To write larger files, use RF64 by setting `large: true` in the WavOutputFormatOptions.");this.writer.write(r.data),this.dataSize+=r.data.byteLength,this.sampleCount+=Math.round(r.duration*this.sampleRate),await this.writer.flush()}finally{a()}}async addSubtitleCue(){throw new Error("WAVE does not support subtitles.")}writeHeader(e,r){this.format._options.onHeader&&this.writer.startTrackingWrites();let i,a=e.source._codec,s=Se(a);s.dataType==="ulaw"?i=7:s.dataType==="alaw"?i=6:s.dataType==="float"?i=3:i=1;let o=r.numberOfChannels,c=r.sampleRate,u=s.sampleSize*o;if(this.riffWriter.writeAscii(this.isRf64?"RF64":"RIFF"),this.isRf64?this.riffWriter.writeU32(4294967295):(this.riffSizePos=this.writer.getPos(),this.riffWriter.writeU32(0)),this.riffWriter.writeAscii("WAVE"),this.isRf64&&(this.riffWriter.writeAscii("ds64"),this.riffWriter.writeU32(28),this.ds64RiffSizePos=this.writer.getPos(),this.riffWriter.writeU64(0),this.ds64DataSizePos=this.writer.getPos(),this.riffWriter.writeU64(0),this.ds64SampleCountPos=this.writer.getPos(),this.riffWriter.writeU64(0),this.riffWriter.writeU32(0)),this.riffWriter.writeAscii("fmt "),this.riffWriter.writeU32(16),this.riffWriter.writeU16(i),this.riffWriter.writeU16(o),this.riffWriter.writeU32(c),this.riffWriter.writeU32(c*u),this.riffWriter.writeU16(u),this.riffWriter.writeU16(8*s.sampleSize),!Vt(this.output._metadataTags)){let l=this.format._options.metadataFormat??"info";l==="info"?this.writeInfoChunk(this.output._metadataTags):l==="id3"?this.writeId3Chunk(this.output._metadataTags):ne(l)}if(this.riffWriter.writeAscii("data"),this.isRf64?this.riffWriter.writeU32(4294967295):(this.dataSizePos=this.writer.getPos(),this.riffWriter.writeU32(0)),this.format._options.onHeader){let{data:l,start:m}=this.writer.stopTrackingWrites();this.format._options.onHeader(l,m)}}writeInfoChunk(e){let r=this.writer.getPos();this.riffWriter.writeAscii("LIST"),this.riffWriter.writeU32(0),this.riffWriter.writeAscii("INFO");let i=new Set,a=(c,u)=>{if(!Je(u)){console.warn(`Didn't write tag '${c}' because '${u}' is not ISO 8859-1-compatible.`);return}let l=u.length+1,m=new Uint8Array(l);for(let d=0;d<u.length;d++)m[d]=u.charCodeAt(d);this.riffWriter.writeAscii(c),this.riffWriter.writeU32(l),this.writer.write(m),l&1&&this.writer.write(new Uint8Array(1)),i.add(c)};for(let{key:c,value:u}of it(e))switch(c){case"title":a("INAM",u),i.add("INAM");break;case"artist":a("IART",u),i.add("IART");break;case"album":a("IPRD",u),i.add("IPRD");break;case"trackNumber":{let l=e.tracksTotal!==void 0?`${u}/${e.tracksTotal}`:u.toString();a("ITRK",l),i.add("ITRK")}break;case"genre":a("IGNR",u),i.add("IGNR");break;case"date":a("ICRD",u.toISOString().slice(0,10)),i.add("ICRD");break;case"comment":a("ICMT",u),i.add("ICMT");break;case"albumArtist":case"discNumber":case"tracksTotal":case"discsTotal":case"description":case"lyrics":case"images":break;case"raw":break;default:ne(c)}if(e.raw)for(let c in e.raw){let u=e.raw[c];u==null||c.length!==4||i.has(c)||typeof u=="string"&&a(c,u)}let s=this.writer.getPos(),o=s-r-8;this.writer.seek(r+4),this.riffWriter.writeU32(o),this.writer.seek(s),o&1&&this.writer.write(new Uint8Array(1))}writeId3Chunk(e){let r=this.writer.getPos();this.riffWriter.writeAscii("ID3 "),this.riffWriter.writeU32(0);let a=new Tr(this.writer).writeId3V2Tag(e),s=this.writer.getPos();this.writer.seek(r+4),this.riffWriter.writeU32(a),this.writer.seek(s),a&1&&this.writer.write(new Uint8Array(1))}async finalize(){let e=await this.mutex.acquire(),r=this.writer.getPos();this.isRf64?(h(this.ds64RiffSizePos!==null),this.writer.seek(this.ds64RiffSizePos),this.riffWriter.writeU64(r-8),h(this.ds64DataSizePos!==null),this.writer.seek(this.ds64DataSizePos),this.riffWriter.writeU64(this.dataSize),h(this.ds64SampleCountPos!==null),this.writer.seek(this.ds64SampleCountPos),this.riffWriter.writeU64(this.sampleCount)):(h(this.riffSizePos!==null),this.writer.seek(this.riffSizePos),this.riffWriter.writeU32(r-8),h(this.dataSizePos!==null),this.writer.seek(this.dataSizePos),this.riffWriter.writeU32(this.dataSize)),e()}};var Di=class{constructor(t){this.sourceSampleRate=null;this.sourceNumberOfChannels=null;this.maxWrittenFrame=null;this.targetSampleRate=t.targetSampleRate,this.targetNumberOfChannels=t.targetNumberOfChannels,this.endTime=t.endTime,this.onSample=t.onSample,this.bufferSizeInFrames=Math.floor(this.targetSampleRate*5),this.bufferSizeInSamples=this.bufferSizeInFrames*this.targetNumberOfChannels,this.outputBuffer=new Float32Array(this.bufferSizeInSamples),this.bufferStartFrame=Math.floor(t.startTime*this.targetSampleRate),this.timestampOffset=t.startTime-this.bufferStartFrame/this.targetSampleRate}doChannelMixerSetup(){h(this.sourceNumberOfChannels!==null);let t=this.sourceNumberOfChannels,e=this.targetNumberOfChannels;t===1&&e===2?this.channelMixer=(r,i)=>r[i*t]:t===1&&e===4?this.channelMixer=(r,i,a)=>r[i*t]*+(a<2):t===1&&e===6?this.channelMixer=(r,i,a)=>r[i*t]*+(a===2):t===2&&e===1?this.channelMixer=(r,i)=>{let a=i*t;return .5*(r[a]+r[a+1])}:t===2&&e===4?this.channelMixer=(r,i,a)=>r[i*t+a]*+(a<2):t===2&&e===6?this.channelMixer=(r,i,a)=>r[i*t+a]*+(a<2):t===4&&e===1?this.channelMixer=(r,i)=>{let a=i*t;return .25*(r[a]+r[a+1]+r[a+2]+r[a+3])}:t===4&&e===2?this.channelMixer=(r,i,a)=>{let s=i*t;return .5*(r[s+a]+r[s+a+2])}:t===4&&e===6?this.channelMixer=(r,i,a)=>{let s=i*t;return a<2?r[s+a]:a===2||a===3?0:r[s+a-2]}:t===6&&e===1?this.channelMixer=(r,i)=>{let a=i*t;return Math.SQRT1_2*(r[a]+r[a+1])+r[a+2]+.5*(r[a+4]+r[a+5])}:t===6&&e===2?this.channelMixer=(r,i,a)=>{let s=i*t;return r[s+a]+Math.SQRT1_2*(r[s+2]+r[s+a+4])}:t===6&&e===4?this.channelMixer=(r,i,a)=>{let s=i*t;return a<2?r[s+a]+Math.SQRT1_2*r[s+2]:r[s+a+2]}:this.channelMixer=(r,i,a)=>a<t?r[i*t+a]:0}ensureTempBufferSize(t){let e=this.tempSourceBuffer.length;for(;e<t;)e*=2;if(e!==this.tempSourceBuffer.length){let r=new Float32Array(e);r.set(this.tempSourceBuffer),this.tempSourceBuffer=r}}async add(t){this.sourceSampleRate===null&&(this.sourceSampleRate=t.sampleRate,this.sourceNumberOfChannels=t.numberOfChannels,this.tempSourceBuffer=new Float32Array(this.sourceSampleRate*this.sourceNumberOfChannels),this.doChannelMixerSetup());let e=t.numberOfFrames*t.numberOfChannels;this.ensureTempBufferSize(e);let r=t.allocationSize({planeIndex:0,format:"f32"}),i=new Float32Array(this.tempSourceBuffer.buffer,0,r/4);t.copyTo(i,{planeIndex:0,format:"f32"});let a=t.timestamp,s=Math.min(t.timestamp+t.duration,this.endTime),o=Math.floor(a*this.targetSampleRate),c=Math.ceil(s*this.targetSampleRate);for(let u=o;u<c;u++){if(u<this.bufferStartFrame)continue;for(;u>=this.bufferStartFrame+this.bufferSizeInFrames;)await this.finalizeCurrentBuffer(),this.bufferStartFrame+=this.bufferSizeInFrames;let l=u-this.bufferStartFrame;h(l<this.bufferSizeInFrames);let f=(u/this.targetSampleRate-a)*this.sourceSampleRate,p=Math.floor(f),b=Math.ceil(f),g=f-p;for(let y=0;y<this.targetNumberOfChannels;y++){let k=0,T=0;p>=0&&p<t.numberOfFrames&&(k=this.channelMixer(i,p,y)),b>=0&&b<t.numberOfFrames&&(T=this.channelMixer(i,b,y));let w=k+g*(T-k),S=l*this.targetNumberOfChannels+y;this.outputBuffer[S]+=w}this.maxWrittenFrame===null?this.maxWrittenFrame=l:this.maxWrittenFrame=Math.max(this.maxWrittenFrame,l)}}async finalizeCurrentBuffer(){if(this.maxWrittenFrame===null)return;let t=(this.maxWrittenFrame+1)*this.targetNumberOfChannels,e=new Float32Array(t);e.set(this.outputBuffer.subarray(0,t));let r=this.bufferStartFrame/this.targetSampleRate,i=new ye({format:"f32",sampleRate:this.targetSampleRate,numberOfChannels:this.targetNumberOfChannels,timestamp:r+this.timestampOffset,data:e});await this.onSample(i),this.outputBuffer.fill(0),this.maxWrittenFrame=null}finalize(){return this.finalizeCurrentBuffer()}};var Vi=class{constructor(){this._connectedTrack=null;this._closingPromise=null;this._closed=!1}_ensureValidAdd(){if(!this._connectedTrack)throw new Error("Source is not connected to an output track.");if(this._connectedTrack.output.state==="canceled")throw new Error("Output has been canceled.");if(this._connectedTrack.output.state==="finalizing"||this._connectedTrack.output.state==="finalized")throw new Error("Output has been finalized.");if(this._connectedTrack.output.state==="pending")throw new Error("Output has not started.");if(this._closed)throw new Error("Source is closed.")}async _start(){}async _flushAndClose(t){}close(){if(this._closingPromise)return;let t=this._connectedTrack;if(!t)throw new Error("Cannot call close without connecting the source to an output track.");if(t.output.state==="pending")throw new Error("Cannot call close before output has been started.");this._closingPromise=(async()=>{await this._flushAndClose(!1),this._closed=!0,!(t.output.state==="finalizing"||t.output.state==="finalized")&&t.output._muxer.onTrackClose(t)})()}async _flushOrWaitForOngoingClose(t){return this._closingPromise??=(async()=>{await this._flushAndClose(t),this._closed=!0})()}},Jt=class extends Vi{constructor(e){super();this._connectedTrack=null;if(!re.includes(e))throw new TypeError(`Invalid video codec '${e}'. Must be one of: ${re.join(", ")}.`);this._codec=e}},Nc=(n,t)=>{if(n.metadata.hasOnlyKeyPackets&&t.type!=="key")throw new Error("Cannot add non-key packets to a hasOnlyKeyPackets video track.")},Yr=class extends Jt{constructor(t){super(t)}add(t,e){if(!(t instanceof Q))throw new TypeError("packet must be an EncodedPacket.");if(t.isMetadataOnly)throw new TypeError("Metadata-only packets cannot be added.");if(e!==void 0&&(!e||typeof e!="object"))throw new TypeError("meta, when provided, must be an object.");return this._ensureValidAdd(),Nc(this._connectedTrack,t),this._connectedTrack.output._muxer.addEncodedVideoPacket(this._connectedTrack,t,e)}},On=class{constructor(t,e){this.source=t;this.encodingConfig=e;this.ensureEncoderPromise=null;this.encoderInitialized=!1;this.encoder=null;this.muxer=null;this.lastMultipleOfKeyFrameInterval=-1;this.codedWidth=null;this.codedHeight=null;this.outputWidth=null;this.outputHeight=null;this.frameRateLastSample=null;this.frameRateLastTimestamp=null;this.frameRateLastEndTimestamp=null;this.preciseTimings=[];this.customEncoder=null;this.customEncoderCallSerializer=new vt;this.customEncoderQueueSize=0;this.alphaEncoder=null;this.splitter=null;this.splitterCreationFailed=!1;this.alphaFrameQueue=[];this.error=null;this.lastMuxerPromise=Promise.resolve()}async add(t,e,r){let i=t;try{this.checkForEncoderError(),this.source._ensureValidAdd();let a=this.encodingConfig,s=a.sizeChangeBehavior??"deny",o=!1;if(this.codedWidth!==null&&this.codedHeight!==null){if((t.codedWidth!==this.codedWidth||t.codedHeight!==this.codedHeight)&&(o=!0,s==="deny"))throw new Error(`Video sample size must remain constant. Expected ${this.codedWidth}x${this.codedHeight}, got ${t.codedWidth}x${t.codedHeight}. To allow the sample size to change over time, set \`sizeChangeBehavior\` to a value other than 'deny' in the encoding options.`)}else this.codedWidth=t.codedWidth,this.codedHeight=t.codedHeight;if(a.transform?.width!==void 0||a.transform?.height!==void 0||a.transform?.rotate!==void 0||a.transform?.crop!==void 0||a.transform?.force===!0||o&&s!=="passThrough"){let m=a.transform?.width,d=a.transform?.height,f=a.transform?.fit??"fill";o&&s!=="passThrough"&&(h(this.outputWidth),h(this.outputHeight),h(s!=="deny"),m=this.outputWidth,d=this.outputHeight,f=s);let p=await t.transform({width:m,height:d,roundDimensionsTo:2,crop:a.transform?.crop,rotate:a.transform?.rotate,fit:f,alpha:a.alpha});(this.outputWidth===null||this.outputHeight===null)&&(this.outputWidth=p.displayWidth,this.outputHeight=p.displayHeight),e&&t.close(),t=p,e=!0}else(this.outputWidth===null||this.outputHeight===null)&&(this.outputWidth=t.codedWidth,this.outputHeight=t.codedHeight);let l=a.transform?.frameRate;if(l!==void 0){let m=t.timestamp+t.duration,d=fo(t.timestamp,l);if(this.frameRateLastSample!==null)if(d<=this.frameRateLastTimestamp){this.frameRateLastSample.close(),this.frameRateLastSample=t.clone(),this.frameRateLastEndTimestamp=m;return}else await this.padFrameRate(d,r);t===i&&(t=t.clone(),e=!0),t.setTimestamp(d),t.setDuration(1/l),this.frameRateLastSample?.close(),this.frameRateLastSample=t.clone(),this.frameRateLastTimestamp=d,this.frameRateLastEndTimestamp=m}await this.processAndEncode(t,r)}finally{e&&t.close()}}async processAndEncode(t,e){let r=this.encodingConfig,i;if(r.transform?.process){let a=r.transform.process(t);if(a instanceof Promise&&(a=await a),a===null)return;Array.isArray(a)||(a=[a]),i=a.map(s=>s instanceof Ae?s:typeof VideoFrame<"u"&&s instanceof VideoFrame?new Ae(s):new Ae(s,{timestamp:t.timestamp,duration:t.duration}))}else i=[t];try{for(let a of i){this.encoderInitialized||(this.ensureEncoderPromise||this.ensureEncoder(a),this.encoderInitialized||await this.ensureEncoderPromise),h(this.encoderInitialized);let s=this.encodingConfig.keyFrameInterval??2,o=Math.floor(a.timestamp/s),c={...e,keyFrame:e?.keyFrame||s===0||o!==this.lastMultipleOfKeyFrameInterval};if(this.lastMultipleOfKeyFrameInterval=o,this.customEncoder){this.customEncoderQueueSize++;let u=a.clone(),l=this.customEncoderCallSerializer.call(()=>this.customEncoder.encode(u,c)).then(()=>this.customEncoderQueueSize--).catch(m=>this.error??=m).finally(()=>{u.close()});this.customEncoderQueueSize>=4&&await l}else{h(this.encoder);let u=a.toVideoFrame(),l=H(this.preciseTimings,u.timestamp,d=>d.microsecondTimestamp),m=l!==-1?this.preciseTimings[l]:null;if(m&&m.microsecondTimestamp===u.timestamp?(m.timestamp!==a.timestamp&&(m.timestampIsValid=!1),m.duration!==a.duration&&(m.durationIsValid=!1)):(this.preciseTimings.splice(l+1,0,{microsecondTimestamp:u.timestamp,timestamp:a.timestamp,duration:a.duration,timestampIsValid:!0,durationIsValid:!0}),this.preciseTimings.length>128&&this.preciseTimings.shift()),!this.alphaEncoder)this.encoder.encode(u,c),u.close();else if(!!u.format&&!u.format.includes("A")||this.splitterCreationFailed)this.alphaFrameQueue.push(null),this.encoder.encode(u,c),u.close();else{let f=u.displayWidth,p=u.displayHeight;this.splitter||(this.splitter=new Lc(f,p));let{colorFrame:b,alphaFrame:g}=await this.splitter.update(u);this.alphaFrameQueue.push(g),this.encoder.encode(b,c),b.close()}this.encoder.encodeQueueSize>=4&&await new Promise(d=>this.encoder.addEventListener("dequeue",d,{once:!0}))}await this.lastMuxerPromise}}finally{for(let a of i)a!==t&&a.close()}}async padFrameRate(t,e){let r=this.encodingConfig.transform.frameRate;h(this.frameRateLastSample);let i=Math.round((t-this.frameRateLastTimestamp)*r);for(let a=1;a<i;a++){let s=this.frameRateLastSample.clone();s.setTimestamp(this.frameRateLastTimestamp+a/r),s.setDuration(1/r),await this.processAndEncode(s,e),s.close()}}ensureEncoder(t){this.ensureEncoderPromise=(async()=>{let e=fc({...this.encodingConfig,width:t.codedWidth,height:t.codedHeight,squarePixelWidth:t.squarePixelWidth,squarePixelHeight:t.squarePixelHeight,framerate:this.source._connectedTrack?.metadata.frameRate});this.encodingConfig.onEncoderConfig?.(e);let r=Ci.find(i=>i.supports(this.encodingConfig.codec,e));if(r)this.customEncoder=new r,this.customEncoder.codec=this.encodingConfig.codec,this.customEncoder.config=e,this.customEncoder.onPacket=(i,a)=>{if(!(i instanceof Q))throw new TypeError("The first argument passed to onPacket must be an EncodedPacket.");if(a!==void 0&&(!a||typeof a!="object"))throw new TypeError("The second argument passed to onPacket must be an object or undefined.");Nc(this.source._connectedTrack,i),this.encodingConfig.onEncodedPacket?.(i,a),this.lastMuxerPromise=this.muxer.addEncodedVideoPacket(this.source._connectedTrack,i,a).catch(s=>{this.error??=s})},await this.customEncoder.init();else{if(typeof VideoEncoder>"u")throw new Error("VideoEncoder is not supported by this browser.");if(e.alpha="discard",this.encodingConfig.alpha==="keep"&&(e.latencyMode="quality"),(e.width%2===1||e.height%2===1)&&(this.encodingConfig.codec==="avc"||this.encodingConfig.codec==="hevc"))throw new Error(`The dimensions ${e.width}x${e.height} are not supported for codec '${this.encodingConfig.codec}'; both width and height must be even numbers. Make sure to round your dimensions to the nearest even number.`);if(!(await VideoEncoder.isConfigSupported(e)).supported)throw new Error(`This specific encoder configuration (${e.codec}, ${e.bitrate} bps, ${e.width}x${e.height}, hardware acceleration: ${e.hardwareAcceleration??"no-preference"}) is not supported by this browser. Consider using another codec or changing your video parameters.`);let s=[],o=[],c=0,u=0,l=(d,f,p)=>{let b={};if(f){let T=new Uint8Array(f.byteLength);f.copyTo(T),b.alpha=T}let g=Q.fromEncodedChunk(d,b),y=H(this.preciseTimings,d.timestamp,T=>T.microsecondTimestamp),k=y!==-1?this.preciseTimings[y]:null;k&&k.microsecondTimestamp===d.timestamp&&(g=g.clone({timestamp:k.timestampIsValid?k.timestamp:void 0,duration:k.durationIsValid?k.duration:void 0})),Nc(this.source._connectedTrack,g),this.encodingConfig.onEncodedPacket?.(g,p),this.lastMuxerPromise=this.muxer.addEncodedVideoPacket(this.source._connectedTrack,g,p).catch(T=>{this.error??=T})},m=new Error("Encoding error").stack;if(this.encoder=new VideoEncoder({output:(d,f)=>{if(!this.alphaEncoder){l(d,null,f);return}let p=this.alphaFrameQueue.shift();h(p!==void 0),p?(this.alphaEncoder.encode(p,{keyFrame:d.type==="key"}),u++,p.close(),s.push({chunk:d,meta:f})):u===0?l(d,null,f):(o.push(c+u),s.push({chunk:d,meta:f}))},error:d=>{d.stack=m,this.error??=d}}),this.encoder.configure(e),this.encodingConfig.alpha==="keep"){let d=new Error("Encoding error").stack;this.alphaEncoder=new VideoEncoder({output:(f,p)=>{u--;let b=s.shift();for(h(b!==void 0),l(b.chunk,f,b.meta),c++;o.length>0&&o[0]===c;){o.shift();let g=s.shift();h(g!==void 0),l(g.chunk,null,g.meta)}},error:f=>{f.stack=d,this.error??=f}}),this.alphaEncoder.configure(e)}}h(this.source._connectedTrack),this.muxer=this.source._connectedTrack.output._muxer,this.encoderInitialized=!0})()}async flushAndClose(t){if(t||this.checkForEncoderError(),!t&&this.frameRateLastSample){let e=this.encodingConfig.transform.frameRate,r=fo(this.frameRateLastEndTimestamp,e);await this.padFrameRate(r)}this.frameRateLastSample?.close(),this.frameRateLastSample=null,this.customEncoder?(t||this.customEncoderCallSerializer.call(()=>this.customEncoder.flush()),await this.customEncoderCallSerializer.call(()=>this.customEncoder.close())):this.encoder&&(t||(await this.encoder.flush(),await this.alphaEncoder?.flush()),this.encoder.state!=="closed"&&this.encoder.close(),this.alphaEncoder&&this.alphaEncoder.state!=="closed"&&this.alphaEncoder.close(),this.alphaFrameQueue.forEach(e=>e?.close()),this.splitter?.close()),t||this.checkForEncoderError()}getQueueSize(){return this.customEncoder?this.customEncoderQueueSize:this.encoder?.encodeQueueSize??0}checkForEncoderError(){if(this.error)throw this.error}},qd=!1,Qs=class Qs{constructor(t,e){this.canvas=null;this.gl=null;this.colorProgram=null;this.alphaProgram=null;this.vao=null;this.sourceTexture=null;this.alphaResolutionLocation=null;this.worker=null;this.pendingRequests=new Map;this.nextRequestId=0;let r=typeof OffscreenCanvas<"u"||typeof document<"u"&&typeof document.createElement=="function";if(!Qs.forceCpu&&r&&!qd)try{typeof OffscreenCanvas<"u"?this.canvas=new OffscreenCanvas(t,e):(this.canvas=document.createElement("canvas"),this.canvas.width=t,this.canvas.height=e);let i=this.canvas.getContext("webgl2",{alpha:!0});if(!i)throw new Error("Couldn't acquire WebGL 2 context.");this.gl=i,this.colorProgram=this.createColorProgram(),this.alphaProgram=this.createAlphaProgram(),this.vao=this.createVAO(),this.sourceTexture=this.createTexture(),this.alphaResolutionLocation=this.gl.getUniformLocation(this.alphaProgram,"u_resolution"),this.gl.useProgram(this.colorProgram),this.gl.uniform1i(this.gl.getUniformLocation(this.colorProgram,"u_sourceTexture"),0),this.gl.useProgram(this.alphaProgram),this.gl.uniform1i(this.gl.getUniformLocation(this.alphaProgram,"u_sourceTexture"),0)}catch(i){this.gl=null,this.canvas=null,qd=!0,console.warn("Falling back to CPU for color/alpha splitting.",i)}}async update(t){return this.gl?this.updateGpu(t):this.updateCpu(t)}updateGpu(t){h(this.gl),h(this.canvas),(t.displayWidth!==this.canvas.width||t.displayHeight!==this.canvas.height)&&(this.canvas.width=t.displayWidth,this.canvas.height=t.displayHeight),this.gl.activeTexture(this.gl.TEXTURE0),this.gl.bindTexture(this.gl.TEXTURE_2D,this.sourceTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,t);let e=this.runColorProgram(t),r=this.runAlphaProgram(t);return t.close(),{colorFrame:e,alphaFrame:r}}createVertexShader(){return h(this.gl),this.createShader(this.gl.VERTEX_SHADER,`#version 300 es
39
+ ${r.notes??""}`,m=this.createInternalChunk(u,r.timestamp,r.duration,"key",l.trim()?$.encode(l):null);s.chunkQueue.push(m),await this.interleaveChunks()}finally{a()}}async interleaveChunks(e=!1){if(!(!e&&!this.allTracksAreKnown())){e:for(;;){let r=null,i=1/0;for(let s of this.trackDatas){if(!e&&s.chunkQueue.length===0&&!s.closed)break e;s.chunkQueue.length>0&&s.chunkQueue[0].timestamp<i&&(r=s,i=s.chunkQueue[0].timestamp)}if(!r)break;let a=r.chunkQueue.shift();this.writeBlock(r,a)}e||await this.writer.flush()}}fixVP9ColorSpace(e,r){if(r.type!=="key"||!e.info.decoderConfig.colorSpace||!e.info.decoderConfig.colorSpace.matrix)return;let i=new W(r.data);i.skipBits(2);let a=i.readBits(1),o=(i.readBits(1)<<1)+a;if(o===3&&i.skipBits(1),i.readBits(1)||i.readBits(1)!==0||(i.skipBits(2),i.readBits(24)!==4817730))return;o>=2&&i.skipBits(1);let m={rgb:7,bt709:2,bt470bg:1,smpte170m:3}[e.info.decoderConfig.colorSpace.matrix];du(r.data,i.pos,i.pos+3,m)}createInternalChunk(e,r,i,a,s=null){return{data:e,type:a,timestamp:r,duration:i,additions:s}}writeBlock(e,r){this.segment||this.createSegment();let i=Math.round(1e3*r.timestamp),a=this.trackDatas.every(m=>{if(e===m)return r.type==="key";let d=m.chunkQueue[0];return d?d.type==="key":m.closed}),s=!1;if(!this.currentCluster)s=!0;else{h(this.currentClusterStartMsTimestamp!==null),h(this.currentClusterMaxMsTimestamp!==null);let m=i-this.currentClusterStartMsTimestamp;s=a&&i>this.currentClusterMaxMsTimestamp&&m>=1e3*(this.format._options.minimumClusterDuration??1)||m>Pp}s&&this.createNewCluster(i);let o=i-this.currentClusterStartMsTimestamp;if(o<xp)return;let c=new Uint8Array(4),u=new DataView(c.buffer);u.setUint8(0,128|e.track.id),u.setInt16(1,o,!1);let l=Math.round(1e3*r.duration);if(r.additions){let m={id:160,data:[{id:161,data:[c,r.data]},r.type==="delta"?{id:251,data:new ln(e.lastWrittenMsTimestamp-i)}:null,r.additions?{id:30113,data:[{id:166,data:[{id:238,data:1},{id:165,data:r.additions}]}]}:null,l>0?{id:155,data:l}:null]};this.ebmlWriter.writeEBML(m)}else{u.setUint8(3,+(r.type==="key")<<7);let m={id:163,data:[c,r.data]};this.ebmlWriter.writeEBML(m)}this.startTimestamp=Math.min(this.startTimestamp,i),this.endTimestamp=Math.max(this.endTimestamp,i+l),e.lastWrittenMsTimestamp=i,this.trackDatasInCurrentCluster.has(e)||this.trackDatasInCurrentCluster.set(e,{firstMsTimestamp:i}),this.currentClusterMaxMsTimestamp=Math.max(this.currentClusterMaxMsTimestamp,i)}createNewCluster(e){this.currentCluster&&this.finalizeCurrentCluster(),this.format._options.onCluster&&this.writer.startTrackingWrites(),this.currentCluster={id:524531317,size:this.format._options.appendOnly?-1:Dd,data:[{id:231,data:e}]},this.ebmlWriter.writeEBML(this.currentCluster),this.currentClusterStartMsTimestamp=e,this.currentClusterMaxMsTimestamp=e,this.trackDatasInCurrentCluster.clear()}finalizeCurrentCluster(){if(h(this.currentCluster),!this.format._options.appendOnly){let a=this.writer.getPos()-this.ebmlWriter.dataOffsets.get(this.currentCluster),s=this.writer.getPos();this.writer.seek(this.ebmlWriter.offsets.get(this.currentCluster)+4),this.ebmlWriter.writeVarInt(a,Dd),this.writer.seek(s)}if(this.format._options.onCluster){h(this.currentClusterStartMsTimestamp!==null);let{data:a,start:s}=this.writer.stopTrackingWrites();this.format._options.onCluster(a,s,this.currentClusterStartMsTimestamp/1e3)}let e=this.ebmlWriter.offsets.get(this.currentCluster)-this.segmentDataOffset,r=new Map;for(let[a,{firstMsTimestamp:s}]of this.trackDatasInCurrentCluster)r.has(s)||r.set(s,[]),r.get(s).push(a);let i=[...r.entries()].sort((a,s)=>a[0]-s[0]);for(let[a,s]of i)h(this.cues),this.cues.data.push({id:187,data:[{id:179,data:a},...s.map(o=>({id:183,data:[{id:247,data:o.track.id},{id:241,data:e}]}))]})}async onTrackClose(e){let r=await this.mutex.acquire(),i=this.trackDatas.find(a=>a.track===e);i&&(i.closed=!0),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),await this.interleaveChunks(),r()}async finalize(){let e=await this.mutex.acquire();this.allTracksKnown.resolve();for(let r of this.trackDatas)r.closed=!0;if(this.segment||this.createSegment(),await this.interleaveChunks(!0),this.currentCluster&&this.finalizeCurrentCluster(),h(this.cues),this.ebmlWriter.writeEBML(this.cues),!this.format._options.appendOnly){let r=this.writer.getPos()-this.segmentDataOffset;this.writer.seek(this.ebmlWriter.offsets.get(this.segment)+4),this.ebmlWriter.writeVarInt(r,Od);let i=this.startTimestamp===1/0?0:this.endTimestamp-this.startTimestamp;this.segmentDuration.data=new fi(i),this.writer.seek(this.ebmlWriter.offsets.get(this.segmentDuration)),this.ebmlWriter.writeEBML(this.segmentDuration),h(this.seekHead),this.writer.seek(this.ebmlWriter.offsets.get(this.seekHead)),this.maybeCreateSeekHead(!0),this.ebmlWriter.writeEBML(this.seekHead)}e()}};var Ls=class{constructor(t){this.writer=t;this.helper=new Uint8Array(8);this.helperView=new DataView(this.helper.buffer)}writeU32(t){this.helperView.setUint32(0,t,!1),this.writer.write(this.helper.subarray(0,4))}writeXingFrame(t){let e=this.writer.getPos(),r=255,i=224|t.mpegVersionId<<3|t.layer<<1,a;t.mpegVersionId&2?a=t.mpegVersionId&1?0:1:a=1;let s=0,o=155,c=-1,u=a*16*4+t.layer*16;for(let g=0;g<16;g++){let y=$n[u+g];if(Yn(a,t.layer,1e3*y,t.sampleRate,s)>=o){c=g;break}}if(c===-1)throw new Error("No suitable bitrate found.");let l=c<<4|t.frequencyIndex<<2|s<<1,m=t.channel<<6|t.modeExtension<<4|t.copyright<<3|t.original<<2|t.emphasis;this.helper[0]=r,this.helper[1]=i,this.helper[2]=l,this.helper[3]=m,this.writer.write(this.helper.subarray(0,4));let d=cr(t.mpegVersionId,t.channel);this.writer.seek(e+d),this.writeU32(or);let f=0;t.frameCount!==null&&(f|=1),t.fileSize!==null&&(f|=2),t.toc!==null&&(f|=4),this.writeU32(f),this.writeU32(t.frameCount??0),this.writeU32(t.fileSize??0),this.writer.write(t.toc??new Uint8Array(100));let p=$n[u+c],b=Yn(a,t.layer,1e3*p,t.sampleRate,s);this.writer.seek(e+b)}};var Ws=class extends ge{constructor(e,r){super(e);this.xingFrameData=null;this.frameCount=0;this.framePositions=[];this.xingFramePos=null;this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(this.format._options.xingHeader===!1),this.mp3Writer=new Ls(this.writer),Vt(this.output._metadataTags)||new wr(this.writer).writeId3V2Tag(this.output._metadataTags),e()}async getMimeType(){return"audio/mpeg"}async addEncodedVideoPacket(){throw new Error("MP3 does not support video.")}async addEncodedAudioPacket(e,r){let i=await this.mutex.acquire();try{let a=this.format._options.xingHeader!==!1;if(!this.xingFrameData&&a){let s=V(r.data);if(s.byteLength<4)throw new Error("Invalid MP3 header in sample.");let o=s.getUint32(0,!1),c=Br(o,null).header;if(!c)throw new Error("Invalid MP3 header in sample.");let u=cr(c.mpegVersionId,c.channel);if(s.byteLength>=u+4){let l=s.getUint32(u,!1);if(l===or||l===ai)return}this.xingFrameData={mpegVersionId:c.mpegVersionId,layer:c.layer,frequencyIndex:c.frequencyIndex,sampleRate:c.sampleRate,channel:c.channel,modeExtension:c.modeExtension,copyright:c.copyright,original:c.original,emphasis:c.emphasis,frameCount:null,fileSize:null,toc:null},this.xingFramePos=this.writer.getPos(),this.mp3Writer.writeXingFrame(this.xingFrameData),this.frameCount++}this.validateTimestamp(e,r.timestamp,r.type==="key"),a&&this.framePositions.push(this.writer.getPos()),this.writer.write(r.data),this.frameCount++,await this.writer.flush()}finally{i()}}async addSubtitleCue(){throw new Error("MP3 does not support subtitles.")}async finalize(){if(!this.xingFrameData||this.xingFramePos===null)return;let e=await this.mutex.acquire(),i=this.writer.getPos()-this.xingFramePos;this.writer.seek(this.xingFramePos);let a=new Uint8Array(100);for(let s=0;s<100;s++){let o=Math.floor(this.framePositions.length*(s/100)),c=this.framePositions[o]-this.xingFramePos;a[s]=256*(c/i)}if(this.xingFrameData.frameCount=this.frameCount,this.xingFrameData.fileSize=i,this.xingFrameData.toc=a,this.format._options.onXingFrame&&this.writer.startTrackingWrites(),this.mp3Writer.writeXingFrame(this.xingFrameData),this.format._options.onXingFrame){let{data:s,start:o}=this.writer.stopTrackingWrites();this.format._options.onXingFrame(s,o)}e()}};var Ip=8192,Hs=class extends ge{constructor(e,r){super(e);this.trackDatas=[];this.bosPagesWritten=!1;this.allTracksKnown=Z();this.pageBytes=new Uint8Array(Ra);this.pageView=new DataView(this.pageBytes.buffer);this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!0),e()}async getMimeType(){return await this.allTracksKnown.promise,_a({codecStrings:this.trackDatas.map(e=>e.codecInfo.codec)})}addEncodedVideoPacket(){throw new Error("Video tracks are not supported.")}getTrackData(e,r){let i=this.trackDatas.find(o=>o.track===e);if(i)return i;let a;do a=Math.floor(2**32*Math.random());while(this.trackDatas.some(o=>o.serialNumber===a));h(e.source._codec==="vorbis"||e.source._codec==="opus"),Ee(r),h(r),h(r.decoderConfig);let s={track:e,serialNumber:a,internalSampleRate:e.source._codec==="opus"?nt:r.decoderConfig.sampleRate,codecInfo:{codec:e.source._codec,vorbisInfo:null,opusInfo:null},vorbisLastBlocksize:null,packetQueue:[],currentTimestampInSamples:0,pagesWritten:0,currentGranulePosition:0,currentLacingValues:[],currentPageData:[],currentPageSize:27,currentPageStartsWithFreshPacket:!0,currentPageStartTimestampInSamples:0,closed:!1};return this.queueHeaderPackets(s,r),this.trackDatas.push(s),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),s}queueHeaderPackets(e,r){if(h(r.decoderConfig),e.track.source._codec==="vorbis"){h(r.decoderConfig.description);let i=j(r.decoderConfig.description);if(i[0]!==2)throw new TypeError("First byte of Vorbis decoder description must be 2.");let a=1,s=()=>{let g=0;for(;;){let y=i[a++];if(y===void 0)throw new TypeError("Vorbis decoder description is too short.");if(g+=y,y<255)return g}},o=s(),c=s();if(i.length-a<=0)throw new TypeError("Vorbis decoder description is too short.");let l=i.subarray(a,a+=o);a+=c;let m=i.subarray(a),d=new Uint8Array(7);d[0]=3,d[1]=118,d[2]=111,d[3]=114,d[4]=98,d[5]=105,d[6]=115;let f=nn(d,this.output._metadataTags,!0);e.packetQueue.push({data:l,timestampInSamples:0,durationInSamples:0,forcePageFlush:!0},{data:f,timestampInSamples:0,durationInSamples:0,forcePageFlush:!1},{data:m,timestampInSamples:0,durationInSamples:0,forcePageFlush:!0});let b=V(l).getUint8(28);e.codecInfo.vorbisInfo={blocksizes:[1<<(b&15),1<<(b>>4)],modeBlockflags:aa(m).modeBlockflags}}else if(e.track.source._codec==="opus"){if(!r.decoderConfig.description)throw new TypeError("For Ogg, Opus decoder description is required.");let i=j(r.decoderConfig.description),a=new Uint8Array(8),s=V(a);s.setUint32(0,1332770163,!1),s.setUint32(4,1415669619,!1);let o=nn(a,this.output._metadataTags,!0);e.packetQueue.push({data:i,timestampInSamples:0,durationInSamples:0,forcePageFlush:!0},{data:o,timestampInSamples:0,durationInSamples:0,forcePageFlush:!0}),e.codecInfo.opusInfo={preSkip:lr(i).preSkip}}}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getTrackData(e,i);this.validateTimestamp(s.track,r.timestamp,r.type==="key");let o=s.currentTimestampInSamples,{durationInSamples:c,vorbisBlockSize:u}=va(r.data,s.codecInfo,s.vorbisLastBlocksize);s.currentTimestampInSamples+=c,s.vorbisLastBlocksize=u,s.packetQueue.push({data:r.data,timestampInSamples:o,durationInSamples:c,forcePageFlush:!1}),await this.interleavePages()}finally{a()}}addSubtitleCue(){throw new Error("Subtitle tracks are not supported.")}allTracksAreKnown(){for(let e of this.output._tracks)if(!e.source._closed&&!this.trackDatas.some(r=>r.track===e))return!1;return!0}async interleavePages(e=!1){if(!this.bosPagesWritten){if(!this.allTracksAreKnown()&&!e)return;for(let r of this.trackDatas)for(;r.packetQueue.length>0;){let i=r.packetQueue.shift();if(this.writePacket(r,i,!1),i.forcePageFlush)break}this.bosPagesWritten=!0}e:for(;;){let r=null,i=1/0;for(let o of this.trackDatas){if(!e&&o.packetQueue.length<=1&&!o.closed)break e;o.packetQueue.length>0&&o.packetQueue[0].timestampInSamples<i&&(r=o,i=o.packetQueue[0].timestampInSamples)}if(!r)break;let a=r.packetQueue.shift(),s=r.packetQueue.length===0;this.writePacket(r,a,s)}e||await this.writer.flush()}writePacket(e,r,i){let a=r.timestampInSamples+r.durationInSamples;if(this.format._options.maximumPageDuration!==void 0){let l=this.format._options.maximumPageDuration*e.internalSampleRate;e.currentLacingValues.length>0&&a-e.currentPageStartTimestampInSamples>l&&this.writePage(e,!1)}let s=r.data.length,o=0,c=0;for(;;){e.currentLacingValues.length===0&&o>0&&(e.currentPageStartsWithFreshPacket=!1);let l=Math.min(255,s);e.currentLacingValues.push(l),e.currentPageSize++,c+=l;let m=s<255;if(e.currentLacingValues.length===255){let d=r.data.subarray(o,c);if(o=c,e.currentPageData.push(d),e.currentPageSize+=d.length,this.writePage(e,i&&m),m)return}if(m)break;s-=255}let u=r.data.subarray(o);e.currentPageData.push(u),e.currentPageSize+=u.length,e.currentGranulePosition=a,(e.currentPageSize>=Ip||r.forcePageFlush)&&this.writePage(e,i)}writePage(e,r){this.pageView.setUint32(0,pn,!0),this.pageView.setUint8(4,0);let i=0;e.currentPageStartsWithFreshPacket||(i|=1),e.pagesWritten===0&&(i|=2),r&&(i|=4),this.pageView.setUint8(5,i);let a=e.currentLacingValues.every(u=>u===255)?-1:e.currentGranulePosition;yu(this.pageView,6,a,!0),this.pageView.setUint32(14,e.serialNumber,!0),this.pageView.setUint32(18,e.pagesWritten,!0),this.pageView.setUint32(22,0,!0),this.pageView.setUint8(26,e.currentLacingValues.length),this.pageBytes.set(e.currentLacingValues,27);let s=27+e.currentLacingValues.length;for(let u of e.currentPageData)this.pageBytes.set(u,s),s+=u.length;let o=this.pageBytes.subarray(0,s),c=Ea(o);if(this.pageView.setUint32(22,c,!0),e.pagesWritten++,e.currentLacingValues.length=0,e.currentPageData.length=0,e.currentPageSize=27,e.currentPageStartsWithFreshPacket=!0,e.currentPageStartTimestampInSamples=e.currentGranulePosition,this.format._options.onPage&&this.writer.startTrackingWrites(),this.writer.write(o),this.format._options.onPage){let{data:u,start:l}=this.writer.stopTrackingWrites();this.format._options.onPage(u,l,e.track.source)}}async onTrackClose(e){let r=await this.mutex.acquire(),i=this.trackDatas.find(a=>a.track===e);i&&(i.closed=!0),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),await this.interleavePages(),r()}async finalize(){let e=await this.mutex.acquire();this.allTracksKnown.resolve();for(let r of this.trackDatas)r.closed=!0;await this.interleavePages(!0);for(let r of this.trackDatas)r.currentLacingValues.length>0&&this.writePage(r,!0);e()}};var Ep=0,Ld=4096,Vd=256,vp=224,Ud=192,zd=new Uint8Array([9,240]),Nd=new Uint8Array([70,1]),qs=class extends ge{constructor(e,r){super(e);this.trackDatas=[];this.tablesWritten=!1;this.continuityCounters=new Map;this.packetBuffer=new Uint8Array(188);this.packetView=V(this.packetBuffer);this.allTracksKnown=Z();this.videoTrackIndex=0;this.audioTrackIndex=0;this.adaptationFieldBuffer=new Uint8Array(184);this.payloadBuffer=new Uint8Array(184);this.format=r}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!0),e()}async getMimeType(){return await this.allTracksKnown.promise,za(this.trackDatas.map(e=>e.codecString))}getVideoTrackData(e,r){let i=this.trackDatas.find(l=>l.track===e);if(i)return i;sr(r),h(r?.decoderConfig);let a=e.source._codec;h(a==="avc"||a==="hevc");let s=a==="avc"?27:36,o=Vd+this.trackDatas.length,c=vp+this.videoTrackIndex++,u={track:e,pid:o,streamType:s,streamId:c,codecString:r.decoderConfig.codec,timestampProcessingQueue:[],packetQueue:[],inputIsAnnexB:null,inputIsAdts:null,avcDecoderConfig:null,hevcDecoderConfig:null,adtsHeader:null,adtsHeaderBitstream:null,firstPacketWritten:!1,closed:!1};return this.trackDatas.push(u),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),u}getAudioTrackData(e,r){let i=this.trackDatas.find(l=>l.track===e);if(i)return i;Ee(r),h(r?.decoderConfig);let a=e.source._codec;h(a==="aac"||a==="mp3"||a==="ac3"||a==="eac3");let s,o;switch(a){case"aac":s=15,o=Ud+this.audioTrackIndex++;break;case"mp3":s=3,o=Ud+this.audioTrackIndex++;break;case"ac3":s=129,o=189;break;case"eac3":s=135,o=189;break}let c=Vd+this.trackDatas.length,u={track:e,pid:c,streamType:s,streamId:o,codecString:r.decoderConfig.codec,timestampProcessingQueue:[],packetQueue:[],inputIsAnnexB:null,inputIsAdts:null,avcDecoderConfig:null,hevcDecoderConfig:null,adtsHeader:null,adtsHeaderBitstream:null,firstPacketWritten:!1,closed:!1};return this.trackDatas.push(u),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),u}async addEncodedVideoPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getVideoTrackData(e,i);this.validateTimestamp(s.track,r.timestamp,r.type==="key");let o=this.prepareVideoPacket(s,r,i);r.type==="key"&&await this.flushTimestampQueue(s),s.timestampProcessingQueue.push({data:o,presentationTimestamp:r.timestamp,decodeTimestamp:null,isKeyframe:r.type==="key"})}finally{a()}}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{let s=this.getAudioTrackData(e,i);this.validateTimestamp(s.track,r.timestamp,r.type==="key");let o=this.prepareAudioPacket(s,r,i);r.type==="key"&&await this.flushTimestampQueue(s),s.timestampProcessingQueue.push({data:o,presentationTimestamp:r.timestamp,decodeTimestamp:null,isKeyframe:r.type==="key"})}finally{a()}}async addSubtitleCue(){throw new Error("MPEG-TS does not support subtitles.")}prepareVideoPacket(e,r,i){let a=e.track.source._codec;if(e.inputIsAnnexB===null){let s=i?.decoderConfig?.description;if(e.inputIsAnnexB=!s,!e.inputIsAnnexB){let o=j(s);a==="avc"?e.avcDecoderConfig=ra(o):e.hevcDecoderConfig=$u(o)}}return e.inputIsAnnexB?this.prepareAnnexBVideoPacket(r.data,a):this.prepareLengthPrefixedVideoPacket(e,r,a)}prepareAnnexBVideoPacket(e,r){let i=[];for(let s of ur(e)){let o=e.subarray(s.offset,s.offset+s.length);(r==="avc"?zt(o[0])===9:at(o[0])===35)||i.push(o)}let a=r==="avc"?zd:Nd;return i.unshift(a),en(i)}prepareLengthPrefixedVideoPacket(e,r,i){let a=r.data,s=i==="avc"?e.avcDecoderConfig.lengthSizeMinusOne+1:e.hevcDecoderConfig.lengthSizeMinusOne+1,o=[];for(let u of Jn(a,s)){let l=a.subarray(u.offset,u.offset+u.length);(i==="avc"?zt(l[0])===9:at(l[0])===35)||o.push(l)}if(r.type==="key")if(i==="avc"){let u=e.avcDecoderConfig;for(let l of u.pictureParameterSets)o.unshift(l);for(let l of u.sequenceParameterSets)o.unshift(l)}else{let u=e.hevcDecoderConfig;for(let l of u.arrays)if(l.nalUnitType===34)for(let m of l.nalUnits)o.unshift(m);for(let l of u.arrays)if(l.nalUnitType===33)for(let m of l.nalUnits)o.unshift(m);for(let l of u.arrays)if(l.nalUnitType===32)for(let m of l.nalUnits)o.unshift(m)}let c=i==="avc"?zd:Nd;return o.unshift(c),en(o)}prepareAudioPacket(e,r,i){let a=e.track.source._codec;if(a==="mp3"||a==="ac3"||a==="eac3")return r.data;if(e.inputIsAdts===null){let u=i?.decoderConfig?.description;if(e.inputIsAdts=!u,!e.inputIsAdts){let l=Tt(j(u)),m=qn(l);e.adtsHeader=m.header,e.adtsHeaderBitstream=m.bitstream}}if(e.inputIsAdts)return r.data;h(e.adtsHeader),h(e.adtsHeaderBitstream);let s=e.adtsHeader,o=r.data.byteLength+s.byteLength;Gn(e.adtsHeaderBitstream,o);let c=new Uint8Array(o);return c.set(s,0),c.set(r.data,s.byteLength),c}allTracksAreKnown(){for(let e of this.output._tracks)if(!e.source._closed&&!this.trackDatas.some(r=>r.track===e))return!1;return!0}async flushTimestampQueue(e,r=!0){if(e.timestampProcessingQueue.length===0)return;let i=e.timestampProcessingQueue.map(a=>a.presentationTimestamp).sort((a,s)=>a-s);for(let a=0;a<e.timestampProcessingQueue.length;a++){let s=e.timestampProcessingQueue[a];s.decodeTimestamp=i[a],e.packetQueue.push(s)}e.timestampProcessingQueue.length=0,r&&await this.interleavePackets()}async interleavePackets(e=!1){if(!this.tablesWritten){if(!this.allTracksAreKnown()&&!e)return;this.writeTables()}e:for(;;){let r=null,i=1/0;for(let s of this.trackDatas){if(!e&&s.packetQueue.length===0&&!s.closed)break e;s.packetQueue.length>0&&s.packetQueue[0].presentationTimestamp<i&&(r=s,i=s.packetQueue[0].presentationTimestamp)}if(!r)break;let a=r.packetQueue.shift();this.writePesPacket(r,a)}e||await this.writer.flush()}writeTables(){h(!this.tablesWritten),this.writePsiSection(Ep,Yr),this.writePsiSection(Ld,Rp(this.trackDatas)),this.tablesWritten=!0}writePsiSection(e,r){let i=0,a=!0;for(;i<r.length;){let o=184-(a?1:0),c=r.length-i,u=Math.min(o,c),l;a?(l=this.payloadBuffer.subarray(0,1+u),l[0]=0,l.set(r.subarray(i,i+u),1)):l=r.subarray(i,i+u),this.writeTsPacket(e,a,null,l),i+=u,a=!1}}writePesPacket(e,r){let i=e.track.type==="video",a=i?10:5,s=new Uint8Array(9+a),o=V(s),c=new W(s.subarray(9));Er(o,0,1,!1),s[3]=e.streamId;let u=e.track.type==="video"?0:Math.min(8+r.data.length,65535);o.setUint16(4,u,!1),o.setUint8(6,132),o.setUint8(7,i?192:128),o.setUint8(8,a);let l=Math.round(r.presentationTimestamp*9e4);if(c.pos=0,c.writeBits(4,i?3:2),c.writeBits(3,l>>>30&7),c.writeBits(1,1),c.writeBits(15,l>>>15&32767),c.writeBits(1,1),c.writeBits(15,l&32767),c.writeBits(1,1),i){h(r.decodeTimestamp!==null);let p=Math.round(r.decodeTimestamp*9e4);c.writeBits(4,1),c.writeBits(3,p>>>30&7),c.writeBits(1,1),c.writeBits(15,p>>>15&32767),c.writeBits(1,1),c.writeBits(15,p&32767),c.writeBits(1,1)}let m=s.length+r.data.length,d=0,f=!0;for(;d<m;){let p=f,b=m-d,g=f&&r.isKeyframe,y=f&&!e.firstPacketWritten,k=Math.max(0,184-b),w;g||y?w=Math.max(2,k):w=k;let T=null;if(w>0){let I=this.adaptationFieldBuffer;w===1?I[0]=0:(I[0]=w-1,I[1]=Number(y)<<7|Number(g)<<6,I.fill(255,2,w)),T=I.subarray(0,w)}let S=Math.min(184-w,b),x=this.payloadBuffer.subarray(0,S),P=0;if(d<s.length){let I=Math.min(s.length-d,S);x.set(s.subarray(d,d+I),0),P=I}let A=Math.max(0,d-s.length),C=A+(S-P);P<S&&x.set(r.data.subarray(A,C),P),this.writeTsPacket(e.pid,p,T,x),d+=S,f=!1}e.firstPacketWritten=!0}writeTsPacket(e,r,i,a){let s=this.continuityCounters.get(e)??0,o=a.length>0,c=i?o?3:2:o?1:0;this.packetBuffer[0]=71,this.packetView.setUint16(1,(r?16384:0)|e&8191,!1),this.packetBuffer[3]=c<<4|s&15,o&&this.continuityCounters.set(e,s+1&15);let u=4;i&&(this.packetBuffer.set(i,u),u+=i.length),this.packetBuffer.set(a,u),u+=a.length,u<188&&this.packetBuffer.fill(255,u);let l=this.writer.getPos();this.writer.write(this.packetBuffer),this.format._options.onPacket&&this.format._options.onPacket(this.packetBuffer.slice(),l)}async onTrackClose(e){let r=await this.mutex.acquire(),i=this.trackDatas.find(a=>a.track===e);i&&(i.closed=!0,await this.flushTimestampQueue(i,!1)),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),await this.interleavePackets(),r()}async finalize(){let e=await this.mutex.acquire();this.allTracksKnown.resolve();for(let r of this.trackDatas)r.closed=!0,await this.flushTimestampQueue(r,!1);await this.interleavePackets(!0),e()}},_p=79764919,Wd=new Uint32Array(256);for(let n=0;n<256;n++){let t=n<<24;for(let e=0;e<8;e++)t=t&2147483648?t<<1^_p:t<<1;Wd[n]=t>>>0&4294967295}var Hd=n=>{let t=4294967295;for(let e=0;e<n.length;e++){let r=n[e];t=(t<<8^Wd[t>>>24^r])>>>0}return t},Yr=new Uint8Array(16);{let n=V(Yr);Yr[0]=0,n.setUint16(1,45069,!1),n.setUint16(3,1,!1),Yr[5]=193,Yr[6]=0,Yr[7]=0,n.setUint16(8,1,!1),n.setUint16(10,57344|Ld&8191,!1),n.setUint32(12,Hd(Yr.subarray(0,12)),!1)}var Rp=n=>{let t=0;for(let c of n)t+=5,c.streamType===129?t+=sn.length:c.streamType===135&&(t+=on.length);let e=9+t+4,r=new Uint8Array(3+e-4),i=V(r);r[0]=2,i.setUint16(1,45056|e&4095,!1),i.setUint16(3,1,!1),r[5]=193,r[6]=0,r[7]=0,i.setUint16(8,65535,!1),i.setUint16(10,61440,!1);let a=12;for(let c of n)r[a++]=c.streamType,i.setUint16(a,57344|c.pid&8191,!1),a+=2,c.streamType===129?(i.setUint16(a,61440|sn.length,!1),a+=2,r.set(sn,a),a+=sn.length):c.streamType===135?(i.setUint16(a,61440|on.length,!1),a+=2,r.set(on,a),a+=on.length):(i.setUint16(a,61440,!1),a+=2);let s=Hd(r),o=new Uint8Array(r.length+4);return o.set(r,0),V(o).setUint32(r.length,s,!1),o};var Gs=class{constructor(t){this.writer=t;this.helper=new Uint8Array(8);this.helperView=new DataView(this.helper.buffer)}writeU16(t){this.helperView.setUint16(0,t,!0),this.writer.write(this.helper.subarray(0,2))}writeU32(t){this.helperView.setUint32(0,t,!0),this.writer.write(this.helper.subarray(0,4))}writeU64(t){this.helperView.setUint32(0,t,!0),this.helperView.setUint32(4,Math.floor(t/2**32),!0),this.writer.write(this.helper)}writeAscii(t){this.writer.write(new TextEncoder().encode(t))}};var Ks=class extends ge{constructor(e,r){super(e);this.headerWritten=!1;this.dataSize=0;this.sampleRate=null;this.sampleCount=0;this.riffSizePos=null;this.dataSizePos=null;this.ds64RiffSizePos=null;this.ds64DataSizePos=null;this.ds64SampleCountPos=null;this.format=r,this.isRf64=!!r._options.large}async start(){let e=await this.mutex.acquire();this.writer=await this.output._getRootWriter(!1),this.riffWriter=new Gs(this.writer),e()}async getMimeType(){return"audio/wav"}async addEncodedVideoPacket(){throw new Error("WAVE does not support video.")}async addEncodedAudioPacket(e,r,i){let a=await this.mutex.acquire();try{if(this.headerWritten||(Ee(i),h(i),h(i.decoderConfig),this.writeHeader(e,i.decoderConfig),this.sampleRate=i.decoderConfig.sampleRate,this.headerWritten=!0),this.validateTimestamp(e,r.timestamp,r.type==="key"),!this.isRf64&&this.writer.getPos()+r.data.byteLength>=2**32)throw new Error("Adding more audio data would exceed the maximum RIFF size of 4 GiB. To write larger files, use RF64 by setting `large: true` in the WavOutputFormatOptions.");this.writer.write(r.data),this.dataSize+=r.data.byteLength,this.sampleCount+=Math.round(r.duration*this.sampleRate),await this.writer.flush()}finally{a()}}async addSubtitleCue(){throw new Error("WAVE does not support subtitles.")}writeHeader(e,r){this.format._options.onHeader&&this.writer.startTrackingWrites();let i,a=e.source._codec,s=Se(a);s.dataType==="ulaw"?i=7:s.dataType==="alaw"?i=6:s.dataType==="float"?i=3:i=1;let o=r.numberOfChannels,c=r.sampleRate,u=s.sampleSize*o;if(this.riffWriter.writeAscii(this.isRf64?"RF64":"RIFF"),this.isRf64?this.riffWriter.writeU32(4294967295):(this.riffSizePos=this.writer.getPos(),this.riffWriter.writeU32(0)),this.riffWriter.writeAscii("WAVE"),this.isRf64&&(this.riffWriter.writeAscii("ds64"),this.riffWriter.writeU32(28),this.ds64RiffSizePos=this.writer.getPos(),this.riffWriter.writeU64(0),this.ds64DataSizePos=this.writer.getPos(),this.riffWriter.writeU64(0),this.ds64SampleCountPos=this.writer.getPos(),this.riffWriter.writeU64(0),this.riffWriter.writeU32(0)),this.riffWriter.writeAscii("fmt "),this.riffWriter.writeU32(16),this.riffWriter.writeU16(i),this.riffWriter.writeU16(o),this.riffWriter.writeU32(c),this.riffWriter.writeU32(c*u),this.riffWriter.writeU16(u),this.riffWriter.writeU16(8*s.sampleSize),!Vt(this.output._metadataTags)){let l=this.format._options.metadataFormat??"info";l==="info"?this.writeInfoChunk(this.output._metadataTags):l==="id3"?this.writeId3Chunk(this.output._metadataTags):ne(l)}if(this.riffWriter.writeAscii("data"),this.isRf64?this.riffWriter.writeU32(4294967295):(this.dataSizePos=this.writer.getPos(),this.riffWriter.writeU32(0)),this.format._options.onHeader){let{data:l,start:m}=this.writer.stopTrackingWrites();this.format._options.onHeader(l,m)}}writeInfoChunk(e){let r=this.writer.getPos();this.riffWriter.writeAscii("LIST"),this.riffWriter.writeU32(0),this.riffWriter.writeAscii("INFO");let i=new Set,a=(c,u)=>{if(!Je(u)){console.warn(`Didn't write tag '${c}' because '${u}' is not ISO 8859-1-compatible.`);return}let l=u.length+1,m=new Uint8Array(l);for(let d=0;d<u.length;d++)m[d]=u.charCodeAt(d);this.riffWriter.writeAscii(c),this.riffWriter.writeU32(l),this.writer.write(m),l&1&&this.writer.write(new Uint8Array(1)),i.add(c)};for(let{key:c,value:u}of it(e))switch(c){case"title":a("INAM",u),i.add("INAM");break;case"artist":a("IART",u),i.add("IART");break;case"album":a("IPRD",u),i.add("IPRD");break;case"trackNumber":{let l=e.tracksTotal!==void 0?`${u}/${e.tracksTotal}`:u.toString();a("ITRK",l),i.add("ITRK")}break;case"genre":a("IGNR",u),i.add("IGNR");break;case"date":a("ICRD",u.toISOString().slice(0,10)),i.add("ICRD");break;case"comment":a("ICMT",u),i.add("ICMT");break;case"albumArtist":case"discNumber":case"tracksTotal":case"discsTotal":case"description":case"lyrics":case"images":break;case"raw":break;default:ne(c)}if(e.raw)for(let c in e.raw){let u=e.raw[c];u==null||c.length!==4||i.has(c)||typeof u=="string"&&a(c,u)}let s=this.writer.getPos(),o=s-r-8;this.writer.seek(r+4),this.riffWriter.writeU32(o),this.writer.seek(s),o&1&&this.writer.write(new Uint8Array(1))}writeId3Chunk(e){let r=this.writer.getPos();this.riffWriter.writeAscii("ID3 "),this.riffWriter.writeU32(0);let a=new wr(this.writer).writeId3V2Tag(e),s=this.writer.getPos();this.writer.seek(r+4),this.riffWriter.writeU32(a),this.writer.seek(s),a&1&&this.writer.write(new Uint8Array(1))}async finalize(){let e=await this.mutex.acquire(),r=this.writer.getPos();this.isRf64?(h(this.ds64RiffSizePos!==null),this.writer.seek(this.ds64RiffSizePos),this.riffWriter.writeU64(r-8),h(this.ds64DataSizePos!==null),this.writer.seek(this.ds64DataSizePos),this.riffWriter.writeU64(this.dataSize),h(this.ds64SampleCountPos!==null),this.writer.seek(this.ds64SampleCountPos),this.riffWriter.writeU64(this.sampleCount)):(h(this.riffSizePos!==null),this.writer.seek(this.riffSizePos),this.riffWriter.writeU32(r-8),h(this.dataSizePos!==null),this.writer.seek(this.dataSizePos),this.riffWriter.writeU32(this.dataSize)),e()}};var Di=class{constructor(t){this.sourceSampleRate=null;this.sourceNumberOfChannels=null;this.maxWrittenFrame=null;this.targetSampleRate=t.targetSampleRate,this.targetNumberOfChannels=t.targetNumberOfChannels,this.endTime=t.endTime,this.onSample=t.onSample,this.bufferSizeInFrames=Math.floor(this.targetSampleRate*5),this.bufferSizeInSamples=this.bufferSizeInFrames*this.targetNumberOfChannels,this.outputBuffer=new Float32Array(this.bufferSizeInSamples),this.bufferStartFrame=Math.floor(t.startTime*this.targetSampleRate),this.timestampOffset=t.startTime-this.bufferStartFrame/this.targetSampleRate}doChannelMixerSetup(){h(this.sourceNumberOfChannels!==null);let t=this.sourceNumberOfChannels,e=this.targetNumberOfChannels;t===1&&e===2?this.channelMixer=(r,i)=>r[i*t]:t===1&&e===4?this.channelMixer=(r,i,a)=>r[i*t]*+(a<2):t===1&&e===6?this.channelMixer=(r,i,a)=>r[i*t]*+(a===2):t===2&&e===1?this.channelMixer=(r,i)=>{let a=i*t;return .5*(r[a]+r[a+1])}:t===2&&e===4?this.channelMixer=(r,i,a)=>r[i*t+a]*+(a<2):t===2&&e===6?this.channelMixer=(r,i,a)=>r[i*t+a]*+(a<2):t===4&&e===1?this.channelMixer=(r,i)=>{let a=i*t;return .25*(r[a]+r[a+1]+r[a+2]+r[a+3])}:t===4&&e===2?this.channelMixer=(r,i,a)=>{let s=i*t;return .5*(r[s+a]+r[s+a+2])}:t===4&&e===6?this.channelMixer=(r,i,a)=>{let s=i*t;return a<2?r[s+a]:a===2||a===3?0:r[s+a-2]}:t===6&&e===1?this.channelMixer=(r,i)=>{let a=i*t;return Math.SQRT1_2*(r[a]+r[a+1])+r[a+2]+.5*(r[a+4]+r[a+5])}:t===6&&e===2?this.channelMixer=(r,i,a)=>{let s=i*t;return r[s+a]+Math.SQRT1_2*(r[s+2]+r[s+a+4])}:t===6&&e===4?this.channelMixer=(r,i,a)=>{let s=i*t;return a<2?r[s+a]+Math.SQRT1_2*r[s+2]:r[s+a+2]}:this.channelMixer=(r,i,a)=>a<t?r[i*t+a]:0}ensureTempBufferSize(t){let e=this.tempSourceBuffer.length;for(;e<t;)e*=2;if(e!==this.tempSourceBuffer.length){let r=new Float32Array(e);r.set(this.tempSourceBuffer),this.tempSourceBuffer=r}}async add(t){this.sourceSampleRate===null&&(this.sourceSampleRate=t.sampleRate,this.sourceNumberOfChannels=t.numberOfChannels,this.tempSourceBuffer=new Float32Array(this.sourceSampleRate*this.sourceNumberOfChannels),this.doChannelMixerSetup());let e=t.numberOfFrames*t.numberOfChannels;this.ensureTempBufferSize(e);let r=t.allocationSize({planeIndex:0,format:"f32"}),i=new Float32Array(this.tempSourceBuffer.buffer,0,r/4);t.copyTo(i,{planeIndex:0,format:"f32"});let a=t.timestamp,s=Math.min(t.timestamp+t.duration,this.endTime),o=Math.floor(a*this.targetSampleRate),c=Math.ceil(s*this.targetSampleRate);for(let u=o;u<c;u++){if(u<this.bufferStartFrame)continue;for(;u>=this.bufferStartFrame+this.bufferSizeInFrames;)await this.finalizeCurrentBuffer(),this.bufferStartFrame+=this.bufferSizeInFrames;let l=u-this.bufferStartFrame;h(l<this.bufferSizeInFrames);let f=(u/this.targetSampleRate-a)*this.sourceSampleRate,p=Math.floor(f),b=Math.ceil(f),g=f-p;for(let y=0;y<this.targetNumberOfChannels;y++){let k=0,w=0;p>=0&&p<t.numberOfFrames&&(k=this.channelMixer(i,p,y)),b>=0&&b<t.numberOfFrames&&(w=this.channelMixer(i,b,y));let T=k+g*(w-k),S=l*this.targetNumberOfChannels+y;this.outputBuffer[S]+=T}this.maxWrittenFrame===null?this.maxWrittenFrame=l:this.maxWrittenFrame=Math.max(this.maxWrittenFrame,l)}}async finalizeCurrentBuffer(){if(this.maxWrittenFrame===null)return;let t=(this.maxWrittenFrame+1)*this.targetNumberOfChannels,e=new Float32Array(t);e.set(this.outputBuffer.subarray(0,t));let r=this.bufferStartFrame/this.targetSampleRate,i=new ye({format:"f32",sampleRate:this.targetSampleRate,numberOfChannels:this.targetNumberOfChannels,timestamp:r+this.timestampOffset,data:e});await this.onSample(i),this.outputBuffer.fill(0),this.maxWrittenFrame=null}finalize(){return this.finalizeCurrentBuffer()}};var Vi=class{constructor(){this._connectedTrack=null;this._closingPromise=null;this._closed=!1}_ensureValidAdd(){if(!this._connectedTrack)throw new Error("Source is not connected to an output track.");if(this._connectedTrack.output.state==="canceled")throw new Error("Output has been canceled.");if(this._connectedTrack.output.state==="finalizing"||this._connectedTrack.output.state==="finalized")throw new Error("Output has been finalized.");if(this._connectedTrack.output.state==="pending")throw new Error("Output has not started.");if(this._closed)throw new Error("Source is closed.")}async _start(){}async _flushAndClose(t){}close(){if(this._closingPromise)return;let t=this._connectedTrack;if(!t)throw new Error("Cannot call close without connecting the source to an output track.");if(t.output.state==="pending")throw new Error("Cannot call close before output has been started.");this._closingPromise=(async()=>{await this._flushAndClose(!1),this._closed=!0,!(t.output.state==="finalizing"||t.output.state==="finalized")&&t.output._muxer.onTrackClose(t)})()}async _flushOrWaitForOngoingClose(t){return this._closingPromise??=(async()=>{await this._flushAndClose(t),this._closed=!0})()}},Jt=class extends Vi{constructor(e){super();this._connectedTrack=null;if(!re.includes(e))throw new TypeError(`Invalid video codec '${e}'. Must be one of: ${re.join(", ")}.`);this._codec=e}},Nc=(n,t)=>{if(n.metadata.hasOnlyKeyPackets&&t.type!=="key")throw new Error("Cannot add non-key packets to a hasOnlyKeyPackets video track.")},Zr=class extends Jt{constructor(t){super(t)}add(t,e){if(!(t instanceof Q))throw new TypeError("packet must be an EncodedPacket.");if(t.isMetadataOnly)throw new TypeError("Metadata-only packets cannot be added.");if(e!==void 0&&(!e||typeof e!="object"))throw new TypeError("meta, when provided, must be an object.");return this._ensureValidAdd(),Nc(this._connectedTrack,t),this._connectedTrack.output._muxer.addEncodedVideoPacket(this._connectedTrack,t,e)}},On=class{constructor(t,e){this.source=t;this.encodingConfig=e;this.ensureEncoderPromise=null;this.encoderInitialized=!1;this.encoder=null;this.muxer=null;this.lastMultipleOfKeyFrameInterval=-1;this.emittedEncoderPackets=0;this.codedWidth=null;this.codedHeight=null;this.outputWidth=null;this.outputHeight=null;this.frameRateLastSample=null;this.frameRateLastTimestamp=null;this.frameRateLastEndTimestamp=null;this.preciseTimings=[];this.customEncoder=null;this.customEncoderCallSerializer=new vt;this.customEncoderQueueSize=0;this.alphaEncoder=null;this.splitter=null;this.splitterCreationFailed=!1;this.alphaFrameQueue=[];this.error=null;this.lastMuxerPromise=Promise.resolve()}async add(t,e,r){let i=t;try{this.checkForEncoderError(),this.source._ensureValidAdd();let a=this.encodingConfig,s=a.sizeChangeBehavior??"deny",o=!1;if(this.codedWidth!==null&&this.codedHeight!==null){if((t.codedWidth!==this.codedWidth||t.codedHeight!==this.codedHeight)&&(o=!0,s==="deny"))throw new Error(`Video sample size must remain constant. Expected ${this.codedWidth}x${this.codedHeight}, got ${t.codedWidth}x${t.codedHeight}. To allow the sample size to change over time, set \`sizeChangeBehavior\` to a value other than 'deny' in the encoding options.`)}else this.codedWidth=t.codedWidth,this.codedHeight=t.codedHeight;if(a.transform?.width!==void 0||a.transform?.height!==void 0||a.transform?.rotate!==void 0||a.transform?.crop!==void 0||a.transform?.force===!0||o&&s!=="passThrough"){let m=a.transform?.width,d=a.transform?.height,f=a.transform?.fit??"fill";o&&s!=="passThrough"&&(h(this.outputWidth),h(this.outputHeight),h(s!=="deny"),m=this.outputWidth,d=this.outputHeight,f=s);let p=await t.transform({width:m,height:d,roundDimensionsTo:2,crop:a.transform?.crop,rotate:a.transform?.rotate,fit:f,alpha:a.alpha});(this.outputWidth===null||this.outputHeight===null)&&(this.outputWidth=p.displayWidth,this.outputHeight=p.displayHeight),e&&t.close(),t=p,e=!0}else(this.outputWidth===null||this.outputHeight===null)&&(this.outputWidth=t.codedWidth,this.outputHeight=t.codedHeight);let l=a.transform?.frameRate;if(l!==void 0){let m=t.timestamp+t.duration,d=fo(t.timestamp,l);if(this.frameRateLastSample!==null)if(d<=this.frameRateLastTimestamp){this.frameRateLastSample.close(),this.frameRateLastSample=t.clone(),this.frameRateLastEndTimestamp=m;return}else await this.padFrameRate(d,r);t===i&&(t=t.clone(),e=!0),t.setTimestamp(d),t.setDuration(1/l),this.frameRateLastSample?.close(),this.frameRateLastSample=t.clone(),this.frameRateLastTimestamp=d,this.frameRateLastEndTimestamp=m}await this.processAndEncode(t,r)}finally{e&&t.close()}}async processAndEncode(t,e){let r=this.encodingConfig,i;if(r.transform?.process){let a=r.transform.process(t);if(a instanceof Promise&&(a=await a),a===null)return;Array.isArray(a)||(a=[a]),i=a.map(s=>s instanceof Ae?s:typeof VideoFrame<"u"&&s instanceof VideoFrame?new Ae(s):new Ae(s,{timestamp:t.timestamp,duration:t.duration}))}else i=[t];try{for(let a of i){this.encoderInitialized||(this.ensureEncoderPromise||this.ensureEncoder(a),this.encoderInitialized||await this.ensureEncoderPromise),h(this.encoderInitialized);let s=this.encodingConfig.keyFrameInterval??2,o=Math.floor(a.timestamp/s),c={...e,keyFrame:e?.keyFrame||s===0||o!==this.lastMultipleOfKeyFrameInterval};if(this.lastMultipleOfKeyFrameInterval=o,this.customEncoder){this.customEncoderQueueSize++;let u=a.clone(),l=this.customEncoderCallSerializer.call(()=>this.customEncoder.encode(u,c)).then(()=>this.customEncoderQueueSize--).catch(m=>this.error??=m).finally(()=>{u.close()});this.customEncoderQueueSize>=4&&await l}else{h(this.encoder);let u=a.toVideoFrame(),l=H(this.preciseTimings,u.timestamp,d=>d.microsecondTimestamp),m=l!==-1?this.preciseTimings[l]:null;if(m&&m.microsecondTimestamp===u.timestamp?(m.timestamp!==a.timestamp&&(m.timestampIsValid=!1),m.duration!==a.duration&&(m.durationIsValid=!1)):(this.preciseTimings.splice(l+1,0,{microsecondTimestamp:u.timestamp,timestamp:a.timestamp,duration:a.duration,timestampIsValid:!0,durationIsValid:!0}),this.preciseTimings.length>128&&this.preciseTimings.shift()),!this.alphaEncoder)this.encoder.encode(u,c),u.close();else if(!!u.format&&!u.format.includes("A")||this.splitterCreationFailed)this.alphaFrameQueue.push(null),this.encoder.encode(u,c),u.close();else{let f=u.displayWidth,p=u.displayHeight;this.splitter||(this.splitter=new Lc(f,p));let{colorFrame:b,alphaFrame:g}=await this.splitter.update(u);this.alphaFrameQueue.push(g),this.encoder.encode(b,c),b.close()}this.encoder.encodeQueueSize>=4&&await new Promise(d=>this.encoder.addEventListener("dequeue",d,{once:!0}))}await this.lastMuxerPromise}}finally{for(let a of i)a!==t&&a.close()}}async padFrameRate(t,e){let r=this.encodingConfig.transform.frameRate;h(this.frameRateLastSample);let i=Math.round((t-this.frameRateLastTimestamp)*r);for(let a=1;a<i;a++){let s=this.frameRateLastSample.clone();s.setTimestamp(this.frameRateLastTimestamp+a/r),s.setDuration(1/r),await this.processAndEncode(s,e),s.close()}}ensureEncoder(t){this.ensureEncoderPromise=(async()=>{let e=fc({...this.encodingConfig,width:t.codedWidth,height:t.codedHeight,squarePixelWidth:t.squarePixelWidth,squarePixelHeight:t.squarePixelHeight,framerate:this.source._connectedTrack?.metadata.frameRate});this.encodingConfig.onEncoderConfig?.(e);let r=Ci.find(i=>i.supports(this.encodingConfig.codec,e));if(r)this.customEncoder=new r,this.customEncoder.codec=this.encodingConfig.codec,this.customEncoder.config=e,this.customEncoder.onPacket=(i,a)=>{if(!(i instanceof Q))throw new TypeError("The first argument passed to onPacket must be an EncodedPacket.");if(a!==void 0&&(!a||typeof a!="object"))throw new TypeError("The second argument passed to onPacket must be an object or undefined.");Nc(this.source._connectedTrack,i),this.encodingConfig.onEncodedPacket?.(i,a),this.lastMuxerPromise=this.muxer.addEncodedVideoPacket(this.source._connectedTrack,i,a).catch(s=>{this.error??=s})},await this.customEncoder.init();else{if(typeof VideoEncoder>"u")throw new Error("VideoEncoder is not supported by this browser.");if(e.alpha="discard",this.encodingConfig.alpha==="keep"&&(e.latencyMode="quality"),(e.width%2===1||e.height%2===1)&&(this.encodingConfig.codec==="avc"||this.encodingConfig.codec==="hevc"))throw new Error(`The dimensions ${e.width}x${e.height} are not supported for codec '${this.encodingConfig.codec}'; both width and height must be even numbers. Make sure to round your dimensions to the nearest even number.`);if(!(await VideoEncoder.isConfigSupported(e)).supported)throw new Error(`This specific encoder configuration (${e.codec}, ${e.bitrate} bps, ${e.width}x${e.height}, hardware acceleration: ${e.hardwareAcceleration??"no-preference"}) is not supported by this browser. Consider using another codec or changing your video parameters.`);let s=[],o=[],c=0,u=0,l=(d,f,p)=>{let b={};if(f){let T=new Uint8Array(f.byteLength);f.copyTo(T),b.alpha=T}let g=Q.fromEncodedChunk(d,b),y=H(this.preciseTimings,d.timestamp,T=>T.microsecondTimestamp),k=y!==-1?this.preciseTimings[y]:null,w=null;this.emittedEncoderPackets===0&&g.type==="delta"&&p?.decoderConfig&&(w=dr(this.encodingConfig.codec,p.decoderConfig,g.data)),(k&&k.microsecondTimestamp===d.timestamp||w!==null)&&(g=g.clone({timestamp:k?.timestampIsValid?k.timestamp:void 0,duration:k?.durationIsValid?k.duration:void 0,type:w??void 0})),Nc(this.source._connectedTrack,g),this.encodingConfig.onEncodedPacket?.(g,p),this.lastMuxerPromise=this.muxer.addEncodedVideoPacket(this.source._connectedTrack,g,p).catch(T=>{this.error??=T}),this.emittedEncoderPackets++},m=new Error("Encoding error").stack;if(this.encoder=new VideoEncoder({output:(d,f)=>{if(!this.alphaEncoder){l(d,null,f);return}let p=this.alphaFrameQueue.shift();h(p!==void 0),p?(this.alphaEncoder.encode(p,{keyFrame:d.type==="key"}),u++,p.close(),s.push({chunk:d,meta:f})):u===0?l(d,null,f):(o.push(c+u),s.push({chunk:d,meta:f}))},error:d=>{d.stack=m,this.error??=d}}),this.encoder.configure(e),this.encodingConfig.alpha==="keep"){let d=new Error("Encoding error").stack;this.alphaEncoder=new VideoEncoder({output:(f,p)=>{u--;let b=s.shift();for(h(b!==void 0),l(b.chunk,f,b.meta),c++;o.length>0&&o[0]===c;){o.shift();let g=s.shift();h(g!==void 0),l(g.chunk,null,g.meta)}},error:f=>{f.stack=d,this.error??=f}}),this.alphaEncoder.configure(e)}}h(this.source._connectedTrack),this.muxer=this.source._connectedTrack.output._muxer,this.encoderInitialized=!0})()}async flushAndClose(t){if(t||this.checkForEncoderError(),!t&&this.frameRateLastSample){let e=this.encodingConfig.transform.frameRate,r=fo(this.frameRateLastEndTimestamp,e);await this.padFrameRate(r)}this.frameRateLastSample?.close(),this.frameRateLastSample=null,this.customEncoder?(t||this.customEncoderCallSerializer.call(()=>this.customEncoder.flush()),await this.customEncoderCallSerializer.call(()=>this.customEncoder.close())):this.encoder&&(t||(await this.encoder.flush(),await this.alphaEncoder?.flush()),this.encoder.state!=="closed"&&this.encoder.close(),this.alphaEncoder&&this.alphaEncoder.state!=="closed"&&this.alphaEncoder.close(),this.alphaFrameQueue.forEach(e=>e?.close()),this.splitter?.close()),t||this.checkForEncoderError()}getQueueSize(){return this.customEncoder?this.customEncoderQueueSize:this.encoder?.encodeQueueSize??0}checkForEncoderError(){if(this.error)throw this.error}},qd=!1,Qs=class Qs{constructor(t,e){this.canvas=null;this.gl=null;this.colorProgram=null;this.alphaProgram=null;this.vao=null;this.sourceTexture=null;this.alphaResolutionLocation=null;this.worker=null;this.pendingRequests=new Map;this.nextRequestId=0;let r=typeof OffscreenCanvas<"u"||typeof document<"u"&&typeof document.createElement=="function";if(!Qs.forceCpu&&r&&!qd)try{typeof OffscreenCanvas<"u"?this.canvas=new OffscreenCanvas(t,e):(this.canvas=document.createElement("canvas"),this.canvas.width=t,this.canvas.height=e);let i=this.canvas.getContext("webgl2",{alpha:!0});if(!i)throw new Error("Couldn't acquire WebGL 2 context.");this.gl=i,this.colorProgram=this.createColorProgram(),this.alphaProgram=this.createAlphaProgram(),this.vao=this.createVAO(),this.sourceTexture=this.createTexture(),this.alphaResolutionLocation=this.gl.getUniformLocation(this.alphaProgram,"u_resolution"),this.gl.useProgram(this.colorProgram),this.gl.uniform1i(this.gl.getUniformLocation(this.colorProgram,"u_sourceTexture"),0),this.gl.useProgram(this.alphaProgram),this.gl.uniform1i(this.gl.getUniformLocation(this.alphaProgram,"u_sourceTexture"),0)}catch(i){this.gl=null,this.canvas=null,qd=!0,console.warn("Falling back to CPU for color/alpha splitting.",i)}}async update(t){return this.gl?this.updateGpu(t):this.updateCpu(t)}updateGpu(t){h(this.gl),h(this.canvas),(t.displayWidth!==this.canvas.width||t.displayHeight!==this.canvas.height)&&(this.canvas.width=t.displayWidth,this.canvas.height=t.displayHeight),this.gl.activeTexture(this.gl.TEXTURE0),this.gl.bindTexture(this.gl.TEXTURE_2D,this.sourceTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,t);let e=this.runColorProgram(t),r=this.runAlphaProgram(t);return t.close(),{colorFrame:e,alphaFrame:r}}createVertexShader(){return h(this.gl),this.createShader(this.gl.VERTEX_SHADER,`#version 300 es
40
40
  in vec2 a_position;
41
41
  in vec2 a_texCoord;
42
42
  out vec2 v_texCoord;
@@ -100,7 +100,7 @@ ${r.notes??""}`,m=this.createInternalChunk(u,r.timestamp,r.duration,"key",l.trim
100
100
 
101
101
  fragColor = result;
102
102
  }
103
- `),r=this.gl.createProgram();return this.gl.attachShader(r,t),this.gl.attachShader(r,e),this.gl.linkProgram(r),r}createShader(t,e){h(this.gl);let r=this.gl.createShader(t);return this.gl.shaderSource(r,e),this.gl.compileShader(r),this.gl.getShaderParameter(r,this.gl.COMPILE_STATUS)||console.error("Shader compile error:",this.gl.getShaderInfoLog(r)),r}createVAO(){h(this.gl),h(this.colorProgram);let t=this.gl.createVertexArray();this.gl.bindVertexArray(t);let e=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),r=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,r),this.gl.bufferData(this.gl.ARRAY_BUFFER,e,this.gl.STATIC_DRAW);let i=this.gl.getAttribLocation(this.colorProgram,"a_position"),a=this.gl.getAttribLocation(this.colorProgram,"a_texCoord");return this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,16,0),this.gl.enableVertexAttribArray(a),this.gl.vertexAttribPointer(a,2,this.gl.FLOAT,!1,16,8),t}createTexture(){h(this.gl);let t=this.gl.createTexture();return this.gl.bindTexture(this.gl.TEXTURE_2D,t),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),t}runColorProgram(t){return h(this.gl),h(this.canvas),this.gl.useProgram(this.colorProgram),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4),new VideoFrame(this.canvas,{timestamp:t.timestamp,duration:t.duration??void 0,alpha:"discard"})}runAlphaProgram(t){h(this.gl),h(this.canvas),this.gl.useProgram(this.alphaProgram),this.gl.uniform2f(this.alphaResolutionLocation,this.canvas.width,this.canvas.height),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4);let{width:e,height:r}=this.canvas,i=Math.ceil(e/2)*Math.ceil(r/2),a=e*r+i*2,s=Math.ceil(a/(e*4)),o=new Uint8Array(4*e*s);this.gl.readPixels(0,0,e,s,this.gl.RGBA,this.gl.UNSIGNED_BYTE,o),o=o.subarray(0,a),h(o[e*r]===128),h(o[o.length-1]===128);let c={format:"I420",codedWidth:e,codedHeight:r,timestamp:t.timestamp,duration:t.duration??void 0,transfer:[o.buffer]};return new VideoFrame(o,c)}updateCpu(t){if(!this.worker){let i=new Blob([`(${Fp.toString()})()`],{type:"application/javascript"}),a=URL.createObjectURL(i);this.worker=new Worker(a),URL.revokeObjectURL(a),this.worker.addEventListener("message",s=>{let o=s.data,c=this.pendingRequests.get(o.id);c&&(this.pendingRequests.delete(o.id),"error"in o?c.reject(new Error(o.error)):c.resolve({colorFrame:o.colorFrame,alphaFrame:o.alphaFrame}))}),this.worker.addEventListener("error",s=>{let o=new Error(s.message||"Color/alpha splitter worker error.");for(let c of this.pendingRequests.values())c.reject(o);this.pendingRequests.clear()})}let e=this.nextRequestId++,r=Z();return this.pendingRequests.set(e,r),this.worker.postMessage({id:e,sourceFrame:t},{transfer:[t]}),r.promise}close(){this.gl?.getExtension("WEBGL_lose_context")?.loseContext(),this.gl=null,this.canvas=null,this.worker?.terminate(),this.worker=null;let t=new Error("Color/alpha splitter closed.");for(let e of this.pendingRequests.values())e.reject(t);this.pendingRequests.clear()}};Qs.forceCpu=!0;var Lc=Qs,Fp=()=>{let n=null,t=Promise.resolve();self.addEventListener("message",a=>{let{id:s,sourceFrame:o}=a.data;t=t.then(async()=>{try{let{colorFrame:c,alphaFrame:u}=await e(o);self.postMessage({id:s,colorFrame:c,alphaFrame:u},{transfer:[c,u]})}catch(c){self.postMessage({id:s,error:c.message})}finally{o.close()}})});let e=async a=>{let s=a.format;if(!s)throw new Error("CPU color/alpha splitting requires a known VideoFrame format.");let o=a.codedWidth,c=a.codedHeight,u=a.allocationSize();if((!n||n.byteLength!==u)&&(n=new Uint8Array(u)),await a.copyTo(n),s==="RGBA"||s==="BGRA")return r(n,o,c,s,a);if(s==="I420A"||s==="I420AP10"||s==="I420AP12"||s==="I422A"||s==="I422AP10"||s==="I422AP12"||s==="I444A"||s==="I444AP10"||s==="I444AP12")return i(n,o,c,s,a);throw new Error(`CPU color/alpha splitting does not support format '${s}'.`)},r=(a,s,o,c,u)=>{let l=s*o,m=Math.ceil(s/2),d=Math.ceil(o/2),f=l+m*d*2,p=new Uint8Array(f);for(let k=0,T=3;k<l;k++,T+=4)p[k]=a[T];p.fill(128,l);let b=new VideoFrame(a,{format:c==="RGBA"?"RGBX":"BGRX",codedWidth:s,codedHeight:o,timestamp:u.timestamp,duration:u.duration??void 0}),g={format:"I420",codedWidth:s,codedHeight:o,timestamp:u.timestamp,duration:u.duration??void 0,transfer:[p.buffer]},y=new VideoFrame(p,g);return{colorFrame:b,alphaFrame:y}},i=(a,s,o,c,u)=>{let l=c.includes("P10"),m=c.includes("P12"),d=l||m?2:1,f,p;c.startsWith("I420")?(f=Math.ceil(s/2),p=Math.ceil(o/2)):c.startsWith("I422")?(f=Math.ceil(s/2),p=o):(f=s,p=o);let b=s*o,g=f*p,y=b*d,k=g*d,T=b*d,w=y+k*2,S=c.replace("A",""),x=Math.ceil(s/2),P=Math.ceil(o/2),A=x*P,C=A*d,I=T+2*C,R=new Uint8Array(I),v=w;R.set(a.subarray(v,v+T),0);let E=T,D=l?512:m?2048:128;d===1?R.fill(D,E):new Uint16Array(R.buffer,E,2*A).fill(D);let U=l?"I420P10":m?"I420P12":"I420",z=new VideoFrame(a.subarray(0,w),{format:S,codedWidth:s,codedHeight:o,timestamp:u.timestamp,duration:u.duration??void 0}),q={format:U,codedWidth:s,codedHeight:o,timestamp:u.timestamp,duration:u.duration??void 0,transfer:[R.buffer]},ke=new VideoFrame(R,q);return{colorFrame:z,alphaFrame:ke}}},Ui=class extends Jt{constructor(t){ks(t),super(t.codec),this._encoder=new On(this,t)}add(t,e){if(!(t instanceof Ae))throw new TypeError("videoSample must be a VideoSample.");return this._encoder.add(t,!1,e)}_flushAndClose(t){return this._encoder.flushAndClose(t)}},Wc=class extends Jt{constructor(t,e){if(!(typeof HTMLCanvasElement<"u"&&t instanceof HTMLCanvasElement)&&!(typeof OffscreenCanvas<"u"&&t instanceof OffscreenCanvas))throw new TypeError("canvas must be an HTMLCanvasElement or OffscreenCanvas.");ks(e),super(e.codec),this._encoder=new On(this,e),this._canvas=t}add(t,e=0,r){if(!Number.isFinite(t)||t<0)throw new TypeError("timestamp must be a non-negative number.");if(!Number.isFinite(e)||e<0)throw new TypeError("duration must be a non-negative number.");let i=new Ae(this._canvas,{timestamp:t,duration:e});return this._encoder.add(i,!0,r)}_flushAndClose(t){return this._encoder.flushAndClose(t)}},Hc=class extends Jt{constructor(e,r,i={}){if(!(e instanceof MediaStreamTrack)||e.kind!=="video")throw new TypeError("track must be a video MediaStreamTrack.");if(ks(r),typeof i!="object"||!i)throw new TypeError("options must be an object.");if(i.frameRate!=null&&(typeof i.frameRate!="number"||i.frameRate<=0))throw new TypeError("options.frameRate, when provided, must be either a positive number or null.");if(i.timestampBase!==void 0&&i.timestampBase!=="synced-zero"&&i.timestampBase!=="zero"&&i.timestampBase!=="unix")throw new TypeError("options.timestampBase, when provided, must be one of 'synced-zero', 'zero', or 'unix'.");r={...r,latencyMode:"realtime"};super(r.codec);this._abortController=null;this._workerTrackId=null;this._workerListener=null;this._promiseWithResolvers=Z();this._errorPromiseAccessed=!1;this._paused=!1;this._lastVideoFrame=null;this._timerHandle=null;this._videoElement=null;this._options=i,this._encoder=new On(this,r),this._track=e}get errorPromise(){return this._errorPromiseAccessed=!0,this._promiseWithResolvers.promise}get paused(){return this._paused}async _start(){this._errorPromiseAccessed||console.warn("Make sure not to ignore the `errorPromise` field on MediaStreamVideoTrackSource, so that any internal errors get bubbled up properly.");let e=this._options.frameRate!==void 0?this._options.frameRate:this._track.getSettings().frameRate??null;this._abortController=new AbortController;let r=null,i=null,a=0,s=!1,o=null,c=0,u=()=>{if(h(e!==null),!this._lastVideoFrame)return;h(i!==null),h(r!==null);let d=performance.now();for(;d-i>1e3/e;){i+=1e3/e;let f=r+a/e,p=new VideoFrame(this._videoElement??this._lastVideoFrame,{timestamp:1e6*f,duration:1e6/e});m(p,d)}};e!==null&&(this._timerHandle=vu(u,4));let l=d=>{if(e===null)m(d);else{let f=performance.now();this._lastVideoFrame?(u(),this._lastVideoFrame?.close(),this._lastVideoFrame=d):(m(d.clone(),f),i=f,this._lastVideoFrame=d)}},m=(d,f=performance.now())=>{if(s){d.close();return}a++;let p=d.timestamp/1e6;if(this._paused){if(r!==null){if(o!==null&&this._options.timestampBase!=="unix"){let y=p-o;c-=y}o=p}d.close();return}if(r===null){r=p;let g,y=this._options.timestampBase??"synced-zero";if(y==="unix")g=Date.now()/1e3;else if(y==="zero")g=0;else{let k=this._connectedTrack.output;k._firstMediaStreamTimestamp===null?(k._firstMediaStreamTimestamp=f/1e3,g=0):g=f/1e3-k._firstMediaStreamTimestamp}c=g-r}if(o=p,this._encoder.getQueueSize()>=8){d.close();return}let b=new Ae(d,{timestamp:p+c});this._encoder.add(b,!0).catch(g=>{s=!0,this._abortController?.abort(),this._promiseWithResolvers.reject(g),this._workerTrackId!==null&&zc({type:"stopTrack",trackId:this._workerTrackId})})};if(typeof MediaStreamTrackProcessor<"u"){let d=new MediaStreamTrackProcessor({track:this._track}),f=new WritableStream({write:l});d.readable.pipeTo(f,{signal:this._abortController.signal}).catch(p=>{p instanceof DOMException&&p.name==="AbortError"||this._promiseWithResolvers.reject(p)})}else if(await Dp())this._workerTrackId=Bp++,zc({type:"videoTrack",trackId:this._workerTrackId,track:this._track}),this._workerListener=f=>{let p=f.data;p.type==="videoFrame"&&p.trackId===this._workerTrackId?l(p.videoFrame):p.type==="error"&&p.trackId===this._workerTrackId&&this._promiseWithResolvers.reject(p.error)},$e.addEventListener("message",this._workerListener);else if(e!==null){let f=document.createElement("video");f.style.position="fixed",f.style.left="-10000px",f.style.top="-10000px",f.style.width="1px",f.style.height="1px",f.style.opacity="0",f.style.pointerEvents="none",f.muted=!0,f.srcObject=new MediaStream([this._track]),document.body.appendChild(f),this._videoElement=f,f.addEventListener("loadeddata",()=>{if(s||!this._videoElement)return;let p=new VideoFrame(f,{timestamp:1e3*performance.now()});l(p),p.close()},{once:!0}),f.play().catch(p=>{s=!0,this._promiseWithResolvers.reject(p)})}else throw new Error("When no explicit frame rate is set, MediaStreamTrackProcessor is required; but it's not supported by this browser.")}pause(){this._paused=!0}resume(){this._paused=!1}async _flushAndClose(e){this._abortController&&(this._abortController.abort(),this._abortController=null),this._timerHandle&&_u(this._timerHandle),this._lastVideoFrame?.close(),this._videoElement&&(this._videoElement.srcObject=null,this._videoElement.remove(),this._videoElement=null),this._workerTrackId!==null&&(h(this._workerListener),zc({type:"stopTrack",trackId:this._workerTrackId}),await new Promise(r=>{let i=a=>{let s=a.data;s.type==="trackStopped"&&s.trackId===this._workerTrackId&&(h(this._workerListener),$e.removeEventListener("message",this._workerListener),$e.removeEventListener("message",i),r())};$e.addEventListener("message",i)})),await this._encoder.flushAndClose(e)}},er=class extends Vi{constructor(e){super();this._connectedTrack=null;if(!ae.includes(e))throw new TypeError(`Invalid audio codec '${e}'. Must be one of: ${ae.join(", ")}.`);this._codec=e}},Zr=class extends er{constructor(t){super(t)}add(t,e){if(!(t instanceof Q))throw new TypeError("packet must be an EncodedPacket.");if(t.isMetadataOnly)throw new TypeError("Metadata-only packets cannot be added.");if(e!==void 0&&(!e||typeof e!="object"))throw new TypeError("meta, when provided, must be an object.");return this._ensureValidAdd(),this._connectedTrack.output._muxer.addEncodedAudioPacket(this._connectedTrack,t,e)}},Dn=class{constructor(t,e){this.source=t;this.encodingConfig=e;this.ensureEncoderPromise=null;this.encoderInitialized=!1;this.encoder=null;this.muxer=null;this.lastNumberOfChannels=null;this.lastSampleRate=null;this.isPcmEncoder=!1;this.outputSampleSize=null;this.writeOutputValue=null;this.customEncoder=null;this.customEncoderCallSerializer=new vt;this.customEncoderQueueSize=0;this.lastEndSampleIndex=null;this.resampler=null;this.error=null;this.lastMuxerPromise=Promise.resolve()}async add(t,e){try{if(this.checkForEncoderError(),this.source._ensureValidAdd(),this.lastNumberOfChannels!==null&&this.lastSampleRate!==null){if(t.numberOfChannels!==this.lastNumberOfChannels||t.sampleRate!==this.lastSampleRate)throw new Error(`Audio parameters must remain constant. Expected ${this.lastNumberOfChannels} channels at ${this.lastSampleRate} Hz, got ${t.numberOfChannels} channels at ${t.sampleRate} Hz.`)}else this.lastNumberOfChannels=t.numberOfChannels,this.lastSampleRate=t.sampleRate;let r=this.encodingConfig;r.transform?.numberOfChannels!==void 0||r.transform?.sampleRate!==void 0?(this.resampler||(this.resampler=new Di({targetNumberOfChannels:r.transform.numberOfChannels??t.numberOfChannels,targetSampleRate:r.transform.sampleRate??t.sampleRate,startTime:t.timestamp,endTime:1/0,onSample:async a=>{await this.processAndEncode(a,!0)}})),await this.resampler.add(t)):await this.processAndEncode(t,e)}finally{e&&t.close()}}async processAndEncode(t,e){let r=this.encodingConfig;if(r.transform?.sampleFormat!==void 0&&hs(t.format)!==r.transform.sampleFormat){let i=gs(t,r.transform.sampleFormat);e&&t.close(),t=i,e=!0}if(r.transform?.process){let i=r.transform.process(t);if(i instanceof Promise&&(i=await i),i===null)return;Array.isArray(i)||(i=[i]);for(let a of i){if(!(a instanceof ye))throw new TypeError("The audio process function must return an AudioSample, null, or an array of AudioSamples.");await this.encodeSample(a,!0)}e&&t.close()}else await this.encodeSample(t,e)}async encodeSample(t,e){try{this.encoderInitialized||(this.ensureEncoderPromise||this.ensureEncoder(t),this.encoderInitialized||await this.ensureEncoderPromise),h(this.encoderInitialized);{let r=Math.round(t.timestamp*t.sampleRate),i=Math.round((t.timestamp+t.duration)*t.sampleRate);if(this.lastEndSampleIndex===null)this.lastEndSampleIndex=i;else{let a=r-this.lastEndSampleIndex;if(a>=64){let s=new ye({data:new Float32Array(a*t.numberOfChannels),format:"f32-planar",sampleRate:t.sampleRate,numberOfChannels:t.numberOfChannels,numberOfFrames:a,timestamp:this.lastEndSampleIndex/t.sampleRate});await this.encodeSample(s,!0)}this.lastEndSampleIndex+=t.numberOfFrames}}if(this.customEncoder){this.customEncoderQueueSize++;let r=t.clone(),i=this.customEncoderCallSerializer.call(()=>this.customEncoder.encode(r)).then(()=>this.customEncoderQueueSize--).catch(a=>this.error??=a).finally(()=>{r.close()});this.customEncoderQueueSize>=4&&await i,await this.lastMuxerPromise}else if(this.isPcmEncoder)await this.doPcmEncoding(t,e);else{h(this.encoder);let r=t.toAudioData();this.encoder.encode(r),r.close(),e&&t.close(),this.encoder.encodeQueueSize>=4&&await new Promise(i=>this.encoder.addEventListener("dequeue",i,{once:!0})),await this.lastMuxerPromise}}finally{e&&t.close()}}async doPcmEncoding(t,e){h(this.outputSampleSize),h(this.writeOutputValue);let{numberOfChannels:r,numberOfFrames:i,sampleRate:a,timestamp:s}=t,o=2048,c=[];for(let d=0;d<i;d+=o){let f=Math.min(o,t.numberOfFrames-d),p=f*r*this.outputSampleSize,b=new ArrayBuffer(p),g=new DataView(b);c.push({frameCount:f,view:g})}let u=t.allocationSize({planeIndex:0,format:"f32-planar"}),l=new Float32Array(u/Float32Array.BYTES_PER_ELEMENT);for(let d=0;d<r;d++){t.copyTo(l,{planeIndex:d,format:"f32-planar"});for(let f=0;f<c.length;f++){let{frameCount:p,view:b}=c[f];for(let g=0;g<p;g++)this.writeOutputValue(b,(g*r+d)*this.outputSampleSize,l[f*o+g])}}e&&t.close();let m={decoderConfig:{codec:this.encodingConfig.codec,numberOfChannels:r,sampleRate:a}};for(let d=0;d<c.length;d++){let{frameCount:f,view:p}=c[d],b=p.buffer,g=d*o,y=new Q(new Uint8Array(b),"key",s+g/a,f/a);this.encodingConfig.onEncodedPacket?.(y,m),await this.muxer.addEncodedAudioPacket(this.source._connectedTrack,y,m)}}ensureEncoder(t){this.ensureEncoderPromise=(async()=>{let{numberOfChannels:e,sampleRate:r}=t,i=pc({numberOfChannels:e,sampleRate:r,...this.encodingConfig});this.encodingConfig.onEncoderConfig?.(i);let a=Ii.find(s=>s.supports(this.encodingConfig.codec,i));if(a)this.customEncoder=new a,this.customEncoder.codec=this.encodingConfig.codec,this.customEncoder.config=i,this.customEncoder.onPacket=(s,o)=>{if(!(s instanceof Q))throw new TypeError("The first argument passed to onPacket must be an EncodedPacket.");if(o!==void 0&&(!o||typeof o!="object"))throw new TypeError("The second argument passed to onPacket must be an object or undefined.");this.encodingConfig.onEncodedPacket?.(s,o),this.lastMuxerPromise=this.muxer.addEncodedAudioPacket(this.source._connectedTrack,s,o).catch(c=>{this.error??=c})},await this.customEncoder.init();else if(ee.includes(this.encodingConfig.codec))this.initPcmEncoder();else{if(typeof AudioEncoder>"u")throw new Error("AudioEncoder is not supported by this browser.");if(!(await AudioEncoder.isConfigSupported(i)).supported)throw new Error(`This specific encoder configuration (${i.codec}, ${i.bitrate} bps, ${i.numberOfChannels} channels, ${i.sampleRate} Hz) is not supported by this browser. Consider using another codec or changing your audio parameters.`);let o=new Error("Encoding error").stack;this.encoder=new AudioEncoder({output:(c,u)=>{if(this.encodingConfig.codec==="aac"&&u?.decoderConfig){let m=!1;if(!u.decoderConfig.description||u.decoderConfig.description.byteLength<2?m=!0:m=Tt(j(u.decoderConfig.description)).objectType===0,m){let d=Number(X(i.codec.split(".")));u.decoderConfig.description=ti({objectType:d,numberOfChannels:u.decoderConfig.numberOfChannels,sampleRate:u.decoderConfig.sampleRate})}}let l=Q.fromEncodedChunk(c);l=l.clone({timestamp:bt(l.timestamp,i.sampleRate),duration:c.duration!=null?bt(l.duration,i.sampleRate):void 0}),this.encodingConfig.onEncodedPacket?.(l,u),this.lastMuxerPromise=this.muxer.addEncodedAudioPacket(this.source._connectedTrack,l,u).catch(m=>{this.error??=m})},error:c=>{c.stack=o,this.error??=c}}),this.encoder.configure(i)}h(this.source._connectedTrack),this.muxer=this.source._connectedTrack.output._muxer,this.encoderInitialized=!0})()}initPcmEncoder(){this.isPcmEncoder=!0;let t=this.encodingConfig.codec,{dataType:e,sampleSize:r,littleEndian:i}=Se(t);switch(this.outputSampleSize=r,r){case 1:e==="unsigned"?this.writeOutputValue=(a,s,o)=>a.setUint8(s,ue((o+1)*127.5,0,255)):e==="signed"?this.writeOutputValue=(a,s,o)=>{a.setInt8(s,ue(Math.round(o*128),-128,127))}:e==="ulaw"?this.writeOutputValue=(a,s,o)=>{let c=ue(Math.floor(o*32767),-32768,32767);a.setUint8(s,od(c))}:e==="alaw"?this.writeOutputValue=(a,s,o)=>{let c=ue(Math.floor(o*32767),-32768,32767);a.setUint8(s,ud(c))}:h(!1);break;case 2:e==="unsigned"?this.writeOutputValue=(a,s,o)=>a.setUint16(s,ue((o+1)*32767.5,0,65535),i):e==="signed"?this.writeOutputValue=(a,s,o)=>a.setInt16(s,ue(Math.round(o*32767),-32768,32767),i):h(!1);break;case 3:e==="unsigned"?this.writeOutputValue=(a,s,o)=>Ir(a,s,ue((o+1)*83886075e-1,0,16777215),i):e==="signed"?this.writeOutputValue=(a,s,o)=>bu(a,s,ue(Math.round(o*8388607),-8388608,8388607),i):h(!1);break;case 4:e==="unsigned"?this.writeOutputValue=(a,s,o)=>a.setUint32(s,ue((o+1)*21474836475e-1,0,4294967295),i):e==="signed"?this.writeOutputValue=(a,s,o)=>a.setInt32(s,ue(Math.round(o*2147483647),-2147483648,2147483647),i):e==="float"?this.writeOutputValue=(a,s,o)=>a.setFloat32(s,o,i):h(!1);break;case 8:e==="float"?this.writeOutputValue=(a,s,o)=>a.setFloat64(s,o,i):h(!1);break;default:ne(r),h(!1)}}async flushAndClose(t){t||this.checkForEncoderError(),!t&&this.resampler&&await this.resampler.finalize(),this.resampler=null,this.customEncoder?(t||this.customEncoderCallSerializer.call(()=>this.customEncoder.flush()),await this.customEncoderCallSerializer.call(()=>this.customEncoder.close())):this.encoder&&(t||await this.encoder.flush(),this.encoder.state!=="closed"&&this.encoder.close()),t||this.checkForEncoderError()}getQueueSize(){return this.customEncoder?this.customEncoderQueueSize:this.isPcmEncoder?0:this.encoder?.encodeQueueSize??0}checkForEncoderError(){if(this.error)throw this.error}},zi=class extends er{constructor(t){Ts(t),super(t.codec),this._encoder=new Dn(this,t)}add(t){if(!(t instanceof ye))throw new TypeError("audioSample must be an AudioSample.");return this._encoder.add(t,!1)}_flushAndClose(t){return this._encoder.flushAndClose(t)}},qc=class extends er{constructor(e){Ts(e);super(e.codec);this._accumulatedTime=0;this._encoder=new Dn(this,e)}async add(e){if(!(e instanceof AudioBuffer))throw new TypeError("audioBuffer must be an AudioBuffer.");let r=ye._fromAudioBuffer(e,this._accumulatedTime);this._accumulatedTime+=e.duration;for(let i of r)await this._encoder.add(i,!0)}_flushAndClose(e){return this._encoder.flushAndClose(e)}},Gc=class extends er{constructor(e,r,i={}){if(!(e instanceof MediaStreamTrack)||e.kind!=="audio")throw new TypeError("track must be an audio MediaStreamTrack.");if(Ts(r),typeof i!="object"||!i)throw new TypeError("options must be an object.");if(i.timestampBase!==void 0&&i.timestampBase!=="synced-zero"&&i.timestampBase!=="zero"&&i.timestampBase!=="unix")throw new TypeError("options.timestampBase, when provided, must be one of 'synced-zero', 'zero', or 'unix'.");super(r.codec);this._abortController=null;this._audioContext=null;this._scriptProcessorNode=null;this._promiseWithResolvers=Z();this._errorPromiseAccessed=!1;this._paused=!1;this._options=i,this._encoder=new Dn(this,r),this._track=e}get errorPromise(){return this._errorPromiseAccessed=!0,this._promiseWithResolvers.promise}get paused(){return this._paused}async _start(){this._errorPromiseAccessed||console.warn("Make sure not to ignore the `errorPromise` field on MediaStreamAudioTrackSource, so that any internal errors get bubbled up properly."),this._abortController=new AbortController;let e=null,r=!1,i=null,a=0,s=o=>{if(r){o.close();return}let c=o.timestamp;if(this._paused){if(e!==null){if(i!==null&&this._options.timestampBase!=="unix"){let l=c-i;a-=l}i=c}o.close();return}if(e===null){e=o.timestamp;let u,l=this._options.timestampBase??"synced-zero";if(l==="unix")u=Date.now()/1e3;else if(l==="zero")u=0;else{let m=this._connectedTrack.output;m._firstMediaStreamTimestamp===null?(m._firstMediaStreamTimestamp=performance.now()/1e3,u=0):u=performance.now()/1e3-m._firstMediaStreamTimestamp}a=u-e}if(i=c,this._encoder.getQueueSize()>=8){o.close();return}o.setTimestamp(c+a),this._encoder.add(o,!0).catch(u=>{r=!0,this._abortController?.abort(),this._promiseWithResolvers.reject(u),this._audioContext?.suspend()})};if(typeof MediaStreamTrackProcessor<"u"){let o=new MediaStreamTrackProcessor({track:this._track}),c=new WritableStream({write:u=>s(new ye(u))});o.readable.pipeTo(c,{signal:this._abortController.signal}).catch(u=>{u instanceof DOMException&&u.name==="AbortError"||this._promiseWithResolvers.reject(u)})}else{let o=window.AudioContext||window.webkitAudioContext;this._audioContext=new o({sampleRate:this._track.getSettings().sampleRate});let c=this._audioContext.createMediaStreamSource(new MediaStream([this._track]));this._scriptProcessorNode=this._audioContext.createScriptProcessor(4096),this._audioContext.state==="suspended"&&await this._audioContext.resume(),c.connect(this._scriptProcessorNode),this._scriptProcessorNode.connect(this._audioContext.destination);let u=0;this._scriptProcessorNode.onaudioprocess=l=>{let m=ye._fromAudioBuffer(l.inputBuffer,u);u+=l.inputBuffer.duration;for(let d of m)s(d)}}}pause(){this._paused=!0}resume(){this._paused=!1}async _flushAndClose(e){this._abortController&&(this._abortController.abort(),this._abortController=null),this._audioContext&&(h(this._scriptProcessorNode),this._scriptProcessorNode.disconnect(),await this._audioContext.suspend()),await this._encoder.flushAndClose(e)}},Mp=()=>{let n=(r,i)=>{i?self.postMessage(r,{transfer:i}):self.postMessage(r)};n({type:"support",supported:typeof MediaStreamTrackProcessor<"u"});let t=new Map,e=new Map;self.addEventListener("message",r=>{let i=r.data;switch(i.type){case"videoTrack":{e.set(i.trackId,i.track);let a=new MediaStreamTrackProcessor({track:i.track}),s=new WritableStream({write:c=>{if(!e.has(i.trackId)){c.close();return}n({type:"videoFrame",trackId:i.trackId,videoFrame:c},[c])}}),o=new AbortController;t.set(i.trackId,o),a.readable.pipeTo(s,{signal:o.signal}).catch(c=>{c instanceof DOMException&&c.name==="AbortError"||n({type:"error",trackId:i.trackId,error:c})})}break;case"stopTrack":{let a=t.get(i.trackId);a&&(a.abort(),t.delete(i.trackId)),e.get(i.trackId)?.stop(),e.delete(i.trackId),n({type:"trackStopped",trackId:i.trackId})}break;default:ne(i)}})},Bp=0,$e=null,Op=()=>{let n=new Blob([`(${Mp.toString()})()`],{type:"application/javascript"}),t=URL.createObjectURL(n);$e=new Worker(t)},Uc=null,Dp=async()=>Uc!==null?Uc:($e||Op(),new Promise(n=>{h($e);let t=e=>{let r=e.data;r.type==="support"&&(Uc=r.supported,$e.removeEventListener("message",t),n(r.supported))};$e.addEventListener("message",t)})),zc=(n,t)=>{h($e),t?$e.postMessage(n,t):$e.postMessage(n)},Ni=class extends Vi{constructor(e){super();this._connectedTrack=null;if(!Me.includes(e))throw new TypeError(`Invalid subtitle codec '${e}'. Must be one of: ${Me.join(", ")}.`);this._codec=e}},Kc=class extends Ni{constructor(e){super(e);this._error=null;this._lastMuxerPromise=Promise.resolve();this._parser=new Bs({codec:e,output:(r,i)=>{this._lastMuxerPromise=this._connectedTrack.output._muxer.addSubtitleCue(this._connectedTrack,r,i).catch(a=>{this._error??=a})}})}add(e){if(typeof e!="string")throw new TypeError("text must be a string.");return this._checkForError(),this._ensureValidAdd(),this._parser.parse(e),this._lastMuxerPromise}_checkForError(){if(this._error)throw this._error}async _flushAndClose(e){e||this._checkForError()}};var Xs=class extends ge{constructor(e,r){if(!(e._target instanceof Le))throw new TypeError("HLS outputs require `OutputOptions.target` to be a PathedTarget.");super(e);this.trackDatas=[];this.isRelativeToUnixEpoch=!1;this.numWrittenMasterPlaylists=0;this.playlists=[];this.playlistDeclarations=[];this.format=r,this.targetSegmentDuration=r._options.targetDuration??2,this.singleFilePerPlaylist=r._options.singleFilePerPlaylist??!1,this.isLive=r._options.live??!1,this.maxLiveSegmentCount=r._options.maxLiveSegmentCount??1/0,this.globalTargetDuration=this.targetSegmentDuration,this.getPlaylistPath=r._options.getPlaylistPath??(({n:i})=>`playlist-${i}.m3u8`),this.getSegmentPath=r._options.getSegmentPath??(i=>i.isSingleFile?`segments-${i.playlist.n}${i.format.fileExtension}`:`segment-${i.playlist.n}-${i.n}${i.format.fileExtension}`),this.getInitPath=r._options.getInitPath??(i=>`init-${i.n}${i.segmentFormat.fileExtension}`)}async start(){let e=await this.mutex.acquire(),r=this.output._tracks.some(y=>y.metadata.isRelativeToUnixEpoch),i=this.output._tracks.some(y=>!y.metadata.isRelativeToUnixEpoch);if(r&&i)throw new Error("All tracks must agree on `relativeToUnixEpoch`: some tracks are relative to the Unix epoch and some are not.");this.isRelativeToUnixEpoch=r;let a=new Map,s=[],o=!1,c=!1,u=!1;for(let y of this.output._tracks){y.type==="video"&&(o=!0);let k=new Map;for(let T of this.output._tracks){if(y===T||!y.canBePairedWith(T))continue;if(y.type===T.type){c||(console.warn(`Illegal pairing of two ${y.type} tracks detected, which is not possible in HLS; treating them as unpaired.`),c=!0);continue}if(y.isVideoTrack()&&y.metadata.hasOnlyKeyPackets||T.isVideoTrack()&&T.metadata.hasOnlyKeyPackets){u||(console.warn("A key-packets-only video track is pairable with another track, which is not possible in HLS; treating them as unpaired."),u=!0);continue}let w=k.get(T.source._codec);w||k.set(T.source._codec,w=[]),w.push(T)}for(let[,T]of k){let w=T.map(P=>P.id).join("-");s.find(P=>P.key===w)||s.push({name:T[0].type+"-"+(s.length+1),key:w,tracks:T,needsEmit:!1,firstNoUri:!1});let x=a.get(y);x||a.set(y,x=[]),x.push(w)}}let l=o?"video":"audio",m=[],d=[],f=[];for(let y of this.output._tracks){let k=a.get(y);if(k){if(h(k.length>0),y.type!==l)continue;for(let T of k){let w=s.find(S=>S.key===T);if(h(w),k.length===1&&w.tracks.length===1){let S=a.get(w.tracks[0]);if(h(S!==void 0),S.length===1){let x=s.find(P=>P.key===S[0]);if(x.tracks.length===1){h(x.tracks[0]===y),m.push({tracks:[y,w.tracks[0]],linkedGroup:null});continue}}}m.push({tracks:[y],linkedGroup:w}),w.needsEmit=!0}}else y.type==="video"?d.push(y):y.type==="audio"&&f.push(y)}let p=({metadata:y})=>{let k="";return k+=`${y.languageCode??J}-`,k+=`${y.name??""}-`,k+=`${y.disposition?.default??!0}-`,k+=`${y.disposition?.primary??!1}-`,k+=`${y.disposition?.forced??!1}-`,k};if(d.length>0)if(new Set(d.map(p)).size>1){let k={key:d.map(T=>T.id).join("-"),name:"video-"+(s.length+1),tracks:d,needsEmit:!0,firstNoUri:!0};s.push(k),m.push({tracks:[d[0]],linkedGroup:k})}else for(let k of d)m.push({tracks:[k],linkedGroup:null});if(f.length>0)if(new Set(f.map(p)).size>1){let k={key:f.map(T=>T.id).join("-"),name:"audio-"+(s.length+1),tracks:f,needsEmit:!0,firstNoUri:!0};s.push(k),m.push({tracks:[f[0]],linkedGroup:k})}else for(let k of f)m.push({tracks:[k],linkedGroup:null});let b=y=>{let k=[],T=0,w=0,S=!1,x=null,P=-1/0;for(let A of y)A.isVideoTrack()?(T++,S||=(A.metadata.rotation??0)!==0):A.isAudioTrack()&&w++,k.push(A.source._codec);for(let A of Dt(this.format._options.segmentFormat)){let C=A.getSupportedCodecs(),I=A.getSupportedTrackCounts();if(k.some(v=>!C.includes(v))||T<I.video.min||T>I.video.max||w<I.audio.min||w>I.audio.max)continue;let R=0;S&&A.supportsVideoRotationMetadata&&R++,R>P&&(x=A,P=R)}return h(x),x},g=async y=>{if(y.some(x=>this.playlists.some(P=>P.tracks.includes(x))))throw new Error("Internal error: track is already registered in a playlist.");let k=b(y),T=this.playlists.length+1,w=await this.getPlaylistPath({n:T,tracks:y,segmentFormat:k});Vp(w);let S={id:this.playlists.length+1,path:w,tracks:y,segmentFormat:k,currentSegmentStartTimestamp:null,currentSegmentStartTimestampIsFixed:!1,nextSegmentId:1,initSegment:null,writtenSegments:[],peakBitrate:null,averageBitrate:null,mediaSequence:0,done:!1,singleFile:null,mutex:new Ie};return this.playlists.push(S),S};for(let y of s)if(y.needsEmit)for(let k=0;k<y.tracks.length;k++){let T=y.tracks[k],w=this.playlists.find(S=>S.tracks[0].id===T.id);w??=await g([T]),this.playlistDeclarations.push({playlist:w,groupId:y.name,noUri:y.firstNoUri&&k===0,references:[]})}for(let y of m){let k=this.playlists.find(T=>T.tracks[0].id===y.tracks[0].id);k??=await g(y.tracks),this.playlistDeclarations.push({playlist:k,groupId:null,noUri:!1,references:y.linkedGroup?this.playlistDeclarations.filter(T=>T.groupId===y.linkedGroup.name):[]})}e()}async getMimeType(){return St}allTracksAreKnown(e){for(let r of e.tracks)if(!r.source._closed&&!this.trackDatas.some(i=>i.track===r))return!1;return!0}async onTrackClose(e){let r=this.trackDatas.find(s=>s.track===e);r&&(r.closed=!0);let i=this.playlists.find(s=>s.tracks.includes(e));h(i);let a=await i.mutex.acquire();try{await this.advancePlaylist(i)}finally{a()}}getVideoTrackData(e,r){let i=this.trackDatas.find(s=>s.track===e);if(i)return i;sr(r),h(r),h(r?.decoderConfig);let a=this.playlists.filter(s=>s.tracks.includes(e));return h(a.length===1),i={track:e,packets:[],playlist:a[0],closed:!1,info:{type:"video",decoderConfig:r.decoderConfig}},this.trackDatas.push(i),i}getAudioTrackData(e,r){let i=this.trackDatas.find(s=>s.track===e);if(i)return i;Ee(r),h(r),h(r?.decoderConfig);let a=this.playlists.filter(s=>s.tracks.includes(e));return h(a.length===1),i={track:e,packets:[],playlist:a[0],closed:!1,info:{type:"audio",decoderConfig:r.decoderConfig}},this.trackDatas.push(i),i}async addEncodedVideoPacket(e,r,i){let a=this.getVideoTrackData(e,i),s=a.playlist,o=await s.mutex.acquire();try{this.validateTimestamp(e,r.timestamp,r.type==="key"),a.packets.push(r),s.currentSegmentStartTimestamp===null?s.currentSegmentStartTimestamp=r.timestamp:s.currentSegmentStartTimestampIsFixed||(s.currentSegmentStartTimestamp=Math.min(s.currentSegmentStartTimestamp,r.timestamp)),await this.advancePlaylist(s)}finally{o()}}async addEncodedAudioPacket(e,r,i){let a=this.getAudioTrackData(e,i),s=a.playlist,o=await s.mutex.acquire();try{this.validateTimestamp(e,r.timestamp,r.type==="key"),a.packets.push(r),s.currentSegmentStartTimestamp===null?s.currentSegmentStartTimestamp=r.timestamp:s.currentSegmentStartTimestampIsFixed||(s.currentSegmentStartTimestamp=Math.min(s.currentSegmentStartTimestamp,r.timestamp)),await this.advancePlaylist(s)}finally{o()}}async addSubtitleCue(e,r,i){throw new Error("Unreachable.")}async advancePlaylist(e){if(h(!e.done),!this.allTracksAreKnown(e))return;if(e.currentSegmentStartTimestamp===null){await this.onPlaylistDone(e);return}let r=this.trackDatas.filter(s=>e.tracks.includes(s.track)),i=r.find(s=>s.info.type==="video"),a=r.find(s=>s.info.type==="audio");for(;;){let s=e.currentSegmentStartTimestamp+this.targetSegmentDuration,o=0,c=0;if(i&&(!i.closed||i.packets.length>0)){let w=i.packets.every(P=>P.timestamp<s),S=null,x=null;if(w){if(!i.closed)return}else for(let P=0;P<i.packets.length;P++){let A=i.packets[P];if(S!==null&&A.timestamp>s)break;P>0&&A.type==="key"&&(S=A,x=P)}if(x!==null){if(o=x,a){let P=a.packets.findIndex(A=>A.timestamp>=S.timestamp);if(P!==-1)c=P;else if(a.closed)c=a.packets.length;else return}}else{if(!i.closed)return;o=i.packets.length;let P=Cu(i.packets,C=>C.timestamp),A=i.packets[P];if(h(A),a)if(A.timestamp<s){let C=a.packets.findIndex(I=>I.timestamp>=s);if(C!==-1)c=C;else if(a.closed)c=a.packets.length;else return}else{let C=a.packets.findIndex(I=>I.timestamp>A.timestamp);if(C!==-1)c=C;else if(a.closed)c=a.packets.length;else return}}}else if(a&&(!a.closed||a.packets.length>0))if(a.packets.every(S=>S.timestamp<s))if(a.closed)c=a.packets.length;else return;else{let S=rr(a.packets,x=>x.timestamp<=s);c=Math.max(S,1)}if(o===0&&c===0){r.every(S=>S.closed)&&await this.onPlaylistDone(e);return}let u=null,l,m;h(this.output._target instanceof Le);let d=this.output._target;if(this.singleFilePerPlaylist)if(e.singleFile===null){let w={n:e.nextSegmentId,format:e.segmentFormat,isSingleFile:!0,playlist:js(e)};l=await this.getSegmentPath(w),Gd(l),m=fe(fe(d.rootPath,e.path),l);let S=await this.output._getTarget({path:m,isRoot:!1,mimeType:e.segmentFormat.mimeType});S._start(),e.singleFile={target:S,path:l,nextOffset:0,info:w}}else l=e.singleFile.path,m=fe(fe(d.rootPath,e.path),l);else u={n:e.nextSegmentId,format:e.segmentFormat,isSingleFile:!1,playlist:js(e)},l=await this.getSegmentPath(u),Gd(l),m=fe(fe(d.rootPath,e.path),l),e.nextSegmentId++;let f=0,p=null,b=new Sr({format:e.segmentFormat,target:new Le(m,async w=>{let S={...w,isRoot:!1};if(w.isRoot)if(e.singleFile){let x=e.singleFile.target.slice(e.singleFile.nextOffset);return x.on("write",({end:P})=>f=Math.max(f,P)),x}else{let x=await this.output._getTarget(S);return p=x,x.on("write",({end:P})=>f=Math.max(f,P)),x}return this.output._getTarget(S)}),initTarget:async()=>{if(e.initSegment)return new Xr;if(e.singleFile){e.initSegment={path:e.singleFile.path,duration:0,timestamp:0,byteSize:0,byteOffset:0,info:null};let w=e.singleFile.target.slice(e.singleFile.nextOffset);return w.on("write",({end:S})=>{e.initSegment.byteSize=Math.max(e.initSegment.byteSize,S)}),w.on("finalized",()=>{e.singleFile.nextOffset=e.initSegment.byteSize}),w}else{let w=js(e),S=await this.getInitPath(w);Up(S),e.initSegment={path:S,duration:0,timestamp:0,byteSize:0,byteOffset:null,info:null};let x=fe(fe(d.rootPath,e.path),S),P=await this.output._getTarget({path:x,isRoot:!1,mimeType:e.segmentFormat.mimeType});return P.on("write",({end:A})=>{e.initSegment.byteSize=Math.max(e.initSegment.byteSize,A)}),P.on("finalized",()=>{this.format._options.onInit?.(P,w)}),P}}}),g=-1/0;try{let w=null,S=null;if(i&&(w=new Yr(i.track.source._codec),b.addVideoTrack(w,i.track.metadata)),a&&(S=new Zr(a.track.source._codec),b.addAudioTrack(S,a.track.metadata)),await b.start(),i){h(w);let x={decoderConfig:i.info.decoderConfig};for(let P=0;P<o;P++){let A=i.packets[P];await w.add(A,x),g=Math.max(g,A.timestamp+A.duration)}}if(a){h(S);let x={decoderConfig:a.info.decoderConfig};for(let P=0;P<c;P++){let A=a.packets[P];await S.add(A,x),g=Math.max(g,A.timestamp+A.duration)}}await b.finalize()}catch(w){throw await b.cancel(),w}u&&(h(p),this.format._options.onSegment?.(p,u)),o>0&&(h(i),i.packets.splice(0,o)),c>0&&(h(a),a.packets.splice(0,c));let y=1/0;i&&i.packets.length>0&&(y=i.packets[0].timestamp),a&&a.packets.length>0&&(y=Math.min(y,a.packets[0].timestamp));let k=y<1/0?y:g;h(Number.isFinite(k));let T=k-e.currentSegmentStartTimestamp;if(h(T>=0),e.writtenSegments.push({path:l,duration:T,timestamp:e.currentSegmentStartTimestamp,byteSize:f,byteOffset:e.singleFile?e.singleFile.nextOffset:null,info:u??null}),this.globalTargetDuration=Math.max(this.globalTargetDuration,T),e.currentSegmentStartTimestamp=k,e.currentSegmentStartTimestampIsFixed=!0,e.singleFile&&(e.singleFile.nextOffset+=f),this.isLive){for(;e.writtenSegments.length>this.maxLiveSegmentCount;){let w=e.writtenSegments.shift();e.mediaSequence++,this.singleFilePerPlaylist||(h(w.info),this.format._options.onSegmentPopped?.(w.path,w.info))}await this.writePlaylist(e),await this.tryWriteMasterPlaylist()}}}async onPlaylistDone(e){h(!e.done),e.done=!0,e.singleFile&&(await e.singleFile.target._flush(),await e.singleFile.target._finalize(),this.format._options.onSegment?.(e.singleFile.target,e.singleFile.info)),await this.writePlaylist(e),this.isLive&&e.writtenSegments.length===0&&await this.tryWriteMasterPlaylist()}updatePlaylistBitrates(e){let r=e.writtenSegments,i=0,a=0,s=0;for(let o=0;o<r.length;o++){s+=r[o].duration;let c=0,u=0;for(let l=o;l<r.length&&(c+=r[l].byteSize,u+=r[l].duration,u>=.5*this.globalTargetDuration&&u<=1.5*this.globalTargetDuration&&(i=Math.max(i,8*c/u)),!(u>1.5*this.globalTargetDuration));l++);}if(i===0)for(let o of r){let c=o.duration||1;i=Math.max(i,8*o.byteSize/c)}for(let o of r)a+=8*o.byteSize;e.peakBitrate=i,e.averageBitrate=a/(s||1)}async writePlaylist(e){h(this.output._target instanceof Le);let r=this.output._target;this.updatePlaylistBitrates(e);let i=!1;for(let d of e.writtenSegments)i||=d.byteOffset!==null;let a=e.tracks[0].isVideoTrack()&&e.tracks[0].metadata.hasOnlyKeyPackets,s=3;(a||i)&&(s=4),e.initSegment&&(s=5),e.initSegment&&!a&&(s=6);let o=this.isLive?this.targetSegmentDuration:this.globalTargetDuration,c=fe(r.rootPath,e.path),u=`#EXTM3U
103
+ `),r=this.gl.createProgram();return this.gl.attachShader(r,t),this.gl.attachShader(r,e),this.gl.linkProgram(r),r}createShader(t,e){h(this.gl);let r=this.gl.createShader(t);return this.gl.shaderSource(r,e),this.gl.compileShader(r),this.gl.getShaderParameter(r,this.gl.COMPILE_STATUS)||console.error("Shader compile error:",this.gl.getShaderInfoLog(r)),r}createVAO(){h(this.gl),h(this.colorProgram);let t=this.gl.createVertexArray();this.gl.bindVertexArray(t);let e=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),r=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,r),this.gl.bufferData(this.gl.ARRAY_BUFFER,e,this.gl.STATIC_DRAW);let i=this.gl.getAttribLocation(this.colorProgram,"a_position"),a=this.gl.getAttribLocation(this.colorProgram,"a_texCoord");return this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,16,0),this.gl.enableVertexAttribArray(a),this.gl.vertexAttribPointer(a,2,this.gl.FLOAT,!1,16,8),t}createTexture(){h(this.gl);let t=this.gl.createTexture();return this.gl.bindTexture(this.gl.TEXTURE_2D,t),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),t}runColorProgram(t){return h(this.gl),h(this.canvas),this.gl.useProgram(this.colorProgram),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4),new VideoFrame(this.canvas,{timestamp:t.timestamp,duration:t.duration??void 0,alpha:"discard"})}runAlphaProgram(t){h(this.gl),h(this.canvas),this.gl.useProgram(this.alphaProgram),this.gl.uniform2f(this.alphaResolutionLocation,this.canvas.width,this.canvas.height),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4);let{width:e,height:r}=this.canvas,i=Math.ceil(e/2)*Math.ceil(r/2),a=e*r+i*2,s=Math.ceil(a/(e*4)),o=new Uint8Array(4*e*s);this.gl.readPixels(0,0,e,s,this.gl.RGBA,this.gl.UNSIGNED_BYTE,o),o=o.subarray(0,a),h(o[e*r]===128),h(o[o.length-1]===128);let c={format:"I420",codedWidth:e,codedHeight:r,timestamp:t.timestamp,duration:t.duration??void 0,transfer:[o.buffer]};return new VideoFrame(o,c)}updateCpu(t){if(!this.worker){let i=new Blob([`(${Fp.toString()})()`],{type:"application/javascript"}),a=URL.createObjectURL(i);this.worker=new Worker(a),URL.revokeObjectURL(a),this.worker.addEventListener("message",s=>{let o=s.data,c=this.pendingRequests.get(o.id);c&&(this.pendingRequests.delete(o.id),"error"in o?c.reject(new Error(o.error)):c.resolve({colorFrame:o.colorFrame,alphaFrame:o.alphaFrame}))}),this.worker.addEventListener("error",s=>{let o=new Error(s.message||"Color/alpha splitter worker error.");for(let c of this.pendingRequests.values())c.reject(o);this.pendingRequests.clear()})}let e=this.nextRequestId++,r=Z();return this.pendingRequests.set(e,r),this.worker.postMessage({id:e,sourceFrame:t},{transfer:[t]}),r.promise}close(){this.gl?.getExtension("WEBGL_lose_context")?.loseContext(),this.gl=null,this.canvas=null,this.worker?.terminate(),this.worker=null;let t=new Error("Color/alpha splitter closed.");for(let e of this.pendingRequests.values())e.reject(t);this.pendingRequests.clear()}};Qs.forceCpu=!0;var Lc=Qs,Fp=()=>{let n=null,t=Promise.resolve();self.addEventListener("message",a=>{let{id:s,sourceFrame:o}=a.data;t=t.then(async()=>{try{let{colorFrame:c,alphaFrame:u}=await e(o);self.postMessage({id:s,colorFrame:c,alphaFrame:u},{transfer:[c,u]})}catch(c){self.postMessage({id:s,error:c.message})}finally{o.close()}})});let e=async a=>{let s=a.format;if(!s)throw new Error("CPU color/alpha splitting requires a known VideoFrame format.");let o=a.codedWidth,c=a.codedHeight,u=a.allocationSize();if((!n||n.byteLength!==u)&&(n=new Uint8Array(u)),await a.copyTo(n),s==="RGBA"||s==="BGRA")return r(n,o,c,s,a);if(s==="I420A"||s==="I420AP10"||s==="I420AP12"||s==="I422A"||s==="I422AP10"||s==="I422AP12"||s==="I444A"||s==="I444AP10"||s==="I444AP12")return i(n,o,c,s,a);throw new Error(`CPU color/alpha splitting does not support format '${s}'.`)},r=(a,s,o,c,u)=>{let l=s*o,m=Math.ceil(s/2),d=Math.ceil(o/2),f=l+m*d*2,p=new Uint8Array(f);for(let k=0,w=3;k<l;k++,w+=4)p[k]=a[w];p.fill(128,l);let b=new VideoFrame(a,{format:c==="RGBA"?"RGBX":"BGRX",codedWidth:s,codedHeight:o,timestamp:u.timestamp,duration:u.duration??void 0}),g={format:"I420",codedWidth:s,codedHeight:o,timestamp:u.timestamp,duration:u.duration??void 0,transfer:[p.buffer]},y=new VideoFrame(p,g);return{colorFrame:b,alphaFrame:y}},i=(a,s,o,c,u)=>{let l=c.includes("P10"),m=c.includes("P12"),d=l||m?2:1,f,p;c.startsWith("I420")?(f=Math.ceil(s/2),p=Math.ceil(o/2)):c.startsWith("I422")?(f=Math.ceil(s/2),p=o):(f=s,p=o);let b=s*o,g=f*p,y=b*d,k=g*d,w=b*d,T=y+k*2,S=c.replace("A",""),x=Math.ceil(s/2),P=Math.ceil(o/2),A=x*P,C=A*d,I=w+2*C,R=new Uint8Array(I),v=T;R.set(a.subarray(v,v+w),0);let E=w,D=l?512:m?2048:128;d===1?R.fill(D,E):new Uint16Array(R.buffer,E,2*A).fill(D);let U=l?"I420P10":m?"I420P12":"I420",z=new VideoFrame(a.subarray(0,T),{format:S,codedWidth:s,codedHeight:o,timestamp:u.timestamp,duration:u.duration??void 0}),q={format:U,codedWidth:s,codedHeight:o,timestamp:u.timestamp,duration:u.duration??void 0,transfer:[R.buffer]},ke=new VideoFrame(R,q);return{colorFrame:z,alphaFrame:ke}}},Ui=class extends Jt{constructor(t){ks(t),super(t.codec),this._encoder=new On(this,t)}add(t,e){if(!(t instanceof Ae))throw new TypeError("videoSample must be a VideoSample.");return this._encoder.add(t,!1,e)}_flushAndClose(t){return this._encoder.flushAndClose(t)}},Wc=class extends Jt{constructor(t,e){if(!(typeof HTMLCanvasElement<"u"&&t instanceof HTMLCanvasElement)&&!(typeof OffscreenCanvas<"u"&&t instanceof OffscreenCanvas))throw new TypeError("canvas must be an HTMLCanvasElement or OffscreenCanvas.");ks(e),super(e.codec),this._encoder=new On(this,e),this._canvas=t}add(t,e=0,r){if(!Number.isFinite(t)||t<0)throw new TypeError("timestamp must be a non-negative number.");if(!Number.isFinite(e)||e<0)throw new TypeError("duration must be a non-negative number.");let i=new Ae(this._canvas,{timestamp:t,duration:e});return this._encoder.add(i,!0,r)}_flushAndClose(t){return this._encoder.flushAndClose(t)}},Hc=class extends Jt{constructor(e,r,i={}){if(!(e instanceof MediaStreamTrack)||e.kind!=="video")throw new TypeError("track must be a video MediaStreamTrack.");if(ks(r),typeof i!="object"||!i)throw new TypeError("options must be an object.");if(i.frameRate!=null&&(typeof i.frameRate!="number"||i.frameRate<=0))throw new TypeError("options.frameRate, when provided, must be either a positive number or null.");if(i.timestampBase!==void 0&&i.timestampBase!=="synced-zero"&&i.timestampBase!=="zero"&&i.timestampBase!=="unix")throw new TypeError("options.timestampBase, when provided, must be one of 'synced-zero', 'zero', or 'unix'.");r={...r,latencyMode:"realtime"};super(r.codec);this._abortController=null;this._workerTrackId=null;this._workerListener=null;this._promiseWithResolvers=Z();this._errorPromiseAccessed=!1;this._paused=!1;this._lastVideoFrame=null;this._timerHandle=null;this._videoElement=null;this._options=i,this._encoder=new On(this,r),this._track=e}get errorPromise(){return this._errorPromiseAccessed=!0,this._promiseWithResolvers.promise}get paused(){return this._paused}async _start(){this._errorPromiseAccessed||console.warn("Make sure not to ignore the `errorPromise` field on MediaStreamVideoTrackSource, so that any internal errors get bubbled up properly.");let e=this._options.frameRate!==void 0?this._options.frameRate:this._track.getSettings().frameRate??null;this._abortController=new AbortController;let r=null,i=null,a=0,s=!1,o=null,c=0,u=()=>{if(h(e!==null),!this._lastVideoFrame)return;h(i!==null),h(r!==null);let d=performance.now();for(;d-i>1e3/e;){i+=1e3/e;let f=r+a/e,p=new VideoFrame(this._videoElement??this._lastVideoFrame,{timestamp:1e6*f,duration:1e6/e});m(p,d)}};e!==null&&(this._timerHandle=vu(u,4));let l=d=>{if(e===null)m(d);else{let f=performance.now();this._lastVideoFrame?(u(),this._lastVideoFrame?.close(),this._lastVideoFrame=d):(m(d.clone(),f),i=f,this._lastVideoFrame=d)}},m=(d,f=performance.now())=>{if(s){d.close();return}a++;let p=d.timestamp/1e6;if(this._paused){if(r!==null){if(o!==null&&this._options.timestampBase!=="unix"){let y=p-o;c-=y}o=p}d.close();return}if(r===null){r=p;let g,y=this._options.timestampBase??"synced-zero";if(y==="unix")g=Date.now()/1e3;else if(y==="zero")g=0;else{let k=this._connectedTrack.output;k._firstMediaStreamTimestamp===null?(k._firstMediaStreamTimestamp=f/1e3,g=0):g=f/1e3-k._firstMediaStreamTimestamp}c=g-r}if(o=p,this._encoder.getQueueSize()>=8){d.close();return}let b=new Ae(d,{timestamp:p+c});this._encoder.add(b,!0).catch(g=>{s=!0,this._abortController?.abort(),this._promiseWithResolvers.reject(g),this._workerTrackId!==null&&zc({type:"stopTrack",trackId:this._workerTrackId})})};if(typeof MediaStreamTrackProcessor<"u"){let d=new MediaStreamTrackProcessor({track:this._track}),f=new WritableStream({write:l});d.readable.pipeTo(f,{signal:this._abortController.signal}).catch(p=>{p instanceof DOMException&&p.name==="AbortError"||this._promiseWithResolvers.reject(p)})}else if(await Dp())this._workerTrackId=Bp++,zc({type:"videoTrack",trackId:this._workerTrackId,track:this._track}),this._workerListener=f=>{let p=f.data;p.type==="videoFrame"&&p.trackId===this._workerTrackId?l(p.videoFrame):p.type==="error"&&p.trackId===this._workerTrackId&&this._promiseWithResolvers.reject(p.error)},$e.addEventListener("message",this._workerListener);else if(e!==null){let f=document.createElement("video");f.style.position="fixed",f.style.left="-10000px",f.style.top="-10000px",f.style.width="1px",f.style.height="1px",f.style.opacity="0",f.style.pointerEvents="none",f.muted=!0,f.srcObject=new MediaStream([this._track]),document.body.appendChild(f),this._videoElement=f,f.addEventListener("loadeddata",()=>{if(s||!this._videoElement)return;let p=new VideoFrame(f,{timestamp:1e3*performance.now()});l(p),p.close()},{once:!0}),f.play().catch(p=>{s=!0,this._promiseWithResolvers.reject(p)})}else throw new Error("When no explicit frame rate is set, MediaStreamTrackProcessor is required; but it's not supported by this browser.")}pause(){this._paused=!0}resume(){this._paused=!1}async _flushAndClose(e){this._abortController&&(this._abortController.abort(),this._abortController=null),this._timerHandle&&_u(this._timerHandle),this._lastVideoFrame?.close(),this._videoElement&&(this._videoElement.srcObject=null,this._videoElement.remove(),this._videoElement=null),this._workerTrackId!==null&&(h(this._workerListener),zc({type:"stopTrack",trackId:this._workerTrackId}),await new Promise(r=>{let i=a=>{let s=a.data;s.type==="trackStopped"&&s.trackId===this._workerTrackId&&(h(this._workerListener),$e.removeEventListener("message",this._workerListener),$e.removeEventListener("message",i),r())};$e.addEventListener("message",i)})),await this._encoder.flushAndClose(e)}},er=class extends Vi{constructor(e){super();this._connectedTrack=null;if(!ae.includes(e))throw new TypeError(`Invalid audio codec '${e}'. Must be one of: ${ae.join(", ")}.`);this._codec=e}},Jr=class extends er{constructor(t){super(t)}add(t,e){if(!(t instanceof Q))throw new TypeError("packet must be an EncodedPacket.");if(t.isMetadataOnly)throw new TypeError("Metadata-only packets cannot be added.");if(e!==void 0&&(!e||typeof e!="object"))throw new TypeError("meta, when provided, must be an object.");return this._ensureValidAdd(),this._connectedTrack.output._muxer.addEncodedAudioPacket(this._connectedTrack,t,e)}},Dn=class{constructor(t,e){this.source=t;this.encodingConfig=e;this.ensureEncoderPromise=null;this.encoderInitialized=!1;this.encoder=null;this.muxer=null;this.lastNumberOfChannels=null;this.lastSampleRate=null;this.isPcmEncoder=!1;this.outputSampleSize=null;this.writeOutputValue=null;this.customEncoder=null;this.customEncoderCallSerializer=new vt;this.customEncoderQueueSize=0;this.lastEndSampleIndex=null;this.resampler=null;this.error=null;this.lastMuxerPromise=Promise.resolve()}async add(t,e){try{if(this.checkForEncoderError(),this.source._ensureValidAdd(),this.lastNumberOfChannels!==null&&this.lastSampleRate!==null){if(t.numberOfChannels!==this.lastNumberOfChannels||t.sampleRate!==this.lastSampleRate)throw new Error(`Audio parameters must remain constant. Expected ${this.lastNumberOfChannels} channels at ${this.lastSampleRate} Hz, got ${t.numberOfChannels} channels at ${t.sampleRate} Hz.`)}else this.lastNumberOfChannels=t.numberOfChannels,this.lastSampleRate=t.sampleRate;let r=this.encodingConfig;r.transform?.numberOfChannels!==void 0||r.transform?.sampleRate!==void 0?(this.resampler||(this.resampler=new Di({targetNumberOfChannels:r.transform.numberOfChannels??t.numberOfChannels,targetSampleRate:r.transform.sampleRate??t.sampleRate,startTime:t.timestamp,endTime:1/0,onSample:async a=>{await this.processAndEncode(a,!0)}})),await this.resampler.add(t)):await this.processAndEncode(t,e)}finally{e&&t.close()}}async processAndEncode(t,e){let r=this.encodingConfig;if(r.transform?.sampleFormat!==void 0&&hs(t.format)!==r.transform.sampleFormat){let i=gs(t,r.transform.sampleFormat);e&&t.close(),t=i,e=!0}if(r.transform?.process){let i=r.transform.process(t);if(i instanceof Promise&&(i=await i),i===null)return;Array.isArray(i)||(i=[i]);for(let a of i){if(!(a instanceof ye))throw new TypeError("The audio process function must return an AudioSample, null, or an array of AudioSamples.");await this.encodeSample(a,!0)}e&&t.close()}else await this.encodeSample(t,e)}async encodeSample(t,e){try{this.encoderInitialized||(this.ensureEncoderPromise||this.ensureEncoder(t),this.encoderInitialized||await this.ensureEncoderPromise),h(this.encoderInitialized);{let r=Math.round(t.timestamp*t.sampleRate),i=Math.round((t.timestamp+t.duration)*t.sampleRate);if(this.lastEndSampleIndex===null)this.lastEndSampleIndex=i;else{let a=r-this.lastEndSampleIndex;if(a>=64){let s=new ye({data:new Float32Array(a*t.numberOfChannels),format:"f32-planar",sampleRate:t.sampleRate,numberOfChannels:t.numberOfChannels,numberOfFrames:a,timestamp:this.lastEndSampleIndex/t.sampleRate});await this.encodeSample(s,!0)}this.lastEndSampleIndex+=t.numberOfFrames}}if(this.customEncoder){this.customEncoderQueueSize++;let r=t.clone(),i=this.customEncoderCallSerializer.call(()=>this.customEncoder.encode(r)).then(()=>this.customEncoderQueueSize--).catch(a=>this.error??=a).finally(()=>{r.close()});this.customEncoderQueueSize>=4&&await i,await this.lastMuxerPromise}else if(this.isPcmEncoder)await this.doPcmEncoding(t,e);else{h(this.encoder);let r=t.toAudioData();this.encoder.encode(r),r.close(),e&&t.close(),this.encoder.encodeQueueSize>=4&&await new Promise(i=>this.encoder.addEventListener("dequeue",i,{once:!0})),await this.lastMuxerPromise}}finally{e&&t.close()}}async doPcmEncoding(t,e){h(this.outputSampleSize),h(this.writeOutputValue);let{numberOfChannels:r,numberOfFrames:i,sampleRate:a,timestamp:s}=t,o=2048,c=[];for(let d=0;d<i;d+=o){let f=Math.min(o,t.numberOfFrames-d),p=f*r*this.outputSampleSize,b=new ArrayBuffer(p),g=new DataView(b);c.push({frameCount:f,view:g})}let u=t.allocationSize({planeIndex:0,format:"f32-planar"}),l=new Float32Array(u/Float32Array.BYTES_PER_ELEMENT);for(let d=0;d<r;d++){t.copyTo(l,{planeIndex:d,format:"f32-planar"});for(let f=0;f<c.length;f++){let{frameCount:p,view:b}=c[f];for(let g=0;g<p;g++)this.writeOutputValue(b,(g*r+d)*this.outputSampleSize,l[f*o+g])}}e&&t.close();let m={decoderConfig:{codec:this.encodingConfig.codec,numberOfChannels:r,sampleRate:a}};for(let d=0;d<c.length;d++){let{frameCount:f,view:p}=c[d],b=p.buffer,g=d*o,y=new Q(new Uint8Array(b),"key",s+g/a,f/a);this.encodingConfig.onEncodedPacket?.(y,m),await this.muxer.addEncodedAudioPacket(this.source._connectedTrack,y,m)}}ensureEncoder(t){this.ensureEncoderPromise=(async()=>{let{numberOfChannels:e,sampleRate:r}=t,i=pc({numberOfChannels:e,sampleRate:r,...this.encodingConfig});this.encodingConfig.onEncoderConfig?.(i);let a=Ii.find(s=>s.supports(this.encodingConfig.codec,i));if(a)this.customEncoder=new a,this.customEncoder.codec=this.encodingConfig.codec,this.customEncoder.config=i,this.customEncoder.onPacket=(s,o)=>{if(!(s instanceof Q))throw new TypeError("The first argument passed to onPacket must be an EncodedPacket.");if(o!==void 0&&(!o||typeof o!="object"))throw new TypeError("The second argument passed to onPacket must be an object or undefined.");this.encodingConfig.onEncodedPacket?.(s,o),this.lastMuxerPromise=this.muxer.addEncodedAudioPacket(this.source._connectedTrack,s,o).catch(c=>{this.error??=c})},await this.customEncoder.init();else if(ee.includes(this.encodingConfig.codec))this.initPcmEncoder();else{if(typeof AudioEncoder>"u")throw new Error("AudioEncoder is not supported by this browser.");if(!(await AudioEncoder.isConfigSupported(i)).supported)throw new Error(`This specific encoder configuration (${i.codec}, ${i.bitrate} bps, ${i.numberOfChannels} channels, ${i.sampleRate} Hz) is not supported by this browser. Consider using another codec or changing your audio parameters.`);let o=new Error("Encoding error").stack;this.encoder=new AudioEncoder({output:(c,u)=>{if(this.encodingConfig.codec==="aac"&&u?.decoderConfig){let m=!1;if(!u.decoderConfig.description||u.decoderConfig.description.byteLength<2?m=!0:m=Tt(j(u.decoderConfig.description)).objectType===0,m){let d=Number(X(i.codec.split(".")));u.decoderConfig.description=ri({objectType:d,numberOfChannels:u.decoderConfig.numberOfChannels,sampleRate:u.decoderConfig.sampleRate})}}let l=Q.fromEncodedChunk(c);l=l.clone({timestamp:bt(l.timestamp,i.sampleRate),duration:c.duration!=null?bt(l.duration,i.sampleRate):void 0}),this.encodingConfig.onEncodedPacket?.(l,u),this.lastMuxerPromise=this.muxer.addEncodedAudioPacket(this.source._connectedTrack,l,u).catch(m=>{this.error??=m})},error:c=>{c.stack=o,this.error??=c}}),this.encoder.configure(i)}h(this.source._connectedTrack),this.muxer=this.source._connectedTrack.output._muxer,this.encoderInitialized=!0})()}initPcmEncoder(){this.isPcmEncoder=!0;let t=this.encodingConfig.codec,{dataType:e,sampleSize:r,littleEndian:i}=Se(t);switch(this.outputSampleSize=r,r){case 1:e==="unsigned"?this.writeOutputValue=(a,s,o)=>a.setUint8(s,ue((o+1)*127.5,0,255)):e==="signed"?this.writeOutputValue=(a,s,o)=>{a.setInt8(s,ue(Math.round(o*128),-128,127))}:e==="ulaw"?this.writeOutputValue=(a,s,o)=>{let c=ue(Math.floor(o*32767),-32768,32767);a.setUint8(s,od(c))}:e==="alaw"?this.writeOutputValue=(a,s,o)=>{let c=ue(Math.floor(o*32767),-32768,32767);a.setUint8(s,ud(c))}:h(!1);break;case 2:e==="unsigned"?this.writeOutputValue=(a,s,o)=>a.setUint16(s,ue((o+1)*32767.5,0,65535),i):e==="signed"?this.writeOutputValue=(a,s,o)=>a.setInt16(s,ue(Math.round(o*32767),-32768,32767),i):h(!1);break;case 3:e==="unsigned"?this.writeOutputValue=(a,s,o)=>Er(a,s,ue((o+1)*83886075e-1,0,16777215),i):e==="signed"?this.writeOutputValue=(a,s,o)=>bu(a,s,ue(Math.round(o*8388607),-8388608,8388607),i):h(!1);break;case 4:e==="unsigned"?this.writeOutputValue=(a,s,o)=>a.setUint32(s,ue((o+1)*21474836475e-1,0,4294967295),i):e==="signed"?this.writeOutputValue=(a,s,o)=>a.setInt32(s,ue(Math.round(o*2147483647),-2147483648,2147483647),i):e==="float"?this.writeOutputValue=(a,s,o)=>a.setFloat32(s,o,i):h(!1);break;case 8:e==="float"?this.writeOutputValue=(a,s,o)=>a.setFloat64(s,o,i):h(!1);break;default:ne(r),h(!1)}}async flushAndClose(t){t||this.checkForEncoderError(),!t&&this.resampler&&await this.resampler.finalize(),this.resampler=null,this.customEncoder?(t||this.customEncoderCallSerializer.call(()=>this.customEncoder.flush()),await this.customEncoderCallSerializer.call(()=>this.customEncoder.close())):this.encoder&&(t||await this.encoder.flush(),this.encoder.state!=="closed"&&this.encoder.close()),t||this.checkForEncoderError()}getQueueSize(){return this.customEncoder?this.customEncoderQueueSize:this.isPcmEncoder?0:this.encoder?.encodeQueueSize??0}checkForEncoderError(){if(this.error)throw this.error}},zi=class extends er{constructor(t){Ts(t),super(t.codec),this._encoder=new Dn(this,t)}add(t){if(!(t instanceof ye))throw new TypeError("audioSample must be an AudioSample.");return this._encoder.add(t,!1)}_flushAndClose(t){return this._encoder.flushAndClose(t)}},qc=class extends er{constructor(e){Ts(e);super(e.codec);this._accumulatedTime=0;this._encoder=new Dn(this,e)}async add(e){if(!(e instanceof AudioBuffer))throw new TypeError("audioBuffer must be an AudioBuffer.");let r=ye._fromAudioBuffer(e,this._accumulatedTime);this._accumulatedTime+=e.duration;for(let i of r)await this._encoder.add(i,!0)}_flushAndClose(e){return this._encoder.flushAndClose(e)}},Gc=class extends er{constructor(e,r,i={}){if(!(e instanceof MediaStreamTrack)||e.kind!=="audio")throw new TypeError("track must be an audio MediaStreamTrack.");if(Ts(r),typeof i!="object"||!i)throw new TypeError("options must be an object.");if(i.timestampBase!==void 0&&i.timestampBase!=="synced-zero"&&i.timestampBase!=="zero"&&i.timestampBase!=="unix")throw new TypeError("options.timestampBase, when provided, must be one of 'synced-zero', 'zero', or 'unix'.");super(r.codec);this._abortController=null;this._audioContext=null;this._scriptProcessorNode=null;this._promiseWithResolvers=Z();this._errorPromiseAccessed=!1;this._paused=!1;this._options=i,this._encoder=new Dn(this,r),this._track=e}get errorPromise(){return this._errorPromiseAccessed=!0,this._promiseWithResolvers.promise}get paused(){return this._paused}async _start(){this._errorPromiseAccessed||console.warn("Make sure not to ignore the `errorPromise` field on MediaStreamAudioTrackSource, so that any internal errors get bubbled up properly."),this._abortController=new AbortController;let e=null,r=!1,i=null,a=0,s=o=>{if(r){o.close();return}let c=o.timestamp;if(this._paused){if(e!==null){if(i!==null&&this._options.timestampBase!=="unix"){let l=c-i;a-=l}i=c}o.close();return}if(e===null){e=o.timestamp;let u,l=this._options.timestampBase??"synced-zero";if(l==="unix")u=Date.now()/1e3;else if(l==="zero")u=0;else{let m=this._connectedTrack.output;m._firstMediaStreamTimestamp===null?(m._firstMediaStreamTimestamp=performance.now()/1e3,u=0):u=performance.now()/1e3-m._firstMediaStreamTimestamp}a=u-e}if(i=c,this._encoder.getQueueSize()>=8){o.close();return}o.setTimestamp(c+a),this._encoder.add(o,!0).catch(u=>{r=!0,this._abortController?.abort(),this._promiseWithResolvers.reject(u),this._audioContext?.suspend()})};if(typeof MediaStreamTrackProcessor<"u"){let o=new MediaStreamTrackProcessor({track:this._track}),c=new WritableStream({write:u=>s(new ye(u))});o.readable.pipeTo(c,{signal:this._abortController.signal}).catch(u=>{u instanceof DOMException&&u.name==="AbortError"||this._promiseWithResolvers.reject(u)})}else{let o=window.AudioContext||window.webkitAudioContext;this._audioContext=new o({sampleRate:this._track.getSettings().sampleRate});let c=this._audioContext.createMediaStreamSource(new MediaStream([this._track]));this._scriptProcessorNode=this._audioContext.createScriptProcessor(4096),this._audioContext.state==="suspended"&&await this._audioContext.resume(),c.connect(this._scriptProcessorNode),this._scriptProcessorNode.connect(this._audioContext.destination);let u=0;this._scriptProcessorNode.onaudioprocess=l=>{let m=ye._fromAudioBuffer(l.inputBuffer,u);u+=l.inputBuffer.duration;for(let d of m)s(d)}}}pause(){this._paused=!0}resume(){this._paused=!1}async _flushAndClose(e){this._abortController&&(this._abortController.abort(),this._abortController=null),this._audioContext&&(h(this._scriptProcessorNode),this._scriptProcessorNode.disconnect(),await this._audioContext.suspend()),await this._encoder.flushAndClose(e)}},Mp=()=>{let n=(r,i)=>{i?self.postMessage(r,{transfer:i}):self.postMessage(r)};n({type:"support",supported:typeof MediaStreamTrackProcessor<"u"});let t=new Map,e=new Map;self.addEventListener("message",r=>{let i=r.data;switch(i.type){case"videoTrack":{e.set(i.trackId,i.track);let a=new MediaStreamTrackProcessor({track:i.track}),s=new WritableStream({write:c=>{if(!e.has(i.trackId)){c.close();return}n({type:"videoFrame",trackId:i.trackId,videoFrame:c},[c])}}),o=new AbortController;t.set(i.trackId,o),a.readable.pipeTo(s,{signal:o.signal}).catch(c=>{c instanceof DOMException&&c.name==="AbortError"||n({type:"error",trackId:i.trackId,error:c})})}break;case"stopTrack":{let a=t.get(i.trackId);a&&(a.abort(),t.delete(i.trackId)),e.get(i.trackId)?.stop(),e.delete(i.trackId),n({type:"trackStopped",trackId:i.trackId})}break;default:ne(i)}})},Bp=0,$e=null,Op=()=>{let n=new Blob([`(${Mp.toString()})()`],{type:"application/javascript"}),t=URL.createObjectURL(n);$e=new Worker(t)},Uc=null,Dp=async()=>Uc!==null?Uc:($e||Op(),new Promise(n=>{h($e);let t=e=>{let r=e.data;r.type==="support"&&(Uc=r.supported,$e.removeEventListener("message",t),n(r.supported))};$e.addEventListener("message",t)})),zc=(n,t)=>{h($e),t?$e.postMessage(n,t):$e.postMessage(n)},Ni=class extends Vi{constructor(e){super();this._connectedTrack=null;if(!Me.includes(e))throw new TypeError(`Invalid subtitle codec '${e}'. Must be one of: ${Me.join(", ")}.`);this._codec=e}},Kc=class extends Ni{constructor(e){super(e);this._error=null;this._lastMuxerPromise=Promise.resolve();this._parser=new Bs({codec:e,output:(r,i)=>{this._lastMuxerPromise=this._connectedTrack.output._muxer.addSubtitleCue(this._connectedTrack,r,i).catch(a=>{this._error??=a})}})}add(e){if(typeof e!="string")throw new TypeError("text must be a string.");return this._checkForError(),this._ensureValidAdd(),this._parser.parse(e),this._lastMuxerPromise}_checkForError(){if(this._error)throw this._error}async _flushAndClose(e){e||this._checkForError()}};var Xs=class extends ge{constructor(e,r){if(!(e._target instanceof Le))throw new TypeError("HLS outputs require `OutputOptions.target` to be a PathedTarget.");super(e);this.trackDatas=[];this.isRelativeToUnixEpoch=!1;this.numWrittenMasterPlaylists=0;this.playlists=[];this.playlistDeclarations=[];this.format=r,this.targetSegmentDuration=r._options.targetDuration??2,this.singleFilePerPlaylist=r._options.singleFilePerPlaylist??!1,this.isLive=r._options.live??!1,this.maxLiveSegmentCount=r._options.maxLiveSegmentCount??1/0,this.globalTargetDuration=this.targetSegmentDuration,this.getPlaylistPath=r._options.getPlaylistPath??(({n:i})=>`playlist-${i}.m3u8`),this.getSegmentPath=r._options.getSegmentPath??(i=>i.isSingleFile?`segments-${i.playlist.n}${i.format.fileExtension}`:`segment-${i.playlist.n}-${i.n}${i.format.fileExtension}`),this.getInitPath=r._options.getInitPath??(i=>`init-${i.n}${i.segmentFormat.fileExtension}`)}async start(){let e=await this.mutex.acquire(),r=this.output._tracks.some(y=>y.metadata.isRelativeToUnixEpoch),i=this.output._tracks.some(y=>!y.metadata.isRelativeToUnixEpoch);if(r&&i)throw new Error("All tracks must agree on `relativeToUnixEpoch`: some tracks are relative to the Unix epoch and some are not.");this.isRelativeToUnixEpoch=r;let a=new Map,s=[],o=!1,c=!1,u=!1;for(let y of this.output._tracks){y.type==="video"&&(o=!0);let k=new Map;for(let w of this.output._tracks){if(y===w||!y.canBePairedWith(w))continue;if(y.type===w.type){c||(console.warn(`Illegal pairing of two ${y.type} tracks detected, which is not possible in HLS; treating them as unpaired.`),c=!0);continue}if(y.isVideoTrack()&&y.metadata.hasOnlyKeyPackets||w.isVideoTrack()&&w.metadata.hasOnlyKeyPackets){u||(console.warn("A key-packets-only video track is pairable with another track, which is not possible in HLS; treating them as unpaired."),u=!0);continue}let T=k.get(w.source._codec);T||k.set(w.source._codec,T=[]),T.push(w)}for(let[,w]of k){let T=w.map(P=>P.id).join("-");s.find(P=>P.key===T)||s.push({name:w[0].type+"-"+(s.length+1),key:T,tracks:w,needsEmit:!1,firstNoUri:!1});let x=a.get(y);x||a.set(y,x=[]),x.push(T)}}let l=o?"video":"audio",m=[],d=[],f=[];for(let y of this.output._tracks){let k=a.get(y);if(k){if(h(k.length>0),y.type!==l)continue;for(let w of k){let T=s.find(S=>S.key===w);if(h(T),k.length===1&&T.tracks.length===1){let S=a.get(T.tracks[0]);if(h(S!==void 0),S.length===1){let x=s.find(P=>P.key===S[0]);if(x.tracks.length===1){h(x.tracks[0]===y),m.push({tracks:[y,T.tracks[0]],linkedGroup:null});continue}}}m.push({tracks:[y],linkedGroup:T}),T.needsEmit=!0}}else y.type==="video"?d.push(y):y.type==="audio"&&f.push(y)}let p=({metadata:y})=>{let k="";return k+=`${y.languageCode??J}-`,k+=`${y.name??""}-`,k+=`${y.disposition?.default??!0}-`,k+=`${y.disposition?.primary??!1}-`,k+=`${y.disposition?.forced??!1}-`,k};if(d.length>0)if(new Set(d.map(p)).size>1){let k={key:d.map(w=>w.id).join("-"),name:"video-"+(s.length+1),tracks:d,needsEmit:!0,firstNoUri:!0};s.push(k),m.push({tracks:[d[0]],linkedGroup:k})}else for(let k of d)m.push({tracks:[k],linkedGroup:null});if(f.length>0)if(new Set(f.map(p)).size>1){let k={key:f.map(w=>w.id).join("-"),name:"audio-"+(s.length+1),tracks:f,needsEmit:!0,firstNoUri:!0};s.push(k),m.push({tracks:[f[0]],linkedGroup:k})}else for(let k of f)m.push({tracks:[k],linkedGroup:null});let b=y=>{let k=[],w=0,T=0,S=!1,x=null,P=-1/0;for(let A of y)A.isVideoTrack()?(w++,S||=(A.metadata.rotation??0)!==0):A.isAudioTrack()&&T++,k.push(A.source._codec);for(let A of Dt(this.format._options.segmentFormat)){let C=A.getSupportedCodecs(),I=A.getSupportedTrackCounts();if(k.some(v=>!C.includes(v))||w<I.video.min||w>I.video.max||T<I.audio.min||T>I.audio.max)continue;let R=0;S&&A.supportsVideoRotationMetadata&&R++,R>P&&(x=A,P=R)}return h(x),x},g=async y=>{if(y.some(x=>this.playlists.some(P=>P.tracks.includes(x))))throw new Error("Internal error: track is already registered in a playlist.");let k=b(y),w=this.playlists.length+1,T=await this.getPlaylistPath({n:w,tracks:y,segmentFormat:k});Vp(T);let S={id:this.playlists.length+1,path:T,tracks:y,segmentFormat:k,currentSegmentStartTimestamp:null,currentSegmentStartTimestampIsFixed:!1,nextSegmentId:1,initSegment:null,writtenSegments:[],peakBitrate:null,averageBitrate:null,mediaSequence:0,done:!1,singleFile:null,mutex:new Ie};return this.playlists.push(S),S};for(let y of s)if(y.needsEmit)for(let k=0;k<y.tracks.length;k++){let w=y.tracks[k],T=this.playlists.find(S=>S.tracks[0].id===w.id);T??=await g([w]),this.playlistDeclarations.push({playlist:T,groupId:y.name,noUri:y.firstNoUri&&k===0,references:[]})}for(let y of m){let k=this.playlists.find(w=>w.tracks[0].id===y.tracks[0].id);k??=await g(y.tracks),this.playlistDeclarations.push({playlist:k,groupId:null,noUri:!1,references:y.linkedGroup?this.playlistDeclarations.filter(w=>w.groupId===y.linkedGroup.name):[]})}e()}async getMimeType(){return St}allTracksAreKnown(e){for(let r of e.tracks)if(!r.source._closed&&!this.trackDatas.some(i=>i.track===r))return!1;return!0}async onTrackClose(e){let r=this.trackDatas.find(s=>s.track===e);r&&(r.closed=!0);let i=this.playlists.find(s=>s.tracks.includes(e));h(i);let a=await i.mutex.acquire();try{await this.advancePlaylist(i)}finally{a()}}getVideoTrackData(e,r){let i=this.trackDatas.find(s=>s.track===e);if(i)return i;sr(r),h(r),h(r?.decoderConfig);let a=this.playlists.filter(s=>s.tracks.includes(e));return h(a.length===1),i={track:e,packets:[],playlist:a[0],closed:!1,info:{type:"video",decoderConfig:r.decoderConfig}},this.trackDatas.push(i),i}getAudioTrackData(e,r){let i=this.trackDatas.find(s=>s.track===e);if(i)return i;Ee(r),h(r),h(r?.decoderConfig);let a=this.playlists.filter(s=>s.tracks.includes(e));return h(a.length===1),i={track:e,packets:[],playlist:a[0],closed:!1,info:{type:"audio",decoderConfig:r.decoderConfig}},this.trackDatas.push(i),i}async addEncodedVideoPacket(e,r,i){let a=this.getVideoTrackData(e,i),s=a.playlist,o=await s.mutex.acquire();try{this.validateTimestamp(e,r.timestamp,r.type==="key"),a.packets.push(r),s.currentSegmentStartTimestamp===null?s.currentSegmentStartTimestamp=r.timestamp:s.currentSegmentStartTimestampIsFixed||(s.currentSegmentStartTimestamp=Math.min(s.currentSegmentStartTimestamp,r.timestamp)),await this.advancePlaylist(s)}finally{o()}}async addEncodedAudioPacket(e,r,i){let a=this.getAudioTrackData(e,i),s=a.playlist,o=await s.mutex.acquire();try{this.validateTimestamp(e,r.timestamp,r.type==="key"),a.packets.push(r),s.currentSegmentStartTimestamp===null?s.currentSegmentStartTimestamp=r.timestamp:s.currentSegmentStartTimestampIsFixed||(s.currentSegmentStartTimestamp=Math.min(s.currentSegmentStartTimestamp,r.timestamp)),await this.advancePlaylist(s)}finally{o()}}async addSubtitleCue(e,r,i){throw new Error("Unreachable.")}async advancePlaylist(e){if(h(!e.done),!this.allTracksAreKnown(e))return;if(e.currentSegmentStartTimestamp===null){await this.onPlaylistDone(e);return}let r=this.trackDatas.filter(s=>e.tracks.includes(s.track)),i=r.find(s=>s.info.type==="video"),a=r.find(s=>s.info.type==="audio");for(;;){let s=e.currentSegmentStartTimestamp+this.targetSegmentDuration,o=0,c=0;if(i&&(!i.closed||i.packets.length>0)){let T=i.packets.every(P=>P.timestamp<s),S=null,x=null;if(T){if(!i.closed)return}else for(let P=0;P<i.packets.length;P++){let A=i.packets[P];if(S!==null&&A.timestamp>s)break;P>0&&A.type==="key"&&(S=A,x=P)}if(x!==null){if(o=x,a){let P=a.packets.findIndex(A=>A.timestamp>=S.timestamp);if(P!==-1)c=P;else if(a.closed)c=a.packets.length;else return}}else{if(!i.closed)return;o=i.packets.length;let P=Cu(i.packets,C=>C.timestamp),A=i.packets[P];if(h(A),a)if(A.timestamp<s){let C=a.packets.findIndex(I=>I.timestamp>=s);if(C!==-1)c=C;else if(a.closed)c=a.packets.length;else return}else{let C=a.packets.findIndex(I=>I.timestamp>A.timestamp);if(C!==-1)c=C;else if(a.closed)c=a.packets.length;else return}}}else if(a&&(!a.closed||a.packets.length>0))if(a.packets.every(S=>S.timestamp<s))if(a.closed)c=a.packets.length;else return;else{let S=rr(a.packets,x=>x.timestamp<=s);c=Math.max(S,1)}if(o===0&&c===0){r.every(S=>S.closed)&&await this.onPlaylistDone(e);return}let u=null,l,m;h(this.output._target instanceof Le);let d=this.output._target;if(this.singleFilePerPlaylist)if(e.singleFile===null){let T={n:e.nextSegmentId,format:e.segmentFormat,isSingleFile:!0,playlist:js(e)};l=await this.getSegmentPath(T),Gd(l),m=fe(fe(d.rootPath,e.path),l);let S=await this.output._getTarget({path:m,isRoot:!1,mimeType:e.segmentFormat.mimeType});S._start(),e.singleFile={target:S,path:l,nextOffset:0,info:T}}else l=e.singleFile.path,m=fe(fe(d.rootPath,e.path),l);else u={n:e.nextSegmentId,format:e.segmentFormat,isSingleFile:!1,playlist:js(e)},l=await this.getSegmentPath(u),Gd(l),m=fe(fe(d.rootPath,e.path),l),e.nextSegmentId++;let f=0,p=null,b=new Ar({format:e.segmentFormat,target:new Le(m,async T=>{let S={...T,isRoot:!1};if(T.isRoot)if(e.singleFile){let x=e.singleFile.target.slice(e.singleFile.nextOffset);return x.on("write",({end:P})=>f=Math.max(f,P)),x}else{let x=await this.output._getTarget(S);return p=x,x.on("write",({end:P})=>f=Math.max(f,P)),x}return this.output._getTarget(S)}),initTarget:async()=>{if(e.initSegment)return new $r;if(e.singleFile){e.initSegment={path:e.singleFile.path,duration:0,timestamp:0,byteSize:0,byteOffset:0,info:null};let T=e.singleFile.target.slice(e.singleFile.nextOffset);return T.on("write",({end:S})=>{e.initSegment.byteSize=Math.max(e.initSegment.byteSize,S)}),T.on("finalized",()=>{e.singleFile.nextOffset=e.initSegment.byteSize}),T}else{let T=js(e),S=await this.getInitPath(T);Up(S),e.initSegment={path:S,duration:0,timestamp:0,byteSize:0,byteOffset:null,info:null};let x=fe(fe(d.rootPath,e.path),S),P=await this.output._getTarget({path:x,isRoot:!1,mimeType:e.segmentFormat.mimeType});return P.on("write",({end:A})=>{e.initSegment.byteSize=Math.max(e.initSegment.byteSize,A)}),P.on("finalized",()=>{this.format._options.onInit?.(P,T)}),P}}}),g=-1/0;try{let T=null,S=null;if(i&&(T=new Zr(i.track.source._codec),b.addVideoTrack(T,i.track.metadata)),a&&(S=new Jr(a.track.source._codec),b.addAudioTrack(S,a.track.metadata)),await b.start(),i){h(T);let x={decoderConfig:i.info.decoderConfig};for(let P=0;P<o;P++){let A=i.packets[P];await T.add(A,x),g=Math.max(g,A.timestamp+A.duration)}}if(a){h(S);let x={decoderConfig:a.info.decoderConfig};for(let P=0;P<c;P++){let A=a.packets[P];await S.add(A,x),g=Math.max(g,A.timestamp+A.duration)}}await b.finalize()}catch(T){throw await b.cancel(),T}u&&(h(p),this.format._options.onSegment?.(p,u)),o>0&&(h(i),i.packets.splice(0,o)),c>0&&(h(a),a.packets.splice(0,c));let y=1/0;i&&i.packets.length>0&&(y=i.packets[0].timestamp),a&&a.packets.length>0&&(y=Math.min(y,a.packets[0].timestamp));let k=y<1/0?y:g;h(Number.isFinite(k));let w=k-e.currentSegmentStartTimestamp;if(h(w>=0),e.writtenSegments.push({path:l,duration:w,timestamp:e.currentSegmentStartTimestamp,byteSize:f,byteOffset:e.singleFile?e.singleFile.nextOffset:null,info:u??null}),this.globalTargetDuration=Math.max(this.globalTargetDuration,w),e.currentSegmentStartTimestamp=k,e.currentSegmentStartTimestampIsFixed=!0,e.singleFile&&(e.singleFile.nextOffset+=f),this.isLive){for(;e.writtenSegments.length>this.maxLiveSegmentCount;){let T=e.writtenSegments.shift();e.mediaSequence++,this.singleFilePerPlaylist||(h(T.info),this.format._options.onSegmentPopped?.(T.path,T.info))}await this.writePlaylist(e),await this.tryWriteMasterPlaylist()}}}async onPlaylistDone(e){h(!e.done),e.done=!0,e.singleFile&&(await e.singleFile.target._flush(),await e.singleFile.target._finalize(),this.format._options.onSegment?.(e.singleFile.target,e.singleFile.info)),await this.writePlaylist(e),this.isLive&&e.writtenSegments.length===0&&await this.tryWriteMasterPlaylist()}updatePlaylistBitrates(e){let r=e.writtenSegments,i=0,a=0,s=0;for(let o=0;o<r.length;o++){s+=r[o].duration;let c=0,u=0;for(let l=o;l<r.length&&(c+=r[l].byteSize,u+=r[l].duration,u>=.5*this.globalTargetDuration&&u<=1.5*this.globalTargetDuration&&(i=Math.max(i,8*c/u)),!(u>1.5*this.globalTargetDuration));l++);}if(i===0)for(let o of r){let c=o.duration||1;i=Math.max(i,8*o.byteSize/c)}for(let o of r)a+=8*o.byteSize;e.peakBitrate=i,e.averageBitrate=a/(s||1)}async writePlaylist(e){h(this.output._target instanceof Le);let r=this.output._target;this.updatePlaylistBitrates(e);let i=!1;for(let d of e.writtenSegments)i||=d.byteOffset!==null;let a=e.tracks[0].isVideoTrack()&&e.tracks[0].metadata.hasOnlyKeyPackets,s=3;(a||i)&&(s=4),e.initSegment&&(s=5),e.initSegment&&!a&&(s=6);let o=this.isLive?this.targetSegmentDuration:this.globalTargetDuration,c=fe(r.rootPath,e.path),u=`#EXTM3U
104
104
  #EXT-X-VERSION:${s}
105
105
  `+(this.isLive?"":`#EXT-X-PLAYLIST-TYPE:VOD
106
106
  `)+`#EXT-X-TARGETDURATION:${Math.ceil(o)}
@@ -116,13 +116,13 @@ ${r.notes??""}`,m=this.createInternalChunk(u,r.timestamp,r.duration,"key",l.trim
116
116
  `).join("")+(e.done?(e.writtenSegments.length>0?`
117
117
  `:"")+`#EXT-X-ENDLIST
118
118
  `:"");this.format._options.onPlaylist?.(u,js(e));let l=await this.output._getTarget({path:c,isRoot:!1,mimeType:St}),m=new Et(l,!0);m.start(),m.write($.encode(u)),await m.flush(),await m.finalize()}async writeMasterPlaylist(){h(this.output._target instanceof Le);let e=this.output._target,r=`#EXTM3U
119
- `,i=!1,a=null,s=0,o=!1;for(let u of this.playlistDeclarations)if(u.groupId===null){let l=u.playlist.tracks[0].isVideoTrack()&&u.playlist.tracks[0].metadata.hasOnlyKeyPackets,m=[];for(let y of u.playlist.tracks){let T=this.trackDatas.find(w=>w.track===y)?.info.decoderConfig.codec??y.source._codec;m.push(T)}let d=0,f=0;if(u.references.length>0){let k=u.references[0].playlist.tracks[0],w=this.trackDatas.find(S=>S.track===k)?.info.decoderConfig.codec??k.source._codec;m.push(w);for(let S of u.references)h(S.playlist.peakBitrate!==null),d=Math.max(d,S.playlist.peakBitrate),f=Math.max(f,S.playlist.averageBitrate??0)}h(u.playlist.peakBitrate!==null);let p=u.playlist.peakBitrate+d,b=(u.playlist.averageBitrate??0)+f;i||(r+=`
120
- `,i=!0),l?r+="#EXT-X-I-FRAME-STREAM-INF:":r+="#EXT-X-STREAM-INF:",r+=`BANDWIDTH=${Math.ceil(p)}`,b>0&&(r+=`,AVERAGE-BANDWIDTH=${Math.ceil(b)}`),r+=`,CODECS="${m.join(",")}"`;let g=u.playlist.tracks.find(y=>y.isVideoTrack());if(g?.isVideoTrack()){let k=this.trackDatas.find(T=>T.track===g)?.info.decoderConfig;if(k){let T=k.displayAspectWidth??k.codedWidth,w=k.displayAspectHeight??k.codedHeight;T!==void 0&&w!==void 0&&(g.metadata.rotation!==void 0&&g.metadata.rotation%180===90&&([T,w]=[w,T]),r+=`,RESOLUTION=${T}x${w}`)}!l&&g.metadata.frameRate!==void 0&&(r+=`,FRAME-RATE=${+g.metadata.frameRate.toFixed(3)}`)}if(!l){let y=new Map;for(let k of u.references){h(k.groupId!==null);let T=k.playlist.tracks[0].type;y.set(T,k.groupId)}for(let[k,T]of y)r+=`,${k.toUpperCase()}="${T}"`}l?(r+=`,URI="${u.playlist.path}"`,r+=`
119
+ `,i=!1,a=null,s=0,o=!1;for(let u of this.playlistDeclarations)if(u.groupId===null){let l=u.playlist.tracks[0].isVideoTrack()&&u.playlist.tracks[0].metadata.hasOnlyKeyPackets,m=[];for(let y of u.playlist.tracks){let w=this.trackDatas.find(T=>T.track===y)?.info.decoderConfig.codec??y.source._codec;m.push(w)}let d=0,f=0;if(u.references.length>0){let k=u.references[0].playlist.tracks[0],T=this.trackDatas.find(S=>S.track===k)?.info.decoderConfig.codec??k.source._codec;m.push(T);for(let S of u.references)h(S.playlist.peakBitrate!==null),d=Math.max(d,S.playlist.peakBitrate),f=Math.max(f,S.playlist.averageBitrate??0)}h(u.playlist.peakBitrate!==null);let p=u.playlist.peakBitrate+d,b=(u.playlist.averageBitrate??0)+f;i||(r+=`
120
+ `,i=!0),l?r+="#EXT-X-I-FRAME-STREAM-INF:":r+="#EXT-X-STREAM-INF:",r+=`BANDWIDTH=${Math.ceil(p)}`,b>0&&(r+=`,AVERAGE-BANDWIDTH=${Math.ceil(b)}`),r+=`,CODECS="${m.join(",")}"`;let g=u.playlist.tracks.find(y=>y.isVideoTrack());if(g?.isVideoTrack()){let k=this.trackDatas.find(w=>w.track===g)?.info.decoderConfig;if(k){let w=k.displayAspectWidth??k.codedWidth,T=k.displayAspectHeight??k.codedHeight;w!==void 0&&T!==void 0&&(g.metadata.rotation!==void 0&&g.metadata.rotation%180===90&&([w,T]=[T,w]),r+=`,RESOLUTION=${w}x${T}`)}!l&&g.metadata.frameRate!==void 0&&(r+=`,FRAME-RATE=${+g.metadata.frameRate.toFixed(3)}`)}if(!l){let y=new Map;for(let k of u.references){h(k.groupId!==null);let w=k.playlist.tracks[0].type;y.set(w,k.groupId)}for(let[k,w]of y)r+=`,${k.toUpperCase()}="${w}"`}l?(r+=`,URI="${u.playlist.path}"`,r+=`
121
121
  `):(r+=`
122
122
  `,r+=`${u.playlist.path}
123
123
  `)}else{h(u.playlist.tracks.length===1);let l=u.playlist.tracks[0],m=l.type,d=l.metadata.name??null,f=l.metadata.languageCode,p=l.metadata.disposition;(a===null||u.groupId!==a)&&(s=0,r+=`
124
- `,o=!1),a=u.groupId,s++,r+=`#EXT-X-MEDIA:TYPE=${m.toUpperCase()},GROUP-ID="${u.groupId}"`,d!==null&&/[\n\r"]/.test(d)&&(console.warn("Dropping track name since it includes a line feed, carriage return, or double quote character, which are not allowed in HLS playlist attributes."),d=null),d??=`${f??u.groupId}-${s}`,r+=`,NAME="${d}"`,f!==void 0&&(r+=`,LANGUAGE="${f}"`);let b=p?.primary??!1,g=p?.default??!0,y=p?.forced??!1;if(b&&!o&&(r+=",DEFAULT=YES",o=!0),(b||g)&&(r+=",AUTOSELECT=YES"),y&&(r+=",FORCED=YES"),m==="audio"){let T=this.trackDatas.find(w=>w.track===l)?.info.decoderConfig;T&&(r+=`,CHANNELS="${T.numberOfChannels}"`)}u.noUri||(r+=`,URI="${u.playlist.path}"`),r+=`
125
- `}this.format._options.onMaster?.(r);let c=await this.mutex.acquire();try{let u;if(this.numWrittenMasterPlaylists===0)u=await this.output._getRootWriter(!0);else{let l=await this.output._getTarget({path:e.rootPath,isRoot:!0,mimeType:St});u=new Et(l,!0),u.start()}u.write($.encode(r)),await u.flush(),await u.finalize(),this.numWrittenMasterPlaylists++}finally{c()}}async tryWriteMasterPlaylist(){h(this.isLive);for(let e of this.playlists)if(e.writtenSegments.length===0&&!e.done)return;await this.writeMasterPlaylist()}async finalize(){(await Promise.all(this.playlists.map(r=>r.mutex.acquire()))).forEach(r=>r());for(let r of this.trackDatas)r.closed=!0;await Promise.all(this.playlists.map(r=>r.done?Promise.resolve():this.advancePlaylist(r))),this.isLive||await this.writeMasterPlaylist()}},Vp=n=>{if(typeof n!="string")throw new TypeError("options.getPlaylistPath must return or resolve to a string");if(/[\n\r"]/.test(n))throw new TypeError("Playlist paths cannot contain line feed, carriage return, or double quote characters.")},Gd=n=>{if(typeof n!="string")throw new TypeError("options.getSegmentPath must return or resolve to a string");if(/[\n\r"]/.test(n))throw new TypeError("Segment paths cannot contain line feed or carriage return characters.")},Up=n=>{if(typeof n!="string")throw new TypeError("options.getInitPath must return or resolve to a string");if(/[\n\r"]/.test(n))throw new TypeError("Init paths cannot contain line feed, carriage return, or double quote characters.")},js=n=>({n:n.id,tracks:n.tracks,segmentFormat:n.segmentFormat});var Re=class{getSupportedVideoCodecs(){return this.getSupportedCodecs().filter(t=>re.includes(t))}getSupportedAudioCodecs(){return this.getSupportedCodecs().filter(t=>ae.includes(t))}getSupportedSubtitleCodecs(){return this.getSupportedCodecs().filter(t=>Me.includes(t))}_codecUnsupportedHint(t){return""}},Li=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.fastStart!==void 0&&![!1,"in-memory","reserve","fragmented"].includes(t.fastStart))throw new TypeError("options.fastStart, when provided, must be false, 'in-memory', 'reserve', or 'fragmented'.");if(t.minimumFragmentDuration!==void 0&&(!Number.isFinite(t.minimumFragmentDuration)||t.minimumFragmentDuration<0))throw new TypeError("options.minimumFragmentDuration, when provided, must be a non-negative number.");if(t.onFtyp!==void 0&&typeof t.onFtyp!="function")throw new TypeError("options.onFtyp, when provided, must be a function.");if(t.onMoov!==void 0&&typeof t.onMoov!="function")throw new TypeError("options.onMoov, when provided, must be a function.");if(t.onMdat!==void 0&&typeof t.onMdat!="function")throw new TypeError("options.onMdat, when provided, must be a function.");if(t.onMoof!==void 0&&typeof t.onMoof!="function")throw new TypeError("options.onMoof, when provided, must be a function.");if(t.metadataFormat!==void 0&&!["mdir","mdta","udta","auto"].includes(t.metadataFormat))throw new TypeError("options.metadataFormat, when provided, must be either 'auto', 'mdir', 'mdta', or 'udta'.");super(),this._options=t}getSupportedTrackCounts(){return{video:{min:0,max:4294967295},audio:{min:0,max:4294967295},subtitle:{min:0,max:4294967295},total:{min:1,max:4294967295}}}get supportsVideoRotationMetadata(){return!0}get supportsTimestampedMediaData(){return!0}_createMuxer(t){return new zs(t,this)}},Wi=class extends Li{constructor(t){super(t)}get _name(){return"MP4"}get fileExtension(){return".mp4"}get mimeType(){return"video/mp4"}getSupportedCodecs(){return[...re,...wt,"pcm-s16","pcm-s16be","pcm-s24","pcm-s24be","pcm-s32","pcm-s32be","pcm-f32","pcm-f32be","pcm-f64","pcm-f64be",...Me]}_codecUnsupportedHint(t){return new Bi().getSupportedCodecs().includes(t)?" Switching to MOV will grant support for this codec.":""}},Mi=class extends Li{constructor(t){super(t)}get _name(){return"CMAF"}get fileExtension(){return".m4s"}get mimeType(){return"video/mp4"}getSupportedCodecs(){return[...re,...wt,"pcm-s16","pcm-s16be","pcm-s24","pcm-s24be","pcm-s32","pcm-s32be","pcm-f32","pcm-f32be","pcm-f64","pcm-f64be",...Me]}},Bi=class extends Li{constructor(t){super(t)}get _name(){return"MOV"}get fileExtension(){return".mov"}get mimeType(){return"video/quicktime"}getSupportedCodecs(){return[...re,...ae]}_codecUnsupportedHint(t){return new Wi().getSupportedCodecs().includes(t)?" Switching to MP4 will grant support for this codec.":""}},Vn=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.appendOnly!==void 0&&typeof t.appendOnly!="boolean")throw new TypeError("options.appendOnly, when provided, must be a boolean.");if(t.minimumClusterDuration!==void 0&&(!Number.isFinite(t.minimumClusterDuration)||t.minimumClusterDuration<0))throw new TypeError("options.minimumClusterDuration, when provided, must be a non-negative number.");if(t.onEbmlHeader!==void 0&&typeof t.onEbmlHeader!="function")throw new TypeError("options.onEbmlHeader, when provided, must be a function.");if(t.onSegmentHeader!==void 0&&typeof t.onSegmentHeader!="function")throw new TypeError("options.onHeader, when provided, must be a function.");if(t.onCluster!==void 0&&typeof t.onCluster!="function")throw new TypeError("options.onCluster, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Ns(t,this)}get _name(){return"Matroska"}getSupportedTrackCounts(){return{video:{min:0,max:127},audio:{min:0,max:127},subtitle:{min:0,max:127},total:{min:1,max:127}}}get fileExtension(){return".mkv"}get mimeType(){return"video/x-matroska"}getSupportedCodecs(){return[...re,...wt,...ee.filter(t=>!["pcm-s8","pcm-f32be","pcm-f64be","ulaw","alaw"].includes(t)),...Me]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!0}},Oi=class extends Vn{constructor(t){super(t)}getSupportedCodecs(){return[...re.filter(t=>["vp8","vp9","av1"].includes(t)),...ae.filter(t=>["opus","vorbis"].includes(t)),...Me]}get _name(){return"WebM"}get fileExtension(){return".webm"}get mimeType(){return"video/webm"}_codecUnsupportedHint(t){return new Vn().getSupportedCodecs().includes(t)?" Switching to MKV will grant support for this codec.":""}},Qc=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.xingHeader!==void 0&&typeof t.xingHeader!="boolean")throw new TypeError("options.xingHeader, when provided, must be a boolean.");if(t.onXingFrame!==void 0&&typeof t.onXingFrame!="function")throw new TypeError("options.onXingFrame, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Ws(t,this)}get _name(){return"MP3"}getSupportedTrackCounts(){return{video:{min:0,max:0},audio:{min:1,max:1},subtitle:{min:0,max:0},total:{min:1,max:1}}}get fileExtension(){return".mp3"}get mimeType(){return"audio/mpeg"}getSupportedCodecs(){return["mp3"]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!1}},jc=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.large!==void 0&&typeof t.large!="boolean")throw new TypeError("options.large, when provided, must be a boolean.");if(t.metadataFormat!==void 0&&!["info","id3"].includes(t.metadataFormat))throw new TypeError("options.metadataFormat, when provided, must be either 'info' or 'id3'.");if(t.onHeader!==void 0&&typeof t.onHeader!="function")throw new TypeError("options.onHeader, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Ks(t,this)}get _name(){return"WAVE"}getSupportedTrackCounts(){return{video:{min:0,max:0},audio:{min:1,max:1},subtitle:{min:0,max:0},total:{min:1,max:1}}}get fileExtension(){return".wav"}get mimeType(){return"audio/wav"}getSupportedCodecs(){return[...ee.filter(t=>["pcm-s16","pcm-s24","pcm-s32","pcm-f32","pcm-u8","ulaw","alaw"].includes(t))]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!1}},Xc=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.maximumPageDuration!==void 0&&(!Number.isFinite(t.maximumPageDuration)||t.maximumPageDuration<=0))throw new TypeError("options.maximumPageDuration, when provided, must be a positive number.");if(t.onPage!==void 0&&typeof t.onPage!="function")throw new TypeError("options.onPage, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Hs(t,this)}get _name(){return"Ogg"}getSupportedTrackCounts(){return{video:{min:0,max:0},audio:{min:0,max:4294967296},subtitle:{min:0,max:0},total:{min:1,max:4294967296}}}get fileExtension(){return".ogg"}get mimeType(){return"application/ogg"}getSupportedCodecs(){return[...ae.filter(t=>["vorbis","opus"].includes(t))]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!1}},$c=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.onFrame!==void 0&&typeof t.onFrame!="function")throw new TypeError("options.onFrame, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Fs(t,this)}get _name(){return"ADTS"}getSupportedTrackCounts(){return{video:{min:0,max:0},audio:{min:1,max:1},subtitle:{min:0,max:0},total:{min:1,max:1}}}get fileExtension(){return".aac"}get mimeType(){return"audio/aac"}getSupportedCodecs(){return["aac"]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!1}},Yc=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.appendOnly!==void 0&&typeof t.appendOnly!="boolean")throw new TypeError("options.appendOnly, when provided, must be a boolean.");super(),this._options=t}_createMuxer(t){return new Ms(t,this)}get _name(){return"FLAC"}getSupportedTrackCounts(){return{video:{min:0,max:0},audio:{min:1,max:1},subtitle:{min:0,max:0},total:{min:1,max:1}}}get fileExtension(){return".flac"}get mimeType(){return"audio/flac"}getSupportedCodecs(){return["flac"]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!1}},Zc=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.onPacket!==void 0&&typeof t.onPacket!="function")throw new TypeError("options.onPacket, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new qs(t,this)}get _name(){return"MPEG-TS"}getSupportedTrackCounts(){return{video:{min:0,max:16},audio:{min:0,max:32},subtitle:{min:0,max:0},total:{min:1,max:48}}}get fileExtension(){return".ts"}get mimeType(){return"video/MP2T"}getSupportedCodecs(){return[...re.filter(t=>["avc","hevc"].includes(t)),...ae.filter(t=>["aac","mp3","ac3","eac3"].includes(t))]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!0}},Jc=class extends Re{constructor(t){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(!(t.segmentFormat instanceof Re)&&(!Array.isArray(t.segmentFormat)||t.segmentFormat.length===0||!t.segmentFormat.every(e=>e instanceof Re)))throw new TypeError("options.segmentFormat must be an OutputFormat or a non-empty array of OutputFormat instances.");if(t.targetDuration!==void 0&&(typeof t.targetDuration!="number"||t.targetDuration<=0))throw new TypeError("options.targetDuration, when provided, must be a positive number.");if(t.singleFilePerPlaylist!==void 0&&typeof t.singleFilePerPlaylist!="boolean")throw new TypeError("options.singleFilePerPlaylist, when provided, must be a boolean.");if(t.live!==void 0&&typeof t.live!="boolean")throw new TypeError("options.live, when provided, must be a boolean.");if(t.maxLiveSegmentCount!==void 0&&(typeof t.maxLiveSegmentCount!="number"||t.maxLiveSegmentCount<1||Number.isFinite(t.maxLiveSegmentCount)&&!Number.isInteger(t.maxLiveSegmentCount)))throw new TypeError("options.maxLiveSegmentCount, when provided, must be a positive integer or Infinity.");if(t.getPlaylistPath!==void 0&&typeof t.getPlaylistPath!="function")throw new TypeError("options.getPlaylistPath, when provided, must be a function.");if(t.getSegmentPath!==void 0&&typeof t.getSegmentPath!="function")throw new TypeError("options.getSegmentPath, when provided, must be a function.");if(t.getInitPath!==void 0&&typeof t.getInitPath!="function")throw new TypeError("options.getInitPath, when provided, must be a function.");if(t.onMaster!==void 0&&typeof t.onMaster!="function")throw new TypeError("options.onMaster, when provided, must be a function.");if(t.onPlaylist!==void 0&&typeof t.onPlaylist!="function")throw new TypeError("options.onPlaylist, when provided, must be a function.");if(t.onSegment!==void 0&&typeof t.onSegment!="function")throw new TypeError("options.onSegment, when provided, must be a function.");if(t.onInit!==void 0&&typeof t.onInit!="function")throw new TypeError("options.onInit, when provided, must be a function.");if(t.onSegmentPopped!==void 0&&typeof t.onSegmentPopped!="function")throw new TypeError("options.onSegmentPopped, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Xs(t,this)}get _name(){return"HTTP Live Streaming (HLS)"}get fileExtension(){return".m3u8"}get mimeType(){return St}getSupportedCodecs(){return[...new Set(Dt(this._options.segmentFormat).flatMap(e=>e.getSupportedCodecs()))]}getSupportedTrackCounts(){let t=!1,e=!1,r=!1;for(let i of Dt(this._options.segmentFormat)){let a=i.getSupportedTrackCounts();t||=a.video.max>0,e||=a.audio.max>0,r||=a.subtitle.max>0}return{video:{min:0,max:t?1/0:0},audio:{min:0,max:e?1/0:0},subtitle:{min:0,max:0},total:{min:1,max:1/0}}}get supportsVideoRotationMetadata(){return Dt(this._options.segmentFormat).some(t=>t.supportsVideoRotationMetadata)}get supportsTimestampedMediaData(){return!0}_codecUnsupportedHint(t){return" Using different segment formats may grant support for this codec."}};var Kd=["video","audio","subtitle"],Hi=class n{constructor(t,e,r,i,a){this.id=t,this.output=e,this.type=r,this.source=i,this.metadata=a}isVideoTrack(){return this.type==="video"}isAudioTrack(){return this.type==="audio"}isSubtitleTrack(){return this.type==="subtitle"}canBePairedWith(t){if(!(t instanceof n))throw new TypeError("other must be an OutputTrack.");if(this===t)return!1;let e=Dt(this.metadata.group),r=Dt(t.metadata.group);for(let i of e)if(this.type!==t.type&&r.some(o=>i===o)||r.some(o=>i._pairedGroups.has(o)))return!0;return!1}},$s=class extends Hi{constructor(t,e,r,i){super(t,e,"video",r,i)}},Ys=class extends Hi{constructor(t,e,r,i){super(t,e,"audio",r,i)}},Zs=class extends Hi{constructor(t,e,r,i){super(t,e,"subtitle",r,i)}},We=class n{constructor(){this._pairedGroups=new Set}pairWith(t){if(!(t instanceof n))throw new TypeError("other must be an OutputTrackGroup.");if(this===t)throw new TypeError("Cannot pair a group with itself.");this._pairedGroups.add(t),t._pairedGroups.add(this)}},eu=n=>{if(!n||typeof n!="object")throw new TypeError("metadata must be an object.");if(n.languageCode!==void 0&&!Bt(n.languageCode))throw new TypeError("metadata.languageCode, when provided, must be a three-letter, ISO 639-2/T language code.");if(n.name!==void 0&&typeof n.name!="string")throw new TypeError("metadata.name, when provided, must be a string.");if(n.disposition!==void 0&&Ru(n.disposition),n.maximumPacketCount!==void 0&&(!Number.isInteger(n.maximumPacketCount)||n.maximumPacketCount<0))throw new TypeError("metadata.maximumPacketCount, when provided, must be a non-negative integer.");if(n.group!==void 0&&!(n.group instanceof We)&&(!Array.isArray(n.group)||n.group.some(t=>!(t instanceof We))))throw new TypeError("metadata.group, when provided, must be an OutputTrackGroup instance or an array of OutputTrackGroup instances.")},Sr=class extends Ye{constructor(e){super();this.state="pending";this.defaultTrackGroup=new We;this._onFinalize=null;this._unfinalizedTargets=new Set;this._rootWriterPromise=null;this._tracks=[];this._startPromise=null;this._cancelPromise=null;this._finalizePromise=null;this._mutex=new Ie;this._metadataTags={};this._rootTarget=null;this._rootTargetPromise=null;this._firstMediaStreamTimestamp=null;if(!e||typeof e!="object")throw new TypeError("options must be an object.");if(!(e.format instanceof Re))throw new TypeError("options.format must be an OutputFormat.");if(!(e.target instanceof Ce||e.target instanceof Le))throw new TypeError("options.target must be a Target or a PathedTarget.");if(e.target instanceof Ce&&this._rememberTarget(e.target),e.initTarget!==void 0&&!(e.initTarget instanceof Ce)&&typeof e.initTarget!="function")throw new Error("options.initTarget, when provided, must be a Target or a function that returns or resolves to a Target.");if(e.onFinalize!==void 0&&typeof e.onFinalize!="function")throw new TypeError("options.onFinalize, when provided, must be a function.");this.format=e.format,this._target=e.target,this._onFinalize=e.onFinalize??null,this._initTarget=e.initTarget??null,this._initTarget instanceof Ce&&this._rememberTarget(this._initTarget),this._muxer=e.format._createMuxer(this)}get target(){let e="Output.target cannot be used when using PathedTarget with an async callback. Use the 'target' event instead.";if(this._rootTargetPromise)throw new TypeError(e);let r=this._getRootTarget();if(r instanceof Promise)throw new TypeError(e);return r}_getTargetValidated(e){h(this._target instanceof Le);let r=this._target.getTarget(e),i=a=>{if(!(a instanceof Ce))throw new TypeError("getTarget must return a Target.");return a};return r instanceof Promise?r.then(i):i(r)}async _getTarget(e){h(this._target instanceof Le);let r=await this._getTargetValidated(e);return this._emit("target",{target:r,request:e,isRoot:e.isRoot}),this.state==="canceled"?await r._close():this._rememberTarget(r),r}_rememberTarget(e){this._unfinalizedTargets.add(e),e.on("finalized",()=>this._unfinalizedTargets.delete(e),{once:!0})}async _getInitTarget(){if(h(this._initTarget!==null),this._initTarget instanceof Ce)return this._initTarget;let e=await this._initTarget();return this.state==="canceled"?await e._close():this._rememberTarget(e),e}_hasInitTarget(){return this._initTarget!==null}_getRootTarget(){if(this._rootTarget)return this._rootTarget;if(this._rootTargetPromise)return this._rootTargetPromise;if(this._target instanceof Ce)return this._emit("target",{target:this._target,request:null,isRoot:!0}),this._rootTarget=this._target,this._target;let e={path:this._target.rootPath,isRoot:!0,mimeType:this.format.mimeType},r=this._getTargetValidated(e),i=a=>(this.state==="canceled"?a._close():this._rememberTarget(a),this._emit("target",{target:a,request:e,isRoot:!0}),this._rootTarget=a,a);return r instanceof Promise?this._rootTargetPromise=r.then(i):i(r)}_getRootWriter(e){return this._rootWriterPromise??=(async()=>{let r=await this._getRootTarget(),i=new Et(r,typeof e=="boolean"?e:e(r));return i.start(),i})()}addVideoTrack(e,r={}){if(!(e instanceof Jt))throw new TypeError("source must be a VideoSource.");if(eu(r),r.rotation!==void 0&&![0,90,180,270].includes(r.rotation))throw new TypeError(`Invalid video rotation: ${r.rotation}. Has to be 0, 90, 180 or 270.`);if(!this.format.supportsVideoRotationMetadata&&r.rotation)throw new Error(`${this.format._name} does not support video rotation metadata.`);if(r.frameRate!==void 0&&(!Number.isFinite(r.frameRate)||r.frameRate<=0))throw new TypeError(`Invalid video frame rate: ${r.frameRate}. Must be a positive number.`);let i={...r};return i.group??=this.defaultTrackGroup,this._addTrack(new $s(this._tracks.length+1,this,e,i))}addAudioTrack(e,r={}){if(!(e instanceof er))throw new TypeError("source must be an AudioSource.");eu(r);let i={...r};return i.group??=this.defaultTrackGroup,this._addTrack(new Ys(this._tracks.length+1,this,e,i))}addSubtitleTrack(e,r={}){if(!(e instanceof Ni))throw new TypeError("source must be a SubtitleSource.");eu(r);let i={...r};return i.group??=this.defaultTrackGroup,this._addTrack(new Zs(this._tracks.length+1,this,e,i))}setMetadataTags(e){if(Yi(e),this.state!=="pending")throw new Error("Cannot set metadata tags after output has been started or canceled.");this._metadataTags=e}_addTrack(e){if(this.state!=="pending")throw new Error("Cannot add track after output has been started or canceled.");if(e.source._connectedTrack)throw new Error("Source is already used for a track.");let r=this.format.getSupportedTrackCounts(),i=this._tracks.reduce((o,c)=>o+(c.type===e.type?1:0),0),a=r[e.type].max;if(i===a)throw new Error(a===0?`${this.format._name} does not support ${e.type} tracks.`:`${this.format._name} does not support more than ${a} ${e.type} track${a===1?"":"s"}.`);let s=r.total.max;if(this._tracks.length===s)throw new Error(`${this.format._name} does not support more than ${s} tracks${s===1?"":"s"} in total.`);if(e.isVideoTrack()){let o=this.format.getSupportedVideoCodecs();if(o.length===0)throw new Error(`${this.format._name} does not support video tracks.`+this.format._codecUnsupportedHint(e.source._codec));if(!o.includes(e.source._codec))throw new Error(`Codec '${e.source._codec}' cannot be contained within ${this.format._name}. Supported video codecs are: ${o.map(c=>`'${c}'`).join(", ")}.`+this.format._codecUnsupportedHint(e.source._codec))}else if(e.isAudioTrack()){let o=this.format.getSupportedAudioCodecs();if(o.length===0)throw new Error(`${this.format._name} does not support audio tracks.`+this.format._codecUnsupportedHint(e.source._codec));if(!o.includes(e.source._codec))throw new Error(`Codec '${e.source._codec}' cannot be contained within ${this.format._name}. Supported audio codecs are: ${o.map(c=>`'${c}'`).join(", ")}.`+this.format._codecUnsupportedHint(e.source._codec))}else if(e.isSubtitleTrack()){let o=this.format.getSupportedSubtitleCodecs();if(o.length===0)throw new Error(`${this.format._name} does not support subtitle tracks.`+this.format._codecUnsupportedHint(e.source._codec));if(!o.includes(e.source._codec))throw new Error(`Codec '${e.source._codec}' cannot be contained within ${this.format._name}. Supported subtitle codecs are: ${o.map(c=>`'${c}'`).join(", ")}.`+this.format._codecUnsupportedHint(e.source._codec))}return this._tracks.push(e),e.source._connectedTrack=e,e}async start(){let e=this.format.getSupportedTrackCounts();for(let i of Kd){let a=this._tracks.reduce((o,c)=>o+(c.type===i?1:0),0),s=e[i].min;if(a<s)throw new Error(s===e[i].max?`${this.format._name} requires exactly ${s} ${i} track${s===1?"":"s"}.`:`${this.format._name} requires at least ${s} ${i} track${s===1?"":"s"}.`)}let r=e.total.min;if(this._tracks.length<r)throw new Error(r===e.total.max?`${this.format._name} requires exactly ${r} track${r===1?"":"s"}.`:`${this.format._name} requires at least ${r} track${r===1?"":"s"}.`);if(this.state==="canceled")throw new Error("Output has been canceled.");return this._startPromise?(console.warn("Output has already been started."),this._startPromise):this._startPromise=(async()=>{this.state="started";let i=await this._mutex.acquire();try{await this._muxer.start();let a=this._tracks.map(s=>s.source._start());await Promise.all(a)}finally{i()}})()}getMimeType(){return this._muxer.getMimeType()}async cancel(){if(this._cancelPromise)return console.warn("Output has already been canceled."),this._cancelPromise;if(this.state==="finalizing"||this.state==="finalized"){this.state==="finalized"&&console.warn("Output has already been finalized.");return}return this._cancelPromise=(async()=>{this.state="canceled";let e=await this._mutex.acquire();try{let r=this._tracks.map(i=>i.source._flushOrWaitForOngoingClose(!0));await Promise.all(r),await Promise.all([...this._unfinalizedTargets].map(i=>i._close())),this._unfinalizedTargets.clear()}finally{e()}})()}async finalize(){if(this.state==="pending")throw new Error("Cannot finalize before starting.");if(this.state==="canceled")throw new Error("Cannot finalize after canceling.");return this._finalizePromise?(console.warn("Output has already been finalized."),this._finalizePromise):this._finalizePromise=(async()=>{this.state="finalizing";let e=await this._mutex.acquire();try{let r=this._tracks.map(i=>i.source._flushOrWaitForOngoingClose(!1));if(await Promise.all(r),await this._muxer.finalize(),this._rootWriterPromise){let i=await this._rootWriterPromise;i.finalized||(await i.flush(),await i.finalize())}this._onFinalize&&await this._onFinalize(),this.state="finalized"}finally{e()}})()}};var Js=n=>{if(!n||typeof n!="object")throw new TypeError("options.video, when provided, must be an object.");if(n?.discard!==void 0&&typeof n.discard!="boolean")throw new TypeError("options.video.discard, when provided, must be a boolean.");if(n?.forceTranscode!==void 0&&typeof n.forceTranscode!="boolean")throw new TypeError("options.video.forceTranscode, when provided, must be a boolean.");if(n?.codec!==void 0&&!re.includes(n.codec))throw new TypeError(`options.video.codec, when provided, must be one of: ${re.join(", ")}.`);if(n?.bitrate!==void 0&&!(n.bitrate instanceof Pe)&&(!Number.isInteger(n.bitrate)||n.bitrate<=0))throw new TypeError("options.video.bitrate, when provided, must be a positive integer or a quality.");if(n?.width!==void 0&&(!Number.isInteger(n.width)||n.width<=0))throw new TypeError("options.video.width, when provided, must be a positive integer.");if(n?.height!==void 0&&(!Number.isInteger(n.height)||n.height<=0))throw new TypeError("options.video.height, when provided, must be a positive integer.");if(n?.fit!==void 0&&!["fill","contain","cover"].includes(n.fit))throw new TypeError("options.video.fit, when provided, must be one of 'fill', 'contain', or 'cover'.");if(n?.width!==void 0&&n.height!==void 0&&n.fit===void 0)throw new TypeError("When both options.video.width and options.video.height are provided, options.video.fit must also be provided.");if(n?.rotate!==void 0&&![0,90,180,270].includes(n.rotate))throw new TypeError("options.video.rotate, when provided, must be 0, 90, 180 or 270.");if(n?.allowRotationMetadata!==void 0&&typeof n.allowRotationMetadata!="boolean")throw new TypeError("options.video.allowRotationMetadata, when provided, must be a boolean.");if(n?.crop!==void 0&&yr(n.crop,"options.video."),n?.frameRate!==void 0&&(!Number.isFinite(n.frameRate)||n.frameRate<=0))throw new TypeError("options.video.frameRate, when provided, must be a finite positive number.");if(n?.alpha!==void 0&&!["discard","keep"].includes(n.alpha))throw new TypeError("options.video.alpha, when provided, must be either 'discard' or 'keep'.");if(n?.keyFrameInterval!==void 0&&(!Number.isFinite(n.keyFrameInterval)||n.keyFrameInterval<0))throw new TypeError("options.video.keyFrameInterval, when provided, must be a non-negative number.");if(n?.process!==void 0&&typeof n.process!="function")throw new TypeError("options.video.process, when provided, must be a function.");if(n?.processedWidth!==void 0&&(!Number.isInteger(n.processedWidth)||n.processedWidth<=0))throw new TypeError("options.video.processedWidth, when provided, must be a positive integer.");if(n?.processedHeight!==void 0&&(!Number.isInteger(n.processedHeight)||n.processedHeight<=0))throw new TypeError("options.video.processedHeight, when provided, must be a positive integer.");if(n?.hardwareAcceleration!==void 0&&!["no-preference","prefer-hardware","prefer-software"].includes(n.hardwareAcceleration))throw new TypeError("options.video.hardwareAcceleration, when provided, must be 'no-preference', 'prefer-hardware' or 'prefer-software'.");if(n?.group!==void 0&&!(n.group instanceof We||Array.isArray(n.group)&&n.group.every(t=>t instanceof We)))throw new TypeError("options.video.group, when provided, must be an OutputTrackGroup or an array of OutputTrackGroups.")},eo=n=>{if(!n||typeof n!="object")throw new TypeError("options.audio, when provided, must be an object.");if(n?.discard!==void 0&&typeof n.discard!="boolean")throw new TypeError("options.audio.discard, when provided, must be a boolean.");if(n?.forceTranscode!==void 0&&typeof n.forceTranscode!="boolean")throw new TypeError("options.audio.forceTranscode, when provided, must be a boolean.");if(n?.codec!==void 0&&!ae.includes(n.codec))throw new TypeError(`options.audio.codec, when provided, must be one of: ${ae.join(", ")}.`);if(n?.bitrate!==void 0&&!(n.bitrate instanceof Pe)&&(!Number.isInteger(n.bitrate)||n.bitrate<=0))throw new TypeError("options.audio.bitrate, when provided, must be a positive integer or a quality.");if(n?.numberOfChannels!==void 0&&(!Number.isInteger(n.numberOfChannels)||n.numberOfChannels<=0))throw new TypeError("options.audio.numberOfChannels, when provided, must be a positive integer.");if(n?.sampleRate!==void 0&&(!Number.isInteger(n.sampleRate)||n.sampleRate<=0))throw new TypeError("options.audio.sampleRate, when provided, must be a positive integer.");if(n?.sampleFormat!==void 0&&!["u8","s16","s32","f32"].includes(n.sampleFormat))throw new TypeError("options.audio.sampleFormat, when provided, must be one of: u8, s16, s32, f32.");if(n?.process!==void 0&&typeof n.process!="function")throw new TypeError("options.audio.process, when provided, must be a function.");if(n?.processedNumberOfChannels!==void 0&&(!Number.isInteger(n.processedNumberOfChannels)||n.processedNumberOfChannels<=0))throw new TypeError("options.audio.processedNumberOfChannels, when provided, must be a positive integer.");if(n?.processedSampleRate!==void 0&&(!Number.isInteger(n.processedSampleRate)||n.processedSampleRate<=0))throw new TypeError("options.audio.processedSampleRate, when provided, must be a positive integer.");if(n?.group!==void 0&&!(n.group instanceof We||Array.isArray(n.group)&&n.group.every(t=>t instanceof We)))throw new TypeError("options.audio.group, when provided, must be an OutputTrackGroup or an array of OutputTrackGroups.")},tu=2,ru=48e3,iu=class n{constructor(t){this._addedCounts={video:0,audio:0,subtitle:0};this._totalTrackCount=0;this._nextOutputTrackId=0;this._outputTrackIds=[];this._outputOwnTrackGroups=[];this._trackPromises=[];this._executed=!1;this._synchronizer=new nu;this._totalDuration=null;this._maxTimestamps=new Map;this._canceled=!1;this.onProgress=void 0;this._computeProgress=!1;this._lastProgress=0;this.isValid=!1;this.utilizedTracks=[];this.discardedTracks=[];if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(!(t.input instanceof Nr))throw new TypeError("options.input must be an Input.");if(!(t.output instanceof Sr))throw new TypeError("options.output must be an Output.");if(t.tracks!==void 0&&t.tracks!=="all"&&t.tracks!=="primary")throw new TypeError("options.tracks, when provided, must be either 'all' or 'primary'.");if(t.output._tracks.length>0||Object.keys(t.output._metadataTags).length>0||t.output.state!=="pending")throw new TypeError("options.output must be fresh: no tracks or metadata tags added and not started.");if(t.video!==void 0&&typeof t.video!="function")if(Array.isArray(t.video))for(let i of t.video)Js(i);else Js(t.video);if(t.audio!==void 0&&typeof t.audio!="function")if(Array.isArray(t.audio))for(let i of t.audio)eo(i);else eo(t.audio);if(t.trim!==void 0&&(!t.trim||typeof t.trim!="object"))throw new TypeError("options.trim, when provided, must be an object.");if(t.trim?.start!==void 0&&!Number.isFinite(t.trim.start))throw new TypeError("options.trim.start, when provided, must be a finite number.");if(t.trim?.end!==void 0&&!Number.isFinite(t.trim.end))throw new TypeError("options.trim.end, when provided, must be a finite number.");if(t.trim?.start!==void 0&&t.trim.end!==void 0&&t.trim.start>=t.trim.end)throw new TypeError("options.trim.start must be less than options.trim.end.");if(t.tags!==void 0&&(typeof t.tags!="object"||!t.tags)&&typeof t.tags!="function")throw new TypeError("options.tags, when provided, must be an object or a function.");if(typeof t.tags=="object"&&Yi(t.tags),t.showWarnings!==void 0&&typeof t.showWarnings!="boolean")throw new TypeError("options.showWarnings, when provided, must be a boolean.");this._options=t,this.input=t.input,this.output=t.output;let{promise:e,resolve:r}=Z();this._started=e,this._start=r}static async init(t){let e=new n(t);return await e._init(),e}async _init(){let t=await this.input.getFormat(),e,r=this._options.tracks;if(r===void 0&&(r=t.name.includes("(HLS)")?"primary":"all"),r==="all")e=await this.input.getTracks();else if(r==="primary"){let f=await this.input.getPrimaryVideoTrack(),p=await this.input.getPrimaryAudioTrack();e=[f,p].filter(b=>b!==null)}else ne(r),h(!1);let i=this.output.format.getSupportedTrackCounts(),a=1,s=1,o=[],c=[];for(let f of e){let p;if(f.isVideoTrack())if(this._options.video)if(typeof this._options.video=="function"){let y=await this._options.video(f,a)??{};if(Array.isArray(y))for(let k of y)Js(k);else Js(y);p=Array.isArray(y)?y:[y],a++}else p=Array.isArray(this._options.video)?this._options.video:[this._options.video];else p=[{}];else if(f.isAudioTrack())if(this._options.audio)if(typeof this._options.audio=="function"){let y=await this._options.audio(f,s)??{};if(Array.isArray(y))for(let k of y)eo(k);else eo(y);p=Array.isArray(y)?y:[y],s++}else p=Array.isArray(this._options.audio)?this._options.audio:[this._options.audio];else p=[{}];else h(!1);let b=p.filter(y=>y.discard);for(let y of b)this.discardedTracks.push({track:f,reason:"discarded_by_user",trackOptions:y});if(p.length===b.length){p.length===0&&this.discardedTracks.push({track:f,reason:"discarded_by_user",trackOptions:{}});continue}let g=p.filter(y=>!y.discard);o.push(f),c.push(g)}this._options.trim?.start!==void 0?this._startTimestamp=this._options.trim.start:this._startTimestamp=Math.max(await this.input.getFirstTimestamp(o),0),this._endTimestamp=Math.max(this._options.trim?.end??1/0,this._startTimestamp);for(let f=0;f<o.length;f++){let p=o[f],b=c[f];for(let g of b){if(this._totalTrackCount===i.total.max){this.discardedTracks.push({track:p,reason:"max_track_count_reached",trackOptions:g});continue}if(this._addedCounts[p.type]===i[p.type].max){this.discardedTracks.push({track:p,reason:"max_track_count_of_type_reached",trackOptions:g});continue}let y=this._nextOutputTrackId++;p.isVideoTrack()?await this._processVideoTrack(p,g,y):p.isAudioTrack()?await this._processAudioTrack(p,g,y):h(!1)}}for(let f=0;f<this.utilizedTracks.length-1;f++)for(let p=f+1;p<this.utilizedTracks.length;p++){let b=this.utilizedTracks[f],g=this.utilizedTracks[p],y=this._outputOwnTrackGroups[f],k=this._outputOwnTrackGroups[p];h(y!==void 0),h(k!==void 0),y&&k&&b.canBePairedWith(g)&&y.pairWith(k)}let u=await this.input.getMetadataTags(),l;if(this._options.tags){let f=typeof this._options.tags=="function"?await this._options.tags(u):this._options.tags;Yi(f),l=f}else l=u;let m=t.mimeType===this.output.format.mimeType,d=u.raw===l.raw;if(u.raw&&d&&!m&&delete l.raw,this.output.setMetadataTags(l),this.isValid=this._totalTrackCount>=i.total.min&&this._addedCounts.video>=i.video.min&&this._addedCounts.audio>=i.audio.min&&this._addedCounts.subtitle>=i.subtitle.min,this._options.showWarnings??!0){let f=[],p=this.discardedTracks.filter(b=>b.reason!=="discarded_by_user");p.length>0&&f.push("Some tracks had to be discarded from the conversion:",p),this.isValid||(f.length>0&&f.push(`
124
+ `,o=!1),a=u.groupId,s++,r+=`#EXT-X-MEDIA:TYPE=${m.toUpperCase()},GROUP-ID="${u.groupId}"`,d!==null&&/[\n\r"]/.test(d)&&(console.warn("Dropping track name since it includes a line feed, carriage return, or double quote character, which are not allowed in HLS playlist attributes."),d=null),d??=`${f??u.groupId}-${s}`,r+=`,NAME="${d}"`,f!==void 0&&(r+=`,LANGUAGE="${f}"`);let b=p?.primary??!1,g=p?.default??!0,y=p?.forced??!1;if(b&&!o&&(r+=",DEFAULT=YES",o=!0),(b||g)&&(r+=",AUTOSELECT=YES"),y&&(r+=",FORCED=YES"),m==="audio"){let w=this.trackDatas.find(T=>T.track===l)?.info.decoderConfig;w&&(r+=`,CHANNELS="${w.numberOfChannels}"`)}u.noUri||(r+=`,URI="${u.playlist.path}"`),r+=`
125
+ `}this.format._options.onMaster?.(r);let c=await this.mutex.acquire();try{let u;if(this.numWrittenMasterPlaylists===0)u=await this.output._getRootWriter(!0);else{let l=await this.output._getTarget({path:e.rootPath,isRoot:!0,mimeType:St});u=new Et(l,!0),u.start()}u.write($.encode(r)),await u.flush(),await u.finalize(),this.numWrittenMasterPlaylists++}finally{c()}}async tryWriteMasterPlaylist(){h(this.isLive);for(let e of this.playlists)if(e.writtenSegments.length===0&&!e.done)return;await this.writeMasterPlaylist()}async finalize(){(await Promise.all(this.playlists.map(r=>r.mutex.acquire()))).forEach(r=>r());for(let r of this.trackDatas)r.closed=!0;await Promise.all(this.playlists.map(r=>r.done?Promise.resolve():this.advancePlaylist(r))),this.isLive||await this.writeMasterPlaylist()}},Vp=n=>{if(typeof n!="string")throw new TypeError("options.getPlaylistPath must return or resolve to a string");if(/[\n\r"]/.test(n))throw new TypeError("Playlist paths cannot contain line feed, carriage return, or double quote characters.")},Gd=n=>{if(typeof n!="string")throw new TypeError("options.getSegmentPath must return or resolve to a string");if(/[\n\r"]/.test(n))throw new TypeError("Segment paths cannot contain line feed or carriage return characters.")},Up=n=>{if(typeof n!="string")throw new TypeError("options.getInitPath must return or resolve to a string");if(/[\n\r"]/.test(n))throw new TypeError("Init paths cannot contain line feed, carriage return, or double quote characters.")},js=n=>({n:n.id,tracks:n.tracks,segmentFormat:n.segmentFormat});var Re=class{getSupportedVideoCodecs(){return this.getSupportedCodecs().filter(t=>re.includes(t))}getSupportedAudioCodecs(){return this.getSupportedCodecs().filter(t=>ae.includes(t))}getSupportedSubtitleCodecs(){return this.getSupportedCodecs().filter(t=>Me.includes(t))}_codecUnsupportedHint(t){return""}},Li=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.fastStart!==void 0&&![!1,"in-memory","reserve","fragmented"].includes(t.fastStart))throw new TypeError("options.fastStart, when provided, must be false, 'in-memory', 'reserve', or 'fragmented'.");if(t.minimumFragmentDuration!==void 0&&(!Number.isFinite(t.minimumFragmentDuration)||t.minimumFragmentDuration<0))throw new TypeError("options.minimumFragmentDuration, when provided, must be a non-negative number.");if(t.onFtyp!==void 0&&typeof t.onFtyp!="function")throw new TypeError("options.onFtyp, when provided, must be a function.");if(t.onMoov!==void 0&&typeof t.onMoov!="function")throw new TypeError("options.onMoov, when provided, must be a function.");if(t.onMdat!==void 0&&typeof t.onMdat!="function")throw new TypeError("options.onMdat, when provided, must be a function.");if(t.onMoof!==void 0&&typeof t.onMoof!="function")throw new TypeError("options.onMoof, when provided, must be a function.");if(t.metadataFormat!==void 0&&!["mdir","mdta","udta","auto"].includes(t.metadataFormat))throw new TypeError("options.metadataFormat, when provided, must be either 'auto', 'mdir', 'mdta', or 'udta'.");super(),this._options=t}getSupportedTrackCounts(){return{video:{min:0,max:4294967295},audio:{min:0,max:4294967295},subtitle:{min:0,max:4294967295},total:{min:1,max:4294967295}}}get supportsVideoRotationMetadata(){return!0}get supportsTimestampedMediaData(){return!0}_createMuxer(t){return new zs(t,this)}},Wi=class extends Li{constructor(t){super(t)}get _name(){return"MP4"}get fileExtension(){return".mp4"}get mimeType(){return"video/mp4"}getSupportedCodecs(){return[...re,...wt,"pcm-s16","pcm-s16be","pcm-s24","pcm-s24be","pcm-s32","pcm-s32be","pcm-f32","pcm-f32be","pcm-f64","pcm-f64be",...Me]}_codecUnsupportedHint(t){return new Bi().getSupportedCodecs().includes(t)?" Switching to MOV will grant support for this codec.":""}},Mi=class extends Li{constructor(t){super(t)}get _name(){return"CMAF"}get fileExtension(){return".m4s"}get mimeType(){return"video/mp4"}getSupportedCodecs(){return[...re,...wt,"pcm-s16","pcm-s16be","pcm-s24","pcm-s24be","pcm-s32","pcm-s32be","pcm-f32","pcm-f32be","pcm-f64","pcm-f64be",...Me]}},Bi=class extends Li{constructor(t){super(t)}get _name(){return"MOV"}get fileExtension(){return".mov"}get mimeType(){return"video/quicktime"}getSupportedCodecs(){return[...re,...ae]}_codecUnsupportedHint(t){return new Wi().getSupportedCodecs().includes(t)?" Switching to MP4 will grant support for this codec.":""}},Vn=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.appendOnly!==void 0&&typeof t.appendOnly!="boolean")throw new TypeError("options.appendOnly, when provided, must be a boolean.");if(t.minimumClusterDuration!==void 0&&(!Number.isFinite(t.minimumClusterDuration)||t.minimumClusterDuration<0))throw new TypeError("options.minimumClusterDuration, when provided, must be a non-negative number.");if(t.onEbmlHeader!==void 0&&typeof t.onEbmlHeader!="function")throw new TypeError("options.onEbmlHeader, when provided, must be a function.");if(t.onSegmentHeader!==void 0&&typeof t.onSegmentHeader!="function")throw new TypeError("options.onHeader, when provided, must be a function.");if(t.onCluster!==void 0&&typeof t.onCluster!="function")throw new TypeError("options.onCluster, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Ns(t,this)}get _name(){return"Matroska"}getSupportedTrackCounts(){return{video:{min:0,max:127},audio:{min:0,max:127},subtitle:{min:0,max:127},total:{min:1,max:127}}}get fileExtension(){return".mkv"}get mimeType(){return"video/x-matroska"}getSupportedCodecs(){return[...re,...wt,...ee.filter(t=>!["pcm-s8","pcm-f32be","pcm-f64be","ulaw","alaw"].includes(t)),...Me]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!0}},Oi=class extends Vn{constructor(t){super(t)}getSupportedCodecs(){return[...re.filter(t=>["vp8","vp9","av1"].includes(t)),...ae.filter(t=>["opus","vorbis"].includes(t)),...Me]}get _name(){return"WebM"}get fileExtension(){return".webm"}get mimeType(){return"video/webm"}_codecUnsupportedHint(t){return new Vn().getSupportedCodecs().includes(t)?" Switching to MKV will grant support for this codec.":""}},Qc=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.xingHeader!==void 0&&typeof t.xingHeader!="boolean")throw new TypeError("options.xingHeader, when provided, must be a boolean.");if(t.onXingFrame!==void 0&&typeof t.onXingFrame!="function")throw new TypeError("options.onXingFrame, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Ws(t,this)}get _name(){return"MP3"}getSupportedTrackCounts(){return{video:{min:0,max:0},audio:{min:1,max:1},subtitle:{min:0,max:0},total:{min:1,max:1}}}get fileExtension(){return".mp3"}get mimeType(){return"audio/mpeg"}getSupportedCodecs(){return["mp3"]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!1}},jc=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.large!==void 0&&typeof t.large!="boolean")throw new TypeError("options.large, when provided, must be a boolean.");if(t.metadataFormat!==void 0&&!["info","id3"].includes(t.metadataFormat))throw new TypeError("options.metadataFormat, when provided, must be either 'info' or 'id3'.");if(t.onHeader!==void 0&&typeof t.onHeader!="function")throw new TypeError("options.onHeader, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Ks(t,this)}get _name(){return"WAVE"}getSupportedTrackCounts(){return{video:{min:0,max:0},audio:{min:1,max:1},subtitle:{min:0,max:0},total:{min:1,max:1}}}get fileExtension(){return".wav"}get mimeType(){return"audio/wav"}getSupportedCodecs(){return[...ee.filter(t=>["pcm-s16","pcm-s24","pcm-s32","pcm-f32","pcm-u8","ulaw","alaw"].includes(t))]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!1}},Xc=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.maximumPageDuration!==void 0&&(!Number.isFinite(t.maximumPageDuration)||t.maximumPageDuration<=0))throw new TypeError("options.maximumPageDuration, when provided, must be a positive number.");if(t.onPage!==void 0&&typeof t.onPage!="function")throw new TypeError("options.onPage, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Hs(t,this)}get _name(){return"Ogg"}getSupportedTrackCounts(){return{video:{min:0,max:0},audio:{min:0,max:4294967296},subtitle:{min:0,max:0},total:{min:1,max:4294967296}}}get fileExtension(){return".ogg"}get mimeType(){return"application/ogg"}getSupportedCodecs(){return[...ae.filter(t=>["vorbis","opus"].includes(t))]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!1}},$c=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.onFrame!==void 0&&typeof t.onFrame!="function")throw new TypeError("options.onFrame, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Fs(t,this)}get _name(){return"ADTS"}getSupportedTrackCounts(){return{video:{min:0,max:0},audio:{min:1,max:1},subtitle:{min:0,max:0},total:{min:1,max:1}}}get fileExtension(){return".aac"}get mimeType(){return"audio/aac"}getSupportedCodecs(){return["aac"]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!1}},Yc=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.appendOnly!==void 0&&typeof t.appendOnly!="boolean")throw new TypeError("options.appendOnly, when provided, must be a boolean.");super(),this._options=t}_createMuxer(t){return new Ms(t,this)}get _name(){return"FLAC"}getSupportedTrackCounts(){return{video:{min:0,max:0},audio:{min:1,max:1},subtitle:{min:0,max:0},total:{min:1,max:1}}}get fileExtension(){return".flac"}get mimeType(){return"audio/flac"}getSupportedCodecs(){return["flac"]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!1}},Zc=class extends Re{constructor(t={}){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.onPacket!==void 0&&typeof t.onPacket!="function")throw new TypeError("options.onPacket, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new qs(t,this)}get _name(){return"MPEG-TS"}getSupportedTrackCounts(){return{video:{min:0,max:16},audio:{min:0,max:32},subtitle:{min:0,max:0},total:{min:1,max:48}}}get fileExtension(){return".ts"}get mimeType(){return"video/MP2T"}getSupportedCodecs(){return[...re.filter(t=>["avc","hevc"].includes(t)),...ae.filter(t=>["aac","mp3","ac3","eac3"].includes(t))]}get supportsVideoRotationMetadata(){return!1}get supportsTimestampedMediaData(){return!0}},Jc=class extends Re{constructor(t){if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(!(t.segmentFormat instanceof Re)&&(!Array.isArray(t.segmentFormat)||t.segmentFormat.length===0||!t.segmentFormat.every(e=>e instanceof Re)))throw new TypeError("options.segmentFormat must be an OutputFormat or a non-empty array of OutputFormat instances.");if(t.targetDuration!==void 0&&(typeof t.targetDuration!="number"||t.targetDuration<=0))throw new TypeError("options.targetDuration, when provided, must be a positive number.");if(t.singleFilePerPlaylist!==void 0&&typeof t.singleFilePerPlaylist!="boolean")throw new TypeError("options.singleFilePerPlaylist, when provided, must be a boolean.");if(t.live!==void 0&&typeof t.live!="boolean")throw new TypeError("options.live, when provided, must be a boolean.");if(t.maxLiveSegmentCount!==void 0&&(typeof t.maxLiveSegmentCount!="number"||t.maxLiveSegmentCount<1||Number.isFinite(t.maxLiveSegmentCount)&&!Number.isInteger(t.maxLiveSegmentCount)))throw new TypeError("options.maxLiveSegmentCount, when provided, must be a positive integer or Infinity.");if(t.getPlaylistPath!==void 0&&typeof t.getPlaylistPath!="function")throw new TypeError("options.getPlaylistPath, when provided, must be a function.");if(t.getSegmentPath!==void 0&&typeof t.getSegmentPath!="function")throw new TypeError("options.getSegmentPath, when provided, must be a function.");if(t.getInitPath!==void 0&&typeof t.getInitPath!="function")throw new TypeError("options.getInitPath, when provided, must be a function.");if(t.onMaster!==void 0&&typeof t.onMaster!="function")throw new TypeError("options.onMaster, when provided, must be a function.");if(t.onPlaylist!==void 0&&typeof t.onPlaylist!="function")throw new TypeError("options.onPlaylist, when provided, must be a function.");if(t.onSegment!==void 0&&typeof t.onSegment!="function")throw new TypeError("options.onSegment, when provided, must be a function.");if(t.onInit!==void 0&&typeof t.onInit!="function")throw new TypeError("options.onInit, when provided, must be a function.");if(t.onSegmentPopped!==void 0&&typeof t.onSegmentPopped!="function")throw new TypeError("options.onSegmentPopped, when provided, must be a function.");super(),this._options=t}_createMuxer(t){return new Xs(t,this)}get _name(){return"HTTP Live Streaming (HLS)"}get fileExtension(){return".m3u8"}get mimeType(){return St}getSupportedCodecs(){return[...new Set(Dt(this._options.segmentFormat).flatMap(e=>e.getSupportedCodecs()))]}getSupportedTrackCounts(){let t=!1,e=!1,r=!1;for(let i of Dt(this._options.segmentFormat)){let a=i.getSupportedTrackCounts();t||=a.video.max>0,e||=a.audio.max>0,r||=a.subtitle.max>0}return{video:{min:0,max:t?1/0:0},audio:{min:0,max:e?1/0:0},subtitle:{min:0,max:0},total:{min:1,max:1/0}}}get supportsVideoRotationMetadata(){return Dt(this._options.segmentFormat).some(t=>t.supportsVideoRotationMetadata)}get supportsTimestampedMediaData(){return!0}_codecUnsupportedHint(t){return" Using different segment formats may grant support for this codec."}};var Kd=["video","audio","subtitle"],Hi=class n{constructor(t,e,r,i,a){this.id=t,this.output=e,this.type=r,this.source=i,this.metadata=a}isVideoTrack(){return this.type==="video"}isAudioTrack(){return this.type==="audio"}isSubtitleTrack(){return this.type==="subtitle"}canBePairedWith(t){if(!(t instanceof n))throw new TypeError("other must be an OutputTrack.");if(this===t)return!1;let e=Dt(this.metadata.group),r=Dt(t.metadata.group);for(let i of e)if(this.type!==t.type&&r.some(o=>i===o)||r.some(o=>i._pairedGroups.has(o)))return!0;return!1}},$s=class extends Hi{constructor(t,e,r,i){super(t,e,"video",r,i)}},Ys=class extends Hi{constructor(t,e,r,i){super(t,e,"audio",r,i)}},Zs=class extends Hi{constructor(t,e,r,i){super(t,e,"subtitle",r,i)}},We=class n{constructor(){this._pairedGroups=new Set}pairWith(t){if(!(t instanceof n))throw new TypeError("other must be an OutputTrackGroup.");if(this===t)throw new TypeError("Cannot pair a group with itself.");this._pairedGroups.add(t),t._pairedGroups.add(this)}},eu=n=>{if(!n||typeof n!="object")throw new TypeError("metadata must be an object.");if(n.languageCode!==void 0&&!Bt(n.languageCode))throw new TypeError("metadata.languageCode, when provided, must be a three-letter, ISO 639-2/T language code.");if(n.name!==void 0&&typeof n.name!="string")throw new TypeError("metadata.name, when provided, must be a string.");if(n.disposition!==void 0&&Ru(n.disposition),n.maximumPacketCount!==void 0&&(!Number.isInteger(n.maximumPacketCount)||n.maximumPacketCount<0))throw new TypeError("metadata.maximumPacketCount, when provided, must be a non-negative integer.");if(n.group!==void 0&&!(n.group instanceof We)&&(!Array.isArray(n.group)||n.group.some(t=>!(t instanceof We))))throw new TypeError("metadata.group, when provided, must be an OutputTrackGroup instance or an array of OutputTrackGroup instances.")},Ar=class extends Ye{constructor(e){super();this.state="pending";this.defaultTrackGroup=new We;this._onFinalize=null;this._unfinalizedTargets=new Set;this._rootWriterPromise=null;this._tracks=[];this._startPromise=null;this._cancelPromise=null;this._finalizePromise=null;this._mutex=new Ie;this._metadataTags={};this._rootTarget=null;this._rootTargetPromise=null;this._firstMediaStreamTimestamp=null;if(!e||typeof e!="object")throw new TypeError("options must be an object.");if(!(e.format instanceof Re))throw new TypeError("options.format must be an OutputFormat.");if(!(e.target instanceof Ce||e.target instanceof Le))throw new TypeError("options.target must be a Target or a PathedTarget.");if(e.target instanceof Ce&&this._rememberTarget(e.target),e.initTarget!==void 0&&!(e.initTarget instanceof Ce)&&typeof e.initTarget!="function")throw new Error("options.initTarget, when provided, must be a Target or a function that returns or resolves to a Target.");if(e.onFinalize!==void 0&&typeof e.onFinalize!="function")throw new TypeError("options.onFinalize, when provided, must be a function.");this.format=e.format,this._target=e.target,this._onFinalize=e.onFinalize??null,this._initTarget=e.initTarget??null,this._initTarget instanceof Ce&&this._rememberTarget(this._initTarget),this._muxer=e.format._createMuxer(this)}get target(){let e="Output.target cannot be used when using PathedTarget with an async callback. Use the 'target' event instead.";if(this._rootTargetPromise)throw new TypeError(e);let r=this._getRootTarget();if(r instanceof Promise)throw new TypeError(e);return r}_getTargetValidated(e){h(this._target instanceof Le);let r=this._target.getTarget(e),i=a=>{if(!(a instanceof Ce))throw new TypeError("getTarget must return a Target.");return a};return r instanceof Promise?r.then(i):i(r)}async _getTarget(e){h(this._target instanceof Le);let r=await this._getTargetValidated(e);return this._emit("target",{target:r,request:e,isRoot:e.isRoot}),this.state==="canceled"?await r._close():this._rememberTarget(r),r}_rememberTarget(e){this._unfinalizedTargets.add(e),e.on("finalized",()=>this._unfinalizedTargets.delete(e),{once:!0})}async _getInitTarget(){if(h(this._initTarget!==null),this._initTarget instanceof Ce)return this._initTarget;let e=await this._initTarget();return this.state==="canceled"?await e._close():this._rememberTarget(e),e}_hasInitTarget(){return this._initTarget!==null}_getRootTarget(){if(this._rootTarget)return this._rootTarget;if(this._rootTargetPromise)return this._rootTargetPromise;if(this._target instanceof Ce)return this._emit("target",{target:this._target,request:null,isRoot:!0}),this._rootTarget=this._target,this._target;let e={path:this._target.rootPath,isRoot:!0,mimeType:this.format.mimeType},r=this._getTargetValidated(e),i=a=>(this.state==="canceled"?a._close():this._rememberTarget(a),this._emit("target",{target:a,request:e,isRoot:!0}),this._rootTarget=a,a);return r instanceof Promise?this._rootTargetPromise=r.then(i):i(r)}_getRootWriter(e){return this._rootWriterPromise??=(async()=>{let r=await this._getRootTarget(),i=new Et(r,typeof e=="boolean"?e:e(r));return i.start(),i})()}addVideoTrack(e,r={}){if(!(e instanceof Jt))throw new TypeError("source must be a VideoSource.");if(eu(r),r.rotation!==void 0&&![0,90,180,270].includes(r.rotation))throw new TypeError(`Invalid video rotation: ${r.rotation}. Has to be 0, 90, 180 or 270.`);if(!this.format.supportsVideoRotationMetadata&&r.rotation)throw new Error(`${this.format._name} does not support video rotation metadata.`);if(r.frameRate!==void 0&&(!Number.isFinite(r.frameRate)||r.frameRate<=0))throw new TypeError(`Invalid video frame rate: ${r.frameRate}. Must be a positive number.`);let i={...r};return i.group??=this.defaultTrackGroup,this._addTrack(new $s(this._tracks.length+1,this,e,i))}addAudioTrack(e,r={}){if(!(e instanceof er))throw new TypeError("source must be an AudioSource.");eu(r);let i={...r};return i.group??=this.defaultTrackGroup,this._addTrack(new Ys(this._tracks.length+1,this,e,i))}addSubtitleTrack(e,r={}){if(!(e instanceof Ni))throw new TypeError("source must be a SubtitleSource.");eu(r);let i={...r};return i.group??=this.defaultTrackGroup,this._addTrack(new Zs(this._tracks.length+1,this,e,i))}setMetadataTags(e){if(Yi(e),this.state!=="pending")throw new Error("Cannot set metadata tags after output has been started or canceled.");this._metadataTags=e}_addTrack(e){if(this.state!=="pending")throw new Error("Cannot add track after output has been started or canceled.");if(e.source._connectedTrack)throw new Error("Source is already used for a track.");let r=this.format.getSupportedTrackCounts(),i=this._tracks.reduce((o,c)=>o+(c.type===e.type?1:0),0),a=r[e.type].max;if(i===a)throw new Error(a===0?`${this.format._name} does not support ${e.type} tracks.`:`${this.format._name} does not support more than ${a} ${e.type} track${a===1?"":"s"}.`);let s=r.total.max;if(this._tracks.length===s)throw new Error(`${this.format._name} does not support more than ${s} tracks${s===1?"":"s"} in total.`);if(e.isVideoTrack()){let o=this.format.getSupportedVideoCodecs();if(o.length===0)throw new Error(`${this.format._name} does not support video tracks.`+this.format._codecUnsupportedHint(e.source._codec));if(!o.includes(e.source._codec))throw new Error(`Codec '${e.source._codec}' cannot be contained within ${this.format._name}. Supported video codecs are: ${o.map(c=>`'${c}'`).join(", ")}.`+this.format._codecUnsupportedHint(e.source._codec))}else if(e.isAudioTrack()){let o=this.format.getSupportedAudioCodecs();if(o.length===0)throw new Error(`${this.format._name} does not support audio tracks.`+this.format._codecUnsupportedHint(e.source._codec));if(!o.includes(e.source._codec))throw new Error(`Codec '${e.source._codec}' cannot be contained within ${this.format._name}. Supported audio codecs are: ${o.map(c=>`'${c}'`).join(", ")}.`+this.format._codecUnsupportedHint(e.source._codec))}else if(e.isSubtitleTrack()){let o=this.format.getSupportedSubtitleCodecs();if(o.length===0)throw new Error(`${this.format._name} does not support subtitle tracks.`+this.format._codecUnsupportedHint(e.source._codec));if(!o.includes(e.source._codec))throw new Error(`Codec '${e.source._codec}' cannot be contained within ${this.format._name}. Supported subtitle codecs are: ${o.map(c=>`'${c}'`).join(", ")}.`+this.format._codecUnsupportedHint(e.source._codec))}return this._tracks.push(e),e.source._connectedTrack=e,e}async start(){let e=this.format.getSupportedTrackCounts();for(let i of Kd){let a=this._tracks.reduce((o,c)=>o+(c.type===i?1:0),0),s=e[i].min;if(a<s)throw new Error(s===e[i].max?`${this.format._name} requires exactly ${s} ${i} track${s===1?"":"s"}.`:`${this.format._name} requires at least ${s} ${i} track${s===1?"":"s"}.`)}let r=e.total.min;if(this._tracks.length<r)throw new Error(r===e.total.max?`${this.format._name} requires exactly ${r} track${r===1?"":"s"}.`:`${this.format._name} requires at least ${r} track${r===1?"":"s"}.`);if(this.state==="canceled")throw new Error("Output has been canceled.");return this._startPromise?(console.warn("Output has already been started."),this._startPromise):this._startPromise=(async()=>{this.state="started";let i=await this._mutex.acquire();try{await this._muxer.start();let a=this._tracks.map(s=>s.source._start());await Promise.all(a)}finally{i()}})()}getMimeType(){return this._muxer.getMimeType()}async cancel(){if(this._cancelPromise)return console.warn("Output has already been canceled."),this._cancelPromise;if(this.state==="finalizing"||this.state==="finalized"){this.state==="finalized"&&console.warn("Output has already been finalized.");return}return this._cancelPromise=(async()=>{this.state="canceled";let e=await this._mutex.acquire();try{let r=this._tracks.map(i=>i.source._flushOrWaitForOngoingClose(!0));await Promise.all(r),await Promise.all([...this._unfinalizedTargets].map(i=>i._close())),this._unfinalizedTargets.clear()}finally{e()}})()}async finalize(){if(this.state==="pending")throw new Error("Cannot finalize before starting.");if(this.state==="canceled")throw new Error("Cannot finalize after canceling.");return this._finalizePromise?(console.warn("Output has already been finalized."),this._finalizePromise):this._finalizePromise=(async()=>{this.state="finalizing";let e=await this._mutex.acquire();try{let r=this._tracks.map(i=>i.source._flushOrWaitForOngoingClose(!1));if(await Promise.all(r),await this._muxer.finalize(),this._rootWriterPromise){let i=await this._rootWriterPromise;i.finalized||(await i.flush(),await i.finalize())}this._onFinalize&&await this._onFinalize(),this.state="finalized"}finally{e()}})()}};var Js=n=>{if(!n||typeof n!="object")throw new TypeError("options.video, when provided, must be an object.");if(n?.discard!==void 0&&typeof n.discard!="boolean")throw new TypeError("options.video.discard, when provided, must be a boolean.");if(n?.forceTranscode!==void 0&&typeof n.forceTranscode!="boolean")throw new TypeError("options.video.forceTranscode, when provided, must be a boolean.");if(n?.codec!==void 0&&!re.includes(n.codec))throw new TypeError(`options.video.codec, when provided, must be one of: ${re.join(", ")}.`);if(n?.bitrate!==void 0&&!(n.bitrate instanceof Pe)&&(!Number.isInteger(n.bitrate)||n.bitrate<=0))throw new TypeError("options.video.bitrate, when provided, must be a positive integer or a quality.");if(n?.width!==void 0&&(!Number.isInteger(n.width)||n.width<=0))throw new TypeError("options.video.width, when provided, must be a positive integer.");if(n?.height!==void 0&&(!Number.isInteger(n.height)||n.height<=0))throw new TypeError("options.video.height, when provided, must be a positive integer.");if(n?.fit!==void 0&&!["fill","contain","cover"].includes(n.fit))throw new TypeError("options.video.fit, when provided, must be one of 'fill', 'contain', or 'cover'.");if(n?.width!==void 0&&n.height!==void 0&&n.fit===void 0)throw new TypeError("When both options.video.width and options.video.height are provided, options.video.fit must also be provided.");if(n?.rotate!==void 0&&![0,90,180,270].includes(n.rotate))throw new TypeError("options.video.rotate, when provided, must be 0, 90, 180 or 270.");if(n?.allowRotationMetadata!==void 0&&typeof n.allowRotationMetadata!="boolean")throw new TypeError("options.video.allowRotationMetadata, when provided, must be a boolean.");if(n?.crop!==void 0&&kr(n.crop,"options.video."),n?.frameRate!==void 0&&(!Number.isFinite(n.frameRate)||n.frameRate<=0))throw new TypeError("options.video.frameRate, when provided, must be a finite positive number.");if(n?.alpha!==void 0&&!["discard","keep"].includes(n.alpha))throw new TypeError("options.video.alpha, when provided, must be either 'discard' or 'keep'.");if(n?.keyFrameInterval!==void 0&&(!Number.isFinite(n.keyFrameInterval)||n.keyFrameInterval<0))throw new TypeError("options.video.keyFrameInterval, when provided, must be a non-negative number.");if(n?.process!==void 0&&typeof n.process!="function")throw new TypeError("options.video.process, when provided, must be a function.");if(n?.processedWidth!==void 0&&(!Number.isInteger(n.processedWidth)||n.processedWidth<=0))throw new TypeError("options.video.processedWidth, when provided, must be a positive integer.");if(n?.processedHeight!==void 0&&(!Number.isInteger(n.processedHeight)||n.processedHeight<=0))throw new TypeError("options.video.processedHeight, when provided, must be a positive integer.");if(n?.hardwareAcceleration!==void 0&&!["no-preference","prefer-hardware","prefer-software"].includes(n.hardwareAcceleration))throw new TypeError("options.video.hardwareAcceleration, when provided, must be 'no-preference', 'prefer-hardware' or 'prefer-software'.");if(n?.group!==void 0&&!(n.group instanceof We||Array.isArray(n.group)&&n.group.every(t=>t instanceof We)))throw new TypeError("options.video.group, when provided, must be an OutputTrackGroup or an array of OutputTrackGroups.")},eo=n=>{if(!n||typeof n!="object")throw new TypeError("options.audio, when provided, must be an object.");if(n?.discard!==void 0&&typeof n.discard!="boolean")throw new TypeError("options.audio.discard, when provided, must be a boolean.");if(n?.forceTranscode!==void 0&&typeof n.forceTranscode!="boolean")throw new TypeError("options.audio.forceTranscode, when provided, must be a boolean.");if(n?.codec!==void 0&&!ae.includes(n.codec))throw new TypeError(`options.audio.codec, when provided, must be one of: ${ae.join(", ")}.`);if(n?.bitrate!==void 0&&!(n.bitrate instanceof Pe)&&(!Number.isInteger(n.bitrate)||n.bitrate<=0))throw new TypeError("options.audio.bitrate, when provided, must be a positive integer or a quality.");if(n?.numberOfChannels!==void 0&&(!Number.isInteger(n.numberOfChannels)||n.numberOfChannels<=0))throw new TypeError("options.audio.numberOfChannels, when provided, must be a positive integer.");if(n?.sampleRate!==void 0&&(!Number.isInteger(n.sampleRate)||n.sampleRate<=0))throw new TypeError("options.audio.sampleRate, when provided, must be a positive integer.");if(n?.sampleFormat!==void 0&&!["u8","s16","s32","f32"].includes(n.sampleFormat))throw new TypeError("options.audio.sampleFormat, when provided, must be one of: u8, s16, s32, f32.");if(n?.process!==void 0&&typeof n.process!="function")throw new TypeError("options.audio.process, when provided, must be a function.");if(n?.processedNumberOfChannels!==void 0&&(!Number.isInteger(n.processedNumberOfChannels)||n.processedNumberOfChannels<=0))throw new TypeError("options.audio.processedNumberOfChannels, when provided, must be a positive integer.");if(n?.processedSampleRate!==void 0&&(!Number.isInteger(n.processedSampleRate)||n.processedSampleRate<=0))throw new TypeError("options.audio.processedSampleRate, when provided, must be a positive integer.");if(n?.group!==void 0&&!(n.group instanceof We||Array.isArray(n.group)&&n.group.every(t=>t instanceof We)))throw new TypeError("options.audio.group, when provided, must be an OutputTrackGroup or an array of OutputTrackGroups.")},tu=2,ru=48e3,iu=class n{constructor(t){this._addedCounts={video:0,audio:0,subtitle:0};this._totalTrackCount=0;this._nextOutputTrackId=0;this._outputTrackIds=[];this._outputOwnTrackGroups=[];this._trackPromises=[];this._executed=!1;this._synchronizer=new nu;this._totalDuration=null;this._maxTimestamps=new Map;this._canceled=!1;this.onProgress=void 0;this._computeProgress=!1;this._lastProgress=0;this.isValid=!1;this.utilizedTracks=[];this.discardedTracks=[];if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(!(t.input instanceof Lr))throw new TypeError("options.input must be an Input.");if(!(t.output instanceof Ar))throw new TypeError("options.output must be an Output.");if(t.tracks!==void 0&&t.tracks!=="all"&&t.tracks!=="primary")throw new TypeError("options.tracks, when provided, must be either 'all' or 'primary'.");if(t.output._tracks.length>0||Object.keys(t.output._metadataTags).length>0||t.output.state!=="pending")throw new TypeError("options.output must be fresh: no tracks or metadata tags added and not started.");if(t.video!==void 0&&typeof t.video!="function")if(Array.isArray(t.video))for(let i of t.video)Js(i);else Js(t.video);if(t.audio!==void 0&&typeof t.audio!="function")if(Array.isArray(t.audio))for(let i of t.audio)eo(i);else eo(t.audio);if(t.trim!==void 0&&(!t.trim||typeof t.trim!="object"))throw new TypeError("options.trim, when provided, must be an object.");if(t.trim?.start!==void 0&&!Number.isFinite(t.trim.start))throw new TypeError("options.trim.start, when provided, must be a finite number.");if(t.trim?.end!==void 0&&!Number.isFinite(t.trim.end))throw new TypeError("options.trim.end, when provided, must be a finite number.");if(t.trim?.start!==void 0&&t.trim.end!==void 0&&t.trim.start>=t.trim.end)throw new TypeError("options.trim.start must be less than options.trim.end.");if(t.tags!==void 0&&(typeof t.tags!="object"||!t.tags)&&typeof t.tags!="function")throw new TypeError("options.tags, when provided, must be an object or a function.");if(typeof t.tags=="object"&&Yi(t.tags),t.showWarnings!==void 0&&typeof t.showWarnings!="boolean")throw new TypeError("options.showWarnings, when provided, must be a boolean.");this._options=t,this.input=t.input,this.output=t.output;let{promise:e,resolve:r}=Z();this._started=e,this._start=r}static async init(t){let e=new n(t);return await e._init(),e}async _init(){let t=await this.input.getFormat(),e,r=this._options.tracks;if(r===void 0&&(r=t.name.includes("(HLS)")?"primary":"all"),r==="all")e=await this.input.getTracks();else if(r==="primary"){let f=await this.input.getPrimaryVideoTrack(),p=await this.input.getPrimaryAudioTrack();e=[f,p].filter(b=>b!==null)}else ne(r),h(!1);let i=this.output.format.getSupportedTrackCounts(),a=1,s=1,o=[],c=[];for(let f of e){let p;if(f.isVideoTrack())if(this._options.video)if(typeof this._options.video=="function"){let y=await this._options.video(f,a)??{};if(Array.isArray(y))for(let k of y)Js(k);else Js(y);p=Array.isArray(y)?y:[y],a++}else p=Array.isArray(this._options.video)?this._options.video:[this._options.video];else p=[{}];else if(f.isAudioTrack())if(this._options.audio)if(typeof this._options.audio=="function"){let y=await this._options.audio(f,s)??{};if(Array.isArray(y))for(let k of y)eo(k);else eo(y);p=Array.isArray(y)?y:[y],s++}else p=Array.isArray(this._options.audio)?this._options.audio:[this._options.audio];else p=[{}];else h(!1);let b=p.filter(y=>y.discard);for(let y of b)this.discardedTracks.push({track:f,reason:"discarded_by_user",trackOptions:y});if(p.length===b.length){p.length===0&&this.discardedTracks.push({track:f,reason:"discarded_by_user",trackOptions:{}});continue}let g=p.filter(y=>!y.discard);o.push(f),c.push(g)}this._options.trim?.start!==void 0?this._startTimestamp=this._options.trim.start:this._startTimestamp=Math.max(await this.input.getFirstTimestamp(o),0),this._endTimestamp=Math.max(this._options.trim?.end??1/0,this._startTimestamp);for(let f=0;f<o.length;f++){let p=o[f],b=c[f];for(let g of b){if(this._totalTrackCount===i.total.max){this.discardedTracks.push({track:p,reason:"max_track_count_reached",trackOptions:g});continue}if(this._addedCounts[p.type]===i[p.type].max){this.discardedTracks.push({track:p,reason:"max_track_count_of_type_reached",trackOptions:g});continue}let y=this._nextOutputTrackId++;p.isVideoTrack()?await this._processVideoTrack(p,g,y):p.isAudioTrack()?await this._processAudioTrack(p,g,y):h(!1)}}for(let f=0;f<this.utilizedTracks.length-1;f++)for(let p=f+1;p<this.utilizedTracks.length;p++){let b=this.utilizedTracks[f],g=this.utilizedTracks[p],y=this._outputOwnTrackGroups[f],k=this._outputOwnTrackGroups[p];h(y!==void 0),h(k!==void 0),y&&k&&b.canBePairedWith(g)&&y.pairWith(k)}let u=await this.input.getMetadataTags(),l;if(this._options.tags){let f=typeof this._options.tags=="function"?await this._options.tags(u):this._options.tags;Yi(f),l=f}else l=u;let m=t.mimeType===this.output.format.mimeType,d=u.raw===l.raw;if(u.raw&&d&&!m&&delete l.raw,this.output.setMetadataTags(l),this.isValid=this._totalTrackCount>=i.total.min&&this._addedCounts.video>=i.video.min&&this._addedCounts.audio>=i.audio.min&&this._addedCounts.subtitle>=i.subtitle.min,this._options.showWarnings??!0){let f=[],p=this.discardedTracks.filter(b=>b.reason!=="discarded_by_user");p.length>0&&f.push("Some tracks had to be discarded from the conversion:",p),this.isValid||(f.length>0&&f.push(`
126
126
 
127
127
  `),f.push(this._getInvalidityExplanation().join(""))),f.length>0&&console.warn(...f)}}_getInvalidityExplanation(){let t=[];if(this.discardedTracks.length===0)t.push("Due to missing tracks, this conversion cannot be executed.");else{let e=this.discardedTracks.every(r=>r.reason==="discarded_by_user"||r.reason==="no_encodable_target_codec")&&this.discardedTracks.some(r=>r.reason==="no_encodable_target_codec");if(t.push("Due to discarded tracks, this conversion cannot be executed."),e){let r=this.discardedTracks.flatMap(a=>a.reason==="discarded_by_user"?[]:a.track.type==="video"?this.output.format.getSupportedVideoCodecs():a.track.type==="audio"?this.output.format.getSupportedAudioCodecs():this.output.format.getSupportedSubtitleCodecs()),i=[...new Set(r)];i.length===1?t.push(`
128
128
  Tracks were discarded because your environment is not able to encode '${i[0]}'.`):t.push(`
@@ -132,5 +132,5 @@ The @mediabunny/aac-encoder extension package provides support for encoding AAC.
132
132
  The @mediabunny/ac3 extension package provides support for encoding and decoding AC-3/E-AC-3.`),i.includes("flac")&&t.push(`
133
133
  The @mediabunny/flac-encoder extension package provides support for encoding FLAC.`)}else t.push(`
134
134
  Check the discardedTracks field for more info.`)}return t}async execute(){if(!this.isValid)throw new Error(`Cannot execute this conversion because its output configuration is invalid. Make sure to always check the isValid field before executing a conversion.
135
- `+this._getInvalidityExplanation().join(""));if(this._executed)throw new Error("Conversion cannot be executed twice.");this._executed=!0;for(let t of this._outputTrackIds)this._synchronizer.declareTrack(t);if(this.onProgress){let e=[...new Set(this.utilizedTracks)].map(async i=>await i.isLive()?1/0:await i.getDurationFromMetadata()??await i.computeDuration()),r=Math.max(0,...await Promise.all(e));this._computeProgress=!0,this._totalDuration=Math.min(r-this._startTimestamp,this._endTimestamp-this._startTimestamp);for(let i of this._outputTrackIds)this._maxTimestamps.set(i,0);this.onProgress?.(0,0)}await this.output.start(),this._start();try{await Promise.all(this._trackPromises)}catch(t){throw this._canceled||this.cancel(),t}if(this._canceled)throw new to;if(await this.output.finalize(),this._computeProgress){let t=Math.min(...this._maxTimestamps.values());this.onProgress?.(1,t)}}async cancel(){if(!(this.output.state==="finalizing"||this.output.state==="finalized")){if(this._canceled){console.warn("Conversion already canceled.");return}this._canceled=!0,await this.output.cancel()}}async _processVideoTrack(t,e,r){let i=await t.getCodec();if(!i){this.discardedTracks.push({track:t,reason:"unknown_source_codec",trackOptions:e});return}let a,s=await t.getRotation(),o=Ze(s+(e.rotate??0)),c=o,u=this.output.format.supportsVideoRotationMetadata&&(e.allowRotationMetadata??!0),l=await t.getSquarePixelWidth(),m=await t.getSquarePixelHeight(),[d,f]=o%180===0?[l,m]:[m,l],p=e.crop;p&&(p=Pi(p,d,f));let[b,g]=p?[p.width,p.height]:[d,f],y=b,k=g,T=y/k;e.width!==void 0&&e.height===void 0?(y=Fr(e.width),k=Fr(Math.round(y/T))):e.width===void 0&&e.height!==void 0?(k=Fr(e.height),y=Fr(Math.round(k*T))):e.width!==void 0&&e.height!==void 0&&(y=Fr(e.width),k=Fr(e.height));let w=await t.getFirstTimestamp(),S=this.output.format.getSupportedVideoCodecs(),x=!!e.forceTranscode||w<this._startTimestamp||!!e.frameRate||e.keyFrameInterval!==void 0||e.process!==void 0||e.bitrate!==void 0||!S.includes(i)||e.codec&&e.codec!==i||y!==b||k!==g||o!==0&&!u||!!p,P=e.alpha??"discard";if(x){if(!await t.canDecode()){this.discardedTracks.push({track:t,reason:"undecodable_source_codec",trackOptions:e});return}e.codec&&(S=S.filter(z=>z===e.codec));let R=e.bitrate??ws,v=await hc(S,{width:e.process&&e.processedWidth?e.processedWidth:y,height:e.process&&e.processedHeight?e.processedHeight:k,bitrate:R});if(!v){this.discardedTracks.push({track:t,reason:"no_encodable_target_codec",trackOptions:e});return}let E={codec:v,bitrate:R,keyFrameInterval:e.keyFrameInterval,sizeChangeBehavior:e.fit??"passThrough",alpha:P,hardwareAcceleration:e.hardwareAcceleration,transform:{}};h(E.transform);let D=y!==b||k!==g||o!==0&&(!u||e.process!==void 0)||!!p||l!==await t.getCodedWidth()||m!==await t.getCodedHeight();if(!D){let z=new Sr({format:new Wi,target:new Xr}),q=new Ui(E);z.addVideoTrack(q),await z.start();let Te=await new Hr(t).getSample(w);if(Te)try{await q.add(Te),Te.close(),await z.finalize()}catch(Un){console.info("Error when probing encoder support. Falling back to rerender path.",Un),D=!0,z.cancel()}else await z.cancel()}e.frameRate&&(E.transform.frameRate=e.frameRate),D&&(c=0,E.transform.width=y,E.transform.height=k,E.transform.fit=e.fit??"fill",E.transform.rotate=Ze(o-s),E.transform.crop=p,E.transform.alpha=P);let U=new Ui(E);a=U,this._trackPromises.push((async()=>{await this._started;let z=new Hr(t);for await(let q of z.samples(this._startTimestamp,this._endTimestamp)){if(this._canceled){q.close();return}let ke=Math.max(q.timestamp-this._startTimestamp,0);q.setTimestamp(ke),await this._registerVideoSample(e,r,U,q),q.close()}U.close(),this._synchronizer.closeTrack(r)})())}else{let I=new Yr(i);a=I,this._trackPromises.push((async()=>{await this._started;let R=new xt(t),E={decoderConfig:await t.getDecoderConfig()??void 0};for await(let D of R.packets(void 0,void 0,{verifyKeyPackets:!0})){if(this._canceled)return;if(D.timestamp>=this._endTimestamp)break;let U=D.clone({timestamp:D.timestamp-this._startTimestamp,sideData:P==="discard"?{}:D.sideData});h(U.timestamp>=0),this._reportProgress(r,U.timestamp+U.duration),await I.add(U,E),this._synchronizer.shouldWait(r,U.timestamp)&&await this._synchronizer.wait(U.timestamp)}I.close(),this._synchronizer.closeTrack(r)})())}let A=null;e.group||(A=new We);let C=await t.getLanguageCode();this.output.addVideoTrack(a,{frameRate:e.frameRate,languageCode:Bt(C)?C:void 0,name:await t.getName()??void 0,disposition:await t.getDisposition(),rotation:c,group:A??e.group}),this._addedCounts.video++,this._totalTrackCount++,this.utilizedTracks.push(t),this._outputTrackIds.push(r),this._outputOwnTrackGroups.push(A)}async _registerVideoSample(t,e,r,i){if(this._canceled)return;this._reportProgress(e,i.timestamp+i.duration);let a;if(!t.process)a=[i];else{let s=t.process(i);s instanceof Promise&&(s=await s),Array.isArray(s)||(s=s===null?[]:[s]),a=s.map(o=>o instanceof Ae?o:typeof VideoFrame<"u"&&o instanceof VideoFrame?new Ae(o):new Ae(o,{timestamp:i.timestamp,duration:i.duration}))}try{for(let s of a){if(this._canceled)break;await r.add(s),this._synchronizer.shouldWait(e,s.timestamp)&&await this._synchronizer.wait(s.timestamp)}}finally{for(let s of a)s!==i&&s.close()}}async _processAudioTrack(t,e,r){let i=await t.getCodec();if(!i){this.discardedTracks.push({track:t,reason:"unknown_source_codec",trackOptions:e});return}let a,s=await t.getNumberOfChannels(),o=await t.getSampleRate(),c=await t.getFirstTimestamp(),u=e.numberOfChannels??s,l=e.sampleRate??o,m=u!==s||l!==o||c<this._startTimestamp||c>this._startTimestamp&&!this.output.format.supportsTimestampedMediaData,d=this.output.format.getSupportedAudioCodecs();if(!e.forceTranscode&&!e.bitrate&&!m&&d.includes(i)&&(!e.codec||e.codec===i)&&!e.process&&e.sampleFormat===void 0){let b=new Zr(i);a=b,this._trackPromises.push((async()=>{await this._started;let g=new xt(t),k={decoderConfig:await t.getDecoderConfig()??void 0};for await(let T of g.packets()){if(this._canceled)return;if(T.timestamp>=this._endTimestamp)break;let w=T.clone({timestamp:T.timestamp-this._startTimestamp});h(w.timestamp>=0),this._reportProgress(r,w.timestamp+w.duration),await b.add(w,k),this._synchronizer.shouldWait(r,w.timestamp)&&await this._synchronizer.wait(w.timestamp)}b.close(),this._synchronizer.closeTrack(r)})())}else{if(!await t.canDecode()){this.discardedTracks.push({track:t,reason:"undecodable_source_codec",trackOptions:e});return}let g=null;e.codec&&(d=d.filter(T=>T===e.codec));let y=e.bitrate??ws,k=await En(d,{numberOfChannels:e.process&&e.processedNumberOfChannels?e.processedNumberOfChannels:u,sampleRate:e.process&&e.processedSampleRate?e.processedSampleRate:l,bitrate:y});if(!k.some(T=>wt.includes(T))&&d.some(T=>wt.includes(T))&&(u!==tu||l!==ru)){let w=(await En(d,{numberOfChannels:tu,sampleRate:ru,bitrate:y})).find(S=>wt.includes(S));w&&(m=!0,g=w,u=tu,l=ru)}else g=k[0]??null;if(g===null){this.discardedTracks.push({track:t,reason:"no_encodable_target_codec",trackOptions:e});return}if(m)a=this._resampleAudio(t,e,r,g,u,l,y);else{let T=new zi({codec:g,bitrate:y});a=T,this._trackPromises.push((async()=>{await this._started;let w=new qr(t);for await(let S of w.samples(void 0,this._endTimestamp)){if(this._canceled){S.close();return}S.setTimestamp(S.timestamp-this._startTimestamp),await this._registerAudioSample(e,r,T,S),S.close()}T.close(),this._synchronizer.closeTrack(r)})())}}let f=null;e.group||(f=new We);let p=await t.getLanguageCode();this.output.addAudioTrack(a,{languageCode:Bt(p)?p:void 0,name:await t.getName()??void 0,disposition:await t.getDisposition(),group:f??e.group}),this._addedCounts.audio++,this._totalTrackCount++,this.utilizedTracks.push(t),this._outputTrackIds.push(r),this._outputOwnTrackGroups.push(f)}async _registerAudioSample(t,e,r,i){if(this._canceled)return;let a=i;t.sampleFormat!==void 0&&hs(a.format)!==t.sampleFormat&&(a=gs(a,t.sampleFormat)),this._reportProgress(e,a.timestamp+a.duration);let s;if(!t.process)s=[a];else{let o=t.process(a);if(o instanceof Promise&&(o=await o),Array.isArray(o)||(o=o===null?[]:[o]),!o.every(c=>c instanceof ye))throw new TypeError("The audio process function must return an AudioSample, null, or an array of AudioSamples.");s=o}try{for(let o of s){if(this._canceled)break;await r.add(o),this._synchronizer.shouldWait(e,o.timestamp)&&await this._synchronizer.wait(o.timestamp)}}finally{a!==i&&a.close();for(let o of s)o!==i&&o.close()}}_resampleAudio(t,e,r,i,a,s,o){let c=new zi({codec:i,bitrate:o});return this._trackPromises.push((async()=>{await this._started;let u=new Di({targetNumberOfChannels:a,targetSampleRate:s,startTime:this._startTimestamp,endTime:this._endTimestamp,onSample:async d=>{h(d.timestamp>=this._startTimestamp),d.setTimestamp(d.timestamp-this._startTimestamp),await this._registerAudioSample(e,r,c,d),d.close()}}),m=new qr(t).samples(this._startTimestamp,this._endTimestamp);for await(let d of m){if(this._canceled){d.close();return}await u.add(d),d.close()}await u.finalize(),c.close(),this._synchronizer.closeTrack(r)})()),c}_reportProgress(t,e){if(!this._computeProgress)return;h(this._totalDuration!==null),this._maxTimestamps.set(t,Math.max(e,this._maxTimestamps.get(t)));let r=Math.min(...this._maxTimestamps.values()),i=ue(r/this._totalDuration,0,1);i!==this._lastProgress&&(this._lastProgress=i,this.onProgress?.(i,r))}},to=class extends Error{constructor(t="Conversion has been canceled."){super(t),this.name="ConversionCanceledError"}},Qd=1,nu=class{constructor(){this.maxTimestamps=new Map;this.resolvers=[]}declareTrack(t){this.maxTimestamps.set(t,0)}shouldWait(t,e){let r=this.maxTimestamps.get(t);h(r!==void 0),this.maxTimestamps.set(t,Math.max(e,r));let i=this.computeMinAndMaybeResolve();return e-i>Qd}wait(t){let{promise:e,resolve:r}=Z();return this.resolvers.push({timestamp:t,resolve:r}),e}closeTrack(t){this.maxTimestamps.delete(t),this.computeMinAndMaybeResolve()}computeMinAndMaybeResolve(){let t=1/0;for(let[,e]of this.maxTimestamps)t=Math.min(t,e);for(let e=0;e<this.resolvers.length;e++){let r=this.resolvers[e];r.timestamp-t<Qd&&(r.resolve(),this.resolvers.splice(e,1),e--)}return t}};var jd=Symbol.for("mediabunny loaded");globalThis[jd]&&console.error(`[WARNING]
136
- Mediabunny was loaded twice. This will likely cause Mediabunny not to work correctly. Check if multiple dependencies are importing different versions of Mediabunny, or if something is being bundled incorrectly.`);globalThis[jd]=!0;export{oc as ADTS,Vm as ALL_FORMATS,Kd as ALL_TRACK_TYPES,ae as AUDIO_CODECS,cs as AdtsInputFormat,$c as AdtsOutputFormat,Dc as AppendOnlyStreamTarget,ar as AttachedFile,Sc as AudioBufferSink,qc as AudioBufferSource,ye as AudioSample,br as AudioSampleResource,qr as AudioSampleSink,zi as AudioSampleSource,er as AudioSource,_n as BaseMediaSampleSink,Yo as BlobSource,$o as BufferSource,jr as BufferTarget,kc as CanvasSink,Wc as CanvasSource,Mi as CmafOutputFormat,so as ConcurrentRunner,iu as Conversion,to as ConversionCanceledError,Cs as CustomAudioDecoder,Es as CustomAudioEncoder,yn as CustomPathedSource,Ps as CustomVideoDecoder,Is as CustomVideoEncoder,Zr as EncodedAudioPacketSource,Q as EncodedPacket,xt as EncodedPacketSink,Yr as EncodedVideoPacketSource,Ye as EventEmitter,Kl as FLAC,Jo as FilePathSource,Vc as FilePathTarget,os as FlacInputFormat,Yc as FlacOutputFormat,uc as HLS,Um as HLS_FORMATS,Si as HlsInputFormat,Jc as HlsOutputFormat,Nr as Input,Yt as InputAudioTrack,be as InputDisposedError,Ve as InputFormat,Gr as InputTrack,$t as InputVideoTrack,Sn as IsobmffInputFormat,Li as IsobmffOutputFormat,Wl as MATROSKA,sc as MP3,nc as MP4,cc as MPEG_TS,An as MatroskaInputFormat,Vi as MediaSource,Gc as MediaStreamAudioTrackSource,Hc as MediaStreamVideoTrackSource,Vn as MkvOutputFormat,Bi as MovOutputFormat,ns as Mp3InputFormat,Qc as Mp3OutputFormat,ts as Mp4InputFormat,Wi as Mp4OutputFormat,us as MpegTsInputFormat,Zc as MpegTsOutputFormat,wt as NON_PCM_AUDIO_CODECS,Xr as NullTarget,Gl as OGG,ss as OggInputFormat,Xc as OggOutputFormat,Sr as Output,Ys as OutputAudioTrack,Re as OutputFormat,Zs as OutputSubtitleTrack,Hi as OutputTrack,We as OutputTrackGroup,$s as OutputVideoTrack,ee as PCM_AUDIO_CODECS,Qe as PathedSource,Le as PathedTarget,ac as QTFF,ws as QUALITY_HIGH,Zm as QUALITY_LOW,Jm as QUALITY_MEDIUM,ef as QUALITY_VERY_HIGH,Ym as QUALITY_VERY_LOW,Pe as Quality,rs as QuickTimeInputFormat,ja as RangedSource,Us as RangedTarget,kn as ReadableStreamSource,He as RichImageData,Me as SUBTITLE_CODECS,_e as Source,zr as SourceRef,Qa as StreamSource,Bn as StreamTarget,Ni as SubtitleSource,Ce as Target,Kc as TextSubtitleSource,Rn as UnsupportedInputFormatError,Zo as UrlSource,re as VIDEO_CODECS,fs as VIDEO_SAMPLE_PIXEL_FORMATS,Ae as VideoSample,xi as VideoSampleColorSpace,At as VideoSampleResource,Hr as VideoSampleSink,Ui as VideoSampleSource,Jt as VideoSource,ql as WAVE,Hl as WEBM,jc as WavOutputFormat,as as WaveInputFormat,is as WebMInputFormat,Oi as WebMOutputFormat,uf as asc,Lm as canDecode,dc as canDecodeAudio,lc as canDecodeVideo,tf as canEncode,As as canEncodeAudio,xs as canEncodeSubtitles,Ss as canEncodeVideo,_s as desc,Xl as getDecodableAudioCodecs,Wm as getDecodableCodecs,jl as getDecodableVideoCodecs,En as getEncodableAudioCodecs,rf as getEncodableCodecs,sd as getEncodableSubtitleCodecs,ad as getEncodableVideoCodecs,nf as getFirstEncodableAudioCodec,af as getFirstEncodableSubtitleCodec,hc as getFirstEncodableVideoCodec,Kr as prefer,sf as registerDecoder,of as registerEncoder,qm as registerVideoSampleTransformer};
135
+ `+this._getInvalidityExplanation().join(""));if(this._executed)throw new Error("Conversion cannot be executed twice.");this._executed=!0;for(let t of this._outputTrackIds)this._synchronizer.declareTrack(t);if(this.onProgress){let e=[...new Set(this.utilizedTracks)].map(async i=>await i.isLive()?1/0:await i.getDurationFromMetadata()??await i.computeDuration()),r=Math.max(0,...await Promise.all(e));this._computeProgress=!0,this._totalDuration=Math.min(r-this._startTimestamp,this._endTimestamp-this._startTimestamp);for(let i of this._outputTrackIds)this._maxTimestamps.set(i,0);this.onProgress?.(0,0)}await this.output.start(),this._start();try{await Promise.all(this._trackPromises)}catch(t){throw this._canceled||this.cancel(),t}if(this._canceled)throw new to;if(await this.output.finalize(),this._computeProgress){let t=Math.min(...this._maxTimestamps.values());this.onProgress?.(1,t)}}async cancel(){if(!(this.output.state==="finalizing"||this.output.state==="finalized")){if(this._canceled){console.warn("Conversion already canceled.");return}this._canceled=!0,await this.output.cancel()}}async _processVideoTrack(t,e,r){let i=await t.getCodec();if(!i){this.discardedTracks.push({track:t,reason:"unknown_source_codec",trackOptions:e});return}let a,s=await t.getRotation(),o=Ze(s+(e.rotate??0)),c=o,u=this.output.format.supportsVideoRotationMetadata&&(e.allowRotationMetadata??!0),l=await t.getSquarePixelWidth(),m=await t.getSquarePixelHeight(),[d,f]=o%180===0?[l,m]:[m,l],p=e.crop;p&&(p=Pi(p,d,f));let[b,g]=p?[p.width,p.height]:[d,f],y=b,k=g,w=y/k;e.width!==void 0&&e.height===void 0?(y=Mr(e.width),k=Mr(Math.round(y/w))):e.width===void 0&&e.height!==void 0?(k=Mr(e.height),y=Mr(Math.round(k*w))):e.width!==void 0&&e.height!==void 0&&(y=Mr(e.width),k=Mr(e.height));let T=await t.getFirstTimestamp(),S=this.output.format.getSupportedVideoCodecs(),x=!!e.forceTranscode||T<this._startTimestamp||!!e.frameRate||e.keyFrameInterval!==void 0||e.process!==void 0||e.bitrate!==void 0||!S.includes(i)||e.codec&&e.codec!==i||y!==b||k!==g||o!==0&&!u||!!p,P=e.alpha??"discard";if(x){if(!await t.canDecode()){this.discardedTracks.push({track:t,reason:"undecodable_source_codec",trackOptions:e});return}e.codec&&(S=S.filter(z=>z===e.codec));let R=e.bitrate??ws,v=await hc(S,{width:e.process&&e.processedWidth?e.processedWidth:y,height:e.process&&e.processedHeight?e.processedHeight:k,bitrate:R});if(!v){this.discardedTracks.push({track:t,reason:"no_encodable_target_codec",trackOptions:e});return}let E={codec:v,bitrate:R,keyFrameInterval:e.keyFrameInterval,sizeChangeBehavior:e.fit??"passThrough",alpha:P,hardwareAcceleration:e.hardwareAcceleration,transform:{}};h(E.transform);let D=y!==b||k!==g||o!==0&&(!u||e.process!==void 0)||!!p||l!==await t.getCodedWidth()||m!==await t.getCodedHeight();if(!D){let z=new Ar({format:new Wi,target:new $r}),q=new Ui(E);z.addVideoTrack(q),await z.start();let Te=await new qr(t).getSample(T);if(Te)try{await q.add(Te),Te.close(),await z.finalize()}catch(Un){console.info("Error when probing encoder support. Falling back to rerender path.",Un),D=!0,z.cancel()}else await z.cancel()}e.frameRate&&(E.transform.frameRate=e.frameRate),D&&(c=0,E.transform.width=y,E.transform.height=k,E.transform.fit=e.fit??"fill",E.transform.rotate=Ze(o-s),E.transform.crop=p,E.transform.alpha=P);let U=new Ui(E);a=U,this._trackPromises.push((async()=>{await this._started;let z=new qr(t);for await(let q of z.samples(this._startTimestamp,this._endTimestamp)){if(this._canceled){q.close();return}let ke=Math.max(q.timestamp-this._startTimestamp,0);q.setTimestamp(ke),await this._registerVideoSample(e,r,U,q),q.close()}U.close(),this._synchronizer.closeTrack(r)})())}else{let I=new Zr(i);a=I,this._trackPromises.push((async()=>{await this._started;let R=new xt(t),E={decoderConfig:await t.getDecoderConfig()??void 0};for await(let D of R.packets(void 0,void 0,{verifyKeyPackets:!0})){if(this._canceled)return;if(D.timestamp>=this._endTimestamp)break;let U=D.clone({timestamp:D.timestamp-this._startTimestamp,sideData:P==="discard"?{}:D.sideData});h(U.timestamp>=0),this._reportProgress(r,U.timestamp+U.duration),await I.add(U,E),this._synchronizer.shouldWait(r,U.timestamp)&&await this._synchronizer.wait(U.timestamp)}I.close(),this._synchronizer.closeTrack(r)})())}let A=null;e.group||(A=new We);let C=await t.getLanguageCode();this.output.addVideoTrack(a,{frameRate:e.frameRate,languageCode:Bt(C)?C:void 0,name:await t.getName()??void 0,disposition:await t.getDisposition(),rotation:c,group:A??e.group}),this._addedCounts.video++,this._totalTrackCount++,this.utilizedTracks.push(t),this._outputTrackIds.push(r),this._outputOwnTrackGroups.push(A)}async _registerVideoSample(t,e,r,i){if(this._canceled)return;this._reportProgress(e,i.timestamp+i.duration);let a;if(!t.process)a=[i];else{let s=t.process(i);s instanceof Promise&&(s=await s),Array.isArray(s)||(s=s===null?[]:[s]),a=s.map(o=>o instanceof Ae?o:typeof VideoFrame<"u"&&o instanceof VideoFrame?new Ae(o):new Ae(o,{timestamp:i.timestamp,duration:i.duration}))}try{for(let s of a){if(this._canceled)break;await r.add(s),this._synchronizer.shouldWait(e,s.timestamp)&&await this._synchronizer.wait(s.timestamp)}}finally{for(let s of a)s!==i&&s.close()}}async _processAudioTrack(t,e,r){let i=await t.getCodec();if(!i){this.discardedTracks.push({track:t,reason:"unknown_source_codec",trackOptions:e});return}let a,s=await t.getNumberOfChannels(),o=await t.getSampleRate(),c=await t.getFirstTimestamp(),u=e.numberOfChannels??s,l=e.sampleRate??o,m=u!==s||l!==o||c<this._startTimestamp||c>this._startTimestamp&&!this.output.format.supportsTimestampedMediaData,d=this.output.format.getSupportedAudioCodecs();if(!e.forceTranscode&&!e.bitrate&&!m&&d.includes(i)&&(!e.codec||e.codec===i)&&!e.process&&e.sampleFormat===void 0){let b=new Jr(i);a=b,this._trackPromises.push((async()=>{await this._started;let g=new xt(t),k={decoderConfig:await t.getDecoderConfig()??void 0};for await(let w of g.packets()){if(this._canceled)return;if(w.timestamp>=this._endTimestamp)break;let T=w.clone({timestamp:w.timestamp-this._startTimestamp});h(T.timestamp>=0),this._reportProgress(r,T.timestamp+T.duration),await b.add(T,k),this._synchronizer.shouldWait(r,T.timestamp)&&await this._synchronizer.wait(T.timestamp)}b.close(),this._synchronizer.closeTrack(r)})())}else{if(!await t.canDecode()){this.discardedTracks.push({track:t,reason:"undecodable_source_codec",trackOptions:e});return}let g=null;e.codec&&(d=d.filter(w=>w===e.codec));let y=e.bitrate??ws,k=await En(d,{numberOfChannels:e.process&&e.processedNumberOfChannels?e.processedNumberOfChannels:u,sampleRate:e.process&&e.processedSampleRate?e.processedSampleRate:l,bitrate:y});if(!k.some(w=>wt.includes(w))&&d.some(w=>wt.includes(w))&&(u!==tu||l!==ru)){let T=(await En(d,{numberOfChannels:tu,sampleRate:ru,bitrate:y})).find(S=>wt.includes(S));T&&(m=!0,g=T,u=tu,l=ru)}else g=k[0]??null;if(g===null){this.discardedTracks.push({track:t,reason:"no_encodable_target_codec",trackOptions:e});return}if(m)a=this._resampleAudio(t,e,r,g,u,l,y);else{let w=new zi({codec:g,bitrate:y});a=w,this._trackPromises.push((async()=>{await this._started;let T=new Gr(t);for await(let S of T.samples(void 0,this._endTimestamp)){if(this._canceled){S.close();return}S.setTimestamp(S.timestamp-this._startTimestamp),await this._registerAudioSample(e,r,w,S),S.close()}w.close(),this._synchronizer.closeTrack(r)})())}}let f=null;e.group||(f=new We);let p=await t.getLanguageCode();this.output.addAudioTrack(a,{languageCode:Bt(p)?p:void 0,name:await t.getName()??void 0,disposition:await t.getDisposition(),group:f??e.group}),this._addedCounts.audio++,this._totalTrackCount++,this.utilizedTracks.push(t),this._outputTrackIds.push(r),this._outputOwnTrackGroups.push(f)}async _registerAudioSample(t,e,r,i){if(this._canceled)return;let a=i;t.sampleFormat!==void 0&&hs(a.format)!==t.sampleFormat&&(a=gs(a,t.sampleFormat)),this._reportProgress(e,a.timestamp+a.duration);let s;if(!t.process)s=[a];else{let o=t.process(a);if(o instanceof Promise&&(o=await o),Array.isArray(o)||(o=o===null?[]:[o]),!o.every(c=>c instanceof ye))throw new TypeError("The audio process function must return an AudioSample, null, or an array of AudioSamples.");s=o}try{for(let o of s){if(this._canceled)break;await r.add(o),this._synchronizer.shouldWait(e,o.timestamp)&&await this._synchronizer.wait(o.timestamp)}}finally{a!==i&&a.close();for(let o of s)o!==i&&o.close()}}_resampleAudio(t,e,r,i,a,s,o){let c=new zi({codec:i,bitrate:o});return this._trackPromises.push((async()=>{await this._started;let u=new Di({targetNumberOfChannels:a,targetSampleRate:s,startTime:this._startTimestamp,endTime:this._endTimestamp,onSample:async d=>{h(d.timestamp>=this._startTimestamp),d.setTimestamp(d.timestamp-this._startTimestamp),await this._registerAudioSample(e,r,c,d),d.close()}}),m=new Gr(t).samples(this._startTimestamp,this._endTimestamp);for await(let d of m){if(this._canceled){d.close();return}await u.add(d),d.close()}await u.finalize(),c.close(),this._synchronizer.closeTrack(r)})()),c}_reportProgress(t,e){if(!this._computeProgress)return;h(this._totalDuration!==null),this._maxTimestamps.set(t,Math.max(e,this._maxTimestamps.get(t)));let r=Math.min(...this._maxTimestamps.values()),i=ue(r/this._totalDuration,0,1);i!==this._lastProgress&&(this._lastProgress=i,this.onProgress?.(i,r))}},to=class extends Error{constructor(t="Conversion has been canceled."){super(t),this.name="ConversionCanceledError"}},Qd=1,nu=class{constructor(){this.maxTimestamps=new Map;this.resolvers=[]}declareTrack(t){this.maxTimestamps.set(t,0)}shouldWait(t,e){let r=this.maxTimestamps.get(t);h(r!==void 0),this.maxTimestamps.set(t,Math.max(e,r));let i=this.computeMinAndMaybeResolve();return e-i>Qd}wait(t){let{promise:e,resolve:r}=Z();return this.resolvers.push({timestamp:t,resolve:r}),e}closeTrack(t){this.maxTimestamps.delete(t),this.computeMinAndMaybeResolve()}computeMinAndMaybeResolve(){let t=1/0;for(let[,e]of this.maxTimestamps)t=Math.min(t,e);for(let e=0;e<this.resolvers.length;e++){let r=this.resolvers[e];r.timestamp-t<Qd&&(r.resolve(),this.resolvers.splice(e,1),e--)}return t}};var jd=Symbol.for("mediabunny loaded");globalThis[jd]&&console.error(`[WARNING]
136
+ Mediabunny was loaded twice. This will likely cause Mediabunny not to work correctly. Check if multiple dependencies are importing different versions of Mediabunny, or if something is being bundled incorrectly.`);globalThis[jd]=!0;export{oc as ADTS,Vm as ALL_FORMATS,Kd as ALL_TRACK_TYPES,ae as AUDIO_CODECS,cs as AdtsInputFormat,$c as AdtsOutputFormat,Dc as AppendOnlyStreamTarget,ar as AttachedFile,Sc as AudioBufferSink,qc as AudioBufferSource,ye as AudioSample,yr as AudioSampleResource,Gr as AudioSampleSink,zi as AudioSampleSource,er as AudioSource,_n as BaseMediaSampleSink,Yo as BlobSource,$o as BufferSource,Xr as BufferTarget,kc as CanvasSink,Wc as CanvasSource,Mi as CmafOutputFormat,so as ConcurrentRunner,iu as Conversion,to as ConversionCanceledError,Cs as CustomAudioDecoder,Es as CustomAudioEncoder,yn as CustomPathedSource,Ps as CustomVideoDecoder,Is as CustomVideoEncoder,Jr as EncodedAudioPacketSource,Q as EncodedPacket,xt as EncodedPacketSink,Zr as EncodedVideoPacketSource,Ye as EventEmitter,Kl as FLAC,Jo as FilePathSource,Vc as FilePathTarget,os as FlacInputFormat,Yc as FlacOutputFormat,uc as HLS,Um as HLS_FORMATS,Si as HlsInputFormat,Jc as HlsOutputFormat,Lr as Input,Yt as InputAudioTrack,be as InputDisposedError,Ve as InputFormat,Kr as InputTrack,$t as InputVideoTrack,Sn as IsobmffInputFormat,Li as IsobmffOutputFormat,Wl as MATROSKA,sc as MP3,nc as MP4,cc as MPEG_TS,An as MatroskaInputFormat,Vi as MediaSource,Gc as MediaStreamAudioTrackSource,Hc as MediaStreamVideoTrackSource,Vn as MkvOutputFormat,Bi as MovOutputFormat,ns as Mp3InputFormat,Qc as Mp3OutputFormat,ts as Mp4InputFormat,Wi as Mp4OutputFormat,us as MpegTsInputFormat,Zc as MpegTsOutputFormat,wt as NON_PCM_AUDIO_CODECS,$r as NullTarget,Gl as OGG,ss as OggInputFormat,Xc as OggOutputFormat,Ar as Output,Ys as OutputAudioTrack,Re as OutputFormat,Zs as OutputSubtitleTrack,Hi as OutputTrack,We as OutputTrackGroup,$s as OutputVideoTrack,ee as PCM_AUDIO_CODECS,Qe as PathedSource,Le as PathedTarget,ac as QTFF,ws as QUALITY_HIGH,Zm as QUALITY_LOW,Jm as QUALITY_MEDIUM,ef as QUALITY_VERY_HIGH,Ym as QUALITY_VERY_LOW,Pe as Quality,rs as QuickTimeInputFormat,ja as RangedSource,Us as RangedTarget,kn as ReadableStreamSource,He as RichImageData,Me as SUBTITLE_CODECS,_e as Source,Nr as SourceRef,Qa as StreamSource,Bn as StreamTarget,Ni as SubtitleSource,Ce as Target,Kc as TextSubtitleSource,Rn as UnsupportedInputFormatError,Zo as UrlSource,re as VIDEO_CODECS,fs as VIDEO_SAMPLE_PIXEL_FORMATS,Ae as VideoSample,xi as VideoSampleColorSpace,At as VideoSampleResource,qr as VideoSampleSink,Ui as VideoSampleSource,Jt as VideoSource,ql as WAVE,Hl as WEBM,jc as WavOutputFormat,as as WaveInputFormat,is as WebMInputFormat,Oi as WebMOutputFormat,uf as asc,Lm as canDecode,dc as canDecodeAudio,lc as canDecodeVideo,tf as canEncode,As as canEncodeAudio,xs as canEncodeSubtitles,Ss as canEncodeVideo,_s as desc,Xl as getDecodableAudioCodecs,Wm as getDecodableCodecs,jl as getDecodableVideoCodecs,En as getEncodableAudioCodecs,rf as getEncodableCodecs,sd as getEncodableSubtitleCodecs,ad as getEncodableVideoCodecs,nf as getFirstEncodableAudioCodec,af as getFirstEncodableSubtitleCodec,hc as getFirstEncodableVideoCodec,Qr as prefer,sf as registerDecoder,of as registerEncoder,qm as registerVideoSampleTransformer};