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

Commit

Permalink
handle thumbnails download
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslawpluta committed Sep 29, 2017
1 parent eb0e9de commit a0832dc
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 207 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,24 @@ youtubedl.getSubs(url, options, function(err, files) {
});
```

### Downloading thumbnails

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

var options = {
// Downloads available thumbnail.
all: false,
// The directory to save the downloaded files in.
cwd: __dirname,
};
youtubedl.getThumbs(url, options, function(err, files) {
if (err) throw err;
console.log('thumbnail file 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
Expand Down
8 changes: 8 additions & 0 deletions example/thumbs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var ytdl = require('..');
var url = 'https://www.youtube.com/watch?v=yy7EUIR0fic';

ytdl.getThumbs(url, { cwd: __dirname }, function thumbs(err, files) {
'use strict';
if (err) { throw err; }
console.dir(files);
});
41 changes: 41 additions & 0 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,47 @@ ytdl.getSubs = function getSubs(url, options, callback) {
});
};

/**
* @param {String} url
* @param {Object} options
* {Boolean} all
* {String} cwd
* @param {Function(!Error, Object)} callback
*/
ytdl.getThumbs = function getSubs(url, options, callback) {
'use strict';
if (typeof options === 'function') {
callback = options;
options = {};
}

var args = ['--skip-download'];

if (options.all) {
args.push('--write-all-thumbnails');
} else {
args.push('--write-thumbnail');
}

if (!options.warrning) {
args.push('--no-warnings');
}

call(url, args, [], { cwd: options.cwd }, function(err, data) {
if (err) { return callback(err); }

var files = [];
for (var i = 0, len = data.length; i < len; i++) {
var line = data[i];
var info = 'Writing thumbnail to: ';
if (line.indexOf(info) !== -1) {
files.push(line.slice(line.indexOf(info) + info.length));
}
}
callback(null, files);
});
};

/**
* @param {!Boolean} descriptions
* @param {!Object} options
Expand Down
264 changes: 142 additions & 122 deletions test/download.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,153 @@
var vows = require('vows');
var ytdl = require('..');
var fs = require('fs');
var path = require('path');
var vows = require('vows');
var ytdl = require('..');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var video1 = 'http://www.youtube.com/watch?v=90AiXO1pAiA';
var video2 = 'https://www.youtube.com/watch?v=179MiZSibco';
var video3 = 'https://www.youtube.com/watch?v=AW8OOp2undg';
var video4 = 'https://www.youtube.com/watch?v=yy7EUIR0fic';
var subtitleFile = '1 1 1-179MiZSibco.en.vtt';
var thumbnailFile = 'Too Many Twists (Heist Night 5_5)-yy7EUIR0fic.jpg';

vows.describe('download').addBatch({
'a video with format specified': {
'topic': function() {
'use strict';
var dl = ytdl(video1, ['-f', '18']);
var cb = this.callback;

dl.on('error', cb);

dl.on('info', function(info) {
var pos = 0;
var progress;

dl.on('data', function(data) {
pos += data.length;
progress = pos / info.size;
});

dl.on('end', function() {
cb(null, progress, info);
});

var filepath = path.join(__dirname, info._filename);
dl.pipe(fs.createWriteStream(filepath));
});
'a video with format specified': {
'topic': function() {
'use strict';
var dl = ytdl(video1, ['-f', '18']);
var cb = this.callback;

dl.on('error', cb);

dl.on('info', function(info) {
var pos = 0;
var progress;

dl.on('data', function(data) {
pos += data.length;
progress = pos / info.size;
});

dl.on('end', function() {
cb(null, progress, info);
});

var filepath = path.join(__dirname, info._filename);
dl.pipe(fs.createWriteStream(filepath));
});
},

'data returned': function(err, progress, data) {
'use strict';
if (err) { throw err; }

assert.equal(progress, 1);
assert.isObject(data);
assert.equal(data.id, '90AiXO1pAiA');
assert.isTrue(/lol-90AiXO1pAiA/.test(data._filename));
assert.equal(data.size, 755906);
},

'file was downloaded': function(err, progress, data) {
'use strict';
if (err) { throw err; }

// Check existance.
var filepath = path.join(__dirname, data._filename);
var exists = fs.existsSync(filepath);
if (exists) {
// Delete file after each test.
fs.unlinkSync(filepath);
} else {
assert.isTrue(exists);
}
}
},

'data returned': function(err, progress, data) {
'use strict';
if (err) { throw err; }

assert.equal(progress, 1);
assert.isObject(data);
assert.equal(data.id, '90AiXO1pAiA');
assert.isTrue(/lol-90AiXO1pAiA/.test(data._filename));
assert.equal(data.size, 755906);
},

'file was downloaded': function(err, progress, data) {
'use strict';
if (err) { throw err; }

// Check existance.
var filepath = path.join(__dirname, data._filename);
var exists = fs.existsSync(filepath);
if (exists) {
// Delete file after each test.
fs.unlinkSync(filepath);
} else {
assert.isTrue(exists);
}
}
},
'a video with no format specified': {
'topic': function() {
'use strict';
var dl = ytdl(video3);
var cb = this.callback;

dl.on('error', cb);

dl.on('info', function(info) {
var pos = 0;
var progress;

dl.on('data', function(data) {
pos += data.length;
progress = pos / info.size;
});

dl.on('end', function() {
cb(null, progress, info);
});

var filepath = path.join(__dirname, info._filename);
dl.pipe(fs.createWriteStream(filepath));
});
},

'data returned': function(err, progress, data) {
'use strict';
if (err) { throw err; }

assert.equal(progress, 1);
assert.isObject(data);
assert.equal(data.id, 'AW8OOp2undg');
assert.equal(data.size, 34289);
'a video with no format specified': {
'topic': function() {
'use strict';
var dl = ytdl(video3);
var cb = this.callback;

dl.on('error', cb);

dl.on('info', function(info) {
var pos = 0;
var progress;

dl.on('data', function(data) {
pos += data.length;
progress = pos / info.size;
});

dl.on('end', function() {
cb(null, progress, info);
});

var filepath = path.join(__dirname, info._filename);
dl.pipe(fs.createWriteStream(filepath));
});
},

'data returned': function(err, progress, data) {
'use strict';
if (err) { throw err; }

assert.equal(progress, 1);
assert.isObject(data);
assert.equal(data.id, 'AW8OOp2undg');
assert.equal(data.size, 34289);
},

'file was downloaded': function(err, progress, data) {
'use strict';
if (err) { throw err; }

// Check existance.
var filepath = path.join(__dirname, data._filename);
var exists = fs.existsSync(filepath);
if (exists) {
// Delete file after each test.
fs.unlinkSync(filepath);
} else {
assert.isTrue(exists);
}
}
},

'file was downloaded': function(err, progress, data) {
'use strict';
if (err) { throw err; }

// Check existance.
var filepath = path.join(__dirname, data._filename);
var exists = fs.existsSync(filepath);
if (exists) {
// Delete file after each test.
fs.unlinkSync(filepath);
} else {
assert.isTrue(exists);
}
}
},
'a video with subtitles': {
topic: function() {
'use strict';
try {
fs.unlinkSync(path.join(__dirname, subtitleFile));
} catch (err) {}
ytdl.getSubs(video2, { lang: 'en', cwd: __dirname }, this.callback);
'a video with subtitles': {
topic: function() {
'use strict';
try {
fs.unlinkSync(path.join(__dirname, subtitleFile));
} catch (err) {}
ytdl.getSubs(video2, { lang: 'en', cwd: __dirname }, this.callback);
},

'subtitles were downloaded': function(err, files) {
'use strict';
if (err) { throw err; }

assert.equal(files[0], subtitleFile);
assert.isTrue(fs.existsSync(path.join(__dirname, subtitleFile)));
fs.unlinkSync(path.join(__dirname, subtitleFile));
}
},

'subtitles were downloaded': function(err, files) {
'use strict';
if (err) { throw err; }

assert.equal(files[0], subtitleFile);
assert.isTrue(fs.existsSync(path.join(__dirname, subtitleFile)));
fs.unlinkSync(path.join(__dirname, subtitleFile));
'a video with thumbnail': {
topic: function() {
'use strict';
try {
fs.unlinkSync(path.join(__dirname, thumbnailFile));
} catch (err) {}
ytdl.getThumbs(video4, { cwd: __dirname }, this.callback);
},

'thumbnail was downloaded': function(err, files) {
'use strict';
if (err) { throw err; }

assert.equal(files[0], thumbnailFile);
assert.isTrue(fs.existsSync(path.join(__dirname, thumbnailFile)));
fs.unlinkSync(path.join(__dirname, thumbnailFile));
}
}
}
}).export(module);
}).export(module);
Loading

0 comments on commit a0832dc

Please sign in to comment.