ffmpeg进度条 - PHP中的编码百分比

Jim*_*mbo 21 php regex parsing ffmpeg

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

看过以下帖子:

ffmpeg可以显示进度条吗?

ffmpeg视频编码进度条

除其他外,我找不到一个有效的例子.

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

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

$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;
Run Code Online (Sandbox Code Playgroud)

$ percent_extracted变量回零,由于数学不是我的强项,我真的不知道如何在这里取得进展.

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

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

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

Jim*_*mbo 42

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

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

ffmpeg -i path/to/input.mov -vcodec videocodec -acodec audiocodec path/to/output.flv 1> block.txt 2>&1
Run Code Online (Sandbox Code Playgroud)

所以,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 . "%";

}
Run Code Online (Sandbox Code Playgroud)

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

你可以以此为呼应出一个页面的唯一一段文字,并从另一个页面,您可以使用jQuery这一段文字,并输出它抢进一个div执行AJAX请求,例如,你的页面的每个上更新10秒 :)


小智 5

ffmpeg现在有一个进度选项,可以更容易地解析输出.

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

在你开始编码之前你可以得到总帧数,以及其他很多信息(这是用bash做的.我是一个Perl程序员,所以我不知道你怎么把信息输入你的PHP脚本).

eval $(ffprobe -of flat = s = _ -show_entries stream = height,width,nb_frames,duration,codec_name path/to/input.mov);
宽度= $ {streams_stream_0_width};
高度= $ {streams_stream_0_height};
帧= $ {streams_stream_0_nb_frames};
videoduration = $ {streams_stream_0_duration};
audioduration = $ {streams_stream_1_duration};
编解码器= $ {streams_stream_0_codec_name};
echo $ width,$ height,$ frames,$ videoduration,$ audioduration,$ codec;

-of flate = s = _表示将每个name = value放在一个单独的行上.-show_entries告诉它显示以下条目(-show_streams的流,-show_format的格式等)stream = ...表示从-show_streams输出中显示这些项目.请尝试以下方法查看可用内容:

ffprobe -show_streams path/to/input.mov

进度文件的输出大约每秒添加一次.编码完成后,内容如下所示.在我的脚本中,每隔一秒我将文件放入一个数组,然后以相反的顺序遍历数组,只使用第一个[最后一个反转之前]两个"进度"行之间的内容,以便我使用文件末尾的最新信息.可能有更好的方法.这是一个没有音频的mp4所以只有一个流.

frame = 86
fps = 0.0
stream_0_0_q = 23.0
total_size = 103173
out_time_ms = 1120000
out_time = 00:00:01.120000
dup_frames = 0
drop_frames = 0
progress = continue
frame = 142
fps = 140.9
stream_0_0_q = 23.0
total_size = 415861
out_time_ms = 3360000
out_time = 00: 00:03.360000
dup_frames = 0
drop_frames = 0
progress = continue
frame = 185
fps = 121.1
stream_0_0_q = 23.0
total_size = 1268982
out_time_ms = 5080000
out_time = 00:00:05.080000
dup_frames = 0
drop_frames = 0
progress = continue
frame = 225
fps = 110.9
stream_0_0_q = 23.0
total_size = 2366000
out_time_ms = 6680000
out_time = 00:00:06.680000
dup_frames = 0
drop_frames = 0
progress = continue
frame = 262
fps = 103.4
stream_0_0_q = 23.0
total_size = 3810570
out_time_ms = 8160000
out_time = 00: 00:08.160000
dup_frames = 0
drop_frames = 0
progress = continue
frame = 299
fps = 84.9
stream_0_0_q = -1.0
total_size = 6710373
out_time_ms = 11880000
out_time = 00:00:11.880000
dup_frames = 0
drop_frames = 0
progress = end