Skip to content

Instantly share code, notes, and snippets.

@creationix
Forked from bryanwb/wav2mp3.js
Created July 26, 2010 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/490990 to your computer and use it in GitHub Desktop.
Save creationix/490990 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
var fs = require('fs');
var sys = require('sys');
var Step = require('step');
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
function convert(file, callback){
if (file.slice(-4) === ".mp3") {
callback(new Error('file ' + file + ' already converted '));
return;
}
Step(
function checkFile() {
fs.stat(file, this);
},
function convertFile(err) {
if (err) { callback(err); return; }
var conversion = exec('soundconverter -b -m ' +
'audio/mpeg -s .mp3 ' + file, this);
},
function deleteOriginal(err) {
if (err) { callback(err); return; }
exec('rm -f ' + file, this);
},
callback
);
};
Step(
function () {
var group = this.group();
process.argv.splice(2).forEach(function (file) {
convert(file, group());
});
},
function (err, results) {
if (err) throw err;
sys.puts(results.length + " successfully converted.");
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment