2016-12-11 2 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

어떤 이유로 비디오 이름이 정의되지 않았기 때문에 내 기능이 엉망이되었습니다. 몇 가지 변경 사항을 적용하고 대상 경로를 쿼리 변수로 추가하면 올바른 코드가 나타납니다.

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(); 
     });    
    });    
});