加载 Opencv 的 VideoWriter 录制的视频时出现“无法解复用流”

bat*_*man 7 c++ opencv ffmpeg

我的程序是

int main(){
    cout << "Start the process" << endl;
    cv::VideoCapture vcap("rtsp://root:pass@192.168.0.90/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;
}
Run Code Online (Sandbox Code Playgroud)

int ex = static_cast<int>(vcap.get(CV_CAP_PROP_FOURCC)); ex 在这里是 0。

我可以录制 TEST.avi,但无法通过 cv::VideoCapture vcap("TEST.avi"); 读取 或 VLC 播放器或 Ubuntu 中的视频。错误是"Could not demultiplex stream"

如果我改为

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.
Run Code Online (Sandbox Code Playgroud)

都有同样的问题。

如果我设置

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

我有错误 Opencv: FFMPEG iYUV is not supported with codec id 14

为了

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
Run Code Online (Sandbox Code Playgroud)

可能有什么问题?那是我的 FFMPEG 有问题吗?

小智 3

获取尺寸。就我而言,我就是这样做的。

`size = images[0].shape[:2]`
Run Code Online (Sandbox Code Playgroud)

当尝试播放录音时,这会出现错误。

`out = cv2.VideoWriter(record_path,fourcc, 15, size)`
Run Code Online (Sandbox Code Playgroud)

我尝试了这个,它成功了。

`out = cv2.VideoWriter(record_path,fourcc, 15, (size[1],size[0]))`
Run Code Online (Sandbox Code Playgroud)