Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not demultiplex stream" in loading Video recorded by Opencv's VideoWriter

Tags:

c++

opencv

ffmpeg

My program is

int main(){
    cout << "Start the process" << endl;
    cv::VideoCapture vcap("rtsp://root:[email protected]/axis-media/media.amp?camera=1");
    cout << "Camera connection done!" << endl;
    cv::Mat image, small;
    //Output video
    cv::Size S = cv::Size((int) vcap.get(CV_CAP_PROP_FRAME_WIDTH), (int) vcap.get(CV_CAP_PROP_FRAME_HEIGHT));
    int ex = static_cast<int>(vcap.get(CV_CAP_PROP_FOURCC));
    int fps = vcap.get(CV_CAP_PROP_FPS);
    cout << "fps " << fps << " ex " << ex << endl;
    cv::VideoWriter outputVideo;
    outputVideo.open("TEST.avi", ex/*CV_FOURCC('X', '2', '6', '4')*/, vcap.get(CV_CAP_PROP_FPS), S, true);
    if(!outputVideo.isOpened()){
        cout << "Could not open the output video for write" << endl;
        return -1;
    }

    for(;;){
        if(!vcap.read(image)){
            std::cout << "No frame" << std::endl;
            cv::waitKey(0);
        }

        cv::resize(image, small, image.size()/2, 0, 0 , cv::INTER_LINEAR);
        cv::imshow("Display", small);
        cv::waitKey(1);
        outputVideo.write(small);
        if(getkey() == '\n')
            break;
    }
    cout << "Camera release" << endl;
    outputVideo.release();
    vcap.release();
    image.release();
    small.release();
    return 0;
}

int ex = static_cast<int>(vcap.get(CV_CAP_PROP_FOURCC)); ex is 0 here.

I can record the TEST.avi, but can't be read by cv::VideoCapture vcap("TEST.avi"); or VLC player or Videos in Ubuntu. The error is "Could not demultiplex stream".

If I changed to

outputVideo.open("TEST.avi", CV_FOURCC('X', '2', '6', '4'), vcap.get(CV_CAP_PROP_FPS), S, true);
outputVideo.open("TEST.avi", CV_FOURCC('P','I','M','1'), vcap.get(CV_CAP_PROP_FPS), S, true);
outputVideo.open("TEST.avi", CV_FOURCC('M', 'P', '4', '2'), vcap.get(CV_CAP_PROP_FPS), S, true);
etc.

all have same problem.

If I set

outputVideo.open("TEST.avi", CV_FOURCC('i', 'Y', 'U', 'V'), vcap.get(CV_CAP_PROP_FPS), S, true);

I have error as Opencv: FFMPEG iYUV is not supported with codec id 14

For

outputVideo.open("TEST.avi", CV_FOURCC('M', 'J', 'P', 'G'), vcap.get(CV_CAP_PROP_FPS), S, true);


OpenCV Error: Assertion failed (img.cols == width && img.rows == height && chann
els == 3) in write, file /home/Softwares/opencv/opencv/modules/videoio/src/
cap_mjpeg_encoder.cpp, line 829
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/Softwares/opencv/opencv/modules/videoio/src/cap_mjpeg_enco
der.cpp:829: error: (-215) img.cols == width && img.rows == height && channels =
= 3 in function write

What could be wrong? Is that my FFMPEG has problem?

like image 738
batuman Avatar asked Jun 16 '16 05:06

batuman


2 Answers

Get the size. In my case I did this.

`size = images[0].shape[:2]`

This gave the error when tried to play record.

`out = cv2.VideoWriter(record_path,fourcc, 15, size)`

I tried this one and it is worked.

`out = cv2.VideoWriter(record_path,fourcc, 15, (size[1],size[0]))`
like image 177
GolD. Avatar answered Nov 04 '22 10:11

GolD.


If you are reading a video from a file and resizing it before displaying the frame using cv2.imshow. Make sure to keep the "out" dimension equivalent to the dimension of the resized frame.

like image 3
Manoj Avatar answered Nov 04 '22 12:11

Manoj