title: Use FFMPEG to stream MP4 from AviSynth via PHP
body: <p>Taking my first steps in live video streaming, I'm trying to conjure an MP4 live stream out of a .avs script that is parameterized via a wrapping PHP script.</p>
  
  <p>I'd like to consume a video stream from PHP like this:</p>
  
  <p><strong>consume_video.html</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      &lt;video width="320" height="240" controls&gt;
        &lt;source src="stream.php?frames=100" type="video/mp4"&gt;
      &lt;/video&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The problem I have is making PHP stream the content via FFMPEG.</p>
  
  <p>Here's a simple example to outline the solution:</p>
  
  <p><strong>stream.php</strong></p>
  
  <pre><code>&lt;?php
  $numFrames = $_GET['frames'];
  $filename = tempnam( __DIR__, 'file' ) . '.avs';
  file_put_contents($filename, "ColorBars()\nTrim(0, $numFrames)" );
  
  header('Content-Type: video/mp4');
  $cmd = "ffmpeg -i $filename -c:v libx264 -crf 22 -c:a libfaac -movflags faststart";
  // ... now what?
  </code></pre>
  
  <p>Is there a way to make this concept work or do I need to follow a different path?</p>
  
  <p>Thanks.</p>