forked from vgaNAR6ta/drags-and-nerds
13c91b464f
préparation du template intégration du fond animé fusion du style background et style pcp
6 lines
25 KiB
JavaScript
6 lines
25 KiB
JavaScript
"use strict";var Mp4Muxer=(()=>{var Ne=Object.defineProperty;var dt=Object.getOwnPropertyDescriptor;var pt=Object.getOwnPropertyNames;var ct=Object.prototype.hasOwnProperty;var Tt=(t,e)=>{for(var s in e)Ne(t,s,{get:e[s],enumerable:!0})},Ct=(t,e,s,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of pt(e))!ct.call(t,n)&&n!==s&&Ne(t,n,{get:()=>e[n],enumerable:!(r=dt(e,n))||r.enumerable});return t};var bt=t=>Ct(Ne({},"__esModule",{value:!0}),t);var Re=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)};var i=(t,e,s)=>(Re(t,e,"read from private field"),s?s.call(t):e.get(t)),f=(t,e,s)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,s)},S=(t,e,s,r)=>(Re(t,e,"write to private field"),r?r.call(t,s):e.set(t,s),s),Ye=(t,e,s,r)=>({set _(n){S(t,e,n,s)},get _(){return i(t,e,r)}}),p=(t,e,s)=>(Re(t,e,"access private method"),s);var fs={};Tt(fs,{ArrayBufferTarget:()=>re,FileSystemWritableFileStreamTarget:()=>ne,Muxer:()=>Ue,StreamTarget:()=>$});var c=new Uint8Array(8),E=new DataView(c.buffer),g=t=>[(t%256+256)%256],C=t=>(E.setUint16(0,t,!1),[c[0],c[1]]),Je=t=>(E.setInt16(0,t,!1),[c[0],c[1]]),Fe=t=>(E.setUint32(0,t,!1),[c[1],c[2],c[3]]),o=t=>(E.setUint32(0,t,!1),[c[0],c[1],c[2],c[3]]),et=t=>(E.setInt32(0,t,!1),[c[0],c[1],c[2],c[3]]),M=t=>(E.setUint32(0,Math.floor(t/2**32),!1),E.setUint32(4,t,!1),[c[0],c[1],c[2],c[3],c[4],c[5],c[6],c[7]]),ge=t=>(E.setInt16(0,2**8*t,!1),[c[0],c[1]]),U=t=>(E.setInt32(0,2**16*t,!1),[c[0],c[1],c[2],c[3]]),Le=t=>(E.setInt32(0,2**30*t,!1),[c[0],c[1],c[2],c[3]]),k=(t,e=!1)=>{let s=Array(t.length).fill(null).map((r,n)=>t.charCodeAt(n));return e&&s.push(0),s},K=t=>t&&t[t.length-1],ye=t=>{let e;for(let s of t)(!e||s.presentationTimestamp>e.presentationTimestamp)&&(e=s);return e},B=(t,e,s=!0)=>{let r=t*e;return s?Math.round(r):r},je=t=>{let e=t*(Math.PI/180),s=Math.cos(e),r=Math.sin(e);return[s,r,0,-r,s,0,0,0,1]},He=je(0),$e=t=>[U(t[0]),U(t[1]),Le(t[2]),U(t[3]),U(t[4]),Le(t[5]),U(t[6]),U(t[7]),Le(t[8])],Z=t=>!t||typeof t!="object"?t:Array.isArray(t)?t.map(Z):Object.fromEntries(Object.entries(t).map(([e,s])=>[e,Z(s)])),H=t=>t>=0&&t<2**32;var y=(t,e,s)=>({type:t,contents:e&&new Uint8Array(e.flat(10)),children:s}),b=(t,e,s,r,n)=>y(t,[g(e),Fe(s),r??[]],n),tt=t=>{let e=512;return t.fragmented?y("ftyp",[k("iso5"),o(e),k("iso5"),k("iso6"),k("mp41")]):y("ftyp",[k("isom"),o(e),k("isom"),t.holdsAvc?k("avc1"):[],k("mp41")])},we=t=>({type:"mdat",largeSize:t}),st=t=>({type:"free",size:t}),ie=(t,e,s=!1)=>y("moov",null,[St(e,t),...t.map(r=>gt(r,e)),s?Xt(t):null]),St=(t,e)=>{let s=B(Math.max(0,...e.filter(l=>l.samples.length>0).map(l=>{let m=ye(l.samples);return m.presentationTimestamp+m.duration})),xe),r=Math.max(...e.map(l=>l.id))+1,n=!H(t)||!H(s),a=n?M:o;return b("mvhd",+n,0,[a(t),a(t),o(xe),a(s),U(1),ge(1),Array(10).fill(0),$e(He),Array(24).fill(0),o(r)])},gt=(t,e)=>y("trak",null,[yt(t,e),xt(t,e)]),yt=(t,e)=>{let s=ye(t.samples),r=B(s?s.presentationTimestamp+s.duration:0,xe),n=!H(e)||!H(r),a=n?M:o,l;return t.info.type==="video"?l=typeof t.info.rotation=="number"?je(t.info.rotation):t.info.rotation:l=He,b("tkhd",+n,3,[a(e),a(e),o(t.id),o(0),a(r),Array(8).fill(0),C(0),C(0),ge(t.info.type==="audio"?1:0),C(0),$e(l),U(t.info.type==="video"?t.info.width:0),U(t.info.type==="video"?t.info.height:0)])},xt=(t,e)=>y("mdia",null,[wt(t,e),vt(t.info.type==="video"?"vide":"soun"),Ot(t)]),wt=(t,e)=>{let s=ye(t.samples),r=B(s?s.presentationTimestamp+s.duration:0,t.timescale),n=!H(e)||!H(r),a=n?M:o;return b("mdhd",+n,0,[a(e),a(e),o(t.timescale),a(r),C(21956),C(0)])},vt=t=>b("hdlr",0,0,[k("mhlr"),k(t),o(0),o(0),o(0),k("mp4-muxer-hdlr",!0)]),Ot=t=>y("minf",null,[t.info.type==="video"?At():kt(),Bt(),Dt(t)]),At=()=>b("vmhd",0,1,[C(0),C(0),C(0),C(0)]),kt=()=>b("smhd",0,0,[C(0),C(0)]),Bt=()=>y("dinf",null,[zt()]),zt=()=>b("dref",0,0,[o(1)],[Ut()]),Ut=()=>b("url ",0,1),Dt=t=>{let e=t.compositionTimeOffsetTable.length>1||t.compositionTimeOffsetTable.some(s=>s.sampleCompositionTimeOffset!==0);return y("stbl",null,[Et(t),Ft(t),jt(t),Ht(t),$t(t),qt(t),e?Wt(t):null])},Et=t=>b("stsd",0,0,[o(1)],[t.info.type==="video"?_t(ss[t.info.codec],t):Nt(rs[t.info.codec],t)]),_t=(t,e)=>y(t,[Array(6).fill(0),C(1),C(0),C(0),Array(12).fill(0),C(e.info.width),C(e.info.height),o(4718592),o(4718592),o(0),C(1),Array(32).fill(0),C(24),Je(65535)],[is[e.info.codec](e)]),It=t=>t.info.decoderConfig&&y("avcC",[...new Uint8Array(t.info.decoderConfig.description)]),Mt=t=>t.info.decoderConfig&&y("hvcC",[...new Uint8Array(t.info.decoderConfig.description)]),Pt=t=>{if(!t.info.decoderConfig)return null;let e=t.info.decoderConfig;if(!e.colorSpace)throw new Error("'colorSpace' is required in the decoder config for VP9.");let s=e.codec.split("."),r=Number(s[1]),n=Number(s[2]),a=Number(s[3]),l=0,m=(a<<4)+(l<<1)+Number(e.colorSpace.fullRange),d=2,T=2,v=2;return b("vpcC",1,0,[g(r),g(n),g(m),g(d),g(T),g(v),C(0)])},Vt=()=>{let t=1,e=1,s=(t<<7)+e;return y("av1C",[s,0,0,0])},Nt=(t,e)=>y(t,[Array(6).fill(0),C(1),C(0),C(0),o(0),C(e.info.numberOfChannels),C(16),C(0),C(0),U(e.info.sampleRate)],[ns[e.info.codec](e)]),Rt=t=>{let e=new Uint8Array(t.info.decoderConfig.description);return b("esds",0,0,[o(58753152),g(32+e.byteLength),C(1),g(0),o(75530368),g(18+e.byteLength),g(64),g(21),Fe(0),o(130071),o(130071),o(92307584),g(e.byteLength),...e,o(109084800),g(1),g(2)])},Lt=t=>y("dOps",[g(0),g(t.info.numberOfChannels),C(3840),o(t.info.sampleRate),ge(0),g(0)]),Ft=t=>b("stts",0,0,[o(t.timeToSampleTable.length),t.timeToSampleTable.map(e=>[o(e.sampleCount),o(e.sampleDelta)])]),jt=t=>{if(t.samples.every(s=>s.type==="key"))return null;let e=[...t.samples.entries()].filter(([,s])=>s.type==="key");return b("stss",0,0,[o(e.length),e.map(([s])=>o(s+1))])},Ht=t=>b("stsc",0,0,[o(t.compactlyCodedChunkTable.length),t.compactlyCodedChunkTable.map(e=>[o(e.firstChunk),o(e.samplesPerChunk),o(1)])]),$t=t=>b("stsz",0,0,[o(0),o(t.samples.length),t.samples.map(e=>o(e.size))]),qt=t=>t.finalizedChunks.length>0&&K(t.finalizedChunks).offset>=2**32?b("co64",0,0,[o(t.finalizedChunks.length),t.finalizedChunks.map(e=>M(e.offset))]):b("stco",0,0,[o(t.finalizedChunks.length),t.finalizedChunks.map(e=>o(e.offset))]),Wt=t=>b("ctts",0,0,[o(t.compositionTimeOffsetTable.length),t.compositionTimeOffsetTable.map(e=>[o(e.sampleCount),o(e.sampleCompositionTimeOffset)])]),Xt=t=>y("mvex",null,t.map(Gt)),Gt=t=>b("trex",0,0,[o(t.id),o(1),o(0),o(0),o(0)]),qe=(t,e)=>y("moof",null,[Zt(t),...e.map(Kt)]),Zt=t=>b("mfhd",0,0,[o(t)]),it=t=>{let e=0,s=0,r=0,n=0,a=t.type==="delta";return s|=+a,a?e|=1:e|=2,e<<24|s<<16|r<<8|n},Kt=t=>y("traf",null,[Qt(t),Yt(t),Jt(t)]),Qt=t=>{let e=0;e|=8,e|=16,e|=32,e|=131072;let s=t.currentChunk.samples[1]??t.currentChunk.samples[0],r={duration:s.timescaleUnitsToNextSample,size:s.size,flags:it(s)};return b("tfhd",0,e,[o(t.id),o(r.duration),o(r.size),o(r.flags)])},Yt=t=>b("tfdt",1,0,[M(B(t.currentChunk.startTimestamp,t.timescale))]),Jt=t=>{let e=t.currentChunk.samples.map(_=>_.timescaleUnitsToNextSample),s=t.currentChunk.samples.map(_=>_.size),r=t.currentChunk.samples.map(it),n=t.currentChunk.samples.map(_=>B(_.presentationTimestamp-_.decodeTimestamp,t.timescale)),a=new Set(e),l=new Set(s),m=new Set(r),d=new Set(n),T=m.size===2&&r[0]!==r[1],v=a.size>1,F=l.size>1,se=!T&&m.size>1,Qe=d.size>1||[...d].some(_=>_!==0),j=0;return j|=1,j|=4*+T,j|=256*+v,j|=512*+F,j|=1024*+se,j|=2048*+Qe,b("trun",1,j,[o(t.currentChunk.samples.length),o(t.currentChunk.offset-t.currentChunk.moofOffset||0),T?o(r[0]):[],t.currentChunk.samples.map((_,Se)=>[v?o(e[Se]):[],F?o(s[Se]):[],se?o(r[Se]):[],Qe?et(n[Se]):[]])])},rt=t=>y("mfra",null,[...t.map(es),ts()]),es=(t,e)=>b("tfra",1,0,[o(t.id),o(63),o(t.finalizedChunks.length),t.finalizedChunks.map(r=>[M(B(r.startTimestamp,t.timescale)),M(r.moofOffset),o(e+1),o(1),o(1)])]),ts=()=>b("mfro",0,0,[o(0)]),ss={avc:"avc1",hevc:"hvc1",vp9:"vp09",av1:"av01"},is={avc:It,hevc:Mt,vp9:Pt,av1:Vt},rs={aac:"mp4a",opus:"Opus"},ns={aac:Rt,opus:Lt};var re=class{constructor(){this.buffer=null}},$=class{constructor(e){this.options=e}},ne=class{constructor(e,s){this.stream=e;this.options=s}};var P,q,ae=class{constructor(){this.pos=0;f(this,P,new Uint8Array(8));f(this,q,new DataView(i(this,P).buffer));this.offsets=new WeakMap}seek(e){this.pos=e}writeU32(e){i(this,q).setUint32(0,e,!1),this.write(i(this,P).subarray(0,4))}writeU64(e){i(this,q).setUint32(0,Math.floor(e/2**32),!1),i(this,q).setUint32(4,e,!1),this.write(i(this,P).subarray(0,8))}writeAscii(e){for(let s=0;s<e.length;s++)i(this,q).setUint8(s%8,e.charCodeAt(s)),s%8===7&&this.write(i(this,P));e.length%8!==0&&this.write(i(this,P).subarray(0,e.length%8))}writeBox(e){if(this.offsets.set(e,this.pos),e.contents&&!e.children)this.writeBoxHeader(e,e.size??e.contents.byteLength+8),this.write(e.contents);else{let s=this.pos;if(this.writeBoxHeader(e,0),e.contents&&this.write(e.contents),e.children)for(let a of e.children)a&&this.writeBox(a);let r=this.pos,n=e.size??r-s;this.seek(s),this.writeBoxHeader(e,n),this.seek(r)}}writeBoxHeader(e,s){this.writeU32(e.largeSize?1:s),this.writeAscii(e.type),e.largeSize&&this.writeU64(s)}measureBoxHeader(e){return 8+(e.largeSize?8:0)}patchBox(e){let s=this.pos;this.seek(this.offsets.get(e)),this.writeBox(e),this.seek(s)}measureBox(e){if(e.contents&&!e.children)return this.measureBoxHeader(e)+e.contents.byteLength;{let s=this.measureBoxHeader(e);if(e.contents&&(s+=e.contents.byteLength),e.children)for(let r of e.children)r&&(s+=this.measureBox(r));return s}}};P=new WeakMap,q=new WeakMap;var me,V,Q,Y,he,We,Oe=class extends ae{constructor(s){super();f(this,he);f(this,me,void 0);f(this,V,new ArrayBuffer(2**16));f(this,Q,new Uint8Array(i(this,V)));f(this,Y,0);S(this,me,s)}write(s){p(this,he,We).call(this,this.pos+s.byteLength),i(this,Q).set(s,this.pos),this.pos+=s.byteLength,S(this,Y,Math.max(i(this,Y),this.pos))}finalize(){p(this,he,We).call(this,this.pos),i(this,me).buffer=i(this,V).slice(0,Math.max(i(this,Y),this.pos))}};me=new WeakMap,V=new WeakMap,Q=new WeakMap,Y=new WeakMap,he=new WeakSet,We=function(s){let r=i(this,V).byteLength;for(;r<s;)r*=2;if(r===i(this,V).byteLength)return;let n=new ArrayBuffer(r),a=new Uint8Array(n);a.set(i(this,Q),0),S(this,V,n),S(this,Q,a)};var ue,N,oe=class extends ae{constructor(s){super();f(this,ue,void 0);f(this,N,[]);S(this,ue,s)}write(s){i(this,N).push({data:s.slice(),start:this.pos}),this.pos+=s.byteLength}flush(){if(i(this,N).length===0)return;let s=[],r=[...i(this,N)].sort((n,a)=>n.start-a.start);s.push({start:r[0].start,size:r[0].data.byteLength});for(let n=1;n<r.length;n++){let a=s[s.length-1],l=r[n];l.start<=a.start+a.size?a.size=Math.max(a.size,l.start+l.data.byteLength-a.start):s.push({start:l.start,size:l.data.byteLength})}for(let n of s){n.data=new Uint8Array(n.size);for(let a of i(this,N))n.start<=a.start&&a.start<n.start+n.size&&n.data.set(a.data,a.start-n.start);i(this,ue).options.onData?.(n.data,n.start)}i(this,N).length=0}finalize(){}};ue=new WeakMap,N=new WeakMap;var as=2**24,os=2,fe,z,O,de,Xe,ke,nt,Be,at,J,ve,le=class extends ae{constructor(s){super();f(this,de);f(this,ke);f(this,Be);f(this,J);f(this,fe,void 0);f(this,z,void 0);f(this,O,[]);if(S(this,fe,s),S(this,z,s.options?.chunkSize??as),!Number.isInteger(i(this,z))||i(this,z)<2**10)throw new Error("Invalid StreamTarget options: chunkSize must be an integer not smaller than 1024.")}write(s){p(this,de,Xe).call(this,s,this.pos),p(this,J,ve).call(this),this.pos+=s.byteLength}finalize(){p(this,J,ve).call(this,!0)}};fe=new WeakMap,z=new WeakMap,O=new WeakMap,de=new WeakSet,Xe=function(s,r){let n=i(this,O).findIndex(T=>T.start<=r&&r<T.start+i(this,z));n===-1&&(n=p(this,Be,at).call(this,r));let a=i(this,O)[n],l=r-a.start,m=s.subarray(0,Math.min(i(this,z)-l,s.byteLength));a.data.set(m,l);let d={start:l,end:l+m.byteLength};if(p(this,ke,nt).call(this,a,d),a.written[0].start===0&&a.written[0].end===i(this,z)&&(a.shouldFlush=!0),i(this,O).length>os){for(let T=0;T<i(this,O).length-1;T++)i(this,O)[T].shouldFlush=!0;p(this,J,ve).call(this)}m.byteLength<s.byteLength&&p(this,de,Xe).call(this,s.subarray(m.byteLength),r+m.byteLength)},ke=new WeakSet,nt=function(s,r){let n=0,a=s.written.length-1,l=-1;for(;n<=a;){let m=Math.floor(n+(a-n+1)/2);s.written[m].start<=r.start?(n=m+1,l=m):a=m-1}for(s.written.splice(l+1,0,r),(l===-1||s.written[l].end<r.start)&&l++;l<s.written.length-1&&s.written[l].end>=s.written[l+1].start;)s.written[l].end=Math.max(s.written[l].end,s.written[l+1].end),s.written.splice(l+1,1)},Be=new WeakSet,at=function(s){let n={start:Math.floor(s/i(this,z))*i(this,z),data:new Uint8Array(i(this,z)),written:[],shouldFlush:!1};return i(this,O).push(n),i(this,O).sort((a,l)=>a.start-l.start),i(this,O).indexOf(n)},J=new WeakSet,ve=function(s=!1){for(let r=0;r<i(this,O).length;r++){let n=i(this,O)[r];if(!(!n.shouldFlush&&!s)){for(let a of n.written)i(this,fe).options.onData?.(n.data.subarray(a.start,a.end),n.start+a.start);i(this,O).splice(r--,1)}}};var Ae=class extends le{constructor(e){super(new $({onData:(s,r)=>e.stream.write({type:"write",data:s,position:r}),chunkSize:e.options?.chunkSize}))}};var xe=1e3,ls=["avc","hevc","vp9","av1"],ms=["aac","opus"],hs=2082844800,us=["strict","offset","cross-track-offset"],h,u,ce,A,x,w,W,X,De,R,L,ee,Ee,ot,_e,lt,Ie,mt,Me,ht,Pe,ut,Te,Ge,D,I,Ve,ft,te,ze,Ce,Ze,G,pe,be,Ke,Ue=class{constructor(e){f(this,Ee);f(this,_e);f(this,Ie);f(this,Me);f(this,Pe);f(this,Te);f(this,D);f(this,Ve);f(this,te);f(this,Ce);f(this,G);f(this,be);f(this,h,void 0);f(this,u,void 0);f(this,ce,void 0);f(this,A,void 0);f(this,x,null);f(this,w,null);f(this,W,Math.floor(Date.now()/1e3)+hs);f(this,X,[]);f(this,De,1);f(this,R,[]);f(this,L,[]);f(this,ee,!1);if(p(this,Ee,ot).call(this,e),e.video=Z(e.video),e.audio=Z(e.audio),e.fastStart=Z(e.fastStart),this.target=e.target,S(this,h,{firstTimestampBehavior:"strict",...e}),e.target instanceof re)S(this,u,new Oe(e.target));else if(e.target instanceof $)S(this,u,e.target.options?.chunked?new le(e.target):new oe(e.target));else if(e.target instanceof ne)S(this,u,new Ae(e.target));else throw new Error(`Invalid target: ${e.target}`);p(this,Me,ht).call(this),p(this,_e,lt).call(this)}addVideoChunk(e,s,r,n){let a=new Uint8Array(e.byteLength);e.copyTo(a),this.addVideoChunkRaw(a,e.type,r??e.timestamp,e.duration,s,n)}addVideoChunkRaw(e,s,r,n,a,l){if(p(this,be,Ke).call(this),!i(this,h).video)throw new Error("No video track declared.");if(typeof i(this,h).fastStart=="object"&&i(this,x).samples.length===i(this,h).fastStart.expectedVideoChunks)throw new Error(`Cannot add more video chunks than specified in 'fastStart' (${i(this,h).fastStart.expectedVideoChunks}).`);let m=p(this,Te,Ge).call(this,i(this,x),e,s,r,n,a,l);if(i(this,h).fastStart==="fragmented"&&i(this,w)){for(;i(this,L).length>0&&i(this,L)[0].decodeTimestamp<=m.decodeTimestamp;){let d=i(this,L).shift();p(this,D,I).call(this,i(this,w),d)}m.decodeTimestamp<=i(this,w).lastDecodeTimestamp?p(this,D,I).call(this,i(this,x),m):i(this,R).push(m)}else p(this,D,I).call(this,i(this,x),m)}addAudioChunk(e,s,r){let n=new Uint8Array(e.byteLength);e.copyTo(n),this.addAudioChunkRaw(n,e.type,r??e.timestamp,e.duration,s)}addAudioChunkRaw(e,s,r,n,a){if(p(this,be,Ke).call(this),!i(this,h).audio)throw new Error("No audio track declared.");if(typeof i(this,h).fastStart=="object"&&i(this,w).samples.length===i(this,h).fastStart.expectedAudioChunks)throw new Error(`Cannot add more audio chunks than specified in 'fastStart' (${i(this,h).fastStart.expectedAudioChunks}).`);let l=p(this,Te,Ge).call(this,i(this,w),e,s,r,n,a);if(i(this,h).fastStart==="fragmented"&&i(this,x)){for(;i(this,R).length>0&&i(this,R)[0].decodeTimestamp<=l.decodeTimestamp;){let m=i(this,R).shift();p(this,D,I).call(this,i(this,x),m)}l.decodeTimestamp<=i(this,x).lastDecodeTimestamp?p(this,D,I).call(this,i(this,w),l):i(this,L).push(l)}else p(this,D,I).call(this,i(this,w),l)}finalize(){if(i(this,ee))throw new Error("Cannot finalize a muxer more than once.");if(i(this,h).fastStart==="fragmented"){for(let s of i(this,R))p(this,D,I).call(this,i(this,x),s);for(let s of i(this,L))p(this,D,I).call(this,i(this,w),s);p(this,Ce,Ze).call(this,!1)}else i(this,x)&&p(this,te,ze).call(this,i(this,x)),i(this,w)&&p(this,te,ze).call(this,i(this,w));let e=[i(this,x),i(this,w)].filter(Boolean);if(i(this,h).fastStart==="in-memory"){let s;for(let n=0;n<2;n++){let a=ie(e,i(this,W)),l=i(this,u).measureBox(a);s=i(this,u).measureBox(i(this,A));let m=i(this,u).pos+l+s;for(let d of i(this,X)){d.offset=m;for(let{data:T}of d.samples)m+=T.byteLength,s+=T.byteLength}if(m<2**32)break;s>=2**32&&(i(this,A).largeSize=!0)}let r=ie(e,i(this,W));i(this,u).writeBox(r),i(this,A).size=s,i(this,u).writeBox(i(this,A));for(let n of i(this,X))for(let a of n.samples)i(this,u).write(a.data),a.data=null}else if(i(this,h).fastStart==="fragmented"){let s=i(this,u).pos,r=rt(e);i(this,u).writeBox(r);let n=i(this,u).pos-s;i(this,u).seek(i(this,u).pos-4),i(this,u).writeU32(n)}else{let s=i(this,u).offsets.get(i(this,A)),r=i(this,u).pos-s;i(this,A).size=r,i(this,A).largeSize=r>=2**32,i(this,u).patchBox(i(this,A));let n=ie(e,i(this,W));if(typeof i(this,h).fastStart=="object"){i(this,u).seek(i(this,ce)),i(this,u).writeBox(n);let a=s-i(this,u).pos;i(this,u).writeBox(st(a))}else i(this,u).writeBox(n)}p(this,G,pe).call(this),i(this,u).finalize(),S(this,ee,!0)}};h=new WeakMap,u=new WeakMap,ce=new WeakMap,A=new WeakMap,x=new WeakMap,w=new WeakMap,W=new WeakMap,X=new WeakMap,De=new WeakMap,R=new WeakMap,L=new WeakMap,ee=new WeakMap,Ee=new WeakSet,ot=function(e){if(e.video){if(!ls.includes(e.video.codec))throw new Error(`Unsupported video codec: ${e.video.codec}`);let s=e.video.rotation;if(typeof s=="number"&&![0,90,180,270].includes(s))throw new Error(`Invalid video rotation: ${s}. Has to be 0, 90, 180 or 270.`);if(Array.isArray(s)&&(s.length!==9||s.some(r=>typeof r!="number")))throw new Error(`Invalid video transformation matrix: ${s.join()}`)}if(e.audio&&!ms.includes(e.audio.codec))throw new Error(`Unsupported audio codec: ${e.audio.codec}`);if(e.firstTimestampBehavior&&!us.includes(e.firstTimestampBehavior))throw new Error(`Invalid first timestamp behavior: ${e.firstTimestampBehavior}`);if(typeof e.fastStart=="object"){if(e.video&&e.fastStart.expectedVideoChunks===void 0)throw new Error("'fastStart' is an object but is missing property 'expectedVideoChunks'.");if(e.audio&&e.fastStart.expectedAudioChunks===void 0)throw new Error("'fastStart' is an object but is missing property 'expectedAudioChunks'.")}else if(![!1,"in-memory","fragmented"].includes(e.fastStart))throw new Error("'fastStart' option must be false, 'in-memory', 'fragmented' or an object.")},_e=new WeakSet,lt=function(){if(i(this,u).writeBox(tt({holdsAvc:i(this,h).video?.codec==="avc",fragmented:i(this,h).fastStart==="fragmented"})),S(this,ce,i(this,u).pos),i(this,h).fastStart==="in-memory")S(this,A,we(!1));else if(i(this,h).fastStart!=="fragmented"){if(typeof i(this,h).fastStart=="object"){let e=p(this,Ie,mt).call(this);i(this,u).seek(i(this,u).pos+e)}S(this,A,we(!0)),i(this,u).writeBox(i(this,A))}p(this,G,pe).call(this)},Ie=new WeakSet,mt=function(){if(typeof i(this,h).fastStart!="object")return;let e=0,s=[i(this,h).fastStart.expectedVideoChunks,i(this,h).fastStart.expectedAudioChunks];for(let r of s)r&&(e+=(4+4)*Math.ceil(2/3*r),e+=4*r,e+=(4+4+4)*Math.ceil(2/3*r),e+=4*r,e+=8*r);return e+=4096,e},Me=new WeakSet,ht=function(){if(i(this,h).video&&S(this,x,{id:1,info:{type:"video",codec:i(this,h).video.codec,width:i(this,h).video.width,height:i(this,h).video.height,rotation:i(this,h).video.rotation??0,decoderConfig:null},timescale:11520,samples:[],finalizedChunks:[],currentChunk:null,firstDecodeTimestamp:void 0,lastDecodeTimestamp:-1,timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,compactlyCodedChunkTable:[]}),i(this,h).audio){let e=p(this,Pe,ut).call(this,2,i(this,h).audio.sampleRate,i(this,h).audio.numberOfChannels);S(this,w,{id:i(this,h).video?2:1,info:{type:"audio",codec:i(this,h).audio.codec,numberOfChannels:i(this,h).audio.numberOfChannels,sampleRate:i(this,h).audio.sampleRate,decoderConfig:{codec:i(this,h).audio.codec,description:e,numberOfChannels:i(this,h).audio.numberOfChannels,sampleRate:i(this,h).audio.sampleRate}},timescale:i(this,h).audio.sampleRate,samples:[],finalizedChunks:[],currentChunk:null,firstDecodeTimestamp:void 0,lastDecodeTimestamp:-1,timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,compactlyCodedChunkTable:[]})}},Pe=new WeakSet,ut=function(e,s,r){let a=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350].indexOf(s),l=r,m="";m+=e.toString(2).padStart(5,"0"),m+=a.toString(2).padStart(4,"0"),a===15&&(m+=s.toString(2).padStart(24,"0")),m+=l.toString(2).padStart(4,"0");let d=Math.ceil(m.length/8)*8;m=m.padEnd(d,"0");let T=new Uint8Array(m.length/8);for(let v=0;v<m.length;v+=8)T[v/8]=parseInt(m.slice(v,v+8),2);return T},Te=new WeakSet,Ge=function(e,s,r,n,a,l,m){let d=n/1e6,T=(n-(m??0))/1e6,v=a/1e6,F=p(this,Ve,ft).call(this,d,T,e);return d=F.presentationTimestamp,T=F.decodeTimestamp,l?.decoderConfig&&(e.info.decoderConfig===null?e.info.decoderConfig=l.decoderConfig:Object.assign(e.info.decoderConfig,l.decoderConfig)),{presentationTimestamp:d,decodeTimestamp:T,duration:v,data:s,size:s.byteLength,type:r,timescaleUnitsToNextSample:B(v,e.timescale)}},D=new WeakSet,I=function(e,s){i(this,h).fastStart!=="fragmented"&&e.samples.push(s);let r=B(s.presentationTimestamp-s.decodeTimestamp,e.timescale);if(e.lastTimescaleUnits!==null){let a=B(s.decodeTimestamp,e.timescale,!1),l=Math.round(a-e.lastTimescaleUnits);if(e.lastTimescaleUnits+=l,e.lastSample.timescaleUnitsToNextSample=l,i(this,h).fastStart!=="fragmented"){let m=K(e.timeToSampleTable);m.sampleCount===1?(m.sampleDelta=l,m.sampleCount++):m.sampleDelta===l?m.sampleCount++:(m.sampleCount--,e.timeToSampleTable.push({sampleCount:2,sampleDelta:l}));let d=K(e.compositionTimeOffsetTable);d.sampleCompositionTimeOffset===r?d.sampleCount++:e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:r})}}else e.lastTimescaleUnits=0,i(this,h).fastStart!=="fragmented"&&(e.timeToSampleTable.push({sampleCount:1,sampleDelta:B(s.duration,e.timescale)}),e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:r}));e.lastSample=s;let n=!1;if(!e.currentChunk)n=!0;else{let a=s.presentationTimestamp-e.currentChunk.startTimestamp;if(i(this,h).fastStart==="fragmented"){let l=i(this,x)??i(this,w);e===l&&s.type==="key"&&a>=1&&(n=!0,p(this,Ce,Ze).call(this))}else n=a>=.5}n&&(e.currentChunk&&p(this,te,ze).call(this,e),e.currentChunk={startTimestamp:s.presentationTimestamp,samples:[]}),e.currentChunk.samples.push(s)},Ve=new WeakSet,ft=function(e,s,r){let n=i(this,h).firstTimestampBehavior==="strict",a=r.lastDecodeTimestamp===-1;if(n&&a&&s!==0)throw new Error(`The first chunk for your media track must have a timestamp of 0 (received DTS=${s}).Non-zero first timestamps are often caused by directly piping frames or audio data from a MediaStreamTrack into the encoder. Their timestamps are typically relative to the age of thedocument, which is probably what you want.
|
|
|
|
If you want to offset all timestamps of a track such that the first one is zero, set firstTimestampBehavior: 'offset' in the options.
|
|
`);if(i(this,h).firstTimestampBehavior==="offset"||i(this,h).firstTimestampBehavior==="cross-track-offset"){r.firstDecodeTimestamp===void 0&&(r.firstDecodeTimestamp=s);let m;i(this,h).firstTimestampBehavior==="offset"?m=r.firstDecodeTimestamp:m=Math.min(i(this,x)?.firstDecodeTimestamp??1/0,i(this,w)?.firstDecodeTimestamp??1/0),s-=m,e-=m}if(s<r.lastDecodeTimestamp)throw new Error(`Timestamps must be monotonically increasing (DTS went from ${r.lastDecodeTimestamp*1e6} to ${s*1e6}).`);return r.lastDecodeTimestamp=s,{presentationTimestamp:e,decodeTimestamp:s}},te=new WeakSet,ze=function(e){if(i(this,h).fastStart==="fragmented")throw new Error("Can't finalize individual chunks 'fastStart' is set to 'fragmented'.");if(e.currentChunk){if(e.finalizedChunks.push(e.currentChunk),i(this,X).push(e.currentChunk),(e.compactlyCodedChunkTable.length===0||K(e.compactlyCodedChunkTable).samplesPerChunk!==e.currentChunk.samples.length)&&e.compactlyCodedChunkTable.push({firstChunk:e.finalizedChunks.length,samplesPerChunk:e.currentChunk.samples.length}),i(this,h).fastStart==="in-memory"){e.currentChunk.offset=0;return}e.currentChunk.offset=i(this,u).pos;for(let s of e.currentChunk.samples)i(this,u).write(s.data),s.data=null;p(this,G,pe).call(this)}},Ce=new WeakSet,Ze=function(e=!0){if(i(this,h).fastStart!=="fragmented")throw new Error("Can't finalize a fragment unless 'fastStart' is set to 'fragmented'.");let s=[i(this,x),i(this,w)].filter(d=>d&&d.currentChunk);if(s.length===0)return;let r=Ye(this,De)._++;if(r===1){let d=ie(s,i(this,W),!0);i(this,u).writeBox(d)}let n=i(this,u).pos,a=qe(r,s);i(this,u).writeBox(a);{let d=we(!1),T=0;for(let F of s)for(let se of F.currentChunk.samples)T+=se.size;let v=i(this,u).measureBox(d)+T;v>=2**32&&(d.largeSize=!0,v=i(this,u).measureBox(d)+T),d.size=v,i(this,u).writeBox(d)}for(let d of s){d.currentChunk.offset=i(this,u).pos,d.currentChunk.moofOffset=n;for(let T of d.currentChunk.samples)i(this,u).write(T.data),T.data=null}let l=i(this,u).pos;i(this,u).seek(i(this,u).offsets.get(a));let m=qe(r,s);i(this,u).writeBox(m),i(this,u).seek(l);for(let d of s)d.finalizedChunks.push(d.currentChunk),i(this,X).push(d.currentChunk),d.currentChunk=null;e&&p(this,G,pe).call(this)},G=new WeakSet,pe=function(){i(this,u)instanceof oe&&i(this,u).flush()},be=new WeakSet,Ke=function(){if(i(this,ee))throw new Error("Cannot add new video or audio chunks after the file has been finalized.")};return bt(fs);})();
|
|
if (typeof module === "object" && typeof module.exports === "object") Object.assign(module.exports, Mp4Muxer)
|