From 3b01f4c0ac764ecb70efaadfd939ca868c28769c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 21 Jan 2021 14:42:43 +0100 Subject: Support progress for ffmpeg tasks --- server/helpers/ffmpeg-utils.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'server/helpers/ffmpeg-utils.ts') diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts index 6f7c186d9..a4d02908d 100644 --- a/server/helpers/ffmpeg-utils.ts +++ b/server/helpers/ffmpeg-utils.ts @@ -1,3 +1,4 @@ +import { Job } from 'bull' import * as ffmpeg from 'fluent-ffmpeg' import { readFile, remove, writeFile } from 'fs-extra' import { dirname, join } from 'path' @@ -124,6 +125,8 @@ interface BaseTranscodeOptions { resolution: VideoResolution isPortraitMode?: boolean + + job?: Job } interface HLSTranscodeOptions extends BaseTranscodeOptions { @@ -188,7 +191,7 @@ async function transcode (options: TranscodeOptions) { command = await builders[options.type](command, options) - await runCommand(command) + await runCommand(command, options.job) await fixHLSPlaylistIfNeeded(options) } @@ -611,11 +614,9 @@ function getFFmpeg (input: string, type: 'live' | 'vod') { return command } -async function runCommand (command: ffmpeg.FfmpegCommand, onEnd?: Function) { +async function runCommand (command: ffmpeg.FfmpegCommand, job?: Job) { return new Promise((res, rej) => { command.on('error', (err, stdout, stderr) => { - if (onEnd) onEnd() - logger.error('Error in transcoding job.', { stdout, stderr }) rej(err) }) @@ -623,11 +624,18 @@ async function runCommand (command: ffmpeg.FfmpegCommand, onEnd?: Function) { command.on('end', (stdout, stderr) => { logger.debug('FFmpeg command ended.', { stdout, stderr }) - if (onEnd) onEnd() - res() }) + if (job) { + command.on('progress', progress => { + if (!progress.percent) return + + job.progress(Math.round(progress.percent)) + .catch(err => logger.warn('Cannot set ffmpeg job progress.', { err })) + }) + } + command.run() }) } -- cgit v1.2.3