Hello, everyone. I've recently been trying to work with FFmpeg and PHP. I can easily convert video files this way using popen() to call FFmpeg, and since FFmpeg outputs its status to STDERR instead of STDOUT, I can even use this to get the progress information from the process.
Here's the code I'm using now for this:
<?php
error_reporting(E_ALL);
set_time_limit(600);
$fileName="OriginalVideo.mp4";
$newName="ConvertedVideo.flv";
/* Add redirection so we can get stderr. */
$handle = popen('"Easy FFmpeg/ffmpeg.exe" -i "Easy FFmpeg/'.$fileName.'" -ar 22050 '.$newName.' 2>&1', 'r');
while ($line=fread($handle,2096)) {
echo $line."<br/><br/>";
}
pclose($handle);
?>
That works fine; the only issue is it then waits until the entire file is converted (that is, until there's nothing left to read) before outputting anything. I'd rather it show progress as the conversion takes place...
To that end, I thought there might be a way to use some AJAX and Javascript to connect to a separate PHP file which takes the resource ID as a GET parameter and reads/echos a single line (and not close the process until there's nothing left to read). However, I can't seem to figure out how to pass the resource ID from one file to another. If I just echo $resource into the source code of the page, it gets converted to the string "resource ID #2", which of course is not the actual ID and thus errors out.
Can anyone tell me how to preserve the resource ID and echo it to the page, or rather, how to pass resource IDs from one PHP file to another? Is it even possible? If not, is there a better way to go about monitoring the process's progress without holding up the buffer? I've tried adding ob_flush() after each line is read, but no dice: it still waits until the process ends before outputting anything.
-IMP 😉 🙂