Skip to content

Instantly share code, notes, and snippets.

@DusanBrejka
Last active September 21, 2023 06:45
Show Gist options
  • Save DusanBrejka/16153fcb757fd9954e94a404d79a2b23 to your computer and use it in GitHub Desktop.
Save DusanBrejka/16153fcb757fd9954e94a404d79a2b23 to your computer and use it in GitHub Desktop.
node-fluent-ffmpeg - Execute Custom FFMPEG arguments hack
/*
As at the time of writing this Fluent ffmpeg-API for node.js has not been updated
for years and still does not support custom FFMPEG attributes, the only solutions
are either forking it or resorting to hacks like this one...
Please use it only when fluent does not support more complex arguments
(like generating multi-rendition HLS with all playlists in a single command)
NOTE: this does not support 'progress' event, but you can do it easily by
parsing 'stderr' event with extractProgress method from fluent-ffmpeg/lib/options.js
*/
const fluent = require('fluent-ffmpeg');
const executeFfmpeg = args => {
let command = fluent().output(' '); // pass "Invalid output" validation
command._outputs[0].isFile = false; // disable adding "-y" argument
command._outputs[0].target = ""; // bypass "Unable to find a suitable output format for ' '"
command._global.get = () => { // append custom arguments
if(typeof args === "string") {
return args.split(' ').filter(c => c !== "" && c !== "\\\n")
} else return args;
};
return command;
};
let cmd = executeFfmpeg('-f lavfi -i testsrc=size=1920x1080:rate=30 -y -t 60 ./test.mp4')
.on('start', commandLine => console.log('start', commandLine))
.on('codecData', codecData => console.log('codecData', codecData))
.on('error', error => console.log('error', error))
.on('stderr', stderr => console.log('stderr', stderr));
cmd.run();
setTimeout(function() {
cmd.kill();
}, 5000)
@privatesam
Copy link

Yes! Brilliant that worked! Thank you very much. So useful. Thanks for sharing and helping!

@utopictown
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment