]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/ffprobe-utils.ts
Correctlu throws an error on manifestIncompatibleCodecsError
[github/Chocobozzz/PeerTube.git] / server / helpers / ffprobe-utils.ts
index d03ab91ac93c23bcdcaca0b0e6c080eb8b0e150e..5b1ad9066b215d4b6b975e23ac1bdbbe07d81aee 100644 (file)
@@ -5,6 +5,12 @@ import { CONFIG } from '../initializers/config'
 import { VIDEO_TRANSCODING_FPS } from '../initializers/constants'
 import { logger } from './logger'
 
+/**
+ *
+ * Helpers to run ffprobe and extract data from the JSON output
+ *
+ */
+
 function ffprobePromise (path: string) {
   return new Promise<ffmpeg.FfprobeData>((res, rej) => {
     ffmpeg.ffprobe(path, (err, data) => {
@@ -158,7 +164,7 @@ async function getVideoFileBitrate (path: string, existingProbe?: ffmpeg.Ffprobe
 async function getDurationFromVideoFile (path: string, existingProbe?: ffmpeg.FfprobeData) {
   const metadata = await getMetadataFromFile(path, existingProbe)
 
-  return Math.floor(metadata.format.duration)
+  return Math.round(metadata.format.duration)
 }
 
 async function getVideoStreamFromFile (path: string, existingProbe?: ffmpeg.FfprobeData) {
@@ -182,6 +188,7 @@ function computeResolutionsToTranscode (videoFileResolution: number, type: 'vod'
     VideoResolution.H_720P,
     VideoResolution.H_240P,
     VideoResolution.H_1080P,
+    VideoResolution.H_1440P,
     VideoResolution.H_4K
   ]
 
@@ -195,6 +202,8 @@ function computeResolutionsToTranscode (videoFileResolution: number, type: 'vod'
 }
 
 async function canDoQuickTranscode (path: string): Promise<boolean> {
+  if (CONFIG.TRANSCODING.PROFILE !== 'default') return false
+
   const probe = await ffprobePromise(path)
 
   return await canDoQuickVideoTranscode(path, probe) &&
@@ -241,6 +250,26 @@ function getClosestFramerateStandard (fps: number, type: 'HD_STANDARD' | 'STANDA
                                     .sort((a, b) => fps % a - fps % b)[0]
 }
 
+function computeFPS (fpsArg: number, resolution: VideoResolution) {
+  let fps = fpsArg
+
+  if (
+    // On small/medium resolutions, limit FPS
+    resolution !== undefined &&
+    resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
+    fps > VIDEO_TRANSCODING_FPS.AVERAGE
+  ) {
+    // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value
+    fps = getClosestFramerateStandard(fps, 'STANDARD')
+  }
+
+  // Hard FPS limits
+  if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD')
+  else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN
+
+  return fps
+}
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -253,6 +282,7 @@ export {
   getVideoStreamFromFile,
   getDurationFromVideoFile,
   getAudioStream,
+  computeFPS,
   getVideoFileFPS,
   ffprobePromise,
   getClosestFramerateStandard,