简体   繁体   中英

Kill Process in ffmpeg in c#

I'm using Process class to execute commands in ffmpeg like this:

string command = "/C ffmpeg -re -i test.mp4 -f mpegts udp://127.0.0.1:" + port.Text;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.Arguments = command;
process.Start();

The leading "/C" indicates that you start it via cmd.exe? In that case process corresponds to cmd which in turn starts ffmpeg. Thus killing cmd doesn't kill ffmpeg.

string command = "-re -i test.mp4 -f mpegts udp://127.0.0.1:" + port.Text;
process.StartInfo.FileName ="ffmpeg";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.Arguments = command;
process.Start();

process.Kill(); should work then.

I created a method to kill ffmpeg process.

 private void KillAllFFMPEG() { Process killFfmpeg = new Process(); ProcessStartInfo taskkillStartInfo = new ProcessStartInfo { FileName = "taskkill", Arguments = "/F /IM ffmpeg.exe", UseShellExecute = false, CreateNoWindow = true }; killFfmpeg.StartInfo = taskkillStartInfo; killFfmpeg.Start(); } 

Just call it wherever you want.

UPDATE 1

In order to kill just one instance of the FFMPEG process, we need to get it's PID first. When you define your ffmpeg process for streaming, define it in the global scope and use following command to get the PID after it is initialized.

 int myProcessId = FfmpegProcess.Id; 

Then call the following

 private void KillFFMPEGByPID(int PID) { Process killFfmpeg = new Process(); ProcessStartInfo taskkillStartInfo = new ProcessStartInfo { FileName = "taskkill", Arguments = "/PID " + Convert.ToString(PID) + " /T", UseShellExecute = false, CreateNoWindow = true }; killFfmpeg.StartInfo = taskkillStartInfo; killFfmpeg.Start(); } 

This will kill only the process with the given PID. /T flag at the end of the argument determines that whole process tree will be killed.

Cheers

So I was going through the same trouble of starting and stopping ffmpeg process in my selenium Nunit test. After bit of struggle I was able to create a simple solution. Sending as "q" a input to the process window of ffmpeg gracefully stops the process and the video recording is not corrupt as well. here is my c# code to start the ffmpeg and stop it after execution.

  1. <\/li>
  2. <\/li><\/ol>
     using System.Diagnostics; using System.IO; namespace FunctionalTests { public class Recording { public static Process process; public static void executeScreenRecordingBatFile() { try { process = new Process(); process.StartInfo.FileName = @"C:\\Program Files (x86)\\StartScreenRecording.bat"; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardInput = true;\/\/ this required to send input to the current process window. bool started = process.Start(); if (started==true) { Console.WriteLine("Bat file started"); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace.ToString()); throw; } } public static void StopScreenRecording() { StreamWriter myStreamWriter = process.StandardInput; \/\/ this required to send StandardInput stream, nothing fancy myStreamWriter.WriteLine("q"); \/\/this will send q as an input to the ffmpeg process window making it stop , please cross check in task manager once if the ffmpeg is still running or closed. } } }<\/code><\/pre>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM