23

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

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!

Amaury Medeiros
  • 1,980
  • 3
  • 21
  • 39
yvetterowe
  • 1,179
  • 6
  • 18
  • 32
  • 1
    AVI is a file format, not a video format. So you should check that the video is encoded in a supported video format, as an AVI file may be encoded in different ways. – Macmade Dec 07 '11 at 12:06

12 Answers12

55

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.

nimcap
  • 9,276
  • 14
  • 56
  • 67
5

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.

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

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

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.

mevatron
  • 13,381
  • 3
  • 51
  • 68
  • 1
    To expand on point 1: people often make the mistake of putting the image file (avi, jpg, etc.) in the same folder as their source code instead of the same folder as the compiled program. – SSteve Dec 07 '11 at 19:10
  • This describes the problem, but not the solution. @nimcap's answer explains how to fix it concisely and accurately. – Johannes Brodwall Aug 10 '16 at 20:23
  • 1
    I copy the opencv_ffmpeg310.dll, opencv_ffmpeg310x64.dll and the problem was solved. Thank you so much. – mesutpiskin Oct 06 '16 at 18:31
4

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.

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

Jav_Rock
  • 21,011
  • 18
  • 115
  • 164
windey1988
  • 41
  • 1
1

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

So try in release compilation mode !

Ismax
  • 31
  • 1
  • 1
    I've had the same missing DLL problem. The "release" version is working for me in both debug and release modes. – cbuchart Nov 05 '13 at 14:00
1

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."

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

Danh
  • 5,455
  • 7
  • 26
  • 41
Andrew
  • 31
  • 1
0

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

Tim Long
  • 1,768
  • 1
  • 19
  • 24
0

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)".

Dane Bouchie
  • 351
  • 4
  • 10
0

For those who didn't find the solution: In openCV4 and "debug" mode follow these steps:

Go to opencv\build\x64\vc15\bin

Copy "opencv_videoio_ffmpeg440_64.dll" to your debug folder of your project.

vahid sabet
  • 120
  • 2
  • 11
-1

You need to just download for your machine Xvid from xvid.org that's all: http://www.xvid.org/Downloads.15.0.html

mor.ch
  • 7
  • 1
-1

Just change your code from this:

VideoCapture capture("guitarplaying.avi");

to this:

CvCapture *input_video = cvCreateFileCapture("guitarplaying.avi");
Caleb Brinkman
  • 2,309
  • 3
  • 20
  • 38
amhoes
  • 1
-1

Open D:\OpenCV\build\x64\vc14\bin and copy the opencv_ffmpeg320.dll or opencv_ffmpeg320_64.dll which is relevant for you based on your Windows (x86 or x64).

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

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

Paste the copied .dll to C:\Python27\ or any location which is accessible from Windows PATH environment variables.

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

thewaywewere
  • 6,704
  • 11
  • 37
  • 42
razmik
  • 149
  • 1
  • 6
-1

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

pip install opencv-python

Sahli Simo
  • 85
  • 2
  • 7