]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/ffmpeg/ffmpeg-live.ts
Don't use safe mode when normalizing
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg / ffmpeg-live.ts
index ff571626c98bb11557c944d3df3820e488c30671..8377dc7e25435f72d72204e813d8afcf8d4092de 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[]
 
@@ -26,7 +27,7 @@ async function getLiveTranscodingCommand (options: {
   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 } = options
 
   const command = getFFmpeg(inputUrl, 'live')
 
@@ -120,14 +121,21 @@ async function getLiveTranscodingCommand (options: {
 
   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 +143,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`)