Merge pull request #1110 from keaganhilliard/tone-json

Added tone json file support
This commit is contained in:
advplyr 2022-11-05 13:13:49 -05:00 committed by GitHub
commit 9627f58541
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 36 deletions

View File

@ -91,6 +91,10 @@
A backup of your original audio files will be stored in <span class="rounded-md bg-neutral-600 text-sm text-white py-0.5 px-1 font-mono">/metadata/cache/items/{{ libraryItemId }}/</span>. Make sure to periodically purge items cache. A backup of your original audio files will be stored in <span class="rounded-md bg-neutral-600 text-sm text-white py-0.5 px-1 font-mono">/metadata/cache/items/{{ libraryItemId }}/</span>. Make sure to periodically purge items cache.
</p> </p>
</div> </div>
<div v-if="selectedTool === 'embed' && audioFiles.length > 1" class="flex items-start mb-2">
<span class="material-icons text-base text-warning pt-1">star</span>
<p class="text-gray-200 ml-2">Chapters are not embedded in multi-track audiobooks.</p>
</div>
<div v-if="selectedTool === 'm4b'" class="flex items-start mb-2"> <div v-if="selectedTool === 'm4b'" class="flex items-start mb-2">
<span class="material-icons text-base text-warning pt-1">star</span> <span class="material-icons text-base text-warning pt-1">star</span>
<p class="text-gray-200 ml-2">Encoding can take up to 30 minutes.</p> <p class="text-gray-200 ml-2">Encoding can take up to 30 minutes.</p>

View File

@ -398,7 +398,8 @@ class LibraryItemController {
} }
const useTone = req.query.tone === '1' const useTone = req.query.tone === '1'
this.audioMetadataManager.updateMetadataForItem(req.user, req.libraryItem, useTone) const forceEmbedChapters = req.query.forceEmbedChapters === '1'
this.audioMetadataManager.updateMetadataForItem(req.user, req.libraryItem, useTone, forceEmbedChapters)
res.sendStatus(200) res.sendStatus(200)
} }

View File

@ -62,7 +62,7 @@ class AbMergeManager {
targetFilename, targetFilename,
targetFilepath: Path.join(libraryItem.path, targetFilename), targetFilepath: Path.join(libraryItem.path, targetFilename),
itemCachePath, itemCachePath,
toneMetadataObject: null toneJsonObject: null
} }
const taskDescription = `Encoding audiobook "${libraryItem.media.metadata.title}" into a single m4b file.` const taskDescription = `Encoding audiobook "${libraryItem.media.metadata.title}" into a single m4b file.`
task.setData('encode-m4b', 'Encoding M4b', taskDescription, taskData) task.setData('encode-m4b', 'Encoding M4b', taskDescription, taskData)
@ -120,22 +120,19 @@ 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(task.data.itemCachePath, 'metadata.json')
try { await toneHelpers.writeToneMetadataJsonFile(libraryItem, libraryItem.media.chapters, toneJsonPath, 1)
await toneHelpers.writeToneChaptersFile(libraryItem.media.chapters, chaptersFilePath) } catch (error) {
} catch (error) { Logger.error(`[AbMergeManager] Write metadata.json failed`, error)
Logger.error(`[AbMergeManager] Write chapters.txt failed`, error) toneJsonPath = null
chaptersFilePath = null
}
} }
const toneMetadataObject = toneHelpers.getToneMetadataObject(libraryItem, chaptersFilePath) task.data.toneJsonObject = {
toneMetadataObject.TrackNumber = 1 'ToneJsonFile': toneJsonPath,
task.data.toneMetadataObject = toneMetadataObject 'TrackNumber': 1,
}
Logger.debug(`[AbMergeManager] Book "${libraryItem.media.metadata.title}" tone metadata object=`, toneMetadataObject)
var workerData = { var workerData = {
inputs: ffmpegInputs, inputs: ffmpegInputs,
@ -190,7 +187,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

@ -15,9 +15,9 @@ class AudioMetadataMangaer {
this.clientEmitter = clientEmitter this.clientEmitter = clientEmitter
} }
updateMetadataForItem(user, libraryItem, useTone = true) { updateMetadataForItem(user, libraryItem, useTone, forceEmbedChapters) {
if (useTone) { if (useTone) {
this.updateMetadataForItemWithTone(user, libraryItem) this.updateMetadataForItemWithTone(user, libraryItem, forceEmbedChapters)
} else { } else {
this.updateMetadataForItemWithFfmpeg(user, libraryItem) this.updateMetadataForItemWithFfmpeg(user, libraryItem)
} }
@ -30,7 +30,7 @@ class AudioMetadataMangaer {
return toneHelpers.getToneMetadataObject(libraryItem) return toneHelpers.getToneMetadataObject(libraryItem)
} }
async updateMetadataForItemWithTone(user, libraryItem) { async updateMetadataForItemWithTone(user, libraryItem, forceEmbedChapters) {
var audioFiles = libraryItem.media.includedAudioFiles var audioFiles = libraryItem.media.includedAudioFiles
const itemAudioMetadataPayload = { const itemAudioMetadataPayload = {
@ -43,26 +43,22 @@ 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 { const chapters = (audioFiles.length == 1 || forceEmbedChapters) ? libraryItem.media.chapters : null
await toneHelpers.writeToneChaptersFile(libraryItem.media.chapters, chaptersFilePath) await toneHelpers.writeToneMetadataJsonFile(libraryItem, chapters, toneJsonPath, audioFiles.length)
} catch (error) { } catch (error) {
Logger.error(`[AudioMetadataManager] Write chapters.txt failed`, error) Logger.error(`[AudioMetadataManager] Write metadata.json failed`, error)
chaptersFilePath = null toneJsonPath = 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, toneJsonPath, itemCacheDir)
results.push(result) results.push(result)
} }
@ -74,7 +70,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 +89,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,70 @@ module.exports.getToneMetadataObject = (libraryItem, chaptersFile) => {
return metadataObject return metadataObject
} }
module.exports.writeToneMetadataJsonFile = (libraryItem, chapters, filePath, trackTotal) => {
const bookMetadata = libraryItem.media.metadata
const coverPath = libraryItem.media.coverPath
const metadataObject = {
'album': bookMetadata.title || '',
'title': bookMetadata.title || '',
'trackTotal': trackTotal,
'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: Math.round(chapter.start * 1000),
length: Math.round((chapter.end - chapter.start) * 1000),
title: chapter.title,
})
}
metadataObject['chapters'] = metadataChapters
}
return fs.writeFile(filePath, JSON.stringify({ meta: metadataObject }, null, 2))
}
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