Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
add getFormats()
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Sep 13, 2013
1 parent 8625ff4 commit 604fa7b
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 181 deletions.
54 changes: 36 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,21 @@ This example can be found in the *example* folder, and will produce an output th

```javascript
var youtubedl = require('youtube-dl');
youtubedl.info('http://www.youtube.com/watch?v=WKsjaOqDXgg',

// called when video page is downloaded and info extracted
function(err, info) {
if (err) throw err;
console.log('id:', info.id);
console.log('title:', info.title);
console.log('url:', info.url);
console.log('thumbnail:', info.thumbnail);
console.log('description:', info.description);
console.log('filename:', info.filename);
console.log('itag:', info.itag);
console.log('resolution:', info.resolution);
}

// optional arguments passed to youtube-dl
// ['--username=user', '--password=hunter2']
);
var url = 'http://www.youtube.com/watch?v=WKsjaOqDXgg';
// optional arguments passed to youtube-dl
var options = ['--username=user', '--password=hunter2'];
youtubedl.getInfo(url, options, function(err, info) {
if (err) throw err;

console.log('id:', info.id);
console.log('title:', info.title);
console.log('url:', info.url);
console.log('thumbnail:', info.thumbnail);
console.log('description:', info.description);
console.log('filename:', info.filename);
console.log('itag:', info.itag);
console.log('resolution:', info.resolution);
});
```

Running that will produce something like
Expand All @@ -98,6 +95,27 @@ Running that will produce something like
itag: 34
resolution: 360x640

## Getting available formats

```js
var youtubedl = require('youtube-dl');
var url = 'http://www.youtube.com/watch?v=WKsjaOqDXgg';
youtubedl.getFormats(url, function(err, formats) {
if (err) throw er;

formats.forEach(function(format) {
console.log(format);
});
});
```

Will print something like

{ itag: 34, filetype: 'flv', resolution: '360x640' }
{ itag: 18, filetype: 'mp4', resolution: '360x640' }
{ itag: 43, filetype: 'webm', resolution: '360x640' }
{ itag: 5, filetype: 'flv', resolution: '240x400' }
{ itag: 17, filetype: 'mp4', resolution: '144x176' }

For more usage info on youtube-dl and the arguments you can pass to it, do `youtube-dl -h` or go to the [youtube-dl documentation][].

Expand Down
11 changes: 11 additions & 0 deletions example/getFormats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var ytdl = require('..');
var url = 'http://www.youtube.com/watch?v=WKsjaOqDXgg';

ytdl.getFormats(url, function(err, formats) {
if (err) throw err;

formats.forEach(function(format) {
console.log(format);
});
}
);
16 changes: 16 additions & 0 deletions example/getInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var ytdl = require('..');
var url = 'http://www.youtube.com/watch?v=WKsjaOqDXgg';

ytdl.getInfo(url, function(err, info) {
if (err) throw err;

console.log('id:', info.id);
console.log('title:', info.title);
console.log('url:', info.url);
console.log('thumbnail:', info.thumbnail);
console.log('description:', info.description);
console.log('filename:', info.filename);
console.log('itag:', info.itag);
console.log('resolution:', info.resolution);
}
);
20 changes: 0 additions & 20 deletions example/info.js

This file was deleted.

121 changes: 121 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

// Arguments we dont want users to use with youtube-dl
// because they will break the module.
var badArgs = [
'-h', '--help'
, '-v', '--version'
, '-U', '--update'
, '-q', '--quiet'
, '-s', '--simulate'
, '-g', '--get-url'
, '-e', '--get-title'
, '--get-thumbnail'
, '--get-description'
, '--get-filename'
, '--no-progress'
, '--console-title'
];

/**
* Helps parse options used in youtube-dl command.
*
* @param {Array.<String>}
* @return {Array.<String>}
*/
exports.parseOpts = function(args) {
var pos;
for (var i = 0, len = badArgs.length; i < len; i++) {
if ((pos = args.indexOf(badArgs[i])) !== -1) {
args.splice(pos, 1);
}
}
return args;
};


/**
* Rounds a number to n decimal places.
*
* @param {Number} num
* @param {Number} n
* @return {Number}
*/
exports.round = function(num, n) {
var dec = Math.pow(10, n);
return Math.round(num * dec + 0.1) / dec;
};


/**
* Converts from bytes kb, mb, and gb to bytes.
*
* @param {String} s
* @return {Number}
*/
exports.toBytes = function(s) {
var speed = parseFloat(s.substring(0, s.length - 1));
switch (s.substr(-1, 1).toLowerCase()) {
case 'b':
return speed;
case 'k':
return speed * 1024;
case 'm':
return speed * 1024 * 1024;
case 'g':
return speed * 1024 * 1024 * 1024;
}
};


/**
* Converst bytes to human readable unit.
* Thank you Amir from StackOverflow.
*
* @param {Number} bytes
* @return {String}
*/
var units = ' KMGTPEZYXWVU';
exports.getHumanSize = function(bytes) {
if (bytes <= 0) { return 0; }
var t2 = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), 12);
return (Math.round(bytes * 100 / Math.pow(1024, t2)) / 100) +
units.charAt(t2).replace(' ', '') + 'B';
};


/**
* Converts ms to human readable time.
*
* @param {Number} ms
* @return {String}
*/
exports.getHumanTime = function(ms) {
var d, h, m, s, set, str, x;
x = ms / 1000;
ms %= 1000;
s = Math.round(x % 60);
x /= 60;
m = Math.round(x % 60);
x /= 60;
h = Math.round(x % 24);
d = Math.round(x / 24);

str = '';
if (d > 0) {
str += d + ' day' + (d > 1 ? 's' : '') + ', ';
set = true;
}
if (set || h > 0) {
str += h + ' hour' + (h > 1 ? 's' : '') + ', ';
set = true;
}
if (set || m > 0) {
str += m + ' minute' + (m > 1 ? 's' : '') + ', ';
set = true;
}
if (set || s > 0) {
str += s + ' second' + (s > 1 ? 's' : '') + ', ';
}

return str + ms + ' ms';
};
Loading

0 comments on commit 604fa7b

Please sign in to comment.