]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/youtube-dl/youtube-dl-cli.ts
Ensure bin dir exists before dl youtube-dl
[github/Chocobozzz/PeerTube.git] / server / helpers / youtube-dl / youtube-dl-cli.ts
index 559f92984ff58784002ad7cce80fcab779686cc6..728f096b5bd581acdb728390c9402b3964f01643 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()
     }
 
@@ -90,11 +92,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,15 +149,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 output = await execa('python', [ youtubeDLBinaryPath, ...completeArgs, url ], processOptions)
+    const { PYTHON_PATH } = CONFIG.IMPORT.VIDEOS.HTTP.YOUTUBE_DL_RELEASE
+    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() })