How do I run ffmpeg with PHP on a Mac with XAMPP - php

There is literally nothing on this anywhere, the little there is does not work and is too advanced for me. If you can answer this you would make my life. How do I run ffmpeg commands from php which is running on my localhost? I prefer XAMPP, but MAMP will do just fine, at this stage I will use anything. Do I need to install ffmpeg on the XAMPP server itself, or does it just need to be installed on the mac? I can run the ffmpeg commands fine from terminal, for example,
ffmpeg -i Tmpfile.mp4 -c:v libx264 -preset ultrafast video.mp4
works fine, but
echo shell_exec("ffmpeg -i Tmpfile.mp4 -c:v libx264 -preset ultrafast video.mp4");
does nothing when I load the webpage with the index.php that that code is in.
I will buy you a cookie if you can solve my problem :)

You should be more specific when describing the problem, e.g. including OSX version, ffmpeg version, etc.
I successfully managed to reproduce your case, here it is the procedure:
1. ffmpeg
I installed ffmpeg via brew. ffmpeg version is currently 3.2.4.
2. PHP server
On macOS Sierra (but other previous versions offer this feature as well), you can start a PHP server by just typing
$ php -S 0.0.0.0:8080
in your shell. The web server will listen on port 8080 in that case.
3. The PHP script
I basically used the script you gave in your question, with a little modification:
<?php
echo "loading, please wait...";
echo shell_exec("ffmpeg -i temp.mp4 -c:v libx264 -preset ultrafast video.mp4 2>&1");
Did you notice the 2>&1? It is used to redirect the sterr output to the stdoutput as ffmpeg would not print anything on standard output :)
4. Safe mode?
Please be sure to disable PHP safe mode, as the shell_exec method is disabled when safe mode is ON (reference here).

Ah, I have found the answer. I needed to add this to the beginning of the php script
exec('unset DYLD_LIBRARY_PATH ;');
putenv('DYLD_LIBRARY_PATH');
putenv('DYLD_LIBRARY_PATH=/usr/bin');

Related

ffmpeg: not found when running ffmpeg via php xampp (mac)

I am trying to execute ffmpeg from php. I have installed ffmpeg locally on my mac via homebrew and I am able to run the commands I need via terminal.
When I try to execute the following code:
<?php
echo "Starting ffmpeg";
$output = shell_exec("ffmpeg -i test.mp3 -codec:a libmp3lame -b:a 128k out.mp3 2>&1");
echo "<pre>$output</pre>";
?>
I am receiving the following on my browser:
Starting ffmpeg
sh: 1: ffmpeg: not found
I am assuming that I somehow need to install ffmpeg to my xampp server but it is not obvious how to do that. After searching online I can find linux and Windows tutorials but I couldn't figure out something out by looking at them.
What I tried doing was to download the ffmpeg static build form https://ffmpeg.org/download.html#build-mac and placed it in the htdocs holder, and then tried to execute ffmpeg as if it was an executable (after changing chmod), but that gave me
sh: 1: ./ffmpeg: Exec format error
How would one go and install and then run ffmpeg on their xampp server?
First check for installation of tha package by "ffmpeg -version"
If it is installed then check for path "whereis ffmpeg", then try your command with full ffmpeg path
I realised that the apache server that xampp starts is a debian instance. I was using the wrong static build (mac instead of debian one). Downloading and using the debian one resolved my issue.

No response from FFMPEG from inside php (works in command line)

So I have this executable working fine in a Windows 10 dev environment:
$cmd = 'C:/ffmpeg/bin/ffmpeg.exe -i video.mp4 -i audio.mp3 -c:v copy -c:a aac output.mp4';
exec($cmd, $output)
However when I move it into the Ubuntu staging environment and change the path to:
/usr/bin/ffmpeg
there is no response from it.
The following works in the ubuntu terminal
ffmpeg -v, ffmpeg -i, php -v, ...
but there is no response from it from inside php.
Am I missing something here?
I've also tried adding sudo to the above command which seems to make no difference.
(Yes I know I should be developing in the same environment as the staging/prod servers but it's not an option in this specific case).

GLIBCXX_3.4.9 not found when running ffmpeg from php in lampp server

I've written a php program which creates a video from sequence of images using ffmpeg.
<?php
$res = shell_exec("ffmpeg -framerate 50 -i image/image%d.png -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4 2>&1");
echo "$res";
When I run it,
it says GLIBCXX_3.4.15 , GLIBCXX_3.4.9 , GLIBCXX_3.4.11 not found.
ffmpeg: /opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not
found (required by /usr/lib/i386-linux-gnu/libjack.so.0) ffmpeg:
/opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.9' not found
(required by /usr/lib/i386-linux-gnu/libzmq.so.3) ffmpeg:
/opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.11' not found
(required by /usr/lib/i386-linux-gnu/libopencv_core.so.2.4) ffmpeg:
/opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.9' not found
(required by /usr/lib/i386-linux-gnu/libopencv_core.so.2.4)
But from the terminal, ffmpeg -framerate 50 -i image/image%d.png -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4 command works fine. Why php can't find the libraries while from the terminal it works fine? and what should I do to fix the problem in php? Thanks in advance.
I also got similar problem while executing the ffmpeg command from PHP script. I found the solution by executing the following command from the terminal. I am pasting that command here so somebody get help from this.
sudo mv /opt/lampp/lib/libstdc++.so.6 /opt/lampp/lib/libstdc++.so.6.orig
Now just run your php script, hope that will work.
If u have tried all the above methods(change name to .orig/_old, copy newer version to the lib folder, change LD_LIBRARY_PATH in xampp),
but still failed to solve this problem. Try this trick which worked for me finally:
1.add below line in your PHP file.
var_dump(shell_exec("whoami"));
you can find the username in your browser after clicking the url related to your PHP file
2.in your terminal
sudo vim /ect/sudoers
#in vim editor, add this line in your sudoers file
"username ALL=(ALL) NOPASSWD:ALL"
#change username to that found in step one
3.change the exec cmd in your PHP file to
exec("sudo python3 urfilename.py")
The main reason for your problem maybe the root permission, so by changing your sudoers file, the error might be tackled.

FFMPEG works from command line but not PHP

I have a strange situation that has just happend. FFMPEG is no longer executing from PHP but will from the command line.
Here is exactly what the command is:
ffmpeg -i ../../uploads/ee78d5deb564901626067cc0008456ed.mp3 -ab 96k -y ../../uploads/mp3/ee78d5deb564901626067cc0008456ed_6203688.mp3
How it is executed in the PHP script:
if(! exec("ffmpeg -i ".$target_path."".$hash_filename.".".$path_extension." -ab 96k -y ".$target_path."mp3/".$hash_filename."_".$session_ID.".mp3")){
echo 'ffmpeg failed';
}
This command did work but not longer does. I have recently updated plesk but I highly doubt that has affected this. The only thing that I think could affect it that I have recently done is have the file upload go to a subdomain. So the directory where the file is located and stored in the command is in a directory outside the document root. However, the move_uploaded_file function works and I have altered the open_basedir in PHP ini to the webspace root.
tail -f /var/log/apache2/error_log
and lets us know what you see there...Adjust for your platform...
this is for lamp (opensuse )
You can try the system() command. It will return you the response from server
system("ffmpeg -i ".$target_path."".$hash_filename.".".$path_extension." -ab 96k -y ".$target_path."mp3/".$hash_filename."_".$session_ID.".mp3")
if the ffmpeg is not supported with current version of the php it will return you the error.
OR
you can modify your command to get ffmpeg with proper path. In my case it works like below code
exec("/usr/local/bin/ffmpeg -i ".$target_path."".$hash_filename.".".$path_extension." -ab 96k -y ".$target_path."mp3/".$hash_filename."_".$session_ID.".mp3"))

PHP's exec() not executing command for FFmpeg

I have installed ffmpeg on my server and it works fine via my terminal. I'm able to successfully convert a file to webm format, so I'm sure the installation is fine. I'm also sure that I only have one installation of ffmpeg installed on my machine.
A problem arises when I try to convert files through PHP via PHP's exec(). When I run the same commands, I ran in the terminal, nothing happens. I looked around stackoverflow and other parts of the net for some help. I tried this to see the output:
exec($cmd, $out, $rv);
echo "output is:\n".implode("\n", $out)."\n exit code:$rv\n";
The output is: "output is: exit code:127"
The command I'm using is in this format:
ffmpeg -i "sample.mov" -vcodec libvpx -r 30 -b "644k" -acodec libvorbis -ab 128000 -ar "44100" -ac 2 -s "352x198" "sample.webm"
I've tried replacing "ffmpeg" with the full path to FFmpeg but that did not work.
Why isn't the script running the command correctly and converting the files?
Thank you!
Error code 127 means the executable (ffmpeg) couldn't be found. Try specifying the whole path (you can that out find in your terminal with which ffmpeg) or compare the value of the PATH environment variable in your php script and terminal.
I have similar problem with ant target executions from php. I can't get whole output from ant command only first few rows and ant target was not executed. In other words is partial executed.
With bellow command I've managed to run it but output of the command is append to log_file.log.
$commandString = 'you_command_here >> log_file.log 2>&1 &';
$command = exec($commandString);
Hope this will work for you.

Categories