]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/ffmpeg-utils.ts
Fix broken migration introduced in 2.2.0-rc.1
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
index 084516e553fd1a0395f24ced3ae8340e3fdcd622..557fb5e3aaf036c0547355aef3c8cc9d1a79876d 100644 (file)
@@ -7,6 +7,7 @@ import { logger } from './logger'
 import { checkFFmpegEncoders } from '../initializers/checker-before-init'
 import { readFile, remove, writeFile } from 'fs-extra'
 import { CONFIG } from '../initializers/config'
+import { VideoFileMetadata } from '@shared/models/videos/video-file-metadata'
 
 /**
  * A toolbox to play with audio
@@ -124,7 +125,8 @@ async function getVideoStreamCodec (path: string) {
     baseProfile = baseProfileMatrix['High'] // Fallback
   }
 
-  const level = videoStream.level.toString(16)
+  let level = videoStream.level.toString(16)
+  if (level.length === 1) level = `0${level}`
 
   return `${videoCodec}.${baseProfile}${level}`
 }
@@ -169,24 +171,26 @@ async function getVideoFileFPS (path: string) {
   return 0
 }
 
-async function getVideoFileBitrate (path: string) {
-  return new Promise<number>((res, rej) => {
+async function getMetadataFromFile <T> (path: string, cb = metadata => metadata) {
+  return new Promise<T>((res, rej) => {
     ffmpeg.ffprobe(path, (err, metadata) => {
       if (err) return rej(err)
 
-      return res(metadata.format.bit_rate)
+      return res(cb(new VideoFileMetadata(metadata)))
     })
   })
 }
 
+async function getVideoFileBitrate (path: string) {
+  return getMetadataFromFile<number>(path, metadata => metadata.format.bit_rate)
+}
+
 function getDurationFromVideoFile (path: string) {
-  return new Promise<number>((res, rej) => {
-    ffmpeg.ffprobe(path, (err, metadata) => {
-      if (err) return rej(err)
+  return getMetadataFromFile<number>(path, metadata => Math.floor(metadata.format.duration))
+}
 
-      return res(Math.floor(metadata.format.duration))
-    })
-  })
+function getVideoStreamFromFile (path: string) {
+  return getMetadataFromFile<any>(path, metadata => metadata.streams.find(s => s.codec_type === 'video') || null)
 }
 
 async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
@@ -341,6 +345,7 @@ export {
   getAudioStreamCodec,
   getVideoStreamSize,
   getVideoFileResolution,
+  getMetadataFromFile,
   getDurationFromVideoFile,
   generateImageFromVideoFile,
   TranscodeOptions,
@@ -418,6 +423,7 @@ async function buildHLSCommand (command: ffmpeg.FfmpegCommand, options: HLSTrans
   const videoPath = getHLSVideoPath(options)
 
   if (options.copyCodecs) command = presetCopy(command)
+  else if (options.resolution === VideoResolution.H_NOVIDEO) command = presetOnlyAudio(command)
   else command = await buildx264Command(command, options)
 
   command = command.outputOption('-hls_time 4')
@@ -450,17 +456,6 @@ async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) {
   await writeFile(options.outputPath, newContent)
 }
 
-function getVideoStreamFromFile (path: string) {
-  return new Promise<any>((res, rej) => {
-    ffmpeg.ffprobe(path, (err, metadata) => {
-      if (err) return rej(err)
-
-      const videoStream = metadata.streams.find(s => s.codec_type === 'video')
-      return res(videoStream || null)
-    })
-  })
-}
-
 /**
  * A slightly customised version of the 'veryfast' x264 preset
  *