]>
Commit | Line | Data |
---|---|---|
edb4ffc7 FA |
1 | import { VideoTranscodingFPS } from './video-transcoding-fps.model' |
2 | ||
40298b02 | 3 | export enum VideoResolution { |
5c7d6508 | 4 | H_NOVIDEO = 0, |
40298b02 C |
5 | H_240P = 240, |
6 | H_360P = 360, | |
7 | H_480P = 480, | |
8 | H_720P = 720, | |
ad3405d0 C |
9 | H_1080P = 1080, |
10 | H_4K = 2160 | |
40298b02 | 11 | } |
edb4ffc7 FA |
12 | |
13 | /** | |
7f196c93 FA |
14 | * Bitrate targets for different resolutions, at VideoTranscodingFPS.AVERAGE. |
15 | * | |
edb4ffc7 FA |
16 | * Sources for individual quality levels: |
17 | * Google Live Encoder: https://support.google.com/youtube/answer/2853702?hl=en | |
77e08517 | 18 | * YouTube Video Info: youtube-dl --list-formats, with sample videos |
edb4ffc7 | 19 | */ |
c1978779 | 20 | function getBaseBitrate (resolution: VideoResolution) { |
edb4ffc7 | 21 | switch (resolution) { |
5c7d6508 | 22 | case VideoResolution.H_NOVIDEO: |
23 | // audio-only | |
24 | return 64 * 1000 | |
25 | ||
9b474844 C |
26 | case VideoResolution.H_240P: |
27 | // quality according to Google Live Encoder: 300 - 700 Kbps | |
77e08517 C |
28 | // Quality according to YouTube Video Info: 285 Kbps |
29 | return 320 * 1000 | |
9b474844 C |
30 | |
31 | case VideoResolution.H_360P: | |
32 | // quality according to Google Live Encoder: 400 - 1,000 Kbps | |
77e08517 C |
33 | // Quality according to YouTube Video Info: 700 Kbps |
34 | return 780 * 1000 | |
9b474844 C |
35 | |
36 | case VideoResolution.H_480P: | |
37 | // quality according to Google Live Encoder: 500 - 2,000 Kbps | |
77e08517 C |
38 | // Quality according to YouTube Video Info: 1300 Kbps |
39 | return 1500 * 1000 | |
9b474844 C |
40 | |
41 | case VideoResolution.H_720P: | |
42 | // quality according to Google Live Encoder: 1,500 - 4,000 Kbps | |
77e08517 C |
43 | // Quality according to YouTube Video Info: 2680 Kbps |
44 | return 2800 * 1000 | |
9b474844 C |
45 | |
46 | case VideoResolution.H_1080P: | |
47 | // quality according to Google Live Encoder: 3000 - 6000 Kbps | |
77e08517 | 48 | // Quality according to YouTube Video Info: 5081 Kbps |
f56ebb36 | 49 | return 5200 * 1000 |
9b474844 C |
50 | |
51 | case VideoResolution.H_4K: // fallthrough | |
52 | default: | |
53 | // quality according to Google Live Encoder: 13000 - 34000 Kbps | |
f56ebb36 | 54 | return 22000 * 1000 |
edb4ffc7 FA |
55 | } |
56 | } | |
57 | ||
7f196c93 FA |
58 | /** |
59 | * Calculate the target bitrate based on video resolution and FPS. | |
2f71dcf8 FA |
60 | * |
61 | * The calculation is based on two values: | |
62 | * Bitrate at VideoTranscodingFPS.AVERAGE is always the same as | |
63 | * getBaseBitrate(). Bitrate at VideoTranscodingFPS.MAX is always | |
64 | * getBaseBitrate() * 1.4. All other values are calculated linearly | |
65 | * between these two points. | |
7f196c93 | 66 | */ |
0229b014 | 67 | export function getTargetBitrate (resolution: VideoResolution, fps: number, fpsTranscodingConstants: VideoTranscodingFPS) { |
7f196c93 FA |
68 | const baseBitrate = getBaseBitrate(resolution) |
69 | // The maximum bitrate, used when fps === VideoTranscodingFPS.MAX | |
70 | // Based on numbers from Youtube, 60 fps bitrate divided by 30 fps bitrate: | |
2f71dcf8 FA |
71 | // 720p: 2600 / 1750 = 1.49 |
72 | // 1080p: 4400 / 3300 = 1.33 | |
7f196c93 FA |
73 | const maxBitrate = baseBitrate * 1.4 |
74 | const maxBitrateDifference = maxBitrate - baseBitrate | |
75 | const maxFpsDifference = fpsTranscodingConstants.MAX - fpsTranscodingConstants.AVERAGE | |
76 | // For 1080p video with default settings, this results in the following formula: | |
77 | // 3300 + (x - 30) * (1320/30) | |
e243c38c FA |
78 | // Example outputs: |
79 | // 1080p10: 2420 kbps, 1080p30: 3300 kbps, 1080p60: 4620 kbps | |
2f71dcf8 | 80 | // 720p10: 1283 kbps, 720p30: 1750 kbps, 720p60: 2450 kbps |
7f196c93 FA |
81 | return baseBitrate + (fps - fpsTranscodingConstants.AVERAGE) * (maxBitrateDifference / maxFpsDifference) |
82 | } | |
83 | ||
edb4ffc7 FA |
84 | /** |
85 | * The maximum bitrate we expect to see on a transcoded video in bytes per second. | |
86 | */ | |
0229b014 | 87 | export function getMaxBitrate (resolution: VideoResolution, fps: number, fpsTranscodingConstants: VideoTranscodingFPS) { |
edb4ffc7 FA |
88 | return getTargetBitrate(resolution, fps, fpsTranscodingConstants) * 2 |
89 | } |