]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/ffmpeg-utils.ts
Remove unnecessary NPM_RUN_BUILD_OPTS docker arg
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
index 54fd031b75ae48bdb71038c869f66509738ed5d6..78ee5fa7f9a12f5f03f52cab8def28c9daab3731 100644 (file)
@@ -16,7 +16,9 @@ import { CONFIG } from '../initializers/config'
 import { execPromise, promisify0 } from './core-utils'
 import { computeFPS, ffprobePromise, getAudioStream, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from './ffprobe-utils'
 import { processImage } from './image-utils'
-import { logger } from './logger'
+import { logger, loggerTagsFactory } from './logger'
+
+const lTags = loggerTagsFactory('ffmpeg')
 
 /**
  *
@@ -60,7 +62,7 @@ async function checkFFmpegEncoders (peertubeAvailableEncoders: AvailableEncoders
     supportedEncoders.set(searchEncoder, availableFFmpegEncoders[searchEncoder] !== undefined)
   }
 
-  logger.info('Built supported ffmpeg encoders.', { supportedEncoders, searchEncoders })
+  logger.info('Built supported ffmpeg encoders.', { supportedEncoders, searchEncoders, ...lTags() })
 
   return supportedEncoders
 }
@@ -115,12 +117,12 @@ async function generateImageFromVideoFile (fromPath: string, folder: string, ima
     const destination = join(folder, imageName)
     await processImage(pendingImagePath, destination, size)
   } catch (err) {
-    logger.error('Cannot generate image from video %s.', fromPath, { err })
+    logger.error('Cannot generate image from video %s.', fromPath, { err, ...lTags() })
 
     try {
       await remove(pendingImagePath)
     } catch (err) {
-      logger.debug('Cannot remove pending image path after generation error.', { err })
+      logger.debug('Cannot remove pending image path after generation error.', { err, ...lTags() })
     }
   }
 }
@@ -202,7 +204,7 @@ const builders: {
 }
 
 async function transcode (options: TranscodeOptions) {
-  logger.debug('Will run transcode.', { options })
+  logger.debug('Will run transcode.', { options, ...lTags() })
 
   let command = getFFmpeg(options.inputPath, 'vod')
     .output(options.outputPath)
@@ -219,7 +221,7 @@ async function transcode (options: TranscodeOptions) {
 // ---------------------------------------------------------------------------
 
 async function getLiveTranscodingCommand (options: {
-  rtmpUrl: string
+  inputUrl: string
 
   outPath: string
   masterPlaylistName: string
@@ -234,10 +236,9 @@ async function getLiveTranscodingCommand (options: {
   availableEncoders: AvailableEncoders
   profile: string
 }) {
-  const { rtmpUrl, outPath, resolutions, fps, bitrate, availableEncoders, profile, masterPlaylistName, ratio } = options
-  const input = rtmpUrl
+  const { inputUrl, outPath, resolutions, fps, bitrate, availableEncoders, profile, masterPlaylistName, ratio } = options
 
-  const command = getFFmpeg(input, 'live')
+  const command = getFFmpeg(inputUrl, 'live')
 
   const varStreamMap: string[] = []
 
@@ -259,7 +260,7 @@ async function getLiveTranscodingCommand (options: {
     const resolutionFPS = computeFPS(fps, resolution)
 
     const baseEncoderBuilderParams = {
-      input,
+      input: inputUrl,
 
       availableEncoders,
       profile,
@@ -285,7 +286,10 @@ async function getLiveTranscodingCommand (options: {
 
       addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i })
 
-      logger.debug('Apply ffmpeg live video params from %s using %s profile.', builderResult.encoder, profile, builderResult)
+      logger.debug(
+        'Apply ffmpeg live video params from %s using %s profile.', builderResult.encoder, profile,
+        { builderResult, fps: resolutionFPS, resolution, ...lTags() }
+      )
 
       command.outputOption(`${buildStreamSuffix('-c:v', i)} ${builderResult.encoder}`)
       applyEncoderOptions(command, builderResult.result)
@@ -309,7 +313,10 @@ async function getLiveTranscodingCommand (options: {
 
       addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i })
 
-      logger.debug('Apply ffmpeg live audio params from %s using %s profile.', builderResult.encoder, profile, builderResult)
+      logger.debug(
+        'Apply ffmpeg live audio params from %s using %s profile.', builderResult.encoder, profile,
+        { builderResult, fps: resolutionFPS, resolution, ...lTags() }
+      )
 
       command.outputOption(`${buildStreamSuffix('-c:a', i)} ${builderResult.encoder}`)
       applyEncoderOptions(command, builderResult.result)
@@ -327,8 +334,8 @@ async function getLiveTranscodingCommand (options: {
   return command
 }
 
-function getLiveMuxingCommand (rtmpUrl: string, outPath: string, masterPlaylistName: string) {
-  const command = getFFmpeg(rtmpUrl, 'live')
+function getLiveMuxingCommand (inputUrl: string, outPath: string, masterPlaylistName: string) {
+  const command = getFFmpeg(inputUrl, 'live')
 
   command.outputOption('-c:v copy')
   command.outputOption('-c:a copy')
@@ -361,10 +368,6 @@ function addDefaultEncoderGlobalParams (options: {
   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')
 }
@@ -532,12 +535,12 @@ async function getEncoderBuilderResult (options: EncoderOptionsBuilderParams & {
 
   for (const encoder of encodersToTry) {
     if (!(await checkFFmpegEncoders(availableEncoders)).get(encoder)) {
-      logger.debug('Encoder %s not available in ffmpeg, skipping.', encoder)
+      logger.debug('Encoder %s not available in ffmpeg, skipping.', encoder, lTags())
       continue
     }
 
     if (!encoders[encoder]) {
-      logger.debug('Encoder %s not available in peertube encoders, skipping.', encoder)
+      logger.debug('Encoder %s not available in peertube encoders, skipping.', encoder, lTags())
       continue
     }
 
@@ -546,11 +549,11 @@ async function getEncoderBuilderResult (options: EncoderOptionsBuilderParams & {
     let builder = builderProfiles[profile]
 
     if (!builder) {
-      logger.debug('Profile %s for encoder %s not available. Fallback to default.', profile, encoder)
+      logger.debug('Profile %s for encoder %s not available. Fallback to default.', profile, encoder, lTags())
       builder = builderProfiles.default
 
       if (!builder) {
-        logger.debug('Default profile for encoder %s not available. Try next available encoder.', encoder)
+        logger.debug('Default profile for encoder %s not available. Try next available encoder.', encoder, lTags())
         continue
       }
     }
@@ -620,7 +623,8 @@ async function presetVideo (options: {
 
     logger.debug(
       'Apply ffmpeg params from %s for %s stream of input %s using %s profile.',
-      builderResult.encoder, streamType, input, profile, builderResult
+      builderResult.encoder, streamType, input, profile,
+      { builderResult, resolution, fps, ...lTags() }
     )
 
     if (streamType === 'video') {
@@ -726,13 +730,13 @@ async function runCommand (options: {
     command.on('start', cmdline => { shellCommand = cmdline })
 
     command.on('error', (err, stdout, stderr) => {
-      if (silent !== true) logger.error('Error in ffmpeg.', { stdout, stderr })
+      if (silent !== true) logger.error('Error in ffmpeg.', { stdout, stderr, shellCommand, ...lTags() })
 
       rej(err)
     })
 
     command.on('end', (stdout, stderr) => {
-      logger.debug('FFmpeg command ended.', { stdout, stderr, shellCommand })
+      logger.debug('FFmpeg command ended.', { stdout, stderr, shellCommand, ...lTags() })
 
       res()
     })
@@ -742,7 +746,7 @@ async function runCommand (options: {
         if (!progress.percent) return
 
         job.progress(Math.round(progress.percent))
-          .catch(err => logger.warn('Cannot set ffmpeg job progress.', { err }))
+          .catch(err => logger.warn('Cannot set ffmpeg job progress.', { err, ...lTags() }))
       })
     }