2016-08-15 199 views
0
var fs = require('fs'); 
var child = require('child_process'); 
var http=require('http') 
var input_file = fs.createReadStream('./remo.mp3'); 
http.createServer(function (req,res) { 
var args = ['-ss',120, 
'-i', 'remo.mp3', 
'-f','mp3', 
'pipe:1' // Output on stdout 
]; 
var trans_proc = child.spawn('ffmpeg', args); 
res.writeHead(200, { 
     'Content-Type': 'audio/mpeg' 
    }); 

trans_proc.stdout.pipe(res) 
trans_proc.stderr.on('data',function (err) { 
console.log(err.toString()); 
}) 
}).listen(2000) 

我試圖削減MP3和流式傳輸到瀏覽器,但瀏覽器,它顯示損壞的文件流媒體ffmpeg的MP3通過節點JS

回答

0

如果你問下載 YouTube視頻我不能告訴作爲一個MP3使用節點 - 但這個線程彈出了谷歌的頂部,當我正在研究這一點。所以,如果不是,也許它可以指向你正確的方向或幫助未來的某個人......莫德斯 - 對不起,如果我沒有這樣做的權利。

Additional StackOverflow Reference

但我用下面的代碼做下載 YouTube的VID爲MP3(下載YouTube VID /轉換成MP3 /下載):

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

};