// @bun
var{defineProperty:H,getOwnPropertyNames:p,getOwnPropertyDescriptor:d}=Object,u=Object.prototype.hasOwnProperty;function n(z){return this[z]}var s=(z)=>{var J=(v??=new WeakMap).get(z),Y;if(J)return J;if(J=H({},"__esModule",{value:!0}),z&&typeof z==="object"||typeof z==="function"){for(var $ of p(z))if(!u.call(J,$))H(J,$,{get:n.bind(z,$),enumerable:!(Y=d(z,$))||Y.enumerable})}return v.set(z,J),J},v;var l=(z)=>z;function o(z,J){this[z]=l.bind(null,J)}var zz=(z,J)=>{for(var Y in J)H(z,Y,{get:J[Y],enumerable:!0,configurable:!0,set:o.bind(J,Y)})};var S=(z,J)=>()=>(z&&(J=z(z=0)),J);var r=import.meta.require;import{createHash as I}from"crypto";import{readFile as a,stat as b}from"fs/promises";async function T(z){try{let J=await a(z);return I("sha256").update(J).digest("hex").slice(0,16)}catch{return""}}function Xz(z){return I("sha256").update(z).digest("hex").slice(0,16)}function U(z){return I("sha256").update(z).digest("hex").slice(0,16)}function Yz(z,J){let Y=JSON.stringify(J,Object.keys(J).sort());return`${U(z)}-${U(Y)}`}async function Zz(z){try{return(await b(z)).mtimeMs}catch{return 0}}async function B(z){try{return await b(z),!0}catch{return!1}}var R=()=>{};import{resolve as P,basename as f,extname as q,join as k}from"path";import{mkdir as E,writeFile as y}from"fs/promises";async function h(){try{return await import("ts-videos")}catch{return null}}async function Gz(z,J,Y,$,N){let G=await h();if(!G)return console.warn("[stx-media] ts-videos not installed, cannot generate HLS manifest"),null;try{let K=P(z);if(!await B(K))return null;let X=await T(K),W=f(z,q(z)),C=k(Y,`${W}-${X}-hls`);await E(C,{recursive:!0});let V=J.length>0?J:m,O=N?.segmentDuration||6,F=(await new G.Input(K).getInfo()).duration||0,L=[],M=0;for(let w of V){let x=k(C,w.label);await E(x,{recursive:!0});let Z=`${w.label}.m3u8`,j=k(x,Z);try{let _=await G.generateHLS(K,{outputDir:x,segmentDuration:O,width:w.width,height:w.height,videoBitrate:w.bitrate,audioBitrate:w.audioBitrate,playlistName:w.label});if(_)L.push({quality:w,url:`${$}/${W}-${X}-hls/${w.label}/${Z}`,path:j}),M+=_.segmentCount||Math.ceil(F/O)}catch(_){console.warn(`[stx-media] Failed to generate HLS for ${w.label}: ${_}`)}}if(L.length===0)return null;let Q=k(C,"master.m3u8"),A=t(L,$,W,X);return await y(Q,A),{manifestUrl:`${$}/${W}-${X}-hls/master.m3u8`,manifestPath:Q,playlists:L,segmentCount:M,duration:F}}catch(K){return console.warn(`[stx-media] Failed to generate HLS manifest: ${K}`),null}}function t(z,J,Y,$){let N=["#EXTM3U","#EXT-X-VERSION:3"];for(let G of z){let{quality:K}=G,X=K.bitrate+(K.audioBitrate||128000),W=`${K.width}x${K.height||Math.round(K.width*9/16)}`;N.push(`#EXT-X-STREAM-INF:BANDWIDTH=${X},RESOLUTION=${W},NAME="${K.label}"`,`${K.label}/${K.label}.m3u8`)}return N.join(`
`)}async function Lz(z,J,Y,$,N){let G=await h();if(!G)return console.warn("[stx-media] ts-videos not installed, cannot generate DASH manifest"),null;try{let K=P(z);if(!await B(K))return null;let X=await T(K),W=f(z,q(z)),C=k(Y,`${W}-${X}-dash`);await E(C,{recursive:!0});let V=J.length>0?J:m,O=N?.segmentDuration||4,g=N?.minBufferTime||2,L=(await new G.Input(K).getInfo()).duration||0,M=[],Q=[];for(let Z of V){let j=k(C,Z.label);await E(j,{recursive:!0});try{if(await G.generateDASH(K,{outputDir:j,segmentDuration:O,width:Z.width,height:Z.height,videoBitrate:Z.bitrate,audioBitrate:Z.audioBitrate})){if(M.push({bandwidth:Z.bitrate,width:Z.width,height:Z.height||Math.round(Z.width*9/16),url:`${$}/${W}-${X}-dash/${Z.label}/video`}),Z.audioBitrate)Q.push({bandwidth:Z.audioBitrate,url:`${$}/${W}-${X}-dash/${Z.label}/audio`})}}catch(_){console.warn(`[stx-media] Failed to generate DASH for ${Z.label}: ${_}`)}}if(M.length===0)return null;let A=k(C,"manifest.mpd"),w=e(M,Q,L,O,g);await y(A,w);let x=[{type:"video",representations:M}];if(Q.length>0)x.push({type:"audio",representations:Q});return{manifestUrl:`${$}/${W}-${X}-dash/manifest.mpd`,manifestPath:A,adaptationSets:x,duration:L}}catch(K){return console.warn(`[stx-media] Failed to generate DASH manifest: ${K}`),null}}function e(z,J,Y,$,N){let G=D(Y),K=D(N),X=`<?xml version="1.0" encoding="UTF-8"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd"
     type="static"
     mediaPresentationDuration="${G}"
     minBufferTime="${K}"
     profiles="urn:mpeg:dash:profile:isoff-on-demand:2011">
  <Period>
    <AdaptationSet mimeType="video/mp4" codecs="avc1.64001f" segmentAlignment="true" startWithSAP="1">
`;for(let W=0;W<z.length;W++){let C=z[W];X+=`      <Representation id="video${W}" bandwidth="${C.bandwidth}" width="${C.width}" height="${C.height}">
        <BaseURL>${C.url}/</BaseURL>
        <SegmentBase indexRange="0-999">
          <Initialization range="0-999"/>
        </SegmentBase>
      </Representation>
`}if(X+=`    </AdaptationSet>
`,J.length>0){X+=`    <AdaptationSet mimeType="audio/mp4" codecs="mp4a.40.2" segmentAlignment="true" startWithSAP="1">
`;for(let W=0;W<J.length;W++){let C=J[W];X+=`      <Representation id="audio${W}" bandwidth="${C.bandwidth}">
        <BaseURL>${C.url}/</BaseURL>
        <SegmentBase indexRange="0-999">
          <Initialization range="0-999"/>
        </SegmentBase>
      </Representation>
`}X+=`    </AdaptationSet>
`}return X+=`  </Period>
</MPD>`,X}function D(z){let J=Math.floor(z/3600),Y=Math.floor(z%3600/60),$=Math.floor(z%60),N="PT";if(J>0)N+=`${J}H`;if(Y>0)N+=`${Y}M`;return N+=`${$}S`,N}var m;var i=S(()=>{R();m=[{label:"1080p",width:1920,height:1080,bitrate:5000000,audioBitrate:192000},{label:"720p",width:1280,height:720,bitrate:2500000,audioBitrate:128000},{label:"480p",width:854,height:480,bitrate:1e6,audioBitrate:96000},{label:"360p",width:640,height:360,bitrate:600000,audioBitrate:64000}]});i();export{Gz as generateHLSManifest,Lz as generateDASHManifest,m as DEFAULT_STREAMING_QUALITIES};
