Fix:Setting book chapters from audio files #1052

This commit is contained in:
advplyr 2022-10-08 17:32:46 -05:00
parent bec599f325
commit d7952dab04

View File

@ -408,54 +408,29 @@ class Book {
setChapters(preferOverdriveMediaMarker = false) { setChapters(preferOverdriveMediaMarker = false) {
// If 1 audio file without chapters, then no chapters will be set // If 1 audio file without chapters, then no chapters will be set
var includedAudioFiles = this.audioFiles.filter(af => !af.exclude) var includedAudioFiles = this.audioFiles.filter(af => !af.exclude)
if (!includedAudioFiles.length) return
// If overdrive media markers are present and preferred, use those instead // If overdrive media markers are present and preferred, use those instead
if (preferOverdriveMediaMarker) { if (preferOverdriveMediaMarker) {
var overdriveChapters = parseOverdriveMediaMarkersAsChapters(includedAudioFiles) var overdriveChapters = parseOverdriveMediaMarkersAsChapters(includedAudioFiles)
if (overdriveChapters) { if (overdriveChapters) {
Logger.info('[Book] Overdrive Media Markers and preference found! Using these for chapter definitions') Logger.info('[Book] Overdrive Media Markers and preference found! Using these for chapter definitions')
return this.chapters = overdriveChapters this.chapters = overdriveChapters
return
} }
} }
if (includedAudioFiles.length === 1) { // IF first audio file has embedded chapters then use embedded chapters
// 1 audio file with chapters if (includedAudioFiles[0].chapters && includedAudioFiles[0].chapters.length) {
if (includedAudioFiles[0].chapters) { Logger.debug(`[Book] setChapters: Using embedded chapters in audio file ${includedAudioFiles[0].metadata.path}`)
this.chapters = includedAudioFiles[0].chapters.map(c => ({ ...c })) this.chapters = includedAudioFiles[0].chapters.map(c => ({ ...c }))
} } else if (includedAudioFiles.length > 1) {
} else { // Build chapters from audio files
this.chapters = [] this.chapters = []
var currChapterId = 0 var currChapterId = 0
var currStartTime = 0 var currStartTime = 0
includedAudioFiles.forEach((file) => { includedAudioFiles.forEach((file) => {
// If audio file has chapters use chapters if (file.duration) {
if (file.chapters && file.chapters.length) {
file.chapters.forEach((chapter) => {
if (currStartTime > this.duration) {
Logger.warn(`[Book] Invalid chapter start time > duration`)
} else {
var chapterAlreadyExists = this.chapters.find(ch => ch.start === currStartTime)
if (!chapterAlreadyExists) {
var chapterDuration = chapter.end - chapter.start
if (chapterDuration > 0) {
var title = `Chapter ${currChapterId}`
if (chapter.title) {
title += ` (${chapter.title})`
}
var endTime = Math.min(this.duration, currStartTime + chapterDuration)
this.chapters.push({
id: currChapterId++,
start: currStartTime,
end: endTime,
title
})
currStartTime += chapterDuration
}
}
}
})
} else if (file.duration) {
// Otherwise just use track has chapter
this.chapters.push({ this.chapters.push({
id: currChapterId++, id: currChapterId++,
start: currStartTime, start: currStartTime,