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

8 comments:

  1. Same problem.

    C:/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

    ReplyDelete
  2. Added the following to ./configure options and it worked.
    --with-mp4v2=no

    ReplyDelete
  3. Not able to compile ZLIB ... I followed your instructions and I get error: sys/types.h No such file or folder.

    What can be the reason for this and more importantly, how can I resolve this error? Please advise.

    [mailme_friends[at]yahoo.com]

    -- Rutu.

    ReplyDelete
  4. I'm cross compiling with MinGW on Ubuntu Hardy. A couple of the above tips helped me; thanks much.

    But 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

    ReplyDelete
  5. er, should have said "trying to build HandBrake by cross-compiling...".

    & that at gmail.com I'm tksharpless

    ReplyDelete
  6. 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.

    A 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

    ReplyDelete
  7. There is no "pr.exe" in coreutils-5.97

    ReplyDelete

About This Blog

KBlog logo This blog is about current events and issues concerning general population. Thanks for visiting the blog and posting your comments.

© Contents by KBlog

© Blogger template by Emporium Digital 2008

Followers

Total Pageviews

icon
Powered By Blogger