title: what is the good function php to deal with ffmpeg and progress bar
body: <p>I ask about ffmpeg and progressbar  </p>
  
  <p>I used exec function with ffmpeg  and output the result to txt file  </p>
  
  <p>and using some of code I found here to      </p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html lang="es"&gt;
  &lt;title&gt;AudioXtractor&lt;/title&gt;
  &lt;body&gt;
  &lt;center&gt;
  
  
  
      &lt;?php 
  
      define('RAPIDLEECH', 'yes');
      define('CONFIG_DIR', 'configs/');
      require_once('configs/config.php');
      define ( 'TEMPLATE_DIR', 'templates/'.$options['template_used'].'/' );
      // Include other useful functions
      require_once('classes/other.php');
      error_reporting(0);
  
      login_check();
  
      include(TEMPLATE_DIR.'header.php');
  
      echo ('&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;AudioXtractor&lt;/b&gt;, un complemento &lt;br&gt; que te permite extraer el audio de tus videos.&lt;br&gt;&lt;br&gt;&lt;br&gt;');
  
      putenv('GDFONTPATH=' . realpath('.')); ?&gt;
  
      &lt;br /&gt;
  
      &lt;FORM method="post"&gt;&lt;center&gt;
      &lt;TABLE&gt;
        &lt;td&gt;Movie:
      &lt;select name="video"&gt;
  
      &lt;?php
      $exts=array(".ac3", ".avi", ".f4v", ".flv", ".mkv", ".mov", ".mp4", ".mpg", ".mpeg", ".rmvb", ".srt", ".swf", ".wav", ".wmv");
      $ext="";
      function vidlist($dir) 
      {
      $results = array();
      $handler = opendir($dir);
      while ($file = readdir($handler)) 
      {
      if (strrchr($file,'.')!="")
      {
      $ext=strtolower(strrchr($file,'.'));
      }
      if ($file != '.' &amp;&amp; $file != '..' &amp;&amp; in_array($ext,$GLOBALS["exts"]))
      {
      $results[] = $file;
      }
      }
      closedir($handler);
      sort($results);
      return $results;
      }
      function Output($command) {
      $output = array($command);
      exec($command.' 2&gt;&amp;1', $output);
      return ($output);
      }
      $files = vidlist("./files/");
      foreach($files as $file)
      {
          echo '&lt;option value="'.$file.'"&gt;'.$file.'&lt;/option&gt;';
      }
  
      ?&gt;
  
  
      &lt;/TABLE&gt;
  
      &lt;br/ &gt;
      New MP3's name: 
      &lt;input type="text" name="nvdo" value="nuevoaudio"&gt; 
      &lt;br/ &gt;
      &lt;br /&gt;
      &lt;br /&gt;
  
      &lt;td colspan=2&gt;&lt;center&gt;&lt;input type=submit style="font-size:16px; font-weight:bold; cursor:pointer;" name="analize" value=Extract /&gt;&lt;/td&gt;
      &lt;tr&gt;
  
      &lt;/FORM&gt;
  
  
      &lt;?php
      if ($_POST['video']!="")
  
          $video = 'files/';
          $video=array();     
          $video[0] = $_POST['video'];
  
      if ($_POST['nvdo']!="")
  
          $nvdo = 'files/';
          $nvdo=array();      
          $nvdo[0] = $_POST['nvdo'];
  
  
  
      foreach ($video as $vdo)
      foreach ($nvdo as $nvd)
  
      if (isset($_POST["analize"])) {
  
          exec("ffmpeg -i files/$vdo -ab 192k files/$nvd.mp3 -y 2&gt; files/$nvd.txt");
  
          $ext=strtolower(strrchr($vdo,'.'));
  
      /////////////////////////////////////////////////////my code //////////////////////////////////////////
  
      $content = @file_get_contents("files/$nvd.txt");
  
  
          //get duration of source
          preg_match("/Duration: (.*?), start:/", $content, $matches);
  
          $rawDuration = $matches[1];
  
          //rawDuration is in 00:00:00.00 format. This converts it to seconds.
          $ar = array_reverse(explode(":", $rawDuration));
          $duration = floatval($ar[0]);
          if (!empty($ar[1])) $duration += intval($ar[1]) * 60;
          if (!empty($ar[2])) $duration += intval($ar[2]) * 60 * 60;
  
          //get the time in the file that is already encoded
          preg_match_all("/time=(.*?) bitrate/", $content, $matches);
  
          $rawTime = array_pop($matches);
  
          //this is needed if there is more than one match
          if (is_array($rawTime)){$rawTime = array_pop($rawTime);}
  
          //rawTime is in 00:00:00.00 format. This converts it to seconds.
          $ar = array_reverse(explode(":", $rawTime));
          $time = floatval($ar[0]);
          if (!empty($ar[1])) $time += intval($ar[1]) * 60;
          if (!empty($ar[2])) $time += intval($ar[2]) * 60 * 60;
  
          //calculate the progress
          $progress = round(($time/$duration) * 100);
  
  
  
  
          echo '&lt;BR&gt;&lt;BR&gt;¡Su video fue convertido correctamente! &lt;BR&gt;&lt;BR&gt;Link al archivo:';
  
          echo ' &lt;a href="files/'.$nvd.'.mp3"&gt;'.$nvd.'.mp3&lt;/a&gt;&lt;BR /&gt;' . "&lt;br&gt;";
          echo "Duration: " . $duration . "&lt;br&gt;";
          echo "Current Time: " . $time . "&lt;br&gt;";
          echo "Progress: " . $progress . "%" . "&lt;br&gt;";
  
      //////////////////////////////////////my coed //////////////////////////////////////////////
      }
  
      ?&gt;
      &lt;br&gt;&lt;br&gt;&lt;a href="index.php"&gt;Volver al RapidLeech&lt;/a&gt;
  
      &lt;br&gt;&lt;br&gt;&lt;br&gt;Formatos Aceptados: &lt;br&gt;&lt;b&gt;.ac3, .avi, .f4v, .flv, .mkv, .mov, .mp3,&lt;br&gt; .mp4, .mpg, .mpeg, .rmvb, .srt, .swf, .wav, .wmv&lt;/b&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;/center&gt;&lt;/body&gt;&lt;/html&gt;
  
      &lt;?php
  
  
  
      ?&gt;
  </code></pre>
  
  <p>so i ask some one he told me the exec stop php script and never give me </p>
  
  <pre><code>You can't get the progress directly if you are using exec, because the php script is stopped until ffmpeg closes. (Because exec returns the whole execution output)
  
  You should use popen, for being able to get the output from the process in real time (without reading any file) for parse and show the progressbar
  
  Here is a example for get the output: 
  
  When you get the progress info from ffmpeg, you can use your code for parse it and show your progressbar
  </code></pre>
  
  <p>this is my popen script</p>
  
  <pre><code>&lt;?php 
  
  $handle = popen ("ffmpeg.exe -i files/fz.mp4 -ab 192k files/vdf.mp3 2&gt;&amp;1 ", 'r');
  $handles = (string)$handle;
  $line = "";
  
  while (false !== ($char = fgetc($handle)))
  {
    if ($char == "\r")
    {
      // You could now parse the $line for status information.
      echo "$line\n";
      $line = "";
    } else {
      $line .= $char;
    }
    ob_flush();
    flush();
  }
  pclose ($handle);
  
  
      //get duration of source
      preg_match("/Duration: (.*?), start:/", $handles, $matches);
  
      $rawDuration = $matches[1];
  
      //rawDuration is in 00:00:00.00 format. This converts it to seconds.
      $ar = array_reverse(explode(":", $rawDuration));
      $duration = floatval($ar[0]);
      if (!empty($ar[1])) $duration += intval($ar[1]) * 60;
      if (!empty($ar[2])) $duration += intval($ar[2]) * 60 * 60;
  
      //get the time in the file that is already encoded
      preg_match_all("/time=(.*?) bitrate/", $handles, $matches);
  
      $rawTime = array_pop($matches);
  
      //this is needed if there is more than one match
      if (is_array($rawTime)){$rawTime = array_pop($rawTime);}
  
      //rawTime is in 00:00:00.00 format. This converts it to seconds.
      $ar = array_reverse(explode(":", $rawTime));
      $time = floatval($ar[0]);
      if (!empty($ar[1])) $time += intval($ar[1]) * 60;
      if (!empty($ar[2])) $time += intval($ar[2]) * 60 * 60;
  
      //calculate the progress
      $progress = round(($time/$duration) * 100);
  
  
  
  
      echo '&lt;BR&gt;&lt;BR&gt;¡Su video fue convertido correctamente! &lt;BR&gt;&lt;BR&gt;Link al archivo:';
  
  
      echo "Duration: " . $duration . "&lt;br&gt;";
      echo "Current Time: " . $time . "&lt;br&gt;";
      echo "Progress: " . $progress . "%" . "&lt;br&gt;";
  
  //////////////////////////////////////my coed //////////////////////////////////////////////
  
  ?&gt;
  </code></pre>
  
  <p>so i dont under stand what is good for ffmpeg progressbAR</p>
  
  <p>is exec or popen</p>
  
  <p>so please give me hints for ffmpeg real time progressbar</p>
  
  <p>what is good for progressbar  </p>
  
  <p>is html5 progress bar </p>
  
  <p>or javascript progress bar</p>