Skip to content

Instantly share code, notes, and snippets.

@burt202
Created April 16, 2014 17:32
Show Gist options
  • Save burt202/10911153 to your computer and use it in GitHub Desktop.
Save burt202/10911153 to your computer and use it in GitHub Desktop.
Read MP3 ID3 Info
var fs = require('fs'),
_ = require('underscore'),
directory = 'mp3s/',
files = fs.readdirSync(directory),
genres = [
'Blues','Classic Rock','Country','Dance','Disco','Funk','Grunge','Hip-Hop','Jazz','Metal',
'New Age','Oldies','Other','Pop','R&B','Rap','Reggae','Rock','Techno','Industrial',
'Alternative','Ska','Death Metal','Pranks','Soundtrack','Euro-Techno','Ambient','Trip-Hop',
'Vocal','Jazz+Funk','Fusion','Trance','Classical','Instrumental','Acid','House','Game',
'Sound Clip','Gospel','Noise','AlternRock','Bass','Soul','Punk','Space','Meditative',
'Instrumental Pop','Instrumental Rock','Ethnic','Gothic','Darkwave','Techno-Industrial',
'Electronic','Pop-Folk','Eurodance','Dream','Southern Rock','Comedy','Cult','Gangsta',
'Top 40','Christian Rap','Pop/Funk','Jungle','Native American','Cabaret','New Wave',
'Psychadelic','Rave','Showtunes','Trailer','Lo-Fi','Tribal','Acid Punk','Acid Jazz','Polka',
'Retro','Musical','Rock & Roll','Hard Rock','Folk','Folk-Rock','National Folk','Swing',
'Fast Fusion','Bebob','Latin','Revival','Celtic','Bluegrass','Avantgarde','Gothic Rock',
'Progressive Rock','Psychedelic Rock','Symphonic Rock','Slow Rock','Big Band','Chorus',
'Easy Listening','Acoustic','Humour','Speech','Chanson','Opera','Chamber Music','Sonata',
'Symphony','Booty Bass','Primus','Porn Groove','Satire','Slow Jam','Club','Tango','Samba',
'Folklore','Ballad','Power Ballad','Rhythmic Soul','Freestyle','Duet','Punk Rock','Drum Solo',
'Acapella','Euro-House','Dance Hall'
];
for (var i = 0; i < files.length; i++) {
var path = directory + files[i],
stats = fs.statSync(path),
tags = readTags(fs.readFileSync(path)),
metadata = {};
metadata.id = _.uniqueId();
metadata.path = path;
metadata.artist = tags.artist;
metadata.title = tags.title;
metadata.album = tags.album;
metadata.genre = tags.genre;
metadata.year = tags.year;
metadata.size = roundNumber(stats.size / 1000000, 1) + 'mb';
console.log(metadata);
}
function roundNumber (num, length) {
return parseFloat(Math.round(num * Math.pow(10, length)) / Math.pow(10, length));
}
function findZero (buffer, start, end) {
var i = start;
while (buffer[i] !== 0) {
if (i >= end) {
return end;
}
i++;
}
return i;
}
function readTags (buffer) {
var offset = buffer.length - 128,
header = buffer.toString('binary', offset, offset + 3),
tags = {};
if ('TAG' !== header) {
return tags;
}
// Skip header
offset += 3;
// Title
tags.title = buffer.toString('ascii', offset, findZero(buffer, offset, offset + 30));
offset += 30;
// Artist
tags.artist = buffer.toString('ascii', offset, findZero(buffer, offset, offset + 30));
offset += 30;
// Album
tags.album = buffer.toString('ascii', offset, findZero(buffer, offset, offset + 30));
offset += 30;
// Year
tags.year = buffer.toString('ascii', offset, findZero(buffer, offset, offset + 4));
offset += 4;
// Genre
tags.genre = genres[buffer[buffer.length - 1]];
return tags;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment