itsghostinc/j


License: MIT

Language: JavaScript


youtube-dl

Build Status npm version Dependency Status

Download videos from youtube in node.js using youtube-dl.

I also made a pure Javascript youtube downloading module. The reason I'm maintaining this one is because it supports a lot more video sites besides youtube.

If you're only interested in downloading only from youtube, you should consider using the other module.

Installation

With npm do:

npm install youtube-dl

Usage

Downloading videos

var fs = require('fs');
var youtubedl = require('youtube-dl');
var video = youtubedl('http://www.youtube.com/watch?v=90AiXO1pAiA',
  // Optional arguments passed to youtube-dl.
  ['--format=18'],
  // Additional options can be given for calling `child_process.execFile()`.
  { cwd: __dirname });

// Will be called when the download starts.
video.on('info', function(info) {
  console.log('Download started');
  console.log('filename: '   info.filename);
  console.log('size: '   info.size);
});

video.pipe(fs.createWriteStream('myvideo.mp4'));

It will produce an output that looks like the following when ran.

Got video info
saving to T-ara - Number Nine - MV - 티아라-Seku9G1kT0c.mp4
100.00%

Resuming partially downloaded videos

var youtubedl = require('./');
var fs = require('fs');
var output = 'myvideo.mp4';

var downloaded = 0;
if (fs.existsSync(output)) {
  downloaded = fs.statSync(output).size;
}

var video = youtubedl('https://www.youtube.com/watch?v=179MiZSibco',

  // Optional arguments passed to youtube-dl.
  ['--format=18'],

  // start will be sent as a range header
  { start: downloaded, cwd: __dirname });

// Will be called when the download starts.
video.on('info', function(info) {
  console.log('Download started');
  console.log('filename: '   info._filename);

  // info.size will be the amount to download, add
  var total = info.size   downloaded;
  console.log('size: '   total);

  if (downloaded > 0) {
    // size will be the amount already downloaded
    console.log('resuming from: '   downloaded);

    // display the remaining bytes to download
    console.log('remaining bytes: '   info.size);
  }
});

video.pipe(fs.createWriteStream('myvideo.mp4', { flags: 'a' }));

// Will be called if download was already completed and there is nothing more to download.
video.on('complete', function complete(info) {
  'use strict';
  console.log('filename: '   info._filename   ' already downloaded.');
});

video.on('end', function() {
  console.log('finished downloading!');
});

It will produce an output that looks like the following when ran.

Output:

[jay@macpro ~/nodejs/node-youtube-dl/example]$ node resume.js
Download started
filename: 1 1 1-179MiZSibco.mp4
size: 5109213
^C
[jay@macpro ~/nodejs/node-youtube-dl/example]$ node resume.js
Download started
filename: 1 1 1-179MiZSibco.mp4
size: 5109213
resuming from: 917504
remaining bytes: 4191709
finished downloading!
[jay@macpro ~/nodejs/node-youtube-dl/example]$

Getting video information

var youtubedl = require('youtube-dl');
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('format id:', info.format_id);
});

Running that will produce something like

id: WKsjaOqDXgg
title: Ace Rimmer to the Rescue
url: http://r5---sn-p5qlsn7e.c.youtube.com/videoplayback?ms=au&ip=160.79.125.18&cp=U0hWTFVQVl9FTENONl9NSlpDOjgtU1VsODlkVmRH&id=58ab2368ea835e08&source=youtube&expire=1377558202&factor=1.25&key=yt1&ipbits=8&mt=1377534150&itag=34&sver=3&upn=-rGWz2vYpN4&fexp=912306,927900,919395,926518,936203,913819,929117,929121,929906,929907,929922,929127,929129,929131,929930,925726,925720,925722,925718,929917,906945,929919,929933,912521,932306,913428,904830,919373,930803,908536,904122,938701,936308,909549,900816,912711,904494,904497,900375,906001&sparams=algorithm,burst,cp,factor,id,ip,ipbits,itag,source,upn,expire&mv=m&burst=40&algorithm=throttle-factor&signature=ABD3A847684AD9B39331E567568D3FA0DCFA4776.7895521E130A042FB3625A17242CE3C02A4460B7&ratebypass=yes
thumbnail: https://i1.ytimg.com/vi/WKsjaOqDXgg/hqdefault.jpg
description: An old Red Dwarf eposide where Ace Rimmer saves the Princess Bonjella.
filename: Ace Rimmer to the Rescue-WKsjaOqDXgg.flv
format id: 34

You can use an array of urls to produce an array of response objects with matching array index (e.g. the 1st response object will match the first url etc...)

var youtubedl = require('youtube-dl');
var url1 = 'http://www.youtube.com/watch?v=WKsjaOqDXgg';
var url2 = 'https://vimeo.com/6586873';
youtubedl.getInfo([url1, url2], function(err, info) {
  if (err) throw err;

  console.log('title for the url1:', info[0].title);
  console.log('title for the url2:', info[1].title);
});

Downloading subtitles

var youtubedl = require('youtube-dl');
var url = 'https://youtu.be/PizwcirYuGY';

var options = {
  // Write automatic subtitle file (youtube only)
  auto: false,
  // Downloads all the available subtitles.
  all: false,
  // Languages of subtitles to download, separated by commas.
  lang: 'en',
  // The directory to save the downloaded files in.
  cwd: __dirname,
};
youtubedl.getSubs(url, options, function(err, files) {
  if (err) throw err;

  console.log('subtitle files downloaded:', files);
});

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.

Downloading playlists

var path = require('path');
var fs   = require('fs');
var ytdl = require('youtube-dl');

function playlist(url) {

  'use strict';
  var video = ytdl(url);

  video.on('error', function error(err) {
    console.log('error 2:', err);
  });

  var size = 0;
  video.on('info', function(info) {
    size = info.size;
    var output = path.join(__dirname   '/', size   '.mp4');
    video.pipe(fs.createWriteStream(output));
  });

  var pos = 0;
  video.on('data', function data(chunk) {
    pos  = chunk.length;
    // `size` should not be 0 here.
    if (size) {
      var percent = (pos / size * 100).toFixed(2);
      process.stdout.cursorTo(0);
      process.stdout.clearLine(1);
      process.stdout.write(percent   '%');
    }
  });

  video.on('next', playlist);

}

playlist('https://www.youtube.com/playlist?list=PLEFA9E9D96CB7F807');

Getting the list of extractors

var youtubedl = require('youtube-dl');
youtubedl.getExtractors(true, function(err, list) {
  console.log('Found '   list.length   ' extractors');
  for (var i = 0; i < list.length; i  ) {
    console.log(list[i]);
  }
});

Will print something like

Found 521 extractors
1up.com
220.ro
24video
3sat

Call the youtube-dl binary directly

This module doesn't have youtube-dl download the video. Instead, it uses the url key from the --dump-json CLI option to create a node stream. That way, it can be used like any other node stream.

If that, or none of the above support your use case, you can use ytdl.exec() to call youtube-dl however you like.

ytdl.exec(url, ['-x', '--audio-format', 'mp3'], {}, function(err, output) {
  if (err) throw err;
  console.log(output.join('\n'));
});

Update

Since the youtube-dl binary is updated regularly, you can run npm run update to check for and download any updates for it. You can also require ../lib/downloader in your app if you'd like to place youtube-dl binary in a specific directory and control when it gets updates.

var downloader = require('../lib/downloader');

downloader('path/to-binary', function error(err, done) {
  'use strict';
  if (err) { return console.log(err.stack); }
  console.log(done);
});

Tests

Tests are written with vows

npm test

License

MIT

Project Statistics

Sourcerank 3
Repository Size 1.64 MB
Stars 0
Forks 0
Watchers 0
Open issues 0
Dependencies 0
Contributors 16
Tags 53
Created
Last updated
Last pushed

Top Contributors See all

fent Przemyslaw Pluta Jay Salvat Jaime Marquínez Ferrándiz Jay Baker Udo Kramer trashman Sergey M. tifroz Jason Penny ▟ ▖▟ ▖ Nicolas Gotchac coderaiser Davide Pastore Dave Squires ❤️    Farrin Reid

Recent Tags See all

v1.11.1 April 06, 2016
v1.11.0 March 29, 2016
v1.10.5 May 07, 2015
v1.10.4 May 07, 2015
v1.10.3 April 19, 2015
v1.10.2 March 24, 2015
v1.10.1 February 26, 2015
v1.10.0 February 19, 2015
v1.9.0 February 13, 2015
v1.8.0 January 15, 2015
v1.7.0 January 08, 2015
v1.6.0 November 23, 2014
v1.5.16 October 24, 2014
v1.5.15 September 25, 2014
v1.5.14 September 24, 2014

Something wrong with this page? Make a suggestion

Last synced: 2016-12-07 01:11:42 UTC

Login to resync this repository