user1105430
user1105430

Reputation: 1399

PHP HandBrakeCLI won't output line by line using proc_open

Is there another way to display line by line, realtime when using HandBrakeCLI with php? So far I am using fgets() in proc_open() function to get by bytes. The fgets() manual mentions the length: "Reading ends when length - 1 bytes have been read, or a newline (which is included in the return value), or an EOF (whichever comes first).". So far it doesn't recognizes new line.

// 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!';

Currently outputs:

Starting...

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)
Done!

The result doesn't respect new lines and keeps searching until it reaches 1024 bytes. Is there a different method I can use to display the output realtime line by line?

Upvotes: 1

Views: 709

Answers (1)

user1105430
user1105430

Reputation: 1399

I figure it out: Use fread() instead of fgets(); fread() is Binary-safe.

Upvotes: 3

Related Questions