0
var child_process = require('child_process');

if (typeof ffmpegrec == "undefined" || ffmpegrec == null)
{
    var ffmpegrec = '';
}


if(record === "1")
{
    ffmpegrec = child_process.spawn("ffmpeg",[
        "-re",                   // Real time mode
        "-f","x11grab",          // Grab screen
        "-framerate","60",              // Framerate
        "-s",width+"x"+height,   // Capture size
        "-i",":"+screen+"+"+top+","+left,        // Capture offset
        "-vb","1024k",            // Target bit rate
        "-f","mpeg1video",             // File format
        "/home/CloudGaming/webContent/recordedVideos/test.mpg"]);
}
else if(record === "0")
{
    ffmpegrec.kill();
}

I am sending a signal of 1 to record the video stream while 0 to stop the video stream. Recording works like a charm but the process can't be killed. Please help out. Thanks in advance!

  • http://stackoverflow.com/questions/9722624/how-to-stop-ffmpeg-remotely Use `ffmpegrec.kill('SIGINT');` – BaseZen Mar 07 '15 at 16:34
  • Doesn't seems to work either. This is the error I got: /home/CloudGaming/hello/newapp.js:113 ffmpegrec.kill('SIGINT'); ^ TypeError: Object has no method 'kill' at Object. (/home/CloudGaming/hello/newapp.js:113:12) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3 – Tommy Quek Mar 07 '15 at 18:05
  • I was assuming, perhaps incorrectly, that you were using https://nodejs.org/api/child_process.html#child_process_child_kill_signal Am I incorrect? How did you initialize the `child_process` global, exactly? – BaseZen Mar 07 '15 at 18:07
  • I tried that and it didn't worked for me as well. – Tommy Quek Mar 07 '15 at 18:09
  • Tried that as well? Doesn't make sense. And you didn't answer the second question. – BaseZen Mar 07 '15 at 18:14
  • if (typeof ffmpegrec == "undefined" || ffmpegrec == null){var ffmpegrec = '';} - this is the global declaration. I didn't use the var in my main code: if(record===1), just for illustration here. "var child_process = require('child_process');" - global child_process – Tommy Quek Mar 07 '15 at 18:45
  • It doesn't make sense to assign `ffmpegrec` to the empty string. And using `var` inside a block obscures that it's meant to be a global. I'll bet you have scoping errors so that the two references to ffmpegrec do not refer to the same object. I'd edit the question to include all live, relevant code that's causing the error, showing full functions if any code is in a function, and showing what's global. Boiling it down for the sake of brevity is probably hiding the actual problem. – BaseZen Mar 07 '15 at 18:55

1 Answers1

1

I'm taking a bigger-picture approach to show how processes can be managed using spawn(), etc.

I think you're making a fundamental error in that you're running this as a script to completion to start the process, and expecting to be able to run it again to stop the process. When the script stops the first time, ffmpegrec no longer has any meaning; the JavaScript engine and all its global variables are disposed of. So on the second run, now you initialize ffmpegrec to an empty string, just so it's some kind of Object. This is voodoo coding.

Here's a demonstration script that stays alive so it can signal its child process after it has spawned it. You must find a way to keep the script running and somehow send it "start" and "stop" commands which then get relayed to the child ffmpeg process. This is a design challenge that's up to you. Then, you have to handle the child process with appropriate error and output listeners, you need to use functional abstraction, set all your variables in the right places, don't mix types, and check for errors. The following works. See if that straightens everything out for you.

var child_process = require('child_process');
var ffmpegrec = null;

function record(setting) {
    if ( setting === 1 ) {
        if ( ffmpegrec !== null ) {
            console.log('Error! ffmpeg already running');
        }
        else {
            // I don't have ffmeg installed, so simulating a long process instead                                                                      
            ffmpegrec = child_process.spawn('sh', ['-c', 'sleep 10']);
            ffmpegrec.on('close', function(code, signal) {
                console.log('child process terminated due to receipt of signal ' + signal + ' or code ' + code);
                ffmpegrec = null;
            });
            ffmpegrec.stdout.on('data', function (data) {
                console.log('stdout: ' + data);
            });
            ffmpegrec.stderr.on('data', function (data) {
                // important to monitor ffmegprec's complaints
                console.log('stderr: ' + data);
            });
            // Can track using "ps" unix util, etc.
            console.log('Initiated recording on PID ' + ffmpegrec.pid);
        }
    }
    else if ( setting === 0 ) {
        if ( ffmpegrec === null ) {
            console.log('Error! Tried to stop recording and no recording active');
        }
        else {
            ffmpegrec.kill('SIGINT');
            // ffmpegrec will become null if and when signal delivery succeeds
            console.log('Terminated recording');
        }
    }
    else {
        console.log('Error! Invalid value for setting: ' + setting);
    }
}


console.log('Starting');
record(0); // Can't stop if not yet started; 1st error                                                                                                 
record(1); // OK                                                                                                                                       
record(1); // Can't start if already started; 2nd error                                                                                                

setTimeout(function() { record(0); /* OK to stop now */ }, 5000);
BaseZen
  • 8,650
  • 3
  • 35
  • 47