2018-10-08 16:26:04 +02:00
|
|
|
import { VideoTranscodingFPS } from './video-transcoding-fps.model'
|
|
|
|
|
2020-08-06 14:58:01 +02:00
|
|
|
export const enum VideoResolution {
|
2019-11-01 02:06:19 +01:00
|
|
|
H_NOVIDEO = 0,
|
2017-10-02 12:20:26 +02:00
|
|
|
H_240P = 240,
|
|
|
|
H_360P = 360,
|
|
|
|
H_480P = 480,
|
|
|
|
H_720P = 720,
|
2019-06-06 14:45:57 +02:00
|
|
|
H_1080P = 1080,
|
2020-12-24 18:02:04 +01:00
|
|
|
H_1440P = 1440,
|
2019-06-06 14:45:57 +02:00
|
|
|
H_4K = 2160
|
2017-10-02 12:20:26 +02:00
|
|
|
}
|
2018-10-08 16:26:04 +02:00
|
|
|
|
|
|
|
/**
|
2018-10-10 00:46:26 +02:00
|
|
|
* Bitrate targets for different resolutions, at VideoTranscodingFPS.AVERAGE.
|
|
|
|
*
|
2018-10-08 16:26:04 +02:00
|
|
|
* Sources for individual quality levels:
|
|
|
|
* Google Live Encoder: https://support.google.com/youtube/answer/2853702?hl=en
|
2019-12-05 14:57:14 +01:00
|
|
|
* YouTube Video Info: youtube-dl --list-formats, with sample videos
|
2018-10-08 16:26:04 +02:00
|
|
|
*/
|
2020-06-16 16:36:02 +02:00
|
|
|
function getBaseBitrate (resolution: number) {
|
|
|
|
if (resolution === VideoResolution.H_NOVIDEO) {
|
|
|
|
// audio-only
|
|
|
|
return 64 * 1000
|
|
|
|
}
|
2019-07-19 10:37:35 +02:00
|
|
|
|
2020-06-16 16:36:02 +02:00
|
|
|
if (resolution <= VideoResolution.H_240P) {
|
|
|
|
// quality according to Google Live Encoder: 300 - 700 Kbps
|
|
|
|
// Quality according to YouTube Video Info: 285 Kbps
|
|
|
|
return 320 * 1000
|
|
|
|
}
|
2019-07-19 10:37:35 +02:00
|
|
|
|
2020-06-16 16:36:02 +02:00
|
|
|
if (resolution <= VideoResolution.H_360P) {
|
|
|
|
// quality according to Google Live Encoder: 400 - 1,000 Kbps
|
|
|
|
// Quality according to YouTube Video Info: 700 Kbps
|
|
|
|
return 780 * 1000
|
|
|
|
}
|
2019-07-19 10:37:35 +02:00
|
|
|
|
2020-06-16 16:36:02 +02:00
|
|
|
if (resolution <= VideoResolution.H_480P) {
|
|
|
|
// quality according to Google Live Encoder: 500 - 2,000 Kbps
|
|
|
|
// Quality according to YouTube Video Info: 1300 Kbps
|
|
|
|
return 1500 * 1000
|
|
|
|
}
|
2019-07-19 10:37:35 +02:00
|
|
|
|
2020-06-16 16:36:02 +02:00
|
|
|
if (resolution <= VideoResolution.H_720P) {
|
|
|
|
// quality according to Google Live Encoder: 1,500 - 4,000 Kbps
|
|
|
|
// Quality according to YouTube Video Info: 2680 Kbps
|
|
|
|
return 2800 * 1000
|
|
|
|
}
|
2019-07-19 10:37:35 +02:00
|
|
|
|
2020-06-16 16:36:02 +02:00
|
|
|
if (resolution <= VideoResolution.H_1080P) {
|
|
|
|
// quality according to Google Live Encoder: 3000 - 6000 Kbps
|
|
|
|
// Quality according to YouTube Video Info: 5081 Kbps
|
|
|
|
return 5200 * 1000
|
2018-10-08 16:26:04 +02:00
|
|
|
}
|
2020-06-16 16:36:02 +02:00
|
|
|
|
2020-12-24 18:02:04 +01:00
|
|
|
if (resolution <= VideoResolution.H_1440P) {
|
|
|
|
// quality according to Google Live Encoder: 6000 - 13000 Kbps
|
|
|
|
// Quality according to YouTube Video Info: 8600 (av01) - 17000 (vp9.2) Kbps
|
|
|
|
return 10_000 * 1000
|
|
|
|
}
|
|
|
|
|
2020-06-16 16:36:02 +02:00
|
|
|
// 4K
|
|
|
|
// quality according to Google Live Encoder: 13000 - 34000 Kbps
|
2020-12-24 18:02:04 +01:00
|
|
|
return 22_000 * 1000
|
2018-10-08 16:26:04 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 00:46:26 +02:00
|
|
|
/**
|
|
|
|
* Calculate the target bitrate based on video resolution and FPS.
|
2018-10-10 01:53:16 +02:00
|
|
|
*
|
|
|
|
* The calculation is based on two values:
|
|
|
|
* Bitrate at VideoTranscodingFPS.AVERAGE is always the same as
|
|
|
|
* getBaseBitrate(). Bitrate at VideoTranscodingFPS.MAX is always
|
|
|
|
* getBaseBitrate() * 1.4. All other values are calculated linearly
|
|
|
|
* between these two points.
|
2018-10-10 00:46:26 +02:00
|
|
|
*/
|
2020-06-16 16:36:02 +02:00
|
|
|
export function getTargetBitrate (resolution: number, fps: number, fpsTranscodingConstants: VideoTranscodingFPS) {
|
2018-10-10 00:46:26 +02:00
|
|
|
const baseBitrate = getBaseBitrate(resolution)
|
|
|
|
// The maximum bitrate, used when fps === VideoTranscodingFPS.MAX
|
|
|
|
// Based on numbers from Youtube, 60 fps bitrate divided by 30 fps bitrate:
|
2018-10-10 01:53:16 +02:00
|
|
|
// 720p: 2600 / 1750 = 1.49
|
|
|
|
// 1080p: 4400 / 3300 = 1.33
|
2018-10-10 00:46:26 +02:00
|
|
|
const maxBitrate = baseBitrate * 1.4
|
|
|
|
const maxBitrateDifference = maxBitrate - baseBitrate
|
|
|
|
const maxFpsDifference = fpsTranscodingConstants.MAX - fpsTranscodingConstants.AVERAGE
|
|
|
|
// For 1080p video with default settings, this results in the following formula:
|
|
|
|
// 3300 + (x - 30) * (1320/30)
|
2018-10-10 01:22:35 +02:00
|
|
|
// Example outputs:
|
|
|
|
// 1080p10: 2420 kbps, 1080p30: 3300 kbps, 1080p60: 4620 kbps
|
2018-10-10 01:53:16 +02:00
|
|
|
// 720p10: 1283 kbps, 720p30: 1750 kbps, 720p60: 2450 kbps
|
2020-11-24 14:08:23 +01:00
|
|
|
return Math.floor(baseBitrate + (fps - fpsTranscodingConstants.AVERAGE) * (maxBitrateDifference / maxFpsDifference))
|
2018-10-10 00:46:26 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 16:26:04 +02:00
|
|
|
/**
|
|
|
|
* The maximum bitrate we expect to see on a transcoded video in bytes per second.
|
|
|
|
*/
|
2018-10-17 13:10:58 +02:00
|
|
|
export function getMaxBitrate (resolution: VideoResolution, fps: number, fpsTranscodingConstants: VideoTranscodingFPS) {
|
2018-10-08 16:26:04 +02:00
|
|
|
return getTargetBitrate(resolution, fps, fpsTranscodingConstants) * 2
|
|
|
|
}
|