Skip to content

Commit

Permalink
Fix:Catch error with transcodes writing concat file & do not fallback…
Browse files Browse the repository at this point in the history
… to AAC encode if error message is a failure to find include file
  • Loading branch information
advplyr committed Apr 27, 2024
1 parent 3ab638e commit 1b2cf50
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
13 changes: 10 additions & 3 deletions server/objects/Stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions server/utils/ffmpegHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1b2cf50

Please sign in to comment.