• Stars
    star
    151
  • Rank 246,057 (Top 5 %)
  • Language
    C++
  • Created almost 13 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

nodejs extension for libavcodec

DEPRECATED: Please use fluent-ffmpeg instead.

Node Audio and Video Transcoding Library BuildStatus

navcodec provides a high level and flexible javascript module for video and audio transcoding based on the excelent libavcodec(from the FFmpeg team) library.

This module can transcode almost any known video and audio format efficiently, it can generate multiple thumbnails or output video in multiple resolutions in parallel quickly with low memory consumption while also optimizing the output for web reproduction.

Follow optimalbits for news and updates regarding this library.

#Examples

Transcoding

navcodec = require('navcodec');

navcodec.open('myinput.mov', null, function(err, media){
  if(media){
    media.addOutput('myoutput.mp4', {
    	width:640,
    	height:480,
    	audioBitrate:128000,
    	videoBitrate:500000
    });
    
    media.transcode(function(err, progress, finished, time){
      console.log(progress);
      if(finished){
        console.log("Total transcoding time: " + time + "ms");
      }
    }
  }
});

This will transcode myinput.mov into myoutput.mp4 according to the given options. The callback will be called with a progress variable between 0 and 100.

Since the transcode uses mostly libavcodec optimized functions, the above example will run really fast.

Available options (and their defaults):

// Input
itsoffset       (n/a)
y               (true)
stats           (true)
logLevel        (n/a)

// Output
streamCopy      (n/a, nullfies a videoBitrate, videoCodec, audioBitrate, audioCodec, sampleRate)

// Video
width           (input width)
height          (input height)
videoBitrate    (input bitrate if available, 0 otherwise)
videoCodec      (standard codec for current file container)
keepAspectRatio (true)
rotate          ('clock' for clockwise, 'cclock' for counter clockwise)
fps             (specify the output frames per second)
minFps          (specify the output minimum frames per second)
skipVideo       (false)
skipVideoFrames (0)*
videoBSF        (n/a)
format          (n/a)

// Audio
audioBitrate    (input bitrate if available, 0 otherwise)
sampleRate      (44100)
channels        (2)
audioCodec      (standard codec for current file container)
skipAudio       (false)
format          (n/a)

// Thumbnails
keepAspectRatio (true)
width           (input width)
height          (input height)

*Experimental feature.

Multiple outputs

Several outputs can be added to the media object, and when transcoding the data will be processed in parallel. This is quite convenient when generating thumbnails (which will be very cheap to generate), or if several output formats are required (will only require one decoding process of the input file):

navcodec.open('myinput.mov', null, function(err, media){
  if(media){
    media.addOutput('myoutput.mp4', {
	    width:640,
	    height:480,
	    audioBitrate:128000,
	    videoBitrate:500000
    });

    media.addOutput('thumbnail.mp4', {
	    width:80,
	    height:60,
	    skipAudio:true,
	    videoBitrate:50000
    });

    media.transcode(function(err, progress, time){
      console.log(progress);
      if(progress === 100){
        console.log("Total transcoding time: " + time + "ms");
      }
    });
  }
});

##Resolution video pyramid

You can use Navcodec to transcode a video into multiple resolutions, forming a pyramid of resolutions, than can later be used to deliver the most efficient version for a given client in terms of screen resolution or bandwidth.

Media##addPyramid(basename: string, maxSize, minSize, options);

So for example, if you want to transcode to a pyramid of images similar to the ones in you tube:

media.addPyramid('new.mp4', {width: 1920}, {witdh: 240}, {keepAspectRatio: true, videoBitrate: '2000k'});

This will produce a pyramid of videos like the following:

1080-new.mp4
70

##Jpeg Thumbnails

A very common use case is the generation of a jpeg thumbnail to represent some video sequence. This can be easily accomplished by calling the function thumbnail. It can be called once or several times if different options are required. See the following example, where for every generated thumbnail a time offset is choosen:

navcodec.open('myinput.mov', null, function(err, media){
  if(media){
    media.thumbnail([{'first128.jpg':1, 'secong128.jpg:100.5'},{width:128,height:128});
    media.thumbnail([{'first64.jpg':1, 'secong64.jpg:100.5'},{width:64,height:64});
    
    media.transcode(function(err, progress, time){
      console.log(progress);
      if(progress === 100){
        console.log("Total transcoding time: " + time + "ms");
      }
    });
  }
});

Note that generating thumbnails at the same time as transcoding a full video is a extreamly cheap operation.

Metadata

Metadata is available after opening the media file. Its just a javascript object with keys and values:

navcodec.open('walk.flac', function(err, media){
  if(media){
    console.log(media.metadata);
  }
});

The previous example would result in the following output:

{
  ARTIST: 'Foo Fighters',
  TITLE: 'Walk',
  ALBUM: 'Wasting Light',
  DATE: '2011',
  track: '11',
  TOTALTRACKS: '11',
  GENRE: 'Rock',
  album_artist: 'Foo Fighters',
  'ALBUM ARTIST': 'Foo Fighters',
  COMMENT: 'EAC V1.0 beta 1, Secure Mode, Test & Copy, AccurateRip, FLAC 1.2.1b -8' 
}

Besides the 'metadata' property, other convenient properties are also available in the media object:

width
height
videoBitrate
audioBitrate
bitrate
samplerate
duration

Optimize for Web

Normally, a mp4 file created by ffmpeg or any other transcoder, places the index at the end of the file (moov block). This makes impossible for a web browser to perform seeking until the whole file has been downloaded.

Navcodec includes a useful utility called relocateMoov that will move the moov atom at the begining of the file, thus making it better for seeking in a web based player. Example:

navcodec.relocateMoov('myinput.mp4','myoutput.mp4', function(err){
  if(err){
    console.log(err);
  }
});

Install

navcodec depends on libavcodec, and this library must be installed before you can install this module using npm.

The current version of libavcodec needed by navcodec is 0.9.1 and above.

For most unixes there are packages available, in ubuntu for example use libavcodec-dev. If you want you can also compile the source code with your preferred settings, this can be useful if you want to get the maximum performance from the library.

For Mac OSX use brew and install the ffmpeg package.

brew install ffmpeg

Note that version 0.9.x of libavcodec is brand new and the afore mentioned package managers may not have yet updated it.

When the libavcodec dependencies are fulfilled, just use npm to install the package:

npm install navcodec

Compiling libavcodec (Recommended)

  • Download the latest stable 0.9.x release from libav.org

  • Uncompress the tarball, Ex:

      tar -xvf libav-0.9.1.tar.gz && cd libav-0.9.1
    
  • Configure your makefile, example:

      ./configure  --prefix=/usr/local --enable-shared --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --cc=/usr/bin/clang --enable-libfaac --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libxvid --enable-libx264
    

You may want to add some hardware acceleration flags for your architecture, for example:

--enable-vda  (Mac OS)
--enable-vaapi (Linux/Unix Intel)
--enable-vdpau (Linux/Unix Nvidia)

you can get a list of other configuration options using:

./configure --help
  • Compile the libraries:

      make
    
  • Install

      make install
    
  • Notes

Mac users will need to install Xcode in order to be able to compile libavcodec by themselves.

It may be needed to install libvpx separatelly if you need WebM support (in Mac):

brew install libvpx

References

Ubuntu HOWTO: Install and use the latest FFmpeg and x264

Roadmap

  • Multiple pass encoding
  • Support presets
  • Support for filters

License

Copyright(c) 2012 Optimal Bits Sweden AB. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

bull

Premium Queue package for handling distributed jobs and messages in NodeJS.
JavaScript
15,314
star
2

redbird

A modern reverse proxy for node
JavaScript
4,384
star
3

node_acl

Access control lists for node applications
JavaScript
2,620
star
4

ground

Ground - MV(C/VM) Javascript Framework
JavaScript
100
star
5

Crawlme

Ajax crawling for your web application
JavaScript
67
star
6

fs.js

A higher level file system API for HTML5 FileSystem.
JavaScript
67
star
7

cabinet

A lightweight fast static file server for node loaded with useful features
JavaScript
55
star
8

fowl

A NodeJS Document Model and Query Layer for FoundationDB
JavaScript
48
star
9

dolphin

A fast, lightweight an modular library to interact with docker daemons.
JavaScript
41
star
10

livef1

Live Formula1 Service written in Nodejs
JavaScript
14
star
11

servion

A file server written in nodejs
JavaScript
9
star
12

blackbird

Confortable interfacing of event based transports
JavaScript
8
star
13

sconf

Simple Configuration for NodeJS Applications
JavaScript
7
star
14

followme

Bot to get high quality twitter followers
JavaScript
5
star
15

kashmir

NodeJS base cache optimised for medium to large data files
JavaScript
5
star
16

marlin

A lightweight file based CMS in node
JavaScript
4
star
17

wing

Realtime model synchronization
JavaScript
4
star
18

tradetonic

A website providing tools for "fibonacci trading".
JavaScript
3
star
19

db.js

IndexedDB Wrapper. Provides an easier to use, cross browser API.
JavaScript
3
star
20

resultqueue

Result queue on top of bull
JavaScript
3
star
21

castmill.com

Castmill's webpage
JavaScript
3
star
22

disturbed

A distributed event emitter for the browser and node
JavaScript
3
star
23

medroid-client

Client for medroid servers
JavaScript
2
star
24

livef1.in

Web app for Livef1.in
JavaScript
2
star
25

vre

Tiled based vector and text rendering engine.
C
2
star
26

node-adleria

Adleria (https://adleria.com) API for nodejs
JavaScript
2
star
27

docker-nfs

Dockerized NFS Server
Shell
2
star
28

ppt-converter

JavaScript
2
star
29

alacarte

Simple menu for different devices
JavaScript
2
star
30

docker-nodejs

Nodejs docker image
1
star
31

gnd-dropdown

A Gnd dropdown component
TypeScript
1
star
32

castmill-api

JavaScript
1
star
33

gnd.io

Ground Web Site
CSS
1
star
34

cfp

Nordea Corporate File Payments
JavaScript
1
star
35

marlin_old

A bi-direction realtime communication library for nodejs and the browsers
1
star
36

branches

Main repository for Optimal Bits
Objective-C
1
star
37

optimalbits.github.com

Optimal Bits Webpage
JavaScript
1
star
38

castmill-electron-old

Electron based castmill player
TypeScript
1
star
39

gnd-todomvc

TodoMVC Demo Application using Ground
JavaScript
1
star