]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/ffmpeg-utils.ts
Don't add live videos in redundancy
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
index 268ed7624a306b270318a7b6dc2dc783bc4f65f4..69564a1a3b4c068b4aaad19987c5b3b0a7b0d74a 100644 (file)
@@ -271,6 +271,8 @@ type TranscodeOptions =
   | QuickTranscodeOptions
 
 function transcode (options: TranscodeOptions) {
+  logger.debug('Will run transcode.', { options })
+
   return new Promise<void>(async (res, rej) => {
     try {
       let command = getFFmpeg(options.inputPath)
@@ -353,7 +355,7 @@ function convertWebPToJPG (path: string, destination: string): Promise<void> {
   })
 }
 
-function runLiveTranscoding (rtmpUrl: string, outPath: string, resolutions: number[], deleteSegments: boolean) {
+function runLiveTranscoding (rtmpUrl: string, outPath: string, resolutions: number[], fps, deleteSegments: boolean) {
   const command = getFFmpeg(rtmpUrl)
   command.inputOption('-fflags nobuffer')
 
@@ -375,10 +377,6 @@ function runLiveTranscoding (rtmpUrl: string, outPath: string, resolutions: numb
     }))
   ])
 
-  const liveFPS = VIDEO_TRANSCODING_FPS.AVERAGE
-
-  command.withFps(liveFPS)
-
   command.outputOption('-b_strategy 1')
   command.outputOption('-bf 16')
   command.outputOption('-preset superfast')
@@ -386,13 +384,14 @@ function runLiveTranscoding (rtmpUrl: string, outPath: string, resolutions: numb
   command.outputOption('-map_metadata -1')
   command.outputOption('-pix_fmt yuv420p')
   command.outputOption('-max_muxing_queue_size 1024')
+  command.outputOption('-g ' + (fps * 2))
 
   for (let i = 0; i < resolutions.length; i++) {
     const resolution = resolutions[i]
 
     command.outputOption(`-map [vout${resolution}]`)
     command.outputOption(`-c:v:${i} libx264`)
-    command.outputOption(`-b:v:${i} ${getTargetBitrate(resolution, liveFPS, VIDEO_TRANSCODING_FPS)}`)
+    command.outputOption(`-b:v:${i} ${getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)}`)
 
     command.outputOption(`-map a:0`)
     command.outputOption(`-c:a:${i} aac`)
@@ -426,8 +425,14 @@ function runLiveMuxing (rtmpUrl: string, outPath: string, deleteSegments: boolea
 }
 
 async function hlsPlaylistToFragmentedMP4 (hlsDirectory: string, segmentFiles: string[], outputPath: string) {
-  const concatFile = 'concat.txt'
-  const concatFilePath = join(hlsDirectory, concatFile)
+  const concatFilePath = join(hlsDirectory, 'concat.txt')
+
+  function cleaner () {
+    remove(concatFilePath)
+      .catch(err => logger.error('Cannot remove concat file in %s.', hlsDirectory, { err }))
+  }
+
+  // First concat the ts files to a mp4 file
   const content = segmentFiles.map(f => 'file ' + f)
                               .join('\n')
 
@@ -437,25 +442,25 @@ async function hlsPlaylistToFragmentedMP4 (hlsDirectory: string, segmentFiles: s
   command.inputOption('-safe 0')
   command.inputOption('-f concat')
 
-  command.outputOption('-c copy')
+  command.outputOption('-c:v copy')
+  command.audioFilter('aresample=async=1:first_pts=0')
   command.output(outputPath)
 
-  command.run()
+  return runCommand(command, cleaner)
+}
 
-  function cleaner () {
-    remove(concatFile)
-    .catch(err => logger.error('Cannot remove concat file in %s.', hlsDirectory, { err }))
-  }
+async function runCommand (command: ffmpeg.FfmpegCommand, onEnd?: Function) {
+  command.run()
 
   return new Promise<string>((res, rej) => {
     command.on('error', err => {
-      cleaner()
+      if (onEnd) onEnd()
 
       rej(err)
     })
 
     command.on('end', () => {
-      cleaner()
+      if (onEnd) onEnd()
 
       res()
     })
@@ -497,14 +502,14 @@ function addDefaultX264Params (command: ffmpeg.FfmpegCommand) {
 }
 
 function addDefaultLiveHLSParams (command: ffmpeg.FfmpegCommand, outPath: string, deleteSegments: boolean) {
-  command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME)
+  command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS)
   command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE)
 
   if (deleteSegments === true) {
     command.outputOption('-hls_flags delete_segments')
   }
 
-  command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%d.ts')}`)
+  command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%04d.ts')}`)
   command.outputOption('-master_pl_name master.m3u8')
   command.outputOption(`-f hls`)