-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlbumArtExtractor.pde
208 lines (177 loc) · 6.82 KB
/
AlbumArtExtractor.pde
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
import java.util.regex.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
class AlbumArtExtractor{
boolean debug = false;
AlbumArtExtractor(){}
AlbumArtExtractor(boolean d){
debug = d;
}
//Read %length% bytes from the FileStream. Return as a byte array
private byte[] readAsByteList(FileInputStream stream, int length){
byte[] buf = new byte[length];
try{
stream.read(buf, 0, length);
}
catch (Exception e){
e.getStackTrace();
}
return buf;
}
//Read %length% bytes from the FIleStream as ASCII string
private String readAsAsciiString(FileInputStream stream, int length){
byte[] data = this.readAsByteList(stream, length);
String res = "";
for (byte x : data) res += Character.toString((char)x);
return res;
}
//Read %count% bytes from the FileStream. Return as long since 4bytes array needs unsigned integer.
private long readAsLong(FileInputStream stream){
//Read just 1byte default
return this.readAsLong(stream, 1);
}
private long readAsLong(FileInputStream stream, int count){
byte[] buf = this.readAsByteList(stream, count);
byte[] res = new byte[8];
for (byte x : buf) x = 0x00;
for (byte x : res) x = 0x00;
for (int i = 0; i < count; i++) res[8-count+i] = buf[i];
return ByteBuffer.wrap(res).getLong();
}
//Check whether tag data is ID3v2.3 or not.
//If the mp3 file has "Extended header", return false;
public boolean checkVersion(String fPath){
try{
FileInputStream f = new FileInputStream(fPath);
String magicID = this.readAsAsciiString(f, 3);
long version = this.readAsLong(f, 2);
long extendedFlag = this.readAsLong(f, 1);
f.close();
return (magicID.equals("ID3") && version == 0x0300 && (extendedFlag & 0x40) == 0);
}
catch (Exception e){
return false;
}
}
//Return how many skips needed to go to the start byte of the albumart
private int getJpegBegin(FileInputStream rabbit){
int res = 0;
byte last = 0, now = 0;
while(true) {
last = now;
try {
now = byte(rabbit.read());
}
catch (Exception e){
e.getStackTrace();
}
res++;
if (last == byte(-1) && now == byte(-40)) {
res -= 2;
break;
}
}
return res;
}
private int getPngBegin(FileInputStream rabbit){
int res = 0;
long[] temp = new long[8];
for (long x : temp) x = 0;
final long[] beginFlag = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
while(true){
for (int i = 1; i < 8; i++) temp[i-1] = temp[i];
temp[7] = this.readAsLong(rabbit, 1);
res++;
boolean match = true;
for (int i = 0; i < 8; i++) if (temp[i] != beginFlag[i]) match = false;
if (match){
res -= 8;
break;
}
}
return res;
}
private int extract(String fPath){
try {
FileInputStream rabbit = new FileInputStream(fPath), turtle = new FileInputStream(fPath);
rabbit.skip(10);
turtle.skip(10);
long count = 1;
while(true){
String frameName = this.readAsAsciiString(rabbit, 4);
boolean validName = Pattern.compile("^[A-Z0-9]+$").matcher(frameName).matches();
if (validName == false) break;
//Get header content-size
int frameSize = int(this.readAsLong(rabbit, 4));
// Skip flags, sync pos with slower cursor
rabbit.skip(2);
turtle.skip(10);
//If looking frame is "Album Picture" frame
if (frameName.equals("APIC")){
if (debug) println("APIC header found");
//Skip some informations, sync pos with slower cursor
rabbit.skip(7);
turtle.skip(10);
String fileFormat = this.readAsAsciiString(rabbit, 3);
boolean isJpeg = (fileFormat.equals("jpe")||fileFormat.equals("jpg"));
//Find where the Picture starts
int sCount;
if (isJpeg) {
if (debug) println("AlbumArtFormat: jpeg");
sCount = this.getJpegBegin(rabbit);
}
else if (fileFormat.equals("png")) {
if (debug) println("AlbumArtFormat: png");
sCount = this.getPngBegin(rabbit);
}
else { //Something else, neither jpeg nor png.
println("Something Strange found");
rabbit.close();
turtle.close();
return -1;
}
//Read image
byte[] img = new byte[frameSize-sCount];
turtle.skip(sCount);
turtle.read(img);
//Create file
String filename = (isJpeg?"out.jpeg":"out.png");
FileOutputStream out = new FileOutputStream(sketchPath(filename));
out.write(img);
//Close streams
out.close();
out = null;
rabbit.close();
turtle.close();
return (isJpeg?1:2);
}
//Skip to the end of the frame.
rabbit.skip(frameSize);
turtle.skip(frameSize);
//Out Of Range
if (count > 74) {
println("APIC header not found");
break;
}
count++;
}
rabbit.close();
turtle.close();
}
catch (Exception e){
println(e.getStackTrace());
e.getStackTrace();
}
return -1;
}
public int generateAlbumArt(String fPath){
/* Return Values
-1: Errors
1: Jpeg
2: Png
*/
if (checkVersion(fPath) == false) return -1;
else return this.extract(fPath);
}
}