refactored conversion

This commit is contained in:
Daniel Sommer 2017-03-27 22:28:35 +02:00
parent cbe726c4fc
commit e8f7c33a46
3 changed files with 80 additions and 73 deletions

View file

@ -9,20 +9,21 @@ const recursive = require('recursive-readdir');
const app = require('./package.json');
const audio = require('./lib/audio');
const util = require('./lib/util');
const cli = require('./lib/cli');
// generate timestamp
const start = process.hrtime();
main();
badger();
// "main"
function main() {
util.printLogo();
parseCLI();
function badger() {
cli.printLogo();
run();
}
// process command line arguments
function parseCLI() {
// start from command lines
function run() {
// general options
commander
.version(app.version)
@ -38,70 +39,33 @@ function parseCLI() {
commander
.command('sort <input> <output>')
.description('sort audio files by tags')
.option('-f, --format <type>', 'specify audio format (\'flac\', \'mp3\')', 'flac')
.option('-f, --format <type>', 'specify audio format (\'flac\', \'mp3\')', '.flac')
.action(sort);
// artwork
commander
.command('artwork <input>')
.description('extract cover artwork')
.option('-f, --format <type>', 'specify audio format (\'flac\', \'mp3\')', 'flac')
.option('-f, --format <type>', 'specify audio format (\'flac\', \'mp3\')', '.flac')
.action(artwork);
// parse command line arguments
commander.parse(process.argv);
}
// convert files
function convert(input, output, options) {
async.waterfall([
function (asyncCallback) {
recursive(input, [ignoreFilter], asyncCallback);
},
function (files, asyncCallback) {
console.log(files.length + ' \'flac\' files found');
// display progressbar
const bar = util.createProgressBar(files.length);
// handle each file
async.eachLimit(files, commander.concurrency, function (file, eachCallback) {
processFileConvert(output, file, options, function (err) {
bar.tick();
if (err) {
return eachCallback(err);
}
eachCallback();
});
}, function (err) {
if (err) {
return asyncCallback(err);
}
asyncCallback();
});
}
], function (err, result) {
util.exit(err, start);
});
function ignoreFilter(file, stats) {
return !stats.isDirectory() && path.extname(file).indexOf('flac') == -1;
// create config object
function getConfig(input, output, options, commander) {
return {
input: path.normalize(input),
output: path.normalize(output),
concurrency: commander.concurrency || os.cpus().length,
bitrate: options.bitrate || 320,
format: options.format || '.flac'
}
}
// move file to location defined by its metadata
function processFileConvert(output, sourceFile, options, callback) {
async.waterfall([
function (asyncCallback) {
audio.extractMetadata(sourceFile, asyncCallback)
},
function (metadata, asyncCallback) {
util.pathFromMetadata(sourceFile, output, metadata, asyncCallback);
},
function (targetFile, asyncCallback) {
audio.convert(sourceFile, targetFile, options.bitrate, asyncCallback);
}
], function (err, results) {
if (err) {
return callback(err);
}
callback(null, results);
// convert files
function convert(input, output, options) {
audio.batchConvert(getConfig(input, output, options, commander), function (err) {
cli.exit(err);
});
}

View file

@ -6,29 +6,71 @@ const metadata = require('musicmetadata');
const ffmpeg = require('fluent-ffmpeg');
const fse = require('fs-extra');
const util = require('./util');
const cli = require('./cli');
// convert all files in input directory
function batchConvert(config, callback) {
async.waterfall([
// get files
function (waterfallCallback) {
util.readDirRecursive(config.input, config.format, waterfallCallback);
},
// display info, prompt user and create progressbar
function (files, waterfallCallback) {
cli.askForConfirmation(files.length + ' files found.\nstart conversion now?', ['yes', 'y'], function (err) {
if (err) {
return waterfallCallback(err);
}
waterfallCallback(null, files, cli.createProgressBar(files.length));
});
},
// process each file
function (files, bar, waterfallCallback) {
async.eachLimit(files, config.concurrency, function (file, eachCallback) {
convert(file, config, function (err) {
bar.tick();
if (err) {
return eachCallback(err);
}
eachCallback();
});
}, waterfallCallback);
}
], callback);
}
// convert file to mp3 at specified bitrate
function convert(input, output, bitrate, callback) {
output = path.join(path.dirname(output), path.basename(output, path.extname(output)) + '.mp3');
async.series([
function (asyncCallback) {
fse.mkdirs(path.dirname(output), asyncCallback);
function convert(source, config, callback) {
async.waterfall([
// get metadata
function (waterfallCallback) {
extractMetadata(source, waterfallCallback);
},
function (asyncCallback) {
ffmpeg(path.normalize(input)).audioCodec('libmp3lame').audioBitrate(bitrate).save(output)
// create path from metadata
function (metadata, waterfallCallback) {
util.getPathByMetadata(source, config.output, metadata, waterfallCallback);
},
// create target directory
function (target, waterfallCallback) {
target = path.join(path.dirname(target), path.basename(target, path.extname(target)) + '.mp3');
fse.mkdirs(path.dirname(target), function (err) {
if (err) {
return waterfallCallback(err);
}
waterfallCallback(null, target, config);
});
},
// convert file to mp3
function (target, config, waterfallCallback) {
ffmpeg(path.normalize(source)).audioCodec('libmp3lame').audioBitrate(config.bitrate).save(target)
.on('error', function (err) {
return asyncCallback(err);
return waterfallCallback(err);
})
.on('end', function () {
asyncCallback();
waterfallCallback();
});
}
], function (err, results) {
if (err) {
return callback(err);
}
callback();
});
], callback);
}
// extract cover artwork
@ -68,5 +110,6 @@ function extractMetadata(sourceFile, callback) {
// api
exports.convert = convert;
exports.batchConvert = batchConvert;
exports.extractArtwork = extractArtwork;
exports.extractMetadata = extractMetadata;

View file

@ -1,6 +1,6 @@
{
"name": "badger-am",
"version": "0.4.0",
"version": "0.4.1",
"license": "MIT",
"description": "audio manager",
"author": "Daniel Sommer <daniel.sommer@velvettear.de>",