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