为什么我无法在 openCV 中打开 avi 视频?

Why can't I open avi video in openCV?

本文关键字:avi 视频 openCV 为什么      更新时间:2023-10-16

我刚刚用openCV2.3.1写了一个简单的视频阅读示例,但似乎我无法打开avi视频:(

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"中设置了一个断点,发现它每次都停在这里。那么为什么avi视频不能打开呢?谢谢!

缺失的OpenCV的ffmpeg.dll在OpenCV 2.3.1中不会产生任何警告/错误,代码静默失败。确保路径中有正确的opencv_ffmpeg*.dll。

1)
请确保视频文件实际上与应用程序位于同一文件夹中(我假设您已经尝试过了),否则请指定绝对路径。

2)
如果你在Windows上,你可能需要一个编解码器包来读取视频文件(例如,K-Lite编解码器包)。

正如Macmade所建议的,AVI只是一个容器,它可以容纳不同的音频、视频,甚至是封闭的字幕编解码器。此外,这里是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 '....VideosSintelsintel_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.264视频编解码器和一个AAC音频编解码器。

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

在调试模式下,但我使用opencv_core220.libopencv_highgui220.lib。我用opencv_core220d.libopencv_highgui220d.lib代替它们。现在我可以阅读了。

在OpenCV 2.4.4中:只有opencv_ffmpeg244.dll(发布dll)而没有opencv_ffmpeg244d.dll(调试dll)

我也遇到了同样的问题。使用videoccapture的示例代码,我的visual studio程序无法打开任何视频文件。那么,nimcap的建议对我有用。缺失的OpenCV的ffmpeg.dll在OpenCV 2.3.1中不会产生任何警告/错误,代码会静默失败。确保路径中有适当的openv_ffmpeg *.dll。"

解决方案:复制opencv_ffmpeg.dll到我的visual studio项目/Debug文件夹,我可以用videoccapture打开几乎任何视频文件。

Windows x64下,您不必重命名任何内容。只需输入下面的… opencv 制造 x64 vc12 bin 中的路径

我只是补充一下,因为我花了比我愿意承认的更多的时间。无法打开文件导致大量奇怪的异常。

如果你发现绝对路径有效,而相对路径不起作用,另一件要检查的事情是确保工作目录设置正确。

在Visual Studio中,这是Project Properties (Configuration Properties) -> Debugging -> Working Directory。我发现我的默认设置为"$(ProjectDir)",而我真正想要的是"$(OutDir)"。

对于没有找到解决方案的人:在openCV4和"debug"

进入opencvbuildx64vc15bin

复制"opencv_videoio_ffmpeg440_64.dll"到项目的调试文件夹

你只需要从xvid.org下载Xvid就可以了:http://www.xvid.org/Downloads.15.0.html

修改你的代码:

VideoCapture capture("guitarplaying.avi");

to this:

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

打开D:OpenCVbuildx64vc14bin,根据Windows (x86 or x64)复制与您相关的opencv_ffmpeg320.dllopencv_ffmpeg320_64.dll

opencv_ffmpeg320.dll -> for 32-bit Windows (x86)

opencv_ffmpeg320_64.dll -> for 64-bit Windows (x64)

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

或者,您可以创建一个文件夹D:OpenCVvendor,并将.dll文件添加到该文件夹,然后将D:OpenCVvendor文件添加到windows environment variables

如果你使用的是Ubuntu 16+,你可以试试:

安装opencv-python