2017-08-07 55 views
0

我有一個javascript腳本的youtube-dl,試圖下載數百個標題文件。我得到錯誤youtube-dl javascript腳本錯誤

我有一個JavaScript腳本。我正在嘗試使用youtube-dl下載〜800個標題文件,我得到超時錯誤並且不下載文件。我的電腦似乎正在移動太快。我需要幫助暫停任務,直到下載完成,然後開始下一個任務。

它在視頻陣列中的工作量非常少。

var json2csv = require('json2csv'); 
var fs = require('fs'); 
var youtubedl = require('youtube-dl'); 

// var fields = ["youtube_id", "title", "date", "duration", "captioned", "views"]; 

var videos = [ 
// More objects ~800+ 
]; 
for (i = 0; i < videos.length; i++) { 

    var v = videos[i]; 
    var url = 'https://youtu.be/'; 
    var options = {}; 

    if (v["captioned"] == 'No') { 
     var url = url + v["youtube_id"]; 

     console.log(url); 
     var options = { 
     auto: true, 
     all: false, 
     lang: 'en', 
     cwd: __dirname + "/auto_generated_captions", 
     }; 
       youtubedl.getSubs(url, options, function(err, files) { 
       console.log("did i get here?"); 
       if (err) throw err; 
       console.log('subtitle files downloaded:', files); 
       }); 

    }; 
    }; 

回答

1

你是對的。你同時下載了太多的數據。嘗試使用藍鳥這樣的承諾庫來控制併發流程:

var json2csv = require('json2csv'); 
var fs = require('fs'); 
var youtubedl = require('youtube-dl'); 
var promise = require('bluebird'); 

// var fields = ["youtube_id", "title", "date", "duration", "captioned", "views"]; 

var videos = [ 
    // More objects ~800+ 
]; 

promise 
    .map(videos, function (v) { 
    var url = 'https://youtu.be/'; 
    var options = {}; 

    if (v["captioned"] == 'No') { 
     var url = url + v["youtube_id"]; 

     console.log(url); 
     var options = { 
     auto: true, 
     all: false, 
     lang: 'en', 
     cwd: __dirname + "/auto_generated_captions", 
     }; 

     return new Promise(function (resolve, reject) { 
     youtubedl.getSubs(url, options, function (err, files) { 
      console.log("did i get here?"); 

      if (err) { 
      reject(err); 
      } else { 
      console.log('subtitle files downloaded:', files); 
      resolve(files); 
      } 
     }); 
     }); 
    } else { 
     // return a promise for this case 
    } 
    }, { concurrency: 5 }) 
    .then(function (results) { 
    console.log(results); 
    });