videoread.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. function mov=videoread(fname,varargin)
  2. %VIDEOREAD Read an video file in a mannter compatible with Matlab's
  3. % built-in AVIREAD function, but using a videoReader object to do
  4. % the work instead.
  5. %
  6. % MOV=VIDEOREAD(FNAME)
  7. % Read the video file FNAME into the Matlab movie structure MOV. MOV
  8. % has two fields, "cdata" and "colormap". Since all videoReader
  9. % plugins currently output 24-bit images in all cases, colormap will
  10. % always be blank.
  11. %
  12. % MOV=VIDEOREAD(FNAME,VRARGS)
  13. % The same as MOV=VIDEOREAD(FNAME), but VRARGS are passed to the
  14. % videoReader constructor. VRARGS should be a cell array of arguments.
  15. % E.g.
  16. % mov = videoread('tests/numbers.wmv3.avi', {'DirectShow'});
  17. %
  18. % MOV=VIDEOREAD(FNAME,INDEX)
  19. % Reads only the frame(s) specified by INDEX. INDEX can be a single
  20. % index or an array of indices into the video stream, where the first
  21. % frame is number one. IMPORTANT NOTES: If you use videoReader
  22. % directly, it assumes frames start at 0, not 1. We use 1-indexing
  23. % here in VIDEOREAD to maximize compatibility with MathWork's AVIREAD.
  24. % Also, if you find yourself making several calls to VIDEOREAD to
  25. % extract different portions of the file and/or if the number of
  26. % frames is large, consider using videoReader directly instead of this
  27. % wrapper function.
  28. %
  29. % MOV=VIDEOREAD(FNAME,VRARGS,INDEX)
  30. % The same as MOV=VIDEOREAD(FNAME,INDEX), but VRARGS are passed to the
  31. % videoReader constructor. VRARGS should be a cell array of arguments.
  32. %
  33. %Copyright (c) 2006 Gerald Dalley
  34. %See "MIT.txt" in the installation directory for licensing details (especially
  35. %when using this library on GNU/Linux).
  36. if nargin==1,
  37. vrargs = {};
  38. elseif nargin==2,
  39. if (iscell(varargin{1})),
  40. vrargs = varargin{1};
  41. else
  42. vrargs = {};
  43. index = varargin{1};
  44. end
  45. elseif nargin==3,
  46. vrargs = varargin{1};
  47. index = varargin{2};
  48. end
  49. vr = videoReader(fname, vrargs{:});
  50. if ~(exist('index','var')),
  51. info = getinfo(vr);
  52. index = (0:info.numFrames-1); % 0-indexed
  53. else
  54. index = index - 1; % 0-indexed
  55. end
  56. mov = struct;
  57. currFrame = -1;
  58. if (size(index)>0)
  59. % index supplied or deduced from file
  60. for i=1:length(index)
  61. f = index(i);
  62. if (f == currFrame+1)
  63. if ~next(vr), error('Could not advance to frame %d\n', f+1); end
  64. else
  65. if ~seek(vr, f), error('Could not seek to frame %d\n', f+1); end
  66. end
  67. currFrame = f;
  68. mov(i).cdata = getframe(vr);
  69. mov(i).colormap = [];
  70. end
  71. else
  72. % no index was supplied and we could not deduce the number of frames
  73. % in the video.
  74. i=1;
  75. while next(vr)
  76. mov(i).cdata = getframe(vr);
  77. mov(i).colormap = [];
  78. i = i+1;
  79. end
  80. end
  81. vr = close(vr);