videoReader.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. function vr = videoReader(url, varargin)
  2. % videoReader class constructor
  3. % Creates a object that reads video streams. We use a plugin
  4. % architecture in the backend to do the actual reading. For example,
  5. % on Windows, DirectShow will typically be used and on Linux, the
  6. % ffmpeg library is often used.
  7. %
  8. % vr = videoReader(url)
  9. % Opens the given video file for reading using the default plugin.
  10. % On Windows, 'DirectShow' is used by default and on Linux,
  11. % 'ffmpegPopen2' is used by default. For most plugins, the url will
  12. % really be a filename.
  13. %
  14. % vr = videoReader(url, ..., 'plugin',pluginName, ...)
  15. % vr = videoReader(url,pluginName, ...)
  16. % Opens the file using the manually specified plugin implementation.
  17. % Available plugins include:
  18. %
  19. % 'DirectShow': preferred method on Windows
  20. % - Only available on Windows
  21. % - See INSTALL.dshow.txt for installation instructions
  22. % - Can load virtually any video file that can be played in
  23. % Microsoft's Windows Media Player. Note that the correct codec
  24. % must be installed to read a file. For example, to read
  25. % tests/numbers.3ivx.avi, the user must have installed an MPEG4
  26. % codec such as 3ivx (www.3ivx.com), DivX (www.divx.com), or XviD
  27. % (www.xvid.org).
  28. % - The URL parameter should be a filename.
  29. % - As a convenience, all forward slashes ('/') are automatically
  30. % converted to backslashes ('\')
  31. %
  32. % 'ffmpegPopen2': safe method on Linux
  33. % - Only supported on GNU/Linux (might work on BSD systems too like Mac
  34. % OS X, but this is untested)
  35. % - See INSTALL.ffmpeg.txt for installation instructions
  36. % - Creates a separate server process to communicate with the
  37. % ffmpeg libraries.
  38. % - Works when the system's version of GCC is very different from
  39. % the one that MathWorks used to compile Matlab.
  40. % - Isolates ffmpeg errors so they typically cannot crash
  41. % Matlab.
  42. % - May allow for more flexible distribution terms for your code
  43. % when it uses videoIO (ffmpeg may be compiled with either
  44. % the LGPL or GPL license).
  45. %
  46. % 'ffmpegDirect': low-overhead method on Linux
  47. % - same as ffmpegPopen2, but the ffmpeg libraries are loaded
  48. % directly by the MEX file.
  49. % - May not work if MathWorks' and your version of GCC are
  50. % incompatible.
  51. % - Slightly faster than ffmpegPopen2 since there is no
  52. % interprocess communication overhead.
  53. %
  54. % vr = videoReader(url, ..., param,arg,...)
  55. % Allows the user to pass extra configuration arguments to plugin.
  56. % Currently no plugin arguments are supported right now. In the
  57. % future, we may allow the user to do things like have DirectShow
  58. % automatically convert to grayscale, or give options to trade off
  59. % speed with seeking precision.
  60. %
  61. % Once you have created a videoReader object, you must next call NEXT,
  62. % SEEK, or STEP at least once so that it will read some frame from disk.
  63. % *After* calling one of these methods, you may call GETFRAME as many
  64. % times as you would like and it will decode and return the current frame
  65. % (without advancing to a different frame). GETINFO may be called at any
  66. % time (even before NEXT, SEEK, or STEP). It returns basic information
  67. % about the video stream. Once you are done using the videoReader, make
  68. % sure you call CLOSE so that any system resources allocated by the plugin
  69. % may be released. Here's a simple example of how you might use
  70. % videoReader:
  71. %
  72. % % take us to the videoReader directory since we know there's a video
  73. % % file there.
  74. % chdir(fileparts(which('videoReaderWrapper.cpp')));
  75. %
  76. % % Construct a videoReader object
  77. % vr = videoReader('tests/numbers.uncompressed.avi');
  78. %
  79. % % Do some processing on the video and display the results
  80. % avgIntensity = [];
  81. % i = 1;
  82. % figure;
  83. % while (next(vr))
  84. % img = getframe(vr);
  85. % avgIntensity(i) = mean(img(:));
  86. % subplot(121); imshow(img); title('current frame');
  87. % subplot(122); plot(avgIntensity); title('avg. intensity vs. frame');
  88. % drawnow; pause(0.1); i = i+1;
  89. % end
  90. % vr = close(vr);
  91. %
  92. % SEE ALSO:
  93. % buildVideoMex
  94. % videoReader/close
  95. % videoReader/getframe
  96. % videoReader/getinfo
  97. % videoReader/getnext
  98. % videoReader/next
  99. % videoReader/seek
  100. % videoReader/step
  101. % videoWriter
  102. %
  103. %Copyright (c) 2006 Gerald Dalley
  104. %See "MIT.txt" in the installation directory for licensing details (especially
  105. %when using this library on GNU/Linux).
  106. if (mod(length(varargin),2) == 0)
  107. plugin = defaultVideoIOPlugin;
  108. pluginArgs = varargin;
  109. else
  110. plugin = varargin{1};
  111. pluginArgs = {varargin{2:end}};
  112. end
  113. [plugin,pluginArgs] = parsePlugin(plugin, pluginArgs);
  114. vr = struct('plugin',mexName(plugin), 'handle',int32(-1));
  115. vr = class(vr, 'videoReader');
  116. [pathstr, name, ext, versn] = fileparts(url);
  117. strArgs = cell(size(pluginArgs));
  118. for i=1:numel(pluginArgs), strArgs{i} = num2str(pluginArgs{i}); end
  119. vr.handle = feval(vr.plugin, 'open', vr.handle, ...
  120. fullfile(pathstr,[name ext versn]), strArgs{:});
  121. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  122. function n = mexName(plugin)
  123. n = ['videoReader_' plugin];
  124. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  125. function [plugin,pluginArgs] = parsePlugin(plugin, pluginArgs)
  126. if (length(pluginArgs) > 0)
  127. [pluginSpecified,idx] = ismember('plugin', {pluginArgs{1:2:end}});
  128. if pluginSpecified
  129. plugin = pluginArgs{idx*2};
  130. pluginArgs = { pluginArgs{1:idx*2-2}, pluginArgs{idx*2+1:end} };
  131. end
  132. end