From 80ef8ee890949b860585331e6c73a3a71b3e5912 Mon Sep 17 00:00:00 2001 From: Joakim Ramer Date: Sun, 29 Jan 2023 23:57:37 +0100 Subject: [PATCH] Fixes m4b chapters only taken from first file. --- server/objects/mediaTypes/Book.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/server/objects/mediaTypes/Book.js b/server/objects/mediaTypes/Book.js index 2e536a26..506e6ee7 100644 --- a/server/objects/mediaTypes/Book.js +++ b/server/objects/mediaTypes/Book.js @@ -421,9 +421,26 @@ class Book { } // IF first audio file has embedded chapters then use embedded chapters - if (includedAudioFiles[0].chapters && includedAudioFiles[0].chapters.length) { - Logger.debug(`[Book] setChapters: Using embedded chapters in audio file ${includedAudioFiles[0].metadata.path}`) - this.chapters = includedAudioFiles[0].chapters.map(c => ({ ...c })) + if (includedAudioFiles[0].chapters?.length) { + Logger.debug(`[Book] setChapters: Using embedded chapters in audio file ${includedAudioFiles[0]?.metadata?.path}`) + this.chapters = [] + let currChapterId = 0 + let currStartTime = 0 + + includedAudioFiles.forEach((file) => { + if (file.duration) { + let chapters = file.chapters?.map((c) => ({ + ...c, + id: c.id + currChapterId, + start: c.start + currStartTime, + end: c.end + currStartTime, + })) + this.chapters = this.chapters.concat(chapters) + + currChapterId += file.chapters?.length ?? 0 + currStartTime += file.duration + } + }) } else if (includedAudioFiles.length > 1) { // Build chapters from audio files this.chapters = [] @@ -461,4 +478,4 @@ class Book { return this.metadata.authorName } } -module.exports = Book \ No newline at end of file +module.exports = Book