diff options
Diffstat (limited to 'server/helpers/youtube-dl/youtube-dl-wrapper.ts')
-rw-r--r-- | server/helpers/youtube-dl/youtube-dl-wrapper.ts | 50 |
1 files changed, 22 insertions, 28 deletions
diff --git a/server/helpers/youtube-dl/youtube-dl-wrapper.ts b/server/helpers/youtube-dl/youtube-dl-wrapper.ts index 6442c1e85..68c29cd86 100644 --- a/server/helpers/youtube-dl/youtube-dl-wrapper.ts +++ b/server/helpers/youtube-dl/youtube-dl-wrapper.ts | |||
@@ -77,38 +77,32 @@ class YoutubeDLWrapper { | |||
77 | 77 | ||
78 | const youtubeDL = await YoutubeDLCLI.safeGet() | 78 | const youtubeDL = await YoutubeDLCLI.safeGet() |
79 | 79 | ||
80 | let timer: NodeJS.Timeout | 80 | try { |
81 | const timeoutPromise = new Promise<string>((_, rej) => { | 81 | await youtubeDL.download({ |
82 | timer = setTimeout(() => rej(new Error('YoutubeDL download timeout.')), timeout) | 82 | url: this.url, |
83 | }) | 83 | format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions), |
84 | 84 | output: pathWithoutExtension, | |
85 | const downloadPromise = youtubeDL.download({ | 85 | timeout, |
86 | url: this.url, | 86 | processOptions |
87 | format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions), | ||
88 | output: pathWithoutExtension, | ||
89 | processOptions | ||
90 | }).then(() => clearTimeout(timer)) | ||
91 | .then(async () => { | ||
92 | // If youtube-dl did not guess an extension for our file, just use .mp4 as default | ||
93 | if (await pathExists(pathWithoutExtension)) { | ||
94 | await move(pathWithoutExtension, pathWithoutExtension + '.mp4') | ||
95 | } | ||
96 | |||
97 | return this.guessVideoPathWithExtension(pathWithoutExtension, fileExt) | ||
98 | }) | 87 | }) |
99 | 88 | ||
100 | return Promise.race([ downloadPromise, timeoutPromise ]) | 89 | // If youtube-dl did not guess an extension for our file, just use .mp4 as default |
101 | .catch(err => { | 90 | if (await pathExists(pathWithoutExtension)) { |
102 | this.guessVideoPathWithExtension(pathWithoutExtension, fileExt) | 91 | await move(pathWithoutExtension, pathWithoutExtension + '.mp4') |
103 | .then(path => { | 92 | } |
104 | logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() }) | ||
105 | 93 | ||
106 | return remove(path) | 94 | return this.guessVideoPathWithExtension(pathWithoutExtension, fileExt) |
107 | }) | 95 | } catch (err) { |
108 | .catch(innerErr => logger.error('Cannot remove file in youtubeDL timeout.', { innerErr, ...lTags() })) | 96 | this.guessVideoPathWithExtension(pathWithoutExtension, fileExt) |
97 | .then(path => { | ||
98 | logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() }) | ||
109 | 99 | ||
110 | throw err | 100 | return remove(path) |
111 | }) | 101 | }) |
102 | .catch(innerErr => logger.error('Cannot remove file in youtubeDL timeout.', { innerErr, ...lTags() })) | ||
103 | |||
104 | throw err | ||
105 | } | ||
112 | } | 106 | } |
113 | 107 | ||
114 | private async guessVideoPathWithExtension (tmpPath: string, sourceExt: string) { | 108 | private async guessVideoPathWithExtension (tmpPath: string, sourceExt: string) { |