title: How to programmatically start/stop FFMPEG stream transcoding
body: <p>I have an ip webcam which provides an MJPEG stream. I can successfully transcode and save that stream with ffmpeg under OSX. The following gives me pretty much what I want:</p>
  
  <pre><code>ffmpeg -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&amp;Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4
  </code></pre>
  
  <p>That will start an FFMPEG session and begin saving the live stream to my test.mp4 file. pressing q will quit ffmpeg and save the file.</p>
  
  <p>I would like to programmatically start &amp; stop the recording using a PHP or Bash shell script. I have tried the following:</p>
  
  <pre><code>&lt;?php
  
  $pid = pcntl_fork();
  
  if($pid == -1){
      die("could not fork"); 
  }elseif($pid){
      // we are the parent...
      print $pid.' started recording. waiting 10 seconds...';
      sleep(10); // Wait 10 seconds
  
      print_r(shell_exec("kill ".$pid)); // Kill the child recording process
  
      echo 'done';
      exit(); 
  }else{
      // we are the child process. call ffmpeg.
      exec('../lib/ffmpeg -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&amp;Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4');
  }
  </code></pre>
  
  <p>But there are two problems:</p>
  
  <ol>
  <li>The ffmpeg process does not end/die (probably because its forked again)</li>
  <li>When I manually kill the ffmpeg process, the video file is not readable</li>
  </ol>