-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFLACMetadataEditor.js
464 lines (414 loc) · 20.3 KB
/
FLACMetadataEditor.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* jslint esversion: 6, bitwise: true */
// ==UserScript==
// @name JS FLACMetadataEditor
// @description Allows you to edit metadata of FLAC files. CO
// @namespace universe.earth.www.ahohnmyc
// @version 0.0.2.1
// @license GPL-3.0-or-later
// @grant none
// ==/UserScript==
const FLACMetadataEditor = (()=>{
'use strict';
const _version = '0.0.2.1';
class VorbisComment extends Array {}
class VorbisCommentPacket {
/* Need to easy initialization */
_addComment(field) {
const value = field.split('=')[1];
field = field.split('=')[0].toUpperCase();
if (!this.hasOwnProperty(field))
this[field] = new VorbisComment();
if (!this[field].some(storedValue=> storedValue===value))
this[field].push(value.toString());
return this;
}
toStringArray() {
const array = [];
Object.keys(this).sort().forEach(key=> {
this[key].forEach(value=> {
array.push(key+'='+value);
});
});
return array;
}
}
class FLACMetadataBlockData {}
class FLACMetadataBlock {
constructor() {
this.blockType = '';
this.blockTypeNubmer = 0;
this.blockSize = 0;
this.data = new FLACMetadataBlockData();
this.offset = 0;
}
get serializedSize() {
switch (this.blockType) {
case 'STREAMINFO': return 34;
case 'PADDING': return this.blockSize;
case 'APPLICATION': return 4+this.data.applicationData.length;
case 'SEEKTABLE': return this.data.points.length*18;
case 'VORBIS_COMMENT':
const totl = this.data.comments.toStringArray().reduce((sum, str)=>sum+4+str.toUTF8().length, 0);
return 4+this.data.vendorString.length+4+ totl;
case 'CUESHEET': return 0;
case 'PICTURE': return 4+4+this.data.MIMEType.toUTF8().length+4+this.data.description.toUTF8().length+4+4+4+4+4+this.data.data.length;
}
}
}
class FLACMetadataBlocks extends Array {}
class FLACMetadata {
constructor() {
this.blocks = new FLACMetadataBlocks();
this.framesOffset = 0;
this.signature = '';
}
}
class _FLACMetadataEditor {
get scriptVersion() {return _version;}
constructor(buffer) {
if (!buffer || typeof buffer !== 'object' || !('byteLength' in buffer)) {
throw new Error('First argument should be an instance of ArrayBuffer or Buffer');
}
this.arrayBuffer = buffer;
this.metadata = new FLACMetadata();
String.prototype.toUTF8 = function(str = null) {
return new TextEncoder().encode(str ? str : this);
};
this._parseMetadata();
return this;
}
/* unpack */
_getBytesAsNumber (array, start=0, end=array.length-start) {return Array.from(array.subarray(start, start+end)).reduce ((result, b)=>result=256*result+b, 0);}
_getBytesAsNumberLittleEndian(array, start=0, end=array.length-start) {return Array.from(array.subarray(start, start+end)).reduceRight((result, b)=>result=256*result+b, 0);}
_getBytesAsHexString (array, start=0, end=array.length-start) {return Array.from(array.subarray(start, start+end)).map(n=>(n>>4).toString(16)+(n&0xF).toString(16)).join('');}
_getBytesAsUTF8String(array, start=0, end=array.length-start) {return new TextDecoder().decode(array.subarray(start, start+end));}
_getBlockType(number){
switch (number) {
case 0: return 'STREAMINFO';
case 1: return 'PADDING';
case 2: return 'APPLICATION';
case 3: return 'SEEKTABLE';
case 4: return 'VORBIS_COMMENT';
case 5: return 'CUESHEET';
case 6: return 'PICTURE';
case 127: return 'invalid, to avoid confusion with a frame sync code';
default: return 'reserved';
}
}
/* pack */
_uint32ToUint8Array(uint32) {
const eightBitMask = 0xff;
return [
(uint32 >>> 24) & eightBitMask,
(uint32 >>> 16) & eightBitMask,
(uint32 >>> 8) & eightBitMask,
uint32 & eightBitMask,
];
}
_uint24ToUint8Array(uint32) {
const eightBitMask = 0xff;
return [
(uint32 >>> 16) & eightBitMask,
(uint32 >>> 8) & eightBitMask,
uint32 & eightBitMask,
];
}
_uint16ToUint8Array(uint32) {
const eightBitMask = 0xff;
return [
(uint32 >>> 8) & eightBitMask,
uint32 & eightBitMask,
];
}
_hexStringToUint8Array(str) {
return str.replace(/(\w\w)/g,'$1,').slice(0,-1).split(',').map(s=> (parseInt(s[0],16)<<4) + parseInt(s[1],16));
}
get _vorbisComment() {
const block = this.metadata.blocks.find(block=>block.blockType==='VORBIS_COMMENT');
if (block)
return block.data;
}
addComment(field, value = null) {
if (field) {
if (!value) {
const splitted = field.split('=');
if (!splitted[1]) return this;
value = splitted[1];
field = splitted[0];
}
field = field.toUpperCase();
if (!this._vorbisComment.comments.hasOwnProperty(field))
this._vorbisComment.comments[field] = new VorbisComment();
if (!this._vorbisComment.comments[field].find(storedValue=> storedValue===value))
this._vorbisComment.comments[field].push(value.toString());
}
return this;
}
removeComment(field = null, value = null) {
if (!field) {
Object.keys(this._vorbisComment.comments).forEach(key=> delete this._vorbisComment.comments[key]);
} else {
field = field.toUpperCase();
if (!value) {
delete this._vorbisComment.comments[field];
} else {
value = value.toString();
if (this.hasOwnProperty(field))
this._vorbisComment.comments[field] = this._vorbisComment.comments[field].filter(storedValue=> storedValue!==value);
}
}
return this;
}
getComment(field) {
return this._vorbisComment.comments[field.toUpperCase()];
}
addPicture(dataInput) {
if (!dataInput.data || !dataInput.data || typeof dataInput.data !== 'object' || !('byteLength' in dataInput.data)) {
throw new Error('Field "data" should be an instance of ArrayBuffer or Buffer');
}
dataInput.data = new Uint8Array(dataInput.data);
const dataDefault = {
APICtype: 3,
MIMEType: 'image/jpeg',
colorDepth: 0,
colorNumber: 0,
data: new Uint8Array([]),
description: '',
width: 0,
height: 0,
};
const block = new FLACMetadataBlock();
block.blockTypeNubmer = 6;
block.blockType = 'PICTURE';
for (let property in dataDefault) {
if (dataInput[property]) {
block.data[property] = dataInput[property];
} else {
block.data[property] = dataDefault[property];
}
}
const bl = this.metadata.blocks;
let index = bl.length;
if (bl[bl.length-1].blockType === 'PADDING') index--;
bl.splice(index, 0, block);
this.metadata.blocks = bl;
return this;
}
_serializeMetadataBlock(block) {
const bytes = new Uint8Array(block.serializedSize);
const data = block.data;
let offset = 0;
switch (block.blockType) {
case 'STREAMINFO':
bytes.set(this._uint16ToUint8Array(data.minBlockSize));
offset += 2;
bytes.set(this._uint16ToUint8Array(data.maxBlockSize), offset);
offset += 2;
bytes.set(this._uint24ToUint8Array(data.minFrameSize), offset);
offset += 3;
bytes.set(this._uint24ToUint8Array(data.maxFrameSize), offset);
offset += 3;
bytes.set(this._uint24ToUint8Array((data.sampleRate<<4) + (data.numberOfChannels-1<<1) + (data.bitsPerSample-1>>4)), offset);
offset += 3;
bytes[offset] = ((data.bitsPerSample-1&0xF)<<4) + (Math.trunc(data.totalSamples/Math.pow(2,32))&0xF);
offset += 1;
bytes.set(this._uint32ToUint8Array(data.totalSamples), offset);
offset += 4;
bytes.set(this._hexStringToUint8Array(data.rawMD5), offset);
break;
case 'PADDING':
break;
case 'APPLICATION':
bytes.set(data.applicationID.toUTF8());
offset += 4;
bytes.set(data.applicationData, offset);
break;
case 'SEEKTABLE':
data.points.forEach(point=> {
bytes.set(this._hexStringToUint8Array(point.sampleNumber), offset);
bytes.set(this._hexStringToUint8Array(point.offset), offset+8);
bytes.set(this._hexStringToUint8Array(point.numberOfSamples), offset+16);
offset += 18;
});
break;
case 'VORBIS_COMMENT':
bytes.set(this._uint32ToUint8Array(data.vendorString.toUTF8().length).reverse(), offset);
offset += 4;
bytes.set(data.vendorString.toUTF8(), offset);
offset += data.vendorString.toUTF8().length;
const comments = data.comments.toStringArray();
bytes.set(this._uint32ToUint8Array(comments.length).reverse(), offset);
offset += 4;
comments.forEach(comment=> {
bytes.set(this._uint32ToUint8Array(comment.toUTF8().length).reverse(), offset);
offset += 4;
bytes.set(comment.toUTF8(), offset);
offset += comment.toUTF8().length;
});
break;
case 'CUESHEET':
break;
case 'PICTURE':
bytes.set(this._uint32ToUint8Array(data.APICtype));
offset += 4;
bytes.set(this._uint32ToUint8Array(data.MIMEType.toUTF8().length), offset);
offset += 4;
bytes.set(data.MIMEType.toUTF8(), offset);
offset += data.MIMEType.toUTF8().length;
bytes.set(this._uint32ToUint8Array(data.description.toUTF8().length), offset);
offset += 4;
bytes.set(data.description.toUTF8(), offset);
offset += data.description.toUTF8().length;
bytes.set(this._uint32ToUint8Array(data.width), offset);
offset += 4;
bytes.set(this._uint32ToUint8Array(data.height), offset);
offset += 4;
bytes.set(this._uint32ToUint8Array(data.colorDepth), offset);
offset += 4;
bytes.set(this._uint32ToUint8Array(data.colorNumber), offset);
offset += 4;
bytes.set(this._uint32ToUint8Array(data.data.length), offset);
offset += 4;
bytes.set(data.data, offset);
break;
}
return bytes;
}
serializeMetadata() {
const newMetadataLengthFull = 4+this.metadata.blocks.reduce((sum, block)=>sum+4+block.serializedSize, 0);
const newSize = newMetadataLengthFull + (this.arrayBuffer.byteLength>this.metadata.framesOffset ? this.arrayBuffer.byteLength-this.metadata.framesOffset : 0);
const bytes = new Uint8Array(newSize);
bytes.set(this.metadata.signature.toUTF8());
let offset = 4;
let lastBlock = false;
this.metadata.blocks.forEach((block, n, blocks)=>{
if (blocks.length-1 === n) lastBlock = true;
bytes[offset] = block.blockTypeNubmer | (lastBlock<<7);
offset += 1;
bytes.set(this._uint24ToUint8Array(block.serializedSize), offset);
offset += 3;
bytes.set(this._serializeMetadataBlock(block), offset);
offset += block.serializedSize;
});
// console.info('old meta size: %d, new: %d, delta: %d', this.metadata.framesOffset, newMetadataLengthFull, Math.abs(this.metadata.framesOffset-newMetadataLengthFull) );
// console.info('old size: %d, new: %d, delta: %d', this.arrayBuffer.byteLength, newSize, Math.abs(this.arrayBuffer.byteLength-newSize) );
// console.info('frames size: %d, to copy: %d', this.arrayBuffer.byteLength-this.metadata.framesOffset, new Uint8Array(this.arrayBuffer).subarray(this.metadata.framesOffset).length);
// console.info('offset: %d', offset );
bytes.set(new Uint8Array(this.arrayBuffer).subarray(this.metadata.framesOffset), offset);
this.arrayBuffer = bytes.buffer;
return this;
}
_parseMetadataBlock(array, arrayOffset, type, size) {
const blockData = array.subarray(arrayOffset, arrayOffset+size);
let offset = 0;
const data = new FLACMetadataBlockData();
switch (type) {
case 'STREAMINFO':
data.minBlockSize = this._getBytesAsNumber(blockData, offset, 2);
offset += 2;
data.maxBlockSize = this._getBytesAsNumber(blockData, offset, 2);
offset += 2;
data.minFrameSize = this._getBytesAsNumber(blockData, offset, 3);
offset += 3;
data.maxFrameSize = this._getBytesAsNumber(blockData, offset, 3);
offset += 3;
data.sampleRate = this._getBytesAsNumber(blockData, offset, 3)>>4;
offset += 2;
data.numberOfChannels = 1+ ((blockData[offset]>>1) &7);
data.bitsPerSample = 1+ ((1&blockData[offset]) <<4) + (blockData[offset+1]>>4);
offset += 1;
data.totalSamples = (blockData[offset]&0xF)*Math.pow(2,32) + this._getBytesAsNumber(blockData, offset+1, 4);
offset += 5;
data.rawMD5 = this._getBytesAsHexString(blockData, offset, 16).toUpperCase();
break;
case 'PADDING':
break;
case 'APPLICATION':
data.applicationID = this._getBytesAsUTF8String(blockData, offset, 4);
offset += 4;
data.applicationData = blockData.subarray(offset);
break;
case 'SEEKTABLE':
data.pointCount = size/18;
data.points = [];
for (let i=0; i<data.pointCount; i++) {
data.points.push({
sampleNumber: this._getBytesAsHexString(blockData, offset, 8),
offset: this._getBytesAsHexString(blockData, offset+8, 8),
numberOfSamples: this._getBytesAsHexString(blockData, offset+16, 2),
});
offset += 18;
}
break;
case 'VORBIS_COMMENT':
const vendorLength = this._getBytesAsNumberLittleEndian(blockData, offset, 4);
offset += 4;
data.vendorString = this._getBytesAsUTF8String(blockData, offset, vendorLength);
offset += vendorLength;
const userCommentListLength = this._getBytesAsNumberLittleEndian(blockData, offset, 4);
offset += 4;
data.comments = new VorbisCommentPacket();
let commentLength = 0;
for (let i=0; i<userCommentListLength; i++) {
commentLength = this._getBytesAsNumberLittleEndian(blockData, offset, 4);
offset += 4;
data.comments._addComment(this._getBytesAsUTF8String(blockData, offset, commentLength));
offset += commentLength;
}
break;
case 'CUESHEET':
break;
case 'PICTURE':
data.APICtype = this._getBytesAsNumber(blockData, offset, 4);
offset += 4;
const MIMELength = this._getBytesAsNumber(blockData, offset, 4);
offset += 4;
data.MIMEType = this._getBytesAsUTF8String(blockData, offset, MIMELength);
offset += MIMELength;
const descriptionLength = this._getBytesAsNumber(blockData, offset, 4);
offset += 4;
data.description = this._getBytesAsUTF8String(blockData, offset, descriptionLength);
offset += descriptionLength;
data.width = this._getBytesAsNumber(blockData, offset, 4);
offset += 4;
data.height = this._getBytesAsNumber(blockData, offset, 4);
offset += 4;
data.colorDepth = this._getBytesAsNumber(blockData, offset, 4);
offset += 4;
data.colorNumber = this._getBytesAsNumber(blockData, offset, 4);
offset += 4;
const binarySize = this._getBytesAsNumber(blockData, offset, 4);
offset += 4;
data.data = blockData.subarray(offset, offset+binarySize);
break;
}
return data;
}
_parseMetadata() {
const bytes = new Uint8Array(this.arrayBuffer);
this.metadata.signature = this._getBytesAsUTF8String(bytes,0,4);
let offset = 4;
let lastBlock = false;
let block;
let iteration = 0;
while (!lastBlock && offset < bytes.length) {
if (iteration++ > 42) throw new RangeError('Too much METADATA_BLOCKS. Looks like file corrupted');
block = new FLACMetadataBlock();
block.offset = offset;
lastBlock = !!(bytes[offset] >> 7);
block.blockTypeNubmer = bytes[offset] & 127;
block.blockType = this._getBlockType(block.blockTypeNubmer);
offset += 1;
block.blockSize = this._getBytesAsNumber(bytes, offset, 3);
offset += 3;
block.data = this._parseMetadataBlock(bytes, offset, block.blockType, block.blockSize);
offset += block.blockSize;
// if (block.blockType !== 'PADDING')
this.metadata.blocks.push(block);
}
this.metadata.framesOffset = offset;
return this;
}
}
return _FLACMetadataEditor;
})();