Skip to content

Commit

Permalink
ffmpeg feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtianjin829 committed Aug 27, 2023
1 parent d299ddc commit f530baa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
10 changes: 7 additions & 3 deletions argp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ function gen(args) {
file: "",
sampleRate: 32000,
output: "out.bin",
useFFmpeg: false
useFFmpeg: false,
help: false
};
args.forEach((v,i)=>{
if(i =< 1) return
if(i <= 1) return

if(v == "--sampleRate" || v == "-r"){
cf.sampleRate = parseInt(args[i+1]);
} else if(v == "--output" || v == "-o"){
} else if(v == "--help" || v == "-h") {
cf.help = true
} else if(v == "--output" || v == "-o"){
cf.output = args[i+1]
} else if(v == "--ffmpeg"){
cf.useFFmpeg = true
Expand Down
20 changes: 16 additions & 4 deletions ffmpeg.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
let cp = require("child_process") // sussy variable name o_O

const pth = require("path")
const os = require("os")
function makeid(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
async function ffmpeg(input){
let ret = {
path: ""
path: "",
exitcode: 0
};
let path = input+".wav"
let path = pth.resolve(os.tmpdir(),makeid(16)+".wav")
let proc = cp.spawn("ffmpeg",["-i",input,path])
proc.stdout.pipe(process.stdout),
proc.stdout.pipe(process.stdout)
let get_code = ()=>new Promise(res=>proc.on("close",res))
ret.exitcode = await get_code();
ret.path = path
Expand Down
10 changes: 10 additions & 0 deletions help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
USAGE: (command) [OPTIONS] FILE
Options:

-o / --output : Specify output file (default is out.bin)
-r / --sampleRate : Sample rate of output file (default is 32000)
--ffmpeg : Use ffmpeg to enable wavtobin to process other audio types

Example:
(command) -r 32000 -o out.bin file.wav
(command) -r 48000 -o /home/myfile.bin --ffmpeg file.mp3
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/node
console.log("[wtb] WaveToBin");
const ffmpeg = require("./ffmpeg")
const ffmpeg = require("./ffmpeg");
(async function(){
try {
// Import library for handling wav files.
Expand All @@ -12,10 +12,14 @@ try {
// Procure argyments
const cf = require("./argp")(process.argv)
// Read the file.

const inputFilePath = cf.file
if(cf.help){
console.log(fs.readFileSync("help.txt").toString("utf8"))
return
}
let inputFilePath = cf.file
if(cf.useFFmpeg){
inputFilePath = ffmpeg(inputFilePath)
console.log("[wtb] Waiting for FFmpeg")
inputFilePath = (await ffmpeg(inputFilePath)).path
}
const fd = fs.readFileSync(inputFilePath);
// Load input file into wf.
Expand Down

0 comments on commit f530baa

Please sign in to comment.