node.js live streaming ffmpeg stdout to res

让人想犯罪 __ 提交于 2020-05-12 10:55:33

问题


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.

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

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.

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

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


回答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.

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.



来源:https://stackoverflow.com/questions/23839741/node-js-live-streaming-ffmpeg-stdout-to-res

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