Skip to content

Instantly share code, notes, and snippets.

@clooth
Created September 10, 2012 21:17
Show Gist options
  • Select an option

  • Save clooth/3693922 to your computer and use it in GitHub Desktop.

Select an option

Save clooth/3693922 to your computer and use it in GitHub Desktop.
var fs = require('fs'),
mmd = require('musicmetadata');
// Parser
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var i = 0;
(function next() {
var file = list[i++];
if (!file) return done(null, results);
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
})();
});
};
var trackCount = 0,
tracksLoaded = 0,
data = {};
walk('./music', function(err, results) {
trackCount = results.length;
results.forEach(function(item) {
var simpleTrackName = item.replace(/^.*\/|\.[^.]*$/g, ''),
parser = new mmd(fs.createReadStream(item));
parser.on('metadata', function(result) {
data[simpleTrackName] = {
title: result.title,
artist: result.artist,
album: result.album
};
console.log(tracksLoaded + '/' + trackCount);
tracksLoaded++;
if (trackCount == tracksLoaded) {
console.log(JSON.stringify(data));
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment