From 8ffb4f88c980616c6a0f5e0731a062d0c93dc344 Mon Sep 17 00:00:00 2001 From: svd Date: Tue, 26 Oct 2021 09:38:09 +0800 Subject: [PATCH 1/3] transcoding maxSeekBackTime more; json add lastUpdate --- server/objects/Stream.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/server/objects/Stream.js b/server/objects/Stream.js index 288f51d4..48abd29e 100644 --- a/server/objects/Stream.js +++ b/server/objects/Stream.js @@ -16,6 +16,7 @@ class Stream extends EventEmitter { this.audiobook = audiobook this.segmentLength = 6 + this.maxSeekBackTime = 30 this.streamPath = Path.join(streamPath, this.id) this.concatFilesPath = Path.join(this.streamPath, 'files.txt') this.playlistPath = Path.join(this.streamPath, 'output.m3u8') @@ -67,7 +68,7 @@ class Stream extends EventEmitter { get segmentStartNumber() { if (!this.startTime) return 0 - return Math.floor(this.startTime / this.segmentLength) + return Math.floor((this.startTime >= this.maxSeekBackTime ? (this.startTime - this.maxSeekBackTime) : 0) / this.segmentLength) } get numSegments() { @@ -104,7 +105,8 @@ class Stream extends EventEmitter { clientCurrentTime: this.clientCurrentTime, startTime: this.startTime, segmentStartNumber: this.segmentStartNumber, - isTranscodeComplete: this.isTranscodeComplete + isTranscodeComplete: this.isTranscodeComplete, + lastUpdate: this.client.user.audiobooks[this.audiobook.id].lastUpdate } } @@ -239,7 +241,8 @@ class Stream extends EventEmitter { this.ffmpeg = Ffmpeg() - var trackStartTime = await writeConcatFile(this.tracks, this.concatFilesPath, this.startTime) + var timeStart = this.startTime >= this.maxSeekBackTime ? (this.startTime - this.maxSeekBackTime) : 0 + var trackStartTime = await writeConcatFile(this.tracks, this.concatFilesPath, timeStart) this.ffmpeg.addInput(this.concatFilesPath) // seek_timestamp : https://ffmpeg.org/ffmpeg.html @@ -250,8 +253,8 @@ class Stream extends EventEmitter { this.ffmpeg.inputOption('-safe 0') // this.ffmpeg.inputOption('-segment_time_metadata 1') - if (this.startTime > 0) { - const shiftedStartTime = this.startTime - trackStartTime + if (timeStart > 0) { + const shiftedStartTime = timeStart - trackStartTime // Issues using exact fractional seconds i.e. 29.49814 - changing to 29.5s var startTimeS = Math.round(shiftedStartTime * 10) / 10 + 's' Logger.info(`[STREAM] Starting Stream at startTime ${secondsToTimestamp(this.startTime)} and Segment #${this.segmentStartNumber}`) From 580b961c4a689d3132ee06ed044f2d84c42470c0 Mon Sep 17 00:00:00 2001 From: svd Date: Tue, 26 Oct 2021 11:36:23 +0800 Subject: [PATCH 2/3] log modify --- server/objects/Stream.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/objects/Stream.js b/server/objects/Stream.js index 48abd29e..1f95938e 100644 --- a/server/objects/Stream.js +++ b/server/objects/Stream.js @@ -257,7 +257,7 @@ class Stream extends EventEmitter { const shiftedStartTime = timeStart - trackStartTime // Issues using exact fractional seconds i.e. 29.49814 - changing to 29.5s var startTimeS = Math.round(shiftedStartTime * 10) / 10 + 's' - Logger.info(`[STREAM] Starting Stream at startTime ${secondsToTimestamp(this.startTime)} and Segment #${this.segmentStartNumber}`) + Logger.info(`[STREAM] Starting Stream at startTime ${secondsToTimestamp(timeStart)} (User startTime ${secondsToTimestamp(this.startTime)}) and Segment #${this.segmentStartNumber}`) this.ffmpeg.inputOption(`-ss ${startTimeS}`) this.ffmpeg.inputOption('-noaccurate_seek') From 4dd9f779e22431b13068ce671baa8e3ded72095f Mon Sep 17 00:00:00 2001 From: advplyr Date: Tue, 26 Oct 2021 16:52:45 -0500 Subject: [PATCH 3/3] Fix check user audiobook data exists when getting last update --- server/objects/Stream.js | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/server/objects/Stream.js b/server/objects/Stream.js index 1f95938e..59597369 100644 --- a/server/objects/Stream.js +++ b/server/objects/Stream.js @@ -68,7 +68,7 @@ class Stream extends EventEmitter { get segmentStartNumber() { if (!this.startTime) return 0 - return Math.floor((this.startTime >= this.maxSeekBackTime ? (this.startTime - this.maxSeekBackTime) : 0) / this.segmentLength) + return Math.floor(Math.max(this.startTime - this.maxSeekBackTime, 0) / this.segmentLength) } get numSegments() { @@ -83,6 +83,18 @@ class Stream extends EventEmitter { return this.audiobook.tracks } + get clientUser() { + return this.client.user || {} + } + + get clientUserAudiobooks() { + return this.clientUser.audiobooks || {} + } + + get clientUserAudiobookData() { + return this.clientUserAudiobooks[this.audiobookId] + } + get clientPlaylistUri() { return `/hls/${this.id}/output.m3u8` } @@ -106,18 +118,16 @@ class Stream extends EventEmitter { startTime: this.startTime, segmentStartNumber: this.segmentStartNumber, isTranscodeComplete: this.isTranscodeComplete, - lastUpdate: this.client.user.audiobooks[this.audiobook.id].lastUpdate + lastUpdate: this.clientUserAudiobookData ? this.clientUserAudiobookData.lastUpdate : 0 } } init() { - var clientUserAudiobooks = this.client.user ? this.client.user.audiobooks || {} : {} - var userAudiobook = clientUserAudiobooks[this.audiobookId] || null - if (userAudiobook) { - var timeRemaining = this.totalDuration - userAudiobook.currentTime - Logger.info('[STREAM] User has progress for audiobook', userAudiobook.progress, `Time Remaining: ${timeRemaining}s`) + if (this.clientUserAudiobookData) { + var timeRemaining = this.totalDuration - this.clientUserAudiobookData.currentTime + Logger.info('[STREAM] User has progress for audiobook', this.clientUserAudiobookData.progress, `Time Remaining: ${timeRemaining}s`) if (timeRemaining > 15) { - this.startTime = userAudiobook.currentTime + this.startTime = this.clientUserAudiobookData.currentTime this.clientCurrentTime = this.startTime } } @@ -241,23 +251,22 @@ class Stream extends EventEmitter { this.ffmpeg = Ffmpeg() - var timeStart = this.startTime >= this.maxSeekBackTime ? (this.startTime - this.maxSeekBackTime) : 0 - var trackStartTime = await writeConcatFile(this.tracks, this.concatFilesPath, timeStart) + var adjustedStartTime = Math.max(this.startTime - this.maxSeekBackTime, 0) + var trackStartTime = await writeConcatFile(this.tracks, this.concatFilesPath, adjustedStartTime) this.ffmpeg.addInput(this.concatFilesPath) // seek_timestamp : https://ffmpeg.org/ffmpeg.html // the argument to the -ss option is considered an actual timestamp, and is not offset by the start time of the file - // note: this may result in the same thing as output seeking, fixes https://github.com/advplyr/audiobookshelf/issues/116 + // fixes https://github.com/advplyr/audiobookshelf/issues/116 this.ffmpeg.inputOption('-seek_timestamp 1') this.ffmpeg.inputFormat('concat') this.ffmpeg.inputOption('-safe 0') - // this.ffmpeg.inputOption('-segment_time_metadata 1') - if (timeStart > 0) { - const shiftedStartTime = timeStart - trackStartTime + if (adjustedStartTime > 0) { + const shiftedStartTime = adjustedStartTime - trackStartTime // Issues using exact fractional seconds i.e. 29.49814 - changing to 29.5s var startTimeS = Math.round(shiftedStartTime * 10) / 10 + 's' - Logger.info(`[STREAM] Starting Stream at startTime ${secondsToTimestamp(timeStart)} (User startTime ${secondsToTimestamp(this.startTime)}) and Segment #${this.segmentStartNumber}`) + Logger.info(`[STREAM] Starting Stream at startTime ${secondsToTimestamp(adjustedStartTime)} (User startTime ${secondsToTimestamp(this.startTime)}) and Segment #${this.segmentStartNumber}`) this.ffmpeg.inputOption(`-ss ${startTimeS}`) this.ffmpeg.inputOption('-noaccurate_seek')