-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkuwoDecoder.js
More file actions
217 lines (186 loc) · 5.88 KB
/
Copy pathkuwoDecoder.js
File metadata and controls
217 lines (186 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const iconv = require("iconv-lite");
// 用于解码 kuwo 这样的使用 ID3v1 且 metadata 使用 GBK 编码的 mp3 buffer
function realText(buffer) {
let endPos = buffer.indexOf(0)
if (endPos >= 0) {
buffer = buffer.slice(0, endPos)
}
return iconv.decode(buffer, "gbk")
}
function paddingWrite(buf, offset, str, length) {
if (buf.length - offset < length) {
length = buf.length - offset
console.warn("paddingWrite: length exceeds", str)
}
let len = buf.write(str, offset, length)
if (len >= length) {
return
}
for (let i = len; i < length && i < buf.length - offset; i++) {
buf.writeUInt8(0, offset + i)
}
}
function readID3v1(buffer) {
let metadata = buffer.slice(buffer.byteLength - 128)
let offset = 0
let length = 3
const header = realText(metadata.slice(offset, offset + length))
offset += length
if (header !== "TAG") {
// 格式不对
return null
}
length = 30
const title = realText(metadata.slice(offset, offset + length))
offset += length
const artist = realText(metadata.slice(offset, offset + length))
offset += length
const album = realText(metadata.slice(offset, offset + length))
offset += length
// pass: year
length = 4
const year = metadata.slice(offset, offset + length)
offset += length
length = 30
const comment = realText(metadata.slice(offset, offset + length))
offset += length
length = 1
const genre = metadata.slice(offset, offset + length)
return [title, artist, album, year, comment, genre]
}
function decodeKuwo(buffer) {
const ret = readID3v1(buffer)
if (!ret) {
// 格式不对
return buffer
}
const [title, artist, album, year, comment, genre] = ret
let metadata = buffer.slice(buffer.byteLength - 128)
let offset = 3
let length = 30
paddingWrite(metadata, offset, title, length)
offset += length
paddingWrite(metadata, offset, artist, length)
offset += length
paddingWrite(metadata, offset, album, length)
offset += length
// pass: year
length = 4
metadata.write(year.toString(), offset, length)
offset += length
length = 30
paddingWrite(metadata, offset, comment, length)
offset += length
metadata.write(genre.toString(), offset, length)
return buffer
}
function convertID3v1ToID3v2(buffer, defaultTitle = "", defaultArtist = "", defaultAlbum = "") {
const Version = 3,
Encoding = "ucs2"
const ret = readID3v1(buffer)
if (!ret) {
// 格式不对
return buffer
}
//省略comment和genre
let [title, artist, album, year, comment, genre] = ret
if (!title.length) {
title = defaultTitle
}
if (!artist.length) {
artist = defaultArtist
}
if (!album.length) {
album = defaultAlbum
}
let metadata = Buffer.alloc(32768)
let offset = 0
let length = 3
metadata.write("ID3", offset, "ascii") // header
offset += length
length = 1
metadata.writeUInt8(Version, offset) // ver, ID3v2.3
offset += length
metadata.writeUInt8(0, offset) // revision
offset += length
metadata.writeUInt8(0, offset) // flag
offset += length
length = 4
// size. 在最后写
offset += length
if (title.length) {
// console.log(title)
metadata.writeUInt8(1, offset + 10)
metadata.writeUInt8(0xff, offset + 11)
metadata.writeUInt8(0xfe, offset + 12)
let contentSize = metadata.write(title, offset + 13, Encoding) + 3
length = 4
metadata.write("TIT2", offset, "ascii")
offset += length
metadata.writeUInt32BE(contentSize, offset)
offset += length
length = 2
metadata.writeUInt16BE(0, offset)
offset += length
offset += contentSize
}
if (artist.length) {
// console.log(artist)
metadata.writeUInt8(1, offset + 10)
metadata.writeUInt8(0xff, offset + 11)
metadata.writeUInt8(0xfe, offset + 12)
let contentSize = metadata.write(artist, offset + 13, Encoding) + 3
length = 4
metadata.write("TPE1", offset, "ascii")
offset += length
metadata.writeUInt32BE(contentSize, offset)
offset += length
length = 2
metadata.writeUInt16BE(0, offset)
offset += length
offset += contentSize
}
if (album.length) {
metadata.writeUInt8(1, offset + 10)
metadata.writeUInt8(0xff, offset + 11)
metadata.writeUInt8(0xfe, offset + 12)
let contentSize = metadata.write(album, offset + 13, Encoding) + 3
length = 4
metadata.write("TALB", offset, "ascii")
offset += length
metadata.writeUInt32BE(contentSize, offset)
offset += length
length = 2
metadata.writeUInt16BE(0, offset)
offset += length
offset += contentSize
}
if (year.readUInt32BE(0)) {
metadata.writeUInt8(0, offset + 10)
let contentSize = metadata.write(year.toString(), offset + 11, "ascii") + 1
// console.log(contentSize)
length = 4
metadata.write("TYER", offset, "ascii")
offset += length
metadata.writeUInt32BE(contentSize, offset)
offset += length
length = 2
metadata.writeUInt16BE(0, offset)
offset += length
offset += contentSize
}
if (offset <= 10) {
return buffer
}
// console.log(offset)
let totalSize = offset
metadata.writeUInt8(totalSize / 0x200000, 6)
totalSize %= 0x200000
metadata.writeUInt8(totalSize / 0x4000, 7)
totalSize %= 0x4000
metadata.writeUInt8(totalSize / 0x80, 8)
totalSize %= 0x80
metadata.writeUInt8(totalSize, 9)
return Buffer.concat([metadata.slice(0, offset), buffer.slice(0, buffer.byteLength - 128)])
}
module.exports = {decodeKuwo, convertID3v1ToID3v2}