FFmpeg will not compile under Visual C++ — and it has too many dependencies on the GCC compiler to make a port viable. However, if you want to use the FFmpeg libraries in your own applications, you can still compile those applications using Visual C++. An important restriction to this is that you have to use the dynamically linked versions of the FFmpeg libraries (i.e. the DLLs), and you have to make sure that Visual-C++-compatible import libraries are created during the FFmpeg build process.
This description of how to use the FFmpeg libraries with Visual C++ is based on Visual C++ 2005 Express Edition Beta 2. If you have a different version, you might have to modify the procedures slightly.
Here are the step-by-step instructions for building the FFmpeg libraries so they can be used with Visual C++:
- Install Visual C++ (if you have not done so already).
- Install MinGW and MSYS as described above.
- Add a call to `vcvars32.bat' (which sets up the environment variables for the Visual C++ tools) as the first line of `msys.bat'. The standard location for `vcvars32.bat' is `C:\Program Files\Microsoft Visual Studio 8\VC\bin\vcvars32.bat', and the standard location for `msys.bat' is `C:\msys\1.0\msys.bat'. If this corresponds to your setup, add the following line as the first line of `msys.bat':
call "C:\Program Files\Microsoft Visual Studio 8\VC\bin\vcvars32.bat" - Start the MSYS shell (file `msys.bat') and type
link.exe. If you get a help message with the command line options oflink.exe, this means your environment variables are set up correctly, the Microsoft linker is on the path and will be used by FFmpeg to create Visual-C++-compatible import libraries. - Extract the current version of FFmpeg and change to the FFmpeg directory.
- Type the command
./configure --enable-shared --disable-static --enable-memalign-hackto configure and, if that did not produce any errors, typemaketo build FFmpeg. - The subdirectories `libavformat', `libavcodec', and `libavutil' should now contain the files `avformat.dll', `avformat.lib', `avcodec.dll', `avcodec.lib', `avutil.dll', and `avutil.lib', respectively. Copy the three DLLs to your System32 directory (typically `C:\Windows\System32').
And here is how to use these libraries with Visual C++:
- Create a new console application (“File / New / Project”) and then select “Win32 Console Application”. On the appropriate page of the Application Wizard, uncheck the “Precompiled headers” option.
- Write the source code for your application, or, for testing, just copy the code from an existing sample application into the source file that Visual C++ has already created for you. (Note that your source filehas to have a
.cppextension; otherwise, Visual C++ will not compile the FFmpeg headers correctly because in C mode, it does not recognize theinlinekeyword.) For example, you can copy `output_example.c' from the FFmpeg distribution (but you will have to make minor modifications so the code will compile under C++, see below). - Open the “Project / Properties” dialog box. In the “Configuration” combo box, select “All Configurations” so that the changes you make will affect both debug and release builds. In the tree view on the left hand side, select “C/C++ / General”, then edit the “Additional Include Directories” setting to contain the complete paths to the `libavformat', `libavcodec', and `libavutil' subdirectories of your FFmpeg directory. Note that the directories have to be separated using semicolons. Now select “Linker / General” from the tree view and edit the “Additional Library Directories” setting to contain the same three directories.
- Still in the “Project / Properties” dialog box, select “Linker / Input” from the tree view, then add the files `avformat.lib', `avcodec.lib', and `avutil.lib' to the end of the “Additional Dependencies”. Note that the names of the libraries have to be separated using spaces.
- Now, select “C/C++ / Code Generation” from the tree view. Select “Debug” in the “Configuration” combo box. Make sure that “Runtime Library” is set to “Multi-threaded Debug DLL”. Then, select “Release” in the “Configuration” combo box and make sure that “Runtime Library” is set to “Multi-threaded DLL”.
- Click “OK” to close the “Project / Properties” dialog box and build the application. Hopefully, it should compile and run cleanly. If you used `output_example.c' as your sample application, you will get a few compiler errors, but they are easy to fix. The first type of error occurs because Visual C++ does not allow an
intto be converted to anenumwithout a cast. To solve the problem, insert the required casts (this error occurs once for aCodecIDand once for aCodecType). The second type of error occurs because C++ requires the return value ofmallocto be cast to the exact type of the pointer it is being assigned to. Visual C++ will complain that, for example,(void *)is being assigned to(uint8_t *)without an explicit cast. So insert an explicit cast in these places to silence the compiler. The third type of error occurs because thesnprintflibrary function is called_snprintfunder Visual C++. So just add an underscore to fix the problem. With these changes, `output_example.c' should compile under Visual C++, and the resulting executable should produce valid video files.