I'm coding up a website back-end that will include user-uploaded video. In order to ensure maximum accessibility, I'm compressing the uploaded videos and re-saving them as .mp4 and .webm format to cover all browsers (or as many as possible anyway). To do this, I'm running an avconv command in the PHP exec() function.
I don't want to make the user wait for the script to finish before the page loads, so I'm running the code asynchronously. My code so far is below.
exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 > /dev/null 2>/dev/null &"');
exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"');
In addition to running the exec functions, I also save the video to a database and send the user an email thanking them for uploading their video.
Here's the rub: I want the server to WAIT until the video conversion is finished, and THEN add it to the database and send the user an email. Basically, the program flow would be:
User uploads video. Video is placed in a temp folder. User is taken to a thank you page indicating their video will be up shortly. The server executes two avconv commands to convert and compress the video for web use. Once BOTH conversions are finished, the video info is added to a MySQL database, an email is sent to the user, and the original uploaded video is deleted.
It may just be my ignorance of the command line (in fact it almost definitely is), but how could I 'queue up' these commands? First do both conversions, then call a PHP script to add to the database, then delete the original video, all while being asynchronous with the original PHP script?
EDIT: I've tried queuing them up with an '&&' operator, like below:
exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 && avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"');
However, that seems to cancel out the fact that I'm running it asynchronously, since the page now seems to wait for the command to finish.