为什么我不能在openCV中打开AVIvideo?

我刚刚写了一个简单的video阅读与openCV2.3.1的例子,但似乎无法打开AVIvideo:(

VideoCapture capture("guitarplaying.avi"); if(!capture.isOpened()){ std::cout<<"cannot read video!\n"; return -1; } Mat frame; namedWindow("frame"); double rate = capture.get(CV_CAP_PROP_FPS); int delay = 1000/rate; while(true) { if(!capture.read(frame)){ break; } imshow("frame",frame); if(waitKey(delay)>=0) break; } capture.release(); 

我在std::cout<<"cannot read video!\n"做了一个断点std::cout<<"cannot read video!\n"并且发现它每次都停在这里。 那么,为什么AVIvideo无法打开? 谢谢!

1)
确保video文件实际上与应用程序在同一个文件夹中(我假设你已经试过了),否则指定绝对path。

2)
如果您在Windows上,则可能需要使用编解码器包来读取video文件(例如, K-Lite编解码器包 )。

正如Macmade所build议的,AVI只是一个容纳不同audio,video甚至是隐藏字幕编解码器的容器。 此外, 这里是Zeranoe的Windows的FFmpeg版本。 如果您执行以下操作,可以获得有关文件编解码器内容的更多信息:

 ffmpeg -i guitarplaying.avi 

您应该看到如下所示的输出:

 ffmpeg version 0.8.7.git, Copyright (c) 2000-2011 the FFmpeg developers built on Dec 6 2011 09:20:43 with gcc 4.6.1 configuration: --pkg-config=pkg-config --enable-gpl --enable-version3 --enable -nonfree --enable-runtime-cpudetect --enable-memalign-hack --enable-postproc --a rch=x86 --target-os=mingw32 --cross-prefix=i686-w64-mingw32- --prefix=/home/wluc as/ffmpeg-cross/build/deploy --enable-libx264 --enable-libvpx --enable-zlib --en able-bzlib --enable-libxvid --enable-libfaac --enable-libmp3lame --enable-libvor bis --enable-libtheora --enable-libopenjpeg --enable-libfreetype libavutil 51. 30. 0 / 51. 30. 0 libavcodec 53. 40. 0 / 53. 40. 0 libavformat 53. 24. 0 / 53. 24. 0 libavdevice 53. 4. 0 / 53. 4. 0 libavfilter 2. 51. 0 / 2. 51. 0 libswscale 2. 1. 0 / 2. 1. 0 libpostproc 51. 2. 0 / 51. 2. 0 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '..\..\Videos\Sintel\sintel_trailer-720p .mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 creation_time : 1970-01-01 00:00:00 title : Sintel Trailer artist : Durian Open Movie Team encoder : Lavf52.62.0 copyright : (c) copyright Blender Foundation | durian.blender.org description : Trailer for the Sintel open movie project Duration: 00:00:52.20, start: 0.000000, bitrate: 1165 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 1033 kb/s, 24 fps, 24 tbr, 24 tbn, 48 tbc Metadata: creation_time : 1970-01-01 00:00:00 handler_name : VideoHandler Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, s16, 126 kb/s Metadata: creation_time : 1970-01-01 00:00:00 handler_name : 

所以,你可以看到这个.mp4容器有一个H.264video编解码器和一个AACaudio编解码器。

缺lessOpenCV的ffmpeg.dll不会在OpenCV 2.3.1中产生任何警告/错误,并且代码会自动失败。 确保你的path中有正确的opencv_ffmpeg * .dll。

可能是你加载了错误的lib文件。就像我一样,我得到了同样的问题。但是我可以在VC6.0中使用opencv版本1.0打开AVI文件。最后,我发现我使用了错误的lib。

在debugging模式下,但是我使用opencv_core220.libopencv_highgui220.lib 。并且用opencv_core220.libopencv_highgui220.libreplace它们。现在我可以读取它了。

在OpenCV 2.4.4中:只有opencv_ffmpeg244.dll(释放dll)而不是opencv_ffmpeg244d.dll(debuggingdll)

所以尝试在发布编译模式!

我也遇到同样的问题。 有了VideoCapture的示例代码,我的Visual Studio程序无法打开任何video文件。 那么,nimcap的build议是适合我的。 “缺lessOpenCV的ffmpeg.dll在OpenCV 2.3.1中不会产生任何警告/错误,并且代码会自动失败,请确保您的path中有正确的opencv_ffmpeg * .dll。

解决scheme:将opencv_ffmpeg.dll复制到我的Visual Studio项目/Debug文件夹,我可以用VideoCapture打开几乎任何video文件。

Windows x64下 ,您不必重命名任何东西。 只需在PATH中放入以下… \ opencv \ build \ x64 \ vc12 \ bin

只需从这里改变你的代码:

 VideoCapture capture("guitarplaying.avi"); 

对此:

 CvCapture *input_video = cvCreateFileCapture("guitarplaying.avi"); 

我只是补充一点,因为我花了比我想承认的更多的时间。 无法打开文件导致了大量奇怪的例外。

如果你发现绝对path工作,而相对path不起作用,另一个要检查的是确保工作目录正确设置。

在Visual Studio中,这是项目属性(configuration属性) – >debugging – >工作目录。 当我真的想要“$(OutDir)”时,我发现我的默认设置为“$(ProjectDir)”。

您需要从xvid.org下载您的机器Xvid: http ://www.xvid.org/Downloads.15.0.html

打开D:\OpenCV\build\x64\vc14\bin并根据您的Windows (x86 or x64)复制与您相关的opencv_ffmpeg320.dllopencv_ffmpeg320_64.dll

opencv_ffmpeg320.dll – >用于32-bit Windows (x86)

opencv_ffmpeg320_64.dll – >对于64-bit Windows (x64)

将复制的.dll粘贴到C:\Python27\或从Windows PATH环境variables可访问的任何位置。

或者,您可以创build一个文件夹D:\OpenCV\vendor\然后将.dll文件添加到该文件夹​​,然后将D:\OpenCV\vendor\到您的windows environment variables