Node.js Handbrake.js Tutorial to Build Video Converter on Command Line Using Javascript Full Project For Beginners

 

 

As a library

Move into your project directory then run:

$ npm install handbrake-js --save

Mac / Linux users may need to run with sudo.

Now you can begin encoding from your app.

const hbjs = require('handbrake-js')

hbjs.spawn({ input: 'something.avi', output: 'something.m4v' })
  .on('error', err => {
    // invalid user input, no video found etc
  })
  .on('progress', progress => {
    console.log(
      'Percent complete: %s, ETA: %s',
      progress.percentComplete,
      progress.eta
    )
  })

As a command-line app

From any directory run the following:

$ npm install -g handbrake-js

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):

$ handbrake --input 'some episode.avi' --output 'some episode.mp4' --preset Normal
Task      % done     FPS       Avg FPS   ETA
Encoding  1.07       131.76    158.12    00h21m11s

 

 

Example in Node.js and Javascript

 

 

index.js

 

 

const hbjs = require("handbrake-js");
const path = require('path')

const options = {
  input: path.resolve(__dirname, "file.wmv"),
  output: "output.mp4",
  preset: "Super HQ 480p30 Surround",
};

/*
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));

 

 

Here you can give the following presets which are shown below

 

 

Preset Type Video Audio Picture Quality Encoding Speed File Size
Very Fast 1080p30 MP4 H.264 AAC stereo Average Very fast Small
Very Fast 720p30 MP4 H.264 AAC stereo Average Very fast Small
Very Fast 576p25 MP4 H.264 AAC stereo Average Very fast Small
Very Fast 480p30 MP4 H.264 AAC stereo Average Very fast Small
Fast 1080p30 MP4 H.264 AAC stereo Standard Fast Average
Fast 720p30 MP4 H.264 AAC stereo Standard Fast Average
Fast 576p25 MP4 H.264 AAC stereo Standard Fast Average
Fast 480p30 MP4 H.264 AAC stereo Standard Fast Average
HQ 1080p30 Surround MP4 H.264 AAC stereo; Dolby Digital (AC-3) High Slow Large
HQ 720p30 Surround MP4 H.264 AAC stereo; Dolby Digital (AC-3) High Slow Large
HQ 576p25 Surround MP4 H.264 AAC stereo; Dolby Digital (AC-3) High Slow Large
HQ 480p30 Surround MP4 H.264 AAC stereo; Dolby Digital (AC-3) High Slow Large
Super HQ 1080p30 Surround MP4 H.264 AAC stereo; Dolby Digital (AC-3) Super high Very slow Very large
Super HQ 720p30 Surround MP4 H.264 AAC stereo; Dolby Digital (AC-3) Super high Very slow Very large
Super HQ 576p25 Surround MP4 H.264 AAC stereo; Dolby Digital (AC-3) Super high Very slow Very large
Super HQ 480p30 Surround MP4 H.264 AAC stereo; Dolby Digital (AC-3) Super high Very slow Very large

 

 

 

Official Github Repository

 

Leave a Reply