ffmpeg is an important component of many open-source projects, such as MPlayer and VLC. I am compiling ffmpeg so that I can use it to transcode multimedia files. This guide shows how to use MinGW to compile ffmpeg either statically or dynamically. Installation of MinGW is explained in this post. The example commands below are meant to be entered into an MSYS window (not Command Prompt). I downloaded the FFmpeg source and unpacked it.
Preparing External Libraries for FFMpeg
FFMpeg can be linked with external libraries to add features to FFmpeg. I am adding most features to FFMpeg by compiling additional libraries. Most of these libraries are also covered in my posts on compiling MPlayer and Mencoder.
- Compression Libraries: Zlib and bzLib
Get the zlib source (zlib127.zip), unzip and compile it:unzip zlib127.zip
cd zlib-1.2.7/
make -f win32/Makefile.gcc
cp -iv zlib1.dll /mingw/bin
cp -iv zconf.h zlib.h /mingw/include
cp -iv libz.a /mingw/lib
cp -iv libz.dll.a /mingw/libOptionally, get bzip2 source from bzip.org and compile it like this:
tar xzvf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6
make
cp bzlib.h /mingw/include/
cp libbz2.a /mingw/lib - libgsm
Download gsm-1.0.13.tar.bz2, unpack and compile it as follows:tar xzvf gsm-1.0.13.tar.bz2
cd gsm-1.0-pl13/
makeJust ignore the compile errors with fchmod and fchown when trying to build applications. Copy the header and static library to /mingw:
mkdir /mingw/include/gsm
cp inc/gsm.h /mingw/include/gsm
cp lib/libgsm.a /mingw/lib - LAME is an excellent MP3 encoder. LAME makes use of the nasm assembler if available. So download yasm and save it as /mingw/bin/nasm.exe. Then, download the LAME source from lame.sf.net and compile it like this.
./configure --prefix=/mingw --enable-expopt=full
make
make install - OGG, Vorbis, Speex and Theora
The source code for these libraries can be obtained from xiph.org. Compile them each like this:
./configure --prefix=/mingw
make
make installAs for speex, use the version 1.2rc1 or later.
- FAAC
FAAC is an MPEG-4 AAC audio encoder. Get the source (faac-1.28.tar.gz) and unpack the package. Then, edit Makefile.am:
SUBDIRS = include libfaac
Also, edit the line beginning with AC_OUTPUT in the file configure.in:
AC_OUTPUT(libfaac/Makefile include/Makefile Makefile)
Then, compile FAAC like this
sh bootstrap
./configure --prefix=/mingw
make
make install - XviD is a popular video encoder used to create movie files distributed in p2p networks. Xvid can use yasm assembler to build optimized binaries if found (
/mingw/bin/yasm.exe
). Get the XviD source from xvid.org and compile as follows:tar xzvf xvidcore-1.3.2.tar.gz
cd xvidcore/build/generic
./configure --prefix=/mingw
make
make installIf you encounter "unrecognized command-line option
-mno-cygwin
" errors, open the file platform.inc in a text editor and delete "-mno-cygwin". Then, runmake
andmake install
again.Copy the import library for xvidcore.dll:
cp -iv \=build/xvidcore.dll.a /mingw/lib/libxvidcore.a
- x264 is yet another good video encoder. x264 requires YASM to build an optimized executable. So get yasm and save it as /mingw/bin/yasm.exe. Then, compile x264 like this:
configure --prefix=/mingw --enable-win32thread --extra-cflags="-DX264_VERSION=20100422"
make
make install
cp -iv x264.h x264_config.h /mingw/include
cp -iv libx264.a /mingw/lib
cp -iv x264.pc /mingw/lib/pkgconfig - Compile librtmp as shown in this post.
Building FFMpeg Statically
You need pr.exe from MYS coreutils package (coreutils-5.97-3-msys-1.0.13-ext.tar.lzma). I configured ffmpeg with the following command:
CPPFLAGS='-DHAVE_INT32_T' ./configure --prefix=/mingw --enable-gpl --enable-nonfree --enable-postproc --enable-avfilter --enable-w32threads --enable-runtime-cpudetect --enable-memalign-hack --enable-bzlib --enable-libfaac --enable-libgsm --enable-libmp3lame --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-zlib --disable-debug
-DHAVE_INT32_T is used to allow static FAAC to be linked. Then, I began compilation and installation:
make
make install
Compiling FFmpeg Dynamically
Some Windows applications, such as Audacity, uses FFmpeg libraries when available. If you want to compile FFmpeg dynamically, append --enable-shared --disable-static
to the ./configure
command for FFmpeg.
./configure --prefix=/mingw --enable-gpl --enable-nonfree --enable-postproc --enable-avfilter --enable-w32threads --enable-runtime-cpudetect --enable-memalign-hack --enable-bzlib --enable-libfaac --enable-libgsm --enable-libmp3lame --enable-librtmp --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-zlib --enable-shared --disable-static --disable-debug
If you get "int32_t" error when compiling libfaac.o, open /mingw/include/faac.h
and insert the following line:
#include <stdint.h>
When I built mplayer with --enable-static
and --disable-ffmpeg_a
options to force linking with shared ffmpeg libraries, I had to rename ffmpeg libraries so their names end in *.a.
cd /mingw/lib
cp -iv libavcodec.dll.a libavcodec.a
cp -iv libavformat.dll.a libavformat.a
cp -iv libavutil.dll.a libavutil.a
cp -iv libpostproc.dll.a libpostproc.a
cp -iv libswscale.dll.a libswscale.a
That was can't compile faac
ReplyDeleteSame problem.
ReplyDeleteC:/MinGW/MSYS/home/z/faac-1.28/common/mp4v2/atom_standard.cpp:(.text+0xac): unde
fined reference to `ntohl@4'
../common/mp4v2/libmp4v2.a(atom_standard.o):C:/MinGW/MSYS/home/z/faac-1.28/commo
n/mp4v2/atom_standard.cpp:(.text+0xc5): more undefined references to `ntohl@4' f
ollow
collect2: ld returned 1 exit status
make[2]: *** [faac.exe] Error 1
make[2]: Leaving directory `/home/z/faac-1.28/frontend'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/z/faac-1.28'
make: *** [all] Error 2
Added the following to ./configure options and it worked.
ReplyDelete--with-mp4v2=no
Not able to compile ZLIB ... I followed your instructions and I get error: sys/types.h No such file or folder.
ReplyDeleteWhat can be the reason for this and more importantly, how can I resolve this error? Please advise.
[mailme_friends[at]yahoo.com]
-- Rutu.
I'm cross compiling with MinGW on Ubuntu Hardy. A couple of the above tips helped me; thanks much.
ReplyDeleteBut now several libsamplerate modules fail to compile with 'fftw3.h not found'; however fftw3-3 is installed and there is a /usr/include/fftw3.h. Any insights?
--Tom
er, should have said "trying to build HandBrake by cross-compiling...".
ReplyDelete& that at gmail.com I'm tksharpless
Thanks for the great walkthrough. I've been looking all around the net for this exact thing, there are just too many libs and pieces needed to bake ffmpeg for the uninitiated to figure out.
ReplyDeleteA small remark though: current versions of ffmpeg no longer recognize "--enable-avfilter-lavf" and "--enable-libfaad". The latter isn't needed anymore due to ffmpeg's own aac decoder providing all the functionality.
So you could drop those parameters and remove the instructions for libfaad.
Once again, thanks for sharing your knowledge :)
-Chris
There is no "pr.exe" in coreutils-5.97
ReplyDelete