Skip to content

Instantly share code, notes, and snippets.

@vitaLee
Created February 28, 2013 10:55
Show Gist options
  • Save vitaLee/5055910 to your computer and use it in GitHub Desktop.
Save vitaLee/5055910 to your computer and use it in GitHub Desktop.
FFMPEG encoding progress
<?php
$logFile = 'progress_logs/%s.log';
$cmd = "/usr/local/bin/ffmpeg -i %s -y progress_test.m4v 2> %s";
for ($i=1; $i < count($argv); $i++) {
$video = $argv[$i];
$log = sprintf($logFile, $video);
exec(sprintf($cmd, $video, $log), $processOutput, $processStatus);
if($processStatus == 0) {
printf("=== Successfully encoded %s ===\n", $video);
}else{
printf("=== Failed to encode %s with output: ===\n %s \n", $video, file_get_contents($log));
}
unlink($log);
}
<?php
while(true) {
$logs = glob('progress_logs/*');
if(!count($logs)){
echo "- NO VIDEO FILES ARE CURRENTLY PROCESSED -\n";
}else{
foreach($logs as $logName) {
echo "=== Encoding progress for log {$logName} ===\n";
$logContent = file_get_contents($logName);
$totalDuration = totalDuration($logContent);
$encodingProgressAt = encodingProgress($logContent);
printf("Total duration of %s is %d seconds\n", $logName, $totalDuration);
printf("Video encoding progress is at %d seconds\n", $encodingProgressAt);
printf("Progress is at %d%%\n", ceil(($encodingProgressAt / $totalDuration)*100.0));
echo "===========================\n";
}
}
sleep(5);
}
function totalDuration($logContent) {
preg_match("/Duration: (.*), start/", $logContent, $matches);
return durationInSeconds($matches[1]);
}
function encodingProgress($logContent) {
preg_match_all("/time=(.*?) bitrate/", $logContent, $matches);
$matchesCount = count($matches[0]);
$lastProgress = $matches[1][$matchesCount-1];
return durationInSeconds($lastProgress);
}
function durationInSeconds($timeCode) {
$pieces = explode(':', $timeCode);
$hours = $pieces[0] * 60 * 60;
$minutes = $pieces[1] * 60;
$seconds = $pieces[2];
return $hours + $minutes + $seconds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment