-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
install.js
93 lines (77 loc) · 2.36 KB
/
install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict'
var fs = require("fs");
var os = require("os");
var ProgressBar = require("progress");
var get = require("simple-get");
var ffmpegPath = require(".");
var pkg = require("./package");
const exitOnError = (err) => {
console.error(err)
process.exit(1)
}
const exitOnErrorOrWarnWith = (msg) => (err) => {
if (err.statusCode === 404) console.warn(msg)
else exitOnError(err)
}
if (!ffmpegPath) {
exitOnError('ffmpeg-static install failed: No binary found for architecture')
}
const noop = () => {}
function downloadFile(url, destinationPath, progressCallback = noop) {
let fulfill, reject;
let totalBytes = 0;
const promise = new Promise((x, y) => {
fulfill = x;
reject = y;
});
get(url, function(err, response) {
if (err || response.statusCode !== 200) {
err = err || new Error('Download failed.')
if (response) {
err.url = url
err.statusCode = response.statusCode
}
reject(err)
return;
}
const file = fs.createWriteStream(destinationPath);
file.on("finish", () => fulfill());
file.on("error", error => reject(error));
response.pipe(file);
totalBytes = parseInt(response.headers["content-length"], 10);
if (progressCallback) {
response.on("data", function(chunk) {
progressCallback(chunk.length, totalBytes);
});
}
});
return promise;
}
let progressBar = null;
function onProgress(deltaBytes, totalBytes) {
if (!progressBar) {
progressBar = new ProgressBar(`Downloading ffmpeg [:bar] :percent :etas `, {
complete: "|",
incomplete: " ",
width: 20,
total: totalBytes
});
}
progressBar.tick(deltaBytes);
}
const release = (
process.env.FFMPEG_BINARY_RELEASE ||
pkg['ffmpeg-static'].binary_release
)
const downloadUrl = `https://github.com/eugeneware/ffmpeg-static/releases/download/${release}/${os.platform()}-${os.arch()}`
const readmeUrl = `${downloadUrl}.README`
const licenseUrl = `${downloadUrl}.LICENSE`
downloadFile(downloadUrl, ffmpegPath, onProgress)
.then(() => {
fs.chmodSync(ffmpegPath, 0o755) // make executable
})
.catch(exitOnError)
.then(() => downloadFile(readmeUrl, `${ffmpegPath}.README`))
.catch(exitOnErrorOrWarnWith('Failed to download the ffmpeg README.'))
.then(() => downloadFile(licenseUrl, `${ffmpegPath}.LICENSE`))
.catch(exitOnErrorOrWarnWith('Failed to download the ffmpeg LICENSE.'))