Handbrake

Video encoding / transcoding / converting for node.js

README

Upgraders, please read the release notes.

handbrake-js


Handbrake-js is Handbrake (v1.6.1) for node.js. It aspires to provide a lean and stable foundation for building video transcoding software in node.js.

HandBrake is a tool for converting video from nearly any format to a selection of modern, widely supported codecs. It can process most common multimedia files and any DVD or BluRay sources that do not contain any copy protection.

Outputs:

File Containers: .MP4(.M4V) and .MKV
Video Encoders: H.264(x264), H.265(x265) MPEG-4 and MPEG-2 (libav), VP8 (libvpx) and Theora(libtheora)
Audio Encoders: AAC, CoreAudio AAC/HE-AAC (OS X Only), MP3, Flac, AC3, or Vorbis
Audio Pass-thru: AC-3, DTS, DTS-HD, AAC and MP3 tracks


Compatible Platforms

Tested on Mac OSX, Ubuntu 14, Windows XP, Windows 7 and Windows 8.1.

Ubuntu 14.04 notice: Transcoding to MP4 fails on Ubuntu since 14.04 for this reason.

Installation

System Requirements

Just node.js. On Mac and Windows, every else is installed automatically. However on Linux, you must install HandbrakeCLI manually with these commands:

  1. ```
  2. sudo add-apt-repository --yes ppa:stebbins/handbrake-releases
  3. sudo apt-get update -qq
  4. sudo apt-get install -qq handbrake-cli
  5. ```

As a library

Move into your project directory then run:
  1. ```sh
  2. $ npm install handbrake-js --save
  3. ```
Mac / Linux users may need to run with sudo.

Now you can begin encoding from your app.

  1. ```js
  2. const hbjs = require('handbrake-js')

  3. hbjs.spawn({ input: 'something.avi', output: 'something.m4v' })
  4.   .on('error', err => {
  5.     // invalid user input, no video found etc
  6.   })
  7.   .on('progress', progress => {
  8.     console.log(
  9.       'Percent complete: %s, ETA: %s',
  10.       progress.percentComplete,
  11.       progress.eta
  12.     )
  13.   })
  14. ```

As a command-line app

From any directory run the following:
  1. ```sh
  2. $ npm install -g handbrake-js
  3. ```
Mac / Linux users may need to run with sudo.

Now, you can call handbrake as you would HandbrakeCLI, using all the usual options. By default, just statistics are output, passing--verbose prints the raw HandbrakeCLI output. This command will transcode an AVI to the more universal H.264 (mp4):
  1. ```
  2. $ handbrake --input 'some episode.avi' --output 'some episode.mp4' --preset Normal
  3. Task      % done     FPS       Avg FPS   ETA
  4. Encoding  1.07       131.76    158.12    00h21m11s
  5. ```

API Reference

Handbrake for node.js.

Example  
  1. ```js
  2. const hbjs = require('handbrake-js')
  3. ```

    _static_
* [.spawn([options])](#module_handbrake-js.spawn) ⇒ [Handbrake](#module_handbrake-js..Handbrake)
        [.exec(options, [onComplete])](#module_handbrake-js.exec)
* [.run(options)](#module_handbrake-js.run) ⇒ Promise
    _inner_
* [~Handbrake](#module_handbrake-js..Handbrake) ⇐ [EventEmitter](http://nodejs.org/api/events.html) * [.output](#module_handbrake-js..Handbrake+output) : string * [.options](#module_handbrake-js..Handbrake+options) : object
            .eError
            .cancel()
            "start"
            "begin"
            "progress" (progress)
            "output" (output)
            "error" (error)
            "end"
            "complete"
            "cancelled"


### hbjs.spawn([options]) ⇒ [Handbrake](#module_handbrake-js..Handbrake)
Spawns a HandbrakeCLI process with the supplied options, returning an instance ofHandbrake on which you can listen for events.

**Kind**: static method of [handbrake-js](#module_handbrake-js)

ParamTypeDescription
---------
[options]object[Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options)
[options.HandbrakeCLIPath]stringOverride

Example  
  1. ```js
  2. const hbjs = require('handbrake-js')

  3. const options = {
  4.   input: 'something.avi',
  5.   output: 'something.mp4',
  6.   preset: 'Normal',
  7.   rotate: 1
  8. }
  9. hbjs.spawn(options)
  10.   .on('error', console.error)
  11.   .on('output', console.log)
  12. ```

hbjs.exec(options, [onComplete])

Runs HandbrakeCLI with the supplied 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.

**Kind**: static method of [handbrake-js](#module_handbrake-js)

ParamTypeDescription
---------
optionsObject[Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options)
[options.HandbrakeCLIPath]stringOverride
[onComplete]functionIf

Example  
  1. ```js
  2. const hbjs = require('handbrake-js')

  3. hbjs.exec({ preset-list: true }, function(err, stdout, stderr){
  4.   if (err) throw err
  5.   console.log(stdout)
  6. })
  7. ```

### hbjs.run(options) ⇒ Promise
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.

**Kind**: static method of [handbrake-js](#module_handbrake-js)

ParamTypeDescription
---------
optionsObject[Options](https://handbrake.fr/docs/en/latest/cli/cli-guide.html#options)
[options.HandbrakeCLIPath]stringOverride

Example  
  1. ```js
  2. const hbjs = require('handbrake-js')

  3. async function start () {
  4.   const result = await hbjs.run({ version: true })
  5.   console.log(result.stdout)
  6.   // prints 'HandBrake 1.3.0'
  7. }

  8. start().catch(console.error)
  9. ```

### handbrake-js~Handbrake ⇐ [EventEmitter](http://nodejs.org/api/events.html)
A handle on the HandbrakeCLI process. Emits events you can monitor to track progress. An instance of this class is returned by spawn.

**Kind**: inner class of [handbrake-js](#module_handbrake-js) **Extends**: [EventEmitter](http://nodejs.org/api/events.html) **Emits**: [start](#module_handbrake-js..Handbrake+event_start), [begin](#module_handbrake-js..Handbrake+event_begin), [progress](#module_handbrake-js..Handbrake+event_progress), [output](#module_handbrake-js..Handbrake+event_output), [error](#module_handbrake-js..Handbrake+event_error), [end](#module_handbrake-js..Handbrake+event_end), [complete](#module_handbrake-js..Handbrake+event_complete), [cancelled](#module_handbrake-js..Handbrake+event_cancelled)

* [~Handbrake](#module_handbrake-js..Handbrake) ⇐ [EventEmitter](http://nodejs.org/api/events.html) * [.output](#module_handbrake-js..Handbrake+output) : string * [.options](#module_handbrake-js..Handbrake+options) : object
    .eError
    .cancel()
    "start"
    "begin"
    "end"
    "complete"
    "cancelled"


#### handbrake.output : string
A string containing all handbrakeCLI output

**Kind**: instance property of [Handbrake](#module_handbrake-js..Handbrake)

#### handbrake.options : object
a copy of the options passed to spawn

**Kind**: instance property of [Handbrake](#module_handbrake-js..Handbrake)

handbrake.eError

All operational errors are emitted via the error event.

**Kind**: instance enum of [Handbrake](#module_handbrake-js..Handbrake)
Properties

NameDefaultDescription
---------
VALIDATIONValidationErrorThrown
INVALID_INPUTInvalidInputThrown
INVALID_PRESETInvalidPresetThrown
OTHEROtherThrown
NOT_FOUNDHandbrakeCLINotFoundThrown


handbrake.cancel()

Cancel the encode, kill the underlying HandbrakeCLI process then emit a cancelled event.

**Kind**: instance method of [Handbrake](#module_handbrake-js..Handbrake)

"start"

Fired as HandbrakeCLI is launched. Nothing has happened yet.

**Kind**: event emitted by [Handbrake](#module_handbrake-js..Handbrake)

"begin"

Fired when encoding begins. If you're expecting an encode and this never fired, something went wrong.

**Kind**: event emitted by [Handbrake](#module_handbrake-js..Handbrake)

"progress" (progress)

Fired at regular intervals passing a progress object.

**Kind**: event emitted by [Handbrake](#module_handbrake-js..Handbrake)

ParamTypeDescription
---------
progressobjectdetails
progress.taskNumbernumbercurrent
progress.taskCountnumbertotal
progress.percentCompletenumberpercent
progress.fpsnumberFrames
progress.avgFpsnumberAverage
progress.etastringEstimated
progress.taskstringTask


"output" (output)

**Kind**: event emitted by [Handbrake](#module_handbrake-js..Handbrake)

ParamTypeDescription
---------
outputstringAn


"error" (error)

**Kind**: event emitted by [Handbrake](#module_handbrake-js..Handbrake)

ParamTypeDescription
---------
errorErrorAll
error.name[eError](#module_handbrake-js..Handbrake+eError)The
error.messagestringError
error.errnostringThe


"end"

Fired on successful completion of an encoding task. Always follows a begin event, with some progress in between.

**Kind**: event emitted by [Handbrake](#module_handbrake-js..Handbrake)

"complete"

Fired when HandbrakeCLI exited cleanly. This does not necessarily mean your encode completed as planned..

**Kind**: event emitted by [Handbrake](#module_handbrake-js..Handbrake)

"cancelled"

If .cancel() was called, this event is emitted once the underlying HandbrakeCLI process has closed.

**Kind**: event emitted by [Handbrake](#module_handbrake-js..Handbrake)


© 2013-23 Lloyd Brookes <75pound@gmail.com>.

Tested by test-runner. Documented by jsdoc-to-markdown.

Handbrake (Authors) is licensed by GNU General Public License Version 2.