opencv: VideoCapture fails when compiled with FFMPEG > 1.2.12 (OpenCV 2.4.11)
I’m working with Hollywood-2 video dataset, an example video actioncliptrain00002.avi
is at my OneDrive: http://1drv.ms/1Mxprmh.
I do sth like:
#include <cstdio>
#include <opencv2/highgui/highgui.hpp>
int main()
{
cv::VideoCapture in("actioncliptrain00002.avi");
cv::Mat frame;
for(int i = 1; ; i++)
{
bool ok = in.read(frame);
printf("%d: ok = %s, rows = %d, cols = %d\n", i, ok ? "true" : "false", frame.rows, frame.cols);
if(!ok)
{
printf("Not ok. Breaking.\n");
break;
}
}
}
If compiled with ffmpeg 1.2.12, it outputs:
1: ok = true, rows = 224, cols = 528
2: ok = true, rows = 224, cols = 528
............
241: ok = true, rows = 224, cols = 528
242: ok = false, rows = 0, cols = 528
Not ok. Breaking.
If compiled with any ffmpeg >= 2.0.7 (including the latest 2.7.2), it outputs:
[mpeg4 @ 0x169ce40] header damaged
[mpeg4 @ 0x16ded60] Context scratch buffers could not be allocated due to unknown size.
<more ffmpeg errors just like above>
1: ok = false, rows = 0, cols = 0
Not ok. Breaking.
My OpenCV/ffmpeg installation script is here: http://pastebin.com/KD4YWpg5
Both ffmpeg 1.2.12 and 2.7.2 are able to do ffmpeg -i actioncliptrain00002.avi dump/%06d.png
without any errors, which makes me think it’s an OpenCV bug in the way it talks to ffmpeg. I have a stable reproducing setup and am happy to provide any further information or help, if needed.
The full Hollywood-2 video dataset is available at ftp://ftp.irisa.fr/local/vistas/actions/Hollywood2-actions.tar.gz
About this issue
- Original URL
- State: open
- Created 9 years ago
- Comments: 15 (5 by maintainers)
I had a related problem. This is how I fixed it: If you compile your own ffmpeg, and you follow the compilation guide on their website (eg. https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu) then you end up with an incomplete installation. First of all they are missing the dependency
libsdl2-dev
. This is needed for some things and for example ffplay will not build without it, so install it withsudo apt-get install libsdl2-dev
Also make sure that any dependencies that you compiled yourself for ffmpeg have the enable shared option. I had to compile libx265 so for that step I changed-DENABLE_SHARED:bool=off
to-DENABLE_SHARED:bool=on
on the cmake line for building libx265. I also compiled libvpx, so for that part, I added--enable-shared
to the options on the./configure
line while building libvpx. Here’s the part most relevent to you @vadimkantorov: when you compile ffmpeg, add--enable-avresample
or else it will not build libavresample. also add--enable-shared
to build all the shared libraries. Next, opencv cmake uses pkg-config to find your libraries. If you built ffmpeg as in the compilation guide, then you will end up with the .pc files in something like /home/YOUR-USER-NAME/ffmpeg_build/lib/pkgconfig. But pkg-config won’t find it there during the the opencv cmake. So find the filelibavresample.pc
and set the PKG_CONFIG_PATH environment variable like for exampleexport PKG_CONFIG_PATH=/home/YOUR-USER-NAME/ffmpeg_build/lib/pkgconfig
Now when you run the cmake for opencv, it will find the libavresample. Make sure you also add-D WITH_FFMPEG=ON
to your cmake command while building opencv.The differences without libavresample being included may be subtle. In my case without libavresample, eveything worked except opening some mjpeg streams from an IP camera with cv2.VideoCapture, even though rtsp streams from the same camera and other mjpeg streams did work. With libavresample installed properly before compiling opencv all of IP cam streams I tested worked.