[eluser]montassis[/eluser]
Hey All,
I am writing a CI application where users can upload video files. I have read quite a lot about doing video conversion using ffmpeg and understand the basics. My problem is that I cannot seem to work out how to run this process in the background so the website doesn't just hang on a blank page while the video is being converted. Currently, I am attempting to run ffmpeg as a background process, but the view is not loaded until the conversion is complete.
Here is a portion of code from my Upload controller
Code:
function uploadVideo() {
$config['upload_path'] = getcwd() . '/uservids/' . $this->session->userdata('accountid');
$config['allowed_types'] = 'mov|wmv|ogv';
$filename = md5(date(DATE_RFC822));
$config['file_name'] = $filename;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('videoFile')) {
[removed code]
} else {
$videoDetails = $this->upload->data();
$videoDetails['accountid'] = $this->session->userdata('accountid');
$videoid = $this->_createVideo($videoDetails);
// page data
$mainData['success'] = $this->upload->data();
$mainData['firstName'] = $this->session->userdata('firstName');
$mainData['lastName'] = $this->session->userdata('lastName');
// form data
$mainData['title'] = array('name' => 'title', 'id' => 'title');
$mainData['comment'] = array('name' => 'title', 'id' => 'title', 'rows' => '8', 'cols' => '37');
$mainData['videoid'] = array('name' => 'videoid', 'value' => $videoid);
// load the view
$data['mainView'] = 'upload/upload_success';
$data['mainData'] = $mainData;
$this->load->view('template', $data);
// conversion
$command = 'ffmpeg -i ' . $videoDetails['full_path'] . ' ' . $videoDetails['file_path'] . $filename . '.ogv';
echo("Running file conversion");
$ps = $this->_runInBackground($command);
// while process is running, print a nice little dot
while ($this->_isProcessRunning($ps)) {
echo(" . ");
ob_flush();
flush();
// sleep for .25 seconds
usleep(250000);
}
}
}
// inserts into database, returns new videoid
function _createVideo($details) {
return $this->Video->createVideo($details);
}
// executes function to run in the background (not working)
function _runInBackground($command, $priority = 0) {
if ($priority)
$PID = shell_exec("nohup nice -n $priority $command 2> /dev/null & echo $! &");
else
$PID = shell_exec("nohup $command > /dev/null 2> /dev/null & echo $! &");
return($PID);
}
// this works
function _isProcessRunning($PID) {
exec("ps $PID", $ProcessState);
return(count($ProcessState) >= 2);
}
}
I know the conversion is working, because I get the output file and a line up the top that says "Running file conversion................................" with a bunch of dots.
So does anyone know how to run this type of thing in the background? Also, if possible, rather than sending the output to /dev/null I wouldnt mind sending it to a text file that I could read and then get info like the duration and number of frames so i could come up with a progress bar.
Any help or info much appreciated.