2013-08-11 5 views
5

youtube url을 mp3 파일로 변환하고 싶습니다. 현재, 그래서 같이 노드의 ytdl 모듈을 사용하여 MP4 다운로드 : 다운로드가 완료node.js : 파이프하는 법 - youtube to mp3 to mp3

fs = require 'fs' 
ytdl = require 'ytdl' 

url = 'http://www.youtube.com/watch?v=v8bOTvg-iaU' 
mp4 = './video.mp4' 

ytdl(url).pipe(fs.createWriteStream(mp4)) 

되면, 정말처럼 유창-는 FFmpeg 모듈을 사용하여 MP3에 MP4 변환 :

ffmpeg = require 'fluent-ffmpeg' 

mp4 = './video.mp4' 
mp3 = './audio.mp3' 

proc = new ffmpeg({source:mp4}) 
proc.setFfmpegPath('/Applications/ffmpeg') 
proc.saveToFile(mp3, (stdout, stderr)-> 
      return console.log stderr if err? 
      return console.log 'done' 
     ) 

mp3 변환을 시작하기 전에 전체 mp4를 저장하고 싶지 않습니다. 어떻게 mp4를 proc으로 파이프하여 mp4 청크를 수신 할 때 변환을 수행합니까? 대신 MP4 파일의 위치를 ​​전달

답변

8

,과 같이, 소스로 ytdl 스트림에 전달합니다

stream = ytdl(url) 

proc = new ffmpeg({source:stream}) 
proc.setFfmpegPath('/Applications/ffmpeg') 
proc.saveToFile(mp3, (stdout, stderr)-> 
      return console.log stderr if err? 
      return console.log 'done' 
     ) 
+0

정확하게 이해하면 이것은 여전히 ​​전체 mp4를 다운로드하지만 저장 및 변환하는 대신 다운로드/스트리밍하는 동안 변환합니다. 원본 mp4 파일이 80MB이고 결과 mp3가 6MB라면 80MB가 다운로드됩니다. –

+0

mp4가 스트림을 통해 당신에게 전송됩니다, 내가 의심스럽게 sepreate mp3 스트림을 요청하게 할 것입니다 .. 당신은 다른 작품에서 모든 것을 dl해야만합니다. –

0

이 나를 위해 작동하지 않습니다. 로컬 .mp4 파일을 설정했지만 스트림을 사용하는 경우 아래 코드는 작동하지 않습니다.

var ytUrl = 'http://www.youtube.com/watch?v=' + data.videoId; 
     var stream = youtubedl(ytUrl, { 
      quality: 'highest' 
     }); 
     var saveLocation = './mp3/' + data.videoId + '.mp3'; 

     var proc = new ffmpeg({ 
      source: './mp3/test.mp4' //using 'stream' does not work 
     }) 
      .withAudioCodec('libmp3lame') 
      .toFormat('mp3') 
      .saveToFile(saveLocation, function(stdout, stderr) { 
       console.log('file has been converted succesfully'); 
      }); 
+0

응답으로 다운로드 %를 보여주는 방법. –

0

이 비교적 오래된 질문이지만, 미래에 사람을 도움이 될 수 있습니다 - 서버의 파일을 저장할 필요없이 MP3로 유튜브 장해를 다운로드 할 수있는 유사한 솔루션을 찾을 때 내가 나 자신 우연히. 나는 기본적으로 응답에 대한 변환을 직접 처리하기로 결정했으며, 내가 기대했던대로 작업하고있다.

는 원래 다른 스레드에서이 질문에 대답 ffmpeg mp3 streaming via node js

module.exports.toMp3 = function(req, res, next){ 
var id = req.params.id; // extra param from front end 
var title = req.params.title; // extra param from front end 
var url = 'https://www.youtube.com/watch?v=' + id; 
var stream = youtubedl(url); //include youtbedl ... var youtubedl = require('ytdl'); 

//set response headers 
res.setHeader('Content-disposition', 'attachment; filename=' + title + '.mp3'); 
res.setHeader('Content-type', 'audio/mpeg'); 

//set stream for conversion 
var proc = new ffmpeg({source: stream}); 

//currently have ffmpeg stored directly on the server, and ffmpegLocation is the path to its location... perhaps not ideal, but what I'm currently settled on. And then sending output directly to response. 
proc.setFfmpegPath(ffmpegLocation); 
proc.withAudioCodec('libmp3lame') 
    .toFormat('mp3') 
    .output(res) 
    .run(); 
proc.on('end', function() { 
    console.log('finished'); 
}); 

};