node.js将ffmpeg stdout实时流式传输到res [英] node.js live streaming ffmpeg stdout to res

查看:315
本文介绍了node.js将ffmpeg stdout实时流式传输到res的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望node.js将一个非常长的音频文件转换为mp3,并且第二个数据在stdout上可用,node.js应该将其发送给客户端以供他们播放.

I want node.js to convert an extremly long audio file to mp3, and the second data is available on stdout, node.js should send it to the client for them to play.

我已经写了以下内容,并且在它起作用的同时,html5音频/视频标签会等到ffmpeg完成100%的转码后,在这里-我想在ffmpeg做事的同时开始播放视频.

I've written the following, and while it works, the html5 audio/video tag waits until ffmpeg is 100% done transcoding, where-as I want to start playing the video while ffmpeg is doing its thing.

var ffmpeg = childProcess.spawn('ffmpeg', [
   '-i', params.location, //location of the specified media file
   '-f', 'mp3',
   'pipe:1'
 ]);
 res.writeHead(200, {
   'Content-Type': 'audio/mp3'
 });
 ffmpeg.stdout.pipe(res);

不知道为什么,但是如果params.location指向电影,则一切似乎正常.但是,如果它是音频文件,则ffmpeg似乎不会输出到stdout,直到其100%转换为止.

EDIT 1: Not sure why, but if params.location points to a movie everything seems to work. But if its an audio file, ffmpeg doesn't seem to be outputting to stdout until its 100% converted.

事实证明,由于mp4文件不是非因果关系,因此您无法将mp4文件转储到标准输出( http://en.wikipedia.org/wiki/Causal_system ).因此,如果您使用webm,它将起作用.只需确保使用webm支持编译ffmpeg(对于自制用户:brew install ffmpeg --with-vpx --with-vorbis即可.)

EDIT 2: Turns out that you can't dump an mp4 file to stdout due to the fact that mp4 files are non Causal (http://en.wikipedia.org/wiki/Causal_system). THerefore if you use webm it works. Just make sure to compile ffmpeg with webm support (for homebrew users: brew install ffmpeg --with-vpx --with-vorbis ).

我上传了一个github要点,其中显示了两个功能来发送实时mp3/webm转码: https://gist.github.com/cobookman/c1a9856a4588496b021a

I've uploaded a github gist showing two functions to send live mp3/webm transcodes: https://gist.github.com/cobookman/c1a9856a4588496b021a

推荐答案

我不知道为什么这对您不起作用,因为我的应用程序中的代码几乎完全相同.区别在于我在输出中使用的是-而不是pipe:1.试试吧.

I don't know why this isn't working for you, as I have almost the exact same code in my applications. The difference is that I am using - instead of pipe:1 for the output. Try that.

我建议的另一件事是从FFmpeg的stderr中读取内容,以便您可以查看FFmpeg是否有任何奇怪的输出,这些信息可能会为您提供有关发生情况的线索.

The other thing I suggest is reading from FFmpeg's stderr so that you can see if there is any odd output from FFmpeg that may give you clues as to what is going on.

这篇关于node.js将ffmpeg stdout实时流式传输到res的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆