常常需要用到,記錄一下
var request = require("request");
var fs = require("fs");
var filePath = 'https://www.youtubeinmp3.com/fetch/?
video=https://www.youtube.com/watch?v=LtpBgAHNVJo';
var saveFile = '三年二班.mp3';
downloadFile(filePath,saveFile).then(function(){
console.log('下載完成')
}).catch(err => console.error(err))
function downloadFile(filePath,saveFile){
return new Promise(function(resolve, reject){
request.get(filePath).pipe(fs.createWriteStream(saveFile).on('finish',function(err){
if (err) { reject() }
resolve();
}))
})
}