1 import { VideoResolution } from "@shared/models"
3 type BitPerPixel = { [ id in VideoResolution ]: number }
5 // https://bitmovin.com/video-bitrate-streaming-hls-dash/
7 const averageBitPerPixel: BitPerPixel = {
8 [VideoResolution.H_NOVIDEO]: 0,
9 [VideoResolution.H_144P]: 0.19,
10 [VideoResolution.H_240P]: 0.17,
11 [VideoResolution.H_360P]: 0.15,
12 [VideoResolution.H_480P]: 0.12,
13 [VideoResolution.H_720P]: 0.11,
14 [VideoResolution.H_1080P]: 0.10,
15 [VideoResolution.H_1440P]: 0.09,
16 [VideoResolution.H_4K]: 0.08
19 const maxBitPerPixel: BitPerPixel = {
20 [VideoResolution.H_NOVIDEO]: 0,
21 [VideoResolution.H_144P]: 0.32,
22 [VideoResolution.H_240P]: 0.29,
23 [VideoResolution.H_360P]: 0.26,
24 [VideoResolution.H_480P]: 0.22,
25 [VideoResolution.H_720P]: 0.19,
26 [VideoResolution.H_1080P]: 0.17,
27 [VideoResolution.H_1440P]: 0.16,
28 [VideoResolution.H_4K]: 0.14
31 function getAverageBitrate (options: {
32 resolution: VideoResolution
36 const targetBitrate = calculateBitrate({ ...options, bitPerPixel: averageBitPerPixel })
37 if (!targetBitrate) return 192 * 1000
42 function getMaxBitrate (options: {
43 resolution: VideoResolution
47 const targetBitrate = calculateBitrate({ ...options, bitPerPixel: maxBitPerPixel })
48 if (!targetBitrate) return 256 * 1000
53 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
62 function calculateBitrate (options: {
63 bitPerPixel: BitPerPixel
64 resolution: VideoResolution
68 const { bitPerPixel, resolution, ratio, fps } = options
70 const resolutionsOrder = [
72 VideoResolution.H_1440P,
73 VideoResolution.H_1080P,
74 VideoResolution.H_720P,
75 VideoResolution.H_480P,
76 VideoResolution.H_360P,
77 VideoResolution.H_240P,
78 VideoResolution.H_144P,
79 VideoResolution.H_NOVIDEO
82 for (const toTestResolution of resolutionsOrder) {
83 if (toTestResolution <= resolution) {
84 return Math.floor(resolution * resolution * ratio * fps * bitPerPixel[toTestResolution])
88 throw new Error('Unknown resolution ' + resolution)