none
c# ffmpeg how to control console application from winforms send message and get message ? RRS feed

  • General discussion

  • I am using ffmpeg and I call it from console. I will put this to c# app and will control it from console. How can I achieve this ? Example I started console app with button. Console app asked a question -> overwrite file ? y/N and I will give this answer from my winforms then console will evaluate it. Later when I want to pause app I will send pause command from winforms app console will pause like I do normally without c#.

    Here is my code not the full code but may give some idea:

            private void createConsoleApp()
            {
                ProcessStartInfo psi = new ProcessStartInfo();
    
                psi.FileName = "cmd.exe";
    
                psi.Arguments = @"/c " + @"FFMPEG\ffmpeg.exe -f dshow -rtbufsize 2146M -f dshow -i video="+'"'+ "UScreenCapture"+'"'+ ":audio="+'"'+ "Stereo Mix (Realtek High Definition Audio)" + '"' + " -vcodec h264_qsv -r 60 -ac 1 -ar 44100 -preset fast -tune zerolatency -vf mpdecimate -pix_fmt yuv420p -threads:1 4"+@" FFMPEG\dshow_out2.mp4";
                psi.UseShellExecute = false;
                
                psi.CreateNoWindow = false;
    
                process.StartInfo = psi;
                process.Exited += new EventHandler(ProcessExited);
                process.EnableRaisingEvents = true;
            }


    I start when button clicked:

            private void recordBtn_Click(object sender, EventArgs e)
            {
                if (!FFMPEg_IsRunning)
                {
                    var result = process.Start();
                    FFMPEg_IsRunning = true;
                }
            }

    I found a way to post messages like this: 

            [return: MarshalAs(UnmanagedType.Bool)]
            [DllImport("user32.dll", SetLastError = true)]
            private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
    
            [DllImport("user32.dll")]
            static extern short VkKeyScan(char ch);
    
            private static void CmdPostKeys(Process cmdProcess, string message)
            {
                foreach (var c in message)
                {
                    PostMessage(cmdProcess.MainWindowHandle, WmKeyUp, (IntPtr)VkKeyScan(c), IntPtr.Zero);
                }
    
                PostMessage(cmdProcess.MainWindowHandle, WmKeyUp, (IntPtr)Keys.Enter, IntPtr.Zero);
            }
    
            private static void CmdPostKeyboard(Process cmdProcess, Keys key)
            {
                PostMessage(cmdProcess.MainWindowHandle, WmKeyUp, (IntPtr)key, IntPtr.Zero);
            }

    What can I do to read dialog of console with same way ?

    Additional problem when I post EXIT or exit message, because of keyboard language difference that takes this as : "exıt" instead "exit". When switch keyboard option to EN no problem. Is there a better way to send input virtually exact expected key ?

    Sorry for bad English,

    Thank You.

    • Edited by gomidas Tuesday, December 19, 2017 7:33 AM
    Monday, December 18, 2017 9:10 AM