From 1b2cf50633f98d7c26174c191552e4b57742e7a5 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 27 Apr 2024 16:41:57 -0500 Subject: [PATCH] Fix:Catch error with transcodes writing concat file & do not fallback to AAC encode if error message is a failure to find include file --- server/objects/Stream.js | 13 ++++++++++--- server/utils/ffmpegHelpers.js | 9 +++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/server/objects/Stream.js b/server/objects/Stream.js index f7012188f4..c49372b0c7 100644 --- a/server/objects/Stream.js +++ b/server/objects/Stream.js @@ -254,8 +254,14 @@ class Stream extends EventEmitter { this.ffmpeg = Ffmpeg() this.furthestSegmentCreated = 0 - var adjustedStartTime = Math.max(this.startTime - this.maxSeekBackTime, 0) - var trackStartTime = await writeConcatFile(this.tracks, this.concatFilesPath, adjustedStartTime) + const adjustedStartTime = Math.max(this.startTime - this.maxSeekBackTime, 0) + const trackStartTime = await writeConcatFile(this.tracks, this.concatFilesPath, adjustedStartTime) + if (trackStartTime == null) { + // Close stream show error + this.ffmpeg = null + this.close('Failed to write stream concat file') + return + } this.ffmpeg.addInput(this.concatFilesPath) // seek_timestamp : https://ffmpeg.org/ffmpeg.html @@ -343,7 +349,8 @@ class Stream extends EventEmitter { // Temporary workaround for https://github.com/advplyr/audiobookshelf/issues/172 and https://github.com/advplyr/audiobookshelf/issues/2157 const aacErrorMsg = 'ffmpeg exited with code 1' - if (audioCodec === 'copy' && this.isAACEncodable && err.message?.startsWith(aacErrorMsg)) { + const errorMessageSuggestsReEncode = err.message?.startsWith(aacErrorMsg) && !err.message?.includes('No such file or directory') + if (audioCodec === 'copy' && this.isAACEncodable && errorMessageSuggestsReEncode) { Logger.info(`[Stream] Re-attempting stream with AAC encode`) this.transcodeOptions.forceAAC = true this.reset(this.startTime) diff --git a/server/utils/ffmpegHelpers.js b/server/utils/ffmpegHelpers.js index 40968465e8..7ec63302a9 100644 --- a/server/utils/ffmpegHelpers.js +++ b/server/utils/ffmpegHelpers.js @@ -35,9 +35,14 @@ async function writeConcatFile(tracks, outputPath, startTime = 0) { return line }) var inputstr = trackPaths.join('\n\n') - await fs.writeFile(outputPath, inputstr) - return firstTrackStartTime + try { + await fs.writeFile(outputPath, inputstr) + return firstTrackStartTime + } catch (error) { + Logger.error(`[ffmpegHelpers] Failed to write stream concat file at "${outputPath}"`, error) + return null + } } module.exports.writeConcatFile = writeConcatFile