[英]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 ). 我已经在Centos 6机器( https://trac.ffmpeg.org/wiki/CompilationGuide/Centos )的根目录上成功安装了FFMpeg。
My workplace of apache/php is /var/www/html 我的apache / php工作场所是/ var / www / html
Now I'm running below command successfully on /var/www/html directory to capture frame from the video file. 现在,我在/ var / www / html目录中成功在命令下面运行,以从视频文件捕获帧。 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. 我想通过php并使用shell_exec()或exec()php函数运行此命令。 My php code for running the command is: 我的运行命令的php代码是:
$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: 当我尝试通过上述php代码运行命令时,出现以下错误:
sh: ffmpeg: command not found
Please help me to solve out this problem. 请帮我解决这个问题。
For Video Thumbnail creation we use ffmpeg. 对于创建视频缩略图,我们使用ffmpeg。
In Linux Systems(centos 6.x) Ffmpeg installation process and Php example:- 在Linux Systems(centos 6.x)Ffmpeg安装过程和Php示例中:-
This process is done in my centos 6 and created thumbnails using php. 这个过程在我的centos 6中完成,并使用php创建了缩略图。
Step1:- Please check If any existing ffmpeg available or not in your linux server .If available please remove that files. 步骤1:-请检查Linux服务器中是否有可用的ffmpeg。如果可用,请删除该文件。
Step2:- For New ffmpeg installation follow this link 步骤2:-对于新的ffmpeg安装,请点击此链接
http://root.uabid.com/compile-ffmpeg-on-centos-6-x/ . http://root.uabid.com/compile-ffmpeg-on-centos-6-x/ 。
Step3:- After installation complete check whereis your ffmpeg installed. 步骤3:-安装完成后,检查ffmpeg的安装位置。
Command: whereis ffmpeg 命令:whereis ffmpeg
type this command in linux command line and check.If for example your ffmpeg path in (/usr/local/bin/ffmpeg). 在linux命令行中键入此命令并检查。例如,您的ffmpeg路径是否位于(/ usr / local / bin / ffmpeg)中。 Use this path in your code. 在代码中使用此路径。
Php Example:- php范例:-
<?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: 流程2:-这适用于静态文件路径:
<?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. 尝试使用which
来定位ffmpeg二进制文件。 At least on Ubuntu/Debian based Linuxes the which
command should be available by default. 至少在基于Ubuntu / Debian的Linux上which
默认情况下应该使用which
命令。 which
returns the file path to the preferred executable of any available application. which
文件路径返回到任何可用应用程序的首选可执行文件。 Eg which php
should return the PHP installation executable that is used by default for CLI operations. 例如which php
应该返回默认用于CLI操作的PHP安装可执行文件。 Therefore which ffmpeg
should return the system default location for the ffmpeg installation. 因此which ffmpeg
应该返回ffmpeg安装的系统默认位置。
<?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 (eg $ chmod +x ffmpeg
). 还要确保运行PHP脚本的Linux用户可以访问ffmpeg(例如$ 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. 如果$ sudo which ffmpeg
返回路径, $ which ffmpeg
路径不返回,请尝试为所需用户重新安装ffmpeg或尝试使用权限。
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). 编辑:尽管要小心which
但不知道破解程序是否可以用恶意代码替换ffmpeg可执行文件(这导致上述PHP脚本运行错误的可执行文件)。
I've seen this problem. 我已经看到了这个问题。 For me 'command not found' may be the $PATH
variable problem. 对我来说,“找不到命令”可能是$PATH
变量问题。
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. 当通过php并使用shell_exec()
或exec()
php函数运行此命令时,与Linux目录中的running命令相比,可以使用不同的$PATH
变量。
For example, the /usr/local/php/etc/vhostphp-fpm.conf
for my php project is below: 例如,我的php项目的/usr/local/php/etc/vhostphp-fpm.conf
如下:
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. 但是ffmpeg
命令位于/data/bin
目录中。 My Linux's $PATH
variable contain the /data/bin
. 我的Linux的$PATH
变量包含/data/bin
。
So add /data/bin
to env[PATH]
variable above can solve the problem. 因此,在上面的env[PATH]
变量中添加/data/bin
可以解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.