ffprobe.js.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: ffprobe.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: ffprobe.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/*jshint node:true, laxcomma:true*/
  20. 'use strict';
  21. var spawn = require('child_process').spawn;
  22. function legacyTag(key) { return key.match(/^TAG:/); }
  23. function legacyDisposition(key) { return key.match(/^DISPOSITION:/); }
  24. function parseFfprobeOutput(out) {
  25. var lines = out.split(/\r\n|\r|\n/);
  26. lines = lines.filter(function (line) {
  27. return line.length > 0;
  28. });
  29. var data = {
  30. streams: [],
  31. format: {},
  32. chapters: []
  33. };
  34. function parseBlock(name) {
  35. var data = {};
  36. var line = lines.shift();
  37. while (typeof line !== 'undefined') {
  38. if (line.toLowerCase() == '[/'+name+']') {
  39. return data;
  40. } else if (line.match(/^\[/)) {
  41. line = lines.shift();
  42. continue;
  43. }
  44. var kv = line.match(/^([^=]+)=(.*)$/);
  45. if (kv) {
  46. if (!(kv[1].match(/^TAG:/)) &amp;&amp; kv[2].match(/^[0-9]+(\.[0-9]+)?$/)) {
  47. data[kv[1]] = Number(kv[2]);
  48. } else {
  49. data[kv[1]] = kv[2];
  50. }
  51. }
  52. line = lines.shift();
  53. }
  54. return data;
  55. }
  56. var line = lines.shift();
  57. while (typeof line !== 'undefined') {
  58. if (line.match(/^\[stream/i)) {
  59. var stream = parseBlock('stream');
  60. data.streams.push(stream);
  61. } else if (line.match(/^\[chapter/i)) {
  62. var chapter = parseBlock('chapter');
  63. data.chapters.push(chapter);
  64. } else if (line.toLowerCase() === '[format]') {
  65. data.format = parseBlock('format');
  66. }
  67. line = lines.shift();
  68. }
  69. return data;
  70. }
  71. module.exports = function(proto) {
  72. /**
  73. * A callback passed to the {@link FfmpegCommand#ffprobe} method.
  74. *
  75. * @callback FfmpegCommand~ffprobeCallback
  76. *
  77. * @param {Error|null} err error object or null if no error happened
  78. * @param {Object} ffprobeData ffprobe output data; this object
  79. * has the same format as what the following command returns:
  80. *
  81. * `ffprobe -print_format json -show_streams -show_format INPUTFILE`
  82. * @param {Array} ffprobeData.streams stream information
  83. * @param {Object} ffprobeData.format format information
  84. */
  85. /**
  86. * Run ffprobe on last specified input
  87. *
  88. * @method FfmpegCommand#ffprobe
  89. * @category Metadata
  90. *
  91. * @param {?Number} [index] 0-based index of input to probe (defaults to last input)
  92. * @param {?String[]} [options] array of output options to return
  93. * @param {FfmpegCommand~ffprobeCallback} callback callback function
  94. *
  95. */
  96. proto.ffprobe = function() {
  97. var input, index = null, options = [], callback;
  98. // the last argument should be the callback
  99. var callback = arguments[arguments.length - 1];
  100. var ended = false
  101. function handleCallback(err, data) {
  102. if (!ended) {
  103. ended = true;
  104. callback(err, data);
  105. }
  106. };
  107. // map the arguments to the correct variable names
  108. switch (arguments.length) {
  109. case 3:
  110. index = arguments[0];
  111. options = arguments[1];
  112. break;
  113. case 2:
  114. if (typeof arguments[0] === 'number') {
  115. index = arguments[0];
  116. } else if (Array.isArray(arguments[0])) {
  117. options = arguments[0];
  118. }
  119. break;
  120. }
  121. if (index === null) {
  122. if (!this._currentInput) {
  123. return handleCallback(new Error('No input specified'));
  124. }
  125. input = this._currentInput;
  126. } else {
  127. input = this._inputs[index];
  128. if (!input) {
  129. return handleCallback(new Error('Invalid input index'));
  130. }
  131. }
  132. // Find ffprobe
  133. this._getFfprobePath(function(err, path) {
  134. if (err) {
  135. return handleCallback(err);
  136. } else if (!path) {
  137. return handleCallback(new Error('Cannot find ffprobe'));
  138. }
  139. var stdout = '';
  140. var stdoutClosed = false;
  141. var stderr = '';
  142. var stderrClosed = false;
  143. // Spawn ffprobe
  144. var src = input.isStream ? 'pipe:0' : input.source;
  145. var ffprobe = spawn(path, ['-show_streams', '-show_format'].concat(options, src));
  146. if (input.isStream) {
  147. // Skip errors on stdin. These get thrown when ffprobe is complete and
  148. // there seems to be no way hook in and close stdin before it throws.
  149. ffprobe.stdin.on('error', function(err) {
  150. if (['ECONNRESET', 'EPIPE'].indexOf(err.code) >= 0) { return; }
  151. handleCallback(err);
  152. });
  153. // Once ffprobe's input stream closes, we need no more data from the
  154. // input
  155. ffprobe.stdin.on('close', function() {
  156. input.source.pause();
  157. input.source.unpipe(ffprobe.stdin);
  158. });
  159. input.source.pipe(ffprobe.stdin);
  160. }
  161. ffprobe.on('error', callback);
  162. // Ensure we wait for captured streams to end before calling callback
  163. var exitError = null;
  164. function handleExit(err) {
  165. if (err) {
  166. exitError = err;
  167. }
  168. if (processExited &amp;&amp; stdoutClosed &amp;&amp; stderrClosed) {
  169. if (exitError) {
  170. if (stderr) {
  171. exitError.message += '\n' + stderr;
  172. }
  173. return handleCallback(exitError);
  174. }
  175. // Process output
  176. var data = parseFfprobeOutput(stdout);
  177. // Handle legacy output with "TAG:x" and "DISPOSITION:x" keys
  178. [data.format].concat(data.streams).forEach(function(target) {
  179. if (target) {
  180. var legacyTagKeys = Object.keys(target).filter(legacyTag);
  181. if (legacyTagKeys.length) {
  182. target.tags = target.tags || {};
  183. legacyTagKeys.forEach(function(tagKey) {
  184. target.tags[tagKey.substr(4)] = target[tagKey];
  185. delete target[tagKey];
  186. });
  187. }
  188. var legacyDispositionKeys = Object.keys(target).filter(legacyDisposition);
  189. if (legacyDispositionKeys.length) {
  190. target.disposition = target.disposition || {};
  191. legacyDispositionKeys.forEach(function(dispositionKey) {
  192. target.disposition[dispositionKey.substr(12)] = target[dispositionKey];
  193. delete target[dispositionKey];
  194. });
  195. }
  196. }
  197. });
  198. handleCallback(null, data);
  199. }
  200. }
  201. // Handle ffprobe exit
  202. var processExited = false;
  203. ffprobe.on('exit', function(code, signal) {
  204. processExited = true;
  205. if (code) {
  206. handleExit(new Error('ffprobe exited with code ' + code));
  207. } else if (signal) {
  208. handleExit(new Error('ffprobe was killed with signal ' + signal));
  209. } else {
  210. handleExit();
  211. }
  212. });
  213. // Handle stdout/stderr streams
  214. ffprobe.stdout.on('data', function(data) {
  215. stdout += data;
  216. });
  217. ffprobe.stdout.on('close', function() {
  218. stdoutClosed = true;
  219. handleExit();
  220. });
  221. ffprobe.stderr.on('data', function(data) {
  222. stderr += data;
  223. });
  224. ffprobe.stderr.on('close', function() {
  225. stderrClosed = true;
  226. handleExit();
  227. });
  228. });
  229. };
  230. };
  231. </code></pre>
  232. </article>
  233. </section>
  234. </div>
  235. <nav>
  236. <h2><a href="index.html">Index</a></h2><ul><li><a href="index.html#installation">Installation</a></li><ul></ul><li><a href="index.html#usage">Usage</a></li><ul><li><a href="index.html#prerequisites">Prerequisites</a></li><li><a href="index.html#creating-an-ffmpeg-command">Creating an FFmpeg command</a></li><li><a href="index.html#specifying-inputs">Specifying inputs</a></li><li><a href="index.html#input-options">Input options</a></li><li><a href="index.html#audio-options">Audio options</a></li><li><a href="index.html#video-options">Video options</a></li><li><a href="index.html#video-frame-size-options">Video frame size options</a></li><li><a href="index.html#specifying-multiple-outputs">Specifying multiple outputs</a></li><li><a href="index.html#output-options">Output options</a></li><li><a href="index.html#miscellaneous-options">Miscellaneous options</a></li><li><a href="index.html#setting-event-handlers">Setting event handlers</a></li><li><a href="index.html#starting-ffmpeg-processing">Starting FFmpeg processing</a></li><li><a href="index.html#controlling-the-ffmpeg-process">Controlling the FFmpeg process</a></li><li><a href="index.html#reading-video-metadata">Reading video metadata</a></li><li><a href="index.html#querying-ffmpeg-capabilities">Querying ffmpeg capabilities</a></li><li><a href="index.html#cloning-an-ffmpegcommand">Cloning an FfmpegCommand</a></li></ul><li><a href="index.html#contributing">Contributing</a></li><ul><li><a href="index.html#code-contributions">Code contributions</a></li><li><a href="index.html#documentation-contributions">Documentation contributions</a></li><li><a href="index.html#updating-the-documentation">Updating the documentation</a></li><li><a href="index.html#running-tests">Running tests</a></li></ul><li><a href="index.html#main-contributors">Main contributors</a></li><ul></ul><li><a href="index.html#license">License</a></li><ul></ul></ul><h3>Classes</h3><ul><li><a href="FfmpegCommand.html">FfmpegCommand</a></li><ul><li> <a href="FfmpegCommand.html#audio-methods">Audio methods</a></li><li> <a href="FfmpegCommand.html#capabilities-methods">Capabilities methods</a></li><li> <a href="FfmpegCommand.html#custom-options-methods">Custom options methods</a></li><li> <a href="FfmpegCommand.html#input-methods">Input methods</a></li><li> <a href="FfmpegCommand.html#metadata-methods">Metadata methods</a></li><li> <a href="FfmpegCommand.html#miscellaneous-methods">Miscellaneous methods</a></li><li> <a href="FfmpegCommand.html#other-methods">Other methods</a></li><li> <a href="FfmpegCommand.html#output-methods">Output methods</a></li><li> <a href="FfmpegCommand.html#processing-methods">Processing methods</a></li><li> <a href="FfmpegCommand.html#video-methods">Video methods</a></li><li> <a href="FfmpegCommand.html#video-size-methods">Video size methods</a></li></ul></ul>
  237. </nav>
  238. <br clear="both">
  239. <footer>
  240. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Sun May 01 2016 12:10:37 GMT+0200 (CEST)
  241. </footer>
  242. <script> prettyPrint(); </script>
  243. <script src="scripts/linenumber.js"> </script>
  244. </body>
  245. </html>