Delay waiting for exec("ffmpeg.. command to finish

Hi

I have a FLV thumbnail creating script that creates 5 thumbnails from a flv using ffmpeg via PHP exec(“”); command.


exec("ffmpeg -i ".$fx." -an -ss 00:".$time1." -an -r 2 -vframes 1 -y ".$config['BASE_DIR']."/thumbs/1/00000001.jpg");
$source = $config['BASE_DIR']."/thumbs/1/00000001.jpg";
$destination = $config['BASE_DIR']."/thumbs/1/1_01.jpg";
 
resize_to_thumb($source,destination,$config['img_max_width'],$config['img_max_height']);

//....4 more times

I guess the questions are:

  1. Does PHP wait for the exec command to complete before continuing on to the next php commands?
  2. If PHP doesn’t wait, would there be a way of forcing it to wait…eg, create a while loop that checked the fileszie of 00000001.jpg etc, and the while loop wouldn’t stop until the filesize has stopped increasing in size (ie. when the image had completely generated) or, when the filesize is > 0 (depending if the output is written incrementally, or all at once)?

I’m wanting to prevent > 5 multiple ffmpeg instances running at the same time, as this script is in a batch FLV uploader, so 400 vids could be uploaded at once, and I don’t wont more than 5 (the number of thumbs per vid) ffmpeg to be happening at once (or is 5 too many?, if so 1 a time would be fine). Just wanting to prevent the possibility of 100s of ffmpeg instances running.
Thanks

http://php.net/function.exec

When allowing user-supplied data to be passed to this function, use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.

Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

Thanks, I’m not sure I understand what the first quote means.

But does the second mean, that PHP waits for the execution of the program ends, because the ouput isn’t being redirected?

When you pass a user variable to a system function, you need to clean it to make sure it will only be used as intended.

Yes.

Yes, by default exec() won’t return until the program finishes. Now, it’s definately possible that some programs might in turn spawn thier own children so to speak, which will do the work, so the parent program can just die off immediately. This would be a case where exec() could possibly return before the operation was actually done(some scenarios), but you probably won’t encounter too many programs like this. In a case like this, you would need to do a loop like you suggested where you just keep sleeping and doing a manual check for completion.

Ok thanks.

So, no need for this while loop then as PHP waits ('cos no output redirection), preventing hundreds of instances of ffmpeg being executed at once (by this same script)?