+function addDefaultEncoderGlobalParams (options: {
+ command: FfmpegCommand
+}) {
+ const { command } = options
+
+ // avoid issues when transcoding some files: https://trac.ffmpeg.org/ticket/6375
+ command.outputOption('-max_muxing_queue_size 1024')
+ // strip all metadata
+ .outputOption('-map_metadata -1')
+ // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it
+ .outputOption('-b_strategy 1')
+ // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
+ .outputOption('-bf 16')
+ // allows import of source material with incompatible pixel formats (e.g. MJPEG video)
+ .outputOption('-pix_fmt yuv420p')
+}
+
+function addDefaultEncoderParams (options: {
+ command: FfmpegCommand
+ encoder: 'libx264' | string
+ streamNum?: number
+ fps?: number
+}) {
+ const { command, encoder, fps, streamNum } = options
+
+ if (encoder === 'libx264') {
+ // 3.1 is the minimal resource allocation for our highest supported resolution
+ command.outputOption(buildStreamSuffix('-level:v', streamNum) + ' 3.1')
+
+ if (fps) {
+ // Keyframe interval of 2 seconds for faster seeking and resolution switching.
+ // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html
+ // https://superuser.com/a/908325
+ command.outputOption(buildStreamSuffix('-g:v', streamNum) + ' ' + (fps * 2))
+ }
+ }
+}
+
+function addDefaultLiveHLSParams (command: FfmpegCommand, outPath: string, masterPlaylistName: string) {
+ command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS)
+ command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE)
+ command.outputOption('-hls_flags delete_segments+independent_segments')
+ command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`)
+ command.outputOption('-master_pl_name ' + masterPlaylistName)
+ command.outputOption(`-f hls`)
+
+ command.output(join(outPath, '%v.m3u8'))
+}
+
+// ---------------------------------------------------------------------------
+// Transcode VOD command builders
+// ---------------------------------------------------------------------------
+
+async function buildx264VODCommand (command: FfmpegCommand, options: TranscodeOptions) {
+ let fps = await getVideoFileFPS(options.inputPath)
+ fps = computeFPS(fps, options.resolution)
+
+ let scaleFilterValue: string
+
+ if (options.resolution !== undefined) {
+ scaleFilterValue = options.isPortraitMode === true
+ ? `w=${options.resolution}:h=-2`
+ : `w=-2:h=${options.resolution}`
+ }
+
+ command = await presetVideo({ command, input: options.inputPath, transcodeOptions: options, fps, scaleFilterValue })
+
+ return command
+}
+
+async function buildAudioMergeCommand (command: FfmpegCommand, options: MergeAudioTranscodeOptions) {
+ command = command.loop(undefined)
+
+ const scaleFilterValue = getScaleCleanerValue()
+ command = await presetVideo({ command, input: options.audioPath, transcodeOptions: options, scaleFilterValue })
+
+ command.outputOption('-preset:v veryfast')
+
+ command = command.input(options.audioPath)
+ .outputOption('-tune stillimage')
+ .outputOption('-shortest')
+
+ return command
+}
+
+function buildOnlyAudioCommand (command: FfmpegCommand, _options: OnlyAudioTranscodeOptions) {
+ command = presetOnlyAudio(command)
+
+ return command
+}
+
+function buildQuickTranscodeCommand (command: FfmpegCommand) {
+ command = presetCopy(command)
+
+ command = command.outputOption('-map_metadata -1') // strip all metadata
+ .outputOption('-movflags faststart')
+
+ return command
+}
+
+function addCommonHLSVODCommandOptions (command: FfmpegCommand, outputPath: string) {
+ return command.outputOption('-hls_time 4')
+ .outputOption('-hls_list_size 0')
+ .outputOption('-hls_playlist_type vod')
+ .outputOption('-hls_segment_filename ' + outputPath)
+ .outputOption('-hls_segment_type fmp4')
+ .outputOption('-f hls')
+ .outputOption('-hls_flags single_file')
+}
+
+async function buildHLSVODCommand (command: FfmpegCommand, options: HLSTranscodeOptions) {
+ const videoPath = getHLSVideoPath(options)
+
+ if (options.copyCodecs) command = presetCopy(command)
+ else if (options.resolution === VideoResolution.H_NOVIDEO) command = presetOnlyAudio(command)
+ else command = await buildx264VODCommand(command, options)
+
+ addCommonHLSVODCommandOptions(command, videoPath)
+
+ return command
+}
+
+function buildHLSVODFromTSCommand (command: FfmpegCommand, options: HLSFromTSTranscodeOptions) {
+ const videoPath = getHLSVideoPath(options)
+
+ command.outputOption('-c copy')
+
+ if (options.isAAC) {
+ // Required for example when copying an AAC stream from an MPEG-TS
+ // Since it's a bitstream filter, we don't need to reencode the audio
+ command.outputOption('-bsf:a aac_adtstoasc')
+ }
+
+ addCommonHLSVODCommandOptions(command, videoPath)
+
+ return command
+}
+
+async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) {
+ if (options.type !== 'hls' && options.type !== 'hls-from-ts') return
+
+ const fileContent = await readFile(options.outputPath)
+
+ const videoFileName = options.hlsPlaylist.videoFilename
+ const videoFilePath = getHLSVideoPath(options)
+
+ // Fix wrong mapping with some ffmpeg versions
+ const newContent = fileContent.toString()
+ .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`)
+
+ await writeFile(options.outputPath, newContent)
+}
+
+function getHLSVideoPath (options: HLSTranscodeOptions | HLSFromTSTranscodeOptions) {
+ return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}`