title: FFMPEG progress bar in php
body: <p>I am using ffmpeg to convert a video file large files take a long time to get encode. i will like to show a progress bar with a percentage/time remaining</p>
  
  <p>i saw and tried this <a href="https://stackoverflow.com/questions/11441517/ffmpeg-progress-bar-encoding-percentage-in-php">example</a> but for some reason the progress bar is not showing.</p>
  
  <p>Html form </p>
  
  <pre><code>&lt;form action="upload.php" method="post" enctype="multipart/form-data"&gt;
  &lt;label for="file"&gt;&lt;span&gt;&lt;/span&gt;&lt;/label&gt;
  &lt;input type="file" name="file" id="file" /&gt; 
  &lt;br/&gt;
  Please enter video title:
  &lt;br/&gt;
  &lt;input type"text" name="videoTitle" /&gt;
  &lt;br /&gt;
  &lt;input type="submit" name="submit" value="Submit" /&gt;
  &lt;/form&gt;
  
  &lt;div id="myDiv" name="myDiv" title="test"&gt;
          &lt;h5&gt;Encoding percentage&lt;/h5&gt;
          &lt;script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"&gt;&lt;/script&gt;
                  &lt;script&gt;
                  $(document).ready(function(){
                  setInterval(function(){
                  $("#screen").load('progress.php')
                  }, 10000);
                  });
                  &lt;/script&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>upload.php ffmpeg script</p>
  
  <pre><code>shell_exec("C:\\ffmpeg\\bin\\ffmpeg.exe -y -i ".$target_file." -c:v libx264 -s:v 854x480 -c:a copy \"{$newFileName}\" &gt; logfile.txt 2&gt;&amp;1"); // script to encode video
  </code></pre>
  
  <p>Progress.php
      
  
  <pre><code>$content = @file_get_contents('blocks.txt');
  
              if($content){
              //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 "Duration: " . $duration . "&lt;br&gt;";
              echo "Current Time: " . $time . "&lt;br&gt;";
              echo "Progress: " . $progress . "%";
  
  }
  
  ?&gt;
  </code></pre>