forked from wulkano/Kap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
download-ffmpeg.js
69 lines (59 loc) · 2.35 KB
/
download-ffmpeg.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const execa = require('execa');
const got = require('got');
const ora = require('ora');
let spinner = ora({text: 'Installing 7zip', stream: process.stdout}).start();
const FFMPEG_URL = 'http://evermeet.cx/ffmpeg/ffmpeg-81632-g09317e3.7z';
const VENDOR_PATH = ['..', 'app', 'vendor'];
const joinPath = (...str) => path.join(__dirname, ...str);
const which = cmd => execa.sync(joinPath('which.sh'), [cmd]).stdout;
const cmdExists = cmd => which(cmd) !== '';
const logErrorAndExit = msg => {
spinner.fail();
console.error(chalk.red(msg));
process.exit(1);
};
if (process.platform === 'darwin') {
if (!cmdExists('brew')) {
let msg = `${chalk.bold('Kap')} needs ${chalk.bold('brew')} in order to `;
msg += `automagically download ${chalk.bold('ffmpeg')}.`;
// TODO add a link to a README.md section that explains what's going on here
logErrorAndExit(msg);
}
execa(joinPath('brew-install-7zip.sh'))
.then(() => {
spinner.succeed();
spinner = ora({text: 'Downloading ffmpeg (0%)', stream: process.stdout}).start();
fs.mkdir(joinPath(...VENDOR_PATH), err => {
if (err && err.code !== 'EEXIST') {
logErrorAndExit(err);
}
const writeStream = fs.createWriteStream(joinPath(...VENDOR_PATH, 'ffmpeg.7z'));
writeStream.on('error', err => logErrorAndExit(err));
writeStream.on('close', () => {
spinner.succeed();
spinner = ora({text: 'Bundling ffmpeg', stream: process.stdout}).start();
execa(joinPath('unzip-ffmpeg.sh'), [joinPath(...VENDOR_PATH)])
.then(() => spinner.succeed())
.catch(err => logErrorAndExit(err));
});
const ffmpegDownloader = got.stream(FFMPEG_URL);
let totalSize;
let downloadedSize = 0;
ffmpegDownloader.on('response', res => {
totalSize = parseInt(res.headers['content-length'], 10);
});
ffmpegDownloader.on('data', chunk => {
downloadedSize += chunk.length;
spinner.text = `Downloading ffmpeg (${(100.0 * downloadedSize / totalSize).toFixed(2)}%)`;
});
ffmpegDownloader.pipe(writeStream);
});
})
.catch(logErrorAndExit);
} else {
logErrorAndExit(`Currently, ${chalk.bold('Kap')} only runs on macOS`);
}