From 6a2b98798bf09fd48dcb4fdf624e7f0ba49b6677 Mon Sep 17 00:00:00 2001 From: MengYX Date: Sat, 7 Nov 2020 01:12:04 +0800 Subject: [PATCH] Fix #103 #100 duplicated metadata --- src/decrypt/ncm.js | 22 ++++++++++++++-------- src/decrypt/util.js | 7 +++---- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/decrypt/ncm.js b/src/decrypt/ncm.js index 9d43863..0ccbb21 100644 --- a/src/decrypt/ncm.js +++ b/src/decrypt/ncm.js @@ -3,6 +3,7 @@ const MetaFlac = require('metaflac-js'); const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857"); const META_KEY = CryptoJS.enc.Hex.parse("2331346C6A6B5F215C5D2630553C2728"); const MagicHeader = [0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D]; +const musicMetadata = require("music-metadata-browser"); import jimp from 'jimp'; import { @@ -50,16 +51,22 @@ export async function Decrypt(file, raw_filename, raw_ext) { imageInfo.buffer = await img.getBufferAsync("image/jpeg") } console.log(imageInfo) + const mime = AudioMimeType[musicMeta.format] try { + let musicBlob = new Blob([audioData], {type: mime}); + const originalMeta = await musicMetadata.parseBlob(musicBlob); + let shouldWrite = !originalMeta.common.album && !originalMeta.common.artists && !originalMeta.common.title if (musicMeta.format === "mp3") { audioData = await WriteMp3Meta( - audioData, artists, info.title, musicMeta.album, imageInfo.buffer, musicMeta.albumPic); + audioData, artists, info.title, musicMeta.album, imageInfo.buffer, musicMeta.albumPic, !shouldWrite) } else if (musicMeta.format === "flac") { const writer = new MetaFlac(Buffer.from(audioData)) - if (writer.getTag("TITLE") === "") writer.setTag("TITLE=" + info.title) - if (writer.getTag("ALBUM") === "") writer.setTag("ALBUM=" + musicMeta.album) - writer.removeTag("ARTIST") - artists.forEach(artist => writer.setTag("ARTIST=" + artist)) + if (shouldWrite) { + writer.setTag("TITLE=" + info.title) + writer.setTag("ALBUM=" + musicMeta.album) + writer.removeTag("ARTIST") + artists.forEach(artist => writer.setTag("ARTIST=" + artist)) + } writer.importPictureFromBuffer(Buffer.from(imageInfo.buffer)) audioData = writer.save() } @@ -67,8 +74,7 @@ export async function Decrypt(file, raw_filename, raw_ext) { console.warn("Error while appending cover image to file " + e) } - const mime = AudioMimeType[musicMeta.format]; - const musicData = new Blob([audioData], {type: mime}); + const musicData = new Blob([audioData], {type: mime}) return { status: true, @@ -79,7 +85,7 @@ export async function Decrypt(file, raw_filename, raw_ext) { picture: imageInfo.url, file: URL.createObjectURL(musicData), mime: mime - }; + } } diff --git a/src/decrypt/util.js b/src/decrypt/util.js index 8d3875c..2e4cdf1 100644 --- a/src/decrypt/util.js +++ b/src/decrypt/util.js @@ -1,4 +1,5 @@ const ID3Writer = require("browser-id3-writer"); +const musicMetadata = require("music-metadata-browser"); export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43]; export const MP3_HEADER = [0x49, 0x44, 0x33]; export const OGG_HEADER = [0x4F, 0x67, 0x67, 0x53]; @@ -92,11 +93,9 @@ export async function GetWebImage(pic_url) { return {"buffer": null, "src": pic_url, "url": "", "type": ""} } -export function WriteMp3Meta(audioData, artistList, title, album, pictureData = null, pictureDesc = "Cover") { +export async function WriteMp3Meta(audioData, artistList, title, album, pictureData = null, pictureDesc = "Cover", cover_only = true) { const writer = new ID3Writer(audioData); - writer.setFrame("TPE1", artistList) - .setFrame("TIT2", title) - .setFrame("TALB", album); + if (!cover_only) writer.setFrame("TPE1", artistList).setFrame("TIT2", title).setFrame("TALB", album); if (pictureData !== null) { writer.setFrame('APIC', { type: 3,