It is possible with PHP. However, there are more suited programming languages like Phyton, Ruby, C++ or C# which cope with background tasks better.
To carry out background tasks in PHP you'd need to use something like exec
, system
, shell_exec
, popen
or something more advanced like fsockopen
and pcntl_fork
.
It all depends on your stack. There are job servers like Gearman
which need some setup, but make working with jobs in php easier as the processing is handled by such servers.
An approach to what you want to achieve:
Create a php script that converts and saves the video
CHMOD the file with 755 to make it executable in CLI
Run the script in the background as soon as a video should be converted, like so:
pclose(popen('start "Process" /B "C:\PHP\php-cgi.exe" -f video_converter.php 2>&1 &', 'r'));
"Process"
is the title of the background cli prompt. It's required to prevent problems. /B
starts the command without a prompt window. The output of the script will be supressed with 2>&1 &
to prevent interruptions.
However, I'd let the script write (e.g. file_put_contents) the convertor-progress to a file and listen with ajax (javascript / jquery) on the website the player resides on. As soon as 100% is reached, the video filepath is returned and the javascript updates and starts the embedded player.
For large video files that would make the convertion take too long to wait, you could send the user an email as soon as the video is ready to be watched - in case you make this application available to the public.
NOTE: The command is specific for windows servers. Non-Windows OS like *NIX, Linux and Sun the following command is used:
shell_exec(sprintf('nohup /usr/bin/env php -q -f %s > /dev/null 2>&1 & echo $!', 'path/to/video_converter.php'));
nohup
is no hangup and keeps the background process running until it's terminated. The rest is about the same as the windows command, just modified a little.
Give it a try and let us know if it worked :-)
Here are some resources worth reading with more info on this topic: