refactor(typescript): utils.WriteMetaFor{ Mp3, Flac }

This commit is contained in:
Emmm Monster 2021-05-24 02:55:42 +08:00
parent 342241b379
commit 37a641e69e
No known key found for this signature in database
GPG Key ID: C98279C83FB50DB9
6 changed files with 106 additions and 76 deletions

View File

@ -60,7 +60,7 @@
import preview from "./component/preview" import preview from "./component/preview"
import {DownloadBlobMusic, RemoveBlobMusic} from "./component/util" import {DownloadBlobMusic, RemoveBlobMusic} from "./component/util"
import config from "../package" import config from "../package"
import {IXAREA_API_ENDPOINT} from "./decrypt/util"; import {IXAREA_API_ENDPOINT} from "@/decrypt/utils";
export default { export default {
name: 'app', name: 'app',

View File

@ -4,21 +4,16 @@ import {
GetArrayBuffer, GetArrayBuffer,
GetImageFromURL, GetImageFromURL,
GetMetaFromFile, GetMetaFromFile,
SniffAudioExt SniffAudioExt, WriteMetaToFlac, WriteMetaToMp3
} from "@/decrypt/utils.ts"; } from "@/decrypt/utils.ts";
import {parseBlob as metaParseBlob} from "music-metadata-browser"; import {parseBlob as metaParseBlob} from "music-metadata-browser";
import jimp from 'jimp';
const CryptoJS = require("crypto-js"); const CryptoJS = require("crypto-js");
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];
import jimp from 'jimp';
import {
WriteMp3Meta
} from "./util"
export async function Decrypt(file, raw_filename, _) { export async function Decrypt(file, raw_filename, _) {
const fileBuffer = await GetArrayBuffer(file); const fileBuffer = await GetArrayBuffer(file);
@ -48,32 +43,27 @@ export async function Decrypt(file, raw_filename, _) {
if (musicMeta.format === undefined) musicMeta.format = SniffAudioExt(audioData); if (musicMeta.format === undefined) musicMeta.format = SniffAudioExt(audioData);
const imageInfo = await GetImageFromURL(musicMeta.albumPic); const imageInfo = await GetImageFromURL(musicMeta.albumPic);
while (imageInfo && imageInfo.buffer.byteLength >= 1 << 24) {
let img = await jimp.read(imageInfo.buffer)
await img.resize(Math.round(img.getHeight() / 2), jimp.AUTO)
imageInfo.buffer = await img.getBufferAsync("image/jpeg")
}
const mime = AudioMimeType[musicMeta.format] const mime = AudioMimeType[musicMeta.format]
try { try {
let musicBlob = new Blob([audioData], {type: mime}); let musicBlob = new Blob([audioData], {type: mime});
const originalMeta = await metaParseBlob(musicBlob); const originalMeta = await metaParseBlob(musicBlob);
let shouldWrite = !originalMeta.common.album && !originalMeta.common.artists && !originalMeta.common.title let shouldWrite = !originalMeta.common.album && !originalMeta.common.artists && !originalMeta.common.title
if (musicMeta.format === "mp3") { if (shouldWrite || imageInfo) {
audioData = await WriteMp3Meta( while (imageInfo && imageInfo.buffer.byteLength >= 1 << 24) {
audioData, artists, info.title, musicMeta.album, imageInfo.buffer, musicMeta.albumPic, shouldWrite ? null : originalMeta) let img = await jimp.read(Buffer.from(imageInfo.buffer))
} else if (musicMeta.format === "flac") { await img.resize(Math.round(img.getHeight() / 2), jimp.AUTO)
const writer = new MetaFlac(Buffer.from(audioData)) imageInfo.buffer = await img.getBufferAsync("image/jpeg")
if (shouldWrite) { }
writer.setTag("TITLE=" + info.title) const newMeta = {title: info.title, artists, album: musicMeta.album, picture: imageInfo?.buffer}
writer.setTag("ALBUM=" + musicMeta.album) if (musicMeta.format === "mp3") {
writer.removeTag("ARTIST") audioData = WriteMetaToMp3(audioData.buffer, newMeta, originalMeta)
artists.forEach(artist => writer.setTag("ARTIST=" + artist)) } else if (musicMeta.format === "flac") {
audioData = WriteMetaToFlac(Buffer.from(audioData), newMeta, originalMeta)
} }
writer.importPictureFromBuffer(Buffer.from(imageInfo.buffer))
audioData = writer.save()
} }
} catch (e) { } catch (e) {
console.warn("Error while appending cover image to file " + e) console.warn("Error while appending cover image to file ", e)
} }
const musicData = new Blob([audioData], {type: mime}) const musicData = new Blob([audioData], {type: mime})

View File

@ -1,7 +1,3 @@
import {
IXAREA_API_ENDPOINT,
WriteMp3Meta
} from "./util";
import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask"; import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask";
import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js' import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js'
import { import {
@ -9,13 +5,11 @@ import {
GetArrayBuffer, GetArrayBuffer,
GetCoverFromFile, GetCoverFromFile,
GetImageFromURL, GetImageFromURL,
GetMetaFromFile, GetMetaFromFile, IXAREA_API_ENDPOINT,
SniffAudioExt SniffAudioExt, WriteMetaToFlac, WriteMetaToMp3
} from "@/decrypt/utils.ts"; } from "@/decrypt/utils.ts";
import {parseBlob as metaParseBlob} from "music-metadata-browser"; import {parseBlob as metaParseBlob} from "music-metadata-browser";
const MetaFlac = require('metaflac-js');
const iconv = require('iconv-lite'); const iconv = require('iconv-lite');
const decode = iconv.decode const decode = iconv.decode
@ -85,15 +79,12 @@ export async function Decrypt(file, raw_filename, raw_ext) {
if (imageInfo) { if (imageInfo) {
imgUrl = imageInfo.url imgUrl = imageInfo.url
try { try {
const newMeta = {picture: imageInfo.buffer, title: info.title, artists: info.artist.split(" _ ")}
if (ext === "mp3") { if (ext === "mp3") {
musicDecoded = await WriteMp3Meta(musicDecoded, musicDecoded = WriteMetaToMp3(musicDecoded, newMeta, musicMeta)
info.artist.split(" _ "), info.title, "",
imageInfo.buffer, "Cover", musicMeta)
musicBlob = new Blob([musicDecoded], {type: mime}); musicBlob = new Blob([musicDecoded], {type: mime});
} else if (ext === 'flac') { } else if (ext === 'flac') {
const writer = new MetaFlac(Buffer.from(musicDecoded)) musicDecoded = WriteMetaToFlac(Buffer.from(musicDecoded), newMeta, musicMeta)
writer.importPictureFromBuffer(Buffer.from(imageInfo.buffer))
musicDecoded = writer.save()
musicBlob = new Blob([musicDecoded], {type: mime}); musicBlob = new Blob([musicDecoded], {type: mime});
} else { } else {
console.info("writing metadata for " + ext + " is not being supported for now") console.info("writing metadata for " + ext + " is not being supported for now")

View File

@ -1,36 +0,0 @@
const ID3Writer = require("browser-id3-writer");
export const IXAREA_API_ENDPOINT = "https://stats.ixarea.com/apis"
export async function WriteMp3Meta(audioData, artistList, title, album, pictureData = null, pictureDesc = "Cover", originalMeta = null) {
const writer = new ID3Writer(audioData);
if (originalMeta !== null) {
artistList = originalMeta.common.artists || artistList
title = originalMeta.common.title || title
album = originalMeta.common.album || album
const frames = originalMeta.native['ID3v2.4'] || originalMeta.native['ID3v2.3'] || originalMeta.native['ID3v2.2'] || []
frames.forEach(frame => {
if (frame.id !== 'TPE1' && frame.id !== 'TIT2' && frame.id !== 'TALB') {
try {
writer.setFrame(frame.id, frame.value)
} catch (e) {
}
}
})
}
writer.setFrame('TPE1', artistList)
.setFrame('TIT2', title)
.setFrame('TALB', album);
if (pictureData !== null) {
writer.setFrame('APIC', {
type: 3,
data: pictureData,
description: pictureDesc,
})
}
writer.addTag();
return writer.arrayBuffer;
}

View File

@ -1,4 +1,6 @@
import {IAudioMetadata} from "music-metadata-browser"; import {IAudioMetadata} from "music-metadata-browser";
import ID3Writer from "browser-id3-writer";
import MetaFlac from "metaflac-js";
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];
@ -19,6 +21,9 @@ export const AudioMimeType: { [key: string]: string } = {
wav: "audio/x-wav" wav: "audio/x-wav"
}; };
export const IXAREA_API_ENDPOINT = "https://stats.ixarea.com/apis"
export function BytesHasPrefix(data: Uint8Array, prefix: number[]): boolean { export function BytesHasPrefix(data: Uint8Array, prefix: number[]): boolean {
if (prefix.length > data.length) return false if (prefix.length > data.length) return false
return prefix.every((val, idx) => { return prefix.every((val, idx) => {
@ -104,3 +109,58 @@ export async function GetImageFromURL(src: string):
console.warn(e) console.warn(e)
} }
} }
export interface IMusicMeta {
title: string
artists?: string[]
album?: string
picture?: ArrayBuffer
picture_desc?: string
}
export function WriteMetaToMp3(audioData: ArrayBuffer, info: IMusicMeta, original: IAudioMetadata) {
const writer = new ID3Writer(audioData);
// reserve original data
const frames = original.native['ID3v2.4'] || original.native['ID3v2.3'] || original.native['ID3v2.2'] || []
frames.forEach(frame => {
if (frame.id !== 'TPE1' && frame.id !== 'TIT2' && frame.id !== 'TALB') {
try {
writer.setFrame(frame.id, frame.value)
} catch (e) {
}
}
})
const old = original.common
writer.setFrame('TPE1', old?.artists || info.artists || [])
.setFrame('TIT2', old?.title || info.title)
.setFrame('TALB', old?.album || info.album || "");
if (info.picture) {
writer.setFrame('APIC', {
type: 3,
data: info.picture,
description: info.picture_desc || "Cover",
})
}
return writer.addTag();
}
export function WriteMetaToFlac(audioData: Buffer, info: IMusicMeta, original: IAudioMetadata) {
const writer = new MetaFlac(audioData)
const old = original.common
if (!old.title && !old.album && old.artists) {
writer.setTag("TITLE=" + info.title)
writer.setTag("ALBUM=" + info.album)
if (info.artists) {
writer.removeTag("ARTIST")
info.artists.forEach(artist => writer.setTag("ARTIST=" + artist))
}
}
if (info.picture) {
writer.importPictureFromBuffer(Buffer.from(info.picture))
}
return writer.save()
}

25
src/shims-browser-id3-writer.d.ts vendored Normal file
View File

@ -0,0 +1,25 @@
declare module "browser-id3-writer" {
export default class ID3Writer {
constructor(buffer: Buffer | ArrayBuffer)
setFrame(name: string, value: string | object | string[])
addTag(): ArrayBuffer
}
}
declare module "metaflac-js" {
export default class Metaflac {
constructor(buffer: Buffer)
setTag(field: string)
removeTag(name: string)
importPictureFromBuffer(picture: Buffer)
save(): Buffer
}
}