From e8f7c33a4688678bb2fd071c3144fd494a4f5c2e Mon Sep 17 00:00:00 2001 From: velvettear Date: Mon, 27 Mar 2017 22:28:35 +0200 Subject: [PATCH] refactored conversion --- badger-am.js | 78 ++++++++++++++-------------------------------------- lib/audio.js | 73 ++++++++++++++++++++++++++++++++++++++---------- package.json | 2 +- 3 files changed, 80 insertions(+), 73 deletions(-) diff --git a/badger-am.js b/badger-am.js index 0bdc984..e94a93e 100755 --- a/badger-am.js +++ b/badger-am.js @@ -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 ') .description('sort audio files by tags') - .option('-f, --format ', 'specify audio format (\'flac\', \'mp3\')', 'flac') + .option('-f, --format ', 'specify audio format (\'flac\', \'mp3\')', '.flac') .action(sort); // artwork commander .command('artwork ') .description('extract cover artwork') - .option('-f, --format ', 'specify audio format (\'flac\', \'mp3\')', 'flac') + .option('-f, --format ', '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); }); } diff --git a/lib/audio.js b/lib/audio.js index a59f229..9ee125f 100644 --- a/lib/audio.js +++ b/lib/audio.js @@ -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; \ No newline at end of file diff --git a/package.json b/package.json index eccf262..eed97ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "badger-am", - "version": "0.4.0", + "version": "0.4.1", "license": "MIT", "description": "audio manager", "author": "Daniel Sommer ",