exec(ffmpeg) is not executing in php but will in terminal? - php

i have a website that allows users to upload videos and I have VPS server with ffmpeg installed, safe mode is off and have tested command in my terminal and it works however in php script when i try and run the command in exec() or even shell_exec() it does not execute. below is my code (changed some variables for privacy).
$video_name = $_FILES["post_vid"]["name"];
$Vurl = "/folder1/folder2/$vrand_file_name/$video_name";
$VnewName = $vrand_vid_name .".mp4";
$VurlNew = "/folder1/folder2/$vrand_file_name/$VnewName";
$convertold = "/home/user/directory/domain.com/$Vurl";
$convertNew = "/home/user/directory/domain.com/$VurlNew" ;
$ffmpegC = "/user/local/bin/ffmpeg";
exec($ffmpegC.' -y -i "'.$convertold.'" -f mp4 "'.$convertNew.'"');
I have the code just after a move_uploaded_file() which runs fine.
also checked and my php is using the same user as my terminal...
any insight to this would be fantastic, thanks in advance.

Please fix your code near exec as follows, and Could you try?
$command = $ffmpegC.' -y -i "'.$convertold.'" -f mp4 "'.$convertNew.'" 2>&1';
exec($command, $array);
var_dump($array);
exit;
By using 2>&1, stderror is entered to $array.
So you can see the cause.
Good luck!

Related

Running exec command php

I have just installed pdf2htmlEX without any issues. If I run the command on the server it works fine. So I know the library itself is doing what it is supposed to do. If I run the command in php via the exec function nothing happens. I assume exec is used in this instance??
In PHP
// doesn't work
$output = exec('pdf2htmlEX --zoom 1.3 pdf/test.pdf');
// doesn't work
$output = shell_exec('pdf2htmlEX --zoom 1.3 pdf/test.pdf');
If I run a general command in the exec function it works fine, so I know exec is enabled and working as expected.
// works fine
$exec = exec('pwd', $output);
print_r($output);
Direct on Command Line not in PHP
// works and generates the file as expected.
pdf2htmlEX --zoom 1.3 pdf/test.pdf
Any help would be greatly appreciated.
Command line reference for library
https://github.com/coolwanglu/pdf2htmlEX/wiki/Quick-Start
EDIT:
After some further digging, it could be that the php script runs as a different user to the command line. My question would then be how do I check/fix this?
You just have to add the output html path and file name in your command :
Most of pdf_to_something packages usage with exec() is package_name [options] <input-filename> <output-filename>
$path = 'path/to/your/folder/';
$command = 'pdf2htmlEX '.$path.'test.pdf '.$path.'test.html';
exec($command);
Try following and check if your file is created in working directory.
exec('pdf2htmlEX --zoom 1.3 pdf/test.pdf',$output);
pdf2htmlEX doesn't output anything.
$cmd= "pdf2htmlEX --zoom 1.3 test/test.pdf";
exec($cmd, $output);
Will create a test.html file in the current directory.
Are you expecting something else?
Note: i've tested this on ubuntu by installing pdf2htmlEX with
sudo apt-get install pdf2htmlex
To get the output,
$a = file_get_contents("test.html");
echo $a;

PHP + winexe -> loads forever

I'm trying to list running services on a windows server via php. Therefore I'm using shell_exec with winexe.
My script:
$cmd = "winexe --interactive=0 --user='***' --password='***' //192.168.***.** \"net start\"";
$output = shell_exec($cmd);
echo $output;
Unfortunately on execution the page loads forever with no result. The command works on the command-line (Debian).
Anyone an idea?
Thanks in advance.
Save $cmd with correct format into a new bash file. Set cmd value for call this file. Remember set execution perms to this file.
Check if your apache user has perms for exec winexe
===
Try to launch
cat </dev/null | winexe --interactive=0 --ostype=1 --user=...

How to get exec to run on IIS 6.0

I installed the software imagemagick on my server "Windows Server 2008".
In cmd it works fine, all commands work.
But when I try to do it in PHP:
$command = "convert fdfdf.jpeg dfdf2.jpeg";
exec($command, $output);
It does not work. I get a blank page and nothing happens,
I have given all the permissions I need to the following:
cmd.exe
whomi.exe
c:\ImageMagick. execute & read to iis_iusrs and everyone.
Safe Mode are off.
What else can I do to make it work?
Try this
$command = "convert fdfdf.jpeg dfdf2.jpeg 2>&1";
exec($command, $output);
var_dump($output);

PHP exec not executing command

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).

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

Categories