-
-
Save Zazama/9e83dee3ca074f3ff907d8d034b77efb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const NodeID3 = require('node-id3') | |
const buffer = fs.readFileSync('./AVAA - Watch Me TEST.aif') | |
const id3ChunkPosition = indexOfID3ChunkPosition(buffer) | |
const chunk = sliceChunk(buffer, id3ChunkPosition); | |
const id3Buffer = chunk.subarray(8) | |
let newId3Buffer = NodeID3.update({ | |
title: "a" | |
}, id3Buffer) | |
const newFileBuffer = Buffer.concat([ | |
buffer.subarray(0, id3ChunkPosition), | |
Buffer.from([0x49, 0x44, 0x33, 0x20]), | |
getChunkSizeBuffer(newId3Buffer), | |
newId3Buffer, | |
buffer.subarray( | |
id3ChunkPosition + 8 + chunk.length | |
) | |
]) | |
fs.writeFileSync('./newfile.aif', newFileBuffer) | |
/** | |
* Find an ID3 chunk with an ID3 Tag inside. | |
* [I D 3 f s s s s I D 3 v] | |
* where s is size byte and v is version byte. | |
*/ | |
function indexOfID3ChunkPosition(buffer) { | |
let chunkPosition = -1 | |
do { | |
chunkPosition = buffer.indexOf("ID3", chunkPosition + 1) | |
if( | |
chunkPosition !== -1 && | |
Buffer.from("ID3").compare( | |
buffer, | |
chunkPosition + 9, // ID3 Tag start | |
chunkPosition + 12 // ID3 ID end | |
) && | |
[2, 3, 4].includes(buffer[chunkPosition + 11]) // ID3 version | |
) { | |
return chunkPosition | |
} | |
} while (chunkPosition !== -1) | |
return -1 | |
} | |
function sliceChunk(buffer, position) { | |
const length = buffer.readUInt32BE(position + 4) | |
return buffer.subarray(position, position + length) | |
} | |
function getChunkSizeBuffer(buffer) { | |
const sizeBuffer = Buffer.alloc(4, 0x00) | |
sizeBuffer.writeUInt32BE(buffer.length) | |
return sizeBuffer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment