util-test.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. var util = require('../lib/util');
  2. var assert = require('assert-diff');
  3. var fs = require('fs');
  4. var path = require('path');
  5. var formats = [
  6. { itag : '18',
  7. type : 'video/mp4; codecs="avc1.42001E, mp4a.40.2"',
  8. quality : 'medium',
  9. container : 'mp4',
  10. resolution : '360p',
  11. encoding : 'H.264',
  12. bitrate : '0.5',
  13. audioEncoding : 'aac',
  14. audioBitrate : 96 },
  15. { itag : '43',
  16. type : 'video/webm; codecs="vp8.0, vorbis"',
  17. quality : 'medium',
  18. container : 'webm',
  19. resolution : '360p',
  20. encoding : 'VP8',
  21. bitrate : '0.5',
  22. audioEncoding : 'vorbis',
  23. audioBitrate : 128 },
  24. { itag : '133',
  25. type : 'video/mp4; codecs="avc1.4d400d"',
  26. quality : null,
  27. container : 'mp4',
  28. resolution : '240p',
  29. encoding : 'H.264',
  30. bitrate : '0.15-0.3',
  31. audioEncoding : null,
  32. audioBitrate : null },
  33. { itag : '36',
  34. type : 'video/3gpp; codecs="mp4v.20.3, mp4a.40.2"',
  35. quality : 'small',
  36. container : '3gp',
  37. resolution : '240p',
  38. encoding : 'MPEG-4 Visual',
  39. bitrate : '0.17',
  40. audioEncoding : 'aac',
  41. audioBitrate : 38 },
  42. { itag : '5',
  43. type : 'video/x-flv',
  44. quality : 'small',
  45. container : 'flv',
  46. resolution : '240p',
  47. encoding : 'Sorenson H.283',
  48. bitrate : '0.25',
  49. audioEncoding : 'mp3',
  50. audioBitrate : 64 },
  51. { itag : '160',
  52. type : 'video/mp4; codecs="avc1.4d400c"',
  53. quality : null,
  54. container : 'mp4',
  55. resolution : '144p',
  56. encoding : 'H.264',
  57. bitrate : '0.1',
  58. audioEncoding : null,
  59. audioBitrate : null },
  60. { itag : '17',
  61. type : 'video/3gpp; codecs="mp4v.20.3, mp4a.40.2"',
  62. quality : 'small',
  63. container : '3gp',
  64. resolution : '144p',
  65. encoding : 'MPEG-4 Visual',
  66. bitrate : '0.05',
  67. audioEncoding : 'aac',
  68. audioBitrate : 24 },
  69. { itag : '140',
  70. type : 'audio/mp4; codecs="mp4a.40.2"',
  71. quality : null,
  72. container : 'mp4',
  73. resolution : null,
  74. enoding : null,
  75. bitrate : null,
  76. audioEncoding : 'aac',
  77. audioBitrate : 128 },
  78. ];
  79. var getItags = function(format) { return format.itag; };
  80. describe('util.parseTime()', function() {
  81. it('Returns milliseconds if given numbers', function() {
  82. assert.equal(1234, util.parseTime(1234));
  83. });
  84. it('Works with minutes and seconds', function() {
  85. assert.equal(2 * 60000 + 36 * 1000, util.parseTime('2m36s'));
  86. });
  87. it('And even only hours and milliseconds', function() {
  88. assert.equal(3 * 3600000 + 4200, util.parseTime('3h4200ms'));
  89. });
  90. });
  91. describe('util.sortFormats()', function() {
  92. describe('with `highest` given', function() {
  93. it('Sorts available formats from highest to lowest quality', function() {
  94. var sortedFormats = formats.slice();
  95. sortedFormats.sort(util.sortFormats);
  96. var itags = sortedFormats.map(getItags);
  97. assert.deepEqual(itags, [
  98. '43', '18', '5', '36', '17', '133', '160', '140'
  99. ]);
  100. });
  101. });
  102. });
  103. describe('util.chooseFormat', function() {
  104. var sortedFormats = formats.slice();
  105. sortedFormats.sort(util.sortFormats);
  106. describe('with no options', function() {
  107. it('Chooses highest quality', function() {
  108. var format = util.chooseFormat(sortedFormats, {});
  109. assert.equal(format.itag, '43');
  110. });
  111. });
  112. describe('with lowest quality wanted', function() {
  113. it('Chooses lowest itag', function() {
  114. var format = util.chooseFormat(sortedFormats, { quality: 'lowest' });
  115. assert.equal(format.itag, '140');
  116. });
  117. });
  118. describe('with itag given', function() {
  119. it('Chooses matching format', function() {
  120. var format = util.chooseFormat(sortedFormats, { quality: 5 });
  121. assert.equal(format.itag, '5');
  122. });
  123. describe('that is not in the format list', function() {
  124. it('Returns an error', function() {
  125. var err = util.chooseFormat(sortedFormats, { quality: 42 });
  126. assert.equal(err.message, 'No such format found: 42');
  127. });
  128. });
  129. });
  130. describe('with list of itags given', function() {
  131. it('Chooses matching format', function() {
  132. var format = util.chooseFormat(sortedFormats, { quality: [99, 160, 18] });
  133. assert.equal(format.itag, '160');
  134. });
  135. });
  136. });
  137. describe('util.filterFormats', function() {
  138. it('Tries to find formats that match', function() {
  139. var filter = function(format) { return format.container === 'mp4'; };
  140. var itags = util.filterFormats(formats, filter).map(getItags);
  141. assert.deepEqual(itags, ['18', '133', '160', '140']);
  142. });
  143. describe('that doesn\'t match any format', function() {
  144. it('Returns an empty list', function() {
  145. var list = util.filterFormats(formats, function() { return false; });
  146. assert.equal(list.length, 0);
  147. });
  148. });
  149. describe('with `video` given', function() {
  150. it('Returns only matching formats', function() {
  151. var itags = util.filterFormats(formats, 'video').map(getItags);
  152. assert.deepEqual(itags, ['18', '43', '133', '36', '5', '160', '17']);
  153. });
  154. });
  155. describe('with `videoonly` given', function() {
  156. it('Returns only matching formats', function() {
  157. var itags = util.filterFormats(formats, 'videoonly').map(getItags);
  158. assert.deepEqual(itags, ['133', '160']);
  159. });
  160. });
  161. describe('with `audio` given', function() {
  162. it('Returns only matching formats', function() {
  163. var itags = util.filterFormats(formats, 'audio').map(getItags);
  164. assert.deepEqual(itags, ['18', '43', '36', '5', '17', '140']);
  165. });
  166. });
  167. describe('with `audioonly` given', function() {
  168. it('Returns only matching formats', function() {
  169. var itags = util.filterFormats(formats, 'audioonly').map(getItags);
  170. assert.deepEqual(itags, ['140']);
  171. });
  172. });
  173. });
  174. describe('util.between()', function() {
  175. it('`left` positioned at the start', function() {
  176. var rs = util.between('<b>hello there friend</b>', '<b>', '</b>');
  177. assert.equal(rs, 'hello there friend');
  178. });
  179. it('somewhere in the middle', function() {
  180. var rs = util.between('something everything nothing', ' ', ' ');
  181. assert.equal(rs, 'everything');
  182. });
  183. it('not found', function() {
  184. var rs = util.between('oh oh _where_ is it', '<b>', '</b>');
  185. assert.equal(rs, '');
  186. });
  187. it('`right` before `left`', function() {
  188. var rs = util.between('>>> a <this> and that', '<', '>');
  189. assert.equal(rs, 'this');
  190. });
  191. });
  192. describe('util.getVideoID()', function() {
  193. it('Retrives the video ID from the url', function() {
  194. var id;
  195. id = util.getVideoID('http://www.youtube.com/watch?v=VIDEO_ID');
  196. assert(id, 'VIDEO_ID');
  197. id = util.getVideoID('http://youtu.be/VIDEO_ID');
  198. assert(id, 'VIDEO_ID');
  199. });
  200. });
  201. describe('util.parseFormats()', function() {
  202. it('Retrieves video formats from info', function() {
  203. var info = require('./files/info/pJk0p-98Xzc_preparsed.json');
  204. var formats = util.parseFormats(info);
  205. assert.ok(formats);
  206. assert.equal(formats.length, 14);
  207. });
  208. });
  209. describe('util.getVideoDescription()', function() {
  210. it('Retrieves formatted video description', function() {
  211. var html = fs.readFileSync(path.resolve(__dirname,
  212. 'files/util/multiline-video-description'), 'utf8');
  213. var cleanDescription = util.getVideoDescription(html);
  214. assert.ok(cleanDescription);
  215. assert.equal(cleanDescription, 'Some Title\n' +
  216. 'Line 1\n' +
  217. '"Line 2"\n' +
  218. '1 First Song 5:30\n' +
  219. '2 Second Song 5:42');
  220. });
  221. });