Skip to content

Commit

Permalink
remove ytdl-core parts
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Jun 30, 2014
1 parent 1bea85c commit efdf822
Show file tree
Hide file tree
Showing 29 changed files with 50 additions and 10,128 deletions.
64 changes: 6 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,10 @@
# node-ytdl [![Build Status](https://secure.travis-ci.org/fent/node-ytdl.png)](http://travis-ci.org/fent/node-ytdl)

Yet another youtube downloading module. This time written with only Javascript and a more node-friendly streaming interface.

A youtube downloader written in Javascript. To be used with the command line. If you're looking to use it in your node program, check out [ytdl-core](https://github.com/fent/node-ytdl-core).

# Usage

```js
var fs = require('fs');
var ytdl = require('ytdl');

ytdl('http://www.youtube.com/watch?v=A02s8omM_hI')
.pipe(fs.createWriteStream('video.flv'));
```


# API
### ytdl(url, options)

Attempts to download a video from the given url. Returns a readable stream. `options` can have the following keys

* `quality` - Video quality to download. Can be an [itag value](http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs) value, `highest`, or `lowest`. Defaults to `highest`.
* `filter` - You can give a filtering function that gets called with each format available. Used to decide what format to download. This function is given the `format` object as its first argument, and should return true if the format is preferable.
* `range` - A byte range in the form `INT-INT` that specifies a part of the video to download. ie 10355705-12452856.

```js
// Example with `filter` option.
ytdl(url, { filter: function(format) { return format.container === 'mp4'; } })
.pipe(fs.createWriteStream('vide.mp4'));
```

`options` can also have any [request](https://github.com/mikeal/request) options.

The returned readable stream emits these additional events.

### Event: 'info'
* `Object` - Info.
* `Object` - Format.

Emitted when the a video's `info` hash is fetched. Along with the chosen format metadata to download. `format.url` might be different if `start` was given. `format.size` will also be available.

Info and format may look like [this](https://gist.github.com/fent/6c8251132e1addb5121e).

### ytdl.getInfo(url, [options], callback(err, info))

Use this if you only want to get metainfo from a video.

`options` gets passed to the `request()`, it can also have a `downloadURL` property set to `true` if you want ytdl to include the download url instead of the regular one. In some cases, a signature needs to be deciphered, and will require ytdl to make additional requests.

### ytdl.cache

A [memory cache](https://github.com/hij1nx/EventVat) is used to store information about recently retrieved videos. This is used to prevent double requests on videos that you want to retrieve the info of, and then download.


# Install

npm install ytdl

# CLI

ytdl can be used from the command line too. Install with the `-g` flag to use it.

ytdl http://www.youtube.com/watch?v=_HSylqgVYQI > cat.flv
ytdl http://www.youtube.com/watch?v=_HSylqgVYQI > myvideo.flv

And it streams!

Expand All @@ -81,6 +25,10 @@ And it streams!
--unfilter-encoding REGEXP Filter out format encoding.
-i, --info Print video info without downloading

# Install

[sudo] npm -g install ytdl


# Tests
Tests are written with [mocha](http://visionmedia.github.com/mocha/)
Expand Down
42 changes: 4 additions & 38 deletions bin/ytdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

var path = require('path');
var fs = require('fs');
var ytdl = require('..');
var ytdl = require('ytdl-core');
var cliff = require('cliff');
var util = require('../lib/util');
require('colors');


Expand Down Expand Up @@ -80,31 +81,6 @@ var opts = require('nomnom')
;


/**
* Converts seconds into human readable time hh:mm:ss
*
* @param {Number} seconds
* @return {String}
*/
function toHumanTime(seconds) {
var h = Math.floor(seconds / 3600);
var m = Math.floor(seconds / 60) % 60;

var time;
if (h > 0) {
time = h + ':';
if (m < 10) { m = '0' + m; }
} else {
time = '';
}

var s = seconds % 60;
if (s < 10) { s = '0' + s; }

return time + m + ':' + s;
}


/**
* Prints basic video information.
*
Expand All @@ -118,7 +94,7 @@ function printVideoInfo(info) {
info.avg_rating.toFixed(1) : info.avg_rating;
console.log('average rating: '.grey.bold + rating);
console.log('view count: '.grey.bold + info.view_count);
console.log('length: '.grey.bold + toHumanTime(info.length_seconds));
console.log('length: '.grey.bold + util.toHumanTime(info.length_seconds));
}


Expand Down Expand Up @@ -219,24 +195,14 @@ readStream.on('error', function(err) {
process.exit(1);
});

// Converst bytes to human readable unit.
// Thank you Amir from StackOverflow.
var units = ' KMGTPEZYXWVU';
function toHumanSize(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';
}

// Print progress bar and some video info if not streaming to stdout.
if (output) {
readStream.on('info', function(info, format) {
printVideoInfo(info);
console.log('container: '.grey.bold + format.container);
console.log('resolution: '.grey.bold + format.resolution);
console.log('encoding: '.grey.bold + format.encoding);
console.log('size: '.grey.bold + toHumanSize(format.size));
console.log('size: '.grey.bold + util.toHumanSize(format.size));
console.log('output: '.grey.bold + output);
console.log();

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.warn('This module is meant to be used by cli. If you want to use ytdl programmatically, visit https://github.com/fent/node-ytdl-core');
module.exports = require('ytdl-core');
39 changes: 0 additions & 39 deletions lib/crequest.js

This file was deleted.

Loading

0 comments on commit efdf822

Please sign in to comment.