本文介绍了node.js:如何管道-youtube到mp4到mp3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将youtube网址转换为mp3文件.目前,我使用节点的ytdl模块下载mp4,如下所示:

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

下载完成后,我使用fluent-ffmpeg模块将mp4转换为mp3,如下所示:

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

我不想在开始mp3转换之前保存整个mp4.如何将mp4传送到proc,以便在接收到mp4块时执行转换?

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?

推荐答案

与其传递mp4文件的位置,不如传递ytdl流作为源,就像这样:

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

这篇关于node.js:如何管道-youtube到mp4到mp3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 16:06