vhs-utils.min.js 13.9 KB
/*! @name @videojs/vhs-utils @version 3.0.4 @license MIT */
!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(r="undefined"!=typeof globalThis?globalThis:r||self).vhsUtils=t()}(this,(function(){"use strict";var r,t,e={mp4:/^(av0?1|avc0?[1234]|vp0?9|flac|opus|mp3|mp4a|mp4v|stpp.ttml.im1t)/,webm:/^(vp0?[89]|av0?1|opus|vorbis)/,ogg:/^(vp0?[89]|theora|flac|opus|vorbis)/,video:/^(av0?1|avc0?[1234]|vp0?[89]|hvc1|hev1|theora|mp4v)/,audio:/^(mp4a|flac|vorbis|opus|ac-[34]|ec-3|alac|mp3|speex|aac)/,text:/^(stpp.ttml.im1t)/,muxerVideo:/^(avc0?1)/,muxerAudio:/^(mp4a)/,muxerText:/a^/},n=["video","audio","text"],o=["Video","Audio","Text"],i=function(r){return r?r.replace(/avc1\.(\d+)\.(\d+)/i,(function(r,t,e){return"avc1."+("00"+Number(t).toString(16)).slice(-2)+"00"+("00"+Number(e).toString(16)).slice(-2)})):r},a=function(r){return r.map(i)},u=function(r){void 0===r&&(r="");var t=r.split(","),o=[];return t.forEach((function(r){var t;r=r.trim(),n.forEach((function(n){var i=e[n].exec(r.toLowerCase());if(i&&!(i.length<=1)){t=n;var a=r.substring(0,i[1].length),u=r.replace(a,"");o.push({type:a,details:u,mediaType:n})}})),t||o.push({type:r,details:"",mediaType:"unknown"})})),o},f=function(r){return void 0===r&&(r=""),e.audio.test(r.trim().toLowerCase())},s=function(r){return void 0===r&&(r=""),e.text.test(r.trim().toLowerCase())},c=function(r){if(r&&"string"==typeof r){var t=r.toLowerCase().split(",").map((function(r){return i(r.trim())})),n="video";1===t.length&&f(t[0])?n="audio":1===t.length&&s(t[0])&&(n="application");var o="mp4";return t.every((function(r){return e.mp4.test(r)}))?o="mp4":t.every((function(r){return e.webm.test(r)}))?o="webm":t.every((function(r){return e.ogg.test(r)}))&&(o="ogg"),n+"/"+o+';codecs="'+r+'"'}},l=Object.freeze({__proto__:null,translateLegacyCodec:i,translateLegacyCodecs:a,mapLegacyAvcCodecs:function(r){return r.replace(/avc1\.(\d+)\.(\d+)/i,(function(r){return a([r])[0]}))},parseCodecs:u,codecsFromDefault:function(r,t){if(!r.mediaGroups.AUDIO||!t)return null;var e=r.mediaGroups.AUDIO[t];if(!e)return null;for(var n in e){var o=e[n];if(o.default&&o.playlists)return u(o.playlists[0].attributes.CODECS)}return null},isVideoCodec:function(r){return void 0===r&&(r=""),e.video.test(r.trim().toLowerCase())},isAudioCodec:f,isTextCodec:s,getMimeForCodec:c,browserSupportsCodec:function(r){return void 0===r&&(r=""),window.MediaSource&&window.MediaSource.isTypeSupported&&window.MediaSource.isTypeSupported(c(r))||!1},muxerSupportsCodec:function(r){return void 0===r&&(r=""),r.toLowerCase().split(",").every((function(r){r=r.trim();for(var t=0;t<o.length;t++){if(e["muxer"+o[t]].test(r))return!0}return!1}))},DEFAULT_AUDIO_CODEC:"mp4a.40.2",DEFAULT_VIDEO_CODEC:"avc1.4d400d"}),p=function(r){return r.toString(2).length},h=function(r){return Math.ceil(p(r)/8)},v=function(r,t,e){return void 0===e&&(e=" "),(function(r,t){for(var e="";t--;)e+=r;return e}(e,t)+r.toString()).slice(-t)},y=function(r){return ArrayBuffer.isView(r)},g=function(r){return r instanceof Uint8Array?r:(Array.isArray(r)||y(r)||r instanceof ArrayBuffer||(r="number"!=typeof r||"number"==typeof r&&r!=r?0:[r]),new Uint8Array(r&&r.buffer||r,r&&r.byteOffset||0,r&&r.byteLength||0))},m=window.BigInt||Number,d=[m("0x1"),m("0x100"),m("0x10000"),m("0x1000000"),m("0x100000000"),m("0x10000000000"),m("0x1000000000000"),m("0x100000000000000"),m("0x10000000000000000")],b=(r=new Uint16Array([65484]),255===(t=new Uint8Array(r.buffer,r.byteOffset,r.byteLength))[0]?"big":204===t[0]?"little":"unknown"),w="big"===b,A="little"===b,L=function(r,t){var e=void 0===t?{}:t,n=e.signed,o=void 0!==n&&n,i=e.le,a=void 0!==i&&i;r=g(r);var u=a?"reduce":"reduceRight",f=(r[u]?r[u]:Array.prototype[u]).call(r,(function(t,e,n){var o=a?n:Math.abs(n+1-r.length);return t+m(e)*d[o]}),m(0));if(o){var s=d[r.length]/m(2)-m(1);(f=m(f))>s&&(f-=s,f-=s,f-=m(2))}return Number(f)},U=function(r,t){var e=(void 0===t?{}:t).le,n=void 0!==e&&e;("bigint"!=typeof r&&"number"!=typeof r||"number"==typeof r&&r!=r)&&(r=0),r=m(r);for(var o=h(r),i=new Uint8Array(new ArrayBuffer(o)),a=0;a<o;a++){var u=n?a:Math.abs(a+1-i.length);i[u]=Number(r/d[a]&m(255)),r<0&&(i[u]=Math.abs(~i[u]),i[u]-=0===a?1:2)}return i},x=function(r,t){if("string"!=typeof r&&r&&"function"==typeof r.toString&&(r=r.toString()),"string"!=typeof r)return new Uint8Array;t||(r=unescape(encodeURIComponent(r)));for(var e=new Uint8Array(r.length),n=0;n<r.length;n++)e[n]=r.charCodeAt(n);return e},T=function(r,t,e){var n=void 0===e?{}:e,o=n.offset,i=void 0===o?0:o,a=n.mask,u=void 0===a?[]:a;r=g(r);var f=(t=g(t)).every?t.every:Array.prototype.every;return t.length&&r.length-i>=t.length&&f.call(t,(function(t,e){return t===(u[e]?u[e]&r[i+e]:r[i+e])}))},C=Object.freeze({__proto__:null,countBits:p,countBytes:h,padStart:v,isTypedArray:y,toUint8:g,toHexString:function(r){r=g(r);for(var t="",e=0;e<r.length;e++)t+=v(r[e].toString(16),2,"0");return t},toBinaryString:function(r){r=g(r);for(var t="",e=0;e<r.length;e++)t+=v(r[e].toString(2),8,"0");return t},ENDIANNESS:b,IS_BIG_ENDIAN:w,IS_LITTLE_ENDIAN:A,bytesToNumber:L,numberToBytes:U,bytesToString:function(r){if(!r)return"";r=Array.prototype.slice.call(r);var t=String.fromCharCode.apply(null,g(r));try{return decodeURIComponent(escape(t))}catch(r){}return t},stringToBytes:x,concatTypedArrays:function(){for(var r=arguments.length,t=new Array(r),e=0;e<r;e++)t[e]=arguments[e];if(t=t.filter((function(r){return r&&(r.byteLength||r.length)&&"string"!=typeof r})),t.length<=1)return g(t[0]);var n=t.reduce((function(r,t,e){return r+(t.byteLength||t.length)}),0),o=new Uint8Array(n),i=0;return t.forEach((function(r){r=g(r),o.set(r,i),i+=r.byteLength})),o},bytesMatch:T,sliceBytes:function(r,t,e){return Uint8Array.prototype.slice?Uint8Array.prototype.slice.call(r,t,e):new Uint8Array(Array.prototype.slice.call(r,t,e))},reverseBytes:function(r){return r.reverse?r.reverse():Array.prototype.reverse.call(r)}}),S=function(r){return"string"==typeof r?x(r):r},E=function r(t,e,n){void 0===n&&(n=!1),e=function(r){return Array.isArray(r)?r.map((function(r){return S(r)})):[S(r)]}(e),t=g(t);var o=[];if(!e.length)return o;for(var i=0;i<t.length;){var a=(t[i]<<24|t[i+1]<<16|t[i+2]<<8|t[i+3])>>>0,u=t.subarray(i+4,i+8);if(0===a)break;var f=i+a;if(f>t.length){if(n)break;f=t.length}var s=t.subarray(i+8,f);T(u,e[0])&&(1===e.length?o.push(s):o.push.apply(o,r(s,e.slice(1),n))),i=f}return o},k={EBML:g([26,69,223,163]),DocType:g([66,130]),Segment:g([24,83,128,103]),SegmentInfo:g([21,73,169,102]),Tracks:g([22,84,174,107]),Track:g([174]),TrackNumber:g([215]),DefaultDuration:g([35,227,131]),TrackEntry:g([174]),TrackType:g([131]),FlagDefault:g([136]),CodecID:g([134]),CodecPrivate:g([99,162]),VideoTrack:g([224]),AudioTrack:g([225]),Cluster:g([31,67,182,117]),Timestamp:g([231]),TimestampScale:g([42,215,177]),BlockGroup:g([160]),BlockDuration:g([155]),Block:g([161]),SimpleBlock:g([163])},_=[128,64,32,16,8,4,2,1],B=function(r,t,e,n){void 0===e&&(e=!0),void 0===n&&(n=!1);var o=function(r){for(var t=1,e=0;e<_.length&&!(r&_[e]);e++)t++;return t}(r[t]),i=r.subarray(t,t+o);return e&&((i=Array.prototype.slice.call(r,t,t+o))[0]^=_[o-1]),{length:o,value:L(i,{signed:n}),bytes:i}},D=function r(t){return"string"==typeof t?t.match(/.{1,2}/g).map((function(t){return r(t)})):"number"==typeof t?U(t):t},R=function r(t,e,n){if(n>=e.length)return e.length;var o=B(e,n,!1);if(T(t.bytes,o.bytes))return n;var i=B(e,n+o.length);return r(t,e,n+i.length+i.value+o.length)},I=function r(t,e){e=function(r){return Array.isArray(r)?r.map((function(r){return D(r)})):[D(r)]}(e),t=g(t);var n=[];if(!e.length)return n;for(var o=0;o<t.length;){var i=B(t,o,!1),a=B(t,o+i.length),u=o+i.length+a.length;127===a.value&&(a.value=R(i,t,u),a.value!==t.length&&(a.value-=u));var f=u+a.value>t.length?t.length:u+a.value,s=t.subarray(u,f);T(e[0],i.bytes)&&(1===e.length?n.push(s):n=n.concat(r(s,e.slice(1)))),o+=i.length+a.length+s.length}return n},N=g([73,68,51]),O=function r(t,e){return void 0===e&&(e=0),(t=g(t)).length-e<10||!T(t,N,{offset:e})?e:(e+=function(r,t){void 0===t&&(t=0);var e=(r=g(r))[t+5],n=r[t+6]<<21|r[t+7]<<14|r[t+8]<<7|r[t+9];return(16&e)>>4?n+20:n+10}(t,e),r(t,e))},z=g([0,0,0,1]),M=g([0,0,1]),F=g([0,0,3]),P=function(r){for(var t=[],e=1;e<r.length-2;)T(r.subarray(e,e+3),F)&&(t.push(e+2),e++),e++;if(0===t.length)return r;var n=r.length-t.length,o=new Uint8Array(n),i=0;for(e=0;e<n;i++,e++)i===t[0]&&(i++,t.shift()),o[e]=r[i];return o},j=function(r,t,e,n){void 0===n&&(n=1/0),r=g(r),e=[].concat(e);for(var o,i=0,a=0;i<r.length&&(a<n||o);){var u=void 0;if(T(r.subarray(i),z)?u=4:T(r.subarray(i),M)&&(u=3),u){if(a++,o)return P(r.subarray(o,i));var f=void 0;"h264"===t?f=31&r[i+u]:"h265"===t&&(f=r[i+u]>>1&63),-1!==e.indexOf(f)&&(o=i+u),i+=u+("h264"===t?1:2)}else i++}return r.subarray(0,0)},q={webm:g([119,101,98,109]),matroska:g([109,97,116,114,111,115,107,97]),flac:g([102,76,97,67]),ogg:g([79,103,103,83]),ac3:g([11,119]),riff:g([82,73,70,70]),avi:g([65,86,73]),wav:g([87,65,86,69]),"3gp":g([102,116,121,112,51,103]),mp4:g([102,116,121,112]),fmp4:g([115,116,121,112]),mov:g([102,116,121,112,113,116]),moov:g([109,111,111,118]),moof:g([109,111,111,102])},G={aac:function(r){var t=O(r);return T(r,[255,16],{offset:t,mask:[255,22]})},mp3:function(r){var t=O(r);return T(r,[255,2],{offset:t,mask:[255,6]})},webm:function(r){var t=I(r,[k.EBML,k.DocType])[0];return T(t,q.webm)},mkv:function(r){var t=I(r,[k.EBML,k.DocType])[0];return T(t,q.matroska)},mp4:function(r){return!G["3gp"](r)&&!G.mov(r)&&(!(!T(r,q.mp4,{offset:4})&&!T(r,q.fmp4,{offset:4}))||(!(!T(r,q.moof,{offset:4})&&!T(r,q.moov,{offset:4}))||void 0))},mov:function(r){return T(r,q.mov,{offset:4})},"3gp":function(r){return T(r,q["3gp"],{offset:4})},ac3:function(r){var t=O(r);return T(r,q.ac3,{offset:t})},ts:function(r){if(r.length<189&&r.length>=1)return 71===r[0];for(var t=0;t+188<r.length&&t<188;){if(71===r[t]&&71===r[t+188])return!0;t+=1}return!1},flac:function(r){var t=O(r);return T(r,q.flac,{offset:t})},ogg:function(r){return T(r,q.ogg)},avi:function(r){return T(r,q.riff)&&T(r,q.avi,{offset:8})},wav:function(r){return T(r,q.riff)&&T(r,q.wav,{offset:8})},h264:function(r){return function(r,t,e){return j(r,"h264",t,e)}(r,7,3).length},h265:function(r){return function(r,t,e){return j(r,"h265",t,e)}(r,[32,33],3).length}},V=Object.keys(G).filter((function(r){return"ts"!==r&&"h264"!==r&&"h265"!==r})).concat(["ts","h264","h265"]);V.forEach((function(r){var t=G[r];G[r]=function(r){return t(g(r))}}));var H=G,$=Object.freeze({__proto__:null,isLikely:H,detectContainerForBytes:function(r){r=g(r);for(var t=0;t<V.length;t++){var e=V[t];if(H[e](r))return e}return""},isLikelyFmp4MediaSegment:function(r){return E(r,["moof"]).length>0}});var Z=Object.freeze({__proto__:null,forEachMediaGroup:function(r,t,e){t.forEach((function(t){for(var n in r.mediaGroups[t])for(var o in r.mediaGroups[t][n]){var i=r.mediaGroups[t][n][o];e(i,t,n,o)}}))}});var J=function(r,t,e){return r(e={path:t,exports:{},require:function(r,t){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}(null==t&&e.path)}},e.exports),e.exports}((function(r,t){var e,n,o,i,a;e=/^((?:[a-zA-Z0-9+\-.]+:)?)(\/\/[^\/?#]*)?((?:[^\/?#]*\/)*[^;?#]*)?(;[^?#]*)?(\?[^#]*)?(#.*)?$/,n=/^([^\/?#]*)(.*)$/,o=/(?:\/|^)\.(?=\/)/g,i=/(?:\/|^)\.\.\/(?!\.\.\/)[^\/]*(?=\/)/g,a={buildAbsoluteURL:function(r,t,e){if(e=e||{},r=r.trim(),!(t=t.trim())){if(!e.alwaysNormalize)return r;var o=a.parseURL(r);if(!o)throw new Error("Error trying to parse base URL.");return o.path=a.normalizePath(o.path),a.buildURLFromParts(o)}var i=a.parseURL(t);if(!i)throw new Error("Error trying to parse relative URL.");if(i.scheme)return e.alwaysNormalize?(i.path=a.normalizePath(i.path),a.buildURLFromParts(i)):t;var u=a.parseURL(r);if(!u)throw new Error("Error trying to parse base URL.");if(!u.netLoc&&u.path&&"/"!==u.path[0]){var f=n.exec(u.path);u.netLoc=f[1],u.path=f[2]}u.netLoc&&!u.path&&(u.path="/");var s={scheme:u.scheme,netLoc:i.netLoc,path:null,params:i.params,query:i.query,fragment:i.fragment};if(!i.netLoc&&(s.netLoc=u.netLoc,"/"!==i.path[0]))if(i.path){var c=u.path,l=c.substring(0,c.lastIndexOf("/")+1)+i.path;s.path=a.normalizePath(l)}else s.path=u.path,i.params||(s.params=u.params,i.query||(s.query=u.query));return null===s.path&&(s.path=e.alwaysNormalize?a.normalizePath(i.path):i.path),a.buildURLFromParts(s)},parseURL:function(r){var t=e.exec(r);return t?{scheme:t[1]||"",netLoc:t[2]||"",path:t[3]||"",params:t[4]||"",query:t[5]||"",fragment:t[6]||""}:null},normalizePath:function(r){for(r=r.split("").reverse().join("").replace(o,"");r.length!==(r=r.replace(i,"")).length;);return r.split("").reverse().join("")},buildURLFromParts:function(r){return r.scheme+r.netLoc+r.path+r.params+r.query+r.fragment}},r.exports=a})),K="http://example.com",Q=function(){function r(){this.listeners={}}var t=r.prototype;return t.on=function(r,t){this.listeners[r]||(this.listeners[r]=[]),this.listeners[r].push(t)},t.off=function(r,t){if(!this.listeners[r])return!1;var e=this.listeners[r].indexOf(t);return this.listeners[r]=this.listeners[r].slice(0),this.listeners[r].splice(e,1),e>-1},t.trigger=function(r){var t=this.listeners[r];if(t)if(2===arguments.length)for(var e=t.length,n=0;n<e;++n)t[n].call(this,arguments[1]);else for(var o=Array.prototype.slice.call(arguments,1),i=t.length,a=0;a<i;++a)t[a].apply(this,o)},t.dispose=function(){this.listeners={}},t.pipe=function(r){this.on("data",(function(t){r.push(t)}))},r}();return{codecs:l,byteHelpers:C,containers:$,decodeB64ToUint8Array:function(r){for(var t,e=(t=r,window.atob?window.atob(t):Buffer.from(t,"base64").toString("binary")),n=new Uint8Array(e.length),o=0;o<e.length;o++)n[o]=e.charCodeAt(o);return n},mediaGroups:Z,resolveUrl:function(r,t){if(/^[a-z]+:/i.test(t))return t;/^data:/.test(r)&&(r=window.location&&window.location.href||"");var e="function"==typeof window.URL,n=/^\/\//.test(r),o=!window.location&&!/\/\//i.test(r);if(e?r=new window.URL(r,window.location||K):/\/\//i.test(r)||(r=J.buildAbsoluteURL(window.location&&window.location.href||"",r)),e){var i=new URL(t,r);return o?i.href.slice(K.length):n?i.href.slice(i.protocol.length):i.href}return J.buildAbsoluteURL(r,t)},Stream:Q}}));