diff --git a/README.md b/README.md index 9cc0538..0e054b7 100644 --- a/README.md +++ b/README.md @@ -399,6 +399,9 @@ Youtube-dl looks for certain environment variables to aid its operations. If You - **YOUTUBE\_DL\_DOWNLOAD\_HOST** - overwrite URL prefix that is used to download the binary file of Youtube-dl. Note: this includes protocol and might even include path prefix. Defaults to `https://yt-dl.org/downloads/latest/youtube-dl`. +- **YOUTUBE_DL_DIRECT_BINARY_DOWNLOAD_URL** - download the youtube-dl binary from the given download url, completely ignoring the `**YOUTUBE\_DL\_DOWNLOAD\_HOST**` option, while performing no lookup for retrieving the latest version of the binary. This is useful if you are hosting the youtube-dl binary on your own S3 bucket. The platform specific extension will be appended. +E.g. the value `https://xxxxx.s3.xxxxxx.amazonaws.com/2020.11.12-youtube-dl` will result in downloading `https://xxxxx.s3.xxxxxx.amazonaws.com/2020.11.12-youtube-dl.exe` on windows. + ### Tests Tests are written with [vows](http://vowsjs.org/) diff --git a/lib/downloader.js b/lib/downloader.js index 724a304..72dcf62 100644 --- a/lib/downloader.js +++ b/lib/downloader.js @@ -6,6 +6,9 @@ const path = require('path') const util = require('util') const fs = require('fs') +const DIRECT_BINARY_DOWNLOAD_URL = + process.env.YOUTUBE_DL_DIRECT_BINARY_DOWNLOAD_URL + const ENDPOINT = process.env.YOUTUBE_DL_DOWNLOAD_HOST || 'https://youtube-dl-binary.vercel.app/' @@ -72,7 +75,7 @@ function downloader (binDir, callback) { createBase(binDir) - // handle overwritin + // handle overwriting if (fs.existsSync(filePath)) { if (!isOverwrite) { return callback('File exists') @@ -85,6 +88,30 @@ function downloader (binDir, callback) { } } + if (DIRECT_BINARY_DOWNLOAD_URL !== undefined) { + const binaryUrl = DIRECT_BINARY_DOWNLOAD_URL + isWin ? '.exe' : '' + + const downloadFile = request.get(binaryUrl) + const outputStream = fs.createWriteStream(filePath, { mode: 493 }) + outputStream.on('close', function end () { + callback(status, version) + fs.writeFileSync( + defaultPath, + JSON.stringify({ + version: 'custom-mirror-version', + path: binDir ? filePath : binDir, + exec: exec('youtube-dl') + }), + 'utf8' + ) + }) + downloadFile.pipe(outputStream) + downloadFile.on('error', function error (err) { + callback(err) + }) + return + } + download( `${ENDPOINT}?platform=${isWin ? 'windows' : 'linux'}`, function error (err, newVersion) {