2012-11-22 62 views
1

有沒有另一種方式來逐行顯示,實時使用HandBrakeCLI和php?到目前爲止,我在proc_open()函數中使用fgets()來獲取字節。 fgets()手冊提到了這樣的長度:「讀取的長度爲-1個字節被讀取或換行符(包含在返回值中)或EOF(以先到者爲準)。」。到目前爲止,它不識別新行。PHP HandBrakeCLI不會一行一行輸出使用proc_open

// disable output buffering so we can send the encoding progress to the browser. 
ob_implicit_flush(true); 

$Command = 'HandBrakeCLI -i "/tmp/in-1.FLV" -t 1 -c 1 -o "/tmp/out.mp4" -f mp4 --strict-anamorphic -e x264 -q 20 --cfr -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0 --gain=0 --audio-copy-mask none --audio-fallback ffac3 -x ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0'; 

echo 'Starting...'; 

ob_flush(); 
flush(); 

$descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from (we do not use it). 
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to and we will read from. 
    2 => array("pipe", "w") // stderr is a file to write to (we do not use it). 
); 

// We do not use exec() because we want to get the Stdout line by line. 
$process = proc_open($Command, $descriptorspec, $pipes); 

if(is_resource($process)) 
{ 
    $line = 1; 
    while(!feof($pipes[1])) 
    { 
     $InputLine = fgets($pipes[1], 1024); 
     if(strlen($InputLine) == 0) break; 
      echo '<pre><strong>Line: '.$line++.'<br /></strong>'; 
       print_r($InputLine); 
      echo '</pre>'; 
     ob_flush(); 
     flush(); 
    } 
    fclose($pipes[0]); 
    fclose($pipes[1]); 
    fclose($pipes[2]); 
} 
echo 'Done!'; 

目前輸出:

開始...

Line: 1
Encoding: task 1 of 1, 3.10 % Encoding: task 1 of 1, 6.61 % Encoding: task 1 of 1, 8.81 % Encoding: task 1 of 1, 11.44 % Encoding: task 1 of 1, 14.52 % Encoding: task 1 of 1, 18.03 % Encoding: task 1 of 1, 21.10 % Encoding: task 1 of 1, 25.05 % Encoding: task 1 of 1, 27.25 % Encoding: task 1 of 1, 29.03 % Encoding: task 1 of 1, 29.91 % Encoding: task 1 of 1, 33.45 % Encoding: task 1 of 1, 36.52 % Encoding: task 1 of 1, 39.19 % (316.92 fps, avg 318.50 fps, ETA 00h00m06s) Encoding: task 1 of 1, 42.46 % (316.92 fps, avg 318.50 fps, ETA 00h00m06s) Encoding: task 1 of 1, 46.44 % (316.92 fps, avg 318.50 fps, ETA 00h00m06s) Encoding: task 1 of 1, 47.32 % (316.92 fps, avg 318.50 fps, ETA 00h00m06s) Encoding: task 1 of 1, 49.96 % (294.34 fps, avg 315.56 fps, ETA 00h00m05s) Encoding: task 1 of 1, 53.03 % (294.34 fps, avg 315.56 fps, ETA 00h00m05s) Encoding: task 1 of 1, 56.54 % (294.34 fps, avg 315.56 fps, ETA 00h00m05s) Encoding: task 1 of 1, 61.08 % (336.12 fps, avg 330.04 fps, ETA 00h00m04s) Encoding: task 1 of 1,
Line: 2
61.95 % (336.12 fps, avg 330.04 fps, ETA 00h00m04s) Encoding: task 1 of 1, 65.03 % (336.12 fps, avg 330.04 fps, ETA 00h00m04s) Encoding: task 1 of 1, 67.13 % (336.12 fps, avg 330.04 fps, ETA 00h00m04s) Encoding: task 1 of 1, 68.89 % (320.33 fps, avg 319.28 fps, ETA 00h00m03s) Encoding: task 1 of 1, 71.12 % (320.33 fps, avg 319.28 fps, ETA 00h00m03s) Encoding: task 1 of 1, 74.42 % (320.33 fps, avg 319.28 fps, ETA 00h00m03s) Encoding: task 1 of 1, 76.65 % (307.54 fps, avg 312.58 fps, ETA 00h00m02s) Encoding: task 1 of 1, 79.72 % (307.54 fps, avg 312.58 fps, ETA 00h00m02s) Encoding: task 1 of 1, 83.38 % (307.54 fps, avg 312.58 fps, ETA 00h00m02s) Encoding: task 1 of 1, 86.01 % (285.58 fps, avg 315.19 fps, ETA 00h00m01s) Encoding: task 1 of 1, 87.33 % (285.58 fps, avg 315.19 fps, ETA 00h00m01s) Encoding: task 1 of 1, 89.08 % (285.58 fps, avg 315.19 fps, ETA 00h00m01s) Encoding: task 1 of 1, 91.72 % (285.58 fps, avg 315.19 fps, ETA 00h00m01s) Encoding: task 1 of 1, 94.79 % (278.94 fps, avg 307.07 fps, ETA 00h00m
Line: 3
00s) Encoding: task 1 of 1, 96.99 % (278.94 fps, avg 307.07 fps, ETA 00h00m00s) Encoding: task 1 of 1, 100.00 % (278.94 fps, avg 307.07 fps, ETA 00h00m00s) Encoding: task 1 of 1, 100.00 % (278.94 fps, avg 307.07 fps, ETA 00h00m00s)
完成!

結果不尊重新行並繼續搜索,直到達到1024字節。 是否有不同的方法可以用來逐行顯示輸出實時?

回答

2

我明白了:使用fread()而不是fgets(); fread()是二進制安全的。

+0

感謝您問及回答此問題。幫助我在這裏 – Simon