2022-09-25 22:56:06 +02:00
|
|
|
const tone = require('node-tone')
|
|
|
|
const fs = require('../libs/fsExtra')
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
2023-04-02 23:13:18 +02:00
|
|
|
function getToneMetadataObject(libraryItem, chapters, trackTotal) {
|
2022-11-03 02:40:50 +01:00
|
|
|
const bookMetadata = libraryItem.media.metadata
|
|
|
|
const coverPath = libraryItem.media.coverPath
|
|
|
|
|
|
|
|
const metadataObject = {
|
|
|
|
'album': bookMetadata.title || '',
|
|
|
|
'title': bookMetadata.title || '',
|
2022-11-05 19:13:52 +01:00
|
|
|
'trackTotal': trackTotal,
|
2022-11-03 02:40:50 +01:00
|
|
|
'additionalFields': {}
|
|
|
|
}
|
|
|
|
if (bookMetadata.subtitle) {
|
|
|
|
metadataObject['subtitle'] = bookMetadata.subtitle
|
|
|
|
}
|
|
|
|
if (bookMetadata.authorName) {
|
|
|
|
metadataObject['artist'] = bookMetadata.authorName
|
|
|
|
metadataObject['albumArtist'] = bookMetadata.authorName
|
|
|
|
}
|
|
|
|
if (bookMetadata.description) {
|
|
|
|
metadataObject['comment'] = bookMetadata.description
|
|
|
|
metadataObject['description'] = bookMetadata.description
|
|
|
|
}
|
|
|
|
if (bookMetadata.narratorName) {
|
|
|
|
metadataObject['narrator'] = bookMetadata.narratorName
|
|
|
|
metadataObject['composer'] = bookMetadata.narratorName
|
|
|
|
}
|
|
|
|
if (bookMetadata.firstSeriesName) {
|
|
|
|
metadataObject['movementName'] = bookMetadata.firstSeriesName
|
|
|
|
}
|
|
|
|
if (bookMetadata.firstSeriesSequence) {
|
|
|
|
metadataObject['movement'] = bookMetadata.firstSeriesSequence
|
|
|
|
}
|
|
|
|
if (bookMetadata.genres.length) {
|
|
|
|
metadataObject['genre'] = bookMetadata.genres.join('/')
|
|
|
|
}
|
|
|
|
if (bookMetadata.publisher) {
|
|
|
|
metadataObject['publisher'] = bookMetadata.publisher
|
|
|
|
}
|
|
|
|
if (bookMetadata.asin) {
|
|
|
|
metadataObject.additionalFields['asin'] = bookMetadata.asin
|
|
|
|
}
|
|
|
|
if (bookMetadata.isbn) {
|
|
|
|
metadataObject.additionalFields['isbn'] = bookMetadata.isbn
|
|
|
|
}
|
|
|
|
if (coverPath) {
|
|
|
|
metadataObject['coverFile'] = coverPath
|
|
|
|
}
|
|
|
|
if (parsePublishedYear(bookMetadata.publishedYear)) {
|
|
|
|
metadataObject['publishingDate'] = parsePublishedYear(bookMetadata.publishedYear)
|
|
|
|
}
|
|
|
|
if (chapters && chapters.length > 0) {
|
|
|
|
let metadataChapters = []
|
|
|
|
for (const chapter of chapters) {
|
|
|
|
metadataChapters.push({
|
2022-11-03 16:32:50 +01:00
|
|
|
start: Math.round(chapter.start * 1000),
|
|
|
|
length: Math.round((chapter.end - chapter.start) * 1000),
|
2022-11-03 02:40:50 +01:00
|
|
|
title: chapter.title,
|
|
|
|
})
|
|
|
|
}
|
2022-11-03 16:32:50 +01:00
|
|
|
metadataObject['chapters'] = metadataChapters
|
2022-11-03 02:40:50 +01:00
|
|
|
}
|
|
|
|
|
2023-04-02 23:13:18 +02:00
|
|
|
return metadataObject
|
|
|
|
}
|
|
|
|
module.exports.getToneMetadataObject = getToneMetadataObject
|
|
|
|
|
|
|
|
module.exports.writeToneMetadataJsonFile = (libraryItem, chapters, filePath, trackTotal) => {
|
|
|
|
const metadataObject = getToneMetadataObject(libraryItem, chapters, trackTotal)
|
2022-11-05 19:13:52 +01:00
|
|
|
return fs.writeFile(filePath, JSON.stringify({ meta: metadataObject }, null, 2))
|
2022-11-03 02:40:50 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:56:06 +02:00
|
|
|
module.exports.tagAudioFile = (filePath, payload) => {
|
|
|
|
return tone.tag(filePath, payload).then((data) => {
|
|
|
|
return true
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[toneHelpers] tagAudioFile: Failed for "${filePath}"`, error)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function parsePublishedYear(publishedYear) {
|
|
|
|
if (isNaN(publishedYear) || !publishedYear || Number(publishedYear) <= 0) return null
|
|
|
|
return `01/01/${publishedYear}`
|
|
|
|
}
|