node.js : how to pipe - youtube to mp4 to mp3 node.js : how to pipe - youtube to mp4 to mp3 node.js node.js

node.js : how to pipe - youtube to mp4 to mp3


Instead of passing the location of mp4 file, pass in the ytdl stream as the source, like so:

stream = ytdl(url)proc = new ffmpeg({source:stream})proc.setFfmpegPath('/Applications/ffmpeg')proc.saveToFile(mp3, (stdout, stderr)->            return console.log stderr if err?            return console.log 'done'        )


This is a relatively old question, but may help someone in the future - I stumbled upon it myself when looking for a similar solution to download a youtube vid as mp3 without needing to save the file on the server. I basically settled on piping the conversion directly to response and is working as I had hoped.

Originally answered this question in a different thread: ffmpeg mp3 streaming via node js

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 the output directly to the response.    proc.setFfmpegPath(ffmpegLocation);    proc.withAudioCodec('libmp3lame')        .toFormat('mp3')        .output(res)        .run();    proc.on('end', function() {        console.log('finished');    });};


This does not work for me. Code below works if I set a local .mp4 file but using stream does not.

var ytUrl = 'http://www.youtube.com/watch?v=' + data.videoId;        var stream = youtubedl(ytUrl, {            quality: 'highest'        });        var saveLocation = './mp3/' + data.videoId + '.mp3';        var proc = new ffmpeg({            source: './mp3/test.mp4' //using 'stream' does not work        })            .withAudioCodec('libmp3lame')            .toFormat('mp3')            .saveToFile(saveLocation, function(stdout, stderr) {                console.log('file has been converted succesfully');            });