image-to-pixel 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/dist/editor.css +1 -1
- package/dist/editor.css.map +2 -2
- package/dist/editor.esm.js +54 -54
- package/dist/editor.esm.js.map +4 -4
- package/dist/image-to-pixel.bundle.esm.js +13 -13
- package/dist/image-to-pixel.bundle.esm.js.map +3 -3
- package/dist/image-to-pixel.cjs +13 -9
- package/dist/image-to-pixel.cjs.map +2 -2
- package/dist/image-to-pixel.esm.js +13 -9
- package/dist/image-to-pixel.esm.js.map +2 -2
- package/dist/image-to-pixel.js +13 -9
- package/dist/image-to-pixel.js.map +2 -2
- package/dist/image-to-pixel.min.js +11 -11
- package/dist/image-to-pixel.min.js.map +3 -3
- package/dist/video.esm.js +2 -2
- package/dist/video.esm.js.map +3 -3
- package/lib/core/video/frames.js +189 -182
- package/lib/editor/exporter.js +267 -225
- package/lib/editor/index.js +1051 -1002
- package/lib/editor/inspector.js +379 -343
- package/lib/editor/storyboard.js +566 -0
- package/lib/editor/styles/editor.css +455 -418
- package/lib/editor/timeline-preview.js +142 -0
- package/package.json +1 -1
- package/types/index.d.ts +2 -0
package/dist/video.esm.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/*! image-to-pixel v3.
|
|
2
|
-
var ae={aac:"mp4a.40.2",opus:"opus"},ie=48e3;async function Oe(e){if(e instanceof ArrayBuffer)return e.slice(0);if(ArrayBuffer.isView(e))return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength);if(typeof Blob<"u"&&e instanceof Blob)return e.arrayBuffer();let t=typeof e=="string"?e:e&&(e.currentSrc||e.src);if(!t)throw new TypeError("decodeAudio() takes a File, Blob, ArrayBuffer, URL or <video>/<audio> element");let n=await fetch(t);if(!n.ok)throw new Error(`Couldn't load the audio: ${n.status} ${n.statusText}`);return n.arrayBuffer()}async function Ie(e,t={}){if(typeof OfflineAudioContext>"u")return null;let n=t.sampleRate||ie,r=await Oe(e),o;try{o=await new OfflineAudioContext(1,1,n).decodeAudioData(r)}catch{return null}let s=Math.max(0,t.start||0),a=Math.min(o.duration,t.end>s?t.end:o.duration),l=Math.floor((a-s)*n);if(l<1)return null;let h=Math.min(t.channels||2,o.numberOfChannels),c=new OfflineAudioContext(h,l,n),i=c.createBufferSource();i.buffer=o,i.connect(c.destination),i.start(0,s,l/n);let u=await c.startRendering(),m=[];for(let b=0;b<h;b++)m.push(u.getChannelData(b));return{sampleRate:n,numberOfChannels:h,channels:m,duration:l/n}}async function ce(e,t=ie,n=2,r=128e3){if(typeof AudioEncoder>"u"||!ae[e])return null;let o=e==="opus"?[48e3]:[...new Set([t,48e3,44100])],s=[...new Set([r,128e3,192e3,16e4,96e3,0])];for(let a of o)for(let l of s){let h={codec:ae[e],sampleRate:a,numberOfChannels:n,...l?{bitrate:l}:{},...e==="aac"?{aac:{format:"aac"}}:{}};if((await AudioEncoder.isConfigSupported(h).catch(()=>({}))).supported)return h}return null}async function ue(e,t=ie,n=2){return!!await ce(e,t,n)}async function ve(e,t){if(e.sampleRate===t)return e;let{numberOfChannels:n,channels:r}=e,o=Math.max(1,Math.round(r[0].length*t/e.sampleRate)),s=new OfflineAudioContext(n,o,t),a=s.createBuffer(n,r[0].length,e.sampleRate);for(let i=0;i<n;i++)a.copyToChannel(r[i],i);let l=s.createBufferSource();l.buffer=a,l.connect(s.destination),l.start();let h=await s.startRendering(),c=[];for(let i=0;i<n;i++)c.push(h.getChannelData(i));return{sampleRate:t,numberOfChannels:n,channels:c,duration:o/t}}async function ye(e,t){if(typeof AudioEncoder>"u")throw new Error("This browser has no WebCodecs AudioEncoder");let n=t.config||{codec:ae[t.codec],sampleRate:e.sampleRate,numberOfChannels:e.numberOfChannels,bitrate:t.bitrate||128e3,...t.codec==="aac"?{aac:{format:"aac"}}:{}};n.sampleRate!==e.sampleRate&&(e=await ve(e,n.sampleRate));let{sampleRate:r,numberOfChannels:o,channels:s}=e,a=null,l=new AudioEncoder({output:(u,m)=>t.onChunk(u,m),error:u=>{a=u}});l.configure(n);let h=Number.isFinite(t.maxDuration)?Math.floor(t.maxDuration*r):1/0,c=Math.min(s[0].length,h),i=Math.round(r/10);for(let u=0;u<c&&!a;u+=i){let m=Math.min(i,c-u),b=new Float32Array(m*o);for(let w=0;w<o;w++)b.set(s[w].subarray(u,u+m),w*m);let g=new AudioData({format:"f32-planar",sampleRate:r,numberOfFrames:m,numberOfChannels:o,timestamp:Math.round(u*1e6/r),data:b});l.encode(g),g.close(),l.encodeQueueSize>16&&await new Promise(w=>setTimeout(w,0))}if(a||await l.flush().catch(u=>{a=u}),l.state!=="closed"&&l.close(),a)throw a}function xe(e,t,n=312){let r=new Uint8Array(19);r.set([79,112,117,115,72,101,97,100]);let o=new DataView(r.buffer);return o.setUint8(8,1),o.setUint8(9,e),o.setUint16(10,n,!0),o.setUint32(12,t,!0),o.setInt16(16,0,!0),o.setUint8(18,0),r}function K(e){if(!e||e.length<19||String.fromCharCode(...e.subarray(0,8))!=="OpusHead")return null;let t=new DataView(e.buffer,e.byteOffset,e.byteLength);return{channels:t.getUint8(9),preSkip:t.getUint16(10,!0),sampleRate:t.getUint32(12,!0)}}var de=new TextEncoder,ee=class{constructor(){this.parts=[],this.size=0}u8(t){return this.bytes([t&255])}u16(t){return this.bytes([t>>>8&255,t&255])}u32(t){return this.bytes([t>>>24&255,t>>>16&255,t>>>8&255,t&255])}str(t){return this.bytes(de.encode(t))}bytes(t){let n=t instanceof Uint8Array?t:Uint8Array.from(t);return this.parts.push(n),this.size+=n.length,this}zeros(t){return this.bytes(new Uint8Array(t))}out(){let t=new Uint8Array(this.size),n=0;for(let r of this.parts)t.set(r,n),n+=r.length;return t}};function B(e,t){let n=new ee;t(n);let r=n.out(),o=new ee().u32(r.length+8).str(e).out(),s=new Uint8Array(r.length+8);return s.set(o),s.set(r,8),s}var A=(e,t,n,r)=>B(e,o=>{o.u8(t).u8(n>>16).u8(n>>8).u8(n),r(o)}),fe=[65536,0,0,0,65536,0,0,0,1073741824],ge=e=>new Uint8Array(e instanceof ArrayBuffer?e:e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)),Z=(e,t)=>[e,t.length,...t],we=e=>[e>>>24&255,e>>>16&255,e>>>8&255,e&255];function Re(e,t){let n=Z(5,[...e]),r=Z(4,[64,21,0,0,0,...we(t),...we(t),...n]),o=Z(6,[2]);return A("esds",0,0,s=>s.bytes(Z(3,[0,2,0,...r,...o])))}var Fe=new Set([100,110,122,244,44,83,86,118,128,138,139,134,135]);function Le(e){if(!e||e.length<7||e[0]!==1)return!1;let t=5,n=e[t++]&31;if(!n)return!1;for(let o=0;o<n;o++){if(t+2>e.length)return!1;let s=e[t]<<8|e[t+1];if(t+=2,s<4||t+s>e.length)return!1;let a=e.subarray(t,t+s);if((a[0]&31)!==7||o===0&&(a[1]!==e[1]||a[2]!==e[2]||a[3]!==e[3]))return!1;t+=s}if(t>=e.length)return!1;let r=e[t++];if(!r)return!1;for(let o=0;o<r;o++){if(t+2>e.length)return!1;let s=e[t]<<8|e[t+1];if(t+=2,s<1||t+s>e.length||(e[t]&31)!==8)return!1;t+=s}return!0}function ze(e){let t=[],n=new DataView(e.buffer,e.byteOffset,e.byteLength);for(let r=0;r+4<=e.length;){let o=n.getUint32(r);if(r+=4,!o||r+o>e.length)break;t.push(e.subarray(r,r+o)),r+=o}return t}function He(e){let t=[];for(let a=1;a<e.length;a++)a>=3&&e[a]===3&&e[a-1]===0&&e[a-2]===0||t.push(e[a]);let n=24,r=()=>{let a=t[n>>3]>>7-(n&7)&1;return n++,a},o=()=>{let a=0;for(;n<t.length*8&&!r();)a++;let l=0;for(let h=0;h<a;h++)l=l*2+r();return 2**a-1+l};o();let s=o();return s===3&&r(),{chroma:s,lumaBits:o(),chromaBits:o()}}function _e(e){let t=ze(e),n=t.filter(s=>(s[0]&31)===7),r=t.filter(s=>(s[0]&31)===8);if(!n.length||!r.length||n[0].length<4)return null;let o=[1,n[0][1],n[0][2],n[0][3],255,224|n.length];for(let s of n)o.push(s.length>>8,s.length&255,...s);o.push(r.length);for(let s of r)o.push(s.length>>8,s.length&255,...s);if(Fe.has(n[0][1])){let s=He(n[0]);o.push(252|s.chroma&3,248|s.lumaBits&7,248|s.chromaBits&7,0)}return Uint8Array.from(o)}function le(e){let t=[];for(let n of e){let r=t[t.length-1];r&&r[1]===n?r[0]++:t.push([1,n])}return t}function Ce({width:e,height:t,timescale:n=9e4,audio:r=null}){let o=[],s=[],a=null,l=null;function h(){if(!r||!s.length)return null;let c=r.sampleRate,i=r.numberOfChannels,u=s[0].ts,m=s.map(C=>Math.round((C.ts-u)*c/1e6)),b=Math.round(s[s.length-1].dur*c/1e6)||(r.codec==="aac"?1024:960),g=m.map((C,M)=>M+1<m.length?Math.max(1,m[M+1]-C):b),w;if(r.codec==="aac"){if(!l)throw new Error("The audio encoder gave no AAC decoder configuration (AudioSpecificConfig)");w=B("mp4a",C=>{C.zeros(6).u16(1).zeros(8).u16(i).u16(16).u16(0).u16(0).u32(c<=65535?c*65536:0),C.bytes(Re(l,r.bitrate||128e3))})}else{let C=K(l),M=C?C.preSkip:312;w=B("Opus",I=>{I.zeros(6).u16(1).zeros(8).u16(i).u16(16).u16(0).u16(0).u32(48e3*65536),I.bytes(B("dOps",F=>F.u8(0).u8(i).u16(M).u32(c).u16(0).u8(0)))})}return{rate:c,durs:g,entry:w,mediaDur:g.reduce((C,M)=>C+M,0)}}return{addChunk(c,i){i&&i.decoderConfig&&i.decoderConfig.description&&!a&&(a=ge(i.decoderConfig.description));let u=new Uint8Array(c.byteLength);c.copyTo(u),o.push({data:u,key:c.type==="key",ts:c.timestamp,dur:c.duration||0})},addSample(c,{key:i,timestamp:u,duration:m}){o.push({data:c,key:!!i,ts:u,dur:m||0})},setDescription(c){a=c},addAudioChunk(c,i){i&&i.decoderConfig&&i.decoderConfig.description&&!l&&(l=ge(i.decoderConfig.description));let u=new Uint8Array(c.byteLength);c.copyTo(u),s.push({data:u,ts:c.timestamp,dur:c.duration||0})},addAudioSample(c,{timestamp:i,duration:u}){s.push({data:c,ts:i,dur:u||0})},setAudioDescription(c){l=c},finish(){if(!o.length)throw new Error("No video frames were encoded");if(!Le(a)){let d=o.find(y=>y.key)||o[0];a=_e(d.data)||a}if(!a)throw new Error("The encoder gave no H.264 decoder configuration (avcC)");let c=d=>Math.round(d*n/1e6),i=o.map(d=>c(d.ts)),u=i.slice().sort((d,y)=>d-y),m=c(o[o.length-1].dur)||(u.length>1?u[u.length-1]-u[u.length-2]:c(33333)),b=u.map((d,y)=>y+1<u.length?u[y+1]-d:m),g=i.map((d,y)=>d-u[y]),w=g.some(d=>d!==0),C=b.reduce((d,y)=>d+y,0),M=Math.round(C*1e3/n),I=le(b),F=le(g),S=o.map((d,y)=>d.key?y+1:0).filter(Boolean),U=o.reduce((d,y)=>d+y.data.length,0),k=h(),H=k?Math.round(k.mediaDur*1e3/k.rate):0,W=k?s.reduce((d,y)=>d+y.data.length,0):0,ne=Math.max(M,H),j=U+W,N=B("ftyp",d=>d.str("isom").u32(512).str("isom").str("iso2").str("avc1").str("mp41")),X=()=>B("dinf",d=>d.bytes(A("dref",0,0,y=>{y.u32(1),y.bytes(A("url ",0,1,()=>{}))}))),p=d=>A("stsc",0,0,y=>y.u32(1).u32(1).u32(d).u32(1)),E=d=>A("stsz",0,0,y=>{y.u32(0).u32(d.length);for(let R of d)y.u32(R.data.length)}),q=d=>A("stco",0,0,y=>y.u32(1).u32(d)),L=d=>B("moov",y=>{y.bytes(A("mvhd",0,0,R=>{R.u32(0).u32(0).u32(1e3).u32(ne).u32(65536).u16(256).zeros(10),fe.forEach(D=>R.u32(D)),R.zeros(24).u32(k?3:2)})),y.bytes(B("trak",R=>{R.bytes(A("tkhd",0,3,D=>{D.u32(0).u32(0).u32(1).u32(0).u32(M).zeros(8).u16(0).u16(0).u16(0).u16(0),fe.forEach(T=>D.u32(T)),D.u32(e<<16).u32(t<<16)})),R.bytes(B("mdia",D=>{D.bytes(A("mdhd",0,0,T=>T.u32(0).u32(0).u32(n).u32(C).u16(21956).u16(0))),D.bytes(A("hdlr",0,0,T=>T.u32(0).str("vide").zeros(12).str("VideoHandler").u8(0))),D.bytes(B("minf",T=>{T.bytes(A("vmhd",0,1,V=>V.u16(0).u16(0).u16(0).u16(0))),T.bytes(X()),T.bytes(B("stbl",V=>{V.bytes(A("stsd",0,0,v=>{v.u32(1),v.bytes(B("avc1",O=>{O.zeros(6).u16(1).zeros(16).u16(e).u16(t).u32(4718592).u32(4718592).u32(0).u16(1);let z=de.encode("image-to-pixel").slice(0,31);O.u8(z.length).bytes(z).zeros(31-z.length).u16(24).u16(65535),O.bytes(B("avcC",se=>se.bytes(a)))}))})),V.bytes(A("stts",0,0,v=>{v.u32(I.length);for(let[O,z]of I)v.u32(O).u32(z)})),w&&V.bytes(A("ctts",1,0,v=>{v.u32(F.length);for(let[O,z]of F)v.u32(O).u32(z>>>0)})),S.length<o.length&&V.bytes(A("stss",0,0,v=>{v.u32(S.length);for(let O of S)v.u32(O)})),V.bytes(p(o.length)),V.bytes(E(o)),V.bytes(q(d))}))}))}))})),k&&y.bytes(B("trak",R=>{R.bytes(A("tkhd",0,3,D=>{D.u32(0).u32(0).u32(2).u32(0).u32(H).zeros(8).u16(0).u16(1).u16(256).u16(0),fe.forEach(T=>D.u32(T)),D.u32(0).u32(0)})),R.bytes(B("mdia",D=>{D.bytes(A("mdhd",0,0,T=>T.u32(0).u32(0).u32(k.rate).u32(k.mediaDur).u16(21956).u16(0))),D.bytes(A("hdlr",0,0,T=>T.u32(0).str("soun").zeros(12).str("SoundHandler").u8(0))),D.bytes(B("minf",T=>{T.bytes(A("smhd",0,0,V=>V.u16(0).u16(0))),T.bytes(X()),T.bytes(B("stbl",V=>{V.bytes(A("stsd",0,0,O=>{O.u32(1),O.bytes(k.entry)}));let v=le(k.durs);V.bytes(A("stts",0,0,O=>{O.u32(v.length);for(let[z,se]of v)O.u32(z).u32(se)})),V.bytes(p(s.length)),V.bytes(E(s)),V.bytes(q(d+U))}))}))}))}))}),oe=L(0).length,Y=N.length+oe+8;if(Y+j>4294967295)throw new Error("Video is larger than 4 GB");let re=L(Y),G=new Uint8Array(Y+j);G.set(N,0),G.set(re,N.length),new DataView(G.buffer).setUint32(N.length+re.length,j+8),G.set(de.encode("mdat"),N.length+re.length+4);let J=Y;for(let d of o)G.set(d.data,J),J+=d.data.length;if(k)for(let d of s)G.set(d.data,J),J+=d.data.length;return G}}}var Ne=new TextEncoder;function qe(e){let t=new Uint8Array(8);t[0]=1;let n=e;for(let r=7;r>=1;r--)t[r]=n%256,n=Math.floor(n/256);return t}function Me(e){let t=[];for(let n=e;n>0;n=Math.floor(n/256))t.unshift(n%256);return t}function te(e){let t=0;for(let o of e)t+=o.length;let n=new Uint8Array(t),r=0;for(let o of e)n.set(o,r),r+=o.length;return n}var _=(e,t)=>te([Uint8Array.from(Me(e)),qe(t.length),t]),x=(e,t,n=0)=>{let r=[];for(let o=t;o>0||r.length===0;o=Math.floor(o/256))r.unshift(o%256);for(;r.length<n;)r.unshift(0);return _(e,Uint8Array.from(r))},Q=(e,t)=>_(e,Ne.encode(t)),be=(e,t)=>{let n=new Uint8Array(8);return new DataView(n.buffer).setFloat64(0,t),_(e,n)},P=(e,t)=>_(e,te(t)),f={EBML:440786851,EBMLVersion:17030,EBMLReadVersion:17143,EBMLMaxIDLength:17138,EBMLMaxSizeLength:17139,DocType:17026,DocTypeVersion:17031,DocTypeReadVersion:17029,Segment:408125543,SeekHead:290298740,Seek:19899,SeekID:21419,SeekPosition:21420,Info:357149030,TimestampScale:2807729,Duration:17545,MuxingApp:19840,WritingApp:22337,Tracks:374648427,TrackEntry:174,TrackNumber:215,TrackUID:29637,TrackType:131,FlagLacing:156,CodecID:134,CodecPrivate:25506,DefaultDuration:2352003,Video:224,PixelWidth:176,PixelHeight:186,CodecDelay:22186,SeekPreRoll:22203,Audio:225,SamplingFrequency:181,Channels:159,Cluster:524531317,Timestamp:231,SimpleBlock:163,Cues:475249515,CuePoint:187,CueTime:179,CueTrackPositions:183,CueTrack:247,CueClusterPosition:241},Ge={vp8:"V_VP8",vp9:"V_VP9",av1:"V_AV1"},$=1,he=2,Ae=e=>new Uint8Array(e instanceof ArrayBuffer?e:e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength));function Ee({width:e,height:t,codec:n="vp9",fps:r=0,audio:o=null}){if(o&&o.codec&&o.codec!=="opus")throw new Error(`WebM audio must be Opus, not ${o.codec}`);let s=[],a=[],l=null,h=null;return{addChunk(c,i){i&&i.decoderConfig&&i.decoderConfig.description&&!l&&(l=Ae(i.decoderConfig.description));let u=new Uint8Array(c.byteLength);c.copyTo(u),s.push({data:u,key:c.type==="key",ms:c.timestamp/1e3})},addFrame(c,{key:i,timestamp:u}){s.push({data:c,key:!!i,ms:u/1e3})},addAudioChunk(c,i){i&&i.decoderConfig&&i.decoderConfig.description&&!h&&(h=Ae(i.decoderConfig.description));let u=new Uint8Array(c.byteLength);c.copyTo(u),a.push({data:u,ms:c.timestamp/1e3,dms:(c.duration||0)/1e3})},addAudioFrame(c,{timestamp:i,duration:u}){a.push({data:c,ms:i/1e3,dms:(u||0)/1e3})},finish(){if(!s.length)throw new Error("No video frames were encoded");let c=!!(o&&a.length),i=r?1e3/r:s.length>1?(s[s.length-1].ms-s[0].ms)/(s.length-1):33.333,u=s[s.length-1].ms+i;if(c){let p=a[a.length-1];u=Math.max(u,p.ms+(p.dms||20))}let m=P(f.Info,[x(f.TimestampScale,1e6),be(f.Duration,u),Q(f.MuxingApp,"image-to-pixel"),Q(f.WritingApp,"image-to-pixel")]),b=[P(f.TrackEntry,[x(f.TrackNumber,$),x(f.TrackUID,$),x(f.TrackType,1),x(f.FlagLacing,0),Q(f.CodecID,Ge[n]||n),...l?[_(f.CodecPrivate,l)]:[],x(f.DefaultDuration,Math.round(i*1e6)),P(f.Video,[x(f.PixelWidth,e),x(f.PixelHeight,t)])])];if(c){let p=K(h)?h:xe(o.numberOfChannels,o.sampleRate),E=K(p).preSkip;b.push(P(f.TrackEntry,[x(f.TrackNumber,he),x(f.TrackUID,he),x(f.TrackType,2),x(f.FlagLacing,0),Q(f.CodecID,"A_OPUS"),_(f.CodecPrivate,p),x(f.CodecDelay,Math.round(E*1e9/48e3)),x(f.SeekPreRoll,8e7),P(f.Audio,[be(f.SamplingFrequency,o.sampleRate),x(f.Channels,o.numberOfChannels)])]))}let g=P(f.Tracks,b),w=s.map((p,E)=>({track:$,data:p.data,key:p.key,ms:p.ms,order:E}));c&&a.forEach((p,E)=>w.push({track:he,data:p.data,key:!0,ms:p.ms,order:s.length+E})),c&&w.sort((p,E)=>p.ms-E.ms||p.track-E.track||p.order-E.order);let C=[],M=null;for(let p of w){let E=Math.round(p.ms),q=p.track===$&&p.key;(!M||q||E-M.start>3e4)&&(M={start:E,key:q,blocks:[]},C.push(M));let L=new Uint8Array(4);L[0]=128|p.track,new DataView(L.buffer).setInt16(1,E-M.start),L[3]=p.key?128:0,M.blocks.push(_(f.SimpleBlock,te([L,p.data])))}let I=C.map(p=>P(f.Cluster,[x(f.Timestamp,p.start),...p.blocks])),F=(p,E,q)=>P(f.SeekHead,[[f.Info,p],[f.Tracks,E],[f.Cues,q]].map(([L,oe])=>P(f.Seek,[_(f.SeekID,Uint8Array.from(Me(L))),x(f.SeekPosition,oe,8)]))),U=F(0,0,0).length,k=U+m.length,H=k+g.length,W=[];C.forEach((p,E)=>{p.key&&W.push(P(f.CuePoint,[x(f.CueTime,p.start),P(f.CueTrackPositions,[x(f.CueTrack,$),x(f.CueClusterPosition,H)])])),H+=I[E].length});let ne=P(f.Cues,W.length?W:[P(f.CuePoint,[x(f.CueTime,0),P(f.CueTrackPositions,[x(f.CueTrack,$),x(f.CueClusterPosition,k+g.length)])])]),j=H,N=P(f.EBML,[x(f.EBMLVersion,1),x(f.EBMLReadVersion,1),x(f.EBMLMaxIDLength,4),x(f.EBMLMaxSizeLength,8),Q(f.DocType,"webm"),x(f.DocTypeVersion,4),x(f.DocTypeReadVersion,2)]),X=P(f.Segment,[F(U,k,j),m,g,...I,ne]);return te([N,X])}}}function ke(e){if(Array.isArray(e))return[e[0]|0,e[1]|0,e[2]|0];let t=String(e).trim().replace(/^#|^0x/i,"");if((t.length===3||t.length===4)&&(t=t.slice(0,3).split("").map(n=>n+n).join("")),!/^[0-9a-f]{6}([0-9a-f]{2})?$/i.test(t))throw new Error(`Invalid colour "${e}"`);return[parseInt(t.slice(0,2),16),parseInt(t.slice(2,4),16),parseInt(t.slice(4,6),16)]}function Te(e,t=1,n={}){t=Math.max(1,Math.round(t));let r=e.width*t,o=e.height*t,s=new Uint8ClampedArray(r*o*4),a=e.data,l=e.width;for(let h=0;h<o;h++){let c=h/t|0;for(let i=0;i<r;i++){let u=(c*l+(i/t|0))*4,m=(h*r+i)*4;s[m]=a[u],s[m+1]=a[u+1],s[m+2]=a[u+2],s[m+3]=a[u+3]}}if(n.grid&&t>=3){let[h,c,i]=ke(n.gridColor||"#000000"),u=n.gridAlpha??.35;for(let m=0;m<o;m++)for(let b=0;b<r;b++){if(b%t&&m%t)continue;let g=(m*r+b)*4,w=s[g+3]/255,C=u+w*(1-u);s[g]=(h*u+s[g]*w*(1-u))/C,s[g+1]=(c*u+s[g+1]*w*(1-u))/C,s[g+2]=(i*u+s[g+2]*w*(1-u))/C,s[g+3]=C*255}}return{width:r,height:o,data:s}}var $e=(e,t,n=4e3)=>new Promise((r,o)=>{let s=setTimeout(()=>{e.removeEventListener(t,a),o(new Error(`Timed out waiting for "${t}"`))},n),a=()=>{clearTimeout(s),r()};e.addEventListener(t,a,{once:!0})}),We={1:"loading was aborted",2:"a network error stopped the download",3:"the browser could not decode it",4:"this browser can't play this video's format"};function De(e){let t=e.error?e.error.code:0,n=new Error(We[t]||"the video could not be loaded");return n.code=t,e.error&&e.error.message&&(n.detail=e.error.message),n}var Se=(e,t,n)=>new Promise((r,o)=>{let s=()=>{clearTimeout(h),e.removeEventListener(t,a),e.removeEventListener("error",l)},a=()=>{s(),r()},l=()=>{s(),o(De(e))},h=setTimeout(()=>{s(),o(new Error(`no response from the video decoder after ${n/1e3} s`))},n);e.addEventListener(t,a,{once:!0}),e.addEventListener("error",l,{once:!0})});async function pe(e,{timeout:t=6e4}={}){if(e.error)throw De(e);return e.readyState<1&&await Se(e,"loadedmetadata",t),e.readyState<2&&await Se(e,"loadeddata",t).catch(n=>{if(n.code)throw n}),e}async function me(e,t){if(t=Math.max(0,Math.min(t,(e.duration||0)-1e-4)),Math.abs(e.currentTime-t)<1e-6&&e.readyState>=2)return;let n="requestVideoFrameCallback"in e?new Promise(o=>{e.requestVideoFrameCallback(()=>o()),setTimeout(o,500)}):null,r=$e(e,"seeked");e.currentTime=t,await r,n&&await n}async function je(e,t=30){if(!("requestVideoFrameCallback"in e))return t;await pe(e);let n={t:e.currentTime,muted:e.muted,paused:e.paused},r=[];try{e.muted=!0,await new Promise(a=>{let l=setTimeout(a,1200),h=(c,i)=>{r.push(i.mediaTime),r.length>=12?(clearTimeout(l),a()):e.requestVideoFrameCallback(h)};e.requestVideoFrameCallback(h),e.play().catch(()=>{clearTimeout(l),a()})})}finally{e.pause(),e.muted=n.muted,await me(e,n.t).catch(()=>{})}let o=[];for(let a=1;a<r.length;a++){let l=r[a]-r[a-1];l>.001&&o.push(l)}if(o.length<3)return t;o.sort((a,l)=>a-l);let s=1/o[Math.floor(o.length/2)];for(let a of[12,15,23.976,24,25,29.97,30,48,50,59.94,60])if(Math.abs(s-a)<.6)return a;return Math.round(s)}function Ve({duration:e,fps:t,start:n=0,end:r=e,maxFrames:o=1/0}){let s=[],a=Math.min(o,Math.max(1,Math.floor((r-n)*t+1e-6)));for(let l=0;l<a;l++)s.push(n+(l+.5)/t);return s}async function Ke(e,t){await pe(e);let n=e.paused;e.pause();let r=Ve({duration:e.duration,...t});for(let o=0;o<r.length;o++){if(t.signal&&t.signal.aborted)throw new DOMException("Aborted","AbortError");await me(e,r[o]),await t.onFrame({index:o,time:r[o],total:r.length})}return n||e.play().catch(()=>{}),r.length}var Pe=["avc1.640033","avc1.4d0033","avc1.42003e","avc1.640028","avc1.42001f"],Ue=[["vp09.00.10.08","vp9"],["vp8","vp8"]];async function dt(e=640,t=480){let n={mp4:!1,webm:!1,webcodecs:typeof VideoEncoder<"u",aac:!1,opus:!1};if(n.webcodecs){for(let r of Pe)if((await VideoEncoder.isConfigSupported({codec:r,width:e,height:t}).catch(()=>({}))).supported){n.mp4=!0;break}for(let[r]of Ue)if((await VideoEncoder.isConfigSupported({codec:r,width:e,height:t}).catch(()=>({}))).supported){n.webm=!0;break}}return n.aac=await ue("aac"),n.opus=await ue("opus"),n}var Be=e=>e+(e&1);async function Qe(e,t,n){let r=e==="mp4"?["aac","opus"]:["opus"];for(let o of r){let s=await ce(o,t.sampleRate,t.numberOfChannels,n);if(s)return{codec:o,config:s}}return null}async function Xe(e){let t=Math.max(1,Math.round(e.scale||1)),n=Be(e.width*t),r=Be(e.height*t),o=e.fps||30,s=e.bitrate||Math.min(2e7,Math.max(2e6,n*r*o*.15));if(typeof VideoEncoder>"u")throw new Error("This browser has no WebCodecs VideoEncoder. Export a GIF or PNG sequence instead.");let a=null,l=null,h=e.format==="mp4"?Pe.map(S=>[S,"avc"]):Ue;for(let[S,U]of h)if((await VideoEncoder.isConfigSupported({codec:S,width:n,height:r,bitrate:s,framerate:o}).catch(()=>({}))).supported){a=S,l=U;break}if(!a)throw new Error(`No ${e.format.toUpperCase()} encoder available in this browser at ${n}\xD7${r}.`);let c=e.audio&&e.audio.channels&&e.audio.channels.length?e.audio:null,i=c?await Qe(e.format,c,e.audioBitrate||128e3):null,u=i?i.codec:null,m=i?{codec:u,sampleRate:i.config.sampleRate,numberOfChannels:i.config.numberOfChannels,bitrate:i.config.bitrate||128e3}:null,b=e.format==="mp4"?Ce({width:n,height:r,audio:m}):Ee({width:n,height:r,codec:l,fps:o,audio:m}),g=null,w=new VideoEncoder({output:(S,U)=>b.addChunk(S,U),error:S=>{g=S}});w.configure({codec:a,width:n,height:r,bitrate:s,framerate:o,latencyMode:"quality",...e.format==="mp4"?{avc:{format:"avc"}}:{}});let C=new OffscreenCanvas(n,r),M=C.getContext("2d"),I=0,F=1e6/o;return{width:n,height:r,codec:a,audioCodec:u,async add(S){if(g)throw g;let U=t>1?Te(S,t):S;M.fillStyle="#000",M.fillRect(0,0,n,r),M.putImageData(new ImageData(new Uint8ClampedArray(U.data),U.width,U.height),0,0);let k=new VideoFrame(C,{timestamp:Math.round(I*F),duration:Math.round(F)});w.encode(k,{keyFrame:I%Math.max(1,Math.round(o*2))===0}),k.close(),I++,w.encodeQueueSize>8&&await new Promise(H=>setTimeout(H,0))},async finish(){if(await w.flush(),g)throw g;return w.close(),u&&I>0&&await ye(c,{config:i.config,maxDuration:I/o,onChunk:(S,U)=>b.addAudioChunk(S,U)}),new Blob([b.finish()],{type:e.format==="mp4"?"video/mp4":"video/webm"})}}}async function ht(e,t){let n=await Xe({...t,width:e[0].width,height:e[0].height});for(let r of e)await n.add(r);return n.finish()}export{ue as audioCodecSupported,Ce as createMp4Muxer,Xe as createVideoEncoder,Ee as createWebmMuxer,Ie as decodeAudio,ye as encodeAudio,ht as encodeVideo,je as estimateFps,ce as findAudioConfig,Ke as forEachFrame,Ve as frameTimes,me as seekTo,pe as videoReady,dt as videoSupport};
|
|
1
|
+
/*! image-to-pixel v3.2.0 | MIT | https://github.com/Tezumie/Image-to-Pixel */
|
|
2
|
+
var se={aac:"mp4a.40.2",opus:"opus"},ie=48e3;async function Oe(e){if(e instanceof ArrayBuffer)return e.slice(0);if(ArrayBuffer.isView(e))return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength);if(typeof Blob<"u"&&e instanceof Blob)return e.arrayBuffer();let t=typeof e=="string"?e:e&&(e.currentSrc||e.src);if(!t)throw new TypeError("decodeAudio() takes a File, Blob, ArrayBuffer, URL or <video>/<audio> element");let n=await fetch(t);if(!n.ok)throw new Error(`Couldn't load the audio: ${n.status} ${n.statusText}`);return n.arrayBuffer()}async function Ie(e,t={}){if(typeof OfflineAudioContext>"u")return null;let n=t.sampleRate||ie,o=await Oe(e),r;try{r=await new OfflineAudioContext(1,1,n).decodeAudioData(o)}catch{return null}let a=Math.max(0,t.start||0),s=Math.min(r.duration,t.end>a?t.end:r.duration),l=Math.floor((s-a)*n);if(l<1)return null;let h=Math.min(t.channels||2,r.numberOfChannels),c=new OfflineAudioContext(h,l,n),i=c.createBufferSource();i.buffer=r,i.connect(c.destination),i.start(0,a,l/n);let u=await c.startRendering(),m=[];for(let C=0;C<h;C++)m.push(u.getChannelData(C));return{sampleRate:n,numberOfChannels:h,channels:m,duration:l/n}}async function ce(e,t=ie,n=2,o=128e3){if(typeof AudioEncoder>"u"||!se[e])return null;let r=e==="opus"?[48e3]:[...new Set([t,48e3,44100])],a=[...new Set([o,128e3,192e3,16e4,96e3,0])];for(let s of r)for(let l of a){let h={codec:se[e],sampleRate:s,numberOfChannels:n,...l?{bitrate:l}:{},...e==="aac"?{aac:{format:"aac"}}:{}};if((await AudioEncoder.isConfigSupported(h).catch(()=>({}))).supported)return h}return null}async function ue(e,t=ie,n=2){return!!await ce(e,t,n)}async function ve(e,t){if(e.sampleRate===t)return e;let{numberOfChannels:n,channels:o}=e,r=Math.max(1,Math.round(o[0].length*t/e.sampleRate)),a=new OfflineAudioContext(n,r,t),s=a.createBuffer(n,o[0].length,e.sampleRate);for(let i=0;i<n;i++)s.copyToChannel(o[i],i);let l=a.createBufferSource();l.buffer=s,l.connect(a.destination),l.start();let h=await a.startRendering(),c=[];for(let i=0;i<n;i++)c.push(h.getChannelData(i));return{sampleRate:t,numberOfChannels:n,channels:c,duration:r/t}}async function ye(e,t){if(typeof AudioEncoder>"u")throw new Error("This browser has no WebCodecs AudioEncoder");let n=t.config||{codec:se[t.codec],sampleRate:e.sampleRate,numberOfChannels:e.numberOfChannels,bitrate:t.bitrate||128e3,...t.codec==="aac"?{aac:{format:"aac"}}:{}};n.sampleRate!==e.sampleRate&&(e=await ve(e,n.sampleRate));let{sampleRate:o,numberOfChannels:r,channels:a}=e,s=null,l=new AudioEncoder({output:(u,m)=>t.onChunk(u,m),error:u=>{s=u}});l.configure(n);let h=Number.isFinite(t.maxDuration)?Math.floor(t.maxDuration*o):1/0,c=Math.min(a[0].length,h),i=Math.round(o/10);for(let u=0;u<c&&!s;u+=i){let m=Math.min(i,c-u),C=new Float32Array(m*r);for(let w=0;w<r;w++)C.set(a[w].subarray(u,u+m),w*m);let g=new AudioData({format:"f32-planar",sampleRate:o,numberOfFrames:m,numberOfChannels:r,timestamp:Math.round(u*1e6/o),data:C});l.encode(g),g.close(),l.encodeQueueSize>16&&await new Promise(w=>setTimeout(w,0))}if(s||await l.flush().catch(u=>{s=u}),l.state!=="closed"&&l.close(),s)throw s}function xe(e,t,n=312){let o=new Uint8Array(19);o.set([79,112,117,115,72,101,97,100]);let r=new DataView(o.buffer);return r.setUint8(8,1),r.setUint8(9,e),r.setUint16(10,n,!0),r.setUint32(12,t,!0),r.setInt16(16,0,!0),r.setUint8(18,0),o}function K(e){if(!e||e.length<19||String.fromCharCode(...e.subarray(0,8))!=="OpusHead")return null;let t=new DataView(e.buffer,e.byteOffset,e.byteLength);return{channels:t.getUint8(9),preSkip:t.getUint16(10,!0),sampleRate:t.getUint32(12,!0)}}var de=new TextEncoder,ee=class{constructor(){this.parts=[],this.size=0}u8(t){return this.bytes([t&255])}u16(t){return this.bytes([t>>>8&255,t&255])}u32(t){return this.bytes([t>>>24&255,t>>>16&255,t>>>8&255,t&255])}str(t){return this.bytes(de.encode(t))}bytes(t){let n=t instanceof Uint8Array?t:Uint8Array.from(t);return this.parts.push(n),this.size+=n.length,this}zeros(t){return this.bytes(new Uint8Array(t))}out(){let t=new Uint8Array(this.size),n=0;for(let o of this.parts)t.set(o,n),n+=o.length;return t}};function B(e,t){let n=new ee;t(n);let o=n.out(),r=new ee().u32(o.length+8).str(e).out(),a=new Uint8Array(o.length+8);return a.set(r),a.set(o,8),a}var A=(e,t,n,o)=>B(e,r=>{r.u8(t).u8(n>>16).u8(n>>8).u8(n),o(r)}),fe=[65536,0,0,0,65536,0,0,0,1073741824],ge=e=>new Uint8Array(e instanceof ArrayBuffer?e:e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)),Z=(e,t)=>[e,t.length,...t],we=e=>[e>>>24&255,e>>>16&255,e>>>8&255,e&255];function Fe(e,t){let n=Z(5,[...e]),o=Z(4,[64,21,0,0,0,...we(t),...we(t),...n]),r=Z(6,[2]);return A("esds",0,0,a=>a.bytes(Z(3,[0,2,0,...o,...r])))}var Re=new Set([100,110,122,244,44,83,86,118,128,138,139,134,135]);function Le(e){if(!e||e.length<7||e[0]!==1)return!1;let t=5,n=e[t++]&31;if(!n)return!1;for(let r=0;r<n;r++){if(t+2>e.length)return!1;let a=e[t]<<8|e[t+1];if(t+=2,a<4||t+a>e.length)return!1;let s=e.subarray(t,t+a);if((s[0]&31)!==7||r===0&&(s[1]!==e[1]||s[2]!==e[2]||s[3]!==e[3]))return!1;t+=a}if(t>=e.length)return!1;let o=e[t++];if(!o)return!1;for(let r=0;r<o;r++){if(t+2>e.length)return!1;let a=e[t]<<8|e[t+1];if(t+=2,a<1||t+a>e.length||(e[t]&31)!==8)return!1;t+=a}return!0}function ze(e){let t=[],n=new DataView(e.buffer,e.byteOffset,e.byteLength);for(let o=0;o+4<=e.length;){let r=n.getUint32(o);if(o+=4,!r||o+r>e.length)break;t.push(e.subarray(o,o+r)),o+=r}return t}function He(e){let t=[];for(let s=1;s<e.length;s++)s>=3&&e[s]===3&&e[s-1]===0&&e[s-2]===0||t.push(e[s]);let n=24,o=()=>{let s=t[n>>3]>>7-(n&7)&1;return n++,s},r=()=>{let s=0;for(;n<t.length*8&&!o();)s++;let l=0;for(let h=0;h<s;h++)l=l*2+o();return 2**s-1+l};r();let a=r();return a===3&&o(),{chroma:a,lumaBits:r(),chromaBits:r()}}function Ne(e){let t=ze(e),n=t.filter(a=>(a[0]&31)===7),o=t.filter(a=>(a[0]&31)===8);if(!n.length||!o.length||n[0].length<4)return null;let r=[1,n[0][1],n[0][2],n[0][3],255,224|n.length];for(let a of n)r.push(a.length>>8,a.length&255,...a);r.push(o.length);for(let a of o)r.push(a.length>>8,a.length&255,...a);if(Re.has(n[0][1])){let a=He(n[0]);r.push(252|a.chroma&3,248|a.lumaBits&7,248|a.chromaBits&7,0)}return Uint8Array.from(r)}function le(e){let t=[];for(let n of e){let o=t[t.length-1];o&&o[1]===n?o[0]++:t.push([1,n])}return t}function be({width:e,height:t,timescale:n=9e4,audio:o=null}){let r=[],a=[],s=null,l=null;function h(){if(!o||!a.length)return null;let c=o.sampleRate,i=o.numberOfChannels,u=a[0].ts,m=a.map(b=>Math.round((b.ts-u)*c/1e6)),C=Math.round(a[a.length-1].dur*c/1e6)||(o.codec==="aac"?1024:960),g=m.map((b,M)=>M+1<m.length?Math.max(1,m[M+1]-b):C),w;if(o.codec==="aac"){if(!l)throw new Error("The audio encoder gave no AAC decoder configuration (AudioSpecificConfig)");w=B("mp4a",b=>{b.zeros(6).u16(1).zeros(8).u16(i).u16(16).u16(0).u16(0).u32(c<=65535?c*65536:0),b.bytes(Fe(l,o.bitrate||128e3))})}else{let b=K(l),M=b?b.preSkip:312;w=B("Opus",I=>{I.zeros(6).u16(1).zeros(8).u16(i).u16(16).u16(0).u16(0).u32(48e3*65536),I.bytes(B("dOps",R=>R.u8(0).u8(i).u16(M).u32(c).u16(0).u8(0)))})}return{rate:c,durs:g,entry:w,mediaDur:g.reduce((b,M)=>b+M,0)}}return{addChunk(c,i){i&&i.decoderConfig&&i.decoderConfig.description&&!s&&(s=ge(i.decoderConfig.description));let u=new Uint8Array(c.byteLength);c.copyTo(u),r.push({data:u,key:c.type==="key",ts:c.timestamp,dur:c.duration||0})},addSample(c,{key:i,timestamp:u,duration:m}){r.push({data:c,key:!!i,ts:u,dur:m||0})},setDescription(c){s=c},addAudioChunk(c,i){i&&i.decoderConfig&&i.decoderConfig.description&&!l&&(l=ge(i.decoderConfig.description));let u=new Uint8Array(c.byteLength);c.copyTo(u),a.push({data:u,ts:c.timestamp,dur:c.duration||0})},addAudioSample(c,{timestamp:i,duration:u}){a.push({data:c,ts:i,dur:u||0})},setAudioDescription(c){l=c},finish(){if(!r.length)throw new Error("No video frames were encoded");if(!Le(s)){let d=r.find(y=>y.key)||r[0];s=Ne(d.data)||s}if(!s)throw new Error("The encoder gave no H.264 decoder configuration (avcC)");let c=d=>Math.round(d*n/1e6),i=r.map(d=>c(d.ts)),u=i.slice().sort((d,y)=>d-y),m=c(r[r.length-1].dur)||(u.length>1?u[u.length-1]-u[u.length-2]:c(33333)),C=u.map((d,y)=>y+1<u.length?u[y+1]-d:m),g=i.map((d,y)=>d-u[y]),w=g.some(d=>d!==0),b=C.reduce((d,y)=>d+y,0),M=Math.round(b*1e3/n),I=le(C),R=le(g),S=r.map((d,y)=>d.key?y+1:0).filter(Boolean),U=r.reduce((d,y)=>d+y.data.length,0),k=h(),H=k?Math.round(k.mediaDur*1e3/k.rate):0,W=k?a.reduce((d,y)=>d+y.data.length,0):0,ne=Math.max(M,H),j=U+W,_=B("ftyp",d=>d.str("isom").u32(512).str("isom").str("iso2").str("avc1").str("mp41")),X=()=>B("dinf",d=>d.bytes(A("dref",0,0,y=>{y.u32(1),y.bytes(A("url ",0,1,()=>{}))}))),p=d=>A("stsc",0,0,y=>y.u32(1).u32(1).u32(d).u32(1)),E=d=>A("stsz",0,0,y=>{y.u32(0).u32(d.length);for(let F of d)y.u32(F.data.length)}),q=d=>A("stco",0,0,y=>y.u32(1).u32(d)),L=d=>B("moov",y=>{y.bytes(A("mvhd",0,0,F=>{F.u32(0).u32(0).u32(1e3).u32(ne).u32(65536).u16(256).zeros(10),fe.forEach(D=>F.u32(D)),F.zeros(24).u32(k?3:2)})),y.bytes(B("trak",F=>{F.bytes(A("tkhd",0,3,D=>{D.u32(0).u32(0).u32(1).u32(0).u32(M).zeros(8).u16(0).u16(0).u16(0).u16(0),fe.forEach(T=>D.u32(T)),D.u32(e<<16).u32(t<<16)})),F.bytes(B("mdia",D=>{D.bytes(A("mdhd",0,0,T=>T.u32(0).u32(0).u32(n).u32(b).u16(21956).u16(0))),D.bytes(A("hdlr",0,0,T=>T.u32(0).str("vide").zeros(12).str("VideoHandler").u8(0))),D.bytes(B("minf",T=>{T.bytes(A("vmhd",0,1,V=>V.u16(0).u16(0).u16(0).u16(0))),T.bytes(X()),T.bytes(B("stbl",V=>{V.bytes(A("stsd",0,0,v=>{v.u32(1),v.bytes(B("avc1",O=>{O.zeros(6).u16(1).zeros(16).u16(e).u16(t).u32(4718592).u32(4718592).u32(0).u16(1);let z=de.encode("image-to-pixel").slice(0,31);O.u8(z.length).bytes(z).zeros(31-z.length).u16(24).u16(65535),O.bytes(B("avcC",ae=>ae.bytes(s)))}))})),V.bytes(A("stts",0,0,v=>{v.u32(I.length);for(let[O,z]of I)v.u32(O).u32(z)})),w&&V.bytes(A("ctts",1,0,v=>{v.u32(R.length);for(let[O,z]of R)v.u32(O).u32(z>>>0)})),S.length<r.length&&V.bytes(A("stss",0,0,v=>{v.u32(S.length);for(let O of S)v.u32(O)})),V.bytes(p(r.length)),V.bytes(E(r)),V.bytes(q(d))}))}))}))})),k&&y.bytes(B("trak",F=>{F.bytes(A("tkhd",0,3,D=>{D.u32(0).u32(0).u32(2).u32(0).u32(H).zeros(8).u16(0).u16(1).u16(256).u16(0),fe.forEach(T=>D.u32(T)),D.u32(0).u32(0)})),F.bytes(B("mdia",D=>{D.bytes(A("mdhd",0,0,T=>T.u32(0).u32(0).u32(k.rate).u32(k.mediaDur).u16(21956).u16(0))),D.bytes(A("hdlr",0,0,T=>T.u32(0).str("soun").zeros(12).str("SoundHandler").u8(0))),D.bytes(B("minf",T=>{T.bytes(A("smhd",0,0,V=>V.u16(0).u16(0))),T.bytes(X()),T.bytes(B("stbl",V=>{V.bytes(A("stsd",0,0,O=>{O.u32(1),O.bytes(k.entry)}));let v=le(k.durs);V.bytes(A("stts",0,0,O=>{O.u32(v.length);for(let[z,ae]of v)O.u32(z).u32(ae)})),V.bytes(p(a.length)),V.bytes(E(a)),V.bytes(q(d+U))}))}))}))}))}),re=L(0).length,Y=_.length+re+8;if(Y+j>4294967295)throw new Error("Video is larger than 4 GB");let oe=L(Y),G=new Uint8Array(Y+j);G.set(_,0),G.set(oe,_.length),new DataView(G.buffer).setUint32(_.length+oe.length,j+8),G.set(de.encode("mdat"),_.length+oe.length+4);let J=Y;for(let d of r)G.set(d.data,J),J+=d.data.length;if(k)for(let d of a)G.set(d.data,J),J+=d.data.length;return G}}}var _e=new TextEncoder;function qe(e){let t=new Uint8Array(8);t[0]=1;let n=e;for(let o=7;o>=1;o--)t[o]=n%256,n=Math.floor(n/256);return t}function Me(e){let t=[];for(let n=e;n>0;n=Math.floor(n/256))t.unshift(n%256);return t}function te(e){let t=0;for(let r of e)t+=r.length;let n=new Uint8Array(t),o=0;for(let r of e)n.set(r,o),o+=r.length;return n}var N=(e,t)=>te([Uint8Array.from(Me(e)),qe(t.length),t]),x=(e,t,n=0)=>{let o=[];for(let r=t;r>0||o.length===0;r=Math.floor(r/256))o.unshift(r%256);for(;o.length<n;)o.unshift(0);return N(e,Uint8Array.from(o))},Q=(e,t)=>N(e,_e.encode(t)),Ce=(e,t)=>{let n=new Uint8Array(8);return new DataView(n.buffer).setFloat64(0,t),N(e,n)},P=(e,t)=>N(e,te(t)),f={EBML:440786851,EBMLVersion:17030,EBMLReadVersion:17143,EBMLMaxIDLength:17138,EBMLMaxSizeLength:17139,DocType:17026,DocTypeVersion:17031,DocTypeReadVersion:17029,Segment:408125543,SeekHead:290298740,Seek:19899,SeekID:21419,SeekPosition:21420,Info:357149030,TimestampScale:2807729,Duration:17545,MuxingApp:19840,WritingApp:22337,Tracks:374648427,TrackEntry:174,TrackNumber:215,TrackUID:29637,TrackType:131,FlagLacing:156,CodecID:134,CodecPrivate:25506,DefaultDuration:2352003,Video:224,PixelWidth:176,PixelHeight:186,CodecDelay:22186,SeekPreRoll:22203,Audio:225,SamplingFrequency:181,Channels:159,Cluster:524531317,Timestamp:231,SimpleBlock:163,Cues:475249515,CuePoint:187,CueTime:179,CueTrackPositions:183,CueTrack:247,CueClusterPosition:241},Ge={vp8:"V_VP8",vp9:"V_VP9",av1:"V_AV1"},$=1,he=2,Ae=e=>new Uint8Array(e instanceof ArrayBuffer?e:e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength));function Ee({width:e,height:t,codec:n="vp9",fps:o=0,audio:r=null}){if(r&&r.codec&&r.codec!=="opus")throw new Error(`WebM audio must be Opus, not ${r.codec}`);let a=[],s=[],l=null,h=null;return{addChunk(c,i){i&&i.decoderConfig&&i.decoderConfig.description&&!l&&(l=Ae(i.decoderConfig.description));let u=new Uint8Array(c.byteLength);c.copyTo(u),a.push({data:u,key:c.type==="key",ms:c.timestamp/1e3})},addFrame(c,{key:i,timestamp:u}){a.push({data:c,key:!!i,ms:u/1e3})},addAudioChunk(c,i){i&&i.decoderConfig&&i.decoderConfig.description&&!h&&(h=Ae(i.decoderConfig.description));let u=new Uint8Array(c.byteLength);c.copyTo(u),s.push({data:u,ms:c.timestamp/1e3,dms:(c.duration||0)/1e3})},addAudioFrame(c,{timestamp:i,duration:u}){s.push({data:c,ms:i/1e3,dms:(u||0)/1e3})},finish(){if(!a.length)throw new Error("No video frames were encoded");let c=!!(r&&s.length),i=o?1e3/o:a.length>1?(a[a.length-1].ms-a[0].ms)/(a.length-1):33.333,u=a[a.length-1].ms+i;if(c){let p=s[s.length-1];u=Math.max(u,p.ms+(p.dms||20))}let m=P(f.Info,[x(f.TimestampScale,1e6),Ce(f.Duration,u),Q(f.MuxingApp,"image-to-pixel"),Q(f.WritingApp,"image-to-pixel")]),C=[P(f.TrackEntry,[x(f.TrackNumber,$),x(f.TrackUID,$),x(f.TrackType,1),x(f.FlagLacing,0),Q(f.CodecID,Ge[n]||n),...l?[N(f.CodecPrivate,l)]:[],x(f.DefaultDuration,Math.round(i*1e6)),P(f.Video,[x(f.PixelWidth,e),x(f.PixelHeight,t)])])];if(c){let p=K(h)?h:xe(r.numberOfChannels,r.sampleRate),E=K(p).preSkip;C.push(P(f.TrackEntry,[x(f.TrackNumber,he),x(f.TrackUID,he),x(f.TrackType,2),x(f.FlagLacing,0),Q(f.CodecID,"A_OPUS"),N(f.CodecPrivate,p),x(f.CodecDelay,Math.round(E*1e9/48e3)),x(f.SeekPreRoll,8e7),P(f.Audio,[Ce(f.SamplingFrequency,r.sampleRate),x(f.Channels,r.numberOfChannels)])]))}let g=P(f.Tracks,C),w=a.map((p,E)=>({track:$,data:p.data,key:p.key,ms:p.ms,order:E}));c&&s.forEach((p,E)=>w.push({track:he,data:p.data,key:!0,ms:p.ms,order:a.length+E})),c&&w.sort((p,E)=>p.ms-E.ms||p.track-E.track||p.order-E.order);let b=[],M=null;for(let p of w){let E=Math.round(p.ms),q=p.track===$&&p.key;(!M||q||E-M.start>3e4)&&(M={start:E,key:q,blocks:[]},b.push(M));let L=new Uint8Array(4);L[0]=128|p.track,new DataView(L.buffer).setInt16(1,E-M.start),L[3]=p.key?128:0,M.blocks.push(N(f.SimpleBlock,te([L,p.data])))}let I=b.map(p=>P(f.Cluster,[x(f.Timestamp,p.start),...p.blocks])),R=(p,E,q)=>P(f.SeekHead,[[f.Info,p],[f.Tracks,E],[f.Cues,q]].map(([L,re])=>P(f.Seek,[N(f.SeekID,Uint8Array.from(Me(L))),x(f.SeekPosition,re,8)]))),U=R(0,0,0).length,k=U+m.length,H=k+g.length,W=[];b.forEach((p,E)=>{p.key&&W.push(P(f.CuePoint,[x(f.CueTime,p.start),P(f.CueTrackPositions,[x(f.CueTrack,$),x(f.CueClusterPosition,H)])])),H+=I[E].length});let ne=P(f.Cues,W.length?W:[P(f.CuePoint,[x(f.CueTime,0),P(f.CueTrackPositions,[x(f.CueTrack,$),x(f.CueClusterPosition,k+g.length)])])]),j=H,_=P(f.EBML,[x(f.EBMLVersion,1),x(f.EBMLReadVersion,1),x(f.EBMLMaxIDLength,4),x(f.EBMLMaxSizeLength,8),Q(f.DocType,"webm"),x(f.DocTypeVersion,4),x(f.DocTypeReadVersion,2)]),X=P(f.Segment,[R(U,k,j),m,g,...I,ne]);return te([_,X])}}}function ke(e){if(Array.isArray(e))return[e[0]|0,e[1]|0,e[2]|0];let t=String(e).trim().replace(/^#|^0x/i,"");if((t.length===3||t.length===4)&&(t=t.slice(0,3).split("").map(n=>n+n).join("")),!/^[0-9a-f]{6}([0-9a-f]{2})?$/i.test(t))throw new Error(`Invalid colour "${e}"`);return[parseInt(t.slice(0,2),16),parseInt(t.slice(2,4),16),parseInt(t.slice(4,6),16)]}function Te(e,t=1,n={}){t=Math.max(1,Math.round(t));let o=e.width*t,r=e.height*t,a=new Uint8ClampedArray(o*r*4),s=e.data,l=e.width;for(let h=0;h<r;h++){let c=h/t|0;for(let i=0;i<o;i++){let u=(c*l+(i/t|0))*4,m=(h*o+i)*4;a[m]=s[u],a[m+1]=s[u+1],a[m+2]=s[u+2],a[m+3]=s[u+3]}}if(n.grid&&t>=3){let[h,c,i]=ke(n.gridColor||"#000000"),u=n.gridAlpha??.35;for(let m=0;m<r;m++)for(let C=0;C<o;C++){if(C%t&&m%t)continue;let g=(m*o+C)*4,w=a[g+3]/255,b=u+w*(1-u);a[g]=(h*u+a[g]*w*(1-u))/b,a[g+1]=(c*u+a[g+1]*w*(1-u))/b,a[g+2]=(i*u+a[g+2]*w*(1-u))/b,a[g+3]=b*255}}return{width:o,height:r,data:a}}var $e=(e,t,n=4e3)=>new Promise((o,r)=>{let a=setTimeout(()=>{e.removeEventListener(t,s),r(new Error(`Timed out waiting for "${t}"`))},n),s=()=>{clearTimeout(a),o()};e.addEventListener(t,s,{once:!0})}),We={1:"loading was aborted",2:"a network error stopped the download",3:"the browser could not decode it",4:"this browser can't play this video's format"};function De(e){let t=e.error?e.error.code:0,n=new Error(We[t]||"the video could not be loaded");return n.code=t,e.error&&e.error.message&&(n.detail=e.error.message),n}var Se=(e,t,n)=>new Promise((o,r)=>{let a=()=>{clearTimeout(h),e.removeEventListener(t,s),e.removeEventListener("error",l)},s=()=>{a(),o()},l=()=>{a(),r(De(e))},h=setTimeout(()=>{a(),r(new Error(`no response from the video decoder after ${n/1e3} s`))},n);e.addEventListener(t,s,{once:!0}),e.addEventListener("error",l,{once:!0})});async function pe(e,{timeout:t=6e4}={}){if(e.error)throw De(e);return e.readyState<1&&await Se(e,"loadedmetadata",t),e.readyState<2&&await Se(e,"loadeddata",t).catch(n=>{if(n.code)throw n}),e}async function me(e,t){if(t=Math.max(0,Math.min(t,(e.duration||0)-1e-4)),Math.abs(e.currentTime-t)<1e-6&&e.readyState>=2)return;let n="requestVideoFrameCallback"in e?new Promise(r=>{e.requestVideoFrameCallback(()=>r()),setTimeout(r,500)}):null,o=$e(e,"seeked");e.currentTime=t,await o,n&&await n}async function je(e,t=30){if(!("requestVideoFrameCallback"in e))return t;await pe(e);let n={t:e.currentTime,muted:e.muted,paused:e.paused},o=[];try{e.muted=!0,await new Promise(s=>{let l=setTimeout(s,1200),h=(c,i)=>{o.push(i.mediaTime),o.length>=12?(clearTimeout(l),s()):e.requestVideoFrameCallback(h)};e.requestVideoFrameCallback(h),e.play().catch(()=>{clearTimeout(l),s()})})}finally{e.pause(),e.muted=n.muted,await me(e,n.t).catch(()=>{})}let r=[];for(let s=1;s<o.length;s++){let l=o[s]-o[s-1];l>.001&&r.push(l)}if(r.length<3)return t;r.sort((s,l)=>s-l);let a=1/r[Math.floor(r.length/2)];for(let s of[12,15,23.976,24,25,29.97,30,48,50,59.94,60])if(Math.abs(a-s)<.6)return s;return Math.round(a)}function Ve({duration:e,fps:t,start:n=0,end:o=e,maxFrames:r=1/0}){let a=[],s=Math.min(r,Math.max(1,Math.floor((o-n)*t+1e-6)));for(let l=0;l<s;l++)a.push(n+(l+.5)/t);return a}async function Ke(e,t){await pe(e);let n=e.paused;e.pause();let o=Array.isArray(t.times)?t.times.filter(r=>Number.isFinite(r)).slice(0,t.maxFrames??1/0):Ve({duration:e.duration,...t});try{for(let r=0;r<o.length;r++){if(t.signal?.aborted)throw new DOMException("Aborted","AbortError");if(await me(e,o[r]),t.signal?.aborted)throw new DOMException("Aborted","AbortError");await t.onFrame({index:r,time:o[r],total:o.length})}}finally{n||e.play().catch(()=>{})}return o.length}var Pe=["avc1.640033","avc1.4d0033","avc1.42003e","avc1.640028","avc1.42001f"],Ue=[["vp09.00.10.08","vp9"],["vp8","vp8"]];async function dt(e=640,t=480){let n={mp4:!1,webm:!1,webcodecs:typeof VideoEncoder<"u",aac:!1,opus:!1};if(n.webcodecs){for(let o of Pe)if((await VideoEncoder.isConfigSupported({codec:o,width:e,height:t}).catch(()=>({}))).supported){n.mp4=!0;break}for(let[o]of Ue)if((await VideoEncoder.isConfigSupported({codec:o,width:e,height:t}).catch(()=>({}))).supported){n.webm=!0;break}}return n.aac=await ue("aac"),n.opus=await ue("opus"),n}var Be=e=>e+(e&1);async function Qe(e,t,n){let o=e==="mp4"?["aac","opus"]:["opus"];for(let r of o){let a=await ce(r,t.sampleRate,t.numberOfChannels,n);if(a)return{codec:r,config:a}}return null}async function Xe(e){let t=Math.max(1,Math.round(e.scale||1)),n=Be(e.width*t),o=Be(e.height*t),r=e.fps||30,a=e.bitrate||Math.min(2e7,Math.max(2e6,n*o*r*.15));if(typeof VideoEncoder>"u")throw new Error("This browser has no WebCodecs VideoEncoder. Export a GIF or PNG sequence instead.");let s=null,l=null,h=e.format==="mp4"?Pe.map(S=>[S,"avc"]):Ue;for(let[S,U]of h)if((await VideoEncoder.isConfigSupported({codec:S,width:n,height:o,bitrate:a,framerate:r}).catch(()=>({}))).supported){s=S,l=U;break}if(!s)throw new Error(`No ${e.format.toUpperCase()} encoder available in this browser at ${n}\xD7${o}.`);let c=e.audio&&e.audio.channels&&e.audio.channels.length?e.audio:null,i=c?await Qe(e.format,c,e.audioBitrate||128e3):null,u=i?i.codec:null,m=i?{codec:u,sampleRate:i.config.sampleRate,numberOfChannels:i.config.numberOfChannels,bitrate:i.config.bitrate||128e3}:null,C=e.format==="mp4"?be({width:n,height:o,audio:m}):Ee({width:n,height:o,codec:l,fps:r,audio:m}),g=null,w=new VideoEncoder({output:(S,U)=>C.addChunk(S,U),error:S=>{g=S}});w.configure({codec:s,width:n,height:o,bitrate:a,framerate:r,latencyMode:"quality",...e.format==="mp4"?{avc:{format:"avc"}}:{}});let b=new OffscreenCanvas(n,o),M=b.getContext("2d"),I=0,R=1e6/r;return{width:n,height:o,codec:s,audioCodec:u,async add(S){if(g)throw g;let U=t>1?Te(S,t):S;M.fillStyle="#000",M.fillRect(0,0,n,o),M.putImageData(new ImageData(new Uint8ClampedArray(U.data),U.width,U.height),0,0);let k=new VideoFrame(b,{timestamp:Math.round(I*R),duration:Math.round(R)});w.encode(k,{keyFrame:I%Math.max(1,Math.round(r*2))===0}),k.close(),I++,w.encodeQueueSize>8&&await new Promise(H=>setTimeout(H,0))},async finish(){if(await w.flush(),g)throw g;return w.close(),u&&I>0&&await ye(c,{config:i.config,maxDuration:I/r,onChunk:(S,U)=>C.addAudioChunk(S,U)}),new Blob([C.finish()],{type:e.format==="mp4"?"video/mp4":"video/webm"})}}}async function ht(e,t){let n=await Xe({...t,width:e[0].width,height:e[0].height});for(let o of e)await n.add(o);return n.finish()}export{ue as audioCodecSupported,be as createMp4Muxer,Xe as createVideoEncoder,Ee as createWebmMuxer,Ie as decodeAudio,ye as encodeAudio,ht as encodeVideo,je as estimateFps,ce as findAudioConfig,Ke as forEachFrame,Ve as frameTimes,me as seekTo,pe as videoReady,dt as videoSupport};
|
|
3
3
|
//# sourceMappingURL=video.esm.js.map
|