UNPKG

@stacksjs/audiox

Version:

Powerful audio processing for your media workflows.

11 lines (9 loc) 12.6 kB
import{a as _,f as D,h as b}from"../chunk-jbgndnxf.js";import V from"node:process";import{EventEmitter as M}from"events";function x(e){return e==null?[]:Array.isArray(e)?e:[e]}function T(e,i,n,t){var s,a=e[i],l=~t.string.indexOf(i)?n==null||n===!0?"":String(n):typeof n==="boolean"?n:~t.boolean.indexOf(i)?n==="false"?!1:n==="true"||(e._.push((s=+n,s*0===0)?s:n),!!n):(s=+n,s*0===0)?s:n;e[i]=a==null?l:Array.isArray(a)?a.concat(l):[a,l]}function q(e,i){e=e||[],i=i||{};var n,t,s,a,l,c={_:[]},o=0,r=0,h=0,u=e.length;let m=i.alias!==void 0,p=i.unknown!==void 0,O=i.default!==void 0;if(i.alias=i.alias||{},i.string=x(i.string),i.boolean=x(i.boolean),m)for(n in i.alias){t=i.alias[n]=x(i.alias[n]);for(o=0;o<t.length;o++)(i.alias[t[o]]=t.concat(n)).splice(o,1)}for(o=i.boolean.length;o-- >0;){t=i.alias[i.boolean[o]]||[];for(r=t.length;r-- >0;)i.boolean.push(t[r])}for(o=i.string.length;o-- >0;){t=i.alias[i.string[o]]||[];for(r=t.length;r-- >0;)i.string.push(t[r])}if(O){for(n in i.default)if(a=typeof i.default[n],t=i.alias[n]=i.alias[n]||[],i[a]!==void 0){i[a].push(n);for(o=0;o<t.length;o++)i[a].push(t[o])}}let $=p?Object.keys(i.alias):[];for(o=0;o<u;o++){if(s=e[o],s==="--"){c._=c._.concat(e.slice(++o));break}for(r=0;r<s.length;r++)if(s.charCodeAt(r)!==45)break;if(r===0)c._.push(s);else if(s.substring(r,r+3)==="no-"){if(a=s.substring(r+3),p&&!~$.indexOf(a))return i.unknown(s);c[a]=!1}else{for(h=r+1;h<s.length;h++)if(s.charCodeAt(h)===61)break;a=s.substring(r,h),l=s.substring(++h)||(o+1===u||(""+e[o+1]).charCodeAt(0)===45||e[++o]),t=r===2?[a]:a;for(h=0;h<t.length;h++){if(a=t[h],p&&!~$.indexOf(a))return i.unknown("-".repeat(r)+a);T(c,a,h+1<t.length||l,i)}}}if(O){for(n in i.default)if(c[n]===void 0)c[n]=i.default[n]}if(m)for(n in c){t=i.alias[n]||[];while(t.length>0)c[t.shift()]=c[n]}return c}var j=(e)=>e.replace(/[<[].+/,"").trim(),B=(e)=>{let i=/<([^>]+)>/g,n=/\[([^\]]+)\]/g,t=[],s=(c)=>{let o=!1,r=c[1];if(r.startsWith("..."))r=r.slice(3),o=!0;return{required:c[0].startsWith("<"),value:r,variadic:o}},a;while(a=i.exec(e))t.push(s(a));let l;while(l=n.exec(e))t.push(s(l));return t},R=(e)=>{let i={alias:{},boolean:[]};for(let[n,t]of e.entries()){if(t.names.length>1)i.alias[t.names[0]]=t.names.slice(1);if(t.isBoolean)if(t.negated){if(!e.some((a,l)=>{return l!==n&&a.names.some((c)=>t.names.includes(c))&&typeof a.required==="boolean"}))i.boolean.push(t.names[0])}else i.boolean.push(t.names[0])}return i},v=(e)=>{return e.sort((i,n)=>{return i.length>n.length?-1:1})[0]},A=(e,i)=>{return e.length>=i?e:`${e}${" ".repeat(i-e.length)}`},U=(e)=>{return e.replace(/([a-z])-([a-z])/g,(i,n,t)=>{return n+t.toUpperCase()})},G=(e,i,n)=>{let t=0,s=i.length,a=e,l;for(;t<s;++t)l=a[i[t]],a=a[i[t]]=t===s-1?n:l!=null?l:!!~i[t+1].indexOf(".")||!(+i[t+1]>-1)?{}:[]},H=(e,i)=>{for(let n of Object.keys(i)){let t=i[n];if(t.shouldTransform){if(e[n]=Array.prototype.concat.call([],e[n]),typeof t.transformFunction==="function")e[n]=e[n].map(t.transformFunction)}}},P=(e)=>{let i=/([^\\\/]+)$/.exec(e);return i?i[1]:""},E=(e)=>{return e.split(".").map((i,n)=>{return n===0?U(i):i}).join(".")};class g extends Error{constructor(e){super(e);if(this.name=this.constructor.name,typeof Error.captureStackTrace==="function")Error.captureStackTrace(this,this.constructor);else this.stack=new Error(e).stack}}class N{constructor(e,i,n){if(this.rawName=e,this.description=i,this.config=Object.assign({},n),e=e.replace(/\.\*/g,""),this.negated=!1,this.names=j(e).split(",").map((t)=>{let s=t.trim().replace(/^-{1,2}/,"");if(s.startsWith("no-"))this.negated=!0,s=s.replace(/^no-/,"");return E(s)}).sort((t,s)=>t.length>s.length?1:-1),this.name=this.names[this.names.length-1],this.negated&&this.config.default==null)this.config.default=!0;if(e.includes("<"))this.required=!0;else if(e.includes("["))this.required=!1;else this.isBoolean=!0}}var I=process.argv,S=`${process.platform}-${process.arch} node-${process.version}`;class w{constructor(e,i,n={},t){this.rawName=e,this.description=i,this.config=n,this.cli=t,this.options=[],this.aliasNames=[],this.name=j(e),this.args=B(e),this.examples=[]}usage(e){return this.usageText=e,this}allowUnknownOptions(){return this.config.allowUnknownOptions=!0,this}ignoreOptionDefaultValue(){return this.config.ignoreOptionDefaultValue=!0,this}version(e,i="-v, --version"){return this.versionNumber=e,this.option(i,"Display version number"),this}example(e){return this.examples.push(e),this}option(e,i,n){let t=new N(e,i,n);return this.options.push(t),this}alias(e){return this.aliasNames.push(e),this}action(e){return this.commandAction=e,this}isMatched(e){return this.name===e||this.aliasNames.includes(e)}get isDefaultCommand(){return this.name===""||this.aliasNames.includes("!")}get isGlobalCommand(){return this instanceof y}hasOption(e){return e=e.split(".")[0],this.options.find((i)=>{return i.names.includes(e)})}outputHelp(){let{name:e,commands:i}=this.cli,{versionNumber:n,options:t,helpCallback:s}=this.cli.globalCommand,a=[{body:`${e}${n?`/${n}`:""}`}];if(a.push({title:"Usage",body:` $ ${e} ${this.usageText||this.rawName}`}),(this.isGlobalCommand||this.isDefaultCommand)&&i.length>0){let o=v(i.map((r)=>r.rawName));a.push({title:"Commands",body:i.map((r)=>{return` ${A(r.rawName,o.length)} ${r.description}`}).join(` `)}),a.push({title:"For more info, run any command with the `--help` flag",body:i.map((r)=>` $ ${e}${r.name===""?"":` ${r.name}`} --help`).join(` `)})}let c=this.isGlobalCommand?t:[...this.options,...t||[]];if(!this.isGlobalCommand&&!this.isDefaultCommand)c=c.filter((o)=>o.name!=="version");if(c.length>0){let o=v(c.map((r)=>r.rawName));a.push({title:"Options",body:c.map((r)=>{return` ${A(r.rawName,o.length)} ${r.description} ${r.config.default===void 0?"":`(default: ${r.config.default})`}`}).join(` `)})}if(this.examples.length>0)a.push({title:"Examples",body:this.examples.map((o)=>{if(typeof o==="function")return o(e);return o}).join(` `)});if(s)a=s(a)||a;console.log(a.map((o)=>{return o.title?`${o.title}: ${o.body}`:o.body}).join(` `))}outputVersion(){let{name:e}=this.cli,{versionNumber:i}=this.cli.globalCommand;if(i)console.log(`${e}/${i} ${S}`)}checkRequiredArgs(){let e=this.args.filter((i)=>i.required).length;if(this.cli.args.length<e)throw new g(`missing required args for command \`${this.rawName}\``)}checkUnknownOptions(){let{options:e,globalCommand:i}=this.cli;if(!this.config.allowUnknownOptions){for(let n of Object.keys(e))if(n!=="--"&&!this.hasOption(n)&&!i.hasOption(n))throw new g(`Unknown option \`${n.length>1?`--${n}`:`-${n}`}\``)}}checkOptionValue(){let{options:e,globalCommand:i}=this.cli,n=[...i.options,...this.options];for(let t of n){let s=e[t.name.split(".")[0]];if(t.required){let a=n.some((l)=>l.negated&&l.names.includes(t.name));if(s===!0||s===!1&&!a)throw new g(`option \`${t.rawName}\` value is missing`)}}}}class y extends w{constructor(e){super("@@global@@","",{},e)}}var f=Object.assign;class k extends M{constructor(e=""){super();this.name=e,this.commands=[],this.rawArgs=[],this.args=[],this.options={},this.globalCommand=new y(this),this.globalCommand.usage("<command> [options]")}usage(e){return this.globalCommand.usage(e),this}command(e,i,n){let t=new w(e,i||"",n,this);return t.globalCommand=this.globalCommand,this.commands.push(t),t}option(e,i,n){return this.globalCommand.option(e,i,n),this}help(e){return this.globalCommand.option("-h, --help","Display this message"),this.globalCommand.helpCallback=e,this.showHelpOnExit=!0,this}version(e,i="-v, --version"){return this.globalCommand.version(e,i),this.showVersionOnExit=!0,this}example(e){return this.globalCommand.example(e),this}outputHelp(){if(this.matchedCommand)this.matchedCommand.outputHelp();else this.globalCommand.outputHelp()}outputVersion(){this.globalCommand.outputVersion()}setParsedInfo({args:e,options:i},n,t){if(this.args=e,this.options=i,n)this.matchedCommand=n;if(t)this.matchedCommandName=t;return this}unsetMatchedCommand(){this.matchedCommand=void 0,this.matchedCommandName=void 0}parse(e=I,{run:i=!0}={}){if(this.rawArgs=e,!this.name)this.name=e[1]?P(e[1]):"cli";let n=!0;for(let s of this.commands){let a=this.mri(e.slice(2),s),l=a.args[0];if(s.isMatched(l)){n=!1;let c=f(f({},a),{args:a.args.slice(1)});this.setParsedInfo(c,s,l),this.emit(`command:${l}`,s)}}if(n){for(let s of this.commands)if(s.name===""){n=!1;let a=this.mri(e.slice(2),s);this.setParsedInfo(a,s),this.emit("command:!",s)}}if(n){let s=this.mri(e.slice(2));this.setParsedInfo(s)}if(this.options.help&&this.showHelpOnExit)this.outputHelp(),i=!1,this.unsetMatchedCommand();if(this.options.version&&this.showVersionOnExit&&this.matchedCommandName==null)this.outputVersion(),i=!1,this.unsetMatchedCommand();let t={args:this.args,options:this.options};if(i)this.runMatchedCommand();if(!this.matchedCommand&&this.args[0])this.emit("command:*");return t}mri(e,i){let n=[...this.globalCommand.options,...i?i.options:[]],t=R(n),s=[],a=e.indexOf("--");if(a>-1)s=e.slice(a+1),e=e.slice(0,a);let l=q(e,t);l=Object.keys(l).reduce((u,m)=>{return f(f({},u),{[E(m)]:l[m]})},{_:[]});let c=l._,o={"--":s},r=i&&i.config.ignoreOptionDefaultValue?i.config.ignoreOptionDefaultValue:this.globalCommand.config.ignoreOptionDefaultValue,h=Object.create(null);for(let u of n){if(!r&&u.config.default!==void 0)for(let m of u.names)o[m]=u.config.default;if(Array.isArray(u.config.type)){if(h[u.name]===void 0)h[u.name]=Object.create(null),h[u.name].shouldTransform=!0,h[u.name].transformFunction=u.config.type[0]}}for(let u of Object.keys(l))if(u!=="_"){let m=u.split(".");G(o,m,l[u]),H(o,h)}return{args:c,options:o}}runMatchedCommand(){let{args:e,options:i,matchedCommand:n}=this;if(!n||!n.commandAction)return;n.checkUnknownOptions(),n.checkOptionValue(),n.checkRequiredArgs();let t=[];return n.args.forEach((s,a)=>{if(s.variadic)t.push(e.slice(a));else t.push(e[a])}),t.push(i),n.commandAction.apply(this,t)}}var C="0.1.1";var d=new k("audiox");d.command("convert <input> [output]","Convert audio file to different format/settings").option("--codec [codec]","Audio codec (aac, mp3, pcm_s16le)").option("--bitrate [bitrate]","Audio bitrate (e.g., 192k)").option("--channels [channels]","Number of audio channels (1, 2, 5.1, 7.1)").option("--sample-rate [rate]","Sample rate (8000, 16000, 44100, 48000)").option("--quality [quality]","Audio quality (0-9)").option("--metadata [metadata]","Audio metadata in format key=value,key2=value2").option("--verbose","Enable verbose logging").example("audiox convert input.mp3 output.wav --codec pcm_s16le --channels 1 --sample-rate 16000").example("audiox convert podcast.mp3 podcast.aac --codec aac --bitrate 128k").example("audiox convert song.wav song.mp3 --metadata title=MySong,artist=Me").action(async(e,i,n)=>{try{let t={verbose:n.verbose};if(n.codec)t.codec=n.codec;if(n.bitrate)t.bitrate=n.bitrate;if(n.channels)t.channels=Number(n.channels);if(n.sampleRate)t.sampleRate=Number(n.sampleRate);if(n.quality)t.quality=Number(n.quality);if(n.metadata){t.metadata={};let s=n.metadata.split(",");for(let a of s){let[l,c]=a.split("=");if(l&&c)t.metadata[l]=c}}if(!i){let s=n.codec==="aac"?"aac":n.codec==="pcm_s16le"?"wav":n.codec==="mp3"?"mp3":"wav";i=e.replace(/\.[^/.]+$/,`.${s}`)}b("cli",`Converting ${e} to ${i}`,n.verbose),await _(e,i,t),b("cli","Conversion complete",n.verbose)}catch(t){console.error("Error:",t.message),V.exit(1)}});d.command("info <input>","Get audio file information").option("--metadata [tags]","Comma-separated metadata tags to retrieve").option("--verbose","Enable verbose logging").example("audiox info music.mp3").example("audiox info podcast.mp3 --metadata title,artist,album").action(async(e,i)=>{try{let n=i.metadata?{metadataTags:i.metadata.split(",")}:void 0;b("cli",`Getting info for ${e}`,i.verbose);let t=await D(e,n);console.log(` Audio Information:`),console.log("----------------");for(let s of t){if(console.log(`Codec: ${s.codec}`),console.log(`Channels: ${s.channels}`),console.log(`Sample Rate: ${s.sampleRate} Hz`),console.log(`Bitrate: ${Math.round(Number.parseInt(s.bitrate)/1000)}k`),console.log(`Duration: ${Math.round(Number.parseFloat(s.duration)*100)/100}s`),s.metadata&&Object.keys(s.metadata).length>0){console.log(` Metadata:`);for(let[a,l]of Object.entries(s.metadata))console.log(`${a}: ${l}`)}console.log("----------------")}}catch(n){console.error("Error:",n.message),V.exit(1)}});d.command("completion","Generate shell completion script").action(()=>{console.log("Shell completion not implemented yet")});d.command("version","Show the version of the Reverse Proxy CLI").action(()=>{console.log(C)});d.version(C);d.help();d.parse();