我在应用程序中使用ffmpeg并且它完美地启动和录制视频但是当我想要停止它时请求按“q”,那么如何将“q”传递给处于运行状态的应用程序。
答案 0 :(得分:1)
答案 1 :(得分:0)
string process = //...process name
Process p = Process.GetProcessesByName(process).FirstOrDefault();
if( p != null)
{
IntPtr h = p.MainWindowHandle;
SetForegroundWindow(h);
SendKeys.SendWait("q");
}
答案 2 :(得分:0)
使用此代码:
[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);
答案 3 :(得分:0)
以下设置可供我结束此过程。在示例中,我在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();