1
0
mirror of https://github.com/danog/plotframes.git synced 2024-12-02 09:17:52 +01:00
plotframes/cli.js

143 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-10-12 00:53:13 +02:00
#!/usr/bin/env node
/*! plotframes | (c) 2015 RodrigoPolo.com | MIT License | https://github.com/rodrigopolo/plotframes/blob/master/LICENSE */
2015-10-17 17:34:41 +02:00
var
plotframes = require('./main'),
2015-10-13 15:55:56 +02:00
dashdash = require('dashdash'),
log = require('single-line-log').stderr,
isWin = /^win/.test(process.platform),
defterminal = isWin ? 'windows' : 'x11';
2015-10-12 00:53:13 +02:00
2015-10-17 17:34:41 +02:00
// Single line output
var clnl = false;
function cutelog(str, nl){
if(nl){
log(str);
}else{
log.clear();
log(str);
}
}
// Display help on console
function showHelp(code){
var help = parser.help({includeEnv: true}).trimRight();
console.error('Usage: plotframes [OPTIONS]\n'+ 'options:\n'+ help);
process.exit(code);
}
// Pad number
function pad(num, size) {
var str = num + "";
while (str.length < size) str = "0" + str;
return str;
}
// Seconds to time format
function toHHMMSS(n) {
var sep = ':',
n = parseFloat(n),
sss = parseInt((n % 1)*1000),
hh = parseInt(n / 3600);
n %= 3600;
var mm = parseInt(n / 60),
ss = parseInt(n % 60);
return pad(hh,2)+sep+pad(mm,2)+sep+pad(ss,2)+'.'+pad(sss,3);
}
// User options parser
var parser = dashdash.createParser({options: [
2015-10-12 00:53:13 +02:00
{
names: ['help', 'h'],
type: 'bool',
help: 'Print this help and exit.'
}, {
names: ['input', 'i'],
type: 'string',
help: 'Specify multimedia file to read. This is the file passed to the ffprobe command. If not specified it is the first argument passed to the script.',
helpArg: 'FILE',
takesArg: false
}, {
names: ['stream', 's'],
type: 'string',
2015-10-17 17:34:41 +02:00
help: 'Specify stream to plot, options are "all", 0, 1, 2... etc.',
2015-10-14 19:07:34 +02:00
helpArg: '0',
2015-10-17 17:34:41 +02:00
default: 'all'
2015-10-12 00:53:13 +02:00
}, {
names: ['output', 'o'],
type: 'string',
help: 'Set the name of the output used by gnuplot. If not specified no output is created. Must be used in conjunction with the terminal option.',
helpArg: 'FILE.png'
}, {
names: ['terminal', 't'],
type: 'string',
2015-10-13 15:55:56 +02:00
help: 'Set the name of the terminal used by gnuplot. By default it is "'+defterminal+'". Must be used in conjunction with the output option. Check the gnuplot manual for the valid values.',
2015-10-12 00:53:13 +02:00
helpArg: 'png',
2015-10-13 15:55:56 +02:00
default: defterminal
2015-10-14 19:07:34 +02:00
}, {
names: ['frames', 'f'],
type: 'bool',
help: 'Create a plot based on frame number instead of frame time.'
2015-10-17 17:34:41 +02:00
}, {
names: ['print', 'p'],
type: 'bool',
help: 'Print the gnuplot script into the stdout and doesn\'t run gnuplot, ideal if you want to edit/study the script.'
2015-10-12 00:53:13 +02:00
}
2015-10-17 17:34:41 +02:00
]});
2015-10-12 00:53:13 +02:00
try {
var opts = parser.parse(process.argv);
} catch (e) {
console.error('Error! Unknown option.');
showHelp(1);
}
if (opts.help) {
showHelp(0);
}
if (!opts.input) {
if(opts._args[0]){
opts.input = opts._args[0];
}else{
console.error('Error! No input defined.');
showHelp(1);
}
}
2015-10-17 17:34:41 +02:00
plotframes.plotScript(opts.input,
function(err, res) {
if (err) {
cutelog(err,false);
process.exit(1);
}else{
if(opts.print){
2015-10-17 23:38:41 +02:00
log.clear();
2015-10-17 17:34:41 +02:00
console.log(res);
2015-10-14 19:07:34 +02:00
}else{
2015-10-17 17:34:41 +02:00
cutelog('All tasks done!',false);
2015-10-14 19:07:34 +02:00
}
}
2015-10-17 17:34:41 +02:00
},{
progress: function(data){
cutelog('Analyzing '+toHHMMSS(data.time)+' / '+data.duration+' '+((data.time/data.length)*100).toFixed(2)+'%',true);
},
stdout: process.stdout,
stderr: process.stderr,
terminal: opts.terminal,
frames: opts.frames,
stream: opts.stream,
output: opts.output,
as_string: opts.print
2015-10-14 19:07:34 +02:00
2015-10-12 00:53:13 +02:00
}
2015-10-17 17:34:41 +02:00
);
2015-10-14 19:32:55 +02:00