在 C# 中的 ffmpeg 中终止进程

Kill Process in ffmpeg in c#

我正在使用 Process class 在 ffmpeg 中执行命令,如下所示:

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();

此代码将视频流式传输到网络,但我想在单击按钮时停止流式传输

我使用了 process.kill() 但即使我关闭了应用程序,进程仍在流式传输

如何在后台停止进程或向其发送 ctrl+c?

我创建了一个方法来终止 ffmpeg 进程。

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();
}

随便调用就可以了。

UPDATE 1

为了只杀死 FFMPEG 进程的一个实例,我们需要先获取它的 PID。当你为流定义你的 ffmpeg 进程时,在全局范围内定义它并在初始化后使用以下命令获取 PID。

int myProcessId = FfmpegProcess.Id;

然后调用下面

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();
}

这将仅终止具有给定 PID 的进程。参数末尾的 /T 标志决定了整个进程树将被杀死。

干杯

前导“/C”表示您通过cmd.exe启动它? 在这种情况下,进程对应于 cmd,后者又启动 ffmpeg。因此杀死 cmd 不会杀死 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(); 应该可以了。

所以我在 selenium Nunit 测试中遇到了启动和停止 ffmpeg 进程的同样麻烦。经过一番努力,我能够创建一个简单的解决方案。将输入作为“q”发送到 ffmpeg 的进程 window 可以优雅地停止该进程,并且视频录制也不会损坏。 这是我的 c# 代码,用于启动 ffmpeg 并在执行后停止它。

  1. 创建一个 bat 文件来启动你的 ffmpeg(你将从你的 c# 代码调用这个 batfile)
  2. 在你的 selenium 测试中,创建一个记录 class 和 2 个方法来开始和停止记录(在我的例子中,我在所有测试之前启动 bat 文件,就像调用 executeScreenRecordingBatFile 方法一样在 onetimesetup 属性中开始录制并在 onetimeteardown 中调用 StopScreenRecording 方法)示例代码如下。
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.
        } 
    }
}