in php exec function is not working to run a shell command.
if I run in terminal
$ avconv -i in.mp4 -f mp3 -ab 192000 -vn rip.mp3
the command is working fine, the command is for converting a video to mp3.
But when I try to execute through PHP it just doesn't work.
exec("avconv -i in.mp4 -f mp3 -ab 192000 -vn rip.mp3");
or if I try exec("whoami"); is giving me the correct output.
Most likely you need to give the command the full path to your files, because the current dir in PHP is not the same as in your shell.
exec("avconv -i /path/to/in.mp4 -f mp3 -ab 192000 -vn /path/to/rip.mp3");
Maybe even the full path to aconv to be safe:
exec("/bin/avconv -i /path/to/in.mp4 -f mp3 -ab 192000 -vn /path/to/rip.mp3");
Check what is the correct path for aconv with which aconv.
To check for any error, add a second parameter to the exec command and print it:
exec('...', $result);
var_dump($result);
You need to follow these steps.
First check the exec() function exists or not.
if(function_exists('exec')){
echo 'Function exists';
}else{
echo 'Function does not exists';
}
If it exists then you may have the syntax error in your exec code. If it does not exists check it is disabled under php.ini by using the function given below.
function disabled_functions(){
$disabled = explode(',', ini_get('disable_functions'));
return $disabled;
}
echo "<pre>";
print_r(disabled_functions());
The above function will list out all the disable functions in php.ini.
If exec exists in the output of the above disabled_functions(). Then go to /etc/php.ini and remove exec from disable_functions
After saving php.ini file restart php-fpm. In case of Redhat/CentOS 7 and Fedora.
sudo systemctl restart php-fpm
In case if you are using Cpanel and WHM Panel
The exec may not be listed in disable_functions of php.ini file.
Login in from WHM Panel, go to MultiPHP Manager, click on System PHP-FPM Configuration Tab, then go to Disabled Functions. Now from here remove the exec.
After removing save it and restart PHP-FPM.
In case you do not have WHM Panel access you may not be able to use this function. So request your hosting provider for shell access and exec for your account only.
okay, it was a file permission issue. www-data had not the permission to write the file, after changing the permission it's working now.
Related
I'm using a shared server managed by Aruba.it
It uses
ffmpeg version 4.1
built with gcc 4.8.5
(GCC) 20150623
(Red Hat 4.8.5-36)
I need to use ffmpeg to fix the loudness of any uploaded MP3 to -12 dB LUFS -1 dB TP
I found on internet the following commands, but not any output.mp3 is generated
PHP
exec("/usr/bin/ffmpeg -i Temp.mp3 -af loudnorm=I=-12:LRA=7:tp=-2:measured_I=-30:measured_LRA=1.1:measured_tp=-11 04:measured_thresh=-40.21:offset=-0.47 -y output.mp3");
Where do I wrong please?
EDIT
Not any error is returned.
If you run it manually, unscripted in your terminal you will get an error:
Unable to find a suitable output format for '04:measured_thresh=-40.21:offset=-0.47'
04:measured_thresh=-40.21:offset=-0.47: Invalid argument
There is an errant space in your command, so change measured_tp=-11 04 to measured_tp=-11.04.
Based on the suggestion by #llogan I found the solution.
It should be in 3 steps and not in only one.
The MP3 to MP3 doesn't work, on my case.
So I converted the input MP3 into wave, normalized it, and converted back to MP3
exec("/usr/bin/ffmpeg -i Temp.mp3 Temp.wav");
exec("/usr/bin/ffmpeg -i Temp.wav -af loudnorm=I=-12:LRA=7:tp=-2:measured_I=-30:measured_LRA=1.1:measured_tp=-11.04:measured_thresh=-40.21:offset=-0.47 output.wav");
exec("/usr/bin/ffmpeg -i output.wav -ab 320k output.mp3");
this solution worked perfectly
With shell_exe or exec use:
$output = shell_exec('/usr/bin/ffmpeg -i Temp.mp3 -af loudnorm=I=-12:LRA=7:tp=-2:measured_I=-30:measured_LRA=1.1:measured_tp=-11 04:measured_thresh=-40.21:offset=-0.47 -y output.mp 2>&1');
2 refers to the second file descriptor of the process, i.e. stderr.
> means redirection.
&1 means the target of the redirection should be the same location as the first file descriptor, i.e. stdout.
In $output you will have the response. If the apache user www-data doesn't have enough rights execute the comand as sudo
I have installed Lampp-x64-5.6.3 in my OpenSuse 13.2 OS. I have built a program which require the execution of qpdf which I have installed from the OpenSuse Repo itself.
Well when I run the commands (given below) I get no response & nothing works at all whereas I am able to execute other binary files within the /usr/bin/ directory.
$execQuery = "/usr/bin/qpdf --decrypt --stream-data=uncompress --force-version=1.4 ".escapeshellarg('/opt/lampp/htdocs/test/test.pdf')." ". escapeshellarg('/opt/lampp/htdocs/test/temptest.pdf');
shell_exec($execQuery);
#OR
$execQuery = "/usr/bin/qpdf '/opt/lampp/htdocs/test/test.pdf' '/opt/lampp/htdocs/test/temptest.pdf'";
shell_exec($execQuery);
PHP safe_mode is off, shell_exec, exec, system etc are enabled. Still I am unable to run this particular binary (/usr/bin/qpdf).
I am getting output when I run the commands echo or ls -l or dir or even skype in the php shell_execute functions.
The permission for the file is: -rwxr-xr-x 1 root root 85248 Jun 18 10:31 /usr/bin/qpdf
But however I am able to execute qpdf command via Terminal of the OS. and it creates the file perfectly.
The directory /opt/lampp/htdocs/test/ is writable by both qpdf and apache/lampp
I have tried almost all methods mentioned in various forums but still can't get this executable run the file.
Thanks in advance.
UPDATE:
As suggested tried out this one:
$command = "/usr/bin/qpdf --decrypt --stream-data=uncompress --force-version=1.4 ".escapeshellarg('/opt/lampp/htdocs/test/test.pdf')." ". escapeshellarg('/opt/lampp/htdocs/test/temptest.pdf');
shell_exec($command. " > /opt/lampp/htdocs/debug.log 2>&1");
The errors are logged!
......
/opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.9' not found
......
SOLUTION:
I simply had to delete the /usr/lib/libstdc++.so.6 file or rename it.
RUN in terminals:
sudo mv /usr/lib/libstdc++.so.6 /usr/lib/libstdc++.so.6___
I simply had to delete the /usr/lib/libstdc++.so.6 file or rename it.
RUN in terminals:
sudo mv /usr/lib/libstdc++.so.6 /usr/lib/libstdc++.so.6___
I have used php's exec to execute FFmpeg command but its not woking when I open it in browser. But when i run this php file script in terminal it works fine.And my php safe mode is off. please help me to get it solved. my php code is
<?php
$output=exec("ffmpeg -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi", $out);
echo $out;
echo $output;
?>
try giving full path where the ffmpeg application is located.
e.g.
/usr/bin/ffmpeg
So your function might look like:
$output=exec("/usr/bin/ffmpeg -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi", $out);
You must check what is the location of "ffmpeg".
I had this problem and it turned out it was Apache's permission on the public directory.
Note: I am running Ubuntu 14 on AWS
After installing FFmpeg I had to change the /var/www/* ownership to www-data.
sudo chown -R www-data:root /var/www
(the www-data is the important part here)
Then I had the following code running, and it works when I access it via URL (Apache)
// test.php
$run = system("/opt/ffmpeg/bin/ffmpeg -i /var/www/html/input.mp4 -vf scale=640:480 /var/www/html/output.mp4 &");
if($run) {
echo "success";
} else {
echo "failed";
}
The /opt/ffmpeg/bin/ffmpeg is where my FFmpeg is running from. Yours might be /usr/bin/ffmpeg or something else. You can locate it by typing locate ffmpeg in the command line and looking through the list it gives you.
The input file was a public .mp4 file and the output.mp4 file was going to the same location.
Run this in your command line: php test.php - works
Run this from your browser: yourwebsite.com/test.php - works
Note that if you are on windows you must use COMMAS. I.E:
$output=exec('"/usr/bin/ffmpeg" -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi', $out);
Like #Arfeen mentioned in his answer, you should execute the command with the path of ffmpeg, but, the given path in the answer "/usr/bin/ffmpeg" is not always the same.
First locate your ffmpeg by using the command :
which ffmpeg
The result in my case is :
/usr/local/bin/ffmpeg
Then go back to your php code and replace "ffmpeg" in the command by the path of ffmpeg (which is /usr/local/bin/ffmpeg in my case).
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>";
?>
I'm attempting to run the following command in PHP (on Ubuntu):
<?php
if (exec("/home/johnboy/ffmpeg/ffmpeg -i test1.mp4 -acodec aac -ab 128kb -vcodec mpeg4 -b 1220kb -mbd 1 -s 320x180 final_video.mov"))
{ echo "Success"; }
else { echo "No good"; }
And I always get "No good" echoed back, and no file created.
Interestingly, if I run the same exact command in Shell, it works, no problems.
Also, when I run the same code above, but subsitute "whoami" instead of the ffmpeg stuff, it works. (It echoes back "Success")
Any ideas on why this wouldn't be working? Thanks.
get the stderr will give the result
try
ffmpeg -i inputfile [more_params] 2>&1
Can the apache/web user reach /home/johnboy/ffmpeg/ffmpeg? That is, perhaps /home/johnboy is 0700 instead of 0755?
Perhaps there are resource limits affecting loading of such a large program and all its libraries?
If you run the script to the php cli sapi, does it behave correctly? Even when running as the apache user?
What does strace -ff show when the apache web user runs the php script through the php cli?
Use this
$ffmpeg = "ffmpeg Installed path";
$flvfile = "source video file with root path";
$png_path " "Destination video file with root path and file type";
exec("$ffmpeg -y -i $flvfile -vframes 1 -ss 00:01:60
-an -vcodec png -f rawvideo -s 110x90 $png_path");
You are using relative paths to the file names. Are you sure you are executing the command in the right directory?
There would be some issues if you want to execute something that way.
1. Maybe you have some permission issue, the webserver have limitation to handle some execution to the system.
2. Maybe your path file is incorrect.
3. you can try to use shell_exec to perform the execution to the system.
Anyway, what I would do to make my execution would go smoothly are,
I will write 2 programs with message passing included between them, for example client server program.
The server would wait some messages from the client to execute some command (all of the command, it would be no permission issues). All you have to do in you web is to call your client application.
What I emphasize is to build some interface from web and the system. It would solve a lot of problem with permission issues.
hope this help.
The function exec() only returns the last line of output, I suspect that the last line of that command is blank. if you want the entire contents of the command you should use shell_exec().
Also keep track of where the command is executing, try: print(shell_exec("pwd"));
Enable safemode and it will work
$output = shell_exec('/home/person/www/ffmpeg 2>&1');
echo "<pre>$output</pre>";
Notice the 2>&1 part ...