2021-09-04 21:17:26 +02:00
|
|
|
const Ffmpeg = require('fluent-ffmpeg')
|
|
|
|
|
2021-09-20 02:22:35 +02:00
|
|
|
if (process.env.FFMPEG_PATH) {
|
2021-09-04 21:17:26 +02:00
|
|
|
Ffmpeg.setFfmpegPath(process.env.FFMPEG_PATH)
|
|
|
|
}
|
|
|
|
|
|
|
|
const { parentPort, workerData } = require("worker_threads")
|
|
|
|
|
2021-11-10 03:30:44 +01:00
|
|
|
parentPort.postMessage({
|
|
|
|
type: 'FFMPEG',
|
|
|
|
level: 'debug',
|
|
|
|
log: '[DownloadWorker] Starting Worker...'
|
|
|
|
})
|
2021-09-04 21:17:26 +02:00
|
|
|
|
|
|
|
const ffmpegCommand = Ffmpeg()
|
|
|
|
const startTime = Date.now()
|
|
|
|
|
2021-09-08 16:15:54 +02:00
|
|
|
workerData.inputs.forEach((inputData) => {
|
|
|
|
ffmpegCommand.input(inputData.input)
|
|
|
|
if (inputData.options) ffmpegCommand.inputOption(inputData.options)
|
|
|
|
})
|
|
|
|
|
2021-09-04 21:17:26 +02:00
|
|
|
if (workerData.options) ffmpegCommand.addOption(workerData.options)
|
2021-09-12 02:59:48 +02:00
|
|
|
if (workerData.outputOptions && workerData.outputOptions.length) ffmpegCommand.addOutputOption(workerData.outputOptions)
|
2021-09-04 21:17:26 +02:00
|
|
|
ffmpegCommand.output(workerData.output)
|
|
|
|
|
|
|
|
var isKilled = false
|
|
|
|
|
|
|
|
async function runFfmpeg() {
|
|
|
|
var success = await new Promise((resolve) => {
|
|
|
|
ffmpegCommand.on('start', (command) => {
|
2021-11-10 03:30:44 +01:00
|
|
|
parentPort.postMessage({
|
|
|
|
type: 'FFMPEG',
|
|
|
|
level: 'info',
|
|
|
|
log: '[DownloadWorker] FFMPEG concat started with command: ' + command
|
|
|
|
})
|
2021-09-04 21:17:26 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
ffmpegCommand.on('stderr', (stdErrline) => {
|
2021-11-10 03:30:44 +01:00
|
|
|
parentPort.postMessage({
|
|
|
|
type: 'FFMPEG',
|
|
|
|
level: 'error',
|
|
|
|
log: '[DownloadWorker] Ffmpeg Stderr: ' + stdErrline
|
|
|
|
})
|
2021-09-04 21:17:26 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
ffmpegCommand.on('error', (err, stdout, stderr) => {
|
|
|
|
if (err.message && err.message.includes('SIGKILL')) {
|
|
|
|
// This is an intentional SIGKILL
|
2021-11-10 03:30:44 +01:00
|
|
|
parentPort.postMessage({
|
|
|
|
type: 'FFMPEG',
|
|
|
|
level: 'info',
|
|
|
|
log: '[DownloadWorker] User Killed worker'
|
|
|
|
})
|
2021-09-04 21:17:26 +02:00
|
|
|
} else {
|
2021-11-10 03:30:44 +01:00
|
|
|
parentPort.postMessage({
|
|
|
|
type: 'FFMPEG',
|
|
|
|
level: 'error',
|
|
|
|
log: '[DownloadWorker] Ffmpeg Err: ' + err.message
|
|
|
|
})
|
2021-09-04 21:17:26 +02:00
|
|
|
}
|
|
|
|
resolve(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
ffmpegCommand.on('end', (stdout, stderr) => {
|
2021-11-10 03:30:44 +01:00
|
|
|
parentPort.postMessage({
|
|
|
|
type: 'FFMPEG',
|
|
|
|
level: 'info',
|
|
|
|
log: '[DownloadWorker] worker ended'
|
|
|
|
})
|
2021-09-04 21:17:26 +02:00
|
|
|
resolve(true)
|
|
|
|
})
|
|
|
|
ffmpegCommand.run()
|
|
|
|
})
|
|
|
|
|
|
|
|
var resultMessage = {
|
|
|
|
type: 'RESULT',
|
|
|
|
isKilled,
|
|
|
|
elapsed: Date.now() - startTime,
|
|
|
|
success
|
|
|
|
}
|
|
|
|
parentPort.postMessage(resultMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
parentPort.on('message', (message) => {
|
|
|
|
if (message === 'STOP') {
|
|
|
|
isKilled = true
|
|
|
|
ffmpegCommand.kill()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
runFfmpeg()
|