Comparing version 0.0.4 to 0.0.5
@@ -13,2 +13,6 @@ var nodeID3 = require('../index.js'); | ||
var success = nodeID3.write(tags, "./example/music.mp3"); //Pass tags and filepath | ||
console.log(success); //true or contains error | ||
console.log(success); | ||
//No image support atm | ||
var read = nodeID3.read("./example/music.mp3"); | ||
console.log(read); |
100
index.js
@@ -76,3 +76,3 @@ var fs = require('fs'); | ||
var size = this.encodeSize(totalSize); | ||
var size = encodeSize(totalSize); | ||
@@ -86,6 +86,5 @@ frames[0].writeUInt8(size[0], 6); | ||
this.removeTags(filepath); | ||
try { | ||
var data = fs.readFileSync(filepath); | ||
data = this.removeTagsFromBuffer(data) || data; | ||
var rewriteFile = Buffer.concat([completeTag, data]); | ||
@@ -100,25 +99,84 @@ fs.writeFileSync(filepath, rewriteFile, 'binary'); | ||
NodeID3.prototype.removeTags = function(filepath) { | ||
try { | ||
var data = fs.readFileSync(filepath); | ||
} catch(e) { | ||
return e; | ||
NodeID3.prototype.read = function(filebuffer) { | ||
if(typeof filebuffer === "string" || filebuffer instanceof String) | ||
filebuffer = fs.readFileSync(filebuffer); | ||
var header = new Buffer(10); | ||
filebuffer.copy(header, 0, getID3Start(filebuffer)) | ||
frameSize = getFrameSize(header); | ||
var ID3Frame = new Buffer(frameSize + 1); | ||
filebuffer.copy(ID3Frame, 0, getID3Start(filebuffer)); | ||
var tags = TIF; | ||
var frames = Object.keys(tags); | ||
for(var i = 0; i < frames.length; i++) { | ||
var frameStart = ID3Frame.indexOf(tags[frames[i]]); | ||
if(frameStart > -1) { | ||
var frameSize = decodeSize(new Buffer([ID3Frame[frameStart + 4], ID3Frame[frameStart + 5], ID3Frame[frameStart + 6], ID3Frame[frameStart + 7]])); | ||
var offset = 1; | ||
if(ID3Frame[frameStart + 11] == 0xFF || ID3Frame[frameStart + 12] == 0xFE) { | ||
offset = 3; | ||
} | ||
var frame = new Buffer(frameSize - offset); | ||
ID3Frame.copy(frame, 0, frameStart + 10 + offset); | ||
tags[frames[i]] = frame.toString('utf8').replace(/\0/g, ""); | ||
} else { | ||
delete tags[frames[i]]; | ||
} | ||
} | ||
var tagStart = String.prototype.indexOf.call(data, (new Buffer("ID3"))) | ||
/*if(ID3Frame.indexOf("APIC")) { | ||
var picture = {}; | ||
var APICFrameStart = ID3Frame.indexOf("APIC"); | ||
var APICFrameSize = decodeSize(new Buffer([ID3Frame[APICFrameStart + 4], ID3Frame[APICFrameStart + 5], ID3Frame[APICFrameStart + 6], ID3Frame[APICFrameStart + 7]])); | ||
var APICFrame = new Buffer(APICFrameSize); | ||
ID3Frame.copy(APICFrame, 0, frameStart + 10); | ||
if(APICFrame.indexOf("image/jpeg")) picture.mime = "jpeg"; | ||
else if(APICFrame.indexOf("image/png")) picture.mime = "png"; | ||
}*/ | ||
if(tagStart == -1 || tagStart > 20) return true; //No Tags found || TEMP FIX (TODO) | ||
return tags; | ||
} | ||
var hSize = new Buffer([data[tagStart + 6], data[tagStart + 7], data[tagStart + 8], data[tagStart + 9]]); | ||
function getID3Start(buffer) { | ||
var tagStart = buffer.indexOf("ID3"); | ||
var musicStart = buffer.indexOf("" + 0xFFFB30); | ||
if(tagStart > musicStart) | ||
return tagStart; | ||
else | ||
return -1; | ||
} | ||
if ((hSize[0] | hSize[1] | hSize[2] | hSize[3]) & 0x80) { | ||
//INVALID TAG SIZE | ||
return false; | ||
} | ||
function getFrameSize(buffer) { | ||
return decodeSize(new Buffer([buffer[6], buffer[7], buffer[8], buffer[9]])); | ||
} | ||
var encSize = ((hSize[0] << 21) + (hSize[1] << 14) + (hSize[2] << 7) + (hSize[3])); | ||
NodeID3.prototype.removeTagsFromBuffer = function (data){ | ||
var ts = String.prototype.indexOf.call(data, (new Buffer("ID3"))); | ||
var newData = data.slice(tagStart + encSize + 10); | ||
if(ts == -1 || ts > 20) return false; | ||
var hSize = new Buffer([data[ts +6], data[ts +7], data[ts +8], data[ts +9]]); | ||
if ((hSize[0] | hSize[1] | hSize[2] | hSize[3]) & 0x80) { | ||
//INVALID TAG SIZE | ||
return false; | ||
} | ||
var encSize = ((hSize[0] << 21) + (hSize[1] << 14) + (hSize[2] << 7) + (hSize[3])); | ||
return data.slice(ts + encSize + 10); | ||
}; | ||
NodeID3.prototype.removeTags = function(filepath) { | ||
try { | ||
var data = fs.readFileSync(filepath); | ||
} catch(e) { | ||
return e; | ||
} | ||
var newData = this.removeTagsFromBuffer(data); | ||
if(!newData) return false; | ||
try { | ||
fs.writeFileSync(filepath, newData, 'binary'); | ||
@@ -132,3 +190,3 @@ } catch(e) { | ||
NodeID3.prototype.encodeSize = function(totalSize) { | ||
function encodeSize(totalSize) { | ||
byte_3 = totalSize & 0x7F; | ||
@@ -141,4 +199,4 @@ byte_2 = (totalSize >> 7) & 0x7F; | ||
NodeID3.prototype.decodeSize = function(totalSize) { | ||
function decodeSize(hSize) { | ||
return ((hSize[0] << 21) + (hSize[1] << 14) + (hSize[2] << 7) + (hSize[3])); | ||
} | ||
@@ -182,3 +240,3 @@ | ||
var bContent = new Buffer(mime_type.length + 4, 'binary'); | ||
var bContent = new Buffer(mime_type.length + 4); | ||
bContent.fill(0); | ||
@@ -185,0 +243,0 @@ bContent.write(mime_type, 1); |
{ | ||
"name": "node-id3", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Pure JavaScript ID3 Tag writer", | ||
@@ -5,0 +5,0 @@ "author": "Jan Metzger <jan.metzger@gmx.net>", |
@@ -5,3 +5,3 @@ # node-id3 | ||
#### Right now, there's only write and remove support (No read, only ID3v2) | ||
#### Right now, there's only write, remove and partial read support (only ID3v2) | ||
@@ -22,9 +22,36 @@ ## Example: | ||
var success = nodeID3.write(tags, "./example/music.mp3"); //Pass tags and filepath | ||
console.log(success); //true or contains error | ||
nodeID3.removeTags("./example/music.mp3") | ||
//Pass tags and filepath | ||
var success = nodeID3.write(tags, "./example/music.mp3"); | ||
//returns true if written correctly | ||
console.log(success); | ||
//Pass filepath/buffer | ||
var read = nodeID3.read("./example/music.mp3"); | ||
//returns tags | ||
console.log(read); | ||
``` | ||
###Write ID3v2-Tags | ||
``` | ||
//Pass tags and filepath | ||
var success = nodeID3.write(tags, "./example/music.mp3"); | ||
//returns true if written correctly | ||
console.log(success); | ||
``` | ||
###Read ID3v2-Tags (currently no support for images) | ||
``` | ||
//Pass filepath/buffer | ||
var read = nodeID3.read("./example/music.mp3"); | ||
//returns tags | ||
console.log(read); | ||
``` | ||
###Remove ID3v2-Tags | ||
``` | ||
nodeID3.removeTags("./music.mp3"); //Pass the path to the mp3 file | ||
nodeID3.removeTags("./example/music.mp3"); //Pass the path to the mp3 file | ||
``` | ||
@@ -31,0 +58,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11826
215
98