video-resolution.enum.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { VideoTranscodingFPS } from './video-transcoding-fps.model'
  2. export enum VideoResolution {
  3. H_240P = 240,
  4. H_360P = 360,
  5. H_480P = 480,
  6. H_720P = 720,
  7. H_1080P = 1080,
  8. H_4K = 2160
  9. }
  10. /**
  11. * Bitrate targets for different resolutions, at VideoTranscodingFPS.AVERAGE.
  12. *
  13. * Sources for individual quality levels:
  14. * Google Live Encoder: https://support.google.com/youtube/answer/2853702?hl=en
  15. * YouTube Video Info (tested with random music video): https://www.h3xed.com/blogmedia/youtube-info.php
  16. */
  17. function getBaseBitrate (resolution: VideoResolution) {
  18. switch (resolution) {
  19. case VideoResolution.H_240P:
  20. // quality according to Google Live Encoder: 300 - 700 Kbps
  21. // Quality according to YouTube Video Info: 186 Kbps
  22. return 250 * 1000
  23. case VideoResolution.H_360P:
  24. // quality according to Google Live Encoder: 400 - 1,000 Kbps
  25. // Quality according to YouTube Video Info: 480 Kbps
  26. return 500 * 1000
  27. case VideoResolution.H_480P:
  28. // quality according to Google Live Encoder: 500 - 2,000 Kbps
  29. // Quality according to YouTube Video Info: 879 Kbps
  30. return 900 * 1000
  31. case VideoResolution.H_720P:
  32. // quality according to Google Live Encoder: 1,500 - 4,000 Kbps
  33. // Quality according to YouTube Video Info: 1752 Kbps
  34. return 1750 * 1000
  35. case VideoResolution.H_1080P:
  36. // quality according to Google Live Encoder: 3000 - 6000 Kbps
  37. // Quality according to YouTube Video Info: 3277 Kbps
  38. return 3300 * 1000
  39. case VideoResolution.H_4K: // fallthrough
  40. default:
  41. // quality according to Google Live Encoder: 13000 - 34000 Kbps
  42. return 15000 * 1000
  43. }
  44. }
  45. /**
  46. * Calculate the target bitrate based on video resolution and FPS.
  47. *
  48. * The calculation is based on two values:
  49. * Bitrate at VideoTranscodingFPS.AVERAGE is always the same as
  50. * getBaseBitrate(). Bitrate at VideoTranscodingFPS.MAX is always
  51. * getBaseBitrate() * 1.4. All other values are calculated linearly
  52. * between these two points.
  53. */
  54. export function getTargetBitrate (resolution: VideoResolution, fps: number, 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:
  58. // 720p: 2600 / 1750 = 1.49
  59. // 1080p: 4400 / 3300 = 1.33
  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)
  65. // Example outputs:
  66. // 1080p10: 2420 kbps, 1080p30: 3300 kbps, 1080p60: 4620 kbps
  67. // 720p10: 1283 kbps, 720p30: 1750 kbps, 720p60: 2450 kbps
  68. return baseBitrate + (fps - fpsTranscodingConstants.AVERAGE) * (maxBitrateDifference / maxFpsDifference)
  69. }
  70. /**
  71. * The maximum bitrate we expect to see on a transcoded video in bytes per second.
  72. */
  73. export function getMaxBitrate (resolution: VideoResolution, fps: number, fpsTranscodingConstants: VideoTranscodingFPS) {
  74. return getTargetBitrate(resolution, fps, fpsTranscodingConstants) * 2
  75. }