-
Notifications
You must be signed in to change notification settings - Fork 19
/
cli.js
executable file
·132 lines (112 loc) · 3.98 KB
/
cli.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env node
var _ = require('lodash');
var cliArgs = require('clarg')();
var ffbinaries = require('./ffbinaries-lib');
var packageJson = require('./package.json');
// gather info from CLI
var dispatchMode = _.get(cliArgs, 'args.0');
var dispatchComponents = _.get(cliArgs, 'args');
var dispatchOpts = {
output: _.get(cliArgs, 'opts.output') || _.get(cliArgs, 'opts.o'),
quiet: _.get(cliArgs, 'opts.quiet') || _.get(cliArgs, 'opts.q'),
version: _.get(cliArgs, 'opts.version') || _.get(cliArgs, 'opts.v'),
platform: (_.get(cliArgs, 'opts.platform') || _.get(cliArgs, 'opts.p'))
};
console.log('ffbinaries downloader');
console.log('------------------------------------');
function displayHelp() {
var lines = [
'',
'Downloads binaries for ffmpeg, ffprobe, ffplay and ffserver.',
'',
'Available platforms:',
' ' + ffbinaries.listPlatforms().join(', '),
'',
'To see what versions are available type:',
' ffbinaries versions',
'',
'To clear cache type:',
' ffbinaries clearcache',
'',
'If no version is specified the latest will be downloaded.',
'You can specify target directory, defaults to working directory.',
'',
'Examples:',
' ffbinaries',
' ffbinaries ffmpeg',
' ffbinaries ffplay --quiet --platform=win-64',
' ffbinaries ffmpeg ffprobe -q --v=3.2 --p=linux-64 --output=/usr/local/bin/ffmpeg'
];
console.log(lines.join('\n'));
}
function displayVersions() {
console.log('ffbinaries downloader version:', packageJson.version);
ffbinaries.listVersions(function displayVersionsResults(err, versions) {
if (versions && Array.isArray(versions)) {
console.log('Available ffmpeg versions:', versions.join(', '));
} else {
console.log('Couldn\'t retrieve list of versions from the server.');
}
});
}
function displayDownloadFilesResult(err, data) {
if (err) {
console.log('------------------------------------');
console.log('Download failed.');
console.log('------------------------------------');
console.log(err);
return process.exit(1);
}
console.log('Destination:', data[0].path);
console.log('Files downloaded:', _.map(data, 'filename').join(', '));
console.log('------------------------------------');
console.log('Binaries downloaded and extracted.');
console.log('------------------------------------');
}
function clearCache() {
ffbinaries.clearCache();
console.log('Cache cleared');
}
function download(components, opts) {
if (!opts.platform) {
opts.platform = ffbinaries.detectPlatform();
console.log('Platform not specified - downloading binaries for current platform.');
}
function fnTicker(data) {
console.log('\x1b[2m' + data.filename + ': Downloading ' + (data.progress * 100).toFixed(1) + '%\x1b[0m');
}
var dlOpts = {
destination: opts.output || process.cwd(),
version: opts.version || false,
platform: ffbinaries.resolvePlatform(opts.platform),
tickerFn: opts.quiet ? null : fnTicker,
tickerInterval: 2500
};
if (!dlOpts.platform) {
console.log('Specified platform "' + opts.platform + '" not supported. Type "ffbinaries help" to see the list of available binaries or run "ffbinaries" without -p switch.');
return process.exit(1);
}
if (!components.length) {
components = null;
}
console.log('Components:', Array.isArray(components) ? components.join(', ') : 'all');
console.log('Platform:', dlOpts.platform);
console.log('ffmpeg version:', dlOpts.version || '(latest)');
ffbinaries.downloadFiles(components, dlOpts, displayDownloadFilesResult);
}
/**
* Dispatches to the correct handler based on input from CLI
*/
function dispatch(mode, components, opts) {
if (mode === 'help' || mode === 'info') {
return displayHelp();
}
if (mode === 'clearcache') {
return clearCache();
}
if (mode === 'version' || mode === 'versions' || mode === 'list') {
return displayVersions();
}
return download(components, opts);
}
dispatch(dispatchMode, dispatchComponents, dispatchOpts);