-
Notifications
You must be signed in to change notification settings - Fork 69
/
cli.js
executable file
·50 lines (44 loc) · 1.5 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
#!/usr/bin/env node
import ansi from 'ansi-escape-sequences'
import commandLineArgs from 'command-line-args'
import * as hbjs from 'handbrake-js'
import cliOptions from '../lib/cli-options.js'
import util from 'util'
const handbrakeOptions = commandLineArgs(cliOptions)._all
function column (n, msg) {
process.stdout.write(ansi.cursor.horizontalAbsolute(n) + msg)
}
function onProgress (progress) {
column(1, progress.task + ' ')
column(11, progress.percentComplete.toFixed(2) + ' ')
column(22, progress.fps.toFixed(2) + ' ')
column(32, progress.avgFps.toFixed(2) + ' ')
column(42, progress.eta)
}
function halt (err) {
console.error(ansi.format(util.inspect(err), 'red'))
process.exitCode = 1
}
/* user intends to encode, so attach progress reporter (unless --verbose was passed) */
if (handbrakeOptions.input && handbrakeOptions.output) {
const handbrake = hbjs.spawn(handbrakeOptions)
.on('error', halt)
.on('complete', console.log.bind(console))
if (handbrakeOptions.verbose) {
handbrake.on('output', process.stdout.write.bind(process.stdout))
} else {
handbrake
.on('begin', function () {
console.log(ansi.format('Task % done FPS Avg FPS ETA', 'bold'))
this.began = true
})
.on('progress', onProgress)
.on('complete', function () {
if (!this.began) console.error(this.output)
})
}
} else {
hbjs.spawn(handbrakeOptions)
.on('error', halt)
.on('output', process.stdout.write.bind(process.stdout))
}