videoWriter.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. function vw = videoWriter(url, varargin)
  2. % videoWriter class constructor
  3. % Creates a object that writes video files. We use a plugin
  4. % architecture in the backend to do the actual writing. For example,
  5. % on Windows, DirectShow will typically be used and on Linux, the
  6. % ffmpeg library is often used.
  7. %
  8. % vw = videoWriter(url)
  9. % Opens the given video file for writing 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. % vw = videoWriter(url,..., 'plugin',pluginName, ...)
  15. % vw = videoWriter(url,pluginName)
  16. % Opens the file using the 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. % - The URL parameter should be a filename.
  23. % - As a convenience, all forward slashes ('/') are automatically
  24. % converted to backslashes ('\')
  25. %
  26. % 'ffmpegPopen2': safe method on Linux
  27. % - Only supported on GNU/Linux (might work on BSD systems too like Mac
  28. % OS X, but this is untested)
  29. % - See INSTALL.ffmpeg.txt for installation instructions
  30. % - Creates a separate server process to communicate with the
  31. % ffmpeg libraries.
  32. % - Works when the system's version of GCC is very different from
  33. % the one that MathWorks used to compile Matlab.
  34. % - Isolates ffmpeg errors so they typically cannot crash
  35. % Matlab.
  36. % - May allow for more flexible distribution terms for your code
  37. % when it uses videoIO (ffmpeg may be compiled with either
  38. % the LGPL or GPL license).
  39. %
  40. % 'ffmpegDirect': low-overhead method on Linux
  41. % - same as ffmpegPopen2, but the ffmpeg libraries are loaded
  42. % directly by the MEX file.
  43. % - May not work if MathWorks' and your version of GCC are
  44. % incompatible.
  45. % - Slightly faster than ffmpegPopen2 since there is no
  46. % interprocess communication overhead.
  47. %
  48. % vw = videoWriter(url, ..., param,arg,...)
  49. % Allows the user to pass extra configuration arguments to plugin.
  50. % At present, all parameter names are case sensitive (but in the
  51. % future they may become case-insensitive).
  52. %
  53. % The following parameters are supported by current plugins:
  54. %
  55. % Plugin
  56. % Parameter ffmpeg* DShow Implementation Notes
  57. % --------- ------- ----- -----------------------------
  58. % width X X Width of the encoded video. Most
  59. % codecs require width to be divisible
  60. % by 2, 4, or 8. Most users will want
  61. % to explicitly pass this parameter.
  62. % The addframe method will
  63. % automatically resize any images
  64. % according to the value chosen here
  65. % (or a default value if none is
  66. % specified here).
  67. %
  68. % height X X Height of the encoded video. Most
  69. % codecs require height to be divisible
  70. % by 2, 4, or 8. Most users will want
  71. % to explicitly pass this parameter.
  72. % The addframe method will
  73. % automatically resize any images
  74. % according to the value chosen here
  75. % (or a default value if none is
  76. % specified here).
  77. %
  78. % codec X X A string specifying the encoder to
  79. % use. The exact set of possible
  80. % codecs is highly system-dependent.
  81. % Most users will want to explicitly
  82. % pass this parameter. To see a list
  83. % of available codecs on a specific
  84. % machine, run:
  85. % codecs = videoWriter([],'codecs');
  86. %
  87. % fourcc X For the DirectShow plugin, this is a
  88. % synonym for 'codec'.
  89. %
  90. % fps X X Frame rate of the recorded video.
  91. % Note that some codecs only work with
  92. % some frame rates. 15, 24, 25, 29.97,
  93. % and 30 should work with most codecs.
  94. %
  95. % framesPerSecond X X An alias for fps.
  96. %
  97. % fpsNum, fpsDenom X X This pair of parameters allows frames
  98. % per second to be specified as a
  99. % rational number. Either both or
  100. % neither parameter must be given.
  101. %
  102. % framesPerSecond_num Alias for fpsNum, fpsDenom pair.
  103. % framesPerSecond_denom
  104. % X X
  105. %
  106. % bitRateTolerance X For supporting codecs, the actual
  107. % bit rate is allowed to vary by +/-
  108. % this value.
  109. %
  110. % showCompressionDialog X If true (a non-zero number), a dialog
  111. % box is presented to the user allowing
  112. % precise manual selection of the codec
  113. % and its parameters. Note: sometimes
  114. % the dialog does not received focus
  115. % automatically so you'll need to
  116. % ALT-TAB to get to it.
  117. %
  118. % codecParams X A MIME Base64-encoded string describing
  119. % the codec setup parameters for a
  120. % DirectShow codec. The contents of this
  121. % string are very codec-specific. Often,
  122. % The best ways to come up with a string
  123. % like this are to first create a
  124. % videoWriter with the
  125. % 'showCompressionDialog' option enabled,
  126. % choose the desired settings, then use
  127. % the GETINFO method to extract the
  128. % 'codecParams' value. Note that this
  129. % representation is the same as used by
  130. % VirtualDub 1.6 and 1.7 in its Sylia
  131. % Script files. Nearly all useful
  132. % DirectShow codecs can only be
  133. % configured with 'codecParams' and they
  134. % ignore the separate 'bitRate' and
  135. % 'gopSize' parameters given below.
  136. %
  137. % bitRate X x Target bits/sec of the encoded video.
  138. % Supported by most ffmpeg codecs.
  139. % To see whether a particular codec uses
  140. % the bitRate parameter, run the
  141. % testBitRate function in the tests/
  142. % subdirectory (NOTE: very few DirectShow
  143. % codecs support it).
  144. %
  145. % gopSize X x Maximum period between keyframes. GOP
  146. % stands for "group of pictures" in MPEG
  147. % lingo. Supported by most ffmpeg
  148. % codecs. To see whether a particular
  149. % codec uses the gopSize parameter, run
  150. % the testGopSize function in the tests/
  151. % subdirectory (NOTE: very few DirectShow
  152. % codecs support it).
  153. %
  154. % maxBFrames X For MPEG codecs, gives the max
  155. % number of bidirectional frames in a
  156. % group of pictures (GOP).
  157. %
  158. % codecs = videoWriter([],'codecs')
  159. % codecs = videoWriter([],pluginName,'codecs')
  160. % codecs = videoWriter([],'codecs','plugin',pluginName)
  161. % Queries the backend for a list of the valid codecs that may be used
  162. % with the 'codec' plugin parameter.
  163. %
  164. % Once you are done using the videoWriter, make sure you call CLOSE so
  165. % that any system resources allocated by the plugin may be released.
  166. % Here's a simple example of how you might use videoWriter to create
  167. % a video of continually adding more motion blur to an image:
  168. %
  169. % % Construct a videoWriter object
  170. % vw = videoWriter('writertest.avi', ...
  171. % 'width',320, 'height',240, 'codec','xvid');
  172. % img = imread('peppers.png');
  173. % h = fspecial('motion',10,5);
  174. % for i=1:100
  175. % addframe(vw, img);
  176. % img = imfilter(img, h);
  177. % end
  178. % vw=close(vw);
  179. %
  180. % SEE ALSO:
  181. % buildVideoMex
  182. % videoWriter/addframe
  183. % videoWriter/close
  184. % videoReader
  185. %
  186. %Copyright (c) 2006 Gerald Dalley
  187. %See "MIT.txt" in the installation directory for licensing details (especially
  188. %when using this library on GNU/Linux).
  189. if (numel(url)==0)
  190. % static method call
  191. if (mod(length(varargin),2) == 0)
  192. plugin = varargin{1};
  193. staticMethod = varargin{2};
  194. methodArgs = {varargin{3:end}};
  195. else
  196. plugin = defaultVideoIOPlugin;
  197. staticMethod = varargin{1};
  198. methodArgs = {varargin{2:end}};
  199. end
  200. [plugin,methodArgs] = parsePlugin(plugin, methodArgs);
  201. vw = feval(mexName(plugin), staticMethod, int32(-1), methodArgs{:});
  202. else
  203. % constructor call
  204. if (mod(length(varargin),2) == 0)
  205. plugin = defaultVideoIOPlugin;
  206. pluginArgs = varargin;
  207. else
  208. plugin = varargin{1};
  209. pluginArgs = {varargin{2:end}};
  210. end
  211. plugin = parsePlugin(plugin, pluginArgs);
  212. vw = struct('plugin',mexName(plugin), 'handle',int32(-1), ...
  213. 'w',int32(-1), 'h',int32(-1));
  214. vw = class(vw, 'videoWriter');
  215. [pathstr, name, ext, versn] = fileparts(url);
  216. strArgs = cell(size(pluginArgs));
  217. for i=1:numel(pluginArgs), strArgs{i} = num2str(pluginArgs{i}); end
  218. [vw.handle,vw.w,vw.h] = feval(vw.plugin, 'open', vw.handle, ...
  219. fullfile(pathstr,[name ext versn]), ...
  220. strArgs{:});
  221. end
  222. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  223. function n = mexName(plugin)
  224. n = ['videoWriter_' plugin];
  225. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  226. function [plugin,pluginArgs] = parsePlugin(plugin, pluginArgs)
  227. if (length(pluginArgs) > 0)
  228. [pluginSpecified,idx] = ismember('plugin', {pluginArgs{1:2:end}});
  229. if pluginSpecified
  230. plugin = pluginArgs{idx*2};
  231. pluginArgs = { pluginArgs{1:idx*2-2}, pluginArgs{idx*2+1:end} };
  232. end
  233. end