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!
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With