chembrad
chembrad

Reputation: 885

Can I create a virtual webcam and stream data to it?

I am looking to stream a video from ffmpeg to OpenCV (a video manipulation library) and I am stumped. My idea is to create a virtual webcam device and then stream a video from ffmpeg to this device and the device will in turn stream like a regular webcam. My motivation is for OpenCV. OpenCV can read in a video stream from a webcam and go along its merry way.

But is this possible? I know there is software to create a virtual webcam, but can it accept a video stream (like from ffmpeg) and can it stream this video like a normal webcam? (I am working in a cygwin environment , if that is important)

Upvotes: 3

Views: 5001

Answers (2)

SSteve
SSteve

Reputation: 10728

You don't need to fool OpenCV into thinking the file is a webcam. You just need to add a delay between each frame. This code will do that:

#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;

int main(int argc, const char * argv[]) {

    VideoCapture cap; 
    cap.open("/Users/steve/Development/opencv2/opencv_extra/testdata/python/videos/bmp24.avi");
    if (!cap.isOpened()) {
        printf("Unable to open video file\n");
        return -1;
    }
    Mat frame; 
    namedWindow("video", 1); 
    for(;;) {
        cap >> frame; 
        if(!frame.data) 
            break; 
        imshow("video", frame); 
        if(waitKey(30) >= 0) //Show each frame for 30ms
            break;
    }

    return 0;
}

Edit: trying to read from a file being created by ffmpeg:

    for(;;) {
        cap >> frame; 
        if(frame.data) 
            imshow("video", frame); //show frame if successfully loaded
        if(waitKey(30) == 27) //Wait 30 ms. Quit if user presses escape 
            break;
    }

I'm not sure how it will handle getting a partial frame at the end of the file while ffmpeg is still creating it.

Upvotes: 1

Brian L
Brian L

Reputation: 3251

Sounds like what you want is VideoCapture::Open, which can open both video devices and files.

If you're using C, the equivalents are cvCaptureFromFile and cvCaptureFromCAM.

Upvotes: 0

Related Questions