i want have progress bar of ffmpeg encoding.this code using percentage value of encoding process.
<?php $content = @file_get_contents("with-logo/output.txt"); //echo $content; if($content) { preg_match("/duration: (.*?), start:/", $content, $matches); $rawduration = $matches[1]; $ar = array_reverse(explode(":", $rawduration)); $duration = floatval($ar[0]); //echo $duration; if (!empty($ar[1])) $duration += intval($ar[1]) * 60; if (!empty($ar[2])) $duration += intval($ar[2]) * 60 * 60; //get time in file encoded preg_match_all("/time=(.*?) bitrate/", $content, $matches); $rawtime = array_pop($matches); //this needed if there more 1 match if (is_array($rawtime)){$rawtime = array_pop($rawtime);} //rawtime in 00:00:00.00 format. converts 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 progress $progress = round(($time/$duration) * 100); echo "duration: " . $duration . "<br>"; echo "current time: " . $time . "<br>"; echo "progress: " . $progress . "%"; } ?> here relevant log line form ffmpeg log files better understanding.
stream mapping: stream #0:0 -> #0:0 (mpeg2video (native) -> h264 (libx264)) stream #0:1 -> #0:1 (pcm_s24le (native) -> aac (native)) press [q] stop, [?] frame= 11 fps=0.0 q=0.0 size= 0kb time=00:00:00.41 bitrate= 0.9kbits/s frame= 22 fps= 21 q=0.0 size= 0kb time=00:00:00.85 bitrate= 0.4kbits/s frame= 33 fps= 21 q=0.0 size= 0kb time=00:00:01.30 bitrate= 0.3kbits/s frame= 43 fps= 20 q=0.0 size= 0kb time=00:00:01.69 bitrate= 0.2kbits/s and code not returning value duration , result of getting php warning , code not calculating current percentage.
here php warning , getting-
php warning: division 0 in /var/www/html/mm/progressbar.php i think can calculate percentage timebut have no idea, how can make work?
or solve problem duration.
thanks help!
short answer:
before run ffmpeg command here, should run following command, , give duration.
ffmpeg -i file.flv 2>&1 | grep "duration" duration: 00:39:43.08, start: 0.040000, bitrate: 386 kb/s you can preg_match wtih '/duration: ([0-9]{*}):([0-9]{2}):([0-9]{2}).([0-9]{2})/' h, m, s , .s variables.
long answer
there way can handle. opposed using php-ffmpeg, work out commands need , run them directly using popen() (http://php.net/manual/en/function.popen.php) or proc_open() (http://php.net/manual/en/function.proc-open.php)
$cmd = "/path/to/ffmpeg -options"; $proc = popen($cmd, 'r'); while (!feof($proc)) { echo fread($proc, 4096); @flush(); } pclose($proc); this hold process open command runs, , grabs output screen happens.
so run twice; once duration, , second time actual conversion. can process output line line, , save progress file/database can read other processes.
remember set php timeout > time takes process file.
Comments
Post a Comment