ffmpeg mp3 streaming via node js

后端 未结 1 1636
遥遥无期
遥遥无期 2021-01-28 18:19
var fs = require(\'fs\');
var child = require(\'child_process\');
var http=require(\'http\')
var input_file = fs.createReadStream(\'./remo.mp3\');
http.createServer(func         


        
相关标签:
1条回答
  • 2021-01-28 18:42

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

    };

    0 讨论(0)
提交回复
热议问题