前段时间帮同学做一个项目,需要在windows下用到ffmpeg。ffmpeg是Linux下的很流行的一个开源解码器,在windows下编译,很自然的想到了使用mingw+msys来模拟Linux环境,于是开始进行配置,没想到遇到的问题还挺多的,在这里和大家一起探讨一下。
1.首先是mingw+msys环境的配置,现在mingw做的很好了,不用两个分开安装了,直接从sourceforge上下一个git的安装。
http://sourceforge.net/projects/mingw/files/ 下载 mingw-get-inst-20110313.exe 。
2. 安装需要勾选MSYS选项
3.如果需要用MSVC++编译程序,需要有.lib文件才能链接到FFmpeg的库,这些.lib文件可以使用微软的工具链中lib命令产生。在安装完成后修改msys.bat (<%mingw_path%>/msys/1.0/msys.bat),在文件的最前面添加
“call "D:/Program Files/Microsoft Visual Studio 10.0/VC/bin/vcvars32.bat"”
(根据vs版本和路径的不同,配置对应的路径)
4.播放MP4需要X264解码库支持,需要到videolan (http://www.videolan.org/developers/x264.html)下载源码进行编译。编译命令:
./configure --prefix=/mingw
make
make install
5.在编译ffmpeg 是出现“expected '')'' before ''pid''”错误,而libx264运行需要pthreads 支持,所以需要pthread的lib 支持,到fmpeg.arrozcru.org下载一个pthread动态库。(http://ffmpeg.arrozcru.org/autobuilds/extra/mingw32/pthreads-w32-CVS_10012009_0211-mingw32.tar.bz2) 。解压文件,将mingw文件夹下的目录分别放到mingw安装文件夹下对应得目录。
6.进入ffmpeg 目录,编译,configure 能够顺利完成,但是make 时报错,“implicit declaration of function ''strcasecmp''”,需要对mingw进行patch。下载strcasecmp.diff (http://fate.arrozcru.org/mingw32/patches/),然后patch:
patch –p2 strcasecmp.diff
7.生成ffmpeg 需要yasm(开源汇编器)支持,去http://www.tortall.net 下载对应的yasm(http://www.tortall.net/projects/yasm/releases/yasm-1.1.0-win32.exe ),重命名为yasm.exe 将其放到<%mingw_path%>/bin文件夹下。
8.这样配置完后可以生成ffmpeg.exe , ffprobe.exe,但是没有ffplay.exe,没有播放器。要生成ffplay.exe需要SDL的支持,在ffmpeg configure 时如果提示sdl support yes ,那么生成结果就有ffplay.exe。
9.SDL(Simple DirectMedia Layer)需要glib的支持, GLib是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库。我们到GTK+官网下载glib (http://www.gtk.org/download-windows.html) ,同时下载它的依赖项zlib(也在相同页面下载)。这样,我们再到sdl官网下载最新版本的SDL-devel-1.2.14-mingw32.tar.gz(http://www.libsdl.org/release/SDL-devel-1.2.14-mingw32.tar.gz),解压后按照目录将文件分别放进对应的mingw文件夹中。
10.终于,可以开始编译ffmpeg了,先下载ffmpeg,首先得先安装git(http://www.kernel.org/pub/software/scm/git/),Git 是用于 Linux 内核开发的版本控制工具,它采用了分布式版本库的方式,不必服务器端软件支持。找到ffmpeg官网,进入download页面,选择git://github.com/lu-zero/ffmpeg.git 源下载。当然,首先得新建一个文件夹来存放下载的源代码:
git clone git://github.com/lu-zero/ffmpeg.git
注意:截图可能有点问题,ffmpeg.org的源可能有点老了,不支持新版的SDL,这问题让我搞了半天,还有videolan.org的源不是稳定版本,会出现“implicit declaration of function ''strdup''”错误。
11.Configure
./configure --enable-memalign-hack --enable-pthreads –-enable-libxvid --enable-libx264 --enable-gpl –-disable-debug –-prefix=/mingw
memalign-hack:是mingw平台配置下必须的
pthreads:libx264要用到的,如果不配置libx264,可以使用w32threads
libxvid, libx264:播放高清mp4需要的解码库
gpl:配置libxvid, libx264 同时需要配置支持GPL
disable-debug:减少编译产生的调试信息
12.configure结果
13.make
这是一个漫长的过程。。。
14.make 结果
生成了ffmpeg.exe ffplay.exe ffprobe.exe
15.测试ffplay,我随便找一个MP4文件来播放,这里选了一个1280p的辛普森一家的片段播放,效果还是很理想的。
具体编译前的准备就不多啰嗦了,还是一样mingw准好就OK了。
下载ffmpeg的源码后,Mingw下编译,出现错误:“libavformat/metadata.c:133:17: error: implicit declaration of function 'strcasecmp'”,
(strcasecmp声明不确切。)
自己实现了两个函数。
- #ifndef __STRCASECMP_H____
- #define __STRCASECMP_H____
- static inline char __hack_charget( char c )
- {
- if(c >= 'a' && c <= 'z')
- c += 'A' - 'a';
- return c;
- }
- static inline int hack_strcasecmp( char const *a, char const *b )
- {
- char ac, bc;
- int r;
- for(;;) {
- ac = __hack_charget(*a++);
- bc = __hack_charget(*b++);
- r = (int)ac - (int)bc;
- if(r)
- return r;
- if(!ac)
- return 0;
- }
- }
- static inline int hack_strncasecmp( char const *a, char const *b, int n )
- {
- char ac, bc;
- int r, i;
- for(i = 0; i < n; ++i) {
- ac = __hack_charget(*a++);
- bc = __hack_charget(*b++);
- r = (int)ac - (int)bc;
- if(r)
- return r;
- if(!ac)
- return 0;
- }
- return 0;
- }
- #define strcasecmp hack_strcasecmp
- #define strncasecmp hack_strncasecmp
- #endif
存储成strcasecmp.h于mingw include目录下。
将这个文件include进avstring.h和metadata.h中,问题解决。
在编译最新版本ffmpeg时遇到上述问题。
通过网上搜索发现解决该问题的方法。
参考
http://comments.gmane.org/gmane.comp.video.ffmpeg.libav.user/10325
- Re: makefile problem on win32 MinGW
- Thomas Sharpless <tksharpless <at> ...> writes:
- > ./configure: line 4652: git: command not found
- >
- > The very last part of the configure script fails but
- > it looks to me like all it is trying to do is make
- > sure the source code is totally current.
- It looks if the repository you are using is
- current, you don't have to worry about the warnings.
- > But then, horrors!
- > $ makecommon.mak:139: *** missing separator. Stop.
- This indicates that your checkout is broken, use:
- $ git config --global core.autocrlf false
- and checkout again.
- Carl Eugen
解决方法
运行
git config --global core.autocrlf false
#然后运行下面的命令删除ffmpeg代码,并重新获取一下即可
git rm --cached -r .
git reset --hard