Skip to content

Commit

Permalink
Passing a custom HandbrakeCLIPath now works correctly. Fixes #77
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Jul 20, 2024
1 parent 221120f commit 3620a33
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 8 deletions.
13 changes: 10 additions & 3 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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']
});
Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions examples/handbrakecli-path-run.js
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 20 additions & 0 deletions examples/handbrakecli-path-spawn.js
Original file line number Diff line number Diff line change
@@ -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))
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Handbrake.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
})
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
"test-runner": "^0.11.0"
},
"standard": {
"ignore": [
"tmp",
"dist"
],
"envs": [
"node"
]
Expand Down
4 changes: 2 additions & 2 deletions test/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
35 changes: 35 additions & 0 deletions test/handbrakecli-path.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3620a33

Please sign in to comment.