<?php

  $recordTime = 60*60;
  $startTime = time();
  set_time_limit($recordTime);  
  $datetime = new DateTime('now', new DateTimezone('America/New_York'));
  $datetime = $datetime->format('Ymd-His');
  
  $output = array();
  $responded = false;
  exec('ping 10.0.0.254 -n 1', $output, $return);
  foreach($output as $line){
	if(strstr($line, 'Reply from') !== false){
		$responded = true;
		break;
	}
  }
  if($responded == false){
  }
  
  $cmd = "U:/secvid/ffmpeg-20190225-2e67f75-win64-static/bin/ffmpeg.exe -y -i rtsp://10.0.0.244:554/11 -c copy -an U:/secvid/".$datetime.".mkv 2>&1";
  
  $logFile = "U:/secvid/".$datetime."_log.txt";
  $fp = fopen($logFile, 'a');
  fwrite($fp, $cmd . PHP_EOL);
  
$descriptorspec = array(
   0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
   2 => array("pipe", "w")    // stderr is a pipe that the child will write to
);

$process = proc_open($cmd, $descriptorspec, $pipes);
echo "<pre>";
if (is_resource($process)) {
    while (!feof($pipes[1])) {
        if(time() > $startTime + $recordTime - 2){
            fwrite($pipes[0], 'q');
            //exec('taskkill /IM ffmpeg.exe', $output, $return);
            //proc_terminate($process, SIGTERM);
            break;
        }
        $s = fread($pipes[1],1);
        print $s;
        fputs($fp, $s);
        flush();
    }
}
fclose($fp);
echo "Ended".PHP_EOL;
echo "</pre>";
?>