最近在做视频模块,要求获取视频的相关信息,比如视频的时长,分辨率的长宽,还有就是视频帧数还有截图等,通过查找资料,最佳的方案就是安装ffmpeg-php这个PHP扩展能提供这样的功能接口。
资料参考: http://wiki.mediatemple.net/w/(dv):Installing_FFmpeg_and_FFmpeg-php http://www.mysql-apache-php.com/ffmpeg-install.htm #svn源地址 https://ffmpeg-php.svn.sourceforge.net/svnroot/ffmpeg-php #可使用软件版本:ffmpeg-php-0.6.1 分支:/branches/ffmpeg-php/ffmpeg-php-0.6.1 #最新版本:ffmpeg-php-0.7.1 trunk/ffmpeg-php 编译报错 不可用
-
安装顺序:
1.ffmpeg 2.ffmpeg-devel 3.ffmpeg-php
-
安装ffmpeg ffmpeg-devel
操作系统:CentOS 6.2 minimal
由于默认的CentOS的软件仓库里没有FFmpeg、FFmpeg-PHP,所以安装第三方的软件仓库。vim /etc/yum.repos.d/dag.repo #输入: [dag] name=Dag RPM Repository for Red Hat Enterprise Linux baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag gpgcheck=0 enabled=1 #gpgcheck 默认为0即可!设置gpgcheck=1会让yum检查每个下载的RPM的GnuPG签名。这么做是因为你需要适当的GnuPG key注册到您的RPM数据库。可以防止被欺骗. #安装 Install ffmpeg 等模块 yum -y install ffmpeg ffmpeg-devel
#装好后通过命令查看是否成功 ffmpeg -formats FFmpeg version 0.6.5, Copyright (c) 2000-2010 the FFmpeg developers built on Jan 29 2012 17:52:15 with gcc 4.4.5 20110214 (Red Hat 4.4.5-6) configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --mandir=/usr/share/man --incdir=/usr/include --disable-avisynth --extra-cflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --enable-avfilter --enable-avfilter-lavf --enable-libdc1394 --enable-libdirac --enable-libfaac --enable-libfaad --enable-libfaadbin --enable-libgsm --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libx264 --enable-gpl --enable-nonfree --enable-postproc --enable-pthreads --enable-shared --enable-swscale --enable-vdpau --enable-version3 --enable-x11grab libavutil 50.15. 1 / 50.15. 1 libavcodec 52.72. 2 / 52.72. 2 libavformat 52.64. 2 / 52.64. 2 libavdevice 52. 2. 0 / 52. 2. 0 libavfilter 1.19. 0 / 1.19. 0 libswscale 0.11. 0 / 0.11. 0 libpostproc 51. 2. 0 / 51. 2. 0
-
-
安装ffmpeg-php
cd ffmpeg-php-0.6.1 /usr/local/php/bin/phpize #php路径一定要对 ./configure --with-php-config=/usr/local/php/bin/php-config --with-ffmpeg=/usr make && make install #make的时候报错见下面错误
报错:主要是“PIX_FMT_RGBA32”的错误
/usr/local/src/ffmpeg-php-0.6.1/ffmpeg_frame.c: In function ‘zim_ffmpeg_frame_toGDImage’: /usr/local/src/ffmpeg-php-0.6.1/ffmpeg_frame.c:336: error: ‘PIX_FMT_RGBA32’ undeclared (first use in this function) /usr/local/src/ffmpeg-php-0.6.1/ffmpeg_frame.c:336: error: (Each undeclared identifier is reported only once /usr/local/src/ffmpeg-php-0.6.1/ffmpeg_frame.c:336: error: for each function it appears in.) /usr/local/src/ffmpeg-php-0.6.1/ffmpeg_frame.c: In function ‘zim_ffmpeg_frame_ffmpeg_frame’: /usr/local/src/ffmpeg-php-0.6.1/ffmpeg_frame.c:421: error: ‘PIX_FMT_RGBA32’ undeclared (first use in this function)
解决错误:
cd ffmpeg-php-0.6.1 vi ffmpeg_frame.c :%s/PIX_FMT_RGBA32/PIX_FMT_RGB32 #替换错误字段 :wq
排除错误,继续安装
/usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-ffmpeg=/usr make && make install #成功返回 # make install Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ #cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/;ls ffmpeg.so #ffmpeg.so文件存在,说明编译成功
-
php.ini 中增加ffmpeg扩展
vim /usr/local/php/etc/php.ini 手工修改查找中的extension_dir = "./"; 修改为 extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"; 并在此行后增加以下几行,然后保存: #开启ffmpeg扩展 extension = "ffmpeg.so"; #实例: ; Directory in which the loadable extensions (modules) reside. ; http://php.net/extension-dir ; extension_dir = "./" ; On windows: ; extension_dir = "ext" extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/" extension = "ffmpeg.so"
查看是否正常
# /usr/local/php/bin/php -v PHP 5.3.15 (cli) (built: Aug 10 2012 23:14:01) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
-
安装ffmpeg-php
-
php调用实例:
参考:/branches/ffmpeg-php/ffmpeg-php-0.6.1/test_ffmpeg.php$moviepath = './test.flv'; $mov = new ffmpeg_movie($movie); printf("file name = %s\n", $mov->getFileName()); printf("duration = %s seconds\n", $mov->getDuration()); printf("frame count = %s\n", $mov->getFrameCount()); printf("frame rate = %0.3f fps\n", $mov->getFrameRate()); printf("comment = %s\n", $mov->getComment()); printf("title = %s\n", $mov->getTitle()); printf("author = %s\n", $mov->getAuthor()); printf("copyright = %s\n", $mov->getCopyright()); printf("get bit rate = %d\n", $mov->getBitRate()); printf("has audio = %s\n", $mov->hasAudio() == 0 ? 'No' : 'Yes'); if ($mov->hasAudio()) { printf("get audio stream id= %s\n", $mov->getAudioStreamId()); printf("get audio codec = %s\n", $mov->getAudioCodec()); printf("get audio bit rate = %d\n", $mov->getAudioBitRate()); printf("get audio sample rate = %d \n", $mov->getAudioSampleRate()); printf("get audio channels = %s\n", $mov->getAudioChannels()); } printf("has video = %s\n", $mov->hasVideo() == 0 ? 'No' : 'Yes'); if ($mov->hasVideo()) { printf("frame height = %d pixels\n", $mov->getFrameHeight()); printf("frame width = %d pixels\n", $mov->getFrameWidth()); printf("get video stream id= %s\n", $mov->getVideoStreamId()); printf("get video codec = %s\n", $mov->getVideoCodec()); printf("get video bit rate = %d\n", $mov->getVideoBitRate()); printf("get pixel format = %s\n", $mov->getPixelFormat()); printf("get pixel aspect ratio = %s\n", $mov->getPixelAspectRatio()); $frame = $mov->getFrame(10); printf("get frame = %s\n", is_object($frame) ? 'true' : 'false'); printf(" get frame number = %d\n", $mov->getFrameNumber()); printf(" get frame width = %d\n", $frame->getWidth()); printf(" get frame height = %d\n", $frame->getHeight()); }
- 思考:通过安装ffmpeg-php扩展的方式获取信息虽然比较方便,但是扩展需要人去维护,一旦不维护了,扩展就无法使用。就像现在的情况 只能使用0.6.1的扩展版本对应FFmpeg version 0.6.5版本。如果想不受版本制约,请看php执行系统应用程序ffmpeg来获取对视频信息操作