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

本小妞迷上赌 提交于 2020-01-11 17:31:25

问题


I want to convert a youtube url into an mp3 file. Currently, I download the mp4 using node's ytdl module, like so:

fs = require 'fs'
ytdl = require 'ytdl'

url = 'http://www.youtube.com/watch?v=v8bOTvg-iaU'
mp4 = './video.mp4'

ytdl(url).pipe(fs.createWriteStream(mp4))

Once the download is complete, I convert the mp4 into mp3 using the fluent-ffmpeg module, like so:

ffmpeg = require 'fluent-ffmpeg'

mp4 = './video.mp4'
mp3 = './audio.mp3'

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

I don't want to have to save the entire mp4 before starting the mp3 conversion. How do I pipe the mp4 into proc so it carries out the conversion as it receives the mp4 chunks?


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/18168432/node-js-how-to-pipe-youtube-to-mp4-to-mp3

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