ffmpeg mp3流经节点js [英] ffmpeg mp3 streaming via node js
问题描述
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和流式传输到浏览器,但在浏览器中显示损坏的文件
i am trying to cut the mp3 and streaming to the browser but in browser it showing corrupted file
推荐答案
我无法确定您是否要使用节点将下载一个YouTube视频作为mp3,但是该线程弹出当我正在研究的时候,在谷歌的顶部。所以,如果没有,也许它可以指向正确的方向或帮助某人在将来... Mods - 抱歉,如果我不这样做。
I cannot tell if you're asking about downloading a youtube video as an mp3 using node - but this thread popped up on top of google when I was researching just that. So, if not, perhaps it can point you in the right direction or help someone in the future... Mods - sorry if I'm not doing this right.
但是,我使用以下代码来执行下载 youtube vid as mp3(download youtube vid / convert to mp3 / download):
But I am using the following code to do download a youtube vid as mp3 (download youtube vid / convert to mp3 / download) :
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');
});
};
这篇关于ffmpeg mp3流经节点js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!