]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/youtube-dl/youtube-dl-wrapper.ts
Reduce error logs
[github/Chocobozzz/PeerTube.git] / server / helpers / youtube-dl / youtube-dl-wrapper.ts
index 6442c1e855b2106a0e2685d7be8bdf81b0e52183..d585e9a95707ac80085131188e706bd752016a06 100644 (file)
@@ -16,7 +16,7 @@ export type YoutubeDLSubs = {
 }[]
 
 const processOptions = {
-  maxBuffer: 1024 * 1024 * 10 // 10MB
+  maxBuffer: 1024 * 1024 * 30 // 30MB
 }
 
 class YoutubeDLWrapper {
@@ -77,38 +77,32 @@ class YoutubeDLWrapper {
 
     const youtubeDL = await YoutubeDLCLI.safeGet()
 
-    let timer: NodeJS.Timeout
-    const timeoutPromise = new Promise<string>((_, rej) => {
-      timer = setTimeout(() => rej(new Error('YoutubeDL download timeout.')), timeout)
-    })
-
-    const downloadPromise = youtubeDL.download({
-      url: this.url,
-      format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions),
-      output: pathWithoutExtension,
-      processOptions
-    }).then(() => clearTimeout(timer))
-      .then(async () => {
-        // If youtube-dl did not guess an extension for our file, just use .mp4 as default
-        if (await pathExists(pathWithoutExtension)) {
-          await move(pathWithoutExtension, pathWithoutExtension + '.mp4')
-        }
-
-        return this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
+    try {
+      await youtubeDL.download({
+        url: this.url,
+        format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions),
+        output: pathWithoutExtension,
+        timeout,
+        processOptions
       })
 
-    return Promise.race([ downloadPromise, timeoutPromise ])
-      .catch(err => {
-        this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
-          .then(path => {
-            logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() })
+      // If youtube-dl did not guess an extension for our file, just use .mp4 as default
+      if (await pathExists(pathWithoutExtension)) {
+        await move(pathWithoutExtension, pathWithoutExtension + '.mp4')
+      }
 
-            return remove(path)
-          })
-          .catch(innerErr => logger.error('Cannot remove file in youtubeDL timeout.', { innerErr, ...lTags() }))
+      return this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
+    } catch (err) {
+      this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
+        .then(path => {
+          logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() })
 
-        throw err
-      })
+          return remove(path)
+        })
+        .catch(innerErr => logger.error('Cannot remove file in youtubeDL timeout.', { innerErr, ...lTags() }))
+
+      throw err
+    }
   }
 
   private async guessVideoPathWithExtension (tmpPath: string, sourceExt: string) {