我在javascript脚本中有youtube-dl,试图下载数百个字幕文件。我收到错误
我有一个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);
});
};
};
答案 0 :(得分:1)
你是对的。您同时下载了太多数据。尝试使用promise bird控制并发流,如bluebird:
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);
});