我正在编写一个从youtube下载视频的nodejs函数。无论视频是否已下载,该功能都应返回。下面的代码应该打印 Invalid Link ,但我得到 undefined 。我是NodeJs和Js的新手。请帮助。
var youtubedl = require('youtube-dl');
var fs = require('fs');
var videoName="";
var results="";
function getVideo(videoLink) {
var video = youtubedl(videoLink);
video.on('error',function (err) {
//console.log(err.message);
results = "Invalid Link"
});
video.on('info', function (info) {
console.log('Download started');
//console.log('Title: ' + info.title);
//console.log('Description: ' + info.description);
//console.log('size: ' + info.size);
videoName = info.title;
});
video.on('end', function () {
//console.log("Finished Downloading Video");
results = "Finished Downloading Video";
});
video.pipe(fs.createWriteStream('youtubeVideo.mp4'));
return results;
};
console.log(getVideo("Some Invalid Link"));
module.exports.getVideo = getVideo;