Wednesday, September 5, 2012

To Compile ffmpeg with MinGW

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.

  1. 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/lib

    Optionally, 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
  2. 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/
    make

    Just 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
  3. 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
  4. 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 install

    As for speex, use the version 1.2rc1 or later.

  5. 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
  6. 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 install

    If you encounter "unrecognized command-line option -mno-cygwin" errors, open the file platform.inc in a text editor and delete "-mno-cygwin". Then, run make and make install again.

    Copy the import library for xvidcore.dll:

    cp -iv \=build/xvidcore.dll.a /mingw/lib/libxvidcore.a
  7. 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
  8. 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

About This Blog

KBlog logo This blog seeks to provide useful information to people, based on the author's knowledge and experience. Thanks for visiting the blog and posting your comments.

© Contents by KBlog

© Blogger template by Emporium Digital 2008

Follow by Email

Total Pageviews