2

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.

Akash Langhani
  • 167
  • 1
  • 10

4 Answers4

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.

Community
  • 1
  • 1
jeremy
  • 4,294
  • 3
  • 22
  • 36
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