1
0
forked from um/web

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 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
};
}
}

View File

@ -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,