Fix #77 ncm flac meta duplicated

Fix #78 write flac cover sometimes fail
This commit is contained in:
MengYX 2020-08-01 01:10:27 +08:00
parent c9a4a901be
commit 4aa2ff7f91
No known key found for this signature in database
GPG Key ID: E63F9C7303E8F604
3 changed files with 38 additions and 30 deletions

View File

@ -58,6 +58,6 @@ export async function CommonDecrypt(file) {
if (!rt_data.rawExt) rt_data.rawExt = raw_ext; if (!rt_data.rawExt) rt_data.rawExt = raw_ext;
if (!rt_data.rawFilename) rt_data.rawFilename = raw_filename; if (!rt_data.rawFilename) rt_data.rawFilename = raw_filename;
console.log(rt_data);
return rt_data; return rt_data;
} }

View File

@ -39,25 +39,30 @@ export async function Decrypt(file, raw_filename, raw_ext) {
if (artists.length === 0) artists.push(info.artist); if (artists.length === 0) artists.push(info.artist);
if (musicMeta.format === undefined) musicMeta.format = DetectAudioExt(audioData, "mp3"); if (musicMeta.format === undefined) musicMeta.format = DetectAudioExt(audioData, "mp3");
console.log(musicMeta)
const imageInfo = await GetWebImage(musicMeta.albumPic); const imageInfo = await GetWebImage(musicMeta.albumPic);
console.log(imageInfo)
try {
if (musicMeta.format === "mp3") { if (musicMeta.format === "mp3") {
audioData = await WriteMp3Meta( audioData = await WriteMp3Meta(
audioData, artists, info.title, musicMeta.album, imageInfo.buffer, musicMeta.albumPic); audioData, artists, info.title, musicMeta.album, imageInfo.buffer, musicMeta.albumPic);
} else if (musicMeta.format === "flac") { } else if (musicMeta.format === "flac") {
const writer = new MetaFlac(Buffer.from(audioData)) const writer = new MetaFlac(Buffer.from(audioData))
writer.setTag("TITLE=" + info.title); //writer.setTag("TITLE=" + info.title);
writer.setTag("ALBUM=" + musicMeta.album); //writer.setTag("ALBUM=" + musicMeta.album);
artists.forEach(artist => writer.setTag("ARTIST=" + artist)); //artists.forEach(artist => writer.setTag("ARTIST=" + artist));
writer.importPictureFromBuffer(Buffer.from(imageInfo.buffer)) writer.importPictureFromBuffer(Buffer.from(imageInfo.buffer))
audioData = writer.save() audioData = writer.save()
} }
console.log(imageInfo) } catch (e) {
console.warn("Error while appending cover image to file " + e)
}
const mime = AudioMimeType[musicMeta.format]; const mime = AudioMimeType[musicMeta.format];
const musicData = new Blob([audioData], {type: mime}); const musicData = new Blob([audioData], {type: mime});
let x = { return {
status: true, status: true,
title: info.title, title: info.title,
artist: info.artist, artist: info.artist,
@ -67,8 +72,6 @@ export async function Decrypt(file, raw_filename, raw_ext) {
file: URL.createObjectURL(musicData), file: URL.createObjectURL(musicData),
mime: mime mime: mime
}; };
console.log(x)
return x;
} }

View File

@ -60,6 +60,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
const musicMeta = await musicMetadata.parseBlob(musicBlob); const musicMeta = await musicMetadata.parseBlob(musicBlob);
for (let metaIdx in musicMeta.native) { for (let metaIdx in musicMeta.native) {
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.log("The metadata is using gbk encoding")
musicMeta.common.artist = decode(musicMeta.common.artist, "gbk"); musicMeta.common.artist = decode(musicMeta.common.artist, "gbk");
musicMeta.common.title = decode(musicMeta.common.title, "gbk"); musicMeta.common.title = decode(musicMeta.common.title, "gbk");
musicMeta.common.album = decode(musicMeta.common.album, "gbk"); musicMeta.common.album = decode(musicMeta.common.album, "gbk");
@ -77,6 +78,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
const imageInfo = await GetWebImage(imgUrl); const imageInfo = await GetWebImage(imgUrl);
if (imageInfo.url !== "") { if (imageInfo.url !== "") {
imgUrl = imageInfo.url imgUrl = imageInfo.url
try {
if (ext === "mp3") { if (ext === "mp3") {
let writer = new ID3Writer(musicDecoded) let writer = new ID3Writer(musicDecoded)
writer.setFrame('APIC', { writer.setFrame('APIC', {
@ -93,6 +95,9 @@ export async function Decrypt(file, raw_filename, raw_ext) {
musicDecoded = writer.save() musicDecoded = writer.save()
musicBlob = new Blob([musicDecoded], {type: mime}); musicBlob = new Blob([musicDecoded], {type: mime});
} }
} catch (e) {
console.warn("Error while appending cover image to file " + e)
}
} }
} }
} }