Nodejs - Spawning child process for ffmpeg

Trying to utilize ffmpeg from my node server. This works on heroku, but I’d much rather it work on render

cp.spawn('ffmpeg', [
    '-i','-',
    '-c:v', 'copy', '-preset', 'ultrafast', '-tune', 'zerolatency',
    '-c:a', 'aac', '-strict', '-2', '-ar', '44100', '-b:a', '64k',=
    '-y',
    '-use_wallclock_as_timestamps', '1',
    '-async', '1',
    '-flush_packets', '1',
    '-rtbufsize', '1000',
    '-bufsize', '1000',
    '-f', 'flv',
    rtmpLink,
  ])

Any idea how this can be done?

Hi @James_Rezendes, ffmpeg is part of our node environment, so you should be able to spawn ffmpeg processes without issue. Can you describe the issue you’re seeing in more detail? Thanks!

That’s what I thought. Seems like something is preventing it from working fully.

Here’s a bit more of my code.

streams[socket.id].stream = cp.spawn('ffmpeg', [
    '-i','-',
    '-c:v', 'copy', '-preset', 'ultrafast', '-tune', 'zerolatency',
    '-c:a', 'aac', '-strict', '-2', '-ar', '44100', '-b:a', '64k',
    '-y',
    '-use_wallclock_as_timestamps', '1',
    '-async', '1',
    '-flush_packets', '1',
    '-rtbufsize', '1000',
    '-bufsize', '1000',
    '-f', 'flv',
    rtmpLink,
  ])

  // Kill the WebSocket connection if ffmpeg dies.
  streams[socket.id].stream.on('close', (code, signal) => {
    console.log('FFmpeg child process closed, code ' + code + ', signal ' + signal);
    if(streams[socket.id].socket && streams[socket.id].socket.close){
      streams[socket.id].stream.kill();
      streams[socket.id].socket.close();
      delete streams[socket.id];
    }
  });

I get the “FFmpeg child process closed, code 1, signal null” error in the console immediately after the spawn. Not sure what else to say, but I hope we can figure it out

I’ve found some more useful logs in the .on(‘data’) listener.

Looks like it’s a problem with my node-media-server.

This might not be a render issue, I’ll get back to this thread if need be, I appreciate your support

1 Like

Any time! Don’t hesitate to reach back out.

Seems like I should be using https instead of http for my flv output stream url for node-media-server.

This is from the docs. Looks like I need to include the paths for the ssl certificate / key. Is this possible with Render?

const NodeMediaServer = require('node-media-server');
 
const config = {
  rtmp: {
    port: 1935,
    chunk_size: 60000,
    gop_cache: true,
    ping: 30,
    ping_timeout: 60
  },
  http: {
    port: 8000,
    allow_origin: '*'
  },
  https: {
    port: 8443,
    key:'./privatekey.pem',
    cert:'./certificate.pem',
  }
};
 
 
var nms = new NodeMediaServer(config)
nms.run();

Render automatically serves all public-facing traffic over HTTPS by terminating TLS at the load balancer level and forwarding traffic on to your service over HTTP within a secure private network. So it shouldn’t ever be necessary to serve traffic over HTTPS. There’s currently no way to access the private key we use on your behalf, but you could generate your own certificate and key if it turns out to be absolutely necessary.

Maybe you can say more about the role that node-media-server plays within your architecture? How did you determine that https is required?

Thanks for the reply

I’m now no longer using node-media-server, and am doing everything from scratch. My live streaming server works like a charm w/ render.

This thread can now be closed

1 Like