Reputation: 15
the problem i'm dealing with is getting the shell output from a ffmpeg command while it's being executed and writing it in an html page using php.
After some research i found a very similar request here :Update page content from live PHP and Python output using Ajax, which seemed to be perfect, but it's not working at all.
The basic idea is to use an AJAX request to invoke the PHP script, which should execute the command and echo the live read content from the process, taking care to use this.readyState==3 (otherwise the JS script would receive the response only upon completion)
For the PHP section i tried using the code in the answer above, (obviously adapted to my needs):
function liveExecuteCommand($command){
while (@ ob_end_flush()); // end all output buffers if any
$proc = popen($command, 'r');
$live_output = "";
$complete_output = "";
while (!feof($proc))
{
$live_output = fread($proc, 4096);
$complete_output = $complete_output . $live_output;
echo "<pre>$live_output</pre>";
@ flush();
}
pclose($proc);
}
And for the AJAX section i used
function getLiveStream(){
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (this.readyState == 3) {
document.getElementById("result").innerHTML = this.responseText;
}
};
var url = 'process/getlive';
ajax.open('GET', url,true);
ajax.send();
}
Which sadly doesn't work.
The command being executed is this: 'ffmpeg.exe -i "C:/Users/BACKUP/Desktop/xampp/htdocs/testarea/test.mp4" -map 0:0 -map 0:1 -c:v libx264 -preset fast -crf 26 -c:a libmp3lame -ar 24000 -q:a 5 "C:\Users\BACKUP\Desktop\xampp\htdocs\testarea\output/test.mkv"'
, which i tested and it works.
When i run the html page and the ajax script within, the ffmpeg command doesn't even run, as i checked in task managet. It simply returns a blank text.
When i run the php script by itself, the command runs, the file is converted but it doesn't echo anything at all.
After some more research i also found this page, which seems to be made for this exact purpose: https://github.com/4poc/php-live-transcode/blob/master/stream.php
The relevant section is at the end, the code before is for dealing with options specific to ffmpeg. But it didn't work either, with the same exact outcomes.
Now i'm considering simply writing the output to a file and reading it from it dinamically, but i'd really like to know the reason why they both don't work for me.
EDIT: PHP Execute shell command asynchronously and retrieve live output answers to how to get content from a temporary file that is being written, not directly from the process.
Upvotes: 0
Views: 2138
Reputation: 31100
ffmpeg writes its status to stderr
not stdout
see http://php.net/manual/en/function.popen.php example 2
<?php
error_reporting(E_ALL);
/* Add redirection so we can get stderr. */
$handle = popen('/path/to/executable 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
?>
Upvotes: 1