From bb7938f66d816709635fb132faa85d23479e922a Mon Sep 17 00:00:00 2001 From: advplyr Date: Tue, 24 Sep 2024 10:54:25 -0500 Subject: [PATCH] Update:When merging embedded chapters from multiple files filter out ~0 duration chapters #3361 --- server/scanner/AudioFileScanner.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/server/scanner/AudioFileScanner.js b/server/scanner/AudioFileScanner.js index 1cd148f6..2a70e6a0 100644 --- a/server/scanner/AudioFileScanner.js +++ b/server/scanner/AudioFileScanner.js @@ -475,16 +475,26 @@ class AudioFileScanner { audioFiles.forEach((file) => { if (file.duration) { - const afChapters = - file.chapters?.map((c) => ({ - ...c, - id: c.id + currChapterId, - start: c.start + currStartTime, - end: c.end + currStartTime - })) ?? [] + // Multi-file audiobook may include the previous and next chapters embedded with close to 0 duration + // Filter these out and log a warning + // See https://github.com/advplyr/audiobookshelf/issues/3361 + const afChaptersCleaned = + file.chapters?.filter((c) => { + if (c.end - c.start < 0.1) { + libraryScan.addLog(LogLevel.WARN, `Chapter "${c.title}" has invalid duration of ${c.end - c.start} seconds. Skipping this chapter.`) + return false + } + return true + }) || [] + const afChapters = afChaptersCleaned.map((c) => ({ + ...c, + id: c.id + currChapterId, + start: c.start + currStartTime, + end: c.end + currStartTime + })) chapters = chapters.concat(afChapters) - currChapterId += file.chapters?.length ?? 0 + currChapterId += afChaptersCleaned.length ?? 0 currStartTime += file.duration } })