aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/videos/bitrate.ts
blob: c1891188f1581dc9fbc4da78b3032695a2012a7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { VideoResolution } from "@shared/models"

type BitPerPixel = { [ id in VideoResolution ]: number }

// https://bitmovin.com/video-bitrate-streaming-hls-dash/

const minLimitBitPerPixel: BitPerPixel = {
  [VideoResolution.H_NOVIDEO]: 0,
  [VideoResolution.H_144P]: 0.02,
  [VideoResolution.H_240P]: 0.02,
  [VideoResolution.H_360P]: 0.02,
  [VideoResolution.H_480P]: 0.02,
  [VideoResolution.H_720P]: 0.02,
  [VideoResolution.H_1080P]: 0.02,
  [VideoResolution.H_1440P]: 0.02,
  [VideoResolution.H_4K]: 0.02
}

const averageBitPerPixel: BitPerPixel = {
  [VideoResolution.H_NOVIDEO]: 0,
  [VideoResolution.H_144P]: 0.19,
  [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_144P]: 0.32,
  [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
}

function getMinLimitBitrate (options: {
  resolution: VideoResolution
  ratio: number
  fps: number
}) {
  const minLimitBitrate = calculateBitrate({ ...options, bitPerPixel: minLimitBitPerPixel })
  if (!minLimitBitrate) return 10 * 1000

  return minLimitBitrate
}

// ---------------------------------------------------------------------------

export {
  getAverageBitrate,
  getMaxBitrate,
  getMinLimitBitrate
}

// ---------------------------------------------------------------------------

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_144P,
    VideoResolution.H_NOVIDEO
  ]

  for (const toTestResolution of resolutionsOrder) {
    if (toTestResolution <= resolution) {
      return Math.floor(resolution * resolution * ratio * fps * bitPerPixel[toTestResolution])
    }
  }

  throw new Error('Unknown resolution ' + resolution)
}