Skip to content

Instantly share code, notes, and snippets.

@vitaLee
Created February 28, 2013 10:55
Show Gist options
  • Select an option

  • Save vitaLee/5055910 to your computer and use it in GitHub Desktop.

Select an option

Save vitaLee/5055910 to your computer and use it in GitHub Desktop.

Revisions

  1. Vitaliy Berov created this gist Feb 28, 2013.
    19 changes: 19 additions & 0 deletions ffmpeg_progress.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <?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);
    }
    46 changes: 46 additions & 0 deletions track_progress.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <?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;
    }