在使用带有PHP的HandBrakeCLI时,有没有其他方法可以逐行显示,实时显示?到目前为止,我在proc_open()函数中使用fgets()来逐字节获取。 fgets()手册提到了长度:“读取时长度 - 读取1个字节,或换行(包含在返回值中)或EOF(以先到者为准)。”。到目前为止,它还没有识别新行。
// 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字节。 我可以使用不同的方法逐行显示输出实时吗?
答案 0 :(得分:2)
我弄明白:使用fread()而不是fgets(); fread()是二元安全的。