diff --git a/dist/index.cjs b/dist/index.cjs index 2b38c3d..1533cba 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -218,7 +218,10 @@ class Handbrake extends events.EventEmitter { } } - const spawnArgs = toSpawnArgs$1(this.options, { + const optionsCopy = Object.assign({}, this.options); + /* All options except HandbrakeCLIPath should be passed into the Handbrake command */ + delete optionsCopy.HandbrakeCLIPath; + const spawnArgs = toSpawnArgs$1(optionsCopy, { optionEqualsValue: true, optionEqualsValueExclusions: ['preset-import-file', 'preset-import-gui', 'subtitle-burned'] }); @@ -628,10 +631,14 @@ function spawn (options = {}, mocks) { * @alias module:handbrake-js.exec */ function exec (options = {}, done) { + const handbrakePath = options.HandbrakeCLIPath || HandbrakeCLIPath; + const optionsCopy = Object.assign({}, options); + /* All options except HandbrakeCLIPath should be passed into the Handbrake command */ + delete optionsCopy.HandbrakeCLIPath; const cmd = util.format( '"%s" %s', - options.HandbrakeCLIPath || HandbrakeCLIPath, - toSpawnArgs$1(options, { quote: true }).join(' ') + handbrakePath, + toSpawnArgs$1(optionsCopy, { quote: true }).join(' ') ); childProcess.exec(cmd, done); } diff --git a/examples/handbrakecli-path-run.js b/examples/handbrakecli-path-run.js new file mode 100644 index 0000000..b145d28 --- /dev/null +++ b/examples/handbrakecli-path-run.js @@ -0,0 +1,18 @@ +import hbjs from 'handbrake-js' +import path from 'path' +import currentModulePaths from 'current-module-paths' +const { __dirname } = currentModulePaths(import.meta.url) + +async function start () { + const options = { + input: path.resolve(__dirname, '../test/video/demo.mkv'), + output: './tmp/output.mp4', + preset: 'Very Fast 480p30', + HandbrakeCLIPath: './tmp/HandbrakeCLI' + } + + const result = await hbjs.run(options) + console.log(result) +} + +start().catch(console.error) diff --git a/examples/handbrakecli-path-spawn.js b/examples/handbrakecli-path-spawn.js new file mode 100644 index 0000000..fe03ec1 --- /dev/null +++ b/examples/handbrakecli-path-spawn.js @@ -0,0 +1,20 @@ +import hbjs from 'handbrake-js' +import path from 'path' +import currentModulePaths from 'current-module-paths' +const { __dirname } = currentModulePaths(import.meta.url) + +const options = { + input: path.resolve(__dirname, '../test/video/demo.mkv'), + output: 'output.mp4', + preset: 'Very Fast 480p30', + HandbrakeCLIPath: './tmp/HandbrakeCLI' +} + +/* +Transcodes the input .mkv to an .mp4 using the 'Normal' preset. +Using spawn enables you to track progress while encoding, +more appropriate for long-running tasks. +*/ +hbjs.spawn(options) + .on('error', console.error) + .on('output', process.stdout.write.bind(process.stdout)) diff --git a/index.js b/index.js index 0ef27d7..2237de4 100644 --- a/index.js +++ b/index.js @@ -74,10 +74,14 @@ function spawn (options = {}, mocks) { * @alias module:handbrake-js.exec */ function exec (options = {}, done) { + const handbrakePath = options.HandbrakeCLIPath || HandbrakeCLIPath + const optionsCopy = Object.assign({}, options) + /* All options except HandbrakeCLIPath should be passed into the Handbrake command */ + delete optionsCopy.HandbrakeCLIPath const cmd = util.format( '"%s" %s', - options.HandbrakeCLIPath || HandbrakeCLIPath, - toSpawnArgs(options, { quote: true }).join(' ') + handbrakePath, + toSpawnArgs(optionsCopy, { quote: true }).join(' ') ) cp.exec(cmd, done) } diff --git a/lib/Handbrake.js b/lib/Handbrake.js index 5b13fcc..3fd0396 100644 --- a/lib/Handbrake.js +++ b/lib/Handbrake.js @@ -72,7 +72,10 @@ class Handbrake extends EventEmitter { } } - const spawnArgs = toSpawnArgs(this.options, { + const optionsCopy = Object.assign({}, this.options) + /* All options except HandbrakeCLIPath should be passed into the Handbrake command */ + delete optionsCopy.HandbrakeCLIPath + const spawnArgs = toSpawnArgs(optionsCopy, { optionEqualsValue: true, optionEqualsValueExclusions: ['preset-import-file', 'preset-import-gui', 'subtitle-burned'] }) diff --git a/package.json b/package.json index 6076bf7..ca0a51c 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,10 @@ "test-runner": "^0.11.0" }, "standard": { + "ignore": [ + "tmp", + "dist" + ], "envs": [ "node" ] diff --git a/test/exec.js b/test/exec.js index 3735f92..34aa277 100644 --- a/test/exec.js +++ b/test/exec.js @@ -17,11 +17,11 @@ tom.test('--preset-list', async function () { }) }) -tom.test('HandbrakeCLIPath', async function () { +tom.test('An incorrect HandbrakeCLIPath should fail but not be passed to the exec cmd', async function () { return new Promise((resolve, reject) => { hbjs.exec({ 'preset-list': true, HandbrakeCLIPath: 'one' }, function (err, stdout, stderr) { if (err) { - a.equal(err.cmd, '"one" --preset-list --HandbrakeCLIPath "one"') + a.equal(err.cmd, '"one" --preset-list') resolve() } else { reject(new Error("Shouldn't reach here")) diff --git a/test/handbrakecli-path.js b/test/handbrakecli-path.js new file mode 100644 index 0000000..98b8b3f --- /dev/null +++ b/test/handbrakecli-path.js @@ -0,0 +1,35 @@ +import TestRunner from 'test-runner' +import hbjs from 'handbrake-js' +import { strict as a } from 'assert' +import path from 'path' +import fs from 'fs' + +const tom = new TestRunner.Tom() + +if (process.platform === 'darwin') { + tom.before('Copy HandbrakeCLI to a different location', async function() { + try { + fs.mkdirSync('./tmp') + } catch (err) { + if (err.code !== 'EEXIST') { + throw err + } + } + fs.cpSync('./bin/HandbrakeCLI', './tmp/HandbrakeCLI') + }) + + tom.test('hbjs:run() - Use custom HandbrakeCLI path', async function () { + const options = { + input: './test/video/demo.mkv', + output: './tmp/output.mp4', + preset: 'Very Fast 480p30', + HandbrakeCLIPath: './tmp/HandbrakeCLI' + } + + const result = await hbjs.run(options) + a.ok(/Encode done!/.test(result.stderr)) + }) +} + + +export default tom