Ffmpeg is a very fast video and audio converter, and even supports live streaming. Here at SWAT we use ffmpeg as an adapter for a multimedia component. This article explains how to compile ffmpeg, which includes the libavcodec library, as well as other known and fast libraries to use together with this powerful tool.
At the end, I will provide some examples of how to play with ffmpeg to generate files for multiple purposes, including files to play on car DVD player and even on your iPod/iPhone.
Let’s start by installing everything.
Installation
This installation guide is very close to being Operating System agnostic.
I applied the same commands on a Mac OS X, Ubuntu, Debian and SuSE installation. Of course there are a little tweaks that were necessary, like updating wget and yasm libraries to a newer version on Mac OS X (if you are interested – I fixed them by installing packages through MacPorts), but that is out of scope of this article.
Before we install ffmpeg, we need to install the related libraries first. There’s a bunch of libraries that includes support to AAC, XVid, FLV files, for example.
I want to keep things available for all users, but I still need to keep them organized. So, I’ll place all sources on my ~/src folder
cd ~
mkdir src
cd src
The first one that we’ll install is libfaac and libfaad, which includes support to MPEG-4 and MPEG-2. The package libfaac is responsible for supporting encoding; libfaad is responsible for decoding.
wget http://downloads.sourceforge.net/faac/faad2-2.7.tar.gz
tar -xvf faad2-2.7.tar.gz faad2-2.7
cd faad2-2.7
autoreconf -vif
echo ‘./configure --prefix=/usr --enable-shared’ >> ./myconfigure.sh
./myconfigure.sh && make && sudo make install
Ffmpeg can take advantage of shared libraries, so it’s way better to use shared libraries. I created a ./myconfigure.sh script, so you can easily remember what was your configure command and enable/disable options later if needed.
Now it’s time for faac:
wget http://downloads.sourceforge.net/faac/faac-1.28.tar.gz
tar -xvf faac-1.28.tar.gz faac-1.28
cd faac-1.28
autoreconf -vif
echo ‘./configure --prefix=/usr --enable-shared’ >> ./myconfigure.sh
./myconfigure.sh && make && sudo make install
Many people would love to have MP3 support, enabling them to extract audio files from video, for example. Others would love to see converted videos on their own DVD player. To support both, we need to install liblame and libxvid.
The lame library actually has an undocumented buffer size, which prohibits ffmpeg from working with it on recent versions. This issue has been reported a thousand times, but until now there’s no patch to fix it. In order to get it working with ffmpeg, you have to pick an old version of lame.
wget http://downloads.sourceforge.net/project/lame/lame/3.97/lame-3.97.tar.gz
tar -xvf lame-3.97.tar.gz lame-3.97
cd lame-3.97
echo ‘./configure --prefix=/usr --enable-shared’ >> ./myconfigure.sh
./myconfigure.sh && make && sudo make install
And libxvid:
wget http://downloads.xvid.org/downloads/xvidcore-1.2.2.tar.gz
tar -xvf xvidcore-1.2.2.tar.gz xvidcore-1.2.2
cd xvidcore-1.2.2
echo ‘./configure --prefix=/usr --enable-shared’ >> ./myconfigure.sh
./myconfigure.sh && make && sudo make install
Before compiling ffmpeg, I’d suggest as a must-have the x264 library. Why? Basically x264 is a free library to encode video streams into the H.264/MPEG-4 AVC format. What does it mean? It means you want encoding support for the standard video compression, published in May 2003. It’s mostly popular with Blu-ray Disc, HD DVD and videos from iTunes.
To compile x264 with full speed, it is required to have yasm library in version >= 0.6.0.
git clone git://git.videolan.org/x264.git
cd x264
echo ‘./configure --prefix=/usr --enable-shared’ >> ./myconfigure.sh
./myconfigure.sh && make && sudo make install
Finally… after 30 minutes., we’ll install ffmpeg!
svn co svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
cd ffmpeg
echo ‘./configure --prefix=/usr --enable-shared --enable-pthreads --enable-gpl --enable-nonfree --enable-postproc --enable-libx264 --enable-libmp3lame --enable-libfaac --enable-libfaad --enable-libxvid’ >> ./myconfigure.sh
./myconfigure.sh && make && sudo make install
Usage
Now it’s time to play with your videos.
To construct a ffmpeg command, you need to know the basic format:
ffmpeg [input options] –i [input file] [output options] [output file]
As our first example, I’ll explain how to pick video files from Youtube and convert them to play on your car DVD player!
Grabbing videos from Youtube
Natively ffmpeg cannot grab Youtube files. However, there’s a Firefox extension that does that very well. Easy Youtube Video Downloader does this task for you. Install the addon, restart Firefox, enter the video, click on download and pick the flv file. Then, just proceed to convert the FLV to XVID file.
Converting video into XVID video
To be able to play on your car DVD player, the video must be XVID compatible.
Since we installed the libxvid, which is responsible to encode/decode XVID files, we’re in heaven. All we need to do is execute in command line:
ffmpeg -i /path/to/downloaded_file.flv -vcodec libxvid -sameq -acodec copy -aspect 16:9 /path/to/encoded_file.avi
Please notice that the –aspect option can heavily influence the resulting encoded video. Options are 4:3 and 16:9. Now, copy the encoded file to your pen-drive and go to listen in your car
Converting video to iPod video
Ffmpeg + x264 integration is awesome. X264 has what we call “presets”, which contains optimized configuration of x264 to some output. One of the presets is ipod320. So, considering we’re converting the video to an iPod 320×240, I’ll use this size as video size.
ffmpeg -i /path/to/downloaded_file.flv -acodec libfaac -ab 128k -s 320x240 -vcodec libx264 -vpre hq -vpre ipod320 -b 768k -bt 768k -aspect 4:3 -threads 0 -f ipod /path/to/encoded_file.mp4
For iPhone, I’d suggest to pick 640×480 and change the preset to ipod640.
Take a look at the final command:
ffmpeg -i /path/to/downloaded_file.flv -acodec libfaac -ab 128k -s 640x480 -vcodec libx264 -vpre hq -vpre ipod640 -b 768k -bt 768k -aspect 4:3 -threads 0 -f ipod /path/to/encoded_file.mp4
Finally, move the video to your library and happy watching!



