Converting Video Formats with FFmpeg
Today's affordable digital video cameras have placed the power of digital recording within most people's reach. Unfortunately, this has been accompanied with a corresponding increase in the variety of file formats and codecs available. Some of these formats are more efficient than others, and some are less encumbered by proprietary licensing restrictions. So, having the ability to convert from one format to another is a great help, as you can decide what format you are comfortable with and use that one instead of being restricted to a specific file format.
FFmpeg is a simple and straightforward application that allows Linux users to convert video files easily between a variety of different formats. In this article, I walk you through installing FFmpeg and provide a few instructive examples to demonstrate the range of applications for which it can be used.
FFmpeg is an open-source audio and video converter that supports most industry-standard codecs and can convert from one file format to another quickly and easily. It also lets you capture video and audio from a live source and process it.
The source code for FFmpeg is available for download from the project Web site (ffmpeg.sourceforge.net/index.php) and at the time of this writing, the latest version available at the site is 0.4.9-pre1.
Once you download the file, extract it using the following command:
tar -zxf ffmpeg-0.4.9-pre1.tar.gz
This creates a new directory containing the source code for FFmpeg. To install it with the default configuration options, run ./configure from within the FFmpeg source directory. Once the configuration script finishes, compile it by issuing make. Once the compile finishes without any errors, you can install FFmpeg by running make install as root.
On the other hand, if you like to have control over what is installed and prefer customizing software installs, you can pass some command-line parameters to the configure script. To see all the options available for the installer, run the following command:
./configure --help
This command gives you multiple screens of the various settings that can be modified, and you can choose any options you like. The on-screen display does a decent job of explaining what each option does, so I will not go into a lot of detail on this.
I suggest that you enable the following options, but this is not a requirement—feel free to experiment:
--enable-mp3lame: highly recommended—you won't be able to encode MP3s without this. Needs lame to be installed already.
--enable-a52: enables GPLed A52 support, needed for decoding some VOB files.
--enable-gpl: required for the previous component; otherwise, not needed.
As I didn't have lame installed on my system, I ran the following command to configure FFmpeg:
./configure --enable-a52 --enable-gpl
Once the configuration is complete, read through the output to make sure no errors were generated. Then, run make, and go have a drink or something as this might take a little while. Once the system finishes compiling FFmpeg, run make install as root to install FFmpeg, and you are done with the installation.
Now that you have successfully installed FFmpeg, you can start experimenting with it. The first thing you have to do is choose a video file with which to experiment. As this is your first time with FFmpeg, making a backup copy of this file is highly recommended. You don't want to be responsible for ruining the only copy of a rare video.
This input file most probably has been encoded using a particular codec, but because FFmpeg supports most of the popular formats, we don't need to worry a lot about that. Formats supported by FFmpeg include MPEG, MPEG-4 (Divx), ASF, AVI, Real Audio/Video and Quicktime. To see a list of all the codecs/formats supported by FFmpeg, run the following command:
ffmpeg --formats
A detailed list of supported file formats is also available at the FFmpeg site.
FFmpeg supports a large list of command-line parameters that control various settings in FFmpeg. To get a listing of the various options available, run the following command:
ffmpeg --help
Don't let the multipage listing scare you from using FFmpeg, the basic usage is actually very simple. To convert a file with the default settings, run the following command:
ffmpeg -i InputFile OutputFile
The -i option tells FFmpeg that the filename immediately after it is the name of the file to be used as input. If this option is omitted, FFmpeg attempts to overwrite that file when it tries to create the output file. FFmpeg uses the extension of the output file to try to determine the format and codec to use, though this can be overridden using command-line parameters (more on this later).
The default settings create an output file that has radio-quality sound (64kbps bitrate) and very bad video quality (200kbps bitrate). Fortunately, these settings can be changed for each encoding, which allows you to choose the quality of each file depending on the need.
To change the audio bitrate, add -ab bitrate to the command used earlier, where bitrate is the bitrate you want to use. See www.mp3-tech.org/tests/gb for information on the sound quality the various bitrates represent. I prefer to encode files with a bitrate between 128–192kbps depending my needs, but you can put in a higher value if you so desire. Keep in mind, however, that the higher the bitrate you use, the larger the output file size will be. Also keep in mind that if your source file is encoded in a low bitrate, increasing the bitrate won't accomplish much other than increasing the output file size.
Now, getting a CD-quality audio track for the video doesn't really make sense if the video looks like it was taken using a five-year-old Webcam having a bad day. Thankfully, this problem also is easily solved by adding another parameter to the command line.
To change the video bitrate, add the -b bitrate option to the command line. The bitrate here can be any numeric value you like, and I have seen bitrates all the way up to 23,000 (DVD Rips). Although the quality of video encoded with a 23,000kbps bitrate is amazing, the resulting file size of that encoding is also very amazing (a 90-minute video is about 4GB). In my experience, most videos look pretty decent at bitrates between 1,000–1,400, but this is a personal preference, so play with the numbers until you figure out what works for you.
So, to encode a video with a 128kbps audio bitrate and 1,200kbps video stream, we would issue the following command:
ffmpeg -i InputFile.avi -ab 128 -b 1200 OutputFile.mpg
If you are creating a video CD or DVD, FFmpeg makes it even easier by letting you specify a target type. Then, it uses the target type to calculate the format options required automatically. To set a target type, add -target type; type can be vcd, svcd, dvd, dv, pal-vcd or ntsc-svcd on the command line. So, if we were creating a VCD, we would run the following command:
ffmpeg -i InputFile.mpg -target vcd vcd_file.mpg
FFmpeg also has support for encoding audio files. The command to convert audio files is the same as the command to encode video files. To convert a WAV file to a 128kbps MP3 file, issue the following command:
ffmpeg -i Input.wav -ab 128 Output.mp3
Now, the biggest selling point of FFmpeg is that you can customize it to a level that you are comfortable with. So, if all you want to do is convert from one codec to another, and you don't really care about the advanced features, you can stop reading here and still be able to encode/decode videos. On the other hand, if you like to have more control over the encoding, keep reading as we cover more of the advanced options available in FFmpeg.
There are far too many options available in FFmpeg for me to go over each of them here, so I cover some of the ones I found most interesting and leave the rest for you to explore.
There are a times when you will want to encode a video using a particular codec and file format. FFmpeg lets you choose the codec with which you want to encode by adding -vcodec codec to the command line, where codec is the name of the codec you want to use. So if we want to encode using the MPEG-4 codec at 1,200kbps video bitrate and 128kbps audio bitrate, the command looks like this:
ffmpeg -i InputFile.mpg -ab 128 -b 1200 -vcodec mpeg4 OutputFile.avi
Let's say you have recorded a video that has a lot of background noise and undesired commentary, so you decide to remove the audio component of the video completely. To accomplish this, all you have to do is add the -an option to the command line, and FFmpeg automatically removes all audio from the output. Keep in mind that using this option negates any other option that affects the audio stream.
So, in our example, to remove the audio component, we would run the following command:
ffmpeg -i InputFile.mpg -an -b 1200 OutputFile.avi
Let's say you downloaded a news video from the Net that you want to listen to on your iPod on the way to work, but in order to do that, you have to remove the video component from the output file. FFmpeg allows you to remove the video component of the file completely by adding the -vn option to the command line. Using this option negates any other option that affects the video stream.
So, in our example, to remove the video component and save the audio as a 256kbps MP3 file, we would run the following command:
ffmpeg -i InputFile.mpg -vn -ab 256 OutputFile.mp3
Many DVDs have multiple language tracks available, and you can choose in which language you want to watch the video. Having multiple audio tracks is cool if you speak multiple languages and want to be able to watch videos in multiple languages. However, if you don't speak multiple languages, the extra audio tracks are useless and are taking up disk space.
FFmpeg lets you choose which streams you want to keep and ignore the rest. The command-line parameter that allows you to map streams is called -map. So, if in our test file, stream 0 is the video stream, stream 1 is the Spanish audio stream and stream 2 is the English audio stream, and we want to keep the English audio in the output file, we would issue the following command:
ffmpeg -i InputFile.mpg -map 0:0 -map 2:1 -b 1200 OutputFile.avi
In my experience, stream 0 in most video files is usually the video stream, and the remaining streams are the audio streams available with the video.
Suramya Tomar is a Linux system administrator who also likes to program. Visit www.suramya.com for more information on his background.