aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/ffprobe-utils.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-08-06 13:35:25 +0200
committerChocobozzz <me@florianbigard.com>2021-08-06 14:13:26 +0200
commit679c12e69c9f3a2d003ee3abe8b8da49f25b2bd3 (patch)
tree03abf589275db05e5b1fa1c89f57049cd807324a /server/helpers/ffprobe-utils.ts
parentc826f34a45757b324a20f71665b44ed10e6953b5 (diff)
downloadPeerTube-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.ts16
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 @@
1import * as ffmpeg from 'fluent-ffmpeg' 1import * as ffmpeg from 'fluent-ffmpeg'
2import { getMaxBitrate, VideoFileMetadata, VideoResolution } from '../../shared/models/videos' 2import { getMaxBitrate } from '@shared/core-utils'
3import { VideoFileMetadata, VideoResolution, VideoTranscodingFPS } from '../../shared/models/videos'
3import { CONFIG } from '../initializers/config' 4import { CONFIG } from '../initializers/config'
4import { VIDEO_TRANSCODING_FPS } from '../initializers/constants' 5import { VIDEO_TRANSCODING_FPS } from '../initializers/constants'
5import { logger } from './logger' 6import { logger } from './logger'
@@ -75,7 +76,7 @@ function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
75 } 76 }
76} 77}
77 78
78async function getVideoStreamSize (path: string, existingProbe?: ffmpeg.FfprobeData) { 79async 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
281function getClosestFramerateStandard (fps: number, type: 'HD_STANDARD' | 'STANDARD'): number { 285function 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}