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