|
1
|
|
|
const zlib = require('zlib') |
|
2
|
|
|
const ID3Definitions = require("./ID3Definitions") |
|
3
|
|
|
const ID3Frames = require('./ID3Frames') |
|
4
|
|
|
const ID3Util = require('./ID3Util') |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Returns array of buffers created by tags specified in the tags argument |
|
8
|
|
|
* @param tags - Object containing tags to be written |
|
9
|
|
|
* @returns {Array} |
|
10
|
|
|
*/ |
|
11
|
|
|
function createBuffersFromTags(tags) { |
|
12
|
|
|
const frames = [] |
|
13
|
|
|
if(!tags) { |
|
14
|
|
|
return frames |
|
15
|
|
|
} |
|
16
|
|
|
const rawObject = Object.keys(tags).reduce((acc, val) => { |
|
17
|
|
|
if(ID3Definitions.FRAME_IDENTIFIERS.v3[val] !== undefined) { |
|
18
|
|
|
acc[ID3Definitions.FRAME_IDENTIFIERS.v3[val]] = tags[val] |
|
19
|
|
|
} else if(ID3Definitions.FRAME_IDENTIFIERS.v4[val] !== undefined) { |
|
20
|
|
|
/** |
|
21
|
|
|
* Currently, node-id3 always writes ID3 version 3. |
|
22
|
|
|
* However, version 3 and 4 are very similar, and node-id3 can also read version 4 frames. |
|
23
|
|
|
* Until version 4 is fully supported, as a workaround, allow writing version 4 frames into a version 3 tag. |
|
24
|
|
|
* If a reader does not support a v4 frame, it's (per spec) supposed to skip it, so it should not be a problem. |
|
25
|
|
|
*/ |
|
26
|
|
|
acc[ID3Definitions.FRAME_IDENTIFIERS.v4[val]] = tags[val] |
|
27
|
|
|
} else { |
|
28
|
|
|
acc[val] = tags[val] |
|
29
|
|
|
} |
|
30
|
|
|
return acc |
|
31
|
|
|
}, {}) |
|
32
|
|
|
|
|
33
|
|
|
Object.keys(rawObject).forEach((specName) => { |
|
34
|
|
|
let frame |
|
35
|
|
|
// Check if invalid specName |
|
36
|
|
|
if(specName.length !== 4) { |
|
37
|
|
|
return |
|
38
|
|
|
} |
|
39
|
|
|
if(ID3Frames[specName] !== undefined) { |
|
40
|
|
|
frame = ID3Frames[specName].create(rawObject[specName], 3) |
|
41
|
|
|
} else if(specName.startsWith('T')) { |
|
42
|
|
|
frame = ID3Frames.GENERIC_TEXT.create(specName, rawObject[specName], 3) |
|
43
|
|
|
} else if(specName.startsWith('W')) { |
|
44
|
|
|
if(ID3Util.getSpecOptions(specName, 3).multiple && rawObject[specName] instanceof Array && rawObject[specName].length > 0) { |
|
45
|
|
|
frame = Buffer.alloc(0) |
|
46
|
|
|
// deduplicate array |
|
47
|
|
|
for(const url of [...new Set(rawObject[specName])]) { |
|
48
|
|
|
frame = Buffer.concat([frame, ID3Frames.GENERIC_URL.create(specName, url, 3)]) |
|
49
|
|
|
} |
|
50
|
|
|
} else { |
|
51
|
|
|
frame = ID3Frames.GENERIC_URL.create(specName, rawObject[specName], 3) |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (frame && frame instanceof Buffer) { |
|
56
|
|
|
frames.push(frame) |
|
57
|
|
|
} |
|
58
|
|
|
}) |
|
59
|
|
|
|
|
60
|
|
|
return frames |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return a buffer with the frames for the specified tags |
|
65
|
|
|
* @param tags - Object containing tags to be written |
|
66
|
|
|
* @returns {Buffer} |
|
67
|
|
|
*/ |
|
68
|
|
|
module.exports.createBufferFromTags = function(tags) { |
|
69
|
|
|
return Buffer.concat(createBuffersFromTags(tags)) |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
module.exports.getTagsFromBuffer = function(filebuffer, options) { |
|
73
|
|
|
const framePosition = ID3Util.getFramePosition(filebuffer) |
|
74
|
|
|
if(framePosition === -1) { |
|
75
|
|
|
return getTagsFromFrames([], 3, options) |
|
76
|
|
|
} |
|
77
|
|
|
const frameSize = ID3Util.decodeSize(filebuffer.slice(framePosition + 6, framePosition + 10)) + 10 |
|
78
|
|
|
const ID3Frame = Buffer.alloc(frameSize + 1) |
|
79
|
|
|
filebuffer.copy(ID3Frame, 0, framePosition) |
|
80
|
|
|
//ID3 version e.g. 3 if ID3v2.3.0 |
|
81
|
|
|
const ID3Version = ID3Frame[3] |
|
82
|
|
|
const tagFlags = ID3Util.parseTagHeaderFlags(ID3Frame) |
|
83
|
|
|
let extendedHeaderOffset = 0 |
|
84
|
|
|
if(tagFlags.extendedHeader) { |
|
85
|
|
|
if(ID3Version === 3) { |
|
86
|
|
|
extendedHeaderOffset = 4 + filebuffer.readUInt32BE(10) |
|
87
|
|
|
} else if(ID3Version === 4) { |
|
88
|
|
|
extendedHeaderOffset = ID3Util.decodeSize(filebuffer.slice(10, 14)) |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
const ID3FrameBody = Buffer.alloc(frameSize - 10 - extendedHeaderOffset) |
|
92
|
|
|
filebuffer.copy(ID3FrameBody, 0, framePosition + 10 + extendedHeaderOffset) |
|
93
|
|
|
|
|
94
|
|
|
const frames = getFramesFromID3Body(ID3FrameBody, ID3Version, options) |
|
95
|
|
|
|
|
96
|
|
|
return getTagsFromFrames(frames, ID3Version, options) |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
function getFramesFromID3Body(ID3FrameBody, ID3Version, options = {}) { |
|
100
|
|
|
let currentPosition = 0 |
|
101
|
|
|
const frames = [] |
|
102
|
|
|
if(!ID3FrameBody || !(ID3FrameBody instanceof Buffer)) { |
|
103
|
|
|
return frames |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
let identifierSize = 4 |
|
107
|
|
|
let textframeHeaderSize = 10 |
|
108
|
|
|
if(ID3Version === 2) { |
|
109
|
|
|
identifierSize = 3 |
|
110
|
|
|
textframeHeaderSize = 6 |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
while(currentPosition < ID3FrameBody.length && ID3FrameBody[currentPosition] !== 0x00) { |
|
114
|
|
|
const bodyFrameHeader = Buffer.alloc(textframeHeaderSize) |
|
115
|
|
|
ID3FrameBody.copy(bodyFrameHeader, 0, currentPosition) |
|
116
|
|
|
|
|
117
|
|
|
let decodeSize = false |
|
118
|
|
|
if(ID3Version === 4) { |
|
119
|
|
|
decodeSize = true |
|
120
|
|
|
} |
|
121
|
|
|
const bodyFrameSize = ID3Util.getFrameSize(bodyFrameHeader, decodeSize, ID3Version) |
|
122
|
|
|
if(bodyFrameSize + 10 > (ID3FrameBody.length - currentPosition)) { |
|
123
|
|
|
break |
|
124
|
|
|
} |
|
125
|
|
|
const specName = bodyFrameHeader.toString('utf8', 0, identifierSize) |
|
126
|
|
|
if(options.exclude instanceof Array && options.exclude.includes(specName) || options.include instanceof Array && !options.include.includes(specName)) { |
|
127
|
|
|
currentPosition += bodyFrameSize + textframeHeaderSize |
|
128
|
|
|
continue |
|
129
|
|
|
} |
|
130
|
|
|
const frameHeaderFlags = ID3Util.parseFrameHeaderFlags(bodyFrameHeader, ID3Version) |
|
131
|
|
|
const bodyFrameBuffer = Buffer.alloc(bodyFrameSize) |
|
132
|
|
|
ID3FrameBody.copy(bodyFrameBuffer, 0, currentPosition + textframeHeaderSize + (frameHeaderFlags.dataLengthIndicator ? 4 : 0)) |
|
133
|
|
|
// Size of sub frame + its header |
|
134
|
|
|
currentPosition += bodyFrameSize + textframeHeaderSize |
|
135
|
|
|
frames.push({ |
|
136
|
|
|
name: specName, |
|
137
|
|
|
flags: frameHeaderFlags, |
|
138
|
|
|
body: frameHeaderFlags.unsynchronisation ? ID3Util.processUnsynchronisedBuffer(bodyFrameBuffer) : bodyFrameBuffer |
|
139
|
|
|
}) |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return frames |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
function getTagsFromFrames(frames, ID3Version, options = {}) { |
|
146
|
|
|
const tags = { } |
|
147
|
|
|
const raw = { } |
|
148
|
|
|
|
|
149
|
|
|
frames.forEach((frame) => { |
|
150
|
|
|
let specName |
|
151
|
|
|
let identifier |
|
152
|
|
|
if(ID3Version === 2) { |
|
153
|
|
|
specName = ID3Definitions.FRAME_IDENTIFIERS.v3[ID3Definitions.FRAME_INTERNAL_IDENTIFIERS.v2[frame.name]] |
|
154
|
|
|
identifier = ID3Definitions.FRAME_INTERNAL_IDENTIFIERS.v2[frame.name] |
|
155
|
|
|
} else if(ID3Version === 3 || ID3Version === 4) { |
|
156
|
|
|
/** |
|
157
|
|
|
* Due to their similarity, it's possible to mix v3 and v4 frames even if they don't exist in their corrosponding spec. |
|
158
|
|
|
* Programs like Mp3tag allow you to do so, so we should allow reading e.g. v4 frames from a v3 ID3 Tag |
|
159
|
|
|
*/ |
|
160
|
|
|
specName = frame.name |
|
161
|
|
|
identifier = ID3Definitions.FRAME_INTERNAL_IDENTIFIERS.v3[frame.name] || ID3Definitions.FRAME_INTERNAL_IDENTIFIERS.v4[frame.name] |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if(!specName || !identifier || frame.flags.encryption) { |
|
165
|
|
|
return |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if(frame.flags.compression) { |
|
169
|
|
|
if(frame.body.length < 5) { |
|
170
|
|
|
return |
|
171
|
|
|
} |
|
172
|
|
|
const inflatedSize = frame.body.readInt32BE() |
|
173
|
|
|
/* |
|
174
|
|
|
* ID3 spec defines that compression is stored in ZLIB format, but doesn't specify if header is present or not. |
|
175
|
|
|
* ZLIB has a 2-byte header. |
|
176
|
|
|
* 1. try if header + body decompression |
|
177
|
|
|
* 2. else try if header is not stored (assume that all content is deflated "body") |
|
178
|
|
|
* 3. else try if inflation works if the header is omitted (implementation dependent) |
|
179
|
|
|
* */ |
|
180
|
|
|
try { |
|
181
|
|
|
frame.body = zlib.inflateSync(frame.body.slice(4)) |
|
182
|
|
|
} catch (e) { |
|
183
|
|
|
try { |
|
184
|
|
|
frame.body = zlib.inflateRawSync(frame.body.slice(4)) |
|
185
|
|
|
} catch (e) { |
|
186
|
|
|
try { |
|
187
|
|
|
frame.body = zlib.inflateRawSync(frame.body.slice(6)) |
|
188
|
|
|
} catch (e) { |
|
189
|
|
|
return |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
if(frame.body.length !== inflatedSize) { |
|
194
|
|
|
return |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
let decoded |
|
199
|
|
|
if(ID3Frames[specName]) { |
|
200
|
|
|
decoded = ID3Frames[specName].read(frame.body, ID3Version) |
|
201
|
|
|
} else if(specName.startsWith('T')) { |
|
202
|
|
|
decoded = ID3Frames.GENERIC_TEXT.read(frame.body, ID3Version) |
|
203
|
|
|
} else if(specName.startsWith('W')) { |
|
204
|
|
|
decoded = ID3Frames.GENERIC_URL.read(frame.body, ID3Version) |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
if(decoded) { |
|
208
|
|
|
if(ID3Util.getSpecOptions(specName, ID3Version).multiple) { |
|
209
|
|
|
if(!options.onlyRaw) { |
|
210
|
|
|
if(!tags[identifier]) { |
|
211
|
|
|
tags[identifier] = [] |
|
212
|
|
|
} |
|
213
|
|
|
tags[identifier].push(decoded) |
|
214
|
|
|
} |
|
215
|
|
|
if(!options.noRaw) { |
|
216
|
|
|
if(!raw[specName]) { |
|
217
|
|
|
raw[specName] = [] |
|
218
|
|
|
} |
|
219
|
|
|
raw[specName].push(decoded) |
|
220
|
|
|
} |
|
221
|
|
|
} else { |
|
222
|
|
|
if(!options.onlyRaw) { |
|
223
|
|
|
tags[identifier] = decoded |
|
224
|
|
|
} |
|
225
|
|
|
if(!options.noRaw) { |
|
226
|
|
|
raw[specName] = decoded |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
}) |
|
231
|
|
|
|
|
232
|
|
|
if(options.onlyRaw) { |
|
233
|
|
|
return raw |
|
234
|
|
|
} |
|
235
|
|
|
if(options.noRaw) { |
|
236
|
|
|
return tags |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
tags.raw = raw |
|
240
|
|
|
return tags |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
module.exports.getTagsFromID3Body = function(body) { |
|
244
|
|
|
return getTagsFromFrames(getFramesFromID3Body(body, 3), 3) |
|
245
|
|
|
} |
|
246
|
|
|
|