From 1da471e136eb16ac26d2078962cd147e7c29a720 Mon Sep 17 00:00:00 2001 From: advplyr Date: Fri, 26 May 2023 17:57:56 -0500 Subject: [PATCH] Fix:Embed metadata tool embed ASIN, SERIES and SERIESPART #1794 --- server/managers/AbMergeManager.js | 2 +- server/managers/AudioMetadataManager.js | 10 ++++++-- server/utils/toneHelpers.js | 32 +++++++++++++++++++++---- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/server/managers/AbMergeManager.js b/server/managers/AbMergeManager.js index 3480e529..f725f53f 100644 --- a/server/managers/AbMergeManager.js +++ b/server/managers/AbMergeManager.js @@ -112,7 +112,7 @@ class AbMergeManager { let toneJsonPath = null try { toneJsonPath = Path.join(task.data.itemCachePath, 'metadata.json') - await toneHelpers.writeToneMetadataJsonFile(libraryItem, libraryItem.media.chapters, toneJsonPath, 1) + await toneHelpers.writeToneMetadataJsonFile(libraryItem, libraryItem.media.chapters, toneJsonPath, 1, 'audio/mp4') } catch (error) { Logger.error(`[AbMergeManager] Write metadata.json failed`, error) toneJsonPath = null diff --git a/server/managers/AudioMetadataManager.js b/server/managers/AudioMetadataManager.js index ac65c0f7..20cf2df8 100644 --- a/server/managers/AudioMetadataManager.js +++ b/server/managers/AudioMetadataManager.js @@ -34,7 +34,10 @@ class AudioMetadataMangaer { } getToneMetadataObjectForApi(libraryItem) { - return toneHelpers.getToneMetadataObject(libraryItem, libraryItem.media.chapters, libraryItem.media.tracks.length) + const audioFiles = libraryItem.media.includedAudioFiles + let mimeType = audioFiles[0].mimeType + if (audioFiles.some(a => a.mimeType !== mimeType)) mimeType = null + return toneHelpers.getToneMetadataObject(libraryItem, libraryItem.media.chapters, libraryItem.media.tracks.length, mimeType) } handleBatchEmbed(user, libraryItems, options = {}) { @@ -56,6 +59,9 @@ class AudioMetadataMangaer { // Only writing chapters for single file audiobooks const chapters = (audioFiles.length == 1 || forceEmbedChapters) ? libraryItem.media.chapters.map(c => ({ ...c })) : null + let mimeType = audioFiles[0].mimeType + if (audioFiles.some(a => a.mimeType !== mimeType)) mimeType = null + // Create task const taskData = { libraryItemId: libraryItem.id, @@ -71,7 +77,7 @@ class AudioMetadataMangaer { } )), coverPath: libraryItem.media.coverPath, - metadataObject: toneHelpers.getToneMetadataObject(libraryItem, chapters, audioFiles.length), + metadataObject: toneHelpers.getToneMetadataObject(libraryItem, chapters, audioFiles.length, mimeType), itemCachePath, chapters, options: { diff --git a/server/utils/toneHelpers.js b/server/utils/toneHelpers.js index 384b4c5d..72ccdb69 100644 --- a/server/utils/toneHelpers.js +++ b/server/utils/toneHelpers.js @@ -2,10 +2,13 @@ const tone = require('node-tone') const fs = require('../libs/fsExtra') const Logger = require('../Logger') -function getToneMetadataObject(libraryItem, chapters, trackTotal) { +function getToneMetadataObject(libraryItem, chapters, trackTotal, mimeType = null) { const bookMetadata = libraryItem.media.metadata const coverPath = libraryItem.media.coverPath + const isMp4 = mimeType === 'audio/mp4' + const isMp3 = mimeType === 'audio/mpeg' + const metadataObject = { 'album': bookMetadata.title || '', 'title': bookMetadata.title || '', @@ -28,10 +31,24 @@ function getToneMetadataObject(libraryItem, chapters, trackTotal) { metadataObject['composer'] = bookMetadata.narratorName } if (bookMetadata.firstSeriesName) { + if (!isMp3) { + metadataObject.additionalFields['----:com.pilabor.tone:SERIES'] = bookMetadata.firstSeriesName + } metadataObject['movementName'] = bookMetadata.firstSeriesName } if (bookMetadata.firstSeriesSequence) { - metadataObject['movement'] = bookMetadata.firstSeriesSequence + // Non-mp3 + if (!isMp3) { + metadataObject.additionalFields['----:com.pilabor.tone:PART'] = bookMetadata.firstSeriesSequence + } + // MP3 Files with non-integer sequence + const isNonIntegerSequence = String(bookMetadata.firstSeriesSequence).includes('.') || isNaN(bookMetadata.firstSeriesSequence) + if (isMp3 && isNonIntegerSequence) { + metadataObject.additionalFields['PART'] = bookMetadata.firstSeriesSequence + } + if (!isNonIntegerSequence) { + metadataObject['movement'] = bookMetadata.firstSeriesSequence + } } if (bookMetadata.genres.length) { metadataObject['genre'] = bookMetadata.genres.join('/') @@ -40,7 +57,12 @@ function getToneMetadataObject(libraryItem, chapters, trackTotal) { metadataObject['publisher'] = bookMetadata.publisher } if (bookMetadata.asin) { - metadataObject.additionalFields['asin'] = bookMetadata.asin + if (!isMp3) { + metadataObject.additionalFields['----:com.pilabor.tone:AUDIBLE_ASIN'] = bookMetadata.asin + } + if (!isMp4) { + metadataObject.additionalFields['asin'] = bookMetadata.asin + } } if (bookMetadata.isbn) { metadataObject.additionalFields['isbn'] = bookMetadata.isbn @@ -67,8 +89,8 @@ function getToneMetadataObject(libraryItem, chapters, trackTotal) { } module.exports.getToneMetadataObject = getToneMetadataObject -module.exports.writeToneMetadataJsonFile = (libraryItem, chapters, filePath, trackTotal) => { - const metadataObject = getToneMetadataObject(libraryItem, chapters, trackTotal) +module.exports.writeToneMetadataJsonFile = (libraryItem, chapters, filePath, trackTotal, mimeType) => { + const metadataObject = getToneMetadataObject(libraryItem, chapters, trackTotal, mimeType) return fs.writeFile(filePath, JSON.stringify({ meta: metadataObject }, null, 2)) }