Search code examples
c#webcamvideo-capturerecordingframe-rate

Does anyone know a way to capture webcam video at a constant framerate?


OK, so I have been having a bit of a tough time with webcam capture, and am in need of help to find a way to capture the video at a consistent frame rate.

I have been using Aforge.AVIWriter and Aforge.VideoFileWriter, but to no avail, and have also typed any related phrase I can think of into Google.

I have had a look at the DirectShowLib, but am yet to find it any more accurate.

The video must have a minimum frame rate of 25fps, it is also to be shown in sync with other data which is collected at the same time.

I have also tried an infinite loop:

        for (; ; )
        {
            if (recvid == false)
            {
                break;
            }
            if (writer.IsOpen)
            {
                Bitmap image = (Bitmap)videoSourcePlayer1.GetCurrentVideoFrame();
                if (image != null)
                {
                    writer.WriteVideoFrame(image);
                }
                Thread.Sleep(40);
            }


        }

Even though this is more accurate for timing, the user can see that the fps changes when they watch the video and view data at the same time.

Any pointers or tips would be greatly appreciated, as I cannot think of a way to go from here.


Solution

  • two main issues that i can see:

    writer.write() is it happening in a seperate thread? if not it will take time and hence the timing might not be accurate.

    second thread.sleep() says that sleep for at-least 40 ms not exactly 40 ms.. to get better results reduce the wait time to 5 ms and do it in a loop.. use the systems time to actually figure out how long you have slept for and then take a frame capture.

    Hope this helps