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

yve*_*owe 21 c++ opencv video-capture image-processing

我刚刚用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();
Run Code Online (Sandbox Code Playgroud)

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

nim*_*cap 54

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


mev*_*ron 5

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

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

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

ffmpeg -i guitarplaying.avi
Run Code Online (Sandbox Code Playgroud)

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

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    :
Run Code Online (Sandbox Code Playgroud)

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