Peach: Local-first video metadata scraper and media manager.
import{t as e}from"./webkit-DljEkldb.js";import{t}from"./media-error-CtsbOTR1.js";import{n}from"./types-HOYOipK7.js";import{t as r}from"./html-video-adapter-BX2o9jnf.js";import{t as i}from"./session-data-Ca8-DKdu.js";import{n as a,t as o}from"./load-chapters-tracks-Btgfpe8i.js";const s={FAIRPLAY:`com.apple.fps`,WIDEVINE:`com.widevine.alpha`,PLAYREADY:`com.microsoft.playready`,CLEARKEY:`org.w3.clearkey`},c=`application/vnd.apple.mpegurl`,l={MISSING_CONFIGURATION:`drmMissingConfiguration`,UNSUPPORTED_KEY_SYSTEM:`drmUnsupportedKeySystem`,CERTIFICATE_REQUEST_FAILED:`drmCertificateRequestFailed`,SERVER_CERTIFICATE_FAILED:`drmServerCertificateFailed`,GENERATE_REQUEST_FAILED:`drmGenerateRequestFailed`,LICENSE_REQUEST_FAILED:`drmLicenseRequestFailed`,UPDATE_LICENSE_FAILED:`drmUpdateLicenseFailed`,CDM_ERROR:`drmCdmError`,OUTPUT_RESTRICTED:`drmOutputRestricted`},u={MISSING_CONFIGURATION:`This media is DRM-protected, but no DRM license server was configured for it.`,MISSING_CERTIFICATE_URL:`This media is DRM-protected, but no DRM application certificate was configured for it.`,UNSUPPORTED_KEY_SYSTEM:`This browser cannot play DRM-protected content with its current security configuration. Try another browser.`,CERTIFICATE_REQUEST_FAILED:`The DRM application certificate could not be loaded.`,SERVER_CERTIFICATE_FAILED:`The DRM application certificate was rejected. It may no longer be valid.`,GENERATE_REQUEST_FAILED:`A DRM license could not be requested for this media.`,LICENSE_REQUEST_FAILED:`The DRM license could not be loaded.`,UPDATE_LICENSE_FAILED:`The DRM license was rejected for this media.`,CDM_ERROR:`The DRM Content Decryption Module failed. Try reloading the page, updating your browser, or another browser.`,OUTPUT_RESTRICTED:`This output is not secure enough for DRM playback. The video may render as a black screen.`};function d(e,n,r=!0){return new t(e,t.MEDIA_ERR_ENCRYPTED,r,n)}function f(e,n,r){if(e instanceof t)return e;let i=d(n,r);return i.data=e,i}async function p({config:e,signal:t}){let{serverCertificateUrl:n}=e;if(!n)return null;let r=await fetch(n,{signal:t}).catch(e=>{throw f(e,u.CERTIFICATE_REQUEST_FAILED,l.CERTIFICATE_REQUEST_FAILED)});if(!r.ok)throw d(u.CERTIFICATE_REQUEST_FAILED,l.CERTIFICATE_REQUEST_FAILED);return r.arrayBuffer()}async function m({config:e,signal:t},n){let r=await fetch(e.licenseUrl,{method:`POST`,headers:{"Content-Type":`application/octet-stream`},body:n,signal:t}).catch(e=>{throw f(e,u.LICENSE_REQUEST_FAILED,l.LICENSE_REQUEST_FAILED)});if(!r.ok)throw d(u.LICENSE_REQUEST_FAILED,l.LICENSE_REQUEST_FAILED);return new Uint8Array(await r.arrayBuffer())}function h(e){return e.toLowerCase().includes(`.m3u8`)}function g(e){return e.includes(`#EXT-X-STREAM-INF`)}function _(e,t){let n=e.split(/\r?\n/),r=n.findIndex(e=>e.startsWith(`#EXT-X-STREAM-INF`));if(r===-1)return null;let i=n.slice(r+1).map(e=>e.trim()).find(e=>e&&!e.startsWith(`#`));if(!i)return null;try{return new URL(i,t).toString()}catch{return null}}function v(e){return new RegExp(String.raw`(?:^|[:,\s])${e}\s*=\s*([0-9.]+)\s*(?=[,]|$)`,`i`)}const y=v(`PART-TARGET`),b=v(`HOLD-BACK`),x=v(`PART-HOLD-BACK`);function S(e,t){let n=t.exec(e);if(!n)return;let r=Number(n[1]);return Number.isFinite(r)?r:void 0}function C(e){let t=e.split(/\r?\n/),n,r=!1,i,a,o,s;for(let e of t){let t=e.trim();if(t.startsWith(`#EXT-X-PLAYLIST-TYPE:`))n=t.slice(21).trim().toUpperCase();else if(t===`#EXT-X-ENDLIST`)r=!0;else if(t.startsWith(`#EXT-X-TARGETDURATION:`)){let e=Number(t.slice(22));Number.isFinite(e)&&(i=e)}else t.startsWith(`#EXT-X-PART-INF`)?a=S(t,y)??a:t.startsWith(`#EXT-X-SERVER-CONTROL`)&&(s=S(t,x)??s,o=S(t,b)??o)}return n===`VOD`||r?{targetLiveWindow:NaN,liveEdgeStartOffset:void 0}:{targetLiveWindow:n===`EVENT`?1/0:0,liveEdgeStartOffset:a===void 0?o||(i===void 0?void 0:i*3):s||a*2}}async function w(e,t){let n=await fetch(e,t);if(!n.ok)throw Error(`Failed to fetch playlist (${n.status}): ${e}`);return{text:await n.text(),url:n.url||e}}async function T(e,t){let n=t?{signal:t}:{},{text:r,url:i}=await w(e,n);if(!g(r))return C(r);let a=_(r,i);if(!a)throw Error(`No media playlist URL found in multivariant playlist`);return C((await w(a,n)).text)}function E(e){class t extends e{#e=null;#t=null;#n=``;attach(e){super.attach(e),this.#a(e)}detach(){this.#r(),super.detach?.()}destroy(){this.#r(),super.destroy?.()}#r(){this.#e?.abort(),this.#e=null,this.#i()}#i(){this.#t?.abort(),this.#t=null,this.#n=``}#a(e){this.#r(),this.#e=new AbortController;let{signal:t}=this.#e;e.addEventListener(`loadstart`,()=>this.#o(e),{signal:t}),e.addEventListener(`emptied`,()=>this.#i(),{signal:t}),(e.currentSrc||e.src)&&this.#o(e)}async#o(e){let t=e.currentSrc||e.src;if(!t||!h(t)||t===this.#n)return;this.#i(),this.#n=t;let n=this.#t=new AbortController;try{let r=await w(t,{signal:n.signal});if(n.signal.aborted||!g(r.text))return;let s=i(r.text,a,r.url);s&&o(e,s,n.signal)}catch{}}}return t}const D={initDataTypes:[`skd`],videoCapabilities:[{contentType:c,robustness:``}],distinctiveIdentifier:`not-allowed`,persistentState:`not-allowed`,sessionTypes:[`temporary`]};function O(e,t={}){let{media:n,signal:r,reportError:i}=e,a=/* @__PURE__ */ new Set,o=null,c=null;async function h(){let t;try{t=await navigator.requestMediaKeySystemAccess(s.FAIRPLAY,[D])}catch(e){throw f(e,u.UNSUPPORTED_KEY_SYSTEM,l.UNSUPPORTED_KEY_SYSTEM)}let i=await t.createMediaKeys(),a=await(c??=p(e));if(a&&!await i.setServerCertificate(a).catch(()=>!1))throw d(u.SERVER_CERTIFICATE_FAILED,l.SERVER_CERTIFICATE_FAILED);return r.aborted||await n.setMediaKeys(i),i}async function g(t,n){try{let i=await m(e,n.message);if(r.aborted)return;await t.update(i).catch(e=>{throw f(e,u.UPDATE_LICENSE_FAILED,l.UPDATE_LICENSE_FAILED)})}catch(e){if(r.aborted)return;i(f(e,u.CDM_ERROR,l.CDM_ERROR))}}function _(e){e.keyStatuses.forEach(e=>{e===`internal-error`?i(d(u.CDM_ERROR,l.CDM_ERROR)):(e===`output-restricted`||e===`output-downscaled`)&&i(d(u.OUTPUT_RESTRICTED,l.OUTPUT_RESTRICTED,!1))})}async function v(e,i,o){let s=e.createSession();a.add(s),s.addEventListener(`message`,e=>void g(s,e),{signal:r}),s.addEventListener(`keystatuseschange`,()=>_(s),{signal:r});try{await s.generateRequest(i,o)}catch(e){if(a.delete(s),await s.close().catch(()=>{}),k(n,e)){t.onUnsupported?.();return}throw f(e,u.GENERATE_REQUEST_FAILED,l.GENERATE_REQUEST_FAILED)}}return{async request(e){if(e.initDataType!==`skd`||!e.initData)return;let t=await(o??=h());r.aborted||await v(t,e.initDataType,e.initData)},async close(){let e=[...a].map(e=>e.close().catch(()=>{}));a.clear();let t=o;o=null,c=null,await Promise.all(e);let r=await t?.catch(()=>null);r&&n.mediaKeys===r&&await n.setMediaKeys(null).catch(()=>{})}}}function k(e,t){return t instanceof DOMException&&t.name===`NotSupportedError`&&!!e.webkitCurrentPlaybackTargetIsWireless}function A(e){return`WebKitMediaKeys`in globalThis&&`webkitSetMediaKeys`in e}function j(e){let{media:t,signal:n,reportError:r}=e,i=t,a=/* @__PURE__ */ new Set,o=null;function s(){if(i.webkitKeys)return;let e=globalThis.WebKitMediaKeys;if(!e||!A(t))throw d(u.UNSUPPORTED_KEY_SYSTEM,l.UNSUPPORTED_KEY_SYSTEM);try{i.webkitSetMediaKeys(new e(`com.apple.fps.1_0`))}catch(e){throw f(e,u.UNSUPPORTED_KEY_SYSTEM,l.UNSUPPORTED_KEY_SYSTEM)}}async function h(t,i){try{let r=await m(e,i.message);if(n.aborted)return;t.update(r)}catch(e){if(n.aborted)return;r(f(e,u.UPDATE_LICENSE_FAILED,l.UPDATE_LICENSE_FAILED))}}function g(e){let t=d(u.CDM_ERROR,l.CDM_ERROR);t.data=e.error,r(t)}return{async request(t){if(!t.initData)return;s();let r=await(o??=p(e));if(n.aborted)return;if(!r)throw d(u.MISSING_CERTIFICATE_URL,l.MISSING_CONFIGURATION);let f=i.webkitKeys.createSession(c,M(t.initData,r));a.add(f),f.addEventListener(`webkitkeymessage`,e=>void h(f,e),{signal:n}),f.addEventListener(`webkitkeyerror`,()=>g(f),{signal:n})},async close(){for(let e of a)try{e.close()}catch{}a.clear(),o=null;try{i.webkitSetMediaKeys(null)}catch{}}}}function M(e,t){let n=new Uint8Array(e),r=P(N(e)),i=new Uint8Array(t),a=new Uint8Array(n.byteLength+4+r.byteLength+4+i.byteLength),o=new DataView(a.buffer),s=0,c=e=>{a.set(e,s),s+=e.byteLength},l=e=>{o.setUint32(s,e.byteLength,!0),s+=4,c(e)};return c(n),l(r),l(i),a}function N(e){let t=new TextDecoder(`utf-16le`).decode(e),n=t.indexOf(`skd://`);return n===-1?t:t.slice(n+6)}function P(e){let t=new Uint8Array(e.length*2),n=new DataView(t.buffer);for(let t=0;t<e.length;t++)n.setUint16(t*2,e.charCodeAt(t),!0);return t}function F(n){class r extends n{#e=null;#t=null;#n=!1;attach(e){super.attach(e),this.#i(e)}detach(){this.#r(),super.detach?.()}destroy(){this.#r(),super.destroy?.()}#r(){this.#e?.abort(),this.#e=null,this.#n=!1,this.#c()}#i(t){this.#r(),this.#e=new AbortController;let{signal:n}=this.#e;t.addEventListener(`encrypted`,e=>this.#o(e,!1),{signal:n}),t.addEventListener(`webkitneedkey`,e=>this.#o(e,!0),{signal:n}),t.addEventListener(`emptied`,()=>void this.#c(),{signal:n}),e(t)&&t.addEventListener(`webkitcurrentplaybacktargetiswirelesschanged`,()=>{this.#n=!1,this.#c()},{signal:n})}#a(){let{drm:e,engine:t}=this.source??{};return t?.nativeHls?.drmSystems??e??{}}#o(e,n){if(n!==this.#n)return;let r=this.target,i=this.#a()[s.FAIRPLAY];if(!r)return;if(!i?.licenseUrl){this.setError(d(u.MISSING_CONFIGURATION,l.MISSING_CONFIGURATION));return}let a=this.#t??=this.#s(r,i);a.keySystem.request(e).catch(e=>{a.disconnect.signal.aborted||this.setError(e instanceof t?e:d(u.CDM_ERROR,l.CDM_ERROR))})}#s(e,t){let n=new AbortController,r=()=>this.#a()[s.FAIRPLAY]??t,i={media:e,get config(){return r()},signal:n.signal,reportError:e=>this.setError(e)};return{keySystem:this.#n?j(i):O(i,{onUnsupported:()=>void this.#l(e)}),disconnect:n}}#c(){let e=this.#t;return this.#t=null,e?.disconnect.abort(),e?.keySystem.close()??Promise.resolve()}async#l(e){this.#n||(this.#n=!0,await this.#c(),this.#e&&!this.#e.signal.aborted&&e.load())}}return r}function I(e){class n extends e{#e=null;#t=null;get error(){return this.#t}setError(e){e.fatal&&(this.#t=e),this.dispatchEvent(new ErrorEvent(`error`,{error:e,message:e.message}))}attach(e){super.attach(e),this.#r(e)}detach(){this.#n(),super.detach?.()}destroy(){this.#n(),super.destroy?.()}#n(){this.#e?.abort(),this.#e=null,this.#t=null}#r(e){this.#n(),this.#e=new AbortController;let n=this.#e.signal;e.addEventListener(`error`,n=>{if(n.target!==e)return;n.stopImmediatePropagation();let r=e.error;if(!r)return;let i=r.code,a=i>=t.MEDIA_ERR_ABORTED&&i<=t.MEDIA_ERR_ENCRYPTED;this.setError(new t(a?void 0:r.message,i,!0))},{signal:n,capture:!0}),e.addEventListener(`emptied`,()=>{this.#t=null},{signal:n})}}return n}function L(e){class t extends e{#e=NaN;#t;#n=null;#r=``;get targetLiveWindow(){return this.#e}get liveEdgeStart(){if(this.#t===void 0)return NaN;let e=this.target;if(!e)return NaN;let{seekable:t,buffered:n}=e,r=t.length?t:n;return r.length?r.end(r.length-1)-this.#t:NaN}attach(e){super.attach(e),this.#a(e)}detach(){this.#i(),super.detach?.()}destroy(){this.#i(),super.destroy?.()}#i(){this.#n?.abort(),this.#n=null,this.#r=``,this.#t=void 0,this.#s(NaN)}#a(e){this.#i(),this.#n=new AbortController;let{signal:t}=this.#n;e.addEventListener(`loadstart`,()=>this.#o(e),{signal:t}),e.addEventListener(`emptied`,()=>{this.#r=``,this.#t=void 0,this.#s(NaN)},{signal:t}),(e.currentSrc||e.src)&&this.#o(e)}async#o(e){let t=e.currentSrc||e.src;if(!t||!h(t)||t===this.#r)return;this.#r=t,this.#t=void 0,this.#s(NaN);let n=this.#n?.signal;try{let r=await T(t,n);if(n?.aborted||(e.currentSrc||e.src)!==t)return;this.#t=r.liveEdgeStartOffset,this.#s(r.targetLiveWindow)}catch{}}#s(e){Object.is(this.#e,e)||(this.#e=e,this.dispatchEvent(new Event(`targetlivewindowchange`)))}}return t}function R(e){class t extends e{#e=n.UNKNOWN;#t=!1;#n=null;get streamType(){return this.#e}set streamType(e){if(e===n.UNKNOWN){this.#t=!1,this.#o(this.#a());return}this.#t=!0,this.#s(e)}attach(e){super.attach(e),this.#i(e)}detach(){this.#r(),this.#o(n.UNKNOWN),super.detach?.()}destroy(){this.#r(),super.destroy?.()}#r(){this.#n?.abort(),this.#n=null}#i(e){this.#r(),this.#n=new AbortController;let{signal:t}=this.#n,r=()=>this.#o(this.#a(e));e.addEventListener(`durationchange`,r,{signal:t}),e.addEventListener(`loadedmetadata`,r,{signal:t}),e.addEventListener(`emptied`,()=>this.#o(n.UNKNOWN),{signal:t}),r()}#a(e=this.target){if(!e)return n.UNKNOWN;let{duration:t}=e;return t===1/0?n.LIVE:Number.isFinite(t)&&t>0?n.ON_DEMAND:n.UNKNOWN}#o(e){this.#t||this.#s(e)}#s(e){this.#e!==e&&(this.#e=e,this.dispatchEvent(new Event(`streamtypechange`)))}}return t}var z=class e extends r{static{this.defaultProps={src:``,source:null,preload:`metadata`,streamType:n.UNKNOWN}}static{this.engineName=`browser`}#e=e.defaultProps.src;#t=e.defaultProps.source;#n=e.defaultProps.preload;get engine(){return null}get src(){return this.#e}set src(e){let{drm:t,engine:n}=this.#t??{},r={...t&&{drm:t},...n&&{engine:n},...e&&{src:e}};this.#t=Object.keys(r).length>0?r:null,this.#e=e,this.target&&(this.target.src=e)}get source(){return this.#t}set source(e){let t=e??null;if(t===this.#t)return;let n=t?.src??``,r=this.#e!==n;this.#t=t,this.#e=n,r&&this.target&&(this.target.src=n)}get preload(){return this.#n}set preload(e){this.#n=e,this.target&&(this.target.preload=e)}attach(e){super.attach(e),this.preload!==e.preload&&(e.preload=this.preload),this.src&&(e.src=this.src)}},B=class extends E(L(R(F(I(z))))){};export{B as t};
//# sourceMappingURL=adapter-CGtKJB3B.js.map