Improve target bitrate calculation

This commit is contained in:
Chocobozzz committed 2021-08-06 14:13:26 +02:00
1 parent c826f34a45
commit 679c12e69c
24 files changed
+263 -204

No files matched your search

+23 -12
View File
@@ -3,10 +3,18 @@ import * as ffmpeg from 'fluent-ffmpeg'
import { readFile, remove, writeFile } from 'fs-extra'
import { dirname, join } from 'path'
import { FFMPEG_NICE, VIDEO_LIVE } from '@server/initializers/constants'
import { AvailableEncoders, EncoderOptions, EncoderOptionsBuilder, EncoderProfile, VideoResolution } from '../../shared/models/videos'
import { pick } from '@shared/core-utils'
import {
AvailableEncoders,
EncoderOptions,
EncoderOptionsBuilder,
EncoderOptionsBuilderParams,
EncoderProfile,
VideoResolution
} from '../../shared/models/videos'
import { CONFIG } from '../initializers/config'
import { execPromise, promisify0 } from './core-utils'
import { computeFPS, ffprobePromise, getAudioStream, getVideoFileBitrate, getVideoFileFPS } from './ffprobe-utils'
import { computeFPS, ffprobePromise, getAudioStream, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from './ffprobe-utils'
import { processImage } from './image-utils'
import { logger } from './logger'
@@ -217,13 +225,16 @@ async function getLiveTranscodingCommand (options: {
masterPlaylistName: string
resolutions: number[]
// Input information
fps: number
bitrate: number
ratio: number
availableEncoders: AvailableEncoders
profile: string
}) {
const { rtmpUrl, outPath, resolutions, fps, bitrate, availableEncoders, profile, masterPlaylistName } = options
const { rtmpUrl, outPath, resolutions, fps, bitrate, availableEncoders, profile, masterPlaylistName, ratio } = options
const input = rtmpUrl
const command = getFFmpeg(input, 'live')
@@ -253,9 +264,12 @@ async function getLiveTranscodingCommand (options: {
availableEncoders,
profile,
fps: resolutionFPS,
inputBitrate: bitrate,
inputRatio: ratio,
resolution,
fps: resolutionFPS,
streamNum: i,
videoType: 'live' as 'live'
}
@@ -502,7 +516,7 @@ function getHLSVideoPath (options: HLSTranscodeOptions | HLSFromTSTranscodeOptio
// Run encoder builder depending on available encoders
// Try encoders by priority: if the encoder is available, run the chosen profile or fallback to the default one
// If the default one does not exist, check the next encoder
async function getEncoderBuilderResult (options: {
async function getEncoderBuilderResult (options: EncoderOptionsBuilderParams & {
streamType: 'video' | 'audio'
input: string
@@ -510,13 +524,8 @@ async function getEncoderBuilderResult (options: {
profile: string
videoType: 'vod' | 'live'
resolution: number
inputBitrate: number
fps?: number
streamNum?: number
}) {
const { availableEncoders, input, profile, resolution, streamType, fps, inputBitrate, streamNum, videoType } = options
const { availableEncoders, profile, streamType, videoType } = options
const encodersToTry = availableEncoders.encodersToTry[videoType][streamType]
const encoders = availableEncoders.available[videoType]
@@ -546,7 +555,7 @@ async function getEncoderBuilderResult (options: {
}
}
const result = await builder({ input, resolution, inputBitrate, fps, streamNum })
const result = await builder(pick(options, [ 'input', 'resolution', 'inputBitrate', 'fps', 'inputRatio', 'streamNum' ]))
return {
result,
@@ -581,6 +590,7 @@ async function presetVideo (options: {
// Audio encoder
const parsedAudio = await getAudioStream(input, probe)
const bitrate = await getVideoFileBitrate(input, probe)
const { ratio } = await getVideoFileResolution(input, probe)
let streamsToProcess: StreamType[] = [ 'audio', 'video' ]
@@ -600,6 +610,7 @@ async function presetVideo (options: {
profile,
fps,
inputBitrate: bitrate,
inputRatio: ratio,
videoType: 'vod' as 'vod'
})
+10 -6
View File
@@ -1,5 +1,6 @@
import * as ffmpeg from 'fluent-ffmpeg'
import { getMaxBitrate, VideoFileMetadata, VideoResolution } from '../../shared/models/videos'
import { getMaxBitrate } from '@shared/core-utils'
import { VideoFileMetadata, VideoResolution, VideoTranscodingFPS } from '../../shared/models/videos'
import { CONFIG } from '../initializers/config'
import { VIDEO_TRANSCODING_FPS } from '../initializers/constants'
import { logger } from './logger'
@@ -75,7 +76,7 @@ function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
}
}
async function getVideoStreamSize (path: string, existingProbe?: ffmpeg.FfprobeData) {
async function getVideoStreamSize (path: string, existingProbe?: ffmpeg.FfprobeData): Promise<{ width: number, height: number }> {
const videoStream = await getVideoStreamFromFile(path, existingProbe)
return videoStream === null
@@ -146,7 +147,10 @@ async function getVideoFileResolution (path: string, existingProbe?: ffmpeg.Ffpr
const size = await getVideoStreamSize(path, existingProbe)
return {
videoFileResolution: Math.min(size.height, size.width),
width: size.width,
height: size.height,
ratio: Math.max(size.height, size.width) / Math.min(size.height, size.width),
resolution: Math.min(size.height, size.width),
isPortraitMode: size.height > size.width
}
}
@@ -243,7 +247,7 @@ async function canDoQuickVideoTranscode (path: string, probe?: ffmpeg.FfprobeDat
const videoStream = await getVideoStreamFromFile(path, probe)
const fps = await getVideoFileFPS(path, probe)
const bitRate = await getVideoFileBitrate(path, probe)
const resolution = await getVideoFileResolution(path, probe)
const resolutionData = await getVideoFileResolution(path, probe)
// If ffprobe did not manage to guess the bitrate
if (!bitRate) return false
@@ -253,7 +257,7 @@ async function canDoQuickVideoTranscode (path: string, probe?: ffmpeg.FfprobeDat
if (videoStream['codec_name'] !== 'h264') return false
if (videoStream['pix_fmt'] !== 'yuv420p') return false
if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false
if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false
if (bitRate > getMaxBitrate({ ...resolutionData, fps })) return false
return true
}
@@ -278,7 +282,7 @@ async function canDoQuickAudioTranscode (path: string, probe?: ffmpeg.FfprobeDat
return true
}
function getClosestFramerateStandard (fps: number, type: 'HD_STANDARD' | 'STANDARD'): number {
function getClosestFramerateStandard <K extends keyof Pick<VideoTranscodingFPS, 'HD_STANDARD' | 'STANDARD'>> (fps: number, type: K) {
return VIDEO_TRANSCODING_FPS[type].slice(0)
.sort((a, b) => fps % a - fps % b)[0]
}