OpenCV trying to read or write to video file causes VIDEOIO exception "Can't find starting number" (icvExtractPattern)

Viewed 10157

So for a school project, I am trying to use cv::VideoCapture to open a .avi file to perform some image processing on it. Also, I am trying to record the video from my (Laptop) camera and save it to a file out.avi. Both times I encountered more or less the same exception. I am using OpenCV 4.1.2 and CLion on Linux Mint.

Trying to use cv::VideoCapture vid(0); works perfectly fine, it shows the output from the Laptop's camera. However, when specifying a path to a video file to open, I get the following error:

VIDIOC_REQBUFS: Inappropriate ioctl for device
[ERROR:0] global /home/aris/dev/opencv/modules/videoio/src/cap.cpp (116) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.1.2-dev) /home/aris/dev/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): ../example_depth.avi in function 'icvExtractPattern'

When trying to create a cv::VideoWriter object (to save the camera video ouput to a file) using:

cv::VideoWriter vidWrit("out.avi", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 10, cv::Size(width, height), true);

I am encountering this error:

[ERROR:0] global /home/aris/dev/opencv/modules/videoio/src/cap.cpp (392) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.1.2-dev) /home/aris/dev/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): out.avi in function 'icvExtractPattern'

Which looks kind of similar to the other one.

Here is a minimal example (note that the program still shows the camera output in task == "write"):

#include <opencv2/opencv.hpp>

int main ()
{
    std::string task = "write";
    if(task == "read") {    // Read from .avi file
        system("pwd");  // Print current path
        cv::VideoCapture vid("../example_depth.avi");   // Throws first exception "Inappropriate ioctl for device"

        int key = 0;
        cv::Mat frame;
        while (key != 27) {
            vid >> frame;
            if (frame.empty())
                break;

            cv::imshow("Video", frame);
            key = cv::waitKey(25);
        }
    } else if (task == "write") {   // Write Laptop video to file
        cv::VideoCapture vid(0);    // Video from camera

        int width = vid.get(cv::VideoCaptureProperties::CAP_PROP_FRAME_WIDTH);
        int height = vid.get(cv::VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT);
        cv::VideoWriter vidWriter("out.avi", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 10, cv::Size(width, height), true);   // Throws second exception

        int key = 0;
        cv::Mat frame;
        while (key != 27) {
            vid >> frame;
            if (frame.empty())
                break;
            vidWriter.write(frame);

            cv::imshow("Video", frame);
            key = cv::waitKey(25);
        }
    }
    return 0;
}

I've been looking for a solution for two days now. Maybe I am missing some library or OpenCV didn't get installed correctly, I don't know about that (yes, I've tried 'recompiling' OpenCV). I used this tutorial to make all the OpenCV files.

2 Answers

Did the file you want to write to (out.avi) already exist?

I got this error with opencv-python==4.1.2.30 and changing the output to a filepath that did not exist solved it for me.

I've seen this issue a couple of times. First check if the path to the video is correct. In your case, "out.avi" should exist. So this is probably is a dependency or conflict issue. My speculation is that this error occurs when a backend video encoder library conflicts with OpenCV. Try reinstalling the libraries. I suggest you follow the instructions from the official documentation, and open an issue if you keep experiencing problems.

Related