Compiling ffmpeg on Win32

ffmpeg is a powerful command-line interpreter for transcoding video on Windows. It is used by many other video programs "under the bonnet" - on my computer - DVD SLideshow GUI; SUPER@; MediaCoder; Pazera FLV2AVI; and so on. I have used it to compress to the streamable Flash Movie [FLV] format for the videos on this site.

----------------------------------------------------------------

Prologue

Reasons for looking at this:

  1. I need to practice compiling Open Source applications
  2. I use it to compress University videos for the Flash Media Streaming server.
  3. It is the "Swiss Army knife" of digital video, and cannot be easily and trustworthily obtained by any other means.
  4. I will use it to get Blender output into the correct Present3D/OSG video format(s).
  5. It's friendly towards, and understands AviSynth/VirtualDub i/o.

ffmpeg is powerful because it is continuously under development adding new video format capabilities as they emerge. However, the ffmpeg project itself was started a while back, and uses Open Source [hereafter 'OS'] tools "Minimum GNU for Windows" aka MinGW. and "Minimum Unix System" aka Msys for its compilation. Though the source may originally have been written and compiled in *nix environments, the resulting GNU executables (exe/dll/...and the other one..) run on Windows. - Aye, if they have been written x-platform.

The compilation tools, like ffmpeg, have also moved on since the project's inception, and the contemporary contents of current easy-install suite are not neccessarily compatible with the earlier incarnations which ffmpeg developers continue to use. As a consequence it is neccessary to use older versions of parts of the tools collection, as recommended here: http://ffmpeg.arrozcru.org/wiki/index.php - one of the lead developers of ffmpeg, if I understand correctly.

My page summarises the arrozcru procedures. I installed MinGW first then Msys I don't think which comes first is critical so long as the correct versions of the tools are there.

To unpack, on Windows, the unusual Zip format encountered you will need the OS 7-Zip http://prdownloads.sourceforge.net/sevenzip/7z457.msi?download can handle the compression formats. You'll need it or WinRar.

Building the compiler environment

DOWNLOAD THE FILES..

For MinGW
( Precede each of these with http://prdownloads.sourceforge.net/mingw/ )
binutils-2.18.50-20080109-2.tar.gz?download
mingwrt-3.15.1-mingw32-dll.tar.gz?download
mingwrt-3.15.1-mingw32-dev.tar.gz?download
w32api-3.12-mingw32-dev.tar.gz?download

For MSYS
(prefix following with http://prdownloads.sourceforge.net/mingw/ )
MSYS-1.0.11-2004.04.30-1.exe?download
MSYS-1.0.11-20080821- dll.tar.gz?download
bash-3.1-MSYS-1.0.11- snapshot.tar.bz2?download
coreutils-5.97-MSYS-1.0.11- snapshot.tar.bz2?download
make-3.81-MSYS-1.0.11-2.tar.bz2 (See: Make installation note, following )
gcc-core-4.2.1-sjlj-2.tar.gz?download
gcc-g%2B%2B-4.2.1-sjlj-2.tar.gz?download

Make installation note: As with all the others it will ask if you want to overwrite the existing make. But the preceding version of make is also handy when it comes to later, general programming, so the solution is to rename the preceding one with its version number before trying to install this one. Rename the old C:\msys\bin\make.exe to C:\msys\bin\make-3.79.exe then go ahead and unpack the new one. In every other case in this article, selecting "Overwrite All" is good.

Deal with MinGW first

, since Msys setup will ask where you put MinGW. Easiest if you have an answer!

Create a directory ( keep it simple, c:\MinGW ) BZ2 is a double-compression, so uncompressing yields an intermediate compressed TAR file which can go anywhere convenient (Desktop?) Then, re-use 7-zip to uncompress the TAR too - this time selecting the correct destination folder - c:\MinGW. You can delete the tar once that's done.

The unpacking creates standard Unix subdirectory trees below MinGW
All of the above packs follow this conventional directory structure (similar to Windows installers always suggesting "/Program Files"), so (with the MinGW set of files) always choose MinGW as the destination, that way files in each pack destined for the /bin/ folder will be ADDED to the common /bin/ folder, and so on. Order of above unpacking is therefore important too - "newer" versions of the same file in a different pack will overwrite an "earlier" pack's version.

Same procedure for Msys, except the top folder you point to is now Msys. Unlike the rcommendation in Arrozcru, I unpacked to a separate folder C:\Msys because at first I was reading a different set of instructions. In the post-unpacking procedure (coming next), you will have to substitute the particular paths you used. To avoid confusion I will describe using the above paths. (Arrozcru, however, put MinGW inside the Msys folder - whatever!)

Post-unpacking


So now I have two folders off the C: drive We need to tell Windows and Msys where the MinGW compiler is at:

Telling Windows
is done by adding C:\MinGW\bin to the end of the PATH Environment Variable.

Note, it's easy, but making a mistake can stop your system functioning - you mustn't delete any PATH stuff that's already there. Just ADD a semi-colon at the end, and type after it C:\MinGW\bin (or whatever your path is).

Telling Msys
Because I sometimes use Microsoft Visual C++ Express (a lovely freebie environment) for programming, I added the line
call "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
at the top of C:\msys\msys.bat - just after the Echo off command.
Also, check that the file C:\msys\etc\fstab contains the correct path, mine says: c:/mingw /mingw i.e. The red bit is telling Msys the path to MinGW so your c:\... may be different.

Arrozcru suggests updating

..but my connection wasn't working so I skipped them for now and set to compiling ffmpeg.

DOWNLOADING ffmpeg SOURCE

This involves SVN downloading. I use TortoiseSVN, and create a source repository of ffmpeg


My first attempts made a mess so my directory structure is more complex than neccessary. I thought the ticked ffmpeg should have landed at the same level as the OpenSceneGraph source... but maybe not?
Arrozcru explains in detail how to use TortoiseSVN neatly.

TIP: In University networks if http:// seems to fail, use https:// - a secure connection. This fixed it for me. As for directory structure - so long as I know the path to the fresh source (the ticked folder), things should be OK. And notice that although, in my case, the path to the ffmpeg source is the messy, I-Thou still need to know:
W:\repository\ffmpeg\ffmpeg\ the equivalent in the Msys Unix environment is
/w/repository/ffmpeg/ffmpeg/ You will need to know yours. (It's called the "Absolute path" to the source.)
Somehow that's beginning to sound philosphical... but it's not. Oo-o-o, No!

COMPILING ffmpeg

Arrozcru talks about making some configure decisions. Forget that for now - as he also suggests. In any case, the default settings render the fullest version the source can muster, any further decisions will tend to be to remove stuff you don't want, or, experimental alternative codecs which will tend to break on the average system.

The two "must have" configure options he mentions are indeed must haves. And also, I have picked up elsewhere (Robert & OSG :) that you should build outside of your fresh source tree, so I diverge very slightly from Arrozcru recommendations here (..and of course made a (fixable) directory mess!)
Fire up Msys. And it opens in your /Home directory.
This illustration is of explorer - not Msys - to give you orientation.



Important: Wherever you run "configure" from (a sort of batch file that comes with the source), that directory will be where the compiler puts its intermediate files during the compilation process - and there are lots - all the folders at the same level as 'John' were added by the configure process. It started with just John in there.
So, in home, at the prompt, (remember we're in Msys - unix now) I type
/w/repository/ffmpeg/ffmpeg/configure --enable-memalign-hack --extra-cflags="-fno-common" --prefix=John

Where.. /w/repository/ffmpeg/ffmpeg/configure

is the absolute (full) path to the batch file

--enable-memalign-hack
--extra-cflags="-fno-common"

are compiler workaround must haves for Windows ( gcc is a multi-platform compiler, and sometimes there's no other solution.)

--prefix=John

where I want the "make install" process to ultimately leave my executable(s).

Press return and wait. If you get errors, See page: Not compiling... for my attempts at empathy.

Otherwise you get the default settings printout.. (click to see'em. - Including ffmpeg capabilities.)
 

Installing ffmpeg

Lastly, you type make install which copies all the rendered, final files to,in my case, the 'John' directory.

At this stage, for me, there was one problem - it created a directory called '~' (I have corrected the mistake in the printout), Because, for prefix, tyring to be precise, I said, ~/John where I should have only said, John. Consequently, it took a while to find my installed files. The process created a directory called '~' - which I didn't notice for about half an hour, and a subdirectory 'John' therein. Duh! Windows Search (unusually!) found it. ffmpeg.exe ended up in C:\msys\home\~\John\bin I just copied the stuff out to where I originally intended, and deleted the spurious.

Arrozcru continues: "include and lib contain the include and lib files to be used when writing a program that uses FFmpeg." (i.e. The library and headers for statically linking-in ffmpeg capabilities into your home-rolled programs. It'll be a while till I'm at that stage, if ever.)

If you just want to run FFmpeg, all you need is /bin/ffmpeg.exe - the standalone command line interpreter.
----------------------------------------------------------------

A.O.R.B

There are a couple of things still to do. Compile as Dynamic Link Libraries (DLLs) - slightly different procedure. DLLs are "Windows plugins", which other programs can call on-the-fly, if they know how.

This is an interesting page - mixing PHP and ffmpeg to passthru videos??! "Roll-your-own YouTube" - worth a look for something generating 3D stereo output in browsers. http://www.gadberry.com/aaron/2005/12/16/how-to-convert-audio-to-another-format- from-a-form-on-a-web-page/
There's also this Creating MyTube with Flex & PHP
...and of course the AviSynth/VirtualDub combo can generate side by side videos. Though I can't take stereo movies yet. I hope in a few weeks to generate stereo blender movies, and use ffmpeg et al to make them compatible with Present3D/OSG. ----------------------------------------------------------------