C++ 为什么我不能在 openCV 中打开 avi 视频?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8414947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:32:11  来源:igfitidea点击:

Why can't I open avi video in openCV?

c++opencvimage-processingvideo-capture

提问by yvetterowe

I just wrote a simple video reading example with openCV2.3.1, but it seems that I cannot open avi video anyway :(

刚刚用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();

I made a breakpoint in std::cout<<"cannot read video!\n"and find that it stopped here every time. So why avi video cannot be opened? Thanks!

我在里面打了个断点std::cout<<"cannot read video!\n",发现每次都停在这里。那么为什么avi视频打不开呢?谢谢!

采纳答案by mevatron

1)
Make sure the video file is actually in the same folder as the application (I'm assuming you've already tried this), otherwise specify the absolute path.

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

2)
If you're on Windows, you may need a codec pack to read the video file (e.g., K-Lite Codec Pack).

2)
如果您使用的是 Windows,您可能需要一个编解码器包来读取视频文件(例如,K-Lite Codec Pack)。

As Macmade suggested, AVI is merely a container which can house different audio, video, or even closed caption codecs. Also, hereare Zeranoe's FFmpeg builds for Windows. You can get further information on your file's codec contents if you do the following:

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

ffmpeg -i guitarplaying.avi

You should see an output that looks like this:

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

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    :

So, as you can see this .mp4 container has a H.264 video codec and an AAC audio codec.

因此,正如您所看到的,这个 .mp4 容器有一个 H.264 视频编解码器和一个 AAC 音频编解码器。

回答by nimcap

A missing OpenCV's ffmpeg.dll does not generate any warnings/errors in OpenCV 2.3.1, and code fails silently. Make sure that you have proper opencv_ffmpeg*.dll in your path.

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

回答by windey1988

May be you load the wrong lib file.Just like me,I get the same problem.But I can open AVI file in VC6.0 which use the opencv version 1.0.Finally,I find that I use the wrong lib.

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

In Debug Mode,but I use opencv_core220.liband opencv_highgui220.lib.And I replace them with opencv_core220d.liband opencv_highgui220d.lib.Now I can read it.

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

回答by Ismax

In OpenCV 2.4.4 : there is only opencv_ffmpeg244.dll (release dll) and not opencv_ffmpeg244d.dll (debug dll)

在 OpenCV 2.4.4 中:只有 opencv_ffmpeg244.dll (release dll) 而没有 opencv_ffmpeg244d.dll (debug dll)

So try in release compilation mode !

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

回答by Andrew

I also encounter the same problem. With the sample code for VideoCapture, my visual studio program failed to open any video file. Then, the suggestion of nimcap works for me. "A missing OpenCV's ffmpeg.dll does not generate any warnings/errors in OpenCV 2.3.1, and code fails silently. Make sure that you have proper opencv_ffmpeg*.dll in your path."

我也遇到同样的问题。使用 VideoCapture 的示例代码,我的 Visual Studio 程序无法打开任何视频文件。然后,nimcap 的建议对我有用。“缺少 OpenCV 的 ffmpeg.dll 不会在 OpenCV 2.3.1 中生成任何警告/错误,并且代码会静默失败。确保您的路径中有正确的 opencv_ffmpeg*.dll。”

Solution: Copy opencv_ffmpeg.dll to my visual studio project /Debugfolder, I can open almost any video file with VideoCapture.

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

回答by Tim Long

Under Windows x64, you don't have to rename anything. Just put the following ...\opencv\build\x64\vc12\binin your PATH.

Windows x64 下,您无需重命名任何内容。只需将以下...\opencv\build\x64\vc12\bin放在您的PATH 中

回答by amhoes

Just change your code from this:

只需从此更改您的代码:

VideoCapture capture("guitarplaying.avi");

to this:

对此:

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

回答by Dane Bouchie

I'm just adding on to this since I spent more time than I'd like to admit. Not being able to open the file caused a ton of strange exceptions.

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

If you find that absolute paths work, while relative paths don't work, another thing to check is to make sure that the working directory set properly.

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

In Visual Studio this is Project Properties (Configuration Properties) -> Debugging -> Working Directory. I found mine set to "$(ProjectDir)" by default when I really wanted "$(OutDir)".

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

回答by Sahli Simo

If you are using Ubuntu 16+, you can try this:

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

pip install opencv-python

pip 安装 opencv-python

回答by razmik

Open D:\OpenCV\build\x64\vc14\binand copy the opencv_ffmpeg320.dllor opencv_ffmpeg320_64.dllwhich is relevant for you based on your Windows (x86 or x64).

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

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

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

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

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

Paste the copied .dllto C:\Python27\or any location which is accessible from Windows PATHenvironment variables.

粘贴复制.dll到的C:\Python27\或可从 WindowsPATH环境变量访问的任何位置。

Alternatively, you could create a folder D:\OpenCV\vendor\and add .dllfile to that folder and then add D:\OpenCV\vendor\to your windows environment variables.

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