]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/ffmpeg/ffmpeg-live.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg / ffmpeg-live.ts
index ff571626c98bb11557c944d3df3820e488c30671..379d7b1ad5b532b72162bd6eb4801aa2b4351cfb 100644 (file)
@@ -1,7 +1,7 @@
 import { FfmpegCommand, FilterSpecification } from 'fluent-ffmpeg'
 import { join } from 'path'
 import { VIDEO_LIVE } from '@server/initializers/constants'
-import { AvailableEncoders } from '@shared/models'
+import { AvailableEncoders, LiveVideoLatencyMode } from '@shared/models'
 import { logger, loggerTagsFactory } from '../logger'
 import { buildStreamSuffix, getFFmpeg, getScaleFilter, StreamType } from './ffmpeg-commons'
 import { getEncoderBuilderResult } from './ffmpeg-encoders'
@@ -15,6 +15,7 @@ async function getLiveTranscodingCommand (options: {
 
   outPath: string
   masterPlaylistName: string
+  latencyMode: LiveVideoLatencyMode
 
   resolutions: number[]
 
@@ -22,11 +23,24 @@ async function getLiveTranscodingCommand (options: {
   fps: number
   bitrate: number
   ratio: number
+  hasAudio: boolean
 
   availableEncoders: AvailableEncoders
   profile: string
 }) {
-  const { inputUrl, outPath, resolutions, fps, bitrate, availableEncoders, profile, masterPlaylistName, ratio } = options
+  const {
+    inputUrl,
+    outPath,
+    resolutions,
+    fps,
+    bitrate,
+    availableEncoders,
+    profile,
+    masterPlaylistName,
+    ratio,
+    latencyMode,
+    hasAudio
+  } = options
 
   const command = getFFmpeg(inputUrl, 'live')
 
@@ -46,6 +60,7 @@ async function getLiveTranscodingCommand (options: {
   addDefaultEncoderGlobalParams(command)
 
   for (let i = 0; i < resolutions.length; i++) {
+    const streamMap: string[] = []
     const resolution = resolutions[i]
     const resolutionFPS = computeFPS(fps, resolution)
 
@@ -93,9 +108,11 @@ async function getLiveTranscodingCommand (options: {
         options: `w=-2:h=${resolution}`,
         outputs: `vout${resolution}`
       })
+
+      streamMap.push(`v:${i}`)
     }
 
-    {
+    if (hasAudio) {
       const streamType: StreamType = 'audio'
       const builderResult = await getEncoderBuilderResult({ ...baseEncoderBuilderParams, streamType })
       if (!builderResult) {
@@ -113,21 +130,30 @@ async function getLiveTranscodingCommand (options: {
 
       command.outputOption(`${buildStreamSuffix('-c:a', i)} ${builderResult.encoder}`)
       applyEncoderOptions(command, builderResult.result)
+
+      streamMap.push(`a:${i}`)
     }
 
-    varStreamMap.push(`v:${i},a:${i}`)
+    varStreamMap.push(streamMap.join(','))
   }
 
   command.complexFilter(complexFilter)
 
-  addDefaultLiveHLSParams(command, outPath, masterPlaylistName)
+  addDefaultLiveHLSParams({ command, outPath, masterPlaylistName, latencyMode })
 
   command.outputOption('-var_stream_map', varStreamMap.join(' '))
 
   return command
 }
 
-function getLiveMuxingCommand (inputUrl: string, outPath: string, masterPlaylistName: string) {
+function getLiveMuxingCommand (options: {
+  inputUrl: string
+  outPath: string
+  masterPlaylistName: string
+  latencyMode: LiveVideoLatencyMode
+}) {
+  const { inputUrl, outPath, masterPlaylistName, latencyMode } = options
+
   const command = getFFmpeg(inputUrl, 'live')
 
   command.outputOption('-c:v copy')
@@ -135,24 +161,41 @@ function getLiveMuxingCommand (inputUrl: string, outPath: string, masterPlaylist
   command.outputOption('-map 0:a?')
   command.outputOption('-map 0:v?')
 
-  addDefaultLiveHLSParams(command, outPath, masterPlaylistName)
+  addDefaultLiveHLSParams({ command, outPath, masterPlaylistName, latencyMode })
 
   return command
 }
 
+function getLiveSegmentTime (latencyMode: LiveVideoLatencyMode) {
+  if (latencyMode === LiveVideoLatencyMode.SMALL_LATENCY) {
+    return VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY
+  }
+
+  return VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY
+}
+
 // ---------------------------------------------------------------------------
 
 export {
+  getLiveSegmentTime,
+
   getLiveTranscodingCommand,
   getLiveMuxingCommand
 }
 
 // ---------------------------------------------------------------------------
 
-function addDefaultLiveHLSParams (command: FfmpegCommand, outPath: string, masterPlaylistName: string) {
-  command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS)
+function addDefaultLiveHLSParams (options: {
+  command: FfmpegCommand
+  outPath: string
+  masterPlaylistName: string
+  latencyMode: LiveVideoLatencyMode
+}) {
+  const { command, outPath, masterPlaylistName, latencyMode } = options
+
+  command.outputOption('-hls_time ' + getLiveSegmentTime(latencyMode))
   command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE)
-  command.outputOption('-hls_flags delete_segments+independent_segments')
+  command.outputOption('-hls_flags delete_segments+independent_segments+program_date_time')
   command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`)
   command.outputOption('-master_pl_name ' + masterPlaylistName)
   command.outputOption(`-f hls`)