FFmpeg package for php - php

Does simple ffmpeg package also work fine when we execute its command from PHP via exec? Or we have some different ffmpeg package for that purpose?
Please help me solving it.

PHP's ffmpeg and the vanilla ffmpeg are independent packages. The php5-ffmpeg package comes with the ffmpeg binary as a dynamic library. This means that you can install only php5-ffmeg without installing the command line ffmpeg package or vice versa or you can have both of them installed.
Therefore, the answer is yes, PHP will work with the command line ffmpeg given it is installed.
I usually prefer using the command line ffmpeg in PHP via exec. This allows for testing certain operations on the command line and once the result is as desired, the command can be used in PHP's exec. On the other hand, in some situations, it may be preferable to use the php5-ffmpeg package because it gives you the ability to use object oriented coding style when dealing with videos, conversion, etc.

Yes simple ffmpeg does work through PHP exec if you have necessary permissions.
Sample command
<?php
/*** convert video to flash ***/
exec("ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv");
?>
Source

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.

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

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

How to install ffmpeg using xampp

I am trying to install ffmpeg onto my localhost server. I have followed countless blogs and tutorials online form other people who are stuck with this same problem. I follow everything to the letter.
Unzip the file
Copy php_ffmpeg.exe to ext folder in php
copy the rest to system 32
As it says however when I do this I get this error when I start apache.
Fair play but it is in the folder
I also get this error as well:
And Again, here it is:
I have added the extention to my php.ini file
extension=php_ffmpeg.dll
The first time I go this to work I placed the ffmpeg.exe file onto my local host server and ran commands like this:
$cmd = "$ffmpegpath -i $input -an -ss $sec -s $size $output";
shell_exec($cmd);
This works fine on my computer but not on an actual server. Could somebody offer some advice or guidene on where I have one wrong installing the extension or why running the .exe file on a lunix server with shell_exec doesn't work
Yeah ive had problems with the extension version, with windows. I found it is much easier to not use the PHP extension and just call the standard FFMPEG for windows binary with exec() within a simple class, ive added an example script. It will be trivial for you to add a ffmpeg_convert() method to the class.
VideoThumbNailer.example.zip
Edit
Just noticed this statement,
Could somebody offer some advice or guidene on where I have one wrong
installing the extension or why running the .exe file on a lunix
server with shell_exec doesn't work
Linux has its own versions of FFMPEG, you cant mix the two. Your need to install FFMPEG on linux with something like:
sudo apt-get -qqy php5-ffmpeg
And change the path to FFMPEG in your cmd to, leaving out the .exe part:
ffmpeg -i $input -an -ss $sec -s $size $output
Hope it helps

Running shell commands from PHP - With root permissions or sudo

I'll explain what I'm trying to do, because someone might have a much better and completely different approach to this.
I'm trying to install FFMPEG, but not just install it, I'm making an automated installer that installs some PHP scripts and also FFMPEG if it's not already installed.
I can't find any automated PHP scripts that can install FFMPEG, so I'm guessing this is more difficuilt than I thought.
I had 3 options to do this:
Build from source on the server using the exec function, this
requires root permissions.
Find a pre-compiled binary of FFMPEG, easily done, and I did it, but
then I realised it didn't have the correct codecs so wasn't working.
Use YUM or APT-GET to install FFMPEG, this would be great but it
requires root permissions.
So, how can I either:
Use YUM/APT-GET from PHP and set sudo
OR
Install codecs for FFMPEG after it has been compiled. Using the pre-compiled binaries that I found, if it were possible then I could just install the missing codecs (lame mp3 I think. Maybe others too). Or do the codecs need to be built into the binary.
I think the prefered option is to use YUM/APT-GET as my targets are mostly Centos 5 and maybe Ubuntu aswell.
sudo in php is a lot easier if you use SSH. Among other things you can make use of "expect" to wait for the appropriate prompts. If there's a way to do that with the built in exec() or system() I'm not aware of it.
If you end up compiling from source or installing from a repo you can handle the install processs through this project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('yum -y install FFMPEG');
//the return will be a string containing the return of the command
echo $return1;
You will need to deal with the edge cases when i.e. there is a GPG key that need to be accepted or dependencies are conflicting. But the project lets you script all Bash interaction.

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