From 24e1d33642861b03ece91cd5276be35eca94183c Mon Sep 17 00:00:00 2001 From: MengYX Date: Tue, 10 Mar 2020 09:31:48 +0800 Subject: [PATCH] Fix errors when parsing ncm files without metadata --- src/App.vue | 2 +- src/decrypt/common.js | 2 +- src/decrypt/ncm.js | 27 +++++++++++++-------------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/App.vue b/src/App.vue index 1cb5ffb..4fa9b3b 100644 --- a/src/App.vue +++ b/src/App.vue @@ -140,8 +140,8 @@ }); if (process.env.NODE_ENV === 'production') { window._paq.push(["trackEvent", "Error", errInfo, filename]); - console.error(errInfo, filename); } + console.error(errInfo, filename); }, changePlaying(url) { this.playing_url = url; diff --git a/src/decrypt/common.js b/src/decrypt/common.js index cef10f9..04d7037 100644 --- a/src/decrypt/common.js +++ b/src/decrypt/common.js @@ -9,7 +9,7 @@ export async function CommonDecrypt(file) { let rt_data; switch (raw_ext) { case "ncm":// Netease Mp3/Flac - rt_data = await NcmDecrypt.Decrypt(file.raw); + rt_data = await NcmDecrypt.Decrypt(file.raw, raw_filename, raw_ext); break; case "mp3":// Raw Mp3 case "flac"://Raw Flac diff --git a/src/decrypt/ncm.js b/src/decrypt/ncm.js index 10c2a16..245d98b 100644 --- a/src/decrypt/ncm.js +++ b/src/decrypt/ncm.js @@ -2,10 +2,9 @@ const CryptoJS = require("crypto-js"); const ID3Writer = require("browser-id3-writer"); const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857"); const META_KEY = CryptoJS.enc.Hex.parse("2331346C6A6B5F215C5D2630553C2728"); -import {AudioMimeType, DetectAudioExt, GetArrayBuffer} from "./util" - -export async function Decrypt(file) { +import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetFileInfo} from "./util" +export async function Decrypt(file, raw_filename, raw_ext) { const fileBuffer = await GetArrayBuffer(file); const dataView = new DataView(fileBuffer); @@ -13,32 +12,34 @@ export async function Decrypt(file) { dataView.getUint32(4, true) !== 0x4d414446) return {status: false, message: "此ncm文件已损坏"}; - const keyDataObj = getKeyData(dataView, fileBuffer, 10); const keyBox = getKeyBox(keyDataObj.data); const musicMetaObj = getMetaData(dataView, fileBuffer, keyDataObj.offset); const musicMeta = musicMetaObj.data; - let audioOffset = musicMetaObj.offset + dataView.getUint32(musicMetaObj.offset + 5, true) + 13; let audioData = new Uint8Array(fileBuffer, audioOffset); for (let cur = 0; cur < audioData.length; ++cur) audioData[cur] ^= keyBox[cur & 0xff]; - if (musicMeta.format === undefined) musicMeta.format = DetectAudioExt(audioData, "mp3"); - const mime = AudioMimeType[musicMeta.format]; + if (musicMeta.album === undefined) musicMeta.album = ""; const artists = []; - musicMeta.artist.forEach(arr => artists.push(arr[0])); + if (!!musicMeta.artist) musicMeta.artist.forEach(arr => artists.push(arr[0])); + const info = GetFileInfo(artists.join(" & "), musicMeta.musicName, raw_filename); + if (artists.length === 0) artists.push(info.artist); + + if (musicMeta.format === undefined) musicMeta.format = DetectAudioExt(audioData, "mp3"); if (musicMeta.format === "mp3") - audioData = await writeID3(audioData, artists, musicMeta.musicName, musicMeta.album, musicMeta.albumPic); + audioData = await writeID3(audioData, artists, info.title, musicMeta.album, musicMeta.albumPic); + const mime = AudioMimeType[musicMeta.format]; const musicData = new Blob([audioData], {type: mime}); return { status: true, - title: musicMeta.musicName, - artist: artists.join(" & "), + title: info.title, + artist: info.artist, ext: musicMeta.format, album: musicMeta.album, picture: musicMeta.albumPic, @@ -129,9 +130,7 @@ function getKeyBox(keyData) { function getMetaData(dataView, fileBuffer, offset) { const metaDataLen = dataView.getUint32(offset, true); offset += 4; - if (metaDataLen === 0) { - return {}; - } + if (metaDataLen === 0) return {data: {}, offset: offset}; const cipherText = new Uint8Array(fileBuffer, offset, metaDataLen).map( data => data ^ 0x63