Ask Your Question
0

How do I display two or more videos using opencv?

asked 2014-06-13 12:17:35 -0600

updated 2014-06-14 15:10:26 -0600

How do I display two or more videos with different lenghts (1,2 mins) using opencv?

int main(){

/* Open the Video File for Reading */
VideoCapture cap("CarSurveillance/Video1.avi"); // 00:00:33
VideoCapture cap2("CarSurveillance/Video2.mp4"); // 00:01:12

if (!cap.isOpened()){
    cout << "Was not possible to Open the Video" << endl;
    cout << "Reasons:" << endl;
    cout << "1st: Wrong PATH" << endl;
    cout << "2nd: Wrong Extension" << endl;
    cout << "3rt: Unknown Reasons" << endl;
    system("PAUSE");
    exit(EXIT_FAILURE);
}
if (!cap2.isOpened()){
    cout << "Was not possible to Open the Video" << endl;
    cout << "Reasons:" << endl;
    cout << "1st: Wrong PATH" << endl;
    cout << "2nd: Wrong Extension" << endl;
    cout << "3rt: Unknown Reasons" << endl;
    system("PAUSE");
    exit(EXIT_FAILURE);
}
namedWindow("Video1", CV_WINDOW_AUTOSIZE); 
bool video1WindowDestroyed = false;
bool video2WindowDestroyed = false;
namedWindow("Video2", CV_WINDOW_AUTOSIZE);
double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Video 1 Frame per seconds : " << fps << endl;
fps = cap2.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Video 2 Frame per seconds : " << fps << endl;
Mat frame, frame2;
cap.read(frame); // read a new frame from video
cap2.read(frame); // read a new frame from video

while (true){

    if ((video1WindowDestroyed == false) && (cap.read(frame) == true)){
        imshow("Video1", frame);
        if (cap.read(frame) == false){
            destroyWindow("Video1");
            video1WindowDestroyed = true;
        }
    }
    if ((video2WindowDestroyed == false) && (cap2.read(frame2) == true)){
        imshow("Video2", frame2);
        if (cap2.read(frame2) == false){
            destroyWindow("Video2");
            video2WindowDestroyed = true;
        }
    }

    if (video2WindowDestroyed == true && video2WindowDestroyed == true){
        break;
    }
    waitKey(30);
}
waitKey(0);
system("PAUSE");
return 0;

}

when first video ends he is slow to close the window and pause the second video, two seconds after restart

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-06-13 16:31:26 -0600

unxnut gravatar image

updated 2014-06-14 17:02:23 -0600

Are you trying to destroy the window that has already been destroyed. You will be better off setting a flag for each of the videos that will indicate that the video has finished. Then, you do not update the completed video any more, or try to read it.

Here is the code that works:

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

const std::string VIDEO1 = "plane-test-video.mov";
const std::string VIDEO2 = "xfactor2012.avi";
const unsigned char ESC = 27;

int main()
{
    try
    {
        cv::VideoCapture video1 ( VIDEO1 );
        if ( ! video1.isOpened() )
            throw ( std::string ( "Could not open " ) + VIDEO1 );

        cv::VideoCapture video2 ( VIDEO2 );
        if ( ! video2.isOpened() )
            throw ( std::string ( "Could not open " ) + VIDEO2 );

        double fps1 = video1.get ( CV_CAP_PROP_FPS );
        double fps2 = video2.get ( CV_CAP_PROP_FPS );
        double frame_delay1 = 1000 / fps1;
        double frame_delay2 = 1000 / fps2;
        int frame_delay = static_cast<int>( std::max ( frame_delay1, frame_delay2 ) );

        // Open display windows

        cv::namedWindow ( "Video1" );
        cv::namedWindow ( "Video2" );

        // Read and display videos

        bool vid1_on = true;
        bool vid2_on = true;
        cv::Mat frame1, frame2;

        while ( true )
        {
            if ( vid1_on )
            {
                if ( vid1_on = video1.read ( frame1 ) )
                    cv::imshow ( "Video1", frame1 );
                else
                    cv::destroyWindow ( "Video1" );
            }

            if ( vid2_on )
            {
                if ( vid2_on = video2.read ( frame2 ) )
                    cv::imshow ( "Video2", frame2 );
                else
                    cv::destroyWindow ( "Video2" );
            }

            char ch = cv::waitKey ( frame_delay );

            if ( ! ( vid1_on || vid2_on ) || ( ch == ESC ) )
                break;
        }


        return ( 0 );
    }
    catch ( std::string& str )
    {
        std::cerr << "Error: " << str << std::endl;
        return ( 1 );
    }
    catch ( cv::Exception e )
    {
        std::cerr << "Exception from OpenCV: " << e.msg << std::endl;
        return ( 1 );
    }
}
edit flag offensive delete link more

Comments

Hello @unxnut Now I'm using flags, but now I have a smaller Problem!

ricardo99 gravatar imagericardo99 ( 2014-06-14 15:11:53 -0600 )edit

Something not right with your logic. In the if block, you check cap.read(frame) == true and inside the block, you check cap.read(frame) == false to set the flag and destroy the window. Notice that each time you perform the read, you are moving to next frame in the video.

unxnut gravatar imageunxnut ( 2014-06-14 16:08:59 -0600 )edit

after reading a frame and it shows I check the next frame if you have not I make video2WindowDestroyed = true then next time do not enter in the first block if (&& if first is false will not read the (cap.read (frame) == true ))

ricardo99 gravatar imagericardo99 ( 2014-06-14 17:03:16 -0600 )edit

I have added the code in the answer. Let me know if there is still problem.

unxnut gravatar imageunxnut ( 2014-06-14 17:04:27 -0600 )edit

Still, in your test worked perfectly?

ricardo99 gravatar imagericardo99 ( 2014-06-14 17:25:26 -0600 )edit

Yes, what problem have you encountered?

unxnut gravatar imageunxnut ( 2014-06-14 17:43:10 -0600 )edit

the same, my videos have 2.5MB and 2.2MB

ricardo99 gravatar imagericardo99 ( 2014-06-14 17:45:55 -0600 )edit

other informations: Win 7 64 bits, opencv 2.4.8, Visual studio 2013 with update 2

ricardo99 gravatar imagericardo99 ( 2014-06-14 18:43:07 -0600 )edit

Hello @unxnut, Thank you very much for your help.

ricardo99 gravatar imagericardo99 ( 2014-06-16 11:08:43 -0600 )edit

Question Tools

Stats

Asked: 2014-06-13 12:17:35 -0600

Seen: 333 times

Last updated: Jun 14 '14