I am using ffmpeg in application and It start and record video perfectly but when I want to stop it ask for press "q", so how can I pass "q" to process which is in runing state from application.
Asked
Active
Viewed 5,132 times
4 Answers
1
FFMpeg responds correctly to a SIGINT and should finish writing the video container file.
(See this if you need info on sending a signal in c# )
I believe recent versions of FFMpeg no longer use 'q', but instead demand ctrl-c to quit.
-
and how to pass signal to cmd from asp.net0. – Akash Langhani Oct 28 '13 at 20:42
-
asp.net with c# on backend – Akash Langhani Oct 29 '13 at 05:57
-
Should be: `GenerateConsoleCtrlEvent(0, currentpid)` – jeremy Oct 29 '13 at 14:24
-
With ffmpeg version 2.7.2 it still need to press [q] to stop recording an HLS stream. – Iman Jul 24 '18 at 06:48
0
string process = //...process name
Process p = Process.GetProcessesByName(process).FirstOrDefault();
if( p != null)
{
IntPtr h = p.MainWindowHandle;
SetForegroundWindow(h);
SendKeys.SendWait("q");
}

Jonesopolis
- 25,034
- 12
- 68
- 112
0
Use this code:
[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);

Ali
- 3,373
- 5
- 42
- 54
0
The setup below is working for me to end the process. In the example I am triggering after 3 seconds but you could pass the 'q' to the process at any time asyncronously. Otherwise it would make more sense to set the record to a specific amount of time.
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();

ickydime
- 1,051
- 10
- 22