• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

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

0
RSPANGLER_AVTIA
Asked:
RSPANGLER_AVTIA
  • 2
  • 2
1 Solution
 
fiboCommented:
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...
0
 
Beverley PortlockCommented:
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




            
0
 
Beverley PortlockCommented:
One other point, I suspect that most of the work in this is being done by ffmpeg and very little by PHP. I cannot see where you will get much saving from 'optimising' the PHP.

I personally would code the whole thing in a shell script and, if needed, call that once via exec
0
 
RSPANGLER_AVTIAAuthor Commented:
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

0
 
RSPANGLER_AVTIAAuthor Commented:
Thanks - that's a good call - appreciate it
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now