Search code examples
c#ffmpeg

How can I send command "q" to FFMPEG with c#


How can I send the command "q" to a FFMPEG process with C#?
I tried it like this:

var p = Process.GetProcessesByName("ffmpeg").FirstOrDefault();

if (p != null)
{
    p.StandardInput.Write("q");
}

But I got this error:

Error message in Visual Studio


Solution

  • You can use the SendMessage call of the user32 api.

    [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,          string lpszClass, string lpszWindow);
    
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
    
    static void Send(string message)
    {
        var notepads = Process.GetProcessesByName("notepad");
        
        if (notepads.Length == 0)
            return;
    
        if (notepads[0] != null)
        {
            var child = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
            SendMessage(child, 0x000C, 0, message);
        }
    }
    

    Change it as you need. I'm not sure it would work for your situation, but it's always good to try.

    Goodluck.