forked from 75lb/handbrake-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (110 loc) · 3.81 KB
/
index.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
import Handbrake from './lib/Handbrake.js'
import util from 'util'
import cp from 'child_process'
import toSpawnArgs from 'object-to-spawn-args'
import { HandbrakeCLIPath } from './lib/config.js'
import cliOptions from './lib/cli-options.js'
/**
* Handbrake for node.js.
* @module handbrake-js
* @typicalname hbjs
* @example
* ```js
* const hbjs = require('handbrake-js')
* ```
*/
/**
* Spawns a HandbrakeCLI process with the supplied [options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options), returning an instance of `Handbrake` on which you can listen for events.
*
* @param {object} [options] - [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI
* @param {string} [options.HandbrakeCLIPath] - Override the built-in `HandbrakeCLI` binary path.
* @returns {module:handbrake-js~Handbrake}
* @alias module:handbrake-js.spawn
* @example
* ```js
* const hbjs = require('handbrake-js')
*
* const options = {
* input: 'something.avi',
* output: 'something.mp4',
* preset: 'Normal',
* rotate: 1
* }
* hbjs.spawn(options)
* .on('error', console.error)
* .on('output', console.log)
* ```
*/
function spawn (options = {}, mocks) {
const handbrake = new Handbrake(options, mocks)
/* defer so the caller can attach event listers on the returned Handbrake instance first */
process.nextTick(function () {
try {
handbrake._run()
} catch (error) {
const err = new Error()
err.message = error.message
err.name = 'InvalidOption'
handbrake._emitError(err)
}
})
return handbrake
}
/**
* Runs HandbrakeCLI with the supplied [options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) calling the supplied callback on completion. The exec method is best suited for short duration tasks where you can wait until completion for the output.
*
* @param options {Object} - [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI
* @param {string} [options.HandbrakeCLIPath] - Override the built-in `HandbrakeCLI` binary path.
* @param [onComplete] {Function} - If passed, `onComplete(err, stdout, stderr)` will be called on completion, `stdout` and `stderr` being strings containing the HandbrakeCLI output.
*
* @example
* ```js
* const hbjs = require('handbrake-js')
*
* hbjs.exec({ preset-list: true }, function(err, stdout, stderr){
* if (err) throw err
* console.log(stdout)
* })
* ```
* @alias module:handbrake-js.exec
*/
function exec (options = {}, done) {
const cmd = util.format(
'"%s" %s',
options.HandbrakeCLIPath || HandbrakeCLIPath,
toSpawnArgs(options, { quote: true }).join(' ')
)
cp.exec(cmd, done)
}
/**
* Identical to `hbjs.exec` except it returns a promise, rather than invoke a callback. Use this when you don't need the progress events reported by `hbjs.spawn`. Fulfils with an object containing the output in two properties: `stdout` and `stderr`.
* @param options {Object} - [Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options) to pass directly to HandbrakeCLI
* @param {string} [options.HandbrakeCLIPath] - Override the built-in `HandbrakeCLI` binary path.
* @returns {Promise}
* @example
* ```js
* const hbjs = require('handbrake-js')
*
* async function start () {
* const result = await hbjs.run({ version: true })
* console.log(result.stdout)
* // prints 'HandBrake 1.3.0'
* }
*
* start().catch(console.error)
* ```
* @alias module:handbrake-js.run
*/
async function run (options) {
return new Promise((resolve, reject) => {
exec(options, function (err, stdout, stderr) {
if (err) {
reject(err)
} else {
resolve({ stdout, stderr })
}
})
})
}
export { cliOptions, spawn, exec, run }
export default { cliOptions, spawn, exec, run }