Link to home
Start Free TrialLog in
Avatar of RSPANGLER_AVTIA
RSPANGLER_AVTIA

asked on

PHP while loop - Need Help Making it As Fast As Possible

Hello,

I have a PHP while loop in place to reduce a video in size, but it's timing out bigtime - please see below. I think the second while loop is sending it into some sort of endless cycle that boggs the process down, but wanted to see if anyone could help me streamline it.

The idea is that I'm bringing down one variable by 8000k, cycling through another variable (3-15), then going back and bringing down the other variable by 8000 and bringing the doing the 5-15 cycle again, etc.

Adding high points because it's time sensitive. Thanks!


$QMP4 = 3;
$BMP4 = 72000;
$MP4MAXSIZE = 550000;

while (filesize($filenameMP4) > $MP4MAXSIZE) {
$BMP4 = $BMP4 - 8000;

		// raise the QMIN for each bitrate
		while ((filesize($filenameMP4) > $MP4MAXSIZE) && ($QMP4 < 16)) {
		
		// clear cache
		clearstatcache();
		
		// Reduce quality to reduce size - start with qmin
		$QMP4 = $QMP4 + 1;
		exec($execstringMP4REDUCE.' 2>&1', $out );
		$execstringMP4REDUCE = 'ffmpeg -y -i OUTPUT/COMBO.avi -s 320x240 -r 15 -b '. $BMP4 .' -qmin '. $QMP4 .' -vcodec mpeg4 -acodec libfaac -ab 24000 -ac 1 '.$filenameMP4;
		echo "$execstringMP4REDUCE<br><pre>";
		
		// GET THE FILE SIZE
		echo $filenameMP4 . ': ' . filesize($filenameMP4) . ' bytes (Max: 768000)<p>';
		} // end the qmin raising loop
} // END FIRST LOOP

Open in new window

Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

Not sure what is really the result of line 16 at the first run in the loop...

I would think line 16 and 17 should be swapped...
Two comments

First, these lines look to be the wrong way round. Define $execstringMP4REDUCE before you pass it to exec

exec($execstringMP4REDUCE.' 2>&1', $out );
$execstringMP4REDUCE = 'ffmpeg -y -i OUTPUT/COMBO.avi -s 320x240 -r 15 -b '. $BMP4 .' -qmin '. $QMP4 .' -vcodec mpeg4 -acodec libfaac -ab 24000 -ac 1 '.$filenameMP4;


Second, I expect that you will need to change PHP's resource settings in PHP.INI as PHP has a timeout set to 30 seconds. Setting this to zero makes it infinite but if you are not on a DEDICATED server then the hosting company may impose a limit that you cannot override or you may not have access to max_execution_time

http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time




            
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RSPANGLER_AVTIA
RSPANGLER_AVTIA

ASKER

Quite correct - I pasted the code wrong (took out some of my stuff)

Here's the amended version
$QMP4 = 3;
$BMP4 = 72000;
$MP4MAXSIZE = 550000;

while (filesize($filenameMP4) > $MP4MAXSIZE) {
$BMP4 = $BMP4 - 8000;

		// raise the QMIN for each bitrate
		while ((filesize($filenameMP4) > $MP4MAXSIZE) && ($QMP4 < 16)) {
		
		// clear cache
		clearstatcache();
		
		// Reduce quality to reduce size - start with qmin
		$QMP4 = $QMP4 + 1;
		exec($execstringMP4REDUCE.' 2>&1', $out );
		$execstringMP4REDUCE = 'ffmpeg -y -i OUTPUT/COMBO.avi -s 320x240 -r 15 -b '. $BMP4 .' -qmin '. $QMP4 .' -vcodec mpeg4 -acodec libfaac -ab 24000 -ac 1 '.$filenameMP4;
		echo "$execstringMP4REDUCE<br><pre>";
		
		// GET THE FILE SIZE
		echo $filenameMP4 . ': ' . filesize($filenameMP4) . ' bytes (Max: 768000)<p>';
		} // end the qmin raising loop
} // END FIRST LOOP

Open in new window

Thanks - that's a good call - appreciate it