+ logger.info('Importing youtubeDL video %s to %s', url, pathWithoutExtension)
+
+ let options = [ '-f', getYoutubeDLVideoFormat(), '-o', pathWithoutExtension ]
+ options = wrapWithProxyOptions(options)
+
+ if (process.env.FFMPEG_PATH) {
+ options = options.concat([ '--ffmpeg-location', process.env.FFMPEG_PATH ])
+ }
+
+ logger.debug('YoutubeDL options for %s.', url, { options })
+
+ return new Promise<string>((res, rej) => {
+ safeGetYoutubeDL()
+ .then(youtubeDL => {
+ youtubeDL.exec(url, options, processOptions, async err => {
+ clearTimeout(timer)
+
+ try {
+ // 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')
+ }
+
+ const path = await guessVideoPathWithExtension(pathWithoutExtension, fileExt)
+
+ if (err) {
+ remove(path)
+ .catch(err => logger.error('Cannot delete path on YoutubeDL error.', { err }))
+
+ return rej(err)
+ }
+
+ return res(path)
+ } catch (err) {
+ return rej(err)
+ }
+ })
+
+ timer = setTimeout(() => {
+ const err = new Error('YoutubeDL download timeout.')
+
+ guessVideoPathWithExtension(pathWithoutExtension, fileExt)
+ .then(path => remove(path))
+ .finally(() => rej(err))
+ .catch(err => {
+ logger.error('Cannot remove file in youtubeDL timeout.', { err })
+ return rej(err)
+ })
+ }, timeout)
+ })
+ .catch(err => rej(err))