Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

privatesam commented Jun 24, 2020

Thanks for this - really useful for something I'm working on. The only thing I'm struggling with is how to kill this once this (in my case) stream is running. command.kill() doesn't do anything I just get Cannot read property 'kill' of undefined

Thanks for any help.

@DusanBrejka
Copy link
Author

DusanBrejka commented Jun 24, 2020

Can you post the full code, please?
Seems like you're trying to access a variable from a different scope.
This will probably help:

let ffmpeg = 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))
    .run();

setTimeout(() => {
    ffmpeg.kill();
}, 5000);

@privatesam
Copy link

privatesam commented Jun 24, 2020

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)`

@DusanBrejka
Copy link
Author

DusanBrejka commented Jun 24, 2020

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)`

@privatesam
Copy link

privatesam commented Jun 24, 2020

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

@utopictown
Copy link

utopictown commented Sep 20, 2021

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