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 --- shared/core-utils/index.ts | 1 + shared/core-utils/videos/bitrate.ts | 86 +++++++++++++++++++++++++++++++++++++ shared/core-utils/videos/index.ts | 1 + 3 files changed, 88 insertions(+) create mode 100644 shared/core-utils/videos/bitrate.ts create mode 100644 shared/core-utils/videos/index.ts (limited to 'shared/core-utils') diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts index 2a7d4d982..e0a6a8087 100644 --- a/shared/core-utils/index.ts +++ b/shared/core-utils/index.ts @@ -5,3 +5,4 @@ export * from './plugins' export * from './renderer' export * from './users' export * from './utils' +export * from './videos' diff --git a/shared/core-utils/videos/bitrate.ts b/shared/core-utils/videos/bitrate.ts new file mode 100644 index 000000000..3d4e47906 --- /dev/null +++ b/shared/core-utils/videos/bitrate.ts @@ -0,0 +1,86 @@ +import { VideoResolution } from "@shared/models" + +type BitPerPixel = { [ id in VideoResolution ]: number } + +// https://bitmovin.com/video-bitrate-streaming-hls-dash/ + +const averageBitPerPixel: BitPerPixel = { + [VideoResolution.H_NOVIDEO]: 0, + [VideoResolution.H_240P]: 0.17, + [VideoResolution.H_360P]: 0.15, + [VideoResolution.H_480P]: 0.12, + [VideoResolution.H_720P]: 0.11, + [VideoResolution.H_1080P]: 0.10, + [VideoResolution.H_1440P]: 0.09, + [VideoResolution.H_4K]: 0.08 +} + +const maxBitPerPixel: BitPerPixel = { + [VideoResolution.H_NOVIDEO]: 0, + [VideoResolution.H_240P]: 0.29, + [VideoResolution.H_360P]: 0.26, + [VideoResolution.H_480P]: 0.22, + [VideoResolution.H_720P]: 0.19, + [VideoResolution.H_1080P]: 0.17, + [VideoResolution.H_1440P]: 0.16, + [VideoResolution.H_4K]: 0.14 +} + +function getAverageBitrate (options: { + resolution: VideoResolution + ratio: number + fps: number +}) { + const targetBitrate = calculateBitrate({ ...options, bitPerPixel: averageBitPerPixel }) + if (!targetBitrate) return 192 * 1000 + + return targetBitrate +} + +function getMaxBitrate (options: { + resolution: VideoResolution + ratio: number + fps: number +}) { + const targetBitrate = calculateBitrate({ ...options, bitPerPixel: maxBitPerPixel }) + if (!targetBitrate) return 256 * 1000 + + return targetBitrate +} + +// --------------------------------------------------------------------------- + +export { + getAverageBitrate, + getMaxBitrate +} + +// --------------------------------------------------------------------------- + +function calculateBitrate (options: { + bitPerPixel: BitPerPixel + resolution: VideoResolution + ratio: number + fps: number +}) { + const { bitPerPixel, resolution, ratio, fps } = options + + const resolutionsOrder = [ + VideoResolution.H_4K, + VideoResolution.H_1440P, + VideoResolution.H_1080P, + VideoResolution.H_720P, + VideoResolution.H_480P, + VideoResolution.H_360P, + VideoResolution.H_240P, + VideoResolution.H_NOVIDEO + ] + + for (const toTestResolution of resolutionsOrder) { + if (toTestResolution <= resolution) { + return resolution * resolution * ratio * fps * bitPerPixel[toTestResolution] + } + } + + throw new Error('Unknown resolution ' + resolution) +} diff --git a/shared/core-utils/videos/index.ts b/shared/core-utils/videos/index.ts new file mode 100644 index 000000000..5a1145f1a --- /dev/null +++ b/shared/core-utils/videos/index.ts @@ -0,0 +1 @@ +export * from './bitrate' -- cgit v1.2.3