Skip to content

Instantly share code, notes, and snippets.

@bryanwb
Created July 26, 2010 18:20
Show Gist options
  • Save bryanwb/490973 to your computer and use it in GitHub Desktop.
Save bryanwb/490973 to your computer and use it in GitHub Desktop.
//usage: $ node wav2mp3.js file1.wav file2.wav
// converts to .mp3 format and deletes original .wav file
// thanks to creationix for his assistance
var fs = require('fs');
var sys = require('sys');
var Step = require('step');
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var counter = 0;
Step(
function doConversions() {
var group = this.group();
process.argv.splice(2).forEach(function(arg){
convert(arg, group());
counter++;
});
},
function printResults(){
sys.puts("" + counter + " files converted to mp3");
}
);
function convert(file, callback){
if (file.slice(-4) === ".mp3"){
sys.puts('file ' + file + ' already converted ');
} else {
Step(
function checkFile(){
fs.stat(file, this);
},
function convertFile(err){
if(err) {
sys.print(err.message)
} else {
var conversion = exec('soundconverter -b -m ' +
'audio/mpeg -s .mp3 ' + file, this);
}
},
function deleteOriginal(err){
if(err){
sys.print(err.message)
} else {
exec('rm -f ' + file, this);
}
},
callback
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment