2017-03-24 22:05:51 +01:00
|
|
|
// requirements
|
2017-03-24 22:11:22 +01:00
|
|
|
const fs = require('fs');
|
2017-03-24 22:56:05 +01:00
|
|
|
const path = require('path');
|
|
|
|
const async = require('async');
|
2017-03-24 22:05:51 +01:00
|
|
|
const metadata = require('musicmetadata');
|
2017-03-24 22:56:05 +01:00
|
|
|
const ffmpeg = require('fluent-ffmpeg');
|
|
|
|
const fse = require('fs-extra');
|
2017-03-24 23:36:41 +01:00
|
|
|
const util = require('./util');
|
2017-03-27 22:28:35 +02:00
|
|
|
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);
|
|
|
|
}
|
2017-03-24 22:56:05 +01:00
|
|
|
|
|
|
|
// convert file to mp3 at specified bitrate
|
2017-03-27 22:28:35 +02:00
|
|
|
function convert(source, config, callback) {
|
|
|
|
async.waterfall([
|
|
|
|
// get metadata
|
|
|
|
function (waterfallCallback) {
|
|
|
|
extractMetadata(source, waterfallCallback);
|
2017-03-24 22:56:05 +01:00
|
|
|
},
|
2017-03-27 22:28:35 +02:00
|
|
|
// 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)
|
2017-03-24 22:56:05 +01:00
|
|
|
.on('error', function (err) {
|
2017-03-27 22:28:35 +02:00
|
|
|
return waterfallCallback(err);
|
2017-03-24 22:56:05 +01:00
|
|
|
})
|
|
|
|
.on('end', function () {
|
2017-03-27 22:28:35 +02:00
|
|
|
waterfallCallback();
|
2017-03-24 22:56:05 +01:00
|
|
|
});
|
|
|
|
}
|
2017-03-27 22:28:35 +02:00
|
|
|
], callback);
|
2017-03-24 23:36:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// extract cover artwork
|
|
|
|
function extractArtwork(sourceFile, callback) {
|
|
|
|
async.waterfall([
|
|
|
|
function (asyncCallback) {
|
|
|
|
extractMetadata(sourceFile, asyncCallback);
|
|
|
|
},
|
|
|
|
function (metadata, asyncCallback) {
|
|
|
|
metadata.picture.forEach(function (picture) {
|
|
|
|
const pic = path.join(path.dirname(sourceFile), path.basename(sourceFile, path.extname(sourceFile)) + '.' + picture.format);
|
|
|
|
const stream = fs.createWriteStream(pic);
|
|
|
|
stream.write(picture.data);
|
|
|
|
stream.end();
|
|
|
|
asyncCallback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
2017-03-24 22:05:51 +01:00
|
|
|
|
|
|
|
// extract metadata for further processing
|
2017-03-24 23:36:41 +01:00
|
|
|
function extractMetadata(sourceFile, callback) {
|
2017-03-24 22:05:51 +01:00
|
|
|
const stream = fs.createReadStream(sourceFile);
|
|
|
|
metadata(stream, function (err, metadata) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
stream.close();
|
2017-03-24 23:36:41 +01:00
|
|
|
callback(null, metadata);
|
2017-03-24 22:05:51 +01:00
|
|
|
});
|
2017-03-24 23:36:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// api
|
|
|
|
exports.convert = convert;
|
2017-03-27 22:28:35 +02:00
|
|
|
exports.batchConvert = batchConvert;
|
2017-03-24 23:36:41 +01:00
|
|
|
exports.extractArtwork = extractArtwork;
|
|
|
|
exports.extractMetadata = extractMetadata;
|