Fix #103 #100 duplicated metadata

This commit is contained in:
MengYX 2020-11-07 01:12:04 +08:00
parent 60e2039e56
commit 6a2b98798b
No known key found for this signature in database
GPG Key ID: E63F9C7303E8F604
2 changed files with 17 additions and 12 deletions

View File

@ -3,6 +3,7 @@ const MetaFlac = require('metaflac-js');
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");
const MagicHeader = [0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D]; const MagicHeader = [0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D];
const musicMetadata = require("music-metadata-browser");
import jimp from 'jimp'; import jimp from 'jimp';
import { import {
@ -50,16 +51,22 @@ export async function Decrypt(file, raw_filename, raw_ext) {
imageInfo.buffer = await img.getBufferAsync("image/jpeg") imageInfo.buffer = await img.getBufferAsync("image/jpeg")
} }
console.log(imageInfo) console.log(imageInfo)
const mime = AudioMimeType[musicMeta.format]
try { 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") { 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, !shouldWrite)
} else if (musicMeta.format === "flac") { } else if (musicMeta.format === "flac") {
const writer = new MetaFlac(Buffer.from(audioData)) const writer = new MetaFlac(Buffer.from(audioData))
if (writer.getTag("TITLE") === "") writer.setTag("TITLE=" + info.title) if (shouldWrite) {
if (writer.getTag("ALBUM") === "") writer.setTag("ALBUM=" + musicMeta.album) writer.setTag("TITLE=" + info.title)
writer.setTag("ALBUM=" + musicMeta.album)
writer.removeTag("ARTIST") writer.removeTag("ARTIST")
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()
} }
@ -67,8 +74,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
console.warn("Error while appending cover image to file " + e) 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 { return {
status: true, status: true,
@ -79,7 +85,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
picture: imageInfo.url, picture: imageInfo.url,
file: URL.createObjectURL(musicData), file: URL.createObjectURL(musicData),
mime: mime mime: mime
}; }
} }

View File

@ -1,4 +1,5 @@
const ID3Writer = require("browser-id3-writer"); const ID3Writer = require("browser-id3-writer");
const musicMetadata = require("music-metadata-browser");
export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43]; export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43];
export const MP3_HEADER = [0x49, 0x44, 0x33]; export const MP3_HEADER = [0x49, 0x44, 0x33];
export const OGG_HEADER = [0x4F, 0x67, 0x67, 0x53]; 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": ""} 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); const writer = new ID3Writer(audioData);
writer.setFrame("TPE1", artistList) if (!cover_only) writer.setFrame("TPE1", artistList).setFrame("TIT2", title).setFrame("TALB", album);
.setFrame("TIT2", title)
.setFrame("TALB", album);
if (pictureData !== null) { if (pictureData !== null) {
writer.setFrame('APIC', { writer.setFrame('APIC', {
type: 3, type: 3,