修正代码中的细节问题

This commit is contained in:
xhacker-zzz 2022-11-21 00:33:03 +08:00
parent 5d1d56e587
commit 5c21f459c6
6 changed files with 12 additions and 10 deletions

View File

@ -34,12 +34,11 @@ export async function Decrypt(file: File, raw_filename: string, raw_ext: string)
console.log('kgm: using wasm decoder'); console.log('kgm: using wasm decoder');
const kgmDecrypted = await DecryptKgmWasm(oriData, raw_ext); const kgmDecrypted = await DecryptKgmWasm(oriData, raw_ext);
// 若 v2 检测失败,降级到 v1 再尝试一次
if (kgmDecrypted.success) { if (kgmDecrypted.success) {
musicDecoded = kgmDecrypted.data; musicDecoded = kgmDecrypted.data;
console.log('kgm wasm decoder suceeded'); console.log('kgm wasm decoder suceeded');
} else { } else {
console.warn('KgmWasm failed with error %s', kgmDecrypted.error || '(no error)'); console.warn('KgmWasm failed with error %s', kgmDecrypted.error || '(unknown error)');
} }
} }

View File

@ -54,7 +54,7 @@ export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string)
throw '不支持的QQ音乐缓存格式'; throw '不支持的QQ音乐缓存格式';
} }
const tag = await metaParseBlob(audioBlob); const tag = await metaParseBlob(audioBlob);
const { title, artist } = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artists == undefined ? tag.common.artist : tag.common.artists.toString()); const { title, artist } = GetMetaFromFile(raw_filename, tag.common.title, String(tag.common.artists || tag.common.artist));
return { return {
title, title,

View File

@ -17,7 +17,7 @@ export async function Decrypt(
if (ext !== raw_ext) file = new Blob([buffer], { type: AudioMimeType[ext] }); if (ext !== raw_ext) file = new Blob([buffer], { type: AudioMimeType[ext] });
} }
const tag = await metaParseBlob(file); const tag = await metaParseBlob(file);
const { title, artist } = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artists == undefined ? tag.common.artist : tag.common.artists.toString()); const { title, artist } = GetMetaFromFile(raw_filename, tag.common.title, String(tag.common.artists || tag.common.artist));
return { return {
title, title,

View File

@ -93,6 +93,7 @@ export function GetMetaFromFile(
const items = filename.split(separator); const items = filename.split(separator);
if (items.length > 1) { if (items.length > 1) {
//由文件名和原metadata共同决定歌手tag(有时从文件名看有多个歌手而metadata只有一个)
if (!meta.artist || meta.artist.split(split_regex).length < items[0].trim().split(split_regex).length) meta.artist = items[0].trim(); if (!meta.artist || meta.artist.split(split_regex).length < items[0].trim().split(split_regex).length) meta.artist = items[0].trim();
if (!meta.title) meta.title = items[1].trim(); if (!meta.title) meta.title = items[1].trim();
} else if (items.length === 1) { } else if (items.length === 1) {
@ -136,7 +137,9 @@ export function WriteMetaToMp3(audioData: Buffer, info: IMusicMeta, original: IA
if (frame.id !== 'TPE1' && frame.id !== 'TIT2' && frame.id !== 'TALB') { if (frame.id !== 'TPE1' && frame.id !== 'TIT2' && frame.id !== 'TALB') {
try { try {
writer.setFrame(frame.id, frame.value); writer.setFrame(frame.id, frame.value);
} catch (e) {} } catch (e) {
console.warn(`failed to write ID3 tag '${frame.id}'`);
}
} }
}); });
@ -176,7 +179,7 @@ export function WriteMetaToFlac(audioData: Buffer, info: IMusicMeta, original: I
export function RewriteMetaToMp3(audioData: Buffer, info: IMusicMeta, original: IAudioMetadata) { export function RewriteMetaToMp3(audioData: Buffer, info: IMusicMeta, original: IAudioMetadata) {
const writer = new ID3Writer(audioData); const writer = new ID3Writer(audioData);
// reserve original data // preserve original data
const frames = original.native['ID3v2.4'] || original.native['ID3v2.3'] || original.native['ID3v2.2'] || []; const frames = original.native['ID3v2.4'] || original.native['ID3v2.3'] || original.native['ID3v2.2'] || [];
frames.forEach((frame) => { frames.forEach((frame) => {
if (frame.id !== 'TPE1' if (frame.id !== 'TPE1'
@ -188,7 +191,7 @@ export function RewriteMetaToMp3(audioData: Buffer, info: IMusicMeta, original:
try { try {
writer.setFrame(frame.id, frame.value); writer.setFrame(frame.id, frame.value);
} catch (e) { } catch (e) {
throw new Error('write unknown mp3 frame failed'); throw new Error(`failed to write ID3 tag '${frame.id}'`);
} }
} }
}); });

View File

@ -49,7 +49,7 @@ export async function Decrypt(file: File, raw_filename: string, raw_ext: string)
const { title, artist } = GetMetaFromFile( const { title, artist } = GetMetaFromFile(
raw_filename, raw_filename,
musicMeta.common.title, musicMeta.common.title,
musicMeta.common.artists == undefined ? musicMeta.common.artist : musicMeta.common.artists.toString(), String(musicMeta.common.artists || musicMeta.common.artist),
raw_filename.indexOf('_') === -1 ? '-' : '_', raw_filename.indexOf('_') === -1 ? '-' : '_',
); );

View File

@ -40,7 +40,7 @@ export async function extractQQMusicMeta(
if (musicMeta.native[metaIdx].some((item) => item.id === 'TCON' && item.value === '(12)')) { if (musicMeta.native[metaIdx].some((item) => item.id === 'TCON' && item.value === '(12)')) {
console.warn('try using gbk encoding to decode meta'); console.warn('try using gbk encoding to decode meta');
musicMeta.common.artist = ''; musicMeta.common.artist = '';
if (musicMeta.common.artists == undefined) { if (musicMeta.common.artists) {
musicMeta.common.artist = iconv.decode(new Buffer(musicMeta.common.artist ?? ''), 'gbk'); musicMeta.common.artist = iconv.decode(new Buffer(musicMeta.common.artist ?? ''), 'gbk');
} }
else { else {
@ -70,7 +70,7 @@ export async function extractQQMusicMeta(
return { return {
title: info.title, title: info.title,
artist: info.artist || '', artist: info.artist,
album: musicMeta.common.album || '', album: musicMeta.common.album || '',
imgUrl: imageURL, imgUrl: imageURL,
blob: await writeMetaToAudioFile({ blob: await writeMetaToAudioFile({