Skip to content

Instantly share code, notes, and snippets.

@Snack-X
Created January 22, 2015 02:36
Show Gist options
  • Save Snack-X/0848b23450fb616379fd to your computer and use it in GitHub Desktop.
Save Snack-X/0848b23450fb616379fd to your computer and use it in GitHub Desktop.
Tag Dump
var fs = require("fs");
var mm = require("musicmetadata");
var glob = require("glob");
if(process.argv.length < 3) {
console.log("node " + process.argv[1] + " /path/to/mp3/files");
process.exit(1);
}
var lookupPath = process.argv[2];
var list = glob.sync(lookupPath + "/**/*.mp3");
console.log("Found " + list.length + " files");
var AlbumLookup = {};
var Album = [];
var Song = [];
function parseSong(idx, after) {
var path = list[idx];
if(typeof path === "undefined") {
after();
return;
}
var stream = fs.createReadStream(path);
var parser = mm(stream);
parser.on("metadata", function(result) {
var album = result.album;
var title = result.title;
var artist = result.artist[0];
var track = result.track.no;
var disc = result.disk.no;
var albumId;
if(typeof AlbumLookup[album] === "undefined") {
Album.push(album);
AlbumLookup[album] = Album.length - 1;
}
albumId = AlbumLookup[album];
Song.push({
albumId: albumId,
disc: disc,
track: track,
title: title,
artist: artist,
});
});
parser.on("done", function(err) {
stream.destroy();
parseSong(idx + 1, after);
});
}
parseSong(0, function() {
fs.writeFileSync("Album.json", JSON.stringify(Album, null, 2), { encoding: "utf8" });
fs.writeFileSync("Song.json", JSON.stringify(Song, null, 2), { encoding: "utf8" });
console.log("dumped to Album.json, Song.json");
});
{
"name": "tagdump",
"version": "1.0.0",
"description": "Tag Dump",
"main": "index.js",
"author": "",
"private": true,
"dependencies": {
"glob": "^4.3.5",
"musicmetadata": "^0.6.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment