12 lines
13 KiB
JavaScript
12 lines
13 KiB
JavaScript
var C=Object.defineProperty;var a=(s,e)=>C(s,"name",{value:e,configurable:!0}),y=(s=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(s,{get:(e,t)=>(typeof require!="undefined"?require:e)[t]}):s)(function(s){if(typeof require!="undefined")return require.apply(this,arguments);throw new Error('Dynamic require of "'+s+'" is not supported')});import{Duplex as L}from"stream";var _=class{constructor(e){let t=y("play-opus");this.options=e,this.encoder=new t.OpusHandler(e.rate,e.channels)}encode(e){return this.encoder.encode(e)}decode(e){return this.encoder.decode(e)}encode_ctl(e,t){this.encode_ctl(e,t)}decode_ctl(e,t){this.decode_ctl(e,t)}delete(){this.encoder.delete()}};a(_,"OpusHandler");var E=class{constructor(e){let t=y("opusscript");this.options=e,this.encoder=new t(e.rate,e.channels)}encode(e){return this.encoder.encode(e,this.options.frameSize)}decode(e){return this.encoder.decode(e)}encode_ctl(e,t){this.encode_ctl(e,t)}decode_ctl(e,t){this.decode_ctl(e,t)}delete(){this.encoder.delete()}};a(E,"OpusHandler");var P={"play-opus":s=>new _(s),opusscript:s=>new E(s)};function D(s,e){if(e){if(e!=="opusscript"&&e!=="play-opus")return new Error(`Supported modules for play-media :
|
|
- play-opus
|
|
- opusscript`);try{y(e)}catch{return new Error(`Module [${e}] Not Found.`)}return P[e](s)}else{for(let[t,r]of Object.entries(P))try{return y(t),r(s)}catch{continue}return new Error(`No modules found. Try to install one of following :
|
|
- play-opus
|
|
- opusscript`)}}a(D,"createOpusHandler");var h=class extends L{constructor(e){super(e.duplex);let t=D(e,e.encoder);if(t instanceof Error)throw t;this.encoder=t,this.pcm_length=e.frameSize*e.channels*2}encode(e){return this.encoder.encode(e)}decode(e){return this.encoder.decode(e)}cleanup(){this.encoder.delete()}_read(){}_destroy(e,t){this.cleanup(),t(e)}_final(e){this.cleanup(),this.push(null),e()}bitrate(e){return this.applyCTL(4002,e)}setFEC(e){return this.applyCTL(4012,e?1:0)}setPLP(e){return this.applyCTL(4014,Math.min(100,Math.max(0,e*100)))}};a(h,"OpusDuplexStream");var k=class extends h{constructor(e){super(e);this.remaining=Buffer.allocUnsafe(0)}_write(e,t,r){let n=Buffer.concat([this.remaining,e]),i=0;for(;n.length>=i+this.pcm_length;){let u=n.slice(i,i+this.pcm_length),p;try{p=this.encode(u)}catch(f){r(f);return}this.push(p),i+=this.pcm_length}i>0&&(this.remaining=n.slice(i)),r()}applyCTL(e,t){this.encoder.encode_ctl(e,t)}};a(k,"OpusEncoder");var M=Buffer.from("OpusHead"),q=Buffer.from("OpusTags"),x=class extends h{constructor(e){super(e)}_write(e,t,r){if(e.compare(M,0,8,0,8)===0)this.opusHead=e,this.emit("opusHead",e);else if(e.compare(q,0,8,0,8)===0)this.opusTags=e,this.emit("opusTags",e);else{let n;try{n=this.encoder.decode(e)}catch(i){r(i);return}this.push(n)}r()}applyCTL(e,t){this.encoder.decode_ctl(e,t)}};a(x,"OpusDecoder");import{Duplex as G}from"stream";var V=26,$=0,W=Buffer.from("OggS"),U=Buffer.from("OpusHead"),N=Buffer.from("OpusTags"),T=class extends G{constructor(e){super(e)}_read(){}_write(e,t,r){for(this.remaining&&(e=Buffer.concat([this.remaining,e]),this.remaining=void 0);;){let n=this.readOggPage(e);if(n)n instanceof Error?r(n):e=n;else break}this.remaining=e,this.ogg_head=void 0,r()}readOggPage(e){if(this.ogg_head)return this.readOggData(e);{let t=this.readOggHead(e);return t?t instanceof Error?t:this.readOggData(t):!1}}readOggData(e){if(!this.ogg_head||e.length<this.ogg_head.totalSize)return!1;let t=0;for(let r of this.ogg_head.sizes){let n=e.slice(t,t+r);if(this.opus_head)r>=8&&n.compare(N,0,8,0,8)===0?this.emit("tags",n):this.push(n);else if(n.compare(U,0,8,0,8)===0)this.emit("head",n),this.opus_head=n;else return new Error("Unknown Segment Found");t+=r}return this.ogg_head=void 0,e.slice(t)}readOggHead(e){if(e.length<V)return!1;if(e.compare(W,0,4,0,4)!==0)return new Error("Capture Pattern is not OggS");if(e.readUInt8(4)!==$)return new Error("OGG version is not equal to 0.");if(e.length<27)return!1;let t=e.readUInt8(26);if(e.length<27+t)return!1;let r=e.slice(27,27+t),n=[],i=0;for(let u=0;u<t;){let p=0,f=255;for(;f===255;){if(u>=r.length)return!1;f=r.readUInt8(u),u++,p+=f}n.push(p),i+=p}return this.ogg_head={sizes:n,totalSize:i,pageSegments:t},e.slice(27+t)}_destroy(e,t){this._cleanup(),t(e)}_final(e){this._cleanup(),this.push(null),e()}_cleanup(){this.remaining=void 0,this.opus_head=void 0,this.ogg_head=void 0}};a(T,"OggDemuxer");import{Duplex as j}from"stream";var o={string:s=>s.toString("utf-8"),uint:s=>parseInt(s.toString("hex"),16),float:s=>parseFloat(s.toString("utf-8"))},g={"1a45dfa3":{name:"ebml",type:0},"4286":{name:"ebmlVersion",type:2,return:o.uint},"42f7":{name:"ebmlReadVersion",type:2,return:o.uint},"42f2":{name:"ebmlMaxIDLength",type:2,return:o.uint},"42f3":{name:"ebmlMaxSizeLength",type:2,return:o.uint},"4282":{name:"docType",type:1,return:o.string},"4287":{name:"docTypeVersion",type:2,return:o.uint},"4285":{name:"docTypeReadVersion",type:2,return:o.uint},"18538067":{name:"segment",type:0},"114d9b74":{name:"seekHead",type:0},"4dbb":{name:"seek",type:0},"53ab":{name:"seekId",type:3},"53ac":{name:"seekPosition",type:2,return:o.uint},"1549a966":{name:"info",type:0},"4489":{name:"duration",type:4,return:o.float},"4d80":{name:"muxingApp",type:1,return:o.string},"5741":{name:"writingApp",type:1,return:o.string},"1f43b675":{name:"cluster",type:0},e7:{name:"clusterTimecode",type:2,return:o.uint},a3:{name:"simpleBlock",type:3},"1654ae6b":{name:"tracks",type:0},ae:{name:"trackEntry",type:0},d7:{name:"trackNumber",type:2,return:o.uint},"83":{name:"trackType",type:2,return:o.uint},"86":{name:"codecID",type:1,return:o.string},e1:{name:"audio",type:0},b5:{name:"samplingFrequency",type:4,return:o.float},"9f":{name:"channels",type:2,return:o.uint},"6264":{name:"bitDepth",type:2,return:o.uint},"1c53bb6b":{name:"cues",type:0},bb:{name:"cuePoint",type:0},b3:{name:"cueTime",type:2,return:o.uint},b7:{name:"cueTrackPositions",type:0},f7:{name:"cueTrack",type:2,return:o.uint},f1:{name:"cueClusterPosition",type:2,return:o.uint}};var d=class{constructor(){this.ebml={},this.segment={},this.audioTrack=-1}parse(e,t){switch(e.name){case"ebml":this.ebml={};break;case"ebmlVersion":e.return&&(this.ebml.version=e.return(t));break;case"ebmlReadVersion":e.return&&(this.ebml.readVersion=e.return(t));break;case"ebmlMaxIDLength":e.return&&(this.ebml.maxIDLength=e.return(t));break;case"ebmlMaxSizeLength":e.return&&(this.ebml.maxSizeWidth=e.return(t));break;case"docType":if(e.return){let r=e.return(t);if(r!=="webm")return new Error("This is not a Webm Stream. [ DocType !== webm ]");this.ebml.docType=r}break;case"docTypeVersion":e.return&&(this.ebml.docTypeVersion=e.return(t));break;case"docTypeReadVersion":e.return&&(this.ebml.docTypeReadVersion=e.return(t));break;case"segment":this.segment={};break;case"seekHead":this.segment.seekHead=[];break;case"seekPosition":e.return&&this.segment.seekHead.push({position:e.return(t)});break;case"info":this.segment.info={};break;case"duration":e.return&&(this.segment.info.duration=e.return(t));break;case"muxingApp":e.return&&(this.segment.info.muxingApp=e.return(t));break;case"writingApp":e.return&&(this.segment.info.writingApp=e.return(t));break;case"cluster":this.segment.cluster={};break;case"clusterTimecode":e.return&&(this.segment.cluster.time=e.return(t));break;case"simpleBlock":break;case"tracks":this.segment.tracks=[];break;case"trackEntry":this.segment.tracks.push({});break;case"trackNumber":e.return&&(this.segment.tracks[this.segment.tracks.length-1].trackNumber=e.return(t));break;case"trackType":if(e.return){let r=e.return(t);r===2&&(this.audioTrack=this.segment.tracks.length-1),this.segment.tracks[this.segment.tracks.length-1].trackType=r}break;case"codecID":if(e.return){let r=e.return(t);if(r!=="A_OPUS"&&this.segment.tracks[this.segment.tracks.length-1].trackType===2)return new Error("Audio Codec is not OPUS");this.segment.tracks[this.segment.tracks.length-1].codecID=r}break;case"audio":this.segment.tracks[this.segment.tracks.length-1].audio={};break;case"samplingFrequency":e.return&&(this.segment.tracks[this.segment.tracks.length-1].audio.rate=e.return(t));break;case"channels":e.return&&(this.segment.tracks[this.segment.tracks.length-1].audio.channels=e.return(t));break;case"bitDepth":e.return&&(this.segment.tracks[this.segment.tracks.length-1].audio.bitDepth=e.return(t));break;case"cues":this.segment.cues=[];break;case"cuePoint":this.segment.cues.push({});break;case"cueTime":e.return&&(this.segment.cues[this.segment.cues.length-1].time=e.return(t));break;case"cueTrack":e.return&&(this.segment.cues[this.segment.cues.length-1].track=e.return(t));break;case"cueClusterPosition":e.return&&(this.segment.cues[this.segment.cues.length-1].position=e.return(t));break;default:break}}};a(d,"WebmHeader");var O=class extends j{constructor(e){super(e);this.cursor=0,this.header=new d,this.headfound=!1,this.data_length=0,this.data_size=0}get vint_length(){let e=0;for(;e<8&&!(1<<7-e&this.chunk[this.cursor]);e++);return++e}get vint_value(){if(!this.chunk)return!1;let e=this.vint_length;if(this.chunk.length<this.cursor+e)return!1;let t=this.chunk[this.cursor]&(1<<8-e)-1;for(let r=this.cursor+1;r<this.cursor+e;r++)t=(t<<8)+this.chunk[r];return this.data_size=e,this.data_length=t,!0}cleanup(){this.cursor=0,this.chunk=void 0,this.remaining=void 0}_read(){}_write(e,t,r){this.remaining?(this.chunk=Buffer.concat([this.remaining,e]),this.remaining=void 0):this.chunk=e;let n=this.readTag();n instanceof Error?r(n):r()}readTag(){if(!this.chunk)return new Error("Chunk is missing");for(;this.chunk.length>this.cursor;){let e=this.cursor,t=this.vint_length;if(this.chunk.length<this.cursor+t)break;let r=this.parseEbmlID(this.chunk.slice(this.cursor,this.cursor+t).toString("hex"));if(this.cursor+=t,!this.vint_value){this.cursor=e;break}if(!r){this.cursor+=this.data_size+this.data_length;continue}if(!this.headfound)if(r.name==="ebml")this.headfound=!0;else return new Error("Failed to find EBML ID at start of stream.");let i=this.chunk.slice(this.cursor+this.data_size,this.cursor+this.data_size+this.data_length),u=this.header.parse(r,i);if(u instanceof Error)return u;if(r.type===0){this.cursor+=this.data_size;continue}if(this.chunk.length<this.cursor+this.data_size+this.data_length){this.cursor=e;break}else this.cursor+=this.data_size+this.data_length;if(r.name==="simpleBlock"){let p=this.header.segment.tracks[this.header.audioTrack];if(!p||p.trackType!==2)return new Error("No audio Track in this webm file.");(i[0]&15)===p.trackNumber&&this.push(i.slice(4))}}this.remaining=this.chunk.slice(this.cursor),this.cursor=0}parseEbmlID(e){return Object.keys(g).includes(e)?g[e]:!1}_destroy(e,t){this.cleanup(),t(e)}_final(e){this.cleanup(),this.push(null),e()}};a(O,"WebmDemuxer");import{request as Y}from"node:https";import{URL as J}from"node:url";import{createGunzip as Z}from"node:zlib";import w from"node:os";import l from"fs";import{resolve as v}from"node:path";import{spawn as z}from"node:child_process";import{Readable as K}from"node:stream";import b from"node:process";var S;async function F(s={}){let e=w.platform(),t=s.args||[];if(t.unshift("-i",typeof s.input=="string"?s.input:"-"),!S)throw new Error("You forgot to initialize FFMPEG. Try using await initializeFFmpeg() before using this function.");let r=S;S==="npx"&&t.unshift(`ffmpeg${e==="win32"?".exe":""}`),t.push("pipe:1");let n=z(r,t,{shell:!0,windowsHide:!0});return s.input instanceof K&&s.input.pipe(n.stdin),n.stdout}a(F,"ffmpeg");async function R(s={}){s.path||(s.path="./node_modules/.bin/"),l.existsSync(s.path)&&l.mkdirSync(s.path,{recursive:!0});let e=w.arch(),t=w.platform(),r=v(s.path,`ffmpeg${t==="win32"?".exe":""}`);if(l.existsSync(r)&&!s.force){b.stdout.write(`You already have ffmpeg installed at ${r}
|
|
`);return}let n=await H("https://github.com/play-dl/play-audio/releases/latest"),i=n.headers.location?.split("tag/")[1];if(n=await H(`https://github.com/play-dl/play-audio/releases/download/${i}/ffmpeg-${t}-${e}.${t==="win32"?"exe.gz":"gz"}`),n.statusCode>400){b.stdout.write(`We don't currently support your platform and architecture.
|
|
If you want support for ffmpeg static build, create a issue at play-audio repository with following logs.
|
|
Platform : ${t}
|
|
Arch : ${e}
|
|
`);return}else n.statusCode>300&&(n=await H(n.headers.location));let u=l.createWriteStream(r);if(n.pipe(Z()).pipe(u),s.debug===!0){let p=parseInt(n.headers["content-length"])-1,f=0;n.on("data",A=>{f+=A.length,b.stdout.clearLine(0),b.stdout.cursorTo(0),b.stdout.write(`FFmpeg Download status : ${(f/p*100).toFixed(0)} %`)})}u.once("finish",()=>{l.chmodSync(r,"755"),b.stdout.write(`
|
|
`)})}a(R,"ffmpeg_download");function H(s,e={}){return new Promise((t,r)=>{let n=new J(s);e.method??="GET";let i={host:n.hostname,path:n.pathname+n.search,headers:e.headers??{},method:e.method},u=Y(i,t);u.on("error",p=>{r(p)}),e.method==="POST"&&u.write(e.body),u.end()})}a(H,"https_getter");async function B(s){let t=`ffmpeg${w.platform()==="win32"?".exe":""}`,r=[];switch(s){case"npx":r=[t],t="npx";break;case"local":let i=[v(`./node_modules/.bin/${t}`),v(`../node_modules/.bin/${t}`)];if(l.existsSync(i[0]))t=i[0];else if(l.existsSync(i[1]))t=i[1];else return!1;break;case void 0:return await B("local")===!1?B("global"):!0}r.push("-h");let n=z(t,r,{shell:!0,windowsHide:!0});return new Promise(i=>{n.once("close",u=>{u===0?(S=t,i(!0)):i(!1)})})}a(B,"initializeFFmpeg");var Ze={OpusDecoder:x,OggDemuxer:T,OpusEncoder:k,WebmDemuxer:O,WebmHeader:d,WebmElements:g,ffmpeg_download:R,ffmpeg:F,initializeFFmpeg:B};export{T as OggDemuxer,x as OpusDecoder,k as OpusEncoder,O as WebmDemuxer,g as WebmElements,d as WebmHeader,Ze as default,F as ffmpeg,R as ffmpeg_download,B as initializeFFmpeg};
|
|
//# sourceMappingURL=index.mjs.map
|