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

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

问题描述

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



查看以下帖子: p>

可以ffmpeg显示进度条吗?





ffmpeg视频编码进度条



等等,我找不到一个工作示例。 / p>

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



我上面链接的第一个帖子给出:

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

preg_match(/ Duration:([^,] +)/,$ log,$ matches);
列表($ 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变量回显零,数学不是我的强项,我真的不知道如何在这里进步。



这里是来自block.txt的ffmpeg输出的一行(如果有用)


time = 00:19:25.16 bitrate = 823.0kbits / s frame = 27963 fps = 7 q = 0.0 size =
117085kB time = 00:19 :25.33比特率= 823.1kbits / s帧= 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


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

解决方案

好的,我找到了我需要的 - 希望这也有助于别人!

首先,您要将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){
//获取源的持续时间
preg_match(/持续时间:(。*?),开始:/,$ content,$ matches );

$ rawDuration = $ matches [1];

// rawDuration是00:00:00.00的格式。这将其转换为秒。
$ 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;

//获取已编码的文件中的时间
preg_match_all(/ time =(。*?)bitrate /,$ content,$ matches);

$ rawTime = array_pop($ matches);

//如果有多个匹配,则需要
if(is_array($ rawTime)){$ rawTime = array_pop($ rawTime);}

// rawTime是00:00:00.00的格式。这将其转换为秒。
$ 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;

//计算进度
$ progress = round(($ time / $ duration)* 100);

echoDuration:。 $持续时间。 <峰; br> 中;
echo当前时间:。 $时间。 <峰; br> 中;
echoProgress:。 $进度。 %;

}

输出剩下的时间百分比。



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


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.

Having looked at the following posts:

Can ffmpeg show a progress bar?

and

ffmpeg video encoding progress bar

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

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

The first post I linked above gives:

$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;

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

Here's one line from the ffmpeg output from block.txt (if it's helpful)

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!

First and foremost, you want to output the ffmpeg data to a text file on the server.

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

So, the ffmpeg output is block.txt. Now in PHP, let's do this!

$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.

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天全站免登陆