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');
|
|
|
|
|
|
|
|
// convert file to mp3 at specified bitrate
|
|
|
|
exports.convert = 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 (asyncCallback) {
|
|
|
|
ffmpeg(path.normalize(input)).audioCodec('libmp3lame').audioBitrate(bitrate).save(output)
|
|
|
|
.on('error', function (err) {
|
|
|
|
return asyncCallback(err);
|
|
|
|
})
|
|
|
|
.on('end', function () {
|
|
|
|
asyncCallback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], function (err, results) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
2017-03-24 22:05:51 +01:00
|
|
|
|
|
|
|
// extract metadata for further processing
|
|
|
|
exports.extractMetadata = function extractMetadata(sourceFile, callback) {
|
|
|
|
const stream = fs.createReadStream(sourceFile);
|
|
|
|
metadata(stream, function (err, metadata) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
stream.close();
|
|
|
|
callback(null, sourceFile, metadata)
|
|
|
|
});
|
|
|
|
};
|