After discovering the solution provided by ebidel, individuals can extract id3v1 tags using jDataView:
document.querySelector('input[type="file"]').onchange = function (e) {
var reader = new FileReader();
reader.onload = function (e) {
var dv = new jDataView(this.result);
// The ID3 "TAG" section starts at byte -128 from the end of file.
// Refer to http://en.wikipedia.org/wiki/ID3 for details
if (dv.getString(3, dv.byteLength - 128) == 'TAG') {
var title = dv.getString(30, dv.tell());
var artist = dv.getString(30, dv.tell());
var album = dv.getString(30, dv.tell());
var year = dv.getString(4, dv.tell());
} else {
// No ID3v1 data found.
}
};
reader.readAsArrayBuffer(this.files[0]);
};
We learn that Chrome and other browsers now support DataView (my focus is on Chrome). I am interested to know:
- How to extract tags utilizing native DataView.
- Extracting id3 v2.4 tags (including APIC image 'coverart')
My goal is to gain insight into working with binary files and understanding concepts like little endian and big endian. To clarify, I seek guidance on extracting a specific tag - let's say the title, the TIT2
tag as an example that will help me grasp the process of extracting other tags too:
function readID3() {
//https://developer.mozilla.org/en-US/docs/Web/API/DataView
//and the position
//http://id3.org/id3v2.4.0-frames
//var id3={};
//id3.TIT2=new DataView(this.result,?offset?,?length?)
/*
?
var a=new DataView(this.result);
console.dir(String.fromCharCode(a.getUint8(0)));
?
*/
}
function readFile() {
var a = new FileReader();
a.onload = readID3;
a.readAsArrayBuffer(this.files[0]);
}
fileBox.addEventListener('change', readFile, false);
This JSFiddle link provides a demonstration.
UPDATE
I have incorporated the use of getString
to read the initial line and check for the presence of an ID3 tag. My next step involves locating the position of the first tag (TIT2), determining its variable length string, and verifying it belongs to version 2.4.
//Header
//ID3v2/file identifier "ID3"
//ID3v2 version $04 00
//ID3v2 flags (%ab000000 in v2.2, %abc00000 in v2.3, %abcd0000 in v2.4.x)
//ID3v2 size 4 * %0xxxxxxx
Potential external resources to explore:
https://developer.mozilla.org/en-US/docs/Web/API/DataView
https://github.com/aadsm/JavaScript-ID3-Reader
Currently, I am utilizing the PHP getid3 library...