From ab14f0e0dca878dbaccc8f6a895a68e4269c9873 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 4 May 2023 15:55:51 +0200 Subject: Prefer video studio instead of video edition Clearer and easier to find in the project --- packages/peertube-runner/server/process/process.ts | 6 +- .../server/process/shared/common.ts | 9 +- .../peertube-runner/server/process/shared/index.ts | 1 - .../server/process/shared/process-studio.ts | 50 ++++---- .../server/process/shared/process-vod.ts | 85 +++++++------ .../server/process/shared/transcoding-profiles.ts | 134 --------------------- .../peertube-runner/server/shared/supported-job.ts | 4 +- 7 files changed, 84 insertions(+), 205 deletions(-) delete mode 100644 packages/peertube-runner/server/process/shared/transcoding-profiles.ts (limited to 'packages/peertube-runner/server') diff --git a/packages/peertube-runner/server/process/process.ts b/packages/peertube-runner/server/process/process.ts index ef231cb38..1caafda8c 100644 --- a/packages/peertube-runner/server/process/process.ts +++ b/packages/peertube-runner/server/process/process.ts @@ -1,7 +1,7 @@ import { logger } from 'packages/peertube-runner/shared/logger' import { RunnerJobLiveRTMPHLSTranscodingPayload, - RunnerJobVideoEditionTranscodingPayload, + RunnerJobStudioTranscodingPayload, RunnerJobVODAudioMergeTranscodingPayload, RunnerJobVODHLSTranscodingPayload, RunnerJobVODWebVideoTranscodingPayload @@ -23,8 +23,8 @@ export async function processJob (options: ProcessOptions) { await processHLSTranscoding(options as ProcessOptions) } else if (job.type === 'live-rtmp-hls-transcoding') { await new ProcessLiveRTMPHLSTranscoding(options as ProcessOptions).process() - } else if (job.type === 'video-edition-transcoding') { - await processStudioTranscoding(options as ProcessOptions) + } else if (job.type === 'video-studio-transcoding') { + await processStudioTranscoding(options as ProcessOptions) } else { logger.error(`Unknown job ${job.type} to process`) return diff --git a/packages/peertube-runner/server/process/shared/common.ts b/packages/peertube-runner/server/process/shared/common.ts index 3cac98388..88f7c33f1 100644 --- a/packages/peertube-runner/server/process/shared/common.ts +++ b/packages/peertube-runner/server/process/shared/common.ts @@ -1,13 +1,12 @@ +import { remove } from 'fs-extra' import { throttle } from 'lodash' import { ConfigManager, downloadFile, logger } from 'packages/peertube-runner/shared' import { join } from 'path' import { buildUUID } from '@shared/extra-utils' -import { FFmpegEdition, FFmpegLive, FFmpegVOD } from '@shared/ffmpeg' +import { FFmpegEdition, FFmpegLive, FFmpegVOD, getDefaultAvailableEncoders, getDefaultEncodersToTry } from '@shared/ffmpeg' import { RunnerJob, RunnerJobPayload } from '@shared/models' import { PeerTubeServer } from '@shared/server-commands' import { getTranscodingLogger } from './transcoding-logger' -import { getAvailableEncoders, getEncodersToTry } from './transcoding-profiles' -import { remove } from 'fs-extra' export type JobWithToken = RunnerJob & { jobToken: string } @@ -92,8 +91,8 @@ function getCommonFFmpegOptions () { tmpDirectory: ConfigManager.Instance.getTranscodingDirectory(), profile: 'default', availableEncoders: { - available: getAvailableEncoders(), - encodersToTry: getEncodersToTry() + available: getDefaultAvailableEncoders(), + encodersToTry: getDefaultEncodersToTry() }, logger: getTranscodingLogger() } diff --git a/packages/peertube-runner/server/process/shared/index.ts b/packages/peertube-runner/server/process/shared/index.ts index 8e09a7869..556c51365 100644 --- a/packages/peertube-runner/server/process/shared/index.ts +++ b/packages/peertube-runner/server/process/shared/index.ts @@ -1,4 +1,3 @@ export * from './common' export * from './process-vod' export * from './transcoding-logger' -export * from './transcoding-profiles' diff --git a/packages/peertube-runner/server/process/shared/process-studio.ts b/packages/peertube-runner/server/process/shared/process-studio.ts index f8262096e..9c745d031 100644 --- a/packages/peertube-runner/server/process/shared/process-studio.ts +++ b/packages/peertube-runner/server/process/shared/process-studio.ts @@ -1,11 +1,11 @@ import { remove } from 'fs-extra' import { pick } from 'lodash' import { logger } from 'packages/peertube-runner/shared' -import { extname, join } from 'path' +import { join } from 'path' import { buildUUID } from '@shared/extra-utils' import { - RunnerJobVideoEditionTranscodingPayload, - VideoEditionTranscodingSuccess, + RunnerJobStudioTranscodingPayload, + VideoStudioTranscodingSuccess, VideoStudioTask, VideoStudioTaskCutPayload, VideoStudioTaskIntroPayload, @@ -16,7 +16,7 @@ import { import { ConfigManager } from '../../../shared/config-manager' import { buildFFmpegEdition, downloadInputFile, JobWithToken, ProcessOptions } from './common' -export async function processStudioTranscoding (options: ProcessOptions) { +export async function processStudioTranscoding (options: ProcessOptions) { const { server, job, runnerToken } = options const payload = job.payload @@ -43,7 +43,7 @@ export async function processStudioTranscoding (options: ProcessOptions) { @@ -124,15 +128,19 @@ async function processAddWatermark (options: TaskProcessorOptions {}, + outputPath, - resolution: payload.output.resolution, - fps: payload.output.fps - }) + inputFileMutexReleaser: () => {}, - const successBody: VODWebVideoTranscodingSuccess = { - videoFile: outputPath - } + resolution: payload.output.resolution, + fps: payload.output.fps + }) - await server.runnerJobs.success({ - jobToken: job.jobToken, - jobUUID: job.uuid, - runnerToken, - payload: successBody - }) + const successBody: VODWebVideoTranscodingSuccess = { + videoFile: outputPath + } - await remove(outputPath) + await server.runnerJobs.success({ + jobToken: job.jobToken, + jobUUID: job.uuid, + runnerToken, + payload: successBody + }) + } finally { + await remove(inputPath) + await remove(outputPath) + } } export async function processHLSTranscoding (options: ProcessOptions) { @@ -105,30 +108,34 @@ export async function processAudioMergeTranscoding (options: ProcessOptions {}, + outputPath, - resolution: payload.output.resolution, - fps: payload.output.fps - }) + inputFileMutexReleaser: () => {}, - const successBody: VODAudioMergeTranscodingSuccess = { - videoFile: outputPath - } + resolution: payload.output.resolution, + fps: payload.output.fps + }) - await server.runnerJobs.success({ - jobToken: job.jobToken, - jobUUID: job.uuid, - runnerToken, - payload: successBody - }) + const successBody: VODAudioMergeTranscodingSuccess = { + videoFile: outputPath + } - await remove(outputPath) + await server.runnerJobs.success({ + jobToken: job.jobToken, + jobUUID: job.uuid, + runnerToken, + payload: successBody + }) + } finally { + await remove(audioPath) + await remove(inputPath) + await remove(outputPath) + } } diff --git a/packages/peertube-runner/server/process/shared/transcoding-profiles.ts b/packages/peertube-runner/server/process/shared/transcoding-profiles.ts deleted file mode 100644 index 492d17d6a..000000000 --- a/packages/peertube-runner/server/process/shared/transcoding-profiles.ts +++ /dev/null @@ -1,134 +0,0 @@ -import { getAverageBitrate, getMinLimitBitrate } from '@shared/core-utils' -import { buildStreamSuffix, ffprobePromise, getAudioStream, getMaxAudioBitrate } from '@shared/ffmpeg' -import { EncoderOptionsBuilder, EncoderOptionsBuilderParams, VideoResolution } from '@shared/models' - -const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => { - const { fps, inputRatio, inputBitrate, resolution } = options - - const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution }) - - return { - outputOptions: [ - ...getCommonOutputOptions(targetBitrate), - - `-r ${fps}` - ] - } -} - -const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => { - const { streamNum, fps, inputBitrate, inputRatio, resolution } = options - - const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution }) - - return { - outputOptions: [ - ...getCommonOutputOptions(targetBitrate, streamNum), - - `${buildStreamSuffix('-r:v', streamNum)} ${fps}`, - `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}` - ] - } -} - -const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum, canCopyAudio }) => { - const probe = await ffprobePromise(input) - - const parsedAudio = await getAudioStream(input, probe) - - // We try to reduce the ceiling bitrate by making rough matches of bitrates - // Of course this is far from perfect, but it might save some space in the end - - const audioCodecName = parsedAudio.audioStream['codec_name'] - - const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate) - - // Force stereo as it causes some issues with HLS playback in Chrome - const base = [ '-channel_layout', 'stereo' ] - - if (bitrate !== -1) { - return { outputOptions: base.concat([ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ]) } - } - - return { outputOptions: base } -} - -const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => { - return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] } -} - -export function getAvailableEncoders () { - return { - vod: { - libx264: { - default: defaultX264VODOptionsBuilder - }, - aac: { - default: defaultAACOptionsBuilder - }, - libfdk_aac: { - default: defaultLibFDKAACVODOptionsBuilder - } - }, - live: { - libx264: { - default: defaultX264LiveOptionsBuilder - }, - aac: { - default: defaultAACOptionsBuilder - } - } - } -} - -export function getEncodersToTry () { - return { - vod: { - video: [ 'libx264' ], - audio: [ 'libfdk_aac', 'aac' ] - }, - - live: { - video: [ 'libx264' ], - audio: [ 'libfdk_aac', 'aac' ] - } - } -} - -// --------------------------------------------------------------------------- - -function getTargetBitrate (options: { - inputBitrate: number - resolution: VideoResolution - ratio: number - fps: number -}) { - const { inputBitrate, resolution, ratio, fps } = options - - const capped = capBitrate(inputBitrate, getAverageBitrate({ resolution, fps, ratio })) - const limit = getMinLimitBitrate({ resolution, fps, ratio }) - - return Math.max(limit, capped) -} - -function capBitrate (inputBitrate: number, targetBitrate: number) { - if (!inputBitrate) return targetBitrate - - // Add 30% margin to input bitrate - const inputBitrateWithMargin = inputBitrate + (inputBitrate * 0.3) - - return Math.min(targetBitrate, inputBitrateWithMargin) -} - -function getCommonOutputOptions (targetBitrate: number, streamNum?: number) { - return [ - `-preset veryfast`, - `${buildStreamSuffix('-maxrate:v', streamNum)} ${targetBitrate}`, - `${buildStreamSuffix('-bufsize:v', streamNum)} ${targetBitrate * 2}`, - - // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it - `-b_strategy 1`, - // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16 - `-bf 16` - ] -} diff --git a/packages/peertube-runner/server/shared/supported-job.ts b/packages/peertube-runner/server/shared/supported-job.ts index 87d5a39cc..1137d8206 100644 --- a/packages/peertube-runner/server/shared/supported-job.ts +++ b/packages/peertube-runner/server/shared/supported-job.ts @@ -2,7 +2,7 @@ import { RunnerJobLiveRTMPHLSTranscodingPayload, RunnerJobPayload, RunnerJobType, - RunnerJobVideoEditionTranscodingPayload, + RunnerJobStudioTranscodingPayload, RunnerJobVODAudioMergeTranscodingPayload, RunnerJobVODHLSTranscodingPayload, RunnerJobVODWebVideoTranscodingPayload, @@ -22,7 +22,7 @@ const supportedMatrix = { 'live-rtmp-hls-transcoding': (_payload: RunnerJobLiveRTMPHLSTranscodingPayload) => { return true }, - 'video-edition-transcoding': (payload: RunnerJobVideoEditionTranscodingPayload) => { + 'video-studio-transcoding': (payload: RunnerJobStudioTranscodingPayload) => { const tasks = payload?.tasks const supported = new Set([ 'add-intro', 'add-outro', 'add-watermark', 'cut' ]) -- cgit v1.2.3