简体   繁体   中英

node.js live streaming ffmpeg stdout to res

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM