Ubuntu: VideoCapture fails to open local avi/mp4 video

Recently, I was doing yolov5 video detection. I wanted to test the local video file first, but I encountered an error:

failed to open video ../vtest.avi
[ERROR:0] VIDEOIO(cvCreateFileCapture_Images(filename.c_str())): raised OpenCV exception:

OpenCV(3.4.12) /home/xxx/Downloads/opencv-3.4.12/modules/videoio/src/cap_images.cpp:246: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): ../vtest.avi in function 'icvExtractPattern'

[ Directly put the conclusion ]: The problem is that when compiling opencv, although ffmpeg is checked, it may be that my ubuntu system itself has not installed ffmpeg or the version of ffmpeg is too low, resulting in the compiled opencv library. Failed to decode, so the video cannot be played. The solution is to install ffmpeg first, and then recompile OpenCV.

The following is the complete thinking process, you can skip it if you don’t need it...
I thought it was not a big problem, maybe the path is wrong. In order to be in place in one step, I simply use the absolute path of the video directly:

/home/xx/videos/vtest.avi

It turned out to be an error. Apparently things are not that simple:

insert image description here
So I tried to search the Internet (baidu/google/stackoverflow/github)
after a fierce operation, but I still haven't found a solution to the problem.

So, I carefully reviewed the current situation:

  1. The operating system is ubtunu, and I have never encountered a similar situation in win10 before
  2. OpenCV is compiled by myself and tested, and other libraries and APIs are used normally
  3. VideoCapture opens the local USB Camera no problem

In this way, the reason why the header file or library file path is wrong when opencv is called is ruled out. And there is no problem with VideoCapture opening the camera, indicating that the api should also be no problem. But why does it report an error when opening the local video?

At this time, I suddenly remembered: the playback of the video actually needs to be decoded first , and only after decoding can it be played. My problem here is most likely due to a decoding failure.

The decoding related to OpenCV uses ffmpeg, so I thought of installing ffmpeg in the ubuntu system:

sudo apt update
sudo apt install ffmpeg

【Full of confidence, try again】

failed to open video ../vtest.avi
[ERROR:0] VIDEOIO(cvCreateFileCapture_Images(filename.c_str())): raised OpenCV exception:

OpenCV(3.4.12) /home/xxx/Downloads/opencv-3.4.12/modules/videoio/src/cap_images.cpp:246: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): ../vtest.avi in function 'icvExtractPattern'

The problem is still...

insert image description here
[Thinking again]: When OpenCV compiles, it will package the required 3rd library, such as ffmpeg, into its own library, such as libopencv_world.so.3.4, so that after the compilation is successful, it will no longer depend on the 3rd library. Although I installed ffmpeg at the ubuntu system level at this time, it didn't help. When the code is running, the old version of OpenCV is actually called, and it still does not support many video encoding methods.

So, I was cruel and compiled OpenCV again:

insert image description here

After waiting patiently for a period of time, the compilation was finally successful. Then quickly test to see if the problem has been solved:

/home/pl/projects/yolov5-trt-deepsort-camera/cmake-build-debug/TrackCounter -v vtest.avi
vtest.avi
fps[0], 
Preprocess Time: 8ms
Detect Time: 7ms

Apparently the problem was resolved successfully.

Summary: Looking back, it seems that the problem is not difficult, but when I encountered it at the time, it took a lot of thinking.
Encounter a problem -> try to solve -> fail -> try to solve -> succeed

Although this process is very distressing, the moment the problem is solved, the mood is still very good, and the ability is improving step by step.

Supongo que te gusta

Origin blog.csdn.net/opencv_yys/article/details/117332027
Recomendado
Clasificación