mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-20 19:06:06 +01:00
68 lines
1.8 KiB
JavaScript
68 lines
1.8 KiB
JavaScript
const Ffmpeg = require('fluent-ffmpeg')
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
Ffmpeg.setFfmpegPath(process.env.FFMPEG_PATH)
|
|
}
|
|
|
|
const { parentPort, workerData } = require("worker_threads")
|
|
const Logger = require('../Logger')
|
|
|
|
Logger.info('[DownloadWorker] Starting Worker...')
|
|
|
|
|
|
const ffmpegCommand = Ffmpeg()
|
|
const startTime = Date.now()
|
|
|
|
ffmpegCommand.input(workerData.input)
|
|
if (workerData.inputFormat) ffmpegCommand.inputFormat(workerData.inputFormat)
|
|
if (workerData.inputOption) ffmpegCommand.inputOption(workerData.inputOption)
|
|
if (workerData.options) ffmpegCommand.addOption(workerData.options)
|
|
ffmpegCommand.output(workerData.output)
|
|
|
|
var isKilled = false
|
|
|
|
async function runFfmpeg() {
|
|
var success = await new Promise((resolve) => {
|
|
ffmpegCommand.on('start', (command) => {
|
|
Logger.info('[DownloadWorker] FFMPEG concat started with command: ' + command)
|
|
})
|
|
|
|
ffmpegCommand.on('stderr', (stdErrline) => {
|
|
Logger.info(stdErrline)
|
|
})
|
|
|
|
ffmpegCommand.on('error', (err, stdout, stderr) => {
|
|
if (err.message && err.message.includes('SIGKILL')) {
|
|
// This is an intentional SIGKILL
|
|
Logger.info('[DownloadWorker] User Killed singleAudio')
|
|
} else {
|
|
Logger.error('[DownloadWorker] Ffmpeg Err', err.message)
|
|
}
|
|
resolve(false)
|
|
})
|
|
|
|
ffmpegCommand.on('end', (stdout, stderr) => {
|
|
Logger.info('[DownloadWorker] singleAudio ended')
|
|
resolve(true)
|
|
})
|
|
ffmpegCommand.run()
|
|
})
|
|
|
|
var resultMessage = {
|
|
type: 'RESULT',
|
|
isKilled,
|
|
elapsed: Date.now() - startTime,
|
|
success
|
|
}
|
|
parentPort.postMessage(resultMessage)
|
|
}
|
|
|
|
parentPort.on('message', (message) => {
|
|
if (message === 'STOP') {
|
|
Logger.info('[DownloadWorker] Requested a hard stop')
|
|
isKilled = true
|
|
ffmpegCommand.kill()
|
|
}
|
|
})
|
|
|
|
runFfmpeg() |