custom.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*jshint node:true*/
  2. 'use strict';
  3. var utils = require('../utils');
  4. /*
  5. *! Custom options methods
  6. */
  7. module.exports = function(proto) {
  8. /**
  9. * Add custom input option(s)
  10. *
  11. * When passing a single string or an array, each string containing two
  12. * words is split (eg. inputOptions('-option value') is supported) for
  13. * compatibility reasons. This is not the case when passing more than
  14. * one argument.
  15. *
  16. * @example
  17. * command.inputOptions('option1');
  18. *
  19. * @example
  20. * command.inputOptions('option1', 'option2');
  21. *
  22. * @example
  23. * command.inputOptions(['option1', 'option2']);
  24. *
  25. * @method FfmpegCommand#inputOptions
  26. * @category Custom options
  27. * @aliases addInputOption,addInputOptions,withInputOption,withInputOptions,inputOption
  28. *
  29. * @param {...String} options option string(s) or string array
  30. * @return FfmpegCommand
  31. */
  32. proto.addInputOption =
  33. proto.addInputOptions =
  34. proto.withInputOption =
  35. proto.withInputOptions =
  36. proto.inputOption =
  37. proto.inputOptions = function(options) {
  38. if (!this._currentInput) {
  39. throw new Error('No input specified');
  40. }
  41. var doSplit = true;
  42. if (arguments.length > 1) {
  43. options = [].slice.call(arguments);
  44. doSplit = false;
  45. }
  46. if (!Array.isArray(options)) {
  47. options = [options];
  48. }
  49. this._currentInput.options(options.reduce(function(options, option) {
  50. var split = String(option).split(' ');
  51. if (doSplit && split.length === 2) {
  52. options.push(split[0], split[1]);
  53. } else {
  54. options.push(option);
  55. }
  56. return options;
  57. }, []));
  58. return this;
  59. };
  60. /**
  61. * Add custom output option(s)
  62. *
  63. * @example
  64. * command.outputOptions('option1');
  65. *
  66. * @example
  67. * command.outputOptions('option1', 'option2');
  68. *
  69. * @example
  70. * command.outputOptions(['option1', 'option2']);
  71. *
  72. * @method FfmpegCommand#outputOptions
  73. * @category Custom options
  74. * @aliases addOutputOption,addOutputOptions,addOption,addOptions,withOutputOption,withOutputOptions,withOption,withOptions,outputOption
  75. *
  76. * @param {...String} options option string(s) or string array
  77. * @return FfmpegCommand
  78. */
  79. proto.addOutputOption =
  80. proto.addOutputOptions =
  81. proto.addOption =
  82. proto.addOptions =
  83. proto.withOutputOption =
  84. proto.withOutputOptions =
  85. proto.withOption =
  86. proto.withOptions =
  87. proto.outputOption =
  88. proto.outputOptions = function(options) {
  89. var doSplit = true;
  90. if (arguments.length > 1) {
  91. options = [].slice.call(arguments);
  92. doSplit = false;
  93. }
  94. if (!Array.isArray(options)) {
  95. options = [options];
  96. }
  97. this._currentOutput.options(options.reduce(function(options, option) {
  98. var split = String(option).split(' ');
  99. if (doSplit && split.length === 2) {
  100. options.push(split[0], split[1]);
  101. } else {
  102. options.push(option);
  103. }
  104. return options;
  105. }, []));
  106. return this;
  107. };
  108. /**
  109. * Specify a complex filtergraph
  110. *
  111. * Calling this method will override any previously set filtergraph, but you can set
  112. * as many filters as needed in one call.
  113. *
  114. * @example <caption>Overlay an image over a video (using a filtergraph string)</caption>
  115. * ffmpeg()
  116. * .input('video.avi')
  117. * .input('image.png')
  118. * .complexFilter('[0:v][1:v]overlay[out]', ['out']);
  119. *
  120. * @example <caption>Overlay an image over a video (using a filter array)</caption>
  121. * ffmpeg()
  122. * .input('video.avi')
  123. * .input('image.png')
  124. * .complexFilter([{
  125. * filter: 'overlay',
  126. * inputs: ['0:v', '1:v'],
  127. * outputs: ['out']
  128. * }], ['out']);
  129. *
  130. * @example <caption>Split video into RGB channels and output a 3x1 video with channels side to side</caption>
  131. * ffmpeg()
  132. * .input('video.avi')
  133. * .complexFilter([
  134. * // Duplicate video stream 3 times into streams a, b, and c
  135. * { filter: 'split', options: '3', outputs: ['a', 'b', 'c'] },
  136. *
  137. * // Create stream 'red' by cancelling green and blue channels from stream 'a'
  138. * { filter: 'lutrgb', options: { g: 0, b: 0 }, inputs: 'a', outputs: 'red' },
  139. *
  140. * // Create stream 'green' by cancelling red and blue channels from stream 'b'
  141. * { filter: 'lutrgb', options: { r: 0, b: 0 }, inputs: 'b', outputs: 'green' },
  142. *
  143. * // Create stream 'blue' by cancelling red and green channels from stream 'c'
  144. * { filter: 'lutrgb', options: { r: 0, g: 0 }, inputs: 'c', outputs: 'blue' },
  145. *
  146. * // Pad stream 'red' to 3x width, keeping the video on the left, and name output 'padded'
  147. * { filter: 'pad', options: { w: 'iw*3', h: 'ih' }, inputs: 'red', outputs: 'padded' },
  148. *
  149. * // Overlay 'green' onto 'padded', moving it to the center, and name output 'redgreen'
  150. * { filter: 'overlay', options: { x: 'w', y: 0 }, inputs: ['padded', 'green'], outputs: 'redgreen'},
  151. *
  152. * // Overlay 'blue' onto 'redgreen', moving it to the right
  153. * { filter: 'overlay', options: { x: '2*w', y: 0 }, inputs: ['redgreen', 'blue']},
  154. * ]);
  155. *
  156. * @method FfmpegCommand#complexFilter
  157. * @category Custom options
  158. * @aliases filterGraph
  159. *
  160. * @param {String|Array} spec filtergraph string or array of filter specification
  161. * objects, each having the following properties:
  162. * @param {String} spec.filter filter name
  163. * @param {String|Array} [spec.inputs] (array of) input stream specifier(s) for the filter,
  164. * defaults to ffmpeg automatically choosing the first unused matching streams
  165. * @param {String|Array} [spec.outputs] (array of) output stream specifier(s) for the filter,
  166. * defaults to ffmpeg automatically assigning the output to the output file
  167. * @param {Object|String|Array} [spec.options] filter options, can be omitted to not set any options
  168. * @param {Array} [map] (array of) stream specifier(s) from the graph to include in
  169. * ffmpeg output, defaults to ffmpeg automatically choosing the first matching streams.
  170. * @return FfmpegCommand
  171. */
  172. proto.filterGraph =
  173. proto.complexFilter = function(spec, map) {
  174. this._complexFilters.clear();
  175. if (!Array.isArray(spec)) {
  176. spec = [spec];
  177. }
  178. this._complexFilters('-filter_complex', utils.makeFilterStrings(spec).join(';'));
  179. if (Array.isArray(map)) {
  180. var self = this;
  181. map.forEach(function(streamSpec) {
  182. self._complexFilters('-map', streamSpec.replace(utils.streamRegexp, '[$1]'));
  183. });
  184. } else if (typeof map === 'string') {
  185. this._complexFilters('-map', map.replace(utils.streamRegexp, '[$1]'));
  186. }
  187. return this;
  188. };
  189. };