Executing ffmpeg commands from a PHP script - php

My problem is pretty simple: I can run ffmpeg commands perfectly fine on my server from the command line, but some of these commands experience trouble when I try to execute them from a PHP script.
For example, the following works in the command line:
ffmpeg -i cat.mpeg cat.avi
When in my PHP script, it also works as:
exec("/usr/local/bin/ffmpeg -i cat.mpeg cat.avi", $output);
This, as I said, works fine. However, this line works from the command line, but not in a PHP script:
ffmpeg -i cat.mpeg -vf scale=480:360 cat2.mpeg
Trying to put that into an exec() produces nothing. I've tried with/without quotes around the dimensions, different formats, etc. From the dozens of different commands I have tried, it seems that any will work from PHP as long as they don't contain the -vf flag. Clearly it works on the server, as executing from the command line proceeds with no issue; is there something silly I am missing here?

I believe it' not about the -vf flag, but about its parameters scale=480:360. Try escapeshellcmd() for escaping the command prior to executing it.

Related

Issues with Line Breaks when trying to Sever and execute a PHP generated bash script

Trying to Execute a bash script I'm serving on the fly using:
wget -O- "https://endpoint.com/my/script/" | bash
When download the script and run the following to clean up the line breaks first it works fine:
sed -i -e 's/\r$//' ~/myscript.sh
If I try to run it directly from wget the linebreak format is an issue. I've tried this and a few reg_ex functions off of here, but I can't seem to find anything that works to do it in php itself before serving the file
echo str_replace("\r\n", "\r", $bash);
I would really like to avoid actually saving the physical file if it's possible to avoid it, but haven't managed yet.

ffmpeg via shell_exec not working

I'm running the following command in the console without any problems:
ffmpeg -i /var/www/html/input.mp4 /var/www/html/output.mp3
But when I run the command in PHP, I get nothing:
<?php
shell_exec("ffmpeg -i /var/www/html/input.mp4 /var/www/html/output.mp3");
?>
Any idea what the problem is here? I've checked my CHMOD permissions, everything looks good. Anything else to check?
Thank you,
I would try running it with full path to ffmpeg. You can get the full path on unix by running which ffmpeg in the command line. My ubuntu box produces output like /usr/bin/ffmpeg. Use that full path in code:
<?php
shell_exec("/usr/bin/ffmpeg -i /var/www/html/input.mp4 /var/www/html/output.mp3");
?>
For sure (as written above) passing full path is to exec is a good idea. Also you should also redirect the error stream to the output you gather - add the " 2>&1" to the end of the command. Take a look at the first comment here.

php exec not returning full output

I am executing a bunch of ssh commands with php's exec. All of them seems to work correctly except one of them:
$command = 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no user#127.0.0.1 "verify /md5 filename"';
exec($command, $output);
These commands are being executed on a Cisco router if that makes any difference.
The output is about 99% completed however it gets cut off at the end. I don't receive the last line at all and the second to last line always gets cut off after the 1st character. I also tried passtru and got the same results.
I've also tried adding 2>&1 to the end of the command and it made no difference. Anyone know what is going on here or have any ideas?
Here are the last 2 lines. This is what I expect:
..........Done!
verify /md5 (flash:filename) = 8743b52063cd84097a65d1633f5c74f5
I get
.
Also, running this on command line works perfectly. I have seen others with similar issues but there wasn't really any solution. This is on php 5.3.3.
I have a little more information to add to this. I created a separate script that only called the exec and this command and got the same output. This rules out any thing else in my file or the framework I'm using.
I also ran the new script from command line and it worked perfectly. Running the script in the browser however returns the bad results.
I can verify that it has something to do with how the data is being returned and those dots. The ...... must load in a way that PHP doesn't like and it thinks the command is done.

exec command works in terminal not with PHP

I'm writing a class who let me access to recutils through PHP.
I have a 'database' file called books.rec in ../database/ and a script who runs my Recutils.php class.
My class simply launch system application with correct parameters.
But When I try to use recins with PHP's exec function, the command doesn't work will it work in command line.
This is the command that is executed by my script :
recins -f Title -v "Moi" -f Author -v "Moche" -f Location -v "loaned" -t Books ../database/books.rec
With PHP : Nothing, the record is not inserted (no error message at all too).
In terminal : OK, the command is well done and my record is inserted.
I also have a method to do a select operation using recsel and it works very well, will it use exactly the same file (and runs from exec too).
So, could someone explain me why the command don't work will another with the same file work ?
Thanks
PS : Further informations : http://www.gnu.org/software/recutils/
I would double check that you are running the command as the same user from the command line and your php script. That may be the problem. exec('whoami')
You said you had a script that starts your php script it should be the same user as that.
You might also want to running a simpler exec command to see if that will work first.
Other things to try:
Try checking stderr output exec('ls /tmp 2>&1', $out); This will redirect standard error to standard out so you get both.
Try using php's shell_exec() which will invoke a shell just like when you are running from the command line(eg. bash). shell_exec('ls /tmp 2>&1 >> /tmp/log') should even put all output into a log file.
I don't think this will help you but it is something to try if all else fails, set it as a background process and see if it completes. exec('nohup php process.php > process.out 2> process.err < /dev/null &'). The & will set the command to run in the background and let the script continue.
Good Luck
Is recins command accessible for PHP ? Also is path to books.rec correct ?
Try with absolute path.

Inconsistent Video Conversion

I have a script calling a command to run an ffmpeg conversion on an uploaded video. It works only at random times however. Sometimes the form will finish submitting and the ffmpeg process will be running; at other times, the ffmpeg command fails to run at all. Here is the command that I'm running in an exec() function:
ffmpeg -i "uploaded_file -b 450k "converted_file" >/dev/null 2>&1 &
Can anyone explain why this will only work on certain tries and not on others?
What if ffmpeg fails and throws and error? Right now you're sending all output to /dev/null so you'll never know.
Change >/dev/null into >>/tmp/ffmpeglog to keep a log

Categories