@ts-videos/mp4 0.1.2 → 0.2.2
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/dist/aac.d.ts +18 -0
- package/dist/aac.js +1 -0
- package/dist/aac.test.d.ts +1 -0
- package/dist/aac.test.js +1 -0
- package/dist/boxes.js +1 -1
- package/dist/chunk-0ea9sv8z.js +2 -0
- package/dist/chunk-30x59cyt.js +2 -0
- package/dist/chunk-5sfmrnz9.js +2 -0
- package/dist/chunk-cbtc0x29.js +2 -0
- package/dist/chunk-cmq2ntnd.js +2 -0
- package/dist/chunk-r455c378.js +2 -0
- package/dist/demuxer.js +1 -1
- package/dist/fragmented.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/muxer.js +1 -1
- package/dist/prores.test.js +1 -1
- package/package.json +2 -2
- package/dist/chunk-c9md36vm.js +0 -2
- package/dist/chunk-m7vh938z.js +0 -2
- package/dist/chunk-qmjyh9jc.js +0 -2
- package/dist/chunk-tghqqx5m.js +0 -2
- package/dist/chunk-yjtc1ksx.js +0 -2
package/dist/aac.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Whether bytes are an `esds` box's contents (version, flags, then an ES descriptor) rather than a bare AudioSpecificConfig. */
|
|
2
|
+
export declare function isEsdsPayload(bytes: Uint8Array): boolean;
|
|
3
|
+
/** The AudioSpecificConfig inside an `esds` box's contents, or undefined when there is none. */
|
|
4
|
+
export declare function audioSpecificConfigFromEsds(payload: Uint8Array): Uint8Array | undefined;
|
|
5
|
+
/** What an AudioSpecificConfig says of the sound: its rate and channels, undefined where it does not state them plainly. */
|
|
6
|
+
export declare function readAudioSpecificConfig(config: Uint8Array): { sampleRate?: number, channels?: number };
|
|
7
|
+
/** An AAC-LC AudioSpecificConfig for a rate and channel count, for a track that came with none. */
|
|
8
|
+
export declare function buildAudioSpecificConfig(sampleRate: number, channels: number): Uint8Array;
|
|
9
|
+
/** An `esds` box's contents around an AudioSpecificConfig. */
|
|
10
|
+
export declare function buildEsdsPayload(config: Uint8Array, bitrate?: number): Uint8Array;
|
|
11
|
+
/**
|
|
12
|
+
* The bytes an MP4 `esds` box holds for an AAC track, whatever the track came
|
|
13
|
+
* with: an AudioSpecificConfig (a WebCodecs encoder, a Matroska file, this
|
|
14
|
+
* demuxer) is wrapped, a whole `esds` payload (what this demuxer gave before)
|
|
15
|
+
* is kept, and a track with no record gets an AAC-LC one for its rate and
|
|
16
|
+
* channels. Written raw, an AudioSpecificConfig made a file no decoder opened.
|
|
17
|
+
*/
|
|
18
|
+
export declare function esdsPayloadFor(track: { codecDescription?: Uint8Array, sampleRate: number, channels: number, bitrate?: number }): Uint8Array;
|
package/dist/aac.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{d,r,m,s,p,i}from"./chunk-cmq2ntnd.js";export{r as audioSpecificConfigFromEsds,s as buildAudioSpecificConfig,p as buildEsdsPayload,i as esdsPayloadFor,d as isEsdsPayload,m as readAudioSpecificConfig};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/aac.test.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{d,r,m,s,p,i}from"./chunk-cmq2ntnd.js";import{describe as o,expect as e,it as n}from"bun:test";o("the AAC decoder record",()=>{n("writes AAC-LC for the common rates",()=>{e(s(44100,2)).toEqual(new Uint8Array([18,16])),e(s(48000,1)).toEqual(new Uint8Array([17,136])),e(m(s(48000,2))).toEqual({sampleRate:48000,channels:2}),e(m(s(44100,8))).toEqual({sampleRate:44100,channels:8})}),n("states a rate outside the table in full",()=>{e(m(s(46875,2))).toEqual({sampleRate:46875,channels:2})}),n("wraps a record in an esds and finds it again",()=>{let a=new Uint8Array([17,144]),t=p(a,128000);e(d(t)).toBe(!0),e(d(a)).toBe(!1),e(r(t)).toEqual(a)}),n("reads an esds written with one-byte lengths, as ffmpeg and iPhones write it",()=>{let a=new Uint8Array([0,0,0,0,3,25,0,1,0,4,17,64,21,0,0,0,0,1,244,0,0,1,244,0,5,2,17,144,6,1,2]);e(r(a)).toEqual(new Uint8Array([17,144]))}),n("gives a muxer an esds whatever the track came with",()=>{let a=new Uint8Array([17,144]),t=p(a);e(r(i({codecDescription:a,sampleRate:48000,channels:2}))).toEqual(a),e(i({codecDescription:t,sampleRate:48000,channels:2})).toBe(t),e(r(i({sampleRate:44100,channels:2}))).toEqual(new Uint8Array([18,16]))})});
|
package/dist/boxes.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{t,
|
|
1
|
+
import{t,l,h,v,B,y,b,c,u,S,U,w,e}from"./chunk-r455c378.js";export{t as BOX_HEADER_SIZE,B as CONTAINER_BOXES,l as EXTENDED_BOX_HEADER_SIZE,b as MOV_BRANDS,y as MP4_BRANDS,e as TIMESCALE_1904_TO_1970,w as encodeLanguageCode,u as isAudioHandler,S as isSubtitleHandler,c as isVideoHandler,U as parseLanguageCode,h as readFourCC,v as writeFourCC};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{r}from"./chunk-cmq2ntnd.js";import{t,l,h,B,c,u,U}from"./chunk-r455c378.js";import{Demuxer as q}from"ts-videos/demuxer";class n extends q{movieTimescale=1000;movieDuration=0n;trackInfos=[];moovBox=null;mdatOffset=0;fragmented=!1;_initialized=!1;majorBrand="";get formatName(){return this.majorBrand==="qt "?"mov":"mp4"}get mimeType(){let s=this._tracks?.find((o)=>o.type==="video"),e=this._tracks?.find((o)=>o.type==="audio"),a="";if(s&&e)a=`${this.getCodecString(s)}, ${this.getCodecString(e)}`;else if(s)a=this.getCodecString(s);else if(e)a=this.getCodecString(e);let i=this.majorBrand==="qt "?"video/quicktime":"video/mp4";return a?`${i}; codecs="${a}"`:i}getCodecString(s){if(s.type==="video"){let e=s;if(e.codec==="h264"&&e.codecDescription){let a=e.codecDescription;if(a.length>=4){let i=a[1].toString(16).padStart(2,"0"),o=a[2].toString(16).padStart(2,"0"),d=a[3].toString(16).padStart(2,"0");return`avc1.${i}${o}${d}`}return"avc1"}if(e.codec==="h265")return"hev1";if(e.codec==="vp9")return"vp09";if(e.codec==="av1")return"av01";return e.codec}if(s.type==="audio"){let e=s;if(e.codec==="aac")return"mp4a.40.2";if(e.codec==="mp3")return"mp4a.40.34";if(e.codec==="opus")return"opus";if(e.codec==="flac")return"flac";return e.codec}return"unknown"}async init(){if(this._initialized)return;this._initialized=!0,await this.parseTopLevelBoxes(),await this.buildTracks(),await this.buildSampleTables()}async parseTopLevelBoxes(){let s=await this.reader.getSize(),e=0;while(s===null||e<s){this.reader.position=e;let a=await this.reader.readU32BE();if(a===null)break;let i=await this.reader.readFourCC();if(!i)break;let o=a;if(o===1){let d=await this.reader.readU64BE();if(d===null)break;o=Number(d)}else if(o===0)o=(s??0)-e;if(i==="ftyp")await this.parseFtyp(e,o);else if(i==="moov")this.moovBox={type:i,size:o,offset:e},await this.parseMoov(e,o);else if(i==="mdat")this.mdatOffset=e+t;else if(i==="moof")this.fragmented=!0;e+=o}}async parseFtyp(s,e){this.reader.position=s+t,this.majorBrand=await this.reader.readFourCC()??"",await this.reader.readU32BE();let a=(e-16)/4;for(let i=0;i<a;i++)await this.reader.readFourCC()}async parseMoov(s,e){await this.parseContainerBox(s+t,e-t)}async parseContainerBox(s,e){let a=[],i=s,o=s+e;while(i<o){this.reader.position=i;let d=await this.reader.readU32BE();if(d===null)break;let m=await this.reader.readFourCC();if(!m)break;let b=d,C=t;if(b===1){let x=await this.reader.readU64BE();if(x===null)break;b=Number(x),C=l}else if(b===0)b=o-i;let g={type:m,size:b,offset:i};if(B.has(m))g.children=await this.parseContainerBox(i+C,b-C);a.push(g),i+=b}return a}async buildTracks(){if(!this.moovBox)throw Error("No moov box found");let s=await this.parseContainerBox(this.moovBox.offset+t,this.moovBox.size-t),e=s.find((i)=>i.type==="mvhd");if(e)await this.parseMvhd(e);let a=s.filter((i)=>i.type==="trak");this._tracks=[];for(let i=0;i<a.length;i++){let o=await this.parseTrack(a[i],i);if(o)this._tracks.push(o)}this._metadata=await this.parseMetadata(s),this._duration=Number(this.movieDuration)/this.movieTimescale}async parseMvhd(s){this.reader.position=s.offset+t;let e=await this.reader.readU8();if(e===null)return;if(await this.reader.skip(3),e===1)await this.reader.skip(16),this.movieTimescale=await this.reader.readU32BE()??1000,this.movieDuration=await this.reader.readU64BE()??0n;else await this.reader.skip(8),this.movieTimescale=await this.reader.readU32BE()??1000,this.movieDuration=BigInt(await this.reader.readU32BE()??0)}async parseTrack(s,e){let a=s.children??[],i=a.find((p)=>p.type==="tkhd"),o=a.find((p)=>p.type==="mdia");if(!i||!o)return null;let d=await this.parseTkhd(i),m=o.children??[],b=m.find((p)=>p.type==="mdhd"),C=m.find((p)=>p.type==="hdlr"),g=m.find((p)=>p.type==="minf");if(!b||!C||!g)return null;let x=await this.parseMdhd(b),k=await this.parseHdlr(C),S=(g.children??[]).find((p)=>p.type==="stbl");if(!S)return null;let v=S.children??[],f=v.find((p)=>p.type==="stsd");if(!f)return null;let E=await this.parseStsd(f,k.handlerType),y={id:d.trackId,index:e,type:c(k.handlerType)?"video":u(k.handlerType)?"audio":"subtitle",timescale:x.timescale,duration:x.duration,samples:[],currentSampleIndex:0};if(this.trackInfos.push(y),c(k.handlerType)&&E.entries.length>0){let p=E.entries[0];return{type:"video",id:d.trackId,index:e,codec:this.getVideoCodec(p.type),width:p.width,height:p.height,profile:this.getVideoProfile(p.type),frameRate:await this.calculateFrameRate(v,x.timescale),isDefault:(d.flags&1)!==0,language:x.language,codecDescription:this.extractCodecDescription(p),colorSpace:this.extractColorSpace(p),rotation:this.getRotation(d.matrix)}}if(u(k.handlerType)&&E.entries.length>0){let p=E.entries[0];return{type:"audio",id:d.trackId,index:e,codec:this.getAudioCodec(p.type),sampleRate:p.sampleRate,channels:p.channelCount,bitsPerSample:p.sampleSize,isDefault:(d.flags&1)!==0,language:x.language,codecDescription:this.extractCodecDescription(p)}}return null}async parseTkhd(s){this.reader.position=s.offset+t;let e=await this.reader.readU8(),a=(await this.reader.readU8()??0)<<16|(await this.reader.readU8()??0)<<8|(await this.reader.readU8()??0),i=0;if(e===1)await this.reader.skip(16),i=await this.reader.readU32BE()??0,await this.reader.skip(12);else await this.reader.skip(8),i=await this.reader.readU32BE()??0,await this.reader.skip(8);await this.reader.skip(16);let o=[];for(let b=0;b<9;b++)o.push(await this.reader.readI32BE()??0);let d=(await this.reader.readU32BE()??0)/65536,m=(await this.reader.readU32BE()??0)/65536;return{trackId:i,flags:a,width:d,height:m,matrix:o}}async parseMdhd(s){this.reader.position=s.offset+t;let e=await this.reader.readU8();await this.reader.skip(3);let a=1000,i=0n;if(e===1)await this.reader.skip(16),a=await this.reader.readU32BE()??1000,i=await this.reader.readU64BE()??0n;else await this.reader.skip(8),a=await this.reader.readU32BE()??1000,i=BigInt(await this.reader.readU32BE()??0);let o=await this.reader.readU16BE()??0,d=U(o);return{timescale:a,duration:i,language:d}}async parseHdlr(s){this.reader.position=s.offset+t,await this.reader.skip(8);let e=await this.reader.readFourCC()??"";await this.reader.skip(12);let a=s.size-t-24,i=a>0?await this.reader.readString(a)??"":"";return{handlerType:e,name:i.replace(/\0/g,"")}}async parseStsd(s,e){this.reader.position=s.offset+t,await this.reader.skip(4);let a=await this.reader.readU32BE()??0,i=[];for(let o=0;o<a;o++){let d=await this.reader.readU32BE()??0,m=await this.reader.readFourCC()??"",b=this.reader.position;await this.reader.skip(6);let C=await this.reader.readU16BE()??0;if(c(e)){await this.reader.skip(16);let g=await this.reader.readU16BE()??0,x=await this.reader.readU16BE()??0,k=await this.reader.readU32BE()??0,T=await this.reader.readU32BE()??0;await this.reader.skip(4);let S=await this.reader.readU16BE()??1,v=await this.reader.readU8()??0,f=await this.reader.readString(31)??"",E=await this.reader.readU16BE()??24;await this.reader.skip(2);let y=await this.parseExtensions(b+d-8);i.push({type:m,dataReferenceIndex:C,width:g,height:x,horizResolution:k,vertResolution:T,frameCount:S,compressorName:f.slice(0,v),depth:E,extensions:y,data:new Uint8Array(0)})}else if(u(e)){let g=await this.reader.readU16BE()??0;await this.reader.skip(6);let x=await this.reader.readU16BE()??2,k=await this.reader.readU16BE()??16;await this.reader.skip(4);let T=(await this.reader.readU32BE()??2890137600)>>>16;if(g===1)await this.reader.skip(16);else if(g===2){await this.reader.skip(4);let v=await this.reader.readBytes(8);if(v)T=Math.round(new DataView(v.buffer,v.byteOffset,8).getFloat64(0))||T;x=await this.reader.readU32BE()??x,await this.reader.skip(20)}let S=await this.parseExtensions(b+d-8);for(let v of S.filter((f)=>f.type==="wave"&&f.data)){let f=v.data,E=new DataView(f.buffer,f.byteOffset,f.byteLength);for(let y=0;y+8<=f.byteLength;){let p=E.getUint32(y);if(p<8||y+p>f.byteLength)break;let I=String.fromCharCode(f[y+4],f[y+5],f[y+6],f[y+7]);if(I==="esds")S.push({type:I,size:p,offset:v.offset+8+y,data:f.subarray(y+8,y+p)});y+=p}}i.push({type:m,dataReferenceIndex:C,channelCount:x,sampleSize:k,sampleRate:T,extensions:S,data:new Uint8Array(0)})}this.reader.position=b+d-8}return{entries:i}}async parseExtensions(s){let e=[];while(this.reader.position<s){let a=await this.reader.readU32BE()??0;if(a<8)break;let i=await this.reader.readFourCC()??"",o=await this.reader.readBytes(a-8);e.push({type:i,size:a,offset:this.reader.position-a,data:o??void 0})}return e}getVideoCodec(s){switch(s){case"avc1":case"avc3":return"h264";case"hev1":case"hvc1":return"h265";case"vp08":return"vp8";case"vp09":return"vp9";case"av01":return"av1";case"mp4v":return"mpeg4";case"apco":case"apcs":case"apcn":case"apch":case"ap4h":case"ap4x":return"prores";default:return"unknown"}}getVideoProfile(s){switch(s){case"apco":return"proxy";case"apcs":return"lt";case"apcn":return"422";case"apch":return"hq";case"ap4h":return"4444";case"ap4x":return"4444-xq";default:return}}extractColorSpace(s){let e=s.extensions.find((b)=>b.type==="colr")?.data;if(!e||e.length<10)return;let a=new DataView(e.buffer,e.byteOffset,e.byteLength),i=h(a,0);if(i!=="nclx"&&i!=="nclc")return;if(i==="nclx"&&e.length<11)return;let o=new Map([[1,"bt709"],[4,"bt470m"],[5,"bt470bg"],[6,"smpte170m"],[7,"smpte240m"],[8,"film"],[9,"bt2020"],[10,"smpte428"],[11,"smpte431"],[12,"smpte432"],[22,"ebu3213"]]).get(a.getUint16(4)),d=new Map([[1,"bt709"],[4,"bt470m"],[5,"bt470bg"],[6,"smpte170m"],[7,"smpte240m"],[8,"linear"],[9,"log100"],[10,"log316"],[11,"iec61966-2-4"],[12,"bt1361e"],[13,"iec61966-2-1"],[14,"bt2020-10"],[15,"bt2020-12"],[16,"smpte2084"],[17,"smpte428"],[18,"arib-std-b67"]]).get(a.getUint16(6)),m=new Map([[0,"rgb"],[1,"bt709"],[4,"fcc"],[5,"bt470bg"],[6,"smpte170m"],[7,"smpte240m"],[8,"ycocg"],[9,"bt2020nc"],[10,"bt2020c"],[11,"smpte2085"],[12,"chroma-derived-nc"],[13,"chroma-derived-c"],[14,"ictcp"]]).get(a.getUint16(8));if(!o&&!d&&!m)return;return{primaries:o,transfer:d,matrix:m,range:i==="nclx"?(e[10]&128)!==0?"full":"limited":void 0}}getAudioCodec(s){switch(s){case"mp4a":return"aac";case".mp3":case"mp3 ":return"mp3";case"Opus":return"opus";case"fLaC":return"flac";case"alac":return"alac";case"ac-3":return"ac3";case"ec-3":return"eac3";default:return"unknown"}}extractCodecDescription(s){for(let e of s.extensions){if(e.type==="avcC"||e.type==="hvcC"||e.type==="av1C")return e.data;if(e.type==="esds"&&e.data)return r(e.data)??e.data}return}getRotation(s){let e=s[0]/65536,a=s[1]/65536,i=s[3]/65536,o=s[4]/65536;if(Math.abs(e-1)<0.01&&Math.abs(o-1)<0.01)return 0;if(Math.abs(a-1)<0.01&&Math.abs(i+1)<0.01)return 90;if(Math.abs(e+1)<0.01&&Math.abs(o+1)<0.01)return 180;if(Math.abs(a+1)<0.01&&Math.abs(i-1)<0.01)return 270;return 0}async calculateFrameRate(s,e){let a=s.find((d)=>d.type==="stts");if(!a||!e)return;let i=0,o=0;for(let{sampleCount:d,sampleDelta:m}of await this.parseStts(a))i+=d,o+=d*m;if(i===0||o===0)return;return Math.round(i*e/o*1000)/1000}async parseMetadata(s){return{}}async buildSampleTables(){if(!this.moovBox)return;let e=(await this.parseContainerBox(this.moovBox.offset+t,this.moovBox.size-t)).filter((a)=>a.type==="trak");for(let a=0;a<e.length&&a<this.trackInfos.length;a++)await this.buildSampleTable(e[a],this.trackInfos[a])}async buildSampleTable(s,e){let i=(s.children??[]).find((w)=>w.type==="mdia");if(!i?.children)return;let o=i.children.find((w)=>w.type==="minf");if(!o?.children)return;let d=o.children.find((w)=>w.type==="stbl");if(!d?.children)return;let m=d.children,b=m.find((w)=>w.type==="stts"),C=m.find((w)=>w.type==="ctts"),g=m.find((w)=>w.type==="stsc"),x=m.find((w)=>w.type==="stsz"),k=m.find((w)=>w.type==="stco"),T=m.find((w)=>w.type==="co64"),S=m.find((w)=>w.type==="stss");if(!b||!g||!x||!k&&!T)return;let v=await this.parseStts(b),f=C?await this.parseCtts(C):null,E=await this.parseStsc(g),y=await this.parseStsz(x),p=k?await this.parseStco(k):await this.parseCo64(T),I=S?new Set(await this.parseStss(S)):null,D=0,O=0,z=0,M=v.length>0?v[0].sampleCount:0,P=0,A=f&&f.length>0?f[0].sampleCount:0;for(let w=0;w<p.length;w++){let R=p[w],L=this.getSamplesPerChunk(E,w+1),V=0;for(let _=0;_<L;_++){let N=y.sampleSize>0?y.sampleSize:y.entrySizes[D]??0,F=v[z]?.sampleDelta??0,H=f&&f[P]?f[P].sampleOffset:0,j=I===null||I.has(D+1);if(e.samples.push({offset:R+V,size:N,timestamp:O/e.timescale,duration:F/e.timescale,isKeyframe:j,compositionTimeOffset:H/e.timescale}),V+=N,O+=F,D++,M--,M===0&&z<v.length-1)z++,M=v[z].sampleCount;if(f){if(A--,A===0&&P<f.length-1)P++,A=f[P].sampleCount}}}}async parseStts(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,a=[];for(let i=0;i<e;i++){let o=await this.reader.readU32BE()??0,d=await this.reader.readU32BE()??0;a.push({sampleCount:o,sampleDelta:d})}return a}async parseCtts(s){this.reader.position=s.offset+t;let e=await this.reader.readU8()??0;await this.reader.skip(3);let a=await this.reader.readU32BE()??0,i=[];for(let o=0;o<a;o++){let d=await this.reader.readU32BE()??0,m=e===1?await this.reader.readI32BE()??0:await this.reader.readU32BE()??0;i.push({sampleCount:d,sampleOffset:m})}return i}async parseStsc(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,a=[];for(let i=0;i<e;i++){let o=await this.reader.readU32BE()??0,d=await this.reader.readU32BE()??0,m=await this.reader.readU32BE()??0;a.push({firstChunk:o,samplesPerChunk:d,sampleDescriptionIndex:m})}return a}async parseStsz(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,a=await this.reader.readU32BE()??0,i=[];if(e===0)for(let o=0;o<a;o++)i.push(await this.reader.readU32BE()??0);return{sampleSize:e,sampleCount:a,entrySizes:i}}async parseStco(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,a=[];for(let i=0;i<e;i++)a.push(await this.reader.readU32BE()??0);return a}async parseCo64(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,a=[];for(let i=0;i<e;i++)a.push(Number(await this.reader.readU64BE()??0n));return a}async parseStss(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,a=[];for(let i=0;i<e;i++)a.push(await this.reader.readU32BE()??0);return a}getSamplesPerChunk(s,e){for(let a=s.length-1;a>=0;a--)if(e>=s[a].firstChunk)return s[a].samplesPerChunk;return 1}async readPacket(s){let e=this.trackInfos.find((o)=>o.id===s);if(!e||e.currentSampleIndex>=e.samples.length)return null;let a=e.samples[e.currentSampleIndex];e.currentSampleIndex++,this.reader.position=a.offset;let i=await this.reader.readBytes(a.size);if(!i)return null;return{data:i,timestamp:a.timestamp,duration:a.duration,isKeyframe:a.isKeyframe,trackId:s,compositionTimeOffset:a.compositionTimeOffset}}async seek(s){for(let e of this.trackInfos){let a=0;for(let i=0;i<e.samples.length;i++){if(e.samples[i].timestamp>s)break;if(e.samples[i].isKeyframe)a=i}e.currentSampleIndex=a}}}
|
|
2
|
+
export{n};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{i}from"./chunk-cmq2ntnd.js";import{w,e}from"./chunk-r455c378.js";import{createMp4ChapterAtom as Z}from"ts-videos/chapters";import{Muxer as tt}from"ts-videos/muxer";import{Writer as B}from"ts-videos/writer";var L=1000,et=0.5;class a extends tt{trackData=new Map;options;mdatStart=0;mdatSize=0;chapters=[];artwork=null;artworkType="jpeg";constructor(r,t={}){super(r);this.options={fastStart:t.fastStart??!0,fragmented:t.fragmented??!1,brand:t.brand??"isom"}}addChapter(r,t){this.chapters.push({title:r,startTimeMs:t})}setArtwork(r,t="jpeg"){this.artwork=r,this.artworkType=t}get formatName(){return"mp4"}get mimeType(){return"video/mp4"}onTrackAdded(r){let t=r.type==="video"?Math.round((r.config.frameRate??30)*1000):r.type==="audio"?r.config.sampleRate:1000;this.trackData.set(r.id,{track:r,timescale:t,samples:[]})}async writeHeader(){if(await this.writeFtyp(),!this.options.fastStart)this.mdatStart=this.writer.position,await this.writer.writeU32BE(1),await this.writer.writeFourCC("mdat"),await this.writer.writeU64BE(16n)}async writeFtyp(){let r=[this.options.brand,"isom","iso2","mp42"],t=16+r.length*4;await this.writer.writeU32BE(t),await this.writer.writeFourCC("ftyp"),await this.writer.writeFourCC(this.options.brand),await this.writer.writeU32BE(512);for(let s of r)await this.writer.writeFourCC(s)}async writeMdatHeader(r){if(r>4294967287)await this.writer.writeU32BE(1),await this.writer.writeFourCC("mdat"),await this.writer.writeU64BE(BigInt(r+16));else await this.writer.writeU32BE(r+8),await this.writer.writeFourCC("mdat")}async writeVideoPacket(r,t){await this.writeSample(r.id,t)}async writeAudioPacket(r,t){await this.writeSample(r.id,t)}async writeSubtitlePacket(r,t){await this.writeSample(r.id,t)}async writeSample(r,t){let s=this.trackData.get(r);if(!s)return;let n={data:t.data,timestamp:t.timestamp,duration:t.duration??0,isKeyframe:t.isKeyframe,compositionTimeOffset:t.compositionTimeOffset??0,size:t.data.byteLength,offset:0};if(this.options.fastStart)s.samples.push(n);else n.offset=this.writer.position,await this.writer.writeBytes(t.data),this.mdatSize+=t.data.byteLength,s.samples.push(n)}async writeTrailer(){if(this.options.fastStart){let r=this.layOutSamples(),t=await this.buildMoov(0);this.mdatStart=this.writer.position+t.byteLength+8;let s=await this.buildMoov(this.mdatStart);await this.writer.writeBytes(s);let n=this.chapters.length>0?this.buildChapterTextSamples():[],o=0;for(let c of this.trackData.values())for(let u of c.samples)o+=u.size;for(let c of n)o+=c.data.byteLength;await this.writeMdatHeader(o);for(let c of r)await this.writer.writeBytes(c.data);for(let c of n)await this.writer.writeBytes(c.data)}else{let r=new Uint8Array(8);new DataView(r.buffer).setBigUint64(0,BigInt(16+this.mdatSize)),await this.writer.patch(this.mdatStart+8,r);let t=await this.buildMoov(this.mdatStart+16);await this.writer.writeBytes(t)}}layOutSamples(){let r=[],t=0;for(let n of this.trackData.values()){let o=0;n.samples.forEach((c,u)=>{r.push({sample:c,stretch:Math.floor(o/et+0.000000001),track:t,index:u}),o+=c.duration}),t++}r.sort((n,o)=>n.stretch-o.stretch||n.track-o.track||n.index-o.index);let s=0;for(let{sample:n}of r)n.offset=s,s+=n.size;return r.map((n)=>n.sample)}chunksOf(r){let t=[],s=-1;for(let n of r.samples){let o=t[t.length-1];if(o&&n.offset===s)o.samples++;else t.push({offset:n.offset,samples:1});s=n.offset+n.size}return t}async buildMoov(r){let t=new B,s=await this.buildMvhd(),n=[],o=0;for(let U of this.trackData.values())for(let p of U.samples)o+=p.size;let c=this.chapters.length>0,u=c?this.tracks.length+1:0,h=c?this.buildChapterTextSamples():[];for(let U of this.trackData.values()){let p=U.track.type!=="subtitle"&&c?u:void 0,E=await this.buildTrak(U,r,p);n.push(E)}if(c&&h.length>0){let U=r+o,p=await this.buildChapterTrak(u,h,U);n.push(p)}let l=await this.buildUdta(),m=s.byteLength+n.reduce((U,p)=>U+p.byteLength,0);if(l)m+=l.byteLength;let d=m+8;await t.writeU32BE(d),await t.writeFourCC("moov"),await t.writeBytes(s);for(let U of n)await t.writeBytes(U);if(l)await t.writeBytes(l);return t.getBuffer()}async buildMvhd(){let r=new B,t=BigInt(Math.floor(Date.now()/1000))+e,s=0n;for(let c of this.trackData.values()){let u=c.samples[c.samples.length-1];if(u){let h=BigInt(Math.round((u.timestamp+u.duration)*L));if(h>s)s=h}}let n=108,o=this.chapters.length>0;await r.writeU32BE(n),await r.writeFourCC("mvhd"),await r.writeU8(0),await r.writeU8(0),await r.writeU8(0),await r.writeU8(0),await r.writeU32BE(Number(t&0xFFFFFFFFn)),await r.writeU32BE(Number(t&0xFFFFFFFFn)),await r.writeU32BE(L),await r.writeU32BE(Number(s&0xFFFFFFFFn)),await r.writeU32BE(65536),await r.writeU16BE(256),await r.writeU16BE(0),await r.writeU32BE(0),await r.writeU32BE(0),await r.writeU32BE(65536),await r.writeU32BE(0),await r.writeU32BE(0),await r.writeU32BE(0),await r.writeU32BE(65536),await r.writeU32BE(0),await r.writeU32BE(0),await r.writeU32BE(0),await r.writeU32BE(1073741824);for(let c=0;c<6;c++)await r.writeU32BE(0);return await r.writeU32BE(this.tracks.length+1+(o?1:0)),r.getBuffer()}async buildTrak(r,t,s){let n=new B,o=await this.buildTkhd(r),c=s?await this.buildTref(s):null,u=await this.buildMdia(r,t),h=8+o.byteLength+u.byteLength;if(c)h+=c.byteLength;if(await n.writeU32BE(h),await n.writeFourCC("trak"),await n.writeBytes(o),c)await n.writeBytes(c);return await n.writeBytes(u),n.getBuffer()}async buildTkhd(r){let t=new B,s=BigInt(Math.floor(Date.now()/1000))+e,n=r.samples[r.samples.length-1],o=n?BigInt(Math.round((n.timestamp+n.duration)*L)):0n,c=92,u=r.track,h=0,l=0,m=0;if(u.type==="video")h=u.config.width,l=u.config.height;else if(u.type==="audio")m=256;await t.writeU32BE(92),await t.writeFourCC("tkhd"),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU8(3),await t.writeU32BE(Number(s&0xFFFFFFFFn)),await t.writeU32BE(Number(s&0xFFFFFFFFn)),await t.writeU32BE(u.id),await t.writeU32BE(0),await t.writeU32BE(Number(o&0xFFFFFFFFn)),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU16BE(m),await t.writeU16BE(0);let d=u.type==="video"?u.config.rotation??0:0,U=65536,p=4294901760,E=d===90?[0,U,0,p,0,0,l*65536,0]:d===180?[p,0,0,0,p,0,h*65536,l*65536]:d===270?[0,p,0,U,0,0,0,h*65536]:[U,0,0,0,U,0,0,0];for(let z of E)await t.writeU32BE(z);return await t.writeU32BE(1073741824),await t.writeU32BE(h*65536),await t.writeU32BE(l*65536),t.getBuffer()}async buildMdia(r,t){let s=new B,n=await this.buildMdhd(r),o=await this.buildHdlr(r),c=await this.buildMinf(r,t),u=8+n.byteLength+o.byteLength+c.byteLength;return await s.writeU32BE(u),await s.writeFourCC("mdia"),await s.writeBytes(n),await s.writeBytes(o),await s.writeBytes(c),s.getBuffer()}async buildMdhd(r){let t=new B,s=BigInt(Math.floor(Date.now()/1000))+e,n=r.samples[r.samples.length-1],o=n?BigInt(Math.round((n.timestamp+n.duration)*r.timescale)):0n,c=32;return await t.writeU32BE(32),await t.writeFourCC("mdhd"),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU32BE(Number(s&0xFFFFFFFFn)),await t.writeU32BE(Number(s&0xFFFFFFFFn)),await t.writeU32BE(r.timescale),await t.writeU32BE(Number(o&0xFFFFFFFFn)),await t.writeU16BE(w("und")),await t.writeU16BE(0),t.getBuffer()}async buildHdlr(r){let t=new B,s="",n="";if(r.track.type==="video")s="vide",n="VideoHandler";else if(r.track.type==="audio")s="soun",n="SoundHandler";else s="text",n="SubtitleHandler";let o=new TextEncoder().encode(`${n}\x00`),c=32+o.byteLength;return await t.writeU32BE(c),await t.writeFourCC("hdlr"),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeFourCC(s),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeBytes(o),t.getBuffer()}async buildMinf(r,t){let s=new B,n;if(r.track.type==="video")n=await this.buildVmhd();else if(r.track.type==="audio")n=await this.buildSmhd();else n=await this.buildNmhd();let o=await this.buildDinf(),c=await this.buildStbl(r,t),u=8+n.byteLength+o.byteLength+c.byteLength;return await s.writeU32BE(u),await s.writeFourCC("minf"),await s.writeBytes(n),await s.writeBytes(o),await s.writeBytes(c),s.getBuffer()}async buildVmhd(){let r=new B,t=20;return await r.writeU32BE(20),await r.writeFourCC("vmhd"),await r.writeU32BE(1),await r.writeU16BE(0),await r.writeU16BE(0),await r.writeU16BE(0),await r.writeU16BE(0),r.getBuffer()}async buildSmhd(){let r=new B,t=16;return await r.writeU32BE(16),await r.writeFourCC("smhd"),await r.writeU32BE(0),await r.writeU16BE(0),await r.writeU16BE(0),r.getBuffer()}async buildNmhd(){let r=new B,t=12;return await r.writeU32BE(12),await r.writeFourCC("nmhd"),await r.writeU32BE(0),r.getBuffer()}async buildDinf(){let r=new B,t=await this.buildDref(),s=8+t.byteLength;return await r.writeU32BE(s),await r.writeFourCC("dinf"),await r.writeBytes(t),r.getBuffer()}async buildDref(){let r=new B,t=28;return await r.writeU32BE(28),await r.writeFourCC("dref"),await r.writeU32BE(0),await r.writeU32BE(1),await r.writeU32BE(12),await r.writeFourCC("url "),await r.writeU32BE(1),r.getBuffer()}async buildStbl(r,t){let s=new B,n=await this.buildStsd(r),o=await this.buildStts(r),c=await this.buildStsc(r),u=await this.buildStsz(r),h=await this.buildStco(r,t),l=null,m=null;if(r.track.type==="video")l=await this.buildStss(r),m=await this.buildCtts(r);let d=8+n.byteLength+o.byteLength+c.byteLength+u.byteLength+h.byteLength;if(l)d+=l.byteLength;if(m)d+=m.byteLength;if(await s.writeU32BE(d),await s.writeFourCC("stbl"),await s.writeBytes(n),await s.writeBytes(o),m)await s.writeBytes(m);if(await s.writeBytes(c),await s.writeBytes(u),await s.writeBytes(h),l)await s.writeBytes(l);return s.getBuffer()}async buildStsd(r){let t=new B,s;if(r.track.type==="video")s=await this.buildVideoSampleEntry(r.track);else if(r.track.type==="audio")s=await this.buildAudioSampleEntry(r.track);else s=new Uint8Array(0);let n=16+s.byteLength;return await t.writeU32BE(n),await t.writeFourCC("stsd"),await t.writeU32BE(0),await t.writeU32BE(1),await t.writeBytes(s),t.getBuffer()}async buildVideoSampleEntry(r){let t=new B,s=r.config,n="";if(s.codec==="h264")n="avc1";else if(s.codec==="h265")n="hvc1";else if(s.codec==="vp9")n="vp09";else if(s.codec==="av1")n="av01";else if(s.codec==="prores")n={proxy:"apco",lt:"apcs","422":"apcn",standard:"apcn",hq:"apch","4444":"ap4h","4444-xq":"ap4x"}[s.profile??"422"]??"apcn";else n="mp4v";let o=s.codecDescription??new Uint8Array(0),c=s.codec==="h264"?"avcC":s.codec==="h265"?"hvcC":s.codec==="vp9"?"vpcC":s.codec==="av1"?"av1C":void 0,u=c&&o.byteLength>0?8+o.byteLength:0,h=86+u;await t.writeU32BE(h),await t.writeFourCC(n),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(1),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(s.width),await t.writeU16BE(s.height),await t.writeU32BE(4718592),await t.writeU32BE(4718592),await t.writeU32BE(0),await t.writeU16BE(1);let l=new Uint8Array(32);if(await t.writeBytes(l),await t.writeU16BE(24),await t.writeI16BE(-1),u>0)await t.writeU32BE(u),await t.writeFourCC(c),await t.writeBytes(o);return t.getBuffer()}async buildAudioSampleEntry(r){let t=new B,s=r.config,n="mp4a";if(s.codec==="opus")n="Opus";else if(s.codec==="flac")n="fLaC";let o=s.codec==="aac"?i(s):s.codecDescription??new Uint8Array(0),c=o.byteLength>0?8+o.byteLength:0,u=36+c;if(await t.writeU32BE(u),await t.writeFourCC(n),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(1),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(s.channels),await t.writeU16BE(s.bitsPerSample??16),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU32BE(s.sampleRate<65536?s.sampleRate*65536:0),c>0)await t.writeU32BE(c),await t.writeFourCC("esds"),await t.writeBytes(o);return t.getBuffer()}async buildStts(r){let t=new B,s=[],n=-1;for(let c of r.samples){let u=Math.round(c.duration*r.timescale);if(u===n&&s.length>0)s[s.length-1].sampleCount++;else s.push({sampleCount:1,sampleDelta:u}),n=u}let o=16+s.length*8;await t.writeU32BE(o),await t.writeFourCC("stts"),await t.writeU32BE(0),await t.writeU32BE(s.length);for(let c of s)await t.writeU32BE(c.sampleCount),await t.writeU32BE(c.sampleDelta);return t.getBuffer()}async buildCtts(r){let t=new B,s=[],n=Number.MIN_SAFE_INTEGER;for(let c of r.samples){let u=Math.round(c.compositionTimeOffset*r.timescale);if(u===n&&s.length>0)s[s.length-1].sampleCount++;else s.push({sampleCount:1,sampleOffset:u}),n=u}if(s.length===0||s.length===1&&s[0].sampleOffset===0)return new Uint8Array(0);let o=16+s.length*8;await t.writeU32BE(o),await t.writeFourCC("ctts"),await t.writeU32BE(0),await t.writeU32BE(s.length);for(let c of s)await t.writeU32BE(c.sampleCount),await t.writeU32BE(c.sampleOffset);return t.getBuffer()}async buildStsc(r){let t=new B,s=[];this.chunksOf(r).forEach((n,o)=>{if(s[s.length-1]?.samples!==n.samples)s.push({firstChunk:o+1,samples:n.samples})}),await t.writeU32BE(16+s.length*12),await t.writeFourCC("stsc"),await t.writeU32BE(0),await t.writeU32BE(s.length);for(let n of s)await t.writeU32BE(n.firstChunk),await t.writeU32BE(n.samples),await t.writeU32BE(1);return t.getBuffer()}async buildStsz(r){let t=new B,s=20+r.samples.length*4;await t.writeU32BE(s),await t.writeFourCC("stsz"),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(r.samples.length);for(let n of r.samples)await t.writeU32BE(n.size);return t.getBuffer()}async buildStco(r,t){let s=new B,n=this.options.fastStart?t:0,o=this.chunksOf(r).map((u)=>n+u.offset),c=16+o.length*4;await s.writeU32BE(c),await s.writeFourCC("stco"),await s.writeU32BE(0),await s.writeU32BE(o.length);for(let u of o)await s.writeU32BE(u);return s.getBuffer()}async buildStss(r){let t=new B,s=[];for(let o=0;o<r.samples.length;o++)if(r.samples[o].isKeyframe)s.push(o+1);if(s.length===r.samples.length)return new Uint8Array(0);let n=16+s.length*4;await t.writeU32BE(n),await t.writeFourCC("stss"),await t.writeU32BE(0),await t.writeU32BE(s.length);for(let o of s)await t.writeU32BE(o);return t.getBuffer()}async buildTref(r){let t=new B;return await t.writeU32BE(20),await t.writeFourCC("tref"),await t.writeU32BE(12),await t.writeFourCC("chap"),await t.writeU32BE(r),t.getBuffer()}buildChapterTextSamples(){if(this.chapters.length===0)return[];let r=0;for(let n of this.trackData.values()){let o=n.samples[n.samples.length-1];if(o)r=Math.max(r,Math.round((o.timestamp+o.duration)*1000))}let t=new TextEncoder,s=[];for(let n=0;n<this.chapters.length;n++){let o=this.chapters[n],c=n+1<this.chapters.length?this.chapters[n+1].startTimeMs:r,u=Math.max(1,Math.round(c-o.startTimeMs)),h=t.encode(o.title),l=new Uint8Array(2+h.length);l[0]=h.length>>8&255,l[1]=h.length&255,l.set(h,2),s.push({data:l,durationMs:u})}return s}async buildTextSampleEntry(){let r=new B;return await r.writeU32BE(59),await r.writeFourCC("text"),await r.writeU32BE(0),await r.writeU16BE(0),await r.writeU16BE(1),await r.writeU32BE(1),await r.writeU32BE(0),await r.writeU16BE(0),await r.writeU16BE(0),await r.writeU16BE(0),await r.writeU16BE(0),await r.writeU16BE(0),await r.writeU16BE(0),await r.writeU16BE(1),await r.writeU32BE(0),await r.writeU32BE(0),await r.writeU16BE(0),await r.writeU16BE(13),await r.writeFourCC("ftab"),await r.writeU16BE(1),await r.writeU16BE(1),await r.writeU8(0),r.getBuffer()}async buildChapterTrak(r,t,s){let n=new B,o=BigInt(Math.floor(Date.now()/1000))+e,c=1000,u=new TextEncoder,h=0;for(let g of t)h+=g.durationMs;let l=new B;await l.writeU32BE(92),await l.writeFourCC("tkhd"),await l.writeU8(0),await l.writeU8(0),await l.writeU8(0),await l.writeU8(1),await l.writeU32BE(Number(o&0xFFFFFFFFn)),await l.writeU32BE(Number(o&0xFFFFFFFFn)),await l.writeU32BE(r),await l.writeU32BE(0),await l.writeU32BE(h),await l.writeU32BE(0),await l.writeU32BE(0),await l.writeU16BE(0),await l.writeU16BE(0),await l.writeU16BE(0),await l.writeU16BE(0),await l.writeU32BE(65536),await l.writeU32BE(0),await l.writeU32BE(0),await l.writeU32BE(0),await l.writeU32BE(65536),await l.writeU32BE(0),await l.writeU32BE(0),await l.writeU32BE(0),await l.writeU32BE(1073741824),await l.writeU32BE(0),await l.writeU32BE(0);let m=l.getBuffer(),d=new B;await d.writeU32BE(32),await d.writeFourCC("mdhd"),await d.writeU32BE(0),await d.writeU32BE(Number(o&0xFFFFFFFFn)),await d.writeU32BE(Number(o&0xFFFFFFFFn)),await d.writeU32BE(1000),await d.writeU32BE(h),await d.writeU16BE(0),await d.writeU16BE(0);let U=d.getBuffer(),p=u.encode("ChapterHandler\x00"),E=new B;await E.writeU32BE(32+p.byteLength),await E.writeFourCC("hdlr"),await E.writeU32BE(0),await E.writeU32BE(0),await E.writeFourCC("text"),await E.writeU32BE(0),await E.writeU32BE(0),await E.writeU32BE(0),await E.writeBytes(p);let z=E.getBuffer(),b=new B;await b.writeU32BE(36),await b.writeFourCC("edts"),await b.writeU32BE(28),await b.writeFourCC("elst"),await b.writeU32BE(0),await b.writeU32BE(1),await b.writeU32BE(h),await b.writeU32BE(0),await b.writeU32BE(65536);let D=b.getBuffer(),f=new B;await f.writeU32BE(76),await f.writeFourCC("gmhd"),await f.writeU32BE(24),await f.writeFourCC("gmin"),await f.writeU32BE(0),await f.writeU16BE(64),await f.writeU16BE(32768),await f.writeU16BE(32768),await f.writeU16BE(32768),await f.writeU16BE(0),await f.writeU16BE(0),await f.writeU32BE(44),await f.writeFourCC("text"),await f.writeU32BE(65536),await f.writeU32BE(0),await f.writeU32BE(0),await f.writeU32BE(0),await f.writeU32BE(65536),await f.writeU32BE(0),await f.writeU32BE(0),await f.writeU32BE(0),await f.writeU32BE(1073741824);let P=f.getBuffer(),O=await this.buildDinf(),N=await this.buildTextSampleEntry(),M=new B;await M.writeU32BE(16+N.byteLength),await M.writeFourCC("stsd"),await M.writeU32BE(0),await M.writeU32BE(1),await M.writeBytes(N);let I=M.getBuffer(),x=[],V=-1;for(let g of t)if(g.durationMs===V&&x.length>0)x[x.length-1].count++;else x.push({count:1,delta:g.durationMs}),V=g.durationMs;let v=new B;await v.writeU32BE(16+x.length*8),await v.writeFourCC("stts"),await v.writeU32BE(0),await v.writeU32BE(x.length);for(let g of x)await v.writeU32BE(g.count),await v.writeU32BE(g.delta);let W=v.getBuffer(),F=new B;await F.writeU32BE(28),await F.writeFourCC("stsc"),await F.writeU32BE(0),await F.writeU32BE(1),await F.writeU32BE(1),await F.writeU32BE(1),await F.writeU32BE(1);let H=F.getBuffer(),k=new B;await k.writeU32BE(20+t.length*4),await k.writeFourCC("stsz"),await k.writeU32BE(0),await k.writeU32BE(0),await k.writeU32BE(t.length);for(let g of t)await k.writeU32BE(g.data.byteLength);let R=k.getBuffer(),S=new B;await S.writeU32BE(16+t.length*4),await S.writeFourCC("stco"),await S.writeU32BE(0),await S.writeU32BE(t.length);let _=s;for(let g of t)await S.writeU32BE(_),_+=g.data.byteLength;let j=S.getBuffer(),J=8+I.byteLength+W.byteLength+H.byteLength+R.byteLength+j.byteLength,C=new B;await C.writeU32BE(J),await C.writeFourCC("stbl"),await C.writeBytes(I),await C.writeBytes(W),await C.writeBytes(H),await C.writeBytes(R),await C.writeBytes(j);let K=C.getBuffer(),Q=8+P.byteLength+O.byteLength+K.byteLength,A=new B;await A.writeU32BE(Q),await A.writeFourCC("minf"),await A.writeBytes(P),await A.writeBytes(O),await A.writeBytes(K);let q=A.getBuffer(),X=8+U.byteLength+z.byteLength+q.byteLength,T=new B;await T.writeU32BE(X),await T.writeFourCC("mdia"),await T.writeBytes(U),await T.writeBytes(z),await T.writeBytes(q);let G=T.getBuffer(),Y=8+m.byteLength+D.byteLength+G.byteLength;return await n.writeU32BE(Y),await n.writeFourCC("trak"),await n.writeBytes(m),await n.writeBytes(D),await n.writeBytes(G),n.getBuffer()}async buildUdta(){let r=this.metadata&&Object.keys(this.metadata).length>0,t=this.artwork!==null,s=this.chapters.length>0;if(!r&&!t&&!s)return null;let n=new B,o=[];if(s)o.push(Z(this.chapters.map((u)=>({title:u.title,startTime:u.startTimeMs}))));if(r||t)o.push(await this.buildMeta());let c=o.reduce((u,h)=>u+h.byteLength,0);await n.writeU32BE(c+8),await n.writeFourCC("udta");for(let u of o)await n.writeBytes(u);return n.getBuffer()}async buildMeta(){let r=new B,t=await this.buildMetaHdlr(),s=await this.buildIlst(),n=4+t.byteLength+s.byteLength;return await r.writeU32BE(n+8),await r.writeFourCC("meta"),await r.writeU32BE(0),await r.writeBytes(t),await r.writeBytes(s),r.getBuffer()}async buildMetaHdlr(){let r=new B,t=33;return await r.writeU32BE(33),await r.writeFourCC("hdlr"),await r.writeU32BE(0),await r.writeU32BE(0),await r.writeFourCC("mdir"),await r.writeFourCC("appl"),await r.writeU32BE(0),await r.writeU32BE(0),await r.writeU8(0),r.getBuffer()}async buildIlst(){let r=new B,t=[],s=this.metadata;if(s.title)t.push(y("©nam",s.title));if(s.artist)t.push(y("©ART",s.artist));if(s.albumArtist)t.push(y("aART",s.albumArtist));if(s.album)t.push(y("©alb",s.album));if(s.genre)t.push(y("©gen",s.genre));if(s.year)t.push(y("©day",String(s.year)));if(s.composer)t.push(y("©wrt",s.composer));if(s.copyright)t.push(y("cprt",s.copyright));if(s.comment)t.push(y("©cmt",s.comment));if(s.encodedBy)t.push(y("©too",s.encodedBy));if(s.narrator)t.push(y("©nrt",s.narrator));if(s.publisher)t.push(y("©pub",s.publisher));if(s.description)t.push(y("desc",s.description));if(this.artwork){let o=this.artworkType==="png"?14:13;t.push(it("covr",this.artwork,o))}let n=t.reduce((o,c)=>o+c.byteLength,0);await r.writeU32BE(n+8),await r.writeFourCC("ilst");for(let o of t)await r.writeBytes(o);return r.getBuffer()}}function y(r,t){let n=new TextEncoder().encode(t),o=16+n.length,c=8+o,u=new Uint8Array(c),h=new DataView(u.buffer);return h.setUint32(0,c),u[4]=r.charCodeAt(0)&255,u[5]=r.charCodeAt(1)&255,u[6]=r.charCodeAt(2)&255,u[7]=r.charCodeAt(3)&255,h.setUint32(8,o),u[12]=100,u[13]=97,u[14]=116,u[15]=97,h.setUint32(16,1),h.setUint32(20,0),u.set(n,24),u}function it(r,t,s){let n=16+t.length,o=8+n,c=new Uint8Array(o),u=new DataView(c.buffer);return u.setUint32(0,o),c[4]=r.charCodeAt(0)&255,c[5]=r.charCodeAt(1)&255,c[6]=r.charCodeAt(2)&255,c[7]=r.charCodeAt(3)&255,u.setUint32(8,n),c[12]=100,c[13]=97,c[14]=116,c[15]=97,u.setUint32(16,s),u.setUint32(20,0),c.set(t,24),c}
|
|
2
|
+
export{a};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{i}from"./chunk-cmq2ntnd.js";import{w,e}from"./chunk-r455c378.js";import{Muxer as y}from"ts-videos/muxer";import{Writer as m}from"ts-videos/writer";var p=1000;class o extends y{trackData=new Map;options;sequenceNumber=1;fragmentStartTime=0;initialized=!1;constructor(a,t={}){super(a);if(this.options={fragmentDuration:t.fragmentDuration??2,brand:t.brand??"iso5",cmaf:t.cmaf??!1,lowLatency:t.lowLatency??!1},this.options.lowLatency)this.options.fragmentDuration=Math.min(0.5,this.options.fragmentDuration)}get formatName(){return"fmp4"}get mimeType(){return"video/mp4"}onTrackAdded(a){let t=a.type==="video"?Math.round((a.config.frameRate??30)*1000):a.type==="audio"?a.config.sampleRate:1000;this.trackData.set(a.id,{track:a,timescale:t,samples:[],baseMediaDecodeTime:0n,defaultSampleDuration:0,defaultSampleSize:0,defaultSampleFlags:a.type==="video"?65536:0})}async writeHeader(){await this.writeFtyp(),await this.writeMoov(),this.initialized=!0}async writeFtyp(){let a=this.options.cmaf?["cmfc","iso6","mp41"]:[this.options.brand,"iso5","iso6","mp41","avc1"],t=16+a.length*4;await this.writer.writeU32BE(t),await this.writer.writeFourCC("ftyp"),await this.writer.writeFourCC(this.options.cmaf?"cmfc":this.options.brand),await this.writer.writeU32BE(512);for(let r of a)await this.writer.writeFourCC(r)}async writeMoov(){let a=new m,t=await this.buildMvhd(),r=await this.buildMvex(),s=[];for(let c of this.trackData.values()){let f=await this.buildTrak(c);s.push(f)}let u=t.byteLength+r.byteLength+s.reduce((c,f)=>c+f.byteLength,0)+8;await a.writeU32BE(u),await a.writeFourCC("moov"),await a.writeBytes(t);for(let c of s)await a.writeBytes(c);await a.writeBytes(r),await this.writer.writeBytes(a.getBuffer())}async buildMvhd(){let a=new m,t=BigInt(Math.floor(Date.now()/1000))+e,r=116;await a.writeU32BE(116),await a.writeFourCC("mvhd"),await a.writeU8(0),await a.writeU8(0),await a.writeU8(0),await a.writeU8(0),await a.writeU32BE(Number(t&0xFFFFFFFFn)),await a.writeU32BE(Number(t&0xFFFFFFFFn)),await a.writeU32BE(p),await a.writeU32BE(0),await a.writeU32BE(65536),await a.writeU16BE(256),await a.writeU16BE(0),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(65536),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(65536),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(1073741824);for(let s=0;s<6;s++)await a.writeU32BE(0);return await a.writeU32BE(this.tracks.length+1),a.getBuffer()}async buildMvex(){let a=new m,t=[];for(let s of this.trackData.values())t.push(await this.buildTrex(s));let r=8+t.reduce((s,n)=>s+n.byteLength,0);await a.writeU32BE(r),await a.writeFourCC("mvex");for(let s of t)await a.writeBytes(s);return a.getBuffer()}async buildTrex(a){let t=new m,r=32;return await t.writeU32BE(32),await t.writeFourCC("trex"),await t.writeU32BE(0),await t.writeU32BE(a.track.id),await t.writeU32BE(1),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(a.defaultSampleFlags),t.getBuffer()}async buildTrak(a){let t=new m,r=await this.buildTkhd(a),s=await this.buildMdia(a),n=8+r.byteLength+s.byteLength;return await t.writeU32BE(n),await t.writeFourCC("trak"),await t.writeBytes(r),await t.writeBytes(s),t.getBuffer()}async buildTkhd(a){let t=new m,r=BigInt(Math.floor(Date.now()/1000))+e,s=92,n=a.track,u=0,c=0,f=0;if(n.type==="video")u=n.config.width,c=n.config.height;else if(n.type==="audio")f=256;return await t.writeU32BE(92),await t.writeFourCC("tkhd"),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU8(3),await t.writeU32BE(Number(r&0xFFFFFFFFn)),await t.writeU32BE(Number(r&0xFFFFFFFFn)),await t.writeU32BE(n.id),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU16BE(f),await t.writeU16BE(0),await t.writeU32BE(65536),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(65536),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(1073741824),await t.writeU32BE(u<<16),await t.writeU32BE(c<<16),t.getBuffer()}async buildMdia(a){let t=new m,r=await this.buildMdhd(a),s=await this.buildHdlr(a),n=await this.buildMinf(a),u=8+r.byteLength+s.byteLength+n.byteLength;return await t.writeU32BE(u),await t.writeFourCC("mdia"),await t.writeBytes(r),await t.writeBytes(s),await t.writeBytes(n),t.getBuffer()}async buildMdhd(a){let t=new m,r=BigInt(Math.floor(Date.now()/1000))+e,s=40;return await t.writeU32BE(40),await t.writeFourCC("mdhd"),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU32BE(Number(r&0xFFFFFFFFn)),await t.writeU32BE(Number(r&0xFFFFFFFFn)),await t.writeU32BE(a.timescale),await t.writeU32BE(0),await t.writeU16BE(w("und")),await t.writeU16BE(0),t.getBuffer()}async buildHdlr(a){let t=new m,r="",s="";if(a.track.type==="video")r="vide",s="VideoHandler";else if(a.track.type==="audio")r="soun",s="SoundHandler";else r="text",s="SubtitleHandler";let n=new TextEncoder().encode(`${s}\x00`),u=32+n.byteLength+8;return await t.writeU32BE(u),await t.writeFourCC("hdlr"),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeFourCC(r),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeBytes(n),t.getBuffer()}async buildMinf(a){let t=new m,r;if(a.track.type==="video")r=await this.buildVmhd();else if(a.track.type==="audio")r=await this.buildSmhd();else r=await this.buildNmhd();let s=await this.buildDinf(),n=await this.buildStbl(a),u=8+r.byteLength+s.byteLength+n.byteLength;return await t.writeU32BE(u),await t.writeFourCC("minf"),await t.writeBytes(r),await t.writeBytes(s),await t.writeBytes(n),t.getBuffer()}async buildVmhd(){let a=new m,t=20;return await a.writeU32BE(20),await a.writeFourCC("vmhd"),await a.writeU32BE(1),await a.writeU16BE(0),await a.writeU16BE(0),await a.writeU16BE(0),await a.writeU16BE(0),a.getBuffer()}async buildSmhd(){let a=new m,t=16;return await a.writeU32BE(16),await a.writeFourCC("smhd"),await a.writeU32BE(0),await a.writeU16BE(0),await a.writeU16BE(0),a.getBuffer()}async buildNmhd(){let a=new m,t=12;return await a.writeU32BE(12),await a.writeFourCC("nmhd"),await a.writeU32BE(0),a.getBuffer()}async buildDinf(){let a=new m,t=await this.buildDref(),r=8+t.byteLength;return await a.writeU32BE(r),await a.writeFourCC("dinf"),await a.writeBytes(t),a.getBuffer()}async buildDref(){let a=new m,t=28;return await a.writeU32BE(28),await a.writeFourCC("dref"),await a.writeU32BE(0),await a.writeU32BE(1),await a.writeU32BE(12),await a.writeFourCC("url "),await a.writeU32BE(1),a.getBuffer()}async buildStbl(a){let t=new m,r=await this.buildStsd(a),s=await this.buildEmptyAtom("stts"),n=await this.buildEmptyAtom("stsc"),u=await this.buildEmptyStsz(),c=await this.buildEmptyAtom("stco"),f=8+r.byteLength+s.byteLength+n.byteLength+u.byteLength+c.byteLength;return await t.writeU32BE(f),await t.writeFourCC("stbl"),await t.writeBytes(r),await t.writeBytes(s),await t.writeBytes(n),await t.writeBytes(u),await t.writeBytes(c),t.getBuffer()}async buildEmptyAtom(a){let t=new m;return await t.writeU32BE(16),await t.writeFourCC(a),await t.writeU32BE(0),await t.writeU32BE(0),t.getBuffer()}async buildEmptyStsz(){let a=new m;return await a.writeU32BE(20),await a.writeFourCC("stsz"),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(0),a.getBuffer()}async buildStsd(a){let t=new m,r;if(a.track.type==="video")r=await this.buildVideoSampleEntry(a.track);else if(a.track.type==="audio")r=await this.buildAudioSampleEntry(a.track);else r=new Uint8Array(0);let s=16+r.byteLength;return await t.writeU32BE(s),await t.writeFourCC("stsd"),await t.writeU32BE(0),await t.writeU32BE(1),await t.writeBytes(r),t.getBuffer()}async buildVideoSampleEntry(a){let t=new m,r=a.config,s="";if(r.codec==="h264")s="avc1";else if(r.codec==="h265")s="hvc1";else if(r.codec==="vp9")s="vp09";else if(r.codec==="av1")s="av01";else s="mp4v";let n=r.codecDescription??new Uint8Array(0),u=r.codec==="h264"?"avcC":r.codec==="h265"?"hvcC":"avcC",c=n.byteLength>0?8+n.byteLength:0,f=86+c;await t.writeU32BE(f),await t.writeFourCC(s),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(1),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(r.width),await t.writeU16BE(r.height),await t.writeU32BE(4718592),await t.writeU32BE(4718592),await t.writeU32BE(0),await t.writeU16BE(1);let l=new Uint8Array(32);if(await t.writeBytes(l),await t.writeU16BE(24),await t.writeI16BE(-1),c>0)await t.writeU32BE(c),await t.writeFourCC(u),await t.writeBytes(n);return t.getBuffer()}async buildAudioSampleEntry(a){let t=new m,r=a.config,s="mp4a";if(r.codec==="opus")s="Opus";else if(r.codec==="flac")s="fLaC";let n=r.codec==="aac"?i(r):r.codecDescription??new Uint8Array(0),u=n.byteLength>0?8+n.byteLength:0,c=36+u;if(await t.writeU32BE(c),await t.writeFourCC(s),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(1),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(r.channels),await t.writeU16BE(r.bitsPerSample??16),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU32BE(r.sampleRate<<16),u>0)await t.writeU32BE(u),await t.writeFourCC("esds"),await t.writeBytes(n);return t.getBuffer()}async writeVideoPacket(a,t){await this.writeSample(a.id,t)}async writeAudioPacket(a,t){await this.writeSample(a.id,t)}async writeSubtitlePacket(a,t){await this.writeSample(a.id,t)}async writeSample(a,t){let r=this.trackData.get(a);if(!r)return;let s=[...this.trackData.values()].some((l)=>l.track.type==="video"),n=[...this.trackData.values()].some((l)=>l.samples.length>0),u=t.timestamp-this.fragmentStartTime>=this.options.fragmentDuration,c=r.track.type==="video"&&t.isKeyframe;if(n&&u&&(c||!s))await this.flushFragment();let f={data:t.data,timestamp:t.timestamp,duration:t.duration??0,isKeyframe:t.isKeyframe,compositionTimeOffset:t.compositionTimeOffset??0,size:t.data.byteLength};r.samples.push(f)}async flushFragment(){let a=!1;for(let n of this.trackData.values())if(n.samples.length>0){a=!0;break}if(!a)return;let t=await this.buildMoof(),r=await this.buildFragmentMdat();await this.writer.writeBytes(t),await this.writer.writeBytes(r),this.sequenceNumber++;let s=this.fragmentStartTime;for(let n of this.trackData.values()){if(n.samples.length>0){n.baseMediaDecodeTime+=n.samples.reduce((u,c)=>u+BigInt(Math.round(c.duration*n.timescale)),0n);for(let u of n.samples)s=Math.max(s,u.timestamp+u.duration)}n.samples=[]}this.fragmentStartTime=s}async buildMoof(){let a=new m,t=await this.buildMfhd(),r=[],s=8;for(let c of this.trackData.values())if(c.samples.length>0){r.push(await this.buildTraf(c,s));for(let f of c.samples)s+=f.size}let n=8+t.byteLength;for(let c of r)n+=c.byteLength;let u=[];s=n+8;for(let c of this.trackData.values())if(c.samples.length>0){u.push(await this.buildTraf(c,s));for(let f of c.samples)s+=f.size}n=8+t.byteLength;for(let c of u)n+=c.byteLength;await a.writeU32BE(n),await a.writeFourCC("moof"),await a.writeBytes(t);for(let c of u)await a.writeBytes(c);return a.getBuffer()}async buildMfhd(){let a=new m,t=16;return await a.writeU32BE(16),await a.writeFourCC("mfhd"),await a.writeU32BE(0),await a.writeU32BE(this.sequenceNumber),a.getBuffer()}async buildTraf(a,t){let r=new m,s=await this.buildTfhd(a),n=await this.buildTfdt(a),u=await this.buildTrun(a,t),c=8+s.byteLength+n.byteLength+u.byteLength;return await r.writeU32BE(c),await r.writeFourCC("traf"),await r.writeBytes(s),await r.writeBytes(n),await r.writeBytes(u),r.getBuffer()}async buildTfhd(a){let t=new m,r=0;if(a.samples.length>0){let u=Math.round(a.samples[0].duration*a.timescale);if(a.samples.every((f)=>Math.round(f.duration*a.timescale)===u))r=u}let s=131072|(r>0?8:0),n=16+(r>0?4:0);if(await t.writeU32BE(n),await t.writeFourCC("tfhd"),await t.writeU8(0),await t.writeU8(s>>16&255),await t.writeU8(s>>8&255),await t.writeU8(s&255),await t.writeU32BE(a.track.id),r>0)await t.writeU32BE(r);return t.getBuffer()}async buildTfdt(a){let t=new m,r=20;return await t.writeU32BE(20),await t.writeFourCC("tfdt"),await t.writeU8(1),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU64BE(a.baseMediaDecodeTime),t.getBuffer()}async buildTrun(a,t){let r=new m,s=!a.samples.every((d,B,h)=>B===0||Math.round(d.duration*a.timescale)===Math.round(h[0].duration*a.timescale)),n=!a.samples.every((d)=>d.size===a.samples[0].size),u=a.samples.some((d)=>d.isKeyframe!==a.samples[0].isKeyframe),c=a.samples.some((d)=>d.compositionTimeOffset!==0),f=1|(s?256:0)|(n?512:0)|(u?1024:0)|(c?2048:0),l=0;if(s)l+=4;if(n)l+=4;if(u)l+=4;if(c)l+=4;let U=20+a.samples.length*l;await r.writeU32BE(U),await r.writeFourCC("trun"),await r.writeU8(0),await r.writeU8(f>>16&255),await r.writeU8(f>>8&255),await r.writeU8(f&255),await r.writeU32BE(a.samples.length),await r.writeU32BE(t);for(let d of a.samples){if(s)await r.writeU32BE(Math.round(d.duration*a.timescale));if(n)await r.writeU32BE(d.size);if(u){let B=d.isKeyframe?0:65536;await r.writeU32BE(B)}if(c)await r.writeI32BE(Math.round(d.compositionTimeOffset*a.timescale))}return r.getBuffer()}async buildFragmentMdat(){let a=new m,t=0;for(let r of this.trackData.values())for(let s of r.samples)t+=s.size;await a.writeU32BE(t+8),await a.writeFourCC("mdat");for(let r of this.trackData.values())for(let s of r.samples)await a.writeBytes(s.data);return a.getBuffer()}async writeTrailer(){await this.flushFragment(),await this.writeMfra()}async writeMfra(){let a=new m,t=[],r=16,s=24;await a.writeU32BE(24),await a.writeFourCC("mfra"),await a.writeU32BE(16),await a.writeFourCC("mfro"),await a.writeU32BE(0),await a.writeU32BE(24),await this.writer.writeBytes(a.getBuffer())}}
|
|
2
|
+
export{o};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{y,b}from"./chunk-r455c378.js";import{n}from"./chunk-0ea9sv8z.js";import{a}from"./chunk-30x59cyt.js";import{o}from"./chunk-5sfmrnz9.js";import{InputFormat as i}from"ts-videos/demuxer";import{OutputFormat as s}from"ts-videos/muxer";import{Reader as m}from"ts-videos/reader";class g extends i{get name(){return"mp4"}get mimeType(){return"video/mp4"}get extensions(){return["mp4","m4v","m4a","f4v"]}async canRead(e){let t=m.fromSource(e);t.position=0;let r=await t.readU32BE();if(r===null||r<8)return!1;if(await t.readFourCC()!=="ftyp")return!1;let p=await t.readFourCC();if(!p)return!1;return y.has(p)}createDemuxer(e){return new n(e)}}class f extends i{get name(){return"mov"}get mimeType(){return"video/quicktime"}get extensions(){return["mov","qt"]}async canRead(e){let t=m.fromSource(e);t.position=0;let r=await t.readU32BE();if(r===null||r<8)return!1;if(await t.readFourCC()!=="ftyp")return!1;let p=await t.readFourCC();if(!p)return!1;return b.has(p)}createDemuxer(e){return new n(e)}}class E extends s{options;constructor(e={}){super();this.options=e}get name(){return"mp4"}get mimeType(){return"video/mp4"}get extension(){return"mp4"}createMuxer(e){return new a(e,this.options)}}class x extends s{options;constructor(e={}){super();this.options={...e,brand:"qt "}}get name(){return"mov"}get mimeType(){return"video/quicktime"}get extension(){return"mov"}createMuxer(e){return new a(e,this.options)}}class C extends s{options;constructor(e={}){super();this.options=e}get name(){return"fmp4"}get mimeType(){return"video/mp4"}get extension(){return"mp4"}createMuxer(e){return new o(e,this.options)}}class F extends s{options;constructor(e={}){super();this.options={...e,cmaf:!0}}get name(){return"cmaf"}get mimeType(){return"video/mp4"}get extension(){return"cmfv"}createMuxer(e){return new o(e,this.options)}}var k=new g,T=new f,M=new E,A=new x,z=new C,D=new F;
|
|
2
|
+
export{g,f,E,x,C,F,k,T,M,A,z,D};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var y=[96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000,7350];function d(n){return n.byteLength>=6&&n[0]===0&&n[1]===0&&n[2]===0&&n[3]===0&&n[4]===3}function h(n,e){if(e+2>n.byteLength)return null;let t=n[e],o=0,u=e+1;for(let c=0;c<4&&u<n.byteLength;c++){let a=n[u++];if(o=o<<7|a&127,!(a&128))break}return{tag:t,start:u,end:Math.min(n.byteLength,u+o)}}function r(n){let e=h(n,4);if(!e||e.tag!==3)return;let t=e.start+3,o=n[e.start+2]??0;if(o&128)t+=2;if(o&64)t+=1+(n[t]??0);if(o&32)t+=2;while(t<e.end){let u=h(n,t);if(!u)return;if(u.tag===4){let c=u.start+13;while(c<u.end){let a=h(n,c);if(!a)return;if(a.tag===5)return n.slice(a.start,a.end);c=a.end}return}t=u.end}return}function m(n){if(n.byteLength<2)return{};let e=0,t=(f)=>{let g=0;for(let l=0;l<f;l++,e++){let A=n[e>>3]??0;g=g<<1|A>>7-(e&7)&1}return g},o=t(5);if(o===31)o=32+t(6);let u=t(4),c=u===15?t(24):y[u],a=t(4);return{sampleRate:c,channels:a===7?8:a||void 0}}function s(n,e){let t=y.indexOf(n),o=e===8?7:Math.max(1,Math.min(6,e));if(t<0){let u=2n<<35n|15n<<31n|BigInt(n)<<7n|BigInt(o)<<3n;return Uint8Array.from({length:5},(c,a)=>Number(u>>BigInt(8*(4-a))&0xFFn))}return new Uint8Array([16|t>>1,(t&1)<<7|o<<3])}function x(n,e){let t=new Uint8Array(5+e.byteLength);return t[0]=n,t[1]=128|e.byteLength>>21&127,t[2]=128|e.byteLength>>14&127,t[3]=128|e.byteLength>>7&127,t[4]=e.byteLength&127,t.set(e,5),t}function p(n,e=0){let t=Math.max(0,Math.min(4294967295,Math.round(e))),o=new Uint8Array(18+n.byteLength);o[0]=64,o[1]=21,new DataView(o.buffer).setUint32(5,t),new DataView(o.buffer).setUint32(9,t),o.set(x(5,n),13);let u=x(4,o),c=x(6,new Uint8Array([2])),a=new Uint8Array(3+u.byteLength+c.byteLength);a.set(u,3),a.set(c,3+u.byteLength);let b=x(3,a),f=new Uint8Array(4+b.byteLength);return f.set(b,4),f}function i(n){let e=n.codecDescription;if(e&&d(e))return e;let t=e&&e.byteLength>=2?e:s(n.sampleRate,n.channels);return p(t,n.bitrate)}
|
|
2
|
+
export{d,r,m,s,p,i};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var t=8,l=16;function h(n,r){return String.fromCharCode(n.getUint8(r),n.getUint8(r+1),n.getUint8(r+2),n.getUint8(r+3))}function v(n,r,o){for(let i=0;i<4;i++)n.setUint8(r+i,o.charCodeAt(i))}var B=new Set(["moov","trak","mdia","minf","stbl","dinf","edts","udta","meta","ilst","moof","traf","mvex","sinf","schi","rinf"]),y=new Set(["isom","iso2","iso3","iso4","iso5","iso6","mp41","mp42","mp71","avc1","av01","hev1","hvc1","M4A ","M4V ","M4P ","M4B ","f4v ","f4a ","dash","msdh","msix"]),b=new Set(["qt "]);function c(n){return n==="vide"}function u(n){return n==="soun"}function S(n){return n==="subt"||n==="text"||n==="sbtl"}function U(n){let r=(n>>10&31)+96,o=(n>>5&31)+96,i=(n&31)+96;return String.fromCharCode(r,o,i)}function w(n){if(n.length!==3)return 21956;let r=n.charCodeAt(0)-96&31,o=n.charCodeAt(1)-96&31,i=n.charCodeAt(2)-96&31;return r<<10|o<<5|i}var e=2082844800n;
|
|
2
|
+
export{t,l,h,v,B,y,b,c,u,S,U,w,e};
|
package/dist/demuxer.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{n}from"./chunk-0ea9sv8z.js";import"./chunk-r455c378.js";import"./chunk-cmq2ntnd.js";export{n as Mp4Demuxer};
|
package/dist/fragmented.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{o}from"./chunk-5sfmrnz9.js";import"./chunk-r455c378.js";import"./chunk-cmq2ntnd.js";export{o as FragmentedMp4Muxer};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{f,
|
|
1
|
+
import{g,f,E,x,C,F,k,T,M,A,z,D}from"./chunk-cbtc0x29.js";import{a}from"./chunk-30x59cyt.js";import{n}from"./chunk-0ea9sv8z.js";import{o}from"./chunk-5sfmrnz9.js";import{t,l,h,v,B,y,b,c,u,S,U,w,e}from"./chunk-r455c378.js";import{d,r,m,s,p,i}from"./chunk-cmq2ntnd.js";export{t as BOX_HEADER_SIZE,D as CMAF_OUTPUT,B as CONTAINER_BOXES,F as CmafOutputFormat,l as EXTENDED_BOX_HEADER_SIZE,z as FMP4_OUTPUT,o as FragmentedMp4Muxer,C as FragmentedMp4OutputFormat,T as MOV,b as MOV_BRANDS,A as MOV_OUTPUT,k as MP4,y as MP4_BRANDS,M as MP4_OUTPUT,f as MovInputFormat,x as MovOutputFormat,n as Mp4Demuxer,g as Mp4InputFormat,a as Mp4Muxer,E as Mp4OutputFormat,e as TIMESCALE_1904_TO_1970,r as audioSpecificConfigFromEsds,s as buildAudioSpecificConfig,p as buildEsdsPayload,w as encodeLanguageCode,i as esdsPayloadFor,u as isAudioHandler,d as isEsdsPayload,S as isSubtitleHandler,c as isVideoHandler,U as parseLanguageCode,m as readAudioSpecificConfig,h as readFourCC,v as writeFourCC};
|
package/dist/muxer.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{a}from"./chunk-30x59cyt.js";import"./chunk-r455c378.js";import"./chunk-cmq2ntnd.js";export{a as Mp4Muxer};
|
package/dist/prores.test.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./chunk-
|
|
1
|
+
import"./chunk-cmq2ntnd.js";import"./chunk-r455c378.js";import"./chunk-0ea9sv8z.js";import{a}from"./chunk-30x59cyt.js";import"./chunk-5sfmrnz9.js";import{f}from"./chunk-cbtc0x29.js";import{describe as i,expect as n,test as c}from"bun:test";import{BufferTarget as m,Input as s}from"ts-videos";i("ProRes sample entries",()=>{c("round-trips the 4444 XQ profile through MOV metadata",async()=>{let r=new m,t=new a(r,{brand:"qt "}),o=t.addVideoTrack({codec:"prores",profile:"4444-xq",width:1920,height:1080,frameRate:24});await t.writePacket(o.id,{data:new Uint8Array([0,0,0,8,105,99,112,102]),timestamp:0,duration:0.041666666666666664,isKeyframe:!0}),await t.finalize();let e=new s(r.buffer);e.setFormats([new f]),n(await e.getPrimaryVideoTrack()).toMatchObject({codec:"prores",profile:"4444-xq"}),await e.close()})});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ts-videos/mp4",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.2",
|
|
5
5
|
"description": "MP4/MOV (ISOBMFF) codec support for ts-videos",
|
|
6
6
|
"author": "Chris Breuer <chris@stacksjs.org>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -57,6 +57,6 @@
|
|
|
57
57
|
"typecheck": "bunx tsc --noEmit"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"ts-videos": "0.
|
|
60
|
+
"ts-videos": "0.2.2"
|
|
61
61
|
}
|
|
62
62
|
}
|
package/dist/chunk-c9md36vm.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{s,e}from"./chunk-qmjyh9jc.js";import{Muxer as Y}from"ts-videos/muxer";import{Writer as B}from"ts-videos/writer";var T=1000;class i extends Y{trackData=new Map;options;mdatStart=0;mdatSize=0;chapters=[];artwork=null;artworkType="jpeg";constructor(a,t={}){super(a);this.options={fastStart:t.fastStart??!0,fragmented:t.fragmented??!1,brand:t.brand??"isom"}}addChapter(a,t){this.chapters.push({title:a,startTimeMs:t})}setArtwork(a,t="jpeg"){this.artwork=a,this.artworkType=t}get formatName(){return"mp4"}get mimeType(){return"video/mp4"}onTrackAdded(a){let t=a.type==="video"?Math.round((a.config.frameRate??30)*1000):a.type==="audio"?a.config.sampleRate:1000;this.trackData.set(a.id,{track:a,timescale:t,samples:[],chunkOffsets:[],currentChunk:null})}async writeHeader(){if(await this.writeFtyp(),!this.options.fastStart)this.mdatStart=this.writer.position,await this.writeMdatHeader(0)}async writeFtyp(){let a=[this.options.brand,"isom","iso2","mp42"],t=16+a.length*4;await this.writer.writeU32BE(t),await this.writer.writeFourCC("ftyp"),await this.writer.writeFourCC(this.options.brand),await this.writer.writeU32BE(512);for(let r of a)await this.writer.writeFourCC(r)}async writeMdatHeader(a){if(a>4294967287)await this.writer.writeU32BE(1),await this.writer.writeFourCC("mdat"),await this.writer.writeU64BE(BigInt(a+16));else await this.writer.writeU32BE(a+8),await this.writer.writeFourCC("mdat")}async writeVideoPacket(a,t){await this.writeSample(a.id,t)}async writeAudioPacket(a,t){await this.writeSample(a.id,t)}async writeSubtitlePacket(a,t){await this.writeSample(a.id,t)}async writeSample(a,t){let r=this.trackData.get(a);if(!r)return;let w={data:t.data,timestamp:t.timestamp,duration:t.duration??0,isKeyframe:t.isKeyframe,compositionTimeOffset:t.compositionTimeOffset??0,size:t.data.byteLength};if(this.options.fastStart)r.samples.push(w);else{let n=this.writer.position;if(await this.writer.writeBytes(t.data),this.mdatSize+=t.data.byteLength,w.size=t.data.byteLength,r.samples.push(w),!r.currentChunk)r.currentChunk={samples:[],offset:n},r.chunkOffsets.push(n);r.currentChunk.samples.push(w)}}async writeTrailer(){if(this.options.fastStart){let a=await this.buildMoov(0);this.mdatStart=this.writer.position+a.byteLength+8,await this.adjustChunkOffsets(this.mdatStart);let t=await this.buildMoov(this.mdatStart);await this.writer.writeBytes(t);let r=this.chapters.length>0?this.buildChapterTextSamples():[],w=0;for(let n of this.trackData.values())for(let o of n.samples)w+=o.size;for(let n of r)w+=n.data.byteLength;await this.writeMdatHeader(w);for(let n of this.trackData.values())for(let o of n.samples)await this.writer.writeBytes(o.data);for(let n of r)await this.writer.writeBytes(n.data)}else{let a=await this.buildMoov(this.mdatStart+8);await this.writer.writeBytes(a)}}async adjustChunkOffsets(a){let t=a;for(let r of this.trackData.values()){r.chunkOffsets=[];for(let w of r.samples)r.chunkOffsets.push(t),t+=w.size}}async buildMoov(a){let t=new B,r=await this.buildMvhd(),w=[],n=0;for(let f of this.trackData.values())for(let y of f.samples)n+=y.size;let o=this.chapters.length>0,c=o?this.tracks.length+1:0,l=o?this.buildChapterTextSamples():[];for(let f of this.trackData.values()){let y=f.track.type==="audio"&&o?c:void 0,m=await this.buildTrak(f,a,y);w.push(m)}if(o&&l.length>0){let f=a+n,y=await this.buildChapterTrak(c,l,f);w.push(y)}let u=await this.buildUdta(),d=r.byteLength+w.reduce((f,y)=>f+y.byteLength,0);if(u)d+=u.byteLength;let U=d+8;await t.writeU32BE(U),await t.writeFourCC("moov"),await t.writeBytes(r);for(let f of w)await t.writeBytes(f);if(u)await t.writeBytes(u);return t.getBuffer()}async buildMvhd(){let a=new B,t=BigInt(Math.floor(Date.now()/1000))+e,r=0n;for(let o of this.trackData.values()){let c=o.samples[o.samples.length-1];if(c){let l=BigInt(Math.round((c.timestamp+c.duration)*T));if(l>r)r=l}}let w=108,n=this.chapters.length>0;await a.writeU32BE(w),await a.writeFourCC("mvhd"),await a.writeU8(0),await a.writeU8(0),await a.writeU8(0),await a.writeU8(0),await a.writeU32BE(Number(t&0xFFFFFFFFn)),await a.writeU32BE(Number(t&0xFFFFFFFFn)),await a.writeU32BE(T),await a.writeU32BE(Number(r&0xFFFFFFFFn)),await a.writeU32BE(65536),await a.writeU16BE(256),await a.writeU16BE(0),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(65536),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(65536),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU32BE(1073741824);for(let o=0;o<6;o++)await a.writeU32BE(0);return await a.writeU32BE(this.tracks.length+1+(n?1:0)),a.getBuffer()}async buildTrak(a,t,r){let w=new B,n=await this.buildTkhd(a),o=r?await this.buildTref(r):null,c=await this.buildMdia(a,t),l=8+n.byteLength+c.byteLength;if(o)l+=o.byteLength;if(await w.writeU32BE(l),await w.writeFourCC("trak"),await w.writeBytes(n),o)await w.writeBytes(o);return await w.writeBytes(c),w.getBuffer()}async buildTkhd(a){let t=new B,r=BigInt(Math.floor(Date.now()/1000))+e,w=a.samples[a.samples.length-1],n=w?BigInt(Math.round((w.timestamp+w.duration)*T)):0n,o=92,c=a.track,l=0,u=0,d=0;if(c.type==="video")l=c.config.width,u=c.config.height;else if(c.type==="audio")d=256;return await t.writeU32BE(92),await t.writeFourCC("tkhd"),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU8(3),await t.writeU32BE(Number(r&0xFFFFFFFFn)),await t.writeU32BE(Number(r&0xFFFFFFFFn)),await t.writeU32BE(c.id),await t.writeU32BE(0),await t.writeU32BE(Number(n&0xFFFFFFFFn)),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU16BE(d),await t.writeU16BE(0),await t.writeU32BE(65536),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(65536),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(1073741824),await t.writeU32BE(l<<16),await t.writeU32BE(u<<16),t.getBuffer()}async buildMdia(a,t){let r=new B,w=await this.buildMdhd(a),n=await this.buildHdlr(a),o=await this.buildMinf(a,t),c=8+w.byteLength+n.byteLength+o.byteLength;return await r.writeU32BE(c),await r.writeFourCC("mdia"),await r.writeBytes(w),await r.writeBytes(n),await r.writeBytes(o),r.getBuffer()}async buildMdhd(a){let t=new B,r=BigInt(Math.floor(Date.now()/1000))+e,w=a.samples[a.samples.length-1],n=w?BigInt(Math.round((w.timestamp+w.duration)*a.timescale)):0n,o=32;return await t.writeU32BE(32),await t.writeFourCC("mdhd"),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU32BE(Number(r&0xFFFFFFFFn)),await t.writeU32BE(Number(r&0xFFFFFFFFn)),await t.writeU32BE(a.timescale),await t.writeU32BE(Number(n&0xFFFFFFFFn)),await t.writeU16BE(s("und")),await t.writeU16BE(0),t.getBuffer()}async buildHdlr(a){let t=new B,r="",w="";if(a.track.type==="video")r="vide",w="VideoHandler";else if(a.track.type==="audio")r="soun",w="SoundHandler";else r="text",w="SubtitleHandler";let n=new TextEncoder().encode(`${w}\x00`),o=32+n.byteLength;return await t.writeU32BE(o),await t.writeFourCC("hdlr"),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeFourCC(r),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeBytes(n),t.getBuffer()}async buildMinf(a,t){let r=new B,w;if(a.track.type==="video")w=await this.buildVmhd();else if(a.track.type==="audio")w=await this.buildSmhd();else w=await this.buildNmhd();let n=await this.buildDinf(),o=await this.buildStbl(a,t),c=8+w.byteLength+n.byteLength+o.byteLength;return await r.writeU32BE(c),await r.writeFourCC("minf"),await r.writeBytes(w),await r.writeBytes(n),await r.writeBytes(o),r.getBuffer()}async buildVmhd(){let a=new B,t=20;return await a.writeU32BE(20),await a.writeFourCC("vmhd"),await a.writeU32BE(1),await a.writeU16BE(0),await a.writeU16BE(0),await a.writeU16BE(0),await a.writeU16BE(0),a.getBuffer()}async buildSmhd(){let a=new B,t=16;return await a.writeU32BE(16),await a.writeFourCC("smhd"),await a.writeU32BE(0),await a.writeU16BE(0),await a.writeU16BE(0),a.getBuffer()}async buildNmhd(){let a=new B,t=12;return await a.writeU32BE(12),await a.writeFourCC("nmhd"),await a.writeU32BE(0),a.getBuffer()}async buildDinf(){let a=new B,t=await this.buildDref(),r=8+t.byteLength;return await a.writeU32BE(r),await a.writeFourCC("dinf"),await a.writeBytes(t),a.getBuffer()}async buildDref(){let a=new B,t=28;return await a.writeU32BE(28),await a.writeFourCC("dref"),await a.writeU32BE(0),await a.writeU32BE(1),await a.writeU32BE(12),await a.writeFourCC("url "),await a.writeU32BE(1),a.getBuffer()}async buildStbl(a,t){let r=new B,w=await this.buildStsd(a),n=await this.buildStts(a),o=await this.buildStsc(a),c=await this.buildStsz(a),l=await this.buildStco(a,t),u=null,d=null;if(a.track.type==="video")u=await this.buildStss(a),d=await this.buildCtts(a);let U=8+w.byteLength+n.byteLength+o.byteLength+c.byteLength+l.byteLength;if(u)U+=u.byteLength;if(d)U+=d.byteLength;if(await r.writeU32BE(U),await r.writeFourCC("stbl"),await r.writeBytes(w),await r.writeBytes(n),d)await r.writeBytes(d);if(await r.writeBytes(o),await r.writeBytes(c),await r.writeBytes(l),u)await r.writeBytes(u);return r.getBuffer()}async buildStsd(a){let t=new B,r;if(a.track.type==="video")r=await this.buildVideoSampleEntry(a.track);else if(a.track.type==="audio")r=await this.buildAudioSampleEntry(a.track);else r=new Uint8Array(0);let w=16+r.byteLength;return await t.writeU32BE(w),await t.writeFourCC("stsd"),await t.writeU32BE(0),await t.writeU32BE(1),await t.writeBytes(r),t.getBuffer()}async buildVideoSampleEntry(a){let t=new B,r=a.config,w="";if(r.codec==="h264")w="avc1";else if(r.codec==="h265")w="hev1";else if(r.codec==="vp9")w="vp09";else if(r.codec==="av1")w="av01";else if(r.codec==="prores")w={proxy:"apco",lt:"apcs","422":"apcn",standard:"apcn",hq:"apch","4444":"ap4h","4444-xq":"ap4x"}[r.profile??"422"]??"apcn";else w="mp4v";let n=r.codecDescription??new Uint8Array(0),o=r.codec==="h264"?"avcC":r.codec==="h265"?"hvcC":r.codec==="vp9"?"vpcC":r.codec==="av1"?"av1C":void 0,c=o&&n.byteLength>0?8+n.byteLength:0,l=86+c;await t.writeU32BE(l),await t.writeFourCC(w),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(1),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(r.width),await t.writeU16BE(r.height),await t.writeU32BE(4718592),await t.writeU32BE(4718592),await t.writeU32BE(0),await t.writeU16BE(1);let u=new Uint8Array(32);if(await t.writeBytes(u),await t.writeU16BE(24),await t.writeI16BE(-1),c>0)await t.writeU32BE(c),await t.writeFourCC(o),await t.writeBytes(n);return t.getBuffer()}async buildAudioSampleEntry(a){let t=new B,r=a.config,w="mp4a";if(r.codec==="opus")w="Opus";else if(r.codec==="flac")w="fLaC";let n=r.codecDescription??new Uint8Array(0),o=n.byteLength>0?8+n.byteLength:0,c=36+o;if(await t.writeU32BE(c),await t.writeFourCC(w),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(1),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(r.channels),await t.writeU16BE(r.bitsPerSample??16),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU32BE(r.sampleRate<<16),o>0)await t.writeU32BE(o),await t.writeFourCC("esds"),await t.writeBytes(n);return t.getBuffer()}async buildStts(a){let t=new B,r=[],w=-1;for(let o of a.samples){let c=Math.round(o.duration*a.timescale);if(c===w&&r.length>0)r[r.length-1].sampleCount++;else r.push({sampleCount:1,sampleDelta:c}),w=c}let n=16+r.length*8;await t.writeU32BE(n),await t.writeFourCC("stts"),await t.writeU32BE(0),await t.writeU32BE(r.length);for(let o of r)await t.writeU32BE(o.sampleCount),await t.writeU32BE(o.sampleDelta);return t.getBuffer()}async buildCtts(a){let t=new B,r=[],w=Number.MIN_SAFE_INTEGER;for(let o of a.samples){let c=Math.round(o.compositionTimeOffset*a.timescale);if(c===w&&r.length>0)r[r.length-1].sampleCount++;else r.push({sampleCount:1,sampleOffset:c}),w=c}if(r.length===0||r.length===1&&r[0].sampleOffset===0)return new Uint8Array(0);let n=16+r.length*8;await t.writeU32BE(n),await t.writeFourCC("ctts"),await t.writeU32BE(0),await t.writeU32BE(r.length);for(let o of r)await t.writeU32BE(o.sampleCount),await t.writeU32BE(o.sampleOffset);return t.getBuffer()}async buildStsc(a){let t=new B,r=28;return await t.writeU32BE(28),await t.writeFourCC("stsc"),await t.writeU32BE(0),await t.writeU32BE(1),await t.writeU32BE(1),await t.writeU32BE(1),await t.writeU32BE(1),t.getBuffer()}async buildStsz(a){let t=new B,r=20+a.samples.length*4;await t.writeU32BE(r),await t.writeFourCC("stsz"),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(a.samples.length);for(let w of a.samples)await t.writeU32BE(w.size);return t.getBuffer()}async buildStco(a,t){let r=new B,w=t,n=[];for(let c of a.samples)n.push(w),w+=c.size;let o=16+n.length*4;await r.writeU32BE(o),await r.writeFourCC("stco"),await r.writeU32BE(0),await r.writeU32BE(n.length);for(let c of n)await r.writeU32BE(c);return r.getBuffer()}async buildStss(a){let t=new B,r=[];for(let n=0;n<a.samples.length;n++)if(a.samples[n].isKeyframe)r.push(n+1);if(r.length===a.samples.length)return new Uint8Array(0);let w=16+r.length*4;await t.writeU32BE(w),await t.writeFourCC("stss"),await t.writeU32BE(0),await t.writeU32BE(r.length);for(let n of r)await t.writeU32BE(n);return t.getBuffer()}async buildTref(a){let t=new B;return await t.writeU32BE(20),await t.writeFourCC("tref"),await t.writeU32BE(12),await t.writeFourCC("chap"),await t.writeU32BE(a),t.getBuffer()}buildChapterTextSamples(){if(this.chapters.length===0)return[];let a=0;for(let w of this.trackData.values()){let n=w.samples[w.samples.length-1];if(n)a=Math.max(a,Math.round((n.timestamp+n.duration)*1000))}let t=new TextEncoder,r=[];for(let w=0;w<this.chapters.length;w++){let n=this.chapters[w],o=w+1<this.chapters.length?this.chapters[w+1].startTimeMs:a,c=Math.max(1,Math.round(o-n.startTimeMs)),l=t.encode(n.title),u=new Uint8Array(2+l.length);u[0]=l.length>>8&255,u[1]=l.length&255,u.set(l,2),r.push({data:u,durationMs:c})}return r}async buildTextSampleEntry(){let a=new B;return await a.writeU32BE(59),await a.writeFourCC("text"),await a.writeU32BE(0),await a.writeU16BE(0),await a.writeU16BE(1),await a.writeU32BE(1),await a.writeU32BE(0),await a.writeU16BE(0),await a.writeU16BE(0),await a.writeU16BE(0),await a.writeU16BE(0),await a.writeU16BE(0),await a.writeU16BE(0),await a.writeU16BE(1),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU16BE(0),await a.writeU16BE(13),await a.writeFourCC("ftab"),await a.writeU16BE(1),await a.writeU16BE(1),await a.writeU8(0),a.getBuffer()}async buildChapterTrak(a,t,r){let w=new B,n=BigInt(Math.floor(Date.now()/1000))+e,o=1000,c=new TextEncoder,l=0;for(let E of t)l+=E.durationMs;let u=new B;await u.writeU32BE(92),await u.writeFourCC("tkhd"),await u.writeU8(0),await u.writeU8(0),await u.writeU8(0),await u.writeU8(1),await u.writeU32BE(Number(n&0xFFFFFFFFn)),await u.writeU32BE(Number(n&0xFFFFFFFFn)),await u.writeU32BE(a),await u.writeU32BE(0),await u.writeU32BE(l),await u.writeU32BE(0),await u.writeU32BE(0),await u.writeU16BE(0),await u.writeU16BE(0),await u.writeU16BE(0),await u.writeU16BE(0),await u.writeU32BE(65536),await u.writeU32BE(0),await u.writeU32BE(0),await u.writeU32BE(0),await u.writeU32BE(65536),await u.writeU32BE(0),await u.writeU32BE(0),await u.writeU32BE(0),await u.writeU32BE(1073741824),await u.writeU32BE(0),await u.writeU32BE(0);let d=u.getBuffer(),U=new B;await U.writeU32BE(32),await U.writeFourCC("mdhd"),await U.writeU32BE(0),await U.writeU32BE(Number(n&0xFFFFFFFFn)),await U.writeU32BE(Number(n&0xFFFFFFFFn)),await U.writeU32BE(1000),await U.writeU32BE(l),await U.writeU16BE(0),await U.writeU16BE(0);let f=U.getBuffer(),y=c.encode("ChapterHandler\x00"),m=new B;await m.writeU32BE(32+y.byteLength),await m.writeFourCC("hdlr"),await m.writeU32BE(0),await m.writeU32BE(0),await m.writeFourCC("text"),await m.writeU32BE(0),await m.writeU32BE(0),await m.writeU32BE(0),await m.writeBytes(y);let z=m.getBuffer(),g=new B;await g.writeU32BE(36),await g.writeFourCC("edts"),await g.writeU32BE(28),await g.writeFourCC("elst"),await g.writeU32BE(0),await g.writeU32BE(1),await g.writeU32BE(l),await g.writeU32BE(0),await g.writeU32BE(65536);let L=g.getBuffer(),h=new B;await h.writeU32BE(76),await h.writeFourCC("gmhd"),await h.writeU32BE(24),await h.writeFourCC("gmin"),await h.writeU32BE(0),await h.writeU16BE(64),await h.writeU16BE(32768),await h.writeU16BE(32768),await h.writeU16BE(32768),await h.writeU16BE(0),await h.writeU16BE(0),await h.writeU32BE(44),await h.writeFourCC("text"),await h.writeU32BE(65536),await h.writeU32BE(0),await h.writeU32BE(0),await h.writeU32BE(0),await h.writeU32BE(65536),await h.writeU32BE(0),await h.writeU32BE(0),await h.writeU32BE(0),await h.writeU32BE(1073741824);let D=h.getBuffer(),P=await this.buildDinf(),O=await this.buildTextSampleEntry(),k=new B;await k.writeU32BE(16+O.byteLength),await k.writeFourCC("stsd"),await k.writeU32BE(0),await k.writeU32BE(1),await k.writeBytes(O);let N=k.getBuffer(),C=[],I=-1;for(let E of t)if(E.durationMs===I&&C.length>0)C[C.length-1].count++;else C.push({count:1,delta:E.durationMs}),I=E.durationMs;let x=new B;await x.writeU32BE(16+C.length*8),await x.writeFourCC("stts"),await x.writeU32BE(0),await x.writeU32BE(C.length);for(let E of C)await x.writeU32BE(E.count),await x.writeU32BE(E.delta);let V=x.getBuffer(),b=new B;await b.writeU32BE(28),await b.writeFourCC("stsc"),await b.writeU32BE(0),await b.writeU32BE(1),await b.writeU32BE(1),await b.writeU32BE(1),await b.writeU32BE(1);let W=b.getBuffer(),v=new B;await v.writeU32BE(20+t.length*4),await v.writeFourCC("stsz"),await v.writeU32BE(0),await v.writeU32BE(0),await v.writeU32BE(t.length);for(let E of t)await v.writeU32BE(E.data.byteLength);let H=v.getBuffer(),M=new B;await M.writeU32BE(16+t.length*4),await M.writeFourCC("stco"),await M.writeU32BE(0),await M.writeU32BE(t.length);let j=r;for(let E of t)await M.writeU32BE(j),j+=E.data.byteLength;let R=M.getBuffer(),G=8+N.byteLength+V.byteLength+W.byteLength+H.byteLength+R.byteLength,F=new B;await F.writeU32BE(G),await F.writeFourCC("stbl"),await F.writeBytes(N),await F.writeBytes(V),await F.writeBytes(W),await F.writeBytes(H),await F.writeBytes(R);let _=F.getBuffer(),J=8+D.byteLength+P.byteLength+_.byteLength,A=new B;await A.writeU32BE(J),await A.writeFourCC("minf"),await A.writeBytes(D),await A.writeBytes(P),await A.writeBytes(_);let K=A.getBuffer(),Q=8+f.byteLength+z.byteLength+K.byteLength,S=new B;await S.writeU32BE(Q),await S.writeFourCC("mdia"),await S.writeBytes(f),await S.writeBytes(z),await S.writeBytes(K);let q=S.getBuffer(),X=8+d.byteLength+L.byteLength+q.byteLength;return await w.writeU32BE(X),await w.writeFourCC("trak"),await w.writeBytes(d),await w.writeBytes(L),await w.writeBytes(q),w.getBuffer()}async buildUdta(){let a=this.metadata&&Object.keys(this.metadata).length>0,t=this.artwork!==null,r=this.chapters.length>0;if(!a&&!t&&!r)return null;let w=new B,n=[];if(r)n.push(await this.buildChpl());if(a||t)n.push(await this.buildMeta());let o=n.reduce((c,l)=>c+l.byteLength,0);await w.writeU32BE(o+8),await w.writeFourCC("udta");for(let c of n)await w.writeBytes(c);return w.getBuffer()}async buildMeta(){let a=new B,t=await this.buildMetaHdlr(),r=await this.buildIlst(),w=4+t.byteLength+r.byteLength;return await a.writeU32BE(w+8),await a.writeFourCC("meta"),await a.writeU32BE(0),await a.writeBytes(t),await a.writeBytes(r),a.getBuffer()}async buildMetaHdlr(){let a=new B,t=33;return await a.writeU32BE(33),await a.writeFourCC("hdlr"),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeFourCC("mdir"),await a.writeFourCC("appl"),await a.writeU32BE(0),await a.writeU32BE(0),await a.writeU8(0),a.getBuffer()}async buildIlst(){let a=new B,t=[],r=this.metadata;if(r.title)t.push(p("©nam",r.title));if(r.artist)t.push(p("©ART",r.artist));if(r.albumArtist)t.push(p("aART",r.albumArtist));if(r.album)t.push(p("©alb",r.album));if(r.genre)t.push(p("©gen",r.genre));if(r.year)t.push(p("©day",String(r.year)));if(r.composer)t.push(p("©wrt",r.composer));if(r.copyright)t.push(p("cprt",r.copyright));if(r.comment)t.push(p("©cmt",r.comment));if(r.encodedBy)t.push(p("©too",r.encodedBy));if(r.narrator)t.push(p("©nrt",r.narrator));if(r.publisher)t.push(p("©pub",r.publisher));if(r.description)t.push(p("desc",r.description));if(this.artwork){let n=this.artworkType==="png"?14:13;t.push(Z("covr",this.artwork,n))}let w=t.reduce((n,o)=>n+o.byteLength,0);await a.writeU32BE(w+8),await a.writeFourCC("ilst");for(let n of t)await a.writeBytes(n);return a.getBuffer()}async buildChpl(){let a=new B,t=new TextEncoder,r=[];for(let o of this.chapters){let c=t.encode(o.title),l=Math.min(c.length,255),u=BigInt(Math.round(o.startTimeMs*1e4)),d=new Uint8Array(9+l);new DataView(d.buffer).setBigUint64(0,u),d[8]=l,d.set(c.subarray(0,l),9),r.push(d)}let n=12+r.reduce((o,c)=>o+c.byteLength,0);await a.writeU32BE(n+8),await a.writeFourCC("chpl"),await a.writeU8(1),await a.writeU8(0),await a.writeU8(0),await a.writeU8(0),await a.writeU32BE(0),await a.writeU32BE(this.chapters.length);for(let o of r)await a.writeBytes(o);return a.getBuffer()}}function p(a,t){let w=new TextEncoder().encode(t),n=16+w.length,o=8+n,c=new Uint8Array(o),l=new DataView(c.buffer);return l.setUint32(0,o),c[4]=a.charCodeAt(0)&255,c[5]=a.charCodeAt(1)&255,c[6]=a.charCodeAt(2)&255,c[7]=a.charCodeAt(3)&255,l.setUint32(8,n),c[12]=100,c[13]=97,c[14]=116,c[15]=97,l.setUint32(16,1),l.setUint32(20,0),c.set(w,24),c}function Z(a,t,r){let w=16+t.length,n=8+w,o=new Uint8Array(n),c=new DataView(o.buffer);return c.setUint32(0,n),o[4]=a.charCodeAt(0)&255,o[5]=a.charCodeAt(1)&255,o[6]=a.charCodeAt(2)&255,o[7]=a.charCodeAt(3)&255,c.setUint32(8,w),o[12]=100,o[13]=97,o[14]=116,o[15]=97,c.setUint32(16,r),c.setUint32(20,0),o.set(t,24),o}
|
|
2
|
-
export{i};
|
package/dist/chunk-m7vh938z.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{m,p}from"./chunk-qmjyh9jc.js";import{a}from"./chunk-yjtc1ksx.js";import{i}from"./chunk-c9md36vm.js";import{r}from"./chunk-tghqqx5m.js";import{InputFormat as u}from"ts-videos/demuxer";import{OutputFormat as s}from"ts-videos/muxer";import{Reader as c}from"ts-videos/reader";class f extends u{get name(){return"mp4"}get mimeType(){return"video/mp4"}get extensions(){return["mp4","m4v","m4a","f4v"]}async canRead(e){let t=c.fromSource(e);t.position=0;let n=await t.readU32BE();if(n===null||n<8)return!1;if(await t.readFourCC()!=="ftyp")return!1;let o=await t.readFourCC();if(!o)return!1;return m.has(o)}createDemuxer(e){return new a(e)}}class w extends u{get name(){return"mov"}get mimeType(){return"video/quicktime"}get extensions(){return["mov","qt"]}async canRead(e){let t=c.fromSource(e);t.position=0;let n=await t.readU32BE();if(n===null||n<8)return!1;if(await t.readFourCC()!=="ftyp")return!1;let o=await t.readFourCC();if(!o)return!1;return p.has(o)}createDemuxer(e){return new a(e)}}class h extends s{options;constructor(e={}){super();this.options=e}get name(){return"mp4"}get mimeType(){return"video/mp4"}get extension(){return"mp4"}createMuxer(e){return new i(e,this.options)}}class B extends s{options;constructor(e={}){super();this.options={...e,brand:"qt "}}get name(){return"mov"}get mimeType(){return"video/quicktime"}get extension(){return"mov"}createMuxer(e){return new i(e,this.options)}}class U extends s{options;constructor(e={}){super();this.options=e}get name(){return"fmp4"}get mimeType(){return"video/mp4"}get extension(){return"mp4"}createMuxer(e){return new r(e,this.options)}}class y extends s{options;constructor(e={}){super();this.options={...e,cmaf:!0}}get name(){return"cmaf"}get mimeType(){return"video/mp4"}get extension(){return"cmfv"}createMuxer(e){return new r(e,this.options)}}var E=new f,x=new w,C=new h,F=new B,v=new U,S=new y;
|
|
2
|
-
export{f,w,h,B,U,y,E,x,C,F,v,S};
|
package/dist/chunk-qmjyh9jc.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
var t=8,c=16;function u(r,i){return String.fromCharCode(r.getUint8(i),r.getUint8(i+1),r.getUint8(i+2),r.getUint8(i+3))}function b(r,i,x){for(let a=0;a<4;a++)r.setUint8(i+a,x.charCodeAt(a))}var d=new Set(["moov","trak","mdia","minf","stbl","dinf","edts","udta","meta","ilst","moof","traf","mvex","sinf","schi","rinf"]),m=new Set(["isom","iso2","iso3","iso4","iso5","iso6","mp41","mp42","mp71","avc1","av01","hev1","hvc1","M4A ","M4V ","M4P ","M4B ","f4v ","f4a ","dash","msdh","msix"]),p=new Set(["qt "]);function n(r){return r==="vide"}function o(r){return r==="soun"}function g(r){return r==="subt"||r==="text"||r==="sbtl"}function l(r){let i=(r>>10&31)+96,x=(r>>5&31)+96,a=(r&31)+96;return String.fromCharCode(i,x,a)}function s(r){if(r.length!==3)return 21956;let i=r.charCodeAt(0)-96&31,x=r.charCodeAt(1)-96&31,a=r.charCodeAt(2)-96&31;return i<<10|x<<5|a}var e=2082844800n;
|
|
2
|
-
export{t,c,u,b,d,m,p,n,o,g,l,s,e};
|
package/dist/chunk-tghqqx5m.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{s,e}from"./chunk-qmjyh9jc.js";import{Muxer as h}from"ts-videos/muxer";import{Writer as u}from"ts-videos/writer";var y=1000;class r extends h{trackData=new Map;options;sequenceNumber=1;fragmentStartTime=0;initialized=!1;constructor(i,t={}){super(i);if(this.options={fragmentDuration:t.fragmentDuration??2,brand:t.brand??"iso5",cmaf:t.cmaf??!1,lowLatency:t.lowLatency??!1},this.options.lowLatency)this.options.fragmentDuration=Math.min(0.5,this.options.fragmentDuration)}get formatName(){return"fmp4"}get mimeType(){return"video/mp4"}onTrackAdded(i){let t=i.type==="video"?Math.round((i.config.frameRate??30)*1000):i.type==="audio"?i.config.sampleRate:1000;this.trackData.set(i.id,{track:i,timescale:t,samples:[],baseMediaDecodeTime:0n,defaultSampleDuration:0,defaultSampleSize:0,defaultSampleFlags:i.type==="video"?65536:0})}async writeHeader(){await this.writeFtyp(),await this.writeMoov(),this.initialized=!0}async writeFtyp(){let i=this.options.cmaf?["cmfc","iso6","mp41"]:[this.options.brand,"iso5","iso6","mp41","avc1"],t=16+i.length*4;await this.writer.writeU32BE(t),await this.writer.writeFourCC("ftyp"),await this.writer.writeFourCC(this.options.cmaf?"cmfc":this.options.brand),await this.writer.writeU32BE(512);for(let a of i)await this.writer.writeFourCC(a)}async writeMoov(){let i=new u,t=await this.buildMvhd(),a=await this.buildMvex(),w=[];for(let o of this.trackData.values()){let m=await this.buildTrak(o);w.push(m)}let c=t.byteLength+a.byteLength+w.reduce((o,m)=>o+m.byteLength,0)+8;await i.writeU32BE(c),await i.writeFourCC("moov"),await i.writeBytes(t);for(let o of w)await i.writeBytes(o);await i.writeBytes(a),await this.writer.writeBytes(i.getBuffer())}async buildMvhd(){let i=new u,t=BigInt(Math.floor(Date.now()/1000))+e,a=116;await i.writeU32BE(116),await i.writeFourCC("mvhd"),await i.writeU8(0),await i.writeU8(0),await i.writeU8(0),await i.writeU8(0),await i.writeU32BE(Number(t&0xFFFFFFFFn)),await i.writeU32BE(Number(t&0xFFFFFFFFn)),await i.writeU32BE(y),await i.writeU32BE(0),await i.writeU32BE(65536),await i.writeU16BE(256),await i.writeU16BE(0),await i.writeU32BE(0),await i.writeU32BE(0),await i.writeU32BE(65536),await i.writeU32BE(0),await i.writeU32BE(0),await i.writeU32BE(0),await i.writeU32BE(65536),await i.writeU32BE(0),await i.writeU32BE(0),await i.writeU32BE(0),await i.writeU32BE(1073741824);for(let w=0;w<6;w++)await i.writeU32BE(0);return await i.writeU32BE(this.tracks.length+1),i.getBuffer()}async buildMvex(){let i=new u,t=[];for(let w of this.trackData.values())t.push(await this.buildTrex(w));let a=8+t.reduce((w,n)=>w+n.byteLength,0);await i.writeU32BE(a),await i.writeFourCC("mvex");for(let w of t)await i.writeBytes(w);return i.getBuffer()}async buildTrex(i){let t=new u,a=32;return await t.writeU32BE(32),await t.writeFourCC("trex"),await t.writeU32BE(0),await t.writeU32BE(i.track.id),await t.writeU32BE(1),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(i.defaultSampleFlags),t.getBuffer()}async buildTrak(i){let t=new u,a=await this.buildTkhd(i),w=await this.buildMdia(i),n=8+a.byteLength+w.byteLength;return await t.writeU32BE(n),await t.writeFourCC("trak"),await t.writeBytes(a),await t.writeBytes(w),t.getBuffer()}async buildTkhd(i){let t=new u,a=BigInt(Math.floor(Date.now()/1000))+e,w=92,n=i.track,c=0,o=0,m=0;if(n.type==="video")c=n.config.width,o=n.config.height;else if(n.type==="audio")m=256;return await t.writeU32BE(92),await t.writeFourCC("tkhd"),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU8(3),await t.writeU32BE(Number(a&0xFFFFFFFFn)),await t.writeU32BE(Number(a&0xFFFFFFFFn)),await t.writeU32BE(n.id),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU16BE(m),await t.writeU16BE(0),await t.writeU32BE(65536),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(65536),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(1073741824),await t.writeU32BE(c<<16),await t.writeU32BE(o<<16),t.getBuffer()}async buildMdia(i){let t=new u,a=await this.buildMdhd(i),w=await this.buildHdlr(i),n=await this.buildMinf(i),c=8+a.byteLength+w.byteLength+n.byteLength;return await t.writeU32BE(c),await t.writeFourCC("mdia"),await t.writeBytes(a),await t.writeBytes(w),await t.writeBytes(n),t.getBuffer()}async buildMdhd(i){let t=new u,a=BigInt(Math.floor(Date.now()/1000))+e,w=40;return await t.writeU32BE(40),await t.writeFourCC("mdhd"),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU32BE(Number(a&0xFFFFFFFFn)),await t.writeU32BE(Number(a&0xFFFFFFFFn)),await t.writeU32BE(i.timescale),await t.writeU32BE(0),await t.writeU16BE(s("und")),await t.writeU16BE(0),t.getBuffer()}async buildHdlr(i){let t=new u,a="",w="";if(i.track.type==="video")a="vide",w="VideoHandler";else if(i.track.type==="audio")a="soun",w="SoundHandler";else a="text",w="SubtitleHandler";let n=new TextEncoder().encode(`${w}\x00`),c=32+n.byteLength+8;return await t.writeU32BE(c),await t.writeFourCC("hdlr"),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeFourCC(a),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeBytes(n),t.getBuffer()}async buildMinf(i){let t=new u,a;if(i.track.type==="video")a=await this.buildVmhd();else if(i.track.type==="audio")a=await this.buildSmhd();else a=await this.buildNmhd();let w=await this.buildDinf(),n=await this.buildStbl(i),c=8+a.byteLength+w.byteLength+n.byteLength;return await t.writeU32BE(c),await t.writeFourCC("minf"),await t.writeBytes(a),await t.writeBytes(w),await t.writeBytes(n),t.getBuffer()}async buildVmhd(){let i=new u,t=20;return await i.writeU32BE(20),await i.writeFourCC("vmhd"),await i.writeU32BE(1),await i.writeU16BE(0),await i.writeU16BE(0),await i.writeU16BE(0),await i.writeU16BE(0),i.getBuffer()}async buildSmhd(){let i=new u,t=16;return await i.writeU32BE(16),await i.writeFourCC("smhd"),await i.writeU32BE(0),await i.writeU16BE(0),await i.writeU16BE(0),i.getBuffer()}async buildNmhd(){let i=new u,t=12;return await i.writeU32BE(12),await i.writeFourCC("nmhd"),await i.writeU32BE(0),i.getBuffer()}async buildDinf(){let i=new u,t=await this.buildDref(),a=8+t.byteLength;return await i.writeU32BE(a),await i.writeFourCC("dinf"),await i.writeBytes(t),i.getBuffer()}async buildDref(){let i=new u,t=28;return await i.writeU32BE(28),await i.writeFourCC("dref"),await i.writeU32BE(0),await i.writeU32BE(1),await i.writeU32BE(12),await i.writeFourCC("url "),await i.writeU32BE(1),i.getBuffer()}async buildStbl(i){let t=new u,a=await this.buildStsd(i),w=await this.buildEmptyAtom("stts"),n=await this.buildEmptyAtom("stsc"),c=await this.buildEmptyStsz(),o=await this.buildEmptyAtom("stco"),m=8+a.byteLength+w.byteLength+n.byteLength+c.byteLength+o.byteLength;return await t.writeU32BE(m),await t.writeFourCC("stbl"),await t.writeBytes(a),await t.writeBytes(w),await t.writeBytes(n),await t.writeBytes(c),await t.writeBytes(o),t.getBuffer()}async buildEmptyAtom(i){let t=new u;return await t.writeU32BE(16),await t.writeFourCC(i),await t.writeU32BE(0),await t.writeU32BE(0),t.getBuffer()}async buildEmptyStsz(){let i=new u;return await i.writeU32BE(20),await i.writeFourCC("stsz"),await i.writeU32BE(0),await i.writeU32BE(0),await i.writeU32BE(0),i.getBuffer()}async buildStsd(i){let t=new u,a;if(i.track.type==="video")a=await this.buildVideoSampleEntry(i.track);else if(i.track.type==="audio")a=await this.buildAudioSampleEntry(i.track);else a=new Uint8Array(0);let w=16+a.byteLength;return await t.writeU32BE(w),await t.writeFourCC("stsd"),await t.writeU32BE(0),await t.writeU32BE(1),await t.writeBytes(a),t.getBuffer()}async buildVideoSampleEntry(i){let t=new u,a=i.config,w="";if(a.codec==="h264")w="avc1";else if(a.codec==="h265")w="hev1";else if(a.codec==="vp9")w="vp09";else if(a.codec==="av1")w="av01";else w="mp4v";let n=a.codecDescription??new Uint8Array(0),c=a.codec==="h264"?"avcC":a.codec==="h265"?"hvcC":"avcC",o=n.byteLength>0?8+n.byteLength:0,m=86+o;await t.writeU32BE(m),await t.writeFourCC(w),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(1),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(a.width),await t.writeU16BE(a.height),await t.writeU32BE(4718592),await t.writeU32BE(4718592),await t.writeU32BE(0),await t.writeU16BE(1);let d=new Uint8Array(32);if(await t.writeBytes(d),await t.writeU16BE(24),await t.writeI16BE(-1),o>0)await t.writeU32BE(o),await t.writeFourCC(c),await t.writeBytes(n);return t.getBuffer()}async buildAudioSampleEntry(i){let t=new u,a=i.config,w="mp4a";if(a.codec==="opus")w="Opus";else if(a.codec==="flac")w="fLaC";let n=a.codecDescription??new Uint8Array(0),c=n.byteLength>0?8+n.byteLength:0,o=36+c;if(await t.writeU32BE(o),await t.writeFourCC(w),await t.writeU32BE(0),await t.writeU16BE(0),await t.writeU16BE(1),await t.writeU32BE(0),await t.writeU32BE(0),await t.writeU16BE(a.channels),await t.writeU16BE(a.bitsPerSample??16),await t.writeU16BE(0),await t.writeU16BE(0),await t.writeU32BE(a.sampleRate<<16),c>0)await t.writeU32BE(c),await t.writeFourCC("esds"),await t.writeBytes(n);return t.getBuffer()}async writeVideoPacket(i,t){await this.writeSample(i.id,t)}async writeAudioPacket(i,t){await this.writeSample(i.id,t)}async writeSubtitlePacket(i,t){await this.writeSample(i.id,t)}async writeSample(i,t){let a=this.trackData.get(i);if(!a)return;let w=[...this.trackData.values()].some((d)=>d.track.type==="video"),n=[...this.trackData.values()].some((d)=>d.samples.length>0),c=t.timestamp-this.fragmentStartTime>=this.options.fragmentDuration,o=a.track.type==="video"&&t.isKeyframe;if(n&&c&&(o||!w))await this.flushFragment();let m={data:t.data,timestamp:t.timestamp,duration:t.duration??0,isKeyframe:t.isKeyframe,compositionTimeOffset:t.compositionTimeOffset??0,size:t.data.byteLength};a.samples.push(m)}async flushFragment(){let i=!1;for(let n of this.trackData.values())if(n.samples.length>0){i=!0;break}if(!i)return;let t=await this.buildMoof(),a=await this.buildFragmentMdat();await this.writer.writeBytes(t),await this.writer.writeBytes(a),this.sequenceNumber++;let w=this.fragmentStartTime;for(let n of this.trackData.values()){if(n.samples.length>0){n.baseMediaDecodeTime+=n.samples.reduce((c,o)=>c+BigInt(Math.round(o.duration*n.timescale)),0n);for(let c of n.samples)w=Math.max(w,c.timestamp+c.duration)}n.samples=[]}this.fragmentStartTime=w}async buildMoof(){let i=new u,t=await this.buildMfhd(),a=[],w=8;for(let o of this.trackData.values())if(o.samples.length>0){a.push(await this.buildTraf(o,w));for(let m of o.samples)w+=m.size}let n=8+t.byteLength;for(let o of a)n+=o.byteLength;let c=[];w=n+8;for(let o of this.trackData.values())if(o.samples.length>0){c.push(await this.buildTraf(o,w));for(let m of o.samples)w+=m.size}n=8+t.byteLength;for(let o of c)n+=o.byteLength;await i.writeU32BE(n),await i.writeFourCC("moof"),await i.writeBytes(t);for(let o of c)await i.writeBytes(o);return i.getBuffer()}async buildMfhd(){let i=new u,t=16;return await i.writeU32BE(16),await i.writeFourCC("mfhd"),await i.writeU32BE(0),await i.writeU32BE(this.sequenceNumber),i.getBuffer()}async buildTraf(i,t){let a=new u,w=await this.buildTfhd(i),n=await this.buildTfdt(i),c=await this.buildTrun(i,t),o=8+w.byteLength+n.byteLength+c.byteLength;return await a.writeU32BE(o),await a.writeFourCC("traf"),await a.writeBytes(w),await a.writeBytes(n),await a.writeBytes(c),a.getBuffer()}async buildTfhd(i){let t=new u,a=0;if(i.samples.length>0){let c=Math.round(i.samples[0].duration*i.timescale);if(i.samples.every((m)=>Math.round(m.duration*i.timescale)===c))a=c}let w=131072|(a>0?8:0),n=16+(a>0?4:0);if(await t.writeU32BE(n),await t.writeFourCC("tfhd"),await t.writeU8(0),await t.writeU8(w>>16&255),await t.writeU8(w>>8&255),await t.writeU8(w&255),await t.writeU32BE(i.track.id),a>0)await t.writeU32BE(a);return t.getBuffer()}async buildTfdt(i){let t=new u,a=20;return await t.writeU32BE(20),await t.writeFourCC("tfdt"),await t.writeU8(1),await t.writeU8(0),await t.writeU8(0),await t.writeU8(0),await t.writeU64BE(i.baseMediaDecodeTime),t.getBuffer()}async buildTrun(i,t){let a=new u,w=!i.samples.every((f,l,U)=>l===0||Math.round(f.duration*i.timescale)===Math.round(U[0].duration*i.timescale)),n=!i.samples.every((f)=>f.size===i.samples[0].size),c=i.samples.some((f)=>f.isKeyframe!==i.samples[0].isKeyframe),o=i.samples.some((f)=>f.compositionTimeOffset!==0),m=1|(w?256:0)|(n?512:0)|(c?1024:0)|(o?2048:0),d=0;if(w)d+=4;if(n)d+=4;if(c)d+=4;if(o)d+=4;let B=20+i.samples.length*d;await a.writeU32BE(B),await a.writeFourCC("trun"),await a.writeU8(0),await a.writeU8(m>>16&255),await a.writeU8(m>>8&255),await a.writeU8(m&255),await a.writeU32BE(i.samples.length),await a.writeU32BE(t);for(let f of i.samples){if(w)await a.writeU32BE(Math.round(f.duration*i.timescale));if(n)await a.writeU32BE(f.size);if(c){let l=f.isKeyframe?0:65536;await a.writeU32BE(l)}if(o)await a.writeI32BE(Math.round(f.compositionTimeOffset*i.timescale))}return a.getBuffer()}async buildFragmentMdat(){let i=new u,t=0;for(let a of this.trackData.values())for(let w of a.samples)t+=w.size;await i.writeU32BE(t+8),await i.writeFourCC("mdat");for(let a of this.trackData.values())for(let w of a.samples)await i.writeBytes(w.data);return i.getBuffer()}async writeTrailer(){await this.flushFragment(),await this.writeMfra()}async writeMfra(){let i=new u,t=[],a=16,w=24;await i.writeU32BE(24),await i.writeFourCC("mfra"),await i.writeU32BE(16),await i.writeFourCC("mfro"),await i.writeU32BE(0),await i.writeU32BE(24),await this.writer.writeBytes(i.getBuffer())}}
|
|
2
|
-
export{r};
|
package/dist/chunk-yjtc1ksx.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{t,c,u,d,n,o,l}from"./chunk-qmjyh9jc.js";import{Demuxer as j}from"ts-videos/demuxer";class a extends j{movieTimescale=1000;movieDuration=0n;trackInfos=[];moovBox=null;mdatOffset=0;fragmented=!1;_initialized=!1;majorBrand="";get formatName(){return this.majorBrand==="qt "?"mov":"mp4"}get mimeType(){let s=this._tracks?.find((p)=>p.type==="video"),e=this._tracks?.find((p)=>p.type==="audio"),r="";if(s&&e)r=`${this.getCodecString(s)}, ${this.getCodecString(e)}`;else if(s)r=this.getCodecString(s);else if(e)r=this.getCodecString(e);let i=this.majorBrand==="qt "?"video/quicktime":"video/mp4";return r?`${i}; codecs="${r}"`:i}getCodecString(s){if(s.type==="video"){let e=s;if(e.codec==="h264"&&e.codecDescription){let r=e.codecDescription;if(r.length>=4){let i=r[1].toString(16).padStart(2,"0"),p=r[2].toString(16).padStart(2,"0"),h=r[3].toString(16).padStart(2,"0");return`avc1.${i}${p}${h}`}return"avc1"}if(e.codec==="h265")return"hev1";if(e.codec==="vp9")return"vp09";if(e.codec==="av1")return"av01";return e.codec}if(s.type==="audio"){let e=s;if(e.codec==="aac")return"mp4a.40.2";if(e.codec==="mp3")return"mp4a.40.34";if(e.codec==="opus")return"opus";if(e.codec==="flac")return"flac";return e.codec}return"unknown"}async init(){if(this._initialized)return;this._initialized=!0,await this.parseTopLevelBoxes(),await this.buildTracks(),await this.buildSampleTables()}async parseTopLevelBoxes(){let s=await this.reader.getSize(),e=0;while(s===null||e<s){this.reader.position=e;let r=await this.reader.readU32BE();if(r===null)break;let i=await this.reader.readFourCC();if(!i)break;let p=r;if(p===1){let h=await this.reader.readU64BE();if(h===null)break;p=Number(h)}else if(p===0)p=(s??0)-e;if(i==="ftyp")await this.parseFtyp(e,p);else if(i==="moov")this.moovBox={type:i,size:p,offset:e},await this.parseMoov(e,p);else if(i==="mdat")this.mdatOffset=e+t;else if(i==="moof")this.fragmented=!0;e+=p}}async parseFtyp(s,e){this.reader.position=s+t,this.majorBrand=await this.reader.readFourCC()??"",await this.reader.readU32BE();let r=(e-16)/4;for(let i=0;i<r;i++)await this.reader.readFourCC()}async parseMoov(s,e){await this.parseContainerBox(s+t,e-t)}async parseContainerBox(s,e){let r=[],i=s,p=s+e;while(i<p){this.reader.position=i;let h=await this.reader.readU32BE();if(h===null)break;let f=await this.reader.readFourCC();if(!f)break;let B=h,v=t;if(B===1){let b=await this.reader.readU64BE();if(b===null)break;B=Number(b),v=c}else if(B===0)B=p-i;let k={type:f,size:B,offset:i};if(d.has(f))k.children=await this.parseContainerBox(i+v,B-v);r.push(k),i+=B}return r}async buildTracks(){if(!this.moovBox)throw Error("No moov box found");let s=await this.parseContainerBox(this.moovBox.offset+t,this.moovBox.size-t),e=s.find((i)=>i.type==="mvhd");if(e)await this.parseMvhd(e);let r=s.filter((i)=>i.type==="trak");this._tracks=[];for(let i=0;i<r.length;i++){let p=await this.parseTrack(r[i],i);if(p)this._tracks.push(p)}this._metadata=await this.parseMetadata(s),this._duration=Number(this.movieDuration)/this.movieTimescale}async parseMvhd(s){this.reader.position=s.offset+t;let e=await this.reader.readU8();if(e===null)return;if(await this.reader.skip(3),e===1)await this.reader.skip(16),this.movieTimescale=await this.reader.readU32BE()??1000,this.movieDuration=await this.reader.readU64BE()??0n;else await this.reader.skip(8),this.movieTimescale=await this.reader.readU32BE()??1000,this.movieDuration=BigInt(await this.reader.readU32BE()??0)}async parseTrack(s,e){let r=s.children??[],i=r.find((m)=>m.type==="tkhd"),p=r.find((m)=>m.type==="mdia");if(!i||!p)return null;let h=await this.parseTkhd(i),f=p.children??[],B=f.find((m)=>m.type==="mdhd"),v=f.find((m)=>m.type==="hdlr"),k=f.find((m)=>m.type==="minf");if(!B||!v||!k)return null;let b=await this.parseMdhd(B),y=await this.parseHdlr(v),U=(k.children??[]).find((m)=>m.type==="stbl");if(!U)return null;let x=(U.children??[]).find((m)=>m.type==="stsd");if(!x)return null;let g=await this.parseStsd(x,y.handlerType),S={id:h.trackId,index:e,type:n(y.handlerType)?"video":o(y.handlerType)?"audio":"subtitle",timescale:b.timescale,duration:b.duration,samples:[],currentSampleIndex:0};if(this.trackInfos.push(S),n(y.handlerType)&&g.entries.length>0){let m=g.entries[0];return{type:"video",id:h.trackId,index:e,codec:this.getVideoCodec(m.type),width:m.width,height:m.height,profile:this.getVideoProfile(m.type),frameRate:this.calculateFrameRate(S),isDefault:(h.flags&1)!==0,language:b.language,codecDescription:this.extractCodecDescription(m),colorSpace:this.extractColorSpace(m),rotation:this.getRotation(h.matrix)}}if(o(y.handlerType)&&g.entries.length>0){let m=g.entries[0];return{type:"audio",id:h.trackId,index:e,codec:this.getAudioCodec(m.type),sampleRate:m.sampleRate,channels:m.channelCount,bitsPerSample:m.sampleSize,isDefault:(h.flags&1)!==0,language:b.language,codecDescription:this.extractCodecDescription(m)}}return null}async parseTkhd(s){this.reader.position=s.offset+t;let e=await this.reader.readU8(),r=(await this.reader.readU8()??0)<<16|(await this.reader.readU8()??0)<<8|(await this.reader.readU8()??0),i=0;if(e===1)await this.reader.skip(16),i=await this.reader.readU32BE()??0,await this.reader.skip(12);else await this.reader.skip(8),i=await this.reader.readU32BE()??0,await this.reader.skip(8);await this.reader.skip(8);let p=[];for(let B=0;B<9;B++)p.push(await this.reader.readI32BE()??0);let h=(await this.reader.readU32BE()??0)/65536,f=(await this.reader.readU32BE()??0)/65536;return{trackId:i,flags:r,width:h,height:f,matrix:p}}async parseMdhd(s){this.reader.position=s.offset+t;let e=await this.reader.readU8();await this.reader.skip(3);let r=1000,i=0n;if(e===1)await this.reader.skip(16),r=await this.reader.readU32BE()??1000,i=await this.reader.readU64BE()??0n;else await this.reader.skip(8),r=await this.reader.readU32BE()??1000,i=BigInt(await this.reader.readU32BE()??0);let p=await this.reader.readU16BE()??0,h=l(p);return{timescale:r,duration:i,language:h}}async parseHdlr(s){this.reader.position=s.offset+t,await this.reader.skip(8);let e=await this.reader.readFourCC()??"";await this.reader.skip(12);let r=s.size-t-24,i=r>0?await this.reader.readString(r)??"":"";return{handlerType:e,name:i.replace(/\0/g,"")}}async parseStsd(s,e){this.reader.position=s.offset+t,await this.reader.skip(4);let r=await this.reader.readU32BE()??0,i=[];for(let p=0;p<r;p++){let h=await this.reader.readU32BE()??0,f=await this.reader.readFourCC()??"",B=this.reader.position;await this.reader.skip(6);let v=await this.reader.readU16BE()??0;if(n(e)){await this.reader.skip(16);let k=await this.reader.readU16BE()??0,b=await this.reader.readU16BE()??0,y=await this.reader.readU32BE()??0,E=await this.reader.readU32BE()??0;await this.reader.skip(4);let U=await this.reader.readU16BE()??1,C=await this.reader.readU8()??0,x=await this.reader.readString(31)??"",g=await this.reader.readU16BE()??24;await this.reader.skip(2);let S=await this.parseExtensions(B+h-8);i.push({type:f,dataReferenceIndex:v,width:k,height:b,horizResolution:y,vertResolution:E,frameCount:U,compressorName:x.slice(0,C),depth:g,extensions:S,data:new Uint8Array(0)})}else if(o(e)){await this.reader.skip(8);let k=await this.reader.readU16BE()??2,b=await this.reader.readU16BE()??16;await this.reader.skip(4);let y=(await this.reader.readU32BE()??-1404829696)>>16,E=await this.parseExtensions(B+h-8);i.push({type:f,dataReferenceIndex:v,channelCount:k,sampleSize:b,sampleRate:y,extensions:E,data:new Uint8Array(0)})}this.reader.position=B+h-8}return{entries:i}}async parseExtensions(s){let e=[];while(this.reader.position<s){let r=await this.reader.readU32BE()??0;if(r<8)break;let i=await this.reader.readFourCC()??"",p=await this.reader.readBytes(r-8);e.push({type:i,size:r,offset:this.reader.position-r,data:p??void 0})}return e}getVideoCodec(s){switch(s){case"avc1":case"avc3":return"h264";case"hev1":case"hvc1":return"h265";case"vp08":return"vp8";case"vp09":return"vp9";case"av01":return"av1";case"mp4v":return"mpeg4";case"apco":case"apcs":case"apcn":case"apch":case"ap4h":case"ap4x":return"prores";default:return"unknown"}}getVideoProfile(s){switch(s){case"apco":return"proxy";case"apcs":return"lt";case"apcn":return"422";case"apch":return"hq";case"ap4h":return"4444";case"ap4x":return"4444-xq";default:return}}extractColorSpace(s){let e=s.extensions.find((f)=>f.type==="colr")?.data;if(!e||e.length<11)return;let r=new DataView(e.buffer,e.byteOffset,e.byteLength);if(u(r,0)!=="nclx")return;let i=new Map([[1,"bt709"],[4,"bt470m"],[5,"bt470bg"],[6,"smpte170m"],[7,"smpte240m"],[8,"film"],[9,"bt2020"],[10,"smpte428"],[11,"smpte431"],[12,"smpte432"],[22,"ebu3213"]]).get(r.getUint16(4)),p=new Map([[1,"bt709"],[4,"bt470m"],[5,"bt470bg"],[6,"smpte170m"],[7,"smpte240m"],[8,"linear"],[9,"log100"],[10,"log316"],[11,"iec61966-2-4"],[12,"bt1361e"],[13,"iec61966-2-1"],[14,"bt2020-10"],[15,"bt2020-12"],[16,"smpte2084"],[17,"smpte428"],[18,"arib-std-b67"]]).get(r.getUint16(6)),h=new Map([[0,"rgb"],[1,"bt709"],[4,"fcc"],[5,"bt470bg"],[6,"smpte170m"],[7,"smpte240m"],[8,"ycocg"],[9,"bt2020nc"],[10,"bt2020c"],[11,"smpte2085"],[12,"chroma-derived-nc"],[13,"chroma-derived-c"],[14,"ictcp"]]).get(r.getUint16(8));if(!i&&!p&&!h)return;return{primaries:i,transfer:p,matrix:h,range:(e[10]&128)!==0?"full":"limited"}}getAudioCodec(s){switch(s){case"mp4a":return"aac";case".mp3":case"mp3 ":return"mp3";case"Opus":return"opus";case"fLaC":return"flac";case"alac":return"alac";case"ac-3":return"ac3";case"ec-3":return"eac3";default:return"unknown"}}extractCodecDescription(s){for(let e of s.extensions)if(e.type==="avcC"||e.type==="hvcC"||e.type==="av1C"||e.type==="esds")return e.data;return}getRotation(s){let e=s[0]/65536,r=s[1]/65536,i=s[3]/65536,p=s[4]/65536;if(Math.abs(e-1)<0.01&&Math.abs(p-1)<0.01)return 0;if(Math.abs(r-1)<0.01&&Math.abs(i+1)<0.01)return 90;if(Math.abs(e+1)<0.01&&Math.abs(p+1)<0.01)return 180;if(Math.abs(r+1)<0.01&&Math.abs(i-1)<0.01)return 270;return 0}calculateFrameRate(s){return}async parseMetadata(s){return{}}async buildSampleTables(){if(!this.moovBox)return;let e=(await this.parseContainerBox(this.moovBox.offset+t,this.moovBox.size-t)).filter((r)=>r.type==="trak");for(let r=0;r<e.length&&r<this.trackInfos.length;r++)await this.buildSampleTable(e[r],this.trackInfos[r])}async buildSampleTable(s,e){let i=(s.children??[]).find((w)=>w.type==="mdia");if(!i?.children)return;let p=i.children.find((w)=>w.type==="minf");if(!p?.children)return;let h=p.children.find((w)=>w.type==="stbl");if(!h?.children)return;let f=h.children,B=f.find((w)=>w.type==="stts"),v=f.find((w)=>w.type==="ctts"),k=f.find((w)=>w.type==="stsc"),b=f.find((w)=>w.type==="stsz"),y=f.find((w)=>w.type==="stco"),E=f.find((w)=>w.type==="co64"),U=f.find((w)=>w.type==="stss");if(!B||!k||!b||!y&&!E)return;let C=await this.parseStts(B),x=v?await this.parseCtts(v):null,g=await this.parseStsc(k),S=await this.parseStsz(b),m=y?await this.parseStco(y):await this.parseCo64(E),I=U?new Set(await this.parseStss(U)):null,z=0,A=0,P=0,D=C.length>0?C[0].sampleCount:0,T=0,M=x&&x.length>0?x[0].sampleCount:0;for(let w=0;w<m.length;w++){let V=m[w],F=this.getSamplesPerChunk(g,w+1),_=0;for(let O=0;O<F;O++){let N=S.sampleSize>0?S.sampleSize:S.entrySizes[z]??0,R=C[P]?.sampleDelta??0,H=x&&x[T]?x[T].sampleOffset:0,L=I===null||I.has(z+1);if(e.samples.push({offset:V+_,size:N,timestamp:A/e.timescale,duration:R/e.timescale,isKeyframe:L,compositionTimeOffset:H/e.timescale}),_+=N,A+=R,z++,D--,D===0&&P<C.length-1)P++,D=C[P].sampleCount;if(x){if(M--,M===0&&T<x.length-1)T++,M=x[T].sampleCount}}}}async parseStts(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,r=[];for(let i=0;i<e;i++){let p=await this.reader.readU32BE()??0,h=await this.reader.readU32BE()??0;r.push({sampleCount:p,sampleDelta:h})}return r}async parseCtts(s){this.reader.position=s.offset+t;let e=await this.reader.readU8()??0;await this.reader.skip(3);let r=await this.reader.readU32BE()??0,i=[];for(let p=0;p<r;p++){let h=await this.reader.readU32BE()??0,f=e===1?await this.reader.readI32BE()??0:await this.reader.readU32BE()??0;i.push({sampleCount:h,sampleOffset:f})}return i}async parseStsc(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,r=[];for(let i=0;i<e;i++){let p=await this.reader.readU32BE()??0,h=await this.reader.readU32BE()??0,f=await this.reader.readU32BE()??0;r.push({firstChunk:p,samplesPerChunk:h,sampleDescriptionIndex:f})}return r}async parseStsz(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,r=await this.reader.readU32BE()??0,i=[];if(e===0)for(let p=0;p<r;p++)i.push(await this.reader.readU32BE()??0);return{sampleSize:e,sampleCount:r,entrySizes:i}}async parseStco(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,r=[];for(let i=0;i<e;i++)r.push(await this.reader.readU32BE()??0);return r}async parseCo64(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,r=[];for(let i=0;i<e;i++)r.push(Number(await this.reader.readU64BE()??0n));return r}async parseStss(s){this.reader.position=s.offset+t,await this.reader.skip(4);let e=await this.reader.readU32BE()??0,r=[];for(let i=0;i<e;i++)r.push(await this.reader.readU32BE()??0);return r}getSamplesPerChunk(s,e){for(let r=s.length-1;r>=0;r--)if(e>=s[r].firstChunk)return s[r].samplesPerChunk;return 1}async readPacket(s){let e=this.trackInfos.find((p)=>p.id===s);if(!e||e.currentSampleIndex>=e.samples.length)return null;let r=e.samples[e.currentSampleIndex];e.currentSampleIndex++,this.reader.position=r.offset;let i=await this.reader.readBytes(r.size);if(!i)return null;return{data:i,timestamp:r.timestamp,duration:r.duration,isKeyframe:r.isKeyframe,trackId:s,compositionTimeOffset:r.compositionTimeOffset}}async seek(s){for(let e of this.trackInfos){let r=0;for(let i=0;i<e.samples.length;i++){if(e.samples[i].timestamp>s)break;if(e.samples[i].isKeyframe)r=i}e.currentSampleIndex=r}}}
|
|
2
|
-
export{a};
|