title: node.js live streaming ffmpeg stdout to res
body: <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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.  </p>
  
  <p>EDIT 2: Turns out that you can't dump an mp4 file to stdout due to the fact that mp4 files are non Causal (<a href="http://en.wikipedia.org/wiki/Causal_system" rel="nofollow">http://en.wikipedia.org/wiki/Causal_system</a>).  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 ).</p>
  
  <p>I've uploaded a github gist showing two functions to send live mp3/webm transcodes: <a href="https://gist.github.com/cobookman/c1a9856a4588496b021a" rel="nofollow">https://gist.github.com/cobookman/c1a9856a4588496b021a</a></p>