Ask Your Question

Revision history [back]

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.

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 );
    }
}