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