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