avconv
Simply spawns an avconv process with any parameters and streams the results to you (meta data + conversion progress). Very small, fast, clean and does only this:
// Anything you wantvar params = ... ; // Returns a duplex streamvar stream = ; // Here you know it's donestream
It also can keep you informed about the progress by emitting events. That's a very unique function of this module. Link that with an UI and it will look flash. See progress
event below.
Support
If you've found avconv useful and would like to contribute to its continued development & support, please feel free to send a donation of any size - it would be greatly appreciated!
Installation
To install avconv, use npm:
$ npm install avconv
Then in your node.js app, get reference to the function like that:
var avconv = ;
Quick examples
Encode an avi video out of images
var params = '-f' 'image2' '-loglevel' 'info' '-i' '/tmp/images/' '-y' '/tmp/output.avi'; // Returns a duplex streamvar stream = ; // Anytime avconv outputs any information, forward these results to process.stdoutstream
- Avconv consultation is not subject of this module. If you need help with parameters, have a look at http://libav.org/avconv.html
- Same goes with node streams. You can do anything with them you want. Pipe them or listen to events. Easy.
- But if your're smart, then go, have a look at the unit tests. They contain some nice examples.
Using streams & pipes
var params = '-i' 'pipe:0' // Tell avconv to expect an input stream (via its stdin) '-f' 's16le' // We only want audio back '-acodec' 'pcm_s16le' 'pipe:1' // Tell avconv to stream the converted data (via its stdout); // Get the duplex streamvar stream = ; // Pipe a file into avconvfs; // Pipe the audio output to a new filestream;
How to watch for results (progress, meta data, output, errors, exit code)?
If you want to watch for errors or for exit codes from the avconv process then you should add event listeners like that:
var stream = ; stream; stream; stream; stream; // You can also pipe the outputstream; stream;
An exit code of 0 (zero) means there was no problem. An exit code of 127 means the program avconv could not be found. I recommend you to use a switch block to deal with various exit codes.
Depending on the log level you have passed onto the avconv process, the output might contain any useful information. Beware that warnings or errors from within the avconv process are still shown as normal output in the data
event.
Whereas errors from the stream are rarely filled (error
event). They happen only when there was an unix-related problem about spawning processes, memory blabbah ...
API
stream = avconv(params, [command = 'avconv'])
Avconv spawns a new avconv process with any given parameters. It does not validate the parameters nor mess with the results. That's all up to you. You would see avconv complaining about bad parameters in the data
event anyway. So:
one mandatory argument
- params - any array list with string arguments as values (see examples)
one optional argument
- command - the path to the avconv executable (for example to a static binary). Defaults to 'avconv'.
one return value
- stream - a readable stream where you can attach well-known events like:
.on('message', function(data) {...})
- a chunk of data with useful information, depending on the log level. Any warnings or errors from avconv are there too..on('progress', function(progress) {...})
- a floating number, 0 means conversion progress is at 0%, 1 is 100% and means, it's done. Very useful if you want to show the conversion progress on an user interface..on('data', function(data) {...})
- a buffer object with converted data (if outputting to pipe:1).on('error', function(data) {...})
- rarely used. Would contain issues related to the OS itself..once('exit', function(exitCode, signal, metadata) {...})
- for the exit code any integer where 0 means OK. Anything above 0 indicates a problem (exit code). The signal tells how the process ended, i.E. can be a SIGTERM you killed it withstream.kill()
. If it's null, then it ended normally.
And of course, you can .kill()
the stream, if you want to abort in the middle. It will kill the process in cold blood and delegate an exit
event to avconv's internals.
Metadata object
Most of the output of avconv is parsed into a metadata object accessable in the exit
event.
Please note that parsing of some stream properties may fail, resulting in null
or NaN
values.
// converting an flv file to webm input: duration: 32056 // milliseconds start: 0 bitrate: null stream: type: "video" codec: "h264" format: "yuv420p" resolution: 320 240 bitrate: 202 // kb/s fps: 2992 type: "audio" codec: "aac" samplerate: 22050 // Hz channels: 2 // will be 6 for 5.1 etc. sampleformat: "fltp" bitrate: 63 // kbs output: stream: type: "video" codec: "libvpx" format: "yuv420p" resolution: 320 240 bitrate: 200 type: "audio" codec: "libvorbis" samplerate: 22050 channels: 2 sampleformat: "fltp" bitrate: null
Changelog
See History.md
Contributors
- Michael Heuberger michael.heuberger@binarykitchen.com
- Jelle De Loecker jelle@kipdola.be
- Jan Scheurer lj1102@googlemail.com
- You?
License
MIT