-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid3.js
60 lines (46 loc) · 1.58 KB
/
id3.js
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
/*
You can find the specific tag names here: https://exiftool.org/TagNames/ID3.html
*/
function decode(decoder, array){
if(!array || !array.length)
return "";
return decoder.decode(new Uint8Array(array));
}
async function id3(url){
let blob = new Uint8Array(await fetch(url).then(data => data.arrayBuffer())),
decoder = new TextDecoder("utf-8"),
decoded = decode(decoder, blob),
boundary = decoded.indexOf("TAG"),
pairs = {},
last = 0;
if(decoded.indexOf("ID3") !== 0)
return;
blob = blob.slice(10, boundary);
for(let i = 1; i < blob.length - 1; i++){
let prev = blob[i - 1],
now = blob[i],
next = blob[i + 1];
if(prev != 0 && now != 0 && next == 0){
let key = decode(decoder, blob.slice(last, i + 1)),
length = (blob[i + 2] << 16) + (blob[i + 3] << 8) + blob[i + 4],
start = i + 8,
end = start + length - 1;
pairs[key] = blob.slice(start, end);
last = end;
i = last;
}
}
for(let key in pairs){
if(key == "APIC" || key == "PIC")
continue;
pairs[key] = decode(new TextDecoder("utf-16"), pairs[key]);
}
if("APIC" in pairs || "PIC" in pairs){
let image = pairs["APIC"] || pairs["PIC"];
pairs.image_mime = decode(decoder, image.slice(0, image.indexOf(3) - 1));
image = image.slice(type.length + 2);
image = image.slice(image.indexOf(0) + 1);
pairs.image = image;
}
return pairs;
}