2016-12-11 5 views
1

ytdl-coreモジュール(https://github.com/fent/node-ytdl-core)を使用してYoutubeビデオオーディオをダウンロードしようとしています。Express APIとytdlでオーディオファイルをダウンロードする

私は私がそのURLでオーディオをダウンロードすることができますExpressを使用してAPIを書いた:

app.get('/api/downloadYoutubeVideo', function (req, res) { 
    res.set('Content-Type', 'audio/mpeg');  

    var videoUrl = req.query.videoUrl; 
    var videoName; 

    ytdl.getInfo(videoUrl, function(err, info){ 
     videoName = info.title.replace('|','').toString('ascii'); 
     res.set('Content-Disposition', 'attachment; filename=' + videoName + '.mp3');  
    }); 

    var videoWritableStream = fs.createWriteStream('C:\\test' + '\\' + videoName); // some path on my computer (exists!) 
    var videoReadableStream = ytdl(videoUrl, { filter: 'audioonly'}); 

    var stream = videoReadableStream.pipe(videoWritableStream); 

}); 

問題は、私は、このAPIを呼び出すときに、私は私のサーバーから504エラーが出るということです。

ダウンロードしたオーディオをローカルディスクに保存したいと考えています。

助けていただければ幸いです。ありがとう

答えて

0

何かの理由でvideoNameが定義されていないので、それは私の機能を混乱させました... ここでは、いくつかの変更の後に正しいコードがあり、クエリ変数として宛先パスを追加しています。

app.get('/api/downloadYoutubeVideo', function (req, res) { 
    var videoUrl = req.query.videoUrl; 
    var destDir = req.query.destDir; 

    var videoReadableStream = ytdl(videoUrl, { filter: 'audioonly'}); 

    ytdl.getInfo(videoUrl, function(err, info){ 
     var videoName = info.title.replace('|','').toString('ascii'); 

     var videoWritableStream = fs.createWriteStream(destDir + '\\' + videoName + '.mp3'); 

     var stream = videoReadableStream.pipe(videoWritableStream); 

     stream.on('finish', function() { 
      res.writeHead(204); 
      res.end(); 
     });    
    });    
}); 
関連する問題