新增FLAC格式音乐标签信息写入

This commit is contained in:
lyswhut
2020-03-21 22:31:02 +08:00
parent bc7c3968b6
commit 83f0ca918a
12 changed files with 1072 additions and 384 deletions
+10 -10
View File
@@ -1,32 +1,32 @@
const fs = require('fs')
const flac = require('flac-metadata')
const { Processor: FlacProcessor, data: { MetaDataBlockVorbisComment: FlacComment } } = require('./flac-metadata')
const vendor = 'reference libFLAC 1.2.1 20070917'
module.exports = (filenPath, meta) => {
const reader = fs.createReadStream(filenPath)
const tempPath = filenPath + '.lxmtemp'
const writer = fs.createWriteStream(tempPath)
const processor = new flac.Processor()
const flacProcessor = new FlacProcessor()
if (meta.APIC) delete meta.APIC
const comments = []
for (const key in meta) {
comments.push(`${key.toUpperCase()}=${meta[key]}`)
}
const vendor = 'lx-music-desktop'
processor.on('preprocess', function(mdb) {
let isInjected = false
flacProcessor.on('preprocess', function(mdb) {
// Remove existing VORBIS_COMMENT block, if any.
if (mdb.type === flac.Processor.MDB_TYPE_VORBIS_COMMENT) {
mdb.remove()
}
if (mdb.type === flacProcessor.MDB_TYPE_VORBIS_COMMENT) mdb.remove()
// Inject new VORBIS_COMMENT block.
if (mdb.removed || mdb.isLast) {
let mdbVorbis = flac.data.MetaDataBlockVorbisComment.create(mdb.isLast, vendor, comments)
if ((mdb.removed || mdb.isLast) && !isInjected) {
isInjected = true
let mdbVorbis = FlacComment.create(mdb.isLast, vendor, comments)
this.push(mdbVorbis.publish())
}
})
reader.pipe(processor).pipe(writer).on('finish', () => {
reader.pipe(flacProcessor).pipe(writer).on('finish', () => {
fs.unlink(filenPath, err => {
if (err) return console.log(err.message)
fs.rename(tempPath, filenPath, err => {