简体   繁体   中英

FFMPEG progress bar in php

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

i saw and tried this example but for some reason the progress bar is not showing.

Html form

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file"><span></span></label>
<input type="file" name="file" id="file" /> 
<br/>
Please enter video title:
<br/>
<input type"text" name="videoTitle" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

<div id="myDiv" name="myDiv" title="test">
        <h5>Encoding percentage</h5>
        <script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
                <script>
                $(document).ready(function(){
                setInterval(function(){
                $("#screen").load('progress.php')
                }, 10000);
                });
                </script>
        </div>

upload.php ffmpeg script

shell_exec("C:\\ffmpeg\\bin\\ffmpeg.exe -y -i ".$target_file." -c:v libx264 -s:v 854x480 -c:a copy \"{$newFileName}\" > logfile.txt 2>&1"); // script to encode video

Progress.php

$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 . "<br>";
            echo "Current Time: " . $time . "<br>";
            echo "Progress: " . $progress . "%";

}

?>

In the last release of FFMpeg , to create a log file you have to use the option -vstats_file

So you have to change your encode video script to:

shell_exec("C:\\ffmpeg\\bin\\ffmpeg.exe -y -i ".$target_file." -c:v libx264 -s:v 854x480 -c:a copy \"{$newFileName}\" -vstats_file PATH_TO_YOUR\logfile.txt"); // script to encode video

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM