Search code examples
matlabvideoavireader

Read avi video - Matlab


I want to read an AVI File in Matlab. I tried it according to this link: http://inside.mines.edu/~whoff/courses/EENG512/lectures/other/Matlab_movies.pdf :

clear all
close all
movieObj = VideoReader('ap001_BL0_SP2_cam03_compressed.avi'); % open file
get(movieObj) % display all information about movie
nFrames = movieObj.NumberOfFrames; %shows 310 in my case
for iFrame=1:2:nFrames
    I = read(movieObj,iFrame); % get one RGB image
    imshow(I,[]); % Display image
end

I get the following error:

Error using VideoReader/read (line 145) The frame index requested is beyond the end of the file.

Error in test_video_read (line 9) I = read(movieObj,iFrame); % get one RGB image

(Shortened) Output from "get(movieObj)" is:

General Settings: 
   Duration = 10.3333
   Name = ap001_BL0_SP2_cam03_compressed.avi
   Type = VideoReader

Video Settings:
   BitsPerPixel = 24
   FrameRate = 30
   Height = 1280
   NumberOfFrames = 310
   VideoFormat = RGB24
   Width = 960

So it should be possible to read the first frame, as there are 310 available! I can play the AVI file in VLC-Player, so the codec should be already installed, right?

I'm using MATLAB R2013a, Windows 7. Can anyone please help, thank you!


Solution

  • VLC player is built using the ffmpeg codecs. VideoReader uses DirectShow and Media Foundation API's that are Windows Platform API's and are different from ffmpeg. So, if a file plays using VLC, it is not guaranteed to be opened by VideoReader. Couple of things you can do:

    1. Can the file be viewed on Windows Media Player? If so, in most cases it should work with VideoReader. If not, then you do not have the suitable codecs. Try installing ffsdhow or K-lite codec pack.
    2. If file works on Windows Media Player but VideoReader does not support, it would indicate a bug. A workaround that has worked for me in the past is that I install codecs mentioned above and give it a try again.
    3. If (1) and (2) do not help, use software like handbrake or Mirro to transcode the file into MP4 which works win VideoReader.

    Hope this helps.

    Dinesh