You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3 lines
14 KiB
3 lines
14 KiB
/*! @name @videojs/vhs-utils @version 3.0.0 @license MIT */
|
|
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("global/window")):"function"==typeof define&&define.amd?define(["global/window"],e):(r="undefined"!=typeof globalThis?globalThis:r||self).vhsUtils=e(r.window)}(this,(function(r){"use strict";function e(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var t,n,o=e(r),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(r){return r?r.replace(/avc1\.(\d+)\.(\d+)/i,(function(r,e,t){return"avc1."+("00"+Number(e).toString(16)).slice(-2)+"00"+("00"+Number(t).toString(16)).slice(-2)})):r},s=function(r){return r.map(f)},c=function(r){void 0===r&&(r="");var e=r.split(","),t=[];return e.forEach((function(r){var e;r=r.trim(),i.forEach((function(n){var o=a[n].exec(r.toLowerCase());if(o&&!(o.length<=1)){e=n;var i=r.substring(0,o[1].length),u=r.replace(i,"");t.push({type:i,details:u,mediaType:n})}})),e||t.push({type:r,details:"",mediaType:"unknown"})})),t},l=function(r){return void 0===r&&(r=""),a.audio.test(r.trim().toLowerCase())},p=function(r){return void 0===r&&(r=""),a.text.test(r.trim().toLowerCase())},h=function(r){if(r&&"string"==typeof r){var e=r.toLowerCase().split(",").map((function(r){return f(r.trim())})),t="video";1===e.length&&l(e[0])?t="audio":1===e.length&&p(e[0])&&(t="application");var n="mp4";return e.every((function(r){return a.mp4.test(r)}))?n="mp4":e.every((function(r){return a.webm.test(r)}))?n="webm":e.every((function(r){return a.ogg.test(r)}))&&(n="ogg"),t+"/"+n+';codecs="'+r+'"'}},v=Object.freeze({__proto__:null,translateLegacyCodec:f,translateLegacyCodecs:s,mapLegacyAvcCodecs:function(r){return r.replace(/avc1\.(\d+)\.(\d+)/i,(function(r){return s([r])[0]}))},parseCodecs:c,codecsFromDefault:function(r,e){if(!r.mediaGroups.AUDIO||!e)return null;var t=r.mediaGroups.AUDIO[e];if(!t)return null;for(var n in t){var o=t[n];if(o.default&&o.playlists)return c(o.playlists[0].attributes.CODECS)}return null},isVideoCodec:function(r){return void 0===r&&(r=""),a.video.test(r.trim().toLowerCase())},isAudioCodec:l,isTextCodec:p,getMimeForCodec:h,browserSupportsCodec:function(r){return void 0===r&&(r=""),o.default.MediaSource&&o.default.MediaSource.isTypeSupported&&o.default.MediaSource.isTypeSupported(h(r))||!1},muxerSupportsCodec:function(r){return void 0===r&&(r=""),r.toLowerCase().split(",").every((function(r){r=r.trim();for(var e=0;e<u.length;e++){if(a["muxer"+u[e]].test(r))return!0}return!1}))},DEFAULT_AUDIO_CODEC:"mp4a.40.2",DEFAULT_VIDEO_CODEC:"avc1.4d400d"}),y=function(r){return r.toString(2).length},g=function(r){return Math.ceil(y(r)/8)},d=function(r,e,t){return void 0===t&&(t=" "),(function(r,e){for(var t="";e--;)t+=r;return t}(t,e)+r.toString()).slice(-e)},m=function(r){return ArrayBuffer.isView(r)},b=function(r){return r instanceof Uint8Array?r:(Array.isArray(r)||m(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))},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=(t=new Uint16Array([65484]),255===(n=new Uint8Array(t.buffer,t.byteOffset,t.byteLength))[0]?"big":204===n[0]?"little":"unknown"),U="big"===L,x="little"===L,T=function(r,e){var t=void 0===e?{}:e,n=t.signed,o=void 0!==n&&n,a=t.le,i=void 0!==a&&a;r=b(r);var u=i?"reduce":"reduceRight",f=(r[u]?r[u]:Array.prototype[u]).call(r,(function(e,t,n){var o=i?n:Math.abs(n+1-r.length);return e+A(t)*w[o]}),A(0));if(o){var s=w[r.length]/A(2)-A(1);(f=A(f))>s&&(f-=s,f-=s,f-=A(2))}return Number(f)},C=function(r,e){var t=(void 0===e?{}:e).le,n=void 0!==t&&t;("bigint"!=typeof r&&"number"!=typeof r||"number"==typeof r&&r!=r)&&(r=0),r=A(r);for(var o=g(r),a=new Uint8Array(new ArrayBuffer(o)),i=0;i<o;i++){var u=n?i:Math.abs(i+1-a.length);a[u]=Number(r/w[i]&A(255)),r<0&&(a[u]=Math.abs(~a[u]),a[u]-=0===i?1:2)}return a},S=function(r,e){if("string"!=typeof r&&r&&"function"==typeof r.toString&&(r=r.toString()),"string"!=typeof r)return new Uint8Array;e||(r=unescape(encodeURIComponent(r)));for(var t=new Uint8Array(r.length),n=0;n<r.length;n++)t[n]=r.charCodeAt(n);return t},E=function(r,e,t){var n=void 0===t?{}:t,o=n.offset,a=void 0===o?0:o,i=n.mask,u=void 0===i?[]:i;r=b(r);var f=(e=b(e)).every?e.every:Array.prototype.every;return e.length&&r.length-a>=e.length&&f.call(e,(function(e,t){return e===(u[t]?u[t]&r[a+t]:r[a+t])}))},k=Object.freeze({__proto__:null,countBits:y,countBytes:g,padStart:d,isTypedArray:m,toUint8:b,toHexString:function(r){r=b(r);for(var e="",t=0;t<r.length;t++)e+=d(r[t].toString(16),2,"0");return e},toBinaryString:function(r){r=b(r);for(var e="",t=0;t<r.length;t++)e+=d(r[t].toString(2),8,"0");return e},ENDIANNESS:L,IS_BIG_ENDIAN:U,IS_LITTLE_ENDIAN:x,bytesToNumber:T,numberToBytes:C,bytesToString:function(r){if(!r)return"";r=Array.prototype.slice.call(r);var e=String.fromCharCode.apply(null,b(r));try{return decodeURIComponent(escape(e))}catch(r){}return e},stringToBytes:S,concatTypedArrays:function(){for(var r=arguments.length,e=new Array(r),t=0;t<r;t++)e[t]=arguments[t];if((e=e.filter((function(r){return r&&(r.byteLength||r.length)&&"string"!=typeof r}))).length<=1)return b(e[0]);var n=e.reduce((function(r,e,t){return r+(e.byteLength||e.length)}),0),o=new Uint8Array(n),a=0;return e.forEach((function(r){r=b(r),o.set(r,a),a+=r.byteLength})),o},bytesMatch:E,sliceBytes:function(r,e,t){return Uint8Array.prototype.slice?Uint8Array.prototype.slice.call(r,e,t):new Uint8Array(Array.prototype.slice.call(r,e,t))},reverseBytes:function(r){return r.reverse?r.reverse():Array.prototype.reverse.call(r)}}),_=function(r){return"string"==typeof r?S(r):r},B=function r(e,t,n){void 0===n&&(n=!1),t=function(r){return Array.isArray(r)?r.map((function(r){return _(r)})):[_(r)]}(t),e=b(e);var o=[];if(!t.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 s=e.subarray(a+8,f);E(u,t[0])&&(1===t.length?o.push(s):o.push.apply(o,r(s,t.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])},I=[128,64,32,16,8,4,2,1],N=function(r,e,t,n){void 0===t&&(t=!0),void 0===n&&(n=!1);var o=function(r){for(var e=1,t=0;t<I.length&&!(r&I[t]);t++)e++;return e}(r[e]),a=r.subarray(e,e+o);return t&&((a=Array.prototype.slice.call(r,e,e+o))[0]^=I[o-1]),{length:o,value:T(a,{signed:n}),bytes:a}},O=function r(e){return"string"==typeof e?e.match(/.{1,2}/g).map((function(e){return r(e)})):"number"==typeof e?C(e):e},R=function r(e,t,n){if(n>=t.length)return t.length;var o=N(t,n,!1);if(E(e.bytes,o.bytes))return n;var a=N(t,n+o.length);return r(e,t,n+a.length+a.value+o.length)},z=function r(e,t){t=function(r){return Array.isArray(r)?r.map((function(r){return O(r)})):[O(r)]}(t),e=b(e);var n=[];if(!t.length)return n;for(var o=0;o<e.length;){var a=N(e,o,!1),i=N(e,o+a.length),u=o+a.length+i.length;127===i.value&&(i.value=R(a,e,u),i.value!==e.length&&(i.value-=u));var f=u+i.value>e.length?e.length:u+i.value,s=e.subarray(u,f);E(t[0],a.bytes)&&(1===t.length?n.push(s):n=n.concat(r(s,t.slice(1)))),o+=a.length+i.length+s.length}return n},M=b([73,68,51]),F=function r(e,t){return void 0===t&&(t=0),(e=b(e)).length-t<10||!E(e,M,{offset:t})?t:(t+=function(r,e){void 0===e&&(e=0);var t=(r=b(r))[e+5],n=r[e+6]<<21|r[e+7]<<14|r[e+8]<<7|r[e+9];return(16&t)>>4?n+20:n+10}(e,t),r(e,t))},j=b([0,0,0,1]),q=b([0,0,1]),P=b([0,0,3]),G=function(r){for(var e=[],t=1;t<r.length-2;)E(r.subarray(t,t+3),P)&&(e.push(t+2),t++),t++;if(0===e.length)return r;var n=r.length-e.length,o=new Uint8Array(n),a=0;for(t=0;t<n;a++,t++)a===e[0]&&(a++,e.shift()),o[t]=r[a];return o},V=function(r,e,t,n){void 0===n&&(n=1/0),r=b(r),t=[].concat(t);for(var o,a=0,i=0;a<r.length&&(i<n||o);){var u=void 0;if(E(r.subarray(a),j)?u=4:E(r.subarray(a),q)&&(u=3),u){if(i++,o)return G(r.subarray(o,a));var f=void 0;"h264"===e?f=31&r[a+u]:"h265"===e&&(f=r[a+u]>>1&63),-1!==t.indexOf(f)&&(o=a+u),a+=u+("h264"===e?1:2)}else a++}return r.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(r){var e=F(r);return E(r,[255,16],{offset:e,mask:[255,22]})},mp3:function(r){var e=F(r);return E(r,[255,2],{offset:e,mask:[255,6]})},webm:function(r){var e=z(r,[D.EBML,D.DocType])[0];return E(e,H.webm)},mkv:function(r){var e=z(r,[D.EBML,D.DocType])[0];return E(e,H.matroska)},mp4:function(r){return!$["3gp"](r)&&!$.mov(r)&&(E(r,H.mp4,{offset:4})||E(r,H.fmp4,{offset:4}))},mov:function(r){return E(r,H.mov,{offset:4})},"3gp":function(r){return E(r,H["3gp"],{offset:4})},ac3:function(r){var e=F(r);return E(r,H.ac3,{offset:e})},ts:function(r){if(r.length<189&&r.length>=1)return 71===r[0];for(var e=0;e+188<r.length&&e<188;){if(71===r[e]&&71===r[e+188])return!0;e+=1}return!1},flac:function(r){var e=F(r);return E(r,H.flac,{offset:e})},ogg:function(r){return E(r,H.ogg)},avi:function(r){return E(r,H.riff)&&E(r,H.avi,{offset:8})},wav:function(r){return E(r,H.riff)&&E(r,H.wav,{offset:8})},h264:function(r){return function(r,e,t){return V(r,"h264",e,t)}(r,7,3).length},h265:function(r){return function(r,e,t){return V(r,"h265",e,t)}(r,[32,33],3).length}},Z=Object.keys($).filter((function(r){return"ts"!==r&&"h264"!==r&&"h265"!==r})).concat(["ts","h264","h265"]);Z.forEach((function(r){var e=$[r];$[r]=function(r){return e(b(r))}}));var J=$,K=Object.freeze({__proto__:null,isLikely:J,detectContainerForBytes:function(r){r=b(r);for(var e=0;e<Z.length;e++){var t=Z[e];if(J[t](r))return t}return""},isLikelyFmp4MediaSegment:function(r){return B(r,["moof"]).length>0}});var Q=Object.freeze({__proto__:null,forEachMediaGroup:function(r,e,t){e.forEach((function(e){for(var n in r.mediaGroups[e])for(var o in r.mediaGroups[e][n]){var a=r.mediaGroups[e][n][o];t(a,e,n,o)}}))}});var W=function(r,e,t){return r(t={path:e,exports:{},require:function(r,e){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}(null==e&&t.path)}},t.exports),t.exports}((function(r,e){var t,n,o,a,i;t=/^((?:[a-zA-Z0-9+\-.]+:)?)(\/\/[^\/?#]*)?((?:[^\/?#]*\/)*[^;?#]*)?(;[^?#]*)?(\?[^#]*)?(#.*)?$/,n=/^([^\/?#]*)(.*)$/,o=/(?:\/|^)\.(?=\/)/g,a=/(?:\/|^)\.\.\/(?!\.\.\/)[^\/]*(?=\/)/g,i={buildAbsoluteURL:function(r,e,t){if(t=t||{},r=r.trim(),!(e=e.trim())){if(!t.alwaysNormalize)return r;var o=i.parseURL(r);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 t.alwaysNormalize?(a.path=i.normalizePath(a.path),i.buildURLFromParts(a)):e;var u=i.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:a.netLoc,path:null,params:a.params,query:a.query,fragment:a.fragment};if(!a.netLoc&&(s.netLoc=u.netLoc,"/"!==a.path[0]))if(a.path){var c=u.path,l=c.substring(0,c.lastIndexOf("/")+1)+a.path;s.path=i.normalizePath(l)}else s.path=u.path,a.params||(s.params=u.params,a.query||(s.query=u.query));return null===s.path&&(s.path=t.alwaysNormalize?i.normalizePath(a.path):a.path),i.buildURLFromParts(s)},parseURL:function(r){var e=t.exec(r);return e?{scheme:e[1]||"",netLoc:e[2]||"",path:e[3]||"",params:e[4]||"",query:e[5]||"",fragment:e[6]||""}:null},normalizePath:function(r){for(r=r.split("").reverse().join("").replace(o,"");r.length!==(r=r.replace(a,"")).length;);return r.split("").reverse().join("")},buildURLFromParts:function(r){return r.scheme+r.netLoc+r.path+r.params+r.query+r.fragment}},r.exports=i}));return{codecs:v,byteHelpers:k,containers:K,decodeB64ToUint8Array:function(r){for(var e,t=(e=r,o.default.atob?o.default.atob(e):Buffer.from(e,"base64").toString("binary")),n=new Uint8Array(t.length),a=0;a<t.length;a++)n[a]=t.charCodeAt(a);return n},mediaGroups:Q,resolveUrl:function(r,e){return/^[a-z]+:/i.test(e)?e:(/\/\//i.test(r)||(r=W.buildAbsoluteURL(o.default.location&&o.default.location.href||"",r)),W.buildAbsoluteURL(r,e))},Stream:function(){function r(){this.listeners={}}var e=r.prototype;return e.on=function(r,e){this.listeners[r]||(this.listeners[r]=[]),this.listeners[r].push(e)},e.off=function(r,e){if(!this.listeners[r])return!1;var t=this.listeners[r].indexOf(e);return this.listeners[r]=this.listeners[r].slice(0),this.listeners[r].splice(t,1),t>-1},e.trigger=function(r){var e=this.listeners[r];if(e)if(2===arguments.length)for(var t=e.length,n=0;n<t;++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(r){this.on("data",(function(e){r.push(e)}))},r}()}}));
|