Last active
September 21, 2023 06:45
-
-
Save DusanBrejka/16153fcb757fd9954e94a404d79a2b23 to your computer and use it in GitHub Desktop.
node-fluent-ffmpeg - Execute Custom FFMPEG arguments hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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) |
Thanks for the reply. Still not working. I had lifted you code pretty much exactly here it is:
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
return typeof args === "string" ? args.split(' ') : args;
};
return command;
};
var command =
executeFfmpeg('-framerate 15 -loop 1 -i snappy.jpg -f rtsp -rtsp_transport tcp -tune stillimage -pix_fmt yuv420p rtsp://localhost:8554 /mystream')
.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))
.run();
setTimeout(function() {
console.log ('killing it...')
command.kill();
}, 5000)`
Ah, alright, that's because run()
method does not return fluent-ffmpeg object.
Try this:
let command =
executeFfmpeg('-framerate 15 -loop 1 -i snappy.jpg -f rtsp -rtsp_transport tcp -tune stillimage -pix_fmt yuv420p rtsp://localhost:8554 /mystream')
.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));
command.run();
setTimeout(function() {
console.log ('killing it...')
command.kill();
}, 5000)`
Yes! Brilliant that worked! Thank you very much. So useful. Thanks for sharing and helping!
this can be considered as well https://www.npmjs.com/package/ffmpeg#add-custom-options
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you post the full code, please?
Seems like you're trying to access a variable from a different scope.
This will probably help: