ffmpeg 进度条 - PHP 中的编码百分比 [英] ffmpeg Progress Bar - Encoding Percentage in PHP

查看:65
本文介绍了ffmpeg 进度条 - PHP 中的编码百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器上用 PHP 和 bash 编写了一个完整的系统,以在我的 VPS 上转换和流式传输 HTML5 格式的视频.转换由后台的ffmpeg完成,内容输出到block.txt.

I've written a whole system in PHP and bash on the server to convert and stream videos in HTML5 on my VPS. The conversion is done by ffmpeg in the background and the contents is output to block.txt.

看了以下帖子:

ffmpeg 可以显示进度条吗?

ffmpeg 视频编码进度条

除此之外,我找不到可行的示例.

amongst others, I can't find a working example.

我需要以百分比形式获取当前编码的进度.

I need to grab the currently encoded progress as a percentage.

我上面链接的第一篇文章给出了:

$log = @file_get_contents('block.txt');

preg_match("/Duration:([^,]+)/", $log, $matches);
list($hours,$minutes,$seconds,$mili) = split(":",$matches[1]);
$seconds = (($hours * 3600) + ($minutes * 60) + $seconds);
$seconds = round($seconds);

$page = join("",file("$txt"));
$kw = explode("time=", $page);
$last = array_pop($kw);
$values = explode(' ', $last);
$curTime = round($values[0]);
$percent_extracted = round((($curTime * 100)/($seconds)));

echo $percent_extracted;

$percent_extracted 变量显示为零,而且由于数学不是我的强项,我真的不知道如何在此处取得进展.

The $percent_extracted variable echoes zero, and as maths is not my strong point, I really don't know how to progress here.

这是来自 block.txt 的 ffmpeg 输出的一行(如果有帮助的话)

时间=00:19:25.16 比特率= 823.0kbits/s 帧=27963 fps= 7 q=0.0 大小=117085kB 时间=00:19:25.33 比特率= 823.1kbits/s 帧=27967 fps= 7q=0.0 大小= 117085kB 时间=00:19:25.49 比特率= 823.0kbits/s帧=27971 fps= 7 q=0.0 大小= 117126kB

time=00:19:25.16 bitrate= 823.0kbits/s frame=27963 fps= 7 q=0.0 size= 117085kB time=00:19:25.33 bitrate= 823.1kbits/s frame=27967 fps= 7 q=0.0 size= 117085kB time=00:19:25.49 bitrate= 823.0kbits/s frame=27971 fps= 7 q=0.0 size= 117126kB

请帮我输出这个百分比,完成后我可以创建自己的进度条.谢谢.

Please help me output this percentage, once done I can create my own progress bar. Thanks.

推荐答案

好的,我找到了我需要的东西 - 希望这也能帮助其他人!

Okay, I've found what I needed - and hopefully this helps someone else as well!

首先,您要将 ffmpeg 数据输出到服务器上的文本文件.

ffmpeg -i path/to/input.mov -vcodec videocodec -acodec audiocodec path/to/output.flv 1> block.txt 2>&1

所以,ffmpeg 的输出是 block.txt.现在在 PHP 中,让我们这样做!

$content = @file_get_contents('../block.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 . "%";

}

这会输出剩余时间的百分比.

This outputs the percentage of time left.

你可以把它作为唯一一段回显到页面的文本,从另一个页面你可以使用 jQuery 执行 AJAX 请求来获取这段文本并将其输出到一个 div 中,例如,更新您的页面每 10 秒一次.:)

You can have this as the only piece of text echoed out to a page, and from another page you can perform an AJAX request using jQuery to grab this piece of text and output it into a div, for example, to update on your page every 10 seconds. :)

这篇关于ffmpeg 进度条 - PHP 中的编码百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆