From 0c9668f77901e7540e2c7045eb0f2974a4842a69 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 21 Apr 2023 14:55:10 +0200 Subject: Implement remote runner jobs in server Move ffmpeg functions to @shared --- server/helpers/core-utils.ts | 56 +---- server/helpers/custom-validators/misc.ts | 5 + server/helpers/custom-validators/runners/jobs.ts | 166 +++++++++++++ .../helpers/custom-validators/runners/runners.ts | 30 +++ server/helpers/debounce.ts | 16 ++ server/helpers/ffmpeg/codecs.ts | 64 +++++ server/helpers/ffmpeg/ffmpeg-commons.ts | 114 --------- server/helpers/ffmpeg/ffmpeg-edition.ts | 258 -------------------- server/helpers/ffmpeg/ffmpeg-encoders.ts | 116 --------- server/helpers/ffmpeg/ffmpeg-image.ts | 14 ++ server/helpers/ffmpeg/ffmpeg-images.ts | 46 ---- server/helpers/ffmpeg/ffmpeg-live.ts | 204 ---------------- server/helpers/ffmpeg/ffmpeg-options.ts | 45 ++++ server/helpers/ffmpeg/ffmpeg-presets.ts | 156 ------------ server/helpers/ffmpeg/ffmpeg-vod.ts | 267 --------------------- server/helpers/ffmpeg/ffprobe-utils.ts | 254 -------------------- server/helpers/ffmpeg/framerate.ts | 44 ++++ server/helpers/ffmpeg/index.ts | 12 +- server/helpers/image-utils.ts | 8 +- server/helpers/peertube-crypto.ts | 3 +- server/helpers/token-generator.ts | 19 ++ server/helpers/webtorrent.ts | 2 +- 22 files changed, 416 insertions(+), 1483 deletions(-) create mode 100644 server/helpers/custom-validators/runners/jobs.ts create mode 100644 server/helpers/custom-validators/runners/runners.ts create mode 100644 server/helpers/debounce.ts create mode 100644 server/helpers/ffmpeg/codecs.ts delete mode 100644 server/helpers/ffmpeg/ffmpeg-commons.ts delete mode 100644 server/helpers/ffmpeg/ffmpeg-edition.ts delete mode 100644 server/helpers/ffmpeg/ffmpeg-encoders.ts create mode 100644 server/helpers/ffmpeg/ffmpeg-image.ts delete mode 100644 server/helpers/ffmpeg/ffmpeg-images.ts delete mode 100644 server/helpers/ffmpeg/ffmpeg-live.ts create mode 100644 server/helpers/ffmpeg/ffmpeg-options.ts delete mode 100644 server/helpers/ffmpeg/ffmpeg-presets.ts delete mode 100644 server/helpers/ffmpeg/ffmpeg-vod.ts delete mode 100644 server/helpers/ffmpeg/ffprobe-utils.ts create mode 100644 server/helpers/ffmpeg/framerate.ts create mode 100644 server/helpers/token-generator.ts (limited to 'server/helpers') diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 73bd994c1..242c49e89 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts @@ -11,6 +11,7 @@ import { truncate } from 'lodash' import { pipeline } from 'stream' import { URL } from 'url' import { promisify } from 'util' +import { promisify1, promisify2, promisify3 } from '@shared/core-utils' const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { if (!oldObject || typeof oldObject !== 'object') { @@ -229,18 +230,6 @@ function execShell (command: string, options?: ExecOptions) { // --------------------------------------------------------------------------- -function isOdd (num: number) { - return (num % 2) !== 0 -} - -function toEven (num: number) { - if (isOdd(num)) return num + 1 - - return num -} - -// --------------------------------------------------------------------------- - function generateRSAKeyPairPromise (size: number) { return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => { const options: RSAKeyPairOptions<'pem', 'pem'> = { @@ -286,40 +275,6 @@ function generateED25519KeyPairPromise () { // --------------------------------------------------------------------------- -function promisify0 (func: (cb: (err: any, result: A) => void) => void): () => Promise { - return function promisified (): Promise { - return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { - func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ]) - }) - } -} - -// Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2 -function promisify1 (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise { - return function promisified (arg: T): Promise { - return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { - func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ]) - }) - } -} - -function promisify2 (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise { - return function promisified (arg1: T, arg2: U): Promise { - return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { - func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ]) - }) - } -} - -// eslint-disable-next-line max-len -function promisify3 (func: (arg1: T, arg2: U, arg3: V, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U, arg3: V) => Promise { - return function promisified (arg1: T, arg2: U, arg3: V): Promise { - return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { - func.apply(null, [ arg1, arg2, arg3, (err: any, res: A) => err ? reject(err) : resolve(res) ]) - }) - } -} - const randomBytesPromise = promisify1(randomBytes) const scryptPromise = promisify3(scrypt) const execPromise2 = promisify2(exec) @@ -345,10 +300,6 @@ export { pageToStartAndCount, peertubeTruncate, - promisify0, - promisify1, - promisify2, - scryptPromise, randomBytesPromise, @@ -360,8 +311,5 @@ export { execPromise, pipelinePromise, - parseSemVersion, - - isOdd, - toEven + parseSemVersion } diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index ebab4c6b2..fa0f469f6 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts @@ -15,6 +15,10 @@ function isSafePath (p: string) { }) } +function isSafeFilename (filename: string, extension: string) { + return typeof filename === 'string' && !!filename.match(new RegExp(`^[a-z0-9-]+\\.${extension}$`)) +} + function isSafePeerTubeFilenameWithoutExtension (filename: string) { return filename.match(/^[a-z0-9-]+$/) } @@ -177,5 +181,6 @@ export { toIntArray, isFileValid, isSafePeerTubeFilenameWithoutExtension, + isSafeFilename, checkMimetypeRegex } diff --git a/server/helpers/custom-validators/runners/jobs.ts b/server/helpers/custom-validators/runners/jobs.ts new file mode 100644 index 000000000..5f755d5bb --- /dev/null +++ b/server/helpers/custom-validators/runners/jobs.ts @@ -0,0 +1,166 @@ +import { UploadFilesForCheck } from 'express' +import validator from 'validator' +import { CONSTRAINTS_FIELDS } from '@server/initializers/constants' +import { + LiveRTMPHLSTranscodingSuccess, + RunnerJobSuccessPayload, + RunnerJobType, + RunnerJobUpdatePayload, + VODAudioMergeTranscodingSuccess, + VODHLSTranscodingSuccess, + VODWebVideoTranscodingSuccess +} from '@shared/models' +import { exists, isFileValid, isSafeFilename } from '../misc' + +const RUNNER_JOBS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.RUNNER_JOBS + +const runnerJobTypes = new Set([ 'vod-hls-transcoding', 'vod-web-video-transcoding', 'vod-audio-merge-transcoding' ]) +function isRunnerJobTypeValid (value: RunnerJobType) { + return runnerJobTypes.has(value) +} + +function isRunnerJobSuccessPayloadValid (value: RunnerJobSuccessPayload, type: RunnerJobType, files: UploadFilesForCheck) { + return isRunnerJobVODWebVideoResultPayloadValid(value as VODWebVideoTranscodingSuccess, type, files) || + isRunnerJobVODHLSResultPayloadValid(value as VODHLSTranscodingSuccess, type, files) || + isRunnerJobVODAudioMergeResultPayloadValid(value as VODHLSTranscodingSuccess, type, files) || + isRunnerJobLiveRTMPHLSResultPayloadValid(value as LiveRTMPHLSTranscodingSuccess, type) +} + +// --------------------------------------------------------------------------- + +function isRunnerJobProgressValid (value: string) { + return validator.isInt(value + '', RUNNER_JOBS_CONSTRAINTS_FIELDS.PROGRESS) +} + +function isRunnerJobUpdatePayloadValid (value: RunnerJobUpdatePayload, type: RunnerJobType, files: UploadFilesForCheck) { + return isRunnerJobVODWebVideoUpdatePayloadValid(value, type, files) || + isRunnerJobVODHLSUpdatePayloadValid(value, type, files) || + isRunnerJobVODAudioMergeUpdatePayloadValid(value, type, files) || + isRunnerJobLiveRTMPHLSUpdatePayloadValid(value, type, files) +} + +// --------------------------------------------------------------------------- + +function isRunnerJobTokenValid (value: string) { + return exists(value) && validator.isLength(value, RUNNER_JOBS_CONSTRAINTS_FIELDS.TOKEN) +} + +function isRunnerJobAbortReasonValid (value: string) { + return validator.isLength(value, RUNNER_JOBS_CONSTRAINTS_FIELDS.REASON) +} + +function isRunnerJobErrorMessageValid (value: string) { + return validator.isLength(value, RUNNER_JOBS_CONSTRAINTS_FIELDS.ERROR_MESSAGE) +} + +// --------------------------------------------------------------------------- + +export { + isRunnerJobTypeValid, + isRunnerJobSuccessPayloadValid, + isRunnerJobUpdatePayloadValid, + isRunnerJobTokenValid, + isRunnerJobErrorMessageValid, + isRunnerJobProgressValid, + isRunnerJobAbortReasonValid +} + +// --------------------------------------------------------------------------- + +function isRunnerJobVODWebVideoResultPayloadValid ( + _value: VODWebVideoTranscodingSuccess, + type: RunnerJobType, + files: UploadFilesForCheck +) { + return type === 'vod-web-video-transcoding' && + isFileValid({ files, field: 'payload[videoFile]', mimeTypeRegex: null, maxSize: null }) +} + +function isRunnerJobVODHLSResultPayloadValid ( + _value: VODHLSTranscodingSuccess, + type: RunnerJobType, + files: UploadFilesForCheck +) { + return type === 'vod-hls-transcoding' && + isFileValid({ files, field: 'payload[videoFile]', mimeTypeRegex: null, maxSize: null }) && + isFileValid({ files, field: 'payload[resolutionPlaylistFile]', mimeTypeRegex: null, maxSize: null }) +} + +function isRunnerJobVODAudioMergeResultPayloadValid ( + _value: VODAudioMergeTranscodingSuccess, + type: RunnerJobType, + files: UploadFilesForCheck +) { + return type === 'vod-audio-merge-transcoding' && + isFileValid({ files, field: 'payload[videoFile]', mimeTypeRegex: null, maxSize: null }) +} + +function isRunnerJobLiveRTMPHLSResultPayloadValid ( + value: LiveRTMPHLSTranscodingSuccess, + type: RunnerJobType +) { + return type === 'live-rtmp-hls-transcoding' && (!value || (typeof value === 'object' && Object.keys(value).length === 0)) +} + +// --------------------------------------------------------------------------- + +function isRunnerJobVODWebVideoUpdatePayloadValid ( + value: RunnerJobUpdatePayload, + type: RunnerJobType, + _files: UploadFilesForCheck +) { + return type === 'vod-web-video-transcoding' && + (!value || (typeof value === 'object' && Object.keys(value).length === 0)) +} + +function isRunnerJobVODHLSUpdatePayloadValid ( + value: RunnerJobUpdatePayload, + type: RunnerJobType, + _files: UploadFilesForCheck +) { + return type === 'vod-hls-transcoding' && + (!value || (typeof value === 'object' && Object.keys(value).length === 0)) +} + +function isRunnerJobVODAudioMergeUpdatePayloadValid ( + value: RunnerJobUpdatePayload, + type: RunnerJobType, + _files: UploadFilesForCheck +) { + return type === 'vod-audio-merge-transcoding' && + (!value || (typeof value === 'object' && Object.keys(value).length === 0)) +} + +function isRunnerJobLiveRTMPHLSUpdatePayloadValid ( + value: RunnerJobUpdatePayload, + type: RunnerJobType, + files: UploadFilesForCheck +) { + let result = type === 'live-rtmp-hls-transcoding' && !!value && !!files + + result &&= isFileValid({ files, field: 'payload[masterPlaylistFile]', mimeTypeRegex: null, maxSize: null, optional: true }) + + result &&= isFileValid({ + files, + field: 'payload[resolutionPlaylistFile]', + mimeTypeRegex: null, + maxSize: null, + optional: !value.resolutionPlaylistFilename + }) + + if (files['payload[resolutionPlaylistFile]']) { + result &&= isSafeFilename(value.resolutionPlaylistFilename, 'm3u8') + } + + return result && + isSafeFilename(value.videoChunkFilename, 'ts') && + ( + ( + value.type === 'remove-chunk' + ) || + ( + value.type === 'add-chunk' && + isFileValid({ files, field: 'payload[videoChunkFile]', mimeTypeRegex: null, maxSize: null }) + ) + ) +} diff --git a/server/helpers/custom-validators/runners/runners.ts b/server/helpers/custom-validators/runners/runners.ts new file mode 100644 index 000000000..953fac3b5 --- /dev/null +++ b/server/helpers/custom-validators/runners/runners.ts @@ -0,0 +1,30 @@ +import validator from 'validator' +import { CONSTRAINTS_FIELDS } from '@server/initializers/constants' +import { exists } from '../misc' + +const RUNNERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.RUNNERS + +function isRunnerRegistrationTokenValid (value: string) { + return exists(value) && validator.isLength(value, RUNNERS_CONSTRAINTS_FIELDS.TOKEN) +} + +function isRunnerTokenValid (value: string) { + return exists(value) && validator.isLength(value, RUNNERS_CONSTRAINTS_FIELDS.TOKEN) +} + +function isRunnerNameValid (value: string) { + return exists(value) && validator.isLength(value, RUNNERS_CONSTRAINTS_FIELDS.NAME) +} + +function isRunnerDescriptionValid (value: string) { + return exists(value) && validator.isLength(value, RUNNERS_CONSTRAINTS_FIELDS.DESCRIPTION) +} + +// --------------------------------------------------------------------------- + +export { + isRunnerRegistrationTokenValid, + isRunnerTokenValid, + isRunnerNameValid, + isRunnerDescriptionValid +} diff --git a/server/helpers/debounce.ts b/server/helpers/debounce.ts new file mode 100644 index 000000000..77d99a894 --- /dev/null +++ b/server/helpers/debounce.ts @@ -0,0 +1,16 @@ +export function Debounce (config: { timeoutMS: number }) { + let timeoutRef: NodeJS.Timeout + + return function (_target, _key, descriptor: PropertyDescriptor) { + const original = descriptor.value + + descriptor.value = function (...args: any[]) { + clearTimeout(timeoutRef) + + timeoutRef = setTimeout(() => { + original.apply(this, args) + + }, config.timeoutMS) + } + } +} diff --git a/server/helpers/ffmpeg/codecs.ts b/server/helpers/ffmpeg/codecs.ts new file mode 100644 index 000000000..3bd7db396 --- /dev/null +++ b/server/helpers/ffmpeg/codecs.ts @@ -0,0 +1,64 @@ +import { FfprobeData } from 'fluent-ffmpeg' +import { getAudioStream, getVideoStream } from '@shared/ffmpeg' +import { logger } from '../logger' +import { forceNumber } from '@shared/core-utils' + +export async function getVideoStreamCodec (path: string) { + const videoStream = await getVideoStream(path) + if (!videoStream) return '' + + const videoCodec = videoStream.codec_tag_string + + if (videoCodec === 'vp09') return 'vp09.00.50.08' + if (videoCodec === 'hev1') return 'hev1.1.6.L93.B0' + + const baseProfileMatrix = { + avc1: { + High: '6400', + Main: '4D40', + Baseline: '42E0' + }, + av01: { + High: '1', + Main: '0', + Professional: '2' + } + } + + let baseProfile = baseProfileMatrix[videoCodec][videoStream.profile] + if (!baseProfile) { + logger.warn('Cannot get video profile codec of %s.', path, { videoStream }) + baseProfile = baseProfileMatrix[videoCodec]['High'] // Fallback + } + + if (videoCodec === 'av01') { + let level = videoStream.level.toString() + if (level.length === 1) level = `0${level}` + + // Guess the tier indicator and bit depth + return `${videoCodec}.${baseProfile}.${level}M.08` + } + + let level = forceNumber(videoStream.level).toString(16) + if (level.length === 1) level = `0${level}` + + // Default, h264 codec + return `${videoCodec}.${baseProfile}${level}` +} + +export async function getAudioStreamCodec (path: string, existingProbe?: FfprobeData) { + const { audioStream } = await getAudioStream(path, existingProbe) + + if (!audioStream) return '' + + const audioCodecName = audioStream.codec_name + + if (audioCodecName === 'opus') return 'opus' + if (audioCodecName === 'vorbis') return 'vorbis' + if (audioCodecName === 'aac') return 'mp4a.40.2' + if (audioCodecName === 'mp3') return 'mp4a.40.34' + + logger.warn('Cannot get audio codec of %s.', path, { audioStream }) + + return 'mp4a.40.2' // Fallback +} diff --git a/server/helpers/ffmpeg/ffmpeg-commons.ts b/server/helpers/ffmpeg/ffmpeg-commons.ts deleted file mode 100644 index 3906a2089..000000000 --- a/server/helpers/ffmpeg/ffmpeg-commons.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { Job } from 'bullmq' -import ffmpeg, { FfmpegCommand } from 'fluent-ffmpeg' -import { execPromise } from '@server/helpers/core-utils' -import { logger, loggerTagsFactory } from '@server/helpers/logger' -import { CONFIG } from '@server/initializers/config' -import { FFMPEG_NICE } from '@server/initializers/constants' -import { EncoderOptions } from '@shared/models' - -const lTags = loggerTagsFactory('ffmpeg') - -type StreamType = 'audio' | 'video' - -function getFFmpeg (input: string, type: 'live' | 'vod') { - // We set cwd explicitly because ffmpeg appears to create temporary files when trancoding which fails in read-only file systems - const command = ffmpeg(input, { - niceness: type === 'live' ? FFMPEG_NICE.LIVE : FFMPEG_NICE.VOD, - cwd: CONFIG.STORAGE.TMP_DIR - }) - - const threads = type === 'live' - ? CONFIG.LIVE.TRANSCODING.THREADS - : CONFIG.TRANSCODING.THREADS - - if (threads > 0) { - // If we don't set any threads ffmpeg will chose automatically - command.outputOption('-threads ' + threads) - } - - return command -} - -function getFFmpegVersion () { - return new Promise((res, rej) => { - (ffmpeg() as any)._getFfmpegPath((err, ffmpegPath) => { - if (err) return rej(err) - if (!ffmpegPath) return rej(new Error('Could not find ffmpeg path')) - - return execPromise(`${ffmpegPath} -version`) - .then(stdout => { - const parsed = stdout.match(/ffmpeg version .?(\d+\.\d+(\.\d+)?)/) - if (!parsed?.[1]) return rej(new Error(`Could not find ffmpeg version in ${stdout}`)) - - // Fix ffmpeg version that does not include patch version (4.4 for example) - let version = parsed[1] - if (version.match(/^\d+\.\d+$/)) { - version += '.0' - } - - return res(version) - }) - .catch(err => rej(err)) - }) - }) -} - -async function runCommand (options: { - command: FfmpegCommand - silent?: boolean // false by default - job?: Job -}) { - const { command, silent = false, job } = options - - return new Promise((res, rej) => { - let shellCommand: string - - command.on('start', cmdline => { shellCommand = cmdline }) - - command.on('error', (err, stdout, stderr) => { - if (silent !== true) logger.error('Error in ffmpeg.', { stdout, stderr, shellCommand, ...lTags() }) - - rej(err) - }) - - command.on('end', (stdout, stderr) => { - logger.debug('FFmpeg command ended.', { stdout, stderr, shellCommand, ...lTags() }) - - res() - }) - - if (job) { - command.on('progress', progress => { - if (!progress.percent) return - - job.updateProgress(Math.round(progress.percent)) - .catch(err => logger.warn('Cannot set ffmpeg job progress.', { err, ...lTags() })) - }) - } - - command.run() - }) -} - -function buildStreamSuffix (base: string, streamNum?: number) { - if (streamNum !== undefined) { - return `${base}:${streamNum}` - } - - return base -} - -function getScaleFilter (options: EncoderOptions): string { - if (options.scaleFilter) return options.scaleFilter.name - - return 'scale' -} - -export { - getFFmpeg, - getFFmpegVersion, - runCommand, - StreamType, - buildStreamSuffix, - getScaleFilter -} diff --git a/server/helpers/ffmpeg/ffmpeg-edition.ts b/server/helpers/ffmpeg/ffmpeg-edition.ts deleted file mode 100644 index 02c5ea8de..000000000 --- a/server/helpers/ffmpeg/ffmpeg-edition.ts +++ /dev/null @@ -1,258 +0,0 @@ -import { FilterSpecification } from 'fluent-ffmpeg' -import { VIDEO_FILTERS } from '@server/initializers/constants' -import { AvailableEncoders } from '@shared/models' -import { logger, loggerTagsFactory } from '../logger' -import { getFFmpeg, runCommand } from './ffmpeg-commons' -import { presetVOD } from './ffmpeg-presets' -import { ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamDuration, getVideoStreamFPS, hasAudioStream } from './ffprobe-utils' - -const lTags = loggerTagsFactory('ffmpeg') - -async function cutVideo (options: { - inputPath: string - outputPath: string - start?: number - end?: number - - availableEncoders: AvailableEncoders - profile: string -}) { - const { inputPath, outputPath, availableEncoders, profile } = options - - logger.debug('Will cut the video.', { options, ...lTags() }) - - const mainProbe = await ffprobePromise(inputPath) - const fps = await getVideoStreamFPS(inputPath, mainProbe) - const { resolution } = await getVideoStreamDimensionsInfo(inputPath, mainProbe) - - let command = getFFmpeg(inputPath, 'vod') - .output(outputPath) - - command = await presetVOD({ - command, - input: inputPath, - availableEncoders, - profile, - resolution, - fps, - canCopyAudio: false, - canCopyVideo: false - }) - - if (options.start) { - command.outputOption('-ss ' + options.start) - } - - if (options.end) { - command.outputOption('-to ' + options.end) - } - - await runCommand({ command }) -} - -async function addWatermark (options: { - inputPath: string - watermarkPath: string - outputPath: string - - availableEncoders: AvailableEncoders - profile: string -}) { - const { watermarkPath, inputPath, outputPath, availableEncoders, profile } = options - - logger.debug('Will add watermark to the video.', { options, ...lTags() }) - - const videoProbe = await ffprobePromise(inputPath) - const fps = await getVideoStreamFPS(inputPath, videoProbe) - const { resolution } = await getVideoStreamDimensionsInfo(inputPath, videoProbe) - - let command = getFFmpeg(inputPath, 'vod') - .output(outputPath) - command.input(watermarkPath) - - command = await presetVOD({ - command, - input: inputPath, - availableEncoders, - profile, - resolution, - fps, - canCopyAudio: true, - canCopyVideo: false - }) - - const complexFilter: FilterSpecification[] = [ - // Scale watermark - { - inputs: [ '[1]', '[0]' ], - filter: 'scale2ref', - options: { - w: 'oh*mdar', - h: `ih*${VIDEO_FILTERS.WATERMARK.SIZE_RATIO}` - }, - outputs: [ '[watermark]', '[video]' ] - }, - - { - inputs: [ '[video]', '[watermark]' ], - filter: 'overlay', - options: { - x: `main_w - overlay_w - (main_h * ${VIDEO_FILTERS.WATERMARK.HORIZONTAL_MARGIN_RATIO})`, - y: `main_h * ${VIDEO_FILTERS.WATERMARK.VERTICAL_MARGIN_RATIO}` - } - } - ] - - command.complexFilter(complexFilter) - - await runCommand({ command }) -} - -async function addIntroOutro (options: { - inputPath: string - introOutroPath: string - outputPath: string - type: 'intro' | 'outro' - - availableEncoders: AvailableEncoders - profile: string -}) { - const { introOutroPath, inputPath, outputPath, availableEncoders, profile, type } = options - - logger.debug('Will add intro/outro to the video.', { options, ...lTags() }) - - const mainProbe = await ffprobePromise(inputPath) - const fps = await getVideoStreamFPS(inputPath, mainProbe) - const { resolution } = await getVideoStreamDimensionsInfo(inputPath, mainProbe) - const mainHasAudio = await hasAudioStream(inputPath, mainProbe) - - const introOutroProbe = await ffprobePromise(introOutroPath) - const introOutroHasAudio = await hasAudioStream(introOutroPath, introOutroProbe) - - let command = getFFmpeg(inputPath, 'vod') - .output(outputPath) - - command.input(introOutroPath) - - if (!introOutroHasAudio && mainHasAudio) { - const duration = await getVideoStreamDuration(introOutroPath, introOutroProbe) - - command.input('anullsrc') - command.withInputFormat('lavfi') - command.withInputOption('-t ' + duration) - } - - command = await presetVOD({ - command, - input: inputPath, - availableEncoders, - profile, - resolution, - fps, - canCopyAudio: false, - canCopyVideo: false - }) - - // Add black background to correctly scale intro/outro with padding - const complexFilter: FilterSpecification[] = [ - { - inputs: [ '1', '0' ], - filter: 'scale2ref', - options: { - w: 'iw', - h: `ih` - }, - outputs: [ 'intro-outro', 'main' ] - }, - { - inputs: [ 'intro-outro', 'main' ], - filter: 'scale2ref', - options: { - w: 'iw', - h: `ih` - }, - outputs: [ 'to-scale', 'main' ] - }, - { - inputs: 'to-scale', - filter: 'drawbox', - options: { - t: 'fill' - }, - outputs: [ 'to-scale-bg' ] - }, - { - inputs: [ '1', 'to-scale-bg' ], - filter: 'scale2ref', - options: { - w: 'iw', - h: 'ih', - force_original_aspect_ratio: 'decrease', - flags: 'spline' - }, - outputs: [ 'to-scale', 'to-scale-bg' ] - }, - { - inputs: [ 'to-scale-bg', 'to-scale' ], - filter: 'overlay', - options: { - x: '(main_w - overlay_w)/2', - y: '(main_h - overlay_h)/2' - }, - outputs: 'intro-outro-resized' - } - ] - - const concatFilter = { - inputs: [], - filter: 'concat', - options: { - n: 2, - v: 1, - unsafe: 1 - }, - outputs: [ 'v' ] - } - - const introOutroFilterInputs = [ 'intro-outro-resized' ] - const mainFilterInputs = [ 'main' ] - - if (mainHasAudio) { - mainFilterInputs.push('0:a') - - if (introOutroHasAudio) { - introOutroFilterInputs.push('1:a') - } else { - // Silent input - introOutroFilterInputs.push('2:a') - } - } - - if (type === 'intro') { - concatFilter.inputs = [ ...introOutroFilterInputs, ...mainFilterInputs ] - } else { - concatFilter.inputs = [ ...mainFilterInputs, ...introOutroFilterInputs ] - } - - if (mainHasAudio) { - concatFilter.options['a'] = 1 - concatFilter.outputs.push('a') - - command.outputOption('-map [a]') - } - - command.outputOption('-map [v]') - - complexFilter.push(concatFilter) - command.complexFilter(complexFilter) - - await runCommand({ command }) -} - -// --------------------------------------------------------------------------- - -export { - cutVideo, - addIntroOutro, - addWatermark -} diff --git a/server/helpers/ffmpeg/ffmpeg-encoders.ts b/server/helpers/ffmpeg/ffmpeg-encoders.ts deleted file mode 100644 index 5bd80ba05..000000000 --- a/server/helpers/ffmpeg/ffmpeg-encoders.ts +++ /dev/null @@ -1,116 +0,0 @@ -import { getAvailableEncoders } from 'fluent-ffmpeg' -import { pick } from '@shared/core-utils' -import { AvailableEncoders, EncoderOptionsBuilder, EncoderOptionsBuilderParams, EncoderProfile } from '@shared/models' -import { promisify0 } from '../core-utils' -import { logger, loggerTagsFactory } from '../logger' - -const lTags = loggerTagsFactory('ffmpeg') - -// Detect supported encoders by ffmpeg -let supportedEncoders: Map -async function checkFFmpegEncoders (peertubeAvailableEncoders: AvailableEncoders): Promise> { - if (supportedEncoders !== undefined) { - return supportedEncoders - } - - const getAvailableEncodersPromise = promisify0(getAvailableEncoders) - const availableFFmpegEncoders = await getAvailableEncodersPromise() - - const searchEncoders = new Set() - for (const type of [ 'live', 'vod' ]) { - for (const streamType of [ 'audio', 'video' ]) { - for (const encoder of peertubeAvailableEncoders.encodersToTry[type][streamType]) { - searchEncoders.add(encoder) - } - } - } - - supportedEncoders = new Map() - - for (const searchEncoder of searchEncoders) { - supportedEncoders.set(searchEncoder, availableFFmpegEncoders[searchEncoder] !== undefined) - } - - logger.info('Built supported ffmpeg encoders.', { supportedEncoders, searchEncoders, ...lTags() }) - - return supportedEncoders -} - -function resetSupportedEncoders () { - supportedEncoders = undefined -} - -// Run encoder builder depending on available encoders -// Try encoders by priority: if the encoder is available, run the chosen profile or fallback to the default one -// If the default one does not exist, check the next encoder -async function getEncoderBuilderResult (options: EncoderOptionsBuilderParams & { - streamType: 'video' | 'audio' - input: string - - availableEncoders: AvailableEncoders - profile: string - - videoType: 'vod' | 'live' -}) { - const { availableEncoders, profile, streamType, videoType } = options - - const encodersToTry = availableEncoders.encodersToTry[videoType][streamType] - const encoders = availableEncoders.available[videoType] - - for (const encoder of encodersToTry) { - if (!(await checkFFmpegEncoders(availableEncoders)).get(encoder)) { - logger.debug('Encoder %s not available in ffmpeg, skipping.', encoder, lTags()) - continue - } - - if (!encoders[encoder]) { - logger.debug('Encoder %s not available in peertube encoders, skipping.', encoder, lTags()) - continue - } - - // An object containing available profiles for this encoder - const builderProfiles: EncoderProfile = encoders[encoder] - let builder = builderProfiles[profile] - - if (!builder) { - logger.debug('Profile %s for encoder %s not available. Fallback to default.', profile, encoder, lTags()) - builder = builderProfiles.default - - if (!builder) { - logger.debug('Default profile for encoder %s not available. Try next available encoder.', encoder, lTags()) - continue - } - } - - const result = await builder( - pick(options, [ - 'input', - 'canCopyAudio', - 'canCopyVideo', - 'resolution', - 'inputBitrate', - 'fps', - 'inputRatio', - 'streamNum' - ]) - ) - - return { - result, - - // If we don't have output options, then copy the input stream - encoder: result.copy === true - ? 'copy' - : encoder - } - } - - return null -} - -export { - checkFFmpegEncoders, - resetSupportedEncoders, - - getEncoderBuilderResult -} diff --git a/server/helpers/ffmpeg/ffmpeg-image.ts b/server/helpers/ffmpeg/ffmpeg-image.ts new file mode 100644 index 000000000..0bb0ff2c0 --- /dev/null +++ b/server/helpers/ffmpeg/ffmpeg-image.ts @@ -0,0 +1,14 @@ +import { FFmpegImage } from '@shared/ffmpeg' +import { getFFmpegCommandWrapperOptions } from './ffmpeg-options' + +export function processGIF (options: Parameters[0]) { + return new FFmpegImage(getFFmpegCommandWrapperOptions('thumbnail')).processGIF(options) +} + +export function generateThumbnailFromVideo (options: Parameters[0]) { + return new FFmpegImage(getFFmpegCommandWrapperOptions('thumbnail')).generateThumbnailFromVideo(options) +} + +export function convertWebPToJPG (options: Parameters[0]) { + return new FFmpegImage(getFFmpegCommandWrapperOptions('thumbnail')).convertWebPToJPG(options) +} diff --git a/server/helpers/ffmpeg/ffmpeg-images.ts b/server/helpers/ffmpeg/ffmpeg-images.ts deleted file mode 100644 index 7f64c6d0a..000000000 --- a/server/helpers/ffmpeg/ffmpeg-images.ts +++ /dev/null @@ -1,46 +0,0 @@ -import ffmpeg from 'fluent-ffmpeg' -import { FFMPEG_NICE } from '@server/initializers/constants' -import { runCommand } from './ffmpeg-commons' - -function convertWebPToJPG (path: string, destination: string): Promise { - const command = ffmpeg(path, { niceness: FFMPEG_NICE.THUMBNAIL }) - .output(destination) - - return runCommand({ command, silent: true }) -} - -function processGIF ( - path: string, - destination: string, - newSize: { width: number, height: number } -): Promise { - const command = ffmpeg(path, { niceness: FFMPEG_NICE.THUMBNAIL }) - .fps(20) - .size(`${newSize.width}x${newSize.height}`) - .output(destination) - - return runCommand({ command }) -} - -async function generateThumbnailFromVideo (fromPath: string, folder: string, imageName: string) { - const pendingImageName = 'pending-' + imageName - - const options = { - filename: pendingImageName, - count: 1, - folder - } - - return new Promise((res, rej) => { - ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL }) - .on('error', rej) - .on('end', () => res(imageName)) - .thumbnail(options) - }) -} - -export { - convertWebPToJPG, - processGIF, - generateThumbnailFromVideo -} diff --git a/server/helpers/ffmpeg/ffmpeg-live.ts b/server/helpers/ffmpeg/ffmpeg-live.ts deleted file mode 100644 index 379d7b1ad..000000000 --- a/server/helpers/ffmpeg/ffmpeg-live.ts +++ /dev/null @@ -1,204 +0,0 @@ -import { FfmpegCommand, FilterSpecification } from 'fluent-ffmpeg' -import { join } from 'path' -import { VIDEO_LIVE } from '@server/initializers/constants' -import { AvailableEncoders, LiveVideoLatencyMode } from '@shared/models' -import { logger, loggerTagsFactory } from '../logger' -import { buildStreamSuffix, getFFmpeg, getScaleFilter, StreamType } from './ffmpeg-commons' -import { getEncoderBuilderResult } from './ffmpeg-encoders' -import { addDefaultEncoderGlobalParams, addDefaultEncoderParams, applyEncoderOptions } from './ffmpeg-presets' -import { computeFPS } from './ffprobe-utils' - -const lTags = loggerTagsFactory('ffmpeg') - -async function getLiveTranscodingCommand (options: { - inputUrl: string - - outPath: string - masterPlaylistName: string - latencyMode: LiveVideoLatencyMode - - resolutions: number[] - - // Input information - fps: number - bitrate: number - ratio: number - hasAudio: boolean - - availableEncoders: AvailableEncoders - profile: string -}) { - const { - inputUrl, - outPath, - resolutions, - fps, - bitrate, - availableEncoders, - profile, - masterPlaylistName, - ratio, - latencyMode, - hasAudio - } = options - - const command = getFFmpeg(inputUrl, 'live') - - const varStreamMap: string[] = [] - - const complexFilter: FilterSpecification[] = [ - { - inputs: '[v:0]', - filter: 'split', - options: resolutions.length, - outputs: resolutions.map(r => `vtemp${r}`) - } - ] - - command.outputOption('-sc_threshold 0') - - addDefaultEncoderGlobalParams(command) - - for (let i = 0; i < resolutions.length; i++) { - const streamMap: string[] = [] - const resolution = resolutions[i] - const resolutionFPS = computeFPS(fps, resolution) - - const baseEncoderBuilderParams = { - input: inputUrl, - - availableEncoders, - profile, - - canCopyAudio: true, - canCopyVideo: true, - - inputBitrate: bitrate, - inputRatio: ratio, - - resolution, - fps: resolutionFPS, - - streamNum: i, - videoType: 'live' as 'live' - } - - { - const streamType: StreamType = 'video' - const builderResult = await getEncoderBuilderResult({ ...baseEncoderBuilderParams, streamType }) - if (!builderResult) { - throw new Error('No available live video encoder found') - } - - command.outputOption(`-map [vout${resolution}]`) - - addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i }) - - logger.debug( - 'Apply ffmpeg live video params from %s using %s profile.', builderResult.encoder, profile, - { builderResult, fps: resolutionFPS, resolution, ...lTags() } - ) - - command.outputOption(`${buildStreamSuffix('-c:v', i)} ${builderResult.encoder}`) - applyEncoderOptions(command, builderResult.result) - - complexFilter.push({ - inputs: `vtemp${resolution}`, - filter: getScaleFilter(builderResult.result), - options: `w=-2:h=${resolution}`, - outputs: `vout${resolution}` - }) - - streamMap.push(`v:${i}`) - } - - if (hasAudio) { - const streamType: StreamType = 'audio' - const builderResult = await getEncoderBuilderResult({ ...baseEncoderBuilderParams, streamType }) - if (!builderResult) { - throw new Error('No available live audio encoder found') - } - - command.outputOption('-map a:0') - - addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i }) - - logger.debug( - 'Apply ffmpeg live audio params from %s using %s profile.', builderResult.encoder, profile, - { builderResult, fps: resolutionFPS, resolution, ...lTags() } - ) - - command.outputOption(`${buildStreamSuffix('-c:a', i)} ${builderResult.encoder}`) - applyEncoderOptions(command, builderResult.result) - - streamMap.push(`a:${i}`) - } - - varStreamMap.push(streamMap.join(',')) - } - - command.complexFilter(complexFilter) - - addDefaultLiveHLSParams({ command, outPath, masterPlaylistName, latencyMode }) - - command.outputOption('-var_stream_map', varStreamMap.join(' ')) - - return command -} - -function getLiveMuxingCommand (options: { - inputUrl: string - outPath: string - masterPlaylistName: string - latencyMode: LiveVideoLatencyMode -}) { - const { inputUrl, outPath, masterPlaylistName, latencyMode } = options - - const command = getFFmpeg(inputUrl, 'live') - - command.outputOption('-c:v copy') - command.outputOption('-c:a copy') - command.outputOption('-map 0:a?') - command.outputOption('-map 0:v?') - - addDefaultLiveHLSParams({ command, outPath, masterPlaylistName, latencyMode }) - - return command -} - -function getLiveSegmentTime (latencyMode: LiveVideoLatencyMode) { - if (latencyMode === LiveVideoLatencyMode.SMALL_LATENCY) { - return VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY - } - - return VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY -} - -// --------------------------------------------------------------------------- - -export { - getLiveSegmentTime, - - getLiveTranscodingCommand, - getLiveMuxingCommand -} - -// --------------------------------------------------------------------------- - -function addDefaultLiveHLSParams (options: { - command: FfmpegCommand - outPath: string - masterPlaylistName: string - latencyMode: LiveVideoLatencyMode -}) { - const { command, outPath, masterPlaylistName, latencyMode } = options - - command.outputOption('-hls_time ' + getLiveSegmentTime(latencyMode)) - command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE) - command.outputOption('-hls_flags delete_segments+independent_segments+program_date_time') - command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`) - command.outputOption('-master_pl_name ' + masterPlaylistName) - command.outputOption(`-f hls`) - - command.output(join(outPath, '%v.m3u8')) -} diff --git a/server/helpers/ffmpeg/ffmpeg-options.ts b/server/helpers/ffmpeg/ffmpeg-options.ts new file mode 100644 index 000000000..db6350d39 --- /dev/null +++ b/server/helpers/ffmpeg/ffmpeg-options.ts @@ -0,0 +1,45 @@ +import { logger } from '@server/helpers/logger' +import { CONFIG } from '@server/initializers/config' +import { FFMPEG_NICE } from '@server/initializers/constants' +import { FFmpegCommandWrapperOptions } from '@shared/ffmpeg' +import { AvailableEncoders } from '@shared/models' + +type CommandType = 'live' | 'vod' | 'thumbnail' + +export function getFFmpegCommandWrapperOptions (type: CommandType, availableEncoders?: AvailableEncoders): FFmpegCommandWrapperOptions { + return { + availableEncoders, + profile: getProfile(type), + + niceness: FFMPEG_NICE[type], + tmpDirectory: CONFIG.STORAGE.TMP_DIR, + threads: getThreads(type), + + logger: { + debug: logger.debug.bind(logger), + info: logger.info.bind(logger), + warn: logger.warn.bind(logger), + error: logger.error.bind(logger) + }, + lTags: { tags: [ 'ffmpeg' ] } + } +} + +// --------------------------------------------------------------------------- +// Private +// --------------------------------------------------------------------------- + +function getThreads (type: CommandType) { + if (type === 'live') return CONFIG.LIVE.TRANSCODING.THREADS + if (type === 'vod') return CONFIG.TRANSCODING.THREADS + + // Auto + return 0 +} + +function getProfile (type: CommandType) { + if (type === 'live') return CONFIG.LIVE.TRANSCODING.PROFILE + if (type === 'vod') return CONFIG.TRANSCODING.PROFILE + + return undefined +} diff --git a/server/helpers/ffmpeg/ffmpeg-presets.ts b/server/helpers/ffmpeg/ffmpeg-presets.ts deleted file mode 100644 index d1160a4a2..000000000 --- a/server/helpers/ffmpeg/ffmpeg-presets.ts +++ /dev/null @@ -1,156 +0,0 @@ -import { FfmpegCommand } from 'fluent-ffmpeg' -import { logger, loggerTagsFactory } from '@server/helpers/logger' -import { pick } from '@shared/core-utils' -import { AvailableEncoders, EncoderOptions } from '@shared/models' -import { buildStreamSuffix, getScaleFilter, StreamType } from './ffmpeg-commons' -import { getEncoderBuilderResult } from './ffmpeg-encoders' -import { ffprobePromise, getVideoStreamBitrate, getVideoStreamDimensionsInfo, hasAudioStream } from './ffprobe-utils' - -const lTags = loggerTagsFactory('ffmpeg') - -// --------------------------------------------------------------------------- - -function addDefaultEncoderGlobalParams (command: FfmpegCommand) { - // avoid issues when transcoding some files: https://trac.ffmpeg.org/ticket/6375 - command.outputOption('-max_muxing_queue_size 1024') - // strip all metadata - .outputOption('-map_metadata -1') - // allows import of source material with incompatible pixel formats (e.g. MJPEG video) - .outputOption('-pix_fmt yuv420p') -} - -function addDefaultEncoderParams (options: { - command: FfmpegCommand - encoder: 'libx264' | string - fps: number - - streamNum?: number -}) { - const { command, encoder, fps, streamNum } = options - - if (encoder === 'libx264') { - // 3.1 is the minimal resource allocation for our highest supported resolution - command.outputOption(buildStreamSuffix('-level:v', streamNum) + ' 3.1') - - if (fps) { - // Keyframe interval of 2 seconds for faster seeking and resolution switching. - // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html - // https://superuser.com/a/908325 - command.outputOption(buildStreamSuffix('-g:v', streamNum) + ' ' + (fps * 2)) - } - } -} - -// --------------------------------------------------------------------------- - -async function presetVOD (options: { - command: FfmpegCommand - input: string - - availableEncoders: AvailableEncoders - profile: string - - canCopyAudio: boolean - canCopyVideo: boolean - - resolution: number - fps: number - - scaleFilterValue?: string -}) { - const { command, input, profile, resolution, fps, scaleFilterValue } = options - - let localCommand = command - .format('mp4') - .outputOption('-movflags faststart') - - addDefaultEncoderGlobalParams(command) - - const probe = await ffprobePromise(input) - - // Audio encoder - const bitrate = await getVideoStreamBitrate(input, probe) - const videoStreamDimensions = await getVideoStreamDimensionsInfo(input, probe) - - let streamsToProcess: StreamType[] = [ 'audio', 'video' ] - - if (!await hasAudioStream(input, probe)) { - localCommand = localCommand.noAudio() - streamsToProcess = [ 'video' ] - } - - for (const streamType of streamsToProcess) { - const builderResult = await getEncoderBuilderResult({ - ...pick(options, [ 'availableEncoders', 'canCopyAudio', 'canCopyVideo' ]), - - input, - inputBitrate: bitrate, - inputRatio: videoStreamDimensions?.ratio || 0, - - profile, - resolution, - fps, - streamType, - - videoType: 'vod' as 'vod' - }) - - if (!builderResult) { - throw new Error('No available encoder found for stream ' + streamType) - } - - logger.debug( - 'Apply ffmpeg params from %s for %s stream of input %s using %s profile.', - builderResult.encoder, streamType, input, profile, - { builderResult, resolution, fps, ...lTags() } - ) - - if (streamType === 'video') { - localCommand.videoCodec(builderResult.encoder) - - if (scaleFilterValue) { - localCommand.outputOption(`-vf ${getScaleFilter(builderResult.result)}=${scaleFilterValue}`) - } - } else if (streamType === 'audio') { - localCommand.audioCodec(builderResult.encoder) - } - - applyEncoderOptions(localCommand, builderResult.result) - addDefaultEncoderParams({ command: localCommand, encoder: builderResult.encoder, fps }) - } - - return localCommand -} - -function presetCopy (command: FfmpegCommand): FfmpegCommand { - return command - .format('mp4') - .videoCodec('copy') - .audioCodec('copy') -} - -function presetOnlyAudio (command: FfmpegCommand): FfmpegCommand { - return command - .format('mp4') - .audioCodec('copy') - .noVideo() -} - -function applyEncoderOptions (command: FfmpegCommand, options: EncoderOptions): FfmpegCommand { - return command - .inputOptions(options.inputOptions ?? []) - .outputOptions(options.outputOptions ?? []) -} - -// --------------------------------------------------------------------------- - -export { - presetVOD, - presetCopy, - presetOnlyAudio, - - addDefaultEncoderGlobalParams, - addDefaultEncoderParams, - - applyEncoderOptions -} diff --git a/server/helpers/ffmpeg/ffmpeg-vod.ts b/server/helpers/ffmpeg/ffmpeg-vod.ts deleted file mode 100644 index d84703eb9..000000000 --- a/server/helpers/ffmpeg/ffmpeg-vod.ts +++ /dev/null @@ -1,267 +0,0 @@ -import { MutexInterface } from 'async-mutex' -import { Job } from 'bullmq' -import { FfmpegCommand } from 'fluent-ffmpeg' -import { readFile, writeFile } from 'fs-extra' -import { dirname } from 'path' -import { VIDEO_TRANSCODING_FPS } from '@server/initializers/constants' -import { pick } from '@shared/core-utils' -import { AvailableEncoders, VideoResolution } from '@shared/models' -import { logger, loggerTagsFactory } from '../logger' -import { getFFmpeg, runCommand } from './ffmpeg-commons' -import { presetCopy, presetOnlyAudio, presetVOD } from './ffmpeg-presets' -import { computeFPS, ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamFPS } from './ffprobe-utils' - -const lTags = loggerTagsFactory('ffmpeg') - -// --------------------------------------------------------------------------- - -type TranscodeVODOptionsType = 'hls' | 'hls-from-ts' | 'quick-transcode' | 'video' | 'merge-audio' | 'only-audio' - -interface BaseTranscodeVODOptions { - type: TranscodeVODOptionsType - - inputPath: string - outputPath: string - - // Will be released after the ffmpeg started - // To prevent a bug where the input file does not exist anymore when running ffmpeg - inputFileMutexReleaser: MutexInterface.Releaser - - availableEncoders: AvailableEncoders - profile: string - - resolution: number - - job?: Job -} - -interface HLSTranscodeOptions extends BaseTranscodeVODOptions { - type: 'hls' - copyCodecs: boolean - hlsPlaylist: { - videoFilename: string - } -} - -interface HLSFromTSTranscodeOptions extends BaseTranscodeVODOptions { - type: 'hls-from-ts' - - isAAC: boolean - - hlsPlaylist: { - videoFilename: string - } -} - -interface QuickTranscodeOptions extends BaseTranscodeVODOptions { - type: 'quick-transcode' -} - -interface VideoTranscodeOptions extends BaseTranscodeVODOptions { - type: 'video' -} - -interface MergeAudioTranscodeOptions extends BaseTranscodeVODOptions { - type: 'merge-audio' - audioPath: string -} - -interface OnlyAudioTranscodeOptions extends BaseTranscodeVODOptions { - type: 'only-audio' -} - -type TranscodeVODOptions = - HLSTranscodeOptions - | HLSFromTSTranscodeOptions - | VideoTranscodeOptions - | MergeAudioTranscodeOptions - | OnlyAudioTranscodeOptions - | QuickTranscodeOptions - -// --------------------------------------------------------------------------- - -const builders: { - [ type in TranscodeVODOptionsType ]: (c: FfmpegCommand, o?: TranscodeVODOptions) => Promise | FfmpegCommand -} = { - 'quick-transcode': buildQuickTranscodeCommand, - 'hls': buildHLSVODCommand, - 'hls-from-ts': buildHLSVODFromTSCommand, - 'merge-audio': buildAudioMergeCommand, - 'only-audio': buildOnlyAudioCommand, - 'video': buildVODCommand -} - -async function transcodeVOD (options: TranscodeVODOptions) { - logger.debug('Will run transcode.', { options, ...lTags() }) - - let command = getFFmpeg(options.inputPath, 'vod') - .output(options.outputPath) - - command = await builders[options.type](command, options) - - command.on('start', () => { - setTimeout(() => { - options.inputFileMutexReleaser() - }, 1000) - }) - - await runCommand({ command, job: options.job }) - - await fixHLSPlaylistIfNeeded(options) -} - -// --------------------------------------------------------------------------- - -export { - transcodeVOD, - - buildVODCommand, - - TranscodeVODOptions, - TranscodeVODOptionsType -} - -// --------------------------------------------------------------------------- - -async function buildVODCommand (command: FfmpegCommand, options: TranscodeVODOptions) { - const probe = await ffprobePromise(options.inputPath) - - let fps = await getVideoStreamFPS(options.inputPath, probe) - fps = computeFPS(fps, options.resolution) - - let scaleFilterValue: string - - if (options.resolution !== undefined) { - const videoStreamInfo = await getVideoStreamDimensionsInfo(options.inputPath, probe) - - scaleFilterValue = videoStreamInfo?.isPortraitMode === true - ? `w=${options.resolution}:h=-2` - : `w=-2:h=${options.resolution}` - } - - command = await presetVOD({ - ...pick(options, [ 'resolution', 'availableEncoders', 'profile' ]), - - command, - input: options.inputPath, - canCopyAudio: true, - canCopyVideo: true, - fps, - scaleFilterValue - }) - - return command -} - -function buildQuickTranscodeCommand (command: FfmpegCommand) { - command = presetCopy(command) - - command = command.outputOption('-map_metadata -1') // strip all metadata - .outputOption('-movflags faststart') - - return command -} - -// --------------------------------------------------------------------------- -// Audio transcoding -// --------------------------------------------------------------------------- - -async function buildAudioMergeCommand (command: FfmpegCommand, options: MergeAudioTranscodeOptions) { - command = command.loop(undefined) - - const scaleFilterValue = getMergeAudioScaleFilterValue() - command = await presetVOD({ - ...pick(options, [ 'resolution', 'availableEncoders', 'profile' ]), - - command, - input: options.audioPath, - canCopyAudio: true, - canCopyVideo: true, - fps: VIDEO_TRANSCODING_FPS.AUDIO_MERGE, - scaleFilterValue - }) - - command.outputOption('-preset:v veryfast') - - command = command.input(options.audioPath) - .outputOption('-tune stillimage') - .outputOption('-shortest') - - return command -} - -function buildOnlyAudioCommand (command: FfmpegCommand, _options: OnlyAudioTranscodeOptions) { - command = presetOnlyAudio(command) - - return command -} - -// --------------------------------------------------------------------------- -// HLS transcoding -// --------------------------------------------------------------------------- - -async function buildHLSVODCommand (command: FfmpegCommand, options: HLSTranscodeOptions) { - const videoPath = getHLSVideoPath(options) - - if (options.copyCodecs) command = presetCopy(command) - else if (options.resolution === VideoResolution.H_NOVIDEO) command = presetOnlyAudio(command) - else command = await buildVODCommand(command, options) - - addCommonHLSVODCommandOptions(command, videoPath) - - return command -} - -function buildHLSVODFromTSCommand (command: FfmpegCommand, options: HLSFromTSTranscodeOptions) { - const videoPath = getHLSVideoPath(options) - - command.outputOption('-c copy') - - if (options.isAAC) { - // Required for example when copying an AAC stream from an MPEG-TS - // Since it's a bitstream filter, we don't need to reencode the audio - command.outputOption('-bsf:a aac_adtstoasc') - } - - addCommonHLSVODCommandOptions(command, videoPath) - - return command -} - -function addCommonHLSVODCommandOptions (command: FfmpegCommand, outputPath: string) { - return command.outputOption('-hls_time 4') - .outputOption('-hls_list_size 0') - .outputOption('-hls_playlist_type vod') - .outputOption('-hls_segment_filename ' + outputPath) - .outputOption('-hls_segment_type fmp4') - .outputOption('-f hls') - .outputOption('-hls_flags single_file') -} - -async function fixHLSPlaylistIfNeeded (options: TranscodeVODOptions) { - if (options.type !== 'hls' && options.type !== 'hls-from-ts') return - - const fileContent = await readFile(options.outputPath) - - const videoFileName = options.hlsPlaylist.videoFilename - const videoFilePath = getHLSVideoPath(options) - - // Fix wrong mapping with some ffmpeg versions - const newContent = fileContent.toString() - .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`) - - await writeFile(options.outputPath, newContent) -} - -// --------------------------------------------------------------------------- -// Helpers -// --------------------------------------------------------------------------- - -function getHLSVideoPath (options: HLSTranscodeOptions | HLSFromTSTranscodeOptions) { - return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}` -} - -// Avoid "height not divisible by 2" error -function getMergeAudioScaleFilterValue () { - return 'trunc(iw/2)*2:trunc(ih/2)*2' -} diff --git a/server/helpers/ffmpeg/ffprobe-utils.ts b/server/helpers/ffmpeg/ffprobe-utils.ts deleted file mode 100644 index fb270b3cb..000000000 --- a/server/helpers/ffmpeg/ffprobe-utils.ts +++ /dev/null @@ -1,254 +0,0 @@ -import { FfprobeData } from 'fluent-ffmpeg' -import { getMaxBitrate } from '@shared/core-utils' -import { - buildFileMetadata, - ffprobePromise, - getAudioStream, - getMaxAudioBitrate, - getVideoStream, - getVideoStreamBitrate, - getVideoStreamDimensionsInfo, - getVideoStreamDuration, - getVideoStreamFPS, - hasAudioStream -} from '@shared/extra-utils/ffprobe' -import { VideoResolution, VideoTranscodingFPS } from '@shared/models' -import { CONFIG } from '../../initializers/config' -import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants' -import { toEven } from '../core-utils' -import { logger } from '../logger' - -/** - * - * Helpers to run ffprobe and extract data from the JSON output - * - */ - -// --------------------------------------------------------------------------- -// Codecs -// --------------------------------------------------------------------------- - -async function getVideoStreamCodec (path: string) { - const videoStream = await getVideoStream(path) - if (!videoStream) return '' - - const videoCodec = videoStream.codec_tag_string - - if (videoCodec === 'vp09') return 'vp09.00.50.08' - if (videoCodec === 'hev1') return 'hev1.1.6.L93.B0' - - const baseProfileMatrix = { - avc1: { - High: '6400', - Main: '4D40', - Baseline: '42E0' - }, - av01: { - High: '1', - Main: '0', - Professional: '2' - } - } - - let baseProfile = baseProfileMatrix[videoCodec][videoStream.profile] - if (!baseProfile) { - logger.warn('Cannot get video profile codec of %s.', path, { videoStream }) - baseProfile = baseProfileMatrix[videoCodec]['High'] // Fallback - } - - if (videoCodec === 'av01') { - let level = videoStream.level.toString() - if (level.length === 1) level = `0${level}` - - // Guess the tier indicator and bit depth - return `${videoCodec}.${baseProfile}.${level}M.08` - } - - let level = videoStream.level.toString(16) - if (level.length === 1) level = `0${level}` - - // Default, h264 codec - return `${videoCodec}.${baseProfile}${level}` -} - -async function getAudioStreamCodec (path: string, existingProbe?: FfprobeData) { - const { audioStream } = await getAudioStream(path, existingProbe) - - if (!audioStream) return '' - - const audioCodecName = audioStream.codec_name - - if (audioCodecName === 'opus') return 'opus' - if (audioCodecName === 'vorbis') return 'vorbis' - if (audioCodecName === 'aac') return 'mp4a.40.2' - if (audioCodecName === 'mp3') return 'mp4a.40.34' - - logger.warn('Cannot get audio codec of %s.', path, { audioStream }) - - return 'mp4a.40.2' // Fallback -} - -// --------------------------------------------------------------------------- -// Resolutions -// --------------------------------------------------------------------------- - -function computeResolutionsToTranscode (options: { - input: number - type: 'vod' | 'live' - includeInput: boolean - strictLower: boolean - hasAudio: boolean -}) { - const { input, type, includeInput, strictLower, hasAudio } = options - - const configResolutions = type === 'vod' - ? CONFIG.TRANSCODING.RESOLUTIONS - : CONFIG.LIVE.TRANSCODING.RESOLUTIONS - - const resolutionsEnabled = new Set() - - // Put in the order we want to proceed jobs - const availableResolutions: VideoResolution[] = [ - VideoResolution.H_NOVIDEO, - VideoResolution.H_480P, - VideoResolution.H_360P, - VideoResolution.H_720P, - VideoResolution.H_240P, - VideoResolution.H_144P, - VideoResolution.H_1080P, - VideoResolution.H_1440P, - VideoResolution.H_4K - ] - - for (const resolution of availableResolutions) { - // Resolution not enabled - if (configResolutions[resolution + 'p'] !== true) continue - // Too big resolution for input file - if (input < resolution) continue - // We only want lower resolutions than input file - if (strictLower && input === resolution) continue - // Audio resolutio but no audio in the video - if (resolution === VideoResolution.H_NOVIDEO && !hasAudio) continue - - resolutionsEnabled.add(resolution) - } - - if (includeInput) { - // Always use an even resolution to avoid issues with ffmpeg - resolutionsEnabled.add(toEven(input)) - } - - return Array.from(resolutionsEnabled) -} - -// --------------------------------------------------------------------------- -// Can quick transcode -// --------------------------------------------------------------------------- - -async function canDoQuickTranscode (path: string): Promise { - if (CONFIG.TRANSCODING.PROFILE !== 'default') return false - - const probe = await ffprobePromise(path) - - return await canDoQuickVideoTranscode(path, probe) && - await canDoQuickAudioTranscode(path, probe) -} - -async function canDoQuickAudioTranscode (path: string, probe?: FfprobeData): Promise { - const parsedAudio = await getAudioStream(path, probe) - - if (!parsedAudio.audioStream) return true - - if (parsedAudio.audioStream['codec_name'] !== 'aac') return false - - const audioBitrate = parsedAudio.bitrate - if (!audioBitrate) return false - - const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate) - if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false - - const channelLayout = parsedAudio.audioStream['channel_layout'] - // Causes playback issues with Chrome - if (!channelLayout || channelLayout === 'unknown' || channelLayout === 'quad') return false - - return true -} - -async function canDoQuickVideoTranscode (path: string, probe?: FfprobeData): Promise { - const videoStream = await getVideoStream(path, probe) - const fps = await getVideoStreamFPS(path, probe) - const bitRate = await getVideoStreamBitrate(path, probe) - const resolutionData = await getVideoStreamDimensionsInfo(path, probe) - - // If ffprobe did not manage to guess the bitrate - if (!bitRate) return false - - // check video params - if (!videoStream) return false - if (videoStream['codec_name'] !== 'h264') return false - if (videoStream['pix_fmt'] !== 'yuv420p') return false - if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false - if (bitRate > getMaxBitrate({ ...resolutionData, fps })) return false - - return true -} - -// --------------------------------------------------------------------------- -// Framerate -// --------------------------------------------------------------------------- - -function getClosestFramerateStandard > (fps: number, type: K) { - return VIDEO_TRANSCODING_FPS[type].slice(0) - .sort((a, b) => fps % a - fps % b)[0] -} - -function computeFPS (fpsArg: number, resolution: VideoResolution) { - let fps = fpsArg - - if ( - // On small/medium resolutions, limit FPS - resolution !== undefined && - resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && - fps > VIDEO_TRANSCODING_FPS.AVERAGE - ) { - // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value - fps = getClosestFramerateStandard(fps, 'STANDARD') - } - - // Hard FPS limits - if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD') - - if (fps < VIDEO_TRANSCODING_FPS.MIN) { - throw new Error(`Cannot compute FPS because ${fps} is lower than our minimum value ${VIDEO_TRANSCODING_FPS.MIN}`) - } - - return fps -} - -// --------------------------------------------------------------------------- - -export { - // Re export ffprobe utils - getVideoStreamDimensionsInfo, - buildFileMetadata, - getMaxAudioBitrate, - getVideoStream, - getVideoStreamDuration, - getAudioStream, - hasAudioStream, - getVideoStreamFPS, - ffprobePromise, - getVideoStreamBitrate, - - getVideoStreamCodec, - getAudioStreamCodec, - - computeFPS, - getClosestFramerateStandard, - - computeResolutionsToTranscode, - - canDoQuickTranscode, - canDoQuickVideoTranscode, - canDoQuickAudioTranscode -} diff --git a/server/helpers/ffmpeg/framerate.ts b/server/helpers/ffmpeg/framerate.ts new file mode 100644 index 000000000..18cb0e0e2 --- /dev/null +++ b/server/helpers/ffmpeg/framerate.ts @@ -0,0 +1,44 @@ +import { VIDEO_TRANSCODING_FPS } from '@server/initializers/constants' +import { VideoResolution } from '@shared/models' + +export function computeOutputFPS (options: { + inputFPS: number + resolution: VideoResolution +}) { + const { resolution } = options + + let fps = options.inputFPS + + if ( + // On small/medium resolutions, limit FPS + resolution !== undefined && + resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && + fps > VIDEO_TRANSCODING_FPS.AVERAGE + ) { + // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value + fps = getClosestFramerateStandard({ fps, type: 'STANDARD' }) + } + + // Hard FPS limits + if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard({ fps, type: 'HD_STANDARD' }) + + if (fps < VIDEO_TRANSCODING_FPS.MIN) { + throw new Error(`Cannot compute FPS because ${fps} is lower than our minimum value ${VIDEO_TRANSCODING_FPS.MIN}`) + } + + return fps +} + +// --------------------------------------------------------------------------- +// Private +// --------------------------------------------------------------------------- + +function getClosestFramerateStandard (options: { + fps: number + type: 'HD_STANDARD' | 'STANDARD' +}) { + const { fps, type } = options + + return VIDEO_TRANSCODING_FPS[type].slice(0) + .sort((a, b) => fps % a - fps % b)[0] +} diff --git a/server/helpers/ffmpeg/index.ts b/server/helpers/ffmpeg/index.ts index e3bb2013f..bf1c73fb6 100644 --- a/server/helpers/ffmpeg/index.ts +++ b/server/helpers/ffmpeg/index.ts @@ -1,8 +1,4 @@ -export * from './ffmpeg-commons' -export * from './ffmpeg-edition' -export * from './ffmpeg-encoders' -export * from './ffmpeg-images' -export * from './ffmpeg-live' -export * from './ffmpeg-presets' -export * from './ffmpeg-vod' -export * from './ffprobe-utils' +export * from './codecs' +export * from './ffmpeg-image' +export * from './ffmpeg-options' +export * from './framerate' diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts index bbd4692ef..05b258d8a 100644 --- a/server/helpers/image-utils.ts +++ b/server/helpers/image-utils.ts @@ -3,7 +3,7 @@ import Jimp, { read as jimpRead } from 'jimp' import { join } from 'path' import { getLowercaseExtension } from '@shared/core-utils' import { buildUUID } from '@shared/extra-utils' -import { convertWebPToJPG, generateThumbnailFromVideo, processGIF } from './ffmpeg/ffmpeg-images' +import { convertWebPToJPG, generateThumbnailFromVideo, processGIF } from './ffmpeg' import { logger, loggerTagsFactory } from './logger' const lTags = loggerTagsFactory('image-utils') @@ -30,7 +30,7 @@ async function processImage (options: { // Use FFmpeg to process GIF if (extension === '.gif') { - await processGIF(path, destination, newSize) + await processGIF({ path, destination, newSize }) } else { await jimpProcessor(path, destination, newSize, extension) } @@ -50,7 +50,7 @@ async function generateImageFromVideoFile (options: { const pendingImagePath = join(folder, pendingImageName) try { - await generateThumbnailFromVideo(fromPath, folder, imageName) + await generateThumbnailFromVideo({ fromPath, folder, imageName }) const destination = join(folder, imageName) await processImage({ path: pendingImagePath, destination, newSize: size }) @@ -99,7 +99,7 @@ async function jimpProcessor (path: string, destination: string, newSize: { widt logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err }) const newName = path + '.jpg' - await convertWebPToJPG(path, newName) + await convertWebPToJPG({ path, destination: newName }) await rename(newName, path) sourceImage = await jimpRead(path) diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts index ae7d11800..95e78a904 100644 --- a/server/helpers/peertube-crypto.ts +++ b/server/helpers/peertube-crypto.ts @@ -2,10 +2,11 @@ import { compare, genSalt, hash } from 'bcrypt' import { createCipheriv, createDecipheriv, createSign, createVerify } from 'crypto' import { Request } from 'express' import { cloneDeep } from 'lodash' +import { promisify1, promisify2 } from '@shared/core-utils' import { sha256 } from '@shared/extra-utils' import { BCRYPT_SALT_SIZE, ENCRYPTION, HTTP_SIGNATURE, PRIVATE_RSA_KEY_SIZE } from '../initializers/constants' import { MActor } from '../types/models' -import { generateRSAKeyPairPromise, promisify1, promisify2, randomBytesPromise, scryptPromise } from './core-utils' +import { generateRSAKeyPairPromise, randomBytesPromise, scryptPromise } from './core-utils' import { jsonld } from './custom-jsonld-signature' import { logger } from './logger' diff --git a/server/helpers/token-generator.ts b/server/helpers/token-generator.ts new file mode 100644 index 000000000..16313b818 --- /dev/null +++ b/server/helpers/token-generator.ts @@ -0,0 +1,19 @@ +import { buildUUID } from '@shared/extra-utils' + +function generateRunnerRegistrationToken () { + return 'ptrrt-' + buildUUID() +} + +function generateRunnerToken () { + return 'ptrt-' + buildUUID() +} + +function generateRunnerJobToken () { + return 'ptrjt-' + buildUUID() +} + +export { + generateRunnerRegistrationToken, + generateRunnerToken, + generateRunnerJobToken +} diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index a3c93e6fe..e690e3890 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts @@ -13,9 +13,9 @@ import { VideoPathManager } from '@server/lib/video-path-manager' import { MVideo } from '@server/types/models/video/video' import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file' import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist' +import { promisify2 } from '@shared/core-utils' import { sha1 } from '@shared/extra-utils' import { CONFIG } from '../initializers/config' -import { promisify2 } from './core-utils' import { logger } from './logger' import { generateVideoImportTmpPath } from './utils' import { extractVideo } from './video' -- cgit v1.2.3