Added json file support

This commit is contained in:
Keagan Hilliard 2022-11-02 19:40:50 -06:00
parent c6e9fe6513
commit c1938f78c2
3 changed files with 87 additions and 25 deletions

View File

@ -120,20 +120,22 @@ class AbMergeManager {
} }
} }
var chaptersFilePath = null var toneJsonPath = null
if (libraryItem.media.chapters.length) { try {
chaptersFilePath = Path.join(task.data.itemCachePath, 'chapters.txt') toneJsonPath = Path.join(itemCacheDir, 'metadata.json')
try { await toneHelpers.writeToneMetadataJsonFile(libraryItem, toneJsonPath)
await toneHelpers.writeToneChaptersFile(libraryItem.media.chapters, chaptersFilePath) } catch (error) {
} catch (error) { Logger.error(`[AudioMetadataManager] Write metadata.json failed`, error)
Logger.error(`[AbMergeManager] Write chapters.txt failed`, error) toneJsonPath = null
chaptersFilePath = null
}
} }
const toneMetadataObject = toneHelpers.getToneMetadataObject(libraryItem, chaptersFilePath) const toneMetadataObject = toneHelpers.getToneMetadataObject(libraryItem, chaptersFilePath)
toneMetadataObject.TrackNumber = 1 toneMetadataObject.TrackNumber = 1
task.data.toneMetadataObject = toneMetadataObject task.data.toneMetadataObject = toneMetadataObject
task.data.toneJsonObject = {
'ToneJsonFile': toneJsonPath,
'TrackNumber': 1,
}
Logger.debug(`[AbMergeManager] Book "${libraryItem.media.metadata.title}" tone metadata object=`, toneMetadataObject) Logger.debug(`[AbMergeManager] Book "${libraryItem.media.metadata.title}" tone metadata object=`, toneMetadataObject)
@ -190,7 +192,7 @@ class AbMergeManager {
} }
// Write metadata to merged file // Write metadata to merged file
const success = await toneHelpers.tagAudioFile(task.data.tempFilepath, task.data.toneMetadataObject) const success = await toneHelpers.tagAudioFile(task.data.tempFilepath, task.data.toneJsonObject)
if (!success) { if (!success) {
Logger.error(`[AbMergeManager] Failed to write metadata to file "${task.data.tempFilepath}"`) Logger.error(`[AbMergeManager] Failed to write metadata to file "${task.data.tempFilepath}"`)
task.setFailed('Failed to write metadata to m4b file') task.setFailed('Failed to write metadata to m4b file')

View File

@ -43,23 +43,18 @@ class AudioMetadataMangaer {
this.emitter('audio_metadata_started', itemAudioMetadataPayload) this.emitter('audio_metadata_started', itemAudioMetadataPayload)
// Write chapters file // Write chapters file
var chaptersFilePath = null var toneJsonPath = null
const itemCacheDir = Path.join(global.MetadataPath, `cache/items/${libraryItem.id}`) const itemCacheDir = Path.join(global.MetadataPath, `cache/items/${libraryItem.id}`)
await fs.ensureDir(itemCacheDir) await fs.ensureDir(itemCacheDir)
if (libraryItem.media.chapters.length) { try {
chaptersFilePath = Path.join(itemCacheDir, 'chapters.txt') toneJsonPath = Path.join(itemCacheDir, 'metadata.json')
try { await toneHelpers.writeToneMetadataJsonFile(libraryItem, toneJsonPath)
await toneHelpers.writeToneChaptersFile(libraryItem.media.chapters, chaptersFilePath) } catch (error) {
} catch (error) { Logger.error(`[AudioMetadataManager] Write metadata.json failed`, error)
Logger.error(`[AudioMetadataManager] Write chapters.txt failed`, error) toneJsonPath = null
chaptersFilePath = null
}
} }
const toneMetadataObject = toneHelpers.getToneMetadataObject(libraryItem, chaptersFilePath)
Logger.debug(`[AudioMetadataManager] Book "${libraryItem.media.metadata.title}" tone metadata object=`, toneMetadataObject)
const results = [] const results = []
for (const af of audioFiles) { for (const af of audioFiles) {
const result = await this.updateAudioFileMetadataWithTone(libraryItem.id, af, toneMetadataObject, itemCacheDir) const result = await this.updateAudioFileMetadataWithTone(libraryItem.id, af, toneMetadataObject, itemCacheDir)
@ -74,7 +69,7 @@ class AudioMetadataMangaer {
this.emitter('audio_metadata_finished', itemAudioMetadataPayload) this.emitter('audio_metadata_finished', itemAudioMetadataPayload)
} }
async updateAudioFileMetadataWithTone(libraryItemId, audioFile, toneMetadataObject, itemCacheDir) { async updateAudioFileMetadataWithTone(libraryItemId, audioFile, toneJsonPath, itemCacheDir) {
const resultPayload = { const resultPayload = {
libraryItemId, libraryItemId,
index: audioFile.index, index: audioFile.index,
@ -93,8 +88,8 @@ class AudioMetadataMangaer {
} }
const _toneMetadataObject = { const _toneMetadataObject = {
...toneMetadataObject, 'ToneJsonFile': toneJsonPath,
'TrackNumber': audioFile.index 'TrackNumber': audioFile.index,
} }
resultPayload.success = await toneHelpers.tagAudioFile(audioFile.metadata.path, _toneMetadataObject) resultPayload.success = await toneHelpers.tagAudioFile(audioFile.metadata.path, _toneMetadataObject)

View File

@ -72,6 +72,71 @@ module.exports.getToneMetadataObject = (libraryItem, chaptersFile) => {
return metadataObject return metadataObject
} }
module.exports.writeToneMetadataJsonFile = (libraryItem, filePath) => {
const bookMetadata = libraryItem.media.metadata
const coverPath = libraryItem.media.coverPath
const chapters = libraryItem.media.chapters
const metadataObject = {
'album': bookMetadata.title || '',
'title': bookMetadata.title || '',
'trackTotal': libraryItem.media.tracks.length,
'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({
start: chapter.start,
length: chapter.end - chapter.start,
title: chapter.title,
})
}
metadataObject['chapters'] = chaptersFile
}
return fs.writeFile(filePath, JSON.stringify({ metadata: metadataObject }))
}
module.exports.tagAudioFile = (filePath, payload) => { module.exports.tagAudioFile = (filePath, payload) => {
return tone.tag(filePath, payload).then((data) => { return tone.tag(filePath, payload).then((data) => {
return true return true