2015-07-10 39 views
1

我想要FFmpeg編碼的進度條。這是我用來獲取編碼過程的百分比值的代碼。FFMPEG(2.5.7)來自PHP的進度條

<?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的警告,對此我getting-

PHP Warning: Division by zero in /var/www/html/mm/progressbar.php 

我想我們還可以計算從time的百分比,但我不知道,我怎麼能使其工作?

或任何幫助解決持續時間的問題。

感謝您的幫助!

回答

1

簡短的回答:

你在這裏運行FFmpeg的命令之前,您應該運行下面的命令,這將使你的時間。

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

然後,您可以說的preg_match wtih '/Duration: ([0-9]{*}):([0-9]{2}):([0-9]{2}).([0-9]{2})/'得到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超時設置爲>處理文件所需的時間。

+0

Hi @Robbbie! 你已經建議的第二個選項,我會盡快嘗試,但現在爲了獲得持續時間的值,我手動在你建議'ffmpeg /tmp/out.mp4 2>&1 | grep「持續時間」'。我沒有得到任何反饋,它只是沒有任何迴應。 – kunal

+0

您的指示缺少-i? – Robbie

+0

哦!我很抱歉,我只是錯過了,你搖滾 謝謝@羅比 – kunal