【问题标题】:FFMPEG (2.5.7 ) Progress Bar from PHPFFMPEG (2.5.7) 来自 PHP 的进度条
【发布时间】: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


    【解决方案1】:

    简答:

    在此处运行 ffMPEG 命令之前,您应该运行以下命令,这将为您提供持续时间。

    ffmpeg -i file.flv 2>&1 | grep "Duration"
       Duration: 00:39:43.08, start: 0.040000, bitrate: 386 kb/s
    

    然后您可以使用 '/Duration: ([0-9]{*}):([0-9]{2}):([0-9]{2}).([0-9]{2})/' preg_match 将 h、m、s 和 .s 放入变量中。

    长答案

    还有另一种处理方式。与使用 php-ffmpeg 不同,只需找出您需要的命令,然后使用 popen() (http://php.net/manual/en/function.popen.php) 或 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);
    

    这实际上会在命令运行时为您打开进程,并在发生时从屏幕获取输出。

    所以运行两次;一次用于持续时间,第二次用于实际转换。然后,您可以逐行处理输出,并将进度保存到其他进程可以读取的另一个文件/数据库中。

    记得将 PHP 超时设置为 > 处理文件所需的时间。

    【讨论】:

    • 嗨@Robbbie!您建议的第二个选项,我会尽快尝试但现在为了获取 Duration 的值,我在您建议的 ffmpeg /tmp/out.mp4 2&gt;&amp;1 | grep "Duration" 的 shell 上手动运行命令。我没有得到任何反馈,它只是卡住了,没有任何回应。
    • 您的说明缺少 -i ?
    • 天哪!对不起,我只是错过了。,谢谢@Robbie
    • 我只想再澄清一件事。目前我有一个网页,用户只需输入文件 ID,然后在存储中搜索文件,FFMPEG 获取编码的输入。现在我正在将输入列表连接到 FFMPEG,如下所示ffmpeg -f concat -i /tmp/mylist.txt
    • 所以首先我需要将文件路径回显到 mylist.txt 文件中。我必须这样做,因为在 FFmpeg 没有将搜索到的文件路径作为输入并且每个 TIme 用户都想要编码新的文件之前,我需要更新 concat 列表并删除以前的文件路径并用搜索到的新文件回显Path.for 在后台搜索文件我使用的是 linux find 命令,我通过 ssh2_exec() php 函数运行。有没有办法可以直接连接find的返回值,我不需要维护输入文件。
    猜你喜欢
    • 2016-04-24
    • 2011-06-21
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 2011-04-15
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多