diff options
author | Chocobozzz <me@florianbigard.com> | 2021-08-06 10:39:40 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-08-06 10:39:40 +0200 |
commit | c826f34a45757b324a20f71665b44ed10e6953b5 (patch) | |
tree | ff29bdff8b4519bbdbbcd3aa0d68521ce2b06ff5 /server/lib/transcoding/video-transcoding-profiles.ts | |
parent | 421ff4618da64f0849353383f690a014024c40da (diff) | |
download | PeerTube-c826f34a45757b324a20f71665b44ed10e6953b5.tar.gz PeerTube-c826f34a45757b324a20f71665b44ed10e6953b5.tar.zst PeerTube-c826f34a45757b324a20f71665b44ed10e6953b5.zip |
Limit live bitrate
Diffstat (limited to 'server/lib/transcoding/video-transcoding-profiles.ts')
-rw-r--r-- | server/lib/transcoding/video-transcoding-profiles.ts | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/server/lib/transcoding/video-transcoding-profiles.ts b/server/lib/transcoding/video-transcoding-profiles.ts index c5ea72a5f..2309f38d4 100644 --- a/server/lib/transcoding/video-transcoding-profiles.ts +++ b/server/lib/transcoding/video-transcoding-profiles.ts | |||
@@ -1,14 +1,7 @@ | |||
1 | import { logger } from '@server/helpers/logger' | 1 | import { logger } from '@server/helpers/logger' |
2 | import { AvailableEncoders, EncoderOptionsBuilder, getTargetBitrate, VideoResolution } from '../../../shared/models/videos' | 2 | import { AvailableEncoders, EncoderOptionsBuilder, getTargetBitrate, VideoResolution } from '../../../shared/models/videos' |
3 | import { buildStreamSuffix, resetSupportedEncoders } from '../../helpers/ffmpeg-utils' | 3 | import { buildStreamSuffix, resetSupportedEncoders } from '../../helpers/ffmpeg-utils' |
4 | import { | 4 | import { canDoQuickAudioTranscode, ffprobePromise, getAudioStream, getMaxAudioBitrate } from '../../helpers/ffprobe-utils' |
5 | canDoQuickAudioTranscode, | ||
6 | ffprobePromise, | ||
7 | getAudioStream, | ||
8 | getMaxAudioBitrate, | ||
9 | getVideoFileBitrate, | ||
10 | getVideoStreamFromFile | ||
11 | } from '../../helpers/ffprobe-utils' | ||
12 | import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants' | 5 | import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants' |
13 | 6 | ||
14 | /** | 7 | /** |
@@ -22,8 +15,8 @@ import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants' | |||
22 | // * https://slhck.info/video/2017/03/01/rate-control.html | 15 | // * https://slhck.info/video/2017/03/01/rate-control.html |
23 | // * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate | 16 | // * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate |
24 | 17 | ||
25 | const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ input, resolution, fps }) => { | 18 | const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ inputBitrate, resolution, fps }) => { |
26 | const targetBitrate = await buildTargetBitrate({ input, resolution, fps }) | 19 | const targetBitrate = buildTargetBitrate({ inputBitrate, resolution, fps }) |
27 | if (!targetBitrate) return { outputOptions: [ ] } | 20 | if (!targetBitrate) return { outputOptions: [ ] } |
28 | 21 | ||
29 | return { | 22 | return { |
@@ -36,8 +29,8 @@ const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ input, reso | |||
36 | } | 29 | } |
37 | } | 30 | } |
38 | 31 | ||
39 | const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async ({ resolution, fps, streamNum }) => { | 32 | const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async ({ resolution, fps, inputBitrate, streamNum }) => { |
40 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | 33 | const targetBitrate = buildTargetBitrate({ inputBitrate, resolution, fps }) |
41 | 34 | ||
42 | return { | 35 | return { |
43 | outputOptions: [ | 36 | outputOptions: [ |
@@ -237,20 +230,16 @@ export { | |||
237 | } | 230 | } |
238 | 231 | ||
239 | // --------------------------------------------------------------------------- | 232 | // --------------------------------------------------------------------------- |
240 | async function buildTargetBitrate (options: { | 233 | |
241 | input: string | 234 | function buildTargetBitrate (options: { |
235 | inputBitrate: number | ||
242 | resolution: VideoResolution | 236 | resolution: VideoResolution |
243 | fps: number | 237 | fps: number |
244 | }) { | 238 | }) { |
245 | const { input, resolution, fps } = options | 239 | const { inputBitrate, resolution, fps } = options |
246 | const probe = await ffprobePromise(input) | ||
247 | |||
248 | const videoStream = await getVideoStreamFromFile(input, probe) | ||
249 | if (!videoStream) return undefined | ||
250 | 240 | ||
251 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | 241 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) |
242 | if (!inputBitrate) return targetBitrate | ||
252 | 243 | ||
253 | // Don't transcode to an higher bitrate than the original file | 244 | return Math.min(targetBitrate, inputBitrate) |
254 | const fileBitrate = await getVideoFileBitrate(input, probe) | ||
255 | return Math.min(targetBitrate, fileBitrate) | ||
256 | } | 245 | } |