【发布时间】:2016-03-06 22:32:15
【问题描述】:
我想要FFmpeg编码的进度条。这是我用来获取编码过程百分比值的代码。
<?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 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 . "%";
}
?>
这里是我的 FFMPEG 日志文件中的相关日志行,以便更好地理解。
Stream mapping:
Stream #0:0 -> #0:0 (mpeg2video (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (pcm_s24le (native) -> aac (native))
Press [q] to stop, [?] for help
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
此代码未返回 Duration 的值,因此我收到 PHP 警告,代码未计算当前百分比。
这是我收到的 PHP 警告-
PHP Warning: Division by zero in /var/www/html/mm/progressbar.php
我想我们也可以从time计算百分比,但我不知道,我怎样才能让它工作?
或任何解决持续时间问题的帮助。
感谢您的帮助!
【问题讨论】:
标签: php ubuntu ffmpeg preg-match