1 import { pick } from '@shared/core-utils'
2 import { FFmpegCommandWrapper } from '../ffmpeg-command-wrapper'
3 import { ffprobePromise, getVideoStreamBitrate, getVideoStreamDimensionsInfo, hasAudioStream } from '../ffprobe'
4 import { addDefaultEncoderGlobalParams, addDefaultEncoderParams, applyEncoderOptions } from './encoder-options'
5 import { getScaleFilter, StreamType } from '../ffmpeg-utils'
7 export async function presetVOD (options: {
8 commandWrapper: FFmpegCommandWrapper
18 scaleFilterValue?: string
20 const { commandWrapper, input, resolution, fps, scaleFilterValue } = options
21 const command = commandWrapper.getCommand()
24 .outputOption('-movflags faststart')
26 addDefaultEncoderGlobalParams(command)
28 const probe = await ffprobePromise(input)
31 const bitrate = await getVideoStreamBitrate(input, probe)
32 const videoStreamDimensions = await getVideoStreamDimensionsInfo(input, probe)
34 let streamsToProcess: StreamType[] = [ 'audio', 'video' ]
36 if (!await hasAudioStream(input, probe)) {
38 streamsToProcess = [ 'video' ]
41 for (const streamType of streamsToProcess) {
42 const builderResult = await commandWrapper.getEncoderBuilderResult({
43 ...pick(options, [ 'canCopyAudio', 'canCopyVideo' ]),
46 inputBitrate: bitrate,
47 inputRatio: videoStreamDimensions?.ratio || 0,
53 videoType: 'vod' as 'vod'
57 throw new Error('No available encoder found for stream ' + streamType)
60 commandWrapper.debugLog(
61 `Apply ffmpeg params from ${builderResult.encoder} for ${streamType} ` +
62 `stream of input ${input} using ${commandWrapper.getProfile()} profile.`,
63 { builderResult, resolution, fps }
66 if (streamType === 'video') {
67 command.videoCodec(builderResult.encoder)
69 if (scaleFilterValue) {
70 command.outputOption(`-vf ${getScaleFilter(builderResult.result)}=${scaleFilterValue}`)
72 } else if (streamType === 'audio') {
73 command.audioCodec(builderResult.encoder)
76 applyEncoderOptions(command, builderResult.result)
77 addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps })
81 export function presetCopy (commandWrapper: FFmpegCommandWrapper) {
82 commandWrapper.getCommand()
88 export function presetOnlyAudio (commandWrapper: FFmpegCommandWrapper) {
89 commandWrapper.getCommand()