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 并流式传输到浏览器,但在浏览器中它显示损坏的文件

4

1 回答 1

0

我不知道您是否在询问有关使用节点将 youtube 视频下载为 mp3 的问题 - 但是当我正在研究这个问题时,这个线程突然出现在谷歌之上。所以,如果不是,也许它可以为您指明正确的方向或在未来帮助某人...... Mods - 抱歉,如果我做的不对。

其他 StackOverflow 参考

但我正在使用以下代码将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');
});

};

于 2016-08-21T04:24:30.913 回答