]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/youtube-dl/youtube-dl-wrapper.ts
Channel sync (#5135)
[github/Chocobozzz/PeerTube.git] / server / helpers / youtube-dl / youtube-dl-wrapper.ts
index 6960fbae4ec4dbf01b2a7908b54a29ce759f499c..7cd5e33107d5a9501a2610b6a1e55ce1c2e26ff0 100644 (file)
@@ -16,12 +16,16 @@ export type YoutubeDLSubs = {
 }[]
 
 const processOptions = {
-  maxBuffer: 1024 * 1024 * 10 // 10MB
+  maxBuffer: 1024 * 1024 * 30 // 30MB
 }
 
 class YoutubeDLWrapper {
 
-  constructor (private readonly url: string = '', private readonly enabledResolutions: number[] = []) {
+  constructor (
+    private readonly url: string,
+    private readonly enabledResolutions: number[],
+    private readonly useBestFormat: boolean
+  ) {
 
   }
 
@@ -30,7 +34,7 @@ class YoutubeDLWrapper {
 
     const info = await youtubeDL.getInfo({
       url: this.url,
-      format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions),
+      format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions, this.useBestFormat),
       additionalYoutubeDLArgs: youtubeDLArgs,
       processOptions
     })
@@ -42,6 +46,24 @@ class YoutubeDLWrapper {
     return infoBuilder.getInfo()
   }
 
+  async getInfoForListImport (options: {
+    latestVideosCount?: number
+  }) {
+    const youtubeDL = await YoutubeDLCLI.safeGet()
+
+    const list = await youtubeDL.getListInfo({
+      url: this.url,
+      latestVideosCount: options.latestVideosCount,
+      processOptions
+    })
+
+    return list.map(info => {
+      const infoBuilder = new YoutubeDLInfoBuilder(info)
+
+      return infoBuilder.getInfo()
+    })
+  }
+
   async getSubtitles (): Promise<YoutubeDLSubs> {
     const cwd = CONFIG.STORAGE.TMP_DIR
 
@@ -73,40 +95,36 @@ class YoutubeDLWrapper {
     // Leave empty the extension, youtube-dl will add it
     const pathWithoutExtension = generateVideoImportTmpPath(this.url, '')
 
-    let timer: NodeJS.Timeout
-
     logger.info('Importing youtubeDL video %s to %s', this.url, pathWithoutExtension, lTags())
 
     const youtubeDL = await YoutubeDLCLI.safeGet()
 
-    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, this.useBestFormat),
+        output: pathWithoutExtension,
+        timeout,
+        processOptions
       })
 
-    return Promise.race([ downloadPromise, timeoutPromise ])
-      .catch(async err => {
-        const path = await this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
+      // 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')
+      }
 
-        remove(path)
-          .catch(err => logger.error('Cannot remove file in youtubeDL timeout.', { err, ...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 error.', { innerErr, ...lTags() }))
+
+      throw err
+    }
   }
 
   private async guessVideoPathWithExtension (tmpPath: string, sourceExt: string) {