Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Macman1993

macrumors 6502
Original poster
Nov 23, 2007
337
16
I have ffmpeg installed on my macbook pro running sl 10.6.4 and I have enabled php on it. I am trying to use php on my machine to run ffmpeg commands to convert video. I have been attempting to use exec() command but it is not working. When I type the command directly into terminal it runs without a problem but when trying to do this through php nothing happens. I am attempting to convert about 600 short videos doing this and no programs that support bulk encodes will work. Heres my code does anyone see why this wouldn't work?

PHP:
exec('ffmpeg -i /Library/WebServer/Documents/video.avi -vcodec libx264 -vpre hq -vpre ipod640 -b 250k -bt 50k -acodec libfaac -ab 56k -ac 2 -s 480x320 /Library/WebServer/Documents/video.mp4');
 
Sometimes you have to specify the exact path to the program/script name. So figure out where exactly the ffmpeg is, and then call it like this...

PHP:
exec('/usr/local/bin/ffmpeg ...');

Of course, substitute /usr/local/bin for the correct path.

HTH!
 
I've checked and my php.ini file lists safe mode as off. I also tried specifying the location of ffmpeg in my script but still no luck.

PHP:
exec('/opt/local/bin/ffmpeg -i /Library/WebServer/Documents/video.avi -vcodec libx264 -vpre hq -vpre ipod640 -b 250k -bt 50k -acodec libfaac -ab 56k -ac 2 -s 480x320 /Library/WebServer/Documents/video.mp4');

Just to give it a shot I also tried this with no luck.

PHP:
define('FFMPEG_LIBRARY', '/opt/local/bin/ffmpeg ');
$exec_string = FFMPEG_LIBRARY.' -i /Library/WebServer/Documents/video.avi -vcodec libx264 -vpre hq -vpre ipod640 -b 250k -bt 50k -acodec libfaac -ab 56k -ac 2 -s 480x320 /Library/WebServer/Documents/video.mp4';
exec($exec_string);
 
When you say "no luck" — what do you see when you run the script (I guess you're running it in a web browser).

What happens if you run the php script from the command line?

Code:
php path/to/script.php
 
Thanks, the idea about running the script through terminal worked and the video converted. I'm trying to use php to run the ffmpeg conversion on all the flv files I have in a folder so I have added to the script to make it loop through all the files and attempt to convert the flv files. Whenever I run this through terminal it wont convert the videos though. I know my conversion command is working fine and I can move the commands out of the "if ($ext=='flv')" statement and it will work. I need to check that these are flv files so I don't try and convert the wrong things but it simply will not work. And terminal doesn't display any errors or even the text "its an flv".

PHP:
<?php
	
	$dir = opendir(".");

		
	while (($file = readdir($dir)) !== false) {
		
		$ext = substr($file, strrpos($file, '.') + 1) ;
		
                //Checks that the file is an flv file so I don't convert files I don't want converted (this condition breaks the script when run through terminal)
		if ($ext=='flv') {
		
		        //Used for testing in a browser to make sure the condition is valid
			echo "its an flv";

  	 		//Code to convert the videos which will not work inside this if loop
  	 		$exec_string ='/opt/local/bin/ffmpeg -i /Library/WebServer/Documents/video.flv -vcodec libx264 -vpre hq -vpre ipod640 -b 250k -bt 50k -acodec libfaac -ab 56k -ac 2 -s 480x320 /Library/WebServer/Documents/video.mp4';
  	 		
  	   		exec($exec_string);
   		}
		   
	}
?>

Oddly enough if I change the condition "if ($ext=='flv')" to something I know is true like "if (1==1)" it runs. I know the condition is working because when viewed in a browser the text "Its an flv" is showing up the proper amount of times.

Can someone please explain what is happening I am at a loss here.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.