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