|
1
|
|
|
const ID3Definitions = require("./ID3Definitions") |
|
2
|
|
|
const ID3Frames = require('./ID3Frames') |
|
3
|
|
|
const ID3Util = require('./ID3Util') |
|
4
|
|
|
const ID3Frame = require('./ID3Frame') |
|
5
|
|
|
const ID3FrameHeader = require('./ID3FrameHeader') |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Returns array of buffers created by tags specified in the tags argument |
|
9
|
|
|
* @param tags - Object containing tags to be written |
|
10
|
|
|
* @returns {Array} |
|
11
|
|
|
*/ |
|
12
|
|
|
function createBuffersFromTags(tags) { |
|
13
|
|
|
const frames = [] |
|
14
|
|
|
if(!tags) { |
|
15
|
|
|
return frames |
|
16
|
|
|
} |
|
17
|
|
|
const rawObject = Object.keys(tags).reduce((acc, val) => { |
|
18
|
|
|
if(ID3Definitions.FRAME_IDENTIFIERS.v3[val] !== undefined) { |
|
19
|
|
|
acc[ID3Definitions.FRAME_IDENTIFIERS.v3[val]] = tags[val] |
|
20
|
|
|
} else if(ID3Definitions.FRAME_IDENTIFIERS.v4[val] !== undefined) { |
|
21
|
|
|
/** |
|
22
|
|
|
* Currently, node-id3 always writes ID3 version 3. |
|
23
|
|
|
* However, version 3 and 4 are very similar, and node-id3 can also read version 4 frames. |
|
24
|
|
|
* Until version 4 is fully supported, as a workaround, allow writing version 4 frames into a version 3 tag. |
|
25
|
|
|
* If a reader does not support a v4 frame, it's (per spec) supposed to skip it, so it should not be a problem. |
|
26
|
|
|
*/ |
|
27
|
|
|
acc[ID3Definitions.FRAME_IDENTIFIERS.v4[val]] = tags[val] |
|
28
|
|
|
} else { |
|
29
|
|
|
acc[val] = tags[val] |
|
30
|
|
|
} |
|
31
|
|
|
return acc |
|
32
|
|
|
}, {}) |
|
33
|
|
|
|
|
34
|
|
|
Object.keys(rawObject).forEach((frameIdentifier) => { |
|
35
|
|
|
let frame |
|
36
|
|
|
// Check if invalid frameIdentifier |
|
37
|
|
|
if(frameIdentifier.length !== 4) { |
|
38
|
|
|
return |
|
39
|
|
|
} |
|
40
|
|
|
if(ID3Frames[frameIdentifier] !== undefined) { |
|
41
|
|
|
frame = ID3Frames[frameIdentifier].create(rawObject[frameIdentifier], 3) |
|
42
|
|
|
} else if(frameIdentifier.startsWith('T')) { |
|
43
|
|
|
frame = ID3Frames.GENERIC_TEXT.create(frameIdentifier, rawObject[frameIdentifier], 3) |
|
44
|
|
|
} else if(frameIdentifier.startsWith('W')) { |
|
45
|
|
|
if(ID3Util.getSpecOptions(frameIdentifier, 3).multiple && rawObject[frameIdentifier] instanceof Array && rawObject[frameIdentifier].length > 0) { |
|
46
|
|
|
frame = Buffer.alloc(0) |
|
47
|
|
|
// deduplicate array |
|
48
|
|
|
for(const url of [...new Set(rawObject[frameIdentifier])]) { |
|
49
|
|
|
frame = Buffer.concat([frame, ID3Frames.GENERIC_URL.create(frameIdentifier, url, 3)]) |
|
50
|
|
|
} |
|
51
|
|
|
} else { |
|
52
|
|
|
frame = ID3Frames.GENERIC_URL.create(frameIdentifier, rawObject[frameIdentifier], 3) |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (frame && frame instanceof Buffer) { |
|
57
|
|
|
frames.push(frame) |
|
58
|
|
|
} |
|
59
|
|
|
}) |
|
60
|
|
|
|
|
61
|
|
|
return frames |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Return a buffer with the frames for the specified tags |
|
66
|
|
|
* @param tags - Object containing tags to be written |
|
67
|
|
|
* @returns {Buffer} |
|
68
|
|
|
*/ |
|
69
|
|
|
module.exports.createBufferFromTags = function(tags) { |
|
70
|
|
|
return Buffer.concat(createBuffersFromTags(tags)) |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
module.exports.getTagsFromBuffer = function(filebuffer, options) { |
|
74
|
|
|
const framePosition = ID3Util.getFramePosition(filebuffer) |
|
75
|
|
|
if(framePosition === -1) { |
|
76
|
|
|
return getTagsFromFrames([], 3, options) |
|
77
|
|
|
} |
|
78
|
|
|
const frameSize = ID3Util.decodeSize(filebuffer.slice(framePosition + 6, framePosition + 10)) + 10 |
|
79
|
|
|
const ID3Frame = Buffer.alloc(frameSize + 1) |
|
80
|
|
|
filebuffer.copy(ID3Frame, 0, framePosition) |
|
81
|
|
|
//ID3 version e.g. 3 if ID3v2.3.0 |
|
82
|
|
|
const ID3Version = ID3Frame[3] |
|
83
|
|
|
const tagFlags = ID3Util.parseTagHeaderFlags(ID3Frame) |
|
84
|
|
|
let extendedHeaderOffset = 0 |
|
85
|
|
|
if(tagFlags.extendedHeader) { |
|
86
|
|
|
if(ID3Version === 3) { |
|
87
|
|
|
extendedHeaderOffset = 4 + filebuffer.readUInt32BE(10) |
|
88
|
|
|
} else if(ID3Version === 4) { |
|
89
|
|
|
extendedHeaderOffset = ID3Util.decodeSize(filebuffer.slice(10, 14)) |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
const ID3FrameBody = Buffer.alloc(frameSize - 10 - extendedHeaderOffset) |
|
93
|
|
|
filebuffer.copy(ID3FrameBody, 0, framePosition + 10 + extendedHeaderOffset) |
|
94
|
|
|
|
|
95
|
|
|
const frames = getFramesFromTagBody(ID3FrameBody, ID3Version, options) |
|
96
|
|
|
|
|
97
|
|
|
return getTagsFromFrames(frames, ID3Version, options) |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
function isFrameDiscarded(frameIdentifier, options) { |
|
101
|
|
|
if(options.exclude instanceof Array && options.exclude.includes(frameIdentifier)) { |
|
102
|
|
|
return true |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return options.include instanceof Array && !options.include.includes(frameIdentifier) |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
function getFramesFromTagBody(tagBody, version, options = {}) { |
|
109
|
|
|
if(!(tagBody instanceof Buffer)) { |
|
110
|
|
|
return [] |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
const frames = [] |
|
114
|
|
|
while(tagBody.length && tagBody[0] !== 0x00) { |
|
115
|
|
|
const frameSize = ID3FrameHeader.getFrameSize(tagBody, version) |
|
116
|
|
|
|
|
117
|
|
|
// Prevent errors due to broken data. |
|
118
|
|
|
if (frameSize > tagBody.length) { |
|
119
|
|
|
break |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
const frameBuffer = tagBody.subarray(0, frameSize) |
|
123
|
|
|
const frame = ID3Frame.createFromBuffer(frameBuffer, version) |
|
124
|
|
|
if(frame && !isFrameDiscarded(frame.identifier, options)) { |
|
125
|
|
|
frames.push(frame) |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
tagBody = tagBody.subarray(frameSize) |
|
129
|
|
|
} |
|
130
|
|
|
return frames |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
function getTagsFromFrames(frames, version, options = {}) { |
|
134
|
|
|
const tags = { } |
|
135
|
|
|
const raw = { } |
|
136
|
|
|
|
|
137
|
|
|
frames.forEach((frame) => { |
|
138
|
|
|
const frameValue = frame.getValue() |
|
139
|
|
|
const frameAlias = ID3Definitions.FRAME_INTERNAL_IDENTIFIERS.v3[frame.identifier] || ID3Definitions.FRAME_INTERNAL_IDENTIFIERS.v4[frame.identifier] |
|
140
|
|
|
|
|
141
|
|
|
if(ID3Util.getSpecOptions(frame.identifier, version).multiple) { |
|
142
|
|
|
if(!options.onlyRaw) { |
|
143
|
|
|
if(!tags[frameAlias]) { |
|
144
|
|
|
tags[frameAlias] = [] |
|
145
|
|
|
} |
|
146
|
|
|
tags[frameAlias].push(frameValue) |
|
147
|
|
|
} |
|
148
|
|
|
if(!options.noRaw) { |
|
149
|
|
|
if(!raw[frame.identifier]) { |
|
150
|
|
|
raw[frame.identifier] = [] |
|
151
|
|
|
} |
|
152
|
|
|
raw[frame.identifier].push(frameValue) |
|
153
|
|
|
} |
|
154
|
|
|
} else { |
|
155
|
|
|
if(!options.onlyRaw) { |
|
156
|
|
|
tags[frameAlias] = frameValue |
|
157
|
|
|
} |
|
158
|
|
|
if(!options.noRaw) { |
|
159
|
|
|
raw[frame.identifier] = frameValue |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
}) |
|
163
|
|
|
|
|
164
|
|
|
if(options.onlyRaw) { |
|
165
|
|
|
return raw |
|
166
|
|
|
} |
|
167
|
|
|
if(options.noRaw) { |
|
168
|
|
|
return tags |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
tags.raw = raw |
|
172
|
|
|
return tags |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
module.exports.getTagsFromID3Body = function(body) { |
|
176
|
|
|
return getTagsFromFrames(getFramesFromTagBody(body, 3), 3) |
|
177
|
|
|
} |
|
178
|
|
|
|