【问题标题】:How to stop ffmpeg user screen recording in c#如何在 C# 中停止 ffmpeg 用户屏幕录制
【发布时间】:2017-07-20 15:45:41
【问题描述】:

我在应用程序中使用 ffmpeg,它可以完美地启动和录制视频,但是当我想停止它时要求按“q”,那么如何将“q”传递给应用程序处于运行状态的进程。

【问题讨论】:

    标签: c# ffmpeg


    【解决方案1】:

    FFMpeg 正确响应 SIGINT 并应完成写入视频容器文件。

    (如果您需要有关在 c# 中发送信号的信息,请参阅 this

    我相信最新版本的 FFMpeg 不再使用“q”,而是要求 ctrl-c 退出。

    【讨论】:

    • 以及如何从 asp.net0 向 cmd 传递信号。
    • asp.net 与 c# 在后端
    • 应该是:GenerateConsoleCtrlEvent(0, currentpid)
    • 使用 ffmpeg 2.7.2 版仍然需要按 [q] 停止录制 HLS 流。
    【解决方案2】:
    string process = //...process name
    
    Process p = Process.GetProcessesByName(process).FirstOrDefault();
    if( p != null)
    {
        IntPtr h = p.MainWindowHandle;
        SetForegroundWindow(h);
        SendKeys.SendWait("q");
    }
    

    【讨论】:

    • SetForegroundWindow(h); SendKeys.SendWait("q");
    【解决方案3】:

    下面的设置可以让我结束这个过程。在示例中,我在 3 秒后触发,但您可以随时将“q”异步传递给进程。否则,将记录设置为特定的时间会更有意义。

                string outputFile = "output.mp4";
                if(File.Exists(outputFile))
                {
                    File.Delete(outputFile);
                }
                string arguments = "-f dshow -i video=\"screen-capture-recorder\" -video_size 1920x1080 -vcodec libx264 -pix_fmt yuv420p -preset ultrafast " + outputFile;
    
                //run the process
                Process proc = new Process();
    
                proc.StartInfo.FileName = "ffmpeg.exe";
                proc.StartInfo.Arguments = arguments;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
    
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardInput = true;
    
                proc.ErrorDataReceived += build_ErrorDataReceived;
                proc.OutputDataReceived += build_OutDataReceived;
                proc.EnableRaisingEvents = true;
                proc.Start();
    
                proc.BeginOutputReadLine();
                proc.BeginErrorReadLine();
    
                await Task.Delay(3000);
    
                StreamWriter inputWriter = proc.StandardInput;
                inputWriter.WriteLine("q");
    
                proc.WaitForExit();
                proc.Close();
                inputWriter.Close();
    

    【讨论】:

      【解决方案4】:

      使用此代码:

       [System.Runtime.InteropServices.DllImport("User32.dll", EntryPoint = "PostMessageA")]
       private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
      
       int Key_Q = 81;
       PostMessage(hWnd, 0x100, Key_Q, 0);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-04
        • 2018-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-28
        • 2021-11-09
        • 2022-10-25
        相关资源
        最近更新 更多