How to stop ffmpeg video recording in c#?

2019-01-29 11:13发布

问题:

I am using FFmpeg in application and it start and record video perfectly but when I want to stop it ask for press "q", I got a System.EntryPointNotFoundException Error message.

How can I send message "q" to process which is in running state from application

    int key_q = 81; 

    [DllImport("user32.dll", EntryPoint = "postmessage")]       
    private static extern bool postmessage(IntPtr hwnd, uint msg, int wparam, int lparam);



    private void button_stop_Click(object sender, EventArgs e)
    {
          string process = "ffmpeg";

          Process[] pro = Process.GetProcessesByName("ffmpeg");

          pro[0].Refresh();

          IntPtr h = pro[0].MainWindowHandle;



          postmessage(h, 0x100, key_q, 0);


    }

回答1:

If you created the process initially, you can keep a handle to its stdin and send it "q" (possibly need to also send it a "\n" after).

If you didn't, then you could some third party .exe (or an internal equivalent) to send it a ctrl+c/ctrl+break to its process ID.

FFmpeg doesn't have a window handle since it's a console app, so you can't send it keystrokes with PostMessage et al. only through console signals (i.e. ctrl+c/break)