1

I'm a bit of a beginner when it comes to PHP, and I'm trying to create a simple(ish) system where files are input, and then converted to html5 video in various resolutions.

I've sorted out how to handle multiple file uploads etc, but now I'm having a problem.

I can't seem to get exec to execute FFMPEG in PHP.

For example, if I type this into my command line (Terminal on Mac OSX 10.8), It converts the video correctly:

ffmpeg -i /Users/charlieryan/Desktop/MOV01785.MPG websample.mov

This correctly outputs the converted video file into my home directory.

However if I run this in PHP as follows:

exec('ffmpeg -i /Users/charlieryan/Desktop/MOV01785.MPG websample.mov');

Absolutely nothing happens ... my stat monitor doesn't register any change in processor use, and I can't find the output file anywhere on my system.

Since I'm a bit of a noob at this, I'm assuming I'm doing something wrong, but what is it?

Thanks,

Charlie

Charlie Ryan
  • 211
  • 4
  • 16

5 Answers5

2

After alot of help from birgire and a lot of fiddling around I've sorted it.

This problem comes from an incompatibility with the MAMP sandbox. Which can be solved as follows:

Go to Terminal and type:

sudo nano /Applications/MAMP/Library/bin/envvars

Then comment out the following lines with a hash (#)

# DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
# export DYLD_LIBRARY_PATH

And then add the following line to the file

export PATH="$PATH:/opt/local/bin"

Then, go back to MAMP and restart your servers, navigate back to the page, and you'll be good to go.

Community
  • 1
  • 1
Charlie Ryan
  • 211
  • 4
  • 16
1

You should first try to see if exec() is allowed:

<?php echo exec('echo "exec() is working"');?>

if it's working you should get

exec() is working

If it works you should try

exec('/full/path/to/ffmpeg -i /Users/charlieryan/Desktop/MOV01785.MPG websample.mov');
birgire
  • 11,258
  • 1
  • 31
  • 54
  • Nope, nothing ... the full path to ffmpeg is /usr/local/bin/ffmpeg ... once again works in terminal, doesn't work in php – Charlie Ryan Mar 03 '13 at 14:29
  • from terminal it outputs in my home folder /Users/charlieryan/ – Charlie Ryan Mar 03 '13 at 14:34
  • you can try 2>&1 at the end of the exec string, to see if it is reporting any errors, i.e. like `exec('ffmpeg -i /Users/charlieryan/Desktop/MOV01785.MPG /Users/charlieryan/websample.mov 2>&1')` – birgire Mar 03 '13 at 14:43
  • for example if you do `echo exec('asdf 2>&1 ')` you get the error message: `asdf: command not found` but no output if you try `echo exec('asdf')` – birgire Mar 03 '13 at 14:49
  • again, absolutely nothing nothing appears. I'm stumped! – Charlie Ryan Mar 03 '13 at 14:51
  • The echo exec('asdf 2>&1 ') gives an error message as it should, but FFMPEG shows nothing – Charlie Ryan Mar 03 '13 at 14:57
  • Thanks for your time, really appreciate it! I just found that adding echo before the exec('ffmpeg... displays the error message "sh: ffmpeg: command not found" ... when I use the full path to FFMPEG I get the following "in /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib" – Charlie Ryan Mar 03 '13 at 14:59
1

I had the same problem, If you use MAMP, the problem is because mamp's php can't find the correct library, I don't know why!, so.. here's the trick. - You should use system's php to execute the php which will call to ffmpeg

In your php code (ex: lib.php | index.php):

function callToSysPHP ($videoName) {
    // $cmd = '/path to php/php <your php script> args';
    // In my case 
    $cmd = '/usr/bin/php myffmpeg.php ' . $videoName; 
    shell_exec($cmd);
}

In myffmpeg.php:

 $videoName = $argv[1];
 //$cmd = 'path to your ffmpeg/your ffmpeg command';
 // In my case my ffmpeg cmd looks like
 $cmd =  '/usr/sbin/' . 'ffmpeg -f image2 -framerate 25 -i ./files/pngs/%1d.png -vf scale=480:640 -vcodec libx264 -refs 16 -preset ultrafast ./files/pngs/'. $videoName .'.mp4 2>&1';
 echo '<pre>'; print_r(shell_exec($cmd)); echo '</pre>';

Basically from your mamp php, call a system php to execute a php file wich calls a ffmpeg throught shell_exec();

I hope this can help you.

  • I spent weeks putting together an application and it worked flawlessly. I had a crash and had to reinstall my Mac OS; and then walked away from the app for a few months. When I came back it wasn't working and I couldn't figure out why. Your solution saved my ass. Thanks!!! – timgavin Mar 14 '16 at 19:43
0

have you ffmpeg installed on windows machine? what happens if you run the same command from command line without php, does it work? If it doesn't, it hasn't to do anything with PHP.

amik
  • 5,613
  • 3
  • 37
  • 62
  • Nope, on Mac OSX 10.8 as stated above ... it works without PHP just fine, but if I try and execute it from within PHP it does nothing – Charlie Ryan Mar 03 '13 at 14:20
0

If '/usr/local/bin/' is the directory where you can find the ffmepg executable try this one:

<?php
$cmd = 'PATH="/usr/local/bin/"; ffmpeg -i /your/file/destination/batman.mp4 2>&1';
echo "<pre>".shell_exec($cmd)."</pre>";
?>