ffmpeg mp3 streaming via node js

偶尔善良 提交于 2019-12-02 14:32:45

问题


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)

i am trying to cut the mp3 and streaming to the browser but in browser it showing corrupted file


回答1:


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.

Additional StackOverflow Reference

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

};



来源:https://stackoverflow.com/questions/38952422/ffmpeg-mp3-streaming-via-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!