简体   繁体   中英

FFMPEG command doesn't work in PHP. (Incompatibilty with MAMP)

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

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.

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');

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.

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.

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>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM