@helios-project/player 0.48.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.
- package/README.md +148 -0
- package/dist/bridge.d.ts +2 -0
- package/dist/bridge.js +169 -0
- package/dist/controllers.d.ts +91 -0
- package/dist/controllers.js +224 -0
- package/dist/features/audio-utils.d.ts +10 -0
- package/dist/features/audio-utils.js +66 -0
- package/dist/features/dom-capture.d.ts +1 -0
- package/dist/features/dom-capture.js +253 -0
- package/dist/features/exporter.d.ts +18 -0
- package/dist/features/exporter.js +228 -0
- package/dist/features/srt-parser.d.ts +7 -0
- package/dist/features/srt-parser.js +75 -0
- package/dist/features/text-tracks.d.ts +40 -0
- package/dist/features/text-tracks.js +99 -0
- package/dist/helios-player.bundle.mjs +7775 -0
- package/dist/helios-player.global.js +633 -0
- package/dist/index.d.ts +166 -0
- package/dist/index.js +1679 -0
- package/package.json +57 -0
|
@@ -0,0 +1,633 @@
|
|
|
1
|
+
(function(fe,ke){typeof exports=="object"&&typeof module<"u"?ke(exports):typeof define=="function"&&define.amd?define(["exports"],ke):(fe=typeof globalThis<"u"?globalThis:fe||self,ke(fe.HeliosPlayer={}))})(this,(function(fe){"use strict";async function ke(t){const e=t.ownerDocument||document;let r=t.cloneNode(!0);await Ir(r),r=Or(t,r),r=Mr(t,r);const s=new XMLSerializer().serializeToString(r),o=Array.from(e.querySelectorAll("style")).map(async C=>{const A=C.textContent||"",_=await gt(A,e.baseURI),F=C.cloneNode(!0);return F.textContent=_,F.outerHTML}),a=(await Promise.all(o)).join(`
|
|
2
|
+
`),l=await Rr(e)+`
|
|
3
|
+
`+a,d=t.scrollWidth||t.offsetWidth||1920,u=t.scrollHeight||t.offsetHeight||1080,f=`
|
|
4
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${d}" height="${u}">
|
|
5
|
+
<foreignObject width="100%" height="100%">
|
|
6
|
+
<div xmlns="http://www.w3.org/1999/xhtml" style="width: 100%; height: 100%;">
|
|
7
|
+
${l}
|
|
8
|
+
${s}
|
|
9
|
+
</div>
|
|
10
|
+
</foreignObject>
|
|
11
|
+
</svg>
|
|
12
|
+
`,g=new Blob([f],{type:"image/svg+xml;charset=utf-8"}),p=URL.createObjectURL(g),y=new Image;await new Promise((C,A)=>{y.onload=()=>C(),y.onerror=_=>A(new Error("Failed to load SVG image for DOM capture")),y.src=p});const T=await createImageBitmap(y);return URL.revokeObjectURL(p),T}async function Rr(t){const r=Array.from(t.querySelectorAll('link[rel="stylesheet"]')).map(async i=>{try{if(!i.href)return"";const s=await fetch(i.href);if(!s.ok)throw new Error(`HTTP ${s.status} ${s.statusText}`);const n=await s.text(),o=await gt(n,i.href);return`<style>/* ${i.href} */
|
|
13
|
+
${o}</style>`}catch(s){return console.warn("Helios: Failed to inline stylesheet:",i.href,s),""}});return(await Promise.all(r)).join(`
|
|
14
|
+
`)}async function gt(t,e){const r=/url\((?:['"]?)(.*?)(?:['"]?)\)/g,s=Array.from(t.matchAll(r)).map(async a=>{const c=a[0],l=a[1];if(l.startsWith("data:"))return null;try{const d=new URL(l,e).href,u=await De(d);return{original:c,replacement:`url("${u}")`}}catch(d){return console.warn(`Helios: Failed to inline CSS asset: ${l}`,d),null}}),n=(await Promise.all(s)).filter(a=>a!==null);let o=t;for(const{original:a,replacement:c}of n)o=o.split(a).join(c);return o}async function Ir(t){const e=[];t.querySelectorAll("img").forEach(s=>{s.src&&!s.src.startsWith("data:")&&e.push(De(s.src).then(n=>{s.src=n}).catch(n=>console.warn("Helios: Failed to inline image:",s.src,n)))}),t.querySelectorAll('[style*="background-image"]').forEach(s=>{const n=s,o=n.style.backgroundImage;if(o&&o.includes("url(")){const a=o.match(/url\(['"]?(.*?)['"]?\)/);a&&a[1]&&!a[1].startsWith("data:")&&e.push(De(a[1]).then(c=>{n.style.backgroundImage=n.style.backgroundImage.replace(a[0],`url("${c}")`)}).catch(c=>console.warn("Helios: Failed to inline background:",a[1],c)))}}),await Promise.all(e)}async function De(t){const e=await fetch(t);if(!e.ok)throw new Error(`HTTP ${e.status} ${e.statusText}`);const r=await e.blob();return new Promise((i,s)=>{const n=new FileReader;n.onloadend=()=>i(n.result),n.onerror=s,n.readAsDataURL(r)})}function Or(t,e){if(t instanceof HTMLCanvasElement&&e instanceof HTMLCanvasElement)try{const s=t.toDataURL(),n=document.createElement("img");return n.src=s,n.style.cssText=t.style.cssText,n.className=t.className,t.id&&(n.id=t.id),t.hasAttribute("width")&&n.setAttribute("width",t.getAttribute("width")),t.hasAttribute("height")&&n.setAttribute("height",t.getAttribute("height")),n}catch(s){return console.warn("Helios: Failed to inline root canvas:",s),e}const r=Array.from(t.querySelectorAll("canvas")),i=Array.from(e.querySelectorAll("canvas"));for(let s=0;s<Math.min(r.length,i.length);s++){const n=r[s],o=i[s];try{const a=n.toDataURL(),c=document.createElement("img");c.src=a,c.style.cssText=n.style.cssText,c.className=n.className,n.id&&(c.id=n.id),n.hasAttribute("width")&&c.setAttribute("width",n.getAttribute("width")),n.hasAttribute("height")&&c.setAttribute("height",n.getAttribute("height")),o.parentNode?.replaceChild(c,o)}catch(a){console.warn("Helios: Failed to inline nested canvas:",a)}}return e}function Mr(t,e){if(t instanceof HTMLVideoElement&&e instanceof HTMLVideoElement){if(t.readyState>=2){const s=wt(t);if(s)return s}return e}const r=Array.from(t.querySelectorAll("video")),i=Array.from(e.querySelectorAll("video"));for(let s=0;s<Math.min(r.length,i.length);s++){const n=r[s],o=i[s];if(n.readyState<2)continue;const a=wt(n);a&&o.parentNode?.replaceChild(a,o)}return e}function wt(t){try{const e=document.createElement("canvas");e.width=t.videoWidth||300,e.height=t.videoHeight||150;const r=e.getContext("2d");if(r){r.drawImage(t,0,0,e.width,e.height);const i=e.toDataURL(),s=document.createElement("img");return s.src=i,s.style.cssText=t.style.cssText,s.className=t.className,t.id&&(s.id=t.id),t.hasAttribute("width")&&s.setAttribute("width",t.getAttribute("width")),t.hasAttribute("height")&&s.setAttribute("height",t.getAttribute("height")),s}}catch(e){console.warn("Helios: Failed to inline video:",e)}return null}async function Vr(t){const e=Array.from(t.querySelectorAll("audio"));return Promise.all(e.map(async r=>{if(!r.src)return{buffer:new ArrayBuffer(0),mimeType:null};try{const i=await fetch(r.src),s=r.getAttribute("volume");return{buffer:await i.arrayBuffer(),mimeType:i.headers.get("content-type"),volume:s!==null?parseFloat(s):r.volume,muted:r.muted,loop:r.loop,startTime:parseFloat(r.getAttribute("data-start-time")||"0")||0}}catch(i){return console.warn("Failed to fetch audio asset:",r.src,i),{buffer:new ArrayBuffer(0),mimeType:null}}}))}async function zr(t,e,r,i=0){if(typeof OfflineAudioContext>"u")throw new Error("OfflineAudioContext not supported in this environment");if(e<=0)return new OfflineAudioContext(2,1,r).startRendering();const s=Math.ceil(e*r),n=new OfflineAudioContext(2,s,r);for(const o of t)if(o.buffer.byteLength!==0)try{const a=await n.decodeAudioData(o.buffer.slice(0)),c=n.createBufferSource();c.buffer=a,c.loop=!!o.loop;const l=n.createGain(),d=o.muted?0:typeof o.volume=="number"?o.volume:1;l.gain.value=d,c.connect(l),l.connect(n.destination);let f=(o.startTime||0)-i,g=0;f<0&&(g=-f,f=0),c.start(f,g)}catch(a){console.warn("Failed to decode audio asset:",a)}return n.startRendering()}class Ur{constructor(e,r){this.instance=e,this.iframe=r}play(){this.instance.play()}pause(){this.instance.pause()}seek(e){this.instance.seek(e)}setAudioVolume(e){this.instance.setAudioVolume(e)}setAudioMuted(e){this.instance.setAudioMuted(e)}setLoop(e){this.instance.setLoop(e)}setPlaybackRate(e){this.instance.setPlaybackRate(e)}setPlaybackRange(e,r){this.instance.setPlaybackRange(e,r)}clearPlaybackRange(){this.instance.clearPlaybackRange()}setCaptions(e){this.instance.setCaptions(e)}setInputProps(e){this.instance.setInputProps(e)}subscribe(e){return this.instance.subscribe(e)}getState(){return this.instance.getState()}dispose(){}onError(e){const r=this.iframe?.contentWindow||window,i=n=>{e({message:n.message,filename:n.filename,lineno:n.lineno,colno:n.colno,stack:n.error?.stack})},s=n=>{e({message:n.reason?.message||String(n.reason),stack:n.reason?.stack})};return r.addEventListener("error",i),r.addEventListener("unhandledrejection",s),()=>{r.removeEventListener("error",i),r.removeEventListener("unhandledrejection",s)}}async getAudioTracks(){const e=this.iframe?.contentDocument||document;return Vr(e)}async getSchema(){return this.instance.schema}async captureFrame(e,r){const i=this.instance.getState(),s=i.fps,n=i.activeCaptions||[];this.instance.seek(e),this.iframe&&this.iframe.contentWindow&&(await new Promise(c=>this.iframe.contentWindow.requestAnimationFrame(()=>c())),await new Promise(c=>this.iframe.contentWindow.requestAnimationFrame(()=>c())));const o=this.iframe?.contentDocument||document;let a=null;if(r?.mode==="dom")try{const c=await ke(o.body);a=new VideoFrame(c,{timestamp:e/s*1e6})}catch(c){return console.error("DOM capture failed:",c),null}else{const c=r?.selector||"canvas",l=o.querySelector(c);l instanceof HTMLCanvasElement&&(a=new VideoFrame(l,{timestamp:e/s*1e6}))}return a?{frame:a,captions:n}:null}}class Wr{constructor(e,r){this.iframeWindow=e,this.lastState=r||{isPlaying:!1,currentFrame:0,duration:0,fps:30},window.addEventListener("message",this.handleMessage)}listeners=[];errorListeners=[];lastState;handleMessage=e=>{e.source===this.iframeWindow&&(e.data?.type==="HELIOS_STATE"?(this.lastState=e.data.state,this.notifyListeners()):e.data?.type==="HELIOS_ERROR"&&this.notifyErrorListeners(e.data.error))};notifyListeners(){this.listeners.forEach(e=>e(this.lastState))}notifyErrorListeners(e){this.errorListeners.forEach(r=>r(e))}play(){this.iframeWindow.postMessage({type:"HELIOS_PLAY"},"*")}pause(){this.iframeWindow.postMessage({type:"HELIOS_PAUSE"},"*")}seek(e){this.iframeWindow.postMessage({type:"HELIOS_SEEK",frame:e},"*")}setAudioVolume(e){this.iframeWindow.postMessage({type:"HELIOS_SET_VOLUME",volume:e},"*")}setAudioMuted(e){this.iframeWindow.postMessage({type:"HELIOS_SET_MUTED",muted:e},"*")}setLoop(e){this.iframeWindow.postMessage({type:"HELIOS_SET_LOOP",loop:e},"*")}setPlaybackRate(e){this.iframeWindow.postMessage({type:"HELIOS_SET_PLAYBACK_RATE",rate:e},"*")}setPlaybackRange(e,r){this.iframeWindow.postMessage({type:"HELIOS_SET_PLAYBACK_RANGE",start:e,end:r},"*")}clearPlaybackRange(){this.iframeWindow.postMessage({type:"HELIOS_CLEAR_PLAYBACK_RANGE"},"*")}setCaptions(e){this.iframeWindow.postMessage({type:"HELIOS_SET_CAPTIONS",captions:e},"*")}setInputProps(e){this.iframeWindow.postMessage({type:"HELIOS_SET_PROPS",props:e},"*")}subscribe(e){return this.listeners.push(e),e(this.lastState),()=>{this.listeners=this.listeners.filter(r=>r!==e)}}onError(e){return this.errorListeners.push(e),()=>{this.errorListeners=this.errorListeners.filter(r=>r!==e)}}getState(){return this.lastState}dispose(){window.removeEventListener("message",this.handleMessage),this.listeners=[],this.errorListeners=[]}async captureFrame(e,r){return new Promise(i=>{const s=n=>{if(n.source===this.iframeWindow&&n.data?.type==="HELIOS_FRAME_DATA"&&n.data.frame===e)if(window.removeEventListener("message",s),n.data.success){const o=n.data.bitmap,a=n.data.captions||[],c=this.lastState.fps||30,l=new VideoFrame(o,{timestamp:e/c*1e6});i({frame:l,captions:a})}else console.error("Bridge capture failed:",n.data.error),i(null)};window.addEventListener("message",s),this.iframeWindow.postMessage({type:"HELIOS_CAPTURE_FRAME",frame:e,selector:r?.selector,mode:r?.mode},"*"),setTimeout(()=>{window.removeEventListener("message",s),i(null)},5e3)})}async getAudioTracks(){return new Promise(e=>{const r=i=>{i.source===this.iframeWindow&&i.data?.type==="HELIOS_AUDIO_DATA"&&(window.removeEventListener("message",r),e(i.data.assets||[]))};window.addEventListener("message",r),this.iframeWindow.postMessage({type:"HELIOS_GET_AUDIO_TRACKS"},"*"),setTimeout(()=>{window.removeEventListener("message",r),e([])},5e3)})}async getSchema(){return new Promise(e=>{let r;const i=s=>{s.source===this.iframeWindow&&s.data?.type==="HELIOS_SCHEMA"&&(window.removeEventListener("message",i),clearTimeout(r),e(s.data.schema))};window.addEventListener("message",i),this.iframeWindow.postMessage({type:"HELIOS_GET_SCHEMA"},"*"),r=window.setTimeout(()=>{window.removeEventListener("message",i),e(void 0)},5e3)})}}function Nr(t){if(!t)return[];const e=[],r=t.trim().replace(/\r\n/g,`
|
|
15
|
+
`).split(/\n\n+/);for(const i of r){const s=i.split(`
|
|
16
|
+
`);if(s.length<2)continue;let n=-1;for(let l=0;l<Math.min(s.length,2);l++)if(s[l].includes("-->")){n=l;break}if(n===-1)continue;const o=s[n],a=s.slice(n+1),c=o.match(/(\d{1,2}:\d{2}:\d{2}[,.]\d{1,3})\s*-->\s*(\d{1,2}:\d{2}:\d{2}[,.]\d{1,3})/);if(c){const l=bt(c[1]),d=bt(c[2]),u=a.join(`
|
|
17
|
+
`).trim();!isNaN(l)&&!isNaN(d)&&e.push({startTime:l,endTime:d,text:u})}}return e}function bt(t){const e=t.replace(",",".").split(":");if(e.length===3){const r=parseFloat(e[0]),i=parseFloat(e[1]),s=parseFloat(e[2]);return r*3600+i*60+s}return NaN}function Dr(t){return!t||t.length===0?"":t.map((e,r)=>{const i=yt(e.startTime),s=yt(e.endTime);return`${r+1}
|
|
18
|
+
${i} --> ${s}
|
|
19
|
+
${e.text}
|
|
20
|
+
|
|
21
|
+
`}).join("")}function yt(t){const e=Math.round(t*1e3),r=Math.floor(e/36e5),i=Math.floor(e%36e5/6e4),s=Math.floor(e%6e4/1e3),n=e%1e3,o=String(r).padStart(2,"0"),a=String(i).padStart(2,"0"),c=String(s).padStart(2,"0"),l=String(n).padStart(3,"0");return`${o}:${a}:${c},${l}`}/*!
|
|
22
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
23
|
+
*
|
|
24
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
25
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
26
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
27
|
+
*/function m(t){if(!t)throw new Error("Assertion failed.")}const Lr=t=>{const e=(t%360+360)%360;if(e===0||e===90||e===180||e===270)return e;throw new Error(`Invalid rotation ${t}.`)},j=t=>t&&t[t.length-1],Ce=t=>t>=0&&t<2**32;class ee{constructor(e){this.bytes=e,this.pos=0}seekToByte(e){this.pos=8*e}readBit(){const e=Math.floor(this.pos/8),r=this.bytes[e]??0,i=7-(this.pos&7),s=(r&1<<i)>>i;return this.pos++,s}readBits(e){if(e===1)return this.readBit();let r=0;for(let i=0;i<e;i++)r<<=1,r|=this.readBit();return r}writeBits(e,r){const i=this.pos+e;for(let s=this.pos;s<i;s++){const n=Math.floor(s/8);let o=this.bytes[n];const a=7-(s&7);o&=~(1<<a),o|=(r&1<<i-s-1)>>i-s-1<<a,this.bytes[n]=o}this.pos=i}readAlignedByte(){if(this.pos%8!==0)throw new Error("Bitstream is not byte-aligned.");const e=this.pos/8,r=this.bytes[e]??0;return this.pos+=8,r}skipBits(e){this.pos+=e}getBitsLeft(){return this.bytes.length*8-this.pos}clone(){const e=new ee(this.bytes);return e.pos=this.pos,e}}const b=t=>{let e=0;for(;t.readBits(1)===0&&e<32;)e++;if(e>=32)throw new Error("Invalid exponential-Golomb code.");return(1<<e)-1+t.readBits(e)},te=t=>{const e=b(t);return(e&1)===0?-(e>>1):e+1>>1},Hr=(t,e,r,i)=>{for(let s=e;s<r;s++){const n=Math.floor(s/8);let o=t[n];const a=7-(s&7);o&=~(1<<a),o|=(i&1<<r-s-1)>>r-s-1<<a,t[n]=o}},q=t=>t.constructor===Uint8Array?t:ArrayBuffer.isView(t)?new Uint8Array(t.buffer,t.byteOffset,t.byteLength):new Uint8Array(t),oe=t=>t.constructor===DataView?t:ArrayBuffer.isView(t)?new DataView(t.buffer,t.byteOffset,t.byteLength):new DataView(t),H=new TextEncoder,Be={bt709:1,bt470bg:5,smpte170m:6,bt2020:9,smpte432:12},Re={bt709:1,smpte170m:6,linear:8,"iec61966-2-1":13,pq:16,hlg:18},Ie={rgb:0,bt709:1,bt470bg:5,smpte170m:6,"bt2020-ncl":9},Tt=t=>!!t&&!!t.primaries&&!!t.transfer&&!!t.matrix&&t.fullRange!==void 0,Oe=t=>t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer||ArrayBuffer.isView(t);class Ct{constructor(){this.currentPromise=Promise.resolve(),this.pending=0}async acquire(){let e;const r=new Promise(s=>{let n=!1;e=()=>{n||(s(),this.pending--,n=!0)}}),i=this.currentPromise;return this.currentPromise=r,this.pending++,await i,e}}const vt=()=>{let t,e;return{promise:new Promise((i,s)=>{t=i,e=s}),resolve:t,reject:e}},ve=t=>{throw new Error(`Unexpected value: ${t}`)},Le=(t,e,r,i)=>{r=r>>>0,r=r&16777215,i?(t.setUint8(e,r&255),t.setUint8(e+1,r>>>8&255),t.setUint8(e+2,r>>>16&255)):(t.setUint8(e,r>>>16&255),t.setUint8(e+1,r>>>8&255),t.setUint8(e+2,r&255))},$r=(t,e,r,i)=>{r=$(r,-8388608,8388607),r<0&&(r=r+16777216&16777215),Le(t,e,r,i)},$=(t,e,r)=>Math.max(e,Math.min(r,t)),St="und",xt=(t,e)=>Math.round(t/e)*e,jr=/^[a-z]{3}$/,qr=t=>jr.test(t),ae=1e6*(1+Number.EPSILON),Qr=(t,e)=>{const r=t<0?-1:1;t=Math.abs(t);let i=0,s=1,n=1,o=0,a=t;for(;;){const c=Math.floor(a),l=c*n+i,d=c*o+s;if(d>e)return{numerator:r*n,denominator:o};if(i=n,s=o,n=l,o=d,a=1/(a-c),!isFinite(a))break}return{numerator:r*n,denominator:o}};class kt{constructor(){this.currentPromise=Promise.resolve()}call(e){return this.currentPromise=this.currentPromise.then(e)}}let He=null;const Gr=()=>He!==null?He:He=!!(typeof navigator<"u"&&(navigator.vendor?.match(/apple/i)||/AppleWebKit/.test(navigator.userAgent)&&!/Chrome/.test(navigator.userAgent)||/\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent)));let $e=null;const je=()=>$e!==null?$e:$e=typeof navigator<"u"&&navigator.userAgent?.includes("Firefox"),qe=function*(t){for(const e in t){const r=t[e];r!==void 0&&(yield{key:e,value:r})}},Xr=t=>{switch(t.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}},Kr=(t,e)=>{if(t.length!==e.length)return!1;for(let r=0;r<t.length;r++)if(t[r]!==e[r])return!1;return!0},Yr=()=>{Symbol.dispose??=Symbol("Symbol.dispose")};/*!
|
|
28
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
29
|
+
*
|
|
30
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
31
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
32
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
33
|
+
*/class Et{constructor(e,r){if(this.data=e,this.mimeType=r,!(e instanceof Uint8Array))throw new TypeError("data must be a Uint8Array.");if(typeof r!="string")throw new TypeError("mimeType must be a string.")}}class At{constructor(e,r,i,s){if(this.data=e,this.mimeType=r,this.name=i,this.description=s,!(e instanceof Uint8Array))throw new TypeError("data must be a Uint8Array.");if(r!==void 0&&typeof r!="string")throw new TypeError("mimeType, when provided, must be a string.");if(i!==void 0&&typeof i!="string")throw new TypeError("name, when provided, must be a string.");if(s!==void 0&&typeof s!="string")throw new TypeError("description, when provided, must be a string.")}}const Zr=t=>{if(!t||typeof t!="object")throw new TypeError("tags must be an object.");if(t.title!==void 0&&typeof t.title!="string")throw new TypeError("tags.title, when provided, must be a string.");if(t.description!==void 0&&typeof t.description!="string")throw new TypeError("tags.description, when provided, must be a string.");if(t.artist!==void 0&&typeof t.artist!="string")throw new TypeError("tags.artist, when provided, must be a string.");if(t.album!==void 0&&typeof t.album!="string")throw new TypeError("tags.album, when provided, must be a string.");if(t.albumArtist!==void 0&&typeof t.albumArtist!="string")throw new TypeError("tags.albumArtist, when provided, must be a string.");if(t.trackNumber!==void 0&&(!Number.isInteger(t.trackNumber)||t.trackNumber<=0))throw new TypeError("tags.trackNumber, when provided, must be a positive integer.");if(t.tracksTotal!==void 0&&(!Number.isInteger(t.tracksTotal)||t.tracksTotal<=0))throw new TypeError("tags.tracksTotal, when provided, must be a positive integer.");if(t.discNumber!==void 0&&(!Number.isInteger(t.discNumber)||t.discNumber<=0))throw new TypeError("tags.discNumber, when provided, must be a positive integer.");if(t.discsTotal!==void 0&&(!Number.isInteger(t.discsTotal)||t.discsTotal<=0))throw new TypeError("tags.discsTotal, when provided, must be a positive integer.");if(t.genre!==void 0&&typeof t.genre!="string")throw new TypeError("tags.genre, when provided, must be a string.");if(t.date!==void 0&&(!(t.date instanceof Date)||Number.isNaN(t.date.getTime())))throw new TypeError("tags.date, when provided, must be a valid Date.");if(t.lyrics!==void 0&&typeof t.lyrics!="string")throw new TypeError("tags.lyrics, when provided, must be a string.");if(t.images!==void 0){if(!Array.isArray(t.images))throw new TypeError("tags.images, when provided, must be an array.");for(const e of t.images){if(!e||typeof e!="object")throw new TypeError("Each image in tags.images must be an object.");if(!(e.data instanceof Uint8Array))throw new TypeError("Each image.data must be a Uint8Array.");if(typeof e.mimeType!="string")throw new TypeError("Each image.mimeType must be a string.");if(!["coverFront","coverBack","unknown"].includes(e.kind))throw new TypeError("Each image.kind must be 'coverFront', 'coverBack', or 'unknown'.")}}if(t.comment!==void 0&&typeof t.comment!="string")throw new TypeError("tags.comment, when provided, must be a string.");if(t.raw!==void 0){if(!t.raw||typeof t.raw!="object")throw new TypeError("tags.raw, when provided, must be an object.");for(const e of Object.values(t.raw))if(e!==null&&typeof e!="string"&&!(e instanceof Uint8Array)&&!(e instanceof Et)&&!(e instanceof At))throw new TypeError("Each value in tags.raw must be a string, Uint8Array, RichImageData, AttachedFile, or null.")}},Jr=t=>{if(!t||typeof t!="object")throw new TypeError("disposition must be an object.");if(t.default!==void 0&&typeof t.default!="boolean")throw new TypeError("disposition.default must be a boolean.");if(t.forced!==void 0&&typeof t.forced!="boolean")throw new TypeError("disposition.forced must be a boolean.");if(t.original!==void 0&&typeof t.original!="boolean")throw new TypeError("disposition.original must be a boolean.");if(t.commentary!==void 0&&typeof t.commentary!="boolean")throw new TypeError("disposition.commentary must be a boolean.");if(t.hearingImpaired!==void 0&&typeof t.hearingImpaired!="boolean")throw new TypeError("disposition.hearingImpaired must be a boolean.");if(t.visuallyImpaired!==void 0&&typeof t.visuallyImpaired!="boolean")throw new TypeError("disposition.visuallyImpaired must be a boolean.")};/*!
|
|
34
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
35
|
+
*
|
|
36
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
37
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
38
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
39
|
+
*/const re=["avc","hevc","vp9","av1","vp8"],Q=["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"],Qe=["aac","opus","mp3","vorbis","flac"],me=[...Qe,...Q],Se=["webvtt"],Me=[{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}],_t=[{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}],Pt=[{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}],Ft=[{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}],ei=(t,e,r,i)=>{if(t==="avc"){const n=Math.ceil(e/16)*Math.ceil(r/16),o=Me.find(u=>n<=u.maxMacroblocks&&i<=u.maxBitrate)??j(Me),a=o?o.level:0,c="64".padStart(2,"0"),l="00",d=a.toString(16).padStart(2,"0");return`avc1.${c}${l}${d}`}else if(t==="hevc"){const a=e*r,c=_t.find(d=>a<=d.maxPictureSize&&i<=d.maxBitrate)??j(_t);return`hev1.1.6.${c.tier}${c.level}.B0`}else{if(t==="vp8")return"vp8";if(t==="vp9"){const n=e*r;return`vp09.00.${(Pt.find(c=>n<=c.maxPictureSize&&i<=c.maxBitrate)??j(Pt)).level.toString().padStart(2,"0")}.08`}else if(t==="av1"){const n=e*r,o=Ft.find(l=>n<=l.maxPictureSize&&i<=l.maxBitrate)??j(Ft);return`av01.0.${o.level.toString().padStart(2,"0")}${o.tier}.08`}}throw new TypeError(`Unhandled codec '${t}'.`)},ti=t=>{const e=t.split("."),r=Number(e[1]),i=Number(e[2]),s=Number(e[3]),n=e[4]?Number(e[4]):1;return[1,1,r,2,1,i,3,1,s,4,1,n]},Bt=t=>{const e=t.split("."),s=(1<<7)+1,n=Number(e[1]),o=e[2],a=Number(o.slice(0,-1)),c=(n<<5)+a,l=o.slice(-1)==="H"?1:0,u=Number(e[3])===8?0:1,f=0,g=e[4]?Number(e[4]):0,p=e[5]?Number(e[5][0]):1,y=e[5]?Number(e[5][1]):1,T=e[5]?Number(e[5][2]):0,C=(l<<7)+(u<<6)+(f<<5)+(g<<4)+(p<<3)+(y<<2)+T;return[s,c,C,0]},ri=(t,e,r)=>{if(t==="aac")return e>=2&&r<=24e3?"mp4a.40.29":r<=24e3?"mp4a.40.5":"mp4a.40.2";if(t==="mp3")return"mp3";if(t==="opus")return"opus";if(t==="vorbis")return"vorbis";if(t==="flac")return"flac";if(Q.includes(t))return t;throw new TypeError(`Unhandled codec '${t}'.`)},Ee=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],Ve=[-1,1,2,3,4,5,6,8],ii=t=>{if(!t||t.byteLength<2)throw new TypeError("AAC description must be at least 2 bytes long.");const e=new ee(t);let r=e.readBits(5);r===31&&(r=32+e.readBits(6));const i=e.readBits(4);let s=null;i===15?s=e.readBits(24):i<Ee.length&&(s=Ee[i]);const n=e.readBits(4);let o=null;return n>=1&&n<=7&&(o=Ve[n]),{objectType:r,frequencyIndex:i,sampleRate:s,channelConfiguration:n,numberOfChannels:o}},Ge=t=>{let e=Ee.indexOf(t.sampleRate),r=null;e===-1&&(e=15,r=t.sampleRate);const i=Ve.indexOf(t.numberOfChannels);if(i===-1)throw new TypeError(`Unsupported number of channels: ${t.numberOfChannels}`);let s=13;t.objectType>=32&&(s+=6),e===15&&(s+=24);const n=Math.ceil(s/8),o=new Uint8Array(n),a=new ee(o);return t.objectType<32?a.writeBits(5,t.objectType):(a.writeBits(5,31),a.writeBits(6,t.objectType-32)),a.writeBits(4,e),e===15&&a.writeBits(24,r),a.writeBits(4,i),o},si=48e3,Rt=/^pcm-([usf])(\d+)+(be)?$/,pe=t=>{if(m(Q.includes(t)),t==="ulaw")return{dataType:"ulaw",sampleSize:1,littleEndian:!0,silentValue:255};if(t==="alaw")return{dataType:"alaw",sampleSize:1,littleEndian:!0,silentValue:213};const e=Rt.exec(t);m(e);let r;e[1]==="u"?r="unsigned":e[1]==="s"?r="signed":r="float";const i=Number(e[2])/8,s=e[3]!=="be",n=t==="pcm-u8"?2**7:0;return{dataType:r,sampleSize:i,littleEndian:s,silentValue:n}},It=t=>t.startsWith("avc1")||t.startsWith("avc3")?"avc":t.startsWith("hev1")||t.startsWith("hvc1")?"hevc":t==="vp8"?"vp8":t.startsWith("vp09")?"vp9":t.startsWith("av01")?"av1":t.startsWith("mp4a.40")||t==="mp4a.67"?"aac":t==="mp3"||t==="mp4a.69"||t==="mp4a.6B"||t==="mp4a.6b"?"mp3":t==="opus"?"opus":t==="vorbis"?"vorbis":t==="flac"?"flac":t==="ulaw"?"ulaw":t==="alaw"?"alaw":Rt.test(t)?t:t==="webvtt"?"webvtt":null,ni=t=>t==="avc"?{avc:{format:"avc"}}:t==="hevc"?{hevc:{format:"hevc"}}:{},oi=t=>t==="aac"?{aac:{format:"aac"}}:t==="opus"?{opus:{format:"opus"}}:{},ai=["avc1","avc3","hev1","hvc1","vp8","vp09","av01"],ci=/^(avc1|avc3)\.[0-9a-fA-F]{6}$/,li=/^(hev1|hvc1)\.(?:[ABC]?\d+)\.[0-9a-fA-F]{1,8}\.[LH]\d+(?:\.[0-9a-fA-F]{1,2}){0,6}$/,di=/^vp09(?:\.\d{2}){3}(?:(?:\.\d{2}){5})?$/,ui=/^av01\.\d\.\d{2}[MH]\.\d{2}(?:\.\d\.\d{3}\.\d{2}\.\d{2}\.\d{2}\.\d)?$/,Ot=t=>{if(!t)throw new TypeError("Video chunk metadata must be provided.");if(typeof t!="object")throw new TypeError("Video chunk metadata must be an object.");if(!t.decoderConfig)throw new TypeError("Video chunk metadata must include a decoder configuration.");if(typeof t.decoderConfig!="object")throw new TypeError("Video chunk metadata decoder configuration must be an object.");if(typeof t.decoderConfig.codec!="string")throw new TypeError("Video chunk metadata decoder configuration must specify a codec string.");if(!ai.some(e=>t.decoderConfig.codec.startsWith(e)))throw new TypeError("Video chunk metadata decoder configuration codec string must be a valid video codec string as specified in the WebCodecs Codec Registry.");if(!Number.isInteger(t.decoderConfig.codedWidth)||t.decoderConfig.codedWidth<=0)throw new TypeError("Video chunk metadata decoder configuration must specify a valid codedWidth (positive integer).");if(!Number.isInteger(t.decoderConfig.codedHeight)||t.decoderConfig.codedHeight<=0)throw new TypeError("Video chunk metadata decoder configuration must specify a valid codedHeight (positive integer).");if(t.decoderConfig.description!==void 0&&!Oe(t.decoderConfig.description))throw new TypeError("Video chunk metadata decoder configuration description, when defined, must be an ArrayBuffer or an ArrayBuffer view.");if(t.decoderConfig.colorSpace!==void 0){const{colorSpace:e}=t.decoderConfig;if(typeof e!="object")throw new TypeError("Video chunk metadata decoder configuration colorSpace, when provided, must be an object.");const r=Object.keys(Be);if(e.primaries!=null&&!r.includes(e.primaries))throw new TypeError(`Video chunk metadata decoder configuration colorSpace primaries, when defined, must be one of ${r.join(", ")}.`);const i=Object.keys(Re);if(e.transfer!=null&&!i.includes(e.transfer))throw new TypeError(`Video chunk metadata decoder configuration colorSpace transfer, when defined, must be one of ${i.join(", ")}.`);const s=Object.keys(Ie);if(e.matrix!=null&&!s.includes(e.matrix))throw new TypeError(`Video chunk metadata decoder configuration colorSpace matrix, when defined, must be one of ${s.join(", ")}.`);if(e.fullRange!=null&&typeof e.fullRange!="boolean")throw new TypeError("Video chunk metadata decoder configuration colorSpace fullRange, when defined, must be a boolean.")}if(t.decoderConfig.codec.startsWith("avc1")||t.decoderConfig.codec.startsWith("avc3")){if(!ci.test(t.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(t.decoderConfig.codec.startsWith("hev1")||t.decoderConfig.codec.startsWith("hvc1")){if(!li.test(t.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(t.decoderConfig.codec.startsWith("vp8")){if(t.decoderConfig.codec!=="vp8")throw new TypeError('Video chunk metadata decoder configuration codec string for VP8 must be "vp8".')}else if(t.decoderConfig.codec.startsWith("vp09")){if(!di.test(t.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(t.decoderConfig.codec.startsWith("av01")&&!ui.test(t.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/.')},hi=["mp4a","mp3","opus","vorbis","flac","ulaw","alaw","pcm"],Mt=t=>{if(!t)throw new TypeError("Audio chunk metadata must be provided.");if(typeof t!="object")throw new TypeError("Audio chunk metadata must be an object.");if(!t.decoderConfig)throw new TypeError("Audio chunk metadata must include a decoder configuration.");if(typeof t.decoderConfig!="object")throw new TypeError("Audio chunk metadata decoder configuration must be an object.");if(typeof t.decoderConfig.codec!="string")throw new TypeError("Audio chunk metadata decoder configuration must specify a codec string.");if(!hi.some(e=>t.decoderConfig.codec.startsWith(e)))throw new TypeError("Audio chunk metadata decoder configuration codec string must be a valid audio codec string as specified in the WebCodecs Codec Registry.");if(!Number.isInteger(t.decoderConfig.sampleRate)||t.decoderConfig.sampleRate<=0)throw new TypeError("Audio chunk metadata decoder configuration must specify a valid sampleRate (positive integer).");if(!Number.isInteger(t.decoderConfig.numberOfChannels)||t.decoderConfig.numberOfChannels<=0)throw new TypeError("Audio chunk metadata decoder configuration must specify a valid numberOfChannels (positive integer).");if(t.decoderConfig.description!==void 0&&!Oe(t.decoderConfig.description))throw new TypeError("Audio chunk metadata decoder configuration description, when defined, must be an ArrayBuffer or an ArrayBuffer view.");if(t.decoderConfig.codec.startsWith("mp4a")&&t.decoderConfig.codec!=="mp4a.69"&&t.decoderConfig.codec!=="mp4a.6B"&&t.decoderConfig.codec!=="mp4a.6b"){if(!["mp4a.40.2","mp4a.40.02","mp4a.40.5","mp4a.40.05","mp4a.40.29","mp4a.67"].includes(t.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(t.decoderConfig.codec.startsWith("mp3")||t.decoderConfig.codec.startsWith("mp4a")){if(t.decoderConfig.codec!=="mp3"&&t.decoderConfig.codec!=="mp4a.69"&&t.decoderConfig.codec!=="mp4a.6B"&&t.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(t.decoderConfig.codec.startsWith("opus")){if(t.decoderConfig.codec!=="opus")throw new TypeError('Audio chunk metadata decoder configuration codec string for Opus must be "opus".');if(t.decoderConfig.description&&t.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(t.decoderConfig.codec.startsWith("vorbis")){if(t.decoderConfig.codec!=="vorbis")throw new TypeError('Audio chunk metadata decoder configuration codec string for Vorbis must be "vorbis".');if(!t.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(t.decoderConfig.codec.startsWith("flac")){if(t.decoderConfig.codec!=="flac")throw new TypeError('Audio chunk metadata decoder configuration codec string for FLAC must be "flac".');if(!t.decoderConfig.description||t.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((t.decoderConfig.codec.startsWith("pcm")||t.decoderConfig.codec.startsWith("ulaw")||t.decoderConfig.codec.startsWith("alaw"))&&!Q.includes(t.decoderConfig.codec))throw new TypeError(`Audio chunk metadata decoder configuration codec string for PCM must be one of the supported PCM codecs (${Q.join(", ")}).`)},Vt=t=>{if(!t)throw new TypeError("Subtitle metadata must be provided.");if(typeof t!="object")throw new TypeError("Subtitle metadata must be an object.");if(!t.config)throw new TypeError("Subtitle metadata must include a config object.");if(typeof t.config!="object")throw new TypeError("Subtitle metadata config must be an object.");if(typeof t.config.description!="string")throw new TypeError("Subtitle metadata config description must be a string.")};/*!
|
|
40
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
41
|
+
*
|
|
42
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
43
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
44
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
45
|
+
*/var Ae;(function(t){t[t.NON_IDR_SLICE=1]="NON_IDR_SLICE",t[t.SLICE_DPA=2]="SLICE_DPA",t[t.SLICE_DPB=3]="SLICE_DPB",t[t.SLICE_DPC=4]="SLICE_DPC",t[t.IDR=5]="IDR",t[t.SEI=6]="SEI",t[t.SPS=7]="SPS",t[t.PPS=8]="PPS",t[t.AUD=9]="AUD",t[t.SPS_EXT=13]="SPS_EXT"})(Ae||(Ae={}));var K;(function(t){t[t.RASL_N=8]="RASL_N",t[t.RASL_R=9]="RASL_R",t[t.BLA_W_LP=16]="BLA_W_LP",t[t.RSV_IRAP_VCL23=23]="RSV_IRAP_VCL23",t[t.VPS_NUT=32]="VPS_NUT",t[t.SPS_NUT=33]="SPS_NUT",t[t.PPS_NUT=34]="PPS_NUT",t[t.AUD_NUT=35]="AUD_NUT",t[t.PREFIX_SEI_NUT=39]="PREFIX_SEI_NUT",t[t.SUFFIX_SEI_NUT=40]="SUFFIX_SEI_NUT"})(K||(K={}));const Xe=function*(t){let e=0,r=-1;for(;e<t.length-2;){const i=t.indexOf(0,e);if(i===-1||i>=t.length-2)break;e=i;let s=0;if(e+3<t.length&&t[e+1]===0&&t[e+2]===0&&t[e+3]===1?s=4:t[e+1]===0&&t[e+2]===1&&(s=3),s===0){e++;continue}r!==-1&&e>r&&(yield{offset:r,length:e-r}),r=e+s,e=r}r!==-1&&r<t.length&&(yield{offset:r,length:t.length-r})},fi=t=>t&31,Ke=t=>{const e=[],r=t.length;for(let i=0;i<r;i++)i+2<r&&t[i]===0&&t[i+1]===0&&t[i+2]===3?(e.push(0,0),i+=2):e.push(t[i]);return new Uint8Array(e)},mi=(t,e)=>{const r=t.reduce((n,o)=>n+e+o.byteLength,0),i=new Uint8Array(r);let s=0;for(const n of t){const o=new DataView(i.buffer,i.byteOffset,i.byteLength);switch(e){case 1:o.setUint8(s,n.byteLength);break;case 2:o.setUint16(s,n.byteLength,!1);break;case 3:Le(o,s,n.byteLength,!1);break;case 4:o.setUint32(s,n.byteLength,!1);break}s+=e,i.set(n,s),s+=n.byteLength}return i},pi=t=>{try{const e=[],r=[],i=[];for(const a of Xe(t)){const c=t.subarray(a.offset,a.offset+a.length),l=fi(c[0]);l===Ae.SPS?e.push(c):l===Ae.PPS?r.push(c):l===Ae.SPS_EXT&&i.push(c)}if(e.length===0||r.length===0)return null;const s=e[0],n=wi(s);m(n!==null);const o=n.profileIdc===100||n.profileIdc===110||n.profileIdc===122||n.profileIdc===144;return{configurationVersion:1,avcProfileIndication:n.profileIdc,profileCompatibility:n.constraintFlags,avcLevelIndication:n.levelIdc,lengthSizeMinusOne:3,sequenceParameterSets:e,pictureParameterSets:r,chromaFormat:o?n.chromaFormatIdc:null,bitDepthLumaMinus8:o?n.bitDepthLumaMinus8:null,bitDepthChromaMinus8:o?n.bitDepthChromaMinus8:null,sequenceParameterSetExt:o?i:null}}catch(e){return console.error("Error building AVC Decoder Configuration Record:",e),null}},gi=t=>{const e=[];e.push(t.configurationVersion),e.push(t.avcProfileIndication),e.push(t.profileCompatibility),e.push(t.avcLevelIndication),e.push(252|t.lengthSizeMinusOne&3),e.push(224|t.sequenceParameterSets.length&31);for(const r of t.sequenceParameterSets){const i=r.byteLength;e.push(i>>8),e.push(i&255);for(let s=0;s<i;s++)e.push(r[s])}e.push(t.pictureParameterSets.length);for(const r of t.pictureParameterSets){const i=r.byteLength;e.push(i>>8),e.push(i&255);for(let s=0;s<i;s++)e.push(r[s])}if(t.avcProfileIndication===100||t.avcProfileIndication===110||t.avcProfileIndication===122||t.avcProfileIndication===144){m(t.chromaFormat!==null),m(t.bitDepthLumaMinus8!==null),m(t.bitDepthChromaMinus8!==null),m(t.sequenceParameterSetExt!==null),e.push(252|t.chromaFormat&3),e.push(248|t.bitDepthLumaMinus8&7),e.push(248|t.bitDepthChromaMinus8&7),e.push(t.sequenceParameterSetExt.length);for(const r of t.sequenceParameterSetExt){const i=r.byteLength;e.push(i>>8),e.push(i&255);for(let s=0;s<i;s++)e.push(r[s])}}return new Uint8Array(e)},wi=t=>{try{const e=new ee(Ke(t));if(e.skipBits(1),e.skipBits(2),e.readBits(5)!==7)return null;const i=e.readAlignedByte(),s=e.readAlignedByte(),n=e.readAlignedByte();b(e);let o=1,a=0,c=0,l=0;if((i===100||i===110||i===122||i===244||i===44||i===83||i===86||i===118||i===128)&&(o=b(e),o===3&&(l=e.readBits(1)),a=b(e),c=b(e),e.skipBits(1),e.readBits(1))){for(let x=0;x<(o!==3?8:12);x++)if(e.readBits(1)){const N=x<6?16:64;let U=8,B=8;for(let G=0;G<N;G++){if(B!==0){const Te=te(e);B=(U+Te+256)%256}U=B===0?U:B}}}b(e);const d=b(e);if(d===0)b(e);else if(d===1){e.skipBits(1),te(e),te(e);const V=b(e);for(let x=0;x<V;x++)te(e)}b(e),e.skipBits(1);const u=b(e),f=b(e),g=16*(u+1),p=16*(f+1);let y=g,T=p;const C=e.readBits(1);if(C||e.skipBits(1),e.skipBits(1),e.readBits(1)){const V=b(e),x=b(e),O=b(e),N=b(e);let U,B;if((l===0?o:0)===0)U=1,B=2-C;else{const Te=o===3?1:2,pt=o===1?2:1;U=Te,B=pt*(2-C)}y-=U*(V+x),T-=B*(O+N)}let _=2,F=2,M=2,L=0,W=null,I=null;if(e.readBits(1)){e.readBits(1)&&e.readBits(8)===255&&(e.skipBits(16),e.skipBits(16)),e.readBits(1)&&e.skipBits(1),e.readBits(1)&&(e.skipBits(3),L=e.readBits(1),e.readBits(1)&&(_=e.readBits(8),F=e.readBits(8),M=e.readBits(8))),e.readBits(1)&&(b(e),b(e)),e.readBits(1)&&(e.skipBits(32),e.skipBits(32),e.skipBits(1));const B=e.readBits(1);B&&zt(e);const G=e.readBits(1);G&&zt(e),(B||G)&&e.skipBits(1),e.skipBits(1),e.readBits(1)&&(e.skipBits(1),b(e),b(e),b(e),b(e),W=b(e),I=b(e))}if(W===null){m(I===null);const V=s&16;if((i===44||i===86||i===100||i===110||i===122||i===244)&&V)W=0,I=0;else{const x=u+1,O=f+1,N=(2-C)*O,U=Me.find(G=>G.level>=n)??j(Me),B=Math.min(Math.floor(U.maxDpbMbs/(x*N)),16);W=B,I=B}}return m(I!==null),{profileIdc:i,constraintFlags:s,levelIdc:n,frameMbsOnlyFlag:C,chromaFormatIdc:o,bitDepthLumaMinus8:a,bitDepthChromaMinus8:c,codedWidth:g,codedHeight:p,displayWidth:y,displayHeight:T,colourPrimaries:_,matrixCoefficients:M,transferCharacteristics:F,fullRangeFlag:L,numReorderFrames:W,maxDecFrameBuffering:I}}catch(e){return console.error("Error parsing AVC SPS:",e),null}},zt=t=>{const e=b(t);t.skipBits(4),t.skipBits(4);for(let r=0;r<=e;r++)b(t),b(t),t.skipBits(1);t.skipBits(5),t.skipBits(5),t.skipBits(5),t.skipBits(5)},Ut=t=>t>>1&63,bi=t=>{try{const e=new ee(Ke(t));e.skipBits(16),e.readBits(4);const r=e.readBits(3),i=e.readBits(1),{general_profile_space:s,general_tier_flag:n,general_profile_idc:o,general_profile_compatibility_flags:a,general_constraint_indicator_flags:c,general_level_idc:l}=Ti(e,r);b(e);const d=b(e);let u=0;d===3&&(u=e.readBits(1));const f=b(e),g=b(e);let p=f,y=g;if(e.readBits(1)){const x=b(e),O=b(e),N=b(e),U=b(e);let B=1,G=1;const Te=u===0?d:0;Te===1?(B=2,G=2):Te===2&&(B=2,G=1),p-=(x+O)*B,y-=(N+U)*G}const T=b(e),C=b(e);b(e);const _=e.readBits(1)?0:r;let F=0;for(let x=_;x<=r;x++)b(e),F=b(e),b(e);b(e),b(e),b(e),b(e),b(e),b(e),e.readBits(1)&&e.readBits(1)&&Ci(e),e.skipBits(1),e.skipBits(1),e.readBits(1)&&(e.skipBits(4),e.skipBits(4),b(e),b(e),e.skipBits(1));const M=b(e);if(vi(e,M),e.readBits(1)){const x=b(e);for(let O=0;O<x;O++)b(e),e.skipBits(1)}e.skipBits(1),e.skipBits(1);let L=2,W=2,I=2,P=0,V=0;if(e.readBits(1)){const x=xi(e,r);L=x.colourPrimaries,W=x.transferCharacteristics,I=x.matrixCoefficients,P=x.fullRangeFlag,V=x.minSpatialSegmentationIdc}return{displayWidth:p,displayHeight:y,colourPrimaries:L,transferCharacteristics:W,matrixCoefficients:I,fullRangeFlag:P,maxDecFrameBuffering:F+1,spsMaxSubLayersMinus1:r,spsTemporalIdNestingFlag:i,generalProfileSpace:s,generalTierFlag:n,generalProfileIdc:o,generalProfileCompatibilityFlags:a,generalConstraintIndicatorFlags:c,generalLevelIdc:l,chromaFormatIdc:d,bitDepthLumaMinus8:T,bitDepthChromaMinus8:C,minSpatialSegmentationIdc:V}}catch(e){return console.error("Error parsing HEVC SPS:",e),null}},yi=t=>{try{const e=[],r=[],i=[],s=[];for(const l of Xe(t)){const d=t.subarray(l.offset,l.offset+l.length),u=Ut(d[0]);u===K.VPS_NUT?e.push(d):u===K.SPS_NUT?r.push(d):u===K.PPS_NUT?i.push(d):(u===K.PREFIX_SEI_NUT||u===K.SUFFIX_SEI_NUT)&&s.push(d)}if(r.length===0||i.length===0)return null;const n=bi(r[0]);if(!n)return null;let o=0;if(i.length>0){const l=i[0],d=new ee(Ke(l));d.skipBits(16),b(d),b(d),d.skipBits(1),d.skipBits(1),d.skipBits(3),d.skipBits(1),d.skipBits(1),b(d),b(d),te(d),d.skipBits(1),d.skipBits(1),d.readBits(1)&&b(d),te(d),te(d),d.skipBits(1),d.skipBits(1),d.skipBits(1),d.skipBits(1);const u=d.readBits(1),f=d.readBits(1);!u&&!f?o=0:u&&!f?o=2:!u&&f?o=3:o=0}const a=[...e.length?[{arrayCompleteness:1,nalUnitType:K.VPS_NUT,nalUnits:e}]:[],...r.length?[{arrayCompleteness:1,nalUnitType:K.SPS_NUT,nalUnits:r}]:[],...i.length?[{arrayCompleteness:1,nalUnitType:K.PPS_NUT,nalUnits:i}]:[],...s.length?[{arrayCompleteness:1,nalUnitType:Ut(s[0][0]),nalUnits:s}]:[]];return{configurationVersion:1,generalProfileSpace:n.generalProfileSpace,generalTierFlag:n.generalTierFlag,generalProfileIdc:n.generalProfileIdc,generalProfileCompatibilityFlags:n.generalProfileCompatibilityFlags,generalConstraintIndicatorFlags:n.generalConstraintIndicatorFlags,generalLevelIdc:n.generalLevelIdc,minSpatialSegmentationIdc:n.minSpatialSegmentationIdc,parallelismType:o,chromaFormatIdc:n.chromaFormatIdc,bitDepthLumaMinus8:n.bitDepthLumaMinus8,bitDepthChromaMinus8:n.bitDepthChromaMinus8,avgFrameRate:0,constantFrameRate:0,numTemporalLayers:n.spsMaxSubLayersMinus1+1,temporalIdNested:n.spsTemporalIdNestingFlag,lengthSizeMinusOne:3,arrays:a}}catch(e){return console.error("Error building HEVC Decoder Configuration Record:",e),null}},Ti=(t,e)=>{const r=t.readBits(2),i=t.readBits(1),s=t.readBits(5);let n=0;for(let d=0;d<32;d++)n=n<<1|t.readBits(1);const o=new Uint8Array(6);for(let d=0;d<6;d++)o[d]=t.readBits(8);const a=t.readBits(8),c=[],l=[];for(let d=0;d<e;d++)c.push(t.readBits(1)),l.push(t.readBits(1));if(e>0)for(let d=e;d<8;d++)t.skipBits(2);for(let d=0;d<e;d++)c[d]&&t.skipBits(88),l[d]&&t.skipBits(8);return{general_profile_space:r,general_tier_flag:i,general_profile_idc:s,general_profile_compatibility_flags:n,general_constraint_indicator_flags:o,general_level_idc:a}},Ci=t=>{for(let e=0;e<4;e++)for(let r=0;r<(e===3?2:6);r++)if(!t.readBits(1))b(t);else{const s=Math.min(64,1<<4+(e<<1));e>1&&te(t);for(let n=0;n<s;n++)te(t)}},vi=(t,e)=>{const r=[];for(let i=0;i<e;i++)r[i]=Si(t,i,e,r)},Si=(t,e,r,i)=>{let s=0,n=0,o=0;if(e!==0&&(n=t.readBits(1)),n){if(e===r){const c=b(t);o=e-(c+1)}else o=e-1;t.readBits(1),b(t);const a=i[o]??0;for(let c=0;c<=a;c++)t.readBits(1)||t.readBits(1);s=i[o]}else{const a=b(t),c=b(t);for(let l=0;l<a;l++)b(t),t.readBits(1);for(let l=0;l<c;l++)b(t),t.readBits(1);s=a+c}return s},xi=(t,e)=>{let r=2,i=2,s=2,n=0,o=0;return t.readBits(1)&&t.readBits(8)===255&&(t.readBits(16),t.readBits(16)),t.readBits(1)&&t.readBits(1),t.readBits(1)&&(t.readBits(3),n=t.readBits(1),t.readBits(1)&&(r=t.readBits(8),i=t.readBits(8),s=t.readBits(8))),t.readBits(1)&&(b(t),b(t)),t.readBits(1),t.readBits(1),t.readBits(1),t.readBits(1)&&(b(t),b(t),b(t),b(t)),t.readBits(1)&&(t.readBits(32),t.readBits(32),t.readBits(1)&&b(t),t.readBits(1)&&ki(t,!0,e)),t.readBits(1)&&(t.readBits(1),t.readBits(1),t.readBits(1),o=b(t),b(t),b(t),b(t),b(t)),{colourPrimaries:r,transferCharacteristics:i,matrixCoefficients:s,fullRangeFlag:n,minSpatialSegmentationIdc:o}},ki=(t,e,r)=>{let i=!1,s=!1,n=!1;i=t.readBits(1)===1,s=t.readBits(1)===1,(i||s)&&(n=t.readBits(1)===1,n&&(t.readBits(8),t.readBits(5),t.readBits(1),t.readBits(5)),t.readBits(4),t.readBits(4),n&&t.readBits(4),t.readBits(5),t.readBits(5),t.readBits(5));for(let o=0;o<=r;o++){const a=t.readBits(1)===1;let c=!0;a||(c=t.readBits(1)===1);let l=!1;c?b(t):l=t.readBits(1)===1;let d=1;l||(d=b(t)+1),i&&Wt(t,d,n),s&&Wt(t,d,n)}},Wt=(t,e,r)=>{for(let i=0;i<e;i++)b(t),b(t),r&&(b(t),b(t)),t.readBits(1)},Ei=t=>{const e=[];e.push(t.configurationVersion),e.push((t.generalProfileSpace&3)<<6|(t.generalTierFlag&1)<<5|t.generalProfileIdc&31),e.push(t.generalProfileCompatibilityFlags>>>24&255),e.push(t.generalProfileCompatibilityFlags>>>16&255),e.push(t.generalProfileCompatibilityFlags>>>8&255),e.push(t.generalProfileCompatibilityFlags&255),e.push(...t.generalConstraintIndicatorFlags),e.push(t.generalLevelIdc&255),e.push(240|t.minSpatialSegmentationIdc>>8&15),e.push(t.minSpatialSegmentationIdc&255),e.push(252|t.parallelismType&3),e.push(252|t.chromaFormatIdc&3),e.push(248|t.bitDepthLumaMinus8&7),e.push(248|t.bitDepthChromaMinus8&7),e.push(t.avgFrameRate>>8&255),e.push(t.avgFrameRate&255),e.push((t.constantFrameRate&3)<<6|(t.numTemporalLayers&7)<<3|(t.temporalIdNested&1)<<2|t.lengthSizeMinusOne&3),e.push(t.arrays.length&255);for(const r of t.arrays){e.push((r.arrayCompleteness&1)<<7|0|r.nalUnitType&63),e.push(r.nalUnits.length>>8&255),e.push(r.nalUnits.length&255);for(const i of r.nalUnits){e.push(i.length>>8&255),e.push(i.length&255);for(let s=0;s<i.length;s++)e.push(i[s])}}return new Uint8Array(e)},Nt=t=>{const e=oe(t),r=e.getUint8(9),i=e.getUint16(10,!0),s=e.getUint32(12,!0),n=e.getInt16(16,!0),o=e.getUint8(18);let a=null;return o&&(a=t.subarray(19,21+r)),{outputChannelCount:r,preSkip:i,inputSampleRate:s,outputGain:n,channelMappingFamily:o,channelMappingTable:a}};var Dt;(function(t){t[t.STREAMINFO=0]="STREAMINFO",t[t.VORBIS_COMMENT=4]="VORBIS_COMMENT",t[t.PICTURE=6]="PICTURE"})(Dt||(Dt={}));/*!
|
|
46
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
47
|
+
*
|
|
48
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
49
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
50
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
51
|
+
*/const Ai=[],_i=[];/*!
|
|
52
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
53
|
+
*
|
|
54
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
55
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
56
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
57
|
+
*/const Lt=new Uint8Array(0);class ce{constructor(e,r,i,s,n=-1,o,a){if(this.data=e,this.type=r,this.timestamp=i,this.duration=s,this.sequenceNumber=n,e===Lt&&o===void 0)throw new Error("Internal error: byteLength must be explicitly provided when constructing metadata-only packets.");if(o===void 0&&(o=e.byteLength),!(e instanceof Uint8Array))throw new TypeError("data must be a Uint8Array.");if(r!=="key"&&r!=="delta")throw new TypeError('type must be either "key" or "delta".');if(!Number.isFinite(i))throw new TypeError("timestamp must be a number.");if(!Number.isFinite(s)||s<0)throw new TypeError("duration must be a non-negative number.");if(!Number.isFinite(n))throw new TypeError("sequenceNumber must be a number.");if(!Number.isInteger(o)||o<0)throw new TypeError("byteLength must be a non-negative integer.");if(a!==void 0&&(typeof a!="object"||!a))throw new TypeError("sideData, when provided, must be an object.");if(a?.alpha!==void 0&&!(a.alpha instanceof Uint8Array))throw new TypeError("sideData.alpha, when provided, must be a Uint8Array.");if(a?.alphaByteLength!==void 0&&(!Number.isInteger(a.alphaByteLength)||a.alphaByteLength<0))throw new TypeError("sideData.alphaByteLength, when provided, must be a non-negative integer.");this.byteLength=o,this.sideData=a??{},this.sideData.alpha&&this.sideData.alphaByteLength===void 0&&(this.sideData.alphaByteLength=this.sideData.alpha.byteLength)}get isMetadataOnly(){return this.data===Lt}get microsecondTimestamp(){return Math.trunc(ae*this.timestamp)}get microsecondDuration(){return Math.trunc(ae*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(e=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:e,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(e,r){if(!(e instanceof EncodedVideoChunk||e instanceof EncodedAudioChunk))throw new TypeError("chunk must be an EncodedVideoChunk or EncodedAudioChunk.");const i=new Uint8Array(e.byteLength);return e.copyTo(i),new ce(i,e.type,e.timestamp/1e6,(e.duration??0)/1e6,void 0,void 0,r)}clone(e){if(e!==void 0&&(typeof e!="object"||e===null))throw new TypeError("options, when provided, must be an object.");if(e?.data!==void 0&&!(e.data instanceof Uint8Array))throw new TypeError("options.data, when provided, must be a Uint8Array.");if(e?.type!==void 0&&e.type!=="key"&&e.type!=="delta")throw new TypeError('options.type, when provided, must be either "key" or "delta".');if(e?.timestamp!==void 0&&!Number.isFinite(e.timestamp))throw new TypeError("options.timestamp, when provided, must be a number.");if(e?.duration!==void 0&&!Number.isFinite(e.duration))throw new TypeError("options.duration, when provided, must be a number.");if(e?.sequenceNumber!==void 0&&!Number.isFinite(e.sequenceNumber))throw new TypeError("options.sequenceNumber, when provided, must be a number.");if(e?.sideData!==void 0&&(typeof e.sideData!="object"||e.sideData===null))throw new TypeError("options.sideData, when provided, must be an object.");return new ce(e?.data??this.data,e?.type??this.type,e?.timestamp??this.timestamp,e?.duration??this.duration,e?.sequenceNumber??this.sequenceNumber,this.byteLength,e?.sideData??this.sideData)}}/*!
|
|
58
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
59
|
+
*
|
|
60
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
61
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
62
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
63
|
+
*/const Pi=t=>{let i=t,s=4096,n=0,o=12,a=0;for(i<0&&(i=-i,n=128),i+=33,i>8191&&(i=8191);(i&s)!==s&&o>=5;)s>>=1,o--;return a=i>>o-4&15,~(n|o-5<<4|a)&255},Fi=t=>{let r=2048,i=0,s=11,n=0,o=t;for(o<0&&(o=-o,i=128),o>4095&&(o=4095);(o&r)!==r&&s>=5;)r>>=1,s--;return n=o>>(s===4?1:s-4)&15,(i|s-4<<4|n)^85};/*!
|
|
64
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
65
|
+
*
|
|
66
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
67
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
68
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
69
|
+
*/Yr();let Ht=-1/0,$t=-1/0,_e=null;typeof FinalizationRegistry<"u"&&(_e=new FinalizationRegistry(t=>{const e=Date.now();t.type==="video"?(e-Ht>=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."),Ht=e),typeof VideoFrame<"u"&&t.data instanceof VideoFrame&&t.data.close()):(e-$t>=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."),$t=e),typeof AudioData<"u"&&t.data instanceof AudioData&&t.data.close())}));const jt=["I420","I420P10","I420P12","I420A","I420AP10","I420AP12","I422","I422P10","I422P12","I422A","I422AP10","I422AP12","I444","I444P10","I444P12","I444A","I444AP10","I444AP12","NV12","RGBA","RGBX","BGRA","BGRX"],Bi=new Set(jt);class ie{get displayWidth(){return this.rotation%180===0?this.codedWidth:this.codedHeight}get displayHeight(){return this.rotation%180===0?this.codedHeight:this.codedWidth}get microsecondTimestamp(){return Math.trunc(ae*this.timestamp)}get microsecondDuration(){return Math.trunc(ae*this.duration)}get hasAlpha(){return this.format&&this.format.includes("A")}constructor(e,r){if(this._closed=!1,e instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&e instanceof SharedArrayBuffer||ArrayBuffer.isView(e)){if(!r||typeof r!="object")throw new TypeError("init must be an object.");if(r.format===void 0||!Bi.has(r.format))throw new TypeError("init.format must be one of: "+jt.join(", "));if(!Number.isInteger(r.codedWidth)||r.codedWidth<=0)throw new TypeError("init.codedWidth must be a positive integer.");if(!Number.isInteger(r.codedHeight)||r.codedHeight<=0)throw new TypeError("init.codedHeight must be a positive integer.");if(r.rotation!==void 0&&![0,90,180,270].includes(r.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(!Number.isFinite(r.timestamp))throw new TypeError("init.timestamp must be a number.");if(r.duration!==void 0&&(!Number.isFinite(r.duration)||r.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");this._data=q(e).slice(),this._layout=r.layout??Oi(r.format,r.codedWidth,r.codedHeight),this.format=r.format,this.codedWidth=r.codedWidth,this.codedHeight=r.codedHeight,this.rotation=r.rotation??0,this.timestamp=r.timestamp,this.duration=r.duration??0,this.colorSpace=new Ye(r.colorSpace)}else if(typeof VideoFrame<"u"&&e instanceof VideoFrame){if(r?.rotation!==void 0&&![0,90,180,270].includes(r.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(r?.timestamp!==void 0&&!Number.isFinite(r?.timestamp))throw new TypeError("init.timestamp, when provided, must be a number.");if(r?.duration!==void 0&&(!Number.isFinite(r.duration)||r.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");this._data=e,this._layout=null,this.format=e.format,this.codedWidth=e.displayWidth,this.codedHeight=e.displayHeight,this.rotation=r?.rotation??0,this.timestamp=r?.timestamp??e.timestamp/1e6,this.duration=r?.duration??(e.duration??0)/1e6,this.colorSpace=new Ye(e.colorSpace)}else if(typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement||typeof SVGImageElement<"u"&&e instanceof SVGImageElement||typeof ImageBitmap<"u"&&e instanceof ImageBitmap||typeof HTMLVideoElement<"u"&&e instanceof HTMLVideoElement||typeof HTMLCanvasElement<"u"&&e instanceof HTMLCanvasElement||typeof OffscreenCanvas<"u"&&e instanceof OffscreenCanvas){if(!r||typeof r!="object")throw new TypeError("init must be an object.");if(r.rotation!==void 0&&![0,90,180,270].includes(r.rotation))throw new TypeError("init.rotation, when provided, must be 0, 90, 180, or 270.");if(!Number.isFinite(r.timestamp))throw new TypeError("init.timestamp must be a number.");if(r.duration!==void 0&&(!Number.isFinite(r.duration)||r.duration<0))throw new TypeError("init.duration, when provided, must be a non-negative number.");if(typeof VideoFrame<"u")return new ie(new VideoFrame(e,{timestamp:Math.trunc(r.timestamp*ae),duration:Math.trunc((r.duration??0)*ae)||void 0}),r);let i=0,s=0;if("naturalWidth"in e?(i=e.naturalWidth,s=e.naturalHeight):"videoWidth"in e?(i=e.videoWidth,s=e.videoHeight):"width"in e&&(i=Number(e.width),s=Number(e.height)),!i||!s)throw new TypeError("Could not determine dimensions.");const n=new OffscreenCanvas(i,s),o=n.getContext("2d",{alpha:je(),willReadFrequently:!0});m(o),o.drawImage(e,0,0),this._data=n,this._layout=null,this.format="RGBX",this.codedWidth=i,this.codedHeight=s,this.rotation=r.rotation??0,this.timestamp=r.timestamp,this.duration=r.duration??0,this.colorSpace=new Ye({matrix:"rgb",primaries:"bt709",transfer:"iec61966-2-1",fullRange:!0})}else throw new TypeError("Invalid data type: Must be a BufferSource or CanvasImageSource.");_e?.register(this,{type:"video",data:this._data},this)}clone(){if(this._closed)throw new Error("VideoSample is closed.");return m(this._data!==null),ge(this._data)?new ie(this._data.clone(),{timestamp:this.timestamp,duration:this.duration,rotation:this.rotation}):this._data instanceof Uint8Array?(m(this._layout),new ie(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})):new ie(this._data,{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.timestamp,duration:this.duration,colorSpace:this.colorSpace,rotation:this.rotation})}close(){this._closed||(_e?.unregister(this),ge(this._data)?this._data.close():this._data=null,this._closed=!0)}allocationSize(e={}){if(qt(e),this._closed)throw new Error("VideoSample is closed.");if(this.format===null)throw new Error("Cannot get allocation size when format is null. Sorry!");if(m(this._data!==null),!ge(this._data)&&(e.colorSpace||e.format&&e.format!==this.format||e.layout||e.rect)){const r=this.toVideoFrame(),i=r.allocationSize(e);return r.close(),i}return ge(this._data)?this._data.allocationSize(e):this._data instanceof Uint8Array?this._data.byteLength:this.codedWidth*this.codedHeight*4}async copyTo(e,r={}){if(!Oe(e))throw new TypeError("destination must be an ArrayBuffer or an ArrayBuffer view.");if(qt(r),this._closed)throw new Error("VideoSample is closed.");if(this.format===null)throw new Error("Cannot copy video sample data when format is null. Sorry!");if(m(this._data!==null),!ge(this._data)&&(r.colorSpace||r.format&&r.format!==this.format||r.layout||r.rect)){const i=this.toVideoFrame(),s=await i.copyTo(e,r);return i.close(),s}if(ge(this._data))return this._data.copyTo(e,r);if(this._data instanceof Uint8Array)return m(this._layout),q(e).set(this._data),this._layout;{const s=this._data.getContext("2d");m(s);const n=s.getImageData(0,0,this.codedWidth,this.codedHeight);return q(e).set(n.data),[{offset:0,stride:4*this.codedWidth}]}}toVideoFrame(){if(this._closed)throw new Error("VideoSample is closed.");return m(this._data!==null),ge(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}):new VideoFrame(this._data,{timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0})}draw(e,r,i,s,n,o,a,c,l){let d=0,u=0,f=this.displayWidth,g=this.displayHeight,p=0,y=0,T=this.displayWidth,C=this.displayHeight;if(o!==void 0?(d=r,u=i,f=s,g=n,p=o,y=a,c!==void 0?(T=c,C=l):(T=f,C=g)):(p=r,y=i,s!==void 0&&(T=s,C=n)),!(typeof CanvasRenderingContext2D<"u"&&e instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<"u"&&e instanceof OffscreenCanvasRenderingContext2D))throw new TypeError("context must be a CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D.");if(!Number.isFinite(d))throw new TypeError("sx must be a number.");if(!Number.isFinite(u))throw new TypeError("sy must be a number.");if(!Number.isFinite(f)||f<0)throw new TypeError("sWidth must be a non-negative number.");if(!Number.isFinite(g)||g<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(y))throw new TypeError("dy must be a number.");if(!Number.isFinite(T)||T<0)throw new TypeError("dWidth must be a non-negative number.");if(!Number.isFinite(C)||C<0)throw new TypeError("dHeight must be a non-negative number.");if(this._closed)throw new Error("VideoSample is closed.");({sx:d,sy:u,sWidth:f,sHeight:g}=this._rotateSourceRegion(d,u,f,g,this.rotation));const A=this.toCanvasImageSource();e.save();const _=p+T/2,F=y+C/2;e.translate(_,F),e.rotate(this.rotation*Math.PI/180);const M=this.rotation%180===0?1:T/C;e.scale(1/M,M),e.drawImage(A,d,u,f,g,-T/2,-C/2,T,C),e.restore()}drawWithFit(e,r){if(!(typeof CanvasRenderingContext2D<"u"&&e instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<"u"&&e instanceof OffscreenCanvasRenderingContext2D))throw new TypeError("context must be a CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(!["fill","contain","cover"].includes(r.fit))throw new TypeError("options.fit must be 'fill', 'contain', or 'cover'.");if(r.rotation!==void 0&&![0,90,180,270].includes(r.rotation))throw new TypeError("options.rotation, when provided, must be 0, 90, 180, or 270.");r.crop!==void 0&&Ii(r.crop,"options.");const i=e.canvas.width,s=e.canvas.height,n=r.rotation??this.rotation,[o,a]=n%180===0?[this.codedWidth,this.codedHeight]:[this.codedHeight,this.codedWidth];r.crop&&Ri(r.crop,o,a);let c,l,d,u;const{sx:f,sy:g,sWidth:p,sHeight:y}=this._rotateSourceRegion(r.crop?.left??0,r.crop?.top??0,r.crop?.width??o,r.crop?.height??a,n);if(r.fit==="fill")c=0,l=0,d=i,u=s;else{const[C,A]=r.crop?[r.crop.width,r.crop.height]:[o,a],_=r.fit==="contain"?Math.min(i/C,s/A):Math.max(i/C,s/A);d=C*_,u=A*_,c=(i-d)/2,l=(s-u)/2}e.save();const T=n%180===0?1:d/u;e.translate(i/2,s/2),e.rotate(n*Math.PI/180),e.scale(1/T,T),e.translate(-i/2,-s/2),e.drawImage(this.toCanvasImageSource(),f,g,p,y,c,l,d,u),e.restore()}_rotateSourceRegion(e,r,i,s,n){return n===90?[e,r,i,s]=[r,this.codedHeight-e-i,s,i]:n===180?[e,r]=[this.codedWidth-e-i,this.codedHeight-r-s]:n===270&&([e,r,i,s]=[this.codedWidth-r-s,e,s,i]),{sx:e,sy:r,sWidth:i,sHeight:s}}toCanvasImageSource(){if(this._closed)throw new Error("VideoSample is closed.");if(m(this._data!==null),this._data instanceof Uint8Array){const e=this.toVideoFrame();return queueMicrotask(()=>e.close()),e}else return this._data}setRotation(e){if(![0,90,180,270].includes(e))throw new TypeError("newRotation must be 0, 90, 180, or 270.");this.rotation=e}setTimestamp(e){if(!Number.isFinite(e))throw new TypeError("newTimestamp must be a number.");this.timestamp=e}setDuration(e){if(!Number.isFinite(e)||e<0)throw new TypeError("newDuration must be a non-negative number.");this.duration=e}[Symbol.dispose](){this.close()}}class Ye{constructor(e){this.primaries=e?.primaries??null,this.transfer=e?.transfer??null,this.matrix=e?.matrix??null,this.fullRange=e?.fullRange??null}toJSON(){return{primaries:this.primaries,transfer:this.transfer,matrix:this.matrix,fullRange:this.fullRange}}}const ge=t=>typeof VideoFrame<"u"&&t instanceof VideoFrame,Ri=(t,e,r)=>{t.left=Math.min(t.left,e),t.top=Math.min(t.top,r),t.width=Math.min(t.width,e-t.left),t.height=Math.min(t.height,r-t.top),m(t.width>=0),m(t.height>=0)},Ii=(t,e)=>{if(!t||typeof t!="object")throw new TypeError(e+"crop, when provided, must be an object.");if(!Number.isInteger(t.left)||t.left<0)throw new TypeError(e+"crop.left must be a non-negative integer.");if(!Number.isInteger(t.top)||t.top<0)throw new TypeError(e+"crop.top must be a non-negative integer.");if(!Number.isInteger(t.width)||t.width<0)throw new TypeError(e+"crop.width must be a non-negative integer.");if(!Number.isInteger(t.height)||t.height<0)throw new TypeError(e+"crop.height must be a non-negative integer.")},qt=t=>{if(!t||typeof t!="object")throw new TypeError("options must be an object.");if(t.colorSpace!==void 0&&!["display-p3","srgb"].includes(t.colorSpace))throw new TypeError("options.colorSpace, when provided, must be 'display-p3' or 'srgb'.");if(t.format!==void 0&&typeof t.format!="string")throw new TypeError("options.format, when provided, must be a string.");if(t.layout!==void 0){if(!Array.isArray(t.layout))throw new TypeError("options.layout, when provided, must be an array.");for(const e of t.layout){if(!e||typeof e!="object")throw new TypeError("Each entry in options.layout must be an object.");if(!Number.isInteger(e.offset)||e.offset<0)throw new TypeError("plane.offset must be a non-negative integer.");if(!Number.isInteger(e.stride)||e.stride<0)throw new TypeError("plane.stride must be a non-negative integer.")}}if(t.rect!==void 0){if(!t.rect||typeof t.rect!="object")throw new TypeError("options.rect, when provided, must be an object.");if(t.rect.x!==void 0&&(!Number.isInteger(t.rect.x)||t.rect.x<0))throw new TypeError("options.rect.x, when provided, must be a non-negative integer.");if(t.rect.y!==void 0&&(!Number.isInteger(t.rect.y)||t.rect.y<0))throw new TypeError("options.rect.y, when provided, must be a non-negative integer.");if(t.rect.width!==void 0&&(!Number.isInteger(t.rect.width)||t.rect.width<0))throw new TypeError("options.rect.width, when provided, must be a non-negative integer.");if(t.rect.height!==void 0&&(!Number.isInteger(t.rect.height)||t.rect.height<0))throw new TypeError("options.rect.height, when provided, must be a non-negative integer.")}},Oi=(t,e,r)=>{const i=Mi(t),s=[];let n=0;for(const o of i){const a=Math.ceil(e/o.widthDivisor),c=Math.ceil(r/o.heightDivisor),l=a*o.sampleBytes,d=l*c;s.push({offset:n,stride:l}),n+=d}return s},Mi=t=>{const e=(r,i,s,n,o)=>{const a=[{sampleBytes:r,widthDivisor:1,heightDivisor:1},{sampleBytes:i,widthDivisor:s,heightDivisor:n},{sampleBytes:i,widthDivisor:s,heightDivisor:n}];return o&&a.push({sampleBytes:r,widthDivisor:1,heightDivisor:1}),a};switch(t){case"I420":return e(1,1,2,2,!1);case"I420P10":case"I420P12":return e(2,2,2,2,!1);case"I420A":return e(1,1,2,2,!0);case"I420AP10":case"I420AP12":return e(2,2,2,2,!0);case"I422":return e(1,1,2,1,!1);case"I422P10":case"I422P12":return e(2,2,2,1,!1);case"I422A":return e(1,1,2,1,!0);case"I422AP10":case"I422AP12":return e(2,2,2,1,!0);case"I444":return e(1,1,1,1,!1);case"I444P10":case"I444P12":return e(2,2,1,1,!1);case"I444A":return e(1,1,1,1,!0);case"I444AP10":case"I444AP12":return e(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:ve(t),m(!1)}},Ze=new Set(["f32","f32-planar","s16","s16-planar","s32","s32-planar","u8","u8-planar"]);class le{get microsecondTimestamp(){return Math.trunc(ae*this.timestamp)}get microsecondDuration(){return Math.trunc(ae*this.duration)}constructor(e){if(this._closed=!1,Pe(e)){if(e.format===null)throw new TypeError("AudioData with null format is not supported.");this._data=e,this.format=e.format,this.sampleRate=e.sampleRate,this.numberOfFrames=e.numberOfFrames,this.numberOfChannels=e.numberOfChannels,this.timestamp=e.timestamp/1e6,this.duration=e.numberOfFrames/e.sampleRate}else{if(!e||typeof e!="object")throw new TypeError("Invalid AudioDataInit: must be an object.");if(!Ze.has(e.format))throw new TypeError("Invalid AudioDataInit: invalid format.");if(!Number.isFinite(e.sampleRate)||e.sampleRate<=0)throw new TypeError("Invalid AudioDataInit: sampleRate must be > 0.");if(!Number.isInteger(e.numberOfChannels)||e.numberOfChannels===0)throw new TypeError("Invalid AudioDataInit: numberOfChannels must be an integer > 0.");if(!Number.isFinite(e?.timestamp))throw new TypeError("init.timestamp must be a number.");const r=e.data.byteLength/(we(e.format)*e.numberOfChannels);if(!Number.isInteger(r))throw new TypeError("Invalid AudioDataInit: data size is not a multiple of frame size.");this.format=e.format,this.sampleRate=e.sampleRate,this.numberOfFrames=r,this.numberOfChannels=e.numberOfChannels,this.timestamp=e.timestamp,this.duration=r/e.sampleRate;let i;if(e.data instanceof ArrayBuffer)i=new Uint8Array(e.data);else if(ArrayBuffer.isView(e.data))i=new Uint8Array(e.data.buffer,e.data.byteOffset,e.data.byteLength);else throw new TypeError("Invalid AudioDataInit: data is not a BufferSource.");const s=this.numberOfFrames*this.numberOfChannels*we(this.format);if(i.byteLength<s)throw new TypeError("Invalid AudioDataInit: insufficient data size.");this._data=i}_e?.register(this,{type:"audio",data:this._data},this)}allocationSize(e){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&&!Ze.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.");const r=e.format??this.format,i=e.frameOffset??0;if(i>=this.numberOfFrames)throw new RangeError("frameOffset out of range");const s=e.frameCount!==void 0?e.frameCount:this.numberOfFrames-i;if(s>this.numberOfFrames-i)throw new RangeError("frameCount out of range");const n=we(r),o=xe(r);if(o&&e.planeIndex>=this.numberOfChannels)throw new RangeError("planeIndex out of range");if(!o&&e.planeIndex!==0)throw new RangeError("planeIndex out of range");return(o?s:s*this.numberOfChannels)*n}copyTo(e,r){if(!Oe(e))throw new TypeError("destination must be an ArrayBuffer or an ArrayBuffer view.");if(!r||typeof r!="object")throw new TypeError("options must be an object.");if(!Number.isInteger(r.planeIndex)||r.planeIndex<0)throw new TypeError("planeIndex must be a non-negative integer.");if(r.format!==void 0&&!Ze.has(r.format))throw new TypeError("Invalid format.");if(r.frameOffset!==void 0&&(!Number.isInteger(r.frameOffset)||r.frameOffset<0))throw new TypeError("frameOffset must be a non-negative integer.");if(r.frameCount!==void 0&&(!Number.isInteger(r.frameCount)||r.frameCount<0))throw new TypeError("frameCount must be a non-negative integer.");if(this._closed)throw new Error("AudioSample is closed.");const{planeIndex:i,format:s,frameCount:n,frameOffset:o}=r,a=this.format,c=s??this.format;if(!c)throw new Error("Destination format not determined");const l=this.numberOfFrames,d=this.numberOfChannels,u=o??0;if(u>=l)throw new RangeError("frameOffset out of range");const f=n!==void 0?n:l-u;if(f>l-u)throw new RangeError("frameCount out of range");const g=we(c),p=xe(c);if(p&&i>=d)throw new RangeError("planeIndex out of range");if(!p&&i!==0)throw new RangeError("planeIndex out of range");const T=(p?f:f*d)*g;if(e.byteLength<T)throw new RangeError("Destination buffer is too small");const C=oe(e),A=Gt(c);if(Pe(this._data))Gr()&&d>2&&c!==a?Vi(this._data,C,a,c,d,i,u,f):this._data.copyTo(e,{planeIndex:i,frameOffset:u,frameCount:f,format:c});else{const _=this._data,F=oe(_),M=Qt(a),L=we(a),W=xe(a);for(let I=0;I<f;I++)if(p){const P=I*g;let V;W?V=(i*l+(I+u))*L:V=((I+u)*d+i)*L;const x=M(F,V);A(C,P,x)}else for(let P=0;P<d;P++){const x=(I*d+P)*g;let O;W?O=(P*l+(I+u))*L:O=((I+u)*d+P)*L;const N=M(F,O);A(C,x,N)}}}clone(){if(this._closed)throw new Error("AudioSample is closed.");if(Pe(this._data)){const e=new le(this._data.clone());return e.setTimestamp(this.timestamp),e}else return new le({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.timestamp,data:this._data})}close(){this._closed||(_e?.unregister(this),Pe(this._data)?this._data.close():this._data=new Uint8Array(0),this._closed=!0)}toAudioData(){if(this._closed)throw new Error("AudioSample is closed.");if(Pe(this._data)){if(this._data.timestamp===this.microsecondTimestamp)return this._data.clone();if(xe(this.format)){const e=this.allocationSize({planeIndex:0,format:this.format}),r=new ArrayBuffer(e*this.numberOfChannels);for(let i=0;i<this.numberOfChannels;i++)this.copyTo(new Uint8Array(r,i*e,e),{planeIndex:i,format:this.format});return new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:r})}else{const e=new ArrayBuffer(this.allocationSize({planeIndex:0,format:this.format}));return this.copyTo(e,{planeIndex:0,format:this.format}),new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:e})}}else return 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()})}toAudioBuffer(){if(this._closed)throw new Error("AudioSample is closed.");const e=new AudioBuffer({numberOfChannels:this.numberOfChannels,length:this.numberOfFrames,sampleRate:this.sampleRate}),r=new Float32Array(this.allocationSize({planeIndex:0,format:"f32-planar"})/4);for(let i=0;i<this.numberOfChannels;i++)this.copyTo(r,{planeIndex:i,format:"f32-planar"}),e.copyToChannel(r,i);return e}setTimestamp(e){if(!Number.isFinite(e))throw new TypeError("newTimestamp must be a number.");this.timestamp=e}[Symbol.dispose](){this.close()}static*_fromAudioBuffer(e,r){if(!(e instanceof AudioBuffer))throw new TypeError("audioBuffer must be an AudioBuffer.");const i=48e3*5,s=e.numberOfChannels,n=e.sampleRate,o=e.length,a=Math.floor(i/s);let c=0,l=o;for(;l>0;){const d=Math.min(a,l),u=new Float32Array(s*d);for(let f=0;f<s;f++)e.copyFromChannel(u.subarray(f*d,(f+1)*d),f,c);yield new le({format:"f32-planar",sampleRate:n,numberOfFrames:d,numberOfChannels:s,timestamp:r+c/n,data:u}),c+=d,l-=d}}static fromAudioBuffer(e,r){if(!(e instanceof AudioBuffer))throw new TypeError("audioBuffer must be an AudioBuffer.");const i=48e3*5,s=e.numberOfChannels,n=e.sampleRate,o=e.length,a=Math.floor(i/s);let c=0,l=o;const d=[];for(;l>0;){const u=Math.min(a,l),f=new Float32Array(s*u);for(let p=0;p<s;p++)e.copyFromChannel(f.subarray(p*u,(p+1)*u),p,c);const g=new le({format:"f32-planar",sampleRate:n,numberOfFrames:u,numberOfChannels:s,timestamp:r+c/n,data:f});d.push(g),c+=u,l-=u}return d}}const we=t=>{switch(t){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")}},xe=t=>{switch(t){case"u8-planar":case"s16-planar":case"s32-planar":case"f32-planar":return!0;default:return!1}},Qt=t=>{switch(t){case"u8":case"u8-planar":return(e,r)=>(e.getUint8(r)-128)/128;case"s16":case"s16-planar":return(e,r)=>e.getInt16(r,!0)/32768;case"s32":case"s32-planar":return(e,r)=>e.getInt32(r,!0)/2147483648;case"f32":case"f32-planar":return(e,r)=>e.getFloat32(r,!0)}},Gt=t=>{switch(t){case"u8":case"u8-planar":return(e,r,i)=>e.setUint8(r,$((i+1)*127.5,0,255));case"s16":case"s16-planar":return(e,r,i)=>e.setInt16(r,$(Math.round(i*32767),-32768,32767),!0);case"s32":case"s32-planar":return(e,r,i)=>e.setInt32(r,$(Math.round(i*2147483647),-2147483648,2147483647),!0);case"f32":case"f32-planar":return(e,r,i)=>e.setFloat32(r,i,!0)}},Pe=t=>typeof AudioData<"u"&&t instanceof AudioData,Vi=(t,e,r,i,s,n,o,a)=>{const c=Qt(r),l=Gt(i),d=we(r),u=we(i),f=xe(r);if(xe(i))if(f){const p=new ArrayBuffer(a*d),y=oe(p);t.copyTo(p,{planeIndex:n,frameOffset:o,frameCount:a,format:r});for(let T=0;T<a;T++){const C=T*d,A=T*u,_=c(y,C);l(e,A,_)}}else{const p=new ArrayBuffer(a*s*d),y=oe(p);t.copyTo(p,{planeIndex:0,frameOffset:o,frameCount:a,format:r});for(let T=0;T<a;T++){const C=(T*s+n)*d,A=T*u,_=c(y,C);l(e,A,_)}}else if(f){const p=a*d,y=new ArrayBuffer(p),T=oe(y);for(let C=0;C<s;C++){t.copyTo(y,{planeIndex:C,frameOffset:o,frameCount:a,format:r});for(let A=0;A<a;A++){const _=A*d,F=(A*s+C)*u,M=c(T,_);l(e,F,M)}}}else{const p=new ArrayBuffer(a*s*d),y=oe(p);t.copyTo(p,{planeIndex:0,frameOffset:o,frameCount:a,format:r});for(let T=0;T<a;T++)for(let C=0;C<s;C++){const A=T*s+C,_=A*d,F=A*u,M=c(y,_);l(e,F,M)}}};/*!
|
|
70
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
71
|
+
*
|
|
72
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
73
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
74
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
75
|
+
*/const zi=t=>{let r=(t.hasVideo?"video/":t.hasAudio?"audio/":"application/")+(t.isQuickTime?"quicktime":"mp4");if(t.codecStrings.length>0){const i=[...new Set(t.codecStrings)];r+=`; codecs="${i.join(", ")}"`}return r};/*!
|
|
76
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
77
|
+
*
|
|
78
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
79
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
80
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
81
|
+
*/const Je=8,Xt=16;/*!
|
|
82
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
83
|
+
*
|
|
84
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
85
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
86
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
87
|
+
*/class et{constructor(e){this.value=e}}class tt{constructor(e){this.value=e}}class Kt{constructor(e){this.value=e}}class de{constructor(e){this.value=e}}var h;(function(t){t[t.EBML=440786851]="EBML",t[t.EBMLVersion=17030]="EBMLVersion",t[t.EBMLReadVersion=17143]="EBMLReadVersion",t[t.EBMLMaxIDLength=17138]="EBMLMaxIDLength",t[t.EBMLMaxSizeLength=17139]="EBMLMaxSizeLength",t[t.DocType=17026]="DocType",t[t.DocTypeVersion=17031]="DocTypeVersion",t[t.DocTypeReadVersion=17029]="DocTypeReadVersion",t[t.Void=236]="Void",t[t.Segment=408125543]="Segment",t[t.SeekHead=290298740]="SeekHead",t[t.Seek=19899]="Seek",t[t.SeekID=21419]="SeekID",t[t.SeekPosition=21420]="SeekPosition",t[t.Duration=17545]="Duration",t[t.Info=357149030]="Info",t[t.TimestampScale=2807729]="TimestampScale",t[t.MuxingApp=19840]="MuxingApp",t[t.WritingApp=22337]="WritingApp",t[t.Tracks=374648427]="Tracks",t[t.TrackEntry=174]="TrackEntry",t[t.TrackNumber=215]="TrackNumber",t[t.TrackUID=29637]="TrackUID",t[t.TrackType=131]="TrackType",t[t.FlagEnabled=185]="FlagEnabled",t[t.FlagDefault=136]="FlagDefault",t[t.FlagForced=21930]="FlagForced",t[t.FlagOriginal=21934]="FlagOriginal",t[t.FlagHearingImpaired=21931]="FlagHearingImpaired",t[t.FlagVisualImpaired=21932]="FlagVisualImpaired",t[t.FlagCommentary=21935]="FlagCommentary",t[t.FlagLacing=156]="FlagLacing",t[t.Name=21358]="Name",t[t.Language=2274716]="Language",t[t.LanguageBCP47=2274717]="LanguageBCP47",t[t.CodecID=134]="CodecID",t[t.CodecPrivate=25506]="CodecPrivate",t[t.CodecDelay=22186]="CodecDelay",t[t.SeekPreRoll=22203]="SeekPreRoll",t[t.DefaultDuration=2352003]="DefaultDuration",t[t.Video=224]="Video",t[t.PixelWidth=176]="PixelWidth",t[t.PixelHeight=186]="PixelHeight",t[t.AlphaMode=21440]="AlphaMode",t[t.Audio=225]="Audio",t[t.SamplingFrequency=181]="SamplingFrequency",t[t.Channels=159]="Channels",t[t.BitDepth=25188]="BitDepth",t[t.SimpleBlock=163]="SimpleBlock",t[t.BlockGroup=160]="BlockGroup",t[t.Block=161]="Block",t[t.BlockAdditions=30113]="BlockAdditions",t[t.BlockMore=166]="BlockMore",t[t.BlockAdditional=165]="BlockAdditional",t[t.BlockAddID=238]="BlockAddID",t[t.BlockDuration=155]="BlockDuration",t[t.ReferenceBlock=251]="ReferenceBlock",t[t.Cluster=524531317]="Cluster",t[t.Timestamp=231]="Timestamp",t[t.Cues=475249515]="Cues",t[t.CuePoint=187]="CuePoint",t[t.CueTime=179]="CueTime",t[t.CueTrackPositions=183]="CueTrackPositions",t[t.CueTrack=247]="CueTrack",t[t.CueClusterPosition=241]="CueClusterPosition",t[t.Colour=21936]="Colour",t[t.MatrixCoefficients=21937]="MatrixCoefficients",t[t.TransferCharacteristics=21946]="TransferCharacteristics",t[t.Primaries=21947]="Primaries",t[t.Range=21945]="Range",t[t.Projection=30320]="Projection",t[t.ProjectionType=30321]="ProjectionType",t[t.ProjectionPoseRoll=30325]="ProjectionPoseRoll",t[t.Attachments=423732329]="Attachments",t[t.AttachedFile=24999]="AttachedFile",t[t.FileDescription=18046]="FileDescription",t[t.FileName=18030]="FileName",t[t.FileMediaType=18016]="FileMediaType",t[t.FileData=18012]="FileData",t[t.FileUID=18094]="FileUID",t[t.Chapters=272869232]="Chapters",t[t.Tags=307544935]="Tags",t[t.Tag=29555]="Tag",t[t.Targets=25536]="Targets",t[t.TargetTypeValue=26826]="TargetTypeValue",t[t.TargetType=25546]="TargetType",t[t.TagTrackUID=25541]="TagTrackUID",t[t.TagEditionUID=25545]="TagEditionUID",t[t.TagChapterUID=25540]="TagChapterUID",t[t.TagAttachmentUID=25542]="TagAttachmentUID",t[t.SimpleTag=26568]="SimpleTag",t[t.TagName=17827]="TagName",t[t.TagLanguage=17530]="TagLanguage",t[t.TagString=17543]="TagString",t[t.TagBinary=17541]="TagBinary",t[t.ContentEncodings=28032]="ContentEncodings",t[t.ContentEncoding=25152]="ContentEncoding",t[t.ContentEncodingOrder=20529]="ContentEncodingOrder",t[t.ContentEncodingScope=20530]="ContentEncodingScope",t[t.ContentCompression=20532]="ContentCompression",t[t.ContentCompAlgo=16980]="ContentCompAlgo",t[t.ContentCompSettings=16981]="ContentCompSettings",t[t.ContentEncryption=20533]="ContentEncryption"})(h||(h={})),h.EBML,h.Segment,h.SeekHead,h.Info,h.Cluster,h.Tracks,h.Cues,h.Attachments,h.Chapters,h.Tags;const Yt=t=>t<256?1:t<65536?2:t<1<<24?3:t<2**32?4:t<2**40?5:6,Zt=t=>t<1n<<8n?1:t<1n<<16n?2:t<1n<<24n?3:t<1n<<32n?4:t<1n<<40n?5:t<1n<<48n?6:t<1n<<56n?7:8,Jt=t=>t>=-64&&t<64?1:t>=-8192&&t<8192?2:t>=-1048576&&t<1<<20?3:t>=-134217728&&t<1<<27?4:t>=-17179869184&&t<2**34?5:6,Ui=t=>{if(t<127)return 1;if(t<16383)return 2;if(t<(1<<21)-1)return 3;if(t<(1<<28)-1)return 4;if(t<2**35-1)return 5;if(t<2**42-1)return 6;throw new Error("EBML varint size not supported "+t)};class Wi{constructor(e){this.writer=e,this.helper=new Uint8Array(8),this.helperView=new DataView(this.helper.buffer),this.offsets=new WeakMap,this.dataOffsets=new WeakMap}writeByte(e){this.helperView.setUint8(0,e),this.writer.write(this.helper.subarray(0,1))}writeFloat32(e){this.helperView.setFloat32(0,e,!1),this.writer.write(this.helper.subarray(0,4))}writeFloat64(e){this.helperView.setFloat64(0,e,!1),this.writer.write(this.helper)}writeUnsignedInt(e,r=Yt(e)){let i=0;switch(r){case 6:this.helperView.setUint8(i++,e/2**40|0);case 5:this.helperView.setUint8(i++,e/2**32|0);case 4:this.helperView.setUint8(i++,e>>24);case 3:this.helperView.setUint8(i++,e>>16);case 2:this.helperView.setUint8(i++,e>>8);case 1:this.helperView.setUint8(i++,e);break;default:throw new Error("Bad unsigned int size "+r)}this.writer.write(this.helper.subarray(0,i))}writeUnsignedBigInt(e,r=Zt(e)){let i=0;for(let s=r-1;s>=0;s--)this.helperView.setUint8(i++,Number(e>>BigInt(s*8)&0xffn));this.writer.write(this.helper.subarray(0,i))}writeSignedInt(e,r=Jt(e)){e<0&&(e+=2**(r*8)),this.writeUnsignedInt(e,r)}writeVarInt(e,r=Ui(e)){let i=0;switch(r){case 1:this.helperView.setUint8(i++,128|e);break;case 2:this.helperView.setUint8(i++,64|e>>8),this.helperView.setUint8(i++,e);break;case 3:this.helperView.setUint8(i++,32|e>>16),this.helperView.setUint8(i++,e>>8),this.helperView.setUint8(i++,e);break;case 4:this.helperView.setUint8(i++,16|e>>24),this.helperView.setUint8(i++,e>>16),this.helperView.setUint8(i++,e>>8),this.helperView.setUint8(i++,e);break;case 5:this.helperView.setUint8(i++,8|e/2**32&7),this.helperView.setUint8(i++,e>>24),this.helperView.setUint8(i++,e>>16),this.helperView.setUint8(i++,e>>8),this.helperView.setUint8(i++,e);break;case 6:this.helperView.setUint8(i++,4|e/2**40&3),this.helperView.setUint8(i++,e/2**32|0),this.helperView.setUint8(i++,e>>24),this.helperView.setUint8(i++,e>>16),this.helperView.setUint8(i++,e>>8),this.helperView.setUint8(i++,e);break;default:throw new Error("Bad EBML varint size "+r)}this.writer.write(this.helper.subarray(0,i))}writeAsciiString(e){this.writer.write(new Uint8Array(e.split("").map(r=>r.charCodeAt(0))))}writeEBML(e){if(e!==null)if(e instanceof Uint8Array)this.writer.write(e);else if(Array.isArray(e))for(const r of e)this.writeEBML(r);else if(this.offsets.set(e,this.writer.getPos()),this.writeUnsignedInt(e.id),Array.isArray(e.data)){const r=this.writer.getPos(),i=e.size===-1?1:e.size??4;e.size===-1?this.writeByte(255):this.writer.seek(this.writer.getPos()+i);const s=this.writer.getPos();if(this.dataOffsets.set(e,s),this.writeEBML(e.data),e.size!==-1){const n=this.writer.getPos()-s,o=this.writer.getPos();this.writer.seek(r),this.writeVarInt(n,i),this.writer.seek(o)}}else if(typeof e.data=="number"){const r=e.size??Yt(e.data);this.writeVarInt(r),this.writeUnsignedInt(e.data,r)}else if(typeof e.data=="bigint"){const r=e.size??Zt(e.data);this.writeVarInt(r),this.writeUnsignedBigInt(e.data,r)}else if(typeof e.data=="string")this.writeVarInt(e.data.length),this.writeAsciiString(e.data);else if(e.data instanceof Uint8Array)this.writeVarInt(e.data.byteLength,e.size),this.writer.write(e.data);else if(e.data instanceof et)this.writeVarInt(4),this.writeFloat32(e.data.value);else if(e.data instanceof tt)this.writeVarInt(8),this.writeFloat64(e.data.value);else if(e.data instanceof Kt){const r=e.size??Jt(e.data.value);this.writeVarInt(r),this.writeSignedInt(e.data.value,r)}else if(e.data instanceof de){const r=H.encode(e.data.value);this.writeVarInt(r.length),this.writer.write(r)}else ve(e.data)}}const Ni={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","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"};/*!
|
|
88
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
89
|
+
*
|
|
90
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
91
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
92
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
93
|
+
*/const Di=t=>{let r=(t.hasVideo?"video/":t.hasAudio?"audio/":"application/")+(t.isWebM?"webm":"x-matroska");if(t.codecStrings.length>0){const i=[...new Set(t.codecStrings.filter(Boolean))];r+=`; codecs="${i.join(", ")}"`}return r};/*!
|
|
94
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
95
|
+
*
|
|
96
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
97
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
98
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
99
|
+
*/const er=7,tr=9,ze=t=>{const e=t.filePos,r=Hi(t,9),i=new ee(r);if(i.readBits(12)!==4095||(i.skipBits(1),i.readBits(2)!==0))return null;const o=i.readBits(1),a=i.readBits(2)+1,c=i.readBits(4);if(c===15)return null;i.skipBits(1);const l=i.readBits(3);if(l===0)throw new Error("ADTS frames with channel configuration 0 are not supported.");i.skipBits(1),i.skipBits(1),i.skipBits(1),i.skipBits(1);const d=i.readBits(13);i.skipBits(11);const u=i.readBits(2)+1;if(u!==1)throw new Error("ADTS frames with more than one AAC frame are not supported.");let f=null;return o===1?t.filePos-=2:f=i.readBits(16),{objectType:a,samplingFrequencyIndex:c,channelConfiguration:l,frameLength:d,numberOfAacFrames:u,crcCheck:f,startPos:e}};/*!
|
|
100
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
101
|
+
*
|
|
102
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
103
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
104
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
105
|
+
*/class be{constructor(e,r,i,s,n){this.bytes=e,this.view=r,this.offset=i,this.start=s,this.end=n,this.bufferPos=s-i}static tempFromBytes(e){return new be(e,oe(e),0,0,e.length)}get length(){return this.end-this.start}get filePos(){return this.offset+this.bufferPos}set filePos(e){this.bufferPos=e-this.offset}get remainingLength(){return Math.max(this.end-this.filePos,0)}skip(e){this.bufferPos+=e}slice(e,r=this.end-e){if(e<this.start||e+r>this.end)throw new RangeError("Slicing outside of original slice.");return new be(this.bytes,this.view,this.offset,e,e+r)}}const Li=(t,e)=>{if(t.filePos<t.start||t.filePos+e>t.end)throw new RangeError(`Tried reading [${t.filePos}, ${t.filePos+e}), but slice is [${t.start}, ${t.end}). This is likely an internal error, please report it alongside the file that caused it.`)},Hi=(t,e)=>{Li(t,e);const r=t.bytes.subarray(t.bufferPos,t.bufferPos+e);return t.bufferPos+=e,r};/*!
|
|
106
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
107
|
+
*
|
|
108
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
109
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
110
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
111
|
+
*/class rr{constructor(e){this.mutex=new Ct,this.firstMediaStreamTimestamp=null,this.trackTimestampInfo=new WeakMap,this.output=e}onTrackClose(e){}validateAndNormalizeTimestamp(e,r,i){r+=e.source._timestampOffset;let s=this.trackTimestampInfo.get(e);if(!s){if(!i)throw new Error("First packet must be a key packet.");s={maxTimestamp:r,maxTimestampBeforeLastKeyPacket:r},this.trackTimestampInfo.set(e,s)}if(r<0)throw new Error(`Timestamps must be non-negative (got ${r}s).`);if(i&&(s.maxTimestampBeforeLastKeyPacket=s.maxTimestamp),r<s.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 ${r}s, but largest timestamp is ${s.maxTimestampBeforeLastKeyPacket}s.`);return s.maxTimestamp=Math.max(s.maxTimestamp,r),r}}/*!
|
|
112
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
113
|
+
*
|
|
114
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
115
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
116
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
117
|
+
*/const Ue=/<(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})>/g,$i=/(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})/,ji=t=>{const e=$i.exec(t);if(!e)throw new Error("Expected match.");return 3600*1e3*Number(e[1]||"0")+60*1e3*Number(e[2])+1e3*Number(e[3])+Number(e[4])},ir=t=>{const e=Math.floor(t/36e5),r=Math.floor(t%(3600*1e3)/(60*1e3)),i=Math.floor(t%(60*1e3)/1e3),s=t%1e3;return e.toString().padStart(2,"0")+":"+r.toString().padStart(2,"0")+":"+i.toString().padStart(2,"0")+"."+s.toString().padStart(3,"0")};/*!
|
|
118
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
119
|
+
*
|
|
120
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
121
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
122
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
123
|
+
*/class sr{constructor(e){this.writer=e,this.helper=new Uint8Array(8),this.helperView=new DataView(this.helper.buffer),this.offsets=new WeakMap}writeU32(e){this.helperView.setUint32(0,e,!1),this.writer.write(this.helper.subarray(0,4))}writeU64(e){this.helperView.setUint32(0,Math.floor(e/2**32),!1),this.helperView.setUint32(4,e,!1),this.writer.write(this.helper.subarray(0,8))}writeAscii(e){for(let r=0;r<e.length;r++)this.helperView.setUint8(r%8,e.charCodeAt(r)),r%8===7&&this.writer.write(this.helper);e.length%8!==0&&this.writer.write(this.helper.subarray(0,e.length%8))}writeBox(e){if(this.offsets.set(e,this.writer.getPos()),e.contents&&!e.children)this.writeBoxHeader(e,e.size??e.contents.byteLength+8),this.writer.write(e.contents);else{const r=this.writer.getPos();if(this.writeBoxHeader(e,0),e.contents&&this.writer.write(e.contents),e.children)for(const n of e.children)n&&this.writeBox(n);const i=this.writer.getPos(),s=e.size??i-r;this.writer.seek(r),this.writeBoxHeader(e,s),this.writer.seek(i)}}writeBoxHeader(e,r){this.writeU32(e.largeSize?1:r),this.writeAscii(e.type),e.largeSize&&this.writeU64(r)}measureBoxHeader(e){return 8+(e.largeSize?8:0)}patchBox(e){const r=this.offsets.get(e);m(r!==void 0);const i=this.writer.getPos();this.writer.seek(r),this.writeBox(e),this.writer.seek(i)}measureBox(e){if(e.contents&&!e.children)return this.measureBoxHeader(e)+e.contents.byteLength;{let r=this.measureBoxHeader(e);if(e.contents&&(r+=e.contents.byteLength),e.children)for(const i of e.children)i&&(r+=this.measureBox(i));return r}}}const k=new Uint8Array(8),Y=new DataView(k.buffer),R=t=>[(t%256+256)%256],v=t=>(Y.setUint16(0,t,!1),[k[0],k[1]]),nr=t=>(Y.setInt16(0,t,!1),[k[0],k[1]]),or=t=>(Y.setUint32(0,t,!1),[k[1],k[2],k[3]]),w=t=>(Y.setUint32(0,t,!1),[k[0],k[1],k[2],k[3]]),ue=t=>(Y.setInt32(0,t,!1),[k[0],k[1],k[2],k[3]]),ye=t=>(Y.setUint32(0,Math.floor(t/2**32),!1),Y.setUint32(4,t,!1),[k[0],k[1],k[2],k[3],k[4],k[5],k[6],k[7]]),ar=t=>(Y.setInt16(0,2**8*t,!1),[k[0],k[1]]),se=t=>(Y.setInt32(0,2**16*t,!1),[k[0],k[1],k[2],k[3]]),rt=t=>(Y.setInt32(0,2**30*t,!1),[k[0],k[1],k[2],k[3]]),it=(t,e)=>{const r=[];let i=t;do{let s=i&127;i>>=7,r.length>0&&(s|=128),r.push(s)}while(i>0||e);return r.reverse()},D=(t,e=!1)=>{const r=Array(t.length).fill(null).map((i,s)=>t.charCodeAt(s));return e&&r.push(0),r},st=t=>{let e=null;for(const r of t)(!e||r.timestamp>e.timestamp)&&(e=r);return e},cr=t=>{const e=t*(Math.PI/180),r=Math.round(Math.cos(e)),i=Math.round(Math.sin(e));return[r,i,0,-i,r,0,0,0,1]},lr=cr(0),dr=t=>[se(t[0]),se(t[1]),rt(t[2]),se(t[3]),se(t[4]),rt(t[5]),se(t[6]),se(t[7]),rt(t[8])],S=(t,e,r)=>({type:t,contents:e&&new Uint8Array(e.flat(10)),children:r}),E=(t,e,r,i,s)=>S(t,[R(e),or(r),i??[]],s),qi=t=>t.isQuickTime?S("ftyp",[D("qt "),w(512),D("qt ")]):t.fragmented?S("ftyp",[D("iso5"),w(512),D("iso5"),D("iso6"),D("mp41")]):S("ftyp",[D("isom"),w(512),D("isom"),t.holdsAvc?D("avc1"):[],D("mp41")]),We=t=>({type:"mdat",largeSize:t}),Qi=t=>({type:"free",size:t}),Fe=t=>S("moov",void 0,[Gi(t.creationTime,t.trackDatas),...t.trackDatas.map(e=>Xi(e,t.creationTime)),t.isFragmented?As(t.trackDatas):null,Ns(t)]),Gi=(t,e)=>{const r=z(Math.max(0,...e.filter(o=>o.samples.length>0).map(o=>{const a=st(o.samples);return a.timestamp+a.duration})),lt),i=Math.max(0,...e.map(o=>o.track.id))+1,s=!Ce(t)||!Ce(r),n=s?ye:w;return E("mvhd",+s,0,[n(t),n(t),w(lt),n(r),se(1),ar(1),Array(10).fill(0),dr(lr),Array(24).fill(0),w(i)])},Xi=(t,e)=>{const r=Ys(t);return S("trak",void 0,[Ki(t,e),Yi(t,e),r.name!==void 0?S("udta",void 0,[S("name",[...H.encode(r.name)])]):null])},Ki=(t,e)=>{const r=st(t.samples),i=z(r?r.timestamp+r.duration:0,lt),s=!Ce(e)||!Ce(i),n=s?ye:w;let o;if(t.type==="video"){const c=t.track.metadata.rotation;o=cr(c??0)}else o=lr;let a=2;return t.track.metadata.disposition?.default!==!1&&(a|=1),E("tkhd",+s,a,[n(e),n(e),w(t.track.id),w(0),n(i),Array(8).fill(0),v(0),v(t.track.id),ar(t.type==="audio"?1:0),v(0),dr(o),se(t.type==="video"?t.info.width:0),se(t.type==="video"?t.info.height:0)])},Yi=(t,e)=>S("mdia",void 0,[Zi(t,e),nt(!0,Ji[t.type],es[t.type]),ts(t)]),Zi=(t,e)=>{const r=st(t.samples),i=z(r?r.timestamp+r.duration:0,t.timescale),s=!Ce(e)||!Ce(i),n=s?ye:w;return E("mdhd",+s,0,[n(e),n(e),w(t.timescale),n(i),v(wr(t.track.metadata.languageCode??St)),v(0)])},Ji={video:"vide",audio:"soun",subtitle:"text"},es={video:"MediabunnyVideoHandler",audio:"MediabunnySoundHandler",subtitle:"MediabunnyTextHandler"},nt=(t,e,r,i="\0\0\0\0")=>E("hdlr",0,0,[t?D("mhlr"):w(0),D(e),D(i),w(0),w(0),D(r,!0)]),ts=t=>S("minf",void 0,[rs[t.type](),is(),os(t)]),rs={video:()=>E("vmhd",0,1,[v(0),v(0),v(0),v(0)]),audio:()=>E("smhd",0,0,[v(0),v(0)]),subtitle:()=>E("nmhd",0,0)},is=()=>S("dinf",void 0,[ss()]),ss=()=>E("dref",0,0,[w(1)],[ns()]),ns=()=>E("url ",0,1),os=t=>{const e=t.compositionTimeOffsetTable.length>1||t.compositionTimeOffsetTable.some(r=>r.sampleCompositionTimeOffset!==0);return S("stbl",void 0,[as(t),Ts(t),e?ks(t):null,e?Es(t):null,vs(t),Ss(t),xs(t),Cs(t)])},as=t=>{let e;if(t.type==="video")e=cs($s(t.track.source._codec,t.info.decoderConfig.codec),t);else if(t.type==="audio"){const r=gr(t.track.source._codec,t.muxer.isQuickTime);m(r),e=fs(r,t)}else t.type==="subtitle"&&(e=bs(Qs[t.track.source._codec],t));return m(e),E("stsd",0,0,[w(1)],[e])},cs=(t,e)=>S(t,[Array(6).fill(0),v(1),v(0),v(0),Array(12).fill(0),v(e.info.width),v(e.info.height),w(4718592),w(4718592),w(0),v(1),Array(32).fill(0),v(24),nr(65535)],[js[e.track.source._codec](e),Tt(e.info.decoderConfig.colorSpace)?ls(e):null]),ls=t=>S("colr",[D("nclx"),v(Be[t.info.decoderConfig.colorSpace.primaries]),v(Re[t.info.decoderConfig.colorSpace.transfer]),v(Ie[t.info.decoderConfig.colorSpace.matrix]),R((t.info.decoderConfig.colorSpace.fullRange?1:0)<<7)]),ds=t=>t.info.decoderConfig&&S("avcC",[...q(t.info.decoderConfig.description)]),us=t=>t.info.decoderConfig&&S("hvcC",[...q(t.info.decoderConfig.description)]),ur=t=>{if(!t.info.decoderConfig)return null;const e=t.info.decoderConfig,r=e.codec.split("."),i=Number(r[1]),s=Number(r[2]),n=Number(r[3]),o=r[4]?Number(r[4]):1,a=r[8]?Number(r[8]):Number(e.colorSpace?.fullRange??0),c=(n<<4)+(o<<1)+a,l=r[5]?Number(r[5]):e.colorSpace?.primaries?Be[e.colorSpace.primaries]:2,d=r[6]?Number(r[6]):e.colorSpace?.transfer?Re[e.colorSpace.transfer]:2,u=r[7]?Number(r[7]):e.colorSpace?.matrix?Ie[e.colorSpace.matrix]:2;return E("vpcC",1,0,[R(i),R(s),R(c),R(l),R(d),R(u),v(0)])},hs=t=>S("av1C",Bt(t.info.decoderConfig.codec)),fs=(t,e)=>{let r=0,i,s=16;if(Q.includes(e.track.source._codec)){const n=e.track.source._codec,{sampleSize:o}=pe(n);s=8*o,s>16&&(r=1)}return r===0?i=[Array(6).fill(0),v(1),v(r),v(0),w(0),v(e.info.numberOfChannels),v(s),v(0),v(0),v(e.info.sampleRate<2**16?e.info.sampleRate:0),v(0)]:i=[Array(6).fill(0),v(1),v(r),v(0),w(0),v(e.info.numberOfChannels),v(Math.min(s,16)),v(0),v(0),v(e.info.sampleRate<2**16?e.info.sampleRate:0),v(0),w(1),w(s/8),w(e.info.numberOfChannels*s/8),w(2)],S(t,i,[qs(e.track.source._codec,e.muxer.isQuickTime)?.(e)??null])},ot=t=>{let e;switch(t.track.source._codec){case"aac":e=64;break;case"mp3":e=107;break;case"vorbis":e=221;break;default:throw new Error(`Unhandled audio codec: ${t.track.source._codec}`)}let r=[...R(e),...R(21),...or(0),...w(0),...w(0)];if(t.info.decoderConfig.description){const i=q(t.info.decoderConfig.description);r=[...r,...R(5),...it(i.byteLength),...i]}return r=[...v(1),...R(0),...R(4),...it(r.length),...r,...R(6),...R(1),...R(2)],r=[...R(3),...it(r.length),...r],E("esds",0,0,r)},he=t=>S("wave",void 0,[ms(t),ps(t),S("\0\0\0\0")]),ms=t=>S("frma",[D(gr(t.track.source._codec,t.muxer.isQuickTime))]),ps=t=>{const{littleEndian:e}=pe(t.track.source._codec);return S("enda",[v(+e)])},gs=t=>{let e=t.info.numberOfChannels,r=3840,i=t.info.sampleRate,s=0,n=0,o=new Uint8Array(0);const a=t.info.decoderConfig?.description;if(a){m(a.byteLength>=18);const c=q(a),l=Nt(c);e=l.outputChannelCount,r=l.preSkip,i=l.inputSampleRate,s=l.outputGain,n=l.channelMappingFamily,l.channelMappingTable&&(o=l.channelMappingTable)}return S("dOps",[R(0),R(e),v(r),w(i),nr(s),R(n),...o])},ws=t=>{const e=t.info.decoderConfig?.description;m(e);const r=q(e);return E("dfLa",0,0,[...r.subarray(4)])},Z=t=>{const{littleEndian:e,sampleSize:r}=pe(t.track.source._codec),i=+e;return E("pcmC",0,0,[R(i),R(8*r)])},bs=(t,e)=>S(t,[Array(6).fill(0),v(1)],[Gs[e.track.source._codec](e)]),ys=t=>S("vttC",[...H.encode(t.info.config.description)]),Ts=t=>E("stts",0,0,[w(t.timeToSampleTable.length),t.timeToSampleTable.map(e=>[w(e.sampleCount),w(e.sampleDelta)])]),Cs=t=>{if(t.samples.every(r=>r.type==="key"))return null;const e=[...t.samples.entries()].filter(([,r])=>r.type==="key");return E("stss",0,0,[w(e.length),e.map(([r])=>w(r+1))])},vs=t=>E("stsc",0,0,[w(t.compactlyCodedChunkTable.length),t.compactlyCodedChunkTable.map(e=>[w(e.firstChunk),w(e.samplesPerChunk),w(1)])]),Ss=t=>{if(t.type==="audio"&&t.info.requiresPcmTransformation){const{sampleSize:e}=pe(t.track.source._codec);return E("stsz",0,0,[w(e*t.info.numberOfChannels),w(t.samples.reduce((r,i)=>r+z(i.duration,t.timescale),0))])}return E("stsz",0,0,[w(0),w(t.samples.length),t.samples.map(e=>w(e.size))])},xs=t=>t.finalizedChunks.length>0&&j(t.finalizedChunks).offset>=2**32?E("co64",0,0,[w(t.finalizedChunks.length),t.finalizedChunks.map(e=>ye(e.offset))]):E("stco",0,0,[w(t.finalizedChunks.length),t.finalizedChunks.map(e=>w(e.offset))]),ks=t=>E("ctts",1,0,[w(t.compositionTimeOffsetTable.length),t.compositionTimeOffsetTable.map(e=>[w(e.sampleCount),ue(e.sampleCompositionTimeOffset)])]),Es=t=>{let e=1/0,r=-1/0,i=1/0,s=-1/0;m(t.compositionTimeOffsetTable.length>0),m(t.samples.length>0);for(let o=0;o<t.compositionTimeOffsetTable.length;o++){const a=t.compositionTimeOffsetTable[o];e=Math.min(e,a.sampleCompositionTimeOffset),r=Math.max(r,a.sampleCompositionTimeOffset)}for(let o=0;o<t.samples.length;o++){const a=t.samples[o];i=Math.min(i,z(a.timestamp,t.timescale)),s=Math.max(s,z(a.timestamp+a.duration,t.timescale))}const n=Math.max(-e,0);return s>=2**31?null:E("cslg",0,0,[ue(n),ue(e),ue(r),ue(i),ue(s)])},As=t=>S("mvex",void 0,t.map(_s)),_s=t=>E("trex",0,0,[w(t.track.id),w(1),w(0),w(0),w(0)]),hr=(t,e)=>S("moof",void 0,[Ps(t),...e.map(Fs)]),Ps=t=>E("mfhd",0,0,[w(t)]),fr=t=>{let e=0,r=0;const i=0,s=0,n=t.type==="delta";return r|=+n,n?e|=1:e|=2,e<<24|r<<16|i<<8|s},Fs=t=>S("traf",void 0,[Bs(t),Rs(t),Is(t)]),Bs=t=>{m(t.currentChunk);let e=0;e|=8,e|=16,e|=32,e|=131072;const r=t.currentChunk.samples[1]??t.currentChunk.samples[0],i={duration:r.timescaleUnitsToNextSample,size:r.size,flags:fr(r)};return E("tfhd",0,e,[w(t.track.id),w(i.duration),w(i.size),w(i.flags)])},Rs=t=>(m(t.currentChunk),E("tfdt",1,0,[ye(z(t.currentChunk.startTimestamp,t.timescale))])),Is=t=>{m(t.currentChunk);const e=t.currentChunk.samples.map(y=>y.timescaleUnitsToNextSample),r=t.currentChunk.samples.map(y=>y.size),i=t.currentChunk.samples.map(fr),s=t.currentChunk.samples.map(y=>z(y.timestamp-y.decodeTimestamp,t.timescale)),n=new Set(e),o=new Set(r),a=new Set(i),c=new Set(s),l=a.size===2&&i[0]!==i[1],d=n.size>1,u=o.size>1,f=!l&&a.size>1,g=c.size>1||[...c].some(y=>y!==0);let p=0;return p|=1,p|=4*+l,p|=256*+d,p|=512*+u,p|=1024*+f,p|=2048*+g,E("trun",1,p,[w(t.currentChunk.samples.length),w(t.currentChunk.offset-t.currentChunk.moofOffset||0),l?w(i[0]):[],t.currentChunk.samples.map((y,T)=>[d?w(e[T]):[],u?w(r[T]):[],f?w(i[T]):[],g?ue(s[T]):[]])])},Os=t=>S("mfra",void 0,[...t.map(Ms),Vs()]),Ms=(t,e)=>E("tfra",1,0,[w(t.track.id),w(63),w(t.finalizedChunks.length),t.finalizedChunks.map(i=>[ye(z(i.samples[0].timestamp,t.timescale)),ye(i.moofOffset),w(e+1),w(1),w(1)])]),Vs=()=>E("mfro",0,0,[w(0)]),zs=()=>S("vtte"),Us=(t,e,r,i,s)=>S("vttc",void 0,[s!==null?S("vsid",[ue(s)]):null,r!==null?S("iden",[...H.encode(r)]):null,e!==null?S("ctim",[...H.encode(ir(e))]):null,i!==null?S("sttg",[...H.encode(i)]):null,S("payl",[...H.encode(t)])]),Ws=t=>S("vtta",[...H.encode(t)]),Ns=t=>{const e=[],r=t.format._options.metadataFormat??"auto",i=t.output._metadataTags;if(r==="mdir"||r==="auto"&&!t.isQuickTime){const s=Ls(i);s&&e.push(s)}else if(r==="mdta"){const s=Hs(i);s&&e.push(s)}else(r==="udta"||r==="auto"&&t.isQuickTime)&&Ds(e,t.output._metadataTags);return e.length===0?null:S("udta",void 0,e)},Ds=(t,e)=>{for(const{key:r,value:i}of qe(e))switch(r){case"title":t.push(J("©nam",i));break;case"description":t.push(J("©des",i));break;case"artist":t.push(J("©ART",i));break;case"album":t.push(J("©alb",i));break;case"albumArtist":t.push(J("albr",i));break;case"genre":t.push(J("©gen",i));break;case"date":t.push(J("©day",i.toISOString().slice(0,10)));break;case"comment":t.push(J("©cmt",i));break;case"lyrics":t.push(J("©lyr",i));break;case"raw":break;case"discNumber":case"discsTotal":case"trackNumber":case"tracksTotal":case"images":break;default:ve(r)}if(e.raw)for(const r in e.raw){const i=e.raw[r];i==null||r.length!==4||t.some(s=>s.type===r)||(typeof i=="string"?t.push(J(r,i)):i instanceof Uint8Array&&t.push(S(r,Array.from(i))))}},J=(t,e)=>{const r=H.encode(e);return S(t,[v(r.length),v(wr("und")),Array.from(r)])},mr={"image/jpeg":13,"image/png":14,"image/bmp":27},pr=(t,e)=>{const r=[];for(const{key:i,value:s}of qe(t))switch(i){case"title":r.push({key:e?"title":"©nam",value:X(s)});break;case"description":r.push({key:e?"description":"©des",value:X(s)});break;case"artist":r.push({key:e?"artist":"©ART",value:X(s)});break;case"album":r.push({key:e?"album":"©alb",value:X(s)});break;case"albumArtist":r.push({key:e?"album_artist":"aART",value:X(s)});break;case"comment":r.push({key:e?"comment":"©cmt",value:X(s)});break;case"genre":r.push({key:e?"genre":"©gen",value:X(s)});break;case"lyrics":r.push({key:e?"lyrics":"©lyr",value:X(s)});break;case"date":r.push({key:e?"date":"©day",value:X(s.toISOString().slice(0,10))});break;case"images":for(const n of s)n.kind==="coverFront"&&r.push({key:"covr",value:S("data",[w(mr[n.mimeType]??0),w(0),Array.from(n.data)])});break;case"trackNumber":if(e){const n=t.tracksTotal!==void 0?`${s}/${t.tracksTotal}`:s.toString();r.push({key:"track",value:X(n)})}else r.push({key:"trkn",value:S("data",[w(0),w(0),v(0),v(s),v(t.tracksTotal??0),v(0)])});break;case"discNumber":e||r.push({key:"disc",value:S("data",[w(0),w(0),v(0),v(s),v(t.discsTotal??0),v(0)])});break;case"tracksTotal":case"discsTotal":break;case"raw":break;default:ve(i)}if(t.raw)for(const i in t.raw){const s=t.raw[i];s==null||!e&&i.length!==4||r.some(n=>n.key===i)||(typeof s=="string"?r.push({key:i,value:X(s)}):s instanceof Uint8Array?r.push({key:i,value:S("data",[w(0),w(0),Array.from(s)])}):s instanceof Et&&r.push({key:i,value:S("data",[w(mr[s.mimeType]??0),w(0),Array.from(s.data)])}))}return r},Ls=t=>{const e=pr(t,!1);return e.length===0?null:E("meta",0,0,void 0,[nt(!1,"mdir","","appl"),S("ilst",void 0,e.map(r=>S(r.key,void 0,[r.value])))])},Hs=t=>{const e=pr(t,!0);return e.length===0?null:S("meta",void 0,[nt(!1,"mdta",""),E("keys",0,0,[w(e.length)],e.map(r=>S("mdta",[...H.encode(r.key)]))),S("ilst",void 0,e.map((r,i)=>{const s=String.fromCharCode(...w(i+1));return S(s,void 0,[r.value])}))])},X=t=>S("data",[w(1),w(0),...H.encode(t)]),$s=(t,e)=>{switch(t){case"avc":return e.startsWith("avc3")?"avc3":"avc1";case"hevc":return"hvc1";case"vp8":return"vp08";case"vp9":return"vp09";case"av1":return"av01"}},js={avc:ds,hevc:us,vp8:ur,vp9:ur,av1:hs},gr=(t,e)=>{switch(t){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"}if(e)switch(t){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(t){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"}},qs=(t,e)=>{switch(t){case"aac":return ot;case"mp3":return ot;case"opus":return gs;case"vorbis":return ot;case"flac":return ws}if(e)switch(t){case"pcm-s24":return he;case"pcm-s24be":return he;case"pcm-s32":return he;case"pcm-s32be":return he;case"pcm-f32":return he;case"pcm-f32be":return he;case"pcm-f64":return he;case"pcm-f64be":return he}else switch(t){case"pcm-s16":return Z;case"pcm-s16be":return Z;case"pcm-s24":return Z;case"pcm-s24be":return Z;case"pcm-s32":return Z;case"pcm-s32be":return Z;case"pcm-f32":return Z;case"pcm-f32be":return Z;case"pcm-f64":return Z;case"pcm-f64be":return Z}return null},Qs={webvtt:"wvtt"},Gs={webvtt:ys},wr=t=>{m(t.length===3);let e=0;for(let r=0;r<3;r++)e<<=5,e+=t.charCodeAt(r)-96;return e};/*!
|
|
124
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
125
|
+
*
|
|
126
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
127
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
128
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
129
|
+
*/class Xs{constructor(){this.ensureMonotonicity=!1,this.trackedWrites=null,this.trackedStart=-1,this.trackedEnd=-1}start(){}maybeTrackWrites(e){if(!this.trackedWrites)return;let r=this.getPos();if(r<this.trackedStart){if(r+e.byteLength<=this.trackedStart)return;e=e.subarray(this.trackedStart-r),r=0}const i=r+e.byteLength-this.trackedStart;let s=this.trackedWrites.byteLength;for(;s<i;)s*=2;if(s!==this.trackedWrites.byteLength){const n=new Uint8Array(s);n.set(this.trackedWrites,0),this.trackedWrites=n}this.trackedWrites.set(e,r-this.trackedStart),this.trackedEnd=Math.max(this.trackedEnd,r+e.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.");const r={data:this.trackedWrites.subarray(0,this.trackedEnd-this.trackedStart),start:this.trackedStart,end:this.trackedEnd};return this.trackedWrites=null,r}}const at=2**16,ct=2**32;class br extends Xs{constructor(e){if(super(),this.pos=0,this.maxPos=0,this.target=e,this.supportsResize="resize"in new ArrayBuffer(0),this.supportsResize)try{this.buffer=new ArrayBuffer(at,{maxByteLength:ct})}catch{this.buffer=new ArrayBuffer(at),this.supportsResize=!1}else this.buffer=new ArrayBuffer(at);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>ct)throw new Error(`ArrayBuffer exceeded maximum size of ${ct} bytes. Please consider using another target.`);if(this.supportsResize)this.buffer.resize(r);else{const i=new ArrayBuffer(r),s=new Uint8Array(i);s.set(this.bytes,0),this.buffer=i,this.bytes=s}}}write(e){this.maybeTrackWrites(e),this.ensureSize(this.pos+e.byteLength),this.bytes.set(e,this.pos),this.target.onwrite?.(this.pos,this.pos+e.byteLength),this.pos+=e.byteLength,this.maxPos=Math.max(this.maxPos,this.pos)}seek(e){this.pos=e}getPos(){return this.pos}async flush(){}async finalize(){this.ensureSize(this.pos),this.target.buffer=this.buffer.slice(0,Math.max(this.maxPos,this.pos))}async close(){}getSlice(e,r){return this.bytes.slice(e,r)}}/*!
|
|
130
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
131
|
+
*
|
|
132
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
133
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
134
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
135
|
+
*/class yr{constructor(){this._output=null,this.onwrite=null}}class Tr extends yr{constructor(){super(...arguments),this.buffer=null}_createWriter(){return new br(this)}}/*!
|
|
136
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
137
|
+
*
|
|
138
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
139
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
140
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
141
|
+
*/const lt=1e3,Ks=2082844800,Ys=t=>{const e={},r=t.track;return r.metadata.name!==void 0&&(e.name=r.metadata.name),e},z=(t,e,r=!0)=>{const i=t*e;return r?Math.round(i):i};class Zs extends rr{constructor(e,r){super(e),this.auxTarget=new Tr,this.auxWriter=this.auxTarget._createWriter(),this.auxBoxWriter=new sr(this.auxWriter),this.mdat=null,this.ftypSize=null,this.trackDatas=[],this.allTracksKnown=vt(),this.creationTime=Math.floor(Date.now()/1e3)+Ks,this.finalizedChunks=[],this.nextFragmentNumber=1,this.maxWrittenTimestamp=-1/0,this.format=r,this.writer=e._writer,this.boxWriter=new sr(this.writer),this.isQuickTime=r instanceof Er;const i=this.writer instanceof br?"in-memory":!1;this.fastStart=r._options.fastStart??i,this.isFragmented=this.fastStart==="fragmented",(this.fastStart==="in-memory"||this.isFragmented)&&(this.writer.ensureMonotonicity=!0),this.minimumFragmentDuration=r._options.minimumFragmentDuration??1}async start(){const e=await this.mutex.acquire(),r=this.output._tracks.some(i=>i.type==="video"&&i.source._codec==="avc");if(this.format._options.onFtyp&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(qi({isQuickTime:this.isQuickTime,holdsAvc:r,fragmented:this.isFragmented})),this.format._options.onFtyp){const{data:i,start:s}=this.writer.stopTrackingWrites();this.format._options.onFtyp(i,s)}if(this.ftypSize=this.writer.getPos(),this.fastStart!=="in-memory")if(this.fastStart==="reserve"){for(const 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||(this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat=We(!0),this.boxWriter.writeBox(this.mdat));await this.writer.flush(),e()}allTracksAreKnown(){for(const 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;const e=this.trackDatas.map(r=>r.type==="video"||r.type==="audio"?r.info.decoderConfig.codec:{webvtt:"wvtt"}[r.track.source._codec]);return zi({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){const s=this.trackDatas.find(l=>l.track===e);if(s)return s;Ot(i),m(i),m(i.decoderConfig);const n={...i.decoderConfig};m(n.codedWidth!==void 0),m(n.codedHeight!==void 0);let o=!1;if(e.source._codec==="avc"&&!n.description){const l=pi(r.data);if(!l)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.");n.description=gi(l),o=!0}else if(e.source._codec==="hevc"&&!n.description){const l=yi(r.data);if(!l)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.");n.description=Ei(l),o=!0}const a=Qr(1/(e.metadata.frameRate??57600),1e6).denominator,c={muxer:this,track:e,type:"video",info:{width:n.codedWidth,height:n.codedHeight,decoderConfig:n,requiresAnnexBTransformation:o},timescale:a,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[]};return this.trackDatas.push(c),this.trackDatas.sort((l,d)=>l.track.id-d.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),c}getAudioTrackData(e,r,i){const s=this.trackDatas.find(c=>c.track===e);if(s)return s;Mt(i),m(i),m(i.decoderConfig);const n={...i.decoderConfig};let o=!1;if(e.source._codec==="aac"&&!n.description){const c=ze(be.tempFromBytes(r.data));if(!c)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.");const l=Ee[c.samplingFrequencyIndex],d=Ve[c.channelConfiguration];if(l===void 0||d===void 0)throw new Error("Invalid ADTS frame header.");n.description=Ge({objectType:c.objectType,sampleRate:l,numberOfChannels:d}),o=!0}const a={muxer:this,track:e,type:"audio",info:{numberOfChannels:i.decoderConfig.numberOfChannels,sampleRate:i.decoderConfig.sampleRate,decoderConfig:n,requiresPcmTransformation:!this.isFragmented&&Q.includes(e.source._codec),requiresAdtsStripping:o},timescale:i.decoderConfig.sampleRate,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[]};return this.trackDatas.push(a),this.trackDatas.sort((c,l)=>c.track.id-l.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),a}getSubtitleTrackData(e,r){const i=this.trackDatas.find(n=>n.track===e);if(i)return i;Vt(r),m(r),m(r.config);const s={muxer:this,track:e,type:"subtitle",info:{config:r.config},timescale:1e3,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[],lastCueEndTimestamp:0,cueQueue:[],nextSourceId:0,cueToSourceId:new WeakMap};return this.trackDatas.push(s),this.trackDatas.sort((n,o)=>n.track.id-o.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),s}async addEncodedVideoPacket(e,r,i){const s=await this.mutex.acquire();try{const n=this.getVideoTrackData(e,r,i);let o=r.data;if(n.info.requiresAnnexBTransformation){const l=[...Xe(o)].map(d=>o.subarray(d.offset,d.offset+d.length));if(l.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=mi(l,4)}const a=this.validateAndNormalizeTimestamp(n.track,r.timestamp,r.type==="key"),c=this.createSampleForTrack(n,o,a,r.duration,r.type);await this.registerSample(n,c)}finally{s()}}async addEncodedAudioPacket(e,r,i){const s=await this.mutex.acquire();try{const n=this.getAudioTrackData(e,r,i);let o=r.data;if(n.info.requiresAdtsStripping){const l=ze(be.tempFromBytes(o));if(!l)throw new Error("Expected ADTS frame, didn't get one.");const d=l.crcCheck===null?er:tr;o=o.subarray(d)}const a=this.validateAndNormalizeTimestamp(n.track,r.timestamp,r.type==="key"),c=this.createSampleForTrack(n,o,a,r.duration,r.type);n.info.requiresPcmTransformation&&await this.maybePadWithSilence(n,a),await this.registerSample(n,c)}finally{s()}}async maybePadWithSilence(e,r){const i=j(e.samples),s=i?i.timestamp+i.duration:0,n=r-s,o=z(n,e.timescale);if(o>0){const{sampleSize:a,silentValue:c}=pe(e.info.decoderConfig.codec),l=o*e.info.numberOfChannels,d=new Uint8Array(a*l).fill(c),u=this.createSampleForTrack(e,new Uint8Array(d.buffer),s,n,"key");await this.registerSample(e,u)}}async addSubtitleCue(e,r,i){const s=await this.mutex.acquire();try{const n=this.getSubtitleTrackData(e,i);this.validateAndNormalizeTimestamp(n.track,r.timestamp,!0),e.source._codec==="webvtt"&&(n.cueQueue.push(r),await this.processWebVTTCues(n,r.timestamp))}finally{s()}}async processWebVTTCues(e,r){for(;e.cueQueue.length>0;){const i=new Set([]);for(const l of e.cueQueue)m(l.timestamp<=r),m(e.lastCueEndTimestamp<=l.timestamp+l.duration),i.add(Math.max(l.timestamp,e.lastCueEndTimestamp)),i.add(l.timestamp+l.duration);const s=[...i].sort((l,d)=>l-d),n=s[0],o=s[1]??n;if(r<o)break;if(e.lastCueEndTimestamp<n){this.auxWriter.seek(0);const l=zs();this.auxBoxWriter.writeBox(l);const d=this.auxWriter.getSlice(0,this.auxWriter.getPos()),u=this.createSampleForTrack(e,d,e.lastCueEndTimestamp,n-e.lastCueEndTimestamp,"key");await this.registerSample(e,u),e.lastCueEndTimestamp=n}this.auxWriter.seek(0);for(let l=0;l<e.cueQueue.length;l++){const d=e.cueQueue[l];if(d.timestamp>=o)break;Ue.lastIndex=0;const u=Ue.test(d.text),f=d.timestamp+d.duration;let g=e.cueToSourceId.get(d);if(g===void 0&&o<f&&(g=e.nextSourceId++,e.cueToSourceId.set(d,g)),d.notes){const y=Ws(d.notes);this.auxBoxWriter.writeBox(y)}const p=Us(d.text,u?n:null,d.identifier??null,d.settings??null,g??null);this.auxBoxWriter.writeBox(p),f===o&&e.cueQueue.splice(l--,1)}const a=this.auxWriter.getSlice(0,this.auxWriter.getPos()),c=this.createSampleForTrack(e,a,n,o-n,"key");await this.registerSample(e,c),e.lastCueEndTimestamp=o}}createSampleForTrack(e,r,i,s,n){return{timestamp:i,decodeTimestamp:i,duration:s,data:r,size:r.byteLength,type:n,timescaleUnitsToNextSample:z(s,e.timescale)}}processTimestamps(e,r){if(e.timestampProcessingQueue.length===0)return;if(e.type==="audio"&&e.info.requiresPcmTransformation){let s=0;for(let n=0;n<e.timestampProcessingQueue.length;n++){const o=e.timestampProcessingQueue[n],a=z(o.duration,e.timescale);s+=a}if(e.timeToSampleTable.length===0)e.timeToSampleTable.push({sampleCount:s,sampleDelta:1});else{const n=j(e.timeToSampleTable);n.sampleCount+=s}e.timestampProcessingQueue.length=0;return}const i=e.timestampProcessingQueue.map(s=>s.timestamp).sort((s,n)=>s-n);for(let s=0;s<e.timestampProcessingQueue.length;s++){const n=e.timestampProcessingQueue[s];n.decodeTimestamp=i[s],!this.isFragmented&&e.lastTimescaleUnits===null&&(n.decodeTimestamp=0);const o=z(n.timestamp-n.decodeTimestamp,e.timescale),a=z(n.duration,e.timescale);if(e.lastTimescaleUnits!==null){m(e.lastSample);const c=z(n.decodeTimestamp,e.timescale,!1),l=Math.round(c-e.lastTimescaleUnits);if(m(l>=0),e.lastTimescaleUnits+=l,e.lastSample.timescaleUnitsToNextSample=l,!this.isFragmented){let d=j(e.timeToSampleTable);if(m(d),d.sampleCount===1){d.sampleDelta=l;const f=e.timeToSampleTable[e.timeToSampleTable.length-2];f&&f.sampleDelta===l&&(f.sampleCount++,e.timeToSampleTable.pop(),d=f)}else d.sampleDelta!==l&&(d.sampleCount--,e.timeToSampleTable.push(d={sampleCount:1,sampleDelta:l}));d.sampleDelta===a?d.sampleCount++:e.timeToSampleTable.push({sampleCount:1,sampleDelta:a});const u=j(e.compositionTimeOffsetTable);m(u),u.sampleCompositionTimeOffset===o?u.sampleCount++:e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:o})}}else e.lastTimescaleUnits=z(n.decodeTimestamp,e.timescale,!1),this.isFragmented||(e.timeToSampleTable.push({sampleCount:1,sampleDelta:a}),e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:o}));e.lastSample=n}if(e.timestampProcessingQueue.length=0,m(e.lastSample),m(e.lastTimescaleUnits!==null),r!==void 0&&e.lastSample.timescaleUnitsToNextSample===0){m(r.type==="key");const s=z(r.timestamp,e.timescale,!1),n=Math.round(s-e.lastTimescaleUnits);e.lastSample.timescaleUnitsToNextSample=n}}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")){const s=e.track.metadata.maximumPacketCount;if(m(s!==void 0),e.samples.length>s)throw new Error(`Track #${e.track.id} has already reached the maximum packet count (${s}). 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);const s=r.timestamp-e.currentChunk.startTimestamp;if(this.isFragmented){const n=this.trackDatas.every(o=>{if(e===o)return r.type==="key";const a=o.sampleQueue[0];return a?a.type==="key":o.track.source._closed});s>=this.minimumFragmentDuration&&n&&r.timestamp>this.maxWrittenTimestamp&&(i=!0,await this.finalizeFragment())}else i=s>=.5}i&&(e.currentChunk&&await this.finalizeCurrentChunk(e),e.currentChunk={startTimestamp:r.timestamp,samples:[],offset:null,moofOffset:null}),m(e.currentChunk),e.currentChunk.samples.push(r),this.isFragmented&&(this.maxWrittenTimestamp=Math.max(this.maxWrittenTimestamp,r.timestamp))}async finalizeCurrentChunk(e){if(m(!this.isFragmented),!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,s)=>i+z(s.duration,e.timescale),0)),(e.compactlyCodedChunkTable.length===0||j(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(const i of e.currentChunk.samples)m(i.data),this.writer.write(i.data),i.data=null;await this.writer.flush()}async interleaveSamples(e=!1){if(m(this.isFragmented),!(!e&&!this.allTracksAreKnown()))e:for(;;){let r=null,i=1/0;for(const n of this.trackDatas){if(!e&&n.sampleQueue.length===0&&!n.track.source._closed)break e;n.sampleQueue.length>0&&n.sampleQueue[0].timestamp<i&&(r=n,i=n.sampleQueue[0].timestamp)}if(!r)break;const s=r.sampleQueue.shift();await this.addSampleToTrack(r,s)}}async finalizeFragment(e=!0){m(this.isFragmented);const r=this.nextFragmentNumber++;if(r===1){this.format._options.onMoov&&this.writer.startTrackingWrites();const g=Fe(this);if(this.boxWriter.writeBox(g),this.format._options.onMoov){const{data:p,start:y}=this.writer.stopTrackingWrites();this.format._options.onMoov(p,y)}}const i=this.trackDatas.filter(g=>g.currentChunk),s=hr(r,i),n=this.writer.getPos(),o=n+this.boxWriter.measureBox(s);let a=o+Je,c=1/0;for(const g of i){g.currentChunk.offset=a,g.currentChunk.moofOffset=n;for(const p of g.currentChunk.samples)a+=p.size;c=Math.min(c,g.currentChunk.startTimestamp)}const l=a-o,d=l>=2**32;if(d)for(const g of i)g.currentChunk.offset+=Xt-Je;this.format._options.onMoof&&this.writer.startTrackingWrites();const u=hr(r,i);if(this.boxWriter.writeBox(u),this.format._options.onMoof){const{data:g,start:p}=this.writer.stopTrackingWrites();this.format._options.onMoof(g,p,c)}m(this.writer.getPos()===o),this.format._options.onMdat&&this.writer.startTrackingWrites();const f=We(d);f.size=l,this.boxWriter.writeBox(f),this.writer.seek(o+(d?Xt:Je));for(const g of i)for(const p of g.currentChunk.samples)this.writer.write(p.data),p.data=null;if(this.format._options.onMdat){const{data:g,start:p}=this.writer.stopTrackingWrites();this.format._options.onMdat(g,p)}for(const g of i)g.finalizedChunks.push(g.currentChunk),this.finalizedChunks.push(g.currentChunk),g.currentChunk=null;e&&await this.writer.flush()}async registerSampleFastStartReserve(e,r){if(this.allTracksAreKnown()){if(!this.mdat){const i=Fe(this),n=this.boxWriter.measureBox(i)+this.computeSampleTableSizeUpperBound()+4096;m(this.ftypSize!==null),this.writer.seek(this.ftypSize+n),this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat=We(!0),this.boxWriter.writeBox(this.mdat);for(const o of this.trackDatas){for(const a of o.sampleQueue)await this.addSampleToTrack(o,a);o.sampleQueue.length=0}}await this.addSampleToTrack(e,r)}else e.sampleQueue.push(r)}computeSampleTableSizeUpperBound(){m(this.fastStart==="reserve");let e=0;for(const r of this.trackDatas){const i=r.track.metadata.maximumPacketCount;m(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){const r=await this.mutex.acquire();if(e.type==="subtitle"&&e.source._codec==="webvtt"){const i=this.trackDatas.find(s=>s.track===e);i&&await this.processWebVTTCues(i,1/0)}this.allTracksAreKnown()&&this.allTracksKnown.resolve(),this.isFragmented&&await this.interleaveSamples(),r()}async finalize(){const e=await this.mutex.acquire();this.allTracksKnown.resolve();for(const r of this.trackDatas)r.type==="subtitle"&&r.track.source._codec==="webvtt"&&await this.processWebVTTCues(r,1/0);if(this.isFragmented){await this.interleaveSamples(!0);for(const r of this.trackDatas)this.processTimestamps(r);await this.finalizeFragment(!1)}else for(const r of this.trackDatas)this.processTimestamps(r),await this.finalizeCurrentChunk(r);if(this.fastStart==="in-memory"){this.mdat=We(!1);let r;for(let s=0;s<2;s++){const n=Fe(this),o=this.boxWriter.measureBox(n);r=this.boxWriter.measureBox(this.mdat);let a=this.writer.getPos()+o+r;for(const c of this.finalizedChunks){c.offset=a;for(const{data:l}of c.samples)m(l),a+=l.byteLength,r+=l.byteLength}if(a<2**32)break;r>=2**32&&(this.mdat.largeSize=!0)}this.format._options.onMoov&&this.writer.startTrackingWrites();const i=Fe(this);if(this.boxWriter.writeBox(i),this.format._options.onMoov){const{data:s,start:n}=this.writer.stopTrackingWrites();this.format._options.onMoov(s,n)}this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat.size=r,this.boxWriter.writeBox(this.mdat);for(const s of this.finalizedChunks)for(const n of s.samples)m(n.data),this.writer.write(n.data),n.data=null;if(this.format._options.onMdat){const{data:s,start:n}=this.writer.stopTrackingWrites();this.format._options.onMdat(s,n)}}else if(this.isFragmented){const r=this.writer.getPos(),i=Os(this.trackDatas);this.boxWriter.writeBox(i);const s=this.writer.getPos()-r;this.writer.seek(this.writer.getPos()-4),this.boxWriter.writeU32(s)}else{m(this.mdat);const r=this.boxWriter.offsets.get(this.mdat);m(r!==void 0);const 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){const{data:n,start:o}=this.writer.stopTrackingWrites();this.format._options.onMdat(n,o)}const s=Fe(this);if(this.fastStart==="reserve"){m(this.ftypSize!==null),this.writer.seek(this.ftypSize),this.format._options.onMoov&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(s);const n=this.boxWriter.offsets.get(this.mdat)-this.writer.getPos();this.boxWriter.writeBox(Qi(n))}else this.format._options.onMoov&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(s);if(this.format._options.onMoov){const{data:n,start:o}=this.writer.stopTrackingWrites();this.format._options.onMoov(n,o)}}e()}}/*!
|
|
142
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
143
|
+
*
|
|
144
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
145
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
146
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
147
|
+
*/const Js=-32768,en=2**15-1,Cr="Mediabunny",vr=6,Sr=5,tn={video:1,audio:2,subtitle:17};class rn extends rr{constructor(e,r){super(e),this.trackDatas=[],this.allTracksKnown=vt(),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.duration=0,this.writer=e._writer,this.format=r,this.ebmlWriter=new Wi(this.writer),this.format._options.appendOnly&&(this.writer.ensureMonotonicity=!0)}async start(){const e=await this.mutex.acquire();this.writeEBMLHeader(),this.createSegmentInfo(),this.createCues(),await this.writer.flush(),e()}writeEBMLHeader(){this.format._options.onEbmlHeader&&this.writer.startTrackingWrites();const e={id:h.EBML,data:[{id:h.EBMLVersion,data:1},{id:h.EBMLReadVersion,data:1},{id:h.EBMLMaxIDLength,data:4},{id:h.EBMLMaxSizeLength,data:8},{id:h.DocType,data:this.format instanceof ut?"webm":"matroska"},{id:h.DocTypeVersion,data:2},{id:h.DocTypeReadVersion,data:2}]};if(this.ebmlWriter.writeEBML(e),this.format._options.onEbmlHeader){const{data:r,start:i}=this.writer.stopTrackingWrites();this.format._options.onEbmlHeader(r,i)}}maybeCreateSeekHead(e){if(this.format._options.appendOnly)return;const r=new Uint8Array([28,83,187,107]),i=new Uint8Array([21,73,169,102]),s=new Uint8Array([22,84,174,107]),n=new Uint8Array([25,65,164,105]),o=new Uint8Array([18,84,195,103]),a={id:h.SeekHead,data:[{id:h.Seek,data:[{id:h.SeekID,data:r},{id:h.SeekPosition,size:5,data:e?this.ebmlWriter.offsets.get(this.cues)-this.segmentDataOffset:0}]},{id:h.Seek,data:[{id:h.SeekID,data:i},{id:h.SeekPosition,size:5,data:e?this.ebmlWriter.offsets.get(this.segmentInfo)-this.segmentDataOffset:0}]},{id:h.Seek,data:[{id:h.SeekID,data:s},{id:h.SeekPosition,size:5,data:e?this.ebmlWriter.offsets.get(this.tracksElement)-this.segmentDataOffset:0}]},this.attachmentsElement?{id:h.Seek,data:[{id:h.SeekID,data:n},{id:h.SeekPosition,size:5,data:e?this.ebmlWriter.offsets.get(this.attachmentsElement)-this.segmentDataOffset:0}]}:null,this.tagsElement?{id:h.Seek,data:[{id:h.SeekID,data:o},{id:h.SeekPosition,size:5,data:e?this.ebmlWriter.offsets.get(this.tagsElement)-this.segmentDataOffset:0}]}:null]};this.seekHead=a}createSegmentInfo(){const e={id:h.Duration,data:new tt(0)};this.segmentDuration=e;const r={id:h.Info,data:[{id:h.TimestampScale,data:1e6},{id:h.MuxingApp,data:Cr},{id:h.WritingApp,data:Cr},this.format._options.appendOnly?null:e]};this.segmentInfo=r}createTracks(){const e={id:h.Tracks,data:[]};this.tracksElement=e;for(const r of this.trackDatas){const i=Ni[r.track.source._codec];m(i);let s=0;if(r.type==="audio"&&r.track.source._codec==="opus"){s=1e6*80;const n=r.info.decoderConfig.description;if(n){const o=q(n),a=Nt(o);s=Math.round(1e9*(a.preSkip/si))}}e.data.push({id:h.TrackEntry,data:[{id:h.TrackNumber,data:r.track.id},{id:h.TrackUID,data:r.track.id},{id:h.TrackType,data:tn[r.type]},r.track.metadata.disposition?.default===!1?{id:h.FlagDefault,data:0}:null,r.track.metadata.disposition?.forced?{id:h.FlagForced,data:1}:null,r.track.metadata.disposition?.hearingImpaired?{id:h.FlagHearingImpaired,data:1}:null,r.track.metadata.disposition?.visuallyImpaired?{id:h.FlagVisualImpaired,data:1}:null,r.track.metadata.disposition?.original?{id:h.FlagOriginal,data:1}:null,r.track.metadata.disposition?.commentary?{id:h.FlagCommentary,data:1}:null,{id:h.FlagLacing,data:0},{id:h.Language,data:r.track.metadata.languageCode??St},{id:h.CodecID,data:i},{id:h.CodecDelay,data:0},{id:h.SeekPreRoll,data:s},r.track.metadata.name!==void 0?{id:h.Name,data:new de(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){const{frameRate:r,rotation:i}=e.track.metadata,s=[e.info.decoderConfig.description?{id:h.CodecPrivate,data:q(e.info.decoderConfig.description)}:null,r?{id:h.DefaultDuration,data:1e9/r}:null],n=i?Lr(-i):0,o=e.info.decoderConfig.colorSpace,a={id:h.Video,data:[{id:h.PixelWidth,data:e.info.width},{id:h.PixelHeight,data:e.info.height},e.info.alphaMode?{id:h.AlphaMode,data:1}:null,Tt(o)?{id:h.Colour,data:[{id:h.MatrixCoefficients,data:Ie[o.matrix]},{id:h.TransferCharacteristics,data:Re[o.transfer]},{id:h.Primaries,data:Be[o.primaries]},{id:h.Range,data:o.fullRange?2:1}]}:null,n?{id:h.Projection,data:[{id:h.ProjectionType,data:0},{id:h.ProjectionPoseRoll,data:new et((n+180)%360-180)}]}:null]};return s.push(a),s}audioSpecificTrackInfo(e){const r=Q.includes(e.track.source._codec)?pe(e.track.source._codec):null;return[e.info.decoderConfig.description?{id:h.CodecPrivate,data:q(e.info.decoderConfig.description)}:null,{id:h.Audio,data:[{id:h.SamplingFrequency,data:new et(e.info.sampleRate)},{id:h.Channels,data:e.info.numberOfChannels},r?{id:h.BitDepth,data:8*r.sampleSize}:null]}]}subtitleSpecificTrackInfo(e){return[{id:h.CodecPrivate,data:H.encode(e.info.config.description)}]}maybeCreateTags(){const e=[],r=(n,o)=>{e.push({id:h.SimpleTag,data:[{id:h.TagName,data:new de(n)},typeof o=="string"?{id:h.TagString,data:new de(o)}:{id:h.TagBinary,data:o}]})},i=this.output._metadataTags,s=new Set;for(const{key:n,value:o}of qe(i))switch(n){case"title":r("TITLE",o),s.add("TITLE");break;case"description":r("DESCRIPTION",o),s.add("DESCRIPTION");break;case"artist":r("ARTIST",o),s.add("ARTIST");break;case"album":r("ALBUM",o),s.add("ALBUM");break;case"albumArtist":r("ALBUM_ARTIST",o),s.add("ALBUM_ARTIST");break;case"genre":r("GENRE",o),s.add("GENRE");break;case"comment":r("COMMENT",o),s.add("COMMENT");break;case"lyrics":r("LYRICS",o),s.add("LYRICS");break;case"date":r("DATE",o.toISOString().slice(0,10)),s.add("DATE");break;case"trackNumber":{const a=i.tracksTotal!==void 0?`${o}/${i.tracksTotal}`:o.toString();r("PART_NUMBER",a),s.add("PART_NUMBER")}break;case"discNumber":{const a=i.discsTotal!==void 0?`${o}/${i.discsTotal}`:o.toString();r("DISC",a),s.add("DISC")}break;case"tracksTotal":case"discsTotal":break;case"images":case"raw":break;default:ve(n)}if(i.raw)for(const n in i.raw){const o=i.raw[n];o==null||s.has(n)||(typeof o=="string"||o instanceof Uint8Array)&&r(n,o)}e.length!==0&&(this.tagsElement={id:h.Tags,data:[{id:h.Tag,data:[{id:h.Targets,data:[{id:h.TargetTypeValue,data:50},{id:h.TargetType,data:"MOVIE"}]},...e]}]})}maybeCreateAttachments(){const e=this.output._metadataTags,r=[],i=new Set,s=e.images??[];for(const n of s){let o=n.name;o===void 0&&(o=(n.kind==="coverFront"?"cover":n.kind==="coverBack"?"back":"image")+(Xr(n.mimeType)??""));let a;for(;;){a=0n;for(let c=0;c<8;c++)a<<=8n,a|=BigInt(Math.floor(Math.random()*256));if(a!==0n&&!i.has(a))break}i.add(a),r.push({id:h.AttachedFile,data:[n.description!==void 0?{id:h.FileDescription,data:new de(n.description)}:null,{id:h.FileName,data:new de(o)},{id:h.FileMediaType,data:n.mimeType},{id:h.FileData,data:n.data},{id:h.FileUID,data:a}]})}for(const[n,o]of Object.entries(e.raw??{}))!(o instanceof At)||!/^\d+$/.test(n)||s.find(c=>c.mimeType===o.mimeType&&Kr(c.data,o.data))||r.push({id:h.AttachedFile,data:[o.description!==void 0?{id:h.FileDescription,data:new de(o.description)}:null,{id:h.FileName,data:new de(o.name??"")},{id:h.FileMediaType,data:o.mimeType??""},{id:h.FileData,data:o.data},{id:h.FileUID,data:BigInt(n)}]});r.length!==0&&(this.attachmentsElement={id:h.Attachments,data:r})}createSegment(){this.createTracks(),this.maybeCreateTags(),this.maybeCreateAttachments(),this.maybeCreateSeekHead(!1);const e={id:h.Segment,size:this.format._options.appendOnly?-1:vr,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){const{data:r,start:i}=this.writer.stopTrackingWrites();this.format._options.onSegmentHeader(r,i)}}createCues(){this.cues={id:h.Cues,data:[]}}get segmentDataOffset(){return m(this.segment),this.ebmlWriter.dataOffsets.get(this.segment)}allTracksAreKnown(){for(const 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;const e=this.trackDatas.map(r=>r.type==="video"||r.type==="audio"?r.info.decoderConfig.codec:{webvtt:"wvtt"}[r.track.source._codec]);return Di({isWebM:this.format instanceof ut,hasVideo:this.trackDatas.some(r=>r.type==="video"),hasAudio:this.trackDatas.some(r=>r.type==="audio"),codecStrings:e})}getVideoTrackData(e,r,i){const s=this.trackDatas.find(o=>o.track===e);if(s)return s;Ot(i),m(i),m(i.decoderConfig),m(i.decoderConfig.codedWidth!==void 0),m(i.decoderConfig.codedHeight!==void 0);const n={track:e,type:"video",info:{width:i.decoderConfig.codedWidth,height:i.decoderConfig.codedHeight,decoderConfig:i.decoderConfig,alphaMode:!!r.sideData.alpha},chunkQueue:[],lastWrittenMsTimestamp:null};return e.source._codec==="vp9"?n.info.decoderConfig={...n.info.decoderConfig,description:new Uint8Array(ti(n.info.decoderConfig.codec))}:e.source._codec==="av1"&&(n.info.decoderConfig={...n.info.decoderConfig,description:new Uint8Array(Bt(n.info.decoderConfig.codec))}),this.trackDatas.push(n),this.trackDatas.sort((o,a)=>o.track.id-a.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),n}getAudioTrackData(e,r,i){const s=this.trackDatas.find(c=>c.track===e);if(s)return s;Mt(i),m(i),m(i.decoderConfig);const n={...i.decoderConfig};let o=!1;if(e.source._codec==="aac"&&!n.description){const c=ze(be.tempFromBytes(r.data));if(!c)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.");const l=Ee[c.samplingFrequencyIndex],d=Ve[c.channelConfiguration];if(l===void 0||d===void 0)throw new Error("Invalid ADTS frame header.");n.description=Ge({objectType:c.objectType,sampleRate:l,numberOfChannels:d}),o=!0}const a={track:e,type:"audio",info:{numberOfChannels:i.decoderConfig.numberOfChannels,sampleRate:i.decoderConfig.sampleRate,decoderConfig:n,requiresAdtsStripping:o},chunkQueue:[],lastWrittenMsTimestamp:null};return this.trackDatas.push(a),this.trackDatas.sort((c,l)=>c.track.id-l.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),a}getSubtitleTrackData(e,r){const i=this.trackDatas.find(n=>n.track===e);if(i)return i;Vt(r),m(r),m(r.config);const s={track:e,type:"subtitle",info:{config:r.config},chunkQueue:[],lastWrittenMsTimestamp:null};return this.trackDatas.push(s),this.trackDatas.sort((n,o)=>n.track.id-o.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),s}async addEncodedVideoPacket(e,r,i){const s=await this.mutex.acquire();try{const n=this.getVideoTrackData(e,r,i),o=r.type==="key";let a=this.validateAndNormalizeTimestamp(n.track,r.timestamp,o),c=r.duration;e.metadata.frameRate!==void 0&&(a=xt(a,1/e.metadata.frameRate),c=xt(c,1/e.metadata.frameRate));const l=n.info.alphaMode?r.sideData.alpha??null:null,d=this.createInternalChunk(r.data,a,c,r.type,l);e.source._codec==="vp9"&&this.fixVP9ColorSpace(n,d),n.chunkQueue.push(d),await this.interleaveChunks()}finally{s()}}async addEncodedAudioPacket(e,r,i){const s=await this.mutex.acquire();try{const n=this.getAudioTrackData(e,r,i);let o=r.data;if(n.info.requiresAdtsStripping){const d=ze(be.tempFromBytes(o));if(!d)throw new Error("Expected ADTS frame, didn't get one.");const u=d.crcCheck===null?er:tr;o=o.subarray(u)}const a=r.type==="key",c=this.validateAndNormalizeTimestamp(n.track,r.timestamp,a),l=this.createInternalChunk(o,c,r.duration,r.type);n.chunkQueue.push(l),await this.interleaveChunks()}finally{s()}}async addSubtitleCue(e,r,i){const s=await this.mutex.acquire();try{const n=this.getSubtitleTrackData(e,i),o=this.validateAndNormalizeTimestamp(n.track,r.timestamp,!0);let a=r.text;const c=Math.round(o*1e3);Ue.lastIndex=0,a=a.replace(Ue,f=>{const p=ji(f.slice(1,-1))-c;return`<${ir(p)}>`});const l=H.encode(a),d=`${r.settings??""}
|
|
148
|
+
${r.identifier??""}
|
|
149
|
+
${r.notes??""}`,u=this.createInternalChunk(l,o,r.duration,"key",d.trim()?H.encode(d):null);n.chunkQueue.push(u),await this.interleaveChunks()}finally{s()}}async interleaveChunks(e=!1){if(!(!e&&!this.allTracksAreKnown())){e:for(;;){let r=null,i=1/0;for(const n of this.trackDatas){if(!e&&n.chunkQueue.length===0&&!n.track.source._closed)break e;n.chunkQueue.length>0&&n.chunkQueue[0].timestamp<i&&(r=n,i=n.chunkQueue[0].timestamp)}if(!r)break;const s=r.chunkQueue.shift();this.writeBlock(r,s)}e||await this.writer.flush()}}fixVP9ColorSpace(e,r){if(r.type!=="key"||!e.info.decoderConfig.colorSpace||!e.info.decoderConfig.colorSpace.matrix)return;const i=new ee(r.data);i.skipBits(2);const s=i.readBits(1),o=(i.readBits(1)<<1)+s;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);const d={rgb:7,bt709:2,bt470bg:1,smpte170m:3}[e.info.decoderConfig.colorSpace.matrix];Hr(r.data,i.pos,i.pos+3,d)}createInternalChunk(e,r,i,s,n=null){return{data:e,type:s,timestamp:r,duration:i,additions:n}}writeBlock(e,r){this.segment||this.createSegment();const i=Math.round(1e3*r.timestamp),s=this.trackDatas.every(d=>{if(e===d)return r.type==="key";const u=d.chunkQueue[0];return u?u.type==="key":d.track.source._closed});let n=!1;if(!this.currentCluster)n=!0;else{m(this.currentClusterStartMsTimestamp!==null),m(this.currentClusterMaxMsTimestamp!==null);const d=i-this.currentClusterStartMsTimestamp;n=s&&i>this.currentClusterMaxMsTimestamp&&d>=1e3*(this.format._options.minimumClusterDuration??1)||d>en}n&&this.createNewCluster(i);const o=i-this.currentClusterStartMsTimestamp;if(o<Js)return;const a=new Uint8Array(4),c=new DataView(a.buffer);c.setUint8(0,128|e.track.id),c.setInt16(1,o,!1);const l=Math.round(1e3*r.duration);if(r.additions){const d={id:h.BlockGroup,data:[{id:h.Block,data:[a,r.data]},r.type==="delta"?{id:h.ReferenceBlock,data:new Kt(e.lastWrittenMsTimestamp-i)}:null,r.additions?{id:h.BlockAdditions,data:[{id:h.BlockMore,data:[{id:h.BlockAddID,data:1},{id:h.BlockAdditional,data:r.additions}]}]}:null,l>0?{id:h.BlockDuration,data:l}:null]};this.ebmlWriter.writeEBML(d)}else{c.setUint8(3,+(r.type==="key")<<7);const d={id:h.SimpleBlock,data:[a,r.data]};this.ebmlWriter.writeEBML(d)}this.duration=Math.max(this.duration,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:h.Cluster,size:this.format._options.appendOnly?-1:Sr,data:[{id:h.Timestamp,data:e}]},this.ebmlWriter.writeEBML(this.currentCluster),this.currentClusterStartMsTimestamp=e,this.currentClusterMaxMsTimestamp=e,this.trackDatasInCurrentCluster.clear()}finalizeCurrentCluster(){if(m(this.currentCluster),!this.format._options.appendOnly){const s=this.writer.getPos()-this.ebmlWriter.dataOffsets.get(this.currentCluster),n=this.writer.getPos();this.writer.seek(this.ebmlWriter.offsets.get(this.currentCluster)+4),this.ebmlWriter.writeVarInt(s,Sr),this.writer.seek(n)}if(this.format._options.onCluster){m(this.currentClusterStartMsTimestamp!==null);const{data:s,start:n}=this.writer.stopTrackingWrites();this.format._options.onCluster(s,n,this.currentClusterStartMsTimestamp/1e3)}const e=this.ebmlWriter.offsets.get(this.currentCluster)-this.segmentDataOffset,r=new Map;for(const[s,{firstMsTimestamp:n}]of this.trackDatasInCurrentCluster)r.has(n)||r.set(n,[]),r.get(n).push(s);const i=[...r.entries()].sort((s,n)=>s[0]-n[0]);for(const[s,n]of i)m(this.cues),this.cues.data.push({id:h.CuePoint,data:[{id:h.CueTime,data:s},...n.map(o=>({id:h.CueTrackPositions,data:[{id:h.CueTrack,data:o.track.id},{id:h.CueClusterPosition,data:e}]}))]})}async onTrackClose(){const e=await this.mutex.acquire();this.allTracksAreKnown()&&this.allTracksKnown.resolve(),await this.interleaveChunks(),e()}async finalize(){const e=await this.mutex.acquire();if(this.allTracksKnown.resolve(),this.segment||this.createSegment(),await this.interleaveChunks(!0),this.currentCluster&&this.finalizeCurrentCluster(),m(this.cues),this.ebmlWriter.writeEBML(this.cues),!this.format._options.appendOnly){const r=this.writer.getPos(),i=this.writer.getPos()-this.segmentDataOffset;this.writer.seek(this.ebmlWriter.offsets.get(this.segment)+4),this.ebmlWriter.writeVarInt(i,vr),this.segmentDuration.data=new tt(this.duration),this.writer.seek(this.ebmlWriter.offsets.get(this.segmentDuration)),this.ebmlWriter.writeEBML(this.segmentDuration),m(this.seekHead),this.writer.seek(this.ebmlWriter.offsets.get(this.seekHead)),this.maybeCreateSeekHead(!0),this.ebmlWriter.writeEBML(this.seekHead),this.writer.seek(r)}e()}}/*!
|
|
150
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
151
|
+
*
|
|
152
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
153
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
154
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
155
|
+
*/class dt{getSupportedVideoCodecs(){return this.getSupportedCodecs().filter(e=>re.includes(e))}getSupportedAudioCodecs(){return this.getSupportedCodecs().filter(e=>me.includes(e))}getSupportedSubtitleCodecs(){return this.getSupportedCodecs().filter(e=>Se.includes(e))}_codecUnsupportedHint(e){return""}}class xr extends dt{constructor(e={}){if(!e||typeof e!="object")throw new TypeError("options must be an object.");if(e.fastStart!==void 0&&![!1,"in-memory","reserve","fragmented"].includes(e.fastStart))throw new TypeError("options.fastStart, when provided, must be false, 'in-memory', 'reserve', or 'fragmented'.");if(e.minimumFragmentDuration!==void 0&&(!Number.isFinite(e.minimumFragmentDuration)||e.minimumFragmentDuration<0))throw new TypeError("options.minimumFragmentDuration, when provided, must be a non-negative number.");if(e.onFtyp!==void 0&&typeof e.onFtyp!="function")throw new TypeError("options.onFtyp, when provided, must be a function.");if(e.onMoov!==void 0&&typeof e.onMoov!="function")throw new TypeError("options.onMoov, when provided, must be a function.");if(e.onMdat!==void 0&&typeof e.onMdat!="function")throw new TypeError("options.onMdat, when provided, must be a function.");if(e.onMoof!==void 0&&typeof e.onMoof!="function")throw new TypeError("options.onMoof, when provided, must be a function.");if(e.metadataFormat!==void 0&&!["mdir","mdta","udta","auto"].includes(e.metadataFormat))throw new TypeError("options.metadataFormat, when provided, must be either 'auto', 'mdir', 'mdta', or 'udta'.");super(),this._options=e}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}_createMuxer(e){return new Zs(e,this)}}class kr extends xr{constructor(e){super(e)}get _name(){return"MP4"}get fileExtension(){return".mp4"}get mimeType(){return"video/mp4"}getSupportedCodecs(){return[...re,...Qe,"pcm-s16","pcm-s16be","pcm-s24","pcm-s24be","pcm-s32","pcm-s32be","pcm-f32","pcm-f32be","pcm-f64","pcm-f64be",...Se]}_codecUnsupportedHint(e){return new Er().getSupportedCodecs().includes(e)?" Switching to MOV will grant support for this codec.":""}}class Er extends xr{constructor(e){super(e)}get _name(){return"MOV"}get fileExtension(){return".mov"}get mimeType(){return"video/quicktime"}getSupportedCodecs(){return[...re,...me]}_codecUnsupportedHint(e){return new kr().getSupportedCodecs().includes(e)?" Switching to MP4 will grant support for this codec.":""}}class Ar extends dt{constructor(e={}){if(!e||typeof e!="object")throw new TypeError("options must be an object.");if(e.appendOnly!==void 0&&typeof e.appendOnly!="boolean")throw new TypeError("options.appendOnly, when provided, must be a boolean.");if(e.minimumClusterDuration!==void 0&&(!Number.isFinite(e.minimumClusterDuration)||e.minimumClusterDuration<0))throw new TypeError("options.minimumClusterDuration, when provided, must be a non-negative number.");if(e.onEbmlHeader!==void 0&&typeof e.onEbmlHeader!="function")throw new TypeError("options.onEbmlHeader, when provided, must be a function.");if(e.onSegmentHeader!==void 0&&typeof e.onSegmentHeader!="function")throw new TypeError("options.onHeader, when provided, must be a function.");if(e.onCluster!==void 0&&typeof e.onCluster!="function")throw new TypeError("options.onCluster, when provided, must be a function.");super(),this._options=e}_createMuxer(e){return new rn(e,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,...Qe,...Q.filter(e=>!["pcm-s8","pcm-f32be","pcm-f64be","ulaw","alaw"].includes(e)),...Se]}get supportsVideoRotationMetadata(){return!1}}class ut extends Ar{constructor(e){super(e)}getSupportedCodecs(){return[...re.filter(e=>["vp8","vp9","av1"].includes(e)),...me.filter(e=>["opus","vorbis"].includes(e)),...Se]}get _name(){return"WebM"}get fileExtension(){return".webm"}get mimeType(){return"video/webm"}_codecUnsupportedHint(e){return new Ar().getSupportedCodecs().includes(e)?" Switching to MKV will grant support for this codec.":""}}/*!
|
|
156
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
157
|
+
*
|
|
158
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
159
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
160
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
161
|
+
*/const sn=t=>{if(!t||typeof t!="object")throw new TypeError("Encoding config must be an object.");if(!re.includes(t.codec))throw new TypeError(`Invalid video codec '${t.codec}'. Must be one of: ${re.join(", ")}.`);if(!(t.bitrate instanceof Ne)&&(!Number.isInteger(t.bitrate)||t.bitrate<=0))throw new TypeError("config.bitrate must be a positive integer or a quality.");if(t.keyFrameInterval!==void 0&&(!Number.isFinite(t.keyFrameInterval)||t.keyFrameInterval<0))throw new TypeError("config.keyFrameInterval, when provided, must be a non-negative number.");if(t.sizeChangeBehavior!==void 0&&!["deny","passThrough","fill","contain","cover"].includes(t.sizeChangeBehavior))throw new TypeError("config.sizeChangeBehavior, when provided, must be 'deny', 'passThrough', 'fill', 'contain' or 'cover'.");if(t.onEncodedPacket!==void 0&&typeof t.onEncodedPacket!="function")throw new TypeError("config.onEncodedChunk, when provided, must be a function.");if(t.onEncoderConfig!==void 0&&typeof t.onEncoderConfig!="function")throw new TypeError("config.onEncoderConfig, when provided, must be a function.");nn(t.codec,t)},nn=(t,e)=>{if(!e||typeof e!="object")throw new TypeError("Encoding options must be an object.");if(e.alpha!==void 0&&!["discard","keep"].includes(e.alpha))throw new TypeError("options.alpha, when provided, must be 'discard' or 'keep'.");if(e.bitrateMode!==void 0&&!["constant","variable"].includes(e.bitrateMode))throw new TypeError("bitrateMode, when provided, must be 'constant' or 'variable'.");if(e.latencyMode!==void 0&&!["quality","realtime"].includes(e.latencyMode))throw new TypeError("latencyMode, when provided, must be 'quality' or 'realtime'.");if(e.fullCodecString!==void 0&&typeof e.fullCodecString!="string")throw new TypeError("fullCodecString, when provided, must be a string.");if(e.fullCodecString!==void 0&&It(e.fullCodecString)!==t)throw new TypeError(`fullCodecString, when provided, must be a string that matches the specified codec (${t}).`);if(e.hardwareAcceleration!==void 0&&!["no-preference","prefer-hardware","prefer-software"].includes(e.hardwareAcceleration))throw new TypeError("hardwareAcceleration, when provided, must be 'no-preference', 'prefer-hardware' or 'prefer-software'.");if(e.scalabilityMode!==void 0&&typeof e.scalabilityMode!="string")throw new TypeError("scalabilityMode, when provided, must be a string.");if(e.contentHint!==void 0&&typeof e.contentHint!="string")throw new TypeError("contentHint, when provided, must be a string.")},on=t=>{const e=t.bitrate instanceof Ne?t.bitrate._toVideoBitrate(t.codec,t.width,t.height):t.bitrate;return{codec:t.fullCodecString??ei(t.codec,t.width,t.height,e),width:t.width,height:t.height,bitrate:e,bitrateMode:t.bitrateMode,alpha:t.alpha??"discard",framerate:t.framerate,latencyMode:t.latencyMode,hardwareAcceleration:t.hardwareAcceleration,scalabilityMode:t.scalabilityMode,contentHint:t.contentHint,...ni(t.codec)}},an=t=>{if(!t||typeof t!="object")throw new TypeError("Encoding config must be an object.");if(!me.includes(t.codec))throw new TypeError(`Invalid audio codec '${t.codec}'. Must be one of: ${me.join(", ")}.`);if(t.bitrate===void 0&&(!Q.includes(t.codec)||t.codec==="flac"))throw new TypeError("config.bitrate must be provided for compressed audio codecs.");if(t.bitrate!==void 0&&!(t.bitrate instanceof Ne)&&(!Number.isInteger(t.bitrate)||t.bitrate<=0))throw new TypeError("config.bitrate, when provided, must be a positive integer or a quality.");if(t.onEncodedPacket!==void 0&&typeof t.onEncodedPacket!="function")throw new TypeError("config.onEncodedChunk, when provided, must be a function.");if(t.onEncoderConfig!==void 0&&typeof t.onEncoderConfig!="function")throw new TypeError("config.onEncoderConfig, when provided, must be a function.");cn(t.codec,t)},cn=(t,e)=>{if(!e||typeof e!="object")throw new TypeError("Encoding options must be an object.");if(e.bitrateMode!==void 0&&!["constant","variable"].includes(e.bitrateMode))throw new TypeError("bitrateMode, when provided, must be 'constant' or 'variable'.");if(e.fullCodecString!==void 0&&typeof e.fullCodecString!="string")throw new TypeError("fullCodecString, when provided, must be a string.");if(e.fullCodecString!==void 0&&It(e.fullCodecString)!==t)throw new TypeError(`fullCodecString, when provided, must be a string that matches the specified codec (${t}).`)},ln=t=>{const e=t.bitrate instanceof Ne?t.bitrate._toAudioBitrate(t.codec):t.bitrate;return{codec:t.fullCodecString??ri(t.codec,t.numberOfChannels,t.sampleRate),numberOfChannels:t.numberOfChannels,sampleRate:t.sampleRate,bitrate:e,bitrateMode:t.bitrateMode,...oi(t.codec)}};class Ne{constructor(e){this._factor=e}_toVideoBitrate(e,r,i){const s=r*i,n={avc:1,hevc:.6,vp9:.6,av1:.4,vp8:1.2},o=1920*1080,a=3e6,c=Math.pow(s/o,.95),u=a*c*n[e]*this._factor;return Math.ceil(u/1e3)*1e3}_toAudioBitrate(e){if(Q.includes(e)||e==="flac")return;const i={aac:128e3,opus:64e3,mp3:16e4,vorbis:64e3}[e];if(!i)throw new Error(`Unhandled codec: ${e}`);let s=i*this._factor;return e==="aac"?s=[96e3,128e3,16e4,192e3].reduce((o,a)=>Math.abs(a-s)<Math.abs(o-s)?a:o):e==="opus"||e==="vorbis"?s=Math.max(6e3,s):e==="mp3"&&(s=[8e3,16e3,24e3,32e3,4e4,48e3,64e3,8e4,96e3,112e3,128e3,16e4,192e3,224e3,256e3,32e4].reduce((o,a)=>Math.abs(a-s)<Math.abs(o-s)?a:o)),Math.round(s/1e3)*1e3}}/*!
|
|
162
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
163
|
+
*
|
|
164
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
165
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
166
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
167
|
+
*/class ht{constructor(){this._connectedTrack=null,this._closingPromise=null,this._closed=!1,this._timestampOffset=0}_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(e){}close(){if(this._closingPromise)return;const e=this._connectedTrack;if(!e)throw new Error("Cannot call close without connecting the source to an output track.");if(e.output.state==="pending")throw new Error("Cannot call close before output has been started.");this._closingPromise=(async()=>{await this._flushAndClose(!1),this._closed=!0,!(e.output.state==="finalizing"||e.output.state==="finalized")&&e.output._muxer.onTrackClose(e)})()}async _flushOrWaitForOngoingClose(e){return this._closingPromise??=(async()=>{await this._flushAndClose(e),this._closed=!0})()}}class _r extends ht{constructor(e){if(super(),this._connectedTrack=null,!re.includes(e))throw new TypeError(`Invalid video codec '${e}'. Must be one of: ${re.join(", ")}.`);this._codec=e}}class dn{constructor(e,r){this.source=e,this.encodingConfig=r,this.ensureEncoderPromise=null,this.encoderInitialized=!1,this.encoder=null,this.muxer=null,this.lastMultipleOfKeyFrameInterval=-1,this.codedWidth=null,this.codedHeight=null,this.resizeCanvas=null,this.customEncoder=null,this.customEncoderCallSerializer=new kt,this.customEncoderQueueSize=0,this.alphaEncoder=null,this.splitter=null,this.splitterCreationFailed=!1,this.alphaFrameQueue=[],this.error=null,this.errorNeedsNewStack=!0}async add(e,r,i){try{if(this.checkForEncoderError(),this.source._ensureValidAdd(),this.codedWidth!==null&&this.codedHeight!==null){if(e.codedWidth!==this.codedWidth||e.codedHeight!==this.codedHeight){const a=this.encodingConfig.sizeChangeBehavior??"deny";if(a!=="passThrough"){if(a==="deny")throw new Error(`Video sample size must remain constant. Expected ${this.codedWidth}x${this.codedHeight}, got ${e.codedWidth}x${e.codedHeight}. To allow the sample size to change over time, set \`sizeChangeBehavior\` to a value other than 'strict' in the encoding options.`);{let c=!1;this.resizeCanvas||(typeof document<"u"?(this.resizeCanvas=document.createElement("canvas"),this.resizeCanvas.width=this.codedWidth,this.resizeCanvas.height=this.codedHeight):this.resizeCanvas=new OffscreenCanvas(this.codedWidth,this.codedHeight),c=!0);const l=this.resizeCanvas.getContext("2d",{alpha:je()});m(l),c||(je()?(l.fillStyle="black",l.fillRect(0,0,this.codedWidth,this.codedHeight)):l.clearRect(0,0,this.codedWidth,this.codedHeight)),e.drawWithFit(l,{fit:a}),r&&e.close(),e=new ie(this.resizeCanvas,{timestamp:e.timestamp,duration:e.duration,rotation:e.rotation}),r=!0}}}}else this.codedWidth=e.codedWidth,this.codedHeight=e.codedHeight;this.encoderInitialized||(this.ensureEncoderPromise||this.ensureEncoder(e),this.encoderInitialized||await this.ensureEncoderPromise),m(this.encoderInitialized);const s=this.encodingConfig.keyFrameInterval??5,n=Math.floor(e.timestamp/s),o={...i,keyFrame:i?.keyFrame||s===0||n!==this.lastMultipleOfKeyFrameInterval};if(this.lastMultipleOfKeyFrameInterval=n,this.customEncoder){this.customEncoderQueueSize++;const a=e.clone(),c=this.customEncoderCallSerializer.call(()=>this.customEncoder.encode(a,o)).then(()=>this.customEncoderQueueSize--).catch(l=>this.error??=l).finally(()=>{a.close()});this.customEncoderQueueSize>=4&&await c}else{m(this.encoder);const a=e.toVideoFrame();if(!this.alphaEncoder)this.encoder.encode(a,o),a.close();else if(!!a.format&&!a.format.includes("A")||this.splitterCreationFailed)this.alphaFrameQueue.push(null),this.encoder.encode(a,o),a.close();else{const l=a.displayWidth,d=a.displayHeight;if(!this.splitter)try{this.splitter=new un(l,d)}catch(u){console.error("Due to an error, only color data will be encoded.",u),this.splitterCreationFailed=!0,this.alphaFrameQueue.push(null),this.encoder.encode(a,o),a.close()}if(this.splitter){const u=this.splitter.extractColor(a),f=this.splitter.extractAlpha(a);this.alphaFrameQueue.push(f),this.encoder.encode(u,o),u.close(),a.close()}}r&&e.close(),this.encoder.encodeQueueSize>=4&&await new Promise(c=>this.encoder.addEventListener("dequeue",c,{once:!0}))}await this.muxer.mutex.currentPromise}finally{r&&e.close()}}ensureEncoder(e){const r=new Error;this.ensureEncoderPromise=(async()=>{const i=on({width:e.codedWidth,height:e.codedHeight,...this.encodingConfig,framerate:this.source._connectedTrack?.metadata.frameRate});this.encodingConfig.onEncoderConfig?.(i);const s=Ai.find(n=>n.supports(this.encodingConfig.codec,i));if(s)this.customEncoder=new s,this.customEncoder.codec=this.encodingConfig.codec,this.customEncoder.config=i,this.customEncoder.onPacket=(n,o)=>{if(!(n instanceof ce))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?.(n,o),this.muxer.addEncodedVideoPacket(this.source._connectedTrack,n,o).catch(a=>{this.error??=a,this.errorNeedsNewStack=!1})},await this.customEncoder.init();else{if(typeof VideoEncoder>"u")throw new Error("VideoEncoder is not supported by this browser.");if(i.alpha="discard",this.encodingConfig.alpha==="keep"&&(i.latencyMode="quality"),(i.width%2===1||i.height%2===1)&&(this.encodingConfig.codec==="avc"||this.encodingConfig.codec==="hevc"))throw new Error(`The dimensions ${i.width}x${i.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(i)).supported)throw new Error(`This specific encoder configuration (${i.codec}, ${i.bitrate} bps, ${i.width}x${i.height}, hardware acceleration: ${i.hardwareAcceleration??"no-preference"}) is not supported by this browser. Consider using another codec or changing your video parameters.`);const a=[],c=[];let l=0,d=0;const u=(f,g,p)=>{const y={};if(g){const C=new Uint8Array(g.byteLength);g.copyTo(C),y.alpha=C}const T=ce.fromEncodedChunk(f,y);this.encodingConfig.onEncodedPacket?.(T,p),this.muxer.addEncodedVideoPacket(this.source._connectedTrack,T,p).catch(C=>{this.error??=C,this.errorNeedsNewStack=!1})};this.encoder=new VideoEncoder({output:(f,g)=>{if(!this.alphaEncoder){u(f,null,g);return}const p=this.alphaFrameQueue.shift();m(p!==void 0),p?(this.alphaEncoder.encode(p,{keyFrame:f.type==="key"}),d++,p.close(),a.push({chunk:f,meta:g})):d===0?u(f,null,g):(c.push(l+d),a.push({chunk:f,meta:g}))},error:f=>{f.stack=r.stack,this.error??=f}}),this.encoder.configure(i),this.encodingConfig.alpha==="keep"&&(this.alphaEncoder=new VideoEncoder({output:(f,g)=>{d--;const p=a.shift();for(m(p!==void 0),u(p.chunk,f,p.meta),l++;c.length>0&&c[0]===l;){c.shift();const y=a.shift();m(y!==void 0),u(y.chunk,null,y.meta)}},error:f=>{f.stack=r.stack,this.error??=f}}),this.alphaEncoder.configure(i))}m(this.source._connectedTrack),this.muxer=this.source._connectedTrack.output._muxer,this.encoderInitialized=!0})()}async flushAndClose(e){e||this.checkForEncoderError(),this.customEncoder?(e||this.customEncoderCallSerializer.call(()=>this.customEncoder.flush()),await this.customEncoderCallSerializer.call(()=>this.customEncoder.close())):this.encoder&&(e||(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(r=>r?.close()),this.splitter?.close()),e||this.checkForEncoderError()}getQueueSize(){return this.customEncoder?this.customEncoderQueueSize:this.encoder?.encodeQueueSize??0}checkForEncoderError(){if(this.error)throw this.errorNeedsNewStack&&(this.error.stack=new Error().stack),this.error}}class un{constructor(e,r){this.lastFrame=null,typeof OffscreenCanvas<"u"?this.canvas=new OffscreenCanvas(e,r):(this.canvas=document.createElement("canvas"),this.canvas.width=e,this.canvas.height=r);const 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)}createVertexShader(){return this.createShader(this.gl.VERTEX_SHADER,`#version 300 es
|
|
168
|
+
in vec2 a_position;
|
|
169
|
+
in vec2 a_texCoord;
|
|
170
|
+
out vec2 v_texCoord;
|
|
171
|
+
|
|
172
|
+
void main() {
|
|
173
|
+
gl_Position = vec4(a_position, 0.0, 1.0);
|
|
174
|
+
v_texCoord = a_texCoord;
|
|
175
|
+
}
|
|
176
|
+
`)}createColorProgram(){const e=this.createVertexShader(),r=this.createShader(this.gl.FRAGMENT_SHADER,`#version 300 es
|
|
177
|
+
precision highp float;
|
|
178
|
+
|
|
179
|
+
uniform sampler2D u_sourceTexture;
|
|
180
|
+
in vec2 v_texCoord;
|
|
181
|
+
out vec4 fragColor;
|
|
182
|
+
|
|
183
|
+
void main() {
|
|
184
|
+
vec4 source = texture(u_sourceTexture, v_texCoord);
|
|
185
|
+
fragColor = vec4(source.rgb, 1.0);
|
|
186
|
+
}
|
|
187
|
+
`),i=this.gl.createProgram();return this.gl.attachShader(i,e),this.gl.attachShader(i,r),this.gl.linkProgram(i),i}createAlphaProgram(){const e=this.createVertexShader(),r=this.createShader(this.gl.FRAGMENT_SHADER,`#version 300 es
|
|
188
|
+
precision highp float;
|
|
189
|
+
|
|
190
|
+
uniform sampler2D u_sourceTexture;
|
|
191
|
+
uniform vec2 u_resolution; // The width and height of the canvas
|
|
192
|
+
in vec2 v_texCoord;
|
|
193
|
+
out vec4 fragColor;
|
|
194
|
+
|
|
195
|
+
// This function determines the value for a single byte in the YUV stream
|
|
196
|
+
float getByteValue(float byteOffset) {
|
|
197
|
+
float width = u_resolution.x;
|
|
198
|
+
float height = u_resolution.y;
|
|
199
|
+
|
|
200
|
+
float yPlaneSize = width * height;
|
|
201
|
+
|
|
202
|
+
if (byteOffset < yPlaneSize) {
|
|
203
|
+
// This byte is in the luma plane. Find the corresponding pixel coordinates to sample from
|
|
204
|
+
float y = floor(byteOffset / width);
|
|
205
|
+
float x = mod(byteOffset, width);
|
|
206
|
+
|
|
207
|
+
// Add 0.5 to sample the center of the texel
|
|
208
|
+
vec2 sampleCoord = (vec2(x, y) + 0.5) / u_resolution;
|
|
209
|
+
|
|
210
|
+
// The luma value is the alpha from the source texture
|
|
211
|
+
return texture(u_sourceTexture, sampleCoord).a;
|
|
212
|
+
} else {
|
|
213
|
+
// Write a fixed value for chroma and beyond
|
|
214
|
+
return 128.0 / 255.0;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
void main() {
|
|
219
|
+
// Each fragment writes 4 bytes (R, G, B, A)
|
|
220
|
+
float pixelIndex = floor(gl_FragCoord.y) * u_resolution.x + floor(gl_FragCoord.x);
|
|
221
|
+
float baseByteOffset = pixelIndex * 4.0;
|
|
222
|
+
|
|
223
|
+
vec4 result;
|
|
224
|
+
for (int i = 0; i < 4; i++) {
|
|
225
|
+
float currentByteOffset = baseByteOffset + float(i);
|
|
226
|
+
result[i] = getByteValue(currentByteOffset);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
fragColor = result;
|
|
230
|
+
}
|
|
231
|
+
`),i=this.gl.createProgram();return this.gl.attachShader(i,e),this.gl.attachShader(i,r),this.gl.linkProgram(i),i}createShader(e,r){const i=this.gl.createShader(e);return this.gl.shaderSource(i,r),this.gl.compileShader(i),this.gl.getShaderParameter(i,this.gl.COMPILE_STATUS)||console.error("Shader compile error:",this.gl.getShaderInfoLog(i)),i}createVAO(){const e=this.gl.createVertexArray();this.gl.bindVertexArray(e);const r=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),i=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,i),this.gl.bufferData(this.gl.ARRAY_BUFFER,r,this.gl.STATIC_DRAW);const s=this.gl.getAttribLocation(this.colorProgram,"a_position"),n=this.gl.getAttribLocation(this.colorProgram,"a_texCoord");return this.gl.enableVertexAttribArray(s),this.gl.vertexAttribPointer(s,2,this.gl.FLOAT,!1,16,0),this.gl.enableVertexAttribArray(n),this.gl.vertexAttribPointer(n,2,this.gl.FLOAT,!1,16,8),e}createTexture(){const e=this.gl.createTexture();return this.gl.bindTexture(this.gl.TEXTURE_2D,e),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),e}updateTexture(e){this.lastFrame!==e&&((e.displayWidth!==this.canvas.width||e.displayHeight!==this.canvas.height)&&(this.canvas.width=e.displayWidth,this.canvas.height=e.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,e),this.lastFrame=e)}extractColor(e){return this.updateTexture(e),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:e.timestamp,duration:e.duration??void 0,alpha:"discard"})}extractAlpha(e){this.updateTexture(e),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);const{width:r,height:i}=this.canvas,s=Math.ceil(r/2)*Math.ceil(i/2),n=r*i+s*2,o=Math.ceil(n/(r*4));let a=new Uint8Array(4*r*o);this.gl.readPixels(0,0,r,o,this.gl.RGBA,this.gl.UNSIGNED_BYTE,a),a=a.subarray(0,n),m(a[r*i]===128),m(a[a.length-1]===128);const c={format:"I420",codedWidth:r,codedHeight:i,timestamp:e.timestamp,duration:e.duration??void 0,transfer:[a.buffer]};return new VideoFrame(a,c)}close(){this.gl.getExtension("WEBGL_lose_context")?.loseContext(),this.gl=null}}class hn extends _r{constructor(e){sn(e),super(e.codec),this._encoder=new dn(this,e)}add(e,r){if(!(e instanceof ie))throw new TypeError("videoSample must be a VideoSample.");return this._encoder.add(e,!1,r)}_flushAndClose(e){return this._encoder.flushAndClose(e)}}class Pr extends ht{constructor(e){if(super(),this._connectedTrack=null,!me.includes(e))throw new TypeError(`Invalid audio codec '${e}'. Must be one of: ${me.join(", ")}.`);this._codec=e}}class fn{constructor(e,r){this.source=e,this.encodingConfig=r,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 kt,this.customEncoderQueueSize=0,this.lastEndSampleIndex=null,this.error=null,this.errorNeedsNewStack=!0}async add(e,r){try{if(this.checkForEncoderError(),this.source._ensureValidAdd(),this.lastNumberOfChannels!==null&&this.lastSampleRate!==null){if(e.numberOfChannels!==this.lastNumberOfChannels||e.sampleRate!==this.lastSampleRate)throw new Error(`Audio parameters must remain constant. Expected ${this.lastNumberOfChannels} channels at ${this.lastSampleRate} Hz, got ${e.numberOfChannels} channels at ${e.sampleRate} Hz.`)}else this.lastNumberOfChannels=e.numberOfChannels,this.lastSampleRate=e.sampleRate;this.encoderInitialized||(this.ensureEncoderPromise||this.ensureEncoder(e),this.encoderInitialized||await this.ensureEncoderPromise),m(this.encoderInitialized);{const i=Math.round(e.timestamp*e.sampleRate),s=Math.round((e.timestamp+e.duration)*e.sampleRate);if(this.lastEndSampleIndex===null)this.lastEndSampleIndex=s;else{const n=i-this.lastEndSampleIndex;if(n>=64){const o=new le({data:new Float32Array(n*e.numberOfChannels),format:"f32-planar",sampleRate:e.sampleRate,numberOfChannels:e.numberOfChannels,numberOfFrames:n,timestamp:this.lastEndSampleIndex/e.sampleRate});await this.add(o,!0)}this.lastEndSampleIndex+=e.numberOfFrames}}if(this.customEncoder){this.customEncoderQueueSize++;const i=e.clone(),s=this.customEncoderCallSerializer.call(()=>this.customEncoder.encode(i)).then(()=>this.customEncoderQueueSize--).catch(n=>this.error??=n).finally(()=>{i.close()});this.customEncoderQueueSize>=4&&await s,await this.muxer.mutex.currentPromise}else if(this.isPcmEncoder)await this.doPcmEncoding(e,r);else{m(this.encoder);const i=e.toAudioData();this.encoder.encode(i),i.close(),r&&e.close(),this.encoder.encodeQueueSize>=4&&await new Promise(s=>this.encoder.addEventListener("dequeue",s,{once:!0})),await this.muxer.mutex.currentPromise}}finally{r&&e.close()}}async doPcmEncoding(e,r){m(this.outputSampleSize),m(this.writeOutputValue);const{numberOfChannels:i,numberOfFrames:s,sampleRate:n,timestamp:o}=e,a=2048,c=[];for(let f=0;f<s;f+=a){const g=Math.min(a,e.numberOfFrames-f),p=g*i*this.outputSampleSize,y=new ArrayBuffer(p),T=new DataView(y);c.push({frameCount:g,view:T})}const l=e.allocationSize({planeIndex:0,format:"f32-planar"}),d=new Float32Array(l/Float32Array.BYTES_PER_ELEMENT);for(let f=0;f<i;f++){e.copyTo(d,{planeIndex:f,format:"f32-planar"});for(let g=0;g<c.length;g++){const{frameCount:p,view:y}=c[g];for(let T=0;T<p;T++)this.writeOutputValue(y,(T*i+f)*this.outputSampleSize,d[g*a+T])}}r&&e.close();const u={decoderConfig:{codec:this.encodingConfig.codec,numberOfChannels:i,sampleRate:n}};for(let f=0;f<c.length;f++){const{frameCount:g,view:p}=c[f],y=p.buffer,T=f*a,C=new ce(new Uint8Array(y),"key",o+T/n,g/n);this.encodingConfig.onEncodedPacket?.(C,u),await this.muxer.addEncodedAudioPacket(this.source._connectedTrack,C,u)}}ensureEncoder(e){const r=new Error;this.ensureEncoderPromise=(async()=>{const{numberOfChannels:i,sampleRate:s}=e,n=ln({numberOfChannels:i,sampleRate:s,...this.encodingConfig});this.encodingConfig.onEncoderConfig?.(n);const o=_i.find(a=>a.supports(this.encodingConfig.codec,n));if(o)this.customEncoder=new o,this.customEncoder.codec=this.encodingConfig.codec,this.customEncoder.config=n,this.customEncoder.onPacket=(a,c)=>{if(!(a instanceof ce))throw new TypeError("The first argument passed to onPacket must be an EncodedPacket.");if(c!==void 0&&(!c||typeof c!="object"))throw new TypeError("The second argument passed to onPacket must be an object or undefined.");this.encodingConfig.onEncodedPacket?.(a,c),this.muxer.addEncodedAudioPacket(this.source._connectedTrack,a,c).catch(l=>{this.error??=l,this.errorNeedsNewStack=!1})},await this.customEncoder.init();else if(Q.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(n)).supported)throw new Error(`This specific encoder configuration (${n.codec}, ${n.bitrate} bps, ${n.numberOfChannels} channels, ${n.sampleRate} Hz) is not supported by this browser. Consider using another codec or changing your audio parameters.`);this.encoder=new AudioEncoder({output:(c,l)=>{if(this.encodingConfig.codec==="aac"&&l?.decoderConfig){let u=!1;if(!l.decoderConfig.description||l.decoderConfig.description.byteLength<2?u=!0:u=ii(q(l.decoderConfig.description)).objectType===0,u){const f=Number(j(n.codec.split(".")));l.decoderConfig.description=Ge({objectType:f,numberOfChannels:l.decoderConfig.numberOfChannels,sampleRate:l.decoderConfig.sampleRate})}}const d=ce.fromEncodedChunk(c);this.encodingConfig.onEncodedPacket?.(d,l),this.muxer.addEncodedAudioPacket(this.source._connectedTrack,d,l).catch(u=>{this.error??=u,this.errorNeedsNewStack=!1})},error:c=>{c.stack=r.stack,this.error??=c}}),this.encoder.configure(n)}m(this.source._connectedTrack),this.muxer=this.source._connectedTrack.output._muxer,this.encoderInitialized=!0})()}initPcmEncoder(){this.isPcmEncoder=!0;const e=this.encodingConfig.codec,{dataType:r,sampleSize:i,littleEndian:s}=pe(e);switch(this.outputSampleSize=i,i){case 1:r==="unsigned"?this.writeOutputValue=(n,o,a)=>n.setUint8(o,$((a+1)*127.5,0,255)):r==="signed"?this.writeOutputValue=(n,o,a)=>{n.setInt8(o,$(Math.round(a*128),-128,127))}:r==="ulaw"?this.writeOutputValue=(n,o,a)=>{const c=$(Math.floor(a*32767),-32768,32767);n.setUint8(o,Pi(c))}:r==="alaw"?this.writeOutputValue=(n,o,a)=>{const c=$(Math.floor(a*32767),-32768,32767);n.setUint8(o,Fi(c))}:m(!1);break;case 2:r==="unsigned"?this.writeOutputValue=(n,o,a)=>n.setUint16(o,$((a+1)*32767.5,0,65535),s):r==="signed"?this.writeOutputValue=(n,o,a)=>n.setInt16(o,$(Math.round(a*32767),-32768,32767),s):m(!1);break;case 3:r==="unsigned"?this.writeOutputValue=(n,o,a)=>Le(n,o,$((a+1)*83886075e-1,0,16777215),s):r==="signed"?this.writeOutputValue=(n,o,a)=>$r(n,o,$(Math.round(a*8388607),-8388608,8388607),s):m(!1);break;case 4:r==="unsigned"?this.writeOutputValue=(n,o,a)=>n.setUint32(o,$((a+1)*21474836475e-1,0,4294967295),s):r==="signed"?this.writeOutputValue=(n,o,a)=>n.setInt32(o,$(Math.round(a*2147483647),-2147483648,2147483647),s):r==="float"?this.writeOutputValue=(n,o,a)=>n.setFloat32(o,a,s):m(!1);break;case 8:r==="float"?this.writeOutputValue=(n,o,a)=>n.setFloat64(o,a,s):m(!1);break;default:ve(i),m(!1)}}async flushAndClose(e){e||this.checkForEncoderError(),this.customEncoder?(e||this.customEncoderCallSerializer.call(()=>this.customEncoder.flush()),await this.customEncoderCallSerializer.call(()=>this.customEncoder.close())):this.encoder&&(e||await this.encoder.flush(),this.encoder.state!=="closed"&&this.encoder.close()),e||this.checkForEncoderError()}getQueueSize(){return this.customEncoder?this.customEncoderQueueSize:this.isPcmEncoder?0:this.encoder?.encodeQueueSize??0}checkForEncoderError(){if(this.error)throw this.errorNeedsNewStack&&(this.error.stack=new Error().stack),this.error}}class mn extends Pr{constructor(e){an(e),super(e.codec),this._encoder=new fn(this,e)}add(e){if(!(e instanceof le))throw new TypeError("audioSample must be an AudioSample.");return this._encoder.add(e,!1)}_flushAndClose(e){return this._encoder.flushAndClose(e)}}class pn extends ht{constructor(e){if(super(),this._connectedTrack=null,!Se.includes(e))throw new TypeError(`Invalid subtitle codec '${e}'. Must be one of: ${Se.join(", ")}.`);this._codec=e}}/*!
|
|
232
|
+
* Copyright (c) 2026-present, Vanilagy and contributors
|
|
233
|
+
*
|
|
234
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
235
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
236
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
237
|
+
*/const gn=["video","audio","subtitle"],ft=t=>{if(!t||typeof t!="object")throw new TypeError("metadata must be an object.");if(t.languageCode!==void 0&&!qr(t.languageCode))throw new TypeError("metadata.languageCode, when provided, must be a three-letter, ISO 639-2/T language code.");if(t.name!==void 0&&typeof t.name!="string")throw new TypeError("metadata.name, when provided, must be a string.");if(t.disposition!==void 0&&Jr(t.disposition),t.maximumPacketCount!==void 0&&(!Number.isInteger(t.maximumPacketCount)||t.maximumPacketCount<0))throw new TypeError("metadata.maximumPacketCount, when provided, must be a non-negative integer.")};class wn{constructor(e){if(this.state="pending",this._tracks=[],this._startPromise=null,this._cancelPromise=null,this._finalizePromise=null,this._mutex=new Ct,this._metadataTags={},!e||typeof e!="object")throw new TypeError("options must be an object.");if(!(e.format instanceof dt))throw new TypeError("options.format must be an OutputFormat.");if(!(e.target instanceof yr))throw new TypeError("options.target must be a Target.");if(e.target._output)throw new Error("Target is already used for another output.");e.target._output=this,this.format=e.format,this.target=e.target,this._writer=e.target._createWriter(),this._muxer=e.format._createMuxer(this)}addVideoTrack(e,r={}){if(!(e instanceof _r))throw new TypeError("source must be a VideoSource.");if(ft(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.`);this._addTrack("video",e,r)}addAudioTrack(e,r={}){if(!(e instanceof Pr))throw new TypeError("source must be an AudioSource.");ft(r),this._addTrack("audio",e,r)}addSubtitleTrack(e,r={}){if(!(e instanceof pn))throw new TypeError("source must be a SubtitleSource.");ft(r),this._addTrack("subtitle",e,r)}setMetadataTags(e){if(Zr(e),this.state!=="pending")throw new Error("Cannot set metadata tags after output has been started or canceled.");this._metadataTags=e}_addTrack(e,r,i){if(this.state!=="pending")throw new Error("Cannot add track after output has been started or canceled.");if(r._connectedTrack)throw new Error("Source is already used for a track.");const s=this.format.getSupportedTrackCounts(),n=this._tracks.reduce((l,d)=>l+(d.type===e?1:0),0),o=s[e].max;if(n===o)throw new Error(o===0?`${this.format._name} does not support ${e} tracks.`:`${this.format._name} does not support more than ${o} ${e} track${o===1?"":"s"}.`);const a=s.total.max;if(this._tracks.length===a)throw new Error(`${this.format._name} does not support more than ${a} tracks${a===1?"":"s"} in total.`);const c={id:this._tracks.length+1,output:this,type:e,source:r,metadata:i};if(c.type==="video"){const l=this.format.getSupportedVideoCodecs();if(l.length===0)throw new Error(`${this.format._name} does not support video tracks.`+this.format._codecUnsupportedHint(c.source._codec));if(!l.includes(c.source._codec))throw new Error(`Codec '${c.source._codec}' cannot be contained within ${this.format._name}. Supported video codecs are: ${l.map(d=>`'${d}'`).join(", ")}.`+this.format._codecUnsupportedHint(c.source._codec))}else if(c.type==="audio"){const l=this.format.getSupportedAudioCodecs();if(l.length===0)throw new Error(`${this.format._name} does not support audio tracks.`+this.format._codecUnsupportedHint(c.source._codec));if(!l.includes(c.source._codec))throw new Error(`Codec '${c.source._codec}' cannot be contained within ${this.format._name}. Supported audio codecs are: ${l.map(d=>`'${d}'`).join(", ")}.`+this.format._codecUnsupportedHint(c.source._codec))}else if(c.type==="subtitle"){const l=this.format.getSupportedSubtitleCodecs();if(l.length===0)throw new Error(`${this.format._name} does not support subtitle tracks.`+this.format._codecUnsupportedHint(c.source._codec));if(!l.includes(c.source._codec))throw new Error(`Codec '${c.source._codec}' cannot be contained within ${this.format._name}. Supported subtitle codecs are: ${l.map(d=>`'${d}'`).join(", ")}.`+this.format._codecUnsupportedHint(c.source._codec))}this._tracks.push(c),r._connectedTrack=c}async start(){const e=this.format.getSupportedTrackCounts();for(const i of gn){const s=this._tracks.reduce((o,a)=>o+(a.type===i?1:0),0),n=e[i].min;if(s<n)throw new Error(n===e[i].max?`${this.format._name} requires exactly ${n} ${i} track${n===1?"":"s"}.`:`${this.format._name} requires at least ${n} ${i} track${n===1?"":"s"}.`)}const 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",this._writer.start();const i=await this._mutex.acquire();await this._muxer.start();const s=this._tracks.map(n=>n.source._start());await Promise.all(s),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"){console.warn("Output has already been finalized.");return}return this._cancelPromise=(async()=>{this.state="canceled";const e=await this._mutex.acquire(),r=this._tracks.map(i=>i.source._flushOrWaitForOngoingClose(!0));await Promise.all(r),await this._writer.close(),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";const e=await this._mutex.acquire(),r=this._tracks.map(i=>i.source._flushOrWaitForOngoingClose(!1));await Promise.all(r),await this._muxer.finalize(),await this._writer.flush(),await this._writer.finalize(),this.state="finalized",e()})()}}class Fr{constructor(e,r){this.controller=e,this.iframe=r}async export(e){const{onProgress:r,signal:i,mode:s="auto",canvasSelector:n="canvas",format:o="mp4",includeCaptions:a=!0}=e;console.log(`Client-side rendering started! Format: ${o}`),this.controller.pause();try{const c=this.controller.getState();let l=0,d=c.duration*c.fps;c.playbackRange&&c.playbackRange.length===2&&(l=c.playbackRange[0],d=c.playbackRange[1]-l);let u=s;if(u==="auto"){const P=await this.controller.captureFrame(l,{selector:n,mode:"canvas"});P&&P.frame?(u="canvas",P.frame.close()):(u="dom",console.log("Canvas not found for auto export, falling back to DOM mode."))}const f=await this.controller.captureFrame(l,{selector:n,mode:u});if(!f||!f.frame)throw new Error(`Failed to capture first frame in mode: ${u}`);const{frame:g,captions:p}=f,y=g.displayWidth,T=g.displayHeight,C=new Tr,A=o==="webm"?new ut:new kr,_=new wn({format:A,target:C}),F={codec:o==="webm"?"vp9":"avc",bitrate:5e6},M=new hn(F);_.addVideoTrack(M);let L=null,W=[];try{if(W=await this.controller.getAudioTracks(),W&&W.length>0){const P=o==="webm"?{codec:"opus"}:{codec:"aac"};L=new mn(P),_.addAudioTrack(L)}}catch(P){console.warn("Failed to setup audio:",P)}await _.start();let I=g;a&&p&&p.length>0&&(I=await this.drawCaptions(g,p),g.close()),await M.add(new ie(I),{keyFrame:!0}),I.close(),r(1/d);for(let P=1;P<d;P++){if(i.aborted)throw new Error("Export aborted");const V=l+P,x=await this.controller.captureFrame(V,{selector:n,mode:u});if(!x||!x.frame)throw new Error(`Frame ${V} missing during export.`);const{frame:O,captions:N}=x,U=P%(c.fps*2)===0;let B=O;a&&N&&N.length>0&&(B=await this.drawCaptions(O,N),O.close()),await M.add(new ie(B),{keyFrame:U}),B.close(),r((P+1)/d)}if(L&&W.length>0){const P=d/c.fps,V=l/c.fps,x=await zr(W,P,48e3,V),O=x.getChannelData(0),N=x.getChannelData(1),U=new Float32Array(O.length+N.length);U.set(O,0),U.set(N,O.length);const B=new le({format:"f32-planar",sampleRate:48e3,numberOfChannels:2,timestamp:0,data:U});await L.add(B)}if(await _.finalize(),C.buffer)this.download(C.buffer,o),console.log("Client-side rendering and download finished!");else throw new Error("Export failed: Output buffer is empty")}catch(c){if(c.message==="Export aborted"){console.log("Export aborted by user.");return}throw console.error("Client-side rendering failed:",c),c}}async drawCaptions(e,r){const i=e.displayWidth,s=e.displayHeight;let n,o;if(typeof OffscreenCanvas<"u"?(n=new OffscreenCanvas(i,s),o=n.getContext("2d")):(n=document.createElement("canvas"),n.width=i,n.height=s,o=n.getContext("2d")),!o)throw new Error("Failed to create canvas context for captions");o.drawImage(e,0,0),o.save();const a=Math.max(16,Math.round(s*.05)),c=a*.5,l=a*1.2,d=s*.05;o.font=`${a}px sans-serif`,o.textAlign="center",o.textBaseline="top";let u=s-d;return[...r].reverse().forEach(g=>{const p=g.text.split(`
|
|
238
|
+
`),y=p.length*l+c*2;let T=0;p.forEach(_=>{const F=o.measureText(_);F.width>T&&(T=F.width)});const C=T+a*1,A=u-y;o.shadowColor="transparent",o.fillStyle="rgba(0, 0, 0, 0.7)",o.fillRect(i/2-C/2,A,C,y),o.shadowColor="black",o.shadowBlur=2,o.shadowOffsetY=1,o.fillStyle="white",p.forEach((_,F)=>{const M=A+c+F*l;o.fillText(_,i/2,M)}),u-=y+4}),o.restore(),new VideoFrame(n,{timestamp:e.timestamp})}download(e,r){const i=r==="webm"?"video/webm":"video/mp4",s=new Blob([e],{type:i}),n=URL.createObjectURL(s),o=document.createElement("a");o.href=n,o.download=`video.${r}`,o.click(),URL.revokeObjectURL(n)}saveCaptionsAsSRT(e,r){const i=Dr(e),s=new Blob([i],{type:"text/srt"}),n=URL.createObjectURL(s),o=document.createElement("a");o.href=n,o.download=r,o.click(),URL.revokeObjectURL(n)}}const bn=typeof window<"u"&&window.VTTCue||null;class yn{startTime;endTime;text;constructor(e,r,i){this.startTime=e,this.endTime=r,this.text=i}}const Tn=bn||yn;class Cn extends EventTarget{_mode="disabled";_cues=[];_kind;_label;_language;_id;_host;constructor(e,r,i,s){super(),this._kind=e,this._label=r,this._language=i,this._id=Math.random().toString(36).substring(2)+Date.now().toString(36),this._host=s}get kind(){return this._kind}get label(){return this._label}get language(){return this._language}get id(){return this._id}get cues(){return this._cues}get mode(){return this._mode}set mode(e){this._mode!==e&&(this._mode=e,this._host.handleTrackModeChange(this))}addCue(e){this._cues.push(e),this._cues.sort((r,i)=>r.startTime-i.startTime),this._mode==="showing"&&this._host.handleTrackModeChange(this)}removeCue(e){const r=this._cues.indexOf(e);r!==-1&&(this._cues.splice(r,1),this._mode==="showing"&&this._host.handleTrackModeChange(this))}}class vn extends EventTarget{tracks=[];get length(){return this.tracks.length}[Symbol.iterator](){return this.tracks[Symbol.iterator]()}addTrack(e){this.tracks.push(e),this[this.tracks.length-1]=e;const r=new Event("addtrack");r.track=e,this.dispatchEvent(r)}getTrackById(e){return this.tracks.find(r=>r.id===e)||null}removeTrack(e){const r=this.tracks.indexOf(e);if(r!==-1){this.tracks.splice(r,1);for(let s=r;s<this.tracks.length;s++)this[s]=this.tracks[s];delete this[this.tracks.length];const i=new Event("removetrack");i.track=e,this.dispatchEvent(i)}}set onaddtrack(e){e&&this.addEventListener("addtrack",e)}}class mt{constructor(e,r){this.startVal=e,this.endVal=r}get length(){return this.endVal>0?1:0}start(e){if(e!==0||this.length===0)throw new Error("IndexSizeError");return this.startVal}end(e){if(e!==0||this.length===0)throw new Error("IndexSizeError");return this.endVal}}const Br=document.createElement("template");Br.innerHTML=`
|
|
239
|
+
<style>
|
|
240
|
+
:host {
|
|
241
|
+
display: block;
|
|
242
|
+
width: 100%;
|
|
243
|
+
aspect-ratio: 16 / 9;
|
|
244
|
+
background-color: #f0f0f0;
|
|
245
|
+
position: relative;
|
|
246
|
+
font-family: var(--helios-font-family, sans-serif);
|
|
247
|
+
|
|
248
|
+
/* CSS Variables for Theming */
|
|
249
|
+
--helios-controls-bg: rgba(0, 0, 0, 0.6);
|
|
250
|
+
--helios-text-color: white;
|
|
251
|
+
--helios-accent-color: #007bff;
|
|
252
|
+
--helios-range-track-color: #555;
|
|
253
|
+
--helios-range-selected-color: rgba(255, 255, 255, 0.2);
|
|
254
|
+
--helios-range-unselected-color: var(--helios-range-track-color);
|
|
255
|
+
--helios-font-family: sans-serif;
|
|
256
|
+
}
|
|
257
|
+
iframe {
|
|
258
|
+
width: 100%;
|
|
259
|
+
height: 100%;
|
|
260
|
+
border: none;
|
|
261
|
+
}
|
|
262
|
+
.controls {
|
|
263
|
+
position: absolute;
|
|
264
|
+
bottom: 0;
|
|
265
|
+
left: 0;
|
|
266
|
+
right: 0;
|
|
267
|
+
background: var(--helios-controls-bg);
|
|
268
|
+
display: flex;
|
|
269
|
+
align-items: center;
|
|
270
|
+
padding: 8px;
|
|
271
|
+
color: var(--helios-text-color);
|
|
272
|
+
transition: opacity 0.3s;
|
|
273
|
+
z-index: 2;
|
|
274
|
+
}
|
|
275
|
+
:host(:not([controls])) .controls {
|
|
276
|
+
display: none;
|
|
277
|
+
pointer-events: none;
|
|
278
|
+
}
|
|
279
|
+
.play-pause-btn {
|
|
280
|
+
background: none;
|
|
281
|
+
border: none;
|
|
282
|
+
color: var(--helios-text-color);
|
|
283
|
+
font-size: 24px;
|
|
284
|
+
cursor: pointer;
|
|
285
|
+
width: 40px;
|
|
286
|
+
height: 40px;
|
|
287
|
+
}
|
|
288
|
+
.volume-control {
|
|
289
|
+
display: flex;
|
|
290
|
+
align-items: center;
|
|
291
|
+
margin-right: 8px;
|
|
292
|
+
}
|
|
293
|
+
.volume-btn {
|
|
294
|
+
background: none;
|
|
295
|
+
border: none;
|
|
296
|
+
color: var(--helios-text-color);
|
|
297
|
+
font-size: 20px;
|
|
298
|
+
cursor: pointer;
|
|
299
|
+
width: 32px;
|
|
300
|
+
height: 32px;
|
|
301
|
+
display: flex;
|
|
302
|
+
align-items: center;
|
|
303
|
+
justify-content: center;
|
|
304
|
+
}
|
|
305
|
+
.volume-slider {
|
|
306
|
+
width: 60px;
|
|
307
|
+
margin-left: 4px;
|
|
308
|
+
height: 4px;
|
|
309
|
+
-webkit-appearance: none;
|
|
310
|
+
background: var(--helios-range-track-color);
|
|
311
|
+
outline: none;
|
|
312
|
+
border-radius: 2px;
|
|
313
|
+
}
|
|
314
|
+
.volume-slider::-webkit-slider-thumb {
|
|
315
|
+
-webkit-appearance: none;
|
|
316
|
+
appearance: none;
|
|
317
|
+
width: 12px;
|
|
318
|
+
height: 12px;
|
|
319
|
+
background: var(--helios-text-color);
|
|
320
|
+
cursor: pointer;
|
|
321
|
+
border-radius: 50%;
|
|
322
|
+
}
|
|
323
|
+
.export-btn {
|
|
324
|
+
background-color: var(--helios-accent-color);
|
|
325
|
+
border: none;
|
|
326
|
+
color: var(--helios-text-color);
|
|
327
|
+
font-size: 14px;
|
|
328
|
+
font-weight: bold;
|
|
329
|
+
cursor: pointer;
|
|
330
|
+
padding: 6px 12px;
|
|
331
|
+
margin: 0 10px;
|
|
332
|
+
border-radius: 4px;
|
|
333
|
+
}
|
|
334
|
+
.export-btn:hover {
|
|
335
|
+
filter: brightness(0.9);
|
|
336
|
+
}
|
|
337
|
+
.export-btn:disabled {
|
|
338
|
+
background-color: #666;
|
|
339
|
+
cursor: not-allowed;
|
|
340
|
+
}
|
|
341
|
+
.scrubber-wrapper {
|
|
342
|
+
flex-grow: 1;
|
|
343
|
+
margin: 0 16px;
|
|
344
|
+
position: relative;
|
|
345
|
+
height: 8px;
|
|
346
|
+
display: flex;
|
|
347
|
+
align-items: center;
|
|
348
|
+
}
|
|
349
|
+
.scrubber {
|
|
350
|
+
width: 100%;
|
|
351
|
+
height: 100%;
|
|
352
|
+
margin: 0;
|
|
353
|
+
position: relative;
|
|
354
|
+
z-index: 1;
|
|
355
|
+
-webkit-appearance: none;
|
|
356
|
+
background: var(--helios-range-track-color);
|
|
357
|
+
outline: none;
|
|
358
|
+
opacity: 0.9;
|
|
359
|
+
transition: opacity .2s;
|
|
360
|
+
}
|
|
361
|
+
.scrubber::-webkit-slider-thumb {
|
|
362
|
+
-webkit-appearance: none;
|
|
363
|
+
appearance: none;
|
|
364
|
+
width: 16px;
|
|
365
|
+
height: 16px;
|
|
366
|
+
background: var(--helios-accent-color);
|
|
367
|
+
cursor: pointer;
|
|
368
|
+
border-radius: 50%;
|
|
369
|
+
}
|
|
370
|
+
.scrubber-tooltip {
|
|
371
|
+
position: absolute;
|
|
372
|
+
bottom: 100%;
|
|
373
|
+
transform: translateX(-50%);
|
|
374
|
+
background: rgba(0, 0, 0, 0.8);
|
|
375
|
+
color: white;
|
|
376
|
+
padding: 4px 8px;
|
|
377
|
+
border-radius: 4px;
|
|
378
|
+
font-size: 12px;
|
|
379
|
+
pointer-events: none;
|
|
380
|
+
white-space: nowrap;
|
|
381
|
+
z-index: 10;
|
|
382
|
+
margin-bottom: 8px;
|
|
383
|
+
}
|
|
384
|
+
.scrubber-tooltip.hidden {
|
|
385
|
+
display: none;
|
|
386
|
+
}
|
|
387
|
+
.markers-container {
|
|
388
|
+
position: absolute;
|
|
389
|
+
inset: 0;
|
|
390
|
+
pointer-events: none;
|
|
391
|
+
z-index: 2;
|
|
392
|
+
}
|
|
393
|
+
.marker {
|
|
394
|
+
position: absolute;
|
|
395
|
+
width: 4px;
|
|
396
|
+
height: 12px;
|
|
397
|
+
background-color: var(--helios-accent-color);
|
|
398
|
+
transform: translateX(-50%);
|
|
399
|
+
cursor: pointer;
|
|
400
|
+
pointer-events: auto;
|
|
401
|
+
border-radius: 2px;
|
|
402
|
+
top: -2px;
|
|
403
|
+
transition: transform 0.1s;
|
|
404
|
+
}
|
|
405
|
+
.marker:hover {
|
|
406
|
+
transform: translateX(-50%) scale(1.2);
|
|
407
|
+
z-index: 10;
|
|
408
|
+
}
|
|
409
|
+
.time-display {
|
|
410
|
+
min-width: 90px;
|
|
411
|
+
text-align: center;
|
|
412
|
+
}
|
|
413
|
+
.status-overlay {
|
|
414
|
+
position: absolute;
|
|
415
|
+
inset: 0;
|
|
416
|
+
background: rgba(0, 0, 0, 0.8);
|
|
417
|
+
backdrop-filter: blur(4px);
|
|
418
|
+
color: white;
|
|
419
|
+
display: flex;
|
|
420
|
+
flex-direction: column;
|
|
421
|
+
align-items: center;
|
|
422
|
+
justify-content: center;
|
|
423
|
+
z-index: 10;
|
|
424
|
+
transition: opacity 0.3s;
|
|
425
|
+
}
|
|
426
|
+
.status-overlay.hidden {
|
|
427
|
+
opacity: 0;
|
|
428
|
+
pointer-events: none;
|
|
429
|
+
}
|
|
430
|
+
.error-msg {
|
|
431
|
+
color: #ff6b6b;
|
|
432
|
+
margin-bottom: 10px;
|
|
433
|
+
font-size: 16px;
|
|
434
|
+
font-weight: bold;
|
|
435
|
+
}
|
|
436
|
+
.retry-btn {
|
|
437
|
+
background-color: #ff6b6b;
|
|
438
|
+
border: none;
|
|
439
|
+
color: white;
|
|
440
|
+
padding: 8px 16px;
|
|
441
|
+
border-radius: 4px;
|
|
442
|
+
cursor: pointer;
|
|
443
|
+
font-size: 14px;
|
|
444
|
+
margin-top: 10px;
|
|
445
|
+
}
|
|
446
|
+
.retry-btn:hover {
|
|
447
|
+
background-color: #ff5252;
|
|
448
|
+
}
|
|
449
|
+
.speed-selector {
|
|
450
|
+
background: rgba(0, 0, 0, 0.4);
|
|
451
|
+
color: var(--helios-text-color);
|
|
452
|
+
border: 1px solid var(--helios-range-track-color);
|
|
453
|
+
border-radius: 4px;
|
|
454
|
+
padding: 4px 8px;
|
|
455
|
+
margin-left: 8px;
|
|
456
|
+
font-size: 12px;
|
|
457
|
+
cursor: pointer;
|
|
458
|
+
}
|
|
459
|
+
.speed-selector:hover {
|
|
460
|
+
background: rgba(0, 0, 0, 0.6);
|
|
461
|
+
}
|
|
462
|
+
.speed-selector:focus {
|
|
463
|
+
outline: none;
|
|
464
|
+
border-color: var(--helios-accent-color);
|
|
465
|
+
}
|
|
466
|
+
.fullscreen-btn {
|
|
467
|
+
background: none;
|
|
468
|
+
border: none;
|
|
469
|
+
color: var(--helios-text-color);
|
|
470
|
+
font-size: 20px;
|
|
471
|
+
cursor: pointer;
|
|
472
|
+
width: 40px;
|
|
473
|
+
height: 40px;
|
|
474
|
+
margin-left: 8px;
|
|
475
|
+
}
|
|
476
|
+
.fullscreen-btn:hover {
|
|
477
|
+
color: var(--helios-accent-color);
|
|
478
|
+
}
|
|
479
|
+
.captions-container {
|
|
480
|
+
position: absolute;
|
|
481
|
+
bottom: 60px;
|
|
482
|
+
left: 50%;
|
|
483
|
+
transform: translateX(-50%);
|
|
484
|
+
width: 80%;
|
|
485
|
+
text-align: center;
|
|
486
|
+
pointer-events: none;
|
|
487
|
+
display: flex;
|
|
488
|
+
flex-direction: column;
|
|
489
|
+
align-items: center;
|
|
490
|
+
gap: 4px;
|
|
491
|
+
z-index: 5;
|
|
492
|
+
}
|
|
493
|
+
.caption-cue {
|
|
494
|
+
background: rgba(0, 0, 0, 0.7);
|
|
495
|
+
color: white;
|
|
496
|
+
padding: 4px 8px;
|
|
497
|
+
border-radius: 4px;
|
|
498
|
+
font-size: 16px;
|
|
499
|
+
text-shadow: 0 1px 2px black;
|
|
500
|
+
white-space: pre-wrap;
|
|
501
|
+
}
|
|
502
|
+
.cc-btn {
|
|
503
|
+
background: none;
|
|
504
|
+
border: none;
|
|
505
|
+
color: var(--helios-text-color);
|
|
506
|
+
font-size: 14px;
|
|
507
|
+
font-weight: bold;
|
|
508
|
+
cursor: pointer;
|
|
509
|
+
width: 32px;
|
|
510
|
+
height: 32px;
|
|
511
|
+
display: flex;
|
|
512
|
+
align-items: center;
|
|
513
|
+
justify-content: center;
|
|
514
|
+
margin-left: 4px;
|
|
515
|
+
opacity: 0.7;
|
|
516
|
+
}
|
|
517
|
+
.cc-btn:hover {
|
|
518
|
+
opacity: 1;
|
|
519
|
+
}
|
|
520
|
+
.cc-btn.active {
|
|
521
|
+
opacity: 1;
|
|
522
|
+
color: var(--helios-accent-color);
|
|
523
|
+
border-bottom: 2px solid var(--helios-accent-color);
|
|
524
|
+
}
|
|
525
|
+
.poster-container {
|
|
526
|
+
position: absolute;
|
|
527
|
+
inset: 0;
|
|
528
|
+
background-color: black;
|
|
529
|
+
z-index: 5;
|
|
530
|
+
display: flex;
|
|
531
|
+
align-items: center;
|
|
532
|
+
justify-content: center;
|
|
533
|
+
cursor: pointer;
|
|
534
|
+
transition: opacity 0.3s;
|
|
535
|
+
}
|
|
536
|
+
.poster-container.hidden {
|
|
537
|
+
opacity: 0;
|
|
538
|
+
pointer-events: none;
|
|
539
|
+
}
|
|
540
|
+
.poster-image {
|
|
541
|
+
position: absolute;
|
|
542
|
+
inset: 0;
|
|
543
|
+
width: 100%;
|
|
544
|
+
height: 100%;
|
|
545
|
+
object-fit: cover;
|
|
546
|
+
opacity: 0.6;
|
|
547
|
+
}
|
|
548
|
+
.big-play-btn {
|
|
549
|
+
position: relative;
|
|
550
|
+
z-index: 10;
|
|
551
|
+
background: rgba(0, 0, 0, 0.7);
|
|
552
|
+
border: 2px solid white;
|
|
553
|
+
border-radius: 50%;
|
|
554
|
+
width: 80px;
|
|
555
|
+
height: 80px;
|
|
556
|
+
color: white;
|
|
557
|
+
font-size: 40px;
|
|
558
|
+
display: flex;
|
|
559
|
+
align-items: center;
|
|
560
|
+
justify-content: center;
|
|
561
|
+
cursor: pointer;
|
|
562
|
+
transition: transform 0.2s;
|
|
563
|
+
}
|
|
564
|
+
.big-play-btn:hover {
|
|
565
|
+
transform: scale(1.1);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
.click-layer {
|
|
569
|
+
position: absolute;
|
|
570
|
+
inset: 0;
|
|
571
|
+
z-index: 1;
|
|
572
|
+
background: transparent;
|
|
573
|
+
}
|
|
574
|
+
:host([interactive]) .click-layer {
|
|
575
|
+
pointer-events: none;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/* Responsive Layouts */
|
|
579
|
+
.controls.layout-compact .volume-slider {
|
|
580
|
+
display: none;
|
|
581
|
+
}
|
|
582
|
+
.controls.layout-tiny .volume-slider {
|
|
583
|
+
display: none;
|
|
584
|
+
}
|
|
585
|
+
.controls.layout-tiny .speed-selector {
|
|
586
|
+
display: none;
|
|
587
|
+
}
|
|
588
|
+
slot {
|
|
589
|
+
display: none;
|
|
590
|
+
}
|
|
591
|
+
</style>
|
|
592
|
+
<slot></slot>
|
|
593
|
+
<div class="status-overlay hidden" part="overlay">
|
|
594
|
+
<div class="status-text">Connecting...</div>
|
|
595
|
+
<button class="retry-btn" style="display: none">Retry</button>
|
|
596
|
+
</div>
|
|
597
|
+
<div class="poster-container hidden" part="poster">
|
|
598
|
+
<img class="poster-image" alt="Video poster" />
|
|
599
|
+
<div class="big-play-btn" aria-label="Play video">▶</div>
|
|
600
|
+
</div>
|
|
601
|
+
<iframe part="iframe" sandbox="allow-scripts allow-same-origin" title="Helios Composition Preview"></iframe>
|
|
602
|
+
<div class="click-layer" part="click-layer"></div>
|
|
603
|
+
<div class="captions-container" part="captions"></div>
|
|
604
|
+
<div class="controls" role="toolbar" aria-label="Playback Controls">
|
|
605
|
+
<button class="play-pause-btn" part="play-pause-button" aria-label="Play">▶</button>
|
|
606
|
+
<div class="volume-control">
|
|
607
|
+
<button class="volume-btn" part="volume-button" aria-label="Mute">🔊</button>
|
|
608
|
+
<input type="range" class="volume-slider" min="0" max="1" step="0.05" value="1" part="volume-slider" aria-label="Volume">
|
|
609
|
+
</div>
|
|
610
|
+
<button class="cc-btn" part="cc-button" aria-label="Toggle Captions">CC</button>
|
|
611
|
+
<button class="export-btn" part="export-button" aria-label="Export video">Export</button>
|
|
612
|
+
<select class="speed-selector" part="speed-selector" aria-label="Playback speed">
|
|
613
|
+
<option value="0.25">0.25x</option>
|
|
614
|
+
<option value="0.5">0.5x</option>
|
|
615
|
+
<option value="1" selected>1x</option>
|
|
616
|
+
<option value="2">2x</option>
|
|
617
|
+
</select>
|
|
618
|
+
<div class="scrubber-wrapper">
|
|
619
|
+
<div class="scrubber-tooltip hidden" part="tooltip"></div>
|
|
620
|
+
<div class="markers-container" part="markers"></div>
|
|
621
|
+
<input type="range" class="scrubber" min="0" value="0" step="1" part="scrubber" aria-label="Seek time">
|
|
622
|
+
</div>
|
|
623
|
+
<div class="time-display" part="time-display">0.00 / 0.00</div>
|
|
624
|
+
<button class="fullscreen-btn" part="fullscreen-button" aria-label="Toggle fullscreen">⛶</button>
|
|
625
|
+
</div>
|
|
626
|
+
`;class ne extends HTMLElement{iframe;_textTracks;_domTracks=new Map;playPauseBtn;volumeBtn;volumeSlider;scrubber;scrubberWrapper;scrubberTooltip;markersContainer;timeDisplay;exportBtn;overlay;statusText;retryBtn;retryAction;speedSelector;fullscreenBtn;captionsContainer;ccBtn;showCaptions=!1;clickLayer;posterContainer;posterImage;bigPlayBtn;pendingSrc=null;isLoaded=!1;resizeObserver;controller=null;directHelios=null;unsubscribe=null;connectionInterval=null;abortController=null;isExporting=!1;isScrubbing=!1;wasPlayingBeforeScrub=!1;lastState=null;pendingProps=null;_error=null;static HAVE_NOTHING=0;static HAVE_METADATA=1;static HAVE_CURRENT_DATA=2;static HAVE_FUTURE_DATA=3;static HAVE_ENOUGH_DATA=4;static NETWORK_EMPTY=0;static NETWORK_IDLE=1;static NETWORK_LOADING=2;static NETWORK_NO_SOURCE=3;_readyState=ne.HAVE_NOTHING;_networkState=ne.NETWORK_EMPTY;get readyState(){return this._readyState}get networkState(){return this._networkState}get error(){return this._error}get currentSrc(){return this.src}canPlayType(e){return""}get defaultMuted(){return this.hasAttribute("muted")}set defaultMuted(e){e?this.setAttribute("muted",""):this.removeAttribute("muted")}_defaultPlaybackRate=1;get defaultPlaybackRate(){return this._defaultPlaybackRate}set defaultPlaybackRate(e){this._defaultPlaybackRate!==e&&(this._defaultPlaybackRate=e,this.dispatchEvent(new Event("ratechange")))}_preservesPitch=!0;get preservesPitch(){return this._preservesPitch}set preservesPitch(e){this._preservesPitch=e}get srcObject(){return null}set srcObject(e){console.warn("HeliosPlayer does not support srcObject")}get crossOrigin(){return this.getAttribute("crossorigin")}set crossOrigin(e){e!==null?this.setAttribute("crossorigin",e):this.removeAttribute("crossorigin")}get seeking(){return this.isScrubbing}get buffered(){return new mt(0,this.duration)}get seekable(){return new mt(0,this.duration)}get played(){return new mt(0,this.duration)}get videoWidth(){if(this.controller){const e=this.controller.getState();if(e.width)return e.width}return parseFloat(this.getAttribute("width")||"0")}get videoHeight(){if(this.controller){const e=this.controller.getState();if(e.height)return e.height}return parseFloat(this.getAttribute("height")||"0")}get currentTime(){if(!this.controller)return 0;const e=this.controller.getState();return e.fps?e.currentFrame/e.fps:0}set currentTime(e){if(this.controller){const r=this.controller.getState();r.fps&&(this.dispatchEvent(new Event("seeking")),this.controller.seek(Math.floor(e*r.fps)),this.dispatchEvent(new Event("seeked")))}}get currentFrame(){return this.controller?this.controller.getState().currentFrame:0}set currentFrame(e){this.controller&&(this.dispatchEvent(new Event("seeking")),this.controller.seek(Math.floor(e)),this.dispatchEvent(new Event("seeked")))}get duration(){return this.controller?this.controller.getState().duration:0}get paused(){return this.controller?!this.controller.getState().isPlaying:!0}get ended(){if(!this.controller)return!1;const e=this.controller.getState();return e.currentFrame>=e.duration*e.fps-1}get volume(){return this.controller?this.controller.getState().volume??1:1}set volume(e){this.controller&&this.controller.setAudioVolume(Math.max(0,Math.min(1,e)))}get muted(){return this.controller?!!this.controller.getState().muted:!1}set muted(e){this.controller&&this.controller.setAudioMuted(e)}get interactive(){return this.hasAttribute("interactive")}set interactive(e){e?this.setAttribute("interactive",""):this.removeAttribute("interactive")}get playbackRate(){return this.controller?this.controller.getState().playbackRate??1:1}set playbackRate(e){this.controller&&this.controller.setPlaybackRate(e)}get fps(){return this.controller?this.controller.getState().fps:0}get src(){return this.getAttribute("src")||""}set src(e){this.setAttribute("src",e)}get autoplay(){return this.hasAttribute("autoplay")}set autoplay(e){e?this.setAttribute("autoplay",""):this.removeAttribute("autoplay")}get loop(){return this.hasAttribute("loop")}set loop(e){e?this.setAttribute("loop",""):this.removeAttribute("loop")}get controls(){return this.hasAttribute("controls")}set controls(e){e?this.setAttribute("controls",""):this.removeAttribute("controls")}get poster(){return this.getAttribute("poster")||""}set poster(e){this.setAttribute("poster",e)}get preload(){return this.getAttribute("preload")||"auto"}set preload(e){this.setAttribute("preload",e)}get sandbox(){return this.getAttribute("sandbox")||"allow-scripts allow-same-origin"}set sandbox(e){this.setAttribute("sandbox",e)}async play(){this.isLoaded?this.controller&&this.controller.play():(this.setAttribute("autoplay",""),this.load())}load(){if(this.pendingSrc){const e=this.pendingSrc;this.pendingSrc=null,this.loadIframe(e)}else{const e=this.getAttribute("src");e&&this.loadIframe(e)}}pause(){this.controller&&this.controller.pause()}static get observedAttributes(){return["src","width","height","autoplay","loop","controls","export-format","input-props","poster","muted","interactive","preload","controlslist","sandbox","export-caption-mode"]}constructor(){super(),this.attachShadow({mode:"open"}),this.shadowRoot.appendChild(Br.content.cloneNode(!0)),this.iframe=this.shadowRoot.querySelector("iframe"),this.playPauseBtn=this.shadowRoot.querySelector(".play-pause-btn"),this.volumeBtn=this.shadowRoot.querySelector(".volume-btn"),this.volumeSlider=this.shadowRoot.querySelector(".volume-slider"),this.scrubber=this.shadowRoot.querySelector(".scrubber"),this.scrubberWrapper=this.shadowRoot.querySelector(".scrubber-wrapper"),this.scrubberTooltip=this.shadowRoot.querySelector(".scrubber-tooltip"),this.markersContainer=this.shadowRoot.querySelector(".markers-container"),this.timeDisplay=this.shadowRoot.querySelector(".time-display"),this.exportBtn=this.shadowRoot.querySelector(".export-btn"),this.overlay=this.shadowRoot.querySelector(".status-overlay"),this.statusText=this.shadowRoot.querySelector(".status-text"),this.retryBtn=this.shadowRoot.querySelector(".retry-btn"),this.speedSelector=this.shadowRoot.querySelector(".speed-selector"),this.fullscreenBtn=this.shadowRoot.querySelector(".fullscreen-btn"),this.captionsContainer=this.shadowRoot.querySelector(".captions-container"),this.ccBtn=this.shadowRoot.querySelector(".cc-btn"),this.clickLayer=this.shadowRoot.querySelector(".click-layer"),this.posterContainer=this.shadowRoot.querySelector(".poster-container"),this.posterImage=this.shadowRoot.querySelector(".poster-image"),this.bigPlayBtn=this.shadowRoot.querySelector(".big-play-btn"),this.retryAction=()=>this.retryConnection(),this.retryBtn.onclick=()=>this.retryAction(),this.clickLayer.addEventListener("click",()=>{this.focus(),this.togglePlayPause()}),this.clickLayer.addEventListener("dblclick",()=>this.toggleFullscreen()),this._textTracks=new vn,this.resizeObserver=new ResizeObserver(e=>{for(const r of e){const i=r.contentRect.width,s=this.shadowRoot.querySelector(".controls");s&&(s.classList.toggle("layout-compact",i<500),s.classList.toggle("layout-tiny",i<350))}})}get textTracks(){return this._textTracks}addTextTrack(e,r="",i=""){const s=new Cn(e,r,i,this);return this._textTracks.addTrack(s),s}handleTrackModeChange(e){if(this.controller)if(e.mode==="showing"){if(e.kind==="captions")for(const i of this._textTracks)i!==e&&i.kind==="captions"&&i.mode==="showing"&&(i.mode="hidden");const r=e.cues.map((i,s)=>({id:i.id||String(s+1),startTime:i.startTime*1e3,endTime:i.endTime*1e3,text:i.text}));this.controller.setCaptions(r)}else{const r=Array.from(this._textTracks).find(i=>i.mode==="showing"&&i.kind==="captions");if(r){const i=r.cues.map((s,n)=>({id:s.id||String(n+1),startTime:s.startTime*1e3,endTime:s.endTime*1e3,text:s.text}));this.controller.setCaptions(i)}else this.controller.setCaptions([])}}attributeChangedCallback(e,r,i){if(r!==i){if(e==="poster"&&(this.posterImage.src=i,this.updatePosterVisibility()),e==="src"&&((this.getAttribute("preload")||"auto")==="none"&&!this.isLoaded?(this.pendingSrc=i,this.updatePosterVisibility(),this.hideStatus()):this.loadIframe(i)),(e==="width"||e==="height")&&this.updateAspectRatio(),e==="loop"&&this.controller&&this.controller.setLoop(this.hasAttribute("loop")),e==="input-props")try{const s=JSON.parse(i);this.pendingProps=s,this.controller&&this.controller.setInputProps(s)}catch(s){console.warn("HeliosPlayer: Invalid JSON in input-props",s)}if(e==="muted"&&this.controller&&this.controller.setAudioMuted(this.hasAttribute("muted")),e==="controlslist"&&this.updateControlsVisibility(),e==="sandbox"){const s=this.getAttribute("sandbox"),n=s===null?"allow-scripts allow-same-origin":s;this.iframe.getAttribute("sandbox")!==n&&(this.iframe.setAttribute("sandbox",n),this.getAttribute("src")&&this.loadIframe(this.getAttribute("src")))}}}updateControlsVisibility(){if(!this.exportBtn||!this.fullscreenBtn)return;const r=(this.getAttribute("controlslist")||"").toLowerCase().split(/\s+/);r.includes("nodownload")?this.exportBtn.style.display="none":this.exportBtn.style.removeProperty("display"),r.includes("nofullscreen")?this.fullscreenBtn.style.display="none":this.fullscreenBtn.style.removeProperty("display")}get inputProps(){return this.pendingProps}set inputProps(e){this.pendingProps=e,this.controller&&e&&this.controller.setInputProps(e)}connectedCallback(){this.setAttribute("tabindex","0"),this.iframe.addEventListener("load",this.handleIframeLoad),window.addEventListener("message",this.handleWindowMessage),this.addEventListener("keydown",this.handleKeydown),document.addEventListener("fullscreenchange",this.updateFullscreenUI),this.playPauseBtn.addEventListener("click",this.togglePlayPause),this.volumeBtn.addEventListener("click",this.toggleMute),this.volumeSlider.addEventListener("input",this.handleVolumeInput),this.scrubber.addEventListener("input",this.handleScrubberInput),this.scrubber.addEventListener("mousedown",this.handleScrubStart),this.scrubber.addEventListener("change",this.handleScrubEnd),this.scrubber.addEventListener("touchstart",this.handleScrubStart,{passive:!0}),this.scrubber.addEventListener("touchend",this.handleScrubEnd),this.scrubber.addEventListener("touchcancel",this.handleScrubEnd),this.scrubberWrapper.addEventListener("mousemove",this.handleScrubberHover),this.scrubberWrapper.addEventListener("mouseleave",this.handleScrubberLeave),this.exportBtn.addEventListener("click",this.renderClientSide),this.speedSelector.addEventListener("change",this.handleSpeedChange),this.fullscreenBtn.addEventListener("click",this.toggleFullscreen),this.ccBtn.addEventListener("click",this.toggleCaptions),this.bigPlayBtn.addEventListener("click",this.handleBigPlayClick),this.posterContainer.addEventListener("click",this.handleBigPlayClick);const e=this.shadowRoot.querySelector("slot");e&&(e.addEventListener("slotchange",this.handleSlotChange),this.handleSlotChange()),this.setControlsDisabled(!0);const r=this.getAttribute("sandbox"),i=r===null?"allow-scripts allow-same-origin":r;this.iframe.getAttribute("sandbox")!==i&&this.iframe.setAttribute("sandbox",i),this.overlay.classList.contains("hidden")&&!this.pendingSrc&&!this.hasAttribute("poster")&&this.showStatus("Connecting...",!1),this.pendingSrc&&this.updatePosterVisibility(),this.updateAspectRatio(),this.updateControlsVisibility(),this.resizeObserver.observe(this)}disconnectedCallback(){this.resizeObserver.disconnect(),this.iframe.removeEventListener("load",this.handleIframeLoad),window.removeEventListener("message",this.handleWindowMessage),this.removeEventListener("keydown",this.handleKeydown),document.removeEventListener("fullscreenchange",this.updateFullscreenUI),this.playPauseBtn.removeEventListener("click",this.togglePlayPause),this.volumeBtn.removeEventListener("click",this.toggleMute),this.volumeSlider.removeEventListener("input",this.handleVolumeInput),this.scrubber.removeEventListener("input",this.handleScrubberInput),this.scrubber.removeEventListener("mousedown",this.handleScrubStart),this.scrubber.removeEventListener("change",this.handleScrubEnd),this.scrubber.removeEventListener("touchstart",this.handleScrubStart),this.scrubber.removeEventListener("touchend",this.handleScrubEnd),this.scrubber.removeEventListener("touchcancel",this.handleScrubEnd),this.scrubberWrapper.removeEventListener("mousemove",this.handleScrubberHover),this.scrubberWrapper.removeEventListener("mouseleave",this.handleScrubberLeave),this.exportBtn.removeEventListener("click",this.renderClientSide),this.speedSelector.removeEventListener("change",this.handleSpeedChange),this.fullscreenBtn.removeEventListener("click",this.toggleFullscreen),this.ccBtn.removeEventListener("click",this.toggleCaptions),this.bigPlayBtn.removeEventListener("click",this.handleBigPlayClick),this.posterContainer.removeEventListener("click",this.handleBigPlayClick);const e=this.shadowRoot.querySelector("slot");e&&e.removeEventListener("slotchange",this.handleSlotChange),this.stopConnectionAttempts(),this.unsubscribe&&this.unsubscribe(),this.controller&&(this.controller.pause(),this.controller.dispose())}loadIframe(e){this._error=null,this._networkState=ne.NETWORK_LOADING,this._readyState=ne.HAVE_NOTHING,this.dispatchEvent(new Event("loadstart")),this.iframe.src=e,this.isLoaded=!0,this.controller&&(this.controller.pause(),this.controller.dispose(),this.controller=null),this.setControlsDisabled(!0),this.hasAttribute("poster")||this.showStatus("Loading...",!1),this.updatePosterVisibility()}handleBigPlayClick=()=>{this.load(),this.controller?this.controller.play():this.setAttribute("autoplay","")};updatePosterVisibility(){if(this.pendingSrc){this.posterContainer.classList.remove("hidden");return}if(this.hasAttribute("poster")){let e=!1;if(this.controller){const r=this.controller.getState();(r.isPlaying||r.currentFrame>0)&&(e=!0)}e?this.posterContainer.classList.add("hidden"):this.posterContainer.classList.remove("hidden")}else this.posterContainer.classList.add("hidden")}setControlsDisabled(e){this.playPauseBtn.disabled=e,this.volumeBtn.disabled=e,this.volumeSlider.disabled=e,this.scrubber.disabled=e,this.speedSelector.disabled=e,this.fullscreenBtn.disabled=e,this.ccBtn.disabled=e,e&&(this.exportBtn.disabled=!0)}lockPlaybackControls(e){this.playPauseBtn.disabled=e,this.volumeBtn.disabled=e,this.volumeSlider.disabled=e,this.scrubber.disabled=e,this.speedSelector.disabled=e,this.fullscreenBtn.disabled=e,this.ccBtn.disabled=e}handleIframeLoad=()=>{this.iframe.contentWindow&&this.startConnectionAttempts()};startConnectionAttempts(){this.stopConnectionAttempts(),this.iframe.contentWindow?.postMessage({type:"HELIOS_CONNECT"},"*");const e=()=>{let i;try{i=this.iframe.contentWindow.helios}catch{}return i?(console.log("HeliosPlayer: Connected via Direct Mode."),this.stopConnectionAttempts(),this.hideStatus(),this.directHelios=i,this.setController(new Ur(i,this.iframe)),this.exportBtn.disabled=!1,!0):!1};if(e())return;const r=Date.now();this.connectionInterval=window.setInterval(()=>{if(this.controller){this.stopConnectionAttempts();return}e()||Date.now()-r>5e3&&(this.stopConnectionAttempts(),this.controller||this.showStatus("Connection Failed. Ensure window.helios is set or connectToParent() is called.",!0))},100)}stopConnectionAttempts(){this.connectionInterval&&(window.clearInterval(this.connectionInterval),this.connectionInterval=null)}handleWindowMessage=e=>{if(e.source===this.iframe.contentWindow&&e.data?.type==="HELIOS_READY"&&(this.stopConnectionAttempts(),this.hideStatus(),!this.controller)){console.log("HeliosPlayer: Connected via Bridge Mode.");const r=this.iframe.contentWindow;r&&(this.setController(new Wr(r,e.data.state)),e.data.state&&this.updateUI(e.data.state),this.exportBtn.disabled=!1)}};handleSlotChange=()=>{const e=this.shadowRoot.querySelector("slot");if(!e)return;const r=e.assignedElements(),i=new Set;r.forEach(s=>{if(s.tagName==="TRACK"){const n=s;if(i.add(n),this._domTracks.has(n))return;const o=n.getAttribute("kind")||"captions",a=n.getAttribute("label")||"",c=n.getAttribute("srclang")||"",l=n.getAttribute("src"),d=n.hasAttribute("default"),u=this.addTextTrack(o,a,c);this._domTracks.set(n,u),l&&fetch(l).then(f=>{if(!f.ok)throw new Error(`Status ${f.status}`);return f.text()}).then(f=>{Nr(f).forEach(p=>{u.addCue(new Tn(p.startTime,p.endTime,p.text))}),d?u.mode="showing":u.mode="disabled"}).catch(f=>console.error("HeliosPlayer: Failed to load captions",f))}});for(const[s,n]of this._domTracks.entries())i.has(s)||(n.mode==="showing"&&(n.mode="hidden"),this._textTracks.removeTrack(n),this._domTracks.delete(s))};setController(e){this.controller&&this.controller.dispose(),this.unsubscribe&&(this.unsubscribe(),this.unsubscribe=null),this.controller=e,this.handleSlotChange(),this._networkState=ne.NETWORK_IDLE,this._readyState=ne.HAVE_ENOUGH_DATA,this.dispatchEvent(new Event("loadedmetadata")),this.dispatchEvent(new Event("loadeddata")),this.dispatchEvent(new Event("canplay")),this.dispatchEvent(new Event("canplaythrough")),this.setControlsDisabled(!1),this.pendingProps&&this.controller.setInputProps(this.pendingProps),this.hasAttribute("muted")&&this.controller.setAudioMuted(!0),this.hasAttribute("loop")&&this.controller.setLoop(!0);const r=this.controller.getState();r&&(this.scrubber.max=String(r.duration*r.fps),this.updateUI(r));const i=this.controller.subscribe(n=>this.updateUI(n)),s=this.controller.onError(n=>{const o=n.message||String(n);this._error={code:4,message:o,MEDIA_ERR_ABORTED:1,MEDIA_ERR_NETWORK:2,MEDIA_ERR_DECODE:3,MEDIA_ERR_SRC_NOT_SUPPORTED:4},this.showStatus("Error: "+o,!0,{label:"Reload",handler:()=>this.retryConnection()}),this.dispatchEvent(new CustomEvent("error",{detail:n}))});this.unsubscribe=()=>{i(),s()},this.hasAttribute("autoplay")&&this.controller.play()}updateAspectRatio(){const e=parseFloat(this.getAttribute("width")||""),r=parseFloat(this.getAttribute("height")||"");!isNaN(e)&&!isNaN(r)&&e>0&&r>0?this.style.aspectRatio=`${e} / ${r}`:this.style.removeProperty("aspect-ratio")}togglePlayPause=()=>{if(!this.controller)return;const e=this.controller.getState();e.currentFrame>=e.duration*e.fps-1?(this.controller.seek(0),this.controller.play()):e.isPlaying?this.controller.pause():this.controller.play()};toggleMute=()=>{if(!this.controller)return;const e=this.controller.getState();this.controller.setAudioMuted(!e.muted)};handleVolumeInput=()=>{if(!this.controller)return;const e=parseFloat(this.volumeSlider.value);this.controller.setAudioVolume(e),e>0&&this.controller.setAudioMuted(!1)};handleScrubberInput=()=>{const e=parseInt(this.scrubber.value,10);this.controller&&this.controller.seek(e)};handleScrubStart=()=>{if(!this.controller)return;this.isScrubbing=!0,this.dispatchEvent(new Event("seeking"));const e=this.controller.getState();this.wasPlayingBeforeScrub=e.isPlaying,this.wasPlayingBeforeScrub&&this.controller.pause()};handleScrubEnd=()=>{this.controller&&(this.isScrubbing=!1,this.dispatchEvent(new Event("seeked")),this.wasPlayingBeforeScrub&&this.controller.play())};handleScrubberHover=e=>{if(!this.controller)return;const r=this.controller.getState(),i=this.scrubberWrapper.getBoundingClientRect(),s=e.clientX-i.left,n=i.width,a=Math.max(0,Math.min(1,s/n))*r.duration;this.scrubberTooltip.textContent=a.toFixed(2)+"s",this.scrubberTooltip.style.left=`${s}px`,this.scrubberTooltip.classList.remove("hidden")};handleScrubberLeave=()=>{this.scrubberTooltip.classList.add("hidden")};handleSpeedChange=()=>{this.controller&&this.controller.setPlaybackRate(parseFloat(this.speedSelector.value))};toggleCaptions=()=>{this.showCaptions=!this.showCaptions,this.ccBtn.classList.toggle("active",this.showCaptions),this.controller&&this.updateUI(this.controller.getState())};handleKeydown=e=>{if(this.isExporting)return;const r=e.composedPath()[0];if(r&&r.tagName){const i=r.tagName.toLowerCase();if(i==="input"||i==="select"||i==="textarea"||e.key===" "&&i==="button")return}if(this.controller)switch(e.key){case" ":case"k":case"K":e.preventDefault(),this.togglePlayPause();break;case"f":case"F":this.toggleFullscreen();break;case"ArrowRight":case"l":case"L":this.seekRelative(e.shiftKey?10:1);break;case"ArrowLeft":case"j":case"J":this.seekRelative(e.shiftKey?-10:-1);break;case"m":case"M":this.toggleMute();break;case".":this.seekRelative(1);break;case",":this.seekRelative(-1);break;case"i":case"I":{const i=this.controller.getState(),s=Math.floor(i.currentFrame),n=i.duration*i.fps;let o=i.playbackRange?i.playbackRange[1]:n;s>=o&&(o=n),this.controller.setPlaybackRange(s,o);break}case"o":case"O":{const i=this.controller.getState(),s=Math.floor(i.currentFrame);let n=i.playbackRange?i.playbackRange[0]:0;s<=n&&(n=0),this.controller.setPlaybackRange(n,s);break}case"x":case"X":this.controller.clearPlaybackRange();break}};seekRelative(e){if(!this.controller)return;const r=this.controller.getState(),i=Math.max(0,Math.min(Math.floor(r.duration*r.fps),r.currentFrame+e));this.controller.seek(i)}toggleFullscreen=()=>{document.fullscreenElement?document.exitFullscreen():this.requestFullscreen().catch(e=>{console.error(`Error attempting to enable fullscreen: ${e.message}`)})};updateFullscreenUI=()=>{document.fullscreenElement===this?(this.fullscreenBtn.textContent="↙",this.fullscreenBtn.title="Exit Fullscreen"):(this.fullscreenBtn.textContent="⛶",this.fullscreenBtn.title="Fullscreen")};updateUI(e){if((e.isPlaying||e.currentFrame>0)&&this.posterContainer.classList.add("hidden"),this.lastState){e.isPlaying!==this.lastState.isPlaying&&this.dispatchEvent(new Event(e.isPlaying?"play":"pause"));const a=this.lastState.currentFrame>=this.lastState.duration*this.lastState.fps-1,c=e.currentFrame>=e.duration*e.fps-1;!a&&c&&!e.isPlaying&&this.dispatchEvent(new Event("ended")),e.currentFrame!==this.lastState.currentFrame&&this.dispatchEvent(new Event("timeupdate")),(e.volume!==this.lastState.volume||e.muted!==this.lastState.muted)&&this.dispatchEvent(new Event("volumechange")),e.playbackRate!==this.lastState.playbackRate&&this.dispatchEvent(new Event("ratechange")),e.duration!==this.lastState.duration&&this.dispatchEvent(new Event("durationchange"))}e.currentFrame>=e.duration*e.fps-1?(this.playPauseBtn.textContent="🔄",this.playPauseBtn.setAttribute("aria-label","Restart")):(this.playPauseBtn.textContent=e.isPlaying?"❚❚":"▶",this.playPauseBtn.setAttribute("aria-label",e.isPlaying?"Pause":"Play"));const i=e.muted||e.volume===0;this.volumeBtn.textContent=i?"🔇":"🔊",this.volumeBtn.setAttribute("aria-label",i?"Unmute":"Mute"),this.volumeSlider.value=String(e.volume!==void 0?e.volume:1),this.isScrubbing||(this.scrubber.value=String(e.currentFrame));const s=(e.currentFrame/e.fps).toFixed(2),n=e.duration.toFixed(2);if(this.timeDisplay.textContent=`${s} / ${n}`,this.scrubber.setAttribute("aria-valuenow",String(e.currentFrame)),this.scrubber.setAttribute("aria-valuemin","0"),this.scrubber.setAttribute("aria-valuemax",String(e.duration*e.fps)),this.scrubber.setAttribute("aria-valuetext",`${s} of ${n} seconds`),e.playbackRate!==void 0&&(this.speedSelector.value=String(e.playbackRate)),(!this.lastState||e.markers!==this.lastState.markers)&&(this.markersContainer.innerHTML="",e.markers&&e.duration>0&&e.markers.forEach(a=>{const c=a.time/e.duration*100;if(c>=0&&c<=100){const l=document.createElement("div");l.className="marker",l.style.left=`${c}%`,a.color&&(l.style.backgroundColor=a.color),l.title=a.label||"",l.addEventListener("click",d=>{d.stopPropagation(),this.controller&&this.controller.seek(Math.floor(a.time*e.fps))}),this.markersContainer.appendChild(l)}})),e.playbackRange){const a=e.duration*e.fps;if(a>0){const[c,l]=e.playbackRange,d=c/a*100,u=l/a*100;this.scrubber.style.background=`linear-gradient(to right,
|
|
627
|
+
var(--helios-range-unselected-color) 0%,
|
|
628
|
+
var(--helios-range-unselected-color) ${d}%,
|
|
629
|
+
var(--helios-range-selected-color) ${d}%,
|
|
630
|
+
var(--helios-range-selected-color) ${u}%,
|
|
631
|
+
var(--helios-range-unselected-color) ${u}%,
|
|
632
|
+
var(--helios-range-unselected-color) 100%
|
|
633
|
+
)`}else this.scrubber.style.background=""}else this.scrubber.style.background="";this.captionsContainer.innerHTML="",this.showCaptions&&e.activeCaptions&&e.activeCaptions.length>0&&e.activeCaptions.forEach(a=>{const c=document.createElement("div");c.className="caption-cue",c.textContent=a.text,this.captionsContainer.appendChild(c)}),this.lastState=e}showStatus(e,r,i){this.overlay.classList.remove("hidden"),this.statusText.textContent=e,this.retryBtn.style.display=r?"block":"none",i?(this.retryBtn.textContent=i.label,this.retryAction=i.handler):(this.retryBtn.textContent="Retry",this.retryAction=()=>this.retryConnection()),this.statusText.classList.toggle("error-msg",r)}hideStatus(){this.overlay.classList.add("hidden")}getController(){return this.controller}async getSchema(){if(this.controller)return this.controller.getSchema()}retryConnection(){this.showStatus("Retrying...",!1),this.load()}renderClientSide=async()=>{if(this.abortController){this.abortController.abort(),this.abortController=null,this.exportBtn.textContent="Export",this.exportBtn.disabled=!1;return}if(!this.controller){console.error("Export not available: Not connected.");return}this.abortController=new AbortController,this.exportBtn.textContent="Cancel",this.isExporting=!0,this.lockPlaybackControls(!0);const e=new Fr(this.controller,this.iframe),r=this.getAttribute("export-mode")||"auto",i=this.getAttribute("canvas-selector")||"canvas",s=this.getAttribute("export-format")||"mp4",n=this.getAttribute("export-caption-mode")||"burn-in";let o=this.showCaptions;if(this.showCaptions&&n==="file"){const a=Array.from(this._textTracks).find(c=>c.mode==="showing"&&c.kind==="captions");if(a){const c=Array.from(a.cues).map(l=>({startTime:l.startTime,endTime:l.endTime,text:l.text}));e.saveCaptionsAsSRT(c,"captions.srt")}o=!1}try{await e.export({onProgress:a=>{this.exportBtn.textContent=`Cancel (${Math.round(a*100)}%)`},signal:this.abortController.signal,mode:r,canvasSelector:i,format:s,includeCaptions:o})}catch(a){a.message!=="Export aborted"&&this.showStatus("Export Failed: "+a.message,!0,{label:"Dismiss",handler:()=>this.hideStatus()}),console.error("Export failed or aborted",a)}finally{this.isExporting=!1,this.lockPlaybackControls(!1),this.exportBtn.textContent="Export",this.exportBtn.disabled=!1,this.abortController=null}}}customElements.get("helios-player")||customElements.define("helios-player",ne),fe.ClientSideExporter=Fr,fe.HeliosPlayer=ne,Object.defineProperty(fe,Symbol.toStringTag,{value:"Module"})}));
|