No response from FFMPEG from inside php (works in command line) - php

So I have this executable working fine in a Windows 10 dev environment:
$cmd = 'C:/ffmpeg/bin/ffmpeg.exe -i video.mp4 -i audio.mp3 -c:v copy -c:a aac output.mp4';
exec($cmd, $output)
However when I move it into the Ubuntu staging environment and change the path to:
/usr/bin/ffmpeg
there is no response from it.
The following works in the ubuntu terminal
ffmpeg -v, ffmpeg -i, php -v, ...
but there is no response from it from inside php.
Am I missing something here?
I've also tried adding sudo to the above command which seems to make no difference.
(Yes I know I should be developing in the same environment as the staging/prod servers but it's not an option in this specific case).

Related

python script running on terminal but not on local server

Absolute path is given instead of relative one
php function to call the python script:
$py_script = escapeshellcmd("/home/manish/anaconda2/bin/python /opt/lampp/htdocs/frontend/master/python_features/audioAnalysis.py featureExtractionFile -i /opt/lampp/htdocs/frontend/master/uploads/rihana.wav -mw 0.5 -ms 0.5 -sw 1 -ss 1 -o /opt/lampp/htdocs/frontend/master/output/rihana_op");
exec($py_script);
Error : Error: file not found or other I/O error. (DECODING FAILED)
Same script runs on terminal and gives the desired output
manish#manish-Vostro-3446:$ /home/manish/anaconda2/bin/python /opt/lampp/htdocs/frontend/master/python_features/audioAnalysis.py featureExtractionFile -i /opt/lampp/htdocs/frontend/master/uploads/rihana.wav -mw 0.5 -ms 0.5 -sw 1 -ss 1 -o /opt/lampp/htdocs/frontend/master/output/rihana_op
Short-term CSV file: /opt/lampp/htdocs/frontend/master/output/rihana_op_st.csv saved
so my question is how can I execute same script via php instead of terminal
For others with this issue. I had the same thing but also a RunTimeWarning about not being able to find ffmpeg. Once I have installed the ffmpeg, then this error also went away.
for mac :
brew install ffmpeg
for linux:
sudo apt install ffmpeg

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

FFMPEG works from command line but not PHP

I have a strange situation that has just happend. FFMPEG is no longer executing from PHP but will from the command line.
Here is exactly what the command is:
ffmpeg -i ../../uploads/ee78d5deb564901626067cc0008456ed.mp3 -ab 96k -y ../../uploads/mp3/ee78d5deb564901626067cc0008456ed_6203688.mp3
How it is executed in the PHP script:
if(! exec("ffmpeg -i ".$target_path."".$hash_filename.".".$path_extension." -ab 96k -y ".$target_path."mp3/".$hash_filename."_".$session_ID.".mp3")){
echo 'ffmpeg failed';
}
This command did work but not longer does. I have recently updated plesk but I highly doubt that has affected this. The only thing that I think could affect it that I have recently done is have the file upload go to a subdomain. So the directory where the file is located and stored in the command is in a directory outside the document root. However, the move_uploaded_file function works and I have altered the open_basedir in PHP ini to the webspace root.
tail -f /var/log/apache2/error_log
and lets us know what you see there...Adjust for your platform...
this is for lamp (opensuse )
You can try the system() command. It will return you the response from server
system("ffmpeg -i ".$target_path."".$hash_filename.".".$path_extension." -ab 96k -y ".$target_path."mp3/".$hash_filename."_".$session_ID.".mp3")
if the ffmpeg is not supported with current version of the php it will return you the error.
OR
you can modify your command to get ffmpeg with proper path. In my case it works like below code
exec("/usr/local/bin/ffmpeg -i ".$target_path."".$hash_filename.".".$path_extension." -ab 96k -y ".$target_path."mp3/".$hash_filename."_".$session_ID.".mp3"))

php exec() works on some calls, not other similar ones

I have a php script that cuts up video. Here are three exec() commands - two execute properly while one does not:
Works:
sudo ffmpeg -i /home/vidserver/videos/$filename.mp4 -ss $ctime -t 00:00:06 -acodec copy -vcodec copy -y /var/vidcache/test$x.mp4
Works:
sudo ffmpeg -i /var/vidcache/test$x.mp4 -qscale:v 1
/var/vidcache/i$x.mpg
Does not work:
sudo ffmpeg -i
concat:"i0.mpg|i1.mpg"
-qscale:v 1 /var/vidcache/output.mpg
/var/vidcache has 777 privs and www-data is in the sudoers file with NOPASSWD (yes, I know - this is just for debug purposes before I lock down security).
When I run the last command from a php script from the command line by itself, it DOES work. (Running as www-data or root.) But when I try to put it in a function called from a web page, it does NOT work.
Any ideas?
This should fix the third exec:
sudo ffmpeg -i "concat:i0.mpg|i1.mpg" -qscale:v 1 /var/vidcache/output.mpg
Here is a good wiki page on how to concat media files in ffmpeg.
Actually, the answer was as stupid as "where does www-data know where to look for files?"
I was assuming a lot with the i0.mpg. It needs a fully qualified directory, obviously.
Anyway, changing the code to look like this worked:
sudo ffmpeg -i concat:"/var/vidcache/i0.mpg|/var/vidcache/i1.mpg"
-qscale:v 1 /var/vidcache/output.mpg

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