FFMPEG works from command line but not PHP - 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"))

Related

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

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

ffmpeg is working in terminal but not in php

I have installed ffmpeg using Ubuntu,
sudo apt-get install ffmpeg
ffmpeg is installed on root "/usr/bin/ffmpeg".
It's working fine in the terminal, but when I execute that using php i don't get any output.
shell_exec(/usr/bin/ffmpeg -y -i ./upload/1535531595first.mp3 -i ./upload/1535531595second.mp3 -filter_complex "[0:0][1:0] amix=inputs=2:duration=longest" -c:a libmp3lame ./download/1535531595outputnow1.mp3)
and using this command in "opt/lampp/htdocs/project-Name/sub-directory/test.php" file.
Any help will be appreciatable. Thanks in Advance. find the attachment cmd terminal working fine but for php not working
It works everywhere perfectly! I'm using FFMPEG with my PHP project too, The most common problems are:
Not using double quotation mark (").
Wrong addressing.
First of all, check your path, It's better to address your file from the root directory like this /user/foo/bar.mp3 and do the same thing for your output.
$input_file = "/user/foo/bar.mp3";
$output_file = "/user/foo/out.mp3";
shell_exec("ffmpeg -i \"{$input_file}\" \"{$output_file}\");
By the way, There is an issue with the web applications When you are using FFMPEG in your PHP project you may get the timeout error because it takes too much time to process.
I'd suggest you use this following script, It executes command-line commands in the background process
function Execute($CMD)
{
$OS = strtoupper(PHP_OS_FAMILY);
if ($OS == 'WINDOWS') {
return pclose(popen("start /B {$CMD}", "r"));
} else {
return shell_exec("{$CMD} > /dev/null 2>/dev/null &");
}
}

sh: ffmpeg: command not found when run command through php

I have installed successfully installed FFMpeg on root of my Centos 6 machine (https://trac.ffmpeg.org/wiki/CompilationGuide/Centos).
My workplace of apache/php is /var/www/html
Now I'm running below command successfully on /var/www/html directory to capture frame from the video file. It's capturing a frame.
[root#localhost html]# ffmpeg -i video.mpg -an -ss 30 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg -s 160x100 frame8 2>&1
I want to run this command through php and using shell_exec() or exec() php functions. My php code for running the command is:
$cmd = "/root/bin/ffmpeg -i /project/app/webroot/videos/video.mpg -ss 00:00:14.435 -f image2 -vframes 1 /project/app/webroot/videothumbnails/example-thumb.jpg";
$locale = 'en_IN.UTF-8';
setlocale(LC_ALL, $locale);
putenv('LC_ALL='.$locale);
echo shell_exec($cmd);
When I'm trying to run command through above php code, I'm getting below error:
sh: ffmpeg: command not found
Please help me to solve out this problem.
For Video Thumbnail creation we use ffmpeg.
In Linux Systems(centos 6.x) Ffmpeg installation process and Php example:-
This process is done in my centos 6 and created thumbnails using php.
Step1:- Please check If any existing ffmpeg available or not in your linux server .If available please remove that files.
Step2:- For New ffmpeg installation follow this link
http://root.uabid.com/compile-ffmpeg-on-centos-6-x/ .
Step3:- After installation complete check whereis your ffmpeg installed.
Command: whereis ffmpeg
type this command in linux command line and check.If for example your ffmpeg path in (/usr/local/bin/ffmpeg).
Use this path in your code.
Php Example:-
<?php
if($extension === 'mp4' OR $extension == 'MP4' )
{
$video = $timestamp.$imagename;
$videoname=substr($imagename,0, -4).$timestamp;
$image = "sites/default/files/content_images/{$videoname}-thumb.jpg";
var_dump($video);
$cmd = "/usr/local/bin/ffmpeg -i /opt/lampp/htdocs/myproject/sites/default/files/content_videos/".$video." -ss 00:00:01.435 -f image2 -vframes 1 /opt/lampp/htdocs/myproject/sites/default/files/content_images/".$videoname."- thumb.jpg";
$cmdstr = $cmd;
$locale = 'en_IN.UTF-8';
setlocale(LC_ALL, $locale);
putenv('LC_ALL='.$locale);
echo exec($cmd);
?>
Process 2:-
This is for static file paths:
<?php
$cmd = "/root/bin/ffmpeg -i /var/www/html/project/app/webroot/videos/example.mp4 -ss 00:00:01.435 -f image2 -vframes 1 /var/www/html/project/app/webroot/videothumbnails/example-thumb.jpg";
$cmdstr = $cmd;
$locale = 'en_IN.UTF-8';
setlocale(LC_ALL, $locale);
putenv('LC_ALL='.$locale);
echo exec($cmd);
?>
Hope this helps to you & others..
Try using which to locate the ffmpeg binary. At least on Ubuntu/Debian based Linuxes the which command should be available by default. which returns the file path to the preferred executable of any available application. E.g. which php should return the PHP installation executable that is used by default for CLI operations. Therefore which ffmpeg should return the system default location for the ffmpeg installation.
<?php
// shell_exec returns the command output or null on error/empty output.
$ffmpeg_path = shell_exec('which ffmpeg');
if ($ffmpeg_path != null) {
shell_exec($ffmpeg_path . ' -i file.mpg --snip--');
}
Also make sure that the Linux user running the PHP script has access to ffmpeg (e.g. $ chmod +x ffmpeg). If $ sudo which ffmpeg returns a path but $ which ffmpeg doesn't, try reinstalling ffmpeg for the wanted user or play around with the permissions.
EDIT: use care with which though, there's no knowing whether a cracker could replace the ffmpeg executable with something malicious (which leads to the above PHP script running the wrong executable).
I've seen this problem. For me 'command not found' may be the $PATH variable problem.
When run this command through php and using shell_exec() or exec() php functions, you may use different $PATH variable compare with running command in linux directory.
For example, the /usr/local/php/etc/vhostphp-fpm.conf for my php project is below:
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
env[LD_LIBRARY_PATH] = /usr/local/lib
but ffmpeg command locates in /data/bin directory. My Linux's $PATH variable contain the /data/bin.
So add /data/bin to env[PATH] variable above can solve the problem.

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