From edb4ffc7e0b13659d7c73b120f2c87b27e4c26a1 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 8 Oct 2018 09:26:04 -0500 Subject: Set bitrate limits for transcoding (fixes #638) (#1135) * Set bitrate limits for transcoding (fixes #638) * added optimization script and test, changed stuff * fix test, improve docs * re-add optimize-old-videos script * added documentation * Don't optimize videos without valid UUID, or redundancy videos * move getUUIDFromFilename * fix tests? * update torrent and file size, some more fixes/improvements * use higher bitrate for high fps video, adjust bitrates * add test video * don't throw error if resolution is undefined * generate test fixture on the fly * use random noise video for bitrate test, add promise * shorten test video to avoid timeout * use existing function to optimize video * various fixes * increase test timeout * limit test fixture size, add link * test fixes * add await * more test fixes, add -b:v parameter * replace ffmpeg wiki link * fix ffmpeg params * fix unit test * add test fixture to .gitgnore * add video transcoding fps model * add missing file --- shared/models/videos/index.ts | 1 + shared/models/videos/video-resolution.enum.ts | 55 ++++++++++++++++++++++ .../models/videos/video-transcoding-fps.model.ts | 6 +++ 3 files changed, 62 insertions(+) create mode 100644 shared/models/videos/video-transcoding-fps.model.ts (limited to 'shared/models') diff --git a/shared/models/videos/index.ts b/shared/models/videos/index.ts index 90a0e3053..056ae06da 100644 --- a/shared/models/videos/index.ts +++ b/shared/models/videos/index.ts @@ -21,6 +21,7 @@ export * from './video-update.model' export * from './video.model' export * from './video-query.type' export * from './video-state.enum' +export * from './video-transcoding-fps.model' export * from './caption/video-caption.model' export * from './caption/video-caption-update.model' export * from './import/video-import-create.model' diff --git a/shared/models/videos/video-resolution.enum.ts b/shared/models/videos/video-resolution.enum.ts index 100fc0e6e..3c52bbf98 100644 --- a/shared/models/videos/video-resolution.enum.ts +++ b/shared/models/videos/video-resolution.enum.ts @@ -1,3 +1,5 @@ +import { VideoTranscodingFPS } from './video-transcoding-fps.model' + export enum VideoResolution { H_240P = 240, H_360P = 360, @@ -5,3 +7,56 @@ export enum VideoResolution { H_720P = 720, H_1080P = 1080 } + +/** + * Bitrate targets for different resolutions and frame rates, in bytes per second. + * Sources for individual quality levels: + * Google Live Encoder: https://support.google.com/youtube/answer/2853702?hl=en + * YouTube Video Info (tested with random music video): https://www.h3xed.com/blogmedia/youtube-info.php + */ +export function getTargetBitrate (resolution: VideoResolution, fps: number, + fpsTranscodingConstants: VideoTranscodingFPS) { + switch (resolution) { + case VideoResolution.H_240P: + // quality according to Google Live Encoder: 300 - 700 Kbps + // Quality according to YouTube Video Info: 186 Kbps + return 250 * 1000 + case VideoResolution.H_360P: + // quality according to Google Live Encoder: 400 - 1,000 Kbps + // Quality according to YouTube Video Info: 480 Kbps + return 500 * 1000 + case VideoResolution.H_480P: + // quality according to Google Live Encoder: 500 - 2,000 Kbps + // Quality according to YouTube Video Info: 879 Kbps + return 900 * 1000 + case VideoResolution.H_720P: + if (fps === fpsTranscodingConstants.MAX) { + // quality according to Google Live Encoder: 2,250 - 6,000 Kbps + // Quality according to YouTube Video Info: 2634 Kbps + return 2600 * 1000 + } else { + // quality according to Google Live Encoder: 1,500 - 4,000 Kbps + // Quality according to YouTube Video Info: 1752 Kbps + return 1750 * 1000 + } + case VideoResolution.H_1080P: // fallthrough + default: + if (fps === fpsTranscodingConstants.MAX) { + // quality according to Google Live Encoder: 3000 - 6000 Kbps + // Quality according to YouTube Video Info: 4387 Kbps + return 4400 * 1000 + } else { + // quality according to Google Live Encoder: 3000 - 6000 Kbps + // Quality according to YouTube Video Info: 3277 Kbps + return 3300 * 1000 + } + } +} + +/** + * The maximum bitrate we expect to see on a transcoded video in bytes per second. + */ +export function getMaxBitrate (resolution: VideoResolution, fps: number, + fpsTranscodingConstants: VideoTranscodingFPS) { + return getTargetBitrate(resolution, fps, fpsTranscodingConstants) * 2 +} diff --git a/shared/models/videos/video-transcoding-fps.model.ts b/shared/models/videos/video-transcoding-fps.model.ts new file mode 100644 index 000000000..82022d2f1 --- /dev/null +++ b/shared/models/videos/video-transcoding-fps.model.ts @@ -0,0 +1,6 @@ +export type VideoTranscodingFPS = { + MIN: number, + AVERAGE: number, + MAX: number, + KEEP_ORIGIN_FPS_RESOLUTION_MIN: number +} -- cgit v1.2.3