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

Commit

Permalink
fix for failed download with no subtitles when requested
Browse files Browse the repository at this point in the history
Cleanup, traverse the list of args backwards, match all possible languages, regex in global var
  • Loading branch information
przemyslawpluta committed Aug 1, 2014
1 parent 36fd75a commit 82a48a4
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,26 @@ fs.exists(file, function(exists) {
});

var isYouTubeRegex = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\//;
var isNoSubsRegex = /WARNING: video doesn't have subtitles/;
var subs = ['--write-sub', '--write-srt', '--srt-lang', '--all-subs'];

// Check if win.
var isWin = /^win/.test(process.platform);

/**
* Regular Expresion IndexOf for Arrays
*
* @param {Array.<String>} arr
* @param {String} rx
*/
function reIndexOf(arr, rx) {
var i;
for (i in arr) {
if (arr[i].toString().match(rx)) { return i; }
}
return -1;
}

/**
* Downloads a video.
*
Expand Down Expand Up @@ -71,7 +87,6 @@ var ytdl = module.exports = function(url, args, options) {
* @param {Function(!Error, String)} callback
*/
function call(video, args1, args2, options, callback) {

var args = args1.concat(util.parseOpts(args2));

// Parse url.
Expand Down Expand Up @@ -105,17 +120,17 @@ function call(video, args1, args2, options, callback) {
if (stderr) {

// Try once to download video if no subtitles available
if (stderr.indexOf('WARNING: video doesn\'t have subtitles') !== -1 && !options.nocc) {
var cleanupOpt = opt[1];
if (!options.nocc && isNoSubsRegex.test(stderr)) {
var cleanupOpt = opt[1].reverse();

['--write-sub', '--srt-lang=en', '--all-subs'].map(function map(item) {
var target = cleanupOpt.indexOf(item);
subs.map(function map(item) {
var target = reIndexOf(cleanupOpt, item);
if (target > -1) { cleanupOpt.splice(target, 1); }
});

options.nocc = true;

return call(video, args1, cleanupOpt, options, callback);
return call(video, args1, cleanupOpt.reverse(), options, callback);

}

Expand Down

0 comments on commit 82a48a4

Please sign in to comment.