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