1 import { logger } from '@server/helpers/logger'
2 import { getTargetBitrate } from '../../shared/models/videos'
3 import { AvailableEncoders, buildStreamSuffix, EncoderOptionsBuilder } from '../helpers/ffmpeg-utils'
5 canDoQuickAudioTranscode,
10 getVideoStreamFromFile
11 } from '../helpers/ffprobe-utils'
12 import { VIDEO_TRANSCODING_FPS } from '../initializers/constants'
16 * Available encoders and profiles for the transcoding jobs
17 * These functions are used by ffmpeg-utils that will get the encoders and options depending on the chosen profile
22 // * https://slhck.info/video/2017/03/01/rate-control.html
23 // * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
25 const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ input, resolution, fps }) => {
26 let targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
28 const probe = await ffprobePromise(input)
30 const videoStream = await getVideoStreamFromFile(input, probe)
32 return { outputOptions: [ ] }
35 // Don't transcode to an higher bitrate than the original file
36 const fileBitrate = await getVideoFileBitrate(input, probe)
37 targetBitrate = Math.min(targetBitrate, fileBitrate)
41 `-maxrate ${targetBitrate}`,
42 `-bufsize ${targetBitrate * 2}`
47 const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async ({ resolution, fps, streamNum }) => {
48 const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
52 `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`,
53 `-maxrate ${targetBitrate}`,
54 `-bufsize ${targetBitrate * 2}`
59 const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum }) => {
60 const probe = await ffprobePromise(input)
62 if (await canDoQuickAudioTranscode(input, probe)) {
63 logger.debug('Copy audio stream %s by AAC encoder.', input)
64 return { copy: true, outputOptions: [] }
67 const parsedAudio = await getAudioStream(input, probe)
69 // We try to reduce the ceiling bitrate by making rough matches of bitrates
70 // Of course this is far from perfect, but it might save some space in the end
72 const audioCodecName = parsedAudio.audioStream['codec_name']
74 const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate)
76 logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName })
78 if (bitrate !== undefined && bitrate !== -1) {
79 return { outputOptions: [ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ] }
82 return { outputOptions: [ ] }
85 const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => {
86 return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] }
89 const availableEncoders: AvailableEncoders = {
92 default: defaultX264VODOptionsBuilder
95 default: defaultAACOptionsBuilder
98 default: defaultLibFDKAACVODOptionsBuilder
103 default: defaultX264LiveOptionsBuilder
106 default: defaultAACOptionsBuilder
111 // ---------------------------------------------------------------------------
117 // ---------------------------------------------------------------------------