Fix errors when parsing ncm files without metadata

This commit is contained in:
MengYX 2020-03-10 09:31:48 +08:00
parent 97ca09dbae
commit 24e1d33642
No known key found for this signature in database
GPG Key ID: E63F9C7303E8F604
3 changed files with 16 additions and 17 deletions

View File

@ -140,8 +140,8 @@
}); });
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
window._paq.push(["trackEvent", "Error", errInfo, filename]); window._paq.push(["trackEvent", "Error", errInfo, filename]);
console.error(errInfo, filename);
} }
console.error(errInfo, filename);
}, },
changePlaying(url) { changePlaying(url) {
this.playing_url = url; this.playing_url = url;

View File

@ -9,7 +9,7 @@ export async function CommonDecrypt(file) {
let rt_data; let rt_data;
switch (raw_ext) { switch (raw_ext) {
case "ncm":// Netease Mp3/Flac case "ncm":// Netease Mp3/Flac
rt_data = await NcmDecrypt.Decrypt(file.raw); rt_data = await NcmDecrypt.Decrypt(file.raw, raw_filename, raw_ext);
break; break;
case "mp3":// Raw Mp3 case "mp3":// Raw Mp3
case "flac"://Raw Flac case "flac"://Raw Flac

View File

@ -2,10 +2,9 @@ const CryptoJS = require("crypto-js");
const ID3Writer = require("browser-id3-writer"); const ID3Writer = require("browser-id3-writer");
const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857"); const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857");
const META_KEY = CryptoJS.enc.Hex.parse("2331346C6A6B5F215C5D2630553C2728"); const META_KEY = CryptoJS.enc.Hex.parse("2331346C6A6B5F215C5D2630553C2728");
import {AudioMimeType, DetectAudioExt, GetArrayBuffer} from "./util" import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetFileInfo} from "./util"
export async function Decrypt(file) {
export async function Decrypt(file, raw_filename, raw_ext) {
const fileBuffer = await GetArrayBuffer(file); const fileBuffer = await GetArrayBuffer(file);
const dataView = new DataView(fileBuffer); const dataView = new DataView(fileBuffer);
@ -13,32 +12,34 @@ export async function Decrypt(file) {
dataView.getUint32(4, true) !== 0x4d414446) dataView.getUint32(4, true) !== 0x4d414446)
return {status: false, message: "此ncm文件已损坏"}; return {status: false, message: "此ncm文件已损坏"};
const keyDataObj = getKeyData(dataView, fileBuffer, 10); const keyDataObj = getKeyData(dataView, fileBuffer, 10);
const keyBox = getKeyBox(keyDataObj.data); const keyBox = getKeyBox(keyDataObj.data);
const musicMetaObj = getMetaData(dataView, fileBuffer, keyDataObj.offset); const musicMetaObj = getMetaData(dataView, fileBuffer, keyDataObj.offset);
const musicMeta = musicMetaObj.data; const musicMeta = musicMetaObj.data;
let audioOffset = musicMetaObj.offset + dataView.getUint32(musicMetaObj.offset + 5, true) + 13; let audioOffset = musicMetaObj.offset + dataView.getUint32(musicMetaObj.offset + 5, true) + 13;
let audioData = new Uint8Array(fileBuffer, audioOffset); let audioData = new Uint8Array(fileBuffer, audioOffset);
for (let cur = 0; cur < audioData.length; ++cur) audioData[cur] ^= keyBox[cur & 0xff]; 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 = []; const artists = [];
musicMeta.artist.forEach(arr => artists.push(arr[0])); if (!!musicMeta.artist) musicMeta.artist.forEach(arr => artists.push(arr[0]));
if (musicMeta.format === "mp3") const info = GetFileInfo(artists.join(" & "), musicMeta.musicName, raw_filename);
audioData = await writeID3(audioData, artists, musicMeta.musicName, musicMeta.album, musicMeta.albumPic); 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, info.title, musicMeta.album, musicMeta.albumPic);
const mime = AudioMimeType[musicMeta.format];
const musicData = new Blob([audioData], {type: mime}); const musicData = new Blob([audioData], {type: mime});
return { return {
status: true, status: true,
title: musicMeta.musicName, title: info.title,
artist: artists.join(" & "), artist: info.artist,
ext: musicMeta.format, ext: musicMeta.format,
album: musicMeta.album, album: musicMeta.album,
picture: musicMeta.albumPic, picture: musicMeta.albumPic,
@ -129,9 +130,7 @@ function getKeyBox(keyData) {
function getMetaData(dataView, fileBuffer, offset) { function getMetaData(dataView, fileBuffer, offset) {
const metaDataLen = dataView.getUint32(offset, true); const metaDataLen = dataView.getUint32(offset, true);
offset += 4; offset += 4;
if (metaDataLen === 0) { if (metaDataLen === 0) return {data: {}, offset: offset};
return {};
}
const cipherText = new Uint8Array(fileBuffer, offset, metaDataLen).map( const cipherText = new Uint8Array(fileBuffer, offset, metaDataLen).map(
data => data ^ 0x63 data => data ^ 0x63