0

Problem

As you can see from the code below, I'm executing a command on ffmpeg.exe bin using childProcess.exec() which creates 2 OS processes:

  • The child process: cmd.exe
  • The child's child process ffmpeg.exe

I'd like to be able to cancel the operation by terminating the ffmpeg.exe process, but neither of the following methods work, since ffmpeg.exe is a separate process:

  • Method 1: const terminatingProcess = childProcess.exec('kill ' + process.pid)
  • Method 2: videoConversionProcess.kill()

I cannot simply terminate the returned pid with another process or run .kill() on the videoConversionProcess since it terminates the cmd.exe process, not the ffmpeg.exe process which it spawned.

Code

const command = '"E:\\test\\ffmpeg.exe" -y -i "VIDEO_URL" -vcodec copy -c copy "E:\\test\\video.ts"'
const videoConversionProcess= childProcess.exec(command)

videoConversionProcess.stdout.on('data', (data) => {
  console.log(data)     
})

When I log the videoConversionProcess I can see the information about the cmd.exe that it spawned, but not the ffmpeg.exe which is doing all the work:

ChildProcess {_events: {…}, _eventsCount: 2, _maxListeners: undefined, _closesNeeded: 3, _closesGot: 0, …}
connected: false
exitCode: null
killed: true
pid: 18804
signalCode: "SIGTERM"
spawnargs: Array(5)
0: "C:\WINDOWS\system32\cmd.exe"
1: "/d"
2: "/s"
3: "/c"
4: ""E:\test\ffmpeg.exe" -y -i "VIDEO_URL" -vcodec copy -c copy "E:\test\video.ts"
...

Question

How do I terminate the operation?

Is there a way to exec the command on the ffmpeg.exe and make Node.js aware of this process? I tried using spawn instead of exec but I couldn't make it work, it didn't spawn the ffmpeg.exe process at all.

AlekseyHoffman
  • 2,438
  • 1
  • 8
  • 31
  • 1
    Does this answer your question? [Can't kill child process on Windows](https://stackoverflow.com/questions/32705857/cant-kill-child-process-on-windows). It suggests using Windows `taskkill` instead of `kill`, allowing you to include the child processes – that other guy Jul 20 '20 at 22:44
  • @thatotherguy hmm, I'm not sure it's the same thing because I'm already executing the command directly on the `ffmpeg.exe`. Let me try those solutions – AlekseyHoffman Jul 20 '20 at 22:49
  • Make sure you're looking at the [top voted, awarded answer](https://stackoverflow.com/a/32814686/1899640)'s suggestion for using `taskkill`. The suggestions for running the script directly do not apply, including the accepted answer. – that other guy Jul 20 '20 at 22:53
  • @thatotherguy thanks, one of those solutions worked for me. I was searching for the solution for like 2 hours, and for some reason google didn't lead me to that thread, thank you for letting me know about it – AlekseyHoffman Jul 20 '20 at 22:59

1 Answers1

3

Ciao, you can try to follow this guide.

In brief, to kill a process in windows you could do:

    exec(`taskkill /PID ${pid} /T /F`, (error, stdout, stderr)=>{
        console.log("taskkill stdout: " + stdout)
        console.log("taskkill stderr: " + stderr)
        if(error){
            console.log("error: " + error.message)
        }
    })
Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30