]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/youtube-dl/youtube-dl-cli.ts
Fix print transcode command test
[github/Chocobozzz/PeerTube.git] / server / helpers / youtube-dl / youtube-dl-cli.ts
index 293acff436d48b40255e602d40500d2a71c82321..13c990a1e186b35c01a7c02054433f9dfe465093 100644 (file)
@@ -1,6 +1,6 @@
 import execa from 'execa'
-import { pathExists, writeFile } from 'fs-extra'
-import { join } from 'path'
+import { ensureDir, pathExists, writeFile } from 'fs-extra'
+import { dirname, join } from 'path'
 import { CONFIG } from '@server/initializers/config'
 import { VideoResolution } from '@shared/models'
 import { logger, loggerTagsFactory } from '../logger'
@@ -15,6 +15,8 @@ export class YoutubeDLCLI {
 
   static async safeGet () {
     if (!await pathExists(youtubeDLBinaryPath)) {
+      await ensureDir(dirname(youtubeDLBinaryPath))
+
       await this.updateYoutubeDLBinary()
     }
 
@@ -55,7 +57,7 @@ export class YoutubeDLCLI {
     }
   }
 
-  static getYoutubeDLVideoFormat (enabledResolutions: VideoResolution[]) {
+  static getYoutubeDLVideoFormat (enabledResolutions: VideoResolution[], useBestFormat: boolean) {
     /**
      * list of format selectors in order or preference
      * see https://github.com/ytdl-org/youtube-dl#format-selection
@@ -67,18 +69,26 @@ export class YoutubeDLCLI {
      *
      * in any case we avoid AV1, see https://github.com/Chocobozzz/PeerTube/issues/3499
      **/
-    const resolution = enabledResolutions.length === 0
-      ? VideoResolution.H_720P
-      : Math.max(...enabledResolutions)
-
-    return [
-      `bestvideo[vcodec^=avc1][height=${resolution}]+bestaudio[ext=m4a]`, // case #1
-      `bestvideo[vcodec!*=av01][vcodec!*=vp9.2][height=${resolution}]+bestaudio`, // case #2
-      `bestvideo[vcodec^=avc1][height<=${resolution}]+bestaudio[ext=m4a]`, // case #3
-      `bestvideo[vcodec!*=av01][vcodec!*=vp9.2]+bestaudio`,
+
+    let result: string[] = []
+
+    if (!useBestFormat) {
+      const resolution = enabledResolutions.length === 0
+        ? VideoResolution.H_720P
+        : Math.max(...enabledResolutions)
+
+      result = [
+        `bestvideo[vcodec^=avc1][height=${resolution}]+bestaudio[ext=m4a]`, // case #1
+        `bestvideo[vcodec!*=av01][vcodec!*=vp9.2][height=${resolution}]+bestaudio`, // case #2
+        `bestvideo[vcodec^=avc1][height<=${resolution}]+bestaudio[ext=m4a]` // case #
+      ]
+    }
+
+    return result.concat([
+      'bestvideo[vcodec!*=av01][vcodec!*=vp9.2]+bestaudio',
       'best[vcodec!*=av01][vcodec!*=vp9.2]', // case fallback for known formats
       'best' // Ultimate fallback
-    ].join('/')
+    ]).join('/')
   }
 
   private constructor () {
@@ -90,11 +100,13 @@ export class YoutubeDLCLI {
     format: string
     output: string
     processOptions: execa.NodeOptions
+    timeout?: number
     additionalYoutubeDLArgs?: string[]
   }) {
     return this.run({
       url: options.url,
       processOptions: options.processOptions,
+      timeout: options.timeout,
       args: (options.additionalYoutubeDLArgs || []).concat([ '-f', options.format, '-o', options.output ])
     })
   }
@@ -145,16 +157,23 @@ export class YoutubeDLCLI {
   private async run (options: {
     url: string
     args: string[]
+    timeout?: number
     processOptions: execa.NodeOptions
   }) {
-    const { url, args, processOptions } = options
+    const { url, args, timeout, processOptions } = options
 
     let completeArgs = this.wrapWithProxyOptions(args)
     completeArgs = this.wrapWithIPOptions(completeArgs)
     completeArgs = this.wrapWithFFmpegOptions(completeArgs)
 
     const { PYTHON_PATH } = CONFIG.IMPORT.VIDEOS.HTTP.YOUTUBE_DL_RELEASE
-    const output = await execa(PYTHON_PATH, [ youtubeDLBinaryPath, ...completeArgs, url ], processOptions)
+    const subProcess = execa(PYTHON_PATH, [ youtubeDLBinaryPath, ...completeArgs, url ], processOptions)
+
+    if (timeout) {
+      setTimeout(() => subProcess.cancel(), timeout)
+    }
+
+    const output = await subProcess
 
     logger.debug('Runned youtube-dl command.', { command: output.command, ...lTags() })