I recently installed FFMpeg on my server, so I could make my script upload videos, then convert them to formats I want, like MP4 and FLV.
When I upload the file, I then call a function below:
PHP Code:
// Handle Video Uploads //
function videoHandle($_FILES, $dir, $number){
if (!file_exists($_SERVER['DOCUMENT_ROOT'].$dir))
mkdir($_SERVER['DOCUMENT_ROOT'].$dir, 0777);
$target_path = $dir . $number;
$cmd = "ffmpeg -i ".$_FILES['upload']['tmp_name']." -y -sameq -f mp4 ".$_SERVER['DOCUMENT_ROOT'].$target_path.".mp4";
$output = runExternal($cmd, &$code);
if($code) echo "Bad Transcoding: <br/><br/>".$cmd."<br/><br/>".$output."<br/><br/>";
else echo "<br/>Successfully Converted to MP4<br/>";
$cmd = "ffmpeg -i ".$_FILES['upload']['tmp_name']." -y -sameq -f flv ".$_SERVER['DOCUMENT_ROOT'].$target_path.".flv";
$output = runExternal($cmd, &$code);
if($code) echo "Bad Transcoding: <br/><br/>".$cmd."<br/><br/>".$output."<br/><br/>";
else echo "Successfully Converted to FLV<br/>";
}
The FFMpeg commands work fine, I've ran them in SSH, but when I call the script, I get:
Quote:
505 Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster development.psbeyond.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
|
Checking the error log, the entry says:
Quote:
|
[05-Feb-2008 13:23:09] PHP Warning: stream_select() [<a href='function.stream-select'>function.stream-select</a>]: unable to select [4]: Interrupted system call (max_fd=10) in /home/cmsdev/public_html/admin/includes/functions.php on line 190
|
It's referring to my function "runExternal" which I got from the php site:
PHP Code:
// Run Linux Command Line //
function runExternal($cmd, &$code) {
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a file to write to
);
$pipes= array();
$process = proc_open($cmd, $descriptorspec, $pipes);
$output= "";
if (!is_resource($process)) return false;
#close child's input imidiately
fclose($pipes[0]);
stream_set_blocking($pipes[1],false);
stream_set_blocking($pipes[2],false);
$todo= array($pipes[1],$pipes[2]);
while( true ) {
$read= array();
if( !feof($pipes[1]) ) $read[]= $pipes[1];
if( !feof($pipes[2]) ) $read[]= $pipes[2];
if (!$read) break;
$ready= stream_select($read, $write=NULL, $ex= NULL, 2);
if ($ready === false) {
break; #should never happen - something died
}
foreach ($read as $r) {
$s= fread($r,1024);
$output.= $s;
}
}
fclose($pipes[1]);
fclose($pipes[2]);
$code = proc_close($process);
return $output;
}
I assume its breaking at the "stream_select" part. I've tried doing it without this function, with just shell_exec, but I encounter the same problem. However it looks like it might be some kind of timeout, or overload of information, because if I just convert the FLV, it seems to work fine. Any ideas as to how to prevent this? I've tried upping the max_execution_time in my php.ini file already, but that didn't fix the problem
I noticed that the files are actually being created successfully, but the rest of the script does not execute (I have some mysql queries that come afterwards that don't execute)
I also just noticed through testing, that the files are still being created even after the Server Error is giving, meaning the commands are being executed in the background after the error.
However, it looks like if the error happens while the first file is still being made, the second command is not executed (and the second file is not created). So it depends on when the crash happens....