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:
var params = [ ... ];
var stream = avconv(params);
stream.on('exit', function(...) {
...
})
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 = require('avconv');
Quick examples
Encode an avi video out of images
var params = [
'-f', 'image2',
'-loglevel', 'info',
'-i', '/tmp/images/',
'-y', '/tmp/output.avi'
];
var stream = avconv(params);
stream.on('message', function(data) {
process.stdout.write(data);
})
- 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',
'-f', 's16le',
'-acodec',
'pcm_s16le',
'pipe:1'
];
var stream = avconv(params);
fs.createReadStream('video.mp4').pipe(stream);
stream.pipe(fs.createWriteStream('audio.raw'));
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 = avconv(params);
stream.on('message', function(data) {
process.stdout.write(data);
});
stream.on('progress', function(progress) {
});
stream.on('error', function(data) {
process.stderr.write(data);
});
stream.on('data', function(data) {
});
stream.pipe(fs.createWriteStream('video.mp4'));
stream.once('exit', function(exitCode, signal, metadata) {
});
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 with stream.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.
{
input: {
duration: 32056,
start: 0,
bitrate: null,
stream: [
[
{
type: "video",
codec: "h264",
format: "yuv420p",
resolution: [ 320, 240 ],
bitrate: 202,
fps: 29.92
},{
type: "audio",
codec: "aac",
samplerate: 22050,
channels: 2,
sampleformat: "fltp",
bitrate: 63
}
]
]
},
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
License
MIT