Skip to content

Commit 3c1a0b2

Browse files
committed
fix: utf-16 encoded ID3 tags supported. Fixed mp3 magic-byte grep logic to skip ID3 data block.
1 parent 28dc2e0 commit 3c1a0b2

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/main/wf/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,7 @@ class WfExtractor {
29992999
'.esdl.amf3',
30003000
'.esdl.json',
30013001
'.esdl',
3002+
'.mp3',
30023003
...customFormats,
30033004
...COMMON_FILE_FORMAT,
30043005
]) {
@@ -3011,9 +3012,11 @@ class WfExtractor {
30113012

30123013
if (found) {
30133014
logger.log(`Asset found at ${curPath}`);
3015+
logger.log(`${found[0]} ${found[1]}`);
30143016
}
30153017
if (digestFound) {
30163018
logger.log(`Asset found at ${curDigestPath}`);
3019+
logger.log(`${digestFound[0]} ${digestFound[1]}`);
30173020
}
30183021

30193022
if (found || digestFound) {

src/main/wf/restoreMp3.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ const spliceBuffer = (buffer, targetIndex, deleteCount, ...items) => {
195195

196196
const restoreCorruptedMp3 = async (filePath, debug) => {
197197
let file = await readFile(filePath);
198+
let modifiedTit2BlockLastByteIndex = 0;
198199

199200
const hexString = file.toString('hex');
200201
if (hexString.slice(0, 6) === '494433') {
@@ -207,12 +208,24 @@ const restoreCorruptedMp3 = async (filePath, debug) => {
207208
hexString.slice(tit2Index + 8, tit2Index + 16),
208209
16
209210
);
210-
const shiftJisTitle = file.slice(
211+
const rawTit2Title = file.slice(
211212
tit2Index / 2 + 11,
212213
tit2Index / 2 + 11 + tit2Size - 1
213214
);
214-
const decodedTitle = iconv.decode(shiftJisTitle, 'SHIFT_JIS');
215+
216+
let encoding = 'SHIFT_JIS';
217+
218+
if (
219+
file.slice(tit2Index / 2 + 10, tit2Index / 2 + 11).toString('hex') ===
220+
'01'
221+
) {
222+
console.log('UTF-16 encoded tit2 found');
223+
encoding = 'utf-16';
224+
}
225+
226+
const decodedTitle = iconv.decode(rawTit2Title, encoding);
215227
const utf8Title = iconv.encode(decodedTitle, 'utf-16');
228+
216229
file[encodingByteIndex] = 1;
217230
utf8Title.length
218231
.toString(16)
@@ -226,18 +239,24 @@ const restoreCorruptedMp3 = async (filePath, debug) => {
226239
try {
227240
if (tit2Size > 1024) throw Error('TOO LARGE TIT2');
228241
file = spliceBuffer(file, tit2Index / 2 + 11, tit2Size, ...utf8Title);
242+
modifiedTit2BlockLastByteIndex =
243+
tit2Index / 2 + 11 + utf8Title.length - tit2Size;
229244
} catch (err) {
230245
console.log(debug, tit2Index, tit2Size);
231246
}
232247
}
233248
}
234249

235-
let byteIndex = file.toString('hex').indexOf('7ffb') / 2;
250+
let byteIndex =
251+
file.toString('hex').indexOf('7ffb', modifiedTit2BlockLastByteIndex * 2) /
252+
2;
236253

237254
while (byteIndex < file.byteLength) {
238255
const header = file.slice(byteIndex, byteIndex + 4).toString('hex');
239256
const binaryHeader = hexesToBin(header);
240257
if (!/^7f/.test(header)) {
258+
console.log(byteIndex);
259+
console.log('Wrong header', header);
241260
break;
242261
}
243262
file[byteIndex] = 255;

0 commit comments

Comments
 (0)