none
Why when using ffmpeg to record as video file the desktop when i click the button to stop the record the ffmpeg.exe is still working ? RRS feed

  • Question

  • The class of the ffmpeg:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Ffmpeg_Recorder
    {
        class Ffmpeg
        {
            Process process;
    
            public void Start(string FileName, int Framerate)
            {
                process = new System.Diagnostics.Process();
                process.StartInfo.FileName = @"i:\SavedScreenshots\ffmpeg.exe"; // Change the directory where ffmpeg.exe is.  
                process.EnableRaisingEvents = false;
                process.StartInfo.WorkingDirectory = @"i:\SavedScreenshots"; // The output directory  
                process.StartInfo.Arguments = @"-f gdigrab -framerate " + Framerate + " -i desktop -preset ultrafast -pix_fmt yuv420p " + FileName;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                Close();
            }
    
            public void Close()
            {
                process.Close();
            }
        }
    }
    


    In Form1

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Ffmpeg_Recorder
    {
        public partial class Form1 : Form
        {
            Ffmpeg ffmpeg = new Ffmpeg();
    
            public Form1()
            {
                var kh = new KeyboardHook(true);
                kh.KeyDown += Kh_KeyDown;
    
                InitializeComponent();
            }
    
            private void Kh_KeyDown(Keys key, bool Shift, bool Ctrl, bool Alt)
            {
                if (key.ToString() == "S")
                    ffmpeg.Start("Anytext.mp4", 24);
                if (key.ToString() == "T")
                    ffmpeg.Close();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
    

    I used a break point and when i click on the key S it's getting first to the Close method in the ffmpeg class not sure why.

    Second time when i click on the key T it's getting also to the Close method on the ffmpeg class.

    I see on the hard disk the video file but it's readable i can't open it and play it and ffmpeg.exe is keep working i see the ffmpeg.exe in the task manager working even after clicking on T.

    What i want to do is to click on S to start recording and click on T to end the recording.

    And without showing the ffmpeg console window. I made it so it will not show the console window but the recording is not good.

    I'm using this line in command prompt to record:

    ffmpeg -f gdigrab -framerate 24 -i desktop -preset ultrafast -pix_fmt yuv420p out.mp4

    Then clicking on Ctrl+C to stop the recording and it's working fine and making good video file.

    But i want to do it from the csharp winforms and without showing the ffmpeg console window.

    Wednesday, November 1, 2017 7:33 PM

All replies

  • I don't see a question. Do you have a question about the C# language? ffmpeg is not part of the C# language and I don't think the KeyboardHook class is either. This forum is only for questions about the C# language.

    I see that the program will create multiple ffmpeg processes and if the "S" key is pressed more than once without a "T" key then you will lose all but the last process, correct? I know that ffmpeg has an option to limit itself to one instance so that can make a difference.

    I don't know what the KeyboardHook class is but I suspect that key.ToString() is not the correct way to test for keys.



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, November 1, 2017 9:16 PM