Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate Command Line Input to Node Fluent ffmpeg

I want to run the ffmpeg command line using node-fluent-ffmpeg (compile png images to a video):

ffmpeg -framerate 20 -i static/tmp/img%03d.png -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4  

I was trying the following, but with no luck.

var ffmpeg = require('fluent-ffmpeg');

var proc = new ffmpeg()
    .addInputOption('-framerate 20')
    .addInputOption('static/tmp/img%03d.png')
    .addInputOption('-c:v libx264')
    .addInputOption('-r 30')
    .addInputOption('-pix_fmt yuv420p out.mp4')
    .output('outputfile.mp4')
    .output(stream);

I looked all over the github repository and all the Q/A on stackoverflow, but with no proper answer.

How can I format the command line to js code?

Thanks!

like image 339
EladA Avatar asked Nov 04 '15 10:11

EladA


1 Answers

You've probably figured this out by now, but I'll answer anyway.

Try this:

var ffmpeg = require('fluent-ffmpeg');

var proc = new ffmpeg();

proc.addInput('static/tmp/img%03d.png')
.on('start', function(ffmpegCommand) {
    /// log something maybe
})
.on('progress', function(data) {
    /// do stuff with progress data if you want
})
.on('end', function() {
    /// encoding is complete, so callback or move on at this point
})
.on('error', function(error) {
    /// error handling
})
.addInputOption('-framerate 20')
.outputOptions(['-c:v libx264', '-r 30', '-pix_fmt yuv420p'])
.output('out.mp4')
.run();

Basically, anything before -i is considered an input option, and anything between the input and output file is considered an output option.

You can add multiple input or output options in one line using an array of options, the fluent ffmpeg github will also point you to some javascript code that works as shortcuts for some of these commands, although I still find it easier to use command line syntax. https://github.com/fluent-ffmpeg/node-fluent-ffmpeg

If you want multiple outputs, you can add more .output like in your example (although your command line ffmpeg wasn't doing that).

like image 85
pgm Avatar answered Sep 23 '22 23:09

pgm