diff options
author | Chocobozzz <me@florianbigard.com> | 2021-08-06 13:35:25 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-08-06 14:13:26 +0200 |
commit | 679c12e69c9f3a2d003ee3abe8b8da49f25b2bd3 (patch) | |
tree | 03abf589275db05e5b1fa1c89f57049cd807324a /server/helpers/ffprobe-utils.ts | |
parent | c826f34a45757b324a20f71665b44ed10e6953b5 (diff) | |
download | PeerTube-679c12e69c9f3a2d003ee3abe8b8da49f25b2bd3.tar.gz PeerTube-679c12e69c9f3a2d003ee3abe8b8da49f25b2bd3.tar.zst PeerTube-679c12e69c9f3a2d003ee3abe8b8da49f25b2bd3.zip |
Improve target bitrate calculation
Diffstat (limited to 'server/helpers/ffprobe-utils.ts')
-rw-r--r-- | server/helpers/ffprobe-utils.ts | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/server/helpers/ffprobe-utils.ts b/server/helpers/ffprobe-utils.ts index bc87e49b1..e58444b07 100644 --- a/server/helpers/ffprobe-utils.ts +++ b/server/helpers/ffprobe-utils.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | import * as ffmpeg from 'fluent-ffmpeg' | 1 | import * as ffmpeg from 'fluent-ffmpeg' |
2 | import { getMaxBitrate, VideoFileMetadata, VideoResolution } from '../../shared/models/videos' | 2 | import { getMaxBitrate } from '@shared/core-utils' |
3 | import { VideoFileMetadata, VideoResolution, VideoTranscodingFPS } from '../../shared/models/videos' | ||
3 | import { CONFIG } from '../initializers/config' | 4 | import { CONFIG } from '../initializers/config' |
4 | import { VIDEO_TRANSCODING_FPS } from '../initializers/constants' | 5 | import { VIDEO_TRANSCODING_FPS } from '../initializers/constants' |
5 | import { logger } from './logger' | 6 | import { logger } from './logger' |
@@ -75,7 +76,7 @@ function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) { | |||
75 | } | 76 | } |
76 | } | 77 | } |
77 | 78 | ||
78 | async function getVideoStreamSize (path: string, existingProbe?: ffmpeg.FfprobeData) { | 79 | async function getVideoStreamSize (path: string, existingProbe?: ffmpeg.FfprobeData): Promise<{ width: number, height: number }> { |
79 | const videoStream = await getVideoStreamFromFile(path, existingProbe) | 80 | const videoStream = await getVideoStreamFromFile(path, existingProbe) |
80 | 81 | ||
81 | return videoStream === null | 82 | return videoStream === null |
@@ -146,7 +147,10 @@ async function getVideoFileResolution (path: string, existingProbe?: ffmpeg.Ffpr | |||
146 | const size = await getVideoStreamSize(path, existingProbe) | 147 | const size = await getVideoStreamSize(path, existingProbe) |
147 | 148 | ||
148 | return { | 149 | return { |
149 | videoFileResolution: Math.min(size.height, size.width), | 150 | width: size.width, |
151 | height: size.height, | ||
152 | ratio: Math.max(size.height, size.width) / Math.min(size.height, size.width), | ||
153 | resolution: Math.min(size.height, size.width), | ||
150 | isPortraitMode: size.height > size.width | 154 | isPortraitMode: size.height > size.width |
151 | } | 155 | } |
152 | } | 156 | } |
@@ -243,7 +247,7 @@ async function canDoQuickVideoTranscode (path: string, probe?: ffmpeg.FfprobeDat | |||
243 | const videoStream = await getVideoStreamFromFile(path, probe) | 247 | const videoStream = await getVideoStreamFromFile(path, probe) |
244 | const fps = await getVideoFileFPS(path, probe) | 248 | const fps = await getVideoFileFPS(path, probe) |
245 | const bitRate = await getVideoFileBitrate(path, probe) | 249 | const bitRate = await getVideoFileBitrate(path, probe) |
246 | const resolution = await getVideoFileResolution(path, probe) | 250 | const resolutionData = await getVideoFileResolution(path, probe) |
247 | 251 | ||
248 | // If ffprobe did not manage to guess the bitrate | 252 | // If ffprobe did not manage to guess the bitrate |
249 | if (!bitRate) return false | 253 | if (!bitRate) return false |
@@ -253,7 +257,7 @@ async function canDoQuickVideoTranscode (path: string, probe?: ffmpeg.FfprobeDat | |||
253 | if (videoStream['codec_name'] !== 'h264') return false | 257 | if (videoStream['codec_name'] !== 'h264') return false |
254 | if (videoStream['pix_fmt'] !== 'yuv420p') return false | 258 | if (videoStream['pix_fmt'] !== 'yuv420p') return false |
255 | if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false | 259 | if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false |
256 | if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false | 260 | if (bitRate > getMaxBitrate({ ...resolutionData, fps })) return false |
257 | 261 | ||
258 | return true | 262 | return true |
259 | } | 263 | } |
@@ -278,7 +282,7 @@ async function canDoQuickAudioTranscode (path: string, probe?: ffmpeg.FfprobeDat | |||
278 | return true | 282 | return true |
279 | } | 283 | } |
280 | 284 | ||
281 | function getClosestFramerateStandard (fps: number, type: 'HD_STANDARD' | 'STANDARD'): number { | 285 | function getClosestFramerateStandard <K extends keyof Pick<VideoTranscodingFPS, 'HD_STANDARD' | 'STANDARD'>> (fps: number, type: K) { |
282 | return VIDEO_TRANSCODING_FPS[type].slice(0) | 286 | return VIDEO_TRANSCODING_FPS[type].slice(0) |
283 | .sort((a, b) => fps % a - fps % b)[0] | 287 | .sort((a, b) => fps % a - fps % b)[0] |
284 | } | 288 | } |