From 4aa2ff7f91f7d46ede098099eb6dddd64ff32308 Mon Sep 17 00:00:00 2001 From: MengYX Date: Sat, 1 Aug 2020 01:10:27 +0800 Subject: [PATCH] Fix #77 ncm flac meta duplicated Fix #78 write flac cover sometimes fail --- src/decrypt/common.js | 2 +- src/decrypt/ncm.js | 31 +++++++++++++++++-------------- src/decrypt/qmc.js | 35 ++++++++++++++++++++--------------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/decrypt/common.js b/src/decrypt/common.js index 8c3e051..e8e88e7 100644 --- a/src/decrypt/common.js +++ b/src/decrypt/common.js @@ -58,6 +58,6 @@ export async function CommonDecrypt(file) { if (!rt_data.rawExt) rt_data.rawExt = raw_ext; if (!rt_data.rawFilename) rt_data.rawFilename = raw_filename; - + console.log(rt_data); return rt_data; } diff --git a/src/decrypt/ncm.js b/src/decrypt/ncm.js index c568e73..b6c0d98 100644 --- a/src/decrypt/ncm.js +++ b/src/decrypt/ncm.js @@ -39,25 +39,30 @@ export async function Decrypt(file, raw_filename, raw_ext) { if (artists.length === 0) artists.push(info.artist); if (musicMeta.format === undefined) musicMeta.format = DetectAudioExt(audioData, "mp3"); + console.log(musicMeta) const imageInfo = await GetWebImage(musicMeta.albumPic); - if (musicMeta.format === "mp3") { - audioData = await WriteMp3Meta( - audioData, artists, info.title, musicMeta.album, imageInfo.buffer, musicMeta.albumPic); - } else if (musicMeta.format === "flac") { - const writer = new MetaFlac(Buffer.from(audioData)) - writer.setTag("TITLE=" + info.title); - writer.setTag("ALBUM=" + musicMeta.album); - artists.forEach(artist => writer.setTag("ARTIST=" + artist)); - writer.importPictureFromBuffer(Buffer.from(imageInfo.buffer)) - audioData = writer.save() - } console.log(imageInfo) + try { + if (musicMeta.format === "mp3") { + audioData = await WriteMp3Meta( + audioData, artists, info.title, musicMeta.album, imageInfo.buffer, musicMeta.albumPic); + } else if (musicMeta.format === "flac") { + const writer = new MetaFlac(Buffer.from(audioData)) + //writer.setTag("TITLE=" + info.title); + //writer.setTag("ALBUM=" + musicMeta.album); + //artists.forEach(artist => writer.setTag("ARTIST=" + artist)); + writer.importPictureFromBuffer(Buffer.from(imageInfo.buffer)) + audioData = writer.save() + } + } catch (e) { + console.warn("Error while appending cover image to file " + e) + } const mime = AudioMimeType[musicMeta.format]; const musicData = new Blob([audioData], {type: mime}); - let x = { + return { status: true, title: info.title, artist: info.artist, @@ -67,8 +72,6 @@ export async function Decrypt(file, raw_filename, raw_ext) { file: URL.createObjectURL(musicData), mime: mime }; - console.log(x) - return x; } diff --git a/src/decrypt/qmc.js b/src/decrypt/qmc.js index 9b37c5a..104e5f3 100644 --- a/src/decrypt/qmc.js +++ b/src/decrypt/qmc.js @@ -60,6 +60,7 @@ export async function Decrypt(file, raw_filename, raw_ext) { const musicMeta = await musicMetadata.parseBlob(musicBlob); for (let metaIdx in musicMeta.native) { if (musicMeta.native[metaIdx].some(item => item.id === "TCON" && item.value === "(12)")) { + console.log("The metadata is using gbk encoding") musicMeta.common.artist = decode(musicMeta.common.artist, "gbk"); musicMeta.common.title = decode(musicMeta.common.title, "gbk"); musicMeta.common.album = decode(musicMeta.common.album, "gbk"); @@ -77,21 +78,25 @@ export async function Decrypt(file, raw_filename, raw_ext) { const imageInfo = await GetWebImage(imgUrl); if (imageInfo.url !== "") { imgUrl = imageInfo.url - if (ext === "mp3") { - let writer = new ID3Writer(musicDecoded) - writer.setFrame('APIC', { - type: 3, - data: imageInfo.buffer, - description: "Cover", - }) - writer.addTag(); - musicDecoded = writer.arrayBuffer - musicBlob = new Blob([musicDecoded], {type: mime}); - } else if (ext === 'flac') { - const writer = new MetaFlac(Buffer.from(musicDecoded)) - writer.importPictureFromBuffer(Buffer.from(imageInfo.buffer)) - musicDecoded = writer.save() - musicBlob = new Blob([musicDecoded], {type: mime}); + try { + if (ext === "mp3") { + let writer = new ID3Writer(musicDecoded) + writer.setFrame('APIC', { + type: 3, + data: imageInfo.buffer, + description: "Cover", + }) + writer.addTag(); + musicDecoded = writer.arrayBuffer + musicBlob = new Blob([musicDecoded], {type: mime}); + } else if (ext === 'flac') { + const writer = new MetaFlac(Buffer.from(musicDecoded)) + writer.importPictureFromBuffer(Buffer.from(imageInfo.buffer)) + musicDecoded = writer.save() + musicBlob = new Blob([musicDecoded], {type: mime}); + } + } catch (e) { + console.warn("Error while appending cover image to file " + e) } } }