Skip to content

Commit

Permalink
add getSubs() function. fixes przemyslawpluta#48
Browse files Browse the repository at this point in the history
  • Loading branch information
fent authored and t3rr0r committed Nov 22, 2014
1 parent 3ec3c4b commit 8ca52a4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ Will print something like
{ itag: 5, filetype: 'flv', resolution: '240x400' }
{ itag: 17, filetype: 'mp4', resolution: '144x176' }

## Downloading subtitles

```js
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][].


Expand Down
37 changes: 37 additions & 0 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,40 @@ ytdl.getFormats = function(url, args, callback) {
callback(null, formats);
});
};

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

var args = [];
args.push('--write' + (options.auto ? '-auto' : '') + '-sub');
if (options.all) {
args.push('--all-subs');
}
if (options.lang) {
args.push('--sub-lang=' + options.lang);
}
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];
if (line.indexOf('[info] Writing video subtitles to: ') === 0) {
files.push(line.slice(35));
}
}
callback(null, files);
});
};
15 changes: 6 additions & 9 deletions test/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 subtitleFile = '1 1 1-179MiZSibco.en.srt';


vows.describe('download').addBatch({
Expand Down Expand Up @@ -59,18 +60,14 @@ vows.describe('download').addBatch({
},
'a video with subtitles': {
topic: function() {
var args = ['--max-quality=22', '--write-srt', '--srt-lang=en'];
var dl = ytdl(video2, args, { cwd: __dirname });
var cb = this.callback;
dl.on('error', cb);
dl.on('end', cb);
dl.resume();
fs.unlinkSync(path.join(__dirname, subtitleFile));
ytdl.getSubs(video2, { lang: 'en', cwd: __dirname }, this.callback);
},

'subtitles were downloaded': function(err) {
'subtitles were downloaded': function(err, files) {
if (err) throw err;
var filepath = path.join(__dirname, '1 1 1-179MiZSibco.en.srt');
assert.isTrue(fs.existsSync(filepath));
assert.equal(files[0], subtitleFile);
assert.isTrue(fs.existsSync(path.join(__dirname, subtitleFile)));
}
}
}).export(module);

0 comments on commit 8ca52a4

Please sign in to comment.