PeerTube/server/core/helpers/ffmpeg/framerate.ts

91 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

2024-08-12 16:17:11 +02:00
import { CONFIG } from '@server/initializers/config.js'
import { logger } from '../logger.js'
export function computeOutputFPS (options: {
inputFPS: number
2024-08-12 16:17:11 +02:00
isOriginResolution: boolean
resolution: number
2024-08-12 16:17:11 +02:00
type: 'vod' | 'live'
}) {
2024-08-12 16:17:11 +02:00
const { resolution, isOriginResolution, type } = options
const settings = type === 'vod'
? buildTranscodingFPSOptions(CONFIG.TRANSCODING.FPS.MAX)
: buildTranscodingFPSOptions(CONFIG.LIVE.TRANSCODING.FPS.MAX)
let fps = options.inputFPS
if (
2024-08-12 16:17:11 +02:00
// On small/medium transcoded resolutions, limit FPS
!isOriginResolution &&
resolution !== undefined &&
2024-08-12 16:17:11 +02:00
resolution < settings.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
fps > settings.AVERAGE
) {
// Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value
2024-08-12 16:17:11 +02:00
fps = getClosestFramerate({ fps, settings, type: 'STANDARD' })
}
2024-08-12 16:17:11 +02:00
if (fps < settings.HARD_MIN) {
throw new Error(`Cannot compute FPS because ${fps} is lower than our minimum value ${settings.HARD_MIN}`)
}
2024-05-29 08:56:53 +02:00
// Cap min FPS
2024-08-12 16:17:11 +02:00
fps = Math.max(fps, settings.TRANSCODED_MIN)
2024-05-29 08:56:53 +02:00
// Cap max FPS
2024-08-12 16:17:11 +02:00
if (fps > settings.TRANSCODED_MAX) {
fps = getClosestFramerate({ fps, settings, type: 'HD_STANDARD' })
}
logger.debug(`Computed output FPS ${fps} for resolution ${resolution}p`, { options, settings })
2024-05-29 08:56:53 +02:00
return fps
}
// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------
2024-08-12 16:17:11 +02:00
function buildTranscodingFPSOptions (maxFPS: number) {
const STANDARD = [ 24, 25, 30 ].filter(v => v <= maxFPS)
if (STANDARD.length === 0) STANDARD.push(maxFPS)
const HD_STANDARD = [ 50, 60, maxFPS ].filter(v => v <= maxFPS)
return {
HARD_MIN: 0.1,
TRANSCODED_MIN: 1,
TRANSCODED_MAX: maxFPS,
STANDARD,
HD_STANDARD,
AVERAGE: Math.min(30, maxFPS),
KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
}
}
function getClosestFramerate (options: {
fps: number
2024-08-12 16:17:11 +02:00
settings: ReturnType<typeof buildTranscodingFPSOptions>
type: Extract<keyof ReturnType<typeof buildTranscodingFPSOptions>, 'HD_STANDARD' | 'STANDARD'>
}) {
2024-08-12 16:17:11 +02:00
const { fps, settings, type } = options
const copy = [ ...settings[type] ]
// Biggest FPS first
const descSorted = copy.sort((a, b) => b - a)
// Find biggest FPS that can be divided by input FPS
const found = descSorted.find(e => fps % e === 0)
if (found) return found
2024-08-12 16:17:11 +02:00
// Approximation to the best result
return copy.sort((a, b) => fps % a - fps % b)[0]
}