From 679c12e69c9f3a2d003ee3abe8b8da49f25b2bd3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 6 Aug 2021 13:35:25 +0200 Subject: Improve target bitrate calculation --- .../lib/transcoding/video-transcoding-profiles.ts | 32 ++++++++++------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'server/lib/transcoding') diff --git a/server/lib/transcoding/video-transcoding-profiles.ts b/server/lib/transcoding/video-transcoding-profiles.ts index 2309f38d4..bca6dfccd 100644 --- a/server/lib/transcoding/video-transcoding-profiles.ts +++ b/server/lib/transcoding/video-transcoding-profiles.ts @@ -1,23 +1,24 @@ import { logger } from '@server/helpers/logger' -import { AvailableEncoders, EncoderOptionsBuilder, getTargetBitrate, VideoResolution } from '../../../shared/models/videos' +import { getAverageBitrate } from '@shared/core-utils' +import { AvailableEncoders, EncoderOptionsBuilder, EncoderOptionsBuilderParams } from '../../../shared/models/videos' import { buildStreamSuffix, resetSupportedEncoders } from '../../helpers/ffmpeg-utils' import { canDoQuickAudioTranscode, ffprobePromise, getAudioStream, getMaxAudioBitrate } from '../../helpers/ffprobe-utils' -import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants' /** * * Available encoders and profiles for the transcoding jobs * These functions are used by ffmpeg-utils that will get the encoders and options depending on the chosen profile * + * Resources: + * * https://slhck.info/video/2017/03/01/rate-control.html + * * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate */ -// Resources: -// * https://slhck.info/video/2017/03/01/rate-control.html -// * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate +const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async (options: EncoderOptionsBuilderParams) => { + const { fps, inputRatio, inputBitrate } = options + if (!fps) return { outputOptions: [ ] } -const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ inputBitrate, resolution, fps }) => { - const targetBitrate = buildTargetBitrate({ inputBitrate, resolution, fps }) - if (!targetBitrate) return { outputOptions: [ ] } + const targetBitrate = capBitrate(inputBitrate, getAverageBitrate({ ...options, fps, ratio: inputRatio })) return { outputOptions: [ @@ -29,8 +30,10 @@ const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ inputBitrat } } -const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async ({ resolution, fps, inputBitrate, streamNum }) => { - const targetBitrate = buildTargetBitrate({ inputBitrate, resolution, fps }) +const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async (options: EncoderOptionsBuilderParams) => { + const { streamNum, fps, inputBitrate, inputRatio } = options + + const targetBitrate = capBitrate(inputBitrate, getAverageBitrate({ ...options, fps, ratio: inputRatio })) return { outputOptions: [ @@ -231,14 +234,7 @@ export { // --------------------------------------------------------------------------- -function buildTargetBitrate (options: { - inputBitrate: number - resolution: VideoResolution - fps: number -}) { - const { inputBitrate, resolution, fps } = options - - const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) +function capBitrate (inputBitrate: number, targetBitrate: number) { if (!inputBitrate) return targetBitrate return Math.min(targetBitrate, inputBitrate) -- cgit v1.2.3