]>
Commit | Line | Data |
---|---|---|
1 | import { VideoTranscodingFPS } from './video-transcoding-fps.model' | |
2 | ||
3 | export 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: VideoResolution) { | |
21 | switch (resolution) { | |
22 | case VideoResolution.H_NOVIDEO: | |
23 | // audio-only | |
24 | return 64 * 1000 | |
25 | ||
26 | case 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 | case VideoResolution.H_360P: | |
32 | // quality according to Google Live Encoder: 400 - 1,000 Kbps | |
33 | // Quality according to YouTube Video Info: 700 Kbps | |
34 | return 780 * 1000 | |
35 | ||
36 | case VideoResolution.H_480P: | |
37 | // quality according to Google Live Encoder: 500 - 2,000 Kbps | |
38 | // Quality according to YouTube Video Info: 1300 Kbps | |
39 | return 1500 * 1000 | |
40 | ||
41 | case VideoResolution.H_720P: | |
42 | // quality according to Google Live Encoder: 1,500 - 4,000 Kbps | |
43 | // Quality according to YouTube Video Info: 2680 Kbps | |
44 | return 2800 * 1000 | |
45 | ||
46 | case VideoResolution.H_1080P: | |
47 | // quality according to Google Live Encoder: 3000 - 6000 Kbps | |
48 | // Quality according to YouTube Video Info: 5081 Kbps | |
49 | return 5200 * 1000 | |
50 | ||
51 | case VideoResolution.H_4K: // fallthrough | |
52 | default: | |
53 | // quality according to Google Live Encoder: 13000 - 34000 Kbps | |
54 | return 22000 * 1000 | |
55 | } | |
56 | } | |
57 | ||
58 | /** | |
59 | * Calculate the target bitrate based on video resolution and FPS. | |
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. | |
66 | */ | |
67 | export function getTargetBitrate (resolution: VideoResolution, fps: number, fpsTranscodingConstants: VideoTranscodingFPS) { | |
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: | |
71 | // 720p: 2600 / 1750 = 1.49 | |
72 | // 1080p: 4400 / 3300 = 1.33 | |
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) | |
78 | // Example outputs: | |
79 | // 1080p10: 2420 kbps, 1080p30: 3300 kbps, 1080p60: 4620 kbps | |
80 | // 720p10: 1283 kbps, 720p30: 1750 kbps, 720p60: 2450 kbps | |
81 | return baseBitrate + (fps - fpsTranscodingConstants.AVERAGE) * (maxBitrateDifference / maxFpsDifference) | |
82 | } | |
83 | ||
84 | /** | |
85 | * The maximum bitrate we expect to see on a transcoded video in bytes per second. | |
86 | */ | |
87 | export function getMaxBitrate (resolution: VideoResolution, fps: number, fpsTranscodingConstants: VideoTranscodingFPS) { | |
88 | return getTargetBitrate(resolution, fps, fpsTranscodingConstants) * 2 | |
89 | } |