util.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. var qs = require('querystring');
  2. var url = require('url');
  3. var Entities = require('html-entities').AllHtmlEntities;
  4. var FORMATS = require('./formats');
  5. /**
  6. * Parses a string representation of amount of milliseconds.
  7. *
  8. * @param {String} time
  9. * @return {Number}
  10. */
  11. var timeRegexp = /(?:(\d+)h)?(?:(\d+)m(?!s))?(?:(\d+)s)?(?:(\d+)(?:ms)?)?/;
  12. exports.parseTime = function(time) {
  13. var result = timeRegexp.exec(time.toString());
  14. var hours = result[1] || 0;
  15. var mins = result[2] || 0;
  16. var secs = result[3] || 0;
  17. var ms = result[4] || 0;
  18. return hours * 3600000 + mins * 60000 + secs * 1000 + parseInt(ms, 10);
  19. };
  20. // Use these to help sort formats, higher is better.
  21. var audioEncodingRanks = {
  22. mp3: 1,
  23. vorbis: 2,
  24. aac: 3,
  25. opus: 4,
  26. flac: 5,
  27. };
  28. var videoEncodingRanks = {
  29. 'Sorenson H.283': 1,
  30. 'VP8': 3,
  31. 'MPEG-4 Visual': 2,
  32. 'VP9': 4,
  33. 'H.264': 5,
  34. };
  35. /**
  36. * Sort formats from highest quality to lowest.
  37. * By resolution, then video bitrate, then audio bitrate.
  38. *
  39. * @param {Object} a
  40. * @param {Object} b
  41. */
  42. exports.sortFormats = function(a, b) {
  43. var ares = a.resolution ? parseInt(a.resolution.slice(0, -1), 10) : 0;
  44. var bres = b.resolution ? parseInt(b.resolution.slice(0, -1), 10) : 0;
  45. var afeats = ~~!!ares * 2 + ~~!!a.audioBitrate;
  46. var bfeats = ~~!!bres * 2 + ~~!!b.audioBitrate;
  47. function getBitrate(c) {
  48. if (c.bitrate) {
  49. var s = c.bitrate.split('-');
  50. return parseFloat(s[s.length - 1], 10);
  51. } else {
  52. return 0;
  53. }
  54. }
  55. function audioScore(c) {
  56. var abitrate = c.audioBitrate || 0;
  57. var aenc = audioEncodingRanks[c.audioEncoding] || 0;
  58. return abitrate + aenc / 10;
  59. }
  60. if (afeats === bfeats) {
  61. if (ares === bres) {
  62. var avbitrate = getBitrate(a);
  63. var bvbitrate = getBitrate(b);
  64. if (avbitrate === bvbitrate) {
  65. var aascore = audioScore(a);
  66. var bascore = audioScore(b);
  67. if (aascore === bascore) {
  68. var avenc = videoEncodingRanks[a.encoding] || 0;
  69. var bvenc = videoEncodingRanks[b.encoding] || 0;
  70. return bvenc - avenc;
  71. } else {
  72. return bascore - aascore;
  73. }
  74. } else {
  75. return bvbitrate - avbitrate;
  76. }
  77. } else {
  78. return bres - ares;
  79. }
  80. } else {
  81. return bfeats - afeats;
  82. }
  83. };
  84. /**
  85. * Choose a format depending on the given options.
  86. *
  87. * @param {Array.<Object>} formats
  88. * @param {Object} options
  89. * @return {Object|Error}
  90. */
  91. exports.chooseFormat = function(formats, options) {
  92. if (typeof options.format === 'object') {
  93. return options.format;
  94. }
  95. if (options.filter) {
  96. formats = exports.filterFormats(formats, options.filter);
  97. if (formats.length === 0) {
  98. return new Error('no formats found with custom filter');
  99. }
  100. }
  101. var format;
  102. var quality = options.quality || 'highest';
  103. switch (quality) {
  104. case 'highest':
  105. format = formats[0];
  106. break;
  107. case 'lowest':
  108. format = formats[formats.length - 1];
  109. break;
  110. default:
  111. var getFormat = function(itag) {
  112. for (var i = 0, len = formats.length; i < len; i++) {
  113. if (formats[i].itag === '' + itag) {
  114. return formats[i];
  115. }
  116. }
  117. return null;
  118. };
  119. if (Array.isArray(quality)) {
  120. for (var i = 0, len = quality.length; i < len; i++) {
  121. format = getFormat(quality[i]);
  122. if (format) { break; }
  123. }
  124. } else {
  125. format = getFormat(quality);
  126. }
  127. }
  128. if (!format) {
  129. return new Error('No such format found: ' + quality);
  130. } else if (format.rtmp) {
  131. return new Error('rtmp protocol not supported');
  132. }
  133. return format;
  134. };
  135. /**
  136. * @param {Array.<Object>} formats
  137. * @param {Function} filter
  138. * @return {Array.<Object>}
  139. */
  140. exports.filterFormats = function(formats, filter) {
  141. var fn;
  142. switch (filter) {
  143. case 'video':
  144. fn = function(format) { return format.bitrate; };
  145. break;
  146. case 'videoonly':
  147. fn = function(format) { return format.bitrate && !format.audioBitrate; };
  148. break;
  149. case 'audio':
  150. fn = function(format) { return format.audioBitrate; };
  151. break;
  152. case 'audioonly':
  153. fn = function(format) { return !format.bitrate && format.audioBitrate; };
  154. break;
  155. default:
  156. fn = filter;
  157. }
  158. return formats.filter(fn);
  159. };
  160. /**
  161. * Extract string inbetween another.
  162. *
  163. * @param {String} haystack
  164. * @param {String} left
  165. * @param {String} right
  166. * @return {String}
  167. */
  168. exports.between = function(haystack, left, right) {
  169. var pos;
  170. pos = haystack.indexOf(left);
  171. if (pos === -1) { return ''; }
  172. haystack = haystack.slice(pos + left.length);
  173. pos = haystack.indexOf(right);
  174. if (pos === -1) { return ''; }
  175. haystack = haystack.slice(0, pos);
  176. return haystack;
  177. };
  178. /**
  179. * Get video ID.
  180. *
  181. * There are a few type of video URL formats.
  182. * - http://www.youtube.com/watch?v=VIDEO_ID
  183. * - http://youtu.be/VIDEO_ID
  184. *
  185. * @param {String} link
  186. * @return {String}
  187. */
  188. exports.getVideoID = function(link) {
  189. var linkParsed = url.parse(link, true);
  190. var id = linkParsed.hostname === 'youtu.be' ?
  191. linkParsed.pathname.slice(1) : linkParsed.query.v;
  192. if (!id) {
  193. throw new Error('No video id found: ' + link);
  194. }
  195. return id;
  196. };
  197. /**
  198. * @param {Object} info
  199. * @param {Boolean} debug
  200. * @return {Array.<Object>}
  201. */
  202. exports.parseFormats = function(info, debug) {
  203. var formats = [];
  204. if (info.url_encoded_fmt_stream_map) {
  205. formats = formats
  206. .concat(info.url_encoded_fmt_stream_map.split(','));
  207. }
  208. if (info.adaptive_fmts) {
  209. formats = formats.concat(info.adaptive_fmts.split(','));
  210. }
  211. formats = formats
  212. .map(function(format) {
  213. var data = qs.parse(format);
  214. if (data.conn && data.conn.indexOf('rtmp') === 0) {
  215. data.rtmp = true;
  216. }
  217. var meta = FORMATS[data.itag];
  218. if (!meta && debug) {
  219. console.warn('No format metadata for itag ' + data.itag + ' found');
  220. }
  221. for (var key in meta) {
  222. data[key] = meta[key];
  223. }
  224. return data;
  225. });
  226. delete info.url_encoded_fmt_stream_map;
  227. delete info.adaptive_fmts;
  228. return formats;
  229. };
  230. /**
  231. * Get video description from html
  232. *
  233. * @param {String} html
  234. * @return {String}
  235. */
  236. exports.getVideoDescription = function(html) {
  237. var regex = /<p.*?id="eow-description".*?>(.+?)<\/p>[\n\r\s]*?<\/div>/im;
  238. var description = html.match(regex);
  239. return description ? new Entities().decode(description[1]
  240. .replace(/\n/g, ' ')
  241. .replace(/\s*<\s*br\s*\/?\s*>\s*/gi, '\n')
  242. .replace(/<\s*\/\s*p\s*>\s*<\s*p[^>]*>/gi, '\n')
  243. .replace(/<.*?>/gi, '')).trim() : '';
  244. };