PHP ffmpeg exec returns null - php

I'm trying to run ffmpeg through a PHP exec call, I've been debugging for a while and looked at lot of responses on here, but still not found any answers...
My simplified call is:
$cmd = 'ffmpeg 2>&1';
exec(escapeshellcmd($cmd), $stdout, $stderr);
var_dump($stderr);
var_dump($stdout);
var_dump($cmd);
exit;
My output is $stderr = int(1) and $stdout = array(0) { }
Also I tried shell_exec($cmd) which returns NULL.
cmd.exe has permissions set for the IUSR account - e.g. I can run $cmd = 'dir' and see a directory listing output.
PHP is not running in safe mode.
The ffmpeg.exe is in the same directory as my php file, but I have the same response giving an absolute path to the ffmpeg.exe file in $cmd.
ffmpeg is executing fine from the command line.
I'm running Windows XP, IIS and PHP 5.3.
EDIT:
If I run 'ffmpeg -h' I get the help commands, which must indicated that ffmpeg is recognised
I've increased the PHP memory limit to 1024 - no luck.

I've now got this working - I think there may have been several issues:
It turns out that $cmd = 'ffmpeg' returns null, so it's not a good test!
Also running escape shell command on '2>&1' echoes 2^>^&1" - I think this was my original problem.
I've now managed to rea test file using: 'ffmpeg -i SAMPLE.AVI 2>&1'.
Working code:
$cmd = 'ffmpeg -i SAMPLE.AVI 2>&1';
exec($cmd, $output, $value);
var_dump($output);
var_dump($value);
var_dump($cmd);
exit;
As noted above ffmpeg is a bit of a memory hog, so it's worth checking memory too.

Related

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

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!

php and ffmpeg from command line in windows - not working, nothing returns

I'm trying to execute ffmpeg within a website and it never output's a file, nothing return's, I have tried it with parameters -version and that works fine.
I can copy and paste the main command into dos and it also works fine.
I'm stuck, I have tried many things including shell_exec but to no avail, nothing comes back other than array(0) { } int(0). help?
//$cmd = 'C:\ffmpeg.exe ffmpeg -version';
$cmd = "C:\ffmpeg.exe -i C:\1.mp3 -i C:\2.mp3 -filter_complex amerge -c:a libmp3lame -q:a 4 C:\out.mp3";
echo exec($cmd, $o, $v);
var_dump($o);
var_dump($v);
got it working, a couple of things.
set IUSR permissions on ffmpeg.exe all input filename and output filename folders to full control.
added double back slash to all filenames. e.g. C:\\out.mp3
now works.

Problems with executing FFMPEG through PHP

Have been trying to execute FFMPEG using a script I have uploaded to my domain
<?php
$output = array();
$result = -1;
exec('../../../../../../usr/bin/ffmpeg -ab 320k -i source.wav dest320.mp3', $output, $result);
var_dump($output, $result);
?>
The example code says the program should not be returning -1 unless there is an error but I have pointed to the exact path that FFMPEG is stored in.....
When I call 'ffmpeg -ab 320k -i source.wav dest320.mp3' from CentOS it works...
Am lost and have spent hte last few hours trying to work it out.
Thanks
CP
Any time you have an exec that doesn't work in php, you should switch to passthru for debugging.
passthru('../../../../../../usr/bin/ffmpeg -ab 320k -i source.wav dest320.mp3 1 2>&1');
Appending 1 2>&1 to the end, pipes your stderr to stout, and returns any errors you have while running your exec.
It is probably safer to use passthru('/usr/bin/env ffmpeg -ab 320k -i source.wav dest320.mp3 1 2>&1'); which will get the correct path for ffmpeg without the need to specify and absolute/relative path.
Also worth noting that if you are using Ubuntu since 12.04, the ffmpeg command has been renamed to avconv.

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

PHP exec() Not Working With ffmpeg

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

Categories