This library abstracts the complex command-line usage of ffmpeg into a fluent, easy to use node.js module. In order to be able to use this module, make sure you have ffmpeg installed on your system (including all necessary encoding libraries like libmp3lame or libx264).
Now including input streaming support (means you can convert on-the-fly using an input- and an outputstream)!
Via npm:
$ npm install fluent-ffmpeg
Or as a submodule:
$ git submodule add git://github.com/schaermu/node-fluent-ffmpeg.git vendor/fluent-ffmpeg
To run unit tests, make sure have nodeunit installed on your system (currently, only npm-installed versions are supported).
$ nodeunit test
Make sure your ffmpeg installation is up-to-date to prevent strange assertion errors because of missing codecs/bugfixes.
You find a lot of usage examples (including a real-time streaming example using flowplayer and express!) in the examples
folder.
fluent-ffmpeg is always initialized using the returned class of your initial (and only) require call. You have to supply an config object to the constructor containing at least the input source (either a ReadableStream or a filepath).
var ffmpeg = require('fluent-ffmpeg');
var proc = new ffmpeg({
// input source, required
source: '/path/to/your_movie.avi',
// timout of the spawned ffmpeg sub-processes in seconds (optional, defaults to 30)
timeout: 30,
// default priority for all ffmpeg sub-processes (optional, defaults to 0 which is no priorization)
priority: 0,
// set a custom [winston](https://github.com/flatiron/winston) logging instance (optional, default null which will cause fluent-ffmpeg to spawn a winston console logger)
logger: null,
// completely disable logging (optional, defaults to false)
nolog: false
});
Since ffmpeg does not support dynamic sizing of your movies, fluent-ffmpeg can do this job for you (using it's internal metadata-discovery). The following size formats are allowed to be passed to withSize
:
320x?
- Fixed width, calculate height?x240
- Fixed height, calculate width50%
- percental resizing320x240
- fixed size (plain ffmpeg way)
Using fluent-ffmpeg, you can auto-pad any video output when converting the aspect ratio. When converting from 4:3 to 16:9, padding is added to the left/right, when converting from 16:9 to 4:3, padding is added to top/bottom.
var ffmpeg = require('fluent-ffmpeg');
var proc = new ffmpeg({ source: '/path/to/your_movie.avi' })
.withAspect('4:3')
.withSize('640x480')
.applyAutopadding(true, 'white')
.saveToFile('/path/to/your_target.avi', function(retcode, error) {
console.log('file has been converted succesfully');
});
This command will auto-pad your 4:3 output video stream using a white background-color (default is black).
This example loads up a predefined preset in the preset folder (currently, fluent-ffmpeg ships with presets for DIVX, Flashvideo and Podcast conversions)
var ffmpeg = require('fluent-ffmpeg');
var proc = new ffmpeg({ source: '/path/to/your_movie.avi' })
.usingPreset('podcast')
.saveToFile('/path/to/your_target.m4v', function(retcode, error) {
console.log('file has been converted succesfully');
});
Using the chainable API, you are able to perform any operation using FFMPEG. the most common options are implemented using methods, for more advanced usages you can still use the addOption(s)
method group.
var ffmpeg = require('fluent-ffmpeg');
var proc = new ffmpeg({ source: '/path/to/your_movie.avi' })
.withVideoBitrate(1024)
.withVideoCodec('divx')
.withAspect('16:9')
.withFps(24)
.withAudioBitrate('128k')
.withAudioCodec('libmp3lame')
.withAudioChannels(2)
.addOption('-vtag', 'DIVX')
.toFormat('avi')
.saveToFile('/path/to/your_target.avi', function(retcode, error) {
console.log('file has been converted succesfully');
});
One pretty neat feature is the ability of fluent-ffmpeg to generate any amount of thumbnails from your movies. The screenshots are taken at automatically determined timemarks using the following formula: (duration_in_sec * 0.9) / number_of_thumbnails
.
var ffmpeg = require('fluent-ffmpeg');
var proc = new ffmpeg({ source: '/path/to/your_movie.avi' })
.withSize('150x100')
.takeScreenshots(5, '/path/to/thumbnail/folder', function(err) {
console.log('screenshots were saved')
});
For more control, you can also set the timemarks for taking screenshots yourself (timemarks are always in seconds):
var ffmpeg = require('fluent-ffmpeg');
var proc = new ffmpeg({ source: '/path/to/your_movie.avi' })
.withSize('150x100')
.takeScreenshots({
count: 2,
timemarks: [ '0.5', '1' ]
}, '/path/to/thumbnail/folder', function(err) {
console.log('screenshots were saved')
});
Using a seperate object, you are able to access various metadata of your video file.
var ffmpegmeta = require('fluent-ffmpeg').Metadata;
// make sure you set the correct path to your video file
ffmpegmeta.get('/path/to/your_movie.avi', function(metadata) {
console.log(require('util').inspect(metadata, false, null));
});
Using the notification callback onCodecData, you can get informations about the input file's codecs being processed:
var ffmpeg = require('fluent-ffmpeg');
var proc = new ffmpeg({ source: '/path/to/your_movie.avi' })
.withAspect('4:3')
.withSize('640x480')
.onCodecData(function(codecinfo) {
console.log(codecinfo);
})
.saveToFile('/path/to/your_target.avi', function(retcode, error) {
console.log('file has been converted succesfully');
});
You can set the call back onProgress if you want to be notified on every progress update (triggered as fast as it's written out by FFMPEG).
var proc = new ffmpeg({ source: '/path/to/your_movie.avi' })
.withAspect('4:3')
.withSize('640x480')
.onProgress(function(progress) {
console.log(progress);
})
.saveToFile('/path/to/your_target.avi', function(retcode, error) {
console.log('file has been converted succesfully');
});
The progress object consists of 6 properties:
frames
- the total processed frame countcurrentFps
- the framerate at which FFMPEG is currently processing the filecurrentKbps
- the throughput at which FFMPEG is currently processing the filetargetSize
- the current size of the target filetimemark
- the timestamp of the frame being processed right nowpercent
- an estimation on the progress (metadata is used, durationsec * fps)
To create a custom preset, you have to create a new file inside the lib/presets
folder. The filename is used as the preset's name ([presetname].js). In order to make the preset work, you have to export a load
function using the CommonJS module specifications:
exports.load = function(ffmpeg) {
// your custom preset code
}
The ffmpeg
parameter is a full fluent-ffmpeg object, you can use all the chaining-goodness from here on. For a good example for the possibilities using presets, check out lib/presets/podcast.js
.
You can adjust the scheduling priority of the child process used by ffmpeg, using renice (http://manpages.ubuntu.com/manpages/intrepid/man1/renice.1.html):
var ffmpeg = require('fluent-ffmpeg');
var proc = new ffmpeg({ source: './source.mp3', priority: 10 })
.withAudioCodec('libvorbis')
.toFormat('ogg')
.saveToFile('./target.ogg', function(retcode, error) {
console.log('file has been converted succesfully');
});
Which will use a niceness of 10 (thus it has a lower scheduling priority than the node process and other processes, which default to a niceness of 0).
If you want to know for sure that the ffmpeg child process will not run for longer than a certain amount of time, you can optionally pass the key 'timeout' into the constructor of the ffmpeg command object. An example of a process that will return an error string of 'timeout' if ffmpeg did not finish within 10 minutes:
var ffmpeg = require('fluent-ffmpeg');
var proc = new ffmpeg({ source: './source.mp3', timeout: 10 * 60 })
.withAudioCodec('libvorbis')
.toFormat('ogg')
.saveToFile('./target.ogg', function(retcode, error) {
if (retcode == ffmpeg.E_PROCESSTIMEOUT) {
console.log('ffmpeg terminated because of timeout');
}
});
Contributions in any form are highly encouraged and welcome! Be it new or improved presets, optimized streaming code or just some cleanup. So start forking!
(The MIT License)
Copyright (c) 2011 Stefan Schaermeli <[email protected]>
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.